blob: a03672eeb292faaef1be765efdd50be7faea03c8 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectTarget.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectTarget.h"
11
12// C Includes
13#include <errno.h>
Greg Clayton81040f42011-02-01 01:13:32 +000014
Chris Lattner24943d22010-06-08 16:52:24 +000015// C++ Includes
16// Other libraries and framework includes
17// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Debugger.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000020#include "lldb/Core/InputReader.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000021#include "lldb/Core/Section.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000022#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/Timer.h"
Greg Clayton801417e2011-07-07 01:59:51 +000024#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Interpreter/CommandInterpreter.h"
26#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000027#include "lldb/Interpreter/Options.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000028#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Clayton5beb99d2011-08-11 02:48:45 +000029#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000030#include "lldb/Interpreter/OptionGroupFile.h"
Greg Claytona42880a2011-10-25 06:44:01 +000031#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton368f8222011-07-07 04:38:25 +000032#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000033#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000034#include "lldb/Interpreter/OptionGroupUInt64.h"
35#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton801417e2011-07-07 01:59:51 +000036#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000037#include "lldb/Symbol/LineTable.h"
38#include "lldb/Symbol/ObjectFile.h"
39#include "lldb/Symbol/SymbolFile.h"
40#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton801417e2011-07-07 01:59:51 +000041#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042#include "lldb/Target/Process.h"
43#include "lldb/Target/StackFrame.h"
44#include "lldb/Target/Thread.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000045#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Greg Claytonabe0fed2011-04-18 08:33:37 +000050
51
52static void
53DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
54{
Greg Clayton52c8b6e2011-04-19 04:19:37 +000055 const ArchSpec &target_arch = target->GetArchitecture();
Greg Claytonabe0fed2011-04-18 08:33:37 +000056
Greg Clayton5beb99d2011-08-11 02:48:45 +000057 Module *exe_module = target->GetExecutableModulePointer();
Greg Claytonabe0fed2011-04-18 08:33:37 +000058 char exe_path[PATH_MAX];
59 bool exe_valid = false;
Greg Clayton5beb99d2011-08-11 02:48:45 +000060 if (exe_module)
61 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Greg Claytonabe0fed2011-04-18 08:33:37 +000062
63 if (!exe_valid)
64 ::strcpy (exe_path, "<none>");
65
66 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
67
68 uint32_t properties = 0;
69 if (target_arch.IsValid())
70 {
71 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
72 properties++;
73 }
74 PlatformSP platform_sp (target->GetPlatform());
75 if (platform_sp)
76 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName());
77
78 ProcessSP process_sp (target->GetProcessSP());
79 bool show_process_status = false;
80 if (process_sp)
81 {
82 lldb::pid_t pid = process_sp->GetID();
83 StateType state = process_sp->GetState();
84 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000085 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000086 const char *state_cstr = StateAsCString (state);
87 if (pid != LLDB_INVALID_PROCESS_ID)
Greg Claytond9919d32011-12-01 23:28:38 +000088 strm.Printf ("%spid=%llu", properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000089 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
90 }
91 if (properties > 0)
92 strm.PutCString (" )\n");
93 else
94 strm.EOL();
95 if (show_process_status)
96 {
97 const bool only_threads_with_stop_reason = true;
98 const uint32_t start_frame = 0;
99 const uint32_t num_frames = 1;
100 const uint32_t num_frames_with_source = 1;
101 process_sp->GetStatus (strm);
102 process_sp->GetThreadStatus (strm,
103 only_threads_with_stop_reason,
104 start_frame,
105 num_frames,
106 num_frames_with_source);
107
108 }
109}
110
111static uint32_t
112DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
113{
114 const uint32_t num_targets = target_list.GetNumTargets();
115 if (num_targets)
116 {
117 TargetSP selected_target_sp (target_list.GetSelectedTarget());
118 strm.PutCString ("Current targets:\n");
119 for (uint32_t i=0; i<num_targets; ++i)
120 {
121 TargetSP target_sp (target_list.GetTargetAtIndex (i));
122 if (target_sp)
123 {
124 bool is_selected = target_sp.get() == selected_target_sp.get();
125 DumpTargetInfo (i,
126 target_sp.get(),
127 is_selected ? "* " : " ",
128 show_stopped_process_status,
129 strm);
130 }
131 }
132 }
133 return num_targets;
134}
135#pragma mark CommandObjectTargetCreate
136
137//-------------------------------------------------------------------------
138// "target create"
139//-------------------------------------------------------------------------
140
141class CommandObjectTargetCreate : public CommandObject
142{
143public:
144 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
145 CommandObject (interpreter,
146 "target create",
147 "Create a target using the argument as the main executable.",
148 NULL),
149 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000150 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000151 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
152 m_core_file (LLDB_OPT_SET_1, false, "core-file", 'c', 0, eArgTypePath, "Fullpath to a core file to use for this target.")
Greg Claytonabe0fed2011-04-18 08:33:37 +0000153 {
154 CommandArgumentEntry arg;
155 CommandArgumentData file_arg;
156
157 // Define the first (and only) variant of this arg.
158 file_arg.arg_type = eArgTypeFilename;
159 file_arg.arg_repetition = eArgRepeatPlain;
160
161 // There is only one variant this argument could be; put it into the argument entry.
162 arg.push_back (file_arg);
163
164 // Push the data for the first argument into the m_arguments vector.
165 m_arguments.push_back (arg);
166
Greg Clayton801417e2011-07-07 01:59:51 +0000167 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000168 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000169 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000170 m_option_group.Finalize();
171 }
172
173 ~CommandObjectTargetCreate ()
174 {
175 }
176
177 Options *
178 GetOptions ()
179 {
180 return &m_option_group;
181 }
182
183 bool
184 Execute (Args& command, CommandReturnObject &result)
185 {
186 const int argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000187 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
188
189 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000190 {
191 const char *file_path = command.GetArgumentAtIndex(0);
192 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000193 FileSpec file_spec;
194
195 if (file_path)
196 file_spec.SetFile (file_path, true);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000197
Greg Claytonabe0fed2011-04-18 08:33:37 +0000198 TargetSP target_sp;
199 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000200 const char *arch_cstr = m_arch_option.GetArchitectureName();
201 const bool get_dependent_files = true;
202 Error error (debugger.GetTargetList().CreateTarget (debugger,
203 file_spec,
204 arch_cstr,
205 get_dependent_files,
206 &m_platform_options,
207 target_sp));
208
Greg Claytonabe0fed2011-04-18 08:33:37 +0000209 if (target_sp)
210 {
211 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000212 if (core_file)
213 {
214 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
215 char core_path[PATH_MAX];
216 core_file.GetPath(core_path, sizeof(core_path));
217
218 if (process_sp)
219 {
220 // Seems wierd that we Launch a core file, but that is
221 // what we do!
222 error = process_sp->LoadCore();
223
224 if (error.Fail())
225 {
226 result.AppendError(error.AsCString("can't find plug-in for core file"));
227 result.SetStatus (eReturnStatusFailed);
228 return false;
229 }
230 else
231 {
232 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
233 result.SetStatus (eReturnStatusSuccessFinishNoResult);
234 }
235 }
236 else
237 {
238 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
239 result.SetStatus (eReturnStatusFailed);
240 }
241 }
242 else
243 {
244 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
245 result.SetStatus (eReturnStatusSuccessFinishNoResult);
246 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000247 }
248 else
249 {
250 result.AppendError(error.AsCString());
251 result.SetStatus (eReturnStatusFailed);
252 }
253 }
254 else
255 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000256 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core-file option.\n", m_cmd_name.c_str());
Greg Claytonabe0fed2011-04-18 08:33:37 +0000257 result.SetStatus (eReturnStatusFailed);
258 }
259 return result.Succeeded();
260
261 }
262
263 int
264 HandleArgumentCompletion (Args &input,
265 int &cursor_index,
266 int &cursor_char_position,
267 OptionElementVector &opt_element_vector,
268 int match_start_point,
269 int max_return_elements,
270 bool &word_complete,
271 StringList &matches)
272 {
273 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
274 completion_str.erase (cursor_char_position);
275
276 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
277 CommandCompletions::eDiskFileCompletion,
278 completion_str.c_str(),
279 match_start_point,
280 max_return_elements,
281 NULL,
282 word_complete,
283 matches);
284 return matches.GetSize();
285 }
286private:
287 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000288 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000289 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000290 OptionGroupFile m_core_file;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000291
292};
293
294#pragma mark CommandObjectTargetList
295
296//----------------------------------------------------------------------
297// "target list"
298//----------------------------------------------------------------------
299
300class CommandObjectTargetList : public CommandObject
301{
302public:
303 CommandObjectTargetList (CommandInterpreter &interpreter) :
304 CommandObject (interpreter,
305 "target list",
306 "List all current targets in the current debug session.",
307 NULL,
308 0)
309 {
310 }
311
312 virtual
313 ~CommandObjectTargetList ()
314 {
315 }
316
317 virtual bool
318 Execute (Args& args, CommandReturnObject &result)
319 {
320 if (args.GetArgumentCount() == 0)
321 {
322 Stream &strm = result.GetOutputStream();
323
324 bool show_stopped_process_status = false;
325 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
326 {
327 strm.PutCString ("No targets.\n");
328 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000329 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000330 }
331 else
332 {
333 result.AppendError ("the 'target list' command takes no arguments\n");
334 result.SetStatus (eReturnStatusFailed);
335 }
336 return result.Succeeded();
337 }
338};
339
340
341#pragma mark CommandObjectTargetSelect
342
343//----------------------------------------------------------------------
344// "target select"
345//----------------------------------------------------------------------
346
347class CommandObjectTargetSelect : public CommandObject
348{
349public:
350 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
351 CommandObject (interpreter,
352 "target select",
353 "Select a target as the current target by target index.",
354 NULL,
355 0)
356 {
357 }
358
359 virtual
360 ~CommandObjectTargetSelect ()
361 {
362 }
363
364 virtual bool
365 Execute (Args& args, CommandReturnObject &result)
366 {
367 if (args.GetArgumentCount() == 1)
368 {
369 bool success = false;
370 const char *target_idx_arg = args.GetArgumentAtIndex(0);
371 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
372 if (success)
373 {
374 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
375 const uint32_t num_targets = target_list.GetNumTargets();
376 if (target_idx < num_targets)
377 {
378 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
379 if (target_sp)
380 {
381 Stream &strm = result.GetOutputStream();
382 target_list.SetSelectedTarget (target_sp.get());
383 bool show_stopped_process_status = false;
384 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000385 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000386 }
387 else
388 {
389 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
390 result.SetStatus (eReturnStatusFailed);
391 }
392 }
393 else
394 {
395 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
396 target_idx,
397 num_targets - 1);
398 result.SetStatus (eReturnStatusFailed);
399 }
400 }
401 else
402 {
403 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
404 result.SetStatus (eReturnStatusFailed);
405 }
406 }
407 else
408 {
409 result.AppendError ("'target select' takes a single argument: a target index\n");
410 result.SetStatus (eReturnStatusFailed);
411 }
412 return result.Succeeded();
413 }
414};
415
Greg Clayton153ccd72011-08-10 02:10:13 +0000416#pragma mark CommandObjectTargetSelect
417
418//----------------------------------------------------------------------
419// "target delete"
420//----------------------------------------------------------------------
421
422class CommandObjectTargetDelete : public CommandObject
423{
424public:
425 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Greg Clayton5beb99d2011-08-11 02:48:45 +0000426 CommandObject (interpreter,
427 "target delete",
428 "Delete one or more targets by target index.",
429 NULL,
430 0),
431 m_option_group (interpreter),
432 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', 0, eArgTypeNone, "Perform extra cleanup to minimize memory consumption after deleting the target.", false)
Greg Clayton153ccd72011-08-10 02:10:13 +0000433 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000434 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
435 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000436 }
437
438 virtual
439 ~CommandObjectTargetDelete ()
440 {
441 }
442
443 virtual bool
444 Execute (Args& args, CommandReturnObject &result)
445 {
446 const size_t argc = args.GetArgumentCount();
447 std::vector<TargetSP> delete_target_list;
448 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
449 bool success = true;
450 TargetSP target_sp;
451 if (argc > 0)
452 {
453 const uint32_t num_targets = target_list.GetNumTargets();
454 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
455 {
456 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
457 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
458 if (success)
459 {
460 if (target_idx < num_targets)
461 {
462 target_sp = target_list.GetTargetAtIndex (target_idx);
463 if (target_sp)
464 {
465 delete_target_list.push_back (target_sp);
466 continue;
467 }
468 }
469 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
470 target_idx,
471 num_targets - 1);
472 result.SetStatus (eReturnStatusFailed);
473 success = false;
474 }
475 else
476 {
477 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
478 result.SetStatus (eReturnStatusFailed);
479 success = false;
480 }
481 }
482
483 }
484 else
485 {
486 target_sp = target_list.GetSelectedTarget();
487 if (target_sp)
488 {
489 delete_target_list.push_back (target_sp);
490 }
491 else
492 {
493 result.AppendErrorWithFormat("no target is currently selected\n");
494 result.SetStatus (eReturnStatusFailed);
495 success = false;
496 }
497 }
498 if (success)
499 {
500 const size_t num_targets_to_delete = delete_target_list.size();
501 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
502 {
503 target_sp = delete_target_list[idx];
504 target_list.DeleteTarget(target_sp);
505 target_sp->Destroy();
506 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000507 // If "--clean" was specified, prune any orphaned shared modules from
508 // the global shared module list
509 if (m_cleanup_option.GetOptionValue ())
510 {
511 ModuleList::RemoveOrphanSharedModules();
512 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000513 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
514 result.SetStatus(eReturnStatusSuccessFinishResult);
515 }
516
517 return result.Succeeded();
518 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000519
520 Options *
521 GetOptions ()
522 {
523 return &m_option_group;
524 }
525
526protected:
527 OptionGroupOptions m_option_group;
528 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000529};
530
Greg Claytonabe0fed2011-04-18 08:33:37 +0000531
Greg Clayton801417e2011-07-07 01:59:51 +0000532#pragma mark CommandObjectTargetVariable
533
534//----------------------------------------------------------------------
535// "target variable"
536//----------------------------------------------------------------------
537
538class CommandObjectTargetVariable : public CommandObject
539{
540public:
541 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
542 CommandObject (interpreter,
Johnny Chen34bfa4f2011-07-12 22:34:30 +0000543 "target variable",
544 "Read global variable(s) prior to running your binary.",
Greg Clayton801417e2011-07-07 01:59:51 +0000545 NULL,
546 0),
547 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000548 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000549 m_option_format (eFormatDefault),
Greg Clayton801417e2011-07-07 01:59:51 +0000550 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
551 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'s', 0, eArgTypePath, "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
552 m_varobj_options()
553 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000554 CommandArgumentEntry arg;
555 CommandArgumentData var_name_arg;
556
557 // Define the first (and only) variant of this arg.
558 var_name_arg.arg_type = eArgTypeVarName;
559 var_name_arg.arg_repetition = eArgRepeatPlus;
560
561 // There is only one variant this argument could be; put it into the argument entry.
562 arg.push_back (var_name_arg);
563
564 // Push the data for the first argument into the m_arguments vector.
565 m_arguments.push_back (arg);
566
Greg Clayton801417e2011-07-07 01:59:51 +0000567 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000568 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000569 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton801417e2011-07-07 01:59:51 +0000570 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
571 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
572 m_option_group.Finalize();
573 }
574
575 virtual
576 ~CommandObjectTargetVariable ()
577 {
578 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000579
580 void
581 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
582 {
Enrico Granata19030d82011-08-15 18:01:31 +0000583 ValueObject::DumpValueObjectOptions options;
584
585 options.SetPointerDepth(m_varobj_options.ptr_depth)
586 .SetMaximumDepth(m_varobj_options.max_depth)
587 .SetShowTypes(m_varobj_options.show_types)
588 .SetShowLocation(m_varobj_options.show_location)
589 .SetUseObjectiveC(m_varobj_options.use_objc)
590 .SetUseDynamicType(m_varobj_options.use_dynamic)
591 .SetUseSyntheticValue((lldb::SyntheticValueType)m_varobj_options.use_synth)
592 .SetFlatOutput(m_varobj_options.flat_output)
593 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
594 .SetIgnoreCap(m_varobj_options.ignore_cap);
595
Greg Clayton5d81f492011-07-08 21:46:14 +0000596 switch (var_sp->GetScope())
597 {
598 case eValueTypeVariableGlobal:
599 if (m_option_variable.show_scope)
600 s.PutCString("GLOBAL: ");
601 break;
602
603 case eValueTypeVariableStatic:
604 if (m_option_variable.show_scope)
605 s.PutCString("STATIC: ");
606 break;
607
608 case eValueTypeVariableArgument:
609 if (m_option_variable.show_scope)
610 s.PutCString(" ARG: ");
611 break;
612
613 case eValueTypeVariableLocal:
614 if (m_option_variable.show_scope)
615 s.PutCString(" LOCAL: ");
616 break;
617
618 default:
619 break;
620 }
621
Greg Claytonfb816422011-07-10 19:21:23 +0000622 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000623 {
Greg Claytonfb816422011-07-10 19:21:23 +0000624 bool show_fullpaths = false;
625 bool show_module = true;
626 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
627 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000628 }
629
Greg Claytona42880a2011-10-25 06:44:01 +0000630 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000631 if (format != eFormatDefault)
632 valobj_sp->SetFormat (format);
633
634 ValueObject::DumpValueObject (s,
635 valobj_sp.get(),
636 root_name,
Greg Claytonccf44502012-01-26 21:08:30 +0000637 options,
638 format);
Greg Clayton5d81f492011-07-08 21:46:14 +0000639
640 }
Greg Clayton801417e2011-07-07 01:59:51 +0000641
Greg Clayton5d81f492011-07-08 21:46:14 +0000642
643 static uint32_t GetVariableCallback (void *baton,
644 const char *name,
645 VariableList &variable_list)
646 {
647 Target *target = static_cast<Target *>(baton);
648 if (target)
649 {
650 return target->GetImages().FindGlobalVariables (ConstString(name),
651 true,
652 UINT32_MAX,
653 variable_list);
654 }
655 return 0;
656 }
657
658
659
Greg Clayton801417e2011-07-07 01:59:51 +0000660 virtual bool
661 Execute (Args& args, CommandReturnObject &result)
662 {
663 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000664 Target *target = exe_ctx.GetTargetPtr();
665 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000666 {
667 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000668 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000669 if (argc > 0)
670 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000671
Greg Clayton801417e2011-07-07 01:59:51 +0000672 for (size_t idx = 0; idx < argc; ++idx)
673 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000674 VariableList variable_list;
675 ValueObjectList valobj_list;
676
Greg Clayton368f8222011-07-07 04:38:25 +0000677 const char *arg = args.GetArgumentAtIndex(idx);
678 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000679 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000680 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000681 {
Greg Clayton368f8222011-07-07 04:38:25 +0000682 RegularExpression regex(arg);
683 if (!regex.IsValid ())
684 {
685 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
686 result.SetStatus (eReturnStatusFailed);
687 return false;
688 }
Greg Claytonfb816422011-07-10 19:21:23 +0000689 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000690 matches = target->GetImages().FindGlobalVariables (regex,
691 true,
692 UINT32_MAX,
693 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000694 }
695 else
696 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000697 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000698 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000699 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000700 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000701 variable_list,
702 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000703 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000704 }
705
706 if (matches == 0)
707 {
708 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
709 result.SetStatus (eReturnStatusFailed);
710 return false;
711 }
712 else
713 {
Greg Clayton801417e2011-07-07 01:59:51 +0000714 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
715 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000716 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000717 if (var_sp)
718 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000719 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
720 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000721 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000722
723 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000724 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000725 }
726 }
727 }
728 }
729 }
730 else
731 {
Greg Claytonfac93882011-10-05 22:17:32 +0000732 bool success = false;
733 StackFrame *frame = exe_ctx.GetFramePtr();
734 CompileUnit *comp_unit = NULL;
735 if (frame)
736 {
737 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
738 if (comp_unit)
739 {
740 const bool can_create = true;
741 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
742 if (comp_unit_varlist_sp)
743 {
744 size_t count = comp_unit_varlist_sp->GetSize();
745 if (count > 0)
746 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000747 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000748 comp_unit->GetDirectory().GetCString(),
749 comp_unit->GetFilename().GetCString());
750
751 success = true;
752 for (uint32_t i=0; i<count; ++i)
753 {
754 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
755 if (var_sp)
756 {
757 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
758
759 if (valobj_sp)
760 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
761 }
762 }
763 }
764 }
765 }
766 }
767 if (!success)
768 {
769 if (frame)
770 {
771 if (comp_unit)
772 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
773 comp_unit->GetDirectory().GetCString(),
774 comp_unit->GetFilename().GetCString());
775 else
776 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
777 }
778 else
779 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
780 result.SetStatus (eReturnStatusFailed);
781 }
Greg Clayton801417e2011-07-07 01:59:51 +0000782 }
783 }
784 else
785 {
786 result.AppendError ("invalid target, create a debug target using the 'target create' command");
787 result.SetStatus (eReturnStatusFailed);
788 return false;
789 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000790
791 if (m_interpreter.TruncationWarningNecessary())
792 {
793 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
794 m_cmd_name.c_str());
795 m_interpreter.TruncationWarningGiven();
796 }
797
Greg Clayton801417e2011-07-07 01:59:51 +0000798 return result.Succeeded();
799 }
800
801 Options *
802 GetOptions ()
803 {
804 return &m_option_group;
805 }
806
807protected:
808 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000809 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000810 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000811 OptionGroupFileList m_option_compile_units;
812 OptionGroupFileList m_option_shared_libraries;
813 OptionGroupValueObjectDisplay m_varobj_options;
814
815};
816
817
Greg Claytone1f50b92011-05-03 22:09:39 +0000818#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000819
Greg Claytone1f50b92011-05-03 22:09:39 +0000820class CommandObjectTargetModulesSearchPathsAdd : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000821{
822public:
823
Greg Claytone1f50b92011-05-03 22:09:39 +0000824 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000825 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000826 "target modules search-paths add",
Chris Lattner24943d22010-06-08 16:52:24 +0000827 "Add new image search paths substitution pairs to the current target.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000828 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000829 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000830 CommandArgumentEntry arg;
831 CommandArgumentData old_prefix_arg;
832 CommandArgumentData new_prefix_arg;
833
834 // Define the first variant of this arg pair.
835 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
836 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
837
838 // Define the first variant of this arg pair.
839 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
840 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
841
842 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
843 // must always occur together, they are treated as two variants of one argument rather than two independent
844 // arguments. Push them both into the first argument position for m_arguments...
845
846 arg.push_back (old_prefix_arg);
847 arg.push_back (new_prefix_arg);
848
849 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000850 }
851
Greg Claytone1f50b92011-05-03 22:09:39 +0000852 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000853 {
854 }
855
856 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000857 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000858 CommandReturnObject &result)
859 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000860 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000861 if (target)
862 {
863 uint32_t argc = command.GetArgumentCount();
864 if (argc & 1)
865 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000866 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000867 result.SetStatus (eReturnStatusFailed);
868 }
869 else
870 {
871 for (uint32_t i=0; i<argc; i+=2)
872 {
873 const char *from = command.GetArgumentAtIndex(i);
874 const char *to = command.GetArgumentAtIndex(i+1);
875
876 if (from[0] && to[0])
877 {
878 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000879 target->GetImageSearchPathList().Append (ConstString(from),
880 ConstString(to),
881 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000882 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000883 }
884 else
885 {
886 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000887 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000888 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000889 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000890 result.SetStatus (eReturnStatusFailed);
891 }
892 }
893 }
894 }
895 else
896 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000897 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000898 result.SetStatus (eReturnStatusFailed);
899 }
900 return result.Succeeded();
901 }
902};
903
Greg Claytone1f50b92011-05-03 22:09:39 +0000904#pragma mark CommandObjectTargetModulesSearchPathsClear
905
906class CommandObjectTargetModulesSearchPathsClear : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000907{
908public:
909
Greg Claytone1f50b92011-05-03 22:09:39 +0000910 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000911 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000912 "target modules search-paths clear",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000913 "Clear all current image search path substitution pairs from the current target.",
Greg Claytone1f50b92011-05-03 22:09:39 +0000914 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000915 {
916 }
917
Greg Claytone1f50b92011-05-03 22:09:39 +0000918 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000919 {
920 }
921
922 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000923 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000924 CommandReturnObject &result)
925 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000926 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000927 if (target)
928 {
929 bool notify = true;
930 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000931 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000932 }
933 else
934 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000935 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000936 result.SetStatus (eReturnStatusFailed);
937 }
938 return result.Succeeded();
939 }
940};
941
Greg Claytone1f50b92011-05-03 22:09:39 +0000942#pragma mark CommandObjectTargetModulesSearchPathsInsert
943
944class CommandObjectTargetModulesSearchPathsInsert : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000945{
946public:
947
Greg Claytone1f50b92011-05-03 22:09:39 +0000948 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000949 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000950 "target modules search-paths insert",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000951 "Insert a new image search path substitution pair into the current target at the specified index.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000952 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000953 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000954 CommandArgumentEntry arg1;
955 CommandArgumentEntry arg2;
956 CommandArgumentData index_arg;
957 CommandArgumentData old_prefix_arg;
958 CommandArgumentData new_prefix_arg;
959
960 // Define the first and only variant of this arg.
961 index_arg.arg_type = eArgTypeIndex;
962 index_arg.arg_repetition = eArgRepeatPlain;
963
964 // Put the one and only variant into the first arg for m_arguments:
965 arg1.push_back (index_arg);
966
967 // Define the first variant of this arg pair.
968 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
969 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
970
971 // Define the first variant of this arg pair.
972 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
973 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
974
975 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
976 // must always occur together, they are treated as two variants of one argument rather than two independent
977 // arguments. Push them both into the same argument position for m_arguments...
978
979 arg2.push_back (old_prefix_arg);
980 arg2.push_back (new_prefix_arg);
981
982 // Add arguments to m_arguments.
983 m_arguments.push_back (arg1);
984 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +0000985 }
986
Greg Claytone1f50b92011-05-03 22:09:39 +0000987 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +0000988 {
989 }
990
991 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000992 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000993 CommandReturnObject &result)
994 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000995 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000996 if (target)
997 {
998 uint32_t argc = command.GetArgumentCount();
999 // check for at least 3 arguments and an odd nubmer of parameters
1000 if (argc >= 3 && argc & 1)
1001 {
1002 bool success = false;
1003
1004 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1005
1006 if (!success)
1007 {
1008 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1009 result.SetStatus (eReturnStatusFailed);
1010 return result.Succeeded();
1011 }
1012
1013 // shift off the index
1014 command.Shift();
1015 argc = command.GetArgumentCount();
1016
1017 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1018 {
1019 const char *from = command.GetArgumentAtIndex(i);
1020 const char *to = command.GetArgumentAtIndex(i+1);
1021
1022 if (from[0] && to[0])
1023 {
1024 bool last_pair = ((argc - i) == 2);
1025 target->GetImageSearchPathList().Insert (ConstString(from),
1026 ConstString(to),
1027 insert_idx,
1028 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001029 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001030 }
1031 else
1032 {
1033 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001034 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001035 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001036 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001037 result.SetStatus (eReturnStatusFailed);
1038 return false;
1039 }
1040 }
1041 }
1042 else
1043 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001044 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001045 result.SetStatus (eReturnStatusFailed);
1046 return result.Succeeded();
1047 }
1048
1049 }
1050 else
1051 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001052 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001053 result.SetStatus (eReturnStatusFailed);
1054 }
1055 return result.Succeeded();
1056 }
1057};
1058
Greg Claytone1f50b92011-05-03 22:09:39 +00001059
1060#pragma mark CommandObjectTargetModulesSearchPathsList
1061
1062
1063class CommandObjectTargetModulesSearchPathsList : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +00001064{
1065public:
1066
Greg Claytone1f50b92011-05-03 22:09:39 +00001067 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001068 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +00001069 "target modules search-paths list",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001070 "List all current image search path substitution pairs in the current target.",
Greg Claytone1f50b92011-05-03 22:09:39 +00001071 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001072 {
1073 }
1074
Greg Claytone1f50b92011-05-03 22:09:39 +00001075 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001076 {
1077 }
1078
1079 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001080 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001081 CommandReturnObject &result)
1082 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001083 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001084 if (target)
1085 {
1086 if (command.GetArgumentCount() != 0)
1087 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001088 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001089 result.SetStatus (eReturnStatusFailed);
1090 return result.Succeeded();
1091 }
1092
1093 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001094 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001095 }
1096 else
1097 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001098 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001099 result.SetStatus (eReturnStatusFailed);
1100 }
1101 return result.Succeeded();
1102 }
1103};
1104
Greg Claytone1f50b92011-05-03 22:09:39 +00001105#pragma mark CommandObjectTargetModulesSearchPathsQuery
1106
1107class CommandObjectTargetModulesSearchPathsQuery : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +00001108{
1109public:
1110
Greg Claytone1f50b92011-05-03 22:09:39 +00001111 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001112 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +00001113 "target modules search-paths query",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001114 "Transform a path using the first applicable image search path.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001115 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001116 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001117 CommandArgumentEntry arg;
1118 CommandArgumentData path_arg;
1119
1120 // Define the first (and only) variant of this arg.
1121 path_arg.arg_type = eArgTypePath;
1122 path_arg.arg_repetition = eArgRepeatPlain;
1123
1124 // There is only one variant this argument could be; put it into the argument entry.
1125 arg.push_back (path_arg);
1126
1127 // Push the data for the first argument into the m_arguments vector.
1128 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001129 }
1130
Greg Claytone1f50b92011-05-03 22:09:39 +00001131 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001132 {
1133 }
1134
1135 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001136 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001137 CommandReturnObject &result)
1138 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001139 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001140 if (target)
1141 {
1142 if (command.GetArgumentCount() != 1)
1143 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001144 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001145 result.SetStatus (eReturnStatusFailed);
1146 return result.Succeeded();
1147 }
1148
1149 ConstString orig(command.GetArgumentAtIndex(0));
1150 ConstString transformed;
1151 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1152 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1153 else
1154 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001155
1156 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001157 }
1158 else
1159 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001160 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001161 result.SetStatus (eReturnStatusFailed);
1162 }
1163 return result.Succeeded();
1164 }
1165};
1166
Greg Claytone1f50b92011-05-03 22:09:39 +00001167//----------------------------------------------------------------------
1168// Static Helper functions
1169//----------------------------------------------------------------------
1170static void
1171DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1172{
1173 if (module)
1174 {
1175 const char *arch_cstr;
1176 if (full_triple)
1177 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1178 else
1179 arch_cstr = module->GetArchitecture().GetArchitectureName();
1180 if (width)
1181 strm.Printf("%-*s", width, arch_cstr);
1182 else
1183 strm.PutCString(arch_cstr);
1184 }
1185}
1186
1187static void
1188DumpModuleUUID (Stream &strm, Module *module)
1189{
Greg Clayton153ccd72011-08-10 02:10:13 +00001190 if (module->GetUUID().IsValid())
1191 module->GetUUID().Dump (&strm);
1192 else
1193 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001194}
1195
1196static uint32_t
1197DumpCompileUnitLineTable
1198(
1199 CommandInterpreter &interpreter,
1200 Stream &strm,
1201 Module *module,
1202 const FileSpec &file_spec,
1203 bool load_addresses
1204 )
1205{
1206 uint32_t num_matches = 0;
1207 if (module)
1208 {
1209 SymbolContextList sc_list;
1210 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1211 0,
1212 false,
1213 eSymbolContextCompUnit,
1214 sc_list);
1215
1216 for (uint32_t i=0; i<num_matches; ++i)
1217 {
1218 SymbolContext sc;
1219 if (sc_list.GetContextAtIndex(i, sc))
1220 {
1221 if (i > 0)
1222 strm << "\n\n";
1223
1224 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1225 << module->GetFileSpec().GetFilename() << "\n";
1226 LineTable *line_table = sc.comp_unit->GetLineTable();
1227 if (line_table)
1228 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001229 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001230 lldb::eDescriptionLevelBrief);
1231 else
1232 strm << "No line table";
1233 }
1234 }
1235 }
1236 return num_matches;
1237}
1238
1239static void
1240DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1241{
1242 if (file_spec_ptr)
1243 {
1244 if (width > 0)
1245 {
1246 char fullpath[PATH_MAX];
1247 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1248 {
1249 strm.Printf("%-*s", width, fullpath);
1250 return;
1251 }
1252 }
1253 else
1254 {
1255 file_spec_ptr->Dump(&strm);
1256 return;
1257 }
1258 }
1259 // Keep the width spacing correct if things go wrong...
1260 if (width > 0)
1261 strm.Printf("%-*s", width, "");
1262}
1263
1264static void
1265DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1266{
1267 if (file_spec_ptr)
1268 {
1269 if (width > 0)
1270 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1271 else
1272 file_spec_ptr->GetDirectory().Dump(&strm);
1273 return;
1274 }
1275 // Keep the width spacing correct if things go wrong...
1276 if (width > 0)
1277 strm.Printf("%-*s", width, "");
1278}
1279
1280static void
1281DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1282{
1283 if (file_spec_ptr)
1284 {
1285 if (width > 0)
1286 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1287 else
1288 file_spec_ptr->GetFilename().Dump(&strm);
1289 return;
1290 }
1291 // Keep the width spacing correct if things go wrong...
1292 if (width > 0)
1293 strm.Printf("%-*s", width, "");
1294}
1295
1296
1297static void
1298DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1299{
1300 if (module)
1301 {
1302 ObjectFile *objfile = module->GetObjectFile ();
1303 if (objfile)
1304 {
1305 Symtab *symtab = objfile->GetSymtab();
1306 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001307 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001308 }
1309 }
1310}
1311
1312static void
1313DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1314{
1315 if (module)
1316 {
1317 ObjectFile *objfile = module->GetObjectFile ();
1318 if (objfile)
1319 {
1320 SectionList *section_list = objfile->GetSectionList();
1321 if (section_list)
1322 {
1323 strm.PutCString ("Sections for '");
1324 strm << module->GetFileSpec();
1325 if (module->GetObjectName())
1326 strm << '(' << module->GetObjectName() << ')';
1327 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1328 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001329 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001330 strm.IndentLess();
1331 }
1332 }
1333 }
1334}
1335
1336static bool
1337DumpModuleSymbolVendor (Stream &strm, Module *module)
1338{
1339 if (module)
1340 {
1341 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1342 if (symbol_vendor)
1343 {
1344 symbol_vendor->Dump(&strm);
1345 return true;
1346 }
1347 }
1348 return false;
1349}
1350
1351static bool
1352LookupAddressInModule
1353(
1354 CommandInterpreter &interpreter,
1355 Stream &strm,
1356 Module *module,
1357 uint32_t resolve_mask,
1358 lldb::addr_t raw_addr,
1359 lldb::addr_t offset,
1360 bool verbose
1361 )
1362{
1363 if (module)
1364 {
1365 lldb::addr_t addr = raw_addr - offset;
1366 Address so_addr;
1367 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001368 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001369 if (target && !target->GetSectionLoadList().IsEmpty())
1370 {
1371 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1372 return false;
Greg Clayton13d24fb2012-01-29 20:56:30 +00001373 else if (so_addr.GetModulePtr() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001374 return false;
1375 }
1376 else
1377 {
1378 if (!module->ResolveFileAddress (addr, so_addr))
1379 return false;
1380 }
1381
Greg Claytone1f50b92011-05-03 22:09:39 +00001382 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
1383 strm.IndentMore();
1384 strm.Indent (" Address: ");
Greg Clayton00b11c32012-01-10 00:25:18 +00001385 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1386 strm.PutCString (" (");
Greg Claytone1f50b92011-05-03 22:09:39 +00001387 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
Greg Clayton00b11c32012-01-10 00:25:18 +00001388 strm.PutCString (")\n");
Greg Claytone1f50b92011-05-03 22:09:39 +00001389 strm.Indent (" Summary: ");
1390 const uint32_t save_indent = strm.GetIndentLevel ();
Greg Clayton2f57db02011-10-01 00:45:15 +00001391 strm.SetIndentLevel (save_indent + 13);
Greg Claytone1f50b92011-05-03 22:09:39 +00001392 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1393 strm.SetIndentLevel (save_indent);
Greg Claytone1f50b92011-05-03 22:09:39 +00001394 // Print out detailed address information when verbose is enabled
1395 if (verbose)
1396 {
Greg Clayton2f57db02011-10-01 00:45:15 +00001397 strm.EOL();
Jason Molendafb66bb52011-09-23 00:27:44 +00001398 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
Greg Claytone1f50b92011-05-03 22:09:39 +00001399 }
1400 strm.IndentLess();
1401 return true;
1402 }
1403
1404 return false;
1405}
1406
1407static uint32_t
1408LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex)
1409{
1410 if (module)
1411 {
1412 SymbolContext sc;
1413
1414 ObjectFile *objfile = module->GetObjectFile ();
1415 if (objfile)
1416 {
1417 Symtab *symtab = objfile->GetSymtab();
1418 if (symtab)
1419 {
1420 uint32_t i;
1421 std::vector<uint32_t> match_indexes;
1422 ConstString symbol_name (name);
1423 uint32_t num_matches = 0;
1424 if (name_is_regex)
1425 {
1426 RegularExpression name_regexp(name);
1427 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1428 eSymbolTypeAny,
1429 match_indexes);
1430 }
1431 else
1432 {
1433 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1434 }
1435
1436
1437 if (num_matches > 0)
1438 {
1439 strm.Indent ();
1440 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1441 name_is_regex ? "the regular expression " : "", name);
1442 DumpFullpath (strm, &module->GetFileSpec(), 0);
1443 strm.PutCString(":\n");
1444 strm.IndentMore ();
1445 Symtab::DumpSymbolHeader (&strm);
1446 for (i=0; i < num_matches; ++i)
1447 {
1448 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
1449 strm.Indent ();
Greg Clayton567e7f32011-09-22 04:58:26 +00001450 symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001451 }
1452 strm.IndentLess ();
1453 return num_matches;
1454 }
1455 }
1456 }
1457 }
1458 return 0;
1459}
1460
1461
1462static void
1463DumpSymbolContextList (CommandInterpreter &interpreter, Stream &strm, SymbolContextList &sc_list, bool prepend_addr, bool verbose)
1464{
1465 strm.IndentMore ();
1466 uint32_t i;
1467 const uint32_t num_matches = sc_list.GetSize();
1468
1469 for (i=0; i<num_matches; ++i)
1470 {
1471 SymbolContext sc;
1472 if (sc_list.GetContextAtIndex(i, sc))
1473 {
1474 strm.Indent();
1475 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope ();
1476
1477 if (prepend_addr)
1478 {
1479 if (sc.line_entry.range.GetBaseAddress().IsValid())
1480 {
1481 sc.line_entry.range.GetBaseAddress().Dump (&strm,
1482 exe_scope,
1483 Address::DumpStyleLoadAddress,
1484 Address::DumpStyleModuleWithFileAddress);
1485 strm.PutCString(" in ");
1486 }
1487 }
Sean Callanand7793d22012-02-11 00:24:04 +00001488
1489 AddressRange range;
1490
1491 sc.GetAddressRange(eSymbolContextEverything,
1492 0,
1493 true,
1494 range);
1495
Greg Claytone1f50b92011-05-03 22:09:39 +00001496 sc.DumpStopContext(&strm,
1497 exe_scope,
Sean Callanand7793d22012-02-11 00:24:04 +00001498 range.GetBaseAddress(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001499 true,
1500 true,
1501 false);
Sean Callanand7793d22012-02-11 00:24:04 +00001502
Greg Claytone1f50b92011-05-03 22:09:39 +00001503 strm.EOL();
1504 if (verbose)
1505 {
1506 if (sc.line_entry.range.GetBaseAddress().IsValid())
1507 {
1508 if (sc.line_entry.range.GetBaseAddress().Dump (&strm,
1509 exe_scope,
1510 Address::DumpStyleDetailedSymbolContext))
1511 strm.PutCString("\n\n");
1512 }
1513 else if (sc.function->GetAddressRange().GetBaseAddress().IsValid())
1514 {
1515 if (sc.function->GetAddressRange().GetBaseAddress().Dump (&strm,
1516 exe_scope,
1517 Address::DumpStyleDetailedSymbolContext))
1518 strm.PutCString("\n\n");
1519 }
1520 }
1521 }
1522 }
1523 strm.IndentLess ();
1524}
1525
1526static uint32_t
1527LookupFunctionInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
1528{
1529 if (module && name && name[0])
1530 {
1531 SymbolContextList sc_list;
1532 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +00001533 const bool include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00001534 const bool append = true;
1535 uint32_t num_matches = 0;
1536 if (name_is_regex)
1537 {
1538 RegularExpression function_name_regex (name);
1539 num_matches = module->FindFunctions (function_name_regex,
1540 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001541 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001542 append,
1543 sc_list);
1544 }
1545 else
1546 {
1547 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001548 num_matches = module->FindFunctions (function_name,
1549 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001550 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1551 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001552 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001553 append,
1554 sc_list);
1555 }
1556
1557 if (num_matches)
1558 {
1559 strm.Indent ();
1560 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1561 DumpFullpath (strm, &module->GetFileSpec(), 0);
1562 strm.PutCString(":\n");
1563 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose);
1564 }
1565 return num_matches;
1566 }
1567 return 0;
1568}
1569
1570static uint32_t
Greg Clayton801417e2011-07-07 01:59:51 +00001571LookupTypeInModule (CommandInterpreter &interpreter,
1572 Stream &strm,
1573 Module *module,
1574 const char *name_cstr,
1575 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001576{
1577 if (module && name_cstr && name_cstr[0])
1578 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001579 TypeList type_list;
1580 const uint32_t max_num_matches = 1;
1581 uint32_t num_matches = 0;
1582 SymbolContext sc;
1583
1584 ConstString name(name_cstr);
1585 num_matches = module->FindTypes(sc, name, NULL, true, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001586
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001587 if (num_matches)
1588 {
1589 strm.Indent ();
1590 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1591 DumpFullpath (strm, &module->GetFileSpec(), 0);
1592 strm.PutCString(":\n");
1593 const uint32_t num_types = type_list.GetSize();
1594 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001595 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001596 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1597 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001598 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001599 // Resolve the clang type so that any forward references
1600 // to types that haven't yet been parsed will get parsed.
1601 type_sp->GetClangFullType ();
1602 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00001603 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001604 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001605 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001606 }
1607 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001608 }
1609 return 0;
1610}
1611
1612static uint32_t
1613LookupFileAndLineInModule (CommandInterpreter &interpreter,
1614 Stream &strm,
1615 Module *module,
1616 const FileSpec &file_spec,
1617 uint32_t line,
1618 bool check_inlines,
1619 bool verbose)
1620{
1621 if (module && file_spec)
1622 {
1623 SymbolContextList sc_list;
1624 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1625 eSymbolContextEverything, sc_list);
1626 if (num_matches > 0)
1627 {
1628 strm.Indent ();
1629 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1630 strm << file_spec;
1631 if (line > 0)
1632 strm.Printf (":%u", line);
1633 strm << " in ";
1634 DumpFullpath (strm, &module->GetFileSpec(), 0);
1635 strm.PutCString(":\n");
1636 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose);
1637 return num_matches;
1638 }
1639 }
1640 return 0;
1641
1642}
1643
Greg Clayton91048ef2011-11-10 01:18:58 +00001644
1645static size_t
1646FindModulesByName (Target *target,
1647 const char *module_name,
1648 ModuleList &module_list,
1649 bool check_global_list)
1650{
1651// Dump specified images (by basename or fullpath)
1652 FileSpec module_file_spec(module_name, false);
1653
1654 const size_t initial_size = module_list.GetSize ();
1655
1656 size_t num_matches = 0;
1657
1658 if (target)
1659 {
1660 num_matches = target->GetImages().FindModules (&module_file_spec,
1661 NULL,
1662 NULL,
1663 NULL,
1664 module_list);
1665
1666 // Not found in our module list for our target, check the main
1667 // shared module list in case it is a extra file used somewhere
1668 // else
1669 if (num_matches == 0)
1670 num_matches = ModuleList::FindSharedModules (module_file_spec,
1671 target->GetArchitecture(),
1672 NULL,
1673 NULL,
1674 module_list);
1675 }
1676 else
1677 {
1678 num_matches = ModuleList::FindSharedModules (module_file_spec,
1679 ArchSpec(),
1680 NULL,
1681 NULL,
1682 module_list);
1683 }
1684
1685 if (check_global_list && num_matches == 0)
1686 {
1687 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001688 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001689 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1690 ModuleSP module_sp;
1691 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1692 {
1693 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1694
1695 if (module)
1696 {
1697 if (FileSpec::Equal(module->GetFileSpec(), module_file_spec, true))
1698 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001699 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001700 module_list.AppendIfNeeded(module_sp);
1701 }
1702 }
1703 }
1704 }
1705 return module_list.GetSize () - initial_size;
1706}
1707
Greg Claytone1f50b92011-05-03 22:09:39 +00001708#pragma mark CommandObjectTargetModulesModuleAutoComplete
1709
1710//----------------------------------------------------------------------
1711// A base command object class that can auto complete with module file
1712// paths
1713//----------------------------------------------------------------------
1714
1715class CommandObjectTargetModulesModuleAutoComplete : public CommandObject
1716{
1717public:
1718
1719 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1720 const char *name,
1721 const char *help,
1722 const char *syntax) :
1723 CommandObject (interpreter, name, help, syntax)
1724 {
1725 CommandArgumentEntry arg;
1726 CommandArgumentData file_arg;
1727
1728 // Define the first (and only) variant of this arg.
1729 file_arg.arg_type = eArgTypeFilename;
1730 file_arg.arg_repetition = eArgRepeatStar;
1731
1732 // There is only one variant this argument could be; put it into the argument entry.
1733 arg.push_back (file_arg);
1734
1735 // Push the data for the first argument into the m_arguments vector.
1736 m_arguments.push_back (arg);
1737 }
1738
1739 virtual
1740 ~CommandObjectTargetModulesModuleAutoComplete ()
1741 {
1742 }
1743
1744 virtual int
1745 HandleArgumentCompletion (Args &input,
1746 int &cursor_index,
1747 int &cursor_char_position,
1748 OptionElementVector &opt_element_vector,
1749 int match_start_point,
1750 int max_return_elements,
1751 bool &word_complete,
1752 StringList &matches)
1753 {
1754 // Arguments are the standard module completer.
1755 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1756 completion_str.erase (cursor_char_position);
1757
1758 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1759 CommandCompletions::eModuleCompletion,
1760 completion_str.c_str(),
1761 match_start_point,
1762 max_return_elements,
1763 NULL,
1764 word_complete,
1765 matches);
1766 return matches.GetSize();
1767 }
1768};
1769
1770#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1771
1772//----------------------------------------------------------------------
1773// A base command object class that can auto complete with module source
1774// file paths
1775//----------------------------------------------------------------------
1776
1777class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObject
1778{
1779public:
1780
1781 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1782 const char *name,
1783 const char *help,
1784 const char *syntax) :
1785 CommandObject (interpreter, name, help, syntax)
1786 {
1787 CommandArgumentEntry arg;
1788 CommandArgumentData source_file_arg;
1789
1790 // Define the first (and only) variant of this arg.
1791 source_file_arg.arg_type = eArgTypeSourceFile;
1792 source_file_arg.arg_repetition = eArgRepeatPlus;
1793
1794 // There is only one variant this argument could be; put it into the argument entry.
1795 arg.push_back (source_file_arg);
1796
1797 // Push the data for the first argument into the m_arguments vector.
1798 m_arguments.push_back (arg);
1799 }
1800
1801 virtual
1802 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1803 {
1804 }
1805
1806 virtual int
1807 HandleArgumentCompletion (Args &input,
1808 int &cursor_index,
1809 int &cursor_char_position,
1810 OptionElementVector &opt_element_vector,
1811 int match_start_point,
1812 int max_return_elements,
1813 bool &word_complete,
1814 StringList &matches)
1815 {
1816 // Arguments are the standard source file completer.
1817 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1818 completion_str.erase (cursor_char_position);
1819
1820 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1821 CommandCompletions::eSourceFileCompletion,
1822 completion_str.c_str(),
1823 match_start_point,
1824 max_return_elements,
1825 NULL,
1826 word_complete,
1827 matches);
1828 return matches.GetSize();
1829 }
1830};
1831
1832
1833#pragma mark CommandObjectTargetModulesDumpSymtab
1834
1835
1836class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1837{
1838public:
1839 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1840 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1841 "target modules dump symtab",
1842 "Dump the symbol table from one or more target modules.",
1843 NULL),
1844 m_options (interpreter)
1845 {
1846 }
1847
1848 virtual
1849 ~CommandObjectTargetModulesDumpSymtab ()
1850 {
1851 }
1852
1853 virtual bool
1854 Execute (Args& command,
1855 CommandReturnObject &result)
1856 {
1857 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1858 if (target == NULL)
1859 {
1860 result.AppendError ("invalid target, create a debug target using the 'target create' command");
1861 result.SetStatus (eReturnStatusFailed);
1862 return false;
1863 }
1864 else
1865 {
1866 uint32_t num_dumped = 0;
1867
1868 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1869 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1870 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1871
1872 if (command.GetArgumentCount() == 0)
1873 {
1874 // Dump all sections for all modules images
1875 const uint32_t num_modules = target->GetImages().GetSize();
1876 if (num_modules > 0)
1877 {
1878 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
1879 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1880 {
1881 if (num_dumped > 0)
1882 {
1883 result.GetOutputStream().EOL();
1884 result.GetOutputStream().EOL();
1885 }
1886 num_dumped++;
1887 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx), m_options.m_sort_order);
1888 }
1889 }
1890 else
1891 {
1892 result.AppendError ("the target has no associated executable images");
1893 result.SetStatus (eReturnStatusFailed);
1894 return false;
1895 }
1896 }
1897 else
1898 {
1899 // Dump specified images (by basename or fullpath)
1900 const char *arg_cstr;
1901 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
1902 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001903 ModuleList module_list;
1904 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
1905 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00001906 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001907 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001908 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001909 Module *module = module_list.GetModulePointerAtIndex(i);
1910 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001911 {
1912 if (num_dumped > 0)
1913 {
1914 result.GetOutputStream().EOL();
1915 result.GetOutputStream().EOL();
1916 }
1917 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00001918 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001919 }
1920 }
1921 }
1922 else
1923 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
1924 }
1925 }
1926
1927 if (num_dumped > 0)
1928 result.SetStatus (eReturnStatusSuccessFinishResult);
1929 else
1930 {
1931 result.AppendError ("no matching executable images found");
1932 result.SetStatus (eReturnStatusFailed);
1933 }
1934 }
1935 return result.Succeeded();
1936 }
1937
1938 virtual Options *
1939 GetOptions ()
1940 {
1941 return &m_options;
1942 }
1943
1944 class CommandOptions : public Options
1945 {
1946 public:
1947
1948 CommandOptions (CommandInterpreter &interpreter) :
1949 Options(interpreter),
1950 m_sort_order (eSortOrderNone)
1951 {
1952 }
1953
1954 virtual
1955 ~CommandOptions ()
1956 {
1957 }
1958
1959 virtual Error
1960 SetOptionValue (uint32_t option_idx, const char *option_arg)
1961 {
1962 Error error;
1963 char short_option = (char) m_getopt_table[option_idx].val;
1964
1965 switch (short_option)
1966 {
1967 case 's':
Greg Claytone1f50b92011-05-03 22:09:39 +00001968 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1969 g_option_table[option_idx].enum_values,
1970 eSortOrderNone,
Greg Clayton61aca5d2011-10-07 18:58:12 +00001971 error);
Greg Claytone1f50b92011-05-03 22:09:39 +00001972 break;
1973
1974 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001975 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone1f50b92011-05-03 22:09:39 +00001976 break;
1977
1978 }
1979 return error;
1980 }
1981
1982 void
1983 OptionParsingStarting ()
1984 {
1985 m_sort_order = eSortOrderNone;
1986 }
1987
1988 const OptionDefinition*
1989 GetDefinitions ()
1990 {
1991 return g_option_table;
1992 }
1993
1994 // Options table: Required for subclasses of Options.
1995 static OptionDefinition g_option_table[];
1996
1997 SortOrder m_sort_order;
1998 };
1999
2000protected:
2001
2002 CommandOptions m_options;
2003};
2004
2005static OptionEnumValueElement
2006g_sort_option_enumeration[4] =
2007{
2008 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2009 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2010 { eSortOrderByName, "name", "Sort output by symbol name."},
2011 { 0, NULL, NULL }
2012};
2013
2014
2015OptionDefinition
2016CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2017{
2018 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2019 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2020};
2021
2022#pragma mark CommandObjectTargetModulesDumpSections
2023
2024//----------------------------------------------------------------------
2025// Image section dumping command
2026//----------------------------------------------------------------------
2027
2028class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2029{
2030public:
2031 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2032 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2033 "target modules dump sections",
2034 "Dump the sections from one or more target modules.",
2035 //"target modules dump sections [<file1> ...]")
2036 NULL)
2037 {
2038 }
2039
2040 virtual
2041 ~CommandObjectTargetModulesDumpSections ()
2042 {
2043 }
2044
2045 virtual bool
2046 Execute (Args& command,
2047 CommandReturnObject &result)
2048 {
2049 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2050 if (target == NULL)
2051 {
2052 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2053 result.SetStatus (eReturnStatusFailed);
2054 return false;
2055 }
2056 else
2057 {
2058 uint32_t num_dumped = 0;
2059
2060 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2061 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2062 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2063
2064 if (command.GetArgumentCount() == 0)
2065 {
2066 // Dump all sections for all modules images
2067 const uint32_t num_modules = target->GetImages().GetSize();
2068 if (num_modules > 0)
2069 {
2070 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2071 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2072 {
2073 num_dumped++;
2074 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2075 }
2076 }
2077 else
2078 {
2079 result.AppendError ("the target has no associated executable images");
2080 result.SetStatus (eReturnStatusFailed);
2081 return false;
2082 }
2083 }
2084 else
2085 {
2086 // Dump specified images (by basename or fullpath)
2087 const char *arg_cstr;
2088 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2089 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002090 ModuleList module_list;
2091 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2092 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002093 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002094 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002095 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002096 Module *module = module_list.GetModulePointerAtIndex(i);
2097 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002098 {
2099 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002100 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002101 }
2102 }
2103 }
2104 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002105 {
2106 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002107 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002108
Greg Claytone1f50b92011-05-03 22:09:39 +00002109 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002110 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002111 }
2112 }
2113
2114 if (num_dumped > 0)
2115 result.SetStatus (eReturnStatusSuccessFinishResult);
2116 else
2117 {
2118 result.AppendError ("no matching executable images found");
2119 result.SetStatus (eReturnStatusFailed);
2120 }
2121 }
2122 return result.Succeeded();
2123 }
2124};
2125
2126
2127#pragma mark CommandObjectTargetModulesDumpSymfile
2128
2129//----------------------------------------------------------------------
2130// Image debug symbol dumping command
2131//----------------------------------------------------------------------
2132
2133class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2134{
2135public:
2136 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2137 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2138 "target modules dump symfile",
2139 "Dump the debug symbol file for one or more target modules.",
2140 //"target modules dump symfile [<file1> ...]")
2141 NULL)
2142 {
2143 }
2144
2145 virtual
2146 ~CommandObjectTargetModulesDumpSymfile ()
2147 {
2148 }
2149
2150 virtual bool
2151 Execute (Args& command,
2152 CommandReturnObject &result)
2153 {
2154 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2155 if (target == NULL)
2156 {
2157 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2158 result.SetStatus (eReturnStatusFailed);
2159 return false;
2160 }
2161 else
2162 {
2163 uint32_t num_dumped = 0;
2164
2165 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2166 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2167 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2168
2169 if (command.GetArgumentCount() == 0)
2170 {
2171 // Dump all sections for all modules images
2172 const uint32_t num_modules = target->GetImages().GetSize();
2173 if (num_modules > 0)
2174 {
2175 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2176 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2177 {
2178 if (DumpModuleSymbolVendor (result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx)))
2179 num_dumped++;
2180 }
2181 }
2182 else
2183 {
2184 result.AppendError ("the target has no associated executable images");
2185 result.SetStatus (eReturnStatusFailed);
2186 return false;
2187 }
2188 }
2189 else
2190 {
2191 // Dump specified images (by basename or fullpath)
2192 const char *arg_cstr;
2193 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2194 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002195 ModuleList module_list;
2196 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2197 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002198 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002199 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002200 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002201 Module *module = module_list.GetModulePointerAtIndex(i);
2202 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002203 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002204 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002205 num_dumped++;
2206 }
2207 }
2208 }
2209 else
2210 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2211 }
2212 }
2213
2214 if (num_dumped > 0)
2215 result.SetStatus (eReturnStatusSuccessFinishResult);
2216 else
2217 {
2218 result.AppendError ("no matching executable images found");
2219 result.SetStatus (eReturnStatusFailed);
2220 }
2221 }
2222 return result.Succeeded();
2223 }
2224};
2225
2226
2227#pragma mark CommandObjectTargetModulesDumpLineTable
2228
2229//----------------------------------------------------------------------
2230// Image debug line table dumping command
2231//----------------------------------------------------------------------
2232
2233class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2234{
2235public:
2236 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2237 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2238 "target modules dump line-table",
2239 "Dump the debug symbol file for one or more target modules.",
2240 NULL)
2241 {
2242 }
2243
2244 virtual
2245 ~CommandObjectTargetModulesDumpLineTable ()
2246 {
2247 }
2248
2249 virtual bool
2250 Execute (Args& command,
2251 CommandReturnObject &result)
2252 {
2253 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2254 if (target == NULL)
2255 {
2256 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2257 result.SetStatus (eReturnStatusFailed);
2258 return false;
2259 }
2260 else
2261 {
2262 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2263 uint32_t total_num_dumped = 0;
2264
2265 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2266 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2267 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2268
2269 if (command.GetArgumentCount() == 0)
2270 {
2271 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2272 result.SetStatus (eReturnStatusFailed);
2273 }
2274 else
2275 {
2276 // Dump specified images (by basename or fullpath)
2277 const char *arg_cstr;
2278 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2279 {
2280 FileSpec file_spec(arg_cstr, false);
2281 const uint32_t num_modules = target->GetImages().GetSize();
2282 if (num_modules > 0)
2283 {
2284 uint32_t num_dumped = 0;
2285 for (uint32_t i = 0; i<num_modules; ++i)
2286 {
2287 if (DumpCompileUnitLineTable (m_interpreter,
2288 result.GetOutputStream(),
2289 target->GetImages().GetModulePointerAtIndex(i),
2290 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002291 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002292 num_dumped++;
2293 }
2294 if (num_dumped == 0)
2295 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2296 else
2297 total_num_dumped += num_dumped;
2298 }
2299 }
2300 }
2301
2302 if (total_num_dumped > 0)
2303 result.SetStatus (eReturnStatusSuccessFinishResult);
2304 else
2305 {
2306 result.AppendError ("no source filenames matched any command arguments");
2307 result.SetStatus (eReturnStatusFailed);
2308 }
2309 }
2310 return result.Succeeded();
2311 }
2312};
2313
2314
2315#pragma mark CommandObjectTargetModulesDump
2316
2317//----------------------------------------------------------------------
2318// Dump multi-word command for target modules
2319//----------------------------------------------------------------------
2320
2321class CommandObjectTargetModulesDump : public CommandObjectMultiword
2322{
2323public:
2324
2325 //------------------------------------------------------------------
2326 // Constructors and Destructors
2327 //------------------------------------------------------------------
2328 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2329 CommandObjectMultiword (interpreter,
2330 "target modules dump",
2331 "A set of commands for dumping information about one or more target modules.",
2332 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2333 {
2334 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2335 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2336 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2337 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2338 }
2339
2340 virtual
2341 ~CommandObjectTargetModulesDump()
2342 {
2343 }
2344};
2345
2346class CommandObjectTargetModulesAdd : public CommandObject
2347{
2348public:
2349 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
2350 CommandObject (interpreter,
2351 "target modules add",
2352 "Add a new module to the current target's modules.",
2353 "target modules add [<module>]")
2354 {
2355 }
2356
2357 virtual
2358 ~CommandObjectTargetModulesAdd ()
2359 {
2360 }
2361
2362 virtual bool
2363 Execute (Args& args,
2364 CommandReturnObject &result)
2365 {
2366 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2367 if (target == NULL)
2368 {
2369 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2370 result.SetStatus (eReturnStatusFailed);
2371 return false;
2372 }
2373 else
2374 {
2375 const size_t argc = args.GetArgumentCount();
2376 if (argc == 0)
2377 {
2378 result.AppendError ("one or more executable image paths must be specified");
2379 result.SetStatus (eReturnStatusFailed);
2380 return false;
2381 }
2382 else
2383 {
2384 for (size_t i=0; i<argc; ++i)
2385 {
2386 const char *path = args.GetArgumentAtIndex(i);
2387 if (path)
2388 {
2389 FileSpec file_spec(path, true);
2390 ArchSpec arch;
2391 if (file_spec.Exists())
2392 {
2393 ModuleSP module_sp (target->GetSharedModule(file_spec, arch));
2394 if (!module_sp)
2395 {
2396 result.AppendError ("one or more executable image paths must be specified");
2397 result.SetStatus (eReturnStatusFailed);
2398 return false;
2399 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002400 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002401 }
2402 else
2403 {
2404 char resolved_path[PATH_MAX];
2405 result.SetStatus (eReturnStatusFailed);
2406 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2407 {
2408 if (strcmp (resolved_path, path) != 0)
2409 {
2410 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2411 break;
2412 }
2413 }
2414 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2415 break;
2416 }
2417 }
2418 }
2419 }
2420 }
2421 return result.Succeeded();
2422 }
2423
2424 int
2425 HandleArgumentCompletion (Args &input,
2426 int &cursor_index,
2427 int &cursor_char_position,
2428 OptionElementVector &opt_element_vector,
2429 int match_start_point,
2430 int max_return_elements,
2431 bool &word_complete,
2432 StringList &matches)
2433 {
2434 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2435 completion_str.erase (cursor_char_position);
2436
2437 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2438 CommandCompletions::eDiskFileCompletion,
2439 completion_str.c_str(),
2440 match_start_point,
2441 max_return_elements,
2442 NULL,
2443 word_complete,
2444 matches);
2445 return matches.GetSize();
2446 }
2447
2448};
2449
2450class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2451{
2452public:
2453 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2454 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2455 "target modules load",
2456 "Set the load addresses for one or more sections in a target module.",
2457 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2458 m_option_group (interpreter),
2459 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2460 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)
2461 {
2462 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2463 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2464 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2465 m_option_group.Finalize();
2466 }
2467
2468 virtual
2469 ~CommandObjectTargetModulesLoad ()
2470 {
2471 }
2472
2473 virtual bool
2474 Execute (Args& args,
2475 CommandReturnObject &result)
2476 {
2477 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2478 if (target == NULL)
2479 {
2480 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2481 result.SetStatus (eReturnStatusFailed);
2482 return false;
2483 }
2484 else
2485 {
2486 const size_t argc = args.GetArgumentCount();
2487 const FileSpec *file_ptr = NULL;
2488 const UUID *uuid_ptr = NULL;
2489 if (m_file_option.GetOptionValue().OptionWasSet())
2490 file_ptr = &m_file_option.GetOptionValue().GetCurrentValue();
2491
2492 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
2493 uuid_ptr = &m_uuid_option_group.GetOptionValue().GetCurrentValue();
2494
2495 if (file_ptr || uuid_ptr)
2496 {
2497
2498 ModuleList matching_modules;
2499 const size_t num_matches = target->GetImages().FindModules (file_ptr, // File spec to match (can be NULL to match by UUID only)
2500 NULL, // Architecture
2501 uuid_ptr, // UUID to match (can be NULL to not match on UUID)
2502 NULL, // Object name
2503 matching_modules);
2504
2505 char path[PATH_MAX];
2506 if (num_matches == 1)
2507 {
2508 Module *module = matching_modules.GetModulePointerAtIndex(0);
2509 if (module)
2510 {
2511 ObjectFile *objfile = module->GetObjectFile();
2512 if (objfile)
2513 {
2514 SectionList *section_list = objfile->GetSectionList();
2515 if (section_list)
2516 {
2517 if (argc == 0)
2518 {
2519 if (m_slide_option.GetOptionValue().OptionWasSet())
2520 {
2521 Module *module = matching_modules.GetModulePointerAtIndex(0);
2522 if (module)
2523 {
2524 ObjectFile *objfile = module->GetObjectFile();
2525 if (objfile)
2526 {
2527 SectionList *section_list = objfile->GetSectionList();
2528 if (section_list)
2529 {
2530 const size_t num_sections = section_list->GetSize();
2531 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2532 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx)
2533 {
2534 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx));
2535 if (section_sp)
2536 target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide);
2537 }
2538 }
2539 }
2540 }
2541 }
2542 else
2543 {
2544 result.AppendError ("one or more section name + load address pair must be specified");
2545 result.SetStatus (eReturnStatusFailed);
2546 return false;
2547 }
2548 }
2549 else
2550 {
2551 if (m_slide_option.GetOptionValue().OptionWasSet())
2552 {
2553 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2554 result.SetStatus (eReturnStatusFailed);
2555 return false;
2556 }
2557
2558 for (size_t i=0; i<argc; i += 2)
2559 {
2560 const char *sect_name = args.GetArgumentAtIndex(i);
2561 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2562 if (sect_name && load_addr_cstr)
2563 {
2564 ConstString const_sect_name(sect_name);
2565 bool success = false;
2566 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2567 if (success)
2568 {
2569 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2570 if (section_sp)
2571 {
2572 target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), load_addr);
2573 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2574 }
2575 else
2576 {
2577 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2578 result.SetStatus (eReturnStatusFailed);
2579 break;
2580 }
2581 }
2582 else
2583 {
2584 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2585 result.SetStatus (eReturnStatusFailed);
2586 break;
2587 }
2588 }
2589 else
2590 {
2591 if (sect_name)
2592 result.AppendError ("section names must be followed by a load address.\n");
2593 else
2594 result.AppendError ("one or more section name + load address pair must be specified.\n");
2595 result.SetStatus (eReturnStatusFailed);
2596 break;
2597 }
2598 }
2599 }
2600 }
2601 else
2602 {
2603 module->GetFileSpec().GetPath (path, sizeof(path));
2604 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2605 result.SetStatus (eReturnStatusFailed);
2606 }
2607 }
2608 else
2609 {
2610 module->GetFileSpec().GetPath (path, sizeof(path));
2611 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2612 result.SetStatus (eReturnStatusFailed);
2613 }
2614 }
2615 else
2616 {
2617 module->GetFileSpec().GetPath (path, sizeof(path));
2618 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2619 result.SetStatus (eReturnStatusFailed);
2620 }
2621 }
2622 else
2623 {
2624 char uuid_cstr[64];
2625 if (file_ptr)
2626 file_ptr->GetPath (path, sizeof(path));
2627 else
2628 path[0] = '\0';
2629
2630 if (uuid_ptr)
2631 uuid_ptr->GetAsCString(uuid_cstr, sizeof(uuid_cstr));
2632 else
2633 uuid_cstr[0] = '\0';
2634 if (num_matches > 1)
2635 {
2636 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2637 path[0] ? " file=" : "",
2638 path,
2639 uuid_cstr[0] ? " uuid=" : "",
2640 uuid_cstr);
2641 for (size_t i=0; i<num_matches; ++i)
2642 {
2643 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2644 result.AppendMessageWithFormat("%s\n", path);
2645 }
2646 }
2647 else
2648 {
2649 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2650 path[0] ? " file=" : "",
2651 path,
2652 uuid_cstr[0] ? " uuid=" : "",
2653 uuid_cstr);
2654 }
2655 result.SetStatus (eReturnStatusFailed);
2656 }
2657 }
2658 else
2659 {
2660 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2661 result.SetStatus (eReturnStatusFailed);
2662 return false;
2663 }
2664 }
2665 return result.Succeeded();
2666 }
2667
2668 virtual Options *
2669 GetOptions ()
2670 {
2671 return &m_option_group;
2672 }
2673
2674protected:
2675 OptionGroupOptions m_option_group;
2676 OptionGroupUUID m_uuid_option_group;
2677 OptionGroupFile m_file_option;
2678 OptionGroupUInt64 m_slide_option;
2679};
2680
2681//----------------------------------------------------------------------
2682// List images with associated information
2683//----------------------------------------------------------------------
2684class CommandObjectTargetModulesList : public CommandObject
2685{
2686public:
2687
2688 class CommandOptions : public Options
2689 {
2690 public:
2691
2692 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002693 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002694 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002695 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002696 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002697 {
2698 }
2699
2700 virtual
2701 ~CommandOptions ()
2702 {
2703 }
2704
2705 virtual Error
2706 SetOptionValue (uint32_t option_idx, const char *option_arg)
2707 {
2708 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002709 if (short_option == 'g')
2710 {
2711 m_use_global_module_list = true;
2712 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002713 else if (short_option == 'a')
2714 {
2715 bool success;
2716 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2717 if (!success)
2718 {
2719 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002720 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002721 }
2722 }
Greg Clayton899025f2011-08-09 00:01:09 +00002723 else
2724 {
2725 uint32_t width = 0;
2726 if (option_arg)
2727 width = strtoul (option_arg, NULL, 0);
2728 m_format_array.push_back(std::make_pair(short_option, width));
2729 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002730 Error error;
2731 return error;
2732 }
2733
2734 void
2735 OptionParsingStarting ()
2736 {
2737 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002738 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002739 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002740 }
2741
2742 const OptionDefinition*
2743 GetDefinitions ()
2744 {
2745 return g_option_table;
2746 }
2747
2748 // Options table: Required for subclasses of Options.
2749
2750 static OptionDefinition g_option_table[];
2751
2752 // Instance variables to hold the values for command options.
2753 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2754 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002755 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002756 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002757 };
2758
2759 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
2760 CommandObject (interpreter,
2761 "target modules list",
2762 "List current executable and dependent shared library images.",
2763 "target modules list [<cmd-options>]"),
2764 m_options (interpreter)
2765 {
2766 }
2767
2768 virtual
2769 ~CommandObjectTargetModulesList ()
2770 {
2771 }
2772
2773 virtual
2774 Options *
2775 GetOptions ()
2776 {
2777 return &m_options;
2778 }
2779
2780 virtual bool
2781 Execute (Args& command,
2782 CommandReturnObject &result)
2783 {
2784 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002785 const bool use_global_module_list = m_options.m_use_global_module_list;
2786 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002787 {
2788 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2789 result.SetStatus (eReturnStatusFailed);
2790 return false;
2791 }
2792 else
2793 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002794 if (target)
2795 {
2796 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2797 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2798 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2799 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002800 // Dump all sections for all modules images
Greg Clayton899025f2011-08-09 00:01:09 +00002801 uint32_t num_modules = 0;
2802 Mutex::Locker locker;
Jim Ingham6bdea822011-10-24 18:36:33 +00002803
2804 Stream &strm = result.GetOutputStream();
2805
2806 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2807 {
2808 if (target)
2809 {
2810 Address module_address;
2811 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2812 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00002813 Module *module = module_address.GetModulePtr();
Jim Ingham6bdea822011-10-24 18:36:33 +00002814 if (module)
2815 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002816 PrintModule (target, module, UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002817 result.SetStatus (eReturnStatusSuccessFinishResult);
2818 }
2819 else
2820 {
2821 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2822 result.SetStatus (eReturnStatusFailed);
2823 }
2824 }
2825 else
2826 {
2827 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2828 result.SetStatus (eReturnStatusFailed);
2829 }
2830 }
2831 else
2832 {
2833 result.AppendError ("Can only look up modules by address with a valid target.");
2834 result.SetStatus (eReturnStatusFailed);
2835 }
2836 return result.Succeeded();
2837 }
2838
Greg Clayton153ccd72011-08-10 02:10:13 +00002839 if (use_global_module_list)
Greg Clayton899025f2011-08-09 00:01:09 +00002840 {
Greg Claytonc149c8b2012-01-27 18:08:35 +00002841 locker.Reset (Module::GetAllocationModuleCollectionMutex()->GetMutex());
Greg Clayton899025f2011-08-09 00:01:09 +00002842 num_modules = Module::GetNumberAllocatedModules();
2843 }
2844 else
2845 num_modules = target->GetImages().GetSize();
2846
Greg Claytone1f50b92011-05-03 22:09:39 +00002847 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002848 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002849 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2850 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002851 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002852 Module *module;
Greg Clayton153ccd72011-08-10 02:10:13 +00002853 if (use_global_module_list)
Greg Clayton899025f2011-08-09 00:01:09 +00002854 {
2855 module = Module::GetAllocatedModuleAtIndex(image_idx);
Greg Clayton13d24fb2012-01-29 20:56:30 +00002856 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00002857 }
2858 else
2859 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002860 module_sp = target->GetImages().GetModuleAtIndex(image_idx);
2861 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002862 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002863
Greg Claytonb5a8f142012-02-05 02:38:54 +00002864 int indent = strm.Printf("[%3u] ", image_idx);
2865 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00002866
Greg Claytone1f50b92011-05-03 22:09:39 +00002867 }
2868 result.SetStatus (eReturnStatusSuccessFinishResult);
2869 }
2870 else
2871 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002872 if (use_global_module_list)
2873 result.AppendError ("the global module list is empty");
2874 else
2875 result.AppendError ("the target has no associated executable images");
Greg Claytone1f50b92011-05-03 22:09:39 +00002876 result.SetStatus (eReturnStatusFailed);
2877 return false;
2878 }
2879 }
2880 return result.Succeeded();
2881 }
2882protected:
Jim Ingham6bdea822011-10-24 18:36:33 +00002883
2884 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00002885 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00002886 {
2887
2888 bool dump_object_name = false;
2889 if (m_options.m_format_array.empty())
2890 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002891 m_options.m_format_array.push_back(std::make_pair('u', 0));
2892 m_options.m_format_array.push_back(std::make_pair('h', 0));
2893 m_options.m_format_array.push_back(std::make_pair('f', 0));
2894 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00002895 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00002896 const size_t num_entries = m_options.m_format_array.size();
2897 bool print_space = false;
2898 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00002899 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002900 if (print_space)
2901 strm.PutChar(' ');
2902 print_space = true;
2903 const char format_char = m_options.m_format_array[i].first;
2904 uint32_t width = m_options.m_format_array[i].second;
2905 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00002906 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002907 case 'A':
2908 DumpModuleArchitecture (strm, module, false, width);
2909 break;
2910
2911 case 't':
2912 DumpModuleArchitecture (strm, module, true, width);
2913 break;
2914
2915 case 'f':
2916 DumpFullpath (strm, &module->GetFileSpec(), width);
2917 dump_object_name = true;
2918 break;
2919
2920 case 'd':
2921 DumpDirectory (strm, &module->GetFileSpec(), width);
2922 break;
2923
2924 case 'b':
2925 DumpBasename (strm, &module->GetFileSpec(), width);
2926 dump_object_name = true;
2927 break;
2928
2929 case 'h':
2930 case 'o':
2931 // Image header address
2932 {
2933 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00002934
Greg Claytonb5a8f142012-02-05 02:38:54 +00002935 ObjectFile *objfile = module->GetObjectFile ();
2936 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00002937 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002938 Address header_addr(objfile->GetHeaderAddress());
2939 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00002940 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002941 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00002942 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00002943 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
2944 if (header_load_addr == LLDB_INVALID_ADDRESS)
2945 {
2946 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
2947 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002948 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00002949 {
2950 if (format_char == 'o')
2951 {
2952 // Show the offset of slide for the image
2953 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
2954 }
2955 else
2956 {
2957 // Show the load address of the image
2958 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
2959 }
2960 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002961 break;
2962 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00002963 // The address was valid, but the image isn't loaded, output the address in an appropriate format
2964 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
2965 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00002966 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002967 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00002968 strm.Printf ("%*s", addr_nibble_width + 2, "");
2969 }
2970 break;
2971 case 'r':
2972 {
2973 uint32_t ref_count = 0;
2974 ModuleSP module_sp (module->shared_from_this());
2975 if (module_sp)
2976 {
2977 // Take one away to make sure we don't count our local "module_sp"
2978 ref_count = module_sp.use_count() - 1;
2979 }
2980 if (width)
2981 strm.Printf("{%*u}", width, ref_count);
2982 else
2983 strm.Printf("{%u}", ref_count);
2984 }
2985 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00002986
Greg Claytonb5a8f142012-02-05 02:38:54 +00002987 case 's':
2988 case 'S':
2989 {
2990 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
2991 if (symbol_vendor)
2992 {
2993 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
2994 if (symbol_file)
2995 {
2996 if (format_char == 'S')
2997 {
2998 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
2999 // Dump symbol file only if different from module file
3000 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3001 {
3002 print_space = false;
3003 break;
3004 }
3005 // Add a newline and indent past the index
3006 strm.Printf ("\n%*s", indent, "");
3007 }
3008 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3009 dump_object_name = true;
3010 break;
3011 }
3012 }
3013 strm.Printf("%.*s", width, "<NONE>");
3014 }
3015 break;
3016
3017 case 'm':
3018 module->GetModificationTime().Dump(&strm, width);
3019 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003020
Greg Claytonb5a8f142012-02-05 02:38:54 +00003021 case 'p':
3022 strm.Printf("%p", module);
3023 break;
3024
3025 case 'u':
3026 DumpModuleUUID(strm, module);
3027 break;
3028
3029 default:
3030 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003031 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003032
3033 }
3034 if (dump_object_name)
3035 {
3036 const char *object_name = module->GetObjectName().GetCString();
3037 if (object_name)
3038 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003039 }
3040 strm.EOL();
3041 }
3042
Greg Claytone1f50b92011-05-03 22:09:39 +00003043 CommandOptions m_options;
3044};
3045
3046OptionDefinition
3047CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3048{
Jim Ingham6bdea822011-10-24 18:36:33 +00003049 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3050 { LLDB_OPT_SET_1, false, "arch", 'A', optional_argument, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003051 { LLDB_OPT_SET_1, false, "triple", 't', optional_argument, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
Greg Claytonb5a8f142012-02-05 02:38:54 +00003052 { LLDB_OPT_SET_1, false, "header", 'h', no_argument, NULL, 0, eArgTypeNone, "Display the image header address as a load address if debugging, a file address otherwise."},
3053 { LLDB_OPT_SET_1, false, "offset", 'o', no_argument, NULL, 0, eArgTypeNone, "Display the image header address offset from the header file address (the slide amount)."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003054 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3055 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3056 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3057 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3058 { LLDB_OPT_SET_1, false, "symfile", 's', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
Greg Claytonb5a8f142012-02-05 02:38:54 +00003059 { LLDB_OPT_SET_1, false, "symfile-unique", 'S', optional_argument, NULL, 0, eArgTypeWidth, "Display the symbol file with optional width only if it is different from the executable object file."},
Greg Clayton153ccd72011-08-10 02:10:13 +00003060 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3061 { LLDB_OPT_SET_1, false, "ref-count", 'r', optional_argument, NULL, 0, eArgTypeWidth, "Display the reference count if the module is still in the shared module cache."},
3062 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003063 { LLDB_OPT_SET_1, false, "global", 'g', no_argument, NULL, 0, eArgTypeNone, "Display the modules from the global module list, not just the current target."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003064 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3065};
3066
3067
3068
3069//----------------------------------------------------------------------
3070// Lookup information in images
3071//----------------------------------------------------------------------
3072class CommandObjectTargetModulesLookup : public CommandObject
3073{
3074public:
3075
3076 enum
3077 {
3078 eLookupTypeInvalid = -1,
3079 eLookupTypeAddress = 0,
3080 eLookupTypeSymbol,
3081 eLookupTypeFileLine, // Line is optional
3082 eLookupTypeFunction,
3083 eLookupTypeType,
3084 kNumLookupTypes
3085 };
3086
3087 class CommandOptions : public Options
3088 {
3089 public:
3090
3091 CommandOptions (CommandInterpreter &interpreter) :
3092 Options(interpreter)
3093 {
3094 OptionParsingStarting();
3095 }
3096
3097 virtual
3098 ~CommandOptions ()
3099 {
3100 }
3101
3102 virtual Error
3103 SetOptionValue (uint32_t option_idx, const char *option_arg)
3104 {
3105 Error error;
3106
3107 char short_option = (char) m_getopt_table[option_idx].val;
3108
3109 switch (short_option)
3110 {
3111 case 'a':
3112 m_type = eLookupTypeAddress;
3113 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3114 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003115 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003116 break;
3117
3118 case 'o':
3119 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3120 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003121 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003122 break;
3123
3124 case 's':
3125 m_str = option_arg;
3126 m_type = eLookupTypeSymbol;
3127 break;
3128
3129 case 'f':
3130 m_file.SetFile (option_arg, false);
3131 m_type = eLookupTypeFileLine;
3132 break;
3133
3134 case 'i':
3135 m_check_inlines = false;
3136 break;
3137
3138 case 'l':
3139 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3140 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003141 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003142 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003143 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003144 m_type = eLookupTypeFileLine;
3145 break;
3146
3147 case 'n':
3148 m_str = option_arg;
3149 m_type = eLookupTypeFunction;
3150 break;
3151
3152 case 't':
3153 m_str = option_arg;
3154 m_type = eLookupTypeType;
3155 break;
3156
3157 case 'v':
3158 m_verbose = 1;
3159 break;
3160
3161 case 'r':
3162 m_use_regex = true;
3163 break;
3164 }
3165
3166 return error;
3167 }
3168
3169 void
3170 OptionParsingStarting ()
3171 {
3172 m_type = eLookupTypeInvalid;
3173 m_str.clear();
3174 m_file.Clear();
3175 m_addr = LLDB_INVALID_ADDRESS;
3176 m_offset = 0;
3177 m_line_number = 0;
3178 m_use_regex = false;
3179 m_check_inlines = true;
3180 m_verbose = false;
3181 }
3182
3183 const OptionDefinition*
3184 GetDefinitions ()
3185 {
3186 return g_option_table;
3187 }
3188
3189 // Options table: Required for subclasses of Options.
3190
3191 static OptionDefinition g_option_table[];
3192 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3193 std::string m_str; // Holds name lookup
3194 FileSpec m_file; // Files for file lookups
3195 lldb::addr_t m_addr; // Holds the address to lookup
3196 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3197 uint32_t m_line_number; // Line number for file+line lookups
3198 bool m_use_regex; // Name lookups in m_str are regular expressions.
3199 bool m_check_inlines;// Check for inline entries when looking up by file/line.
3200 bool m_verbose; // Enable verbose lookup info
3201
3202 };
3203
3204 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
3205 CommandObject (interpreter,
3206 "target modules lookup",
3207 "Look up information within executable and dependent shared library images.",
3208 NULL),
3209 m_options (interpreter)
3210 {
3211 CommandArgumentEntry arg;
3212 CommandArgumentData file_arg;
3213
3214 // Define the first (and only) variant of this arg.
3215 file_arg.arg_type = eArgTypeFilename;
3216 file_arg.arg_repetition = eArgRepeatStar;
3217
3218 // There is only one variant this argument could be; put it into the argument entry.
3219 arg.push_back (file_arg);
3220
3221 // Push the data for the first argument into the m_arguments vector.
3222 m_arguments.push_back (arg);
3223 }
3224
3225 virtual
3226 ~CommandObjectTargetModulesLookup ()
3227 {
3228 }
3229
3230 virtual Options *
3231 GetOptions ()
3232 {
3233 return &m_options;
3234 }
3235
3236
3237 bool
3238 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3239 {
3240 switch (m_options.m_type)
3241 {
3242 case eLookupTypeAddress:
3243 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3244 {
3245 if (LookupAddressInModule (m_interpreter,
3246 result.GetOutputStream(),
3247 module,
3248 eSymbolContextEverything,
3249 m_options.m_addr,
3250 m_options.m_offset,
3251 m_options.m_verbose))
3252 {
3253 result.SetStatus(eReturnStatusSuccessFinishResult);
3254 return true;
3255 }
3256 }
3257 break;
3258
3259 case eLookupTypeSymbol:
3260 if (!m_options.m_str.empty())
3261 {
3262 if (LookupSymbolInModule (m_interpreter, result.GetOutputStream(), module, m_options.m_str.c_str(), m_options.m_use_regex))
3263 {
3264 result.SetStatus(eReturnStatusSuccessFinishResult);
3265 return true;
3266 }
3267 }
3268 break;
3269
3270 case eLookupTypeFileLine:
3271 if (m_options.m_file)
3272 {
3273
3274 if (LookupFileAndLineInModule (m_interpreter,
3275 result.GetOutputStream(),
3276 module,
3277 m_options.m_file,
3278 m_options.m_line_number,
3279 m_options.m_check_inlines,
3280 m_options.m_verbose))
3281 {
3282 result.SetStatus(eReturnStatusSuccessFinishResult);
3283 return true;
3284 }
3285 }
3286 break;
3287
3288 case eLookupTypeFunction:
3289 if (!m_options.m_str.empty())
3290 {
3291 if (LookupFunctionInModule (m_interpreter,
3292 result.GetOutputStream(),
3293 module,
3294 m_options.m_str.c_str(),
3295 m_options.m_use_regex,
3296 m_options.m_verbose))
3297 {
3298 result.SetStatus(eReturnStatusSuccessFinishResult);
3299 return true;
3300 }
3301 }
3302 break;
3303
3304 case eLookupTypeType:
3305 if (!m_options.m_str.empty())
3306 {
3307 if (LookupTypeInModule (m_interpreter,
3308 result.GetOutputStream(),
3309 module,
3310 m_options.m_str.c_str(),
3311 m_options.m_use_regex))
3312 {
3313 result.SetStatus(eReturnStatusSuccessFinishResult);
3314 return true;
3315 }
3316 }
3317 break;
3318
3319 default:
3320 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3321 syntax_error = true;
3322 break;
3323 }
3324
3325 result.SetStatus (eReturnStatusFailed);
3326 return false;
3327 }
3328
3329 virtual bool
3330 Execute (Args& command,
3331 CommandReturnObject &result)
3332 {
3333 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3334 if (target == NULL)
3335 {
3336 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3337 result.SetStatus (eReturnStatusFailed);
3338 return false;
3339 }
3340 else
3341 {
3342 bool syntax_error = false;
3343 uint32_t i;
3344 uint32_t num_successful_lookups = 0;
3345 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3346 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3347 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3348 // Dump all sections for all modules images
3349
3350 if (command.GetArgumentCount() == 0)
3351 {
3352 // Dump all sections for all modules images
3353 const uint32_t num_modules = target->GetImages().GetSize();
3354 if (num_modules > 0)
3355 {
3356 for (i = 0; i<num_modules && syntax_error == false; ++i)
3357 {
3358 if (LookupInModule (m_interpreter, target->GetImages().GetModulePointerAtIndex(i), result, syntax_error))
3359 {
3360 result.GetOutputStream().EOL();
3361 num_successful_lookups++;
3362 }
3363 }
3364 }
3365 else
3366 {
3367 result.AppendError ("the target has no associated executable images");
3368 result.SetStatus (eReturnStatusFailed);
3369 return false;
3370 }
3371 }
3372 else
3373 {
3374 // Dump specified images (by basename or fullpath)
3375 const char *arg_cstr;
3376 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3377 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003378 ModuleList module_list;
3379 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3380 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003381 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003382 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003383 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003384 Module *module = module_list.GetModulePointerAtIndex(i);
3385 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003386 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003387 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003388 {
3389 result.GetOutputStream().EOL();
3390 num_successful_lookups++;
3391 }
3392 }
3393 }
3394 }
3395 else
3396 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3397 }
3398 }
3399
3400 if (num_successful_lookups > 0)
3401 result.SetStatus (eReturnStatusSuccessFinishResult);
3402 else
3403 result.SetStatus (eReturnStatusFailed);
3404 }
3405 return result.Succeeded();
3406 }
3407protected:
3408
3409 CommandOptions m_options;
3410};
3411
3412OptionDefinition
3413CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3414{
3415 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3416 { LLDB_OPT_SET_1, false, "offset", 'o', required_argument, NULL, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
Jim Inghame7a91c12011-06-20 23:38:11 +00003417 { LLDB_OPT_SET_2| LLDB_OPT_SET_4
3418 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_5 */ ,
3419 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003420 { LLDB_OPT_SET_2, true, "symbol", 's', required_argument, NULL, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003421 { LLDB_OPT_SET_3, true, "file", 'f', required_argument, NULL, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules."},
3422 { LLDB_OPT_SET_3, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
3423 { LLDB_OPT_SET_3, false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Check inline line entries (must be used in conjunction with --file)."},
3424 { LLDB_OPT_SET_4, true, "function", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
3425 { LLDB_OPT_SET_5, true, "type", 't', required_argument, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
3426 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
3427 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3428};
Chris Lattner24943d22010-06-08 16:52:24 +00003429
3430
Jim Inghamd60d94a2011-03-11 03:53:59 +00003431#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003432
3433//-------------------------------------------------------------------------
3434// CommandObjectMultiwordImageSearchPaths
3435//-------------------------------------------------------------------------
3436
Greg Claytone1f50b92011-05-03 22:09:39 +00003437class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003438{
3439public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003440
3441 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3442 CommandObjectMultiword (interpreter,
3443 "target modules search-paths",
3444 "A set of commands for operating on debugger target image search paths.",
3445 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003446 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003447 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3448 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3449 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3450 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3451 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003452 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003453
3454 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003455 {
3456 }
3457};
3458
Greg Claytone1f50b92011-05-03 22:09:39 +00003459
3460
3461#pragma mark CommandObjectTargetModules
3462
3463//-------------------------------------------------------------------------
3464// CommandObjectTargetModules
3465//-------------------------------------------------------------------------
3466
3467class CommandObjectTargetModules : public CommandObjectMultiword
3468{
3469public:
3470 //------------------------------------------------------------------
3471 // Constructors and Destructors
3472 //------------------------------------------------------------------
3473 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3474 CommandObjectMultiword (interpreter,
3475 "target modules",
3476 "A set of commands for accessing information for one or more target modules.",
3477 "target modules <sub-command> ...")
3478 {
3479 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3480 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3481 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3482 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3483 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3484 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3485 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
3486
3487 }
3488 virtual
3489 ~CommandObjectTargetModules()
3490 {
3491 }
3492
3493private:
3494 //------------------------------------------------------------------
3495 // For CommandObjectTargetModules only
3496 //------------------------------------------------------------------
3497 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3498};
3499
3500
Jim Inghamd60d94a2011-03-11 03:53:59 +00003501#pragma mark CommandObjectTargetStopHookAdd
3502
3503//-------------------------------------------------------------------------
3504// CommandObjectTargetStopHookAdd
3505//-------------------------------------------------------------------------
3506
3507class CommandObjectTargetStopHookAdd : public CommandObject
3508{
3509public:
3510
3511 class CommandOptions : public Options
3512 {
3513 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00003514 CommandOptions (CommandInterpreter &interpreter) :
3515 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00003516 m_line_start(0),
3517 m_line_end (UINT_MAX),
3518 m_func_name_type_mask (eFunctionNameTypeAuto),
3519 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00003520 m_thread_specified (false),
3521 m_use_one_liner (false),
3522 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003523 {
3524 }
3525
3526 ~CommandOptions () {}
3527
Greg Claytonb3448432011-03-24 21:19:54 +00003528 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00003529 GetDefinitions ()
3530 {
3531 return g_option_table;
3532 }
3533
3534 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00003535 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003536 {
3537 Error error;
3538 char short_option = (char) m_getopt_table[option_idx].val;
3539 bool success;
3540
3541 switch (short_option)
3542 {
3543 case 'c':
3544 m_class_name = option_arg;
3545 m_sym_ctx_specified = true;
3546 break;
3547
3548 case 'e':
3549 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
3550 if (!success)
3551 {
Greg Clayton9c236732011-10-26 00:56:27 +00003552 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003553 break;
3554 }
3555 m_sym_ctx_specified = true;
3556 break;
3557
3558 case 'l':
3559 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
3560 if (!success)
3561 {
Greg Clayton9c236732011-10-26 00:56:27 +00003562 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003563 break;
3564 }
3565 m_sym_ctx_specified = true;
3566 break;
3567
3568 case 'n':
3569 m_function_name = option_arg;
3570 m_func_name_type_mask |= eFunctionNameTypeAuto;
3571 m_sym_ctx_specified = true;
3572 break;
3573
3574 case 'f':
3575 m_file_name = option_arg;
3576 m_sym_ctx_specified = true;
3577 break;
3578 case 's':
3579 m_module_name = option_arg;
3580 m_sym_ctx_specified = true;
3581 break;
3582 case 't' :
3583 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003584 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003585 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00003586 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003587 m_thread_specified = true;
3588 }
3589 break;
3590 case 'T':
3591 m_thread_name = option_arg;
3592 m_thread_specified = true;
3593 break;
3594 case 'q':
3595 m_queue_name = option_arg;
3596 m_thread_specified = true;
3597 break;
3598 case 'x':
3599 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003600 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003601 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003602 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003603 m_thread_specified = true;
3604 }
3605 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003606 case 'o':
3607 m_use_one_liner = true;
3608 m_one_liner = option_arg;
3609 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003610 default:
Greg Clayton9c236732011-10-26 00:56:27 +00003611 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003612 break;
3613 }
3614 return error;
3615 }
3616
3617 void
Greg Clayton143fcc32011-04-13 00:18:08 +00003618 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003619 {
3620 m_class_name.clear();
3621 m_function_name.clear();
3622 m_line_start = 0;
3623 m_line_end = UINT_MAX;
3624 m_file_name.clear();
3625 m_module_name.clear();
3626 m_func_name_type_mask = eFunctionNameTypeAuto;
3627 m_thread_id = LLDB_INVALID_THREAD_ID;
3628 m_thread_index = UINT32_MAX;
3629 m_thread_name.clear();
3630 m_queue_name.clear();
3631
3632 m_sym_ctx_specified = false;
3633 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003634
3635 m_use_one_liner = false;
3636 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003637 }
3638
3639
Greg Claytonb3448432011-03-24 21:19:54 +00003640 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00003641
3642 std::string m_class_name;
3643 std::string m_function_name;
3644 uint32_t m_line_start;
3645 uint32_t m_line_end;
3646 std::string m_file_name;
3647 std::string m_module_name;
3648 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
3649 lldb::tid_t m_thread_id;
3650 uint32_t m_thread_index;
3651 std::string m_thread_name;
3652 std::string m_queue_name;
3653 bool m_sym_ctx_specified;
3654 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003655 // Instance variables to hold the values for one_liner options.
3656 bool m_use_one_liner;
3657 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003658 };
3659
3660 Options *
3661 GetOptions ()
3662 {
3663 return &m_options;
3664 }
3665
3666 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
3667 CommandObject (interpreter,
3668 "target stop-hook add ",
3669 "Add a hook to be executed when the target stops.",
Greg Claytonf15996e2011-04-07 22:46:35 +00003670 "target stop-hook add"),
3671 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003672 {
3673 }
3674
3675 ~CommandObjectTargetStopHookAdd ()
3676 {
3677 }
3678
3679 static size_t
3680 ReadCommandsCallbackFunction (void *baton,
3681 InputReader &reader,
3682 lldb::InputReaderAction notification,
3683 const char *bytes,
3684 size_t bytes_len)
3685 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003686 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003687 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00003688 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00003689 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003690
3691 switch (notification)
3692 {
3693 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00003694 if (!batch_mode)
3695 {
3696 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
3697 if (reader.GetPrompt())
3698 out_stream->Printf ("%s", reader.GetPrompt());
3699 out_stream->Flush();
3700 }
Jim Inghame15511a2011-05-05 01:03:36 +00003701 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003702 break;
3703
3704 case eInputReaderDeactivate:
3705 break;
3706
3707 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00003708 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003709 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003710 out_stream->Printf ("%s", reader.GetPrompt());
3711 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003712 }
Jim Inghame15511a2011-05-05 01:03:36 +00003713 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003714 break;
3715
Caroline Tice4a348082011-05-02 20:41:46 +00003716 case eInputReaderAsynchronousOutputWritten:
3717 break;
3718
Jim Inghamd60d94a2011-03-11 03:53:59 +00003719 case eInputReaderGotToken:
3720 if (bytes && bytes_len && baton)
3721 {
3722 StringList *commands = new_stop_hook->GetCommandPointer();
3723 if (commands)
3724 {
3725 commands->AppendString (bytes, bytes_len);
3726 }
3727 }
Caroline Tice892fadd2011-06-16 16:27:19 +00003728 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003729 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003730 out_stream->Printf ("%s", reader.GetPrompt());
3731 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003732 }
3733 break;
3734
3735 case eInputReaderInterrupt:
3736 {
3737 // Finish, and cancel the stop hook.
3738 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00003739 if (!batch_mode)
3740 {
3741 out_stream->Printf ("Stop hook cancelled.\n");
3742 out_stream->Flush();
3743 }
3744
Jim Inghamd60d94a2011-03-11 03:53:59 +00003745 reader.SetIsDone (true);
3746 }
Jim Inghame15511a2011-05-05 01:03:36 +00003747 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003748 break;
3749
3750 case eInputReaderEndOfFile:
3751 reader.SetIsDone (true);
3752 break;
3753
3754 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00003755 if (!got_interrupted && !batch_mode)
3756 {
Greg Clayton444e35b2011-10-19 18:09:39 +00003757 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00003758 out_stream->Flush();
3759 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00003760 break;
3761 }
3762
3763 return bytes_len;
3764 }
3765
3766 bool
3767 Execute (Args& command,
3768 CommandReturnObject &result)
3769 {
3770 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3771 if (target)
3772 {
3773 Target::StopHookSP new_hook_sp;
3774 target->AddStopHook (new_hook_sp);
3775
3776 // First step, make the specifier.
3777 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
3778 if (m_options.m_sym_ctx_specified)
3779 {
3780 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
3781
3782 if (!m_options.m_module_name.empty())
3783 {
3784 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
3785 }
3786
3787 if (!m_options.m_class_name.empty())
3788 {
3789 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
3790 }
3791
3792 if (!m_options.m_file_name.empty())
3793 {
3794 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
3795 }
3796
3797 if (m_options.m_line_start != 0)
3798 {
3799 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
3800 }
3801
3802 if (m_options.m_line_end != UINT_MAX)
3803 {
3804 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
3805 }
3806
3807 if (!m_options.m_function_name.empty())
3808 {
3809 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
3810 }
3811 }
3812
3813 if (specifier_ap.get())
3814 new_hook_sp->SetSpecifier (specifier_ap.release());
3815
3816 // Next see if any of the thread options have been entered:
3817
3818 if (m_options.m_thread_specified)
3819 {
3820 ThreadSpec *thread_spec = new ThreadSpec();
3821
3822 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
3823 {
3824 thread_spec->SetTID (m_options.m_thread_id);
3825 }
3826
3827 if (m_options.m_thread_index != UINT32_MAX)
3828 thread_spec->SetIndex (m_options.m_thread_index);
3829
3830 if (!m_options.m_thread_name.empty())
3831 thread_spec->SetName (m_options.m_thread_name.c_str());
3832
3833 if (!m_options.m_queue_name.empty())
3834 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
3835
3836 new_hook_sp->SetThreadSpecifier (thread_spec);
3837
3838 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00003839 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003840 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00003841 // Use one-liner.
3842 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00003843 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00003844 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00003845 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00003846 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00003847 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
3848 // the new stop hook's command string.
3849 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
3850 if (!reader_sp)
3851 {
3852 result.AppendError("out of memory\n");
3853 result.SetStatus (eReturnStatusFailed);
3854 target->RemoveStopHookByID (new_hook_sp->GetID());
3855 return false;
3856 }
3857
3858 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
3859 new_hook_sp.get(), // baton
3860 eInputReaderGranularityLine, // token size, to pass to callback function
3861 "DONE", // end token
3862 "> ", // prompt
3863 true)); // echo input
3864 if (!err.Success())
3865 {
3866 result.AppendError (err.AsCString());
3867 result.SetStatus (eReturnStatusFailed);
3868 target->RemoveStopHookByID (new_hook_sp->GetID());
3869 return false;
3870 }
3871 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003872 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00003873 result.SetStatus (eReturnStatusSuccessFinishNoResult);
3874 }
3875 else
3876 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00003877 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00003878 result.SetStatus (eReturnStatusFailed);
3879 }
3880
3881 return result.Succeeded();
3882 }
3883private:
3884 CommandOptions m_options;
3885};
3886
Greg Claytonb3448432011-03-24 21:19:54 +00003887OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00003888CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
3889{
Johnny Chen60fe60e2011-05-02 23:47:55 +00003890 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
3891 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00003892 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
3893 "Set the module within which the stop-hook is to be run."},
3894 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
3895 "The stop hook is run only for the thread whose index matches this argument."},
3896 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
3897 "The stop hook is run only for the thread whose TID matches this argument."},
3898 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
3899 "The stop hook is run only for the thread whose thread name matches this argument."},
3900 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
3901 "The stop hook is run only for threads in the queue whose name is given by this argument."},
3902 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
3903 "Specify the source file within which the stop-hook is to be run." },
3904 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
3905 "Set the start of the line range for which the stop-hook is to be run."},
3906 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
3907 "Set the end of the line range for which the stop-hook is to be run."},
3908 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
3909 "Specify the class within which the stop-hook is to be run." },
3910 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
3911 "Set the function name within which the stop hook will be run." },
3912 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3913};
3914
3915#pragma mark CommandObjectTargetStopHookDelete
3916
3917//-------------------------------------------------------------------------
3918// CommandObjectTargetStopHookDelete
3919//-------------------------------------------------------------------------
3920
3921class CommandObjectTargetStopHookDelete : public CommandObject
3922{
3923public:
3924
3925 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
3926 CommandObject (interpreter,
Jason Molenda3fccc902011-11-10 23:03:44 +00003927 "target stop-hook delete",
Jim Inghamd60d94a2011-03-11 03:53:59 +00003928 "Delete a stop-hook.",
Jason Molenda3fccc902011-11-10 23:03:44 +00003929 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00003930 {
3931 }
3932
3933 ~CommandObjectTargetStopHookDelete ()
3934 {
3935 }
3936
3937 bool
3938 Execute (Args& command,
3939 CommandReturnObject &result)
3940 {
3941 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3942 if (target)
3943 {
3944 // FIXME: see if we can use the breakpoint id style parser?
3945 size_t num_args = command.GetArgumentCount();
3946 if (num_args == 0)
3947 {
3948 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
3949 {
3950 result.SetStatus (eReturnStatusFailed);
3951 return false;
3952 }
3953 else
3954 {
3955 target->RemoveAllStopHooks();
3956 }
3957 }
3958 else
3959 {
3960 bool success;
3961 for (size_t i = 0; i < num_args; i++)
3962 {
3963 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
3964 if (!success)
3965 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00003966 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00003967 result.SetStatus(eReturnStatusFailed);
3968 return false;
3969 }
3970 success = target->RemoveStopHookByID (user_id);
3971 if (!success)
3972 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00003973 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00003974 result.SetStatus(eReturnStatusFailed);
3975 return false;
3976 }
3977 }
3978 }
3979 result.SetStatus (eReturnStatusSuccessFinishNoResult);
3980 }
3981 else
3982 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00003983 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00003984 result.SetStatus (eReturnStatusFailed);
3985 }
3986
3987 return result.Succeeded();
3988 }
3989};
3990#pragma mark CommandObjectTargetStopHookEnableDisable
3991
3992//-------------------------------------------------------------------------
3993// CommandObjectTargetStopHookEnableDisable
3994//-------------------------------------------------------------------------
3995
3996class CommandObjectTargetStopHookEnableDisable : public CommandObject
3997{
3998public:
3999
4000 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
4001 CommandObject (interpreter,
4002 name,
4003 help,
4004 syntax),
4005 m_enable (enable)
4006 {
4007 }
4008
4009 ~CommandObjectTargetStopHookEnableDisable ()
4010 {
4011 }
4012
4013 bool
4014 Execute (Args& command,
4015 CommandReturnObject &result)
4016 {
4017 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4018 if (target)
4019 {
4020 // FIXME: see if we can use the breakpoint id style parser?
4021 size_t num_args = command.GetArgumentCount();
4022 bool success;
4023
4024 if (num_args == 0)
4025 {
4026 target->SetAllStopHooksActiveState (m_enable);
4027 }
4028 else
4029 {
4030 for (size_t i = 0; i < num_args; i++)
4031 {
4032 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4033 if (!success)
4034 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004035 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004036 result.SetStatus(eReturnStatusFailed);
4037 return false;
4038 }
4039 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4040 if (!success)
4041 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004042 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004043 result.SetStatus(eReturnStatusFailed);
4044 return false;
4045 }
4046 }
4047 }
4048 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4049 }
4050 else
4051 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004052 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004053 result.SetStatus (eReturnStatusFailed);
4054 }
4055 return result.Succeeded();
4056 }
4057private:
4058 bool m_enable;
4059};
4060
4061#pragma mark CommandObjectTargetStopHookList
4062
4063//-------------------------------------------------------------------------
4064// CommandObjectTargetStopHookList
4065//-------------------------------------------------------------------------
4066
4067class CommandObjectTargetStopHookList : public CommandObject
4068{
4069public:
4070
4071 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
4072 CommandObject (interpreter,
Jason Molenda3fccc902011-11-10 23:03:44 +00004073 "target stop-hook list",
Jim Inghamd60d94a2011-03-11 03:53:59 +00004074 "List all stop-hooks.",
Jason Molenda3fccc902011-11-10 23:03:44 +00004075 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004076 {
4077 }
4078
4079 ~CommandObjectTargetStopHookList ()
4080 {
4081 }
4082
4083 bool
4084 Execute (Args& command,
4085 CommandReturnObject &result)
4086 {
4087 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004088 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004089 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004090 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004091 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004092 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004093 }
4094
4095 size_t num_hooks = target->GetNumStopHooks ();
4096 if (num_hooks == 0)
4097 {
4098 result.GetOutputStream().PutCString ("No stop hooks.\n");
4099 }
4100 else
4101 {
4102 for (size_t i = 0; i < num_hooks; i++)
4103 {
4104 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4105 if (i > 0)
4106 result.GetOutputStream().PutCString ("\n");
4107 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4108 }
4109 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004110 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004111 return result.Succeeded();
4112 }
4113};
4114
4115#pragma mark CommandObjectMultiwordTargetStopHooks
4116//-------------------------------------------------------------------------
4117// CommandObjectMultiwordTargetStopHooks
4118//-------------------------------------------------------------------------
4119
4120class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4121{
4122public:
4123
4124 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4125 CommandObjectMultiword (interpreter,
4126 "target stop-hook",
4127 "A set of commands for operating on debugger target stop-hooks.",
4128 "target stop-hook <subcommand> [<subcommand-options>]")
4129 {
4130 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4131 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4132 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4133 false,
4134 "target stop-hook disable [<id>]",
4135 "Disable a stop-hook.",
4136 "target stop-hook disable")));
4137 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4138 true,
4139 "target stop-hook enable [<id>]",
4140 "Enable a stop-hook.",
4141 "target stop-hook enable")));
4142 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4143 }
4144
4145 ~CommandObjectMultiwordTargetStopHooks()
4146 {
4147 }
4148};
4149
4150
Chris Lattner24943d22010-06-08 16:52:24 +00004151
4152#pragma mark CommandObjectMultiwordTarget
4153
4154//-------------------------------------------------------------------------
4155// CommandObjectMultiwordTarget
4156//-------------------------------------------------------------------------
4157
Greg Clayton63094e02010-06-23 01:19:29 +00004158CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004159 CommandObjectMultiword (interpreter,
4160 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004161 "A set of commands for operating on debugger targets.",
4162 "target <subcommand> [<subcommand-options>]")
4163{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004164
4165 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004166 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004167 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4168 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004169 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004170 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004171 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004172}
4173
4174CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4175{
4176}
4177
Greg Claytonabe0fed2011-04-18 08:33:37 +00004178