blob: aa43a2304aad93004f195791e35c6bdc24564af3 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectTarget.h"
13
14// C Includes
15#include <errno.h>
Greg Claytonc4a99bc2011-02-01 01:13:32 +000016
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
19// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000022#include "lldb/Core/IOHandler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000025#include "lldb/Core/Section.h"
Greg Clayton7260f622011-04-18 08:33:37 +000026#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton644247c2011-07-07 01:59:51 +000028#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000029#include "lldb/DataFormatters/ValueObjectPrinter.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000030#include "lldb/Host/StringConvert.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000031#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000034#include "lldb/Interpreter/Options.h"
Greg Clayton7260f622011-04-18 08:33:37 +000035#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000036#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000037#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000038#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton715c2362011-07-07 04:38:25 +000039#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Clayton7260f622011-04-18 08:33:37 +000040#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000041#include "lldb/Interpreter/OptionGroupUInt64.h"
42#include "lldb/Interpreter/OptionGroupUUID.h"
Jason Molendac6127dd2014-11-21 02:25:15 +000043#include "lldb/Interpreter/OptionGroupString.h"
Greg Clayton644247c2011-07-07 01:59:51 +000044#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton1f746072012-08-29 21:13:06 +000045#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000046#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000047#include "lldb/Symbol/LineTable.h"
48#include "lldb/Symbol/ObjectFile.h"
49#include "lldb/Symbol/SymbolFile.h"
50#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000051#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000052#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000053#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000055#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000056#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000058#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
60using namespace lldb;
61using namespace lldb_private;
62
Greg Clayton7260f622011-04-18 08:33:37 +000063
64
65static void
66DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
67{
Greg Clayton176761e2011-04-19 04:19:37 +000068 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000069
Greg Claytonaa149cb2011-08-11 02:48:45 +000070 Module *exe_module = target->GetExecutableModulePointer();
Greg Clayton7260f622011-04-18 08:33:37 +000071 char exe_path[PATH_MAX];
72 bool exe_valid = false;
Greg Claytonaa149cb2011-08-11 02:48:45 +000073 if (exe_module)
74 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000075
Greg Clayton7260f622011-04-18 08:33:37 +000076 if (!exe_valid)
77 ::strcpy (exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000078
Greg Clayton7260f622011-04-18 08:33:37 +000079 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000080
Greg Clayton7260f622011-04-18 08:33:37 +000081 uint32_t properties = 0;
82 if (target_arch.IsValid())
83 {
84 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
85 properties++;
86 }
87 PlatformSP platform_sp (target->GetPlatform());
88 if (platform_sp)
Greg Clayton57abc5d2013-05-10 21:47:16 +000089 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000090
Greg Clayton7260f622011-04-18 08:33:37 +000091 ProcessSP process_sp (target->GetProcessSP());
92 bool show_process_status = false;
93 if (process_sp)
94 {
95 lldb::pid_t pid = process_sp->GetID();
96 StateType state = process_sp->GetState();
97 if (show_stopped_process_status)
Greg Clayton2637f822011-11-17 01:23:07 +000098 show_process_status = StateIsStoppedState(state, true);
Greg Clayton7260f622011-04-18 08:33:37 +000099 const char *state_cstr = StateAsCString (state);
100 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000101 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Clayton7260f622011-04-18 08:33:37 +0000102 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
103 }
104 if (properties > 0)
105 strm.PutCString (" )\n");
106 else
107 strm.EOL();
108 if (show_process_status)
109 {
110 const bool only_threads_with_stop_reason = true;
111 const uint32_t start_frame = 0;
112 const uint32_t num_frames = 1;
113 const uint32_t num_frames_with_source = 1;
114 process_sp->GetStatus (strm);
115 process_sp->GetThreadStatus (strm,
116 only_threads_with_stop_reason,
117 start_frame,
118 num_frames,
119 num_frames_with_source);
120
121 }
122}
123
124static uint32_t
125DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
126{
127 const uint32_t num_targets = target_list.GetNumTargets();
128 if (num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000129 {
Greg Clayton7260f622011-04-18 08:33:37 +0000130 TargetSP selected_target_sp (target_list.GetSelectedTarget());
131 strm.PutCString ("Current targets:\n");
132 for (uint32_t i=0; i<num_targets; ++i)
133 {
134 TargetSP target_sp (target_list.GetTargetAtIndex (i));
135 if (target_sp)
136 {
137 bool is_selected = target_sp.get() == selected_target_sp.get();
138 DumpTargetInfo (i,
139 target_sp.get(),
140 is_selected ? "* " : " ",
141 show_stopped_process_status,
142 strm);
143 }
144 }
145 }
146 return num_targets;
147}
148#pragma mark CommandObjectTargetCreate
149
150//-------------------------------------------------------------------------
151// "target create"
152//-------------------------------------------------------------------------
153
Jim Ingham5a988412012-06-08 21:56:10 +0000154class CommandObjectTargetCreate : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000155{
156public:
157 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000158 CommandObjectParsed (interpreter,
159 "target create",
160 "Create a target using the argument as the main executable.",
161 NULL),
Greg Clayton7260f622011-04-18 08:33:37 +0000162 m_option_group (interpreter),
Greg Clayton644247c2011-07-07 01:59:51 +0000163 m_arch_option (),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000164 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000165 m_platform_path (LLDB_OPT_SET_1, false, "platform-path", 'P', 0, eArgTypePath, "Path to the remote file to use for this target."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000166 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
Jim Inghambf22b962013-07-11 01:47:46 +0000167 m_remote_file (LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename, "Fullpath to the file on the remote host if debugging remotely."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000168 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Clayton7260f622011-04-18 08:33:37 +0000169 {
170 CommandArgumentEntry arg;
171 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000172
Greg Clayton7260f622011-04-18 08:33:37 +0000173 // Define the first (and only) variant of this arg.
174 file_arg.arg_type = eArgTypeFilename;
175 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000176
Greg Clayton7260f622011-04-18 08:33:37 +0000177 // There is only one variant this argument could be; put it into the argument entry.
178 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000179
Greg Clayton7260f622011-04-18 08:33:37 +0000180 // Push the data for the first argument into the m_arguments vector.
181 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000182
Greg Clayton644247c2011-07-07 01:59:51 +0000183 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000184 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000185 m_option_group.Append (&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000186 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Jim Inghambf22b962013-07-11 01:47:46 +0000187 m_option_group.Append (&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000188 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000189 m_option_group.Finalize();
190 }
191
192 ~CommandObjectTargetCreate ()
193 {
194 }
195
196 Options *
197 GetOptions ()
198 {
199 return &m_option_group;
200 }
201
Greg Claytonc7bece562013-01-25 18:06:21 +0000202 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +0000203 HandleArgumentCompletion (Args &input,
204 int &cursor_index,
205 int &cursor_char_position,
206 OptionElementVector &opt_element_vector,
207 int match_start_point,
208 int max_return_elements,
209 bool &word_complete,
210 StringList &matches)
211 {
212 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
213 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000214
Jim Ingham5a988412012-06-08 21:56:10 +0000215 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
216 CommandCompletions::eDiskFileCompletion,
217 completion_str.c_str(),
218 match_start_point,
219 max_return_elements,
220 NULL,
221 word_complete,
222 matches);
223 return matches.GetSize();
224 }
225
226protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000227 bool
Jim Ingham5a988412012-06-08 21:56:10 +0000228 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000229 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000230 const size_t argc = command.GetArgumentCount();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000231 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
Jim Inghambf22b962013-07-11 01:47:46 +0000232 FileSpec remote_file (m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000233
Greg Clayton5acc1252014-08-15 18:00:45 +0000234 if (core_file)
235 {
236 if (!core_file.Exists())
237 {
238 result.AppendErrorWithFormat("core file '%s' doesn't exist", core_file.GetPath().c_str());
239 result.SetStatus (eReturnStatusFailed);
240 return false;
241
242 }
243 if (!core_file.Readable())
244 {
245 result.AppendErrorWithFormat("core file '%s' is not readable", core_file.GetPath().c_str());
246 result.SetStatus (eReturnStatusFailed);
247 return false;
248 }
249 }
250
Daniel Maleae0f8f572013-08-26 23:57:52 +0000251 if (argc == 1 || core_file || remote_file)
Greg Clayton7260f622011-04-18 08:33:37 +0000252 {
Greg Clayton1c5f1862012-11-30 19:05:35 +0000253 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
254 if (symfile)
255 {
Greg Clayton5acc1252014-08-15 18:00:45 +0000256 if (symfile.Exists())
257 {
258 if (!symfile.Readable())
259 {
260 result.AppendErrorWithFormat("symbol file '%s' is not readable", core_file.GetPath().c_str());
261 result.SetStatus (eReturnStatusFailed);
262 return false;
263 }
264 }
265 else
Greg Clayton1c5f1862012-11-30 19:05:35 +0000266 {
267 char symfile_path[PATH_MAX];
268 symfile.GetPath(symfile_path, sizeof(symfile_path));
269 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
270 result.SetStatus (eReturnStatusFailed);
271 return false;
272 }
273 }
274
Greg Clayton7260f622011-04-18 08:33:37 +0000275 const char *file_path = command.GetArgumentAtIndex(0);
276 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000277 FileSpec file_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000278
Daniel Maleae0f8f572013-08-26 23:57:52 +0000279 if (file_path)
280 file_spec.SetFile (file_path, true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000281
Daniel Maleae0f8f572013-08-26 23:57:52 +0000282 bool must_set_platform_path = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000283
Greg Clayton7260f622011-04-18 08:33:37 +0000284 Debugger &debugger = m_interpreter.GetDebugger();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000285
286 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000287 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Clayton1c5f1862012-11-30 19:05:35 +0000288 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000289 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000290 file_path,
Greg Claytond26a1e52015-01-28 22:08:17 +0000291 arch_cstr,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000292 get_dependent_files,
Greg Claytond26a1e52015-01-28 22:08:17 +0000293 NULL,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000294 target_sp));
295
Greg Clayton7260f622011-04-18 08:33:37 +0000296 if (target_sp)
297 {
Greg Claytond26a1e52015-01-28 22:08:17 +0000298 // Only get the platform after we create the target because we might have
299 // switched platforms depending on what the arguments were to CreateTarget()
300 // we can't rely on the selected platform.
301
302 PlatformSP platform_sp = target_sp->GetPlatform();
303
304 if (remote_file)
305 {
306 if (platform_sp)
307 {
308 // I have a remote file.. two possible cases
309 if (file_spec && file_spec.Exists())
310 {
311 // if the remote file does not exist, push it there
312 if (!platform_sp->GetFileExists (remote_file))
313 {
314 Error err = platform_sp->PutFile(file_spec, remote_file);
315 if (err.Fail())
316 {
317 result.AppendError(err.AsCString());
318 result.SetStatus (eReturnStatusFailed);
319 return false;
320 }
321 }
322 }
323 else
324 {
325 // there is no local file and we need one
326 // in order to make the remote ---> local transfer we need a platform
327 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
328 if (!platform_sp)
329 {
330 result.AppendError("unable to perform remote debugging without a platform");
331 result.SetStatus (eReturnStatusFailed);
332 return false;
333 }
334 if (file_path)
335 {
336 // copy the remote file to the local file
337 Error err = platform_sp->GetFile(remote_file, file_spec);
338 if (err.Fail())
339 {
340 result.AppendError(err.AsCString());
341 result.SetStatus (eReturnStatusFailed);
342 return false;
343 }
344 }
345 else
346 {
347 // make up a local file
348 result.AppendError("remote --> local transfer without local path is not implemented yet");
349 result.SetStatus (eReturnStatusFailed);
350 return false;
351 }
352 }
353 }
354 else
355 {
356 result.AppendError("no platform found for target");
357 result.SetStatus (eReturnStatusFailed);
358 return false;
359 }
360 }
361
Jim Inghambf22b962013-07-11 01:47:46 +0000362 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000363 {
364 ModuleSP module_sp (target_sp->GetExecutableModule());
365 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000366 {
367 if (symfile)
368 module_sp->SetSymbolFileFileSpec(symfile);
369 if (remote_file)
370 {
371 std::string remote_path = remote_file.GetPath();
372 target_sp->SetArg0(remote_path.c_str());
373 module_sp->SetPlatformFileSpec(remote_file);
374 }
375 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000376 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000377
Greg Clayton7260f622011-04-18 08:33:37 +0000378 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000379 if (must_set_platform_path)
380 {
381 ModuleSpec main_module_spec(file_spec);
382 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
383 if (module_sp)
384 module_sp->SetPlatformFileSpec(remote_file);
385 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000386 if (core_file)
387 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000388 char core_path[PATH_MAX];
389 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000390 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000391 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000392 FileSpec core_file_dir;
393 core_file_dir.GetDirectory() = core_file.GetDirectory();
394 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000395
Greg Claytonc859e2d2012-02-13 23:10:39 +0000396 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
397
398 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000399 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000400 // Seems wierd that we Launch a core file, but that is
401 // what we do!
402 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000403
Greg Claytonc859e2d2012-02-13 23:10:39 +0000404 if (error.Fail())
405 {
406 result.AppendError(error.AsCString("can't find plug-in for core file"));
407 result.SetStatus (eReturnStatusFailed);
408 return false;
409 }
410 else
411 {
412 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
413 result.SetStatus (eReturnStatusSuccessFinishNoResult);
414 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000415 }
416 else
417 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000418 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
419 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000420 }
421 }
422 else
423 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000424 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000425 result.SetStatus (eReturnStatusFailed);
426 }
427 }
428 else
429 {
430 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
431 result.SetStatus (eReturnStatusSuccessFinishNoResult);
432 }
Greg Clayton7260f622011-04-18 08:33:37 +0000433 }
434 else
435 {
436 result.AppendError(error.AsCString());
437 result.SetStatus (eReturnStatusFailed);
438 }
439 }
440 else
441 {
Jason Molenda65e06422015-01-20 03:06:17 +0000442 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core option.\n", m_cmd_name.c_str());
Greg Clayton7260f622011-04-18 08:33:37 +0000443 result.SetStatus (eReturnStatusFailed);
444 }
445 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000446 }
447
Greg Clayton7260f622011-04-18 08:33:37 +0000448private:
449 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000450 OptionGroupArchitecture m_arch_option;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000451 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000452 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000453 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000454 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000455 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000456};
457
458#pragma mark CommandObjectTargetList
459
460//----------------------------------------------------------------------
461// "target list"
462//----------------------------------------------------------------------
463
Jim Ingham5a988412012-06-08 21:56:10 +0000464class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000465{
466public:
467 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000468 CommandObjectParsed (interpreter,
469 "target list",
470 "List all current targets in the current debug session.",
471 NULL,
472 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000473 {
474 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000475
Greg Clayton7260f622011-04-18 08:33:37 +0000476 virtual
477 ~CommandObjectTargetList ()
478 {
479 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000480
Jim Ingham5a988412012-06-08 21:56:10 +0000481protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000482 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000483 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000484 {
485 if (args.GetArgumentCount() == 0)
486 {
487 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000488
Greg Clayton7260f622011-04-18 08:33:37 +0000489 bool show_stopped_process_status = false;
490 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
491 {
492 strm.PutCString ("No targets.\n");
493 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000494 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000495 }
496 else
497 {
498 result.AppendError ("the 'target list' command takes no arguments\n");
499 result.SetStatus (eReturnStatusFailed);
500 }
501 return result.Succeeded();
502 }
503};
504
505
506#pragma mark CommandObjectTargetSelect
507
508//----------------------------------------------------------------------
509// "target select"
510//----------------------------------------------------------------------
511
Jim Ingham5a988412012-06-08 21:56:10 +0000512class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000513{
514public:
515 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000516 CommandObjectParsed (interpreter,
517 "target select",
518 "Select a target as the current target by target index.",
519 NULL,
520 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000521 {
522 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000523
Greg Clayton7260f622011-04-18 08:33:37 +0000524 virtual
525 ~CommandObjectTargetSelect ()
526 {
527 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528
Jim Ingham5a988412012-06-08 21:56:10 +0000529protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000530 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000531 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000532 {
533 if (args.GetArgumentCount() == 1)
534 {
535 bool success = false;
536 const char *target_idx_arg = args.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000537 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton7260f622011-04-18 08:33:37 +0000538 if (success)
539 {
540 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
541 const uint32_t num_targets = target_list.GetNumTargets();
542 if (target_idx < num_targets)
543 {
544 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
545 if (target_sp)
546 {
547 Stream &strm = result.GetOutputStream();
548 target_list.SetSelectedTarget (target_sp.get());
549 bool show_stopped_process_status = false;
550 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000551 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000552 }
553 else
554 {
555 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
556 result.SetStatus (eReturnStatusFailed);
557 }
558 }
559 else
560 {
Todd Fiala352237d2014-08-29 20:14:21 +0000561 if (num_targets > 0)
562 {
563 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
564 target_idx,
565 num_targets - 1);
566 } else
567 {
568 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
569 target_idx);
570 }
Greg Clayton7260f622011-04-18 08:33:37 +0000571 result.SetStatus (eReturnStatusFailed);
572 }
573 }
574 else
575 {
576 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
577 result.SetStatus (eReturnStatusFailed);
578 }
579 }
580 else
581 {
582 result.AppendError ("'target select' takes a single argument: a target index\n");
583 result.SetStatus (eReturnStatusFailed);
584 }
585 return result.Succeeded();
586 }
587};
588
Greg Clayton3418c852011-08-10 02:10:13 +0000589#pragma mark CommandObjectTargetSelect
590
591//----------------------------------------------------------------------
592// "target delete"
593//----------------------------------------------------------------------
594
Jim Ingham5a988412012-06-08 21:56:10 +0000595class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000596{
597public:
598 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000599 CommandObjectParsed (interpreter,
600 "target delete",
601 "Delete one or more targets by target index.",
602 NULL,
603 0),
Greg Claytonaa149cb2011-08-11 02:48:45 +0000604 m_option_group (interpreter),
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000605 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', "Perform extra cleanup to minimize memory consumption after deleting the target.", false, false)
Greg Clayton3418c852011-08-10 02:10:13 +0000606 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000607 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
608 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000609 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000610
Greg Clayton3418c852011-08-10 02:10:13 +0000611 virtual
612 ~CommandObjectTargetDelete ()
613 {
614 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000615
Jim Ingham5a988412012-06-08 21:56:10 +0000616 Options *
617 GetOptions ()
618 {
619 return &m_option_group;
620 }
621
622protected:
Greg Clayton3418c852011-08-10 02:10:13 +0000623 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000624 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton3418c852011-08-10 02:10:13 +0000625 {
626 const size_t argc = args.GetArgumentCount();
627 std::vector<TargetSP> delete_target_list;
628 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
629 bool success = true;
630 TargetSP target_sp;
631 if (argc > 0)
632 {
633 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000634 // Bail out if don't have any targets.
635 if (num_targets == 0) {
636 result.AppendError("no targets to delete");
637 result.SetStatus(eReturnStatusFailed);
638 success = false;
639 }
640
Greg Clayton3418c852011-08-10 02:10:13 +0000641 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
642 {
643 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
Vince Harron5275aaa2015-01-15 20:08:35 +0000644 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton3418c852011-08-10 02:10:13 +0000645 if (success)
646 {
647 if (target_idx < num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000648 {
Greg Clayton3418c852011-08-10 02:10:13 +0000649 target_sp = target_list.GetTargetAtIndex (target_idx);
650 if (target_sp)
651 {
652 delete_target_list.push_back (target_sp);
653 continue;
654 }
655 }
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000656 if (num_targets > 1)
657 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
658 target_idx,
659 num_targets - 1);
660 else
661 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
662 target_idx);
663
Greg Clayton3418c852011-08-10 02:10:13 +0000664 result.SetStatus (eReturnStatusFailed);
665 success = false;
666 }
667 else
668 {
669 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
670 result.SetStatus (eReturnStatusFailed);
671 success = false;
672 }
673 }
Greg Clayton3418c852011-08-10 02:10:13 +0000674 }
675 else
676 {
677 target_sp = target_list.GetSelectedTarget();
678 if (target_sp)
679 {
680 delete_target_list.push_back (target_sp);
681 }
682 else
683 {
684 result.AppendErrorWithFormat("no target is currently selected\n");
685 result.SetStatus (eReturnStatusFailed);
686 success = false;
687 }
688 }
689 if (success)
690 {
691 const size_t num_targets_to_delete = delete_target_list.size();
692 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
693 {
694 target_sp = delete_target_list[idx];
695 target_list.DeleteTarget(target_sp);
696 target_sp->Destroy();
697 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000698 // If "--clean" was specified, prune any orphaned shared modules from
699 // the global shared module list
700 if (m_cleanup_option.GetOptionValue ())
701 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000702 const bool mandatory = true;
703 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000704 }
Greg Clayton3418c852011-08-10 02:10:13 +0000705 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
706 result.SetStatus(eReturnStatusSuccessFinishResult);
707 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000708
Greg Clayton3418c852011-08-10 02:10:13 +0000709 return result.Succeeded();
710 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000711
Greg Claytonaa149cb2011-08-11 02:48:45 +0000712 OptionGroupOptions m_option_group;
713 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000714};
715
Greg Clayton7260f622011-04-18 08:33:37 +0000716
Greg Clayton644247c2011-07-07 01:59:51 +0000717#pragma mark CommandObjectTargetVariable
718
719//----------------------------------------------------------------------
720// "target variable"
721//----------------------------------------------------------------------
722
Jim Ingham5a988412012-06-08 21:56:10 +0000723class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000724{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000725 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
726 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
727
Greg Clayton644247c2011-07-07 01:59:51 +0000728public:
729 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000730 CommandObjectParsed (interpreter,
731 "target variable",
Greg Claytondfdd1eb2012-11-03 00:10:22 +0000732 "Read global variable(s) prior to, or while running your binary.",
Jim Ingham5a988412012-06-08 21:56:10 +0000733 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000734 eFlagRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000735 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000736 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000737 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000738 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
739 SHORT_OPTION_FILE, 0, eArgTypeFilename,
740 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
741 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
742 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
743 "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
Greg Clayton644247c2011-07-07 01:59:51 +0000744 m_varobj_options()
745 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000746 CommandArgumentEntry arg;
747 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000748
Johnny Chen81ab3f52011-08-22 22:22:00 +0000749 // Define the first (and only) variant of this arg.
750 var_name_arg.arg_type = eArgTypeVarName;
751 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000752
Johnny Chen81ab3f52011-08-22 22:22:00 +0000753 // There is only one variant this argument could be; put it into the argument entry.
754 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000755
Johnny Chen81ab3f52011-08-22 22:22:00 +0000756 // Push the data for the first argument into the m_arguments vector.
757 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000758
Greg Clayton644247c2011-07-07 01:59:51 +0000759 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000760 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000761 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton644247c2011-07-07 01:59:51 +0000762 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
763 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
764 m_option_group.Finalize();
765 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000766
Greg Clayton644247c2011-07-07 01:59:51 +0000767 virtual
768 ~CommandObjectTargetVariable ()
769 {
770 }
Greg Clayton884fb692011-07-08 21:46:14 +0000771
772 void
773 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
774 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000775 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000776
Enrico Granata560558e2015-02-11 02:35:39 +0000777 if (false == valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
778 true == valobj_sp->IsRuntimeSupportValue())
779 return;
780
Greg Clayton884fb692011-07-08 21:46:14 +0000781 switch (var_sp->GetScope())
782 {
783 case eValueTypeVariableGlobal:
784 if (m_option_variable.show_scope)
785 s.PutCString("GLOBAL: ");
786 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000787
Greg Clayton884fb692011-07-08 21:46:14 +0000788 case eValueTypeVariableStatic:
789 if (m_option_variable.show_scope)
790 s.PutCString("STATIC: ");
791 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000792
Greg Clayton884fb692011-07-08 21:46:14 +0000793 case eValueTypeVariableArgument:
794 if (m_option_variable.show_scope)
795 s.PutCString(" ARG: ");
796 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797
Greg Clayton884fb692011-07-08 21:46:14 +0000798 case eValueTypeVariableLocal:
799 if (m_option_variable.show_scope)
800 s.PutCString(" LOCAL: ");
801 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000802
Greg Clayton884fb692011-07-08 21:46:14 +0000803 default:
804 break;
805 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000806
Greg Clayton45ba8542011-07-10 19:21:23 +0000807 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000808 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000809 bool show_fullpaths = false;
810 bool show_module = true;
811 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
812 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000813 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000814
Greg Clayton1deb7962011-10-25 06:44:01 +0000815 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000816 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000817 options.SetFormat(format);
818
819 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000821 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000822 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000823
Greg Claytonc7bece562013-01-25 18:06:21 +0000824 static size_t GetVariableCallback (void *baton,
825 const char *name,
826 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000827 {
828 Target *target = static_cast<Target *>(baton);
829 if (target)
830 {
831 return target->GetImages().FindGlobalVariables (ConstString(name),
832 true,
833 UINT32_MAX,
834 variable_list);
835 }
836 return 0;
837 }
Greg Clayton884fb692011-07-08 21:46:14 +0000838
Jim Ingham5a988412012-06-08 21:56:10 +0000839 Options *
840 GetOptions ()
841 {
842 return &m_option_group;
843 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000844
Jim Ingham5a988412012-06-08 21:56:10 +0000845protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000846 void
847 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
848 {
849 size_t count = variable_list.GetSize();
850 if (count > 0)
851 {
852 if (sc.module_sp)
853 {
854 if (sc.comp_unit)
855 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000856 s.Printf ("Global variables for %s in %s:\n",
857 sc.comp_unit->GetPath().c_str(),
858 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000859 }
860 else
861 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000862 s.Printf ("Global variables for %s\n",
863 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000864 }
865 }
866 else if (sc.comp_unit)
867 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000868 s.Printf ("Global variables for %s\n",
869 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000870 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000871
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000872 for (uint32_t i=0; i<count; ++i)
873 {
874 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
875 if (var_sp)
876 {
877 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000878
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000879 if (valobj_sp)
880 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
881 }
882 }
883 }
884
885 }
Greg Clayton644247c2011-07-07 01:59:51 +0000886 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000887 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton644247c2011-07-07 01:59:51 +0000888 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000889 Target *target = m_exe_ctx.GetTargetPtr();
890 const size_t argc = args.GetArgumentCount();
891 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000892
Greg Claytonf9fc6092013-01-09 19:44:40 +0000893 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000894 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000895
896 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000897 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000898 VariableList variable_list;
899 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000900
Greg Claytonf9fc6092013-01-09 19:44:40 +0000901 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000902 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000903 bool use_var_name = false;
904 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000905 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000906 RegularExpression regex(arg);
907 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000908 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000909 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000910 result.SetStatus (eReturnStatusFailed);
911 return false;
912 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000913 use_var_name = true;
914 matches = target->GetImages().FindGlobalVariables (regex,
915 true,
916 UINT32_MAX,
917 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000918 }
919 else
920 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000921 Error error (Variable::GetValuesForVariableExpressionPath (arg,
922 m_exe_ctx.GetBestExecutionContextScope(),
923 GetVariableCallback,
924 target,
925 variable_list,
926 valobj_list));
927 matches = variable_list.GetSize();
928 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000929
Greg Claytonf9fc6092013-01-09 19:44:40 +0000930 if (matches == 0)
931 {
932 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
933 result.SetStatus (eReturnStatusFailed);
934 return false;
935 }
936 else
937 {
938 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000939 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000940 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
941 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000942 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000943 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
944 if (!valobj_sp)
945 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946
Greg Claytonf9fc6092013-01-09 19:44:40 +0000947 if (valobj_sp)
948 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000949 }
950 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000951 }
Greg Clayton644247c2011-07-07 01:59:51 +0000952 }
953 }
954 else
955 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000956 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
957 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
958 SymbolContextList sc_list;
959 const size_t num_compile_units = compile_units.GetSize();
960 const size_t num_shlibs = shlibs.GetSize();
961 if (num_compile_units == 0 && num_shlibs == 0)
962 {
963 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000964 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000965 CompileUnit *comp_unit = NULL;
966 if (frame)
967 {
968 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
969 if (sc.comp_unit)
970 {
971 const bool can_create = true;
972 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
973 if (comp_unit_varlist_sp)
974 {
975 size_t count = comp_unit_varlist_sp->GetSize();
976 if (count > 0)
977 {
978 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
979 success = true;
980 }
981 }
982 }
983 }
984 if (!success)
985 {
986 if (frame)
987 {
988 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000989 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
990 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000991 else
992 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000993 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000994 else
995 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
996 result.SetStatus (eReturnStatusFailed);
997 }
998 }
999 else
1000 {
1001 SymbolContextList sc_list;
1002 const bool append = true;
1003 // We have one or more compile unit or shlib
1004 if (num_shlibs > 0)
1005 {
1006 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
1007 {
1008 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
1009 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001010
Greg Claytonf9fc6092013-01-09 19:44:40 +00001011 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
1012 if (module_sp)
1013 {
1014 if (num_compile_units > 0)
1015 {
1016 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1017 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1018 }
1019 else
1020 {
1021 SymbolContext sc;
1022 sc.module_sp = module_sp;
1023 sc_list.Append(sc);
1024 }
1025 }
1026 else
1027 {
1028 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001029 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1030 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001031 }
1032 }
1033 }
1034 else
1035 {
1036 // No shared libraries, we just want to find globals for the compile units files that were specified
1037 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1038 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1039 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001040
Greg Claytonf9fc6092013-01-09 19:44:40 +00001041 const uint32_t num_scs = sc_list.GetSize();
1042 if (num_scs > 0)
1043 {
1044 SymbolContext sc;
1045 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1046 {
1047 if (sc_list.GetContextAtIndex(sc_idx, sc))
1048 {
1049 if (sc.comp_unit)
1050 {
1051 const bool can_create = true;
1052 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1053 if (comp_unit_varlist_sp)
1054 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1055 }
1056 else if (sc.module_sp)
1057 {
1058 // Get all global variables for this module
1059 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1060 VariableList variable_list;
1061 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1062 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1063 }
1064 }
1065 }
1066 }
1067 }
Greg Clayton644247c2011-07-07 01:59:51 +00001068 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001069
Enrico Granata61a80ba2011-08-12 16:42:31 +00001070 if (m_interpreter.TruncationWarningNecessary())
1071 {
1072 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1073 m_cmd_name.c_str());
1074 m_interpreter.TruncationWarningGiven();
1075 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001076
Greg Clayton644247c2011-07-07 01:59:51 +00001077 return result.Succeeded();
1078 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001079
Greg Clayton644247c2011-07-07 01:59:51 +00001080 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001081 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001082 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001083 OptionGroupFileList m_option_compile_units;
1084 OptionGroupFileList m_option_shared_libraries;
1085 OptionGroupValueObjectDisplay m_varobj_options;
1086
1087};
1088
1089
Greg Claytoneffe5c92011-05-03 22:09:39 +00001090#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001091
Jim Ingham5a988412012-06-08 21:56:10 +00001092class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093{
1094public:
1095
Greg Claytoneffe5c92011-05-03 22:09:39 +00001096 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001097 CommandObjectParsed (interpreter,
1098 "target modules search-paths add",
1099 "Add new image search paths substitution pairs to the current target.",
1100 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 {
Caroline Tice405fe672010-10-04 22:28:36 +00001102 CommandArgumentEntry arg;
1103 CommandArgumentData old_prefix_arg;
1104 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001105
Caroline Tice405fe672010-10-04 22:28:36 +00001106 // Define the first variant of this arg pair.
1107 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1108 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001109
Caroline Tice405fe672010-10-04 22:28:36 +00001110 // Define the first variant of this arg pair.
1111 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1112 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001113
Caroline Tice405fe672010-10-04 22:28:36 +00001114 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1115 // must always occur together, they are treated as two variants of one argument rather than two independent
1116 // arguments. Push them both into the first argument position for m_arguments...
1117
1118 arg.push_back (old_prefix_arg);
1119 arg.push_back (new_prefix_arg);
1120
1121 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 }
1123
Greg Claytoneffe5c92011-05-03 22:09:39 +00001124 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125 {
1126 }
1127
Jim Ingham5a988412012-06-08 21:56:10 +00001128protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001130 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 CommandReturnObject &result)
1132 {
Greg Claytona7015092010-09-18 01:14:36 +00001133 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (target)
1135 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001136 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 if (argc & 1)
1138 {
Greg Clayton7260f622011-04-18 08:33:37 +00001139 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 result.SetStatus (eReturnStatusFailed);
1141 }
1142 else
1143 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001144 for (size_t i=0; i<argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 {
1146 const char *from = command.GetArgumentAtIndex(i);
1147 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001148
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 if (from[0] && to[0])
1150 {
1151 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001152 target->GetImageSearchPathList().Append (ConstString(from),
1153 ConstString(to),
1154 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001155 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 }
1157 else
1158 {
1159 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001160 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161 else
Greg Clayton7260f622011-04-18 08:33:37 +00001162 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 result.SetStatus (eReturnStatusFailed);
1164 }
1165 }
1166 }
1167 }
1168 else
1169 {
Greg Clayton7260f622011-04-18 08:33:37 +00001170 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 result.SetStatus (eReturnStatusFailed);
1172 }
1173 return result.Succeeded();
1174 }
1175};
1176
Greg Claytoneffe5c92011-05-03 22:09:39 +00001177#pragma mark CommandObjectTargetModulesSearchPathsClear
1178
Jim Ingham5a988412012-06-08 21:56:10 +00001179class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180{
1181public:
1182
Greg Claytoneffe5c92011-05-03 22:09:39 +00001183 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001184 CommandObjectParsed (interpreter,
1185 "target modules search-paths clear",
1186 "Clear all current image search path substitution pairs from the current target.",
1187 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 {
1189 }
1190
Greg Claytoneffe5c92011-05-03 22:09:39 +00001191 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 {
1193 }
1194
Jim Ingham5a988412012-06-08 21:56:10 +00001195protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001197 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 CommandReturnObject &result)
1199 {
Greg Claytona7015092010-09-18 01:14:36 +00001200 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 if (target)
1202 {
1203 bool notify = true;
1204 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001205 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 }
1207 else
1208 {
Greg Clayton7260f622011-04-18 08:33:37 +00001209 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 result.SetStatus (eReturnStatusFailed);
1211 }
1212 return result.Succeeded();
1213 }
1214};
1215
Greg Claytoneffe5c92011-05-03 22:09:39 +00001216#pragma mark CommandObjectTargetModulesSearchPathsInsert
1217
Jim Ingham5a988412012-06-08 21:56:10 +00001218class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219{
1220public:
1221
Greg Claytoneffe5c92011-05-03 22:09:39 +00001222 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001223 CommandObjectParsed (interpreter,
1224 "target modules search-paths insert",
1225 "Insert a new image search path substitution pair into the current target at the specified index.",
1226 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 {
Caroline Tice405fe672010-10-04 22:28:36 +00001228 CommandArgumentEntry arg1;
1229 CommandArgumentEntry arg2;
1230 CommandArgumentData index_arg;
1231 CommandArgumentData old_prefix_arg;
1232 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001233
Caroline Tice405fe672010-10-04 22:28:36 +00001234 // Define the first and only variant of this arg.
1235 index_arg.arg_type = eArgTypeIndex;
1236 index_arg.arg_repetition = eArgRepeatPlain;
1237
1238 // Put the one and only variant into the first arg for m_arguments:
1239 arg1.push_back (index_arg);
1240
1241 // Define the first variant of this arg pair.
1242 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1243 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001244
Caroline Tice405fe672010-10-04 22:28:36 +00001245 // Define the first variant of this arg pair.
1246 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1247 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001248
Caroline Tice405fe672010-10-04 22:28:36 +00001249 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1250 // must always occur together, they are treated as two variants of one argument rather than two independent
1251 // arguments. Push them both into the same argument position for m_arguments...
1252
1253 arg2.push_back (old_prefix_arg);
1254 arg2.push_back (new_prefix_arg);
1255
1256 // Add arguments to m_arguments.
1257 m_arguments.push_back (arg1);
1258 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
1260
Greg Claytoneffe5c92011-05-03 22:09:39 +00001261 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 {
1263 }
1264
Jim Ingham5a988412012-06-08 21:56:10 +00001265protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001267 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268 CommandReturnObject &result)
1269 {
Greg Claytona7015092010-09-18 01:14:36 +00001270 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 if (target)
1272 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001273 size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 // check for at least 3 arguments and an odd nubmer of parameters
1275 if (argc >= 3 && argc & 1)
1276 {
1277 bool success = false;
1278
Vince Harron5275aaa2015-01-15 20:08:35 +00001279 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280
1281 if (!success)
1282 {
1283 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1284 result.SetStatus (eReturnStatusFailed);
1285 return result.Succeeded();
1286 }
1287
1288 // shift off the index
1289 command.Shift();
1290 argc = command.GetArgumentCount();
1291
1292 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1293 {
1294 const char *from = command.GetArgumentAtIndex(i);
1295 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001296
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 if (from[0] && to[0])
1298 {
1299 bool last_pair = ((argc - i) == 2);
1300 target->GetImageSearchPathList().Insert (ConstString(from),
1301 ConstString(to),
1302 insert_idx,
1303 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001304 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305 }
1306 else
1307 {
1308 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001309 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 else
Greg Clayton7260f622011-04-18 08:33:37 +00001311 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 result.SetStatus (eReturnStatusFailed);
1313 return false;
1314 }
1315 }
1316 }
1317 else
1318 {
Greg Clayton7260f622011-04-18 08:33:37 +00001319 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 result.SetStatus (eReturnStatusFailed);
1321 return result.Succeeded();
1322 }
1323
1324 }
1325 else
1326 {
Greg Clayton7260f622011-04-18 08:33:37 +00001327 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 result.SetStatus (eReturnStatusFailed);
1329 }
1330 return result.Succeeded();
1331 }
1332};
1333
Greg Claytoneffe5c92011-05-03 22:09:39 +00001334
1335#pragma mark CommandObjectTargetModulesSearchPathsList
1336
1337
Jim Ingham5a988412012-06-08 21:56:10 +00001338class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339{
1340public:
1341
Greg Claytoneffe5c92011-05-03 22:09:39 +00001342 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001343 CommandObjectParsed (interpreter,
1344 "target modules search-paths list",
1345 "List all current image search path substitution pairs in the current target.",
1346 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 {
1348 }
1349
Greg Claytoneffe5c92011-05-03 22:09:39 +00001350 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351 {
1352 }
1353
Jim Ingham5a988412012-06-08 21:56:10 +00001354protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001356 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 CommandReturnObject &result)
1358 {
Greg Claytona7015092010-09-18 01:14:36 +00001359 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360 if (target)
1361 {
1362 if (command.GetArgumentCount() != 0)
1363 {
Greg Clayton7260f622011-04-18 08:33:37 +00001364 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365 result.SetStatus (eReturnStatusFailed);
1366 return result.Succeeded();
1367 }
1368
1369 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001370 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371 }
1372 else
1373 {
Greg Clayton7260f622011-04-18 08:33:37 +00001374 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 result.SetStatus (eReturnStatusFailed);
1376 }
1377 return result.Succeeded();
1378 }
1379};
1380
Greg Claytoneffe5c92011-05-03 22:09:39 +00001381#pragma mark CommandObjectTargetModulesSearchPathsQuery
1382
Jim Ingham5a988412012-06-08 21:56:10 +00001383class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384{
1385public:
1386
Greg Claytoneffe5c92011-05-03 22:09:39 +00001387 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001388 CommandObjectParsed (interpreter,
1389 "target modules search-paths query",
1390 "Transform a path using the first applicable image search path.",
1391 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 {
Caroline Tice405fe672010-10-04 22:28:36 +00001393 CommandArgumentEntry arg;
1394 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001395
Caroline Tice405fe672010-10-04 22:28:36 +00001396 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001397 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001398 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001399
Caroline Tice405fe672010-10-04 22:28:36 +00001400 // There is only one variant this argument could be; put it into the argument entry.
1401 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001402
Caroline Tice405fe672010-10-04 22:28:36 +00001403 // Push the data for the first argument into the m_arguments vector.
1404 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405 }
1406
Greg Claytoneffe5c92011-05-03 22:09:39 +00001407 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 {
1409 }
1410
Jim Ingham5a988412012-06-08 21:56:10 +00001411protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001413 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 CommandReturnObject &result)
1415 {
Greg Claytona7015092010-09-18 01:14:36 +00001416 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417 if (target)
1418 {
1419 if (command.GetArgumentCount() != 1)
1420 {
Greg Clayton7260f622011-04-18 08:33:37 +00001421 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422 result.SetStatus (eReturnStatusFailed);
1423 return result.Succeeded();
1424 }
1425
1426 ConstString orig(command.GetArgumentAtIndex(0));
1427 ConstString transformed;
1428 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1429 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1430 else
1431 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001432
1433 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434 }
1435 else
1436 {
Greg Clayton7260f622011-04-18 08:33:37 +00001437 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 result.SetStatus (eReturnStatusFailed);
1439 }
1440 return result.Succeeded();
1441 }
1442};
1443
Greg Claytoneffe5c92011-05-03 22:09:39 +00001444//----------------------------------------------------------------------
1445// Static Helper functions
1446//----------------------------------------------------------------------
1447static void
1448DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1449{
1450 if (module)
1451 {
1452 const char *arch_cstr;
1453 if (full_triple)
1454 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1455 else
1456 arch_cstr = module->GetArchitecture().GetArchitectureName();
1457 if (width)
1458 strm.Printf("%-*s", width, arch_cstr);
1459 else
1460 strm.PutCString(arch_cstr);
1461 }
1462}
1463
1464static void
1465DumpModuleUUID (Stream &strm, Module *module)
1466{
Jim Ingham28eb5712012-10-12 17:34:26 +00001467 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001468 module->GetUUID().Dump (&strm);
1469 else
1470 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001471}
1472
1473static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001474DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1475 Stream &strm,
1476 Module *module,
1477 const FileSpec &file_spec,
1478 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001479{
1480 uint32_t num_matches = 0;
1481 if (module)
1482 {
1483 SymbolContextList sc_list;
1484 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1485 0,
1486 false,
1487 eSymbolContextCompUnit,
1488 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001489
Greg Claytoneffe5c92011-05-03 22:09:39 +00001490 for (uint32_t i=0; i<num_matches; ++i)
1491 {
1492 SymbolContext sc;
1493 if (sc_list.GetContextAtIndex(i, sc))
1494 {
1495 if (i > 0)
1496 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001497
Greg Claytoneffe5c92011-05-03 22:09:39 +00001498 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1499 << module->GetFileSpec().GetFilename() << "\n";
1500 LineTable *line_table = sc.comp_unit->GetLineTable();
1501 if (line_table)
1502 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001503 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001504 lldb::eDescriptionLevelBrief);
1505 else
1506 strm << "No line table";
1507 }
1508 }
1509 }
1510 return num_matches;
1511}
1512
1513static void
1514DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1515{
1516 if (file_spec_ptr)
1517 {
1518 if (width > 0)
1519 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001520 std::string fullpath = file_spec_ptr->GetPath();
1521 strm.Printf("%-*s", width, fullpath.c_str());
1522 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001523 }
1524 else
1525 {
1526 file_spec_ptr->Dump(&strm);
1527 return;
1528 }
1529 }
1530 // Keep the width spacing correct if things go wrong...
1531 if (width > 0)
1532 strm.Printf("%-*s", width, "");
1533}
1534
1535static void
1536DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1537{
1538 if (file_spec_ptr)
1539 {
1540 if (width > 0)
1541 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1542 else
1543 file_spec_ptr->GetDirectory().Dump(&strm);
1544 return;
1545 }
1546 // Keep the width spacing correct if things go wrong...
1547 if (width > 0)
1548 strm.Printf("%-*s", width, "");
1549}
1550
1551static void
1552DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1553{
1554 if (file_spec_ptr)
1555 {
1556 if (width > 0)
1557 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1558 else
1559 file_spec_ptr->GetFilename().Dump(&strm);
1560 return;
1561 }
1562 // Keep the width spacing correct if things go wrong...
1563 if (width > 0)
1564 strm.Printf("%-*s", width, "");
1565}
1566
1567
1568static void
1569DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1570{
1571 if (module)
1572 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001573 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1574 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001575 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001576 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001577 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001578 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001579 }
1580 }
1581}
1582
1583static void
1584DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1585{
1586 if (module)
1587 {
Greg Clayton3046e662013-07-10 01:23:25 +00001588 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001589 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001590 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001591 strm.Printf ("Sections for '%s' (%s):\n",
1592 module->GetSpecificationDescription().c_str(),
1593 module->GetArchitecture().GetArchitectureName());
1594 strm.IndentMore();
1595 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1596 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001597 }
1598 }
1599}
1600
1601static bool
1602DumpModuleSymbolVendor (Stream &strm, Module *module)
1603{
1604 if (module)
1605 {
1606 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1607 if (symbol_vendor)
1608 {
1609 symbol_vendor->Dump(&strm);
1610 return true;
1611 }
1612 }
1613 return false;
1614}
1615
Greg Claytonc4a8a762012-05-15 18:43:44 +00001616static void
1617DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1618{
1619 strm.IndentMore();
1620 strm.Indent (" Address: ");
1621 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1622 strm.PutCString (" (");
1623 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1624 strm.PutCString (")\n");
1625 strm.Indent (" Summary: ");
1626 const uint32_t save_indent = strm.GetIndentLevel ();
1627 strm.SetIndentLevel (save_indent + 13);
1628 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1629 strm.SetIndentLevel (save_indent);
1630 // Print out detailed address information when verbose is enabled
1631 if (verbose)
1632 {
1633 strm.EOL();
1634 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1635 }
1636 strm.IndentLess();
1637}
1638
Greg Claytoneffe5c92011-05-03 22:09:39 +00001639static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001640LookupAddressInModule (CommandInterpreter &interpreter,
1641 Stream &strm,
1642 Module *module,
1643 uint32_t resolve_mask,
1644 lldb::addr_t raw_addr,
1645 lldb::addr_t offset,
1646 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001647{
1648 if (module)
1649 {
1650 lldb::addr_t addr = raw_addr - offset;
1651 Address so_addr;
1652 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001653 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001654 if (target && !target->GetSectionLoadList().IsEmpty())
1655 {
1656 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1657 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001658 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001659 return false;
1660 }
1661 else
1662 {
1663 if (!module->ResolveFileAddress (addr, so_addr))
1664 return false;
1665 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001666
Greg Claytoneffe5c92011-05-03 22:09:39 +00001667 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001668 DumpAddress (exe_scope, so_addr, verbose, strm);
1669// strm.IndentMore();
1670// strm.Indent (" Address: ");
1671// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1672// strm.PutCString (" (");
1673// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1674// strm.PutCString (")\n");
1675// strm.Indent (" Summary: ");
1676// const uint32_t save_indent = strm.GetIndentLevel ();
1677// strm.SetIndentLevel (save_indent + 13);
1678// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1679// strm.SetIndentLevel (save_indent);
1680// // Print out detailed address information when verbose is enabled
1681// if (verbose)
1682// {
1683// strm.EOL();
1684// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1685// }
1686// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001687 return true;
1688 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001689
Greg Claytoneffe5c92011-05-03 22:09:39 +00001690 return false;
1691}
1692
1693static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001694LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001695{
1696 if (module)
1697 {
1698 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001699
Michael Sartaina7499c92013-07-01 19:45:50 +00001700 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1701 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001702 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001703 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001704 if (symtab)
1705 {
1706 uint32_t i;
1707 std::vector<uint32_t> match_indexes;
1708 ConstString symbol_name (name);
1709 uint32_t num_matches = 0;
1710 if (name_is_regex)
1711 {
1712 RegularExpression name_regexp(name);
1713 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1714 eSymbolTypeAny,
1715 match_indexes);
1716 }
1717 else
1718 {
1719 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1720 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001721
Greg Claytoneffe5c92011-05-03 22:09:39 +00001722 if (num_matches > 0)
1723 {
1724 strm.Indent ();
1725 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1726 name_is_regex ? "the regular expression " : "", name);
1727 DumpFullpath (strm, &module->GetFileSpec(), 0);
1728 strm.PutCString(":\n");
1729 strm.IndentMore ();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001730 //Symtab::DumpSymbolHeader (&strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001731 for (i=0; i < num_matches; ++i)
1732 {
1733 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Claytonc4a8a762012-05-15 18:43:44 +00001734 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1735 symbol->GetAddress(),
1736 verbose,
1737 strm);
1738
1739// strm.Indent ();
1740// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001741 }
1742 strm.IndentLess ();
1743 return num_matches;
1744 }
1745 }
1746 }
1747 }
1748 return 0;
1749}
1750
1751
1752static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001753DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001754{
1755 strm.IndentMore ();
1756 uint32_t i;
1757 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001758
Greg Claytoneffe5c92011-05-03 22:09:39 +00001759 for (i=0; i<num_matches; ++i)
1760 {
1761 SymbolContext sc;
1762 if (sc_list.GetContextAtIndex(i, sc))
1763 {
Sean Callananf6172c22012-02-11 00:24:04 +00001764 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001765
Sean Callananf6172c22012-02-11 00:24:04 +00001766 sc.GetAddressRange(eSymbolContextEverything,
1767 0,
1768 true,
1769 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001770
Greg Claytonc4a8a762012-05-15 18:43:44 +00001771 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001772 }
1773 }
1774 strm.IndentLess ();
1775}
1776
Greg Claytonc7bece562013-01-25 18:06:21 +00001777static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001778LookupFunctionInModule (CommandInterpreter &interpreter,
1779 Stream &strm,
1780 Module *module,
1781 const char *name,
1782 bool name_is_regex,
1783 bool include_inlines,
1784 bool include_symbols,
1785 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001786{
1787 if (module && name && name[0])
1788 {
1789 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001790 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001791 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001792 if (name_is_regex)
1793 {
1794 RegularExpression function_name_regex (name);
1795 num_matches = module->FindFunctions (function_name_regex,
1796 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001797 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001798 append,
1799 sc_list);
1800 }
1801 else
1802 {
1803 ConstString function_name (name);
Sean Callananb6d70eb2011-10-12 02:08:07 +00001804 num_matches = module->FindFunctions (function_name,
1805 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001806 eFunctionNameTypeAuto,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001807 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001808 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001809 append,
1810 sc_list);
1811 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001812
Greg Claytoneffe5c92011-05-03 22:09:39 +00001813 if (num_matches)
1814 {
1815 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001816 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001817 DumpFullpath (strm, &module->GetFileSpec(), 0);
1818 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001819 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001820 }
1821 return num_matches;
1822 }
1823 return 0;
1824}
1825
Greg Claytonc7bece562013-01-25 18:06:21 +00001826static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001827LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001828 Stream &strm,
1829 Module *module,
1830 const char *name_cstr,
1831 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001832{
1833 if (module && name_cstr && name_cstr[0])
1834 {
Greg Claytond1767f02011-12-08 02:13:16 +00001835 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001836 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001837 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001838 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001839 SymbolContext sc;
1840
1841 ConstString name(name_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001842 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001843
Greg Claytond1767f02011-12-08 02:13:16 +00001844 if (num_matches)
1845 {
1846 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001847 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001848 DumpFullpath (strm, &module->GetFileSpec(), 0);
1849 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001850 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001851 {
Greg Claytond1767f02011-12-08 02:13:16 +00001852 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001853 {
Greg Claytond1767f02011-12-08 02:13:16 +00001854 // Resolve the clang type so that any forward references
1855 // to types that haven't yet been parsed will get parsed.
1856 type_sp->GetClangFullType ();
1857 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001858 // Print all typedef chains
1859 TypeSP typedef_type_sp (type_sp);
1860 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1861 while (typedefed_type_sp)
1862 {
1863 strm.EOL();
1864 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1865 typedefed_type_sp->GetClangFullType ();
1866 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1867 typedef_type_sp = typedefed_type_sp;
1868 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1869 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001870 }
Greg Claytond1767f02011-12-08 02:13:16 +00001871 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001872 }
Greg Claytond1767f02011-12-08 02:13:16 +00001873 }
1874 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001875 }
1876 return 0;
1877}
1878
Greg Claytonc7bece562013-01-25 18:06:21 +00001879static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001880LookupTypeHere (CommandInterpreter &interpreter,
1881 Stream &strm,
1882 const SymbolContext &sym_ctx,
1883 const char *name_cstr,
1884 bool name_is_regex)
1885{
1886 if (!sym_ctx.module_sp)
1887 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001888
Sean Callanand38b4a92012-06-06 20:49:55 +00001889 TypeList type_list;
1890 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001891 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001892 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001893
Sean Callanand38b4a92012-06-06 20:49:55 +00001894 ConstString name(name_cstr);
1895 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001896
Sean Callanand38b4a92012-06-06 20:49:55 +00001897 if (num_matches)
1898 {
1899 strm.Indent ();
1900 strm.PutCString("Best match found in ");
1901 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1902 strm.PutCString(":\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001903
Sean Callanand38b4a92012-06-06 20:49:55 +00001904 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1905 if (type_sp)
1906 {
1907 // Resolve the clang type so that any forward references
1908 // to types that haven't yet been parsed will get parsed.
1909 type_sp->GetClangFullType ();
1910 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1911 // Print all typedef chains
1912 TypeSP typedef_type_sp (type_sp);
1913 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1914 while (typedefed_type_sp)
1915 {
1916 strm.EOL();
1917 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1918 typedefed_type_sp->GetClangFullType ();
1919 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1920 typedef_type_sp = typedefed_type_sp;
1921 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1922 }
1923 }
1924 strm.EOL();
1925 }
1926 return num_matches;
1927}
1928
1929static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001930LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001931 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001932 Module *module,
1933 const FileSpec &file_spec,
1934 uint32_t line,
1935 bool check_inlines,
1936 bool verbose)
1937{
1938 if (module && file_spec)
1939 {
1940 SymbolContextList sc_list;
1941 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1942 eSymbolContextEverything, sc_list);
1943 if (num_matches > 0)
1944 {
1945 strm.Indent ();
1946 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1947 strm << file_spec;
1948 if (line > 0)
1949 strm.Printf (":%u", line);
1950 strm << " in ";
1951 DumpFullpath (strm, &module->GetFileSpec(), 0);
1952 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001953 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001954 return num_matches;
1955 }
1956 }
1957 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001958}
1959
Greg Clayton8ee64382011-11-10 01:18:58 +00001960
1961static size_t
1962FindModulesByName (Target *target,
1963 const char *module_name,
1964 ModuleList &module_list,
1965 bool check_global_list)
1966{
1967// Dump specified images (by basename or fullpath)
1968 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001969 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001970
Greg Clayton8ee64382011-11-10 01:18:58 +00001971 const size_t initial_size = module_list.GetSize ();
1972
Greg Claytonf3156262012-07-11 20:46:47 +00001973 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001974 {
1975 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001976 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001977 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001978 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001979 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001980 {
1981 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001982
Greg Clayton8ee64382011-11-10 01:18:58 +00001983 if (module)
1984 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001985 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001986 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001987 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001988 module_list.AppendIfNeeded(module_sp);
1989 }
1990 }
1991 }
1992 }
Greg Claytonf3156262012-07-11 20:46:47 +00001993 else
1994 {
1995 if (target)
1996 {
1997 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001998
Greg Claytonf3156262012-07-11 20:46:47 +00001999 // Not found in our module list for our target, check the main
2000 // shared module list in case it is a extra file used somewhere
2001 // else
2002 if (num_matches == 0)
2003 {
2004 module_spec.GetArchitecture() = target->GetArchitecture();
2005 ModuleList::FindSharedModules (module_spec, module_list);
2006 }
2007 }
2008 else
2009 {
2010 ModuleList::FindSharedModules (module_spec,module_list);
2011 }
2012 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002013
Greg Clayton8ee64382011-11-10 01:18:58 +00002014 return module_list.GetSize () - initial_size;
2015}
2016
Greg Claytoneffe5c92011-05-03 22:09:39 +00002017#pragma mark CommandObjectTargetModulesModuleAutoComplete
2018
2019//----------------------------------------------------------------------
2020// A base command object class that can auto complete with module file
2021// paths
2022//----------------------------------------------------------------------
2023
Jim Ingham5a988412012-06-08 21:56:10 +00002024class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002025{
2026public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002027 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2028 const char *name,
2029 const char *help,
2030 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002031 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002032 {
2033 CommandArgumentEntry arg;
2034 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002035
Greg Claytoneffe5c92011-05-03 22:09:39 +00002036 // Define the first (and only) variant of this arg.
2037 file_arg.arg_type = eArgTypeFilename;
2038 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002039
Greg Claytoneffe5c92011-05-03 22:09:39 +00002040 // There is only one variant this argument could be; put it into the argument entry.
2041 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002042
Greg Claytoneffe5c92011-05-03 22:09:39 +00002043 // Push the data for the first argument into the m_arguments vector.
2044 m_arguments.push_back (arg);
2045 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002046
Greg Claytoneffe5c92011-05-03 22:09:39 +00002047 virtual
2048 ~CommandObjectTargetModulesModuleAutoComplete ()
2049 {
2050 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002051
Greg Claytoneffe5c92011-05-03 22:09:39 +00002052 virtual int
2053 HandleArgumentCompletion (Args &input,
2054 int &cursor_index,
2055 int &cursor_char_position,
2056 OptionElementVector &opt_element_vector,
2057 int match_start_point,
2058 int max_return_elements,
2059 bool &word_complete,
2060 StringList &matches)
2061 {
2062 // Arguments are the standard module completer.
2063 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2064 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002065
Greg Claytoneffe5c92011-05-03 22:09:39 +00002066 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2067 CommandCompletions::eModuleCompletion,
2068 completion_str.c_str(),
2069 match_start_point,
2070 max_return_elements,
2071 NULL,
2072 word_complete,
2073 matches);
2074 return matches.GetSize();
2075 }
2076};
2077
2078#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2079
2080//----------------------------------------------------------------------
2081// A base command object class that can auto complete with module source
2082// file paths
2083//----------------------------------------------------------------------
2084
Jim Ingham5a988412012-06-08 21:56:10 +00002085class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002086{
2087public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002088 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002089 const char *name,
2090 const char *help,
2091 const char *syntax,
2092 uint32_t flags) :
2093 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002094 {
2095 CommandArgumentEntry arg;
2096 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002097
Greg Claytoneffe5c92011-05-03 22:09:39 +00002098 // Define the first (and only) variant of this arg.
2099 source_file_arg.arg_type = eArgTypeSourceFile;
2100 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002101
Greg Claytoneffe5c92011-05-03 22:09:39 +00002102 // There is only one variant this argument could be; put it into the argument entry.
2103 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002104
Greg Claytoneffe5c92011-05-03 22:09:39 +00002105 // Push the data for the first argument into the m_arguments vector.
2106 m_arguments.push_back (arg);
2107 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002108
Greg Claytoneffe5c92011-05-03 22:09:39 +00002109 virtual
2110 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2111 {
2112 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002113
Greg Claytoneffe5c92011-05-03 22:09:39 +00002114 virtual int
2115 HandleArgumentCompletion (Args &input,
2116 int &cursor_index,
2117 int &cursor_char_position,
2118 OptionElementVector &opt_element_vector,
2119 int match_start_point,
2120 int max_return_elements,
2121 bool &word_complete,
2122 StringList &matches)
2123 {
2124 // Arguments are the standard source file completer.
2125 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2126 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002127
Greg Claytoneffe5c92011-05-03 22:09:39 +00002128 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2129 CommandCompletions::eSourceFileCompletion,
2130 completion_str.c_str(),
2131 match_start_point,
2132 max_return_elements,
2133 NULL,
2134 word_complete,
2135 matches);
2136 return matches.GetSize();
2137 }
2138};
2139
2140
2141#pragma mark CommandObjectTargetModulesDumpSymtab
2142
2143
2144class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2145{
2146public:
2147 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2148 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2149 "target modules dump symtab",
2150 "Dump the symbol table from one or more target modules.",
2151 NULL),
2152 m_options (interpreter)
2153 {
2154 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002155
Greg Claytoneffe5c92011-05-03 22:09:39 +00002156 virtual
2157 ~CommandObjectTargetModulesDumpSymtab ()
2158 {
2159 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002160
Jim Ingham5a988412012-06-08 21:56:10 +00002161 virtual Options *
2162 GetOptions ()
2163 {
2164 return &m_options;
2165 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002166
Jim Ingham5a988412012-06-08 21:56:10 +00002167 class CommandOptions : public Options
2168 {
2169 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002170 CommandOptions (CommandInterpreter &interpreter) :
2171 Options(interpreter),
2172 m_sort_order (eSortOrderNone)
2173 {
2174 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002175
Jim Ingham5a988412012-06-08 21:56:10 +00002176 virtual
2177 ~CommandOptions ()
2178 {
2179 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002180
Jim Ingham5a988412012-06-08 21:56:10 +00002181 virtual Error
2182 SetOptionValue (uint32_t option_idx, const char *option_arg)
2183 {
2184 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002185 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002186
Jim Ingham5a988412012-06-08 21:56:10 +00002187 switch (short_option)
2188 {
2189 case 's':
2190 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2191 g_option_table[option_idx].enum_values,
2192 eSortOrderNone,
2193 error);
2194 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002195
Jim Ingham5a988412012-06-08 21:56:10 +00002196 default:
2197 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2198 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002199
Jim Ingham5a988412012-06-08 21:56:10 +00002200 }
2201 return error;
2202 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002203
Jim Ingham5a988412012-06-08 21:56:10 +00002204 void
2205 OptionParsingStarting ()
2206 {
2207 m_sort_order = eSortOrderNone;
2208 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002209
Jim Ingham5a988412012-06-08 21:56:10 +00002210 const OptionDefinition*
2211 GetDefinitions ()
2212 {
2213 return g_option_table;
2214 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002215
Jim Ingham5a988412012-06-08 21:56:10 +00002216 // Options table: Required for subclasses of Options.
2217 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002218
Jim Ingham5a988412012-06-08 21:56:10 +00002219 SortOrder m_sort_order;
2220 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002221
Jim Ingham5a988412012-06-08 21:56:10 +00002222protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002223 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002224 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002225 CommandReturnObject &result)
2226 {
2227 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2228 if (target == NULL)
2229 {
2230 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2231 result.SetStatus (eReturnStatusFailed);
2232 return false;
2233 }
2234 else
2235 {
2236 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002237
Greg Claytoneffe5c92011-05-03 22:09:39 +00002238 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2239 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2240 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002241
Greg Claytoneffe5c92011-05-03 22:09:39 +00002242 if (command.GetArgumentCount() == 0)
2243 {
2244 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002245 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002246 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002247 if (num_modules > 0)
2248 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002249 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002250 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002251 {
2252 if (num_dumped > 0)
2253 {
2254 result.GetOutputStream().EOL();
2255 result.GetOutputStream().EOL();
2256 }
2257 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002258 DumpModuleSymtab (m_interpreter,
2259 result.GetOutputStream(),
2260 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2261 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002262 }
2263 }
2264 else
2265 {
2266 result.AppendError ("the target has no associated executable images");
2267 result.SetStatus (eReturnStatusFailed);
2268 return false;
2269 }
2270 }
2271 else
2272 {
2273 // Dump specified images (by basename or fullpath)
2274 const char *arg_cstr;
2275 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2276 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002277 ModuleList module_list;
2278 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2279 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002280 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002281 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002282 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002283 Module *module = module_list.GetModulePointerAtIndex(i);
2284 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002285 {
2286 if (num_dumped > 0)
2287 {
2288 result.GetOutputStream().EOL();
2289 result.GetOutputStream().EOL();
2290 }
2291 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002292 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002293 }
2294 }
2295 }
2296 else
2297 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2298 }
2299 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002300
Greg Claytoneffe5c92011-05-03 22:09:39 +00002301 if (num_dumped > 0)
2302 result.SetStatus (eReturnStatusSuccessFinishResult);
2303 else
2304 {
2305 result.AppendError ("no matching executable images found");
2306 result.SetStatus (eReturnStatusFailed);
2307 }
2308 }
2309 return result.Succeeded();
2310 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002311
Greg Claytoneffe5c92011-05-03 22:09:39 +00002312 CommandOptions m_options;
2313};
2314
2315static OptionEnumValueElement
2316g_sort_option_enumeration[4] =
2317{
2318 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2319 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2320 { eSortOrderByName, "name", "Sort output by symbol name."},
2321 { 0, NULL, NULL }
2322};
2323
2324
2325OptionDefinition
2326CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2327{
Zachary Turnerd37221d2014-07-09 16:31:49 +00002328 { LLDB_OPT_SET_1, false, "sort", 's', OptionParser::eRequiredArgument, NULL, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2329 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002330};
2331
2332#pragma mark CommandObjectTargetModulesDumpSections
2333
2334//----------------------------------------------------------------------
2335// Image section dumping command
2336//----------------------------------------------------------------------
2337
2338class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2339{
2340public:
2341 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2342 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2343 "target modules dump sections",
2344 "Dump the sections from one or more target modules.",
2345 //"target modules dump sections [<file1> ...]")
2346 NULL)
2347 {
2348 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002349
Greg Claytoneffe5c92011-05-03 22:09:39 +00002350 virtual
2351 ~CommandObjectTargetModulesDumpSections ()
2352 {
2353 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002354
Jim Ingham5a988412012-06-08 21:56:10 +00002355protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002356 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002357 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002358 CommandReturnObject &result)
2359 {
2360 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2361 if (target == NULL)
2362 {
2363 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2364 result.SetStatus (eReturnStatusFailed);
2365 return false;
2366 }
2367 else
2368 {
2369 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002370
Greg Claytoneffe5c92011-05-03 22:09:39 +00002371 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2372 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2373 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002374
Greg Claytoneffe5c92011-05-03 22:09:39 +00002375 if (command.GetArgumentCount() == 0)
2376 {
2377 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002378 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002379 if (num_modules > 0)
2380 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002381 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002382 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002383 {
2384 num_dumped++;
2385 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2386 }
2387 }
2388 else
2389 {
2390 result.AppendError ("the target has no associated executable images");
2391 result.SetStatus (eReturnStatusFailed);
2392 return false;
2393 }
2394 }
2395 else
2396 {
2397 // Dump specified images (by basename or fullpath)
2398 const char *arg_cstr;
2399 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2400 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002401 ModuleList module_list;
2402 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2403 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002404 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002405 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002406 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002407 Module *module = module_list.GetModulePointerAtIndex(i);
2408 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002409 {
2410 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002411 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002412 }
2413 }
2414 }
2415 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002416 {
2417 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002418 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002419
Greg Claytoneffe5c92011-05-03 22:09:39 +00002420 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002421 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002422 }
2423 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002424
Greg Claytoneffe5c92011-05-03 22:09:39 +00002425 if (num_dumped > 0)
2426 result.SetStatus (eReturnStatusSuccessFinishResult);
2427 else
2428 {
2429 result.AppendError ("no matching executable images found");
2430 result.SetStatus (eReturnStatusFailed);
2431 }
2432 }
2433 return result.Succeeded();
2434 }
2435};
2436
2437
2438#pragma mark CommandObjectTargetModulesDumpSymfile
2439
2440//----------------------------------------------------------------------
2441// Image debug symbol dumping command
2442//----------------------------------------------------------------------
2443
2444class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2445{
2446public:
2447 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2448 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2449 "target modules dump symfile",
2450 "Dump the debug symbol file for one or more target modules.",
2451 //"target modules dump symfile [<file1> ...]")
2452 NULL)
2453 {
2454 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002455
Greg Claytoneffe5c92011-05-03 22:09:39 +00002456 virtual
2457 ~CommandObjectTargetModulesDumpSymfile ()
2458 {
2459 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002460
Jim Ingham5a988412012-06-08 21:56:10 +00002461protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002462 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002463 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002464 CommandReturnObject &result)
2465 {
2466 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2467 if (target == NULL)
2468 {
2469 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2470 result.SetStatus (eReturnStatusFailed);
2471 return false;
2472 }
2473 else
2474 {
2475 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002476
Greg Claytoneffe5c92011-05-03 22:09:39 +00002477 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2478 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2479 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002480
Greg Claytoneffe5c92011-05-03 22:09:39 +00002481 if (command.GetArgumentCount() == 0)
2482 {
2483 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002484 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002485 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002486 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002487 if (num_modules > 0)
2488 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002489 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002490 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2491 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002492 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002493 num_dumped++;
2494 }
2495 }
2496 else
2497 {
2498 result.AppendError ("the target has no associated executable images");
2499 result.SetStatus (eReturnStatusFailed);
2500 return false;
2501 }
2502 }
2503 else
2504 {
2505 // Dump specified images (by basename or fullpath)
2506 const char *arg_cstr;
2507 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2508 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002509 ModuleList module_list;
2510 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2511 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002512 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002513 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002514 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002515 Module *module = module_list.GetModulePointerAtIndex(i);
2516 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002517 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002518 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002519 num_dumped++;
2520 }
2521 }
2522 }
2523 else
2524 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2525 }
2526 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002527
Greg Claytoneffe5c92011-05-03 22:09:39 +00002528 if (num_dumped > 0)
2529 result.SetStatus (eReturnStatusSuccessFinishResult);
2530 else
2531 {
2532 result.AppendError ("no matching executable images found");
2533 result.SetStatus (eReturnStatusFailed);
2534 }
2535 }
2536 return result.Succeeded();
2537 }
2538};
2539
2540
2541#pragma mark CommandObjectTargetModulesDumpLineTable
2542
2543//----------------------------------------------------------------------
2544// Image debug line table dumping command
2545//----------------------------------------------------------------------
2546
2547class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2548{
2549public:
2550 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2551 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002552 "target modules dump line-table",
Jim Inghamcc0273d2013-06-18 20:27:11 +00002553 "Dump the line table for one or more compilation units.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00002554 NULL,
2555 eFlagRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002556 {
2557 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002558
Greg Claytoneffe5c92011-05-03 22:09:39 +00002559 virtual
2560 ~CommandObjectTargetModulesDumpLineTable ()
2561 {
2562 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002563
Jim Ingham5a988412012-06-08 21:56:10 +00002564protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002565 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002566 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002567 CommandReturnObject &result)
2568 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002569 Target *target = m_exe_ctx.GetTargetPtr();
2570 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002571
Greg Claytonf9fc6092013-01-09 19:44:40 +00002572 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2573 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2574 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002575
Greg Claytonf9fc6092013-01-09 19:44:40 +00002576 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002577 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002578 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00002579 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002580 }
2581 else
2582 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002583 // Dump specified images (by basename or fullpath)
2584 const char *arg_cstr;
2585 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002586 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002587 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002588
Greg Claytonf9fc6092013-01-09 19:44:40 +00002589 const ModuleList &target_modules = target->GetImages();
2590 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002591 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002592 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002593 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002594 uint32_t num_dumped = 0;
2595 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002596 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002597 if (DumpCompileUnitLineTable (m_interpreter,
2598 result.GetOutputStream(),
2599 target_modules.GetModulePointerAtIndexUnlocked(i),
2600 file_spec,
2601 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2602 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002603 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002604 if (num_dumped == 0)
2605 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2606 else
2607 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002608 }
2609 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002610 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002611
Greg Claytonf9fc6092013-01-09 19:44:40 +00002612 if (total_num_dumped > 0)
2613 result.SetStatus (eReturnStatusSuccessFinishResult);
2614 else
2615 {
2616 result.AppendError ("no source filenames matched any command arguments");
2617 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002618 }
2619 return result.Succeeded();
2620 }
2621};
2622
2623
2624#pragma mark CommandObjectTargetModulesDump
2625
2626//----------------------------------------------------------------------
2627// Dump multi-word command for target modules
2628//----------------------------------------------------------------------
2629
2630class CommandObjectTargetModulesDump : public CommandObjectMultiword
2631{
2632public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002633 //------------------------------------------------------------------
2634 // Constructors and Destructors
2635 //------------------------------------------------------------------
2636 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2637 CommandObjectMultiword (interpreter,
2638 "target modules dump",
2639 "A set of commands for dumping information about one or more target modules.",
2640 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2641 {
2642 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2643 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2644 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2645 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2646 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002647
Greg Claytoneffe5c92011-05-03 22:09:39 +00002648 virtual
2649 ~CommandObjectTargetModulesDump()
2650 {
2651 }
2652};
2653
Jim Ingham5a988412012-06-08 21:56:10 +00002654class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002655{
2656public:
2657 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002658 CommandObjectParsed (interpreter,
2659 "target modules add",
2660 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002661 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002662 m_option_group (interpreter),
2663 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable.")
Greg Claytoneffe5c92011-05-03 22:09:39 +00002664 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002665 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002666 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002667 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002668 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002669
Greg Claytoneffe5c92011-05-03 22:09:39 +00002670 virtual
2671 ~CommandObjectTargetModulesAdd ()
2672 {
2673 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002674
Greg Clayton50a24bd2012-11-29 22:16:27 +00002675 virtual Options *
2676 GetOptions ()
2677 {
2678 return &m_option_group;
2679 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002680
Greg Claytonc7bece562013-01-25 18:06:21 +00002681 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00002682 HandleArgumentCompletion (Args &input,
2683 int &cursor_index,
2684 int &cursor_char_position,
2685 OptionElementVector &opt_element_vector,
2686 int match_start_point,
2687 int max_return_elements,
2688 bool &word_complete,
2689 StringList &matches)
2690 {
2691 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2692 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002693
Jim Ingham5a988412012-06-08 21:56:10 +00002694 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2695 CommandCompletions::eDiskFileCompletion,
2696 completion_str.c_str(),
2697 match_start_point,
2698 max_return_elements,
2699 NULL,
2700 word_complete,
2701 matches);
2702 return matches.GetSize();
2703 }
2704
2705protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002706 OptionGroupOptions m_option_group;
2707 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002708 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002709
Greg Claytoneffe5c92011-05-03 22:09:39 +00002710 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002711 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002712 CommandReturnObject &result)
2713 {
2714 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2715 if (target == NULL)
2716 {
2717 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2718 result.SetStatus (eReturnStatusFailed);
2719 return false;
2720 }
2721 else
2722 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002723 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002724
Greg Claytoneffe5c92011-05-03 22:09:39 +00002725 const size_t argc = args.GetArgumentCount();
2726 if (argc == 0)
2727 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002728 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2729 {
2730 // We are given a UUID only, go locate the file
2731 ModuleSpec module_spec;
2732 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002733 if (m_symbol_file.GetOptionValue().OptionWasSet())
2734 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002735 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2736 {
2737 ModuleSP module_sp (target->GetSharedModule (module_spec));
2738 if (module_sp)
2739 {
2740 result.SetStatus (eReturnStatusSuccessFinishResult);
2741 return true;
2742 }
2743 else
2744 {
2745 StreamString strm;
2746 module_spec.GetUUID().Dump (&strm);
2747 if (module_spec.GetFileSpec())
2748 {
2749 if (module_spec.GetSymbolFileSpec())
2750 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002751 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s and symbol file %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002752 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002753 module_spec.GetFileSpec().GetPath().c_str(),
2754 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002755 }
2756 else
2757 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002758 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002759 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002760 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002761 }
2762 }
2763 else
2764 {
2765 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2766 strm.GetString().c_str());
2767 }
2768 result.SetStatus (eReturnStatusFailed);
2769 return false;
2770 }
2771 }
2772 else
2773 {
2774 StreamString strm;
2775 module_spec.GetUUID().Dump (&strm);
2776 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2777 result.SetStatus (eReturnStatusFailed);
2778 return false;
2779 }
2780 }
2781 else
2782 {
2783 result.AppendError ("one or more executable image paths must be specified");
2784 result.SetStatus (eReturnStatusFailed);
2785 return false;
2786 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002787 }
2788 else
2789 {
2790 for (size_t i=0; i<argc; ++i)
2791 {
2792 const char *path = args.GetArgumentAtIndex(i);
2793 if (path)
2794 {
2795 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002796 if (file_spec.Exists())
2797 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002798 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002799 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2800 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002801 if (m_symbol_file.GetOptionValue().OptionWasSet())
2802 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002803 if (!module_spec.GetArchitecture().IsValid())
2804 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002805 Error error;
2806 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002807 if (!module_sp)
2808 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002809 const char *error_cstr = error.AsCString();
2810 if (error_cstr)
2811 result.AppendError (error_cstr);
2812 else
2813 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002814 result.SetStatus (eReturnStatusFailed);
2815 return false;
2816 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002817 else
2818 {
2819 flush = true;
2820 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002821 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002822 }
2823 else
2824 {
2825 char resolved_path[PATH_MAX];
2826 result.SetStatus (eReturnStatusFailed);
2827 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2828 {
2829 if (strcmp (resolved_path, path) != 0)
2830 {
2831 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2832 break;
2833 }
2834 }
2835 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2836 break;
2837 }
2838 }
2839 }
2840 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002841
Sean Callananb36c6c02012-12-13 01:39:39 +00002842 if (flush)
2843 {
2844 ProcessSP process = target->GetProcessSP();
2845 if (process)
2846 process->Flush();
2847 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002848 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002849
Greg Claytoneffe5c92011-05-03 22:09:39 +00002850 return result.Succeeded();
2851 }
2852
Greg Claytoneffe5c92011-05-03 22:09:39 +00002853};
2854
2855class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2856{
2857public:
2858 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2859 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2860 "target modules load",
2861 "Set the load addresses for one or more sections in a target module.",
2862 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2863 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002864 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName, "Fullpath or basename for module to load.", ""),
Greg Claytoneffe5c92011-05-03 22:09:39 +00002865 m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset, "Set the load address for all sections to be the virtual address in the file plus the offset.", 0)
2866 {
2867 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2868 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2869 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2870 m_option_group.Finalize();
2871 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002872
Greg Claytoneffe5c92011-05-03 22:09:39 +00002873 virtual
2874 ~CommandObjectTargetModulesLoad ()
2875 {
2876 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002877
Jim Ingham5a988412012-06-08 21:56:10 +00002878 virtual Options *
2879 GetOptions ()
2880 {
2881 return &m_option_group;
2882 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002883
Jim Ingham5a988412012-06-08 21:56:10 +00002884protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002885 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002886 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002887 CommandReturnObject &result)
2888 {
2889 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2890 if (target == NULL)
2891 {
2892 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2893 result.SetStatus (eReturnStatusFailed);
2894 return false;
2895 }
2896 else
2897 {
2898 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002899 ModuleSpec module_spec;
2900 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002901 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002902 {
2903 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002904 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2905 const bool use_global_module_list = true;
2906 ModuleList module_list;
2907 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2908 if (num_matches == 1)
2909 {
2910 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2911 }
2912 else if (num_matches > 1 )
2913 {
2914 search_using_module_spec = false;
2915 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2916 result.SetStatus (eReturnStatusFailed);
2917 }
2918 else
2919 {
2920 search_using_module_spec = false;
2921 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2922 result.SetStatus (eReturnStatusFailed);
2923 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002924 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002925
Greg Claytoneffe5c92011-05-03 22:09:39 +00002926 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002927 {
2928 search_using_module_spec = true;
2929 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2930 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002931
Greg Claytonb9a01b32012-02-26 05:51:37 +00002932 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002933 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002934 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002935 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002936
2937 char path[PATH_MAX];
2938 if (num_matches == 1)
2939 {
2940 Module *module = matching_modules.GetModulePointerAtIndex(0);
2941 if (module)
2942 {
2943 ObjectFile *objfile = module->GetObjectFile();
2944 if (objfile)
2945 {
Greg Clayton3046e662013-07-10 01:23:25 +00002946 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002947 if (section_list)
2948 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002949 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002950 if (argc == 0)
2951 {
2952 if (m_slide_option.GetOptionValue().OptionWasSet())
2953 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002954 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002955 const bool slide_is_offset = true;
2956 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002957 }
2958 else
2959 {
2960 result.AppendError ("one or more section name + load address pair must be specified");
2961 result.SetStatus (eReturnStatusFailed);
2962 return false;
2963 }
2964 }
2965 else
2966 {
2967 if (m_slide_option.GetOptionValue().OptionWasSet())
2968 {
2969 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2970 result.SetStatus (eReturnStatusFailed);
2971 return false;
2972 }
2973
2974 for (size_t i=0; i<argc; i += 2)
2975 {
2976 const char *sect_name = args.GetArgumentAtIndex(i);
2977 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2978 if (sect_name && load_addr_cstr)
2979 {
2980 ConstString const_sect_name(sect_name);
2981 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00002982 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002983 if (success)
2984 {
2985 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2986 if (section_sp)
2987 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002988 if (section_sp->IsThreadSpecific())
2989 {
2990 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2991 result.SetStatus (eReturnStatusFailed);
2992 break;
2993 }
2994 else
2995 {
Greg Clayton7820bd12012-07-07 01:24:12 +00002996 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00002997 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00002998 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00002999 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003000 }
3001 else
3002 {
3003 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
3004 result.SetStatus (eReturnStatusFailed);
3005 break;
3006 }
3007 }
3008 else
3009 {
3010 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
3011 result.SetStatus (eReturnStatusFailed);
3012 break;
3013 }
3014 }
3015 else
3016 {
3017 if (sect_name)
3018 result.AppendError ("section names must be followed by a load address.\n");
3019 else
3020 result.AppendError ("one or more section name + load address pair must be specified.\n");
3021 result.SetStatus (eReturnStatusFailed);
3022 break;
3023 }
3024 }
3025 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003026
Greg Clayton741f3f92012-03-27 21:10:07 +00003027 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003028 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003029 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003030 Process *process = m_exe_ctx.GetProcessPtr();
3031 if (process)
3032 process->Flush();
3033 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003034 }
3035 else
3036 {
3037 module->GetFileSpec().GetPath (path, sizeof(path));
3038 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3039 result.SetStatus (eReturnStatusFailed);
3040 }
3041 }
3042 else
3043 {
3044 module->GetFileSpec().GetPath (path, sizeof(path));
3045 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3046 result.SetStatus (eReturnStatusFailed);
3047 }
3048 }
3049 else
3050 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003051 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3052 if (module_spec_file)
3053 {
3054 module_spec_file->GetPath (path, sizeof(path));
3055 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3056 }
3057 else
3058 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003059 result.SetStatus (eReturnStatusFailed);
3060 }
3061 }
3062 else
3063 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003064 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003065
Greg Claytonb9a01b32012-02-26 05:51:37 +00003066 if (module_spec.GetFileSpec())
3067 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003068 else
3069 path[0] = '\0';
3070
Greg Claytonb9a01b32012-02-26 05:51:37 +00003071 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003072 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003073 if (num_matches > 1)
3074 {
3075 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3076 path[0] ? " file=" : "",
3077 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003078 !uuid_str.empty() ? " uuid=" : "",
3079 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003080 for (size_t i=0; i<num_matches; ++i)
3081 {
3082 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3083 result.AppendMessageWithFormat("%s\n", path);
3084 }
3085 }
3086 else
3087 {
3088 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3089 path[0] ? " file=" : "",
3090 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003091 !uuid_str.empty() ? " uuid=" : "",
3092 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003093 }
3094 result.SetStatus (eReturnStatusFailed);
3095 }
3096 }
3097 else
3098 {
3099 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3100 result.SetStatus (eReturnStatusFailed);
3101 return false;
3102 }
3103 }
3104 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003105 }
3106
Greg Claytoneffe5c92011-05-03 22:09:39 +00003107 OptionGroupOptions m_option_group;
3108 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003109 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003110 OptionGroupUInt64 m_slide_option;
3111};
3112
3113//----------------------------------------------------------------------
3114// List images with associated information
3115//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003116class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003117{
3118public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003119 class CommandOptions : public Options
3120 {
3121 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003122 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003123 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003124 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003125 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003126 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003127 {
3128 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003129
Greg Claytoneffe5c92011-05-03 22:09:39 +00003130 virtual
3131 ~CommandOptions ()
3132 {
3133 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003134
Greg Claytoneffe5c92011-05-03 22:09:39 +00003135 virtual Error
3136 SetOptionValue (uint32_t option_idx, const char *option_arg)
3137 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003138 Error error;
3139
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003140 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003141 if (short_option == 'g')
3142 {
3143 m_use_global_module_list = true;
3144 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003145 else if (short_option == 'a')
3146 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003147 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3148 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003149 }
Greg Clayton65a03992011-08-09 00:01:09 +00003150 else
3151 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003152 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003153 if (option_arg)
3154 width = strtoul (option_arg, NULL, 0);
3155 m_format_array.push_back(std::make_pair(short_option, width));
3156 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003157 return error;
3158 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003159
Greg Claytoneffe5c92011-05-03 22:09:39 +00003160 void
3161 OptionParsingStarting ()
3162 {
3163 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003164 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003165 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003166 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003167
Greg Claytoneffe5c92011-05-03 22:09:39 +00003168 const OptionDefinition*
3169 GetDefinitions ()
3170 {
3171 return g_option_table;
3172 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003173
Greg Claytoneffe5c92011-05-03 22:09:39 +00003174 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003175
Greg Claytoneffe5c92011-05-03 22:09:39 +00003176 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003177
Greg Claytoneffe5c92011-05-03 22:09:39 +00003178 // Instance variables to hold the values for command options.
3179 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3180 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003181 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003182 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003183 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003184
Greg Claytoneffe5c92011-05-03 22:09:39 +00003185 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003186 CommandObjectParsed (interpreter,
3187 "target modules list",
3188 "List current executable and dependent shared library images.",
3189 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003190 m_options (interpreter)
3191 {
3192 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003193
Greg Claytoneffe5c92011-05-03 22:09:39 +00003194 virtual
3195 ~CommandObjectTargetModulesList ()
3196 {
3197 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003198
Greg Claytoneffe5c92011-05-03 22:09:39 +00003199 virtual
3200 Options *
3201 GetOptions ()
3202 {
3203 return &m_options;
3204 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003205
Jim Ingham5a988412012-06-08 21:56:10 +00003206protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003207 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00003208 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003209 CommandReturnObject &result)
3210 {
3211 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003212 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003213 // Define a local module list here to ensure it lives longer than any "locker"
3214 // object which might lock its contents below (through the "module_list_ptr"
3215 // variable).
3216 ModuleList module_list;
Greg Clayton3418c852011-08-10 02:10:13 +00003217 if (target == NULL && use_global_module_list == false)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003218 {
3219 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3220 result.SetStatus (eReturnStatusFailed);
3221 return false;
3222 }
3223 else
3224 {
Greg Clayton3418c852011-08-10 02:10:13 +00003225 if (target)
3226 {
3227 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3228 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3229 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3230 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003231 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003232 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003233
Jim Inghamc10312c2011-10-24 18:36:33 +00003234 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3235 {
3236 if (target)
3237 {
3238 Address module_address;
3239 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3240 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003241 ModuleSP module_sp (module_address.GetModule());
3242 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003243 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003244 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003245 result.SetStatus (eReturnStatusSuccessFinishResult);
3246 }
3247 else
3248 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003249 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003250 result.SetStatus (eReturnStatusFailed);
3251 }
3252 }
3253 else
3254 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003255 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003256 result.SetStatus (eReturnStatusFailed);
3257 }
3258 }
3259 else
3260 {
3261 result.AppendError ("Can only look up modules by address with a valid target.");
3262 result.SetStatus (eReturnStatusFailed);
3263 }
3264 return result.Succeeded();
3265 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003266
Greg Claytonc7bece562013-01-25 18:06:21 +00003267 size_t num_modules = 0;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003268 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3269 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3270 // the global module list directly.
Enrico Granata17598482012-11-08 02:22:02 +00003271 const ModuleList *module_list_ptr = NULL;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003272 const size_t argc = command.GetArgumentCount();
3273 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003274 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003275 if (use_global_module_list)
3276 {
3277 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3278 num_modules = Module::GetNumberAllocatedModules();
3279 }
3280 else
3281 {
3282 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003283 }
Greg Clayton65a03992011-08-09 00:01:09 +00003284 }
3285 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003286 {
3287 for (size_t i=0; i<argc; ++i)
3288 {
3289 // Dump specified images (by basename or fullpath)
3290 const char *arg_cstr = command.GetArgumentAtIndex(i);
3291 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3292 if (num_matches == 0)
3293 {
3294 if (argc == 1)
3295 {
3296 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3297 result.SetStatus (eReturnStatusFailed);
3298 return false;
3299 }
3300 }
3301 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003302
Greg Claytonc4a8a762012-05-15 18:43:44 +00003303 module_list_ptr = &module_list;
3304 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003305
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003306 if (module_list_ptr != NULL)
3307 {
3308 locker.Lock(module_list_ptr->GetMutex());
3309 num_modules = module_list_ptr->GetSize();
3310 }
Greg Clayton65a03992011-08-09 00:01:09 +00003311
Greg Claytoneffe5c92011-05-03 22:09:39 +00003312 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003313 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003314 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3315 {
Greg Clayton3418c852011-08-10 02:10:13 +00003316 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003317 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003318 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003319 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003320 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003321 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003322 }
3323 else
3324 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003325 module = Module::GetAllocatedModuleAtIndex(image_idx);
3326 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003327 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003328
Greg Claytonc7bece562013-01-25 18:06:21 +00003329 const size_t indent = strm.Printf("[%3u] ", image_idx);
3330 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003331
Greg Claytoneffe5c92011-05-03 22:09:39 +00003332 }
3333 result.SetStatus (eReturnStatusSuccessFinishResult);
3334 }
3335 else
3336 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003337 if (argc)
3338 {
3339 if (use_global_module_list)
3340 result.AppendError ("the global module list has no matching modules");
3341 else
3342 result.AppendError ("the target has no matching modules");
3343 }
Greg Clayton3418c852011-08-10 02:10:13 +00003344 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003345 {
3346 if (use_global_module_list)
3347 result.AppendError ("the global module list is empty");
3348 else
3349 result.AppendError ("the target has no associated executable images");
3350 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003351 result.SetStatus (eReturnStatusFailed);
3352 return false;
3353 }
3354 }
3355 return result.Succeeded();
3356 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003357
3358 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003359 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003360 {
3361
Jim Ingham28eb5712012-10-12 17:34:26 +00003362 if (module == NULL)
3363 {
3364 strm.PutCString("Null module");
3365 return;
3366 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003367
Jim Inghamc10312c2011-10-24 18:36:33 +00003368 bool dump_object_name = false;
3369 if (m_options.m_format_array.empty())
3370 {
Greg Claytonc9660542012-02-05 02:38:54 +00003371 m_options.m_format_array.push_back(std::make_pair('u', 0));
3372 m_options.m_format_array.push_back(std::make_pair('h', 0));
3373 m_options.m_format_array.push_back(std::make_pair('f', 0));
3374 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003375 }
Greg Claytonc9660542012-02-05 02:38:54 +00003376 const size_t num_entries = m_options.m_format_array.size();
3377 bool print_space = false;
3378 for (size_t i=0; i<num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003379 {
Greg Claytonc9660542012-02-05 02:38:54 +00003380 if (print_space)
3381 strm.PutChar(' ');
3382 print_space = true;
3383 const char format_char = m_options.m_format_array[i].first;
3384 uint32_t width = m_options.m_format_array[i].second;
3385 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003386 {
Greg Claytonc9660542012-02-05 02:38:54 +00003387 case 'A':
3388 DumpModuleArchitecture (strm, module, false, width);
3389 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003390
Greg Claytonc9660542012-02-05 02:38:54 +00003391 case 't':
3392 DumpModuleArchitecture (strm, module, true, width);
3393 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003394
Greg Claytonc9660542012-02-05 02:38:54 +00003395 case 'f':
3396 DumpFullpath (strm, &module->GetFileSpec(), width);
3397 dump_object_name = true;
3398 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003399
Greg Claytonc9660542012-02-05 02:38:54 +00003400 case 'd':
3401 DumpDirectory (strm, &module->GetFileSpec(), width);
3402 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003403
Greg Claytonc9660542012-02-05 02:38:54 +00003404 case 'b':
3405 DumpBasename (strm, &module->GetFileSpec(), width);
3406 dump_object_name = true;
3407 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003408
Greg Claytonc9660542012-02-05 02:38:54 +00003409 case 'h':
3410 case 'o':
3411 // Image header address
3412 {
3413 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003414
Greg Claytonc9660542012-02-05 02:38:54 +00003415 ObjectFile *objfile = module->GetObjectFile ();
3416 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003417 {
Greg Claytonc9660542012-02-05 02:38:54 +00003418 Address header_addr(objfile->GetHeaderAddress());
3419 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003420 {
Greg Claytonc9660542012-02-05 02:38:54 +00003421 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003422 {
Greg Claytonc9660542012-02-05 02:38:54 +00003423 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3424 if (header_load_addr == LLDB_INVALID_ADDRESS)
3425 {
3426 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3427 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003428 else
Greg Claytonc9660542012-02-05 02:38:54 +00003429 {
3430 if (format_char == 'o')
3431 {
3432 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003433 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003434 }
3435 else
3436 {
3437 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003438 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003439 }
3440 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003441 break;
3442 }
Greg Claytonc9660542012-02-05 02:38:54 +00003443 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3444 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3445 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003446 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003447 }
Greg Claytonc9660542012-02-05 02:38:54 +00003448 strm.Printf ("%*s", addr_nibble_width + 2, "");
3449 }
3450 break;
3451 case 'r':
3452 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003453 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003454 ModuleSP module_sp (module->shared_from_this());
3455 if (module_sp)
3456 {
3457 // Take one away to make sure we don't count our local "module_sp"
3458 ref_count = module_sp.use_count() - 1;
3459 }
3460 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003461 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003462 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003463 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003464 }
3465 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003466
Greg Claytonc9660542012-02-05 02:38:54 +00003467 case 's':
3468 case 'S':
3469 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003470 const SymbolVendor *symbol_vendor = module->GetSymbolVendor();
Greg Claytonc9660542012-02-05 02:38:54 +00003471 if (symbol_vendor)
3472 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003473 const FileSpec symfile_spec = symbol_vendor->GetMainFileSpec();
3474 if (format_char == 'S')
Greg Claytonc9660542012-02-05 02:38:54 +00003475 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003476 // Dump symbol file only if different from module file
3477 if (!symfile_spec || symfile_spec == module->GetFileSpec())
Greg Claytonc9660542012-02-05 02:38:54 +00003478 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003479 print_space = false;
3480 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003481 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003482 // Add a newline and indent past the index
3483 strm.Printf ("\n%*s", indent, "");
Greg Claytonc9660542012-02-05 02:38:54 +00003484 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003485 DumpFullpath (strm, &symfile_spec, width);
3486 dump_object_name = true;
3487 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003488 }
3489 strm.Printf("%.*s", width, "<NONE>");
3490 }
3491 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003492
Greg Claytonc9660542012-02-05 02:38:54 +00003493 case 'm':
3494 module->GetModificationTime().Dump(&strm, width);
3495 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003496
Greg Claytonc9660542012-02-05 02:38:54 +00003497 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003498 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003499 break;
3500
3501 case 'u':
3502 DumpModuleUUID(strm, module);
3503 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003504
Greg Claytonc9660542012-02-05 02:38:54 +00003505 default:
3506 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003507 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003508
Greg Claytonc9660542012-02-05 02:38:54 +00003509 }
3510 if (dump_object_name)
3511 {
3512 const char *object_name = module->GetObjectName().GetCString();
3513 if (object_name)
3514 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003515 }
3516 strm.EOL();
3517 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003518
Greg Claytoneffe5c92011-05-03 22:09:39 +00003519 CommandOptions m_options;
3520};
3521
3522OptionDefinition
3523CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3524{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003525 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3526 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
3527 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
3528 { LLDB_OPT_SET_1, false, "header", 'h', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the image header address as a load address if debugging, a file address otherwise."},
3529 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the image header address offset from the header file address (the slide amount)."},
3530 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3531 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3532 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3533 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3534 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3535 { LLDB_OPT_SET_1, false, "symfile-unique", 'S', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the symbol file with optional width only if it is different from the executable object file."},
3536 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3537 { LLDB_OPT_SET_1, false, "ref-count", 'r', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the reference count if the module is still in the shared module cache."},
3538 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeNone, "Display the module pointer."},
3539 { LLDB_OPT_SET_1, false, "global", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the modules from the global module list, not just the current target."},
3540 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003541};
3542
Jason Molenda380241a2012-07-12 00:20:07 +00003543#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003544
Jason Molenda380241a2012-07-12 00:20:07 +00003545//----------------------------------------------------------------------
3546// Lookup unwind information in images
3547//----------------------------------------------------------------------
3548
3549class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3550{
3551public:
3552
3553 enum
3554 {
3555 eLookupTypeInvalid = -1,
3556 eLookupTypeAddress = 0,
3557 eLookupTypeSymbol,
3558 eLookupTypeFunction,
3559 eLookupTypeFunctionOrSymbol,
3560 kNumLookupTypes
3561 };
3562
3563 class CommandOptions : public Options
3564 {
3565 public:
3566
3567 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003568 Options(interpreter),
3569 m_type(eLookupTypeInvalid),
3570 m_str(),
3571 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003572 {
3573 }
3574
3575 virtual
3576 ~CommandOptions ()
3577 {
3578 }
3579
3580 virtual Error
3581 SetOptionValue (uint32_t option_idx, const char *option_arg)
3582 {
3583 Error error;
3584
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003585 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003586
3587 switch (short_option)
3588 {
3589 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003590 {
3591 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003592 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003593 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003594 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003595 if (m_addr == LLDB_INVALID_ADDRESS)
3596 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3597 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003598 }
Jason Molenda380241a2012-07-12 00:20:07 +00003599
3600 case 'n':
Jason Molenda535ab862013-04-23 04:30:57 +00003601 {
Jason Molenda380241a2012-07-12 00:20:07 +00003602 m_str = option_arg;
3603 m_type = eLookupTypeFunctionOrSymbol;
3604 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003605 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003606
3607 default:
3608 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3609 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003610 }
3611
3612 return error;
3613 }
3614
3615 void
3616 OptionParsingStarting ()
3617 {
3618 m_type = eLookupTypeInvalid;
3619 m_str.clear();
3620 m_addr = LLDB_INVALID_ADDRESS;
3621 }
3622
3623 const OptionDefinition*
3624 GetDefinitions ()
3625 {
3626 return g_option_table;
3627 }
3628
3629 // Options table: Required for subclasses of Options.
3630
3631 static OptionDefinition g_option_table[];
3632
3633 // Instance variables to hold the values for command options.
3634
3635 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3636 std::string m_str; // Holds name lookup
3637 lldb::addr_t m_addr; // Holds the address to lookup
3638 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003639
Jason Molenda380241a2012-07-12 00:20:07 +00003640 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3641 CommandObjectParsed (interpreter,
3642 "target modules show-unwind",
3643 "Show synthesized unwind instructions for a function.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003644 NULL,
3645 eFlagRequiresTarget |
3646 eFlagRequiresProcess |
3647 eFlagProcessMustBeLaunched |
3648 eFlagProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003649 m_options (interpreter)
3650 {
3651 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003652
Jason Molenda380241a2012-07-12 00:20:07 +00003653 virtual
3654 ~CommandObjectTargetModulesShowUnwind ()
3655 {
3656 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003657
Jason Molenda380241a2012-07-12 00:20:07 +00003658 virtual
3659 Options *
3660 GetOptions ()
3661 {
3662 return &m_options;
3663 }
3664
3665protected:
3666 bool
3667 DoExecute (Args& command,
3668 CommandReturnObject &result)
3669 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003670 Target *target = m_exe_ctx.GetTargetPtr();
3671 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda380241a2012-07-12 00:20:07 +00003672 ABI *abi = NULL;
3673 if (process)
3674 abi = process->GetABI().get();
3675
3676 if (process == NULL)
3677 {
3678 result.AppendError ("You must have a process running to use this command.");
3679 result.SetStatus (eReturnStatusFailed);
3680 return false;
3681 }
3682
3683 ThreadList threads(process->GetThreadList());
3684 if (threads.GetSize() == 0)
3685 {
3686 result.AppendError ("The process must be paused to use this command.");
3687 result.SetStatus (eReturnStatusFailed);
3688 return false;
3689 }
3690
3691 ThreadSP thread(threads.GetThreadAtIndex(0));
3692 if (thread.get() == NULL)
3693 {
3694 result.AppendError ("The process must be paused to use this command.");
3695 result.SetStatus (eReturnStatusFailed);
3696 return false;
3697 }
3698
Jason Molenda535ab862013-04-23 04:30:57 +00003699 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003700
Jason Molenda380241a2012-07-12 00:20:07 +00003701 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3702 {
Jason Molenda380241a2012-07-12 00:20:07 +00003703 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003704 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3705 }
3706 else if (m_options.m_type == eLookupTypeAddress && target)
3707 {
3708 Address addr;
3709 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003710 {
3711 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003712 ModuleSP module_sp (addr.GetModule());
3713 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3714 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003715 {
Jason Molenda535ab862013-04-23 04:30:57 +00003716 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003717 }
Jason Molenda535ab862013-04-23 04:30:57 +00003718 }
3719 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003720 else
3721 {
3722 result.AppendError ("address-expression or function name option must be specified.");
3723 result.SetStatus (eReturnStatusFailed);
3724 return false;
3725 }
Jason Molenda380241a2012-07-12 00:20:07 +00003726
Jason Molenda535ab862013-04-23 04:30:57 +00003727 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003728 if (num_matches == 0)
3729 {
3730 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3731 result.SetStatus (eReturnStatusFailed);
3732 return false;
3733 }
3734
Jason Molenda535ab862013-04-23 04:30:57 +00003735 for (uint32_t idx = 0; idx < num_matches; idx++)
3736 {
3737 SymbolContext sc;
3738 sc_list.GetContextAtIndex(idx, sc);
3739 if (sc.symbol == NULL && sc.function == NULL)
3740 continue;
3741 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3742 continue;
3743 AddressRange range;
3744 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3745 continue;
3746 if (!range.GetBaseAddress().IsValid())
3747 continue;
3748 ConstString funcname(sc.GetFunctionName());
3749 if (funcname.IsEmpty())
3750 continue;
3751 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3752 if (abi)
3753 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003754
Jason Molenda535ab862013-04-23 04:30:57 +00003755 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3756 if (func_unwinders_sp.get() == NULL)
3757 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003758
Jason Molenda34549b82015-01-13 06:04:04 +00003759 result.GetOutputStream().Printf("UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda535ab862013-04-23 04:30:57 +00003760
Todd Fiala05625242014-08-25 20:29:09 +00003761 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread.get(), -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003762 if (non_callsite_unwind_plan.get())
3763 {
Jason Molenda34549b82015-01-13 06:04:04 +00003764 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n", non_callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003765 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003766 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003767 if (callsite_unwind_plan.get())
3768 {
Jason Molenda34549b82015-01-13 06:04:04 +00003769 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003770 }
Jason Molenda535ab862013-04-23 04:30:57 +00003771 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3772 if (fast_unwind_plan.get())
3773 {
Jason Molenda34549b82015-01-13 06:04:04 +00003774 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003775 }
3776
Jason Molenda34549b82015-01-13 06:04:04 +00003777 result.GetOutputStream().Printf("\n");
3778
3779 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread.get(), 0);
3780 if (assembly_sp)
3781 {
3782 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3783 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3784 result.GetOutputStream().Printf("\n");
3785 }
3786
3787
3788 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3789 if (ehframe_sp)
3790 {
3791 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3792 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3793 result.GetOutputStream().Printf("\n");
3794 }
3795
3796 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread.get(), 0);
3797 if (ehframe_augmented_sp)
3798 {
3799 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3800 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3801 result.GetOutputStream().Printf("\n");
3802 }
3803
3804 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3805 if (compact_unwind_sp)
3806 {
3807 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3808 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3809 result.GetOutputStream().Printf("\n");
3810 }
3811
3812 if (fast_unwind_plan)
3813 {
3814 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3815 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3816 result.GetOutputStream().Printf("\n");
3817 }
3818
3819 ABISP abi_sp = process->GetABI();
3820 if (abi_sp)
3821 {
3822 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3823 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3824 {
3825 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3826 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3827 result.GetOutputStream().Printf("\n");
3828 }
3829
3830 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3831 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3832 {
3833 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3834 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3835 result.GetOutputStream().Printf("\n");
3836 }
3837 }
Jason Molenda535ab862013-04-23 04:30:57 +00003838
3839 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003840 }
3841 return result.Succeeded();
3842 }
3843
3844 CommandOptions m_options;
3845};
3846
3847OptionDefinition
3848CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3849{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003850 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3851 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3852 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jason Molenda380241a2012-07-12 00:20:07 +00003853};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003854
3855//----------------------------------------------------------------------
3856// Lookup information in images
3857//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003858class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003859{
3860public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003861 enum
3862 {
3863 eLookupTypeInvalid = -1,
3864 eLookupTypeAddress = 0,
3865 eLookupTypeSymbol,
3866 eLookupTypeFileLine, // Line is optional
3867 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003868 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003869 eLookupTypeType,
3870 kNumLookupTypes
3871 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003872
Greg Claytoneffe5c92011-05-03 22:09:39 +00003873 class CommandOptions : public Options
3874 {
3875 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003876 CommandOptions (CommandInterpreter &interpreter) :
3877 Options(interpreter)
3878 {
3879 OptionParsingStarting();
3880 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003881
Greg Claytoneffe5c92011-05-03 22:09:39 +00003882 virtual
3883 ~CommandOptions ()
3884 {
3885 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003886
Greg Claytoneffe5c92011-05-03 22:09:39 +00003887 virtual Error
3888 SetOptionValue (uint32_t option_idx, const char *option_arg)
3889 {
3890 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003891
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003892 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003893
Greg Claytoneffe5c92011-05-03 22:09:39 +00003894 switch (short_option)
3895 {
3896 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003897 {
3898 m_type = eLookupTypeAddress;
3899 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3900 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3901 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003902 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003903
Greg Claytoneffe5c92011-05-03 22:09:39 +00003904 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003905 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003906 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003907 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003908 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003909
Greg Claytoneffe5c92011-05-03 22:09:39 +00003910 case 's':
3911 m_str = option_arg;
3912 m_type = eLookupTypeSymbol;
3913 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003914
Greg Claytoneffe5c92011-05-03 22:09:39 +00003915 case 'f':
3916 m_file.SetFile (option_arg, false);
3917 m_type = eLookupTypeFileLine;
3918 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003919
Greg Claytoneffe5c92011-05-03 22:09:39 +00003920 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003921 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003922 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003923
Greg Claytoneffe5c92011-05-03 22:09:39 +00003924 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003925 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003926 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003927 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003928 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003929 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003930 m_type = eLookupTypeFileLine;
3931 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003932
Greg Claytonc4a8a762012-05-15 18:43:44 +00003933 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003934 m_str = option_arg;
3935 m_type = eLookupTypeFunction;
3936 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003937
Greg Claytonc4a8a762012-05-15 18:43:44 +00003938 case 'n':
3939 m_str = option_arg;
3940 m_type = eLookupTypeFunctionOrSymbol;
3941 break;
3942
Greg Claytoneffe5c92011-05-03 22:09:39 +00003943 case 't':
3944 m_str = option_arg;
3945 m_type = eLookupTypeType;
3946 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003947
Greg Claytoneffe5c92011-05-03 22:09:39 +00003948 case 'v':
3949 m_verbose = 1;
3950 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003951
Sean Callanand38b4a92012-06-06 20:49:55 +00003952 case 'A':
3953 m_print_all = true;
3954 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003955
Greg Claytoneffe5c92011-05-03 22:09:39 +00003956 case 'r':
3957 m_use_regex = true;
3958 break;
3959 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003960
Greg Claytoneffe5c92011-05-03 22:09:39 +00003961 return error;
3962 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003963
Greg Claytoneffe5c92011-05-03 22:09:39 +00003964 void
3965 OptionParsingStarting ()
3966 {
3967 m_type = eLookupTypeInvalid;
3968 m_str.clear();
3969 m_file.Clear();
3970 m_addr = LLDB_INVALID_ADDRESS;
3971 m_offset = 0;
3972 m_line_number = 0;
3973 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003974 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003975 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003976 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003977 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003978
Greg Claytoneffe5c92011-05-03 22:09:39 +00003979 const OptionDefinition*
3980 GetDefinitions ()
3981 {
3982 return g_option_table;
3983 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003984
Greg Claytoneffe5c92011-05-03 22:09:39 +00003985 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003986
Greg Claytoneffe5c92011-05-03 22:09:39 +00003987 static OptionDefinition g_option_table[];
3988 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3989 std::string m_str; // Holds name lookup
3990 FileSpec m_file; // Files for file lookups
3991 lldb::addr_t m_addr; // Holds the address to lookup
3992 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3993 uint32_t m_line_number; // Line number for file+line lookups
3994 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00003995 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003996 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00003997 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003998 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003999
Greg Claytoneffe5c92011-05-03 22:09:39 +00004000 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004001 CommandObjectParsed (interpreter,
4002 "target modules lookup",
4003 "Look up information within executable and dependent shared library images.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00004004 NULL,
4005 eFlagRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00004006 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004007 {
4008 CommandArgumentEntry arg;
4009 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004010
Greg Claytoneffe5c92011-05-03 22:09:39 +00004011 // Define the first (and only) variant of this arg.
4012 file_arg.arg_type = eArgTypeFilename;
4013 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004014
Greg Claytoneffe5c92011-05-03 22:09:39 +00004015 // There is only one variant this argument could be; put it into the argument entry.
4016 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004017
Greg Claytoneffe5c92011-05-03 22:09:39 +00004018 // Push the data for the first argument into the m_arguments vector.
4019 m_arguments.push_back (arg);
4020 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004021
Greg Claytoneffe5c92011-05-03 22:09:39 +00004022 virtual
4023 ~CommandObjectTargetModulesLookup ()
4024 {
4025 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004026
Greg Claytoneffe5c92011-05-03 22:09:39 +00004027 virtual Options *
4028 GetOptions ()
4029 {
4030 return &m_options;
4031 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004032
Sean Callanand38b4a92012-06-06 20:49:55 +00004033 bool
4034 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4035 {
4036 switch (m_options.m_type)
4037 {
4038 case eLookupTypeAddress:
4039 case eLookupTypeFileLine:
4040 case eLookupTypeFunction:
4041 case eLookupTypeFunctionOrSymbol:
4042 case eLookupTypeSymbol:
4043 default:
4044 return false;
4045 case eLookupTypeType:
4046 break;
4047 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004048
Jason Molendab57e4a12013-11-04 09:33:30 +00004049 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004050
Sean Callanand38b4a92012-06-06 20:49:55 +00004051 if (!frame)
4052 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004053
Sean Callanand38b4a92012-06-06 20:49:55 +00004054 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004055
Sean Callanand38b4a92012-06-06 20:49:55 +00004056 if (!sym_ctx.module_sp)
4057 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004058
Sean Callanand38b4a92012-06-06 20:49:55 +00004059 switch (m_options.m_type)
4060 {
4061 default:
4062 return false;
4063 case eLookupTypeType:
4064 if (!m_options.m_str.empty())
4065 {
4066 if (LookupTypeHere (m_interpreter,
4067 result.GetOutputStream(),
4068 sym_ctx,
4069 m_options.m_str.c_str(),
4070 m_options.m_use_regex))
4071 {
4072 result.SetStatus(eReturnStatusSuccessFinishResult);
4073 return true;
4074 }
4075 }
4076 break;
4077 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004078
Sean Callanand38b4a92012-06-06 20:49:55 +00004079 return true;
4080 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004081
Greg Claytoneffe5c92011-05-03 22:09:39 +00004082 bool
4083 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4084 {
4085 switch (m_options.m_type)
4086 {
4087 case eLookupTypeAddress:
4088 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4089 {
4090 if (LookupAddressInModule (m_interpreter,
4091 result.GetOutputStream(),
4092 module,
Greg Clayton2501e5e2015-01-15 02:59:20 +00004093 eSymbolContextEverything | (m_options.m_verbose ? eSymbolContextVariable : 0),
Greg Claytoneffe5c92011-05-03 22:09:39 +00004094 m_options.m_addr,
4095 m_options.m_offset,
4096 m_options.m_verbose))
4097 {
4098 result.SetStatus(eReturnStatusSuccessFinishResult);
4099 return true;
4100 }
4101 }
4102 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004103
Greg Claytoneffe5c92011-05-03 22:09:39 +00004104 case eLookupTypeSymbol:
4105 if (!m_options.m_str.empty())
4106 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004107 if (LookupSymbolInModule (m_interpreter,
4108 result.GetOutputStream(),
4109 module,
4110 m_options.m_str.c_str(),
4111 m_options.m_use_regex,
4112 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004113 {
4114 result.SetStatus(eReturnStatusSuccessFinishResult);
4115 return true;
4116 }
4117 }
4118 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004119
Greg Claytoneffe5c92011-05-03 22:09:39 +00004120 case eLookupTypeFileLine:
4121 if (m_options.m_file)
4122 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004123 if (LookupFileAndLineInModule (m_interpreter,
4124 result.GetOutputStream(),
4125 module,
4126 m_options.m_file,
4127 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004128 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004129 m_options.m_verbose))
4130 {
4131 result.SetStatus(eReturnStatusSuccessFinishResult);
4132 return true;
4133 }
4134 }
4135 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004136
4137 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004138 case eLookupTypeFunction:
4139 if (!m_options.m_str.empty())
4140 {
4141 if (LookupFunctionInModule (m_interpreter,
4142 result.GetOutputStream(),
4143 module,
4144 m_options.m_str.c_str(),
4145 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004146 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004147 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004148 m_options.m_verbose))
4149 {
4150 result.SetStatus(eReturnStatusSuccessFinishResult);
4151 return true;
4152 }
4153 }
4154 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004155
Greg Claytoneffe5c92011-05-03 22:09:39 +00004156 case eLookupTypeType:
4157 if (!m_options.m_str.empty())
4158 {
4159 if (LookupTypeInModule (m_interpreter,
4160 result.GetOutputStream(),
4161 module,
4162 m_options.m_str.c_str(),
4163 m_options.m_use_regex))
4164 {
4165 result.SetStatus(eReturnStatusSuccessFinishResult);
4166 return true;
4167 }
4168 }
4169 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004170
Greg Claytoneffe5c92011-05-03 22:09:39 +00004171 default:
4172 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4173 syntax_error = true;
4174 break;
4175 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004176
Greg Claytoneffe5c92011-05-03 22:09:39 +00004177 result.SetStatus (eReturnStatusFailed);
4178 return false;
4179 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004180
Jim Ingham5a988412012-06-08 21:56:10 +00004181protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004182 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004183 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004184 CommandReturnObject &result)
4185 {
4186 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4187 if (target == NULL)
4188 {
4189 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4190 result.SetStatus (eReturnStatusFailed);
4191 return false;
4192 }
4193 else
4194 {
4195 bool syntax_error = false;
4196 uint32_t i;
4197 uint32_t num_successful_lookups = 0;
4198 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4199 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4200 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4201 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004202
Greg Claytoneffe5c92011-05-03 22:09:39 +00004203 if (command.GetArgumentCount() == 0)
4204 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004205 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004206
Sean Callanand38b4a92012-06-06 20:49:55 +00004207 // Where it is possible to look in the current symbol context
4208 // first, try that. If this search was successful and --all
4209 // was not passed, don't print anything else.
4210 if (LookupHere (m_interpreter, result, syntax_error))
4211 {
4212 result.GetOutputStream().EOL();
4213 num_successful_lookups++;
4214 if (!m_options.m_print_all)
4215 {
4216 result.SetStatus (eReturnStatusSuccessFinishResult);
4217 return result.Succeeded();
4218 }
4219 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004220
Sean Callanand38b4a92012-06-06 20:49:55 +00004221 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004222
Enrico Granata17598482012-11-08 02:22:02 +00004223 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004224 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004225 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004226 if (num_modules > 0)
4227 {
4228 for (i = 0; i<num_modules && syntax_error == false; ++i)
4229 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004230 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004231
Sean Callanand38b4a92012-06-06 20:49:55 +00004232 if (module_pointer != current_module.get() &&
4233 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004234 {
4235 result.GetOutputStream().EOL();
4236 num_successful_lookups++;
4237 }
4238 }
4239 }
4240 else
4241 {
4242 result.AppendError ("the target has no associated executable images");
4243 result.SetStatus (eReturnStatusFailed);
4244 return false;
4245 }
4246 }
4247 else
4248 {
4249 // Dump specified images (by basename or fullpath)
4250 const char *arg_cstr;
4251 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4252 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004253 ModuleList module_list;
4254 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4255 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004256 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004257 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004258 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004259 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004260 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004261 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004262 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004263 {
4264 result.GetOutputStream().EOL();
4265 num_successful_lookups++;
4266 }
4267 }
4268 }
4269 }
4270 else
4271 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4272 }
4273 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004274
Greg Claytoneffe5c92011-05-03 22:09:39 +00004275 if (num_successful_lookups > 0)
4276 result.SetStatus (eReturnStatusSuccessFinishResult);
4277 else
4278 result.SetStatus (eReturnStatusFailed);
4279 }
4280 return result.Succeeded();
4281 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004282
Greg Claytoneffe5c92011-05-03 22:09:39 +00004283 CommandOptions m_options;
4284};
4285
4286OptionDefinition
4287CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4288{
Zachary Turnerd37221d2014-07-09 16:31:49 +00004289 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4290 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
Greg Claytonc4a8a762012-05-15 18:43:44 +00004291 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4292 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Zachary Turnerd37221d2014-07-09 16:31:49 +00004293 false, "regex", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4294 { LLDB_OPT_SET_2, true, "symbol", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules."},
4295 { LLDB_OPT_SET_3, true, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules."},
4296 { LLDB_OPT_SET_3, false, "line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
Jim Inghama8f55662012-06-04 22:47:34 +00004297 { LLDB_OPT_SET_FROM_TO(3,5),
Zachary Turnerd37221d2014-07-09 16:31:49 +00004298 false, "no-inlines", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4299 { LLDB_OPT_SET_4, true, "function", 'F', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
4300 { LLDB_OPT_SET_5, true, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules."},
4301 { LLDB_OPT_SET_6, true, "type", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
4302 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4303 { LLDB_OPT_SET_ALL, false, "all", 'A', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available."},
4304 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004305};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004306
4307
Jim Ingham9575d842011-03-11 03:53:59 +00004308#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004309
4310//-------------------------------------------------------------------------
4311// CommandObjectMultiwordImageSearchPaths
4312//-------------------------------------------------------------------------
4313
Greg Claytoneffe5c92011-05-03 22:09:39 +00004314class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004315{
4316public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004317 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4318 CommandObjectMultiword (interpreter,
4319 "target modules search-paths",
4320 "A set of commands for operating on debugger target image search paths.",
4321 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004322 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004323 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4324 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4325 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4326 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4327 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004328 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004329
Greg Claytoneffe5c92011-05-03 22:09:39 +00004330 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004331 {
4332 }
4333};
4334
Greg Claytoneffe5c92011-05-03 22:09:39 +00004335
4336
4337#pragma mark CommandObjectTargetModules
4338
4339//-------------------------------------------------------------------------
4340// CommandObjectTargetModules
4341//-------------------------------------------------------------------------
4342
4343class CommandObjectTargetModules : public CommandObjectMultiword
4344{
4345public:
4346 //------------------------------------------------------------------
4347 // Constructors and Destructors
4348 //------------------------------------------------------------------
4349 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4350 CommandObjectMultiword (interpreter,
4351 "target modules",
4352 "A set of commands for accessing information for one or more target modules.",
4353 "target modules <sub-command> ...")
4354 {
4355 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4356 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004357 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4358 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4359 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4360 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004361 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004362
4363 }
4364 virtual
4365 ~CommandObjectTargetModules()
4366 {
4367 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004368
Greg Claytoneffe5c92011-05-03 22:09:39 +00004369private:
4370 //------------------------------------------------------------------
4371 // For CommandObjectTargetModules only
4372 //------------------------------------------------------------------
4373 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4374};
4375
4376
Greg Claytone72dfb32012-02-24 01:59:29 +00004377
Jim Ingham5a988412012-06-08 21:56:10 +00004378class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004379{
4380public:
4381 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004382 CommandObjectParsed (interpreter,
4383 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004384 "Add a debug symbol file to one of the target's current modules by specifying a path to a debug symbols file, or using the options to specify a module to download symbols for.",
Jason Molendab9c9f9b2013-02-02 06:00:36 +00004385 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004386 m_option_group (interpreter),
4387 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4388 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4389
Greg Claytone72dfb32012-02-24 01:59:29 +00004390 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004391 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4392 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4393 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4394 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004395 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004396
Greg Claytone72dfb32012-02-24 01:59:29 +00004397 virtual
4398 ~CommandObjectTargetSymbolsAdd ()
4399 {
4400 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004401
Greg Claytonc7bece562013-01-25 18:06:21 +00004402 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00004403 HandleArgumentCompletion (Args &input,
4404 int &cursor_index,
4405 int &cursor_char_position,
4406 OptionElementVector &opt_element_vector,
4407 int match_start_point,
4408 int max_return_elements,
4409 bool &word_complete,
4410 StringList &matches)
4411 {
4412 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4413 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004414
Jim Ingham5a988412012-06-08 21:56:10 +00004415 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4416 CommandCompletions::eDiskFileCompletion,
4417 completion_str.c_str(),
4418 match_start_point,
4419 max_return_elements,
4420 NULL,
4421 word_complete,
4422 matches);
4423 return matches.GetSize();
4424 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004425
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004426 virtual Options *
4427 GetOptions ()
4428 {
4429 return &m_option_group;
4430 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004431
Jim Ingham5a988412012-06-08 21:56:10 +00004432protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004433 bool
4434 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004435 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004436 bool &flush,
4437 CommandReturnObject &result)
4438 {
Greg Clayton89deb062012-12-12 01:15:30 +00004439 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4440 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004441 {
4442 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004443 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004444
Greg Clayton89deb062012-12-12 01:15:30 +00004445 if (!module_spec.GetUUID().IsValid())
4446 {
4447 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4448 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4449 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004450 // We now have a module that represents a symbol file
4451 // that can be used for a module that might exist in the
4452 // current target, so we need to find that module in the
4453 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004454 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004455
Greg Clayton9aae0a12013-05-15 19:52:08 +00004456 size_t num_matches = 0;
4457 // First extract all module specs from the symbol file
4458 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004459 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004460 {
4461 // Now extract the module spec that matches the target architecture
4462 ModuleSpec target_arch_module_spec;
4463 ModuleSpec symfile_module_spec;
4464 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4465 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4466 {
4467 // See if it has a UUID?
4468 if (symfile_module_spec.GetUUID().IsValid())
4469 {
4470 // It has a UUID, look for this UUID in the target modules
4471 ModuleSpec symfile_uuid_module_spec;
4472 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4473 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4474 }
4475 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004476
Greg Clayton9aae0a12013-05-15 19:52:08 +00004477 if (num_matches == 0)
4478 {
4479 // No matches yet, iterate through the module specs to find a UUID value that
4480 // we can match up to an image in our target
4481 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4482 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4483 {
4484 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4485 {
4486 if (symfile_module_spec.GetUUID().IsValid())
4487 {
4488 // It has a UUID, look for this UUID in the target modules
4489 ModuleSpec symfile_uuid_module_spec;
4490 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4491 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004492 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004493 }
4494 }
4495 }
4496 }
4497
4498 // Just try to match up the file by basename if we have no matches at this point
4499 if (num_matches == 0)
4500 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004501
Greg Clayton91c0e742013-01-11 23:44:27 +00004502 while (num_matches == 0)
4503 {
4504 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4505 // Empty string returned, lets bail
4506 if (!filename_no_extension)
4507 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004508
Greg Clayton91c0e742013-01-11 23:44:27 +00004509 // Check if there was no extension to strip and the basename is the same
4510 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4511 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004512
Greg Clayton91c0e742013-01-11 23:44:27 +00004513 // Replace basename with one less extension
4514 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004515
Greg Clayton91c0e742013-01-11 23:44:27 +00004516 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4517 }
4518
Greg Clayton89deb062012-12-12 01:15:30 +00004519 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004520 {
Greg Clayton89deb062012-12-12 01:15:30 +00004521 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4522 }
4523 else if (num_matches == 1)
4524 {
4525 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004526
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004527 // The module has not yet created its symbol vendor, we can just
4528 // give the existing target module the symfile path to use for
4529 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004530 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004531
Greg Clayton136dff82012-12-14 02:15:00 +00004532 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004533 if (symbol_vendor)
4534 {
4535 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004536
Greg Clayton89deb062012-12-12 01:15:30 +00004537 if (symbol_file)
4538 {
4539 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004540
Greg Clayton89deb062012-12-12 01:15:30 +00004541 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4542 {
4543 // Provide feedback that the symfile has been successfully added.
4544 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004545 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004546 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004547 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004548
Greg Clayton89deb062012-12-12 01:15:30 +00004549 // Let clients know something changed in the module
4550 // if it is currently loaded
4551 ModuleList module_list;
4552 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004553 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004554
Greg Clayton91c0e742013-01-11 23:44:27 +00004555 // Make sure we load any scripting resources that may be embedded
4556 // in the debug info files in case the platform supports that.
4557 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004558 StreamString feedback_stream;
4559 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004560 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004561 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4562 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4563 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004564 else if (feedback_stream.GetSize())
4565 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004566
Greg Clayton89deb062012-12-12 01:15:30 +00004567 flush = true;
4568 result.SetStatus (eReturnStatusSuccessFinishResult);
4569 return true;
4570 }
4571 }
4572 }
4573 // Clear the symbol file spec if anything went wrong
4574 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004575 }
4576
4577 if (module_spec.GetUUID().IsValid())
4578 {
4579 StreamString ss_symfile_uuid;
4580 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4581 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4582 symfile_path,
4583 ss_symfile_uuid.GetData(),
4584 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4585 ? "\n please specify the full path to the symbol file"
4586 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004587 }
4588 else
4589 {
Greg Clayton89deb062012-12-12 01:15:30 +00004590 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4591 symfile_path,
4592 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4593 ? "\n please specify the full path to the symbol file"
4594 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004595 }
4596 }
4597 else
4598 {
4599 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004600 }
Greg Clayton89deb062012-12-12 01:15:30 +00004601 result.SetStatus (eReturnStatusFailed);
4602 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004603 }
4604
Greg Claytone72dfb32012-02-24 01:59:29 +00004605 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004606 DoExecute (Args& args,
Greg Claytone72dfb32012-02-24 01:59:29 +00004607 CommandReturnObject &result)
4608 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004609 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004610 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004611 bool flush = false;
4612 ModuleSpec module_spec;
4613 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4614 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4615 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004616
Greg Claytonf9fc6092013-01-09 19:44:40 +00004617 const size_t argc = args.GetArgumentCount();
4618 if (argc == 0)
4619 {
4620 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004621 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004622 bool success = false;
4623 bool error_set = false;
4624 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004625 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004626 Process *process = m_exe_ctx.GetProcessPtr();
4627 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004628 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004629 const StateType process_state = process->GetState();
4630 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004631 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004632 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004633 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004634 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004635 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4636 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004637 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004638 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004639 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004640 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4641 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004642 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004643 module_spec.GetUUID() = frame_module_sp->GetUUID();
4644 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004645 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004646 else
4647 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004648 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004649 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004650 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004651 }
4652 else
4653 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004654 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004655 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004656 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004657 }
4658 else
4659 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004660 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004661 error_set = true;
4662 }
4663 }
4664 else
4665 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004666 result.AppendError ("a process must exist in order to use the --frame option");
4667 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004668 }
4669 }
4670 else
4671 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004672 if (uuid_option_set)
4673 {
4674 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4675 success |= module_spec.GetUUID().IsValid();
4676 }
4677 else if (file_option_set)
4678 {
4679 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4680 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4681 if (module_sp)
4682 {
4683 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4684 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4685 module_spec.GetUUID() = module_sp->GetUUID();
4686 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4687 }
4688 else
4689 {
4690 module_spec.GetArchitecture() = target->GetArchitecture();
4691 }
4692 success |= module_spec.GetFileSpec().Exists();
4693 }
4694 }
4695
4696 if (success)
4697 {
4698 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4699 {
4700 if (module_spec.GetSymbolFileSpec())
4701 success = AddModuleSymbols (target, module_spec, flush, result);
4702 }
4703 }
4704
4705 if (!success && !error_set)
4706 {
4707 StreamString error_strm;
4708 if (uuid_option_set)
4709 {
4710 error_strm.PutCString("unable to find debug symbols for UUID ");
4711 module_spec.GetUUID().Dump (&error_strm);
4712 }
4713 else if (file_option_set)
4714 {
4715 error_strm.PutCString("unable to find debug symbols for the executable file ");
4716 error_strm << module_spec.GetFileSpec();
4717 }
4718 else if (frame_option_set)
4719 {
4720 error_strm.PutCString("unable to find debug symbols for the current frame");
4721 }
4722 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004723 }
4724 }
4725 else
4726 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004727 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4728 }
4729 }
4730 else
4731 {
4732 if (uuid_option_set)
4733 {
4734 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4735 }
4736 else if (file_option_set)
4737 {
4738 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4739 }
4740 else if (frame_option_set)
4741 {
4742 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4743 }
4744 else
4745 {
4746 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004747
Greg Claytonf9fc6092013-01-09 19:44:40 +00004748 for (size_t i=0; i<argc; ++i)
4749 {
4750 const char *symfile_path = args.GetArgumentAtIndex(i);
4751 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004752 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004753 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4754 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004755 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004756 FileSpec symfile_spec;
4757 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4758 module_spec.GetSymbolFileSpec() = symfile_spec;
4759 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004760
Greg Claytonf9fc6092013-01-09 19:44:40 +00004761 ArchSpec arch;
4762 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004763
Greg Claytonf9fc6092013-01-09 19:44:40 +00004764 if (symfile_exists)
4765 {
4766 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004767 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004768 }
4769 else
4770 {
4771 char resolved_symfile_path[PATH_MAX];
4772 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4773 {
4774 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4775 {
4776 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4777 break;
4778 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004779 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004780 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4781 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004782 }
4783 }
4784 }
4785 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004786 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004787
Greg Claytonf9fc6092013-01-09 19:44:40 +00004788 if (flush)
4789 {
4790 Process *process = m_exe_ctx.GetProcessPtr();
4791 if (process)
4792 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004793 }
4794 return result.Succeeded();
4795 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004796
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004797 OptionGroupOptions m_option_group;
4798 OptionGroupUUID m_uuid_option_group;
4799 OptionGroupFile m_file_option;
4800 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004801};
4802
4803
4804#pragma mark CommandObjectTargetSymbols
4805
4806//-------------------------------------------------------------------------
4807// CommandObjectTargetSymbols
4808//-------------------------------------------------------------------------
4809
4810class CommandObjectTargetSymbols : public CommandObjectMultiword
4811{
4812public:
4813 //------------------------------------------------------------------
4814 // Constructors and Destructors
4815 //------------------------------------------------------------------
4816 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4817 CommandObjectMultiword (interpreter,
4818 "target symbols",
4819 "A set of commands for adding and managing debug symbol files.",
4820 "target symbols <sub-command> ...")
4821 {
4822 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004823
Greg Claytone72dfb32012-02-24 01:59:29 +00004824 }
4825 virtual
4826 ~CommandObjectTargetSymbols()
4827 {
4828 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004829
Greg Claytone72dfb32012-02-24 01:59:29 +00004830private:
4831 //------------------------------------------------------------------
4832 // For CommandObjectTargetModules only
4833 //------------------------------------------------------------------
4834 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4835};
4836
4837
Jim Ingham9575d842011-03-11 03:53:59 +00004838#pragma mark CommandObjectTargetStopHookAdd
4839
4840//-------------------------------------------------------------------------
4841// CommandObjectTargetStopHookAdd
4842//-------------------------------------------------------------------------
4843
Greg Clayton44d93782014-01-27 23:43:24 +00004844class CommandObjectTargetStopHookAdd :
4845 public CommandObjectParsed,
4846 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004847{
4848public:
4849
4850 class CommandOptions : public Options
4851 {
4852 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004853 CommandOptions (CommandInterpreter &interpreter) :
4854 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004855 m_line_start(0),
4856 m_line_end (UINT_MAX),
4857 m_func_name_type_mask (eFunctionNameTypeAuto),
4858 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004859 m_thread_specified (false),
4860 m_use_one_liner (false),
4861 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004862 {
4863 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004864
Jim Ingham9575d842011-03-11 03:53:59 +00004865 ~CommandOptions () {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004866
Greg Claytone0d378b2011-03-24 21:19:54 +00004867 const OptionDefinition*
Jim Ingham9575d842011-03-11 03:53:59 +00004868 GetDefinitions ()
4869 {
4870 return g_option_table;
4871 }
4872
4873 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +00004874 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham9575d842011-03-11 03:53:59 +00004875 {
4876 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004877 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004878 bool success;
4879
4880 switch (short_option)
4881 {
4882 case 'c':
4883 m_class_name = option_arg;
4884 m_sym_ctx_specified = true;
4885 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004886
Jim Ingham9575d842011-03-11 03:53:59 +00004887 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004888 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004889 if (!success)
4890 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004891 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004892 break;
4893 }
4894 m_sym_ctx_specified = true;
4895 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004896
Jim Ingham9575d842011-03-11 03:53:59 +00004897 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004898 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004899 if (!success)
4900 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004901 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004902 break;
4903 }
4904 m_sym_ctx_specified = true;
4905 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004906
Sean Callanand4a7c122012-02-11 01:22:21 +00004907 case 'i':
4908 m_no_inlines = true;
4909 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004910
Jim Ingham9575d842011-03-11 03:53:59 +00004911 case 'n':
4912 m_function_name = option_arg;
4913 m_func_name_type_mask |= eFunctionNameTypeAuto;
4914 m_sym_ctx_specified = true;
4915 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004916
Jim Ingham9575d842011-03-11 03:53:59 +00004917 case 'f':
4918 m_file_name = option_arg;
4919 m_sym_ctx_specified = true;
4920 break;
4921 case 's':
4922 m_module_name = option_arg;
4923 m_sym_ctx_specified = true;
4924 break;
4925 case 't' :
4926 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004927 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004928 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004929 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004930 m_thread_specified = true;
4931 }
4932 break;
4933 case 'T':
4934 m_thread_name = option_arg;
4935 m_thread_specified = true;
4936 break;
4937 case 'q':
4938 m_queue_name = option_arg;
4939 m_thread_specified = true;
4940 break;
4941 case 'x':
4942 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004943 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004944 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004945 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004946 m_thread_specified = true;
4947 }
4948 break;
Johnny Chenb1372c02011-05-02 23:47:55 +00004949 case 'o':
4950 m_use_one_liner = true;
4951 m_one_liner = option_arg;
4952 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004953 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004954 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Ingham9575d842011-03-11 03:53:59 +00004955 break;
4956 }
4957 return error;
4958 }
4959
4960 void
Greg Claytonf6b8b582011-04-13 00:18:08 +00004961 OptionParsingStarting ()
Jim Ingham9575d842011-03-11 03:53:59 +00004962 {
4963 m_class_name.clear();
4964 m_function_name.clear();
4965 m_line_start = 0;
4966 m_line_end = UINT_MAX;
4967 m_file_name.clear();
4968 m_module_name.clear();
4969 m_func_name_type_mask = eFunctionNameTypeAuto;
4970 m_thread_id = LLDB_INVALID_THREAD_ID;
4971 m_thread_index = UINT32_MAX;
4972 m_thread_name.clear();
4973 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004974
Sean Callanand4a7c122012-02-11 01:22:21 +00004975 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004976 m_sym_ctx_specified = false;
4977 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004978
4979 m_use_one_liner = false;
4980 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004981 }
4982
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004983
Greg Claytone0d378b2011-03-24 21:19:54 +00004984 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004985
Jim Ingham9575d842011-03-11 03:53:59 +00004986 std::string m_class_name;
4987 std::string m_function_name;
4988 uint32_t m_line_start;
4989 uint32_t m_line_end;
4990 std::string m_file_name;
4991 std::string m_module_name;
4992 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4993 lldb::tid_t m_thread_id;
4994 uint32_t m_thread_index;
4995 std::string m_thread_name;
4996 std::string m_queue_name;
4997 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004998 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004999 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00005000 // Instance variables to hold the values for one_liner options.
5001 bool m_use_one_liner;
5002 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00005003 };
5004
5005 Options *
5006 GetOptions ()
5007 {
5008 return &m_options;
5009 }
5010
5011 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005012 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00005013 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005014 "Add a hook to be executed when the target stops.",
5015 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005016 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005017 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005018 {
5019 }
5020
5021 ~CommandObjectTargetStopHookAdd ()
5022 {
5023 }
5024
Jim Ingham5a988412012-06-08 21:56:10 +00005025protected:
Greg Clayton44d93782014-01-27 23:43:24 +00005026 virtual void
5027 IOHandlerActivated (IOHandler &io_handler)
5028 {
5029 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5030 if (output_sp)
5031 {
5032 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5033 output_sp->Flush();
5034 }
5035 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005036
Greg Clayton44d93782014-01-27 23:43:24 +00005037 virtual void
5038 IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
5039 {
5040 if (m_stop_hook_sp)
5041 {
5042 if (line.empty())
5043 {
5044 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5045 if (error_sp)
5046 {
5047 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5048 error_sp->Flush();
5049 }
5050 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5051 if (target)
5052 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5053 }
5054 else
5055 {
5056 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5057 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5058 if (output_sp)
5059 {
5060 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5061 output_sp->Flush();
5062 }
5063 }
5064 m_stop_hook_sp.reset();
5065 }
5066 io_handler.SetIsDone(true);
5067 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005068
Jim Ingham9575d842011-03-11 03:53:59 +00005069 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005070 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005071 {
Greg Clayton44d93782014-01-27 23:43:24 +00005072 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005073
Jim Ingham893c9322014-11-22 01:42:44 +00005074 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005075 if (target)
5076 {
Greg Clayton44d93782014-01-27 23:43:24 +00005077 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005078
5079 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005080 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005081 if (m_options.m_sym_ctx_specified)
5082 {
5083 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005084
Jim Ingham9575d842011-03-11 03:53:59 +00005085 if (!m_options.m_module_name.empty())
5086 {
5087 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5088 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005089
Jim Ingham9575d842011-03-11 03:53:59 +00005090 if (!m_options.m_class_name.empty())
5091 {
5092 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5093 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005094
Jim Ingham9575d842011-03-11 03:53:59 +00005095 if (!m_options.m_file_name.empty())
5096 {
5097 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5098 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005099
Jim Ingham9575d842011-03-11 03:53:59 +00005100 if (m_options.m_line_start != 0)
5101 {
5102 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5103 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005104
Jim Ingham9575d842011-03-11 03:53:59 +00005105 if (m_options.m_line_end != UINT_MAX)
5106 {
5107 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5108 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005109
Jim Ingham9575d842011-03-11 03:53:59 +00005110 if (!m_options.m_function_name.empty())
5111 {
5112 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5113 }
5114 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005115
Jim Ingham9575d842011-03-11 03:53:59 +00005116 if (specifier_ap.get())
5117 new_hook_sp->SetSpecifier (specifier_ap.release());
5118
5119 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005120
Jim Ingham9575d842011-03-11 03:53:59 +00005121 if (m_options.m_thread_specified)
5122 {
5123 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005124
Jim Ingham9575d842011-03-11 03:53:59 +00005125 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5126 {
5127 thread_spec->SetTID (m_options.m_thread_id);
5128 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005129
Jim Ingham9575d842011-03-11 03:53:59 +00005130 if (m_options.m_thread_index != UINT32_MAX)
5131 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005132
Jim Ingham9575d842011-03-11 03:53:59 +00005133 if (!m_options.m_thread_name.empty())
5134 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005135
Jim Ingham9575d842011-03-11 03:53:59 +00005136 if (!m_options.m_queue_name.empty())
5137 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005138
Jim Ingham9575d842011-03-11 03:53:59 +00005139 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005140
Jim Ingham9575d842011-03-11 03:53:59 +00005141 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005142 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005143 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005144 // Use one-liner.
5145 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005146 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005147 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005148 else
Jim Ingham9575d842011-03-11 03:53:59 +00005149 {
Greg Clayton44d93782014-01-27 23:43:24 +00005150 m_stop_hook_sp = new_hook_sp;
5151 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
5152 *this, // IOHandlerDelegate
5153 true, // Run IOHandler in async mode
5154 NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
5155
Jim Ingham9575d842011-03-11 03:53:59 +00005156 }
Jim Ingham9575d842011-03-11 03:53:59 +00005157 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5158 }
5159 else
5160 {
Greg Clayton7260f622011-04-18 08:33:37 +00005161 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005162 result.SetStatus (eReturnStatusFailed);
5163 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005164
Jim Ingham9575d842011-03-11 03:53:59 +00005165 return result.Succeeded();
5166 }
5167private:
5168 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005169 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005170};
5171
Greg Claytone0d378b2011-03-24 21:19:54 +00005172OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005173CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5174{
Zachary Turnerd37221d2014-07-09 16:31:49 +00005175 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005176 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005177 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005178 "Set the module within which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005179 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005180 "The stop hook is run only for the thread whose index matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005181 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005182 "The stop hook is run only for the thread whose TID matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005183 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005184 "The stop hook is run only for the thread whose thread name matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005185 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005186 "The stop hook is run only for threads in the queue whose name is given by this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005187 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005188 "Specify the source file within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005189 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005190 "Set the start of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005191 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005192 "Set the end of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005193 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005194 "Specify the class within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005195 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005196 "Set the function name within which the stop hook will be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005197 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jim Ingham9575d842011-03-11 03:53:59 +00005198};
5199
5200#pragma mark CommandObjectTargetStopHookDelete
5201
5202//-------------------------------------------------------------------------
5203// CommandObjectTargetStopHookDelete
5204//-------------------------------------------------------------------------
5205
Jim Ingham5a988412012-06-08 21:56:10 +00005206class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005207{
5208public:
5209
5210 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005211 CommandObjectParsed (interpreter,
5212 "target stop-hook delete",
5213 "Delete a stop-hook.",
5214 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005215 {
5216 }
5217
5218 ~CommandObjectTargetStopHookDelete ()
5219 {
5220 }
5221
Jim Ingham5a988412012-06-08 21:56:10 +00005222protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005223 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005224 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005225 {
Jim Ingham893c9322014-11-22 01:42:44 +00005226 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005227 if (target)
5228 {
5229 // FIXME: see if we can use the breakpoint id style parser?
5230 size_t num_args = command.GetArgumentCount();
5231 if (num_args == 0)
5232 {
5233 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5234 {
5235 result.SetStatus (eReturnStatusFailed);
5236 return false;
5237 }
5238 else
5239 {
5240 target->RemoveAllStopHooks();
5241 }
5242 }
5243 else
5244 {
5245 bool success;
5246 for (size_t i = 0; i < num_args; i++)
5247 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005248 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005249 if (!success)
5250 {
Greg Clayton7260f622011-04-18 08:33:37 +00005251 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005252 result.SetStatus(eReturnStatusFailed);
5253 return false;
5254 }
5255 success = target->RemoveStopHookByID (user_id);
5256 if (!success)
5257 {
Greg Clayton7260f622011-04-18 08:33:37 +00005258 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005259 result.SetStatus(eReturnStatusFailed);
5260 return false;
5261 }
5262 }
5263 }
5264 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5265 }
5266 else
5267 {
Greg Clayton7260f622011-04-18 08:33:37 +00005268 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005269 result.SetStatus (eReturnStatusFailed);
5270 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005271
Jim Ingham9575d842011-03-11 03:53:59 +00005272 return result.Succeeded();
5273 }
5274};
5275#pragma mark CommandObjectTargetStopHookEnableDisable
5276
5277//-------------------------------------------------------------------------
5278// CommandObjectTargetStopHookEnableDisable
5279//-------------------------------------------------------------------------
5280
Jim Ingham5a988412012-06-08 21:56:10 +00005281class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005282{
5283public:
5284
5285 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005286 CommandObjectParsed (interpreter,
5287 name,
5288 help,
5289 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005290 m_enable (enable)
5291 {
5292 }
5293
5294 ~CommandObjectTargetStopHookEnableDisable ()
5295 {
5296 }
5297
Jim Ingham5a988412012-06-08 21:56:10 +00005298protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005299 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005300 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005301 {
Jim Ingham893c9322014-11-22 01:42:44 +00005302 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005303 if (target)
5304 {
5305 // FIXME: see if we can use the breakpoint id style parser?
5306 size_t num_args = command.GetArgumentCount();
5307 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005308
Jim Ingham9575d842011-03-11 03:53:59 +00005309 if (num_args == 0)
5310 {
5311 target->SetAllStopHooksActiveState (m_enable);
5312 }
5313 else
5314 {
5315 for (size_t i = 0; i < num_args; i++)
5316 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005317 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005318 if (!success)
5319 {
Greg Clayton7260f622011-04-18 08:33:37 +00005320 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005321 result.SetStatus(eReturnStatusFailed);
5322 return false;
5323 }
5324 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5325 if (!success)
5326 {
Greg Clayton7260f622011-04-18 08:33:37 +00005327 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005328 result.SetStatus(eReturnStatusFailed);
5329 return false;
5330 }
5331 }
5332 }
5333 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5334 }
5335 else
5336 {
Greg Clayton7260f622011-04-18 08:33:37 +00005337 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005338 result.SetStatus (eReturnStatusFailed);
5339 }
5340 return result.Succeeded();
5341 }
5342private:
5343 bool m_enable;
5344};
5345
5346#pragma mark CommandObjectTargetStopHookList
5347
5348//-------------------------------------------------------------------------
5349// CommandObjectTargetStopHookList
5350//-------------------------------------------------------------------------
5351
Jim Ingham5a988412012-06-08 21:56:10 +00005352class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005353{
5354public:
5355
5356 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005357 CommandObjectParsed (interpreter,
5358 "target stop-hook list",
5359 "List all stop-hooks.",
5360 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005361 {
5362 }
5363
5364 ~CommandObjectTargetStopHookList ()
5365 {
5366 }
5367
Jim Ingham5a988412012-06-08 21:56:10 +00005368protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005369 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005370 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005371 {
Jim Ingham893c9322014-11-22 01:42:44 +00005372 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005373 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005374 {
Greg Clayton7260f622011-04-18 08:33:37 +00005375 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005376 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005377 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005378 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005379
Jim Ingham9575d842011-03-11 03:53:59 +00005380 size_t num_hooks = target->GetNumStopHooks ();
5381 if (num_hooks == 0)
5382 {
5383 result.GetOutputStream().PutCString ("No stop hooks.\n");
5384 }
5385 else
5386 {
5387 for (size_t i = 0; i < num_hooks; i++)
5388 {
5389 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5390 if (i > 0)
5391 result.GetOutputStream().PutCString ("\n");
5392 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5393 }
5394 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005395 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005396 return result.Succeeded();
5397 }
5398};
5399
5400#pragma mark CommandObjectMultiwordTargetStopHooks
5401//-------------------------------------------------------------------------
5402// CommandObjectMultiwordTargetStopHooks
5403//-------------------------------------------------------------------------
5404
5405class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5406{
5407public:
5408
5409 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5410 CommandObjectMultiword (interpreter,
5411 "target stop-hook",
5412 "A set of commands for operating on debugger target stop-hooks.",
5413 "target stop-hook <subcommand> [<subcommand-options>]")
5414 {
5415 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5416 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5417 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5418 false,
5419 "target stop-hook disable [<id>]",
5420 "Disable a stop-hook.",
5421 "target stop-hook disable")));
5422 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5423 true,
5424 "target stop-hook enable [<id>]",
5425 "Enable a stop-hook.",
5426 "target stop-hook enable")));
5427 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5428 }
5429
5430 ~CommandObjectMultiwordTargetStopHooks()
5431 {
5432 }
5433};
5434
5435
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005436
5437#pragma mark CommandObjectMultiwordTarget
5438
5439//-------------------------------------------------------------------------
5440// CommandObjectMultiwordTarget
5441//-------------------------------------------------------------------------
5442
Greg Clayton66111032010-06-23 01:19:29 +00005443CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005444 CommandObjectMultiword (interpreter,
5445 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005446 "A set of commands for operating on debugger targets.",
5447 "target <subcommand> [<subcommand-options>]")
5448{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005449
Greg Clayton7260f622011-04-18 08:33:37 +00005450 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005451 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005452 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5453 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005454 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005455 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005456 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005457 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005458}
5459
5460CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5461{
5462}
5463
Greg Clayton7260f622011-04-18 08:33:37 +00005464