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