blob: bb1b3f8ae7da8aa066693e9571e669c921607b05 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectTarget.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 "CommandObjectTarget.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000014#include "lldb/Core/IOHandler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleSpec.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000017#include "lldb/Core/Section.h"
Greg Clayton7260f622011-04-18 08:33:37 +000018#include "lldb/Core/State.h"
Greg Clayton644247c2011-07-07 01:59:51 +000019#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000020#include "lldb/DataFormatters/ValueObjectPrinter.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000021#include "lldb/Host/OptionParser.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000022#include "lldb/Host/StringConvert.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000023#include "lldb/Host/Symbols.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton7260f622011-04-18 08:33:37 +000027#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000028#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000029#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000030#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton7260f622011-04-18 08:33:37 +000031#include "lldb/Interpreter/OptionGroupPlatform.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/Interpreter/OptionGroupString.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000033#include "lldb/Interpreter/OptionGroupUInt64.h"
34#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton644247c2011-07-07 01:59:51 +000035#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "lldb/Interpreter/OptionGroupVariable.h"
37#include "lldb/Interpreter/Options.h"
Greg Clayton1f746072012-08-29 21:13:06 +000038#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000039#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000040#include "lldb/Symbol/LineTable.h"
41#include "lldb/Symbol/ObjectFile.h"
42#include "lldb/Symbol/SymbolFile.h"
43#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000044#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000045#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000046#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000048#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000049#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000051#include "lldb/Target/ThreadSpec.h"
Pavel Labath38d06322017-06-29 14:32:17 +000052#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
Zachary Turner7d86ee52017-03-08 17:56:08 +000054#include "llvm/Support/FileSystem.h"
55
Pavel Labath7e2cfbf2016-11-09 09:59:18 +000056// C Includes
57// C++ Includes
58#include <cerrno>
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060using namespace lldb;
61using namespace lldb_private;
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063static void DumpTargetInfo(uint32_t target_idx, Target *target,
64 const char *prefix_cstr,
65 bool show_stopped_process_status, Stream &strm) {
66 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 Module *exe_module = target->GetExecutableModulePointer();
69 char exe_path[PATH_MAX];
70 bool exe_valid = false;
71 if (exe_module)
72 exe_valid = exe_module->GetFileSpec().GetPath(exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 if (!exe_valid)
75 ::strcpy(exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 strm.Printf("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx,
78 exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 uint32_t properties = 0;
81 if (target_arch.IsValid()) {
82 strm.Printf("%sarch=", properties++ > 0 ? ", " : " ( ");
83 target_arch.DumpTriple(strm);
84 properties++;
85 }
86 PlatformSP platform_sp(target->GetPlatform());
87 if (platform_sp)
88 strm.Printf("%splatform=%s", properties++ > 0 ? ", " : " ( ",
89 platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 ProcessSP process_sp(target->GetProcessSP());
92 bool show_process_status = false;
93 if (process_sp) {
94 lldb::pid_t pid = process_sp->GetID();
95 StateType state = process_sp->GetState();
96 if (show_stopped_process_status)
97 show_process_status = StateIsStoppedState(state, true);
98 const char *state_cstr = StateAsCString(state);
99 if (pid != LLDB_INVALID_PROCESS_ID)
100 strm.Printf("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
101 strm.Printf("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
102 }
103 if (properties > 0)
104 strm.PutCString(" )\n");
105 else
106 strm.EOL();
107 if (show_process_status) {
108 const bool only_threads_with_stop_reason = true;
109 const uint32_t start_frame = 0;
110 const uint32_t num_frames = 1;
111 const uint32_t num_frames_with_source = 1;
Jim Ingham6a9767c2016-11-08 20:36:40 +0000112 const bool stop_format = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 process_sp->GetStatus(strm);
114 process_sp->GetThreadStatus(strm, only_threads_with_stop_reason,
115 start_frame, num_frames,
Jim Ingham6a9767c2016-11-08 20:36:40 +0000116 num_frames_with_source, stop_format);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 }
Greg Clayton7260f622011-04-18 08:33:37 +0000118}
119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120static uint32_t DumpTargetList(TargetList &target_list,
121 bool show_stopped_process_status, Stream &strm) {
122 const uint32_t num_targets = target_list.GetNumTargets();
123 if (num_targets) {
124 TargetSP selected_target_sp(target_list.GetSelectedTarget());
125 strm.PutCString("Current targets:\n");
126 for (uint32_t i = 0; i < num_targets; ++i) {
127 TargetSP target_sp(target_list.GetTargetAtIndex(i));
128 if (target_sp) {
129 bool is_selected = target_sp.get() == selected_target_sp.get();
130 DumpTargetInfo(i, target_sp.get(), is_selected ? "* " : " ",
131 show_stopped_process_status, strm);
132 }
Greg Clayton7260f622011-04-18 08:33:37 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
135 return num_targets;
Greg Clayton7260f622011-04-18 08:33:37 +0000136}
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000137
Pavel Labath8cb1cd92016-11-16 10:54:22 +0000138// TODO: Remove this once llvm can pretty-print time points
139static void DumpTimePoint(llvm::sys::TimePoint<> tp, Stream &s, uint32_t width) {
140#ifndef LLDB_DISABLE_POSIX
141 char time_buf[32];
142 time_t time = llvm::sys::toTimeT(tp);
143 char *time_cstr = ::ctime_r(&time, time_buf);
144 if (time_cstr) {
145 char *newline = ::strpbrk(time_cstr, "\n\r");
146 if (newline)
147 *newline = '\0';
148 if (width > 0)
149 s.Printf("%-*s", width, time_cstr);
150 else
151 s.PutCString(time_cstr);
152 } else if (width > 0)
153 s.Printf("%-*s", width, "");
154#endif
155}
156
Greg Clayton7260f622011-04-18 08:33:37 +0000157#pragma mark CommandObjectTargetCreate
158
159//-------------------------------------------------------------------------
160// "target create"
161//-------------------------------------------------------------------------
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163class CommandObjectTargetCreate : public CommandObjectParsed {
Greg Clayton7260f622011-04-18 08:33:37 +0000164public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 CommandObjectTargetCreate(CommandInterpreter &interpreter)
166 : CommandObjectParsed(
167 interpreter, "target create",
168 "Create a target using the argument as the main executable.",
169 nullptr),
170 m_option_group(), m_arch_option(),
171 m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename,
172 "Fullpath to a core file to use for this target."),
173 m_platform_path(LLDB_OPT_SET_1, false, "platform-path", 'P', 0,
174 eArgTypePath,
175 "Path to the remote file to use for this target."),
176 m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0,
177 eArgTypeFilename, "Fullpath to a stand alone debug "
178 "symbols file for when debug symbols "
179 "are not in the executable."),
180 m_remote_file(
181 LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename,
182 "Fullpath to the file on the remote host if debugging remotely."),
183 m_add_dependents(LLDB_OPT_SET_1, false, "no-dependents", 'd',
184 "Don't load dependent files when creating the target, "
185 "just add the specified executable.",
186 true, true) {
187 CommandArgumentEntry arg;
188 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 // Define the first (and only) variant of this arg.
191 file_arg.arg_type = eArgTypeFilename;
192 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 // There is only one variant this argument could be; put it into the
195 // argument entry.
196 arg.push_back(file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 // Push the data for the first argument into the m_arguments vector.
199 m_arguments.push_back(arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
202 m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
203 m_option_group.Append(&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
204 m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
205 m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
206 m_option_group.Append(&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
207 m_option_group.Finalize();
208 }
Greg Clayton7260f622011-04-18 08:33:37 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 ~CommandObjectTargetCreate() override = default;
Greg Clayton7260f622011-04-18 08:33:37 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 Options *GetOptions() override { return &m_option_group; }
Greg Clayton7260f622011-04-18 08:33:37 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 int HandleArgumentCompletion(Args &input, int &cursor_index,
215 int &cursor_char_position,
216 OptionElementVector &opt_element_vector,
217 int match_start_point, int max_return_elements,
218 bool &word_complete,
219 StringList &matches) override {
220 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
221 completion_str.erase(cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 CommandCompletions::InvokeCommonCompletionCallbacks(
224 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
225 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
226 word_complete, matches);
227 return matches.GetSize();
228 }
Jim Ingham5a988412012-06-08 21:56:10 +0000229
230protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 bool DoExecute(Args &command, CommandReturnObject &result) override {
232 const size_t argc = command.GetArgumentCount();
233 FileSpec core_file(m_core_file.GetOptionValue().GetCurrentValue());
234 FileSpec remote_file(m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (core_file) {
237 if (!core_file.Exists()) {
238 result.AppendErrorWithFormat("core file '%s' doesn't exist",
239 core_file.GetPath().c_str());
240 result.SetStatus(eReturnStatusFailed);
241 return false;
242 }
243 if (!core_file.Readable()) {
244 result.AppendErrorWithFormat("core file '%s' is not readable",
245 core_file.GetPath().c_str());
246 result.SetStatus(eReturnStatusFailed);
247 return false;
248 }
Greg Clayton7260f622011-04-18 08:33:37 +0000249 }
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (argc == 1 || core_file || remote_file) {
252 FileSpec symfile(m_symbol_file.GetOptionValue().GetCurrentValue());
253 if (symfile) {
254 if (symfile.Exists()) {
255 if (!symfile.Readable()) {
256 result.AppendErrorWithFormat("symbol file '%s' is not readable",
257 symfile.GetPath().c_str());
258 result.SetStatus(eReturnStatusFailed);
259 return false;
260 }
261 } else {
262 char symfile_path[PATH_MAX];
263 symfile.GetPath(symfile_path, sizeof(symfile_path));
264 result.AppendErrorWithFormat("invalid symbol file path '%s'",
265 symfile_path);
266 result.SetStatus(eReturnStatusFailed);
267 return false;
268 }
269 }
270
271 const char *file_path = command.GetArgumentAtIndex(0);
Pavel Labathf9d16472017-05-15 13:02:37 +0000272 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
273 Timer scoped_timer(func_cat, "(lldb) target create '%s'", file_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 FileSpec file_spec;
275
276 if (file_path)
277 file_spec.SetFile(file_path, true);
278
279 bool must_set_platform_path = false;
280
281 Debugger &debugger = m_interpreter.GetDebugger();
282
283 TargetSP target_sp;
Zachary Turnera47464b2016-11-18 20:44:46 +0000284 llvm::StringRef arch_cstr = m_arch_option.GetArchitectureName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 const bool get_dependent_files =
286 m_add_dependents.GetOptionValue().GetCurrentValue();
Zachary Turner97206d52017-05-12 04:51:55 +0000287 Status error(debugger.GetTargetList().CreateTarget(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 debugger, file_path, arch_cstr, get_dependent_files, nullptr,
289 target_sp));
290
291 if (target_sp) {
292 // Only get the platform after we create the target because we might
293 // have
294 // switched platforms depending on what the arguments were to
295 // CreateTarget()
296 // we can't rely on the selected platform.
297
298 PlatformSP platform_sp = target_sp->GetPlatform();
299
300 if (remote_file) {
301 if (platform_sp) {
302 // I have a remote file.. two possible cases
303 if (file_spec && file_spec.Exists()) {
304 // if the remote file does not exist, push it there
305 if (!platform_sp->GetFileExists(remote_file)) {
Zachary Turner97206d52017-05-12 04:51:55 +0000306 Status err = platform_sp->PutFile(file_spec, remote_file);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 if (err.Fail()) {
308 result.AppendError(err.AsCString());
309 result.SetStatus(eReturnStatusFailed);
310 return false;
311 }
312 }
313 } else {
314 // there is no local file and we need one
315 // in order to make the remote ---> local transfer we need a
316 // platform
317 // TODO: if the user has passed in a --platform argument, use it
318 // to fetch the right platform
319 if (!platform_sp) {
320 result.AppendError(
321 "unable to perform remote debugging without a platform");
322 result.SetStatus(eReturnStatusFailed);
323 return false;
324 }
325 if (file_path) {
326 // copy the remote file to the local file
Zachary Turner97206d52017-05-12 04:51:55 +0000327 Status err = platform_sp->GetFile(remote_file, file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 if (err.Fail()) {
329 result.AppendError(err.AsCString());
330 result.SetStatus(eReturnStatusFailed);
331 return false;
332 }
333 } else {
334 // make up a local file
335 result.AppendError("remote --> local transfer without local "
336 "path is not implemented yet");
337 result.SetStatus(eReturnStatusFailed);
338 return false;
339 }
340 }
341 } else {
342 result.AppendError("no platform found for target");
343 result.SetStatus(eReturnStatusFailed);
344 return false;
345 }
346 }
347
348 if (symfile || remote_file) {
349 ModuleSP module_sp(target_sp->GetExecutableModule());
350 if (module_sp) {
351 if (symfile)
352 module_sp->SetSymbolFileFileSpec(symfile);
353 if (remote_file) {
354 std::string remote_path = remote_file.GetPath();
355 target_sp->SetArg0(remote_path.c_str());
356 module_sp->SetPlatformFileSpec(remote_file);
357 }
358 }
359 }
360
361 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
362 if (must_set_platform_path) {
363 ModuleSpec main_module_spec(file_spec);
364 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
365 if (module_sp)
366 module_sp->SetPlatformFileSpec(remote_file);
367 }
368 if (core_file) {
369 char core_path[PATH_MAX];
370 core_file.GetPath(core_path, sizeof(core_path));
371 if (core_file.Exists()) {
372 if (!core_file.Readable()) {
373 result.AppendMessageWithFormat(
374 "Core file '%s' is not readable.\n", core_path);
375 result.SetStatus(eReturnStatusFailed);
376 return false;
377 }
378 FileSpec core_file_dir;
379 core_file_dir.GetDirectory() = core_file.GetDirectory();
380 target_sp->GetExecutableSearchPaths().Append(core_file_dir);
381
382 ProcessSP process_sp(target_sp->CreateProcess(
Zachary Turner31659452016-11-17 21:15:14 +0000383 m_interpreter.GetDebugger().GetListener(), llvm::StringRef(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 &core_file));
385
386 if (process_sp) {
387 // Seems weird that we Launch a core file, but that is
388 // what we do!
389 error = process_sp->LoadCore();
390
391 if (error.Fail()) {
392 result.AppendError(
393 error.AsCString("can't find plug-in for core file"));
394 result.SetStatus(eReturnStatusFailed);
395 return false;
396 } else {
397 result.AppendMessageWithFormat(
398 "Core file '%s' (%s) was loaded.\n", core_path,
399 target_sp->GetArchitecture().GetArchitectureName());
400 result.SetStatus(eReturnStatusSuccessFinishNoResult);
401 }
402 } else {
403 result.AppendErrorWithFormat(
404 "Unable to find process plug-in for core file '%s'\n",
405 core_path);
406 result.SetStatus(eReturnStatusFailed);
407 }
408 } else {
409 result.AppendErrorWithFormat("Core file '%s' does not exist\n",
410 core_path);
411 result.SetStatus(eReturnStatusFailed);
412 }
413 } else {
414 result.AppendMessageWithFormat(
415 "Current executable set to '%s' (%s).\n", file_path,
416 target_sp->GetArchitecture().GetArchitectureName());
417 result.SetStatus(eReturnStatusSuccessFinishNoResult);
418 }
419 } else {
420 result.AppendError(error.AsCString());
421 result.SetStatus(eReturnStatusFailed);
422 }
423 } else {
424 result.AppendErrorWithFormat("'%s' takes exactly one executable path "
425 "argument, or use the --core option.\n",
426 m_cmd_name.c_str());
427 result.SetStatus(eReturnStatusFailed);
428 }
429 return result.Succeeded();
430 }
431
Greg Clayton7260f622011-04-18 08:33:37 +0000432private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 OptionGroupOptions m_option_group;
434 OptionGroupArchitecture m_arch_option;
435 OptionGroupFile m_core_file;
436 OptionGroupFile m_platform_path;
437 OptionGroupFile m_symbol_file;
438 OptionGroupFile m_remote_file;
439 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000440};
441
442#pragma mark CommandObjectTargetList
443
444//----------------------------------------------------------------------
445// "target list"
446//----------------------------------------------------------------------
447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448class CommandObjectTargetList : public CommandObjectParsed {
Greg Clayton7260f622011-04-18 08:33:37 +0000449public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 CommandObjectTargetList(CommandInterpreter &interpreter)
451 : CommandObjectParsed(
452 interpreter, "target list",
453 "List all current targets in the current debug session.", nullptr) {
454 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 ~CommandObjectTargetList() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000457
Jim Ingham5a988412012-06-08 21:56:10 +0000458protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 bool DoExecute(Args &args, CommandReturnObject &result) override {
460 if (args.GetArgumentCount() == 0) {
461 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 bool show_stopped_process_status = false;
464 if (DumpTargetList(m_interpreter.GetDebugger().GetTargetList(),
465 show_stopped_process_status, strm) == 0) {
466 strm.PutCString("No targets.\n");
467 }
468 result.SetStatus(eReturnStatusSuccessFinishResult);
469 } else {
470 result.AppendError("the 'target list' command takes no arguments\n");
471 result.SetStatus(eReturnStatusFailed);
Greg Clayton7260f622011-04-18 08:33:37 +0000472 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 return result.Succeeded();
474 }
Greg Clayton7260f622011-04-18 08:33:37 +0000475};
476
Greg Clayton7260f622011-04-18 08:33:37 +0000477#pragma mark CommandObjectTargetSelect
478
479//----------------------------------------------------------------------
480// "target select"
481//----------------------------------------------------------------------
482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483class CommandObjectTargetSelect : public CommandObjectParsed {
Greg Clayton7260f622011-04-18 08:33:37 +0000484public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 CommandObjectTargetSelect(CommandInterpreter &interpreter)
486 : CommandObjectParsed(
487 interpreter, "target select",
488 "Select a target as the current target by target index.", nullptr) {
489 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 ~CommandObjectTargetSelect() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000492
Jim Ingham5a988412012-06-08 21:56:10 +0000493protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 bool DoExecute(Args &args, CommandReturnObject &result) override {
495 if (args.GetArgumentCount() == 1) {
496 bool success = false;
497 const char *target_idx_arg = args.GetArgumentAtIndex(0);
498 uint32_t target_idx =
499 StringConvert::ToUInt32(target_idx_arg, UINT32_MAX, 0, &success);
500 if (success) {
501 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
502 const uint32_t num_targets = target_list.GetNumTargets();
503 if (target_idx < num_targets) {
504 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
505 if (target_sp) {
506 Stream &strm = result.GetOutputStream();
507 target_list.SetSelectedTarget(target_sp.get());
508 bool show_stopped_process_status = false;
509 DumpTargetList(target_list, show_stopped_process_status, strm);
510 result.SetStatus(eReturnStatusSuccessFinishResult);
511 } else {
512 result.AppendErrorWithFormat("target #%u is NULL in target list\n",
513 target_idx);
514 result.SetStatus(eReturnStatusFailed);
515 }
516 } else {
517 if (num_targets > 0) {
518 result.AppendErrorWithFormat(
519 "index %u is out of range, valid target indexes are 0 - %u\n",
520 target_idx, num_targets - 1);
521 } else {
522 result.AppendErrorWithFormat(
523 "index %u is out of range since there are no active targets\n",
524 target_idx);
525 }
526 result.SetStatus(eReturnStatusFailed);
Greg Clayton7260f622011-04-18 08:33:37 +0000527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 } else {
529 result.AppendErrorWithFormat("invalid index string value '%s'\n",
530 target_idx_arg);
531 result.SetStatus(eReturnStatusFailed);
532 }
533 } else {
534 result.AppendError(
535 "'target select' takes a single argument: a target index\n");
536 result.SetStatus(eReturnStatusFailed);
Greg Clayton7260f622011-04-18 08:33:37 +0000537 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 return result.Succeeded();
539 }
Greg Clayton7260f622011-04-18 08:33:37 +0000540};
541
Greg Clayton3418c852011-08-10 02:10:13 +0000542#pragma mark CommandObjectTargetSelect
543
544//----------------------------------------------------------------------
545// "target delete"
546//----------------------------------------------------------------------
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548class CommandObjectTargetDelete : public CommandObjectParsed {
Greg Clayton3418c852011-08-10 02:10:13 +0000549public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 CommandObjectTargetDelete(CommandInterpreter &interpreter)
551 : CommandObjectParsed(interpreter, "target delete",
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000552 "Delete one or more targets by target index.",
553 nullptr),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 m_option_group(), m_all_option(LLDB_OPT_SET_1, false, "all", 'a',
555 "Delete all targets.", false, true),
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000556 m_cleanup_option(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 LLDB_OPT_SET_1, false, "clean", 'c',
558 "Perform extra cleanup to minimize memory consumption after "
559 "deleting the target. "
560 "By default, LLDB will keep in memory any modules previously "
561 "loaded by the target as well "
562 "as all of its debug info. Specifying --clean will unload all of "
563 "these shared modules and "
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000564 "cause them to be reparsed again the next time the target is run",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 false, true) {
566 m_option_group.Append(&m_all_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
567 m_option_group.Append(&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
568 m_option_group.Finalize();
569 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 ~CommandObjectTargetDelete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 Options *GetOptions() override { return &m_option_group; }
Jim Ingham5a988412012-06-08 21:56:10 +0000574
575protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 bool DoExecute(Args &args, CommandReturnObject &result) override {
577 const size_t argc = args.GetArgumentCount();
578 std::vector<TargetSP> delete_target_list;
579 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
580 TargetSP target_sp;
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000581
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 if (m_all_option.GetOptionValue()) {
583 for (int i = 0; i < target_list.GetNumTargets(); ++i)
584 delete_target_list.push_back(target_list.GetTargetAtIndex(i));
585 } else if (argc > 0) {
586 const uint32_t num_targets = target_list.GetNumTargets();
587 // Bail out if don't have any targets.
588 if (num_targets == 0) {
589 result.AppendError("no targets to delete");
590 result.SetStatus(eReturnStatusFailed);
591 return false;
592 }
593
Zachary Turner97d2c402016-10-05 23:40:23 +0000594 for (auto &entry : args.entries()) {
595 uint32_t target_idx;
596 if (entry.ref.getAsInteger(0, target_idx)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 result.AppendErrorWithFormat("invalid target index '%s'\n",
Zachary Turner97d2c402016-10-05 23:40:23 +0000598 entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 result.SetStatus(eReturnStatusFailed);
600 return false;
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000601 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 if (target_idx < num_targets) {
603 target_sp = target_list.GetTargetAtIndex(target_idx);
604 if (target_sp) {
605 delete_target_list.push_back(target_sp);
606 continue;
607 }
Greg Clayton3418c852011-08-10 02:10:13 +0000608 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 if (num_targets > 1)
610 result.AppendErrorWithFormat("target index %u is out of range, valid "
611 "target indexes are 0 - %u\n",
612 target_idx, num_targets - 1);
Greg Clayton3418c852011-08-10 02:10:13 +0000613 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614 result.AppendErrorWithFormat(
615 "target index %u is out of range, the only valid index is 0\n",
616 target_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 result.SetStatus(eReturnStatusFailed);
619 return false;
620 }
621 } else {
622 target_sp = target_list.GetSelectedTarget();
623 if (!target_sp) {
624 result.AppendErrorWithFormat("no target is currently selected\n");
625 result.SetStatus(eReturnStatusFailed);
626 return false;
627 }
628 delete_target_list.push_back(target_sp);
Greg Clayton3418c852011-08-10 02:10:13 +0000629 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 const size_t num_targets_to_delete = delete_target_list.size();
632 for (size_t idx = 0; idx < num_targets_to_delete; ++idx) {
633 target_sp = delete_target_list[idx];
634 target_list.DeleteTarget(target_sp);
635 target_sp->Destroy();
636 }
637 // If "--clean" was specified, prune any orphaned shared modules from
638 // the global shared module list
639 if (m_cleanup_option.GetOptionValue()) {
640 const bool mandatory = true;
641 ModuleList::RemoveOrphanSharedModules(mandatory);
642 }
643 result.GetOutputStream().Printf("%u targets deleted.\n",
644 (uint32_t)num_targets_to_delete);
645 result.SetStatus(eReturnStatusSuccessFinishResult);
646
647 return true;
648 }
649
650 OptionGroupOptions m_option_group;
651 OptionGroupBoolean m_all_option;
652 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000653};
654
Greg Clayton644247c2011-07-07 01:59:51 +0000655#pragma mark CommandObjectTargetVariable
656
657//----------------------------------------------------------------------
658// "target variable"
659//----------------------------------------------------------------------
660
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661class CommandObjectTargetVariable : public CommandObjectParsed {
662 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
663 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000664
Greg Clayton644247c2011-07-07 01:59:51 +0000665public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000666 CommandObjectTargetVariable(CommandInterpreter &interpreter)
667 : CommandObjectParsed(interpreter, "target variable",
668 "Read global variables for the current target, "
669 "before or while running a process.",
670 nullptr, eCommandRequiresTarget),
671 m_option_group(),
672 m_option_variable(false), // Don't include frame options
673 m_option_format(eFormatDefault),
674 m_option_compile_units(LLDB_OPT_SET_1, false, "file", SHORT_OPTION_FILE,
675 0, eArgTypeFilename,
676 "A basename or fullpath to a file that contains "
677 "global variables. This option can be "
678 "specified multiple times."),
679 m_option_shared_libraries(
680 LLDB_OPT_SET_1, false, "shlib", SHORT_OPTION_SHLB, 0,
681 eArgTypeFilename,
682 "A basename or fullpath to a shared library to use in the search "
683 "for global "
684 "variables. This option can be specified multiple times."),
685 m_varobj_options() {
686 CommandArgumentEntry arg;
687 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000688
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689 // Define the first (and only) variant of this arg.
690 var_name_arg.arg_type = eArgTypeVarName;
691 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000692
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 // There is only one variant this argument could be; put it into the
694 // argument entry.
695 arg.push_back(var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000696
Kate Stoneb9c1b512016-09-06 20:57:50 +0000697 // Push the data for the first argument into the m_arguments vector.
698 m_arguments.push_back(arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000699
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
701 m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
702 m_option_group.Append(&m_option_format,
703 OptionGroupFormat::OPTION_GROUP_FORMAT |
704 OptionGroupFormat::OPTION_GROUP_GDB_FMT,
705 LLDB_OPT_SET_1);
706 m_option_group.Append(&m_option_compile_units, LLDB_OPT_SET_ALL,
707 LLDB_OPT_SET_1);
708 m_option_group.Append(&m_option_shared_libraries, LLDB_OPT_SET_ALL,
709 LLDB_OPT_SET_1);
710 m_option_group.Finalize();
711 }
712
713 ~CommandObjectTargetVariable() override = default;
714
715 void DumpValueObject(Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp,
716 const char *root_name) {
717 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
718
719 if (!valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
720 valobj_sp->IsRuntimeSupportValue())
721 return;
722
723 switch (var_sp->GetScope()) {
724 case eValueTypeVariableGlobal:
725 if (m_option_variable.show_scope)
726 s.PutCString("GLOBAL: ");
727 break;
728
729 case eValueTypeVariableStatic:
730 if (m_option_variable.show_scope)
731 s.PutCString("STATIC: ");
732 break;
733
734 case eValueTypeVariableArgument:
735 if (m_option_variable.show_scope)
736 s.PutCString(" ARG: ");
737 break;
738
739 case eValueTypeVariableLocal:
740 if (m_option_variable.show_scope)
741 s.PutCString(" LOCAL: ");
742 break;
743
744 case eValueTypeVariableThreadLocal:
745 if (m_option_variable.show_scope)
746 s.PutCString("THREAD: ");
747 break;
748
749 default:
750 break;
Greg Clayton644247c2011-07-07 01:59:51 +0000751 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000752
Kate Stoneb9c1b512016-09-06 20:57:50 +0000753 if (m_option_variable.show_decl) {
754 bool show_fullpaths = false;
755 bool show_module = true;
756 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
757 s.PutCString(": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000758 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 const Format format = m_option_format.GetFormat();
761 if (format != eFormatDefault)
762 options.SetFormat(format);
Greg Clayton884fb692011-07-08 21:46:14 +0000763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 options.SetRootValueObjectName(root_name);
765
766 valobj_sp->Dump(s, options);
767 }
768
769 static size_t GetVariableCallback(void *baton, const char *name,
770 VariableList &variable_list) {
771 Target *target = static_cast<Target *>(baton);
772 if (target) {
773 return target->GetImages().FindGlobalVariables(ConstString(name), true,
774 UINT32_MAX, variable_list);
Jim Ingham5a988412012-06-08 21:56:10 +0000775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 return 0;
777 }
778
779 Options *GetOptions() override { return &m_option_group; }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000780
Jim Ingham5a988412012-06-08 21:56:10 +0000781protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 void DumpGlobalVariableList(const ExecutionContext &exe_ctx,
783 const SymbolContext &sc,
784 const VariableList &variable_list, Stream &s) {
785 size_t count = variable_list.GetSize();
786 if (count > 0) {
787 if (sc.module_sp) {
788 if (sc.comp_unit) {
789 s.Printf("Global variables for %s in %s:\n",
790 sc.comp_unit->GetPath().c_str(),
791 sc.module_sp->GetFileSpec().GetPath().c_str());
792 } else {
793 s.Printf("Global variables for %s\n",
794 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000795 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 } else if (sc.comp_unit) {
797 s.Printf("Global variables for %s\n", sc.comp_unit->GetPath().c_str());
798 }
799
800 for (uint32_t i = 0; i < count; ++i) {
801 VariableSP var_sp(variable_list.GetVariableAtIndex(i));
802 if (var_sp) {
803 ValueObjectSP valobj_sp(ValueObjectVariable::Create(
804 exe_ctx.GetBestExecutionContextScope(), var_sp));
805
806 if (valobj_sp)
807 DumpValueObject(s, var_sp, valobj_sp,
808 var_sp->GetName().GetCString());
809 }
810 }
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000811 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 bool DoExecute(Args &args, CommandReturnObject &result) override {
815 Target *target = m_exe_ctx.GetTargetPtr();
816 const size_t argc = args.GetArgumentCount();
817 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 if (argc > 0) {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000820
Zachary Turner97d2c402016-10-05 23:40:23 +0000821 // TODO: Convert to entry-based iteration. Requires converting
822 // DumpValueObject.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000823 for (size_t idx = 0; idx < argc; ++idx) {
824 VariableList variable_list;
825 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000826
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 const char *arg = args.GetArgumentAtIndex(idx);
828 size_t matches = 0;
829 bool use_var_name = false;
830 if (m_option_variable.use_regex) {
Zachary Turner95eae422016-09-21 16:01:28 +0000831 RegularExpression regex(llvm::StringRef::withNullAsEmpty(arg));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 if (!regex.IsValid()) {
833 result.GetErrorStream().Printf(
834 "error: invalid regular expression: '%s'\n", arg);
835 result.SetStatus(eReturnStatusFailed);
836 return false;
837 }
838 use_var_name = true;
839 matches = target->GetImages().FindGlobalVariables(
840 regex, true, UINT32_MAX, variable_list);
841 } else {
Zachary Turner97206d52017-05-12 04:51:55 +0000842 Status error(Variable::GetValuesForVariableExpressionPath(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 arg, m_exe_ctx.GetBestExecutionContextScope(),
844 GetVariableCallback, target, variable_list, valobj_list));
845 matches = variable_list.GetSize();
Greg Clayton644247c2011-07-07 01:59:51 +0000846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847
848 if (matches == 0) {
849 result.GetErrorStream().Printf(
850 "error: can't find global variable '%s'\n", arg);
851 result.SetStatus(eReturnStatusFailed);
852 return false;
853 } else {
854 for (uint32_t global_idx = 0; global_idx < matches; ++global_idx) {
855 VariableSP var_sp(variable_list.GetVariableAtIndex(global_idx));
856 if (var_sp) {
857 ValueObjectSP valobj_sp(
858 valobj_list.GetValueObjectAtIndex(global_idx));
859 if (!valobj_sp)
860 valobj_sp = ValueObjectVariable::Create(
861 m_exe_ctx.GetBestExecutionContextScope(), var_sp);
862
863 if (valobj_sp)
864 DumpValueObject(s, var_sp, valobj_sp,
865 use_var_name ? var_sp->GetName().GetCString()
866 : arg);
Greg Claytonf9fc6092013-01-09 19:44:40 +0000867 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 }
869 }
870 }
871 } else {
872 const FileSpecList &compile_units =
873 m_option_compile_units.GetOptionValue().GetCurrentValue();
874 const FileSpecList &shlibs =
875 m_option_shared_libraries.GetOptionValue().GetCurrentValue();
876 SymbolContextList sc_list;
877 const size_t num_compile_units = compile_units.GetSize();
878 const size_t num_shlibs = shlibs.GetSize();
879 if (num_compile_units == 0 && num_shlibs == 0) {
880 bool success = false;
881 StackFrame *frame = m_exe_ctx.GetFramePtr();
882 CompileUnit *comp_unit = nullptr;
883 if (frame) {
884 SymbolContext sc = frame->GetSymbolContext(eSymbolContextCompUnit);
885 if (sc.comp_unit) {
886 const bool can_create = true;
887 VariableListSP comp_unit_varlist_sp(
888 sc.comp_unit->GetVariableList(can_create));
889 if (comp_unit_varlist_sp) {
890 size_t count = comp_unit_varlist_sp->GetSize();
891 if (count > 0) {
892 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
893 success = true;
894 }
895 }
896 }
897 }
898 if (!success) {
899 if (frame) {
900 if (comp_unit)
901 result.AppendErrorWithFormat(
902 "no global variables in current compile unit: %s\n",
903 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000904 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 result.AppendErrorWithFormat(
906 "no debug information for frame %u\n",
907 frame->GetFrameIndex());
908 } else
909 result.AppendError("'target variable' takes one or more global "
910 "variable names as arguments\n");
911 result.SetStatus(eReturnStatusFailed);
912 }
913 } else {
914 SymbolContextList sc_list;
915 const bool append = true;
916 // We have one or more compile unit or shlib
917 if (num_shlibs > 0) {
918 for (size_t shlib_idx = 0; shlib_idx < num_shlibs; ++shlib_idx) {
919 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
920 ModuleSpec module_spec(module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000921
Kate Stoneb9c1b512016-09-06 20:57:50 +0000922 ModuleSP module_sp(
923 target->GetImages().FindFirstModule(module_spec));
924 if (module_sp) {
925 if (num_compile_units > 0) {
926 for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
927 module_sp->FindCompileUnits(
928 compile_units.GetFileSpecAtIndex(cu_idx), append,
929 sc_list);
930 } else {
931 SymbolContext sc;
932 sc.module_sp = module_sp;
933 sc_list.Append(sc);
934 }
935 } else {
936 // Didn't find matching shlib/module in target...
937 result.AppendErrorWithFormat(
938 "target doesn't contain the specified shared library: %s\n",
939 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000941 }
942 } else {
943 // No shared libraries, we just want to find globals for the compile
944 // units files that were specified
945 for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
946 target->GetImages().FindCompileUnits(
947 compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
Greg Clayton644247c2011-07-07 01:59:51 +0000948 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 const uint32_t num_scs = sc_list.GetSize();
951 if (num_scs > 0) {
952 SymbolContext sc;
953 for (uint32_t sc_idx = 0; sc_idx < num_scs; ++sc_idx) {
954 if (sc_list.GetContextAtIndex(sc_idx, sc)) {
955 if (sc.comp_unit) {
956 const bool can_create = true;
957 VariableListSP comp_unit_varlist_sp(
958 sc.comp_unit->GetVariableList(can_create));
959 if (comp_unit_varlist_sp)
960 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp,
961 s);
962 } else if (sc.module_sp) {
963 // Get all global variables for this module
964 lldb_private::RegularExpression all_globals_regex(
Zachary Turner95eae422016-09-21 16:01:28 +0000965 llvm::StringRef(
966 ".")); // Any global with at least one character
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967 VariableList variable_list;
968 sc.module_sp->FindGlobalVariables(all_globals_regex, append,
969 UINT32_MAX, variable_list);
970 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
971 }
972 }
973 }
Enrico Granata61a80ba2011-08-12 16:42:31 +0000974 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 }
Greg Clayton644247c2011-07-07 01:59:51 +0000976 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 if (m_interpreter.TruncationWarningNecessary()) {
979 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
980 m_cmd_name.c_str());
981 m_interpreter.TruncationWarningGiven();
982 }
983
984 return result.Succeeded();
985 }
986
987 OptionGroupOptions m_option_group;
988 OptionGroupVariable m_option_variable;
989 OptionGroupFormat m_option_format;
990 OptionGroupFileList m_option_compile_units;
991 OptionGroupFileList m_option_shared_libraries;
992 OptionGroupValueObjectDisplay m_varobj_options;
Greg Clayton644247c2011-07-07 01:59:51 +0000993};
994
Greg Claytoneffe5c92011-05-03 22:09:39 +0000995#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000999 CommandObjectTargetModulesSearchPathsAdd(CommandInterpreter &interpreter)
1000 : CommandObjectParsed(interpreter, "target modules search-paths add",
1001 "Add new image search paths substitution pairs to "
1002 "the current target.",
1003 nullptr) {
1004 CommandArgumentEntry arg;
1005 CommandArgumentData old_prefix_arg;
1006 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001007
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 // Define the first variant of this arg pair.
1009 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1010 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001011
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 // Define the first variant of this arg pair.
1013 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1014 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001015
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 // There are two required arguments that must always occur together, i.e. an
1017 // argument "pair". Because they
1018 // must always occur together, they are treated as two variants of one
1019 // argument rather than two independent
1020 // arguments. Push them both into the first argument position for
1021 // m_arguments...
Caroline Tice405fe672010-10-04 22:28:36 +00001022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 arg.push_back(old_prefix_arg);
1024 arg.push_back(new_prefix_arg);
Caroline Tice405fe672010-10-04 22:28:36 +00001025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026 m_arguments.push_back(arg);
1027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 ~CommandObjectTargetModulesSearchPathsAdd() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030
Jim Ingham5a988412012-06-08 21:56:10 +00001031protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 bool DoExecute(Args &command, CommandReturnObject &result) override {
1033 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1034 if (target) {
1035 const size_t argc = command.GetArgumentCount();
1036 if (argc & 1) {
1037 result.AppendError("add requires an even number of arguments\n");
1038 result.SetStatus(eReturnStatusFailed);
1039 } else {
1040 for (size_t i = 0; i < argc; i += 2) {
1041 const char *from = command.GetArgumentAtIndex(i);
1042 const char *to = command.GetArgumentAtIndex(i + 1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001043
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 if (from[0] && to[0]) {
1045 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
1046 if (log) {
1047 log->Printf("target modules search path adding ImageSearchPath "
1048 "pair: '%s' -> '%s'",
1049 from, to);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 bool last_pair = ((argc - i) == 2);
1052 target->GetImageSearchPathList().Append(
1053 ConstString(from), ConstString(to),
1054 last_pair); // Notify if this is the last pair
1055 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1056 } else {
1057 if (from[0])
1058 result.AppendError("<path-prefix> can't be empty\n");
1059 else
1060 result.AppendError("<new-path-prefix> can't be empty\n");
1061 result.SetStatus(eReturnStatusFailed);
1062 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 }
1065 } else {
1066 result.AppendError("invalid target\n");
1067 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 return result.Succeeded();
1070 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071};
1072
Greg Claytoneffe5c92011-05-03 22:09:39 +00001073#pragma mark CommandObjectTargetModulesSearchPathsClear
1074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001077 CommandObjectTargetModulesSearchPathsClear(CommandInterpreter &interpreter)
1078 : CommandObjectParsed(interpreter, "target modules search-paths clear",
1079 "Clear all current image search path substitution "
1080 "pairs from the current target.",
1081 "target modules search-paths clear") {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 ~CommandObjectTargetModulesSearchPathsClear() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084
Jim Ingham5a988412012-06-08 21:56:10 +00001085protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 bool DoExecute(Args &command, CommandReturnObject &result) override {
1087 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1088 if (target) {
1089 bool notify = true;
1090 target->GetImageSearchPathList().Clear(notify);
1091 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1092 } else {
1093 result.AppendError("invalid target\n");
1094 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 return result.Succeeded();
1097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098};
1099
Greg Claytoneffe5c92011-05-03 22:09:39 +00001100#pragma mark CommandObjectTargetModulesSearchPathsInsert
1101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 CommandObjectTargetModulesSearchPathsInsert(CommandInterpreter &interpreter)
1105 : CommandObjectParsed(interpreter, "target modules search-paths insert",
1106 "Insert a new image search path substitution pair "
1107 "into the current target at the specified index.",
1108 nullptr) {
1109 CommandArgumentEntry arg1;
1110 CommandArgumentEntry arg2;
1111 CommandArgumentData index_arg;
1112 CommandArgumentData old_prefix_arg;
1113 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001114
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 // Define the first and only variant of this arg.
1116 index_arg.arg_type = eArgTypeIndex;
1117 index_arg.arg_repetition = eArgRepeatPlain;
Caroline Tice405fe672010-10-04 22:28:36 +00001118
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 // Put the one and only variant into the first arg for m_arguments:
1120 arg1.push_back(index_arg);
Caroline Tice405fe672010-10-04 22:28:36 +00001121
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 // Define the first variant of this arg pair.
1123 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1124 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001125
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 // Define the first variant of this arg pair.
1127 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1128 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 // There are two required arguments that must always occur together, i.e. an
1131 // argument "pair". Because they
1132 // must always occur together, they are treated as two variants of one
1133 // argument rather than two independent
1134 // arguments. Push them both into the same argument position for
1135 // m_arguments...
Caroline Tice405fe672010-10-04 22:28:36 +00001136
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 arg2.push_back(old_prefix_arg);
1138 arg2.push_back(new_prefix_arg);
Caroline Tice405fe672010-10-04 22:28:36 +00001139
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 // Add arguments to m_arguments.
1141 m_arguments.push_back(arg1);
1142 m_arguments.push_back(arg2);
1143 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 ~CommandObjectTargetModulesSearchPathsInsert() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146
Jim Ingham5a988412012-06-08 21:56:10 +00001147protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001148 bool DoExecute(Args &command, CommandReturnObject &result) override {
1149 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1150 if (target) {
1151 size_t argc = command.GetArgumentCount();
1152 // check for at least 3 arguments and an odd number of parameters
1153 if (argc >= 3 && argc & 1) {
1154 bool success = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155
Kate Stoneb9c1b512016-09-06 20:57:50 +00001156 uint32_t insert_idx = StringConvert::ToUInt32(
1157 command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 if (!success) {
1160 result.AppendErrorWithFormat(
1161 "<index> parameter is not an integer: '%s'.\n",
1162 command.GetArgumentAtIndex(0));
1163 result.SetStatus(eReturnStatusFailed);
1164 return result.Succeeded();
1165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167 // shift off the index
1168 command.Shift();
1169 argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) {
1172 const char *from = command.GetArgumentAtIndex(i);
1173 const char *to = command.GetArgumentAtIndex(i + 1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001174
Kate Stoneb9c1b512016-09-06 20:57:50 +00001175 if (from[0] && to[0]) {
1176 bool last_pair = ((argc - i) == 2);
1177 target->GetImageSearchPathList().Insert(
1178 ConstString(from), ConstString(to), insert_idx, last_pair);
1179 result.SetStatus(eReturnStatusSuccessFinishNoResult);
1180 } else {
1181 if (from[0])
1182 result.AppendError("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 result.AppendError("<new-path-prefix> can't be empty\n");
1185 result.SetStatus(eReturnStatusFailed);
1186 return false;
1187 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189 } else {
1190 result.AppendError("insert requires at least three arguments\n");
1191 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 return result.Succeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 }
1194
1195 } else {
1196 result.AppendError("invalid target\n");
1197 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 return result.Succeeded();
1200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201};
1202
Greg Claytoneffe5c92011-05-03 22:09:39 +00001203#pragma mark CommandObjectTargetModulesSearchPathsList
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001207 CommandObjectTargetModulesSearchPathsList(CommandInterpreter &interpreter)
1208 : CommandObjectParsed(interpreter, "target modules search-paths list",
1209 "List all current image search path substitution "
1210 "pairs in the current target.",
1211 "target modules search-paths list") {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212
Kate Stoneb9c1b512016-09-06 20:57:50 +00001213 ~CommandObjectTargetModulesSearchPathsList() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214
Jim Ingham5a988412012-06-08 21:56:10 +00001215protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001216 bool DoExecute(Args &command, CommandReturnObject &result) override {
1217 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1218 if (target) {
1219 if (command.GetArgumentCount() != 0) {
1220 result.AppendError("list takes no arguments\n");
1221 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222 return result.Succeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 }
1224
1225 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
1226 result.SetStatus(eReturnStatusSuccessFinishResult);
1227 } else {
1228 result.AppendError("invalid target\n");
1229 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231 return result.Succeeded();
1232 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233};
1234
Greg Claytoneffe5c92011-05-03 22:09:39 +00001235#pragma mark CommandObjectTargetModulesSearchPathsQuery
1236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 CommandObjectTargetModulesSearchPathsQuery(CommandInterpreter &interpreter)
1240 : CommandObjectParsed(
1241 interpreter, "target modules search-paths query",
1242 "Transform a path using the first applicable image search path.",
1243 nullptr) {
1244 CommandArgumentEntry arg;
1245 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247 // Define the first (and only) variant of this arg.
1248 path_arg.arg_type = eArgTypeDirectoryName;
1249 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001250
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251 // There is only one variant this argument could be; put it into the
1252 // argument entry.
1253 arg.push_back(path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001254
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 // Push the data for the first argument into the m_arguments vector.
1256 m_arguments.push_back(arg);
1257 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 ~CommandObjectTargetModulesSearchPathsQuery() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260
Jim Ingham5a988412012-06-08 21:56:10 +00001261protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 bool DoExecute(Args &command, CommandReturnObject &result) override {
1263 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1264 if (target) {
1265 if (command.GetArgumentCount() != 1) {
1266 result.AppendError("query requires one argument\n");
1267 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268 return result.Succeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 }
1270
1271 ConstString orig(command.GetArgumentAtIndex(0));
1272 ConstString transformed;
1273 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1274 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1275 else
1276 result.GetOutputStream().Printf("%s\n", orig.GetCString());
1277
1278 result.SetStatus(eReturnStatusSuccessFinishResult);
1279 } else {
1280 result.AppendError("invalid target\n");
1281 result.SetStatus(eReturnStatusFailed);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 return result.Succeeded();
1284 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001285};
1286
Greg Claytoneffe5c92011-05-03 22:09:39 +00001287//----------------------------------------------------------------------
1288// Static Helper functions
1289//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290static void DumpModuleArchitecture(Stream &strm, Module *module,
1291 bool full_triple, uint32_t width) {
1292 if (module) {
1293 StreamString arch_strm;
Todd Fiala7df337f2015-10-13 23:41:19 +00001294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 if (full_triple)
1296 module->GetArchitecture().DumpTriple(arch_strm);
Greg Clayton3418c852011-08-10 02:10:13 +00001297 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 arch_strm.PutCString(module->GetArchitecture().GetArchitectureName());
1299 std::string arch_str = arch_strm.GetString();
1300
1301 if (width)
1302 strm.Printf("%-*s", width, arch_str.c_str());
1303 else
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00001304 strm.PutCString(arch_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001306}
1307
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308static void DumpModuleUUID(Stream &strm, Module *module) {
1309 if (module && module->GetUUID().IsValid())
1310 module->GetUUID().Dump(&strm);
1311 else
1312 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001313}
1314
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315static uint32_t DumpCompileUnitLineTable(CommandInterpreter &interpreter,
1316 Stream &strm, Module *module,
1317 const FileSpec &file_spec,
1318 bool load_addresses) {
1319 uint32_t num_matches = 0;
1320 if (module) {
1321 SymbolContextList sc_list;
1322 num_matches = module->ResolveSymbolContextsForFileSpec(
1323 file_spec, 0, false, eSymbolContextCompUnit, sc_list);
1324
1325 for (uint32_t i = 0; i < num_matches; ++i) {
1326 SymbolContext sc;
1327 if (sc_list.GetContextAtIndex(i, sc)) {
1328 if (i > 0)
1329 strm << "\n\n";
1330
1331 strm << "Line table for " << *static_cast<FileSpec *>(sc.comp_unit)
1332 << " in `" << module->GetFileSpec().GetFilename() << "\n";
1333 LineTable *line_table = sc.comp_unit->GetLineTable();
1334 if (line_table)
1335 line_table->GetDescription(
1336 &strm, interpreter.GetExecutionContext().GetTargetPtr(),
1337 lldb::eDescriptionLevelBrief);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001338 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 strm << "No line table";
1340 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001341 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 }
1343 return num_matches;
1344}
1345
1346static void DumpFullpath(Stream &strm, const FileSpec *file_spec_ptr,
1347 uint32_t width) {
1348 if (file_spec_ptr) {
1349 if (width > 0) {
1350 std::string fullpath = file_spec_ptr->GetPath();
1351 strm.Printf("%-*s", width, fullpath.c_str());
1352 return;
1353 } else {
1354 file_spec_ptr->Dump(&strm);
1355 return;
1356 }
1357 }
1358 // Keep the width spacing correct if things go wrong...
1359 if (width > 0)
1360 strm.Printf("%-*s", width, "");
1361}
1362
1363static void DumpDirectory(Stream &strm, const FileSpec *file_spec_ptr,
1364 uint32_t width) {
1365 if (file_spec_ptr) {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001366 if (width > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1368 else
1369 file_spec_ptr->GetDirectory().Dump(&strm);
1370 return;
1371 }
1372 // Keep the width spacing correct if things go wrong...
1373 if (width > 0)
1374 strm.Printf("%-*s", width, "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001375}
1376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377static void DumpBasename(Stream &strm, const FileSpec *file_spec_ptr,
1378 uint32_t width) {
1379 if (file_spec_ptr) {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001380 if (width > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1382 else
1383 file_spec_ptr->GetFilename().Dump(&strm);
1384 return;
1385 }
1386 // Keep the width spacing correct if things go wrong...
1387 if (width > 0)
1388 strm.Printf("%-*s", width, "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001389}
1390
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391static size_t DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list) {
1392 size_t num_dumped = 0;
1393 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
1394 const size_t num_modules = module_list.GetSize();
1395 if (num_modules > 0) {
1396 strm.Printf("Dumping headers for %" PRIu64 " module(s).\n",
1397 static_cast<uint64_t>(num_modules));
Greg Claytonc4a8a762012-05-15 18:43:44 +00001398 strm.IndentMore();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1400 Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
1401 if (module) {
1402 if (num_dumped++ > 0) {
1403 strm.EOL();
1404 strm.EOL();
1405 }
1406 ObjectFile *objfile = module->GetObjectFile();
1407 objfile->Dump(&strm);
1408 }
Greg Claytonc4a8a762012-05-15 18:43:44 +00001409 }
1410 strm.IndentLess();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 }
1412 return num_dumped;
Greg Claytonc4a8a762012-05-15 18:43:44 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415static void DumpModuleSymtab(CommandInterpreter &interpreter, Stream &strm,
1416 Module *module, SortOrder sort_order) {
1417 if (module) {
1418 SymbolVendor *sym_vendor = module->GetSymbolVendor();
1419 if (sym_vendor) {
1420 Symtab *symtab = sym_vendor->GetSymtab();
1421 if (symtab)
1422 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(),
1423 sort_order);
1424 }
1425 }
1426}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001427
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428static void DumpModuleSections(CommandInterpreter &interpreter, Stream &strm,
1429 Module *module) {
1430 if (module) {
1431 SectionList *section_list = module->GetSectionList();
1432 if (section_list) {
1433 strm.Printf("Sections for '%s' (%s):\n",
1434 module->GetSpecificationDescription().c_str(),
1435 module->GetArchitecture().GetArchitectureName());
1436 strm.IndentMore();
1437 section_list->Dump(&strm,
1438 interpreter.GetExecutionContext().GetTargetPtr(), true,
1439 UINT32_MAX);
1440 strm.IndentLess();
1441 }
1442 }
1443}
1444
1445static bool DumpModuleSymbolVendor(Stream &strm, Module *module) {
1446 if (module) {
1447 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1448 if (symbol_vendor) {
1449 symbol_vendor->Dump(&strm);
1450 return true;
1451 }
1452 }
1453 return false;
1454}
1455
1456static void DumpAddress(ExecutionContextScope *exe_scope,
1457 const Address &so_addr, bool verbose, Stream &strm) {
1458 strm.IndentMore();
1459 strm.Indent(" Address: ");
1460 so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1461 strm.PutCString(" (");
1462 so_addr.Dump(&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1463 strm.PutCString(")\n");
1464 strm.Indent(" Summary: ");
1465 const uint32_t save_indent = strm.GetIndentLevel();
1466 strm.SetIndentLevel(save_indent + 13);
1467 so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription);
1468 strm.SetIndentLevel(save_indent);
1469 // Print out detailed address information when verbose is enabled
1470 if (verbose) {
1471 strm.EOL();
1472 so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1473 }
1474 strm.IndentLess();
1475}
1476
1477static bool LookupAddressInModule(CommandInterpreter &interpreter, Stream &strm,
1478 Module *module, uint32_t resolve_mask,
1479 lldb::addr_t raw_addr, lldb::addr_t offset,
1480 bool verbose) {
1481 if (module) {
1482 lldb::addr_t addr = raw_addr - offset;
1483 Address so_addr;
1484 SymbolContext sc;
1485 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
1486 if (target && !target->GetSectionLoadList().IsEmpty()) {
1487 if (!target->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1488 return false;
1489 else if (so_addr.GetModule().get() != module)
1490 return false;
1491 } else {
1492 if (!module->ResolveFileAddress(addr, so_addr))
1493 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001494 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001495
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 ExecutionContextScope *exe_scope =
1497 interpreter.GetExecutionContext().GetBestExecutionContextScope();
1498 DumpAddress(exe_scope, so_addr, verbose, strm);
1499 // strm.IndentMore();
1500 // strm.Indent (" Address: ");
1501 // so_addr.Dump (&strm, exe_scope,
1502 // Address::DumpStyleModuleWithFileAddress);
1503 // strm.PutCString (" (");
1504 // so_addr.Dump (&strm, exe_scope,
1505 // Address::DumpStyleSectionNameOffset);
1506 // strm.PutCString (")\n");
1507 // strm.Indent (" Summary: ");
1508 // const uint32_t save_indent = strm.GetIndentLevel ();
1509 // strm.SetIndentLevel (save_indent + 13);
1510 // so_addr.Dump (&strm, exe_scope,
1511 // Address::DumpStyleResolvedDescription);
1512 // strm.SetIndentLevel (save_indent);
1513 // // Print out detailed address information when verbose is enabled
1514 // if (verbose)
1515 // {
1516 // strm.EOL();
1517 // so_addr.Dump (&strm, exe_scope,
1518 // Address::DumpStyleDetailedSymbolContext);
1519 // }
1520 // strm.IndentLess();
1521 return true;
1522 }
1523
1524 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001525}
1526
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
1528 Stream &strm, Module *module,
1529 const char *name, bool name_is_regex,
1530 bool verbose) {
1531 if (module) {
1532 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001533
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534 SymbolVendor *sym_vendor = module->GetSymbolVendor();
1535 if (sym_vendor) {
1536 Symtab *symtab = sym_vendor->GetSymtab();
1537 if (symtab) {
1538 std::vector<uint32_t> match_indexes;
1539 ConstString symbol_name(name);
1540 uint32_t num_matches = 0;
1541 if (name_is_regex) {
Zachary Turner95eae422016-09-21 16:01:28 +00001542 RegularExpression name_regexp(symbol_name.GetStringRef());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType(
1544 name_regexp, eSymbolTypeAny, match_indexes);
1545 } else {
1546 num_matches =
1547 symtab->AppendSymbolIndexesWithName(symbol_name, match_indexes);
1548 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001549
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 if (num_matches > 0) {
1551 strm.Indent();
1552 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1553 name_is_regex ? "the regular expression " : "", name);
1554 DumpFullpath(strm, &module->GetFileSpec(), 0);
1555 strm.PutCString(":\n");
1556 strm.IndentMore();
1557 for (uint32_t i = 0; i < num_matches; ++i) {
1558 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
1559 if (symbol && symbol->ValueIsAddress()) {
1560 DumpAddress(interpreter.GetExecutionContext()
1561 .GetBestExecutionContextScope(),
1562 symbol->GetAddressRef(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564 }
1565 strm.IndentLess();
1566 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001568 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001569 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001570 }
1571 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001572}
1573
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574static void DumpSymbolContextList(ExecutionContextScope *exe_scope,
1575 Stream &strm, SymbolContextList &sc_list,
1576 bool verbose) {
1577 strm.IndentMore();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001580
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 for (uint32_t i = 0; i < num_matches; ++i) {
1582 SymbolContext sc;
1583 if (sc_list.GetContextAtIndex(i, sc)) {
1584 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 DumpAddress(exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001589 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 }
1591 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001592}
1593
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594static size_t LookupFunctionInModule(CommandInterpreter &interpreter,
1595 Stream &strm, Module *module,
1596 const char *name, bool name_is_regex,
1597 bool include_inlines, bool include_symbols,
1598 bool verbose) {
1599 if (module && name && name[0]) {
1600 SymbolContextList sc_list;
1601 const bool append = true;
1602 size_t num_matches = 0;
1603 if (name_is_regex) {
Zachary Turner95eae422016-09-21 16:01:28 +00001604 RegularExpression function_name_regex((llvm::StringRef(name)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001605 num_matches = module->FindFunctions(function_name_regex, include_symbols,
1606 include_inlines, append, sc_list);
1607 } else {
1608 ConstString function_name(name);
1609 num_matches = module->FindFunctions(
1610 function_name, nullptr, eFunctionNameTypeAuto, include_symbols,
1611 include_inlines, append, sc_list);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001612 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 if (num_matches) {
1615 strm.Indent();
1616 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1617 num_matches > 1 ? "es" : "");
1618 DumpFullpath(strm, &module->GetFileSpec(), 0);
1619 strm.PutCString(":\n");
1620 DumpSymbolContextList(
1621 interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1622 strm, sc_list, verbose);
Sean Callanand38b4a92012-06-06 20:49:55 +00001623 }
1624 return num_matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 }
1626 return 0;
Sean Callanand38b4a92012-06-06 20:49:55 +00001627}
1628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629static size_t LookupTypeInModule(CommandInterpreter &interpreter, Stream &strm,
1630 Module *module, const char *name_cstr,
1631 bool name_is_regex) {
1632 if (module && name_cstr && name_cstr[0]) {
1633 TypeList type_list;
1634 const uint32_t max_num_matches = UINT32_MAX;
1635 size_t num_matches = 0;
1636 bool name_is_fully_qualified = false;
1637 SymbolContext sc;
1638
1639 ConstString name(name_cstr);
1640 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1641 num_matches =
1642 module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches,
1643 searched_symbol_files, type_list);
1644
1645 if (num_matches) {
1646 strm.Indent();
1647 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1648 num_matches > 1 ? "es" : "");
1649 DumpFullpath(strm, &module->GetFileSpec(), 0);
1650 strm.PutCString(":\n");
1651 for (TypeSP type_sp : type_list.Types()) {
1652 if (type_sp) {
1653 // Resolve the clang type so that any forward references
1654 // to types that haven't yet been parsed will get parsed.
1655 type_sp->GetFullCompilerType();
1656 type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1657 // Print all typedef chains
1658 TypeSP typedef_type_sp(type_sp);
1659 TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1660 while (typedefed_type_sp) {
1661 strm.EOL();
1662 strm.Printf(" typedef '%s': ",
1663 typedef_type_sp->GetName().GetCString());
1664 typedefed_type_sp->GetFullCompilerType();
1665 typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull,
1666 true);
1667 typedef_type_sp = typedefed_type_sp;
1668 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1669 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001670 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671 strm.EOL();
1672 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001673 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674 return num_matches;
1675 }
1676 return 0;
1677}
1678
1679static size_t LookupTypeHere(CommandInterpreter &interpreter, Stream &strm,
1680 const SymbolContext &sym_ctx,
1681 const char *name_cstr, bool name_is_regex) {
1682 if (!sym_ctx.module_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001683 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001684
1685 TypeList type_list;
1686 const uint32_t max_num_matches = UINT32_MAX;
1687 size_t num_matches = 1;
1688 bool name_is_fully_qualified = false;
1689
1690 ConstString name(name_cstr);
1691 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1692 num_matches = sym_ctx.module_sp->FindTypes(
1693 sym_ctx, name, name_is_fully_qualified, max_num_matches,
1694 searched_symbol_files, type_list);
1695
1696 if (num_matches) {
1697 strm.Indent();
1698 strm.PutCString("Best match found in ");
1699 DumpFullpath(strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1700 strm.PutCString(":\n");
1701
1702 TypeSP type_sp(type_list.GetTypeAtIndex(0));
1703 if (type_sp) {
1704 // Resolve the clang type so that any forward references
1705 // to types that haven't yet been parsed will get parsed.
1706 type_sp->GetFullCompilerType();
1707 type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1708 // Print all typedef chains
1709 TypeSP typedef_type_sp(type_sp);
1710 TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1711 while (typedefed_type_sp) {
1712 strm.EOL();
1713 strm.Printf(" typedef '%s': ",
1714 typedef_type_sp->GetName().GetCString());
1715 typedefed_type_sp->GetFullCompilerType();
1716 typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1717 typedef_type_sp = typedefed_type_sp;
1718 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1719 }
1720 }
1721 strm.EOL();
1722 }
1723 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001724}
1725
Kate Stoneb9c1b512016-09-06 20:57:50 +00001726static uint32_t LookupFileAndLineInModule(CommandInterpreter &interpreter,
1727 Stream &strm, Module *module,
1728 const FileSpec &file_spec,
1729 uint32_t line, bool check_inlines,
1730 bool verbose) {
1731 if (module && file_spec) {
1732 SymbolContextList sc_list;
1733 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(
1734 file_spec, line, check_inlines, eSymbolContextEverything, sc_list);
1735 if (num_matches > 0) {
1736 strm.Indent();
1737 strm.Printf("%u match%s found in ", num_matches,
1738 num_matches > 1 ? "es" : "");
1739 strm << file_spec;
1740 if (line > 0)
1741 strm.Printf(":%u", line);
1742 strm << " in ";
1743 DumpFullpath(strm, &module->GetFileSpec(), 0);
1744 strm.PutCString(":\n");
1745 DumpSymbolContextList(
1746 interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1747 strm, sc_list, verbose);
1748 return num_matches;
Greg Clayton8ee64382011-11-10 01:18:58 +00001749 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001750 }
1751 return 0;
1752}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001753
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754static size_t FindModulesByName(Target *target, const char *module_name,
1755 ModuleList &module_list,
1756 bool check_global_list) {
1757 FileSpec module_file_spec(module_name, false);
1758 ModuleSpec module_spec(module_file_spec);
1759
1760 const size_t initial_size = module_list.GetSize();
1761
1762 if (check_global_list) {
1763 // Check the global list
1764 std::lock_guard<std::recursive_mutex> guard(
1765 Module::GetAllocationModuleCollectionMutex());
1766 const size_t num_modules = Module::GetNumberAllocatedModules();
1767 ModuleSP module_sp;
1768 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1769 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1770
1771 if (module) {
1772 if (module->MatchesModuleSpec(module_spec)) {
1773 module_sp = module->shared_from_this();
1774 module_list.AppendIfNeeded(module_sp);
Greg Claytonf3156262012-07-11 20:46:47 +00001775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776 }
Greg Claytonf3156262012-07-11 20:46:47 +00001777 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001778 } else {
1779 if (target) {
1780 const size_t num_matches =
1781 target->GetImages().FindModules(module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001782
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783 // Not found in our module list for our target, check the main
1784 // shared module list in case it is a extra file used somewhere
1785 // else
1786 if (num_matches == 0) {
1787 module_spec.GetArchitecture() = target->GetArchitecture();
1788 ModuleList::FindSharedModules(module_spec, module_list);
1789 }
1790 } else {
1791 ModuleList::FindSharedModules(module_spec, module_list);
1792 }
1793 }
1794
1795 return module_list.GetSize() - initial_size;
Greg Clayton8ee64382011-11-10 01:18:58 +00001796}
1797
Greg Claytoneffe5c92011-05-03 22:09:39 +00001798#pragma mark CommandObjectTargetModulesModuleAutoComplete
1799
1800//----------------------------------------------------------------------
1801// A base command object class that can auto complete with module file
1802// paths
1803//----------------------------------------------------------------------
1804
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805class CommandObjectTargetModulesModuleAutoComplete
1806 : public CommandObjectParsed {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001807public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 CommandObjectTargetModulesModuleAutoComplete(CommandInterpreter &interpreter,
1809 const char *name,
1810 const char *help,
1811 const char *syntax)
1812 : CommandObjectParsed(interpreter, name, help, syntax) {
1813 CommandArgumentEntry arg;
1814 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001815
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 // Define the first (and only) variant of this arg.
1817 file_arg.arg_type = eArgTypeFilename;
1818 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001819
Kate Stoneb9c1b512016-09-06 20:57:50 +00001820 // There is only one variant this argument could be; put it into the
1821 // argument entry.
1822 arg.push_back(file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001823
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824 // Push the data for the first argument into the m_arguments vector.
1825 m_arguments.push_back(arg);
1826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001827
Kate Stoneb9c1b512016-09-06 20:57:50 +00001828 ~CommandObjectTargetModulesModuleAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001829
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 int HandleArgumentCompletion(Args &input, int &cursor_index,
1831 int &cursor_char_position,
1832 OptionElementVector &opt_element_vector,
1833 int match_start_point, int max_return_elements,
1834 bool &word_complete,
1835 StringList &matches) override {
1836 // Arguments are the standard module completer.
1837 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
1838 completion_str.erase(cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001839
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 CommandCompletions::InvokeCommonCompletionCallbacks(
1841 GetCommandInterpreter(), CommandCompletions::eModuleCompletion,
1842 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
1843 word_complete, matches);
1844 return matches.GetSize();
1845 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001846};
1847
1848#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1849
1850//----------------------------------------------------------------------
1851// A base command object class that can auto complete with module source
1852// file paths
1853//----------------------------------------------------------------------
1854
Kate Stoneb9c1b512016-09-06 20:57:50 +00001855class CommandObjectTargetModulesSourceFileAutoComplete
1856 : public CommandObjectParsed {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001857public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001858 CommandObjectTargetModulesSourceFileAutoComplete(
1859 CommandInterpreter &interpreter, const char *name, const char *help,
1860 const char *syntax, uint32_t flags)
1861 : CommandObjectParsed(interpreter, name, help, syntax, flags) {
1862 CommandArgumentEntry arg;
1863 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001864
Kate Stoneb9c1b512016-09-06 20:57:50 +00001865 // Define the first (and only) variant of this arg.
1866 source_file_arg.arg_type = eArgTypeSourceFile;
1867 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001868
Kate Stoneb9c1b512016-09-06 20:57:50 +00001869 // There is only one variant this argument could be; put it into the
1870 // argument entry.
1871 arg.push_back(source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001872
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873 // Push the data for the first argument into the m_arguments vector.
1874 m_arguments.push_back(arg);
1875 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001876
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877 ~CommandObjectTargetModulesSourceFileAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001878
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879 int HandleArgumentCompletion(Args &input, int &cursor_index,
1880 int &cursor_char_position,
1881 OptionElementVector &opt_element_vector,
1882 int match_start_point, int max_return_elements,
1883 bool &word_complete,
1884 StringList &matches) override {
1885 // Arguments are the standard source file completer.
1886 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
1887 completion_str.erase(cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001888
Kate Stoneb9c1b512016-09-06 20:57:50 +00001889 CommandCompletions::InvokeCommonCompletionCallbacks(
1890 GetCommandInterpreter(), CommandCompletions::eSourceFileCompletion,
1891 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
1892 word_complete, matches);
1893 return matches.GetSize();
1894 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001895};
1896
Adrian McCarthy543725c2016-04-04 21:21:49 +00001897#pragma mark CommandObjectTargetModulesDumpObjfile
1898
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899class CommandObjectTargetModulesDumpObjfile
1900 : public CommandObjectTargetModulesModuleAutoComplete {
Adrian McCarthy543725c2016-04-04 21:21:49 +00001901public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902 CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter)
1903 : CommandObjectTargetModulesModuleAutoComplete(
1904 interpreter, "target modules dump objfile",
1905 "Dump the object file headers from one or more target modules.",
1906 nullptr) {}
Adrian McCarthy543725c2016-04-04 21:21:49 +00001907
Kate Stoneb9c1b512016-09-06 20:57:50 +00001908 ~CommandObjectTargetModulesDumpObjfile() override = default;
Adrian McCarthy543725c2016-04-04 21:21:49 +00001909
1910protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911 bool DoExecute(Args &command, CommandReturnObject &result) override {
1912 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1913 if (target == nullptr) {
1914 result.AppendError("invalid target, create a debug target using the "
1915 "'target create' command");
1916 result.SetStatus(eReturnStatusFailed);
1917 return false;
Adrian McCarthy543725c2016-04-04 21:21:49 +00001918 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001919
1920 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1921 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1922 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1923
1924 size_t num_dumped = 0;
1925 if (command.GetArgumentCount() == 0) {
1926 // Dump all headers for all modules images
1927 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(),
1928 target->GetImages());
1929 if (num_dumped == 0) {
1930 result.AppendError("the target has no associated executable images");
1931 result.SetStatus(eReturnStatusFailed);
1932 }
1933 } else {
1934 // Find the modules that match the basename or full path.
1935 ModuleList module_list;
1936 const char *arg_cstr;
1937 for (int arg_idx = 0;
1938 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
1939 ++arg_idx) {
1940 size_t num_matched =
1941 FindModulesByName(target, arg_cstr, module_list, true);
1942 if (num_matched == 0) {
1943 result.AppendWarningWithFormat(
1944 "Unable to find an image that matches '%s'.\n", arg_cstr);
1945 }
1946 }
1947 // Dump all the modules we found.
1948 num_dumped =
1949 DumpModuleObjfileHeaders(result.GetOutputStream(), module_list);
1950 }
1951
1952 if (num_dumped > 0) {
1953 result.SetStatus(eReturnStatusSuccessFinishResult);
1954 } else {
1955 result.AppendError("no matching executable images found");
1956 result.SetStatus(eReturnStatusFailed);
1957 }
1958 return result.Succeeded();
1959 }
Adrian McCarthy543725c2016-04-04 21:21:49 +00001960};
1961
Greg Claytoneffe5c92011-05-03 22:09:39 +00001962#pragma mark CommandObjectTargetModulesDumpSymtab
1963
Zachary Turner1f0f5b52016-09-22 20:22:55 +00001964static OptionEnumValueElement g_sort_option_enumeration[4] = {
1965 {eSortOrderNone, "none",
1966 "No sorting, use the original symbol table order."},
1967 {eSortOrderByAddress, "address", "Sort output by symbol address."},
1968 {eSortOrderByName, "name", "Sort output by symbol name."},
1969 {0, nullptr, nullptr}};
1970
1971static OptionDefinition g_target_modules_dump_symtab_options[] = {
1972 // clang-format off
1973 { LLDB_OPT_SET_1, false, "sort", 's', OptionParser::eRequiredArgument, nullptr, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table." }
1974 // clang-format on
1975};
1976
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977class CommandObjectTargetModulesDumpSymtab
1978 : public CommandObjectTargetModulesModuleAutoComplete {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001979public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 CommandObjectTargetModulesDumpSymtab(CommandInterpreter &interpreter)
1981 : CommandObjectTargetModulesModuleAutoComplete(
1982 interpreter, "target modules dump symtab",
1983 "Dump the symbol table from one or more target modules.", nullptr),
1984 m_options() {}
1985
1986 ~CommandObjectTargetModulesDumpSymtab() override = default;
1987
1988 Options *GetOptions() override { return &m_options; }
1989
1990 class CommandOptions : public Options {
1991 public:
1992 CommandOptions() : Options(), m_sort_order(eSortOrderNone) {}
1993
1994 ~CommandOptions() override = default;
1995
Zachary Turner97206d52017-05-12 04:51:55 +00001996 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1997 ExecutionContext *execution_context) override {
1998 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001999 const int short_option = m_getopt_table[option_idx].val;
2000
2001 switch (short_option) {
2002 case 's':
2003 m_sort_order = (SortOrder)Args::StringToOptionEnum(
Zachary Turnerfe114832016-11-12 16:56:47 +00002004 option_arg, GetDefinitions()[option_idx].enum_values,
2005 eSortOrderNone, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 break;
2007
2008 default:
2009 error.SetErrorStringWithFormat("invalid short option character '%c'",
2010 short_option);
2011 break;
2012 }
2013 return error;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002014 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002015
Kate Stoneb9c1b512016-09-06 20:57:50 +00002016 void OptionParsingStarting(ExecutionContext *execution_context) override {
2017 m_sort_order = eSortOrderNone;
Jim Ingham5a988412012-06-08 21:56:10 +00002018 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002019
Zachary Turner1f0f5b52016-09-22 20:22:55 +00002020 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00002021 return llvm::makeArrayRef(g_target_modules_dump_symtab_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00002022 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002023
Kate Stoneb9c1b512016-09-06 20:57:50 +00002024 SortOrder m_sort_order;
2025 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002026
Jim Ingham5a988412012-06-08 21:56:10 +00002027protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002028 bool DoExecute(Args &command, CommandReturnObject &result) override {
2029 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2030 if (target == nullptr) {
2031 result.AppendError("invalid target, create a debug target using the "
2032 "'target create' command");
2033 result.SetStatus(eReturnStatusFailed);
2034 return false;
2035 } else {
2036 uint32_t num_dumped = 0;
2037
2038 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2039 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2040 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2041
2042 if (command.GetArgumentCount() == 0) {
2043 // Dump all sections for all modules images
2044 std::lock_guard<std::recursive_mutex> guard(
2045 target->GetImages().GetMutex());
2046 const size_t num_modules = target->GetImages().GetSize();
2047 if (num_modules > 0) {
2048 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64
2049 " modules.\n",
2050 (uint64_t)num_modules);
2051 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2052 if (num_dumped > 0) {
2053 result.GetOutputStream().EOL();
2054 result.GetOutputStream().EOL();
2055 }
2056 num_dumped++;
2057 DumpModuleSymtab(
2058 m_interpreter, result.GetOutputStream(),
2059 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2060 m_options.m_sort_order);
2061 }
2062 } else {
2063 result.AppendError("the target has no associated executable images");
2064 result.SetStatus(eReturnStatusFailed);
2065 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067 } else {
2068 // Dump specified images (by basename or fullpath)
2069 const char *arg_cstr;
2070 for (int arg_idx = 0;
2071 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2072 ++arg_idx) {
2073 ModuleList module_list;
2074 const size_t num_matches =
2075 FindModulesByName(target, arg_cstr, module_list, true);
2076 if (num_matches > 0) {
2077 for (size_t i = 0; i < num_matches; ++i) {
2078 Module *module = module_list.GetModulePointerAtIndex(i);
2079 if (module) {
2080 if (num_dumped > 0) {
2081 result.GetOutputStream().EOL();
2082 result.GetOutputStream().EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002084 num_dumped++;
2085 DumpModuleSymtab(m_interpreter, result.GetOutputStream(),
2086 module, m_options.m_sort_order);
2087 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002088 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089 } else
2090 result.AppendWarningWithFormat(
2091 "Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002092 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093 }
2094
2095 if (num_dumped > 0)
2096 result.SetStatus(eReturnStatusSuccessFinishResult);
2097 else {
2098 result.AppendError("no matching executable images found");
2099 result.SetStatus(eReturnStatusFailed);
2100 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002102 return result.Succeeded();
2103 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002104
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105 CommandOptions m_options;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002106};
2107
Greg Claytoneffe5c92011-05-03 22:09:39 +00002108#pragma mark CommandObjectTargetModulesDumpSections
2109
2110//----------------------------------------------------------------------
2111// Image section dumping command
2112//----------------------------------------------------------------------
2113
Kate Stoneb9c1b512016-09-06 20:57:50 +00002114class CommandObjectTargetModulesDumpSections
2115 : public CommandObjectTargetModulesModuleAutoComplete {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002116public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002117 CommandObjectTargetModulesDumpSections(CommandInterpreter &interpreter)
2118 : CommandObjectTargetModulesModuleAutoComplete(
2119 interpreter, "target modules dump sections",
2120 "Dump the sections from one or more target modules.",
2121 //"target modules dump sections [<file1> ...]")
2122 nullptr) {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002123
Kate Stoneb9c1b512016-09-06 20:57:50 +00002124 ~CommandObjectTargetModulesDumpSections() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002125
Jim Ingham5a988412012-06-08 21:56:10 +00002126protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127 bool DoExecute(Args &command, CommandReturnObject &result) override {
2128 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2129 if (target == nullptr) {
2130 result.AppendError("invalid target, create a debug target using the "
2131 "'target create' command");
2132 result.SetStatus(eReturnStatusFailed);
2133 return false;
2134 } else {
2135 uint32_t num_dumped = 0;
2136
2137 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2138 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2139 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2140
2141 if (command.GetArgumentCount() == 0) {
2142 // Dump all sections for all modules images
2143 const size_t num_modules = target->GetImages().GetSize();
2144 if (num_modules > 0) {
2145 result.GetOutputStream().Printf("Dumping sections for %" PRIu64
2146 " modules.\n",
2147 (uint64_t)num_modules);
2148 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2149 num_dumped++;
2150 DumpModuleSections(
2151 m_interpreter, result.GetOutputStream(),
2152 target->GetImages().GetModulePointerAtIndex(image_idx));
2153 }
2154 } else {
2155 result.AppendError("the target has no associated executable images");
2156 result.SetStatus(eReturnStatusFailed);
2157 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002158 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 } else {
2160 // Dump specified images (by basename or fullpath)
2161 const char *arg_cstr;
2162 for (int arg_idx = 0;
2163 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2164 ++arg_idx) {
2165 ModuleList module_list;
2166 const size_t num_matches =
2167 FindModulesByName(target, arg_cstr, module_list, true);
2168 if (num_matches > 0) {
2169 for (size_t i = 0; i < num_matches; ++i) {
2170 Module *module = module_list.GetModulePointerAtIndex(i);
2171 if (module) {
2172 num_dumped++;
2173 DumpModuleSections(m_interpreter, result.GetOutputStream(),
2174 module);
2175 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002176 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 } else {
2178 // Check the global list
2179 std::lock_guard<std::recursive_mutex> guard(
2180 Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002181
Kate Stoneb9c1b512016-09-06 20:57:50 +00002182 result.AppendWarningWithFormat(
2183 "Unable to find an image that matches '%s'.\n", arg_cstr);
2184 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002185 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002186 }
2187
2188 if (num_dumped > 0)
2189 result.SetStatus(eReturnStatusSuccessFinishResult);
2190 else {
2191 result.AppendError("no matching executable images found");
2192 result.SetStatus(eReturnStatusFailed);
2193 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002195 return result.Succeeded();
2196 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002197};
2198
Greg Claytoneffe5c92011-05-03 22:09:39 +00002199#pragma mark CommandObjectTargetModulesDumpSymfile
2200
2201//----------------------------------------------------------------------
2202// Image debug symbol dumping command
2203//----------------------------------------------------------------------
2204
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205class CommandObjectTargetModulesDumpSymfile
2206 : public CommandObjectTargetModulesModuleAutoComplete {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002207public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002208 CommandObjectTargetModulesDumpSymfile(CommandInterpreter &interpreter)
2209 : CommandObjectTargetModulesModuleAutoComplete(
2210 interpreter, "target modules dump symfile",
2211 "Dump the debug symbol file for one or more target modules.",
2212 //"target modules dump symfile [<file1> ...]")
2213 nullptr) {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002214
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 ~CommandObjectTargetModulesDumpSymfile() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002216
Jim Ingham5a988412012-06-08 21:56:10 +00002217protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002218 bool DoExecute(Args &command, CommandReturnObject &result) override {
2219 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2220 if (target == nullptr) {
2221 result.AppendError("invalid target, create a debug target using the "
2222 "'target create' command");
2223 result.SetStatus(eReturnStatusFailed);
2224 return false;
2225 } else {
2226 uint32_t num_dumped = 0;
2227
2228 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2229 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2230 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2231
2232 if (command.GetArgumentCount() == 0) {
2233 // Dump all sections for all modules images
2234 const ModuleList &target_modules = target->GetImages();
2235 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2236 const size_t num_modules = target_modules.GetSize();
2237 if (num_modules > 0) {
2238 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64
2239 " modules.\n",
2240 (uint64_t)num_modules);
2241 for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2242 if (DumpModuleSymbolVendor(
2243 result.GetOutputStream(),
2244 target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
2245 num_dumped++;
2246 }
2247 } else {
2248 result.AppendError("the target has no associated executable images");
2249 result.SetStatus(eReturnStatusFailed);
2250 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002251 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002252 } else {
2253 // Dump specified images (by basename or fullpath)
2254 const char *arg_cstr;
2255 for (int arg_idx = 0;
2256 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2257 ++arg_idx) {
2258 ModuleList module_list;
2259 const size_t num_matches =
2260 FindModulesByName(target, arg_cstr, module_list, true);
2261 if (num_matches > 0) {
2262 for (size_t i = 0; i < num_matches; ++i) {
2263 Module *module = module_list.GetModulePointerAtIndex(i);
2264 if (module) {
2265 if (DumpModuleSymbolVendor(result.GetOutputStream(), module))
2266 num_dumped++;
2267 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002269 } else
2270 result.AppendWarningWithFormat(
2271 "Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002272 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273 }
2274
2275 if (num_dumped > 0)
2276 result.SetStatus(eReturnStatusSuccessFinishResult);
2277 else {
2278 result.AppendError("no matching executable images found");
2279 result.SetStatus(eReturnStatusFailed);
2280 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002281 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002282 return result.Succeeded();
2283 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002284};
2285
Greg Claytoneffe5c92011-05-03 22:09:39 +00002286#pragma mark CommandObjectTargetModulesDumpLineTable
2287
2288//----------------------------------------------------------------------
2289// Image debug line table dumping command
2290//----------------------------------------------------------------------
2291
Kate Stoneb9c1b512016-09-06 20:57:50 +00002292class CommandObjectTargetModulesDumpLineTable
2293 : public CommandObjectTargetModulesSourceFileAutoComplete {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002294public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295 CommandObjectTargetModulesDumpLineTable(CommandInterpreter &interpreter)
2296 : CommandObjectTargetModulesSourceFileAutoComplete(
2297 interpreter, "target modules dump line-table",
2298 "Dump the line table for one or more compilation units.", nullptr,
2299 eCommandRequiresTarget) {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002300
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301 ~CommandObjectTargetModulesDumpLineTable() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002302
Jim Ingham5a988412012-06-08 21:56:10 +00002303protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002304 bool DoExecute(Args &command, CommandReturnObject &result) override {
2305 Target *target = m_exe_ctx.GetTargetPtr();
2306 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002307
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2309 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2310 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002311
Kate Stoneb9c1b512016-09-06 20:57:50 +00002312 if (command.GetArgumentCount() == 0) {
2313 result.AppendError("file option must be specified.");
2314 result.SetStatus(eReturnStatusFailed);
2315 return result.Succeeded();
2316 } else {
2317 // Dump specified images (by basename or fullpath)
2318 const char *arg_cstr;
2319 for (int arg_idx = 0;
2320 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2321 ++arg_idx) {
2322 FileSpec file_spec(arg_cstr, false);
2323
2324 const ModuleList &target_modules = target->GetImages();
2325 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2326 const size_t num_modules = target_modules.GetSize();
2327 if (num_modules > 0) {
2328 uint32_t num_dumped = 0;
2329 for (uint32_t i = 0; i < num_modules; ++i) {
2330 if (DumpCompileUnitLineTable(
2331 m_interpreter, result.GetOutputStream(),
2332 target_modules.GetModulePointerAtIndexUnlocked(i),
2333 file_spec, m_exe_ctx.GetProcessPtr() &&
2334 m_exe_ctx.GetProcessRef().IsAlive()))
2335 num_dumped++;
2336 }
2337 if (num_dumped == 0)
2338 result.AppendWarningWithFormat(
2339 "No source filenames matched '%s'.\n", arg_cstr);
2340 else
2341 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002342 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002343 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345
2346 if (total_num_dumped > 0)
2347 result.SetStatus(eReturnStatusSuccessFinishResult);
2348 else {
2349 result.AppendError("no source filenames matched any command arguments");
2350 result.SetStatus(eReturnStatusFailed);
2351 }
2352 return result.Succeeded();
2353 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002354};
2355
Greg Claytoneffe5c92011-05-03 22:09:39 +00002356#pragma mark CommandObjectTargetModulesDump
2357
2358//----------------------------------------------------------------------
2359// Dump multi-word command for target modules
2360//----------------------------------------------------------------------
2361
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362class CommandObjectTargetModulesDump : public CommandObjectMultiword {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002363public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 //------------------------------------------------------------------
2365 // Constructors and Destructors
2366 //------------------------------------------------------------------
2367 CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
2368 : CommandObjectMultiword(interpreter, "target modules dump",
2369 "Commands for dumping information about one or "
2370 "more target modules.",
2371 "target modules dump "
2372 "[headers|symtab|sections|symfile|line-table] "
2373 "[<file1> <file2> ...]") {
2374 LoadSubCommand("objfile",
2375 CommandObjectSP(
2376 new CommandObjectTargetModulesDumpObjfile(interpreter)));
2377 LoadSubCommand(
2378 "symtab",
2379 CommandObjectSP(new CommandObjectTargetModulesDumpSymtab(interpreter)));
2380 LoadSubCommand("sections",
2381 CommandObjectSP(new CommandObjectTargetModulesDumpSections(
2382 interpreter)));
2383 LoadSubCommand("symfile",
2384 CommandObjectSP(
2385 new CommandObjectTargetModulesDumpSymfile(interpreter)));
2386 LoadSubCommand("line-table",
2387 CommandObjectSP(new CommandObjectTargetModulesDumpLineTable(
2388 interpreter)));
2389 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002390
Kate Stoneb9c1b512016-09-06 20:57:50 +00002391 ~CommandObjectTargetModulesDump() override = default;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002392};
2393
Kate Stoneb9c1b512016-09-06 20:57:50 +00002394class CommandObjectTargetModulesAdd : public CommandObjectParsed {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002395public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002396 CommandObjectTargetModulesAdd(CommandInterpreter &interpreter)
2397 : CommandObjectParsed(interpreter, "target modules add",
2398 "Add a new module to the current target's modules.",
2399 "target modules add [<module>]"),
Todd Fialae1cfbc72016-08-11 23:51:28 +00002400 m_option_group(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002401 m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0,
2402 eArgTypeFilename, "Fullpath to a stand alone debug "
2403 "symbols file for when debug symbols "
2404 "are not in the executable.") {
2405 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2406 LLDB_OPT_SET_1);
2407 m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2408 m_option_group.Finalize();
2409 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002410
Kate Stoneb9c1b512016-09-06 20:57:50 +00002411 ~CommandObjectTargetModulesAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002412
Kate Stoneb9c1b512016-09-06 20:57:50 +00002413 Options *GetOptions() override { return &m_option_group; }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002414
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 int HandleArgumentCompletion(Args &input, int &cursor_index,
2416 int &cursor_char_position,
2417 OptionElementVector &opt_element_vector,
2418 int match_start_point, int max_return_elements,
2419 bool &word_complete,
2420 StringList &matches) override {
2421 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
2422 completion_str.erase(cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002423
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 CommandCompletions::InvokeCommonCompletionCallbacks(
2425 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
2426 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
2427 word_complete, matches);
2428 return matches.GetSize();
2429 }
Jim Ingham5a988412012-06-08 21:56:10 +00002430
2431protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002432 OptionGroupOptions m_option_group;
2433 OptionGroupUUID m_uuid_option_group;
2434 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002435
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 bool DoExecute(Args &args, CommandReturnObject &result) override {
2437 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2438 if (target == nullptr) {
2439 result.AppendError("invalid target, create a debug target using the "
2440 "'target create' command");
2441 result.SetStatus(eReturnStatusFailed);
2442 return false;
2443 } else {
2444 bool flush = false;
2445
2446 const size_t argc = args.GetArgumentCount();
2447 if (argc == 0) {
2448 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2449 // We are given a UUID only, go locate the file
2450 ModuleSpec module_spec;
2451 module_spec.GetUUID() =
2452 m_uuid_option_group.GetOptionValue().GetCurrentValue();
2453 if (m_symbol_file.GetOptionValue().OptionWasSet())
2454 module_spec.GetSymbolFileSpec() =
2455 m_symbol_file.GetOptionValue().GetCurrentValue();
2456 if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
2457 ModuleSP module_sp(target->GetSharedModule(module_spec));
2458 if (module_sp) {
2459 result.SetStatus(eReturnStatusSuccessFinishResult);
2460 return true;
2461 } else {
2462 StreamString strm;
2463 module_spec.GetUUID().Dump(&strm);
2464 if (module_spec.GetFileSpec()) {
2465 if (module_spec.GetSymbolFileSpec()) {
2466 result.AppendErrorWithFormat(
2467 "Unable to create the executable or symbol file with "
2468 "UUID %s with path %s and symbol file %s",
Zachary Turnerc1564272016-11-16 21:15:24 +00002469 strm.GetData(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002470 module_spec.GetFileSpec().GetPath().c_str(),
2471 module_spec.GetSymbolFileSpec().GetPath().c_str());
2472 } else {
2473 result.AppendErrorWithFormat(
2474 "Unable to create the executable or symbol file with "
2475 "UUID %s with path %s",
Zachary Turnerc1564272016-11-16 21:15:24 +00002476 strm.GetData(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002477 module_spec.GetFileSpec().GetPath().c_str());
2478 }
2479 } else {
2480 result.AppendErrorWithFormat("Unable to create the executable "
2481 "or symbol file with UUID %s",
Zachary Turnerc1564272016-11-16 21:15:24 +00002482 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483 }
2484 result.SetStatus(eReturnStatusFailed);
2485 return false;
2486 }
2487 } else {
2488 StreamString strm;
2489 module_spec.GetUUID().Dump(&strm);
2490 result.AppendErrorWithFormat(
2491 "Unable to locate the executable or symbol file with UUID %s",
Zachary Turnerc1564272016-11-16 21:15:24 +00002492 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 result.SetStatus(eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002494 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 }
2496 } else {
2497 result.AppendError(
2498 "one or more executable image paths must be specified");
2499 result.SetStatus(eReturnStatusFailed);
2500 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002501 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002502 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00002503 for (auto &entry : args.entries()) {
2504 if (entry.ref.empty())
2505 continue;
2506
2507 FileSpec file_spec(entry.ref, true);
2508 if (file_spec.Exists()) {
2509 ModuleSpec module_spec(file_spec);
2510 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
2511 module_spec.GetUUID() =
2512 m_uuid_option_group.GetOptionValue().GetCurrentValue();
2513 if (m_symbol_file.GetOptionValue().OptionWasSet())
2514 module_spec.GetSymbolFileSpec() =
2515 m_symbol_file.GetOptionValue().GetCurrentValue();
2516 if (!module_spec.GetArchitecture().IsValid())
2517 module_spec.GetArchitecture() = target->GetArchitecture();
Zachary Turner97206d52017-05-12 04:51:55 +00002518 Status error;
Zachary Turner97d2c402016-10-05 23:40:23 +00002519 ModuleSP module_sp(target->GetSharedModule(module_spec, &error));
2520 if (!module_sp) {
2521 const char *error_cstr = error.AsCString();
2522 if (error_cstr)
2523 result.AppendError(error_cstr);
2524 else
2525 result.AppendErrorWithFormat("unsupported module: %s",
2526 entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 result.SetStatus(eReturnStatusFailed);
Zachary Turner97d2c402016-10-05 23:40:23 +00002528 return false;
2529 } else {
2530 flush = true;
2531 }
2532 result.SetStatus(eReturnStatusSuccessFinishResult);
2533 } else {
2534 std::string resolved_path = file_spec.GetPath();
2535 result.SetStatus(eReturnStatusFailed);
2536 if (resolved_path != entry.ref) {
2537 result.AppendErrorWithFormat(
2538 "invalid module path '%s' with resolved path '%s'\n",
2539 entry.ref.str().c_str(), resolved_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002540 break;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002541 }
Zachary Turner97d2c402016-10-05 23:40:23 +00002542 result.AppendErrorWithFormat("invalid module path '%s'\n",
2543 entry.c_str());
2544 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002545 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002546 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002547 }
2548
2549 if (flush) {
2550 ProcessSP process = target->GetProcessSP();
2551 if (process)
2552 process->Flush();
2553 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002554 }
2555
Kate Stoneb9c1b512016-09-06 20:57:50 +00002556 return result.Succeeded();
2557 }
2558};
2559
2560class CommandObjectTargetModulesLoad
2561 : public CommandObjectTargetModulesModuleAutoComplete {
2562public:
2563 CommandObjectTargetModulesLoad(CommandInterpreter &interpreter)
2564 : CommandObjectTargetModulesModuleAutoComplete(
2565 interpreter, "target modules load", "Set the load addresses for "
2566 "one or more sections in a "
2567 "target module.",
2568 "target modules load [--file <module> --uuid <uuid>] <sect-name> "
2569 "<address> [<sect-name> <address> ....]"),
2570 m_option_group(),
2571 m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName,
2572 "Fullpath or basename for module to load.", ""),
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002573 m_load_option(LLDB_OPT_SET_1, false, "load", 'l',
Hafiz Abid Qadeerb10fb962017-01-24 23:07:27 +00002574 "Write file contents to the memory.", false, true),
2575 m_pc_option(LLDB_OPT_SET_1, false, "--set-pc-to-entry", 'p',
2576 "Set PC to the entry point."
2577 " Only applicable with '--load' option.",
2578 false, true),
Kate Stoneb9c1b512016-09-06 20:57:50 +00002579 m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset,
2580 "Set the load address for all sections to be the "
2581 "virtual address in the file plus the offset.",
2582 0) {
2583 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2584 LLDB_OPT_SET_1);
2585 m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002586 m_option_group.Append(&m_load_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Hafiz Abid Qadeerb10fb962017-01-24 23:07:27 +00002587 m_option_group.Append(&m_pc_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002588 m_option_group.Append(&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2589 m_option_group.Finalize();
2590 }
2591
2592 ~CommandObjectTargetModulesLoad() override = default;
2593
2594 Options *GetOptions() override { return &m_option_group; }
2595
2596protected:
2597 bool DoExecute(Args &args, CommandReturnObject &result) override {
2598 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002599 const bool load = m_load_option.GetOptionValue().GetCurrentValue();
Hafiz Abid Qadeerb10fb962017-01-24 23:07:27 +00002600 const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002601 if (target == nullptr) {
2602 result.AppendError("invalid target, create a debug target using the "
2603 "'target create' command");
2604 result.SetStatus(eReturnStatusFailed);
2605 return false;
2606 } else {
2607 const size_t argc = args.GetArgumentCount();
2608 ModuleSpec module_spec;
2609 bool search_using_module_spec = false;
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002610
2611 // Allow "load" option to work without --file or --uuid
2612 // option.
2613 if (load) {
2614 if (!m_file_option.GetOptionValue().OptionWasSet() &&
2615 !m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2616 ModuleList &module_list = target->GetImages();
2617 if (module_list.GetSize() == 1) {
2618 search_using_module_spec = true;
2619 module_spec.GetFileSpec() =
2620 module_list.GetModuleAtIndex(0)->GetFileSpec();
2621 }
2622 }
2623 }
2624
Kate Stoneb9c1b512016-09-06 20:57:50 +00002625 if (m_file_option.GetOptionValue().OptionWasSet()) {
2626 search_using_module_spec = true;
2627 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2628 const bool use_global_module_list = true;
2629 ModuleList module_list;
2630 const size_t num_matches = FindModulesByName(
2631 target, arg_cstr, module_list, use_global_module_list);
2632 if (num_matches == 1) {
2633 module_spec.GetFileSpec() =
2634 module_list.GetModuleAtIndex(0)->GetFileSpec();
2635 } else if (num_matches > 1) {
2636 search_using_module_spec = false;
2637 result.AppendErrorWithFormat(
2638 "more than 1 module matched by name '%s'\n", arg_cstr);
2639 result.SetStatus(eReturnStatusFailed);
2640 } else {
2641 search_using_module_spec = false;
2642 result.AppendErrorWithFormat("no object file for module '%s'\n",
2643 arg_cstr);
2644 result.SetStatus(eReturnStatusFailed);
2645 }
2646 }
2647
2648 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2649 search_using_module_spec = true;
2650 module_spec.GetUUID() =
2651 m_uuid_option_group.GetOptionValue().GetCurrentValue();
2652 }
2653
2654 if (search_using_module_spec) {
2655 ModuleList matching_modules;
2656 const size_t num_matches =
2657 target->GetImages().FindModules(module_spec, matching_modules);
2658
2659 char path[PATH_MAX];
2660 if (num_matches == 1) {
2661 Module *module = matching_modules.GetModulePointerAtIndex(0);
2662 if (module) {
2663 ObjectFile *objfile = module->GetObjectFile();
2664 if (objfile) {
2665 SectionList *section_list = module->GetSectionList();
2666 if (section_list) {
2667 bool changed = false;
2668 if (argc == 0) {
2669 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2670 const addr_t slide =
2671 m_slide_option.GetOptionValue().GetCurrentValue();
2672 const bool slide_is_offset = true;
2673 module->SetLoadAddress(*target, slide, slide_is_offset,
2674 changed);
2675 } else {
2676 result.AppendError("one or more section name + load "
2677 "address pair must be specified");
2678 result.SetStatus(eReturnStatusFailed);
2679 return false;
2680 }
2681 } else {
2682 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2683 result.AppendError("The \"--slide <offset>\" option can't "
2684 "be used in conjunction with setting "
2685 "section load addresses.\n");
2686 result.SetStatus(eReturnStatusFailed);
2687 return false;
2688 }
2689
2690 for (size_t i = 0; i < argc; i += 2) {
2691 const char *sect_name = args.GetArgumentAtIndex(i);
2692 const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1);
2693 if (sect_name && load_addr_cstr) {
2694 ConstString const_sect_name(sect_name);
2695 bool success = false;
2696 addr_t load_addr = StringConvert::ToUInt64(
2697 load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2698 if (success) {
2699 SectionSP section_sp(
2700 section_list->FindSectionByName(const_sect_name));
2701 if (section_sp) {
2702 if (section_sp->IsThreadSpecific()) {
2703 result.AppendErrorWithFormat(
2704 "thread specific sections are not yet "
2705 "supported (section '%s')\n",
2706 sect_name);
2707 result.SetStatus(eReturnStatusFailed);
2708 break;
2709 } else {
2710 if (target->GetSectionLoadList()
2711 .SetSectionLoadAddress(section_sp,
2712 load_addr))
2713 changed = true;
2714 result.AppendMessageWithFormat(
2715 "section '%s' loaded at 0x%" PRIx64 "\n",
2716 sect_name, load_addr);
2717 }
2718 } else {
2719 result.AppendErrorWithFormat("no section found that "
2720 "matches the section "
2721 "name '%s'\n",
2722 sect_name);
2723 result.SetStatus(eReturnStatusFailed);
2724 break;
2725 }
2726 } else {
2727 result.AppendErrorWithFormat(
2728 "invalid load address string '%s'\n",
2729 load_addr_cstr);
2730 result.SetStatus(eReturnStatusFailed);
2731 break;
2732 }
2733 } else {
2734 if (sect_name)
2735 result.AppendError("section names must be followed by "
2736 "a load address.\n");
2737 else
2738 result.AppendError("one or more section name + load "
2739 "address pair must be specified.\n");
2740 result.SetStatus(eReturnStatusFailed);
2741 break;
2742 }
2743 }
2744 }
2745
2746 if (changed) {
2747 target->ModulesDidLoad(matching_modules);
2748 Process *process = m_exe_ctx.GetProcessPtr();
2749 if (process)
2750 process->Flush();
2751 }
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002752 if (load) {
Zachary Turner97206d52017-05-12 04:51:55 +00002753 Status error = module->LoadInMemory(*target, set_pc);
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002754 if (error.Fail()) {
2755 result.AppendError(error.AsCString());
2756 return false;
2757 }
2758 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002759 } else {
2760 module->GetFileSpec().GetPath(path, sizeof(path));
2761 result.AppendErrorWithFormat(
2762 "no sections in object file '%s'\n", path);
2763 result.SetStatus(eReturnStatusFailed);
2764 }
2765 } else {
2766 module->GetFileSpec().GetPath(path, sizeof(path));
2767 result.AppendErrorWithFormat("no object file for module '%s'\n",
2768 path);
2769 result.SetStatus(eReturnStatusFailed);
2770 }
2771 } else {
2772 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2773 if (module_spec_file) {
2774 module_spec_file->GetPath(path, sizeof(path));
2775 result.AppendErrorWithFormat("invalid module '%s'.\n", path);
2776 } else
2777 result.AppendError("no module spec");
2778 result.SetStatus(eReturnStatusFailed);
2779 }
2780 } else {
2781 std::string uuid_str;
2782
2783 if (module_spec.GetFileSpec())
2784 module_spec.GetFileSpec().GetPath(path, sizeof(path));
2785 else
2786 path[0] = '\0';
2787
2788 if (module_spec.GetUUIDPtr())
2789 uuid_str = module_spec.GetUUID().GetAsString();
2790 if (num_matches > 1) {
2791 result.AppendErrorWithFormat(
2792 "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "",
2793 path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str());
2794 for (size_t i = 0; i < num_matches; ++i) {
2795 if (matching_modules.GetModulePointerAtIndex(i)
2796 ->GetFileSpec()
2797 .GetPath(path, sizeof(path)))
2798 result.AppendMessageWithFormat("%s\n", path);
2799 }
2800 } else {
2801 result.AppendErrorWithFormat(
2802 "no modules were found that match%s%s%s%s.\n",
2803 path[0] ? " file=" : "", path,
2804 !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str());
2805 }
2806 result.SetStatus(eReturnStatusFailed);
2807 }
2808 } else {
2809 result.AppendError("either the \"--file <module>\" or the \"--uuid "
2810 "<uuid>\" option must be specified.\n");
2811 result.SetStatus(eReturnStatusFailed);
2812 return false;
2813 }
2814 }
2815 return result.Succeeded();
2816 }
2817
2818 OptionGroupOptions m_option_group;
2819 OptionGroupUUID m_uuid_option_group;
2820 OptionGroupString m_file_option;
Hafiz Abid Qadeer4687db02017-01-19 17:32:50 +00002821 OptionGroupBoolean m_load_option;
Hafiz Abid Qadeerb10fb962017-01-24 23:07:27 +00002822 OptionGroupBoolean m_pc_option;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 OptionGroupUInt64 m_slide_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002824};
2825
2826//----------------------------------------------------------------------
2827// List images with associated information
2828//----------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +00002829
2830static OptionDefinition g_target_modules_list_options[] = {
2831 // clang-format off
2832 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Display the image at this address." },
2833 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the architecture when listing images." },
2834 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the triple when listing images." },
2835 { LLDB_OPT_SET_1, false, "header", 'h', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the image header address as a load address if debugging, a file address otherwise." },
2836 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the image header address offset from the header file address (the slide amount)." },
2837 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the UUID when listing images." },
2838 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image object file." },
2839 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the directory with optional width for the image object file." },
2840 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the basename with optional width for the image object file." },
2841 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width." },
2842 { LLDB_OPT_SET_1, false, "symfile-unique", 'S', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the symbol file with optional width only if it is different from the executable object file." },
2843 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the modification time with optional width of the module." },
2844 { LLDB_OPT_SET_1, false, "ref-count", 'r', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the reference count if the module is still in the shared module cache." },
2845 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the module pointer." },
2846 { LLDB_OPT_SET_1, false, "global", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the modules from the global module list, not just the current target." }
2847 // clang-format on
2848};
2849
Kate Stoneb9c1b512016-09-06 20:57:50 +00002850class CommandObjectTargetModulesList : public CommandObjectParsed {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002851public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002852 class CommandOptions : public Options {
2853 public:
2854 CommandOptions()
2855 : Options(), m_format_array(), m_use_global_module_list(false),
2856 m_module_addr(LLDB_INVALID_ADDRESS) {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002857
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002859
Zachary Turner97206d52017-05-12 04:51:55 +00002860 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2861 ExecutionContext *execution_context) override {
2862 Status error;
Greg Claytonb9d5df52012-12-06 22:49:16 +00002863
Kate Stoneb9c1b512016-09-06 20:57:50 +00002864 const int short_option = m_getopt_table[option_idx].val;
2865 if (short_option == 'g') {
2866 m_use_global_module_list = true;
2867 } else if (short_option == 'a') {
2868 m_module_addr = Args::StringToAddress(execution_context, option_arg,
2869 LLDB_INVALID_ADDRESS, &error);
2870 } else {
2871 unsigned long width = 0;
Zachary Turnerfe114832016-11-12 16:56:47 +00002872 option_arg.getAsInteger(0, width);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002873 m_format_array.push_back(std::make_pair(short_option, width));
2874 }
2875 return error;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002876 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002877
Kate Stoneb9c1b512016-09-06 20:57:50 +00002878 void OptionParsingStarting(ExecutionContext *execution_context) override {
2879 m_format_array.clear();
2880 m_use_global_module_list = false;
2881 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002882 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002883
Zachary Turner1f0f5b52016-09-22 20:22:55 +00002884 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00002885 return llvm::makeArrayRef(g_target_modules_list_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00002886 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002887
2888 // Instance variables to hold the values for command options.
2889 typedef std::vector<std::pair<char, uint32_t>> FormatWidthCollection;
2890 FormatWidthCollection m_format_array;
2891 bool m_use_global_module_list;
2892 lldb::addr_t m_module_addr;
2893 };
2894
2895 CommandObjectTargetModulesList(CommandInterpreter &interpreter)
2896 : CommandObjectParsed(
2897 interpreter, "target modules list",
2898 "List current executable and dependent shared library images.",
2899 "target modules list [<cmd-options>]"),
2900 m_options() {}
2901
2902 ~CommandObjectTargetModulesList() override = default;
2903
2904 Options *GetOptions() override { return &m_options; }
2905
Jim Ingham5a988412012-06-08 21:56:10 +00002906protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002907 bool DoExecute(Args &command, CommandReturnObject &result) override {
2908 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2909 const bool use_global_module_list = m_options.m_use_global_module_list;
2910 // Define a local module list here to ensure it lives longer than any
2911 // "locker"
2912 // object which might lock its contents below (through the "module_list_ptr"
2913 // variable).
2914 ModuleList module_list;
2915 if (target == nullptr && !use_global_module_list) {
2916 result.AppendError("invalid target, create a debug target using the "
2917 "'target create' command");
2918 result.SetStatus(eReturnStatusFailed);
2919 return false;
2920 } else {
2921 if (target) {
2922 uint32_t addr_byte_size =
2923 target->GetArchitecture().GetAddressByteSize();
2924 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2925 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2926 }
2927 // Dump all sections for all modules images
2928 Stream &strm = result.GetOutputStream();
2929
2930 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS) {
2931 if (target) {
2932 Address module_address;
2933 if (module_address.SetLoadAddress(m_options.m_module_addr, target)) {
2934 ModuleSP module_sp(module_address.GetModule());
2935 if (module_sp) {
2936 PrintModule(target, module_sp.get(), 0, strm);
2937 result.SetStatus(eReturnStatusSuccessFinishResult);
2938 } else {
2939 result.AppendErrorWithFormat(
2940 "Couldn't find module matching address: 0x%" PRIx64 ".",
2941 m_options.m_module_addr);
2942 result.SetStatus(eReturnStatusFailed);
Greg Clayton3418c852011-08-10 02:10:13 +00002943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002944 } else {
2945 result.AppendErrorWithFormat(
2946 "Couldn't find module containing address: 0x%" PRIx64 ".",
2947 m_options.m_module_addr);
2948 result.SetStatus(eReturnStatusFailed);
2949 }
2950 } else {
2951 result.AppendError(
2952 "Can only look up modules by address with a valid target.");
2953 result.SetStatus(eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002954 }
2955 return result.Succeeded();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002956 }
Jim Inghamc10312c2011-10-24 18:36:33 +00002957
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958 size_t num_modules = 0;
2959
2960 // This locker will be locked on the mutex in module_list_ptr if it is
2961 // non-nullptr.
2962 // Otherwise it will lock the AllocationModuleCollectionMutex when
2963 // accessing
2964 // the global module list directly.
2965 std::unique_lock<std::recursive_mutex> guard(
2966 Module::GetAllocationModuleCollectionMutex(), std::defer_lock);
2967
2968 const ModuleList *module_list_ptr = nullptr;
2969 const size_t argc = command.GetArgumentCount();
2970 if (argc == 0) {
2971 if (use_global_module_list) {
2972 guard.lock();
2973 num_modules = Module::GetNumberAllocatedModules();
2974 } else {
2975 module_list_ptr = &target->GetImages();
Jim Ingham28eb5712012-10-12 17:34:26 +00002976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002977 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00002978 // TODO: Convert to entry based iteration. Requires converting
2979 // FindModulesByName.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002980 for (size_t i = 0; i < argc; ++i) {
2981 // Dump specified images (by basename or fullpath)
2982 const char *arg_cstr = command.GetArgumentAtIndex(i);
2983 const size_t num_matches = FindModulesByName(
2984 target, arg_cstr, module_list, use_global_module_list);
2985 if (num_matches == 0) {
2986 if (argc == 1) {
2987 result.AppendErrorWithFormat("no modules found that match '%s'",
2988 arg_cstr);
2989 result.SetStatus(eReturnStatusFailed);
2990 return false;
Jim Inghamc10312c2011-10-24 18:36:33 +00002991 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002992 }
Greg Claytonc9660542012-02-05 02:38:54 +00002993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002994
2995 module_list_ptr = &module_list;
2996 }
2997
2998 std::unique_lock<std::recursive_mutex> lock;
2999 if (module_list_ptr != nullptr) {
3000 lock =
3001 std::unique_lock<std::recursive_mutex>(module_list_ptr->GetMutex());
3002
3003 num_modules = module_list_ptr->GetSize();
3004 }
3005
3006 if (num_modules > 0) {
3007 for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
3008 ModuleSP module_sp;
3009 Module *module;
3010 if (module_list_ptr) {
3011 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
3012 module = module_sp.get();
3013 } else {
3014 module = Module::GetAllocatedModuleAtIndex(image_idx);
3015 module_sp = module->shared_from_this();
3016 }
3017
3018 const size_t indent = strm.Printf("[%3u] ", image_idx);
3019 PrintModule(target, module, indent, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003020 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003021 result.SetStatus(eReturnStatusSuccessFinishResult);
3022 } else {
3023 if (argc) {
3024 if (use_global_module_list)
3025 result.AppendError(
3026 "the global module list has no matching modules");
3027 else
3028 result.AppendError("the target has no matching modules");
3029 } else {
3030 if (use_global_module_list)
3031 result.AppendError("the global module list is empty");
3032 else
3033 result.AppendError(
3034 "the target has no associated executable images");
3035 }
3036 result.SetStatus(eReturnStatusFailed);
3037 return false;
3038 }
3039 }
3040 return result.Succeeded();
3041 }
3042
3043 void PrintModule(Target *target, Module *module, int indent, Stream &strm) {
3044 if (module == nullptr) {
3045 strm.PutCString("Null module");
3046 return;
Jim Inghamc10312c2011-10-24 18:36:33 +00003047 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003048
Kate Stoneb9c1b512016-09-06 20:57:50 +00003049 bool dump_object_name = false;
3050 if (m_options.m_format_array.empty()) {
3051 m_options.m_format_array.push_back(std::make_pair('u', 0));
3052 m_options.m_format_array.push_back(std::make_pair('h', 0));
3053 m_options.m_format_array.push_back(std::make_pair('f', 0));
3054 m_options.m_format_array.push_back(std::make_pair('S', 0));
3055 }
3056 const size_t num_entries = m_options.m_format_array.size();
3057 bool print_space = false;
3058 for (size_t i = 0; i < num_entries; ++i) {
3059 if (print_space)
3060 strm.PutChar(' ');
3061 print_space = true;
3062 const char format_char = m_options.m_format_array[i].first;
3063 uint32_t width = m_options.m_format_array[i].second;
3064 switch (format_char) {
3065 case 'A':
3066 DumpModuleArchitecture(strm, module, false, width);
3067 break;
3068
3069 case 't':
3070 DumpModuleArchitecture(strm, module, true, width);
3071 break;
3072
3073 case 'f':
3074 DumpFullpath(strm, &module->GetFileSpec(), width);
3075 dump_object_name = true;
3076 break;
3077
3078 case 'd':
3079 DumpDirectory(strm, &module->GetFileSpec(), width);
3080 break;
3081
3082 case 'b':
3083 DumpBasename(strm, &module->GetFileSpec(), width);
3084 dump_object_name = true;
3085 break;
3086
3087 case 'h':
3088 case 'o':
3089 // Image header address
3090 {
3091 uint32_t addr_nibble_width =
3092 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
3093 : 16;
3094
3095 ObjectFile *objfile = module->GetObjectFile();
3096 if (objfile) {
3097 Address header_addr(objfile->GetHeaderAddress());
3098 if (header_addr.IsValid()) {
3099 if (target && !target->GetSectionLoadList().IsEmpty()) {
3100 lldb::addr_t header_load_addr =
3101 header_addr.GetLoadAddress(target);
3102 if (header_load_addr == LLDB_INVALID_ADDRESS) {
3103 header_addr.Dump(&strm, target,
3104 Address::DumpStyleModuleWithFileAddress,
3105 Address::DumpStyleFileAddress);
3106 } else {
3107 if (format_char == 'o') {
3108 // Show the offset of slide for the image
3109 strm.Printf(
3110 "0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width,
3111 header_load_addr - header_addr.GetFileAddress());
3112 } else {
3113 // Show the load address of the image
3114 strm.Printf("0x%*.*" PRIx64, addr_nibble_width,
3115 addr_nibble_width, header_load_addr);
3116 }
3117 }
3118 break;
3119 }
3120 // The address was valid, but the image isn't loaded, output the
3121 // address in an appropriate format
3122 header_addr.Dump(&strm, target, Address::DumpStyleFileAddress);
3123 break;
3124 }
3125 }
3126 strm.Printf("%*s", addr_nibble_width + 2, "");
3127 }
3128 break;
3129
3130 case 'r': {
3131 size_t ref_count = 0;
3132 ModuleSP module_sp(module->shared_from_this());
3133 if (module_sp) {
3134 // Take one away to make sure we don't count our local "module_sp"
3135 ref_count = module_sp.use_count() - 1;
3136 }
3137 if (width)
3138 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
3139 else
3140 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
3141 } break;
3142
3143 case 's':
3144 case 'S': {
3145 const SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3146 if (symbol_vendor) {
3147 const FileSpec symfile_spec = symbol_vendor->GetMainFileSpec();
3148 if (format_char == 'S') {
3149 // Dump symbol file only if different from module file
3150 if (!symfile_spec || symfile_spec == module->GetFileSpec()) {
3151 print_space = false;
3152 break;
3153 }
3154 // Add a newline and indent past the index
3155 strm.Printf("\n%*s", indent, "");
3156 }
3157 DumpFullpath(strm, &symfile_spec, width);
3158 dump_object_name = true;
3159 break;
3160 }
3161 strm.Printf("%.*s", width, "<NONE>");
3162 } break;
3163
3164 case 'm':
Pavel Labath7e2cfbf2016-11-09 09:59:18 +00003165 DumpTimePoint(module->GetModificationTime(), strm, width);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166 break;
3167
3168 case 'p':
3169 strm.Printf("%p", static_cast<void *>(module));
3170 break;
3171
3172 case 'u':
3173 DumpModuleUUID(strm, module);
3174 break;
3175
3176 default:
3177 break;
3178 }
3179 }
3180 if (dump_object_name) {
3181 const char *object_name = module->GetObjectName().GetCString();
3182 if (object_name)
3183 strm.Printf("(%s)", object_name);
3184 }
3185 strm.EOL();
3186 }
3187
3188 CommandOptions m_options;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003189};
3190
Jason Molenda380241a2012-07-12 00:20:07 +00003191#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003192
Jason Molenda380241a2012-07-12 00:20:07 +00003193//----------------------------------------------------------------------
3194// Lookup unwind information in images
3195//----------------------------------------------------------------------
3196
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003197static OptionDefinition g_target_modules_show_unwind_options[] = {
3198 // clang-format off
3199 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name." },
3200 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address" }
3201 // clang-format on
3202};
3203
Kate Stoneb9c1b512016-09-06 20:57:50 +00003204class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
Jason Molenda380241a2012-07-12 00:20:07 +00003205public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206 enum {
3207 eLookupTypeInvalid = -1,
3208 eLookupTypeAddress = 0,
3209 eLookupTypeSymbol,
3210 eLookupTypeFunction,
3211 eLookupTypeFunctionOrSymbol,
3212 kNumLookupTypes
3213 };
Jason Molenda380241a2012-07-12 00:20:07 +00003214
Kate Stoneb9c1b512016-09-06 20:57:50 +00003215 class CommandOptions : public Options {
3216 public:
3217 CommandOptions()
3218 : Options(), m_type(eLookupTypeInvalid), m_str(),
3219 m_addr(LLDB_INVALID_ADDRESS) {}
Jason Molenda380241a2012-07-12 00:20:07 +00003220
Kate Stoneb9c1b512016-09-06 20:57:50 +00003221 ~CommandOptions() override = default;
Jason Molenda380241a2012-07-12 00:20:07 +00003222
Zachary Turner97206d52017-05-12 04:51:55 +00003223 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3224 ExecutionContext *execution_context) override {
3225 Status error;
Jason Molenda380241a2012-07-12 00:20:07 +00003226
Kate Stoneb9c1b512016-09-06 20:57:50 +00003227 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003228
Kate Stoneb9c1b512016-09-06 20:57:50 +00003229 switch (short_option) {
3230 case 'a': {
3231 m_str = option_arg;
3232 m_type = eLookupTypeAddress;
3233 m_addr = Args::StringToAddress(execution_context, option_arg,
3234 LLDB_INVALID_ADDRESS, &error);
3235 if (m_addr == LLDB_INVALID_ADDRESS)
3236 error.SetErrorStringWithFormat("invalid address string '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00003237 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 break;
3239 }
Jason Molenda380241a2012-07-12 00:20:07 +00003240
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241 case 'n':
3242 m_str = option_arg;
3243 m_type = eLookupTypeFunctionOrSymbol;
3244 break;
Michael Sartainb1e15922013-08-22 20:42:30 +00003245
Kate Stoneb9c1b512016-09-06 20:57:50 +00003246 default:
3247 error.SetErrorStringWithFormat("unrecognized option %c.", short_option);
3248 break;
3249 }
Jason Molenda380241a2012-07-12 00:20:07 +00003250
Kate Stoneb9c1b512016-09-06 20:57:50 +00003251 return error;
Jason Molenda380241a2012-07-12 00:20:07 +00003252 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003253
Kate Stoneb9c1b512016-09-06 20:57:50 +00003254 void OptionParsingStarting(ExecutionContext *execution_context) override {
3255 m_type = eLookupTypeInvalid;
3256 m_str.clear();
3257 m_addr = LLDB_INVALID_ADDRESS;
Jason Molenda380241a2012-07-12 00:20:07 +00003258 }
3259
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003260 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00003261 return llvm::makeArrayRef(g_target_modules_show_unwind_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003262 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003263
3264 // Instance variables to hold the values for command options.
3265
3266 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3267 std::string m_str; // Holds name lookup
3268 lldb::addr_t m_addr; // Holds the address to lookup
3269 };
3270
3271 CommandObjectTargetModulesShowUnwind(CommandInterpreter &interpreter)
3272 : CommandObjectParsed(
3273 interpreter, "target modules show-unwind",
3274 "Show synthesized unwind instructions for a function.", nullptr,
3275 eCommandRequiresTarget | eCommandRequiresProcess |
3276 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
3277 m_options() {}
3278
3279 ~CommandObjectTargetModulesShowUnwind() override = default;
3280
3281 Options *GetOptions() override { return &m_options; }
3282
Jason Molenda380241a2012-07-12 00:20:07 +00003283protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003284 bool DoExecute(Args &command, CommandReturnObject &result) override {
3285 Target *target = m_exe_ctx.GetTargetPtr();
3286 Process *process = m_exe_ctx.GetProcessPtr();
3287 ABI *abi = nullptr;
3288 if (process)
3289 abi = process->GetABI().get();
Jason Molenda380241a2012-07-12 00:20:07 +00003290
Kate Stoneb9c1b512016-09-06 20:57:50 +00003291 if (process == nullptr) {
3292 result.AppendError(
3293 "You must have a process running to use this command.");
3294 result.SetStatus(eReturnStatusFailed);
3295 return false;
Jason Molenda380241a2012-07-12 00:20:07 +00003296 }
3297
Kate Stoneb9c1b512016-09-06 20:57:50 +00003298 ThreadList threads(process->GetThreadList());
3299 if (threads.GetSize() == 0) {
3300 result.AppendError("The process must be paused to use this command.");
3301 result.SetStatus(eReturnStatusFailed);
3302 return false;
3303 }
3304
3305 ThreadSP thread(threads.GetThreadAtIndex(0));
3306 if (!thread) {
3307 result.AppendError("The process must be paused to use this command.");
3308 result.SetStatus(eReturnStatusFailed);
3309 return false;
3310 }
3311
3312 SymbolContextList sc_list;
3313
3314 if (m_options.m_type == eLookupTypeFunctionOrSymbol) {
3315 ConstString function_name(m_options.m_str.c_str());
3316 target->GetImages().FindFunctions(function_name, eFunctionNameTypeAuto,
3317 true, false, true, sc_list);
3318 } else if (m_options.m_type == eLookupTypeAddress && target) {
3319 Address addr;
3320 if (target->GetSectionLoadList().ResolveLoadAddress(m_options.m_addr,
3321 addr)) {
3322 SymbolContext sc;
3323 ModuleSP module_sp(addr.GetModule());
3324 module_sp->ResolveSymbolContextForAddress(addr,
3325 eSymbolContextEverything, sc);
3326 if (sc.function || sc.symbol) {
3327 sc_list.Append(sc);
3328 }
3329 }
3330 } else {
3331 result.AppendError(
3332 "address-expression or function name option must be specified.");
3333 result.SetStatus(eReturnStatusFailed);
3334 return false;
3335 }
3336
3337 size_t num_matches = sc_list.GetSize();
3338 if (num_matches == 0) {
3339 result.AppendErrorWithFormat("no unwind data found that matches '%s'.",
3340 m_options.m_str.c_str());
3341 result.SetStatus(eReturnStatusFailed);
3342 return false;
3343 }
3344
3345 for (uint32_t idx = 0; idx < num_matches; idx++) {
3346 SymbolContext sc;
3347 sc_list.GetContextAtIndex(idx, sc);
3348 if (sc.symbol == nullptr && sc.function == nullptr)
3349 continue;
3350 if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
3351 continue;
3352 AddressRange range;
3353 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
3354 false, range))
3355 continue;
3356 if (!range.GetBaseAddress().IsValid())
3357 continue;
3358 ConstString funcname(sc.GetFunctionName());
3359 if (funcname.IsEmpty())
3360 continue;
3361 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3362 if (abi)
3363 start_addr = abi->FixCodeAddress(start_addr);
3364
3365 FuncUnwindersSP func_unwinders_sp(
3366 sc.module_sp->GetObjectFile()
3367 ->GetUnwindTable()
3368 .GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3369 if (!func_unwinders_sp)
3370 continue;
3371
3372 result.GetOutputStream().Printf(
3373 "UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n\n",
3374 sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(),
3375 funcname.AsCString(), start_addr);
3376
3377 UnwindPlanSP non_callsite_unwind_plan =
3378 func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread, -1);
3379 if (non_callsite_unwind_plan) {
3380 result.GetOutputStream().Printf(
3381 "Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n",
3382 non_callsite_unwind_plan->GetSourceName().AsCString());
3383 }
3384 UnwindPlanSP callsite_unwind_plan =
3385 func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
3386 if (callsite_unwind_plan) {
3387 result.GetOutputStream().Printf(
3388 "Synchronous (restricted to call-sites) UnwindPlan is '%s'\n",
3389 callsite_unwind_plan->GetSourceName().AsCString());
3390 }
3391 UnwindPlanSP fast_unwind_plan =
3392 func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread);
3393 if (fast_unwind_plan) {
3394 result.GetOutputStream().Printf(
3395 "Fast UnwindPlan is '%s'\n",
3396 fast_unwind_plan->GetSourceName().AsCString());
3397 }
3398
3399 result.GetOutputStream().Printf("\n");
3400
3401 UnwindPlanSP assembly_sp =
3402 func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread, 0);
3403 if (assembly_sp) {
3404 result.GetOutputStream().Printf(
3405 "Assembly language inspection UnwindPlan:\n");
3406 assembly_sp->Dump(result.GetOutputStream(), thread.get(),
3407 LLDB_INVALID_ADDRESS);
3408 result.GetOutputStream().Printf("\n");
3409 }
3410
3411 UnwindPlanSP ehframe_sp =
3412 func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3413 if (ehframe_sp) {
3414 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3415 ehframe_sp->Dump(result.GetOutputStream(), thread.get(),
3416 LLDB_INVALID_ADDRESS);
3417 result.GetOutputStream().Printf("\n");
3418 }
3419
3420 UnwindPlanSP ehframe_augmented_sp =
3421 func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread, 0);
3422 if (ehframe_augmented_sp) {
3423 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3424 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(),
3425 LLDB_INVALID_ADDRESS);
3426 result.GetOutputStream().Printf("\n");
3427 }
3428
Pavel Labathcdda23e2017-06-27 11:16:26 +00003429 if (UnwindPlanSP plan_sp =
3430 func_unwinders_sp->GetDebugFrameUnwindPlan(*target, 0)) {
3431 result.GetOutputStream().Printf("debug_frame UnwindPlan:\n");
3432 plan_sp->Dump(result.GetOutputStream(), thread.get(),
3433 LLDB_INVALID_ADDRESS);
3434 result.GetOutputStream().Printf("\n");
3435 }
3436
3437 if (UnwindPlanSP plan_sp =
3438 func_unwinders_sp->GetDebugFrameAugmentedUnwindPlan(*target,
3439 *thread, 0)) {
3440 result.GetOutputStream().Printf("debug_frame augmented UnwindPlan:\n");
3441 plan_sp->Dump(result.GetOutputStream(), thread.get(),
3442 LLDB_INVALID_ADDRESS);
3443 result.GetOutputStream().Printf("\n");
3444 }
3445
Kate Stoneb9c1b512016-09-06 20:57:50 +00003446 UnwindPlanSP arm_unwind_sp =
3447 func_unwinders_sp->GetArmUnwindUnwindPlan(*target, 0);
3448 if (arm_unwind_sp) {
3449 result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n");
3450 arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3451 LLDB_INVALID_ADDRESS);
3452 result.GetOutputStream().Printf("\n");
3453 }
3454
3455 UnwindPlanSP compact_unwind_sp =
3456 func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3457 if (compact_unwind_sp) {
3458 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3459 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3460 LLDB_INVALID_ADDRESS);
3461 result.GetOutputStream().Printf("\n");
3462 }
3463
3464 if (fast_unwind_plan) {
3465 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3466 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(),
3467 LLDB_INVALID_ADDRESS);
3468 result.GetOutputStream().Printf("\n");
3469 }
3470
3471 ABISP abi_sp = process->GetABI();
3472 if (abi_sp) {
3473 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3474 if (abi_sp->CreateDefaultUnwindPlan(arch_default)) {
3475 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3476 arch_default.Dump(result.GetOutputStream(), thread.get(),
3477 LLDB_INVALID_ADDRESS);
3478 result.GetOutputStream().Printf("\n");
3479 }
3480
3481 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3482 if (abi_sp->CreateFunctionEntryUnwindPlan(arch_entry)) {
3483 result.GetOutputStream().Printf(
3484 "Arch default at entry point UnwindPlan:\n");
3485 arch_entry.Dump(result.GetOutputStream(), thread.get(),
3486 LLDB_INVALID_ADDRESS);
3487 result.GetOutputStream().Printf("\n");
3488 }
3489 }
3490
3491 result.GetOutputStream().Printf("\n");
3492 }
3493 return result.Succeeded();
3494 }
3495
3496 CommandOptions m_options;
Jason Molenda380241a2012-07-12 00:20:07 +00003497};
3498
Greg Claytoneffe5c92011-05-03 22:09:39 +00003499//----------------------------------------------------------------------
3500// Lookup information in images
3501//----------------------------------------------------------------------
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003502
3503static OptionDefinition g_target_modules_lookup_options[] = {
3504 // clang-format off
3505 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules." },
3506 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup." },
3507 /* FIXME: re-enable regex for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */
3508 { LLDB_OPT_SET_2 | LLDB_OPT_SET_4 | LLDB_OPT_SET_5, false, "regex", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions." },
3509 { LLDB_OPT_SET_2, true, "symbol", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules." },
3510 { LLDB_OPT_SET_3, true, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules." },
3511 { LLDB_OPT_SET_3, false, "line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)." },
3512 { LLDB_OPT_SET_FROM_TO(3,5), false, "no-inlines", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)." },
3513 { LLDB_OPT_SET_4, true, "function", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules." },
3514 { LLDB_OPT_SET_5, true, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules." },
3515 { LLDB_OPT_SET_6, true, "type", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules." },
3516 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose lookup information." },
3517 { LLDB_OPT_SET_ALL, false, "all", 'A', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available." },
3518 // clang-format on
3519};
3520
Kate Stoneb9c1b512016-09-06 20:57:50 +00003521class CommandObjectTargetModulesLookup : public CommandObjectParsed {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003522public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003523 enum {
3524 eLookupTypeInvalid = -1,
3525 eLookupTypeAddress = 0,
3526 eLookupTypeSymbol,
3527 eLookupTypeFileLine, // Line is optional
3528 eLookupTypeFunction,
3529 eLookupTypeFunctionOrSymbol,
3530 eLookupTypeType,
3531 kNumLookupTypes
3532 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003533
Kate Stoneb9c1b512016-09-06 20:57:50 +00003534 class CommandOptions : public Options {
3535 public:
3536 CommandOptions() : Options() { OptionParsingStarting(nullptr); }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003537
Kate Stoneb9c1b512016-09-06 20:57:50 +00003538 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003539
Zachary Turner97206d52017-05-12 04:51:55 +00003540 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3541 ExecutionContext *execution_context) override {
3542 Status error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003543
Kate Stoneb9c1b512016-09-06 20:57:50 +00003544 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003545
Kate Stoneb9c1b512016-09-06 20:57:50 +00003546 switch (short_option) {
3547 case 'a': {
3548 m_type = eLookupTypeAddress;
3549 m_addr = Args::StringToAddress(execution_context, option_arg,
3550 LLDB_INVALID_ADDRESS, &error);
3551 } break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003552
Kate Stoneb9c1b512016-09-06 20:57:50 +00003553 case 'o':
Zachary Turnerfe114832016-11-12 16:56:47 +00003554 if (option_arg.getAsInteger(0, m_offset))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003555 error.SetErrorStringWithFormat("invalid offset string '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00003556 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003557 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003558
Kate Stoneb9c1b512016-09-06 20:57:50 +00003559 case 's':
3560 m_str = option_arg;
3561 m_type = eLookupTypeSymbol;
3562 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003563
Kate Stoneb9c1b512016-09-06 20:57:50 +00003564 case 'f':
3565 m_file.SetFile(option_arg, false);
3566 m_type = eLookupTypeFileLine;
3567 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003568
Kate Stoneb9c1b512016-09-06 20:57:50 +00003569 case 'i':
3570 m_include_inlines = false;
3571 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003572
Kate Stoneb9c1b512016-09-06 20:57:50 +00003573 case 'l':
Zachary Turnerfe114832016-11-12 16:56:47 +00003574 if (option_arg.getAsInteger(0, m_line_number))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003575 error.SetErrorStringWithFormat("invalid line number string '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00003576 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003577 else if (m_line_number == 0)
3578 error.SetErrorString("zero is an invalid line number");
3579 m_type = eLookupTypeFileLine;
3580 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003581
Kate Stoneb9c1b512016-09-06 20:57:50 +00003582 case 'F':
3583 m_str = option_arg;
3584 m_type = eLookupTypeFunction;
3585 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003586
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587 case 'n':
3588 m_str = option_arg;
3589 m_type = eLookupTypeFunctionOrSymbol;
3590 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003591
Kate Stoneb9c1b512016-09-06 20:57:50 +00003592 case 't':
3593 m_str = option_arg;
3594 m_type = eLookupTypeType;
3595 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003596
Kate Stoneb9c1b512016-09-06 20:57:50 +00003597 case 'v':
3598 m_verbose = 1;
3599 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003600
Kate Stoneb9c1b512016-09-06 20:57:50 +00003601 case 'A':
3602 m_print_all = true;
3603 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003604
Kate Stoneb9c1b512016-09-06 20:57:50 +00003605 case 'r':
3606 m_use_regex = true;
3607 break;
3608 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003609
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 return error;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003611 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003612
Kate Stoneb9c1b512016-09-06 20:57:50 +00003613 void OptionParsingStarting(ExecutionContext *execution_context) override {
3614 m_type = eLookupTypeInvalid;
3615 m_str.clear();
3616 m_file.Clear();
3617 m_addr = LLDB_INVALID_ADDRESS;
3618 m_offset = 0;
3619 m_line_number = 0;
3620 m_use_regex = false;
3621 m_include_inlines = true;
3622 m_verbose = false;
3623 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003624 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003625
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003626 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00003627 return llvm::makeArrayRef(g_target_modules_lookup_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00003628 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003629
Kate Stoneb9c1b512016-09-06 20:57:50 +00003630 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3631 std::string m_str; // Holds name lookup
3632 FileSpec m_file; // Files for file lookups
3633 lldb::addr_t m_addr; // Holds the address to lookup
3634 lldb::addr_t
3635 m_offset; // Subtract this offset from m_addr before doing lookups.
3636 uint32_t m_line_number; // Line number for file+line lookups
3637 bool m_use_regex; // Name lookups in m_str are regular expressions.
3638 bool m_include_inlines; // Check for inline entries when looking up by
3639 // file/line.
3640 bool m_verbose; // Enable verbose lookup info
3641 bool m_print_all; // Print all matches, even in cases where there's a best
3642 // match.
3643 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003644
Kate Stoneb9c1b512016-09-06 20:57:50 +00003645 CommandObjectTargetModulesLookup(CommandInterpreter &interpreter)
3646 : CommandObjectParsed(interpreter, "target modules lookup",
3647 "Look up information within executable and "
3648 "dependent shared library images.",
3649 nullptr, eCommandRequiresTarget),
3650 m_options() {
3651 CommandArgumentEntry arg;
3652 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003653
Kate Stoneb9c1b512016-09-06 20:57:50 +00003654 // Define the first (and only) variant of this arg.
3655 file_arg.arg_type = eArgTypeFilename;
3656 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003657
Kate Stoneb9c1b512016-09-06 20:57:50 +00003658 // There is only one variant this argument could be; put it into the
3659 // argument entry.
3660 arg.push_back(file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003661
Kate Stoneb9c1b512016-09-06 20:57:50 +00003662 // Push the data for the first argument into the m_arguments vector.
3663 m_arguments.push_back(arg);
3664 }
3665
3666 ~CommandObjectTargetModulesLookup() override = default;
3667
3668 Options *GetOptions() override { return &m_options; }
3669
3670 bool LookupHere(CommandInterpreter &interpreter, CommandReturnObject &result,
3671 bool &syntax_error) {
3672 switch (m_options.m_type) {
3673 case eLookupTypeAddress:
3674 case eLookupTypeFileLine:
3675 case eLookupTypeFunction:
3676 case eLookupTypeFunctionOrSymbol:
3677 case eLookupTypeSymbol:
3678 default:
3679 return false;
3680 case eLookupTypeType:
3681 break;
Sean Callanand38b4a92012-06-06 20:49:55 +00003682 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003683
Kate Stoneb9c1b512016-09-06 20:57:50 +00003684 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003685
Kate Stoneb9c1b512016-09-06 20:57:50 +00003686 if (!frame)
3687 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003688
Kate Stoneb9c1b512016-09-06 20:57:50 +00003689 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Greg Claytonc4a8a762012-05-15 18:43:44 +00003690
Kate Stoneb9c1b512016-09-06 20:57:50 +00003691 if (!sym_ctx.module_sp)
3692 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003693
Kate Stoneb9c1b512016-09-06 20:57:50 +00003694 switch (m_options.m_type) {
3695 default:
3696 return false;
3697 case eLookupTypeType:
3698 if (!m_options.m_str.empty()) {
3699 if (LookupTypeHere(m_interpreter, result.GetOutputStream(), sym_ctx,
3700 m_options.m_str.c_str(), m_options.m_use_regex)) {
3701 result.SetStatus(eReturnStatusSuccessFinishResult);
3702 return true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003703 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704 }
3705 break;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003706 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003707
Kate Stoneb9c1b512016-09-06 20:57:50 +00003708 return true;
3709 }
3710
3711 bool LookupInModule(CommandInterpreter &interpreter, Module *module,
3712 CommandReturnObject &result, bool &syntax_error) {
3713 switch (m_options.m_type) {
3714 case eLookupTypeAddress:
3715 if (m_options.m_addr != LLDB_INVALID_ADDRESS) {
3716 if (LookupAddressInModule(
3717 m_interpreter, result.GetOutputStream(), module,
3718 eSymbolContextEverything |
3719 (m_options.m_verbose
3720 ? static_cast<int>(eSymbolContextVariable)
3721 : 0),
3722 m_options.m_addr, m_options.m_offset, m_options.m_verbose)) {
3723 result.SetStatus(eReturnStatusSuccessFinishResult);
3724 return true;
3725 }
3726 }
3727 break;
3728
3729 case eLookupTypeSymbol:
3730 if (!m_options.m_str.empty()) {
3731 if (LookupSymbolInModule(m_interpreter, result.GetOutputStream(),
3732 module, m_options.m_str.c_str(),
3733 m_options.m_use_regex, m_options.m_verbose)) {
3734 result.SetStatus(eReturnStatusSuccessFinishResult);
3735 return true;
3736 }
3737 }
3738 break;
3739
3740 case eLookupTypeFileLine:
3741 if (m_options.m_file) {
3742 if (LookupFileAndLineInModule(
3743 m_interpreter, result.GetOutputStream(), module,
3744 m_options.m_file, m_options.m_line_number,
3745 m_options.m_include_inlines, m_options.m_verbose)) {
3746 result.SetStatus(eReturnStatusSuccessFinishResult);
3747 return true;
3748 }
3749 }
3750 break;
3751
3752 case eLookupTypeFunctionOrSymbol:
3753 case eLookupTypeFunction:
3754 if (!m_options.m_str.empty()) {
3755 if (LookupFunctionInModule(
3756 m_interpreter, result.GetOutputStream(), module,
3757 m_options.m_str.c_str(), m_options.m_use_regex,
3758 m_options.m_include_inlines,
3759 m_options.m_type ==
3760 eLookupTypeFunctionOrSymbol, // include symbols
3761 m_options.m_verbose)) {
3762 result.SetStatus(eReturnStatusSuccessFinishResult);
3763 return true;
3764 }
3765 }
3766 break;
3767
3768 case eLookupTypeType:
3769 if (!m_options.m_str.empty()) {
3770 if (LookupTypeInModule(m_interpreter, result.GetOutputStream(), module,
3771 m_options.m_str.c_str(),
3772 m_options.m_use_regex)) {
3773 result.SetStatus(eReturnStatusSuccessFinishResult);
3774 return true;
3775 }
3776 }
3777 break;
3778
3779 default:
3780 m_options.GenerateOptionUsage(
3781 result.GetErrorStream(), this,
3782 GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3783 syntax_error = true;
3784 break;
3785 }
3786
3787 result.SetStatus(eReturnStatusFailed);
3788 return false;
3789 }
3790
Jim Ingham5a988412012-06-08 21:56:10 +00003791protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003792 bool DoExecute(Args &command, CommandReturnObject &result) override {
3793 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3794 if (target == nullptr) {
3795 result.AppendError("invalid target, create a debug target using the "
3796 "'target create' command");
3797 result.SetStatus(eReturnStatusFailed);
3798 return false;
3799 } else {
3800 bool syntax_error = false;
3801 uint32_t i;
3802 uint32_t num_successful_lookups = 0;
3803 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3804 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3805 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3806 // Dump all sections for all modules images
3807
3808 if (command.GetArgumentCount() == 0) {
3809 ModuleSP current_module;
3810
3811 // Where it is possible to look in the current symbol context
3812 // first, try that. If this search was successful and --all
3813 // was not passed, don't print anything else.
3814 if (LookupHere(m_interpreter, result, syntax_error)) {
3815 result.GetOutputStream().EOL();
3816 num_successful_lookups++;
3817 if (!m_options.m_print_all) {
3818 result.SetStatus(eReturnStatusSuccessFinishResult);
3819 return result.Succeeded();
3820 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003821 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003822
Kate Stoneb9c1b512016-09-06 20:57:50 +00003823 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003824
Kate Stoneb9c1b512016-09-06 20:57:50 +00003825 const ModuleList &target_modules = target->GetImages();
3826 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
3827 const size_t num_modules = target_modules.GetSize();
3828 if (num_modules > 0) {
3829 for (i = 0; i < num_modules && !syntax_error; ++i) {
3830 Module *module_pointer =
3831 target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003832
Kate Stoneb9c1b512016-09-06 20:57:50 +00003833 if (module_pointer != current_module.get() &&
3834 LookupInModule(
3835 m_interpreter,
3836 target_modules.GetModulePointerAtIndexUnlocked(i), result,
3837 syntax_error)) {
3838 result.GetOutputStream().EOL();
3839 num_successful_lookups++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003840 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003841 }
3842 } else {
3843 result.AppendError("the target has no associated executable images");
3844 result.SetStatus(eReturnStatusFailed);
3845 return false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003847 } else {
3848 // Dump specified images (by basename or fullpath)
3849 const char *arg_cstr;
3850 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr &&
3851 !syntax_error;
3852 ++i) {
3853 ModuleList module_list;
3854 const size_t num_matches =
3855 FindModulesByName(target, arg_cstr, module_list, false);
3856 if (num_matches > 0) {
3857 for (size_t j = 0; j < num_matches; ++j) {
3858 Module *module = module_list.GetModulePointerAtIndex(j);
3859 if (module) {
3860 if (LookupInModule(m_interpreter, module, result,
3861 syntax_error)) {
3862 result.GetOutputStream().EOL();
3863 num_successful_lookups++;
3864 }
3865 }
3866 }
3867 } else
3868 result.AppendWarningWithFormat(
3869 "Unable to find an image that matches '%s'.\n", arg_cstr);
3870 }
3871 }
3872
3873 if (num_successful_lookups > 0)
3874 result.SetStatus(eReturnStatusSuccessFinishResult);
3875 else
3876 result.SetStatus(eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003877 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003878 return result.Succeeded();
3879 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003880
Kate Stoneb9c1b512016-09-06 20:57:50 +00003881 CommandOptions m_options;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003882};
3883
Jim Ingham9575d842011-03-11 03:53:59 +00003884#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003885
3886//-------------------------------------------------------------------------
3887// CommandObjectMultiwordImageSearchPaths
3888//-------------------------------------------------------------------------
3889
Kate Stoneb9c1b512016-09-06 20:57:50 +00003890class CommandObjectTargetModulesImageSearchPaths
3891 : public CommandObjectMultiword {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003893 CommandObjectTargetModulesImageSearchPaths(CommandInterpreter &interpreter)
3894 : CommandObjectMultiword(
3895 interpreter, "target modules search-paths",
3896 "Commands for managing module search paths for a target.",
3897 "target modules search-paths <subcommand> [<subcommand-options>]") {
3898 LoadSubCommand(
3899 "add", CommandObjectSP(
3900 new CommandObjectTargetModulesSearchPathsAdd(interpreter)));
3901 LoadSubCommand(
3902 "clear", CommandObjectSP(new CommandObjectTargetModulesSearchPathsClear(
3903 interpreter)));
3904 LoadSubCommand(
3905 "insert",
3906 CommandObjectSP(
3907 new CommandObjectTargetModulesSearchPathsInsert(interpreter)));
3908 LoadSubCommand(
3909 "list", CommandObjectSP(new CommandObjectTargetModulesSearchPathsList(
3910 interpreter)));
3911 LoadSubCommand(
3912 "query", CommandObjectSP(new CommandObjectTargetModulesSearchPathsQuery(
3913 interpreter)));
3914 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003915
Kate Stoneb9c1b512016-09-06 20:57:50 +00003916 ~CommandObjectTargetModulesImageSearchPaths() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003917};
3918
Greg Claytoneffe5c92011-05-03 22:09:39 +00003919#pragma mark CommandObjectTargetModules
3920
3921//-------------------------------------------------------------------------
3922// CommandObjectTargetModules
3923//-------------------------------------------------------------------------
3924
Kate Stoneb9c1b512016-09-06 20:57:50 +00003925class CommandObjectTargetModules : public CommandObjectMultiword {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003926public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003927 //------------------------------------------------------------------
3928 // Constructors and Destructors
3929 //------------------------------------------------------------------
3930 CommandObjectTargetModules(CommandInterpreter &interpreter)
3931 : CommandObjectMultiword(interpreter, "target modules",
3932 "Commands for accessing information for one or "
3933 "more target modules.",
3934 "target modules <sub-command> ...") {
3935 LoadSubCommand(
3936 "add", CommandObjectSP(new CommandObjectTargetModulesAdd(interpreter)));
3937 LoadSubCommand("load", CommandObjectSP(new CommandObjectTargetModulesLoad(
3938 interpreter)));
3939 LoadSubCommand("dump", CommandObjectSP(new CommandObjectTargetModulesDump(
3940 interpreter)));
3941 LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetModulesList(
3942 interpreter)));
3943 LoadSubCommand(
3944 "lookup",
3945 CommandObjectSP(new CommandObjectTargetModulesLookup(interpreter)));
3946 LoadSubCommand(
3947 "search-paths",
3948 CommandObjectSP(
3949 new CommandObjectTargetModulesImageSearchPaths(interpreter)));
3950 LoadSubCommand(
3951 "show-unwind",
3952 CommandObjectSP(new CommandObjectTargetModulesShowUnwind(interpreter)));
3953 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003954
Kate Stoneb9c1b512016-09-06 20:57:50 +00003955 ~CommandObjectTargetModules() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003956
Greg Claytoneffe5c92011-05-03 22:09:39 +00003957private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003958 //------------------------------------------------------------------
3959 // For CommandObjectTargetModules only
3960 //------------------------------------------------------------------
3961 DISALLOW_COPY_AND_ASSIGN(CommandObjectTargetModules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003962};
3963
Kate Stoneb9c1b512016-09-06 20:57:50 +00003964class CommandObjectTargetSymbolsAdd : public CommandObjectParsed {
Greg Claytone72dfb32012-02-24 01:59:29 +00003965public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003966 CommandObjectTargetSymbolsAdd(CommandInterpreter &interpreter)
3967 : CommandObjectParsed(
3968 interpreter, "target symbols add",
3969 "Add a debug symbol file to one of the target's current modules by "
3970 "specifying a path to a debug symbols file, or using the options "
3971 "to specify a module to download symbols for.",
Eugene Zemtsov7cff7d42017-07-24 22:52:39 +00003972 "target symbols add <cmd-options> [<symfile>]",
3973 eCommandRequiresTarget),
Todd Fialae1cfbc72016-08-11 23:51:28 +00003974 m_option_group(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003975 m_file_option(
3976 LLDB_OPT_SET_1, false, "shlib", 's',
3977 CommandCompletions::eModuleCompletion, eArgTypeShlibName,
3978 "Fullpath or basename for module to find debug symbols for."),
3979 m_current_frame_option(
3980 LLDB_OPT_SET_2, false, "frame", 'F',
3981 "Locate the debug symbols the currently selected frame.", false,
3982 true)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00003983
Kate Stoneb9c1b512016-09-06 20:57:50 +00003984 {
3985 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
3986 LLDB_OPT_SET_1);
3987 m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3988 m_option_group.Append(&m_current_frame_option, LLDB_OPT_SET_2,
3989 LLDB_OPT_SET_2);
3990 m_option_group.Finalize();
3991 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003992
Kate Stoneb9c1b512016-09-06 20:57:50 +00003993 ~CommandObjectTargetSymbolsAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003994
Kate Stoneb9c1b512016-09-06 20:57:50 +00003995 int HandleArgumentCompletion(Args &input, int &cursor_index,
3996 int &cursor_char_position,
3997 OptionElementVector &opt_element_vector,
3998 int match_start_point, int max_return_elements,
3999 bool &word_complete,
4000 StringList &matches) override {
4001 std::string completion_str(input.GetArgumentAtIndex(cursor_index));
4002 completion_str.erase(cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004003
Kate Stoneb9c1b512016-09-06 20:57:50 +00004004 CommandCompletions::InvokeCommonCompletionCallbacks(
4005 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
4006 completion_str.c_str(), match_start_point, max_return_elements, nullptr,
4007 word_complete, matches);
4008 return matches.GetSize();
4009 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004010
Kate Stoneb9c1b512016-09-06 20:57:50 +00004011 Options *GetOptions() override { return &m_option_group; }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004012
Jim Ingham5a988412012-06-08 21:56:10 +00004013protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004014 bool AddModuleSymbols(Target *target, ModuleSpec &module_spec, bool &flush,
4015 CommandReturnObject &result) {
4016 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4017 if (symbol_fspec) {
4018 char symfile_path[PATH_MAX];
4019 symbol_fspec.GetPath(symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004020
Kate Stoneb9c1b512016-09-06 20:57:50 +00004021 if (!module_spec.GetUUID().IsValid()) {
4022 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4023 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4024 }
4025 // We now have a module that represents a symbol file
4026 // that can be used for a module that might exist in the
4027 // current target, so we need to find that module in the
4028 // target
4029 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004030
Kate Stoneb9c1b512016-09-06 20:57:50 +00004031 size_t num_matches = 0;
4032 // First extract all module specs from the symbol file
4033 lldb_private::ModuleSpecList symfile_module_specs;
4034 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(),
4035 0, 0, symfile_module_specs)) {
4036 // Now extract the module spec that matches the target architecture
4037 ModuleSpec target_arch_module_spec;
4038 ModuleSpec symfile_module_spec;
4039 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4040 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec,
4041 symfile_module_spec)) {
4042 // See if it has a UUID?
4043 if (symfile_module_spec.GetUUID().IsValid()) {
4044 // It has a UUID, look for this UUID in the target modules
4045 ModuleSpec symfile_uuid_module_spec;
4046 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4047 num_matches = target->GetImages().FindModules(
4048 symfile_uuid_module_spec, matching_module_list);
4049 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004050 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004051
4052 if (num_matches == 0) {
4053 // No matches yet, iterate through the module specs to find a UUID
4054 // value that
4055 // we can match up to an image in our target
4056 const size_t num_symfile_module_specs =
4057 symfile_module_specs.GetSize();
4058 for (size_t i = 0; i < num_symfile_module_specs && num_matches == 0;
4059 ++i) {
4060 if (symfile_module_specs.GetModuleSpecAtIndex(
4061 i, symfile_module_spec)) {
4062 if (symfile_module_spec.GetUUID().IsValid()) {
4063 // It has a UUID, look for this UUID in the target modules
4064 ModuleSpec symfile_uuid_module_spec;
4065 symfile_uuid_module_spec.GetUUID() =
4066 symfile_module_spec.GetUUID();
4067 num_matches = target->GetImages().FindModules(
4068 symfile_uuid_module_spec, matching_module_list);
4069 }
4070 }
4071 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004073 }
4074
4075 // Just try to match up the file by basename if we have no matches at this
4076 // point
4077 if (num_matches == 0)
4078 num_matches =
4079 target->GetImages().FindModules(module_spec, matching_module_list);
4080
4081 while (num_matches == 0) {
4082 ConstString filename_no_extension(
4083 module_spec.GetFileSpec().GetFileNameStrippingExtension());
4084 // Empty string returned, lets bail
4085 if (!filename_no_extension)
4086 break;
4087
4088 // Check if there was no extension to strip and the basename is the same
4089 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4090 break;
4091
4092 // Replace basename with one less extension
4093 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4094
4095 num_matches =
4096 target->GetImages().FindModules(module_spec, matching_module_list);
4097 }
4098
4099 if (num_matches > 1) {
4100 result.AppendErrorWithFormat("multiple modules match symbol file '%s', "
4101 "use the --uuid option to resolve the "
4102 "ambiguity.\n",
4103 symfile_path);
4104 } else if (num_matches == 1) {
4105 ModuleSP module_sp(matching_module_list.GetModuleAtIndex(0));
4106
4107 // The module has not yet created its symbol vendor, we can just
4108 // give the existing target module the symfile path to use for
4109 // when it decides to create it!
4110 module_sp->SetSymbolFileFileSpec(symbol_fspec);
4111
4112 SymbolVendor *symbol_vendor =
4113 module_sp->GetSymbolVendor(true, &result.GetErrorStream());
4114 if (symbol_vendor) {
4115 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4116
4117 if (symbol_file) {
4118 ObjectFile *object_file = symbol_file->GetObjectFile();
4119
4120 if (object_file && object_file->GetFileSpec() == symbol_fspec) {
4121 // Provide feedback that the symfile has been successfully added.
4122 const FileSpec &module_fs = module_sp->GetFileSpec();
4123 result.AppendMessageWithFormat(
4124 "symbol file '%s' has been added to '%s'\n", symfile_path,
4125 module_fs.GetPath().c_str());
4126
4127 // Let clients know something changed in the module
4128 // if it is currently loaded
4129 ModuleList module_list;
4130 module_list.Append(module_sp);
4131 target->SymbolsDidLoad(module_list);
4132
4133 // Make sure we load any scripting resources that may be embedded
4134 // in the debug info files in case the platform supports that.
Zachary Turner97206d52017-05-12 04:51:55 +00004135 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004136 StreamString feedback_stream;
4137 module_sp->LoadScriptingResourceInTarget(target, error,
4138 &feedback_stream);
4139 if (error.Fail() && error.AsCString())
4140 result.AppendWarningWithFormat(
4141 "unable to load scripting data for module %s - error "
4142 "reported was %s",
4143 module_sp->GetFileSpec()
4144 .GetFileNameStrippingExtension()
4145 .GetCString(),
4146 error.AsCString());
4147 else if (feedback_stream.GetSize())
4148 result.AppendWarningWithFormat("%s", feedback_stream.GetData());
4149
4150 flush = true;
4151 result.SetStatus(eReturnStatusSuccessFinishResult);
4152 return true;
4153 }
4154 }
4155 }
4156 // Clear the symbol file spec if anything went wrong
4157 module_sp->SetSymbolFileFileSpec(FileSpec());
4158 }
4159
Zachary Turner7d86ee52017-03-08 17:56:08 +00004160 namespace fs = llvm::sys::fs;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004161 if (module_spec.GetUUID().IsValid()) {
4162 StreamString ss_symfile_uuid;
4163 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4164 result.AppendErrorWithFormat(
4165 "symbol file '%s' (%s) does not match any existing module%s\n",
4166 symfile_path, ss_symfile_uuid.GetData(),
Zachary Turner7d86ee52017-03-08 17:56:08 +00004167 !fs::is_regular_file(symbol_fspec.GetPath())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004168 ? "\n please specify the full path to the symbol file"
4169 : "");
4170 } else {
4171 result.AppendErrorWithFormat(
4172 "symbol file '%s' does not match any existing module%s\n",
4173 symfile_path,
Zachary Turner7d86ee52017-03-08 17:56:08 +00004174 !fs::is_regular_file(symbol_fspec.GetPath())
Kate Stoneb9c1b512016-09-06 20:57:50 +00004175 ? "\n please specify the full path to the symbol file"
4176 : "");
4177 }
4178 } else {
4179 result.AppendError(
4180 "one or more executable image paths must be specified");
4181 }
4182 result.SetStatus(eReturnStatusFailed);
4183 return false;
4184 }
4185
4186 bool DoExecute(Args &args, CommandReturnObject &result) override {
4187 Target *target = m_exe_ctx.GetTargetPtr();
4188 result.SetStatus(eReturnStatusFailed);
4189 bool flush = false;
4190 ModuleSpec module_spec;
4191 const bool uuid_option_set =
4192 m_uuid_option_group.GetOptionValue().OptionWasSet();
4193 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4194 const bool frame_option_set =
4195 m_current_frame_option.GetOptionValue().OptionWasSet();
4196 const size_t argc = args.GetArgumentCount();
4197
4198 if (argc == 0) {
4199 if (uuid_option_set || file_option_set || frame_option_set) {
4200 bool success = false;
4201 bool error_set = false;
4202 if (frame_option_set) {
4203 Process *process = m_exe_ctx.GetProcessPtr();
4204 if (process) {
4205 const StateType process_state = process->GetState();
4206 if (StateIsStoppedState(process_state, true)) {
4207 StackFrame *frame = m_exe_ctx.GetFramePtr();
4208 if (frame) {
4209 ModuleSP frame_module_sp(
4210 frame->GetSymbolContext(eSymbolContextModule).module_sp);
4211 if (frame_module_sp) {
4212 if (frame_module_sp->GetPlatformFileSpec().Exists()) {
4213 module_spec.GetArchitecture() =
4214 frame_module_sp->GetArchitecture();
4215 module_spec.GetFileSpec() =
4216 frame_module_sp->GetPlatformFileSpec();
4217 }
4218 module_spec.GetUUID() = frame_module_sp->GetUUID();
4219 success = module_spec.GetUUID().IsValid() ||
4220 module_spec.GetFileSpec();
4221 } else {
4222 result.AppendError("frame has no module");
4223 error_set = true;
4224 }
4225 } else {
4226 result.AppendError("invalid current frame");
4227 error_set = true;
4228 }
4229 } else {
4230 result.AppendErrorWithFormat("process is not stopped: %s",
4231 StateAsCString(process_state));
4232 error_set = true;
4233 }
4234 } else {
4235 result.AppendError(
4236 "a process must exist in order to use the --frame option");
4237 error_set = true;
4238 }
4239 } else {
4240 if (uuid_option_set) {
4241 module_spec.GetUUID() =
4242 m_uuid_option_group.GetOptionValue().GetCurrentValue();
4243 success |= module_spec.GetUUID().IsValid();
4244 } else if (file_option_set) {
4245 module_spec.GetFileSpec() =
4246 m_file_option.GetOptionValue().GetCurrentValue();
4247 ModuleSP module_sp(
4248 target->GetImages().FindFirstModule(module_spec));
4249 if (module_sp) {
4250 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4251 module_spec.GetPlatformFileSpec() =
4252 module_sp->GetPlatformFileSpec();
4253 module_spec.GetUUID() = module_sp->GetUUID();
4254 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4255 } else {
4256 module_spec.GetArchitecture() = target->GetArchitecture();
4257 }
4258 success |= module_spec.GetUUID().IsValid() ||
4259 module_spec.GetFileSpec().Exists();
4260 }
4261 }
4262
4263 if (success) {
4264 if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
4265 if (module_spec.GetSymbolFileSpec())
4266 success = AddModuleSymbols(target, module_spec, flush, result);
4267 }
4268 }
4269
4270 if (!success && !error_set) {
4271 StreamString error_strm;
4272 if (uuid_option_set) {
4273 error_strm.PutCString("unable to find debug symbols for UUID ");
4274 module_spec.GetUUID().Dump(&error_strm);
4275 } else if (file_option_set) {
4276 error_strm.PutCString(
4277 "unable to find debug symbols for the executable file ");
4278 error_strm << module_spec.GetFileSpec();
4279 } else if (frame_option_set) {
4280 error_strm.PutCString(
4281 "unable to find debug symbols for the current frame");
4282 }
Zachary Turnerc1564272016-11-16 21:15:24 +00004283 result.AppendError(error_strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004284 }
4285 } else {
4286 result.AppendError("one or more symbol file paths must be specified, "
4287 "or options must be specified");
4288 }
4289 } else {
4290 if (uuid_option_set) {
4291 result.AppendError("specify either one or more paths to symbol files "
4292 "or use the --uuid option without arguments");
Kate Stoneb9c1b512016-09-06 20:57:50 +00004293 } else if (frame_option_set) {
4294 result.AppendError("specify either one or more paths to symbol files "
4295 "or use the --frame option without arguments");
Eugene Zemtsov7cff7d42017-07-24 22:52:39 +00004296 } else if (file_option_set && argc > 1) {
4297 result.AppendError("specify at most one symbol file path when "
4298 "--shlib option is set");
Kate Stoneb9c1b512016-09-06 20:57:50 +00004299 } else {
4300 PlatformSP platform_sp(target->GetPlatform());
4301
Zachary Turner97d2c402016-10-05 23:40:23 +00004302 for (auto &entry : args.entries()) {
4303 if (!entry.ref.empty()) {
4304 module_spec.GetSymbolFileSpec().SetFile(entry.ref, true);
Eugene Zemtsov7cff7d42017-07-24 22:52:39 +00004305 if (file_option_set) {
4306 module_spec.GetFileSpec() =
4307 m_file_option.GetOptionValue().GetCurrentValue();
4308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004309 if (platform_sp) {
4310 FileSpec symfile_spec;
4311 if (platform_sp
4312 ->ResolveSymbolFile(*target, module_spec, symfile_spec)
4313 .Success())
4314 module_spec.GetSymbolFileSpec() = symfile_spec;
4315 }
4316
4317 ArchSpec arch;
4318 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
4319
4320 if (symfile_exists) {
4321 if (!AddModuleSymbols(target, module_spec, flush, result))
4322 break;
4323 } else {
Zachary Turner97d2c402016-10-05 23:40:23 +00004324 std::string resolved_symfile_path =
4325 module_spec.GetSymbolFileSpec().GetPath();
4326 if (resolved_symfile_path != entry.ref) {
4327 result.AppendErrorWithFormat(
4328 "invalid module path '%s' with resolved path '%s'\n",
4329 entry.c_str(), resolved_symfile_path.c_str());
4330 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004331 }
4332 result.AppendErrorWithFormat("invalid module path '%s'\n",
Zachary Turner97d2c402016-10-05 23:40:23 +00004333 entry.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004334 break;
4335 }
4336 }
4337 }
4338 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004339 }
4340
Kate Stoneb9c1b512016-09-06 20:57:50 +00004341 if (flush) {
4342 Process *process = m_exe_ctx.GetProcessPtr();
4343 if (process)
4344 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004345 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004346 return result.Succeeded();
4347 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004348
Kate Stoneb9c1b512016-09-06 20:57:50 +00004349 OptionGroupOptions m_option_group;
4350 OptionGroupUUID m_uuid_option_group;
4351 OptionGroupFile m_file_option;
4352 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004353};
4354
Greg Claytone72dfb32012-02-24 01:59:29 +00004355#pragma mark CommandObjectTargetSymbols
4356
4357//-------------------------------------------------------------------------
4358// CommandObjectTargetSymbols
4359//-------------------------------------------------------------------------
4360
Kate Stoneb9c1b512016-09-06 20:57:50 +00004361class CommandObjectTargetSymbols : public CommandObjectMultiword {
Greg Claytone72dfb32012-02-24 01:59:29 +00004362public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004363 //------------------------------------------------------------------
4364 // Constructors and Destructors
4365 //------------------------------------------------------------------
4366 CommandObjectTargetSymbols(CommandInterpreter &interpreter)
4367 : CommandObjectMultiword(
4368 interpreter, "target symbols",
4369 "Commands for adding and managing debug symbol files.",
4370 "target symbols <sub-command> ...") {
4371 LoadSubCommand(
4372 "add", CommandObjectSP(new CommandObjectTargetSymbolsAdd(interpreter)));
4373 }
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004374
Kate Stoneb9c1b512016-09-06 20:57:50 +00004375 ~CommandObjectTargetSymbols() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004376
Greg Claytone72dfb32012-02-24 01:59:29 +00004377private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004378 //------------------------------------------------------------------
4379 // For CommandObjectTargetModules only
4380 //------------------------------------------------------------------
4381 DISALLOW_COPY_AND_ASSIGN(CommandObjectTargetSymbols);
Greg Claytone72dfb32012-02-24 01:59:29 +00004382};
4383
Jim Ingham9575d842011-03-11 03:53:59 +00004384#pragma mark CommandObjectTargetStopHookAdd
4385
4386//-------------------------------------------------------------------------
4387// CommandObjectTargetStopHookAdd
4388//-------------------------------------------------------------------------
4389
Zachary Turner1f0f5b52016-09-22 20:22:55 +00004390static OptionDefinition g_target_stop_hook_add_options[] = {
4391 // clang-format off
4392 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner, "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
4393 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Set the module within which the stop-hook is to be run." },
4394 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex, "The stop hook is run only for the thread whose index matches this argument." },
4395 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadID, "The stop hook is run only for the thread whose TID matches this argument." },
4396 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadName, "The stop hook is run only for the thread whose thread name matches this argument." },
4397 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeQueueName, "The stop hook is run only for threads in the queue whose name is given by this argument." },
4398 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "Specify the source file within which the stop-hook is to be run." },
4399 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum, "Set the start of the line range for which the stop-hook is to be run." },
4400 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum, "Set the end of the line range for which the stop-hook is to be run." },
4401 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeClassName, "Specify the class within which the stop-hook is to be run." },
4402 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Set the function name within which the stop hook will be run." },
4403 // clang-format on
4404};
4405
Kate Stoneb9c1b512016-09-06 20:57:50 +00004406class CommandObjectTargetStopHookAdd : public CommandObjectParsed,
4407 public IOHandlerDelegateMultiline {
Jim Ingham9575d842011-03-11 03:53:59 +00004408public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004409 class CommandOptions : public Options {
4410 public:
4411 CommandOptions()
4412 : Options(), m_line_start(0), m_line_end(UINT_MAX),
4413 m_func_name_type_mask(eFunctionNameTypeAuto),
4414 m_sym_ctx_specified(false), m_thread_specified(false),
4415 m_use_one_liner(false), m_one_liner() {}
4416
4417 ~CommandOptions() override = default;
4418
Zachary Turner1f0f5b52016-09-22 20:22:55 +00004419 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
Zachary Turner70602432016-09-22 21:06:13 +00004420 return llvm::makeArrayRef(g_target_stop_hook_add_options);
Zachary Turner1f0f5b52016-09-22 20:22:55 +00004421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004422
Zachary Turner97206d52017-05-12 04:51:55 +00004423 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
4424 ExecutionContext *execution_context) override {
4425 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004426 const int short_option = m_getopt_table[option_idx].val;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004427
4428 switch (short_option) {
4429 case 'c':
4430 m_class_name = option_arg;
4431 m_sym_ctx_specified = true;
4432 break;
4433
4434 case 'e':
Zachary Turnerfe114832016-11-12 16:56:47 +00004435 if (option_arg.getAsInteger(0, m_line_end)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004436 error.SetErrorStringWithFormat("invalid end line number: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +00004437 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004438 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004440 m_sym_ctx_specified = true;
4441 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004442
Kate Stoneb9c1b512016-09-06 20:57:50 +00004443 case 'l':
Zachary Turnerfe114832016-11-12 16:56:47 +00004444 if (option_arg.getAsInteger(0, m_line_start)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00004445 error.SetErrorStringWithFormat("invalid start line number: \"%s\"",
Zachary Turnerfe114832016-11-12 16:56:47 +00004446 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004447 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004448 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004449 m_sym_ctx_specified = true;
4450 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004451
Kate Stoneb9c1b512016-09-06 20:57:50 +00004452 case 'i':
4453 m_no_inlines = true;
4454 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004455
Kate Stoneb9c1b512016-09-06 20:57:50 +00004456 case 'n':
4457 m_function_name = option_arg;
4458 m_func_name_type_mask |= eFunctionNameTypeAuto;
4459 m_sym_ctx_specified = true;
4460 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004461
Kate Stoneb9c1b512016-09-06 20:57:50 +00004462 case 'f':
4463 m_file_name = option_arg;
4464 m_sym_ctx_specified = true;
4465 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004466
Kate Stoneb9c1b512016-09-06 20:57:50 +00004467 case 's':
4468 m_module_name = option_arg;
4469 m_sym_ctx_specified = true;
4470 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004471
Kate Stoneb9c1b512016-09-06 20:57:50 +00004472 case 't':
Zachary Turnerfe114832016-11-12 16:56:47 +00004473 if (option_arg.getAsInteger(0, m_thread_id))
Kate Stoneb9c1b512016-09-06 20:57:50 +00004474 error.SetErrorStringWithFormat("invalid thread id string '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00004475 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004476 m_thread_specified = true;
4477 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004478
Kate Stoneb9c1b512016-09-06 20:57:50 +00004479 case 'T':
4480 m_thread_name = option_arg;
4481 m_thread_specified = true;
4482 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004483
Kate Stoneb9c1b512016-09-06 20:57:50 +00004484 case 'q':
4485 m_queue_name = option_arg;
4486 m_thread_specified = true;
4487 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004488
Kate Stoneb9c1b512016-09-06 20:57:50 +00004489 case 'x':
Zachary Turnerfe114832016-11-12 16:56:47 +00004490 if (option_arg.getAsInteger(0, m_thread_index))
Kate Stoneb9c1b512016-09-06 20:57:50 +00004491 error.SetErrorStringWithFormat("invalid thread index string '%s'",
Zachary Turnerfe114832016-11-12 16:56:47 +00004492 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004493 m_thread_specified = true;
4494 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004495
Kate Stoneb9c1b512016-09-06 20:57:50 +00004496 case 'o':
4497 m_use_one_liner = true;
4498 m_one_liner = option_arg;
4499 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004500
Kate Stoneb9c1b512016-09-06 20:57:50 +00004501 default:
4502 error.SetErrorStringWithFormat("unrecognized option %c.", short_option);
4503 break;
4504 }
4505 return error;
Jim Ingham9575d842011-03-11 03:53:59 +00004506 }
4507
Kate Stoneb9c1b512016-09-06 20:57:50 +00004508 void OptionParsingStarting(ExecutionContext *execution_context) override {
4509 m_class_name.clear();
4510 m_function_name.clear();
4511 m_line_start = 0;
4512 m_line_end = UINT_MAX;
4513 m_file_name.clear();
4514 m_module_name.clear();
4515 m_func_name_type_mask = eFunctionNameTypeAuto;
4516 m_thread_id = LLDB_INVALID_THREAD_ID;
4517 m_thread_index = UINT32_MAX;
4518 m_thread_name.clear();
4519 m_queue_name.clear();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004520
Kate Stoneb9c1b512016-09-06 20:57:50 +00004521 m_no_inlines = false;
4522 m_sym_ctx_specified = false;
4523 m_thread_specified = false;
4524
4525 m_use_one_liner = false;
4526 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004527 }
4528
Kate Stoneb9c1b512016-09-06 20:57:50 +00004529 std::string m_class_name;
4530 std::string m_function_name;
4531 uint32_t m_line_start;
4532 uint32_t m_line_end;
4533 std::string m_file_name;
4534 std::string m_module_name;
4535 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4536 lldb::tid_t m_thread_id;
4537 uint32_t m_thread_index;
4538 std::string m_thread_name;
4539 std::string m_queue_name;
4540 bool m_sym_ctx_specified;
4541 bool m_no_inlines;
4542 bool m_thread_specified;
4543 // Instance variables to hold the values for one_liner options.
4544 bool m_use_one_liner;
4545 std::string m_one_liner;
4546 };
4547
4548 CommandObjectTargetStopHookAdd(CommandInterpreter &interpreter)
4549 : CommandObjectParsed(interpreter, "target stop-hook add",
4550 "Add a hook to be executed when the target stops.",
4551 "target stop-hook add"),
4552 IOHandlerDelegateMultiline("DONE",
4553 IOHandlerDelegate::Completion::LLDBCommand),
4554 m_options() {}
4555
4556 ~CommandObjectTargetStopHookAdd() override = default;
4557
4558 Options *GetOptions() override { return &m_options; }
4559
Jim Ingham5a988412012-06-08 21:56:10 +00004560protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004561 void IOHandlerActivated(IOHandler &io_handler) override {
4562 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
4563 if (output_sp) {
4564 output_sp->PutCString(
4565 "Enter your stop hook command(s). Type 'DONE' to end.\n");
4566 output_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00004567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004568 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004569
Kate Stoneb9c1b512016-09-06 20:57:50 +00004570 void IOHandlerInputComplete(IOHandler &io_handler,
4571 std::string &line) override {
4572 if (m_stop_hook_sp) {
4573 if (line.empty()) {
4574 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
4575 if (error_sp) {
4576 error_sp->Printf("error: stop hook #%" PRIu64
4577 " aborted, no commands.\n",
4578 m_stop_hook_sp->GetID());
4579 error_sp->Flush();
Greg Clayton44d93782014-01-27 23:43:24 +00004580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004581 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Jim Ingham9575d842011-03-11 03:53:59 +00004582 if (target)
Kate Stoneb9c1b512016-09-06 20:57:50 +00004583 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
4584 } else {
4585 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
4586 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
4587 if (output_sp) {
4588 output_sp->Printf("Stop hook #%" PRIu64 " added.\n",
4589 m_stop_hook_sp->GetID());
4590 output_sp->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00004591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004592 }
4593 m_stop_hook_sp.reset();
Jim Ingham9575d842011-03-11 03:53:59 +00004594 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004595 io_handler.SetIsDone(true);
4596 }
4597
4598 bool DoExecute(Args &command, CommandReturnObject &result) override {
4599 m_stop_hook_sp.reset();
4600
4601 Target *target = GetSelectedOrDummyTarget();
4602 if (target) {
4603 Target::StopHookSP new_hook_sp = target->CreateStopHook();
4604
4605 // First step, make the specifier.
4606 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
4607 if (m_options.m_sym_ctx_specified) {
4608 specifier_ap.reset(new SymbolContextSpecifier(
4609 m_interpreter.GetDebugger().GetSelectedTarget()));
4610
4611 if (!m_options.m_module_name.empty()) {
4612 specifier_ap->AddSpecification(
4613 m_options.m_module_name.c_str(),
4614 SymbolContextSpecifier::eModuleSpecified);
4615 }
4616
4617 if (!m_options.m_class_name.empty()) {
4618 specifier_ap->AddSpecification(
4619 m_options.m_class_name.c_str(),
4620 SymbolContextSpecifier::eClassOrNamespaceSpecified);
4621 }
4622
4623 if (!m_options.m_file_name.empty()) {
4624 specifier_ap->AddSpecification(
4625 m_options.m_file_name.c_str(),
4626 SymbolContextSpecifier::eFileSpecified);
4627 }
4628
4629 if (m_options.m_line_start != 0) {
4630 specifier_ap->AddLineSpecification(
4631 m_options.m_line_start,
4632 SymbolContextSpecifier::eLineStartSpecified);
4633 }
4634
4635 if (m_options.m_line_end != UINT_MAX) {
4636 specifier_ap->AddLineSpecification(
4637 m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4638 }
4639
4640 if (!m_options.m_function_name.empty()) {
4641 specifier_ap->AddSpecification(
4642 m_options.m_function_name.c_str(),
4643 SymbolContextSpecifier::eFunctionSpecified);
4644 }
4645 }
4646
4647 if (specifier_ap)
4648 new_hook_sp->SetSpecifier(specifier_ap.release());
4649
4650 // Next see if any of the thread options have been entered:
4651
4652 if (m_options.m_thread_specified) {
4653 ThreadSpec *thread_spec = new ThreadSpec();
4654
4655 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID) {
4656 thread_spec->SetTID(m_options.m_thread_id);
4657 }
4658
4659 if (m_options.m_thread_index != UINT32_MAX)
4660 thread_spec->SetIndex(m_options.m_thread_index);
4661
4662 if (!m_options.m_thread_name.empty())
4663 thread_spec->SetName(m_options.m_thread_name.c_str());
4664
4665 if (!m_options.m_queue_name.empty())
4666 thread_spec->SetQueueName(m_options.m_queue_name.c_str());
4667
4668 new_hook_sp->SetThreadSpecifier(thread_spec);
4669 }
4670 if (m_options.m_use_one_liner) {
4671 // Use one-liner.
4672 new_hook_sp->GetCommandPointer()->AppendString(
4673 m_options.m_one_liner.c_str());
4674 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n",
4675 new_hook_sp->GetID());
4676 } else {
4677 m_stop_hook_sp = new_hook_sp;
4678 m_interpreter.GetLLDBCommandsFromIOHandler(
4679 "> ", // Prompt
4680 *this, // IOHandlerDelegate
4681 true, // Run IOHandler in async mode
4682 nullptr); // Baton for the "io_handler" that will be passed back
4683 // into our IOHandlerDelegate functions
4684 }
4685 result.SetStatus(eReturnStatusSuccessFinishNoResult);
4686 } else {
4687 result.AppendError("invalid target\n");
4688 result.SetStatus(eReturnStatusFailed);
4689 }
4690
4691 return result.Succeeded();
4692 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004693
Jim Ingham9575d842011-03-11 03:53:59 +00004694private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004695 CommandOptions m_options;
4696 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00004697};
4698
Jim Ingham9575d842011-03-11 03:53:59 +00004699#pragma mark CommandObjectTargetStopHookDelete
4700
4701//-------------------------------------------------------------------------
4702// CommandObjectTargetStopHookDelete
4703//-------------------------------------------------------------------------
4704
Kate Stoneb9c1b512016-09-06 20:57:50 +00004705class CommandObjectTargetStopHookDelete : public CommandObjectParsed {
Jim Ingham9575d842011-03-11 03:53:59 +00004706public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004707 CommandObjectTargetStopHookDelete(CommandInterpreter &interpreter)
4708 : CommandObjectParsed(interpreter, "target stop-hook delete",
4709 "Delete a stop-hook.",
4710 "target stop-hook delete [<idx>]") {}
Jim Ingham9575d842011-03-11 03:53:59 +00004711
Kate Stoneb9c1b512016-09-06 20:57:50 +00004712 ~CommandObjectTargetStopHookDelete() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00004713
Jim Ingham5a988412012-06-08 21:56:10 +00004714protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004715 bool DoExecute(Args &command, CommandReturnObject &result) override {
4716 Target *target = GetSelectedOrDummyTarget();
4717 if (target) {
4718 // FIXME: see if we can use the breakpoint id style parser?
4719 size_t num_args = command.GetArgumentCount();
4720 if (num_args == 0) {
4721 if (!m_interpreter.Confirm("Delete all stop hooks?", true)) {
4722 result.SetStatus(eReturnStatusFailed);
4723 return false;
4724 } else {
4725 target->RemoveAllStopHooks();
Jim Ingham9575d842011-03-11 03:53:59 +00004726 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004727 } else {
4728 bool success;
4729 for (size_t i = 0; i < num_args; i++) {
4730 lldb::user_id_t user_id = StringConvert::ToUInt32(
4731 command.GetArgumentAtIndex(i), 0, 0, &success);
4732 if (!success) {
4733 result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4734 command.GetArgumentAtIndex(i));
4735 result.SetStatus(eReturnStatusFailed);
4736 return false;
4737 }
4738 success = target->RemoveStopHookByID(user_id);
4739 if (!success) {
4740 result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4741 command.GetArgumentAtIndex(i));
4742 result.SetStatus(eReturnStatusFailed);
4743 return false;
4744 }
Jim Ingham9575d842011-03-11 03:53:59 +00004745 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004746 }
4747 result.SetStatus(eReturnStatusSuccessFinishNoResult);
4748 } else {
4749 result.AppendError("invalid target\n");
4750 result.SetStatus(eReturnStatusFailed);
Jim Ingham9575d842011-03-11 03:53:59 +00004751 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004752
4753 return result.Succeeded();
4754 }
Jim Ingham9575d842011-03-11 03:53:59 +00004755};
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004756
Jim Ingham9575d842011-03-11 03:53:59 +00004757#pragma mark CommandObjectTargetStopHookEnableDisable
4758
4759//-------------------------------------------------------------------------
4760// CommandObjectTargetStopHookEnableDisable
4761//-------------------------------------------------------------------------
4762
Kate Stoneb9c1b512016-09-06 20:57:50 +00004763class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed {
Jim Ingham9575d842011-03-11 03:53:59 +00004764public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004765 CommandObjectTargetStopHookEnableDisable(CommandInterpreter &interpreter,
4766 bool enable, const char *name,
4767 const char *help, const char *syntax)
4768 : CommandObjectParsed(interpreter, name, help, syntax), m_enable(enable) {
4769 }
Jim Ingham9575d842011-03-11 03:53:59 +00004770
Kate Stoneb9c1b512016-09-06 20:57:50 +00004771 ~CommandObjectTargetStopHookEnableDisable() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00004772
Jim Ingham5a988412012-06-08 21:56:10 +00004773protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004774 bool DoExecute(Args &command, CommandReturnObject &result) override {
4775 Target *target = GetSelectedOrDummyTarget();
4776 if (target) {
4777 // FIXME: see if we can use the breakpoint id style parser?
4778 size_t num_args = command.GetArgumentCount();
4779 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004780
Kate Stoneb9c1b512016-09-06 20:57:50 +00004781 if (num_args == 0) {
4782 target->SetAllStopHooksActiveState(m_enable);
4783 } else {
4784 for (size_t i = 0; i < num_args; i++) {
4785 lldb::user_id_t user_id = StringConvert::ToUInt32(
4786 command.GetArgumentAtIndex(i), 0, 0, &success);
4787 if (!success) {
4788 result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4789 command.GetArgumentAtIndex(i));
4790 result.SetStatus(eReturnStatusFailed);
4791 return false;
4792 }
4793 success = target->SetStopHookActiveStateByID(user_id, m_enable);
4794 if (!success) {
4795 result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4796 command.GetArgumentAtIndex(i));
4797 result.SetStatus(eReturnStatusFailed);
4798 return false;
4799 }
Jim Ingham9575d842011-03-11 03:53:59 +00004800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004801 }
4802 result.SetStatus(eReturnStatusSuccessFinishNoResult);
4803 } else {
4804 result.AppendError("invalid target\n");
4805 result.SetStatus(eReturnStatusFailed);
Jim Ingham9575d842011-03-11 03:53:59 +00004806 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004807 return result.Succeeded();
4808 }
4809
Jim Ingham9575d842011-03-11 03:53:59 +00004810private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004811 bool m_enable;
Jim Ingham9575d842011-03-11 03:53:59 +00004812};
4813
4814#pragma mark CommandObjectTargetStopHookList
4815
4816//-------------------------------------------------------------------------
4817// CommandObjectTargetStopHookList
4818//-------------------------------------------------------------------------
4819
Kate Stoneb9c1b512016-09-06 20:57:50 +00004820class CommandObjectTargetStopHookList : public CommandObjectParsed {
Jim Ingham9575d842011-03-11 03:53:59 +00004821public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004822 CommandObjectTargetStopHookList(CommandInterpreter &interpreter)
4823 : CommandObjectParsed(interpreter, "target stop-hook list",
4824 "List all stop-hooks.",
4825 "target stop-hook list [<type>]") {}
Jim Ingham9575d842011-03-11 03:53:59 +00004826
Kate Stoneb9c1b512016-09-06 20:57:50 +00004827 ~CommandObjectTargetStopHookList() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00004828
Jim Ingham5a988412012-06-08 21:56:10 +00004829protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004830 bool DoExecute(Args &command, CommandReturnObject &result) override {
4831 Target *target = GetSelectedOrDummyTarget();
4832 if (!target) {
4833 result.AppendError("invalid target\n");
4834 result.SetStatus(eReturnStatusFailed);
4835 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00004836 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004837
4838 size_t num_hooks = target->GetNumStopHooks();
4839 if (num_hooks == 0) {
4840 result.GetOutputStream().PutCString("No stop hooks.\n");
4841 } else {
4842 for (size_t i = 0; i < num_hooks; i++) {
4843 Target::StopHookSP this_hook = target->GetStopHookAtIndex(i);
4844 if (i > 0)
4845 result.GetOutputStream().PutCString("\n");
4846 this_hook->GetDescription(&(result.GetOutputStream()),
4847 eDescriptionLevelFull);
4848 }
4849 }
4850 result.SetStatus(eReturnStatusSuccessFinishResult);
4851 return result.Succeeded();
4852 }
Jim Ingham9575d842011-03-11 03:53:59 +00004853};
4854
4855#pragma mark CommandObjectMultiwordTargetStopHooks
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004856
Jim Ingham9575d842011-03-11 03:53:59 +00004857//-------------------------------------------------------------------------
4858// CommandObjectMultiwordTargetStopHooks
4859//-------------------------------------------------------------------------
4860
Kate Stoneb9c1b512016-09-06 20:57:50 +00004861class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword {
Jim Ingham9575d842011-03-11 03:53:59 +00004862public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00004863 CommandObjectMultiwordTargetStopHooks(CommandInterpreter &interpreter)
4864 : CommandObjectMultiword(
4865 interpreter, "target stop-hook",
4866 "Commands for operating on debugger target stop-hooks.",
4867 "target stop-hook <subcommand> [<subcommand-options>]") {
4868 LoadSubCommand("add", CommandObjectSP(
4869 new CommandObjectTargetStopHookAdd(interpreter)));
4870 LoadSubCommand(
4871 "delete",
4872 CommandObjectSP(new CommandObjectTargetStopHookDelete(interpreter)));
4873 LoadSubCommand("disable",
4874 CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4875 interpreter, false, "target stop-hook disable [<id>]",
4876 "Disable a stop-hook.", "target stop-hook disable")));
4877 LoadSubCommand("enable",
4878 CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4879 interpreter, true, "target stop-hook enable [<id>]",
4880 "Enable a stop-hook.", "target stop-hook enable")));
4881 LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetStopHookList(
4882 interpreter)));
4883 }
Jim Ingham9575d842011-03-11 03:53:59 +00004884
Kate Stoneb9c1b512016-09-06 20:57:50 +00004885 ~CommandObjectMultiwordTargetStopHooks() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00004886};
4887
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004888#pragma mark CommandObjectMultiwordTarget
4889
4890//-------------------------------------------------------------------------
4891// CommandObjectMultiwordTarget
4892//-------------------------------------------------------------------------
4893
Kate Stoneb9c1b512016-09-06 20:57:50 +00004894CommandObjectMultiwordTarget::CommandObjectMultiwordTarget(
4895 CommandInterpreter &interpreter)
4896 : CommandObjectMultiword(interpreter, "target",
4897 "Commands for operating on debugger targets.",
4898 "target <subcommand> [<subcommand-options>]") {
4899 LoadSubCommand("create",
4900 CommandObjectSP(new CommandObjectTargetCreate(interpreter)));
4901 LoadSubCommand("delete",
4902 CommandObjectSP(new CommandObjectTargetDelete(interpreter)));
4903 LoadSubCommand("list",
4904 CommandObjectSP(new CommandObjectTargetList(interpreter)));
4905 LoadSubCommand("select",
4906 CommandObjectSP(new CommandObjectTargetSelect(interpreter)));
4907 LoadSubCommand(
4908 "stop-hook",
4909 CommandObjectSP(new CommandObjectMultiwordTargetStopHooks(interpreter)));
4910 LoadSubCommand("modules",
4911 CommandObjectSP(new CommandObjectTargetModules(interpreter)));
4912 LoadSubCommand("symbols",
4913 CommandObjectSP(new CommandObjectTargetSymbols(interpreter)));
4914 LoadSubCommand("variable",
4915 CommandObjectSP(new CommandObjectTargetVariable(interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004916}
4917
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004918CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget() = default;