| 1 | //===-- SymbolFileCTF.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "SymbolFileCTF.h" |
| 10 | |
| 11 | #include "lldb/Core/Module.h" |
| 12 | #include "lldb/Core/PluginManager.h" |
| 13 | #include "lldb/Host/Config.h" |
| 14 | #include "lldb/Symbol/CompileUnit.h" |
| 15 | #include "lldb/Symbol/Function.h" |
| 16 | #include "lldb/Symbol/ObjectFile.h" |
| 17 | #include "lldb/Symbol/Symbol.h" |
| 18 | #include "lldb/Symbol/SymbolContext.h" |
| 19 | #include "lldb/Symbol/Symtab.h" |
| 20 | #include "lldb/Symbol/TypeList.h" |
| 21 | #include "lldb/Symbol/TypeMap.h" |
| 22 | #include "lldb/Symbol/Variable.h" |
| 23 | #include "lldb/Symbol/VariableList.h" |
| 24 | #include "lldb/Utility/DataExtractor.h" |
| 25 | #include "lldb/Utility/LLDBLog.h" |
| 26 | #include "lldb/Utility/Log.h" |
| 27 | #include "lldb/Utility/RegularExpression.h" |
| 28 | #include "lldb/Utility/StreamBuffer.h" |
| 29 | #include "lldb/Utility/StreamString.h" |
| 30 | #include "lldb/Utility/Timer.h" |
| 31 | #include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_ZLIB |
| 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | |
| 34 | #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h" |
| 35 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 36 | |
| 37 | #include <memory> |
| 38 | #include <optional> |
| 39 | |
| 40 | #if LLVM_ENABLE_ZLIB |
| 41 | #include <zlib.h> |
| 42 | #endif |
| 43 | |
| 44 | using namespace llvm; |
| 45 | using namespace lldb; |
| 46 | using namespace lldb_private; |
| 47 | |
| 48 | LLDB_PLUGIN_DEFINE(SymbolFileCTF) |
| 49 | |
| 50 | char SymbolFileCTF::ID; |
| 51 | |
| 52 | SymbolFileCTF::SymbolFileCTF(lldb::ObjectFileSP objfile_sp) |
| 53 | : SymbolFileCommon(std::move(objfile_sp)) {} |
| 54 | |
| 55 | void SymbolFileCTF::Initialize() { |
| 56 | PluginManager::RegisterPlugin(name: GetPluginNameStatic(), |
| 57 | description: GetPluginDescriptionStatic(), create_callback: CreateInstance); |
| 58 | } |
| 59 | |
| 60 | void SymbolFileCTF::Terminate() { |
| 61 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
| 62 | } |
| 63 | |
| 64 | llvm::StringRef SymbolFileCTF::GetPluginDescriptionStatic() { |
| 65 | return "Compact C Type Format Symbol Reader" ; |
| 66 | } |
| 67 | |
| 68 | SymbolFile *SymbolFileCTF::CreateInstance(ObjectFileSP objfile_sp) { |
| 69 | return new SymbolFileCTF(std::move(objfile_sp)); |
| 70 | } |
| 71 | |
| 72 | bool SymbolFileCTF::() { |
| 73 | if (m_header) |
| 74 | return true; |
| 75 | |
| 76 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 77 | |
| 78 | ModuleSP module_sp(m_objfile_sp->GetModule()); |
| 79 | const SectionList *section_list = module_sp->GetSectionList(); |
| 80 | if (!section_list) |
| 81 | return false; |
| 82 | |
| 83 | SectionSP section_sp( |
| 84 | section_list->FindSectionByType(sect_type: lldb::eSectionTypeCTF, check_children: true)); |
| 85 | if (!section_sp) |
| 86 | return false; |
| 87 | |
| 88 | m_objfile_sp->ReadSectionData(section: section_sp.get(), section_data&: m_data); |
| 89 | |
| 90 | if (m_data.GetByteSize() == 0) |
| 91 | return false; |
| 92 | |
| 93 | StreamString module_desc; |
| 94 | GetObjectFile()->GetModule()->GetDescription(s&: module_desc.AsRawOstream(), |
| 95 | level: lldb::eDescriptionLevelBrief); |
| 96 | LLDB_LOG(log, "Parsing Compact C Type format for {0}" , module_desc.GetData()); |
| 97 | |
| 98 | lldb::offset_t offset = 0; |
| 99 | |
| 100 | // Parse CTF header. |
| 101 | constexpr size_t = sizeof(ctf_header_t); |
| 102 | if (!m_data.ValidOffsetForDataOfSize(offset, length: ctf_header_size)) { |
| 103 | LLDB_LOG(log, "CTF parsing failed: insufficient data for CTF header" ); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | m_header.emplace(); |
| 108 | |
| 109 | ctf_header_t & = *m_header; |
| 110 | ctf_header.preamble.magic = m_data.GetU16(offset_ptr: &offset); |
| 111 | ctf_header.preamble.version = m_data.GetU8(offset_ptr: &offset); |
| 112 | ctf_header.preamble.flags = m_data.GetU8(offset_ptr: &offset); |
| 113 | ctf_header.parlabel = m_data.GetU32(offset_ptr: &offset); |
| 114 | ctf_header.parname = m_data.GetU32(offset_ptr: &offset); |
| 115 | ctf_header.lbloff = m_data.GetU32(offset_ptr: &offset); |
| 116 | ctf_header.objtoff = m_data.GetU32(offset_ptr: &offset); |
| 117 | ctf_header.funcoff = m_data.GetU32(offset_ptr: &offset); |
| 118 | ctf_header.typeoff = m_data.GetU32(offset_ptr: &offset); |
| 119 | ctf_header.stroff = m_data.GetU32(offset_ptr: &offset); |
| 120 | ctf_header.strlen = m_data.GetU32(offset_ptr: &offset); |
| 121 | |
| 122 | // Validate the preamble. |
| 123 | if (ctf_header.preamble.magic != g_ctf_magic) { |
| 124 | LLDB_LOG(log, "CTF parsing failed: invalid magic: {0:x}" , |
| 125 | ctf_header.preamble.magic); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | if (ctf_header.preamble.version != g_ctf_version) { |
| 130 | LLDB_LOG(log, "CTF parsing failed: unsupported version: {0}" , |
| 131 | ctf_header.preamble.version); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | LLDB_LOG(log, "Parsed valid CTF preamble: version {0}, flags {1:x}" , |
| 136 | ctf_header.preamble.version, ctf_header.preamble.flags); |
| 137 | |
| 138 | m_body_offset = offset; |
| 139 | |
| 140 | if (ctf_header.preamble.flags & eFlagCompress) { |
| 141 | // The body has been compressed with zlib deflate. Header offsets point into |
| 142 | // the decompressed data. |
| 143 | #if LLVM_ENABLE_ZLIB |
| 144 | const std::size_t decompressed_size = ctf_header.stroff + ctf_header.strlen; |
| 145 | DataBufferSP decompressed_data = |
| 146 | std::make_shared<DataBufferHeap>(args: decompressed_size, args: 0x0); |
| 147 | |
| 148 | z_stream zstr; |
| 149 | memset(s: &zstr, c: 0, n: sizeof(zstr)); |
| 150 | zstr.next_in = (Bytef *)const_cast<uint8_t *>(m_data.GetDataStart() + |
| 151 | sizeof(ctf_header_t)); |
| 152 | zstr.avail_in = m_data.BytesLeft(offset); |
| 153 | zstr.next_out = |
| 154 | (Bytef *)const_cast<uint8_t *>(decompressed_data->GetBytes()); |
| 155 | zstr.avail_out = decompressed_size; |
| 156 | |
| 157 | int rc = inflateInit(&zstr); |
| 158 | if (rc != Z_OK) { |
| 159 | LLDB_LOG(log, "CTF parsing failed: inflate initialization error: {0}" , |
| 160 | zError(rc)); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | rc = inflate(strm: &zstr, Z_FINISH); |
| 165 | if (rc != Z_STREAM_END) { |
| 166 | LLDB_LOG(log, "CTF parsing failed: inflate error: {0}" , zError(rc)); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | rc = inflateEnd(strm: &zstr); |
| 171 | if (rc != Z_OK) { |
| 172 | LLDB_LOG(log, "CTF parsing failed: inflate end error: {0}" , zError(rc)); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if (zstr.total_out != decompressed_size) { |
| 177 | LLDB_LOG(log, |
| 178 | "CTF parsing failed: decompressed size ({0}) doesn't match " |
| 179 | "expected size ([1})" , |
| 180 | zstr.total_out, decompressed_size); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | m_data = DataExtractor(decompressed_data, m_data.GetByteOrder(), |
| 185 | m_data.GetAddressByteSize()); |
| 186 | m_body_offset = 0; |
| 187 | #else |
| 188 | LLDB_LOG( |
| 189 | log, |
| 190 | "CTF parsing failed: data is compressed but no zlib inflate support" ); |
| 191 | return false; |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | // Validate the header. |
| 196 | if (!m_data.ValidOffset(offset: m_body_offset + ctf_header.lbloff)) { |
| 197 | LLDB_LOG(log, |
| 198 | "CTF parsing failed: invalid label section offset in header: {0}" , |
| 199 | ctf_header.lbloff); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | if (!m_data.ValidOffset(offset: m_body_offset + ctf_header.objtoff)) { |
| 204 | LLDB_LOG(log, |
| 205 | "CTF parsing failed: invalid object section offset in header: {0}" , |
| 206 | ctf_header.objtoff); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | if (!m_data.ValidOffset(offset: m_body_offset + ctf_header.funcoff)) { |
| 211 | LLDB_LOG( |
| 212 | log, |
| 213 | "CTF parsing failed: invalid function section offset in header: {0}" , |
| 214 | ctf_header.funcoff); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | if (!m_data.ValidOffset(offset: m_body_offset + ctf_header.typeoff)) { |
| 219 | LLDB_LOG(log, |
| 220 | "CTF parsing failed: invalid type section offset in header: {0}" , |
| 221 | ctf_header.typeoff); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | if (!m_data.ValidOffset(offset: m_body_offset + ctf_header.stroff)) { |
| 226 | LLDB_LOG(log, |
| 227 | "CTF parsing failed: invalid string section offset in header: {0}" , |
| 228 | ctf_header.stroff); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | const lldb::offset_t str_end_offset = |
| 233 | m_body_offset + ctf_header.stroff + ctf_header.strlen; |
| 234 | if (!m_data.ValidOffset(offset: str_end_offset - 1)) { |
| 235 | LLDB_LOG(log, |
| 236 | "CTF parsing failed: invalid string section length in header: {0}" , |
| 237 | ctf_header.strlen); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | if (m_body_offset + ctf_header.stroff + ctf_header.parlabel > |
| 242 | str_end_offset) { |
| 243 | LLDB_LOG(log, |
| 244 | "CTF parsing failed: invalid parent label offset: {0} exceeds end " |
| 245 | "of string section ({1})" , |
| 246 | ctf_header.parlabel, str_end_offset); |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | if (m_body_offset + ctf_header.stroff + ctf_header.parname > str_end_offset) { |
| 251 | LLDB_LOG(log, |
| 252 | "CTF parsing failed: invalid parent name offset: {0} exceeds end " |
| 253 | "of string section ({1})" , |
| 254 | ctf_header.parname, str_end_offset); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | LLDB_LOG(log, |
| 259 | "Parsed valid CTF header: lbloff = {0}, objtoff = {1}, funcoff = " |
| 260 | "{2}, typeoff = {3}, stroff = {4}, strlen = {5}" , |
| 261 | ctf_header.lbloff, ctf_header.objtoff, ctf_header.funcoff, |
| 262 | ctf_header.typeoff, ctf_header.stroff, ctf_header.strlen); |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | void SymbolFileCTF::InitializeObject() { |
| 268 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 269 | |
| 270 | auto type_system_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC); |
| 271 | if (auto err = type_system_or_err.takeError()) { |
| 272 | LLDB_LOG_ERROR(log, std::move(err), "Unable to get type system: {0}" ); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | auto ts = *type_system_or_err; |
| 277 | m_ast = llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get()); |
| 278 | LazyBool optimized = eLazyBoolNo; |
| 279 | m_comp_unit_sp = std::make_shared<CompileUnit>( |
| 280 | args: m_objfile_sp->GetModule(), args: nullptr, args: "" , args: 0, args: eLanguageTypeC, args&: optimized); |
| 281 | |
| 282 | ParseTypes(cu&: *m_comp_unit_sp); |
| 283 | } |
| 284 | |
| 285 | llvm::StringRef SymbolFileCTF::ReadString(lldb::offset_t str_offset) const { |
| 286 | lldb::offset_t offset = m_body_offset + m_header->stroff + str_offset; |
| 287 | if (!m_data.ValidOffset(offset)) |
| 288 | return "(invalid)" ; |
| 289 | const char *str = m_data.GetCStr(offset_ptr: &offset); |
| 290 | if (str && !*str) |
| 291 | return "(anon)" ; |
| 292 | return llvm::StringRef(str); |
| 293 | } |
| 294 | |
| 295 | /// Return the integer display representation encoded in the given data. |
| 296 | static uint32_t GetEncoding(uint32_t data) { |
| 297 | // Mask bits 24–31. |
| 298 | return ((data)&0xff000000) >> 24; |
| 299 | } |
| 300 | |
| 301 | /// Return the integral width in bits encoded in the given data. |
| 302 | static uint32_t GetBits(uint32_t data) { |
| 303 | // Mask bits 0-15. |
| 304 | return (data)&0x0000ffff; |
| 305 | } |
| 306 | |
| 307 | /// Return the type kind encoded in the given data. |
| 308 | uint32_t GetKind(uint32_t data) { |
| 309 | // Mask bits 26–31. |
| 310 | return ((data)&0xf800) >> 11; |
| 311 | } |
| 312 | |
| 313 | /// Return the variable length encoded in the given data. |
| 314 | uint32_t GetVLen(uint32_t data) { |
| 315 | // Mask bits 0–24. |
| 316 | return (data)&0x3ff; |
| 317 | } |
| 318 | |
| 319 | static uint32_t GetBytes(uint32_t bits) { return bits / sizeof(unsigned); } |
| 320 | |
| 321 | static clang::TagTypeKind TranslateRecordKind(CTFType::Kind type) { |
| 322 | switch (type) { |
| 323 | case CTFType::Kind::eStruct: |
| 324 | return clang::TagTypeKind::Struct; |
| 325 | case CTFType::Kind::eUnion: |
| 326 | return clang::TagTypeKind::Union; |
| 327 | default: |
| 328 | lldbassert(false && "Invalid record kind!" ); |
| 329 | return clang::TagTypeKind::Struct; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | llvm::Expected<TypeSP> |
| 334 | SymbolFileCTF::CreateInteger(const CTFInteger &ctf_integer) { |
| 335 | lldb::BasicType basic_type = |
| 336 | TypeSystemClang::GetBasicTypeEnumeration(name: ctf_integer.name); |
| 337 | if (basic_type == eBasicTypeInvalid) |
| 338 | return llvm::make_error<llvm::StringError>( |
| 339 | Args: llvm::formatv(Fmt: "unsupported integer type: no corresponding basic clang " |
| 340 | "type for '{0}'" , |
| 341 | Vals: ctf_integer.name), |
| 342 | Args: llvm::inconvertibleErrorCode()); |
| 343 | |
| 344 | CompilerType compiler_type = m_ast->GetBasicType(type: basic_type); |
| 345 | |
| 346 | if (basic_type != eBasicTypeVoid && basic_type != eBasicTypeBool) { |
| 347 | // Make sure the type we got is an integer type. |
| 348 | bool compiler_type_is_signed = false; |
| 349 | if (!compiler_type.IsIntegerType(is_signed&: compiler_type_is_signed)) |
| 350 | return llvm::make_error<llvm::StringError>( |
| 351 | Args: llvm::formatv( |
| 352 | Fmt: "Found compiler type for '{0}' but it's not an integer type: {1}" , |
| 353 | Vals: ctf_integer.name, |
| 354 | Vals: compiler_type.GetDisplayTypeName().GetStringRef()), |
| 355 | Args: llvm::inconvertibleErrorCode()); |
| 356 | |
| 357 | // Make sure the signing matches between the CTF and the compiler type. |
| 358 | const bool type_is_signed = (ctf_integer.encoding & IntEncoding::eSigned); |
| 359 | if (compiler_type_is_signed != type_is_signed) |
| 360 | return llvm::make_error<llvm::StringError>( |
| 361 | Args: llvm::formatv(Fmt: "Found integer compiler type for {0} but compiler type " |
| 362 | "is {1} and {0} is {2}" , |
| 363 | Vals: ctf_integer.name, |
| 364 | Vals: compiler_type_is_signed ? "signed" : "unsigned" , |
| 365 | Vals: type_is_signed ? "signed" : "unsigned" ), |
| 366 | Args: llvm::inconvertibleErrorCode()); |
| 367 | } |
| 368 | |
| 369 | Declaration decl; |
| 370 | return MakeType(uid: ctf_integer.uid, name: ConstString(ctf_integer.name), |
| 371 | byte_size: GetBytes(bits: ctf_integer.bits), context: nullptr, LLDB_INVALID_UID, |
| 372 | encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, compiler_qual_type: compiler_type, |
| 373 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 374 | } |
| 375 | |
| 376 | llvm::Expected<lldb::TypeSP> |
| 377 | SymbolFileCTF::CreateModifier(const CTFModifier &ctf_modifier) { |
| 378 | Type *ref_type = ResolveTypeUID(type_uid: ctf_modifier.type); |
| 379 | if (!ref_type) |
| 380 | return llvm::make_error<llvm::StringError>( |
| 381 | Args: llvm::formatv(Fmt: "Could not find modified type: {0}" , Vals: ctf_modifier.type), |
| 382 | Args: llvm::inconvertibleErrorCode()); |
| 383 | |
| 384 | CompilerType compiler_type; |
| 385 | |
| 386 | switch (ctf_modifier.kind) { |
| 387 | case CTFType::ePointer: |
| 388 | compiler_type = ref_type->GetFullCompilerType().GetPointerType(); |
| 389 | break; |
| 390 | case CTFType::eConst: |
| 391 | compiler_type = ref_type->GetFullCompilerType().AddConstModifier(); |
| 392 | break; |
| 393 | case CTFType::eVolatile: |
| 394 | compiler_type = ref_type->GetFullCompilerType().AddVolatileModifier(); |
| 395 | break; |
| 396 | case CTFType::eRestrict: |
| 397 | compiler_type = ref_type->GetFullCompilerType().AddRestrictModifier(); |
| 398 | break; |
| 399 | default: |
| 400 | return llvm::make_error<llvm::StringError>( |
| 401 | Args: llvm::formatv(Fmt: "ParseModifier called with unsupported kind: {0}" , |
| 402 | Vals: ctf_modifier.kind), |
| 403 | Args: llvm::inconvertibleErrorCode()); |
| 404 | } |
| 405 | |
| 406 | Declaration decl; |
| 407 | return MakeType(uid: ctf_modifier.uid, name: ConstString(), byte_size: 0, context: nullptr, LLDB_INVALID_UID, |
| 408 | encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: compiler_type, |
| 409 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 410 | } |
| 411 | |
| 412 | llvm::Expected<lldb::TypeSP> |
| 413 | SymbolFileCTF::CreateTypedef(const CTFTypedef &ctf_typedef) { |
| 414 | Type *underlying_type = ResolveTypeUID(type_uid: ctf_typedef.type); |
| 415 | if (!underlying_type) |
| 416 | return llvm::make_error<llvm::StringError>( |
| 417 | Args: llvm::formatv(Fmt: "Could not find typedef underlying type: {0}" , |
| 418 | Vals: ctf_typedef.type), |
| 419 | Args: llvm::inconvertibleErrorCode()); |
| 420 | |
| 421 | CompilerType target_ast_type = underlying_type->GetFullCompilerType(); |
| 422 | clang::DeclContext *decl_ctx = m_ast->GetTranslationUnitDecl(); |
| 423 | CompilerType ast_typedef = target_ast_type.CreateTypedef( |
| 424 | name: ctf_typedef.name.data(), decl_ctx: m_ast->CreateDeclContext(ctx: decl_ctx), payload: 0); |
| 425 | |
| 426 | Declaration decl; |
| 427 | return MakeType(uid: ctf_typedef.uid, name: ConstString(ctf_typedef.name), byte_size: 0, context: nullptr, |
| 428 | LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, |
| 429 | compiler_qual_type: ast_typedef, compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 430 | } |
| 431 | |
| 432 | llvm::Expected<lldb::TypeSP> |
| 433 | SymbolFileCTF::CreateArray(const CTFArray &ctf_array) { |
| 434 | Type *element_type = ResolveTypeUID(type_uid: ctf_array.type); |
| 435 | if (!element_type) |
| 436 | return llvm::make_error<llvm::StringError>( |
| 437 | Args: llvm::formatv(Fmt: "Could not find array element type: {0}" , Vals: ctf_array.type), |
| 438 | Args: llvm::inconvertibleErrorCode()); |
| 439 | |
| 440 | auto element_size_or_err = element_type->GetByteSize(exe_scope: nullptr); |
| 441 | if (!element_size_or_err) |
| 442 | return element_size_or_err.takeError(); |
| 443 | |
| 444 | uint64_t size = ctf_array.nelems * *element_size_or_err; |
| 445 | |
| 446 | CompilerType compiler_type = m_ast->CreateArrayType( |
| 447 | element_type: element_type->GetFullCompilerType(), element_count: ctf_array.nelems, |
| 448 | /*is_gnu_vector*/ is_vector: false); |
| 449 | |
| 450 | Declaration decl; |
| 451 | return MakeType(uid: ctf_array.uid, name: ConstString(), byte_size: size, context: nullptr, LLDB_INVALID_UID, |
| 452 | encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: compiler_type, |
| 453 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 454 | } |
| 455 | |
| 456 | llvm::Expected<lldb::TypeSP> |
| 457 | SymbolFileCTF::CreateEnum(const CTFEnum &ctf_enum) { |
| 458 | Declaration decl; |
| 459 | CompilerType enum_type = m_ast->CreateEnumerationType( |
| 460 | ctf_enum.name, m_ast->GetTranslationUnitDecl(), OptionalClangModuleID(), |
| 461 | decl, m_ast->GetBasicType(type: eBasicTypeInt), |
| 462 | /*is_scoped=*/false); |
| 463 | |
| 464 | for (const CTFEnum::Value &value : ctf_enum.values) { |
| 465 | Declaration value_decl; |
| 466 | m_ast->AddEnumerationValueToEnumerationType( |
| 467 | enum_type, decl: value_decl, name: value.name.data(), enum_value: value.value, enum_value_bit_size: ctf_enum.size); |
| 468 | } |
| 469 | TypeSystemClang::CompleteTagDeclarationDefinition(type: enum_type); |
| 470 | |
| 471 | return MakeType(uid: ctf_enum.uid, name: ConstString(), byte_size: 0, context: nullptr, LLDB_INVALID_UID, |
| 472 | encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: enum_type, |
| 473 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 474 | } |
| 475 | |
| 476 | llvm::Expected<lldb::TypeSP> |
| 477 | SymbolFileCTF::CreateFunction(const CTFFunction &ctf_function) { |
| 478 | std::vector<CompilerType> arg_types; |
| 479 | for (uint32_t arg : ctf_function.args) { |
| 480 | if (Type *arg_type = ResolveTypeUID(type_uid: arg)) |
| 481 | arg_types.push_back(x: arg_type->GetFullCompilerType()); |
| 482 | } |
| 483 | |
| 484 | Type *ret_type = ResolveTypeUID(type_uid: ctf_function.return_type); |
| 485 | if (!ret_type) |
| 486 | return llvm::make_error<llvm::StringError>( |
| 487 | Args: llvm::formatv(Fmt: "Could not find function return type: {0}" , |
| 488 | Vals: ctf_function.return_type), |
| 489 | Args: llvm::inconvertibleErrorCode()); |
| 490 | |
| 491 | CompilerType func_type = m_ast->CreateFunctionType( |
| 492 | result_type: ret_type->GetFullCompilerType(), args: arg_types, is_variadic: ctf_function.variadic, type_quals: 0, |
| 493 | cc: clang::CallingConv::CC_C); |
| 494 | |
| 495 | Declaration decl; |
| 496 | return MakeType(uid: ctf_function.uid, name: ConstString(ctf_function.name), byte_size: 0, context: nullptr, |
| 497 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: func_type, |
| 498 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 499 | } |
| 500 | |
| 501 | llvm::Expected<lldb::TypeSP> |
| 502 | SymbolFileCTF::CreateRecord(const CTFRecord &ctf_record) { |
| 503 | const clang::TagTypeKind tag_kind = TranslateRecordKind(type: ctf_record.kind); |
| 504 | CompilerType record_type = m_ast->CreateRecordType( |
| 505 | decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: eAccessPublic, name: ctf_record.name.data(), |
| 506 | kind: llvm::to_underlying(E: tag_kind), language: eLanguageTypeC); |
| 507 | m_compiler_types[record_type.GetOpaqueQualType()] = &ctf_record; |
| 508 | Declaration decl; |
| 509 | return MakeType(uid: ctf_record.uid, name: ConstString(ctf_record.name), byte_size: ctf_record.size, |
| 510 | context: nullptr, LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, |
| 511 | decl, compiler_qual_type: record_type, compiler_type_resolve_state: lldb_private::Type::ResolveState::Forward); |
| 512 | } |
| 513 | |
| 514 | bool SymbolFileCTF::CompleteType(CompilerType &compiler_type) { |
| 515 | // Check if we have a CTF type for the given incomplete compiler type. |
| 516 | auto it = m_compiler_types.find(Val: compiler_type.GetOpaqueQualType()); |
| 517 | if (it == m_compiler_types.end()) |
| 518 | return false; |
| 519 | |
| 520 | const CTFType *ctf_type = it->second; |
| 521 | assert(ctf_type && "m_compiler_types should only contain valid CTF types" ); |
| 522 | |
| 523 | // We only support resolving record types. |
| 524 | assert(llvm::isa<CTFRecord>(ctf_type)); |
| 525 | |
| 526 | // Cast to the appropriate CTF type. |
| 527 | const CTFRecord *ctf_record = static_cast<const CTFRecord *>(ctf_type); |
| 528 | |
| 529 | // If any of the fields are incomplete, we cannot complete the type. |
| 530 | for (const CTFRecord::Field &field : ctf_record->fields) { |
| 531 | if (!ResolveTypeUID(type_uid: field.type)) { |
| 532 | LLDB_LOG(GetLog(LLDBLog::Symbols), |
| 533 | "Cannot complete type {0} because field {1} is incomplete" , |
| 534 | ctf_type->uid, field.type); |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // Complete the record type. |
| 540 | m_ast->StartTagDeclarationDefinition(type: compiler_type); |
| 541 | for (const CTFRecord::Field &field : ctf_record->fields) { |
| 542 | Type *field_type = ResolveTypeUID(type_uid: field.type); |
| 543 | assert(field_type && "field must be complete" ); |
| 544 | const uint32_t field_size = |
| 545 | llvm::expectedToOptional(E: field_type->GetByteSize(exe_scope: nullptr)).value_or(u: 0); |
| 546 | TypeSystemClang::AddFieldToRecordType(type: compiler_type, name: field.name, |
| 547 | field_type: field_type->GetFullCompilerType(), |
| 548 | access: eAccessPublic, bitfield_bit_size: field_size); |
| 549 | } |
| 550 | m_ast->CompleteTagDeclarationDefinition(type: compiler_type); |
| 551 | |
| 552 | // Now that the compiler type is complete, we don't need to remember it |
| 553 | // anymore and can remove the CTF record type. |
| 554 | m_compiler_types.erase(Val: compiler_type.GetOpaqueQualType()); |
| 555 | m_ctf_types.erase(Val: ctf_type->uid); |
| 556 | |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | llvm::Expected<lldb::TypeSP> |
| 561 | SymbolFileCTF::CreateForward(const CTFForward &ctf_forward) { |
| 562 | CompilerType forward_compiler_type = m_ast->CreateRecordType( |
| 563 | decl_ctx: nullptr, owning_module: OptionalClangModuleID(), access_type: eAccessPublic, name: ctf_forward.name, |
| 564 | kind: llvm::to_underlying(E: clang::TagTypeKind::Struct), language: eLanguageTypeC); |
| 565 | Declaration decl; |
| 566 | return MakeType(uid: ctf_forward.uid, name: ConstString(ctf_forward.name), byte_size: 0, context: nullptr, |
| 567 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, |
| 568 | compiler_qual_type: forward_compiler_type, compiler_type_resolve_state: Type::ResolveState::Forward); |
| 569 | } |
| 570 | |
| 571 | llvm::Expected<TypeSP> SymbolFileCTF::CreateType(CTFType *ctf_type) { |
| 572 | if (!ctf_type) |
| 573 | return llvm::make_error<llvm::StringError>( |
| 574 | Args: "cannot create type for unparsed type" , Args: llvm::inconvertibleErrorCode()); |
| 575 | |
| 576 | switch (ctf_type->kind) { |
| 577 | case CTFType::Kind::eInteger: |
| 578 | return CreateInteger(ctf_integer: *static_cast<CTFInteger *>(ctf_type)); |
| 579 | case CTFType::Kind::eConst: |
| 580 | case CTFType::Kind::ePointer: |
| 581 | case CTFType::Kind::eRestrict: |
| 582 | case CTFType::Kind::eVolatile: |
| 583 | return CreateModifier(ctf_modifier: *static_cast<CTFModifier *>(ctf_type)); |
| 584 | case CTFType::Kind::eTypedef: |
| 585 | return CreateTypedef(ctf_typedef: *static_cast<CTFTypedef *>(ctf_type)); |
| 586 | case CTFType::Kind::eArray: |
| 587 | return CreateArray(ctf_array: *static_cast<CTFArray *>(ctf_type)); |
| 588 | case CTFType::Kind::eEnum: |
| 589 | return CreateEnum(ctf_enum: *static_cast<CTFEnum *>(ctf_type)); |
| 590 | case CTFType::Kind::eFunction: |
| 591 | return CreateFunction(ctf_function: *static_cast<CTFFunction *>(ctf_type)); |
| 592 | case CTFType::Kind::eStruct: |
| 593 | case CTFType::Kind::eUnion: |
| 594 | return CreateRecord(ctf_record: *static_cast<CTFRecord *>(ctf_type)); |
| 595 | case CTFType::Kind::eForward: |
| 596 | return CreateForward(ctf_forward: *static_cast<CTFForward *>(ctf_type)); |
| 597 | case CTFType::Kind::eUnknown: |
| 598 | case CTFType::Kind::eFloat: |
| 599 | case CTFType::Kind::eSlice: |
| 600 | return llvm::make_error<llvm::StringError>( |
| 601 | Args: llvm::formatv(Fmt: "unsupported type (uid = {0}, name = {1}, kind = {2})" , |
| 602 | Vals&: ctf_type->uid, Vals&: ctf_type->name, Vals&: ctf_type->kind), |
| 603 | Args: llvm::inconvertibleErrorCode()); |
| 604 | } |
| 605 | llvm_unreachable("Unexpected CTF type kind" ); |
| 606 | } |
| 607 | |
| 608 | llvm::Expected<std::unique_ptr<CTFType>> |
| 609 | SymbolFileCTF::ParseType(lldb::offset_t &offset, lldb::user_id_t uid) { |
| 610 | ctf_stype_t ctf_stype; |
| 611 | ctf_stype.name = m_data.GetU32(offset_ptr: &offset); |
| 612 | ctf_stype.info = m_data.GetU32(offset_ptr: &offset); |
| 613 | ctf_stype.size = m_data.GetU32(offset_ptr: &offset); |
| 614 | |
| 615 | llvm::StringRef name = ReadString(str_offset: ctf_stype.name); |
| 616 | const uint32_t kind = GetKind(data: ctf_stype.info); |
| 617 | const uint32_t variable_length = GetVLen(data: ctf_stype.info); |
| 618 | const uint32_t type = ctf_stype.GetType(); |
| 619 | const uint32_t size = ctf_stype.GetSize(); |
| 620 | |
| 621 | switch (kind) { |
| 622 | case TypeKind::eInteger: { |
| 623 | const uint32_t vdata = m_data.GetU32(offset_ptr: &offset); |
| 624 | const uint32_t bits = GetBits(data: vdata); |
| 625 | const uint32_t encoding = GetEncoding(data: vdata); |
| 626 | return std::make_unique<CTFInteger>(args&: uid, args&: name, args: bits, args: encoding); |
| 627 | } |
| 628 | case TypeKind::eConst: |
| 629 | return std::make_unique<CTFConst>(args&: uid, args: type); |
| 630 | case TypeKind::ePointer: |
| 631 | return std::make_unique<CTFPointer>(args&: uid, args: type); |
| 632 | case TypeKind::eRestrict: |
| 633 | return std::make_unique<CTFRestrict>(args&: uid, args: type); |
| 634 | case TypeKind::eVolatile: |
| 635 | return std::make_unique<CTFVolatile>(args&: uid, args: type); |
| 636 | case TypeKind::eTypedef: |
| 637 | return std::make_unique<CTFTypedef>(args&: uid, args&: name, args: type); |
| 638 | case TypeKind::eArray: { |
| 639 | const uint32_t type = m_data.GetU32(offset_ptr: &offset); |
| 640 | const uint32_t index = m_data.GetU32(offset_ptr: &offset); |
| 641 | const uint32_t nelems = m_data.GetU32(offset_ptr: &offset); |
| 642 | return std::make_unique<CTFArray>(args&: uid, args&: name, args: type, args: index, args: nelems); |
| 643 | } |
| 644 | case TypeKind::eEnum: { |
| 645 | std::vector<CTFEnum::Value> values; |
| 646 | for (uint32_t i = 0; i < variable_length; ++i) { |
| 647 | const uint32_t value_name = m_data.GetU32(offset_ptr: &offset); |
| 648 | const uint32_t value = m_data.GetU32(offset_ptr: &offset); |
| 649 | values.emplace_back(args: ReadString(str_offset: value_name), args: value); |
| 650 | } |
| 651 | return std::make_unique<CTFEnum>(args&: uid, args&: name, args: variable_length, args: size, args&: values); |
| 652 | } |
| 653 | case TypeKind::eFunction: { |
| 654 | std::vector<uint32_t> args; |
| 655 | bool variadic = false; |
| 656 | for (uint32_t i = 0; i < variable_length; ++i) { |
| 657 | const uint32_t arg_uid = m_data.GetU32(offset_ptr: &offset); |
| 658 | // If the last argument is 0, this is a variadic function. |
| 659 | if (arg_uid == 0) { |
| 660 | variadic = true; |
| 661 | break; |
| 662 | } |
| 663 | args.push_back(x: arg_uid); |
| 664 | } |
| 665 | // If the number of arguments is odd, a single uint32_t of padding is |
| 666 | // inserted to maintain alignment. |
| 667 | if (variable_length % 2 == 1) |
| 668 | m_data.GetU32(offset_ptr: &offset); |
| 669 | return std::make_unique<CTFFunction>(args&: uid, args&: name, args: variable_length, args: type, args&: args, |
| 670 | args&: variadic); |
| 671 | } |
| 672 | case TypeKind::eStruct: |
| 673 | case TypeKind::eUnion: { |
| 674 | std::vector<CTFRecord::Field> fields; |
| 675 | for (uint32_t i = 0; i < variable_length; ++i) { |
| 676 | const uint32_t field_name = m_data.GetU32(offset_ptr: &offset); |
| 677 | const uint32_t type = m_data.GetU32(offset_ptr: &offset); |
| 678 | uint64_t field_offset = 0; |
| 679 | if (size < g_ctf_field_threshold) { |
| 680 | field_offset = m_data.GetU16(offset_ptr: &offset); |
| 681 | m_data.GetU16(offset_ptr: &offset); // Padding |
| 682 | } else { |
| 683 | const uint32_t offset_hi = m_data.GetU32(offset_ptr: &offset); |
| 684 | const uint32_t offset_lo = m_data.GetU32(offset_ptr: &offset); |
| 685 | field_offset = (((uint64_t)offset_hi) << 32) | ((uint64_t)offset_lo); |
| 686 | } |
| 687 | fields.emplace_back(args: ReadString(str_offset: field_name), args: type, args&: field_offset); |
| 688 | } |
| 689 | return std::make_unique<CTFRecord>(args: static_cast<CTFType::Kind>(kind), args&: uid, |
| 690 | args&: name, args: variable_length, args: size, args&: fields); |
| 691 | } |
| 692 | case TypeKind::eForward: |
| 693 | return std::make_unique<CTFForward>(args&: uid, args&: name); |
| 694 | case TypeKind::eUnknown: |
| 695 | return std::make_unique<CTFType>(args: static_cast<CTFType::Kind>(kind), args&: uid, |
| 696 | args&: name); |
| 697 | case TypeKind::eFloat: |
| 698 | case TypeKind::eSlice: |
| 699 | offset += (variable_length * sizeof(uint32_t)); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | return llvm::make_error<llvm::StringError>( |
| 704 | Args: llvm::formatv(Fmt: "unsupported type (name = {0}, kind = {1}, vlength = {2})" , |
| 705 | Vals&: name, Vals: kind, Vals: variable_length), |
| 706 | Args: llvm::inconvertibleErrorCode()); |
| 707 | } |
| 708 | |
| 709 | size_t SymbolFileCTF::ParseTypes(CompileUnit &cu) { |
| 710 | if (!ParseHeader()) |
| 711 | return 0; |
| 712 | |
| 713 | if (!m_types.empty()) |
| 714 | return 0; |
| 715 | |
| 716 | if (!m_ast) |
| 717 | return 0; |
| 718 | |
| 719 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 720 | LLDB_LOG(log, "Parsing CTF types" ); |
| 721 | |
| 722 | lldb::offset_t type_offset = m_body_offset + m_header->typeoff; |
| 723 | const lldb::offset_t type_offset_end = m_body_offset + m_header->stroff; |
| 724 | |
| 725 | lldb::user_id_t type_uid = 1; |
| 726 | while (type_offset < type_offset_end) { |
| 727 | llvm::Expected<std::unique_ptr<CTFType>> type_or_error = |
| 728 | ParseType(offset&: type_offset, uid: type_uid); |
| 729 | if (type_or_error) { |
| 730 | m_ctf_types[(*type_or_error)->uid] = std::move(*type_or_error); |
| 731 | } else { |
| 732 | LLDB_LOG_ERROR(log, type_or_error.takeError(), |
| 733 | "Failed to parse type {1} at offset {2}: {0}" , type_uid, |
| 734 | type_offset); |
| 735 | } |
| 736 | type_uid++; |
| 737 | } |
| 738 | |
| 739 | LLDB_LOG(log, "Parsed {0} CTF types" , m_ctf_types.size()); |
| 740 | |
| 741 | for (lldb::user_id_t uid = 1; uid < type_uid; ++uid) |
| 742 | ResolveTypeUID(type_uid: uid); |
| 743 | |
| 744 | LLDB_LOG(log, "Created {0} CTF types" , m_types.size()); |
| 745 | |
| 746 | return m_types.size(); |
| 747 | } |
| 748 | |
| 749 | size_t SymbolFileCTF::ParseFunctions(CompileUnit &cu) { |
| 750 | if (!ParseHeader()) |
| 751 | return 0; |
| 752 | |
| 753 | if (!m_functions.empty()) |
| 754 | return 0; |
| 755 | |
| 756 | if (!m_ast) |
| 757 | return 0; |
| 758 | |
| 759 | Symtab *symtab = GetObjectFile()->GetModule()->GetSymtab(); |
| 760 | if (!symtab) |
| 761 | return 0; |
| 762 | |
| 763 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 764 | LLDB_LOG(log, "Parsing CTF functions" ); |
| 765 | |
| 766 | lldb::offset_t function_offset = m_body_offset + m_header->funcoff; |
| 767 | const lldb::offset_t function_offset_end = m_body_offset + m_header->typeoff; |
| 768 | |
| 769 | uint32_t symbol_idx = 0; |
| 770 | Declaration decl; |
| 771 | while (function_offset < function_offset_end) { |
| 772 | const uint32_t info = m_data.GetU32(offset_ptr: &function_offset); |
| 773 | const uint16_t kind = GetKind(data: info); |
| 774 | const uint16_t variable_length = GetVLen(data: info); |
| 775 | |
| 776 | Symbol *symbol = symtab->FindSymbolWithType( |
| 777 | symbol_type: eSymbolTypeCode, symbol_debug_type: Symtab::eDebugYes, symbol_visibility: Symtab::eVisibilityAny, start_idx&: symbol_idx); |
| 778 | |
| 779 | // Skip padding. |
| 780 | if (kind == TypeKind::eUnknown && variable_length == 0) |
| 781 | continue; |
| 782 | |
| 783 | // Skip unexpected kinds. |
| 784 | if (kind != TypeKind::eFunction) |
| 785 | continue; |
| 786 | |
| 787 | const uint32_t ret_uid = m_data.GetU32(offset_ptr: &function_offset); |
| 788 | const uint32_t num_args = variable_length; |
| 789 | |
| 790 | std::vector<CompilerType> arg_types; |
| 791 | arg_types.reserve(n: num_args); |
| 792 | |
| 793 | bool is_variadic = false; |
| 794 | for (uint32_t i = 0; i < variable_length; i++) { |
| 795 | const uint32_t arg_uid = m_data.GetU32(offset_ptr: &function_offset); |
| 796 | |
| 797 | // If the last argument is 0, this is a variadic function. |
| 798 | if (arg_uid == 0) { |
| 799 | is_variadic = true; |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | Type *arg_type = ResolveTypeUID(type_uid: arg_uid); |
| 804 | arg_types.push_back(x: arg_type ? arg_type->GetFullCompilerType() |
| 805 | : CompilerType()); |
| 806 | } |
| 807 | |
| 808 | if (symbol) { |
| 809 | Type *ret_type = ResolveTypeUID(type_uid: ret_uid); |
| 810 | AddressRange func_range = |
| 811 | AddressRange(symbol->GetFileAddress(), symbol->GetByteSize(), |
| 812 | GetObjectFile()->GetModule()->GetSectionList()); |
| 813 | |
| 814 | // Create function type. |
| 815 | CompilerType func_type = m_ast->CreateFunctionType( |
| 816 | result_type: ret_type ? ret_type->GetFullCompilerType() : CompilerType(), |
| 817 | args: arg_types, is_variadic, type_quals: 0, cc: clang::CallingConv::CC_C); |
| 818 | lldb::user_id_t function_type_uid = m_types.size() + 1; |
| 819 | TypeSP type_sp = |
| 820 | MakeType(uid: function_type_uid, name: symbol->GetName(), byte_size: 0, context: nullptr, |
| 821 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: func_type, |
| 822 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
| 823 | m_types[function_type_uid] = type_sp; |
| 824 | |
| 825 | // Create function. |
| 826 | lldb::user_id_t func_uid = m_functions.size(); |
| 827 | FunctionSP function_sp = std::make_shared<Function>( |
| 828 | args: &cu, args&: func_uid, args&: function_type_uid, args&: symbol->GetMangled(), args: type_sp.get(), |
| 829 | args: symbol->GetAddress(), args: AddressRanges{func_range}); |
| 830 | m_functions.emplace_back(args&: function_sp); |
| 831 | cu.AddFunction(function_sp); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | LLDB_LOG(log, "CTF parsed {0} functions" , m_functions.size()); |
| 836 | |
| 837 | return m_functions.size(); |
| 838 | } |
| 839 | |
| 840 | static DWARFExpression CreateDWARFExpression(ModuleSP module_sp, |
| 841 | const Symbol &symbol) { |
| 842 | if (!module_sp) |
| 843 | return DWARFExpression(); |
| 844 | |
| 845 | const ArchSpec &architecture = module_sp->GetArchitecture(); |
| 846 | ByteOrder byte_order = architecture.GetByteOrder(); |
| 847 | uint32_t address_size = architecture.GetAddressByteSize(); |
| 848 | uint32_t byte_size = architecture.GetDataByteSize(); |
| 849 | |
| 850 | StreamBuffer<32> stream(Stream::eBinary, address_size, byte_order); |
| 851 | stream.PutHex8(uvalue: lldb_private::dwarf::DW_OP_addr); |
| 852 | stream.PutMaxHex64(uvalue: symbol.GetFileAddress(), byte_size: address_size, byte_order); |
| 853 | |
| 854 | DataBufferSP buffer = |
| 855 | std::make_shared<DataBufferHeap>(args: stream.GetData(), args: stream.GetSize()); |
| 856 | lldb_private::DataExtractor (buffer, byte_order, address_size, |
| 857 | byte_size); |
| 858 | DWARFExpression result(extractor); |
| 859 | result.SetRegisterKind(eRegisterKindDWARF); |
| 860 | |
| 861 | return result; |
| 862 | } |
| 863 | |
| 864 | size_t SymbolFileCTF::ParseObjects(CompileUnit &comp_unit) { |
| 865 | if (!ParseHeader()) |
| 866 | return 0; |
| 867 | |
| 868 | if (!m_variables.empty()) |
| 869 | return 0; |
| 870 | |
| 871 | if (!m_ast) |
| 872 | return 0; |
| 873 | |
| 874 | ModuleSP module_sp = GetObjectFile()->GetModule(); |
| 875 | Symtab *symtab = module_sp->GetSymtab(); |
| 876 | if (!symtab) |
| 877 | return 0; |
| 878 | |
| 879 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 880 | LLDB_LOG(log, "Parsing CTF objects" ); |
| 881 | |
| 882 | lldb::offset_t object_offset = m_body_offset + m_header->objtoff; |
| 883 | const lldb::offset_t object_offset_end = m_body_offset + m_header->funcoff; |
| 884 | |
| 885 | uint32_t symbol_idx = 0; |
| 886 | Declaration decl; |
| 887 | while (object_offset < object_offset_end) { |
| 888 | const uint32_t type_uid = m_data.GetU32(offset_ptr: &object_offset); |
| 889 | |
| 890 | if (Symbol *symbol = |
| 891 | symtab->FindSymbolWithType(symbol_type: eSymbolTypeData, symbol_debug_type: Symtab::eDebugYes, |
| 892 | symbol_visibility: Symtab::eVisibilityAny, start_idx&: symbol_idx)) { |
| 893 | Variable::RangeList ranges; |
| 894 | ranges.Append(base: symbol->GetFileAddress(), size: symbol->GetByteSize()); |
| 895 | |
| 896 | auto type_sp = std::make_shared<SymbolFileType>(args&: *this, args: type_uid); |
| 897 | |
| 898 | DWARFExpressionList location( |
| 899 | module_sp, CreateDWARFExpression(module_sp, symbol: *symbol), nullptr); |
| 900 | |
| 901 | lldb::user_id_t variable_type_uid = m_variables.size(); |
| 902 | m_variables.emplace_back(args: std::make_shared<Variable>( |
| 903 | args&: variable_type_uid, args: symbol->GetName().AsCString(), |
| 904 | args: symbol->GetName().AsCString(), args&: type_sp, args: eValueTypeVariableGlobal, |
| 905 | args: m_comp_unit_sp.get(), args&: ranges, args: &decl, args&: location, args: symbol->IsExternal(), |
| 906 | /*artificial=*/args: false, |
| 907 | /*location_is_constant_data*/ args: false)); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | LLDB_LOG(log, "Parsed {0} CTF objects" , m_variables.size()); |
| 912 | |
| 913 | return m_variables.size(); |
| 914 | } |
| 915 | |
| 916 | uint32_t SymbolFileCTF::CalculateAbilities() { |
| 917 | if (!m_objfile_sp) |
| 918 | return 0; |
| 919 | |
| 920 | if (!ParseHeader()) |
| 921 | return 0; |
| 922 | |
| 923 | return VariableTypes | Functions | GlobalVariables; |
| 924 | } |
| 925 | |
| 926 | uint32_t SymbolFileCTF::ResolveSymbolContext(const Address &so_addr, |
| 927 | SymbolContextItem resolve_scope, |
| 928 | SymbolContext &sc) { |
| 929 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
| 930 | if (m_objfile_sp->GetSymtab() == nullptr) |
| 931 | return 0; |
| 932 | |
| 933 | uint32_t resolved_flags = 0; |
| 934 | |
| 935 | // Resolve symbols. |
| 936 | if (resolve_scope & eSymbolContextSymbol) { |
| 937 | sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress( |
| 938 | file_addr: so_addr.GetFileAddress()); |
| 939 | if (sc.symbol) |
| 940 | resolved_flags |= eSymbolContextSymbol; |
| 941 | } |
| 942 | |
| 943 | // Resolve functions. |
| 944 | if (resolve_scope & eSymbolContextFunction) { |
| 945 | for (FunctionSP function_sp : m_functions) { |
| 946 | if (llvm::any_of( |
| 947 | Range: function_sp->GetAddressRanges(), P: [&](const AddressRange range) { |
| 948 | return range.ContainsFileAddress(file_addr: so_addr.GetFileAddress()); |
| 949 | })) { |
| 950 | sc.function = function_sp.get(); |
| 951 | resolved_flags |= eSymbolContextFunction; |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // Resolve variables. |
| 958 | if (resolve_scope & eSymbolContextVariable) { |
| 959 | for (VariableSP variable_sp : m_variables) { |
| 960 | if (variable_sp->LocationIsValidForAddress(address: so_addr.GetFileAddress())) { |
| 961 | sc.variable = variable_sp.get(); |
| 962 | break; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | return resolved_flags; |
| 968 | } |
| 969 | |
| 970 | CompUnitSP SymbolFileCTF::ParseCompileUnitAtIndex(uint32_t idx) { |
| 971 | if (idx == 0) |
| 972 | return m_comp_unit_sp; |
| 973 | return {}; |
| 974 | } |
| 975 | |
| 976 | size_t |
| 977 | SymbolFileCTF::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { |
| 978 | return ParseObjects(comp_unit&: *m_comp_unit_sp); |
| 979 | } |
| 980 | |
| 981 | void SymbolFileCTF::AddSymbols(Symtab &symtab) { |
| 982 | // CTF does not encode symbols. |
| 983 | // We rely on the existing symbol table to map symbols to type. |
| 984 | } |
| 985 | |
| 986 | lldb_private::Type *SymbolFileCTF::ResolveTypeUID(lldb::user_id_t type_uid) { |
| 987 | auto type_it = m_types.find(Val: type_uid); |
| 988 | if (type_it != m_types.end()) |
| 989 | return type_it->second.get(); |
| 990 | |
| 991 | auto ctf_type_it = m_ctf_types.find(Val: type_uid); |
| 992 | if (ctf_type_it == m_ctf_types.end()) |
| 993 | return nullptr; |
| 994 | |
| 995 | CTFType *ctf_type = ctf_type_it->second.get(); |
| 996 | assert(ctf_type && "m_ctf_types should only contain valid CTF types" ); |
| 997 | |
| 998 | Log *log = GetLog(mask: LLDBLog::Symbols); |
| 999 | |
| 1000 | llvm::Expected<TypeSP> type_or_error = CreateType(ctf_type); |
| 1001 | if (!type_or_error) { |
| 1002 | LLDB_LOG_ERROR(log, type_or_error.takeError(), |
| 1003 | "Failed to create type for {1}: {0}" , ctf_type->uid); |
| 1004 | return {}; |
| 1005 | } |
| 1006 | |
| 1007 | TypeSP type_sp = *type_or_error; |
| 1008 | |
| 1009 | if (log) { |
| 1010 | StreamString ss; |
| 1011 | type_sp->Dump(s: &ss, show_context: true); |
| 1012 | LLDB_LOGV(log, "Adding type {0}: {1}" , type_sp->GetID(), |
| 1013 | llvm::StringRef(ss.GetString()).rtrim()); |
| 1014 | } |
| 1015 | |
| 1016 | m_types[type_uid] = type_sp; |
| 1017 | |
| 1018 | // Except for record types which we'll need to complete later, we don't need |
| 1019 | // the CTF type anymore. |
| 1020 | if (!isa<CTFRecord>(Val: ctf_type)) |
| 1021 | m_ctf_types.erase(Val: type_uid); |
| 1022 | |
| 1023 | return type_sp.get(); |
| 1024 | } |
| 1025 | |
| 1026 | void SymbolFileCTF::FindTypes(const lldb_private::TypeQuery &match, |
| 1027 | lldb_private::TypeResults &results) { |
| 1028 | // Make sure we haven't already searched this SymbolFile before. |
| 1029 | if (results.AlreadySearched(sym_file: this)) |
| 1030 | return; |
| 1031 | |
| 1032 | ConstString name = match.GetTypeBasename(); |
| 1033 | for (TypeSP type_sp : GetTypeList().Types()) { |
| 1034 | if (type_sp && type_sp->GetName() == name) { |
| 1035 | results.InsertUnique(type_sp); |
| 1036 | if (results.Done(query: match)) |
| 1037 | return; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void SymbolFileCTF::FindTypesByRegex( |
| 1043 | const lldb_private::RegularExpression ®ex, uint32_t max_matches, |
| 1044 | lldb_private::TypeMap &types) { |
| 1045 | ParseTypes(cu&: *m_comp_unit_sp); |
| 1046 | |
| 1047 | size_t matches = 0; |
| 1048 | for (TypeSP type_sp : GetTypeList().Types()) { |
| 1049 | if (matches == max_matches) |
| 1050 | break; |
| 1051 | if (type_sp && regex.Execute(string: type_sp->GetName())) |
| 1052 | types.Insert(type: type_sp); |
| 1053 | matches++; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | void SymbolFileCTF::FindFunctions( |
| 1058 | const lldb_private::Module::LookupInfo &lookup_info, |
| 1059 | const lldb_private::CompilerDeclContext &parent_decl_ctx, |
| 1060 | bool include_inlines, lldb_private::SymbolContextList &sc_list) { |
| 1061 | ParseFunctions(cu&: *m_comp_unit_sp); |
| 1062 | |
| 1063 | ConstString name = lookup_info.GetLookupName(); |
| 1064 | for (FunctionSP function_sp : m_functions) { |
| 1065 | if (function_sp && function_sp->GetName() == name) { |
| 1066 | lldb_private::SymbolContext sc; |
| 1067 | sc.comp_unit = m_comp_unit_sp.get(); |
| 1068 | sc.function = function_sp.get(); |
| 1069 | sc_list.Append(sc); |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | void SymbolFileCTF::FindFunctions(const lldb_private::RegularExpression ®ex, |
| 1075 | bool include_inlines, |
| 1076 | lldb_private::SymbolContextList &sc_list) { |
| 1077 | for (FunctionSP function_sp : m_functions) { |
| 1078 | if (function_sp && regex.Execute(string: function_sp->GetName())) { |
| 1079 | lldb_private::SymbolContext sc; |
| 1080 | sc.comp_unit = m_comp_unit_sp.get(); |
| 1081 | sc.function = function_sp.get(); |
| 1082 | sc_list.Append(sc); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | void SymbolFileCTF::FindGlobalVariables( |
| 1088 | lldb_private::ConstString name, |
| 1089 | const lldb_private::CompilerDeclContext &parent_decl_ctx, |
| 1090 | uint32_t max_matches, lldb_private::VariableList &variables) { |
| 1091 | ParseObjects(comp_unit&: *m_comp_unit_sp); |
| 1092 | |
| 1093 | size_t matches = 0; |
| 1094 | for (VariableSP variable_sp : m_variables) { |
| 1095 | if (matches == max_matches) |
| 1096 | break; |
| 1097 | if (variable_sp && variable_sp->GetName() == name) { |
| 1098 | variables.AddVariable(var_sp: variable_sp); |
| 1099 | matches++; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void SymbolFileCTF::FindGlobalVariables( |
| 1105 | const lldb_private::RegularExpression ®ex, uint32_t max_matches, |
| 1106 | lldb_private::VariableList &variables) { |
| 1107 | ParseObjects(comp_unit&: *m_comp_unit_sp); |
| 1108 | |
| 1109 | size_t matches = 0; |
| 1110 | for (VariableSP variable_sp : m_variables) { |
| 1111 | if (matches == max_matches) |
| 1112 | break; |
| 1113 | if (variable_sp && regex.Execute(string: variable_sp->GetName())) { |
| 1114 | variables.AddVariable(var_sp: variable_sp); |
| 1115 | matches++; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |