Index: lldb/include/lldb/Symbol/TypeSystem.h =================================================================== --- lldb/include/lldb/Symbol/TypeSystem.h +++ lldb/include/lldb/Symbol/TypeSystem.h @@ -392,6 +392,12 @@ lldb::opaque_compiler_type_t type, Stream *s, lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0; + /// Dump a textual representation of the internal TypeSystem state to the + /// given stream. + /// + /// This should not modify the state of the TypeSystem if possible. + virtual void Dump(llvm::raw_ostream &output) = 0; + // TODO: These methods appear unused. Should they be removed? virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0; Index: lldb/source/Commands/CommandObjectTarget.cpp =================================================================== --- lldb/source/Commands/CommandObjectTarget.cpp +++ lldb/source/Commands/CommandObjectTarget.cpp @@ -4990,6 +4990,55 @@ ~CommandObjectMultiwordTargetStopHooks() override = default; }; +#pragma mark CommandObjectTargetDumpTypesystem + +/// Dumps the TypeSystem of the selected Target. +class CommandObjectTargetDumpTypesystem : public CommandObjectParsed { +public: + CommandObjectTargetDumpTypesystem(CommandInterpreter &interpreter) + : CommandObjectParsed( + interpreter, "target dump typesystem", + "Dump the state of the target's internal type system.\n" + "Intended to be used for debugging LLDB itself.", + nullptr, eCommandRequiresTarget) {} + + ~CommandObjectTargetDumpTypesystem() override = default; + +protected: + bool DoExecute(Args &command, CommandReturnObject &result) override { + if (!command.empty()) { + result.AppendError("target dump typesystem doesn't take arguments."); + return result.Succeeded(); + } + + // Go over every scratch TypeSystem and dump to the command output. + for (TypeSystem *ts : GetSelectedTarget().GetScratchTypeSystems()) + ts->Dump(result.GetOutputStream().AsRawOstream()); + + result.SetStatus(eReturnStatusSuccessFinishResult); + return result.Succeeded(); + } +}; + +#pragma mark CommandObjectTargetDump + +/// Multi-word command for 'target dump'. +class CommandObjectTargetDump : public CommandObjectMultiword { +public: + // Constructors and Destructors + CommandObjectTargetDump(CommandInterpreter &interpreter) + : CommandObjectMultiword( + interpreter, "target dump", + "Commands for dumping information about the target.", + "target dump [typesystem]") { + LoadSubCommand( + "typesystem", + CommandObjectSP(new CommandObjectTargetDumpTypesystem(interpreter))); + } + + ~CommandObjectTargetDump() override = default; +}; + #pragma mark CommandObjectMultiwordTarget // CommandObjectMultiwordTarget @@ -5003,6 +5052,8 @@ CommandObjectSP(new CommandObjectTargetCreate(interpreter))); LoadSubCommand("delete", CommandObjectSP(new CommandObjectTargetDelete(interpreter))); + LoadSubCommand("dump", + CommandObjectSP(new CommandObjectTargetDump(interpreter))); LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetList(interpreter))); LoadSubCommand("select", Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -3846,7 +3846,7 @@ llvm::dyn_cast_or_null(&ts_or_err.get()); if (!clang) return; - clang->Dump(s); + clang->Dump(s.AsRawOstream()); } SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() { Index: lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp +++ lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp @@ -1358,4 +1358,6 @@ return static_cast(context.GetOpaqueDeclContext()); } -void PdbAstBuilder::Dump(Stream &stream) { m_clang.Dump(stream); } +void PdbAstBuilder::Dump(Stream &stream) { + m_clang.Dump(stream.AsRawOstream()); +} Index: lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp +++ lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp @@ -1461,7 +1461,7 @@ llvm::dyn_cast_or_null(&type_system_or_err.get()); if (!clang_type_system) return; - clang_type_system->Dump(s); + clang_type_system->Dump(s.AsRawOstream()); } void SymbolFilePDB::FindTypesByRegex( Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h =================================================================== --- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -938,7 +938,8 @@ LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override; #endif - void Dump(Stream &s); + /// \see lldb_private::TypeSystem::Dump + void Dump(llvm::raw_ostream &output) override; /// Dump clang AST types from the symbol file. /// @@ -1162,6 +1163,9 @@ return GetForTarget(target, InferIsolatedASTKindFromLangOpts(lang_opts)); } + /// \see lldb_private::TypeSystem::Dump + void Dump(llvm::raw_ostream &output) override; + UserExpression * GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp =================================================================== --- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -8358,9 +8358,8 @@ } #endif -void TypeSystemClang::Dump(Stream &s) { - Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl()); - tu->dump(s.AsRawOstream()); +void TypeSystemClang::Dump(llvm::raw_ostream &output) { + GetTranslationUnitDecl()->dump(output); } void TypeSystemClang::DumpFromSymbolFile(Stream &s, @@ -9746,6 +9745,41 @@ return &scratch_ast.GetIsolatedAST(*ast_kind); } +/// Returns a human-readable name that uniquely identifiers the sub-AST kind. +static llvm::StringRef +GetNameForIsolatedASTKind(ScratchTypeSystemClang::IsolatedASTKind kind) { + switch (kind) { + case ScratchTypeSystemClang::IsolatedASTKind::CppModules: + return "C++ modules"; + } + llvm_unreachable("Unimplemented IsolatedASTKind?"); +} + +void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) { + // First dump the main scratch AST. + output << "State of scratch Clang type system:\n"; + TypeSystemClang::Dump(output); + + // Now sort the isolated sub-ASTs. + typedef std::pair KeyAndTS; + std::vector sorted_typesystems; + for (const auto &a : m_isolated_asts) + sorted_typesystems.emplace_back(a.first, a.second.get()); + llvm::stable_sort(sorted_typesystems, + [](const KeyAndTS &lhs, const KeyAndTS &rhs) { + return lhs.first < rhs.first; + }); + + // Dump each sub-AST too. + for (const auto &a : sorted_typesystems) { + IsolatedASTKind kind = + static_cast(a.first); + output << "State of scratch Clang type subsystem " + << GetNameForIsolatedASTKind(kind) << ":\n"; + a.second->Dump(output); + } +} + UserExpression *ScratchTypeSystemClang::GetUserExpression( llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, Expression::ResultType desired_type, Index: lldb/test/API/commands/target/dump/Makefile =================================================================== --- /dev/null +++ lldb/test/API/commands/target/dump/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules Index: lldb/test/API/commands/target/dump/TestTargetDumpTypeSystem.py =================================================================== --- /dev/null +++ lldb/test/API/commands/target/dump/TestTargetDumpTypeSystem.py @@ -0,0 +1,33 @@ +import lldb +from lldbsuite.test.lldbtest import * +from lldbsuite.test.decorators import * +import lldbsuite.test.lldbutil as lldbutil + +class TestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + @no_debug_info_test + def test_dumping(self): + """ Tests dumping an empty and non-empty scratch AST. """ + self.build() + self.createTestTarget() + + # Make sure DummyStruct is not in the scratch AST by default. + self.expect("target dump typesystem", matching=False, substrs=["struct DummyStruct"]) + + # Move DummyStruct into the scratch AST via the expression evaluator. + # FIXME: Once there is an SB API for using variable paths on globals + # then this should be done this way. + self.expect_expr("s", result_type="DummyStruct") + + # Dump the scratch AST and make sure DummyStruct is in there. + self.expect("target dump typesystem", substrs=["struct DummyStruct"]) + + @no_debug_info_test + def test_invalid_arg(self): + """ Test an invalid invocation on 'target dump typesystem'. """ + self.build() + self.createTestTarget() + self.expect("target dump typesystem arg", error=True, + substrs=["error: target dump typesystem doesn't take arguments."]) Index: lldb/test/API/commands/target/dump/main.cpp =================================================================== --- /dev/null +++ lldb/test/API/commands/target/dump/main.cpp @@ -0,0 +1,7 @@ +struct DummyStruct { + int i; +}; + +DummyStruct s; + +int main() { return s.i; }