blob: 17d5fd53b1c08db1b2542d8994aea75e80c0fe91 [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 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000214 char core_path[PATH_MAX];
215 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000216 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000217 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000218 FileSpec core_file_dir;
219 core_file_dir.GetDirectory() = core_file.GetDirectory();
220 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000221
Greg Clayton9ce95382012-02-13 23:10:39 +0000222 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
223
224 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000225 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000226 // Seems wierd that we Launch a core file, but that is
227 // what we do!
228 error = process_sp->LoadCore();
229
230 if (error.Fail())
231 {
232 result.AppendError(error.AsCString("can't find plug-in for core file"));
233 result.SetStatus (eReturnStatusFailed);
234 return false;
235 }
236 else
237 {
238 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
239 result.SetStatus (eReturnStatusSuccessFinishNoResult);
240 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000241 }
242 else
243 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000244 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
245 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000246 }
247 }
248 else
249 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000250 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000251 result.SetStatus (eReturnStatusFailed);
252 }
253 }
254 else
255 {
256 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
257 result.SetStatus (eReturnStatusSuccessFinishNoResult);
258 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000259 }
260 else
261 {
262 result.AppendError(error.AsCString());
263 result.SetStatus (eReturnStatusFailed);
264 }
265 }
266 else
267 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000268 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 +0000269 result.SetStatus (eReturnStatusFailed);
270 }
271 return result.Succeeded();
272
273 }
274
275 int
276 HandleArgumentCompletion (Args &input,
277 int &cursor_index,
278 int &cursor_char_position,
279 OptionElementVector &opt_element_vector,
280 int match_start_point,
281 int max_return_elements,
282 bool &word_complete,
283 StringList &matches)
284 {
285 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
286 completion_str.erase (cursor_char_position);
287
288 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
289 CommandCompletions::eDiskFileCompletion,
290 completion_str.c_str(),
291 match_start_point,
292 max_return_elements,
293 NULL,
294 word_complete,
295 matches);
296 return matches.GetSize();
297 }
298private:
299 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000300 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000301 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000302 OptionGroupFile m_core_file;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000303
304};
305
306#pragma mark CommandObjectTargetList
307
308//----------------------------------------------------------------------
309// "target list"
310//----------------------------------------------------------------------
311
312class CommandObjectTargetList : public CommandObject
313{
314public:
315 CommandObjectTargetList (CommandInterpreter &interpreter) :
316 CommandObject (interpreter,
317 "target list",
318 "List all current targets in the current debug session.",
319 NULL,
320 0)
321 {
322 }
323
324 virtual
325 ~CommandObjectTargetList ()
326 {
327 }
328
329 virtual bool
330 Execute (Args& args, CommandReturnObject &result)
331 {
332 if (args.GetArgumentCount() == 0)
333 {
334 Stream &strm = result.GetOutputStream();
335
336 bool show_stopped_process_status = false;
337 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
338 {
339 strm.PutCString ("No targets.\n");
340 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000341 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000342 }
343 else
344 {
345 result.AppendError ("the 'target list' command takes no arguments\n");
346 result.SetStatus (eReturnStatusFailed);
347 }
348 return result.Succeeded();
349 }
350};
351
352
353#pragma mark CommandObjectTargetSelect
354
355//----------------------------------------------------------------------
356// "target select"
357//----------------------------------------------------------------------
358
359class CommandObjectTargetSelect : public CommandObject
360{
361public:
362 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
363 CommandObject (interpreter,
364 "target select",
365 "Select a target as the current target by target index.",
366 NULL,
367 0)
368 {
369 }
370
371 virtual
372 ~CommandObjectTargetSelect ()
373 {
374 }
375
376 virtual bool
377 Execute (Args& args, CommandReturnObject &result)
378 {
379 if (args.GetArgumentCount() == 1)
380 {
381 bool success = false;
382 const char *target_idx_arg = args.GetArgumentAtIndex(0);
383 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
384 if (success)
385 {
386 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
387 const uint32_t num_targets = target_list.GetNumTargets();
388 if (target_idx < num_targets)
389 {
390 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
391 if (target_sp)
392 {
393 Stream &strm = result.GetOutputStream();
394 target_list.SetSelectedTarget (target_sp.get());
395 bool show_stopped_process_status = false;
396 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000397 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000398 }
399 else
400 {
401 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
402 result.SetStatus (eReturnStatusFailed);
403 }
404 }
405 else
406 {
407 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
408 target_idx,
409 num_targets - 1);
410 result.SetStatus (eReturnStatusFailed);
411 }
412 }
413 else
414 {
415 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
416 result.SetStatus (eReturnStatusFailed);
417 }
418 }
419 else
420 {
421 result.AppendError ("'target select' takes a single argument: a target index\n");
422 result.SetStatus (eReturnStatusFailed);
423 }
424 return result.Succeeded();
425 }
426};
427
Greg Clayton153ccd72011-08-10 02:10:13 +0000428#pragma mark CommandObjectTargetSelect
429
430//----------------------------------------------------------------------
431// "target delete"
432//----------------------------------------------------------------------
433
434class CommandObjectTargetDelete : public CommandObject
435{
436public:
437 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Greg Clayton5beb99d2011-08-11 02:48:45 +0000438 CommandObject (interpreter,
439 "target delete",
440 "Delete one or more targets by target index.",
441 NULL,
442 0),
443 m_option_group (interpreter),
444 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 +0000445 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000446 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
447 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000448 }
449
450 virtual
451 ~CommandObjectTargetDelete ()
452 {
453 }
454
455 virtual bool
456 Execute (Args& args, CommandReturnObject &result)
457 {
458 const size_t argc = args.GetArgumentCount();
459 std::vector<TargetSP> delete_target_list;
460 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
461 bool success = true;
462 TargetSP target_sp;
463 if (argc > 0)
464 {
465 const uint32_t num_targets = target_list.GetNumTargets();
466 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
467 {
468 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
469 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
470 if (success)
471 {
472 if (target_idx < num_targets)
473 {
474 target_sp = target_list.GetTargetAtIndex (target_idx);
475 if (target_sp)
476 {
477 delete_target_list.push_back (target_sp);
478 continue;
479 }
480 }
481 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
482 target_idx,
483 num_targets - 1);
484 result.SetStatus (eReturnStatusFailed);
485 success = false;
486 }
487 else
488 {
489 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
490 result.SetStatus (eReturnStatusFailed);
491 success = false;
492 }
493 }
494
495 }
496 else
497 {
498 target_sp = target_list.GetSelectedTarget();
499 if (target_sp)
500 {
501 delete_target_list.push_back (target_sp);
502 }
503 else
504 {
505 result.AppendErrorWithFormat("no target is currently selected\n");
506 result.SetStatus (eReturnStatusFailed);
507 success = false;
508 }
509 }
510 if (success)
511 {
512 const size_t num_targets_to_delete = delete_target_list.size();
513 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
514 {
515 target_sp = delete_target_list[idx];
516 target_list.DeleteTarget(target_sp);
517 target_sp->Destroy();
518 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000519 // If "--clean" was specified, prune any orphaned shared modules from
520 // the global shared module list
521 if (m_cleanup_option.GetOptionValue ())
522 {
523 ModuleList::RemoveOrphanSharedModules();
524 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000525 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
526 result.SetStatus(eReturnStatusSuccessFinishResult);
527 }
528
529 return result.Succeeded();
530 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000531
532 Options *
533 GetOptions ()
534 {
535 return &m_option_group;
536 }
537
538protected:
539 OptionGroupOptions m_option_group;
540 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000541};
542
Greg Claytonabe0fed2011-04-18 08:33:37 +0000543
Greg Clayton801417e2011-07-07 01:59:51 +0000544#pragma mark CommandObjectTargetVariable
545
546//----------------------------------------------------------------------
547// "target variable"
548//----------------------------------------------------------------------
549
550class CommandObjectTargetVariable : public CommandObject
551{
552public:
553 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
554 CommandObject (interpreter,
Johnny Chen34bfa4f2011-07-12 22:34:30 +0000555 "target variable",
556 "Read global variable(s) prior to running your binary.",
Greg Clayton801417e2011-07-07 01:59:51 +0000557 NULL,
558 0),
559 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000560 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000561 m_option_format (eFormatDefault),
Greg Clayton801417e2011-07-07 01:59:51 +0000562 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."),
563 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."),
564 m_varobj_options()
565 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000566 CommandArgumentEntry arg;
567 CommandArgumentData var_name_arg;
568
569 // Define the first (and only) variant of this arg.
570 var_name_arg.arg_type = eArgTypeVarName;
571 var_name_arg.arg_repetition = eArgRepeatPlus;
572
573 // There is only one variant this argument could be; put it into the argument entry.
574 arg.push_back (var_name_arg);
575
576 // Push the data for the first argument into the m_arguments vector.
577 m_arguments.push_back (arg);
578
Greg Clayton801417e2011-07-07 01:59:51 +0000579 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000580 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000581 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 +0000582 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
583 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
584 m_option_group.Finalize();
585 }
586
587 virtual
588 ~CommandObjectTargetVariable ()
589 {
590 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000591
592 void
593 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
594 {
Enrico Granata19030d82011-08-15 18:01:31 +0000595 ValueObject::DumpValueObjectOptions options;
596
Enrico Granata3069c622012-03-01 04:24:26 +0000597 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000598 .SetMaximumDepth(m_varobj_options.max_depth)
599 .SetShowTypes(m_varobj_options.show_types)
600 .SetShowLocation(m_varobj_options.show_location)
601 .SetUseObjectiveC(m_varobj_options.use_objc)
602 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000603 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000604 .SetFlatOutput(m_varobj_options.flat_output)
605 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
606 .SetIgnoreCap(m_varobj_options.ignore_cap);
607
Greg Clayton5d81f492011-07-08 21:46:14 +0000608 switch (var_sp->GetScope())
609 {
610 case eValueTypeVariableGlobal:
611 if (m_option_variable.show_scope)
612 s.PutCString("GLOBAL: ");
613 break;
614
615 case eValueTypeVariableStatic:
616 if (m_option_variable.show_scope)
617 s.PutCString("STATIC: ");
618 break;
619
620 case eValueTypeVariableArgument:
621 if (m_option_variable.show_scope)
622 s.PutCString(" ARG: ");
623 break;
624
625 case eValueTypeVariableLocal:
626 if (m_option_variable.show_scope)
627 s.PutCString(" LOCAL: ");
628 break;
629
630 default:
631 break;
632 }
633
Greg Claytonfb816422011-07-10 19:21:23 +0000634 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000635 {
Greg Claytonfb816422011-07-10 19:21:23 +0000636 bool show_fullpaths = false;
637 bool show_module = true;
638 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
639 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000640 }
641
Greg Claytona42880a2011-10-25 06:44:01 +0000642 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000643 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000644 options.SetFormat(format);
645
646 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000647
648 ValueObject::DumpValueObject (s,
649 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000650 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000651
652 }
Greg Clayton801417e2011-07-07 01:59:51 +0000653
Greg Clayton5d81f492011-07-08 21:46:14 +0000654
655 static uint32_t GetVariableCallback (void *baton,
656 const char *name,
657 VariableList &variable_list)
658 {
659 Target *target = static_cast<Target *>(baton);
660 if (target)
661 {
662 return target->GetImages().FindGlobalVariables (ConstString(name),
663 true,
664 UINT32_MAX,
665 variable_list);
666 }
667 return 0;
668 }
669
670
671
Greg Clayton801417e2011-07-07 01:59:51 +0000672 virtual bool
673 Execute (Args& args, CommandReturnObject &result)
674 {
675 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000676 Target *target = exe_ctx.GetTargetPtr();
677 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000678 {
679 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000680 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000681 if (argc > 0)
682 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000683
Greg Clayton801417e2011-07-07 01:59:51 +0000684 for (size_t idx = 0; idx < argc; ++idx)
685 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000686 VariableList variable_list;
687 ValueObjectList valobj_list;
688
Greg Clayton368f8222011-07-07 04:38:25 +0000689 const char *arg = args.GetArgumentAtIndex(idx);
690 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000691 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000692 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000693 {
Greg Clayton368f8222011-07-07 04:38:25 +0000694 RegularExpression regex(arg);
695 if (!regex.IsValid ())
696 {
697 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
698 result.SetStatus (eReturnStatusFailed);
699 return false;
700 }
Greg Claytonfb816422011-07-10 19:21:23 +0000701 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000702 matches = target->GetImages().FindGlobalVariables (regex,
703 true,
704 UINT32_MAX,
705 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000706 }
707 else
708 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000709 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000710 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000711 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000712 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000713 variable_list,
714 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000715 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000716 }
717
718 if (matches == 0)
719 {
720 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
721 result.SetStatus (eReturnStatusFailed);
722 return false;
723 }
724 else
725 {
Greg Clayton801417e2011-07-07 01:59:51 +0000726 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
727 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000728 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000729 if (var_sp)
730 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000731 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
732 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000733 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000734
735 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000736 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000737 }
738 }
739 }
740 }
741 }
742 else
743 {
Greg Claytonfac93882011-10-05 22:17:32 +0000744 bool success = false;
745 StackFrame *frame = exe_ctx.GetFramePtr();
746 CompileUnit *comp_unit = NULL;
747 if (frame)
748 {
749 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
750 if (comp_unit)
751 {
752 const bool can_create = true;
753 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
754 if (comp_unit_varlist_sp)
755 {
756 size_t count = comp_unit_varlist_sp->GetSize();
757 if (count > 0)
758 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000759 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000760 comp_unit->GetDirectory().GetCString(),
761 comp_unit->GetFilename().GetCString());
762
763 success = true;
764 for (uint32_t i=0; i<count; ++i)
765 {
766 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
767 if (var_sp)
768 {
769 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
770
771 if (valobj_sp)
772 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
773 }
774 }
775 }
776 }
777 }
778 }
779 if (!success)
780 {
781 if (frame)
782 {
783 if (comp_unit)
784 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
785 comp_unit->GetDirectory().GetCString(),
786 comp_unit->GetFilename().GetCString());
787 else
788 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
789 }
790 else
791 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
792 result.SetStatus (eReturnStatusFailed);
793 }
Greg Clayton801417e2011-07-07 01:59:51 +0000794 }
795 }
796 else
797 {
798 result.AppendError ("invalid target, create a debug target using the 'target create' command");
799 result.SetStatus (eReturnStatusFailed);
800 return false;
801 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000802
803 if (m_interpreter.TruncationWarningNecessary())
804 {
805 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
806 m_cmd_name.c_str());
807 m_interpreter.TruncationWarningGiven();
808 }
809
Greg Clayton801417e2011-07-07 01:59:51 +0000810 return result.Succeeded();
811 }
812
813 Options *
814 GetOptions ()
815 {
816 return &m_option_group;
817 }
818
819protected:
820 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000821 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000822 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000823 OptionGroupFileList m_option_compile_units;
824 OptionGroupFileList m_option_shared_libraries;
825 OptionGroupValueObjectDisplay m_varobj_options;
826
827};
828
829
Greg Claytone1f50b92011-05-03 22:09:39 +0000830#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000831
Greg Claytone1f50b92011-05-03 22:09:39 +0000832class CommandObjectTargetModulesSearchPathsAdd : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000833{
834public:
835
Greg Claytone1f50b92011-05-03 22:09:39 +0000836 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000837 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000838 "target modules search-paths add",
Chris Lattner24943d22010-06-08 16:52:24 +0000839 "Add new image search paths substitution pairs to the current target.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000840 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000841 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000842 CommandArgumentEntry arg;
843 CommandArgumentData old_prefix_arg;
844 CommandArgumentData new_prefix_arg;
845
846 // Define the first variant of this arg pair.
847 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
848 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
849
850 // Define the first variant of this arg pair.
851 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
852 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
853
854 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
855 // must always occur together, they are treated as two variants of one argument rather than two independent
856 // arguments. Push them both into the first argument position for m_arguments...
857
858 arg.push_back (old_prefix_arg);
859 arg.push_back (new_prefix_arg);
860
861 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000862 }
863
Greg Claytone1f50b92011-05-03 22:09:39 +0000864 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000865 {
866 }
867
868 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000869 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000870 CommandReturnObject &result)
871 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000872 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000873 if (target)
874 {
875 uint32_t argc = command.GetArgumentCount();
876 if (argc & 1)
877 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000878 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000879 result.SetStatus (eReturnStatusFailed);
880 }
881 else
882 {
883 for (uint32_t i=0; i<argc; i+=2)
884 {
885 const char *from = command.GetArgumentAtIndex(i);
886 const char *to = command.GetArgumentAtIndex(i+1);
887
888 if (from[0] && to[0])
889 {
890 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000891 target->GetImageSearchPathList().Append (ConstString(from),
892 ConstString(to),
893 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000894 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000895 }
896 else
897 {
898 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000899 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000900 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000901 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000902 result.SetStatus (eReturnStatusFailed);
903 }
904 }
905 }
906 }
907 else
908 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000909 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000910 result.SetStatus (eReturnStatusFailed);
911 }
912 return result.Succeeded();
913 }
914};
915
Greg Claytone1f50b92011-05-03 22:09:39 +0000916#pragma mark CommandObjectTargetModulesSearchPathsClear
917
918class CommandObjectTargetModulesSearchPathsClear : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000919{
920public:
921
Greg Claytone1f50b92011-05-03 22:09:39 +0000922 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000923 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000924 "target modules search-paths clear",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000925 "Clear all current image search path substitution pairs from the current target.",
Greg Claytone1f50b92011-05-03 22:09:39 +0000926 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000927 {
928 }
929
Greg Claytone1f50b92011-05-03 22:09:39 +0000930 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000931 {
932 }
933
934 bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000935 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000936 CommandReturnObject &result)
937 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000938 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000939 if (target)
940 {
941 bool notify = true;
942 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000943 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000944 }
945 else
946 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000947 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000948 result.SetStatus (eReturnStatusFailed);
949 }
950 return result.Succeeded();
951 }
952};
953
Greg Claytone1f50b92011-05-03 22:09:39 +0000954#pragma mark CommandObjectTargetModulesSearchPathsInsert
955
956class CommandObjectTargetModulesSearchPathsInsert : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +0000957{
958public:
959
Greg Claytone1f50b92011-05-03 22:09:39 +0000960 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000961 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +0000962 "target modules search-paths insert",
Caroline Ticeabb507a2010-09-08 21:06:11 +0000963 "Insert a new image search path substitution pair into the current target at the specified index.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000964 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000965 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000966 CommandArgumentEntry arg1;
967 CommandArgumentEntry arg2;
968 CommandArgumentData index_arg;
969 CommandArgumentData old_prefix_arg;
970 CommandArgumentData new_prefix_arg;
971
972 // Define the first and only variant of this arg.
973 index_arg.arg_type = eArgTypeIndex;
974 index_arg.arg_repetition = eArgRepeatPlain;
975
976 // Put the one and only variant into the first arg for m_arguments:
977 arg1.push_back (index_arg);
978
979 // Define the first variant of this arg pair.
980 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
981 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
982
983 // Define the first variant of this arg pair.
984 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
985 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
986
987 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
988 // must always occur together, they are treated as two variants of one argument rather than two independent
989 // arguments. Push them both into the same argument position for m_arguments...
990
991 arg2.push_back (old_prefix_arg);
992 arg2.push_back (new_prefix_arg);
993
994 // Add arguments to m_arguments.
995 m_arguments.push_back (arg1);
996 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +0000997 }
998
Greg Claytone1f50b92011-05-03 22:09:39 +0000999 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001000 {
1001 }
1002
1003 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001004 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001005 CommandReturnObject &result)
1006 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001007 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001008 if (target)
1009 {
1010 uint32_t argc = command.GetArgumentCount();
1011 // check for at least 3 arguments and an odd nubmer of parameters
1012 if (argc >= 3 && argc & 1)
1013 {
1014 bool success = false;
1015
1016 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1017
1018 if (!success)
1019 {
1020 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1021 result.SetStatus (eReturnStatusFailed);
1022 return result.Succeeded();
1023 }
1024
1025 // shift off the index
1026 command.Shift();
1027 argc = command.GetArgumentCount();
1028
1029 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1030 {
1031 const char *from = command.GetArgumentAtIndex(i);
1032 const char *to = command.GetArgumentAtIndex(i+1);
1033
1034 if (from[0] && to[0])
1035 {
1036 bool last_pair = ((argc - i) == 2);
1037 target->GetImageSearchPathList().Insert (ConstString(from),
1038 ConstString(to),
1039 insert_idx,
1040 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001041 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001042 }
1043 else
1044 {
1045 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001046 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001047 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001048 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001049 result.SetStatus (eReturnStatusFailed);
1050 return false;
1051 }
1052 }
1053 }
1054 else
1055 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001056 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001057 result.SetStatus (eReturnStatusFailed);
1058 return result.Succeeded();
1059 }
1060
1061 }
1062 else
1063 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001064 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001065 result.SetStatus (eReturnStatusFailed);
1066 }
1067 return result.Succeeded();
1068 }
1069};
1070
Greg Claytone1f50b92011-05-03 22:09:39 +00001071
1072#pragma mark CommandObjectTargetModulesSearchPathsList
1073
1074
1075class CommandObjectTargetModulesSearchPathsList : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +00001076{
1077public:
1078
Greg Claytone1f50b92011-05-03 22:09:39 +00001079 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001080 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +00001081 "target modules search-paths list",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001082 "List all current image search path substitution pairs in the current target.",
Greg Claytone1f50b92011-05-03 22:09:39 +00001083 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001084 {
1085 }
1086
Greg Claytone1f50b92011-05-03 22:09:39 +00001087 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001088 {
1089 }
1090
1091 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001092 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001093 CommandReturnObject &result)
1094 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001095 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001096 if (target)
1097 {
1098 if (command.GetArgumentCount() != 0)
1099 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001100 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001101 result.SetStatus (eReturnStatusFailed);
1102 return result.Succeeded();
1103 }
1104
1105 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001106 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001107 }
1108 else
1109 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001110 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001111 result.SetStatus (eReturnStatusFailed);
1112 }
1113 return result.Succeeded();
1114 }
1115};
1116
Greg Claytone1f50b92011-05-03 22:09:39 +00001117#pragma mark CommandObjectTargetModulesSearchPathsQuery
1118
1119class CommandObjectTargetModulesSearchPathsQuery : public CommandObject
Chris Lattner24943d22010-06-08 16:52:24 +00001120{
1121public:
1122
Greg Claytone1f50b92011-05-03 22:09:39 +00001123 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001124 CommandObject (interpreter,
Greg Claytone1f50b92011-05-03 22:09:39 +00001125 "target modules search-paths query",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001126 "Transform a path using the first applicable image search path.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001127 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001128 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001129 CommandArgumentEntry arg;
1130 CommandArgumentData path_arg;
1131
1132 // Define the first (and only) variant of this arg.
1133 path_arg.arg_type = eArgTypePath;
1134 path_arg.arg_repetition = eArgRepeatPlain;
1135
1136 // There is only one variant this argument could be; put it into the argument entry.
1137 arg.push_back (path_arg);
1138
1139 // Push the data for the first argument into the m_arguments vector.
1140 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001141 }
1142
Greg Claytone1f50b92011-05-03 22:09:39 +00001143 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001144 {
1145 }
1146
1147 bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001148 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001149 CommandReturnObject &result)
1150 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001151 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001152 if (target)
1153 {
1154 if (command.GetArgumentCount() != 1)
1155 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001156 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001157 result.SetStatus (eReturnStatusFailed);
1158 return result.Succeeded();
1159 }
1160
1161 ConstString orig(command.GetArgumentAtIndex(0));
1162 ConstString transformed;
1163 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1164 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1165 else
1166 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001167
1168 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001169 }
1170 else
1171 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001172 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001173 result.SetStatus (eReturnStatusFailed);
1174 }
1175 return result.Succeeded();
1176 }
1177};
1178
Greg Claytone1f50b92011-05-03 22:09:39 +00001179//----------------------------------------------------------------------
1180// Static Helper functions
1181//----------------------------------------------------------------------
1182static void
1183DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1184{
1185 if (module)
1186 {
1187 const char *arch_cstr;
1188 if (full_triple)
1189 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1190 else
1191 arch_cstr = module->GetArchitecture().GetArchitectureName();
1192 if (width)
1193 strm.Printf("%-*s", width, arch_cstr);
1194 else
1195 strm.PutCString(arch_cstr);
1196 }
1197}
1198
1199static void
1200DumpModuleUUID (Stream &strm, Module *module)
1201{
Greg Clayton153ccd72011-08-10 02:10:13 +00001202 if (module->GetUUID().IsValid())
1203 module->GetUUID().Dump (&strm);
1204 else
1205 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001206}
1207
1208static uint32_t
1209DumpCompileUnitLineTable
1210(
1211 CommandInterpreter &interpreter,
1212 Stream &strm,
1213 Module *module,
1214 const FileSpec &file_spec,
1215 bool load_addresses
1216 )
1217{
1218 uint32_t num_matches = 0;
1219 if (module)
1220 {
1221 SymbolContextList sc_list;
1222 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1223 0,
1224 false,
1225 eSymbolContextCompUnit,
1226 sc_list);
1227
1228 for (uint32_t i=0; i<num_matches; ++i)
1229 {
1230 SymbolContext sc;
1231 if (sc_list.GetContextAtIndex(i, sc))
1232 {
1233 if (i > 0)
1234 strm << "\n\n";
1235
1236 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1237 << module->GetFileSpec().GetFilename() << "\n";
1238 LineTable *line_table = sc.comp_unit->GetLineTable();
1239 if (line_table)
1240 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001241 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001242 lldb::eDescriptionLevelBrief);
1243 else
1244 strm << "No line table";
1245 }
1246 }
1247 }
1248 return num_matches;
1249}
1250
1251static void
1252DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1253{
1254 if (file_spec_ptr)
1255 {
1256 if (width > 0)
1257 {
1258 char fullpath[PATH_MAX];
1259 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1260 {
1261 strm.Printf("%-*s", width, fullpath);
1262 return;
1263 }
1264 }
1265 else
1266 {
1267 file_spec_ptr->Dump(&strm);
1268 return;
1269 }
1270 }
1271 // Keep the width spacing correct if things go wrong...
1272 if (width > 0)
1273 strm.Printf("%-*s", width, "");
1274}
1275
1276static void
1277DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1278{
1279 if (file_spec_ptr)
1280 {
1281 if (width > 0)
1282 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1283 else
1284 file_spec_ptr->GetDirectory().Dump(&strm);
1285 return;
1286 }
1287 // Keep the width spacing correct if things go wrong...
1288 if (width > 0)
1289 strm.Printf("%-*s", width, "");
1290}
1291
1292static void
1293DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1294{
1295 if (file_spec_ptr)
1296 {
1297 if (width > 0)
1298 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1299 else
1300 file_spec_ptr->GetFilename().Dump(&strm);
1301 return;
1302 }
1303 // Keep the width spacing correct if things go wrong...
1304 if (width > 0)
1305 strm.Printf("%-*s", width, "");
1306}
1307
1308
1309static void
1310DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1311{
1312 if (module)
1313 {
1314 ObjectFile *objfile = module->GetObjectFile ();
1315 if (objfile)
1316 {
1317 Symtab *symtab = objfile->GetSymtab();
1318 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001319 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001320 }
1321 }
1322}
1323
1324static void
1325DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1326{
1327 if (module)
1328 {
1329 ObjectFile *objfile = module->GetObjectFile ();
1330 if (objfile)
1331 {
1332 SectionList *section_list = objfile->GetSectionList();
1333 if (section_list)
1334 {
1335 strm.PutCString ("Sections for '");
1336 strm << module->GetFileSpec();
1337 if (module->GetObjectName())
1338 strm << '(' << module->GetObjectName() << ')';
1339 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1340 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001341 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001342 strm.IndentLess();
1343 }
1344 }
1345 }
1346}
1347
1348static bool
1349DumpModuleSymbolVendor (Stream &strm, Module *module)
1350{
1351 if (module)
1352 {
1353 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1354 if (symbol_vendor)
1355 {
1356 symbol_vendor->Dump(&strm);
1357 return true;
1358 }
1359 }
1360 return false;
1361}
1362
1363static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001364LookupAddressInModule (CommandInterpreter &interpreter,
1365 Stream &strm,
1366 Module *module,
1367 uint32_t resolve_mask,
1368 lldb::addr_t raw_addr,
1369 lldb::addr_t offset,
1370 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001371{
1372 if (module)
1373 {
1374 lldb::addr_t addr = raw_addr - offset;
1375 Address so_addr;
1376 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001377 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001378 if (target && !target->GetSectionLoadList().IsEmpty())
1379 {
1380 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1381 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001382 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001383 return false;
1384 }
1385 else
1386 {
1387 if (!module->ResolveFileAddress (addr, so_addr))
1388 return false;
1389 }
1390
Greg Claytone1f50b92011-05-03 22:09:39 +00001391 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
1392 strm.IndentMore();
1393 strm.Indent (" Address: ");
Greg Clayton00b11c32012-01-10 00:25:18 +00001394 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1395 strm.PutCString (" (");
Greg Claytone1f50b92011-05-03 22:09:39 +00001396 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
Greg Clayton00b11c32012-01-10 00:25:18 +00001397 strm.PutCString (")\n");
Greg Claytone1f50b92011-05-03 22:09:39 +00001398 strm.Indent (" Summary: ");
1399 const uint32_t save_indent = strm.GetIndentLevel ();
Greg Clayton2f57db02011-10-01 00:45:15 +00001400 strm.SetIndentLevel (save_indent + 13);
Greg Claytone1f50b92011-05-03 22:09:39 +00001401 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1402 strm.SetIndentLevel (save_indent);
Greg Claytone1f50b92011-05-03 22:09:39 +00001403 // Print out detailed address information when verbose is enabled
1404 if (verbose)
1405 {
Greg Clayton2f57db02011-10-01 00:45:15 +00001406 strm.EOL();
Jason Molendafb66bb52011-09-23 00:27:44 +00001407 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
Greg Claytone1f50b92011-05-03 22:09:39 +00001408 }
1409 strm.IndentLess();
1410 return true;
1411 }
1412
1413 return false;
1414}
1415
1416static uint32_t
1417LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex)
1418{
1419 if (module)
1420 {
1421 SymbolContext sc;
1422
1423 ObjectFile *objfile = module->GetObjectFile ();
1424 if (objfile)
1425 {
1426 Symtab *symtab = objfile->GetSymtab();
1427 if (symtab)
1428 {
1429 uint32_t i;
1430 std::vector<uint32_t> match_indexes;
1431 ConstString symbol_name (name);
1432 uint32_t num_matches = 0;
1433 if (name_is_regex)
1434 {
1435 RegularExpression name_regexp(name);
1436 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1437 eSymbolTypeAny,
1438 match_indexes);
1439 }
1440 else
1441 {
1442 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1443 }
1444
1445
1446 if (num_matches > 0)
1447 {
1448 strm.Indent ();
1449 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1450 name_is_regex ? "the regular expression " : "", name);
1451 DumpFullpath (strm, &module->GetFileSpec(), 0);
1452 strm.PutCString(":\n");
1453 strm.IndentMore ();
1454 Symtab::DumpSymbolHeader (&strm);
1455 for (i=0; i < num_matches; ++i)
1456 {
1457 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
1458 strm.Indent ();
Greg Clayton567e7f32011-09-22 04:58:26 +00001459 symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001460 }
1461 strm.IndentLess ();
1462 return num_matches;
1463 }
1464 }
1465 }
1466 }
1467 return 0;
1468}
1469
1470
1471static void
1472DumpSymbolContextList (CommandInterpreter &interpreter, Stream &strm, SymbolContextList &sc_list, bool prepend_addr, bool verbose)
1473{
1474 strm.IndentMore ();
1475 uint32_t i;
1476 const uint32_t num_matches = sc_list.GetSize();
1477
1478 for (i=0; i<num_matches; ++i)
1479 {
1480 SymbolContext sc;
1481 if (sc_list.GetContextAtIndex(i, sc))
1482 {
1483 strm.Indent();
1484 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope ();
1485
1486 if (prepend_addr)
1487 {
1488 if (sc.line_entry.range.GetBaseAddress().IsValid())
1489 {
1490 sc.line_entry.range.GetBaseAddress().Dump (&strm,
1491 exe_scope,
1492 Address::DumpStyleLoadAddress,
1493 Address::DumpStyleModuleWithFileAddress);
1494 strm.PutCString(" in ");
1495 }
1496 }
Sean Callanand7793d22012-02-11 00:24:04 +00001497
1498 AddressRange range;
1499
1500 sc.GetAddressRange(eSymbolContextEverything,
1501 0,
1502 true,
1503 range);
1504
Greg Claytone1f50b92011-05-03 22:09:39 +00001505 sc.DumpStopContext(&strm,
1506 exe_scope,
Sean Callanand7793d22012-02-11 00:24:04 +00001507 range.GetBaseAddress(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001508 true,
1509 true,
1510 false);
Sean Callanand7793d22012-02-11 00:24:04 +00001511
Greg Claytone1f50b92011-05-03 22:09:39 +00001512 strm.EOL();
1513 if (verbose)
1514 {
1515 if (sc.line_entry.range.GetBaseAddress().IsValid())
1516 {
1517 if (sc.line_entry.range.GetBaseAddress().Dump (&strm,
1518 exe_scope,
1519 Address::DumpStyleDetailedSymbolContext))
1520 strm.PutCString("\n\n");
1521 }
1522 else if (sc.function->GetAddressRange().GetBaseAddress().IsValid())
1523 {
1524 if (sc.function->GetAddressRange().GetBaseAddress().Dump (&strm,
1525 exe_scope,
1526 Address::DumpStyleDetailedSymbolContext))
1527 strm.PutCString("\n\n");
1528 }
1529 }
1530 }
1531 }
1532 strm.IndentLess ();
1533}
1534
1535static uint32_t
Sean Callanan9ad19532012-02-11 01:22:21 +00001536LookupFunctionInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool include_inlines, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001537{
1538 if (module && name && name[0])
1539 {
1540 SymbolContextList sc_list;
1541 const bool include_symbols = false;
1542 const bool append = true;
1543 uint32_t num_matches = 0;
1544 if (name_is_regex)
1545 {
1546 RegularExpression function_name_regex (name);
1547 num_matches = module->FindFunctions (function_name_regex,
1548 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001549 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001550 append,
1551 sc_list);
1552 }
1553 else
1554 {
1555 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001556 num_matches = module->FindFunctions (function_name,
1557 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001558 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1559 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001560 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001561 append,
1562 sc_list);
1563 }
1564
1565 if (num_matches)
1566 {
1567 strm.Indent ();
1568 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1569 DumpFullpath (strm, &module->GetFileSpec(), 0);
1570 strm.PutCString(":\n");
1571 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose);
1572 }
1573 return num_matches;
1574 }
1575 return 0;
1576}
1577
1578static uint32_t
Greg Clayton801417e2011-07-07 01:59:51 +00001579LookupTypeInModule (CommandInterpreter &interpreter,
1580 Stream &strm,
1581 Module *module,
1582 const char *name_cstr,
1583 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001584{
1585 if (module && name_cstr && name_cstr[0])
1586 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001587 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001588 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001589 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001590 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001591 SymbolContext sc;
1592
1593 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001594 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001595
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001596 if (num_matches)
1597 {
1598 strm.Indent ();
1599 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1600 DumpFullpath (strm, &module->GetFileSpec(), 0);
1601 strm.PutCString(":\n");
1602 const uint32_t num_types = type_list.GetSize();
1603 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001604 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001605 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1606 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001607 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001608 // Resolve the clang type so that any forward references
1609 // to types that haven't yet been parsed will get parsed.
1610 type_sp->GetClangFullType ();
1611 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00001612 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001613 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001614 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001615 }
1616 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001617 }
1618 return 0;
1619}
1620
1621static uint32_t
1622LookupFileAndLineInModule (CommandInterpreter &interpreter,
1623 Stream &strm,
1624 Module *module,
1625 const FileSpec &file_spec,
1626 uint32_t line,
1627 bool check_inlines,
1628 bool verbose)
1629{
1630 if (module && file_spec)
1631 {
1632 SymbolContextList sc_list;
1633 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1634 eSymbolContextEverything, sc_list);
1635 if (num_matches > 0)
1636 {
1637 strm.Indent ();
1638 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1639 strm << file_spec;
1640 if (line > 0)
1641 strm.Printf (":%u", line);
1642 strm << " in ";
1643 DumpFullpath (strm, &module->GetFileSpec(), 0);
1644 strm.PutCString(":\n");
1645 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose);
1646 return num_matches;
1647 }
1648 }
1649 return 0;
1650
1651}
1652
Greg Clayton91048ef2011-11-10 01:18:58 +00001653
1654static size_t
1655FindModulesByName (Target *target,
1656 const char *module_name,
1657 ModuleList &module_list,
1658 bool check_global_list)
1659{
1660// Dump specified images (by basename or fullpath)
1661 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001662 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001663
1664 const size_t initial_size = module_list.GetSize ();
1665
1666 size_t num_matches = 0;
1667
1668 if (target)
1669 {
Greg Clayton444fe992012-02-26 05:51:37 +00001670 num_matches = target->GetImages().FindModules (module_spec, module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001671
1672 // Not found in our module list for our target, check the main
1673 // shared module list in case it is a extra file used somewhere
1674 // else
1675 if (num_matches == 0)
Greg Clayton444fe992012-02-26 05:51:37 +00001676 {
1677 module_spec.GetArchitecture() = target->GetArchitecture();
1678 num_matches = ModuleList::FindSharedModules (module_spec, module_list);
1679 }
Greg Clayton91048ef2011-11-10 01:18:58 +00001680 }
1681 else
1682 {
Greg Clayton444fe992012-02-26 05:51:37 +00001683 num_matches = ModuleList::FindSharedModules (module_spec,module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001684 }
1685
1686 if (check_global_list && num_matches == 0)
1687 {
1688 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001689 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001690 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1691 ModuleSP module_sp;
1692 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1693 {
1694 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1695
1696 if (module)
1697 {
Greg Clayton444fe992012-02-26 05:51:37 +00001698 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001699 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001700 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001701 module_list.AppendIfNeeded(module_sp);
1702 }
1703 }
1704 }
1705 }
1706 return module_list.GetSize () - initial_size;
1707}
1708
Greg Claytone1f50b92011-05-03 22:09:39 +00001709#pragma mark CommandObjectTargetModulesModuleAutoComplete
1710
1711//----------------------------------------------------------------------
1712// A base command object class that can auto complete with module file
1713// paths
1714//----------------------------------------------------------------------
1715
1716class CommandObjectTargetModulesModuleAutoComplete : public CommandObject
1717{
1718public:
1719
1720 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1721 const char *name,
1722 const char *help,
1723 const char *syntax) :
1724 CommandObject (interpreter, name, help, syntax)
1725 {
1726 CommandArgumentEntry arg;
1727 CommandArgumentData file_arg;
1728
1729 // Define the first (and only) variant of this arg.
1730 file_arg.arg_type = eArgTypeFilename;
1731 file_arg.arg_repetition = eArgRepeatStar;
1732
1733 // There is only one variant this argument could be; put it into the argument entry.
1734 arg.push_back (file_arg);
1735
1736 // Push the data for the first argument into the m_arguments vector.
1737 m_arguments.push_back (arg);
1738 }
1739
1740 virtual
1741 ~CommandObjectTargetModulesModuleAutoComplete ()
1742 {
1743 }
1744
1745 virtual int
1746 HandleArgumentCompletion (Args &input,
1747 int &cursor_index,
1748 int &cursor_char_position,
1749 OptionElementVector &opt_element_vector,
1750 int match_start_point,
1751 int max_return_elements,
1752 bool &word_complete,
1753 StringList &matches)
1754 {
1755 // Arguments are the standard module completer.
1756 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1757 completion_str.erase (cursor_char_position);
1758
1759 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1760 CommandCompletions::eModuleCompletion,
1761 completion_str.c_str(),
1762 match_start_point,
1763 max_return_elements,
1764 NULL,
1765 word_complete,
1766 matches);
1767 return matches.GetSize();
1768 }
1769};
1770
1771#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1772
1773//----------------------------------------------------------------------
1774// A base command object class that can auto complete with module source
1775// file paths
1776//----------------------------------------------------------------------
1777
1778class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObject
1779{
1780public:
1781
1782 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1783 const char *name,
1784 const char *help,
1785 const char *syntax) :
1786 CommandObject (interpreter, name, help, syntax)
1787 {
1788 CommandArgumentEntry arg;
1789 CommandArgumentData source_file_arg;
1790
1791 // Define the first (and only) variant of this arg.
1792 source_file_arg.arg_type = eArgTypeSourceFile;
1793 source_file_arg.arg_repetition = eArgRepeatPlus;
1794
1795 // There is only one variant this argument could be; put it into the argument entry.
1796 arg.push_back (source_file_arg);
1797
1798 // Push the data for the first argument into the m_arguments vector.
1799 m_arguments.push_back (arg);
1800 }
1801
1802 virtual
1803 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1804 {
1805 }
1806
1807 virtual int
1808 HandleArgumentCompletion (Args &input,
1809 int &cursor_index,
1810 int &cursor_char_position,
1811 OptionElementVector &opt_element_vector,
1812 int match_start_point,
1813 int max_return_elements,
1814 bool &word_complete,
1815 StringList &matches)
1816 {
1817 // Arguments are the standard source file completer.
1818 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1819 completion_str.erase (cursor_char_position);
1820
1821 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1822 CommandCompletions::eSourceFileCompletion,
1823 completion_str.c_str(),
1824 match_start_point,
1825 max_return_elements,
1826 NULL,
1827 word_complete,
1828 matches);
1829 return matches.GetSize();
1830 }
1831};
1832
1833
1834#pragma mark CommandObjectTargetModulesDumpSymtab
1835
1836
1837class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1838{
1839public:
1840 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1841 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1842 "target modules dump symtab",
1843 "Dump the symbol table from one or more target modules.",
1844 NULL),
1845 m_options (interpreter)
1846 {
1847 }
1848
1849 virtual
1850 ~CommandObjectTargetModulesDumpSymtab ()
1851 {
1852 }
1853
1854 virtual bool
1855 Execute (Args& command,
1856 CommandReturnObject &result)
1857 {
1858 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1859 if (target == NULL)
1860 {
1861 result.AppendError ("invalid target, create a debug target using the 'target create' command");
1862 result.SetStatus (eReturnStatusFailed);
1863 return false;
1864 }
1865 else
1866 {
1867 uint32_t num_dumped = 0;
1868
1869 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1870 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1871 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1872
1873 if (command.GetArgumentCount() == 0)
1874 {
1875 // Dump all sections for all modules images
1876 const uint32_t num_modules = target->GetImages().GetSize();
1877 if (num_modules > 0)
1878 {
1879 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
1880 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1881 {
1882 if (num_dumped > 0)
1883 {
1884 result.GetOutputStream().EOL();
1885 result.GetOutputStream().EOL();
1886 }
1887 num_dumped++;
1888 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx), m_options.m_sort_order);
1889 }
1890 }
1891 else
1892 {
1893 result.AppendError ("the target has no associated executable images");
1894 result.SetStatus (eReturnStatusFailed);
1895 return false;
1896 }
1897 }
1898 else
1899 {
1900 // Dump specified images (by basename or fullpath)
1901 const char *arg_cstr;
1902 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
1903 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001904 ModuleList module_list;
1905 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
1906 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00001907 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001908 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001909 {
Greg Clayton91048ef2011-11-10 01:18:58 +00001910 Module *module = module_list.GetModulePointerAtIndex(i);
1911 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001912 {
1913 if (num_dumped > 0)
1914 {
1915 result.GetOutputStream().EOL();
1916 result.GetOutputStream().EOL();
1917 }
1918 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00001919 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001920 }
1921 }
1922 }
1923 else
1924 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
1925 }
1926 }
1927
1928 if (num_dumped > 0)
1929 result.SetStatus (eReturnStatusSuccessFinishResult);
1930 else
1931 {
1932 result.AppendError ("no matching executable images found");
1933 result.SetStatus (eReturnStatusFailed);
1934 }
1935 }
1936 return result.Succeeded();
1937 }
1938
1939 virtual Options *
1940 GetOptions ()
1941 {
1942 return &m_options;
1943 }
1944
1945 class CommandOptions : public Options
1946 {
1947 public:
1948
1949 CommandOptions (CommandInterpreter &interpreter) :
1950 Options(interpreter),
1951 m_sort_order (eSortOrderNone)
1952 {
1953 }
1954
1955 virtual
1956 ~CommandOptions ()
1957 {
1958 }
1959
1960 virtual Error
1961 SetOptionValue (uint32_t option_idx, const char *option_arg)
1962 {
1963 Error error;
1964 char short_option = (char) m_getopt_table[option_idx].val;
1965
1966 switch (short_option)
1967 {
1968 case 's':
Greg Claytone1f50b92011-05-03 22:09:39 +00001969 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1970 g_option_table[option_idx].enum_values,
1971 eSortOrderNone,
Greg Clayton61aca5d2011-10-07 18:58:12 +00001972 error);
Greg Claytone1f50b92011-05-03 22:09:39 +00001973 break;
1974
1975 default:
Greg Clayton9c236732011-10-26 00:56:27 +00001976 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Greg Claytone1f50b92011-05-03 22:09:39 +00001977 break;
1978
1979 }
1980 return error;
1981 }
1982
1983 void
1984 OptionParsingStarting ()
1985 {
1986 m_sort_order = eSortOrderNone;
1987 }
1988
1989 const OptionDefinition*
1990 GetDefinitions ()
1991 {
1992 return g_option_table;
1993 }
1994
1995 // Options table: Required for subclasses of Options.
1996 static OptionDefinition g_option_table[];
1997
1998 SortOrder m_sort_order;
1999 };
2000
2001protected:
2002
2003 CommandOptions m_options;
2004};
2005
2006static OptionEnumValueElement
2007g_sort_option_enumeration[4] =
2008{
2009 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2010 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2011 { eSortOrderByName, "name", "Sort output by symbol name."},
2012 { 0, NULL, NULL }
2013};
2014
2015
2016OptionDefinition
2017CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2018{
2019 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2020 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2021};
2022
2023#pragma mark CommandObjectTargetModulesDumpSections
2024
2025//----------------------------------------------------------------------
2026// Image section dumping command
2027//----------------------------------------------------------------------
2028
2029class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2030{
2031public:
2032 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2033 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2034 "target modules dump sections",
2035 "Dump the sections from one or more target modules.",
2036 //"target modules dump sections [<file1> ...]")
2037 NULL)
2038 {
2039 }
2040
2041 virtual
2042 ~CommandObjectTargetModulesDumpSections ()
2043 {
2044 }
2045
2046 virtual bool
2047 Execute (Args& command,
2048 CommandReturnObject &result)
2049 {
2050 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2051 if (target == NULL)
2052 {
2053 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2054 result.SetStatus (eReturnStatusFailed);
2055 return false;
2056 }
2057 else
2058 {
2059 uint32_t num_dumped = 0;
2060
2061 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2062 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2063 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2064
2065 if (command.GetArgumentCount() == 0)
2066 {
2067 // Dump all sections for all modules images
2068 const uint32_t num_modules = target->GetImages().GetSize();
2069 if (num_modules > 0)
2070 {
2071 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2072 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2073 {
2074 num_dumped++;
2075 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2076 }
2077 }
2078 else
2079 {
2080 result.AppendError ("the target has no associated executable images");
2081 result.SetStatus (eReturnStatusFailed);
2082 return false;
2083 }
2084 }
2085 else
2086 {
2087 // Dump specified images (by basename or fullpath)
2088 const char *arg_cstr;
2089 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2090 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002091 ModuleList module_list;
2092 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2093 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002094 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002095 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002096 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002097 Module *module = module_list.GetModulePointerAtIndex(i);
2098 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002099 {
2100 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002101 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002102 }
2103 }
2104 }
2105 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002106 {
2107 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002108 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002109
Greg Claytone1f50b92011-05-03 22:09:39 +00002110 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002111 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002112 }
2113 }
2114
2115 if (num_dumped > 0)
2116 result.SetStatus (eReturnStatusSuccessFinishResult);
2117 else
2118 {
2119 result.AppendError ("no matching executable images found");
2120 result.SetStatus (eReturnStatusFailed);
2121 }
2122 }
2123 return result.Succeeded();
2124 }
2125};
2126
2127
2128#pragma mark CommandObjectTargetModulesDumpSymfile
2129
2130//----------------------------------------------------------------------
2131// Image debug symbol dumping command
2132//----------------------------------------------------------------------
2133
2134class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2135{
2136public:
2137 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2138 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2139 "target modules dump symfile",
2140 "Dump the debug symbol file for one or more target modules.",
2141 //"target modules dump symfile [<file1> ...]")
2142 NULL)
2143 {
2144 }
2145
2146 virtual
2147 ~CommandObjectTargetModulesDumpSymfile ()
2148 {
2149 }
2150
2151 virtual bool
2152 Execute (Args& command,
2153 CommandReturnObject &result)
2154 {
2155 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2156 if (target == NULL)
2157 {
2158 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2159 result.SetStatus (eReturnStatusFailed);
2160 return false;
2161 }
2162 else
2163 {
2164 uint32_t num_dumped = 0;
2165
2166 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2167 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2168 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2169
2170 if (command.GetArgumentCount() == 0)
2171 {
2172 // Dump all sections for all modules images
2173 const uint32_t num_modules = target->GetImages().GetSize();
2174 if (num_modules > 0)
2175 {
2176 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2177 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2178 {
2179 if (DumpModuleSymbolVendor (result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx)))
2180 num_dumped++;
2181 }
2182 }
2183 else
2184 {
2185 result.AppendError ("the target has no associated executable images");
2186 result.SetStatus (eReturnStatusFailed);
2187 return false;
2188 }
2189 }
2190 else
2191 {
2192 // Dump specified images (by basename or fullpath)
2193 const char *arg_cstr;
2194 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2195 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002196 ModuleList module_list;
2197 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2198 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002199 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002200 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002201 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002202 Module *module = module_list.GetModulePointerAtIndex(i);
2203 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002204 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002205 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002206 num_dumped++;
2207 }
2208 }
2209 }
2210 else
2211 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2212 }
2213 }
2214
2215 if (num_dumped > 0)
2216 result.SetStatus (eReturnStatusSuccessFinishResult);
2217 else
2218 {
2219 result.AppendError ("no matching executable images found");
2220 result.SetStatus (eReturnStatusFailed);
2221 }
2222 }
2223 return result.Succeeded();
2224 }
2225};
2226
2227
2228#pragma mark CommandObjectTargetModulesDumpLineTable
2229
2230//----------------------------------------------------------------------
2231// Image debug line table dumping command
2232//----------------------------------------------------------------------
2233
2234class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2235{
2236public:
2237 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2238 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2239 "target modules dump line-table",
2240 "Dump the debug symbol file for one or more target modules.",
2241 NULL)
2242 {
2243 }
2244
2245 virtual
2246 ~CommandObjectTargetModulesDumpLineTable ()
2247 {
2248 }
2249
2250 virtual bool
2251 Execute (Args& command,
2252 CommandReturnObject &result)
2253 {
2254 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2255 if (target == NULL)
2256 {
2257 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2258 result.SetStatus (eReturnStatusFailed);
2259 return false;
2260 }
2261 else
2262 {
2263 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2264 uint32_t total_num_dumped = 0;
2265
2266 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2267 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2268 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2269
2270 if (command.GetArgumentCount() == 0)
2271 {
2272 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2273 result.SetStatus (eReturnStatusFailed);
2274 }
2275 else
2276 {
2277 // Dump specified images (by basename or fullpath)
2278 const char *arg_cstr;
2279 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2280 {
2281 FileSpec file_spec(arg_cstr, false);
2282 const uint32_t num_modules = target->GetImages().GetSize();
2283 if (num_modules > 0)
2284 {
2285 uint32_t num_dumped = 0;
2286 for (uint32_t i = 0; i<num_modules; ++i)
2287 {
2288 if (DumpCompileUnitLineTable (m_interpreter,
2289 result.GetOutputStream(),
2290 target->GetImages().GetModulePointerAtIndex(i),
2291 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002292 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002293 num_dumped++;
2294 }
2295 if (num_dumped == 0)
2296 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2297 else
2298 total_num_dumped += num_dumped;
2299 }
2300 }
2301 }
2302
2303 if (total_num_dumped > 0)
2304 result.SetStatus (eReturnStatusSuccessFinishResult);
2305 else
2306 {
2307 result.AppendError ("no source filenames matched any command arguments");
2308 result.SetStatus (eReturnStatusFailed);
2309 }
2310 }
2311 return result.Succeeded();
2312 }
2313};
2314
2315
2316#pragma mark CommandObjectTargetModulesDump
2317
2318//----------------------------------------------------------------------
2319// Dump multi-word command for target modules
2320//----------------------------------------------------------------------
2321
2322class CommandObjectTargetModulesDump : public CommandObjectMultiword
2323{
2324public:
2325
2326 //------------------------------------------------------------------
2327 // Constructors and Destructors
2328 //------------------------------------------------------------------
2329 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2330 CommandObjectMultiword (interpreter,
2331 "target modules dump",
2332 "A set of commands for dumping information about one or more target modules.",
2333 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2334 {
2335 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2336 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2337 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2338 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2339 }
2340
2341 virtual
2342 ~CommandObjectTargetModulesDump()
2343 {
2344 }
2345};
2346
2347class CommandObjectTargetModulesAdd : public CommandObject
2348{
2349public:
2350 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
2351 CommandObject (interpreter,
2352 "target modules add",
2353 "Add a new module to the current target's modules.",
2354 "target modules add [<module>]")
2355 {
2356 }
2357
2358 virtual
2359 ~CommandObjectTargetModulesAdd ()
2360 {
2361 }
2362
2363 virtual bool
2364 Execute (Args& args,
2365 CommandReturnObject &result)
2366 {
2367 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2368 if (target == NULL)
2369 {
2370 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2371 result.SetStatus (eReturnStatusFailed);
2372 return false;
2373 }
2374 else
2375 {
2376 const size_t argc = args.GetArgumentCount();
2377 if (argc == 0)
2378 {
2379 result.AppendError ("one or more executable image paths must be specified");
2380 result.SetStatus (eReturnStatusFailed);
2381 return false;
2382 }
2383 else
2384 {
2385 for (size_t i=0; i<argc; ++i)
2386 {
2387 const char *path = args.GetArgumentAtIndex(i);
2388 if (path)
2389 {
2390 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002391 if (file_spec.Exists())
2392 {
Greg Clayton444fe992012-02-26 05:51:37 +00002393 ModuleSpec module_spec (file_spec);
2394 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002395 if (!module_sp)
2396 {
2397 result.AppendError ("one or more executable image paths must be specified");
2398 result.SetStatus (eReturnStatusFailed);
2399 return false;
2400 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002401 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002402 }
2403 else
2404 {
2405 char resolved_path[PATH_MAX];
2406 result.SetStatus (eReturnStatusFailed);
2407 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2408 {
2409 if (strcmp (resolved_path, path) != 0)
2410 {
2411 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2412 break;
2413 }
2414 }
2415 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2416 break;
2417 }
2418 }
2419 }
2420 }
2421 }
2422 return result.Succeeded();
2423 }
2424
2425 int
2426 HandleArgumentCompletion (Args &input,
2427 int &cursor_index,
2428 int &cursor_char_position,
2429 OptionElementVector &opt_element_vector,
2430 int match_start_point,
2431 int max_return_elements,
2432 bool &word_complete,
2433 StringList &matches)
2434 {
2435 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2436 completion_str.erase (cursor_char_position);
2437
2438 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2439 CommandCompletions::eDiskFileCompletion,
2440 completion_str.c_str(),
2441 match_start_point,
2442 max_return_elements,
2443 NULL,
2444 word_complete,
2445 matches);
2446 return matches.GetSize();
2447 }
2448
2449};
2450
2451class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2452{
2453public:
2454 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2455 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2456 "target modules load",
2457 "Set the load addresses for one or more sections in a target module.",
2458 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2459 m_option_group (interpreter),
2460 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2461 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)
2462 {
2463 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2464 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2465 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2466 m_option_group.Finalize();
2467 }
2468
2469 virtual
2470 ~CommandObjectTargetModulesLoad ()
2471 {
2472 }
2473
2474 virtual bool
2475 Execute (Args& args,
2476 CommandReturnObject &result)
2477 {
2478 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2479 if (target == NULL)
2480 {
2481 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2482 result.SetStatus (eReturnStatusFailed);
2483 return false;
2484 }
2485 else
2486 {
2487 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002488 ModuleSpec module_spec;
2489 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002490 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002491 {
2492 search_using_module_spec = true;
2493 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2494 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002495
2496 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002497 {
2498 search_using_module_spec = true;
2499 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2500 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002501
Greg Clayton444fe992012-02-26 05:51:37 +00002502 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002503 {
2504
2505 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002506 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002507
2508 char path[PATH_MAX];
2509 if (num_matches == 1)
2510 {
2511 Module *module = matching_modules.GetModulePointerAtIndex(0);
2512 if (module)
2513 {
2514 ObjectFile *objfile = module->GetObjectFile();
2515 if (objfile)
2516 {
2517 SectionList *section_list = objfile->GetSectionList();
2518 if (section_list)
2519 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002520 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002521 if (argc == 0)
2522 {
2523 if (m_slide_option.GetOptionValue().OptionWasSet())
2524 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002525 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2526 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002527 }
2528 else
2529 {
2530 result.AppendError ("one or more section name + load address pair must be specified");
2531 result.SetStatus (eReturnStatusFailed);
2532 return false;
2533 }
2534 }
2535 else
2536 {
2537 if (m_slide_option.GetOptionValue().OptionWasSet())
2538 {
2539 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2540 result.SetStatus (eReturnStatusFailed);
2541 return false;
2542 }
2543
2544 for (size_t i=0; i<argc; i += 2)
2545 {
2546 const char *sect_name = args.GetArgumentAtIndex(i);
2547 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2548 if (sect_name && load_addr_cstr)
2549 {
2550 ConstString const_sect_name(sect_name);
2551 bool success = false;
2552 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2553 if (success)
2554 {
2555 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2556 if (section_sp)
2557 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002558 if (section_sp->IsThreadSpecific())
2559 {
2560 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2561 result.SetStatus (eReturnStatusFailed);
2562 break;
2563 }
2564 else
2565 {
2566 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), load_addr))
2567 changed = true;
2568 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2569 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002570 }
2571 else
2572 {
2573 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2574 result.SetStatus (eReturnStatusFailed);
2575 break;
2576 }
2577 }
2578 else
2579 {
2580 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2581 result.SetStatus (eReturnStatusFailed);
2582 break;
2583 }
2584 }
2585 else
2586 {
2587 if (sect_name)
2588 result.AppendError ("section names must be followed by a load address.\n");
2589 else
2590 result.AppendError ("one or more section name + load address pair must be specified.\n");
2591 result.SetStatus (eReturnStatusFailed);
2592 break;
2593 }
2594 }
2595 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002596
2597 if (changed)
2598 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002599 }
2600 else
2601 {
2602 module->GetFileSpec().GetPath (path, sizeof(path));
2603 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2604 result.SetStatus (eReturnStatusFailed);
2605 }
2606 }
2607 else
2608 {
2609 module->GetFileSpec().GetPath (path, sizeof(path));
2610 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2611 result.SetStatus (eReturnStatusFailed);
2612 }
2613 }
2614 else
2615 {
2616 module->GetFileSpec().GetPath (path, sizeof(path));
2617 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2618 result.SetStatus (eReturnStatusFailed);
2619 }
2620 }
2621 else
2622 {
2623 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002624
2625 if (module_spec.GetFileSpec())
2626 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002627 else
2628 path[0] = '\0';
2629
Greg Clayton444fe992012-02-26 05:51:37 +00002630 if (module_spec.GetUUIDPtr())
2631 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002632 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 Clayton3508c382012-02-24 01:59:29 +00002813 ModuleSP module_sp (module_address.GetModule());
2814 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002815 {
Greg Clayton3508c382012-02-24 01:59:29 +00002816 PrintModule (target, module_sp.get(), 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':
Sean Callanan9ad19532012-02-11 01:22:21 +00003135 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003136 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;
Sean Callanan9ad19532012-02-11 01:22:21 +00003179 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003180 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.
Sean Callanan9ad19532012-02-11 01:22:21 +00003199 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003200 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,
Sean Callanan9ad19532012-02-11 01:22:21 +00003279 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003280 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,
Sean Callanan9ad19532012-02-11 01:22:21 +00003296 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003297 m_options.m_verbose))
3298 {
3299 result.SetStatus(eReturnStatusSuccessFinishResult);
3300 return true;
3301 }
3302 }
3303 break;
3304
3305 case eLookupTypeType:
3306 if (!m_options.m_str.empty())
3307 {
3308 if (LookupTypeInModule (m_interpreter,
3309 result.GetOutputStream(),
3310 module,
3311 m_options.m_str.c_str(),
3312 m_options.m_use_regex))
3313 {
3314 result.SetStatus(eReturnStatusSuccessFinishResult);
3315 return true;
3316 }
3317 }
3318 break;
3319
3320 default:
3321 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3322 syntax_error = true;
3323 break;
3324 }
3325
3326 result.SetStatus (eReturnStatusFailed);
3327 return false;
3328 }
3329
3330 virtual bool
3331 Execute (Args& command,
3332 CommandReturnObject &result)
3333 {
3334 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3335 if (target == NULL)
3336 {
3337 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3338 result.SetStatus (eReturnStatusFailed);
3339 return false;
3340 }
3341 else
3342 {
3343 bool syntax_error = false;
3344 uint32_t i;
3345 uint32_t num_successful_lookups = 0;
3346 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3347 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3348 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3349 // Dump all sections for all modules images
3350
3351 if (command.GetArgumentCount() == 0)
3352 {
3353 // Dump all sections for all modules images
3354 const uint32_t num_modules = target->GetImages().GetSize();
3355 if (num_modules > 0)
3356 {
3357 for (i = 0; i<num_modules && syntax_error == false; ++i)
3358 {
3359 if (LookupInModule (m_interpreter, target->GetImages().GetModulePointerAtIndex(i), result, syntax_error))
3360 {
3361 result.GetOutputStream().EOL();
3362 num_successful_lookups++;
3363 }
3364 }
3365 }
3366 else
3367 {
3368 result.AppendError ("the target has no associated executable images");
3369 result.SetStatus (eReturnStatusFailed);
3370 return false;
3371 }
3372 }
3373 else
3374 {
3375 // Dump specified images (by basename or fullpath)
3376 const char *arg_cstr;
3377 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3378 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003379 ModuleList module_list;
3380 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3381 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003382 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003383 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003384 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003385 Module *module = module_list.GetModulePointerAtIndex(i);
3386 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003387 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003388 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003389 {
3390 result.GetOutputStream().EOL();
3391 num_successful_lookups++;
3392 }
3393 }
3394 }
3395 }
3396 else
3397 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3398 }
3399 }
3400
3401 if (num_successful_lookups > 0)
3402 result.SetStatus (eReturnStatusSuccessFinishResult);
3403 else
3404 result.SetStatus (eReturnStatusFailed);
3405 }
3406 return result.Succeeded();
3407 }
3408protected:
3409
3410 CommandOptions m_options;
3411};
3412
3413OptionDefinition
3414CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3415{
3416 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3417 { 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 +00003418 { LLDB_OPT_SET_2| LLDB_OPT_SET_4
3419 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_5 */ ,
3420 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003421 { 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 +00003422 { 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."},
3423 { 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)."},
Sean Callanan9ad19532012-02-11 01:22:21 +00003424 { LLDB_OPT_SET_3|
3425 LLDB_OPT_SET_4, false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003426 { 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."},
3427 { 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."},
3428 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
3429 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3430};
Chris Lattner24943d22010-06-08 16:52:24 +00003431
3432
Jim Inghamd60d94a2011-03-11 03:53:59 +00003433#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003434
3435//-------------------------------------------------------------------------
3436// CommandObjectMultiwordImageSearchPaths
3437//-------------------------------------------------------------------------
3438
Greg Claytone1f50b92011-05-03 22:09:39 +00003439class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003440{
3441public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003442
3443 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3444 CommandObjectMultiword (interpreter,
3445 "target modules search-paths",
3446 "A set of commands for operating on debugger target image search paths.",
3447 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003448 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003449 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3450 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3451 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3452 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3453 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003454 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003455
3456 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003457 {
3458 }
3459};
3460
Greg Claytone1f50b92011-05-03 22:09:39 +00003461
3462
3463#pragma mark CommandObjectTargetModules
3464
3465//-------------------------------------------------------------------------
3466// CommandObjectTargetModules
3467//-------------------------------------------------------------------------
3468
3469class CommandObjectTargetModules : public CommandObjectMultiword
3470{
3471public:
3472 //------------------------------------------------------------------
3473 // Constructors and Destructors
3474 //------------------------------------------------------------------
3475 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3476 CommandObjectMultiword (interpreter,
3477 "target modules",
3478 "A set of commands for accessing information for one or more target modules.",
3479 "target modules <sub-command> ...")
3480 {
3481 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3482 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3483 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3484 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3485 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3486 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3487 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
3488
3489 }
3490 virtual
3491 ~CommandObjectTargetModules()
3492 {
3493 }
3494
3495private:
3496 //------------------------------------------------------------------
3497 // For CommandObjectTargetModules only
3498 //------------------------------------------------------------------
3499 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3500};
3501
3502
Greg Clayton3508c382012-02-24 01:59:29 +00003503
3504class CommandObjectTargetSymbolsAdd : public CommandObject
3505{
3506public:
3507 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
3508 CommandObject (interpreter,
3509 "target symbols add",
3510 "Add a debug symbol file to one of the target's current modules.",
3511 "target symbols add [<symfile>]")
3512 {
3513 }
3514
3515 virtual
3516 ~CommandObjectTargetSymbolsAdd ()
3517 {
3518 }
3519
3520 virtual bool
3521 Execute (Args& args,
3522 CommandReturnObject &result)
3523 {
3524 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3525 if (target == NULL)
3526 {
3527 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3528 result.SetStatus (eReturnStatusFailed);
3529 return false;
3530 }
3531 else
3532 {
3533 const size_t argc = args.GetArgumentCount();
3534 if (argc == 0)
3535 {
3536 result.AppendError ("one or more symbol file paths must be specified");
3537 result.SetStatus (eReturnStatusFailed);
3538 return false;
3539 }
3540 else
3541 {
3542 for (size_t i=0; i<argc; ++i)
3543 {
3544 const char *symfile_path = args.GetArgumentAtIndex(i);
3545 if (symfile_path)
3546 {
3547 FileSpec symfile_spec(symfile_path, true);
3548 ArchSpec arch;
3549 if (symfile_spec.Exists())
3550 {
3551 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
3552 if (symfile_module_sp)
3553 {
3554 // We now have a module that represents a symbol file
3555 // that can be used for a module that might exist in the
3556 // current target, so we need to find that module in the
3557 // target
3558
3559 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_module_sp->GetUUID()));
3560 if (old_module_sp)
3561 {
Greg Claytond4f16c82012-03-29 21:43:25 +00003562 // The module has not yet created its symbol vendor, we can just
3563 // give the existing target module the symfile path to use for
3564 // when it decides to create it!
3565 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
Greg Clayton3508c382012-02-24 01:59:29 +00003566
Greg Claytond4f16c82012-03-29 21:43:25 +00003567 // Let clients know something changed in the module
3568 // if it is currently loaded
3569 ModuleList module_list;
3570 module_list.Append (old_module_sp);
3571 target->ModulesDidLoad (module_list);
Greg Clayton3508c382012-02-24 01:59:29 +00003572 }
3573 }
3574 else
3575 {
3576 result.AppendError ("one or more executable image paths must be specified");
3577 result.SetStatus (eReturnStatusFailed);
3578 return false;
3579 }
3580 result.SetStatus (eReturnStatusSuccessFinishResult);
3581 }
3582 else
3583 {
3584 char resolved_symfile_path[PATH_MAX];
3585 result.SetStatus (eReturnStatusFailed);
3586 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
3587 {
3588 if (strcmp (resolved_symfile_path, symfile_path) != 0)
3589 {
3590 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
3591 break;
3592 }
3593 }
3594 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
3595 break;
3596 }
3597 }
3598 }
3599 }
3600 }
3601 return result.Succeeded();
3602 }
3603
3604 int
3605 HandleArgumentCompletion (Args &input,
3606 int &cursor_index,
3607 int &cursor_char_position,
3608 OptionElementVector &opt_element_vector,
3609 int match_start_point,
3610 int max_return_elements,
3611 bool &word_complete,
3612 StringList &matches)
3613 {
3614 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
3615 completion_str.erase (cursor_char_position);
3616
3617 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
3618 CommandCompletions::eDiskFileCompletion,
3619 completion_str.c_str(),
3620 match_start_point,
3621 max_return_elements,
3622 NULL,
3623 word_complete,
3624 matches);
3625 return matches.GetSize();
3626 }
3627
3628};
3629
3630
3631#pragma mark CommandObjectTargetSymbols
3632
3633//-------------------------------------------------------------------------
3634// CommandObjectTargetSymbols
3635//-------------------------------------------------------------------------
3636
3637class CommandObjectTargetSymbols : public CommandObjectMultiword
3638{
3639public:
3640 //------------------------------------------------------------------
3641 // Constructors and Destructors
3642 //------------------------------------------------------------------
3643 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
3644 CommandObjectMultiword (interpreter,
3645 "target symbols",
3646 "A set of commands for adding and managing debug symbol files.",
3647 "target symbols <sub-command> ...")
3648 {
3649 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
3650
3651 }
3652 virtual
3653 ~CommandObjectTargetSymbols()
3654 {
3655 }
3656
3657private:
3658 //------------------------------------------------------------------
3659 // For CommandObjectTargetModules only
3660 //------------------------------------------------------------------
3661 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
3662};
3663
3664
Jim Inghamd60d94a2011-03-11 03:53:59 +00003665#pragma mark CommandObjectTargetStopHookAdd
3666
3667//-------------------------------------------------------------------------
3668// CommandObjectTargetStopHookAdd
3669//-------------------------------------------------------------------------
3670
3671class CommandObjectTargetStopHookAdd : public CommandObject
3672{
3673public:
3674
3675 class CommandOptions : public Options
3676 {
3677 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00003678 CommandOptions (CommandInterpreter &interpreter) :
3679 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00003680 m_line_start(0),
3681 m_line_end (UINT_MAX),
3682 m_func_name_type_mask (eFunctionNameTypeAuto),
3683 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00003684 m_thread_specified (false),
3685 m_use_one_liner (false),
3686 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003687 {
3688 }
3689
3690 ~CommandOptions () {}
3691
Greg Claytonb3448432011-03-24 21:19:54 +00003692 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00003693 GetDefinitions ()
3694 {
3695 return g_option_table;
3696 }
3697
3698 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00003699 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003700 {
3701 Error error;
3702 char short_option = (char) m_getopt_table[option_idx].val;
3703 bool success;
3704
3705 switch (short_option)
3706 {
3707 case 'c':
3708 m_class_name = option_arg;
3709 m_sym_ctx_specified = true;
3710 break;
3711
3712 case 'e':
3713 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
3714 if (!success)
3715 {
Greg Clayton9c236732011-10-26 00:56:27 +00003716 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003717 break;
3718 }
3719 m_sym_ctx_specified = true;
3720 break;
3721
3722 case 'l':
3723 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
3724 if (!success)
3725 {
Greg Clayton9c236732011-10-26 00:56:27 +00003726 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003727 break;
3728 }
3729 m_sym_ctx_specified = true;
3730 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00003731
3732 case 'i':
3733 m_no_inlines = true;
3734 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003735
3736 case 'n':
3737 m_function_name = option_arg;
3738 m_func_name_type_mask |= eFunctionNameTypeAuto;
3739 m_sym_ctx_specified = true;
3740 break;
3741
3742 case 'f':
3743 m_file_name = option_arg;
3744 m_sym_ctx_specified = true;
3745 break;
3746 case 's':
3747 m_module_name = option_arg;
3748 m_sym_ctx_specified = true;
3749 break;
3750 case 't' :
3751 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003752 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003753 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00003754 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003755 m_thread_specified = true;
3756 }
3757 break;
3758 case 'T':
3759 m_thread_name = option_arg;
3760 m_thread_specified = true;
3761 break;
3762 case 'q':
3763 m_queue_name = option_arg;
3764 m_thread_specified = true;
3765 break;
3766 case 'x':
3767 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003768 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003769 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003770 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003771 m_thread_specified = true;
3772 }
3773 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003774 case 'o':
3775 m_use_one_liner = true;
3776 m_one_liner = option_arg;
3777 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003778 default:
Greg Clayton9c236732011-10-26 00:56:27 +00003779 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003780 break;
3781 }
3782 return error;
3783 }
3784
3785 void
Greg Clayton143fcc32011-04-13 00:18:08 +00003786 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003787 {
3788 m_class_name.clear();
3789 m_function_name.clear();
3790 m_line_start = 0;
3791 m_line_end = UINT_MAX;
3792 m_file_name.clear();
3793 m_module_name.clear();
3794 m_func_name_type_mask = eFunctionNameTypeAuto;
3795 m_thread_id = LLDB_INVALID_THREAD_ID;
3796 m_thread_index = UINT32_MAX;
3797 m_thread_name.clear();
3798 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00003799
3800 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003801 m_sym_ctx_specified = false;
3802 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003803
3804 m_use_one_liner = false;
3805 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003806 }
3807
3808
Greg Claytonb3448432011-03-24 21:19:54 +00003809 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00003810
3811 std::string m_class_name;
3812 std::string m_function_name;
3813 uint32_t m_line_start;
3814 uint32_t m_line_end;
3815 std::string m_file_name;
3816 std::string m_module_name;
3817 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
3818 lldb::tid_t m_thread_id;
3819 uint32_t m_thread_index;
3820 std::string m_thread_name;
3821 std::string m_queue_name;
3822 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00003823 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003824 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00003825 // Instance variables to hold the values for one_liner options.
3826 bool m_use_one_liner;
3827 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003828 };
3829
3830 Options *
3831 GetOptions ()
3832 {
3833 return &m_options;
3834 }
3835
3836 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
3837 CommandObject (interpreter,
3838 "target stop-hook add ",
3839 "Add a hook to be executed when the target stops.",
Greg Claytonf15996e2011-04-07 22:46:35 +00003840 "target stop-hook add"),
3841 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003842 {
3843 }
3844
3845 ~CommandObjectTargetStopHookAdd ()
3846 {
3847 }
3848
3849 static size_t
3850 ReadCommandsCallbackFunction (void *baton,
3851 InputReader &reader,
3852 lldb::InputReaderAction notification,
3853 const char *bytes,
3854 size_t bytes_len)
3855 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003856 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003857 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00003858 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00003859 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003860
3861 switch (notification)
3862 {
3863 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00003864 if (!batch_mode)
3865 {
3866 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
3867 if (reader.GetPrompt())
3868 out_stream->Printf ("%s", reader.GetPrompt());
3869 out_stream->Flush();
3870 }
Jim Inghame15511a2011-05-05 01:03:36 +00003871 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003872 break;
3873
3874 case eInputReaderDeactivate:
3875 break;
3876
3877 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00003878 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003879 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003880 out_stream->Printf ("%s", reader.GetPrompt());
3881 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003882 }
Jim Inghame15511a2011-05-05 01:03:36 +00003883 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003884 break;
3885
Caroline Tice4a348082011-05-02 20:41:46 +00003886 case eInputReaderAsynchronousOutputWritten:
3887 break;
3888
Jim Inghamd60d94a2011-03-11 03:53:59 +00003889 case eInputReaderGotToken:
3890 if (bytes && bytes_len && baton)
3891 {
3892 StringList *commands = new_stop_hook->GetCommandPointer();
3893 if (commands)
3894 {
3895 commands->AppendString (bytes, bytes_len);
3896 }
3897 }
Caroline Tice892fadd2011-06-16 16:27:19 +00003898 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003899 {
Caroline Tice892fadd2011-06-16 16:27:19 +00003900 out_stream->Printf ("%s", reader.GetPrompt());
3901 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00003902 }
3903 break;
3904
3905 case eInputReaderInterrupt:
3906 {
3907 // Finish, and cancel the stop hook.
3908 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00003909 if (!batch_mode)
3910 {
3911 out_stream->Printf ("Stop hook cancelled.\n");
3912 out_stream->Flush();
3913 }
3914
Jim Inghamd60d94a2011-03-11 03:53:59 +00003915 reader.SetIsDone (true);
3916 }
Jim Inghame15511a2011-05-05 01:03:36 +00003917 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003918 break;
3919
3920 case eInputReaderEndOfFile:
3921 reader.SetIsDone (true);
3922 break;
3923
3924 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00003925 if (!got_interrupted && !batch_mode)
3926 {
Greg Clayton444e35b2011-10-19 18:09:39 +00003927 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00003928 out_stream->Flush();
3929 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00003930 break;
3931 }
3932
3933 return bytes_len;
3934 }
3935
3936 bool
3937 Execute (Args& command,
3938 CommandReturnObject &result)
3939 {
3940 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3941 if (target)
3942 {
3943 Target::StopHookSP new_hook_sp;
3944 target->AddStopHook (new_hook_sp);
3945
3946 // First step, make the specifier.
3947 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
3948 if (m_options.m_sym_ctx_specified)
3949 {
3950 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
3951
3952 if (!m_options.m_module_name.empty())
3953 {
3954 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
3955 }
3956
3957 if (!m_options.m_class_name.empty())
3958 {
3959 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
3960 }
3961
3962 if (!m_options.m_file_name.empty())
3963 {
3964 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
3965 }
3966
3967 if (m_options.m_line_start != 0)
3968 {
3969 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
3970 }
3971
3972 if (m_options.m_line_end != UINT_MAX)
3973 {
3974 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
3975 }
3976
3977 if (!m_options.m_function_name.empty())
3978 {
3979 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
3980 }
3981 }
3982
3983 if (specifier_ap.get())
3984 new_hook_sp->SetSpecifier (specifier_ap.release());
3985
3986 // Next see if any of the thread options have been entered:
3987
3988 if (m_options.m_thread_specified)
3989 {
3990 ThreadSpec *thread_spec = new ThreadSpec();
3991
3992 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
3993 {
3994 thread_spec->SetTID (m_options.m_thread_id);
3995 }
3996
3997 if (m_options.m_thread_index != UINT32_MAX)
3998 thread_spec->SetIndex (m_options.m_thread_index);
3999
4000 if (!m_options.m_thread_name.empty())
4001 thread_spec->SetName (m_options.m_thread_name.c_str());
4002
4003 if (!m_options.m_queue_name.empty())
4004 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4005
4006 new_hook_sp->SetThreadSpecifier (thread_spec);
4007
4008 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004009 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004010 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004011 // Use one-liner.
4012 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004013 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004014 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004015 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004016 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004017 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4018 // the new stop hook's command string.
4019 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4020 if (!reader_sp)
4021 {
4022 result.AppendError("out of memory\n");
4023 result.SetStatus (eReturnStatusFailed);
4024 target->RemoveStopHookByID (new_hook_sp->GetID());
4025 return false;
4026 }
4027
4028 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4029 new_hook_sp.get(), // baton
4030 eInputReaderGranularityLine, // token size, to pass to callback function
4031 "DONE", // end token
4032 "> ", // prompt
4033 true)); // echo input
4034 if (!err.Success())
4035 {
4036 result.AppendError (err.AsCString());
4037 result.SetStatus (eReturnStatusFailed);
4038 target->RemoveStopHookByID (new_hook_sp->GetID());
4039 return false;
4040 }
4041 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004042 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004043 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4044 }
4045 else
4046 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004047 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004048 result.SetStatus (eReturnStatusFailed);
4049 }
4050
4051 return result.Succeeded();
4052 }
4053private:
4054 CommandOptions m_options;
4055};
4056
Greg Claytonb3448432011-03-24 21:19:54 +00004057OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004058CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4059{
Johnny Chen60fe60e2011-05-02 23:47:55 +00004060 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
4061 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004062 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4063 "Set the module within which the stop-hook is to be run."},
4064 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
4065 "The stop hook is run only for the thread whose index matches this argument."},
4066 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
4067 "The stop hook is run only for the thread whose TID matches this argument."},
4068 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
4069 "The stop hook is run only for the thread whose thread name matches this argument."},
4070 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
4071 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4072 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4073 "Specify the source file within which the stop-hook is to be run." },
4074 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4075 "Set the start of the line range for which the stop-hook is to be run."},
4076 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4077 "Set the end of the line range for which the stop-hook is to be run."},
4078 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
4079 "Specify the class within which the stop-hook is to be run." },
4080 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4081 "Set the function name within which the stop hook will be run." },
4082 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4083};
4084
4085#pragma mark CommandObjectTargetStopHookDelete
4086
4087//-------------------------------------------------------------------------
4088// CommandObjectTargetStopHookDelete
4089//-------------------------------------------------------------------------
4090
4091class CommandObjectTargetStopHookDelete : public CommandObject
4092{
4093public:
4094
4095 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
4096 CommandObject (interpreter,
Jason Molenda3fccc902011-11-10 23:03:44 +00004097 "target stop-hook delete",
Jim Inghamd60d94a2011-03-11 03:53:59 +00004098 "Delete a stop-hook.",
Jason Molenda3fccc902011-11-10 23:03:44 +00004099 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004100 {
4101 }
4102
4103 ~CommandObjectTargetStopHookDelete ()
4104 {
4105 }
4106
4107 bool
4108 Execute (Args& command,
4109 CommandReturnObject &result)
4110 {
4111 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4112 if (target)
4113 {
4114 // FIXME: see if we can use the breakpoint id style parser?
4115 size_t num_args = command.GetArgumentCount();
4116 if (num_args == 0)
4117 {
4118 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4119 {
4120 result.SetStatus (eReturnStatusFailed);
4121 return false;
4122 }
4123 else
4124 {
4125 target->RemoveAllStopHooks();
4126 }
4127 }
4128 else
4129 {
4130 bool success;
4131 for (size_t i = 0; i < num_args; i++)
4132 {
4133 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4134 if (!success)
4135 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004136 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004137 result.SetStatus(eReturnStatusFailed);
4138 return false;
4139 }
4140 success = target->RemoveStopHookByID (user_id);
4141 if (!success)
4142 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004143 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004144 result.SetStatus(eReturnStatusFailed);
4145 return false;
4146 }
4147 }
4148 }
4149 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4150 }
4151 else
4152 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004153 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004154 result.SetStatus (eReturnStatusFailed);
4155 }
4156
4157 return result.Succeeded();
4158 }
4159};
4160#pragma mark CommandObjectTargetStopHookEnableDisable
4161
4162//-------------------------------------------------------------------------
4163// CommandObjectTargetStopHookEnableDisable
4164//-------------------------------------------------------------------------
4165
4166class CommandObjectTargetStopHookEnableDisable : public CommandObject
4167{
4168public:
4169
4170 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
4171 CommandObject (interpreter,
4172 name,
4173 help,
4174 syntax),
4175 m_enable (enable)
4176 {
4177 }
4178
4179 ~CommandObjectTargetStopHookEnableDisable ()
4180 {
4181 }
4182
4183 bool
4184 Execute (Args& command,
4185 CommandReturnObject &result)
4186 {
4187 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4188 if (target)
4189 {
4190 // FIXME: see if we can use the breakpoint id style parser?
4191 size_t num_args = command.GetArgumentCount();
4192 bool success;
4193
4194 if (num_args == 0)
4195 {
4196 target->SetAllStopHooksActiveState (m_enable);
4197 }
4198 else
4199 {
4200 for (size_t i = 0; i < num_args; i++)
4201 {
4202 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4203 if (!success)
4204 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004205 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004206 result.SetStatus(eReturnStatusFailed);
4207 return false;
4208 }
4209 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4210 if (!success)
4211 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004212 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004213 result.SetStatus(eReturnStatusFailed);
4214 return false;
4215 }
4216 }
4217 }
4218 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4219 }
4220 else
4221 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004222 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004223 result.SetStatus (eReturnStatusFailed);
4224 }
4225 return result.Succeeded();
4226 }
4227private:
4228 bool m_enable;
4229};
4230
4231#pragma mark CommandObjectTargetStopHookList
4232
4233//-------------------------------------------------------------------------
4234// CommandObjectTargetStopHookList
4235//-------------------------------------------------------------------------
4236
4237class CommandObjectTargetStopHookList : public CommandObject
4238{
4239public:
4240
4241 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
4242 CommandObject (interpreter,
Jason Molenda3fccc902011-11-10 23:03:44 +00004243 "target stop-hook list",
Jim Inghamd60d94a2011-03-11 03:53:59 +00004244 "List all stop-hooks.",
Jason Molenda3fccc902011-11-10 23:03:44 +00004245 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004246 {
4247 }
4248
4249 ~CommandObjectTargetStopHookList ()
4250 {
4251 }
4252
4253 bool
4254 Execute (Args& command,
4255 CommandReturnObject &result)
4256 {
4257 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004258 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004259 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004260 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004261 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004262 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004263 }
4264
4265 size_t num_hooks = target->GetNumStopHooks ();
4266 if (num_hooks == 0)
4267 {
4268 result.GetOutputStream().PutCString ("No stop hooks.\n");
4269 }
4270 else
4271 {
4272 for (size_t i = 0; i < num_hooks; i++)
4273 {
4274 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4275 if (i > 0)
4276 result.GetOutputStream().PutCString ("\n");
4277 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4278 }
4279 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004280 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004281 return result.Succeeded();
4282 }
4283};
4284
4285#pragma mark CommandObjectMultiwordTargetStopHooks
4286//-------------------------------------------------------------------------
4287// CommandObjectMultiwordTargetStopHooks
4288//-------------------------------------------------------------------------
4289
4290class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4291{
4292public:
4293
4294 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4295 CommandObjectMultiword (interpreter,
4296 "target stop-hook",
4297 "A set of commands for operating on debugger target stop-hooks.",
4298 "target stop-hook <subcommand> [<subcommand-options>]")
4299 {
4300 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4301 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4302 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4303 false,
4304 "target stop-hook disable [<id>]",
4305 "Disable a stop-hook.",
4306 "target stop-hook disable")));
4307 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4308 true,
4309 "target stop-hook enable [<id>]",
4310 "Enable a stop-hook.",
4311 "target stop-hook enable")));
4312 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4313 }
4314
4315 ~CommandObjectMultiwordTargetStopHooks()
4316 {
4317 }
4318};
4319
4320
Chris Lattner24943d22010-06-08 16:52:24 +00004321
4322#pragma mark CommandObjectMultiwordTarget
4323
4324//-------------------------------------------------------------------------
4325// CommandObjectMultiwordTarget
4326//-------------------------------------------------------------------------
4327
Greg Clayton63094e02010-06-23 01:19:29 +00004328CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004329 CommandObjectMultiword (interpreter,
4330 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004331 "A set of commands for operating on debugger targets.",
4332 "target <subcommand> [<subcommand-options>]")
4333{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004334
4335 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004336 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004337 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4338 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004339 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004340 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00004341 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004342 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004343}
4344
4345CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4346{
4347}
4348
Greg Claytonabe0fed2011-04-18 08:33:37 +00004349