|
| 1 | +//===-- GoLanguageRuntime.cpp --------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "GoLanguageRuntime.h" |
| 11 | + |
| 12 | +#include "lldb/Breakpoint/BreakpointLocation.h" |
| 13 | +#include "lldb/Core/ConstString.h" |
| 14 | +#include "lldb/Core/Error.h" |
| 15 | +#include "lldb/Core/Log.h" |
| 16 | +#include "lldb/Core/Module.h" |
| 17 | +#include "lldb/Core/PluginManager.h" |
| 18 | +#include "lldb/Core/Scalar.h" |
| 19 | +#include "lldb/Core/ValueObject.h" |
| 20 | +#include "lldb/Core/ValueObjectMemory.h" |
| 21 | +#include "lldb/Symbol/GoASTContext.h" |
| 22 | +#include "lldb/Symbol/Symbol.h" |
| 23 | +#include "lldb/Symbol/TypeList.h" |
| 24 | +#include "lldb/Target/Process.h" |
| 25 | +#include "lldb/Target/RegisterContext.h" |
| 26 | +#include "lldb/Target/SectionLoadList.h" |
| 27 | +#include "lldb/Target/StopInfo.h" |
| 28 | +#include "lldb/Target/Target.h" |
| 29 | +#include "lldb/Target/Thread.h" |
| 30 | +#include "llvm/ADT/Twine.h" |
| 31 | + |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +using namespace lldb; |
| 35 | +using namespace lldb_private; |
| 36 | + |
| 37 | +namespace { |
| 38 | +ValueObjectSP GetChild(ValueObject& obj, const char* name, bool dereference = true) { |
| 39 | + ConstString name_const_str(name); |
| 40 | + ValueObjectSP result = obj.GetChildMemberWithName(name_const_str, true); |
| 41 | + if (dereference && result && result->IsPointerType()) { |
| 42 | + Error err; |
| 43 | + result = result->Dereference(err); |
| 44 | + if (err.Fail()) |
| 45 | + result.reset(); |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +ConstString ReadString(ValueObject& str, Process* process) { |
| 51 | + ConstString result; |
| 52 | + ValueObjectSP data = GetChild(str, "str", false); |
| 53 | + ValueObjectSP len = GetChild(str, "len"); |
| 54 | + if (len && data) |
| 55 | + { |
| 56 | + Error err; |
| 57 | + lldb::addr_t addr = data->GetPointerValue(); |
| 58 | + if (addr == LLDB_INVALID_ADDRESS) |
| 59 | + return result; |
| 60 | + uint64_t byte_size = len->GetValueAsUnsigned(0); |
| 61 | + char* buf = new char[byte_size + 1]; |
| 62 | + buf[byte_size] = 0; |
| 63 | + size_t bytes_read = process->ReadMemory (addr, |
| 64 | + buf, |
| 65 | + byte_size, |
| 66 | + err); |
| 67 | + if (!(err.Fail() || bytes_read != byte_size)) |
| 68 | + result = ConstString(buf, bytes_read); |
| 69 | + delete[] buf; |
| 70 | + } |
| 71 | + return result; |
| 72 | +} |
| 73 | + |
| 74 | +ConstString |
| 75 | +ReadTypeName(ValueObjectSP type, Process* process) |
| 76 | +{ |
| 77 | + if (ValueObjectSP uncommon = GetChild(*type, "x")) |
| 78 | + { |
| 79 | + ValueObjectSP name = GetChild(*uncommon, "name"); |
| 80 | + ValueObjectSP package = GetChild(*uncommon, "pkgpath"); |
| 81 | + if (name && name->GetPointerValue() != 0 && package && package->GetPointerValue() != 0) |
| 82 | + { |
| 83 | + ConstString package_const_str = ReadString(*package, process); |
| 84 | + ConstString name_const_str = ReadString(*name, process); |
| 85 | + if (package_const_str.GetLength() == 0) |
| 86 | + return name_const_str; |
| 87 | + return ConstString((package_const_str.GetStringRef() + "." + name_const_str.GetStringRef()).str()); |
| 88 | + } |
| 89 | + } |
| 90 | + ValueObjectSP name = GetChild(*type, "_string"); |
| 91 | + if (name) |
| 92 | + return ReadString(*name, process); |
| 93 | + return ConstString(""); |
| 94 | +} |
| 95 | + |
| 96 | +CompilerType |
| 97 | +LookupRuntimeType(ValueObjectSP type, ExecutionContext* exe_ctx, bool* is_direct) |
| 98 | +{ |
| 99 | + uint8_t kind = GetChild(*type, "kind")->GetValueAsUnsigned(0); |
| 100 | + *is_direct = GoASTContext::IsDirectIface(kind); |
| 101 | + if (GoASTContext::IsPointerKind(kind)) |
| 102 | + { |
| 103 | + CompilerType type_ptr = type->GetCompilerType().GetPointerType(); |
| 104 | + Error err; |
| 105 | + ValueObjectSP elem = type->CreateValueObjectFromAddress("elem", type->GetAddressOf() + type->GetByteSize(), *exe_ctx, type_ptr)->Dereference(err); |
| 106 | + if (err.Fail()) |
| 107 | + return CompilerType(); |
| 108 | + bool tmp_direct; |
| 109 | + return LookupRuntimeType(elem, exe_ctx, &tmp_direct).GetPointerType(); |
| 110 | + } |
| 111 | + Target *target = exe_ctx->GetTargetPtr(); |
| 112 | + Process *process = exe_ctx->GetProcessPtr(); |
| 113 | + |
| 114 | + ConstString const_typename = ReadTypeName(type, process); |
| 115 | + if (const_typename.GetLength() == 0) |
| 116 | + return CompilerType(); |
| 117 | + |
| 118 | + SymbolContext sc; |
| 119 | + TypeList type_list; |
| 120 | + uint32_t num_matches = target->GetImages().FindTypes (sc, |
| 121 | + const_typename, |
| 122 | + false, |
| 123 | + 2, |
| 124 | + type_list); |
| 125 | + if (num_matches > 0) { |
| 126 | + return type_list.GetTypeAtIndex(0)->GetFullCompilerType(); |
| 127 | + } |
| 128 | + return CompilerType(); |
| 129 | +} |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | +bool |
| 134 | +GoLanguageRuntime::CouldHaveDynamicValue (ValueObject &in_value) |
| 135 | +{ |
| 136 | + return GoASTContext::IsGoInterface(in_value.GetCompilerType()); |
| 137 | +} |
| 138 | + |
| 139 | +bool |
| 140 | +GoLanguageRuntime::GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, |
| 141 | + TypeAndOrName &class_type_or_name, Address &dynamic_address, |
| 142 | + Value::ValueType &value_type) |
| 143 | +{ |
| 144 | + value_type = Value::eValueTypeScalar; |
| 145 | + class_type_or_name.Clear(); |
| 146 | + if (CouldHaveDynamicValue (in_value)) |
| 147 | + { |
| 148 | + Error err; |
| 149 | + ValueObjectSP iface = in_value.GetStaticValue(); |
| 150 | + ValueObjectSP data_sp = GetChild(*iface, "data", false); |
| 151 | + if (!data_sp) |
| 152 | + return false; |
| 153 | + |
| 154 | + if (ValueObjectSP tab = GetChild(*iface, "tab")) |
| 155 | + iface = tab; |
| 156 | + ValueObjectSP type = GetChild(*iface, "_type"); |
| 157 | + if (!type) |
| 158 | + { |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + bool direct; |
| 163 | + ExecutionContext exe_ctx (in_value.GetExecutionContextRef()); |
| 164 | + CompilerType final_type = LookupRuntimeType(type, &exe_ctx, &direct); |
| 165 | + if (!final_type) |
| 166 | + return false; |
| 167 | + if (direct) |
| 168 | + { |
| 169 | + class_type_or_name.SetCompilerType(final_type); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + // TODO: implement reference types or fix caller to support dynamic types that aren't pointers |
| 174 | + // so we don't have to introduce this extra pointer. |
| 175 | + class_type_or_name.SetCompilerType(final_type.GetPointerType()); |
| 176 | + } |
| 177 | + |
| 178 | + dynamic_address.SetLoadAddress(data_sp->GetPointerValue(), exe_ctx.GetTargetPtr()); |
| 179 | + |
| 180 | + return true; |
| 181 | + } |
| 182 | + return false; |
| 183 | +} |
| 184 | + |
| 185 | +TypeAndOrName |
| 186 | +GoLanguageRuntime::FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value) |
| 187 | +{ |
| 188 | + return type_and_or_name; |
| 189 | +} |
| 190 | + |
| 191 | +//------------------------------------------------------------------ |
| 192 | +// Static Functions |
| 193 | +//------------------------------------------------------------------ |
| 194 | +LanguageRuntime * |
| 195 | +GoLanguageRuntime::CreateInstance (Process *process, lldb::LanguageType language) |
| 196 | +{ |
| 197 | + if (language == eLanguageTypeGo) |
| 198 | + return new GoLanguageRuntime (process); |
| 199 | + else |
| 200 | + return NULL; |
| 201 | +} |
| 202 | + |
| 203 | +void |
| 204 | +GoLanguageRuntime::Initialize() |
| 205 | +{ |
| 206 | + PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 207 | + "Go Language Runtime", |
| 208 | + CreateInstance); |
| 209 | +} |
| 210 | + |
| 211 | +void |
| 212 | +GoLanguageRuntime::Terminate() |
| 213 | +{ |
| 214 | + PluginManager::UnregisterPlugin (CreateInstance); |
| 215 | +} |
| 216 | + |
| 217 | +lldb_private::ConstString |
| 218 | +GoLanguageRuntime::GetPluginNameStatic() |
| 219 | +{ |
| 220 | + static ConstString g_name("golang"); |
| 221 | + return g_name; |
| 222 | +} |
| 223 | + |
| 224 | +//------------------------------------------------------------------ |
| 225 | +// PluginInterface protocol |
| 226 | +//------------------------------------------------------------------ |
| 227 | +lldb_private::ConstString |
| 228 | +GoLanguageRuntime::GetPluginName() |
| 229 | +{ |
| 230 | + return GetPluginNameStatic(); |
| 231 | +} |
| 232 | + |
| 233 | +uint32_t |
| 234 | +GoLanguageRuntime::GetPluginVersion() |
| 235 | +{ |
| 236 | + return 1; |
| 237 | +} |
| 238 | + |
0 commit comments