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