| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1 | //===- lldb-test.cpp ------------------------------------------ *- C++ --*-===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "FormatUtil.h" |
| 10 | #include "SystemInitializerTest.h" |
| 11 | |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 12 | #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 13 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Debugger.h" |
| 15 | #include "lldb/Core/Module.h" |
| 16 | #include "lldb/Core/Section.h" |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 17 | #include "lldb/Expression/IRMemoryMap.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 18 | #include "lldb/Initialization/SystemLifetimeManager.h" |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 20 | #include "lldb/Interpreter/CommandReturnObject.h" |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/ClangASTContext.h" |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/CompileUnit.h" |
| 23 | #include "lldb/Symbol/LineTable.h" |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/SymbolFile.h" |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/TypeList.h" |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/TypeMap.h" |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/VariableList.h" |
| Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Language.h" |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 29 | #include "lldb/Target/Process.h" |
| 30 | #include "lldb/Target/Target.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 31 | #include "lldb/Utility/DataExtractor.h" |
| Pavel Labath | 62a8254 | 2018-12-15 13:45:38 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/State.h" |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 33 | #include "lldb/Utility/StreamString.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 34 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/IntervalMap.h" |
| Jonas Devlieghere | e0ea8d8 | 2019-09-10 00:20:50 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/ScopeExit.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringRef.h" |
| 38 | #include "llvm/Support/CommandLine.h" |
| 39 | #include "llvm/Support/ManagedStatic.h" |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Path.h" |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 42 | #include "llvm/Support/PrettyStackTrace.h" |
| 43 | #include "llvm/Support/Signals.h" |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 44 | #include "llvm/Support/WithColor.h" |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 45 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 46 | #include <cstdio> |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 47 | #include <thread> |
| 48 | |
| 49 | using namespace lldb; |
| 50 | using namespace lldb_private; |
| 51 | using namespace llvm; |
| 52 | |
| 53 | namespace opts { |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 54 | static cl::SubCommand BreakpointSubcommand("breakpoints", |
| 55 | "Test breakpoint resolution"); |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 56 | cl::SubCommand ObjectFileSubcommand("object-file", |
| 57 | "Display LLDB object file information"); |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 58 | cl::SubCommand SymbolsSubcommand("symbols", "Dump symbols for an object file"); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 59 | cl::SubCommand IRMemoryMapSubcommand("ir-memory-map", "Test IRMemoryMap"); |
| Vedant Kumar | 7e4c84a | 2018-05-31 22:09:00 +0000 | [diff] [blame] | 60 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 61 | cl::opt<std::string> Log("log", cl::desc("Path to a log file"), cl::init(""), |
| Vedant Kumar | 7e4c84a | 2018-05-31 22:09:00 +0000 | [diff] [blame] | 62 | cl::sub(BreakpointSubcommand), |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 63 | cl::sub(ObjectFileSubcommand), |
| 64 | cl::sub(SymbolsSubcommand), |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 65 | cl::sub(IRMemoryMapSubcommand)); |
| 66 | |
| 67 | /// Create a target using the file pointed to by \p Filename, or abort. |
| 68 | TargetSP createTarget(Debugger &Dbg, const std::string &Filename); |
| 69 | |
| 70 | /// Read \p Filename into a null-terminated buffer, or abort. |
| 71 | std::unique_ptr<MemoryBuffer> openFile(const std::string &Filename); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 72 | |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 73 | namespace breakpoint { |
| 74 | static cl::opt<std::string> Target(cl::Positional, cl::desc("<target>"), |
| 75 | cl::Required, cl::sub(BreakpointSubcommand)); |
| 76 | static cl::opt<std::string> CommandFile(cl::Positional, |
| 77 | cl::desc("<command-file>"), |
| 78 | cl::init("-"), |
| 79 | cl::sub(BreakpointSubcommand)); |
| 80 | static cl::opt<bool> Persistent( |
| 81 | "persistent", |
| 82 | cl::desc("Don't automatically remove all breakpoints before each command"), |
| 83 | cl::sub(BreakpointSubcommand)); |
| 84 | |
| 85 | static llvm::StringRef plural(uintmax_t value) { return value == 1 ? "" : "s"; } |
| 86 | static void dumpState(const BreakpointList &List, LinePrinter &P); |
| 87 | static std::string substitute(StringRef Cmd); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 88 | static int evaluateBreakpoints(Debugger &Dbg); |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 89 | } // namespace breakpoint |
| 90 | |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 91 | namespace object { |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 92 | cl::opt<bool> SectionContents("contents", |
| 93 | cl::desc("Dump each section's contents"), |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 94 | cl::sub(ObjectFileSubcommand)); |
| Aaron Smith | 037ed1b | 2018-12-06 21:36:39 +0000 | [diff] [blame] | 95 | cl::opt<bool> SectionDependentModules("dep-modules", |
| 96 | cl::desc("Dump each dependent module"), |
| 97 | cl::sub(ObjectFileSubcommand)); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 98 | cl::list<std::string> InputFilenames(cl::Positional, cl::desc("<input files>"), |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 99 | cl::OneOrMore, |
| 100 | cl::sub(ObjectFileSubcommand)); |
| 101 | } // namespace object |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 102 | |
| 103 | namespace symbols { |
| Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 104 | static cl::opt<std::string> InputFile(cl::Positional, cl::desc("<input file>"), |
| 105 | cl::Required, cl::sub(SymbolsSubcommand)); |
| 106 | |
| 107 | static cl::opt<std::string> |
| 108 | SymbolPath("symbol-file", |
| 109 | cl::desc("The file from which to fetch symbol information."), |
| 110 | cl::value_desc("file"), cl::sub(SymbolsSubcommand)); |
| 111 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 112 | enum class FindType { |
| 113 | None, |
| 114 | Function, |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 115 | Block, |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 116 | Namespace, |
| 117 | Type, |
| 118 | Variable, |
| 119 | }; |
| 120 | static cl::opt<FindType> Find( |
| 121 | "find", cl::desc("Choose search type:"), |
| 122 | cl::values( |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 123 | clEnumValN(FindType::None, "none", "No search, just dump the module."), |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 124 | clEnumValN(FindType::Function, "function", "Find functions."), |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 125 | clEnumValN(FindType::Block, "block", "Find blocks."), |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 126 | clEnumValN(FindType::Namespace, "namespace", "Find namespaces."), |
| 127 | clEnumValN(FindType::Type, "type", "Find types."), |
| 128 | clEnumValN(FindType::Variable, "variable", "Find global variables.")), |
| 129 | cl::sub(SymbolsSubcommand)); |
| 130 | |
| 131 | static cl::opt<std::string> Name("name", cl::desc("Name to find."), |
| 132 | cl::sub(SymbolsSubcommand)); |
| 133 | static cl::opt<bool> |
| 134 | Regex("regex", |
| 135 | cl::desc("Search using regular expressions (avaliable for variables " |
| 136 | "and functions only)."), |
| 137 | cl::sub(SymbolsSubcommand)); |
| 138 | static cl::opt<std::string> |
| 139 | Context("context", |
| 140 | cl::desc("Restrict search to the context of the given variable."), |
| 141 | cl::value_desc("variable"), cl::sub(SymbolsSubcommand)); |
| 142 | |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 143 | static cl::opt<std::string> CompilerContext( |
| 144 | "compiler-context", |
| 145 | cl::desc("Specify a compiler context as \"kind:name,...\"."), |
| 146 | cl::value_desc("context"), cl::sub(SymbolsSubcommand)); |
| 147 | |
| Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 148 | static cl::opt<std::string> |
| 149 | Language("language", cl::desc("Specify a language type, like C99."), |
| 150 | cl::value_desc("language"), cl::sub(SymbolsSubcommand)); |
| 151 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 152 | static cl::list<FunctionNameType> FunctionNameFlags( |
| 153 | "function-flags", cl::desc("Function search flags:"), |
| 154 | cl::values(clEnumValN(eFunctionNameTypeAuto, "auto", |
| 155 | "Automatically deduce flags based on name."), |
| 156 | clEnumValN(eFunctionNameTypeFull, "full", "Full function name."), |
| 157 | clEnumValN(eFunctionNameTypeBase, "base", "Base name."), |
| 158 | clEnumValN(eFunctionNameTypeMethod, "method", "Method name."), |
| 159 | clEnumValN(eFunctionNameTypeSelector, "selector", |
| 160 | "Selector name.")), |
| 161 | cl::sub(SymbolsSubcommand)); |
| 162 | static FunctionNameType getFunctionNameFlags() { |
| 163 | FunctionNameType Result = FunctionNameType(0); |
| 164 | for (FunctionNameType Flag : FunctionNameFlags) |
| 165 | Result = FunctionNameType(Result | Flag); |
| 166 | return Result; |
| 167 | } |
| 168 | |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 169 | static cl::opt<bool> DumpAST("dump-ast", |
| 170 | cl::desc("Dump AST restored from symbols."), |
| 171 | cl::sub(SymbolsSubcommand)); |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 172 | static cl::opt<bool> |
| 173 | DumpClangAST("dump-clang-ast", |
| 174 | cl::desc("Dump clang AST restored from symbols."), |
| 175 | cl::sub(SymbolsSubcommand)); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 176 | |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 177 | static cl::opt<bool> Verify("verify", cl::desc("Verify symbol information."), |
| 178 | cl::sub(SymbolsSubcommand)); |
| 179 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 180 | static cl::opt<std::string> File("file", |
| 181 | cl::desc("File (compile unit) to search."), |
| 182 | cl::sub(SymbolsSubcommand)); |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 183 | static cl::opt<int> Line("line", cl::desc("Line to search."), |
| 184 | cl::sub(SymbolsSubcommand)); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 185 | |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 186 | static Expected<CompilerDeclContext> getDeclContext(SymbolFile &Symfile); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 187 | |
| 188 | static Error findFunctions(lldb_private::Module &Module); |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 189 | static Error findBlocks(lldb_private::Module &Module); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 190 | static Error findNamespaces(lldb_private::Module &Module); |
| 191 | static Error findTypes(lldb_private::Module &Module); |
| 192 | static Error findVariables(lldb_private::Module &Module); |
| 193 | static Error dumpModule(lldb_private::Module &Module); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 194 | static Error dumpAST(lldb_private::Module &Module); |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 195 | static Error dumpClangAST(lldb_private::Module &Module); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 196 | static Error verify(lldb_private::Module &Module); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 197 | |
| Pavel Labath | 38d9ff7 | 2018-06-12 13:26:43 +0000 | [diff] [blame] | 198 | static Expected<Error (*)(lldb_private::Module &)> getAction(); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 199 | static int dumpSymbols(Debugger &Dbg); |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 200 | } // namespace symbols |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 201 | |
| 202 | namespace irmemorymap { |
| 203 | static cl::opt<std::string> Target(cl::Positional, cl::desc("<target>"), |
| 204 | cl::Required, |
| 205 | cl::sub(IRMemoryMapSubcommand)); |
| 206 | static cl::opt<std::string> CommandFile(cl::Positional, |
| 207 | cl::desc("<command-file>"), |
| 208 | cl::init("-"), |
| 209 | cl::sub(IRMemoryMapSubcommand)); |
| Vedant Kumar | f616b9d | 2018-05-31 22:09:00 +0000 | [diff] [blame] | 210 | static cl::opt<bool> UseHostOnlyAllocationPolicy( |
| 211 | "host-only", cl::desc("Use the host-only allocation policy"), |
| 212 | cl::init(false), cl::sub(IRMemoryMapSubcommand)); |
| 213 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 214 | using AllocationT = std::pair<addr_t, addr_t>; |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 215 | using AddrIntervalMap = |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 216 | IntervalMap<addr_t, unsigned, 8, IntervalMapHalfOpenInfo<addr_t>>; |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 217 | |
| 218 | struct IRMemoryMapTestState { |
| 219 | TargetSP Target; |
| 220 | IRMemoryMap Map; |
| 221 | |
| 222 | AddrIntervalMap::Allocator IntervalMapAllocator; |
| 223 | AddrIntervalMap Allocations; |
| 224 | |
| 225 | StringMap<addr_t> Label2AddrMap; |
| 226 | |
| 227 | IRMemoryMapTestState(TargetSP Target) |
| 228 | : Target(Target), Map(Target), Allocations(IntervalMapAllocator) {} |
| 229 | }; |
| 230 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 231 | bool evalMalloc(StringRef Line, IRMemoryMapTestState &State); |
| 232 | bool evalFree(StringRef Line, IRMemoryMapTestState &State); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 233 | int evaluateMemoryMapCommands(Debugger &Dbg); |
| 234 | } // namespace irmemorymap |
| 235 | |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 236 | } // namespace opts |
| 237 | |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 238 | std::vector<CompilerContext> parseCompilerContext() { |
| 239 | std::vector<CompilerContext> result; |
| 240 | if (opts::symbols::CompilerContext.empty()) |
| 241 | return result; |
| 242 | |
| 243 | StringRef str{opts::symbols::CompilerContext}; |
| 244 | SmallVector<StringRef, 8> entries_str; |
| 245 | str.split(entries_str, ',', /*maxSplit*/-1, /*keepEmpty=*/false); |
| 246 | for (auto entry_str : entries_str) { |
| 247 | StringRef key, value; |
| 248 | std::tie(key, value) = entry_str.split(':'); |
| 249 | auto kind = |
| 250 | StringSwitch<CompilerContextKind>(key) |
| 251 | .Case("TranslationUnit", CompilerContextKind::TranslationUnit) |
| 252 | .Case("Module", CompilerContextKind::Module) |
| 253 | .Case("Namespace", CompilerContextKind::Namespace) |
| 254 | .Case("Class", CompilerContextKind::Class) |
| Adrian Prantl | 330ae19a | 2019-08-21 18:06:56 +0000 | [diff] [blame] | 255 | .Case("Struct", CompilerContextKind::Struct) |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 256 | .Case("Union", CompilerContextKind::Union) |
| 257 | .Case("Function", CompilerContextKind::Function) |
| 258 | .Case("Variable", CompilerContextKind::Variable) |
| Adrian Prantl | 330ae19a | 2019-08-21 18:06:56 +0000 | [diff] [blame] | 259 | .Case("Enum", CompilerContextKind::Enum) |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 260 | .Case("Typedef", CompilerContextKind::Typedef) |
| Adrian Prantl | 330ae19a | 2019-08-21 18:06:56 +0000 | [diff] [blame] | 261 | .Case("AnyModule", CompilerContextKind::AnyModule) |
| 262 | .Case("AnyType", CompilerContextKind::AnyType) |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 263 | .Default(CompilerContextKind::Invalid); |
| 264 | if (value.empty()) { |
| 265 | WithColor::error() << "compiler context entry has no \"name\"\n"; |
| 266 | exit(1); |
| 267 | } |
| 268 | result.push_back({kind, ConstString{value}}); |
| 269 | } |
| 270 | outs() << "Search context: {\n"; |
| 271 | for (auto entry: result) |
| 272 | entry.Dump(); |
| 273 | outs() << "}\n"; |
| 274 | |
| 275 | return result; |
| 276 | } |
| 277 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 278 | template <typename... Args> |
| 279 | static Error make_string_error(const char *Format, Args &&... args) { |
| 280 | return llvm::make_error<llvm::StringError>( |
| 281 | llvm::formatv(Format, std::forward<Args>(args)...).str(), |
| 282 | llvm::inconvertibleErrorCode()); |
| 283 | } |
| 284 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 285 | TargetSP opts::createTarget(Debugger &Dbg, const std::string &Filename) { |
| 286 | TargetSP Target; |
| Jonas Devlieghere | f9a07e9 | 2018-09-20 09:09:05 +0000 | [diff] [blame] | 287 | Status ST = Dbg.GetTargetList().CreateTarget( |
| 288 | Dbg, Filename, /*triple*/ "", eLoadDependentsNo, |
| 289 | /*platform_options*/ nullptr, Target); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 290 | if (ST.Fail()) { |
| 291 | errs() << formatv("Failed to create target '{0}: {1}\n", Filename, ST); |
| 292 | exit(1); |
| 293 | } |
| 294 | return Target; |
| 295 | } |
| 296 | |
| 297 | std::unique_ptr<MemoryBuffer> opts::openFile(const std::string &Filename) { |
| 298 | auto MB = MemoryBuffer::getFileOrSTDIN(Filename); |
| 299 | if (!MB) { |
| 300 | errs() << formatv("Could not open file '{0}: {1}\n", Filename, |
| 301 | MB.getError().message()); |
| 302 | exit(1); |
| 303 | } |
| 304 | return std::move(*MB); |
| 305 | } |
| 306 | |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 307 | void opts::breakpoint::dumpState(const BreakpointList &List, LinePrinter &P) { |
| 308 | P.formatLine("{0} breakpoint{1}", List.GetSize(), plural(List.GetSize())); |
| 309 | if (List.GetSize() > 0) |
| 310 | P.formatLine("At least one breakpoint."); |
| 311 | for (size_t i = 0, e = List.GetSize(); i < e; ++i) { |
| 312 | BreakpointSP BP = List.GetBreakpointAtIndex(i); |
| 313 | P.formatLine("Breakpoint ID {0}:", BP->GetID()); |
| 314 | AutoIndent Indent(P, 2); |
| 315 | P.formatLine("{0} location{1}.", BP->GetNumLocations(), |
| 316 | plural(BP->GetNumLocations())); |
| 317 | if (BP->GetNumLocations() > 0) |
| 318 | P.formatLine("At least one location."); |
| 319 | P.formatLine("{0} resolved location{1}.", BP->GetNumResolvedLocations(), |
| 320 | plural(BP->GetNumResolvedLocations())); |
| 321 | if (BP->GetNumResolvedLocations() > 0) |
| 322 | P.formatLine("At least one resolved location."); |
| 323 | for (size_t l = 0, le = BP->GetNumLocations(); l < le; ++l) { |
| 324 | BreakpointLocationSP Loc = BP->GetLocationAtIndex(l); |
| 325 | P.formatLine("Location ID {0}:", Loc->GetID()); |
| 326 | AutoIndent Indent(P, 2); |
| 327 | P.formatLine("Enabled: {0}", Loc->IsEnabled()); |
| 328 | P.formatLine("Resolved: {0}", Loc->IsResolved()); |
| Jim Ingham | 0858126 | 2018-03-12 21:17:04 +0000 | [diff] [blame] | 329 | SymbolContext sc; |
| 330 | Loc->GetAddress().CalculateSymbolContext(&sc); |
| 331 | lldb_private::StreamString S; |
| 332 | sc.DumpStopContext(&S, BP->GetTarget().GetProcessSP().get(), |
| 333 | Loc->GetAddress(), false, true, false, true, true); |
| 334 | P.formatLine("Address: {0}", S.GetString()); |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | P.NewLine(); |
| 338 | } |
| 339 | |
| 340 | std::string opts::breakpoint::substitute(StringRef Cmd) { |
| 341 | std::string Result; |
| 342 | raw_string_ostream OS(Result); |
| 343 | while (!Cmd.empty()) { |
| 344 | switch (Cmd[0]) { |
| 345 | case '%': |
| 346 | if (Cmd.consume_front("%p") && (Cmd.empty() || !isalnum(Cmd[0]))) { |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 347 | OS << sys::path::parent_path(breakpoint::CommandFile); |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 348 | break; |
| 349 | } |
| Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 350 | LLVM_FALLTHROUGH; |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 351 | default: |
| 352 | size_t pos = Cmd.find('%'); |
| 353 | OS << Cmd.substr(0, pos); |
| 354 | Cmd = Cmd.substr(pos); |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | return std::move(OS.str()); |
| 359 | } |
| 360 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 361 | int opts::breakpoint::evaluateBreakpoints(Debugger &Dbg) { |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 362 | TargetSP Target = opts::createTarget(Dbg, breakpoint::Target); |
| 363 | std::unique_ptr<MemoryBuffer> MB = opts::openFile(breakpoint::CommandFile); |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 364 | |
| 365 | LinePrinter P(4, outs()); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 366 | StringRef Rest = MB->getBuffer(); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 367 | int HadErrors = 0; |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 368 | while (!Rest.empty()) { |
| 369 | StringRef Line; |
| 370 | std::tie(Line, Rest) = Rest.split('\n'); |
| Adrian McCarthy | 4447d15 | 2019-06-07 21:13:30 +0000 | [diff] [blame] | 371 | Line = Line.ltrim().rtrim(); |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 372 | if (Line.empty() || Line[0] == '#') |
| 373 | continue; |
| 374 | |
| 375 | if (!Persistent) |
| 376 | Target->RemoveAllBreakpoints(/*internal_also*/ true); |
| 377 | |
| 378 | std::string Command = substitute(Line); |
| 379 | P.formatLine("Command: {0}", Command); |
| 380 | CommandReturnObject Result; |
| 381 | if (!Dbg.GetCommandInterpreter().HandleCommand( |
| 382 | Command.c_str(), /*add_to_history*/ eLazyBoolNo, Result)) { |
| 383 | P.formatLine("Failed: {0}", Result.GetErrorData()); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 384 | HadErrors = 1; |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 385 | continue; |
| 386 | } |
| 387 | |
| 388 | dumpState(Target->GetBreakpointList(/*internal*/ false), P); |
| 389 | } |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 390 | return HadErrors; |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 393 | Expected<CompilerDeclContext> |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 394 | opts::symbols::getDeclContext(SymbolFile &Symfile) { |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 395 | if (Context.empty()) |
| 396 | return CompilerDeclContext(); |
| 397 | VariableList List; |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 398 | Symfile.FindGlobalVariables(ConstString(Context), nullptr, UINT32_MAX, List); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 399 | if (List.Empty()) |
| 400 | return make_string_error("Context search didn't find a match."); |
| 401 | if (List.GetSize() > 1) |
| 402 | return make_string_error("Context search found multiple matches."); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 403 | return List.GetVariableAtIndex(0)->GetDeclContext(); |
| 404 | } |
| 405 | |
| 406 | Error opts::symbols::findFunctions(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 407 | SymbolFile &Symfile = *Module.GetSymbolFile(); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 408 | SymbolContextList List; |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 409 | if (!File.empty()) { |
| 410 | assert(Line != 0); |
| 411 | |
| Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 412 | FileSpec src_file(File); |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 413 | size_t cu_count = Module.GetNumCompileUnits(); |
| 414 | for (size_t i = 0; i < cu_count; i++) { |
| 415 | lldb::CompUnitSP cu_sp = Module.GetCompileUnitAtIndex(i); |
| 416 | if (!cu_sp) |
| 417 | continue; |
| 418 | |
| 419 | LineEntry le; |
| 420 | cu_sp->FindLineEntry(0, Line, &src_file, false, &le); |
| 421 | if (!le.IsValid()) |
| 422 | continue; |
| Greg Clayton | 8a77792 | 2019-05-06 20:01:21 +0000 | [diff] [blame] | 423 | const bool include_inlined_functions = false; |
| 424 | auto addr = |
| 425 | le.GetSameLineContiguousAddressRange(include_inlined_functions) |
| 426 | .GetBaseAddress(); |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 427 | if (!addr.IsValid()) |
| 428 | continue; |
| 429 | |
| 430 | SymbolContext sc; |
| 431 | uint32_t resolved = |
| 432 | addr.CalculateSymbolContext(&sc, eSymbolContextFunction); |
| 433 | if (resolved & eSymbolContextFunction) |
| 434 | List.Append(sc); |
| 435 | } |
| 436 | } else if (Regex) { |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 437 | RegularExpression RE(Name); |
| 438 | assert(RE.IsValid()); |
| Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 439 | List.Clear(); |
| 440 | Symfile.FindFunctions(RE, true, List); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 441 | } else { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 442 | Expected<CompilerDeclContext> ContextOr = getDeclContext(Symfile); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 443 | if (!ContextOr) |
| 444 | return ContextOr.takeError(); |
| 445 | CompilerDeclContext *ContextPtr = |
| 446 | ContextOr->IsValid() ? &*ContextOr : nullptr; |
| 447 | |
| Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 448 | List.Clear(); |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 449 | Symfile.FindFunctions(ConstString(Name), ContextPtr, getFunctionNameFlags(), |
| Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 450 | true, List); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 451 | } |
| 452 | outs() << formatv("Found {0} functions:\n", List.GetSize()); |
| 453 | StreamString Stream; |
| 454 | List.Dump(&Stream, nullptr); |
| 455 | outs() << Stream.GetData() << "\n"; |
| 456 | return Error::success(); |
| 457 | } |
| 458 | |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 459 | Error opts::symbols::findBlocks(lldb_private::Module &Module) { |
| 460 | assert(!Regex); |
| 461 | assert(!File.empty()); |
| 462 | assert(Line != 0); |
| 463 | |
| 464 | SymbolContextList List; |
| 465 | |
| Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 466 | FileSpec src_file(File); |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 467 | size_t cu_count = Module.GetNumCompileUnits(); |
| 468 | for (size_t i = 0; i < cu_count; i++) { |
| 469 | lldb::CompUnitSP cu_sp = Module.GetCompileUnitAtIndex(i); |
| 470 | if (!cu_sp) |
| 471 | continue; |
| 472 | |
| 473 | LineEntry le; |
| 474 | cu_sp->FindLineEntry(0, Line, &src_file, false, &le); |
| 475 | if (!le.IsValid()) |
| 476 | continue; |
| Greg Clayton | 8a77792 | 2019-05-06 20:01:21 +0000 | [diff] [blame] | 477 | const bool include_inlined_functions = false; |
| 478 | auto addr = le.GetSameLineContiguousAddressRange(include_inlined_functions) |
| 479 | .GetBaseAddress(); |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 480 | if (!addr.IsValid()) |
| 481 | continue; |
| 482 | |
| 483 | SymbolContext sc; |
| 484 | uint32_t resolved = addr.CalculateSymbolContext(&sc, eSymbolContextBlock); |
| 485 | if (resolved & eSymbolContextBlock) |
| 486 | List.Append(sc); |
| 487 | } |
| 488 | |
| 489 | outs() << formatv("Found {0} blocks:\n", List.GetSize()); |
| 490 | StreamString Stream; |
| 491 | List.Dump(&Stream, nullptr); |
| 492 | outs() << Stream.GetData() << "\n"; |
| 493 | return Error::success(); |
| 494 | } |
| 495 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 496 | Error opts::symbols::findNamespaces(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 497 | SymbolFile &Symfile = *Module.GetSymbolFile(); |
| 498 | Expected<CompilerDeclContext> ContextOr = getDeclContext(Symfile); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 499 | if (!ContextOr) |
| 500 | return ContextOr.takeError(); |
| 501 | CompilerDeclContext *ContextPtr = |
| 502 | ContextOr->IsValid() ? &*ContextOr : nullptr; |
| 503 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 504 | CompilerDeclContext Result = |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 505 | Symfile.FindNamespace(ConstString(Name), ContextPtr); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 506 | if (Result) |
| 507 | outs() << "Found namespace: " |
| 508 | << Result.GetScopeQualifiedName().GetStringRef() << "\n"; |
| 509 | else |
| 510 | outs() << "Namespace not found.\n"; |
| 511 | return Error::success(); |
| 512 | } |
| 513 | |
| 514 | Error opts::symbols::findTypes(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 515 | SymbolFile &Symfile = *Module.GetSymbolFile(); |
| 516 | Expected<CompilerDeclContext> ContextOr = getDeclContext(Symfile); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 517 | if (!ContextOr) |
| 518 | return ContextOr.takeError(); |
| 519 | CompilerDeclContext *ContextPtr = |
| 520 | ContextOr->IsValid() ? &*ContextOr : nullptr; |
| 521 | |
| Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 522 | LanguageSet languages; |
| 523 | if (!Language.empty()) |
| 524 | languages.Insert(Language::GetLanguageTypeFromString(Language)); |
| 525 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 526 | DenseSet<SymbolFile *> SearchedFiles; |
| 527 | TypeMap Map; |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 528 | if (!Name.empty()) |
| Adrian Prantl | d4d428e | 2019-09-30 16:42:28 +0000 | [diff] [blame] | 529 | Symfile.FindTypes(ConstString(Name), ContextPtr, UINT32_MAX, SearchedFiles, |
| 530 | Map); |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 531 | else |
| Adrian Prantl | 3b73dcd | 2019-11-12 09:25:59 -0800 | [diff] [blame^] | 532 | Module.FindTypes(parseCompilerContext(), languages, SearchedFiles, Map); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 533 | |
| 534 | outs() << formatv("Found {0} types:\n", Map.GetSize()); |
| 535 | StreamString Stream; |
| 536 | Map.Dump(&Stream, false); |
| 537 | outs() << Stream.GetData() << "\n"; |
| 538 | return Error::success(); |
| 539 | } |
| 540 | |
| 541 | Error opts::symbols::findVariables(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 542 | SymbolFile &Symfile = *Module.GetSymbolFile(); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 543 | VariableList List; |
| 544 | if (Regex) { |
| 545 | RegularExpression RE(Name); |
| 546 | assert(RE.IsValid()); |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 547 | Symfile.FindGlobalVariables(RE, UINT32_MAX, List); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 548 | } else if (!File.empty()) { |
| 549 | CompUnitSP CU; |
| 550 | for (size_t Ind = 0; !CU && Ind < Module.GetNumCompileUnits(); ++Ind) { |
| 551 | CompUnitSP Candidate = Module.GetCompileUnitAtIndex(Ind); |
| 552 | if (!Candidate || Candidate->GetFilename().GetStringRef() != File) |
| 553 | continue; |
| 554 | if (CU) |
| 555 | return make_string_error("Multiple compile units for file `{0}` found.", |
| 556 | File); |
| 557 | CU = std::move(Candidate); |
| 558 | } |
| 559 | |
| 560 | if (!CU) |
| 561 | return make_string_error("Compile unit `{0}` not found.", File); |
| 562 | |
| 563 | List.AddVariables(CU->GetVariableList(true).get()); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 564 | } else { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 565 | Expected<CompilerDeclContext> ContextOr = getDeclContext(Symfile); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 566 | if (!ContextOr) |
| 567 | return ContextOr.takeError(); |
| 568 | CompilerDeclContext *ContextPtr = |
| 569 | ContextOr->IsValid() ? &*ContextOr : nullptr; |
| 570 | |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 571 | Symfile.FindGlobalVariables(ConstString(Name), ContextPtr, UINT32_MAX, List); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 572 | } |
| 573 | outs() << formatv("Found {0} variables:\n", List.GetSize()); |
| 574 | StreamString Stream; |
| 575 | List.Dump(&Stream, false); |
| 576 | outs() << Stream.GetData() << "\n"; |
| 577 | return Error::success(); |
| 578 | } |
| 579 | |
| 580 | Error opts::symbols::dumpModule(lldb_private::Module &Module) { |
| 581 | StreamString Stream; |
| 582 | Module.ParseAllDebugSymbols(); |
| 583 | Module.Dump(&Stream); |
| 584 | outs() << Stream.GetData() << "\n"; |
| 585 | return Error::success(); |
| 586 | } |
| 587 | |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 588 | Error opts::symbols::dumpAST(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 589 | Module.ParseAllDebugSymbols(); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 590 | |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 591 | SymbolFile *symfile = Module.GetSymbolFile(); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 592 | if (!symfile) |
| 593 | return make_string_error("Module has no symbol file."); |
| 594 | |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 595 | llvm::Expected<TypeSystem &> type_system_or_err = |
| Alex Langford | 0e252e3 | 2019-07-30 22:12:34 +0000 | [diff] [blame] | 596 | symfile->GetTypeSystemForLanguage(eLanguageTypeC_plus_plus); |
| 597 | if (!type_system_or_err) |
| 598 | return make_string_error("Can't retrieve ClangASTContext"); |
| 599 | |
| 600 | auto *clang_ast_ctx = |
| 601 | llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get()); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 602 | if (!clang_ast_ctx) |
| Alex Langford | 0e252e3 | 2019-07-30 22:12:34 +0000 | [diff] [blame] | 603 | return make_string_error("Retrieved TypeSystem was not a ClangASTContext"); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 604 | |
| 605 | auto ast_ctx = clang_ast_ctx->getASTContext(); |
| 606 | if (!ast_ctx) |
| 607 | return make_string_error("Can't retrieve AST context."); |
| 608 | |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 609 | clang::TranslationUnitDecl *tu = ast_ctx->getTranslationUnitDecl(); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 610 | if (!tu) |
| 611 | return make_string_error("Can't retrieve translation unit declaration."); |
| 612 | |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 613 | tu->print(outs()); |
| 614 | |
| 615 | return Error::success(); |
| 616 | } |
| 617 | |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 618 | Error opts::symbols::dumpClangAST(lldb_private::Module &Module) { |
| 619 | Module.ParseAllDebugSymbols(); |
| 620 | |
| 621 | SymbolFile *symfile = Module.GetSymbolFile(); |
| 622 | if (!symfile) |
| 623 | return make_string_error("Module has no symbol file."); |
| 624 | |
| 625 | llvm::Expected<TypeSystem &> type_system_or_err = |
| 626 | symfile->GetTypeSystemForLanguage(eLanguageTypeC_plus_plus); |
| 627 | if (!type_system_or_err) |
| 628 | return make_string_error("Can't retrieve ClangASTContext"); |
| 629 | |
| 630 | auto *clang_ast_ctx = |
| 631 | llvm::dyn_cast_or_null<ClangASTContext>(&type_system_or_err.get()); |
| 632 | if (!clang_ast_ctx) |
| 633 | return make_string_error("Retrieved TypeSystem was not a ClangASTContext"); |
| 634 | |
| 635 | StreamString Stream; |
| 636 | clang_ast_ctx->DumpFromSymbolFile(Stream, Name); |
| 637 | outs() << Stream.GetData() << "\n"; |
| 638 | |
| 639 | return Error::success(); |
| 640 | } |
| 641 | |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 642 | Error opts::symbols::verify(lldb_private::Module &Module) { |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 643 | SymbolFile *symfile = Module.GetSymbolFile(); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 644 | if (!symfile) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 645 | return make_string_error("Module has no symbol file."); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 646 | |
| 647 | uint32_t comp_units_count = symfile->GetNumCompileUnits(); |
| 648 | |
| 649 | outs() << "Found " << comp_units_count << " compile units.\n"; |
| 650 | |
| 651 | for (uint32_t i = 0; i < comp_units_count; i++) { |
| Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 652 | lldb::CompUnitSP comp_unit = symfile->GetCompileUnitAtIndex(i); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 653 | if (!comp_unit) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 654 | return make_string_error("Connot parse compile unit {0}.", i); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 655 | |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 656 | outs() << "Processing '" << comp_unit->GetFilename().AsCString() |
| 657 | << "' compile unit.\n"; |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 658 | |
| 659 | LineTable *lt = comp_unit->GetLineTable(); |
| 660 | if (!lt) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 661 | return make_string_error("Can't get a line table of a compile unit."); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 662 | |
| 663 | uint32_t count = lt->GetSize(); |
| 664 | |
| 665 | outs() << "The line table contains " << count << " entries.\n"; |
| 666 | |
| 667 | if (count == 0) |
| 668 | continue; |
| 669 | |
| 670 | LineEntry le; |
| 671 | if (!lt->GetLineEntryAtIndex(0, le)) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 672 | return make_string_error("Can't get a line entry of a compile unit."); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 673 | |
| 674 | for (uint32_t i = 1; i < count; i++) { |
| 675 | lldb::addr_t curr_end = |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 676 | le.range.GetBaseAddress().GetFileAddress() + le.range.GetByteSize(); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 677 | |
| 678 | if (!lt->GetLineEntryAtIndex(i, le)) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 679 | return make_string_error("Can't get a line entry of a compile unit"); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 680 | |
| 681 | if (curr_end > le.range.GetBaseAddress().GetFileAddress()) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 682 | return make_string_error( |
| 683 | "Line table of a compile unit is inconsistent."); |
| Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | outs() << "The symbol information is verified.\n"; |
| 688 | |
| 689 | return Error::success(); |
| 690 | } |
| 691 | |
| Pavel Labath | 38d9ff7 | 2018-06-12 13:26:43 +0000 | [diff] [blame] | 692 | Expected<Error (*)(lldb_private::Module &)> opts::symbols::getAction() { |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 693 | if (Verify && DumpAST) |
| 694 | return make_string_error( |
| 695 | "Cannot both verify symbol information and dump AST."); |
| 696 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 697 | if (Verify) { |
| 698 | if (Find != FindType::None) |
| 699 | return make_string_error( |
| 700 | "Cannot both search and verify symbol information."); |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 701 | if (Regex || !Context.empty() || !Name.empty() || !File.empty() || |
| 702 | Line != 0) |
| 703 | return make_string_error( |
| 704 | "-regex, -context, -name, -file and -line options are not " |
| 705 | "applicable for symbol verification."); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 706 | return verify; |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 709 | if (DumpAST) { |
| 710 | if (Find != FindType::None) |
| Aaron Smith | 037ed1b | 2018-12-06 21:36:39 +0000 | [diff] [blame] | 711 | return make_string_error("Cannot both search and dump AST."); |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 712 | if (Regex || !Context.empty() || !Name.empty() || !File.empty() || |
| 713 | Line != 0) |
| 714 | return make_string_error( |
| 715 | "-regex, -context, -name, -file and -line options are not " |
| 716 | "applicable for dumping AST."); |
| 717 | return dumpAST; |
| 718 | } |
| 719 | |
| Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 720 | if (DumpClangAST) { |
| 721 | if (Find != FindType::None) |
| 722 | return make_string_error("Cannot both search and dump clang AST."); |
| 723 | if (Regex || !Context.empty() || !File.empty() || Line != 0) |
| 724 | return make_string_error( |
| 725 | "-regex, -context, -name, -file and -line options are not " |
| 726 | "applicable for dumping clang AST."); |
| 727 | return dumpClangAST; |
| 728 | } |
| 729 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 730 | if (Regex && !Context.empty()) |
| 731 | return make_string_error( |
| 732 | "Cannot search using both regular expressions and context."); |
| 733 | |
| 734 | if (Regex && !RegularExpression(Name).IsValid()) |
| 735 | return make_string_error("`{0}` is not a valid regular expression.", Name); |
| 736 | |
| 737 | if (Regex + !Context.empty() + !File.empty() >= 2) |
| 738 | return make_string_error( |
| 739 | "Only one of -regex, -context and -file may be used simultaneously."); |
| 740 | if (Regex && Name.empty()) |
| 741 | return make_string_error("-regex used without a -name"); |
| 742 | |
| 743 | switch (Find) { |
| 744 | case FindType::None: |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 745 | if (!Context.empty() || !Name.empty() || !File.empty() || Line != 0) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 746 | return make_string_error( |
| 747 | "Specify search type (-find) to use search options."); |
| 748 | return dumpModule; |
| 749 | |
| 750 | case FindType::Function: |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 751 | if (!File.empty() + (Line != 0) == 1) |
| 752 | return make_string_error("Both file name and line number must be " |
| 753 | "specified when searching a function " |
| 754 | "by file position."); |
| 755 | if (Regex + (getFunctionNameFlags() != 0) + !File.empty() >= 2) |
| 756 | return make_string_error("Only one of regular expression, function-flags " |
| 757 | "and file position may be used simultaneously " |
| 758 | "when searching a function."); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 759 | return findFunctions; |
| 760 | |
| Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 761 | case FindType::Block: |
| 762 | if (File.empty() || Line == 0) |
| 763 | return make_string_error("Both file name and line number must be " |
| 764 | "specified when searching a block."); |
| 765 | if (Regex || getFunctionNameFlags() != 0) |
| 766 | return make_string_error("Cannot use regular expression or " |
| 767 | "function-flags for searching a block."); |
| 768 | return findBlocks; |
| 769 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 770 | case FindType::Namespace: |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 771 | if (Regex || !File.empty() || Line != 0) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 772 | return make_string_error("Cannot search for namespaces using regular " |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 773 | "expressions, file names or line numbers."); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 774 | return findNamespaces; |
| 775 | |
| 776 | case FindType::Type: |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 777 | if (Regex || !File.empty() || Line != 0) |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 778 | return make_string_error("Cannot search for types using regular " |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 779 | "expressions, file names or line numbers."); |
| Adrian Prantl | 260aa0f | 2019-08-20 16:44:25 +0000 | [diff] [blame] | 780 | if (!Name.empty() && !CompilerContext.empty()) |
| 781 | return make_string_error("Name is ignored if compiler context present."); |
| 782 | |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 783 | return findTypes; |
| 784 | |
| 785 | case FindType::Variable: |
| Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 786 | if (Line != 0) |
| 787 | return make_string_error("Cannot search for variables " |
| 788 | "using line numbers."); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 789 | return findVariables; |
| 790 | } |
| Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 791 | |
| 792 | llvm_unreachable("Unsupported symbol action."); |
| Pavel Labath | e6954cb | 2018-06-12 12:57:36 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | int opts::symbols::dumpSymbols(Debugger &Dbg) { |
| 796 | auto ActionOr = getAction(); |
| 797 | if (!ActionOr) { |
| 798 | logAllUnhandledErrors(ActionOr.takeError(), WithColor::error(), ""); |
| 799 | return 1; |
| 800 | } |
| 801 | auto Action = *ActionOr; |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 802 | |
| Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 803 | outs() << "Module: " << InputFile << "\n"; |
| 804 | ModuleSpec Spec{FileSpec(InputFile)}; |
| 805 | StringRef Symbols = SymbolPath.empty() ? InputFile : SymbolPath; |
| 806 | Spec.GetSymbolFileSpec().SetFile(Symbols, FileSpec::Style::native); |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 807 | |
| Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 808 | auto ModulePtr = std::make_shared<lldb_private::Module>(Spec); |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 809 | SymbolFile *Symfile = ModulePtr->GetSymbolFile(); |
| 810 | if (!Symfile) { |
| Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 811 | WithColor::error() << "Module has no symbol vendor.\n"; |
| 812 | return 1; |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 813 | } |
| Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 814 | |
| 815 | if (Error E = Action(*ModulePtr)) { |
| 816 | WithColor::error() << toString(std::move(E)) << "\n"; |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | return 0; |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| Pavel Labath | 280d2e8 | 2018-12-12 12:35:25 +0000 | [diff] [blame] | 823 | static void dumpSectionList(LinePrinter &Printer, const SectionList &List, bool is_subsection) { |
| 824 | size_t Count = List.GetNumSections(0); |
| 825 | if (Count == 0) { |
| 826 | Printer.formatLine("There are no {0}sections", is_subsection ? "sub" : ""); |
| 827 | return; |
| 828 | } |
| 829 | Printer.formatLine("Showing {0} {1}sections", Count, |
| 830 | is_subsection ? "sub" : ""); |
| 831 | for (size_t I = 0; I < Count; ++I) { |
| 832 | auto S = List.GetSectionAtIndex(I); |
| 833 | assert(S); |
| 834 | AutoIndent Indent(Printer, 2); |
| 835 | Printer.formatLine("Index: {0}", I); |
| Pavel Labath | 0d38e4f | 2018-12-18 15:56:45 +0000 | [diff] [blame] | 836 | Printer.formatLine("ID: {0:x}", S->GetID()); |
| Pavel Labath | 280d2e8 | 2018-12-12 12:35:25 +0000 | [diff] [blame] | 837 | Printer.formatLine("Name: {0}", S->GetName().GetStringRef()); |
| 838 | Printer.formatLine("Type: {0}", S->GetTypeAsCString()); |
| Pavel Labath | 62a8254 | 2018-12-15 13:45:38 +0000 | [diff] [blame] | 839 | Printer.formatLine("Permissions: {0}", GetPermissionsAsCString(S->GetPermissions())); |
| Pavel Labath | ef8683a | 2018-12-12 15:46:18 +0000 | [diff] [blame] | 840 | Printer.formatLine("Thread specific: {0:y}", S->IsThreadSpecific()); |
| Pavel Labath | 62a8254 | 2018-12-15 13:45:38 +0000 | [diff] [blame] | 841 | Printer.formatLine("VM address: {0:x}", S->GetFileAddress()); |
| Pavel Labath | 280d2e8 | 2018-12-12 12:35:25 +0000 | [diff] [blame] | 842 | Printer.formatLine("VM size: {0}", S->GetByteSize()); |
| 843 | Printer.formatLine("File size: {0}", S->GetFileSize()); |
| 844 | |
| 845 | if (opts::object::SectionContents) { |
| Jonas Devlieghere | f4af9a9 | 2019-07-11 20:26:53 +0000 | [diff] [blame] | 846 | lldb_private::DataExtractor Data; |
| Pavel Labath | 280d2e8 | 2018-12-12 12:35:25 +0000 | [diff] [blame] | 847 | S->GetSectionData(Data); |
| 848 | ArrayRef<uint8_t> Bytes = {Data.GetDataStart(), Data.GetDataEnd()}; |
| 849 | Printer.formatBinary("Data: ", Bytes, 0); |
| 850 | } |
| 851 | |
| 852 | if (S->GetType() == eSectionTypeContainer) |
| 853 | dumpSectionList(Printer, S->GetChildren(), true); |
| 854 | Printer.NewLine(); |
| 855 | } |
| 856 | } |
| 857 | |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 858 | static int dumpObjectFiles(Debugger &Dbg) { |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 859 | LinePrinter Printer(4, llvm::outs()); |
| 860 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 861 | int HadErrors = 0; |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 862 | for (const auto &File : opts::object::InputFilenames) { |
| Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 863 | ModuleSpec Spec{FileSpec(File)}; |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 864 | |
| Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 865 | auto ModulePtr = std::make_shared<lldb_private::Module>(Spec); |
| Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 866 | |
| 867 | ObjectFile *ObjectPtr = ModulePtr->GetObjectFile(); |
| 868 | if (!ObjectPtr) { |
| 869 | WithColor::error() << File << " not recognised as an object file\n"; |
| 870 | HadErrors = 1; |
| 871 | continue; |
| 872 | } |
| 873 | |
| Pavel Labath | 96979ce | 2018-03-06 15:56:20 +0000 | [diff] [blame] | 874 | // Fetch symbol vendor before we get the section list to give the symbol |
| 875 | // vendor a chance to populate it. |
| Pavel Labath | 465eae3 | 2019-08-06 09:12:42 +0000 | [diff] [blame] | 876 | ModulePtr->GetSymbolFile(); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 877 | SectionList *Sections = ModulePtr->GetSectionList(); |
| 878 | if (!Sections) { |
| 879 | llvm::errs() << "Could not load sections for module " << File << "\n"; |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 880 | HadErrors = 1; |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 881 | continue; |
| 882 | } |
| 883 | |
| Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 884 | Printer.formatLine("Plugin name: {0}", ObjectPtr->GetPluginName()); |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 885 | Printer.formatLine("Architecture: {0}", |
| 886 | ModulePtr->GetArchitecture().GetTriple().getTriple()); |
| 887 | Printer.formatLine("UUID: {0}", ModulePtr->GetUUID().GetAsString()); |
| Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 888 | Printer.formatLine("Executable: {0}", ObjectPtr->IsExecutable()); |
| 889 | Printer.formatLine("Stripped: {0}", ObjectPtr->IsStripped()); |
| 890 | Printer.formatLine("Type: {0}", ObjectPtr->GetType()); |
| 891 | Printer.formatLine("Strata: {0}", ObjectPtr->GetStrata()); |
| Pavel Labath | 976af43 | 2019-01-10 09:32:31 +0000 | [diff] [blame] | 892 | Printer.formatLine("Base VM address: {0:x}", |
| 893 | ObjectPtr->GetBaseAddress().GetFileAddress()); |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 894 | |
| Pavel Labath | 280d2e8 | 2018-12-12 12:35:25 +0000 | [diff] [blame] | 895 | dumpSectionList(Printer, *Sections, /*is_subsection*/ false); |
| Aaron Smith | 037ed1b | 2018-12-06 21:36:39 +0000 | [diff] [blame] | 896 | |
| 897 | if (opts::object::SectionDependentModules) { |
| 898 | // A non-empty section list ensures a valid object file. |
| 899 | auto Obj = ModulePtr->GetObjectFile(); |
| 900 | FileSpecList Files; |
| 901 | auto Count = Obj->GetDependentModules(Files); |
| 902 | Printer.formatLine("Showing {0} dependent module(s)", Count); |
| 903 | for (size_t I = 0; I < Files.GetSize(); ++I) { |
| 904 | AutoIndent Indent(Printer, 2); |
| 905 | Printer.formatLine("Name: {0}", |
| 906 | Files.GetFileSpecAtIndex(I).GetCString()); |
| 907 | } |
| 908 | Printer.NewLine(); |
| 909 | } |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 910 | } |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 911 | return HadErrors; |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 914 | bool opts::irmemorymap::evalMalloc(StringRef Line, |
| 915 | IRMemoryMapTestState &State) { |
| 916 | // ::= <label> = malloc <size> <alignment> |
| 917 | StringRef Label; |
| 918 | std::tie(Label, Line) = Line.split('='); |
| 919 | if (Line.empty()) |
| 920 | return false; |
| 921 | Label = Label.trim(); |
| 922 | Line = Line.trim(); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 923 | size_t Size; |
| 924 | uint8_t Alignment; |
| 925 | int Matches = sscanf(Line.data(), "malloc %zu %hhu", &Size, &Alignment); |
| 926 | if (Matches != 2) |
| 927 | return false; |
| 928 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 929 | outs() << formatv("Command: {0} = malloc(size={1}, alignment={2})\n", Label, |
| 930 | Size, Alignment); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 931 | if (!isPowerOf2_32(Alignment)) { |
| 932 | outs() << "Malloc error: alignment is not a power of 2\n"; |
| 933 | exit(1); |
| 934 | } |
| 935 | |
| Vedant Kumar | f616b9d | 2018-05-31 22:09:00 +0000 | [diff] [blame] | 936 | IRMemoryMap::AllocationPolicy AP = |
| 937 | UseHostOnlyAllocationPolicy ? IRMemoryMap::eAllocationPolicyHostOnly |
| 938 | : IRMemoryMap::eAllocationPolicyProcessOnly; |
| 939 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 940 | // Issue the malloc in the target process with "-rw" permissions. |
| 941 | const uint32_t Permissions = 0x3; |
| 942 | const bool ZeroMemory = false; |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 943 | Status ST; |
| 944 | addr_t Addr = |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 945 | State.Map.Malloc(Size, Alignment, Permissions, AP, ZeroMemory, ST); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 946 | if (ST.Fail()) { |
| 947 | outs() << formatv("Malloc error: {0}\n", ST); |
| 948 | return true; |
| 949 | } |
| 950 | |
| 951 | // Print the result of the allocation before checking its validity. |
| 952 | outs() << formatv("Malloc: address = {0:x}\n", Addr); |
| 953 | |
| 954 | // Check that the allocation is aligned. |
| 955 | if (!Addr || Addr % Alignment != 0) { |
| 956 | outs() << "Malloc error: zero or unaligned allocation detected\n"; |
| 957 | exit(1); |
| 958 | } |
| 959 | |
| Pavel Labath | a352ed2 | 2018-12-27 09:32:04 +0000 | [diff] [blame] | 960 | // In case of Size == 0, we still expect the returned address to be unique and |
| 961 | // non-overlapping. |
| 962 | addr_t EndOfRegion = Addr + std::max<size_t>(Size, 1); |
| 963 | if (State.Allocations.overlaps(Addr, EndOfRegion)) { |
| 964 | auto I = State.Allocations.find(Addr); |
| 965 | outs() << "Malloc error: overlapping allocation detected" |
| 966 | << formatv(", previous allocation at [{0:x}, {1:x})\n", I.start(), |
| 967 | I.stop()); |
| 968 | exit(1); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| Aaron Smith | 037ed1b | 2018-12-06 21:36:39 +0000 | [diff] [blame] | 971 | // Insert the new allocation into the interval map. Use unique allocation |
| 972 | // IDs to inhibit interval coalescing. |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 973 | static unsigned AllocationID = 0; |
| Pavel Labath | a352ed2 | 2018-12-27 09:32:04 +0000 | [diff] [blame] | 974 | State.Allocations.insert(Addr, EndOfRegion, AllocationID++); |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 975 | |
| 976 | // Store the label -> address mapping. |
| 977 | State.Label2AddrMap[Label] = Addr; |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 978 | |
| 979 | return true; |
| 980 | } |
| 981 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 982 | bool opts::irmemorymap::evalFree(StringRef Line, IRMemoryMapTestState &State) { |
| 983 | // ::= free <label> |
| 984 | if (!Line.consume_front("free")) |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 985 | return false; |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 986 | StringRef Label = Line.trim(); |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 987 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 988 | outs() << formatv("Command: free({0})\n", Label); |
| 989 | auto LabelIt = State.Label2AddrMap.find(Label); |
| 990 | if (LabelIt == State.Label2AddrMap.end()) { |
| 991 | outs() << "Free error: Invalid allocation label\n"; |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 992 | exit(1); |
| 993 | } |
| 994 | |
| 995 | Status ST; |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 996 | addr_t Addr = LabelIt->getValue(); |
| 997 | State.Map.Free(Addr, ST); |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 998 | if (ST.Fail()) { |
| 999 | outs() << formatv("Free error: {0}\n", ST); |
| 1000 | exit(1); |
| 1001 | } |
| 1002 | |
| 1003 | // Erase the allocation from the live interval map. |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 1004 | auto Interval = State.Allocations.find(Addr); |
| 1005 | if (Interval != State.Allocations.end()) { |
| 1006 | outs() << formatv("Free: [{0:x}, {1:x})\n", Interval.start(), |
| 1007 | Interval.stop()); |
| 1008 | Interval.erase(); |
| 1009 | } |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1010 | |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
| 1014 | int opts::irmemorymap::evaluateMemoryMapCommands(Debugger &Dbg) { |
| 1015 | // Set up a Target. |
| 1016 | TargetSP Target = opts::createTarget(Dbg, irmemorymap::Target); |
| 1017 | |
| 1018 | // Set up a Process. In order to allocate memory within a target, this |
| 1019 | // process must be alive and must support JIT'ing. |
| 1020 | CommandReturnObject Result; |
| 1021 | Dbg.SetAsyncExecution(false); |
| 1022 | CommandInterpreter &CI = Dbg.GetCommandInterpreter(); |
| 1023 | auto IssueCmd = [&](const char *Cmd) -> bool { |
| 1024 | return CI.HandleCommand(Cmd, eLazyBoolNo, Result); |
| 1025 | }; |
| 1026 | if (!IssueCmd("b main") || !IssueCmd("run")) { |
| 1027 | outs() << formatv("Failed: {0}\n", Result.GetErrorData()); |
| 1028 | exit(1); |
| 1029 | } |
| 1030 | |
| 1031 | ProcessSP Process = Target->GetProcessSP(); |
| 1032 | if (!Process || !Process->IsAlive() || !Process->CanJIT()) { |
| 1033 | outs() << "Cannot use process to test IRMemoryMap\n"; |
| 1034 | exit(1); |
| 1035 | } |
| 1036 | |
| 1037 | // Set up an IRMemoryMap and associated testing state. |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 1038 | IRMemoryMapTestState State(Target); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Parse and apply commands from the command file. |
| 1041 | std::unique_ptr<MemoryBuffer> MB = opts::openFile(irmemorymap::CommandFile); |
| 1042 | StringRef Rest = MB->getBuffer(); |
| 1043 | while (!Rest.empty()) { |
| 1044 | StringRef Line; |
| 1045 | std::tie(Line, Rest) = Rest.split('\n'); |
| Adrian McCarthy | 4447d15 | 2019-06-07 21:13:30 +0000 | [diff] [blame] | 1046 | Line = Line.ltrim().rtrim(); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (Line.empty() || Line[0] == '#') |
| 1049 | continue; |
| 1050 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 1051 | if (evalMalloc(Line, State)) |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1052 | continue; |
| 1053 | |
| Vedant Kumar | c418b5c | 2018-06-04 17:11:15 +0000 | [diff] [blame] | 1054 | if (evalFree(Line, State)) |
| Vedant Kumar | cc5a616 | 2018-05-31 22:09:01 +0000 | [diff] [blame] | 1055 | continue; |
| 1056 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1057 | errs() << "Could not parse line: " << Line << "\n"; |
| 1058 | exit(1); |
| 1059 | } |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1063 | int main(int argc, const char *argv[]) { |
| 1064 | StringRef ToolName = argv[0]; |
| 1065 | sys::PrintStackTraceOnErrorSignal(ToolName); |
| 1066 | PrettyStackTraceProgram X(argc, argv); |
| 1067 | llvm_shutdown_obj Y; |
| 1068 | |
| 1069 | cl::ParseCommandLineOptions(argc, argv, "LLDB Testing Utility\n"); |
| 1070 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 1071 | SystemLifetimeManager DebuggerLifetime; |
| Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 1072 | if (auto e = DebuggerLifetime.Initialize( |
| Jonas Devlieghere | a8f3ae7 | 2019-08-14 22:19:23 +0000 | [diff] [blame] | 1073 | std::make_unique<SystemInitializerTest>(), nullptr)) { |
| Jonas Devlieghere | 15eacd7 | 2018-12-03 17:28:29 +0000 | [diff] [blame] | 1074 | WithColor::error() << "initialization failed: " << toString(std::move(e)) |
| 1075 | << '\n'; |
| 1076 | return 1; |
| 1077 | } |
| 1078 | |
| Jonas Devlieghere | e0ea8d8 | 2019-09-10 00:20:50 +0000 | [diff] [blame] | 1079 | auto TerminateDebugger = |
| 1080 | llvm::make_scope_exit([&] { DebuggerLifetime.Terminate(); }); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1081 | |
| 1082 | auto Dbg = lldb_private::Debugger::CreateInstance(); |
| Jan Kratochvil | 8c82c41 | 2019-06-17 14:46:17 +0000 | [diff] [blame] | 1083 | ModuleList::GetGlobalModuleListProperties().SetEnableExternalLookup(false); |
| Jan Kratochvil | 2b38951 | 2019-07-29 16:10:16 +0000 | [diff] [blame] | 1084 | CommandReturnObject Result; |
| 1085 | Dbg->GetCommandInterpreter().HandleCommand( |
| 1086 | "settings set plugin.process.gdb-remote.packet-timeout 60", |
| 1087 | /*add_to_history*/ eLazyBoolNo, Result); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1088 | |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1089 | if (!opts::Log.empty()) |
| 1090 | Dbg->EnableLog("lldb", {"all"}, opts::Log, 0, errs()); |
| 1091 | |
| Pavel Labath | 7c94582 | 2018-02-26 18:50:16 +0000 | [diff] [blame] | 1092 | if (opts::BreakpointSubcommand) |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 1093 | return opts::breakpoint::evaluateBreakpoints(*Dbg); |
| Pavel Labath | 62a7f80 | 2018-06-29 12:15:54 +0000 | [diff] [blame] | 1094 | if (opts::ObjectFileSubcommand) |
| 1095 | return dumpObjectFiles(*Dbg); |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 1096 | if (opts::SymbolsSubcommand) |
| 1097 | return opts::symbols::dumpSymbols(*Dbg); |
| Vedant Kumar | c1cd826 | 2018-05-30 19:39:10 +0000 | [diff] [blame] | 1098 | if (opts::IRMemoryMapSubcommand) |
| 1099 | return opts::irmemorymap::evaluateMemoryMapCommands(*Dbg); |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1100 | |
| Pavel Labath | 90b0a53 | 2018-05-03 10:57:16 +0000 | [diff] [blame] | 1101 | WithColor::error() << "No command specified.\n"; |
| 1102 | return 1; |
| Zachary Turner | 888a428 | 2017-12-01 00:52:51 +0000 | [diff] [blame] | 1103 | } |