Author: zturner Date: Mon Nov 5 09:40:28 2018 New Revision: 346149 URL: http://llvm.org/viewvc/llvm-project?rev=346149&view=rev Log: Add a target modules dump ast command.
This is useful for investigating the clang ast as you reconstruct it via by parsing debug info. It can also be used to write tests against. Differential Revision: https://reviews.llvm.org/D54072 Modified: lldb/trunk/include/lldb/Symbol/ClangASTContext.h lldb/trunk/include/lldb/Symbol/SymbolFile.h lldb/trunk/source/Commands/CommandObjectTarget.cpp lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h lldb/trunk/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h lldb/trunk/source/Symbol/ClangASTContext.cpp Modified: lldb/trunk/include/lldb/Symbol/ClangASTContext.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/ClangASTContext.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/include/lldb/Symbol/ClangASTContext.h (original) +++ lldb/trunk/include/lldb/Symbol/ClangASTContext.h Mon Nov 5 09:40:28 2018 @@ -194,9 +194,10 @@ public: uint32_t GetPointerByteSize() override; - static clang::DeclContext *GetTranslationUnitDecl(clang::ASTContext *ast); + static clang::TranslationUnitDecl * + GetTranslationUnitDecl(clang::ASTContext *ast); - clang::DeclContext *GetTranslationUnitDecl() { + clang::TranslationUnitDecl *GetTranslationUnitDecl() { return GetTranslationUnitDecl(getASTContext()); } @@ -926,6 +927,8 @@ public: //---------------------------------------------------------------------- // Dumping types //---------------------------------------------------------------------- + void Dump(Stream &s); + void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, Modified: lldb/trunk/include/lldb/Symbol/SymbolFile.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/SymbolFile.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/include/lldb/Symbol/SymbolFile.h (original) +++ lldb/trunk/include/lldb/Symbol/SymbolFile.h Mon Nov 5 09:40:28 2018 @@ -161,6 +161,8 @@ public: uint32_t line, bool check_inlines, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); + + virtual void DumpClangAST(Stream &s) {} virtual uint32_t FindGlobalVariables(const ConstString &name, const CompilerDeclContext *parent_decl_ctx, Modified: lldb/trunk/source/Commands/CommandObjectTarget.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectTarget.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Commands/CommandObjectTarget.cpp (original) +++ lldb/trunk/source/Commands/CommandObjectTarget.cpp Mon Nov 5 09:40:28 2018 @@ -2227,6 +2227,85 @@ protected: } }; +#pragma mark CommandObjectTargetModulesDumpSections + +//---------------------------------------------------------------------- +// Clang AST dumping command +//---------------------------------------------------------------------- + +class CommandObjectTargetModulesDumpClangAST + : public CommandObjectTargetModulesModuleAutoComplete { +public: + CommandObjectTargetModulesDumpClangAST(CommandInterpreter &interpreter) + : CommandObjectTargetModulesModuleAutoComplete( + interpreter, "target modules dump ast", + "Dump the clang ast for a given module's symbol file.", + //"target modules dump ast [<file1> ...]") + nullptr) {} + + ~CommandObjectTargetModulesDumpClangAST() override = default; + +protected: + bool DoExecute(Args &command, CommandReturnObject &result) override { + Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); + if (target == nullptr) { + result.AppendError("invalid target, create a debug target using the " + "'target create' command"); + result.SetStatus(eReturnStatusFailed); + return false; + } + + const size_t num_modules = target->GetImages().GetSize(); + if (num_modules == 0) { + result.AppendError("the target has no associated executable images"); + result.SetStatus(eReturnStatusFailed); + return false; + } + + if (command.GetArgumentCount() == 0) { + // Dump all ASTs for all modules images + result.GetOutputStream().Printf("Dumping clang ast for %" PRIu64 + " modules.\n", + (uint64_t)num_modules); + for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { + if (m_interpreter.WasInterrupted()) + break; + Module *m = target->GetImages().GetModulePointerAtIndex(image_idx); + SymbolFile *sf = m->GetSymbolVendor()->GetSymbolFile(); + sf->DumpClangAST(result.GetOutputStream()); + } + result.SetStatus(eReturnStatusSuccessFinishResult); + return true; + } + + // Dump specified ASTs (by basename or fullpath) + for (const Args::ArgEntry &arg : command.entries()) { + ModuleList module_list; + const size_t num_matches = + FindModulesByName(target, arg.c_str(), module_list, true); + if (num_matches == 0) { + // Check the global list + std::lock_guard<std::recursive_mutex> guard( + Module::GetAllocationModuleCollectionMutex()); + + result.AppendWarningWithFormat( + "Unable to find an image that matches '%s'.\n", arg.c_str()); + continue; + } + + for (size_t i = 0; i < num_matches; ++i) { + if (m_interpreter.WasInterrupted()) + break; + Module *m = module_list.GetModulePointerAtIndex(i); + SymbolFile *sf = m->GetSymbolVendor()->GetSymbolFile(); + sf->DumpClangAST(result.GetOutputStream()); + } + } + result.SetStatus(eReturnStatusSuccessFinishResult); + return true; + } +}; + #pragma mark CommandObjectTargetModulesDumpSymfile //---------------------------------------------------------------------- @@ -2402,12 +2481,13 @@ public: // Constructors and Destructors //------------------------------------------------------------------ CommandObjectTargetModulesDump(CommandInterpreter &interpreter) - : CommandObjectMultiword(interpreter, "target modules dump", - "Commands for dumping information about one or " - "more target modules.", - "target modules dump " - "[headers|symtab|sections|symfile|line-table] " - "[<file1> <file2> ...]") { + : CommandObjectMultiword( + interpreter, "target modules dump", + "Commands for dumping information about one or " + "more target modules.", + "target modules dump " + "[headers|symtab|sections|ast|symfile|line-table] " + "[<file1> <file2> ...]") { LoadSubCommand("objfile", CommandObjectSP( new CommandObjectTargetModulesDumpObjfile(interpreter))); @@ -2420,6 +2500,9 @@ public: LoadSubCommand("symfile", CommandObjectSP( new CommandObjectTargetModulesDumpSymfile(interpreter))); + LoadSubCommand( + "ast", CommandObjectSP( + new CommandObjectTargetModulesDumpClangAST(interpreter))); LoadSubCommand("line-table", CommandObjectSP(new CommandObjectTargetModulesDumpLineTable( interpreter))); Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp Mon Nov 5 09:40:28 2018 @@ -3854,6 +3854,14 @@ uint32_t SymbolFileDWARF::GetPluginVersi void SymbolFileDWARF::Dump(lldb_private::Stream &s) { m_index->Dump(s); } +void SymbolFileDWARF::DumpClangAST(Stream &s) { + TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus); + ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts); + if (!clang) + return; + clang->Dump(s); +} + SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() { if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) { lldb::ModuleSP module_sp(m_debug_map_module_wp.lock()); Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h Mon Nov 5 09:40:28 2018 @@ -329,6 +329,8 @@ public: void Dump(lldb_private::Stream &s) override; + void DumpClangAST(lldb_private::Stream &s) override; + protected: typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *> DIEToTypePtr; Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp Mon Nov 5 09:40:28 2018 @@ -1227,6 +1227,13 @@ CompilerDeclContext SymbolFileDWARFDebug return matching_namespace; } +void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) { + ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool { + oso_dwarf->DumpClangAST(s); + return true; + }); +} + //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h Mon Nov 5 09:40:28 2018 @@ -126,6 +126,8 @@ public: std::vector<lldb_private::CallEdge> ParseCallEdgesInFunction(lldb_private::UserID func_id) override; + void DumpClangAST(lldb_private::Stream &s) override; + //------------------------------------------------------------------ // PluginInterface protocol //------------------------------------------------------------------ Modified: lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp Mon Nov 5 09:40:28 2018 @@ -1391,6 +1391,12 @@ size_t SymbolFileNativePDB::ParseFunctio return 0; } +void SymbolFileNativePDB::DumpClangAST(Stream &s) { + if (!m_clang) + return; + m_clang->Dump(s); +} + uint32_t SymbolFileNativePDB::FindGlobalVariables( const ConstString &name, const CompilerDeclContext *parent_decl_ctx, uint32_t max_matches, VariableList &variables) { Modified: lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h Mon Nov 5 09:40:28 2018 @@ -155,6 +155,8 @@ public: ClangASTContext &GetASTContext() { return *m_clang; } ClangASTImporter &GetASTImporter() { return *m_importer; } + void DumpClangAST(Stream &s) override; + private: size_t FindTypesByName(llvm::StringRef name, uint32_t max_matches, TypeMap &types); Modified: lldb/trunk/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp Mon Nov 5 09:40:28 2018 @@ -967,7 +967,7 @@ clang::DeclContext *PDBASTParser::GetDec return child_context; // Split context and retrieve nested namespaces - auto curr_context = m_ast.GetTranslationUnitDecl(); + clang::DeclContext *curr_context = m_ast.GetTranslationUnitDecl(); std::string::size_type from = 0; while (from < context_size) { auto to = context.find("::", from); Modified: lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp Mon Nov 5 09:40:28 2018 @@ -1356,6 +1356,14 @@ uint32_t SymbolFilePDB::FindTypes( return types.GetSize(); } +void SymbolFilePDB::DumpClangAST(Stream &s) { + auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); + auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system); + if (!clang) + return; + clang->Dump(s); +} + void SymbolFilePDB::FindTypesByRegex( const lldb_private::RegularExpression ®ex, uint32_t max_matches, lldb_private::TypeMap &types) { Modified: lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h Mon Nov 5 09:40:28 2018 @@ -169,6 +169,8 @@ public: const llvm::pdb::IPDBSession &GetPDBSession() const; + void DumpClangAST(lldb_private::Stream &s) override; + private: struct SecContribInfo { uint32_t Offset; Modified: lldb/trunk/source/Symbol/ClangASTContext.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTContext.cpp?rev=346149&r1=346148&r2=346149&view=diff ============================================================================== --- lldb/trunk/source/Symbol/ClangASTContext.cpp (original) +++ lldb/trunk/source/Symbol/ClangASTContext.cpp Mon Nov 5 09:40:28 2018 @@ -1285,7 +1285,7 @@ CompilerType ClangASTContext::GetCString return CompilerType(ast, ast->getPointerType(char_type)); } -clang::DeclContext * +clang::TranslationUnitDecl * ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) { return ast->getTranslationUnitDecl(); } @@ -8965,6 +8965,11 @@ ClangASTContext::ConvertStringToFloatVal //---------------------------------------------------------------------- #define DEPTH_INCREMENT 2 +void ClangASTContext::Dump(Stream &s) { + TranslationUnitDecl *tu = GetTranslationUnitDecl(); + tu->dump(s.AsRawOstream()); +} + void ClangASTContext::DumpValue( lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, lldb::Format format, const DataExtractor &data, _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits