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