blob: 1978606f3ebc7d81310c921d0e10b7f83b0a1a8f [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
Jim Inghamda26bd22012-06-08 21:56:10 +0000141class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000142{
143public:
144 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000145 CommandObjectParsed (interpreter,
146 "target create",
147 "Create a target using the argument as the main executable.",
148 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000149 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
Jim Inghamda26bd22012-06-08 21:56:10 +0000183 int
184 HandleArgumentCompletion (Args &input,
185 int &cursor_index,
186 int &cursor_char_position,
187 OptionElementVector &opt_element_vector,
188 int match_start_point,
189 int max_return_elements,
190 bool &word_complete,
191 StringList &matches)
192 {
193 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
194 completion_str.erase (cursor_char_position);
195
196 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
197 CommandCompletions::eDiskFileCompletion,
198 completion_str.c_str(),
199 match_start_point,
200 max_return_elements,
201 NULL,
202 word_complete,
203 matches);
204 return matches.GetSize();
205 }
206
207protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000208 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000209 DoExecute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000210 {
211 const int argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000212 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
213
214 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000215 {
216 const char *file_path = command.GetArgumentAtIndex(0);
217 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000218 FileSpec file_spec;
219
220 if (file_path)
221 file_spec.SetFile (file_path, true);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000222
Greg Claytonabe0fed2011-04-18 08:33:37 +0000223 TargetSP target_sp;
224 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000225 const char *arch_cstr = m_arch_option.GetArchitectureName();
226 const bool get_dependent_files = true;
227 Error error (debugger.GetTargetList().CreateTarget (debugger,
228 file_spec,
229 arch_cstr,
230 get_dependent_files,
231 &m_platform_options,
232 target_sp));
233
Greg Claytonabe0fed2011-04-18 08:33:37 +0000234 if (target_sp)
235 {
236 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000237 if (core_file)
238 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000239 char core_path[PATH_MAX];
240 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000241 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000242 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000243 FileSpec core_file_dir;
244 core_file_dir.GetDirectory() = core_file.GetDirectory();
245 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000246
Greg Clayton9ce95382012-02-13 23:10:39 +0000247 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
248
249 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000250 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000251 // Seems wierd that we Launch a core file, but that is
252 // what we do!
253 error = process_sp->LoadCore();
254
255 if (error.Fail())
256 {
257 result.AppendError(error.AsCString("can't find plug-in for core file"));
258 result.SetStatus (eReturnStatusFailed);
259 return false;
260 }
261 else
262 {
263 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
264 result.SetStatus (eReturnStatusSuccessFinishNoResult);
265 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000266 }
267 else
268 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000269 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
270 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000271 }
272 }
273 else
274 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000275 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000276 result.SetStatus (eReturnStatusFailed);
277 }
278 }
279 else
280 {
281 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
282 result.SetStatus (eReturnStatusSuccessFinishNoResult);
283 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000284 }
285 else
286 {
287 result.AppendError(error.AsCString());
288 result.SetStatus (eReturnStatusFailed);
289 }
290 }
291 else
292 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000293 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 +0000294 result.SetStatus (eReturnStatusFailed);
295 }
296 return result.Succeeded();
297
298 }
299
Greg Claytonabe0fed2011-04-18 08:33:37 +0000300private:
301 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000302 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000303 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000304 OptionGroupFile m_core_file;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000305
306};
307
308#pragma mark CommandObjectTargetList
309
310//----------------------------------------------------------------------
311// "target list"
312//----------------------------------------------------------------------
313
Jim Inghamda26bd22012-06-08 21:56:10 +0000314class CommandObjectTargetList : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000315{
316public:
317 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000318 CommandObjectParsed (interpreter,
319 "target list",
320 "List all current targets in the current debug session.",
321 NULL,
322 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000323 {
324 }
325
326 virtual
327 ~CommandObjectTargetList ()
328 {
329 }
330
Jim Inghamda26bd22012-06-08 21:56:10 +0000331protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000332 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000333 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000334 {
335 if (args.GetArgumentCount() == 0)
336 {
337 Stream &strm = result.GetOutputStream();
338
339 bool show_stopped_process_status = false;
340 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
341 {
342 strm.PutCString ("No targets.\n");
343 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000344 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000345 }
346 else
347 {
348 result.AppendError ("the 'target list' command takes no arguments\n");
349 result.SetStatus (eReturnStatusFailed);
350 }
351 return result.Succeeded();
352 }
353};
354
355
356#pragma mark CommandObjectTargetSelect
357
358//----------------------------------------------------------------------
359// "target select"
360//----------------------------------------------------------------------
361
Jim Inghamda26bd22012-06-08 21:56:10 +0000362class CommandObjectTargetSelect : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000363{
364public:
365 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000366 CommandObjectParsed (interpreter,
367 "target select",
368 "Select a target as the current target by target index.",
369 NULL,
370 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000371 {
372 }
373
374 virtual
375 ~CommandObjectTargetSelect ()
376 {
377 }
378
Jim Inghamda26bd22012-06-08 21:56:10 +0000379protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000380 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000381 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000382 {
383 if (args.GetArgumentCount() == 1)
384 {
385 bool success = false;
386 const char *target_idx_arg = args.GetArgumentAtIndex(0);
387 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
388 if (success)
389 {
390 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
391 const uint32_t num_targets = target_list.GetNumTargets();
392 if (target_idx < num_targets)
393 {
394 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
395 if (target_sp)
396 {
397 Stream &strm = result.GetOutputStream();
398 target_list.SetSelectedTarget (target_sp.get());
399 bool show_stopped_process_status = false;
400 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000401 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000402 }
403 else
404 {
405 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
406 result.SetStatus (eReturnStatusFailed);
407 }
408 }
409 else
410 {
411 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
412 target_idx,
413 num_targets - 1);
414 result.SetStatus (eReturnStatusFailed);
415 }
416 }
417 else
418 {
419 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
420 result.SetStatus (eReturnStatusFailed);
421 }
422 }
423 else
424 {
425 result.AppendError ("'target select' takes a single argument: a target index\n");
426 result.SetStatus (eReturnStatusFailed);
427 }
428 return result.Succeeded();
429 }
430};
431
Greg Clayton153ccd72011-08-10 02:10:13 +0000432#pragma mark CommandObjectTargetSelect
433
434//----------------------------------------------------------------------
435// "target delete"
436//----------------------------------------------------------------------
437
Jim Inghamda26bd22012-06-08 21:56:10 +0000438class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton153ccd72011-08-10 02:10:13 +0000439{
440public:
441 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000442 CommandObjectParsed (interpreter,
443 "target delete",
444 "Delete one or more targets by target index.",
445 NULL,
446 0),
Greg Clayton5beb99d2011-08-11 02:48:45 +0000447 m_option_group (interpreter),
448 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 +0000449 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000450 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
451 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000452 }
453
454 virtual
455 ~CommandObjectTargetDelete ()
456 {
457 }
458
Jim Inghamda26bd22012-06-08 21:56:10 +0000459 Options *
460 GetOptions ()
461 {
462 return &m_option_group;
463 }
464
465protected:
Greg Clayton153ccd72011-08-10 02:10:13 +0000466 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000467 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton153ccd72011-08-10 02:10:13 +0000468 {
469 const size_t argc = args.GetArgumentCount();
470 std::vector<TargetSP> delete_target_list;
471 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
472 bool success = true;
473 TargetSP target_sp;
474 if (argc > 0)
475 {
476 const uint32_t num_targets = target_list.GetNumTargets();
477 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
478 {
479 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
480 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
481 if (success)
482 {
483 if (target_idx < num_targets)
484 {
485 target_sp = target_list.GetTargetAtIndex (target_idx);
486 if (target_sp)
487 {
488 delete_target_list.push_back (target_sp);
489 continue;
490 }
491 }
492 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
493 target_idx,
494 num_targets - 1);
495 result.SetStatus (eReturnStatusFailed);
496 success = false;
497 }
498 else
499 {
500 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
501 result.SetStatus (eReturnStatusFailed);
502 success = false;
503 }
504 }
505
506 }
507 else
508 {
509 target_sp = target_list.GetSelectedTarget();
510 if (target_sp)
511 {
512 delete_target_list.push_back (target_sp);
513 }
514 else
515 {
516 result.AppendErrorWithFormat("no target is currently selected\n");
517 result.SetStatus (eReturnStatusFailed);
518 success = false;
519 }
520 }
521 if (success)
522 {
523 const size_t num_targets_to_delete = delete_target_list.size();
524 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
525 {
526 target_sp = delete_target_list[idx];
527 target_list.DeleteTarget(target_sp);
528 target_sp->Destroy();
529 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000530 // If "--clean" was specified, prune any orphaned shared modules from
531 // the global shared module list
532 if (m_cleanup_option.GetOptionValue ())
533 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000534 const bool mandatory = true;
535 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000536 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000537 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
538 result.SetStatus(eReturnStatusSuccessFinishResult);
539 }
540
541 return result.Succeeded();
542 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000543
Greg Clayton5beb99d2011-08-11 02:48:45 +0000544 OptionGroupOptions m_option_group;
545 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000546};
547
Greg Claytonabe0fed2011-04-18 08:33:37 +0000548
Greg Clayton801417e2011-07-07 01:59:51 +0000549#pragma mark CommandObjectTargetVariable
550
551//----------------------------------------------------------------------
552// "target variable"
553//----------------------------------------------------------------------
554
Jim Inghamda26bd22012-06-08 21:56:10 +0000555class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000556{
557public:
558 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000559 CommandObjectParsed (interpreter,
560 "target variable",
561 "Read global variable(s) prior to running your binary.",
562 NULL,
563 0),
Greg Clayton801417e2011-07-07 01:59:51 +0000564 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000565 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000566 m_option_format (eFormatDefault),
Greg Clayton801417e2011-07-07 01:59:51 +0000567 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."),
568 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."),
569 m_varobj_options()
570 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000571 CommandArgumentEntry arg;
572 CommandArgumentData var_name_arg;
573
574 // Define the first (and only) variant of this arg.
575 var_name_arg.arg_type = eArgTypeVarName;
576 var_name_arg.arg_repetition = eArgRepeatPlus;
577
578 // There is only one variant this argument could be; put it into the argument entry.
579 arg.push_back (var_name_arg);
580
581 // Push the data for the first argument into the m_arguments vector.
582 m_arguments.push_back (arg);
583
Greg Clayton801417e2011-07-07 01:59:51 +0000584 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000585 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000586 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 +0000587 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
588 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
589 m_option_group.Finalize();
590 }
591
592 virtual
593 ~CommandObjectTargetVariable ()
594 {
595 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000596
597 void
598 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
599 {
Enrico Granata19030d82011-08-15 18:01:31 +0000600 ValueObject::DumpValueObjectOptions options;
601
Enrico Granata3069c622012-03-01 04:24:26 +0000602 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000603 .SetMaximumDepth(m_varobj_options.max_depth)
604 .SetShowTypes(m_varobj_options.show_types)
605 .SetShowLocation(m_varobj_options.show_location)
606 .SetUseObjectiveC(m_varobj_options.use_objc)
607 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000608 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000609 .SetFlatOutput(m_varobj_options.flat_output)
610 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
611 .SetIgnoreCap(m_varobj_options.ignore_cap);
612
Greg Clayton5d81f492011-07-08 21:46:14 +0000613 switch (var_sp->GetScope())
614 {
615 case eValueTypeVariableGlobal:
616 if (m_option_variable.show_scope)
617 s.PutCString("GLOBAL: ");
618 break;
619
620 case eValueTypeVariableStatic:
621 if (m_option_variable.show_scope)
622 s.PutCString("STATIC: ");
623 break;
624
625 case eValueTypeVariableArgument:
626 if (m_option_variable.show_scope)
627 s.PutCString(" ARG: ");
628 break;
629
630 case eValueTypeVariableLocal:
631 if (m_option_variable.show_scope)
632 s.PutCString(" LOCAL: ");
633 break;
634
635 default:
636 break;
637 }
638
Greg Claytonfb816422011-07-10 19:21:23 +0000639 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000640 {
Greg Claytonfb816422011-07-10 19:21:23 +0000641 bool show_fullpaths = false;
642 bool show_module = true;
643 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
644 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000645 }
646
Greg Claytona42880a2011-10-25 06:44:01 +0000647 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000648 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000649 options.SetFormat(format);
650
651 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000652
653 ValueObject::DumpValueObject (s,
654 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000655 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000656
657 }
Greg Clayton801417e2011-07-07 01:59:51 +0000658
Greg Clayton5d81f492011-07-08 21:46:14 +0000659
660 static uint32_t GetVariableCallback (void *baton,
661 const char *name,
662 VariableList &variable_list)
663 {
664 Target *target = static_cast<Target *>(baton);
665 if (target)
666 {
667 return target->GetImages().FindGlobalVariables (ConstString(name),
668 true,
669 UINT32_MAX,
670 variable_list);
671 }
672 return 0;
673 }
674
675
676
Jim Inghamda26bd22012-06-08 21:56:10 +0000677 Options *
678 GetOptions ()
679 {
680 return &m_option_group;
681 }
682
683protected:
Greg Clayton801417e2011-07-07 01:59:51 +0000684 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000685 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000686 {
687 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000688 Target *target = exe_ctx.GetTargetPtr();
689 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000690 {
691 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000692 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000693 if (argc > 0)
694 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000695
Greg Clayton801417e2011-07-07 01:59:51 +0000696 for (size_t idx = 0; idx < argc; ++idx)
697 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000698 VariableList variable_list;
699 ValueObjectList valobj_list;
700
Greg Clayton368f8222011-07-07 04:38:25 +0000701 const char *arg = args.GetArgumentAtIndex(idx);
702 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000703 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000704 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000705 {
Greg Clayton368f8222011-07-07 04:38:25 +0000706 RegularExpression regex(arg);
707 if (!regex.IsValid ())
708 {
709 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
710 result.SetStatus (eReturnStatusFailed);
711 return false;
712 }
Greg Claytonfb816422011-07-10 19:21:23 +0000713 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000714 matches = target->GetImages().FindGlobalVariables (regex,
715 true,
716 UINT32_MAX,
717 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000718 }
719 else
720 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000721 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000722 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000723 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000724 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000725 variable_list,
726 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000727 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000728 }
729
730 if (matches == 0)
731 {
732 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
733 result.SetStatus (eReturnStatusFailed);
734 return false;
735 }
736 else
737 {
Greg Clayton801417e2011-07-07 01:59:51 +0000738 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
739 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000740 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000741 if (var_sp)
742 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000743 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
744 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000745 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000746
747 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000748 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000749 }
750 }
751 }
752 }
753 }
754 else
755 {
Greg Claytonfac93882011-10-05 22:17:32 +0000756 bool success = false;
757 StackFrame *frame = exe_ctx.GetFramePtr();
758 CompileUnit *comp_unit = NULL;
759 if (frame)
760 {
761 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
762 if (comp_unit)
763 {
764 const bool can_create = true;
765 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
766 if (comp_unit_varlist_sp)
767 {
768 size_t count = comp_unit_varlist_sp->GetSize();
769 if (count > 0)
770 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000771 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000772 comp_unit->GetDirectory().GetCString(),
773 comp_unit->GetFilename().GetCString());
774
775 success = true;
776 for (uint32_t i=0; i<count; ++i)
777 {
778 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
779 if (var_sp)
780 {
781 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
782
783 if (valobj_sp)
784 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
785 }
786 }
787 }
788 }
789 }
790 }
791 if (!success)
792 {
793 if (frame)
794 {
795 if (comp_unit)
796 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
797 comp_unit->GetDirectory().GetCString(),
798 comp_unit->GetFilename().GetCString());
799 else
800 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
801 }
802 else
803 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
804 result.SetStatus (eReturnStatusFailed);
805 }
Greg Clayton801417e2011-07-07 01:59:51 +0000806 }
807 }
808 else
809 {
810 result.AppendError ("invalid target, create a debug target using the 'target create' command");
811 result.SetStatus (eReturnStatusFailed);
812 return false;
813 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000814
815 if (m_interpreter.TruncationWarningNecessary())
816 {
817 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
818 m_cmd_name.c_str());
819 m_interpreter.TruncationWarningGiven();
820 }
821
Greg Clayton801417e2011-07-07 01:59:51 +0000822 return result.Succeeded();
823 }
824
Greg Clayton801417e2011-07-07 01:59:51 +0000825 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000826 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000827 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000828 OptionGroupFileList m_option_compile_units;
829 OptionGroupFileList m_option_shared_libraries;
830 OptionGroupValueObjectDisplay m_varobj_options;
831
832};
833
834
Greg Claytone1f50b92011-05-03 22:09:39 +0000835#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000836
Jim Inghamda26bd22012-06-08 21:56:10 +0000837class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000838{
839public:
840
Greg Claytone1f50b92011-05-03 22:09:39 +0000841 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000842 CommandObjectParsed (interpreter,
843 "target modules search-paths add",
844 "Add new image search paths substitution pairs to the current target.",
845 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000846 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000847 CommandArgumentEntry arg;
848 CommandArgumentData old_prefix_arg;
849 CommandArgumentData new_prefix_arg;
850
851 // Define the first variant of this arg pair.
852 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
853 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
854
855 // Define the first variant of this arg pair.
856 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
857 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
858
859 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
860 // must always occur together, they are treated as two variants of one argument rather than two independent
861 // arguments. Push them both into the first argument position for m_arguments...
862
863 arg.push_back (old_prefix_arg);
864 arg.push_back (new_prefix_arg);
865
866 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000867 }
868
Greg Claytone1f50b92011-05-03 22:09:39 +0000869 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000870 {
871 }
872
Jim Inghamda26bd22012-06-08 21:56:10 +0000873protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000874 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000875 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000876 CommandReturnObject &result)
877 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000878 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000879 if (target)
880 {
881 uint32_t argc = command.GetArgumentCount();
882 if (argc & 1)
883 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000884 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000885 result.SetStatus (eReturnStatusFailed);
886 }
887 else
888 {
889 for (uint32_t i=0; i<argc; i+=2)
890 {
891 const char *from = command.GetArgumentAtIndex(i);
892 const char *to = command.GetArgumentAtIndex(i+1);
893
894 if (from[0] && to[0])
895 {
896 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000897 target->GetImageSearchPathList().Append (ConstString(from),
898 ConstString(to),
899 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000900 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000901 }
902 else
903 {
904 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000905 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000906 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000907 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000908 result.SetStatus (eReturnStatusFailed);
909 }
910 }
911 }
912 }
913 else
914 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000915 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000916 result.SetStatus (eReturnStatusFailed);
917 }
918 return result.Succeeded();
919 }
920};
921
Greg Claytone1f50b92011-05-03 22:09:39 +0000922#pragma mark CommandObjectTargetModulesSearchPathsClear
923
Jim Inghamda26bd22012-06-08 21:56:10 +0000924class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000925{
926public:
927
Greg Claytone1f50b92011-05-03 22:09:39 +0000928 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000929 CommandObjectParsed (interpreter,
930 "target modules search-paths clear",
931 "Clear all current image search path substitution pairs from the current target.",
932 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000933 {
934 }
935
Greg Claytone1f50b92011-05-03 22:09:39 +0000936 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000937 {
938 }
939
Jim Inghamda26bd22012-06-08 21:56:10 +0000940protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000941 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000942 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000943 CommandReturnObject &result)
944 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000945 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000946 if (target)
947 {
948 bool notify = true;
949 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000950 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000951 }
952 else
953 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000954 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000955 result.SetStatus (eReturnStatusFailed);
956 }
957 return result.Succeeded();
958 }
959};
960
Greg Claytone1f50b92011-05-03 22:09:39 +0000961#pragma mark CommandObjectTargetModulesSearchPathsInsert
962
Jim Inghamda26bd22012-06-08 21:56:10 +0000963class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000964{
965public:
966
Greg Claytone1f50b92011-05-03 22:09:39 +0000967 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000968 CommandObjectParsed (interpreter,
969 "target modules search-paths insert",
970 "Insert a new image search path substitution pair into the current target at the specified index.",
971 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000972 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000973 CommandArgumentEntry arg1;
974 CommandArgumentEntry arg2;
975 CommandArgumentData index_arg;
976 CommandArgumentData old_prefix_arg;
977 CommandArgumentData new_prefix_arg;
978
979 // Define the first and only variant of this arg.
980 index_arg.arg_type = eArgTypeIndex;
981 index_arg.arg_repetition = eArgRepeatPlain;
982
983 // Put the one and only variant into the first arg for m_arguments:
984 arg1.push_back (index_arg);
985
986 // Define the first variant of this arg pair.
987 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
988 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
989
990 // Define the first variant of this arg pair.
991 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
992 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
993
994 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
995 // must always occur together, they are treated as two variants of one argument rather than two independent
996 // arguments. Push them both into the same argument position for m_arguments...
997
998 arg2.push_back (old_prefix_arg);
999 arg2.push_back (new_prefix_arg);
1000
1001 // Add arguments to m_arguments.
1002 m_arguments.push_back (arg1);
1003 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001004 }
1005
Greg Claytone1f50b92011-05-03 22:09:39 +00001006 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001007 {
1008 }
1009
Jim Inghamda26bd22012-06-08 21:56:10 +00001010protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001011 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001012 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001013 CommandReturnObject &result)
1014 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001015 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001016 if (target)
1017 {
1018 uint32_t argc = command.GetArgumentCount();
1019 // check for at least 3 arguments and an odd nubmer of parameters
1020 if (argc >= 3 && argc & 1)
1021 {
1022 bool success = false;
1023
1024 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1025
1026 if (!success)
1027 {
1028 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1029 result.SetStatus (eReturnStatusFailed);
1030 return result.Succeeded();
1031 }
1032
1033 // shift off the index
1034 command.Shift();
1035 argc = command.GetArgumentCount();
1036
1037 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1038 {
1039 const char *from = command.GetArgumentAtIndex(i);
1040 const char *to = command.GetArgumentAtIndex(i+1);
1041
1042 if (from[0] && to[0])
1043 {
1044 bool last_pair = ((argc - i) == 2);
1045 target->GetImageSearchPathList().Insert (ConstString(from),
1046 ConstString(to),
1047 insert_idx,
1048 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001049 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001050 }
1051 else
1052 {
1053 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001054 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001055 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001056 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001057 result.SetStatus (eReturnStatusFailed);
1058 return false;
1059 }
1060 }
1061 }
1062 else
1063 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001064 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001065 result.SetStatus (eReturnStatusFailed);
1066 return result.Succeeded();
1067 }
1068
1069 }
1070 else
1071 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001072 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001073 result.SetStatus (eReturnStatusFailed);
1074 }
1075 return result.Succeeded();
1076 }
1077};
1078
Greg Claytone1f50b92011-05-03 22:09:39 +00001079
1080#pragma mark CommandObjectTargetModulesSearchPathsList
1081
1082
Jim Inghamda26bd22012-06-08 21:56:10 +00001083class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001084{
1085public:
1086
Greg Claytone1f50b92011-05-03 22:09:39 +00001087 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001088 CommandObjectParsed (interpreter,
1089 "target modules search-paths list",
1090 "List all current image search path substitution pairs in the current target.",
1091 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001092 {
1093 }
1094
Greg Claytone1f50b92011-05-03 22:09:39 +00001095 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001096 {
1097 }
1098
Jim Inghamda26bd22012-06-08 21:56:10 +00001099protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001100 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001101 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001102 CommandReturnObject &result)
1103 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001104 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001105 if (target)
1106 {
1107 if (command.GetArgumentCount() != 0)
1108 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001109 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001110 result.SetStatus (eReturnStatusFailed);
1111 return result.Succeeded();
1112 }
1113
1114 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001115 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001116 }
1117 else
1118 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001119 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001120 result.SetStatus (eReturnStatusFailed);
1121 }
1122 return result.Succeeded();
1123 }
1124};
1125
Greg Claytone1f50b92011-05-03 22:09:39 +00001126#pragma mark CommandObjectTargetModulesSearchPathsQuery
1127
Jim Inghamda26bd22012-06-08 21:56:10 +00001128class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001129{
1130public:
1131
Greg Claytone1f50b92011-05-03 22:09:39 +00001132 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001133 CommandObjectParsed (interpreter,
1134 "target modules search-paths query",
1135 "Transform a path using the first applicable image search path.",
1136 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001137 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001138 CommandArgumentEntry arg;
1139 CommandArgumentData path_arg;
1140
1141 // Define the first (and only) variant of this arg.
1142 path_arg.arg_type = eArgTypePath;
1143 path_arg.arg_repetition = eArgRepeatPlain;
1144
1145 // There is only one variant this argument could be; put it into the argument entry.
1146 arg.push_back (path_arg);
1147
1148 // Push the data for the first argument into the m_arguments vector.
1149 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001150 }
1151
Greg Claytone1f50b92011-05-03 22:09:39 +00001152 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001153 {
1154 }
1155
Jim Inghamda26bd22012-06-08 21:56:10 +00001156protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001157 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001158 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001159 CommandReturnObject &result)
1160 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001161 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001162 if (target)
1163 {
1164 if (command.GetArgumentCount() != 1)
1165 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001166 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001167 result.SetStatus (eReturnStatusFailed);
1168 return result.Succeeded();
1169 }
1170
1171 ConstString orig(command.GetArgumentAtIndex(0));
1172 ConstString transformed;
1173 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1174 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1175 else
1176 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001177
1178 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001179 }
1180 else
1181 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001182 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001183 result.SetStatus (eReturnStatusFailed);
1184 }
1185 return result.Succeeded();
1186 }
1187};
1188
Greg Claytone1f50b92011-05-03 22:09:39 +00001189//----------------------------------------------------------------------
1190// Static Helper functions
1191//----------------------------------------------------------------------
1192static void
1193DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1194{
1195 if (module)
1196 {
1197 const char *arch_cstr;
1198 if (full_triple)
1199 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1200 else
1201 arch_cstr = module->GetArchitecture().GetArchitectureName();
1202 if (width)
1203 strm.Printf("%-*s", width, arch_cstr);
1204 else
1205 strm.PutCString(arch_cstr);
1206 }
1207}
1208
1209static void
1210DumpModuleUUID (Stream &strm, Module *module)
1211{
Greg Clayton153ccd72011-08-10 02:10:13 +00001212 if (module->GetUUID().IsValid())
1213 module->GetUUID().Dump (&strm);
1214 else
1215 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001216}
1217
1218static uint32_t
1219DumpCompileUnitLineTable
1220(
1221 CommandInterpreter &interpreter,
1222 Stream &strm,
1223 Module *module,
1224 const FileSpec &file_spec,
1225 bool load_addresses
1226 )
1227{
1228 uint32_t num_matches = 0;
1229 if (module)
1230 {
1231 SymbolContextList sc_list;
1232 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1233 0,
1234 false,
1235 eSymbolContextCompUnit,
1236 sc_list);
1237
1238 for (uint32_t i=0; i<num_matches; ++i)
1239 {
1240 SymbolContext sc;
1241 if (sc_list.GetContextAtIndex(i, sc))
1242 {
1243 if (i > 0)
1244 strm << "\n\n";
1245
1246 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1247 << module->GetFileSpec().GetFilename() << "\n";
1248 LineTable *line_table = sc.comp_unit->GetLineTable();
1249 if (line_table)
1250 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001251 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001252 lldb::eDescriptionLevelBrief);
1253 else
1254 strm << "No line table";
1255 }
1256 }
1257 }
1258 return num_matches;
1259}
1260
1261static void
1262DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1263{
1264 if (file_spec_ptr)
1265 {
1266 if (width > 0)
1267 {
1268 char fullpath[PATH_MAX];
1269 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1270 {
1271 strm.Printf("%-*s", width, fullpath);
1272 return;
1273 }
1274 }
1275 else
1276 {
1277 file_spec_ptr->Dump(&strm);
1278 return;
1279 }
1280 }
1281 // Keep the width spacing correct if things go wrong...
1282 if (width > 0)
1283 strm.Printf("%-*s", width, "");
1284}
1285
1286static void
1287DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1288{
1289 if (file_spec_ptr)
1290 {
1291 if (width > 0)
1292 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1293 else
1294 file_spec_ptr->GetDirectory().Dump(&strm);
1295 return;
1296 }
1297 // Keep the width spacing correct if things go wrong...
1298 if (width > 0)
1299 strm.Printf("%-*s", width, "");
1300}
1301
1302static void
1303DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1304{
1305 if (file_spec_ptr)
1306 {
1307 if (width > 0)
1308 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1309 else
1310 file_spec_ptr->GetFilename().Dump(&strm);
1311 return;
1312 }
1313 // Keep the width spacing correct if things go wrong...
1314 if (width > 0)
1315 strm.Printf("%-*s", width, "");
1316}
1317
1318
1319static void
1320DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1321{
1322 if (module)
1323 {
1324 ObjectFile *objfile = module->GetObjectFile ();
1325 if (objfile)
1326 {
1327 Symtab *symtab = objfile->GetSymtab();
1328 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001329 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001330 }
1331 }
1332}
1333
1334static void
1335DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1336{
1337 if (module)
1338 {
1339 ObjectFile *objfile = module->GetObjectFile ();
1340 if (objfile)
1341 {
1342 SectionList *section_list = objfile->GetSectionList();
1343 if (section_list)
1344 {
1345 strm.PutCString ("Sections for '");
1346 strm << module->GetFileSpec();
1347 if (module->GetObjectName())
1348 strm << '(' << module->GetObjectName() << ')';
1349 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1350 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001351 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001352 strm.IndentLess();
1353 }
1354 }
1355 }
1356}
1357
1358static bool
1359DumpModuleSymbolVendor (Stream &strm, Module *module)
1360{
1361 if (module)
1362 {
1363 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1364 if (symbol_vendor)
1365 {
1366 symbol_vendor->Dump(&strm);
1367 return true;
1368 }
1369 }
1370 return false;
1371}
1372
Greg Clayton2ad894b2012-05-15 18:43:44 +00001373static void
1374DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1375{
1376 strm.IndentMore();
1377 strm.Indent (" Address: ");
1378 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1379 strm.PutCString (" (");
1380 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1381 strm.PutCString (")\n");
1382 strm.Indent (" Summary: ");
1383 const uint32_t save_indent = strm.GetIndentLevel ();
1384 strm.SetIndentLevel (save_indent + 13);
1385 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1386 strm.SetIndentLevel (save_indent);
1387 // Print out detailed address information when verbose is enabled
1388 if (verbose)
1389 {
1390 strm.EOL();
1391 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1392 }
1393 strm.IndentLess();
1394}
1395
Greg Claytone1f50b92011-05-03 22:09:39 +00001396static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001397LookupAddressInModule (CommandInterpreter &interpreter,
1398 Stream &strm,
1399 Module *module,
1400 uint32_t resolve_mask,
1401 lldb::addr_t raw_addr,
1402 lldb::addr_t offset,
1403 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001404{
1405 if (module)
1406 {
1407 lldb::addr_t addr = raw_addr - offset;
1408 Address so_addr;
1409 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001410 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001411 if (target && !target->GetSectionLoadList().IsEmpty())
1412 {
1413 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1414 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001415 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001416 return false;
1417 }
1418 else
1419 {
1420 if (!module->ResolveFileAddress (addr, so_addr))
1421 return false;
1422 }
1423
Greg Claytone1f50b92011-05-03 22:09:39 +00001424 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001425 DumpAddress (exe_scope, so_addr, verbose, strm);
1426// strm.IndentMore();
1427// strm.Indent (" Address: ");
1428// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1429// strm.PutCString (" (");
1430// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1431// strm.PutCString (")\n");
1432// strm.Indent (" Summary: ");
1433// const uint32_t save_indent = strm.GetIndentLevel ();
1434// strm.SetIndentLevel (save_indent + 13);
1435// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1436// strm.SetIndentLevel (save_indent);
1437// // Print out detailed address information when verbose is enabled
1438// if (verbose)
1439// {
1440// strm.EOL();
1441// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1442// }
1443// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001444 return true;
1445 }
1446
1447 return false;
1448}
1449
1450static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001451LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001452{
1453 if (module)
1454 {
1455 SymbolContext sc;
1456
1457 ObjectFile *objfile = module->GetObjectFile ();
1458 if (objfile)
1459 {
1460 Symtab *symtab = objfile->GetSymtab();
1461 if (symtab)
1462 {
1463 uint32_t i;
1464 std::vector<uint32_t> match_indexes;
1465 ConstString symbol_name (name);
1466 uint32_t num_matches = 0;
1467 if (name_is_regex)
1468 {
1469 RegularExpression name_regexp(name);
1470 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1471 eSymbolTypeAny,
1472 match_indexes);
1473 }
1474 else
1475 {
1476 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1477 }
1478
1479
1480 if (num_matches > 0)
1481 {
1482 strm.Indent ();
1483 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1484 name_is_regex ? "the regular expression " : "", name);
1485 DumpFullpath (strm, &module->GetFileSpec(), 0);
1486 strm.PutCString(":\n");
1487 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001488 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001489 for (i=0; i < num_matches; ++i)
1490 {
1491 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001492 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1493 symbol->GetAddress(),
1494 verbose,
1495 strm);
1496
1497// strm.Indent ();
1498// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001499 }
1500 strm.IndentLess ();
1501 return num_matches;
1502 }
1503 }
1504 }
1505 }
1506 return 0;
1507}
1508
1509
1510static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001511DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001512{
1513 strm.IndentMore ();
1514 uint32_t i;
1515 const uint32_t num_matches = sc_list.GetSize();
1516
1517 for (i=0; i<num_matches; ++i)
1518 {
1519 SymbolContext sc;
1520 if (sc_list.GetContextAtIndex(i, sc))
1521 {
Sean Callanand7793d22012-02-11 00:24:04 +00001522 AddressRange range;
1523
1524 sc.GetAddressRange(eSymbolContextEverything,
1525 0,
1526 true,
1527 range);
1528
Greg Clayton2ad894b2012-05-15 18:43:44 +00001529 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001530 }
1531 }
1532 strm.IndentLess ();
1533}
1534
1535static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001536LookupFunctionInModule (CommandInterpreter &interpreter,
1537 Stream &strm,
1538 Module *module,
1539 const char *name,
1540 bool name_is_regex,
1541 bool include_inlines,
1542 bool include_symbols,
1543 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001544{
1545 if (module && name && name[0])
1546 {
1547 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001548 const bool append = true;
1549 uint32_t num_matches = 0;
1550 if (name_is_regex)
1551 {
1552 RegularExpression function_name_regex (name);
1553 num_matches = module->FindFunctions (function_name_regex,
1554 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001555 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001556 append,
1557 sc_list);
1558 }
1559 else
1560 {
1561 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001562 num_matches = module->FindFunctions (function_name,
1563 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001564 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1565 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001566 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001567 append,
1568 sc_list);
1569 }
1570
1571 if (num_matches)
1572 {
1573 strm.Indent ();
1574 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1575 DumpFullpath (strm, &module->GetFileSpec(), 0);
1576 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001577 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001578 }
1579 return num_matches;
1580 }
1581 return 0;
1582}
1583
1584static uint32_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001585LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001586 Stream &strm,
1587 Module *module,
1588 const char *name_cstr,
1589 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001590{
1591 if (module && name_cstr && name_cstr[0])
1592 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001593 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001594 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001595 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001596 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001597 SymbolContext sc;
1598
1599 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001600 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001601
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001602 if (num_matches)
1603 {
1604 strm.Indent ();
1605 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1606 DumpFullpath (strm, &module->GetFileSpec(), 0);
1607 strm.PutCString(":\n");
1608 const uint32_t num_types = type_list.GetSize();
1609 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001610 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001611 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1612 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001613 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001614 // Resolve the clang type so that any forward references
1615 // to types that haven't yet been parsed will get parsed.
1616 type_sp->GetClangFullType ();
1617 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001618 // Print all typedef chains
1619 TypeSP typedef_type_sp (type_sp);
1620 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1621 while (typedefed_type_sp)
1622 {
1623 strm.EOL();
1624 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1625 typedefed_type_sp->GetClangFullType ();
1626 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1627 typedef_type_sp = typedefed_type_sp;
1628 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1629 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001630 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001631 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001632 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001633 }
1634 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001635 }
1636 return 0;
1637}
1638
1639static uint32_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001640LookupTypeHere (CommandInterpreter &interpreter,
1641 Stream &strm,
1642 const SymbolContext &sym_ctx,
1643 const char *name_cstr,
1644 bool name_is_regex)
1645{
1646 if (!sym_ctx.module_sp)
1647 return 0;
1648
1649 TypeList type_list;
1650 const uint32_t max_num_matches = UINT32_MAX;
1651 uint32_t num_matches = 1;
1652 bool name_is_fully_qualified = false;
1653
1654 ConstString name(name_cstr);
1655 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1656
1657 if (num_matches)
1658 {
1659 strm.Indent ();
1660 strm.PutCString("Best match found in ");
1661 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1662 strm.PutCString(":\n");
1663
1664 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1665 if (type_sp)
1666 {
1667 // Resolve the clang type so that any forward references
1668 // to types that haven't yet been parsed will get parsed.
1669 type_sp->GetClangFullType ();
1670 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1671 // Print all typedef chains
1672 TypeSP typedef_type_sp (type_sp);
1673 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1674 while (typedefed_type_sp)
1675 {
1676 strm.EOL();
1677 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1678 typedefed_type_sp->GetClangFullType ();
1679 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1680 typedef_type_sp = typedefed_type_sp;
1681 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1682 }
1683 }
1684 strm.EOL();
1685 }
1686 return num_matches;
1687}
1688
1689static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001690LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001691 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001692 Module *module,
1693 const FileSpec &file_spec,
1694 uint32_t line,
1695 bool check_inlines,
1696 bool verbose)
1697{
1698 if (module && file_spec)
1699 {
1700 SymbolContextList sc_list;
1701 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1702 eSymbolContextEverything, sc_list);
1703 if (num_matches > 0)
1704 {
1705 strm.Indent ();
1706 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1707 strm << file_spec;
1708 if (line > 0)
1709 strm.Printf (":%u", line);
1710 strm << " in ";
1711 DumpFullpath (strm, &module->GetFileSpec(), 0);
1712 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001713 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001714 return num_matches;
1715 }
1716 }
1717 return 0;
1718
1719}
1720
Greg Clayton91048ef2011-11-10 01:18:58 +00001721
1722static size_t
1723FindModulesByName (Target *target,
1724 const char *module_name,
1725 ModuleList &module_list,
1726 bool check_global_list)
1727{
1728// Dump specified images (by basename or fullpath)
1729 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001730 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001731
1732 const size_t initial_size = module_list.GetSize ();
1733
1734 size_t num_matches = 0;
1735
1736 if (target)
1737 {
Greg Clayton444fe992012-02-26 05:51:37 +00001738 num_matches = target->GetImages().FindModules (module_spec, module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001739
1740 // Not found in our module list for our target, check the main
1741 // shared module list in case it is a extra file used somewhere
1742 // else
1743 if (num_matches == 0)
Greg Clayton444fe992012-02-26 05:51:37 +00001744 {
1745 module_spec.GetArchitecture() = target->GetArchitecture();
1746 num_matches = ModuleList::FindSharedModules (module_spec, module_list);
1747 }
Greg Clayton91048ef2011-11-10 01:18:58 +00001748 }
1749 else
1750 {
Greg Clayton444fe992012-02-26 05:51:37 +00001751 num_matches = ModuleList::FindSharedModules (module_spec,module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001752 }
1753
1754 if (check_global_list && num_matches == 0)
1755 {
1756 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001757 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001758 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1759 ModuleSP module_sp;
1760 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1761 {
1762 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1763
1764 if (module)
1765 {
Greg Clayton444fe992012-02-26 05:51:37 +00001766 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001767 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001768 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001769 module_list.AppendIfNeeded(module_sp);
1770 }
1771 }
1772 }
1773 }
1774 return module_list.GetSize () - initial_size;
1775}
1776
Greg Claytone1f50b92011-05-03 22:09:39 +00001777#pragma mark CommandObjectTargetModulesModuleAutoComplete
1778
1779//----------------------------------------------------------------------
1780// A base command object class that can auto complete with module file
1781// paths
1782//----------------------------------------------------------------------
1783
Jim Inghamda26bd22012-06-08 21:56:10 +00001784class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001785{
1786public:
1787
1788 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1789 const char *name,
1790 const char *help,
1791 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001792 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001793 {
1794 CommandArgumentEntry arg;
1795 CommandArgumentData file_arg;
1796
1797 // Define the first (and only) variant of this arg.
1798 file_arg.arg_type = eArgTypeFilename;
1799 file_arg.arg_repetition = eArgRepeatStar;
1800
1801 // There is only one variant this argument could be; put it into the argument entry.
1802 arg.push_back (file_arg);
1803
1804 // Push the data for the first argument into the m_arguments vector.
1805 m_arguments.push_back (arg);
1806 }
1807
1808 virtual
1809 ~CommandObjectTargetModulesModuleAutoComplete ()
1810 {
1811 }
1812
1813 virtual int
1814 HandleArgumentCompletion (Args &input,
1815 int &cursor_index,
1816 int &cursor_char_position,
1817 OptionElementVector &opt_element_vector,
1818 int match_start_point,
1819 int max_return_elements,
1820 bool &word_complete,
1821 StringList &matches)
1822 {
1823 // Arguments are the standard module completer.
1824 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1825 completion_str.erase (cursor_char_position);
1826
1827 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1828 CommandCompletions::eModuleCompletion,
1829 completion_str.c_str(),
1830 match_start_point,
1831 max_return_elements,
1832 NULL,
1833 word_complete,
1834 matches);
1835 return matches.GetSize();
1836 }
1837};
1838
1839#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1840
1841//----------------------------------------------------------------------
1842// A base command object class that can auto complete with module source
1843// file paths
1844//----------------------------------------------------------------------
1845
Jim Inghamda26bd22012-06-08 21:56:10 +00001846class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001847{
1848public:
1849
1850 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1851 const char *name,
1852 const char *help,
1853 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001854 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001855 {
1856 CommandArgumentEntry arg;
1857 CommandArgumentData source_file_arg;
1858
1859 // Define the first (and only) variant of this arg.
1860 source_file_arg.arg_type = eArgTypeSourceFile;
1861 source_file_arg.arg_repetition = eArgRepeatPlus;
1862
1863 // There is only one variant this argument could be; put it into the argument entry.
1864 arg.push_back (source_file_arg);
1865
1866 // Push the data for the first argument into the m_arguments vector.
1867 m_arguments.push_back (arg);
1868 }
1869
1870 virtual
1871 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1872 {
1873 }
1874
1875 virtual int
1876 HandleArgumentCompletion (Args &input,
1877 int &cursor_index,
1878 int &cursor_char_position,
1879 OptionElementVector &opt_element_vector,
1880 int match_start_point,
1881 int max_return_elements,
1882 bool &word_complete,
1883 StringList &matches)
1884 {
1885 // Arguments are the standard source file completer.
1886 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1887 completion_str.erase (cursor_char_position);
1888
1889 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1890 CommandCompletions::eSourceFileCompletion,
1891 completion_str.c_str(),
1892 match_start_point,
1893 max_return_elements,
1894 NULL,
1895 word_complete,
1896 matches);
1897 return matches.GetSize();
1898 }
1899};
1900
1901
1902#pragma mark CommandObjectTargetModulesDumpSymtab
1903
1904
1905class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1906{
1907public:
1908 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1909 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1910 "target modules dump symtab",
1911 "Dump the symbol table from one or more target modules.",
1912 NULL),
1913 m_options (interpreter)
1914 {
1915 }
1916
1917 virtual
1918 ~CommandObjectTargetModulesDumpSymtab ()
1919 {
1920 }
1921
Jim Inghamda26bd22012-06-08 21:56:10 +00001922 virtual Options *
1923 GetOptions ()
1924 {
1925 return &m_options;
1926 }
1927
1928 class CommandOptions : public Options
1929 {
1930 public:
1931
1932 CommandOptions (CommandInterpreter &interpreter) :
1933 Options(interpreter),
1934 m_sort_order (eSortOrderNone)
1935 {
1936 }
1937
1938 virtual
1939 ~CommandOptions ()
1940 {
1941 }
1942
1943 virtual Error
1944 SetOptionValue (uint32_t option_idx, const char *option_arg)
1945 {
1946 Error error;
1947 char short_option = (char) m_getopt_table[option_idx].val;
1948
1949 switch (short_option)
1950 {
1951 case 's':
1952 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1953 g_option_table[option_idx].enum_values,
1954 eSortOrderNone,
1955 error);
1956 break;
1957
1958 default:
1959 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1960 break;
1961
1962 }
1963 return error;
1964 }
1965
1966 void
1967 OptionParsingStarting ()
1968 {
1969 m_sort_order = eSortOrderNone;
1970 }
1971
1972 const OptionDefinition*
1973 GetDefinitions ()
1974 {
1975 return g_option_table;
1976 }
1977
1978 // Options table: Required for subclasses of Options.
1979 static OptionDefinition g_option_table[];
1980
1981 SortOrder m_sort_order;
1982 };
1983
1984protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00001985 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001986 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00001987 CommandReturnObject &result)
1988 {
1989 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1990 if (target == NULL)
1991 {
1992 result.AppendError ("invalid target, create a debug target using the 'target create' command");
1993 result.SetStatus (eReturnStatusFailed);
1994 return false;
1995 }
1996 else
1997 {
1998 uint32_t num_dumped = 0;
1999
2000 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2001 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2002 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2003
2004 if (command.GetArgumentCount() == 0)
2005 {
2006 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002007 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002008 const uint32_t num_modules = target->GetImages().GetSize();
2009 if (num_modules > 0)
2010 {
2011 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2012 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2013 {
2014 if (num_dumped > 0)
2015 {
2016 result.GetOutputStream().EOL();
2017 result.GetOutputStream().EOL();
2018 }
2019 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002020 DumpModuleSymtab (m_interpreter,
2021 result.GetOutputStream(),
2022 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2023 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002024 }
2025 }
2026 else
2027 {
2028 result.AppendError ("the target has no associated executable images");
2029 result.SetStatus (eReturnStatusFailed);
2030 return false;
2031 }
2032 }
2033 else
2034 {
2035 // Dump specified images (by basename or fullpath)
2036 const char *arg_cstr;
2037 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2038 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002039 ModuleList module_list;
2040 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2041 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002042 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002043 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002044 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002045 Module *module = module_list.GetModulePointerAtIndex(i);
2046 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002047 {
2048 if (num_dumped > 0)
2049 {
2050 result.GetOutputStream().EOL();
2051 result.GetOutputStream().EOL();
2052 }
2053 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002054 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002055 }
2056 }
2057 }
2058 else
2059 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2060 }
2061 }
2062
2063 if (num_dumped > 0)
2064 result.SetStatus (eReturnStatusSuccessFinishResult);
2065 else
2066 {
2067 result.AppendError ("no matching executable images found");
2068 result.SetStatus (eReturnStatusFailed);
2069 }
2070 }
2071 return result.Succeeded();
2072 }
2073
Greg Claytone1f50b92011-05-03 22:09:39 +00002074
2075 CommandOptions m_options;
2076};
2077
2078static OptionEnumValueElement
2079g_sort_option_enumeration[4] =
2080{
2081 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2082 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2083 { eSortOrderByName, "name", "Sort output by symbol name."},
2084 { 0, NULL, NULL }
2085};
2086
2087
2088OptionDefinition
2089CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2090{
2091 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2092 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2093};
2094
2095#pragma mark CommandObjectTargetModulesDumpSections
2096
2097//----------------------------------------------------------------------
2098// Image section dumping command
2099//----------------------------------------------------------------------
2100
2101class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2102{
2103public:
2104 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2105 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2106 "target modules dump sections",
2107 "Dump the sections from one or more target modules.",
2108 //"target modules dump sections [<file1> ...]")
2109 NULL)
2110 {
2111 }
2112
2113 virtual
2114 ~CommandObjectTargetModulesDumpSections ()
2115 {
2116 }
2117
Jim Inghamda26bd22012-06-08 21:56:10 +00002118protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002119 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002120 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002121 CommandReturnObject &result)
2122 {
2123 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2124 if (target == NULL)
2125 {
2126 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2127 result.SetStatus (eReturnStatusFailed);
2128 return false;
2129 }
2130 else
2131 {
2132 uint32_t num_dumped = 0;
2133
2134 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2135 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2136 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2137
2138 if (command.GetArgumentCount() == 0)
2139 {
2140 // Dump all sections for all modules images
2141 const uint32_t num_modules = target->GetImages().GetSize();
2142 if (num_modules > 0)
2143 {
2144 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2145 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2146 {
2147 num_dumped++;
2148 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2149 }
2150 }
2151 else
2152 {
2153 result.AppendError ("the target has no associated executable images");
2154 result.SetStatus (eReturnStatusFailed);
2155 return false;
2156 }
2157 }
2158 else
2159 {
2160 // Dump specified images (by basename or fullpath)
2161 const char *arg_cstr;
2162 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2163 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002164 ModuleList module_list;
2165 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2166 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002167 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002168 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002169 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002170 Module *module = module_list.GetModulePointerAtIndex(i);
2171 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002172 {
2173 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002174 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002175 }
2176 }
2177 }
2178 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002179 {
2180 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002181 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002182
Greg Claytone1f50b92011-05-03 22:09:39 +00002183 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002184 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002185 }
2186 }
2187
2188 if (num_dumped > 0)
2189 result.SetStatus (eReturnStatusSuccessFinishResult);
2190 else
2191 {
2192 result.AppendError ("no matching executable images found");
2193 result.SetStatus (eReturnStatusFailed);
2194 }
2195 }
2196 return result.Succeeded();
2197 }
2198};
2199
2200
2201#pragma mark CommandObjectTargetModulesDumpSymfile
2202
2203//----------------------------------------------------------------------
2204// Image debug symbol dumping command
2205//----------------------------------------------------------------------
2206
2207class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2208{
2209public:
2210 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2211 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2212 "target modules dump symfile",
2213 "Dump the debug symbol file for one or more target modules.",
2214 //"target modules dump symfile [<file1> ...]")
2215 NULL)
2216 {
2217 }
2218
2219 virtual
2220 ~CommandObjectTargetModulesDumpSymfile ()
2221 {
2222 }
2223
Jim Inghamda26bd22012-06-08 21:56:10 +00002224protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002225 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002226 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002227 CommandReturnObject &result)
2228 {
2229 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2230 if (target == NULL)
2231 {
2232 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2233 result.SetStatus (eReturnStatusFailed);
2234 return false;
2235 }
2236 else
2237 {
2238 uint32_t num_dumped = 0;
2239
2240 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2241 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2242 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2243
2244 if (command.GetArgumentCount() == 0)
2245 {
2246 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002247 ModuleList &target_modules = target->GetImages();
2248 Mutex::Locker modules_locker (target_modules.GetMutex());
2249 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002250 if (num_modules > 0)
2251 {
2252 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2253 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2254 {
Jim Ingham93367902012-05-30 02:19:25 +00002255 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002256 num_dumped++;
2257 }
2258 }
2259 else
2260 {
2261 result.AppendError ("the target has no associated executable images");
2262 result.SetStatus (eReturnStatusFailed);
2263 return false;
2264 }
2265 }
2266 else
2267 {
2268 // Dump specified images (by basename or fullpath)
2269 const char *arg_cstr;
2270 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2271 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002272 ModuleList module_list;
2273 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2274 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002275 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002276 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002277 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002278 Module *module = module_list.GetModulePointerAtIndex(i);
2279 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002280 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002281 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002282 num_dumped++;
2283 }
2284 }
2285 }
2286 else
2287 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2288 }
2289 }
2290
2291 if (num_dumped > 0)
2292 result.SetStatus (eReturnStatusSuccessFinishResult);
2293 else
2294 {
2295 result.AppendError ("no matching executable images found");
2296 result.SetStatus (eReturnStatusFailed);
2297 }
2298 }
2299 return result.Succeeded();
2300 }
2301};
2302
2303
2304#pragma mark CommandObjectTargetModulesDumpLineTable
2305
2306//----------------------------------------------------------------------
2307// Image debug line table dumping command
2308//----------------------------------------------------------------------
2309
2310class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2311{
2312public:
2313 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2314 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2315 "target modules dump line-table",
2316 "Dump the debug symbol file for one or more target modules.",
2317 NULL)
2318 {
2319 }
2320
2321 virtual
2322 ~CommandObjectTargetModulesDumpLineTable ()
2323 {
2324 }
2325
Jim Inghamda26bd22012-06-08 21:56:10 +00002326protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002327 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002328 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002329 CommandReturnObject &result)
2330 {
2331 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2332 if (target == NULL)
2333 {
2334 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2335 result.SetStatus (eReturnStatusFailed);
2336 return false;
2337 }
2338 else
2339 {
2340 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2341 uint32_t total_num_dumped = 0;
2342
2343 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2344 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2345 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2346
2347 if (command.GetArgumentCount() == 0)
2348 {
2349 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2350 result.SetStatus (eReturnStatusFailed);
2351 }
2352 else
2353 {
2354 // Dump specified images (by basename or fullpath)
2355 const char *arg_cstr;
2356 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2357 {
2358 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002359
2360 ModuleList &target_modules = target->GetImages();
2361 Mutex::Locker modules_locker(target_modules.GetMutex());
2362 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002363 if (num_modules > 0)
2364 {
2365 uint32_t num_dumped = 0;
2366 for (uint32_t i = 0; i<num_modules; ++i)
2367 {
2368 if (DumpCompileUnitLineTable (m_interpreter,
2369 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002370 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002371 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002372 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002373 num_dumped++;
2374 }
2375 if (num_dumped == 0)
2376 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2377 else
2378 total_num_dumped += num_dumped;
2379 }
2380 }
2381 }
2382
2383 if (total_num_dumped > 0)
2384 result.SetStatus (eReturnStatusSuccessFinishResult);
2385 else
2386 {
2387 result.AppendError ("no source filenames matched any command arguments");
2388 result.SetStatus (eReturnStatusFailed);
2389 }
2390 }
2391 return result.Succeeded();
2392 }
2393};
2394
2395
2396#pragma mark CommandObjectTargetModulesDump
2397
2398//----------------------------------------------------------------------
2399// Dump multi-word command for target modules
2400//----------------------------------------------------------------------
2401
2402class CommandObjectTargetModulesDump : public CommandObjectMultiword
2403{
2404public:
2405
2406 //------------------------------------------------------------------
2407 // Constructors and Destructors
2408 //------------------------------------------------------------------
2409 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2410 CommandObjectMultiword (interpreter,
2411 "target modules dump",
2412 "A set of commands for dumping information about one or more target modules.",
2413 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2414 {
2415 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2416 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2417 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2418 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2419 }
2420
2421 virtual
2422 ~CommandObjectTargetModulesDump()
2423 {
2424 }
2425};
2426
Jim Inghamda26bd22012-06-08 21:56:10 +00002427class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002428{
2429public:
2430 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002431 CommandObjectParsed (interpreter,
2432 "target modules add",
2433 "Add a new module to the current target's modules.",
2434 "target modules add [<module>]")
Greg Claytone1f50b92011-05-03 22:09:39 +00002435 {
2436 }
2437
2438 virtual
2439 ~CommandObjectTargetModulesAdd ()
2440 {
2441 }
2442
Jim Inghamda26bd22012-06-08 21:56:10 +00002443 int
2444 HandleArgumentCompletion (Args &input,
2445 int &cursor_index,
2446 int &cursor_char_position,
2447 OptionElementVector &opt_element_vector,
2448 int match_start_point,
2449 int max_return_elements,
2450 bool &word_complete,
2451 StringList &matches)
2452 {
2453 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2454 completion_str.erase (cursor_char_position);
2455
2456 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2457 CommandCompletions::eDiskFileCompletion,
2458 completion_str.c_str(),
2459 match_start_point,
2460 max_return_elements,
2461 NULL,
2462 word_complete,
2463 matches);
2464 return matches.GetSize();
2465 }
2466
2467protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002468 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002469 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002470 CommandReturnObject &result)
2471 {
2472 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2473 if (target == NULL)
2474 {
2475 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2476 result.SetStatus (eReturnStatusFailed);
2477 return false;
2478 }
2479 else
2480 {
2481 const size_t argc = args.GetArgumentCount();
2482 if (argc == 0)
2483 {
2484 result.AppendError ("one or more executable image paths must be specified");
2485 result.SetStatus (eReturnStatusFailed);
2486 return false;
2487 }
2488 else
2489 {
2490 for (size_t i=0; i<argc; ++i)
2491 {
2492 const char *path = args.GetArgumentAtIndex(i);
2493 if (path)
2494 {
2495 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002496 if (file_spec.Exists())
2497 {
Greg Clayton444fe992012-02-26 05:51:37 +00002498 ModuleSpec module_spec (file_spec);
2499 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002500 if (!module_sp)
2501 {
2502 result.AppendError ("one or more executable image paths must be specified");
2503 result.SetStatus (eReturnStatusFailed);
2504 return false;
2505 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002506 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002507 }
2508 else
2509 {
2510 char resolved_path[PATH_MAX];
2511 result.SetStatus (eReturnStatusFailed);
2512 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2513 {
2514 if (strcmp (resolved_path, path) != 0)
2515 {
2516 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2517 break;
2518 }
2519 }
2520 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2521 break;
2522 }
2523 }
2524 }
2525 }
2526 }
2527 return result.Succeeded();
2528 }
2529
Greg Claytone1f50b92011-05-03 22:09:39 +00002530};
2531
2532class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2533{
2534public:
2535 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2536 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2537 "target modules load",
2538 "Set the load addresses for one or more sections in a target module.",
2539 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2540 m_option_group (interpreter),
2541 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2542 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)
2543 {
2544 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2545 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2546 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2547 m_option_group.Finalize();
2548 }
2549
2550 virtual
2551 ~CommandObjectTargetModulesLoad ()
2552 {
2553 }
2554
Jim Inghamda26bd22012-06-08 21:56:10 +00002555 virtual Options *
2556 GetOptions ()
2557 {
2558 return &m_option_group;
2559 }
2560
2561protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002562 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002563 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002564 CommandReturnObject &result)
2565 {
2566 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2567 if (target == NULL)
2568 {
2569 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2570 result.SetStatus (eReturnStatusFailed);
2571 return false;
2572 }
2573 else
2574 {
2575 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002576 ModuleSpec module_spec;
2577 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002578 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002579 {
2580 search_using_module_spec = true;
2581 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2582 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002583
2584 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002585 {
2586 search_using_module_spec = true;
2587 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2588 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002589
Greg Clayton444fe992012-02-26 05:51:37 +00002590 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002591 {
2592
2593 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002594 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002595
2596 char path[PATH_MAX];
2597 if (num_matches == 1)
2598 {
2599 Module *module = matching_modules.GetModulePointerAtIndex(0);
2600 if (module)
2601 {
2602 ObjectFile *objfile = module->GetObjectFile();
2603 if (objfile)
2604 {
2605 SectionList *section_list = objfile->GetSectionList();
2606 if (section_list)
2607 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002608 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002609 if (argc == 0)
2610 {
2611 if (m_slide_option.GetOptionValue().OptionWasSet())
2612 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002613 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2614 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002615 }
2616 else
2617 {
2618 result.AppendError ("one or more section name + load address pair must be specified");
2619 result.SetStatus (eReturnStatusFailed);
2620 return false;
2621 }
2622 }
2623 else
2624 {
2625 if (m_slide_option.GetOptionValue().OptionWasSet())
2626 {
2627 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2628 result.SetStatus (eReturnStatusFailed);
2629 return false;
2630 }
2631
2632 for (size_t i=0; i<argc; i += 2)
2633 {
2634 const char *sect_name = args.GetArgumentAtIndex(i);
2635 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2636 if (sect_name && load_addr_cstr)
2637 {
2638 ConstString const_sect_name(sect_name);
2639 bool success = false;
2640 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2641 if (success)
2642 {
2643 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2644 if (section_sp)
2645 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002646 if (section_sp->IsThreadSpecific())
2647 {
2648 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2649 result.SetStatus (eReturnStatusFailed);
2650 break;
2651 }
2652 else
2653 {
2654 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), load_addr))
2655 changed = true;
2656 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2657 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002658 }
2659 else
2660 {
2661 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2662 result.SetStatus (eReturnStatusFailed);
2663 break;
2664 }
2665 }
2666 else
2667 {
2668 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2669 result.SetStatus (eReturnStatusFailed);
2670 break;
2671 }
2672 }
2673 else
2674 {
2675 if (sect_name)
2676 result.AppendError ("section names must be followed by a load address.\n");
2677 else
2678 result.AppendError ("one or more section name + load address pair must be specified.\n");
2679 result.SetStatus (eReturnStatusFailed);
2680 break;
2681 }
2682 }
2683 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002684
2685 if (changed)
2686 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002687 }
2688 else
2689 {
2690 module->GetFileSpec().GetPath (path, sizeof(path));
2691 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2692 result.SetStatus (eReturnStatusFailed);
2693 }
2694 }
2695 else
2696 {
2697 module->GetFileSpec().GetPath (path, sizeof(path));
2698 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2699 result.SetStatus (eReturnStatusFailed);
2700 }
2701 }
2702 else
2703 {
2704 module->GetFileSpec().GetPath (path, sizeof(path));
2705 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2706 result.SetStatus (eReturnStatusFailed);
2707 }
2708 }
2709 else
2710 {
2711 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002712
2713 if (module_spec.GetFileSpec())
2714 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002715 else
2716 path[0] = '\0';
2717
Greg Clayton444fe992012-02-26 05:51:37 +00002718 if (module_spec.GetUUIDPtr())
2719 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002720 else
2721 uuid_cstr[0] = '\0';
2722 if (num_matches > 1)
2723 {
2724 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2725 path[0] ? " file=" : "",
2726 path,
2727 uuid_cstr[0] ? " uuid=" : "",
2728 uuid_cstr);
2729 for (size_t i=0; i<num_matches; ++i)
2730 {
2731 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2732 result.AppendMessageWithFormat("%s\n", path);
2733 }
2734 }
2735 else
2736 {
2737 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2738 path[0] ? " file=" : "",
2739 path,
2740 uuid_cstr[0] ? " uuid=" : "",
2741 uuid_cstr);
2742 }
2743 result.SetStatus (eReturnStatusFailed);
2744 }
2745 }
2746 else
2747 {
2748 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2749 result.SetStatus (eReturnStatusFailed);
2750 return false;
2751 }
2752 }
2753 return result.Succeeded();
2754 }
2755
Greg Claytone1f50b92011-05-03 22:09:39 +00002756 OptionGroupOptions m_option_group;
2757 OptionGroupUUID m_uuid_option_group;
2758 OptionGroupFile m_file_option;
2759 OptionGroupUInt64 m_slide_option;
2760};
2761
2762//----------------------------------------------------------------------
2763// List images with associated information
2764//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002765class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002766{
2767public:
2768
2769 class CommandOptions : public Options
2770 {
2771 public:
2772
2773 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002774 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002775 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002776 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002777 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002778 {
2779 }
2780
2781 virtual
2782 ~CommandOptions ()
2783 {
2784 }
2785
2786 virtual Error
2787 SetOptionValue (uint32_t option_idx, const char *option_arg)
2788 {
2789 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002790 if (short_option == 'g')
2791 {
2792 m_use_global_module_list = true;
2793 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002794 else if (short_option == 'a')
2795 {
2796 bool success;
2797 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2798 if (!success)
2799 {
2800 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002801 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002802 }
2803 }
Greg Clayton899025f2011-08-09 00:01:09 +00002804 else
2805 {
2806 uint32_t width = 0;
2807 if (option_arg)
2808 width = strtoul (option_arg, NULL, 0);
2809 m_format_array.push_back(std::make_pair(short_option, width));
2810 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002811 Error error;
2812 return error;
2813 }
2814
2815 void
2816 OptionParsingStarting ()
2817 {
2818 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002819 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002820 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002821 }
2822
2823 const OptionDefinition*
2824 GetDefinitions ()
2825 {
2826 return g_option_table;
2827 }
2828
2829 // Options table: Required for subclasses of Options.
2830
2831 static OptionDefinition g_option_table[];
2832
2833 // Instance variables to hold the values for command options.
2834 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2835 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002836 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002837 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002838 };
2839
2840 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002841 CommandObjectParsed (interpreter,
2842 "target modules list",
2843 "List current executable and dependent shared library images.",
2844 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002845 m_options (interpreter)
2846 {
2847 }
2848
2849 virtual
2850 ~CommandObjectTargetModulesList ()
2851 {
2852 }
2853
2854 virtual
2855 Options *
2856 GetOptions ()
2857 {
2858 return &m_options;
2859 }
2860
Jim Inghamda26bd22012-06-08 21:56:10 +00002861protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002862 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002863 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002864 CommandReturnObject &result)
2865 {
2866 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002867 const bool use_global_module_list = m_options.m_use_global_module_list;
2868 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002869 {
2870 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2871 result.SetStatus (eReturnStatusFailed);
2872 return false;
2873 }
2874 else
2875 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002876 if (target)
2877 {
2878 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2879 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2880 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2881 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002882 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002883 Stream &strm = result.GetOutputStream();
2884
2885 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2886 {
2887 if (target)
2888 {
2889 Address module_address;
2890 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2891 {
Greg Clayton3508c382012-02-24 01:59:29 +00002892 ModuleSP module_sp (module_address.GetModule());
2893 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002894 {
Greg Clayton3508c382012-02-24 01:59:29 +00002895 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002896 result.SetStatus (eReturnStatusSuccessFinishResult);
2897 }
2898 else
2899 {
2900 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2901 result.SetStatus (eReturnStatusFailed);
2902 }
2903 }
2904 else
2905 {
2906 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2907 result.SetStatus (eReturnStatusFailed);
2908 }
2909 }
2910 else
2911 {
2912 result.AppendError ("Can only look up modules by address with a valid target.");
2913 result.SetStatus (eReturnStatusFailed);
2914 }
2915 return result.Succeeded();
2916 }
2917
Jim Ingham93367902012-05-30 02:19:25 +00002918 uint32_t num_modules = 0;
2919 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
2920 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
2921 // the global module list directly.
2922
Greg Clayton2ad894b2012-05-15 18:43:44 +00002923 ModuleList module_list;
2924 ModuleList *module_list_ptr = NULL;
2925 const size_t argc = command.GetArgumentCount();
2926 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00002927 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002928 if (use_global_module_list)
2929 {
2930 locker.Lock (Module::GetAllocationModuleCollectionMutex());
2931 num_modules = Module::GetNumberAllocatedModules();
2932 }
2933 else
2934 {
2935 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00002936 }
Greg Clayton899025f2011-08-09 00:01:09 +00002937 }
2938 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002939 {
2940 for (size_t i=0; i<argc; ++i)
2941 {
2942 // Dump specified images (by basename or fullpath)
2943 const char *arg_cstr = command.GetArgumentAtIndex(i);
2944 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2945 if (num_matches == 0)
2946 {
2947 if (argc == 1)
2948 {
2949 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
2950 result.SetStatus (eReturnStatusFailed);
2951 return false;
2952 }
2953 }
2954 }
2955
Greg Clayton2ad894b2012-05-15 18:43:44 +00002956 module_list_ptr = &module_list;
2957 }
Jim Ingham93367902012-05-30 02:19:25 +00002958
2959 if (module_list_ptr != NULL)
2960 {
2961 locker.Lock(module_list_ptr->GetMutex());
2962 num_modules = module_list_ptr->GetSize();
2963 }
Greg Clayton899025f2011-08-09 00:01:09 +00002964
Greg Claytone1f50b92011-05-03 22:09:39 +00002965 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002966 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002967 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2968 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002969 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002970 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00002971 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00002972 {
Jim Ingham93367902012-05-30 02:19:25 +00002973 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00002974 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002975 }
2976 else
2977 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002978 module = Module::GetAllocatedModuleAtIndex(image_idx);
2979 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00002980 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002981
Greg Claytonb5a8f142012-02-05 02:38:54 +00002982 int indent = strm.Printf("[%3u] ", image_idx);
2983 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00002984
Greg Claytone1f50b92011-05-03 22:09:39 +00002985 }
2986 result.SetStatus (eReturnStatusSuccessFinishResult);
2987 }
2988 else
2989 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002990 if (argc)
2991 {
2992 if (use_global_module_list)
2993 result.AppendError ("the global module list has no matching modules");
2994 else
2995 result.AppendError ("the target has no matching modules");
2996 }
Greg Clayton153ccd72011-08-10 02:10:13 +00002997 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002998 {
2999 if (use_global_module_list)
3000 result.AppendError ("the global module list is empty");
3001 else
3002 result.AppendError ("the target has no associated executable images");
3003 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003004 result.SetStatus (eReturnStatusFailed);
3005 return false;
3006 }
3007 }
3008 return result.Succeeded();
3009 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003010
3011 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003012 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003013 {
3014
3015 bool dump_object_name = false;
3016 if (m_options.m_format_array.empty())
3017 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003018 m_options.m_format_array.push_back(std::make_pair('u', 0));
3019 m_options.m_format_array.push_back(std::make_pair('h', 0));
3020 m_options.m_format_array.push_back(std::make_pair('f', 0));
3021 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003022 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003023 const size_t num_entries = m_options.m_format_array.size();
3024 bool print_space = false;
3025 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003026 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003027 if (print_space)
3028 strm.PutChar(' ');
3029 print_space = true;
3030 const char format_char = m_options.m_format_array[i].first;
3031 uint32_t width = m_options.m_format_array[i].second;
3032 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003033 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003034 case 'A':
3035 DumpModuleArchitecture (strm, module, false, width);
3036 break;
3037
3038 case 't':
3039 DumpModuleArchitecture (strm, module, true, width);
3040 break;
3041
3042 case 'f':
3043 DumpFullpath (strm, &module->GetFileSpec(), width);
3044 dump_object_name = true;
3045 break;
3046
3047 case 'd':
3048 DumpDirectory (strm, &module->GetFileSpec(), width);
3049 break;
3050
3051 case 'b':
3052 DumpBasename (strm, &module->GetFileSpec(), width);
3053 dump_object_name = true;
3054 break;
3055
3056 case 'h':
3057 case 'o':
3058 // Image header address
3059 {
3060 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003061
Greg Claytonb5a8f142012-02-05 02:38:54 +00003062 ObjectFile *objfile = module->GetObjectFile ();
3063 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003064 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003065 Address header_addr(objfile->GetHeaderAddress());
3066 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003067 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003068 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003069 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003070 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3071 if (header_load_addr == LLDB_INVALID_ADDRESS)
3072 {
3073 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3074 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003075 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003076 {
3077 if (format_char == 'o')
3078 {
3079 // Show the offset of slide for the image
3080 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
3081 }
3082 else
3083 {
3084 // Show the load address of the image
3085 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
3086 }
3087 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003088 break;
3089 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003090 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3091 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3092 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003093 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003094 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003095 strm.Printf ("%*s", addr_nibble_width + 2, "");
3096 }
3097 break;
3098 case 'r':
3099 {
3100 uint32_t ref_count = 0;
3101 ModuleSP module_sp (module->shared_from_this());
3102 if (module_sp)
3103 {
3104 // Take one away to make sure we don't count our local "module_sp"
3105 ref_count = module_sp.use_count() - 1;
3106 }
3107 if (width)
3108 strm.Printf("{%*u}", width, ref_count);
3109 else
3110 strm.Printf("{%u}", ref_count);
3111 }
3112 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003113
Greg Claytonb5a8f142012-02-05 02:38:54 +00003114 case 's':
3115 case 'S':
3116 {
3117 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3118 if (symbol_vendor)
3119 {
3120 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3121 if (symbol_file)
3122 {
3123 if (format_char == 'S')
3124 {
3125 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3126 // Dump symbol file only if different from module file
3127 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3128 {
3129 print_space = false;
3130 break;
3131 }
3132 // Add a newline and indent past the index
3133 strm.Printf ("\n%*s", indent, "");
3134 }
3135 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3136 dump_object_name = true;
3137 break;
3138 }
3139 }
3140 strm.Printf("%.*s", width, "<NONE>");
3141 }
3142 break;
3143
3144 case 'm':
3145 module->GetModificationTime().Dump(&strm, width);
3146 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003147
Greg Claytonb5a8f142012-02-05 02:38:54 +00003148 case 'p':
3149 strm.Printf("%p", module);
3150 break;
3151
3152 case 'u':
3153 DumpModuleUUID(strm, module);
3154 break;
3155
3156 default:
3157 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003158 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003159
3160 }
3161 if (dump_object_name)
3162 {
3163 const char *object_name = module->GetObjectName().GetCString();
3164 if (object_name)
3165 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003166 }
3167 strm.EOL();
3168 }
3169
Greg Claytone1f50b92011-05-03 22:09:39 +00003170 CommandOptions m_options;
3171};
3172
3173OptionDefinition
3174CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3175{
Jim Ingham6bdea822011-10-24 18:36:33 +00003176 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3177 { 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 +00003178 { 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 +00003179 { 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."},
3180 { 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 +00003181 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3182 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3183 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3184 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3185 { 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 +00003186 { 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 +00003187 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3188 { 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."},
3189 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003190 { 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 +00003191 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3192};
3193
3194
3195
3196//----------------------------------------------------------------------
3197// Lookup information in images
3198//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003199class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003200{
3201public:
3202
3203 enum
3204 {
3205 eLookupTypeInvalid = -1,
3206 eLookupTypeAddress = 0,
3207 eLookupTypeSymbol,
3208 eLookupTypeFileLine, // Line is optional
3209 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003210 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003211 eLookupTypeType,
3212 kNumLookupTypes
3213 };
3214
3215 class CommandOptions : public Options
3216 {
3217 public:
3218
3219 CommandOptions (CommandInterpreter &interpreter) :
3220 Options(interpreter)
3221 {
3222 OptionParsingStarting();
3223 }
3224
3225 virtual
3226 ~CommandOptions ()
3227 {
3228 }
3229
3230 virtual Error
3231 SetOptionValue (uint32_t option_idx, const char *option_arg)
3232 {
3233 Error error;
3234
3235 char short_option = (char) m_getopt_table[option_idx].val;
3236
3237 switch (short_option)
3238 {
3239 case 'a':
3240 m_type = eLookupTypeAddress;
3241 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3242 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003243 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003244 break;
3245
3246 case 'o':
3247 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3248 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003249 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003250 break;
3251
3252 case 's':
3253 m_str = option_arg;
3254 m_type = eLookupTypeSymbol;
3255 break;
3256
3257 case 'f':
3258 m_file.SetFile (option_arg, false);
3259 m_type = eLookupTypeFileLine;
3260 break;
3261
3262 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003263 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003264 break;
3265
3266 case 'l':
3267 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3268 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003269 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003270 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003271 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003272 m_type = eLookupTypeFileLine;
3273 break;
3274
Greg Clayton2ad894b2012-05-15 18:43:44 +00003275 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003276 m_str = option_arg;
3277 m_type = eLookupTypeFunction;
3278 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003279
3280 case 'n':
3281 m_str = option_arg;
3282 m_type = eLookupTypeFunctionOrSymbol;
3283 break;
3284
Greg Claytone1f50b92011-05-03 22:09:39 +00003285 case 't':
3286 m_str = option_arg;
3287 m_type = eLookupTypeType;
3288 break;
3289
3290 case 'v':
3291 m_verbose = 1;
3292 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003293
3294 case 'A':
3295 m_print_all = true;
3296 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003297
3298 case 'r':
3299 m_use_regex = true;
3300 break;
3301 }
3302
3303 return error;
3304 }
3305
3306 void
3307 OptionParsingStarting ()
3308 {
3309 m_type = eLookupTypeInvalid;
3310 m_str.clear();
3311 m_file.Clear();
3312 m_addr = LLDB_INVALID_ADDRESS;
3313 m_offset = 0;
3314 m_line_number = 0;
3315 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003316 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003317 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003318 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003319 }
3320
3321 const OptionDefinition*
3322 GetDefinitions ()
3323 {
3324 return g_option_table;
3325 }
3326
3327 // Options table: Required for subclasses of Options.
3328
3329 static OptionDefinition g_option_table[];
3330 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3331 std::string m_str; // Holds name lookup
3332 FileSpec m_file; // Files for file lookups
3333 lldb::addr_t m_addr; // Holds the address to lookup
3334 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3335 uint32_t m_line_number; // Line number for file+line lookups
3336 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003337 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003338 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003339 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003340
3341 };
3342
3343 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003344 CommandObjectParsed (interpreter,
3345 "target modules lookup",
3346 "Look up information within executable and dependent shared library images.",
3347 NULL),
3348 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003349 {
3350 CommandArgumentEntry arg;
3351 CommandArgumentData file_arg;
3352
3353 // Define the first (and only) variant of this arg.
3354 file_arg.arg_type = eArgTypeFilename;
3355 file_arg.arg_repetition = eArgRepeatStar;
3356
3357 // There is only one variant this argument could be; put it into the argument entry.
3358 arg.push_back (file_arg);
3359
3360 // Push the data for the first argument into the m_arguments vector.
3361 m_arguments.push_back (arg);
3362 }
3363
3364 virtual
3365 ~CommandObjectTargetModulesLookup ()
3366 {
3367 }
3368
3369 virtual Options *
3370 GetOptions ()
3371 {
3372 return &m_options;
3373 }
3374
Sean Callanan56d31ec2012-06-06 20:49:55 +00003375 bool
3376 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3377 {
3378 switch (m_options.m_type)
3379 {
3380 case eLookupTypeAddress:
3381 case eLookupTypeFileLine:
3382 case eLookupTypeFunction:
3383 case eLookupTypeFunctionOrSymbol:
3384 case eLookupTypeSymbol:
3385 default:
3386 return false;
3387 case eLookupTypeType:
3388 break;
3389 }
3390
3391 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3392
3393 StackFrameSP frame = exe_ctx.GetFrameSP();
3394
3395 if (!frame)
3396 return false;
3397
3398 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3399
3400 if (!sym_ctx.module_sp)
3401 return false;
3402
3403 switch (m_options.m_type)
3404 {
3405 default:
3406 return false;
3407 case eLookupTypeType:
3408 if (!m_options.m_str.empty())
3409 {
3410 if (LookupTypeHere (m_interpreter,
3411 result.GetOutputStream(),
3412 sym_ctx,
3413 m_options.m_str.c_str(),
3414 m_options.m_use_regex))
3415 {
3416 result.SetStatus(eReturnStatusSuccessFinishResult);
3417 return true;
3418 }
3419 }
3420 break;
3421 }
3422
3423 return true;
3424 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003425
3426 bool
3427 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3428 {
3429 switch (m_options.m_type)
3430 {
3431 case eLookupTypeAddress:
3432 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3433 {
3434 if (LookupAddressInModule (m_interpreter,
3435 result.GetOutputStream(),
3436 module,
3437 eSymbolContextEverything,
3438 m_options.m_addr,
3439 m_options.m_offset,
3440 m_options.m_verbose))
3441 {
3442 result.SetStatus(eReturnStatusSuccessFinishResult);
3443 return true;
3444 }
3445 }
3446 break;
3447
3448 case eLookupTypeSymbol:
3449 if (!m_options.m_str.empty())
3450 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003451 if (LookupSymbolInModule (m_interpreter,
3452 result.GetOutputStream(),
3453 module,
3454 m_options.m_str.c_str(),
3455 m_options.m_use_regex,
3456 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003457 {
3458 result.SetStatus(eReturnStatusSuccessFinishResult);
3459 return true;
3460 }
3461 }
3462 break;
3463
3464 case eLookupTypeFileLine:
3465 if (m_options.m_file)
3466 {
3467
3468 if (LookupFileAndLineInModule (m_interpreter,
3469 result.GetOutputStream(),
3470 module,
3471 m_options.m_file,
3472 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003473 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003474 m_options.m_verbose))
3475 {
3476 result.SetStatus(eReturnStatusSuccessFinishResult);
3477 return true;
3478 }
3479 }
3480 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003481
3482 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003483 case eLookupTypeFunction:
3484 if (!m_options.m_str.empty())
3485 {
3486 if (LookupFunctionInModule (m_interpreter,
3487 result.GetOutputStream(),
3488 module,
3489 m_options.m_str.c_str(),
3490 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003491 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003492 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003493 m_options.m_verbose))
3494 {
3495 result.SetStatus(eReturnStatusSuccessFinishResult);
3496 return true;
3497 }
3498 }
3499 break;
3500
Greg Clayton2ad894b2012-05-15 18:43:44 +00003501
Greg Claytone1f50b92011-05-03 22:09:39 +00003502 case eLookupTypeType:
3503 if (!m_options.m_str.empty())
3504 {
3505 if (LookupTypeInModule (m_interpreter,
3506 result.GetOutputStream(),
3507 module,
3508 m_options.m_str.c_str(),
3509 m_options.m_use_regex))
3510 {
3511 result.SetStatus(eReturnStatusSuccessFinishResult);
3512 return true;
3513 }
3514 }
3515 break;
3516
3517 default:
3518 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3519 syntax_error = true;
3520 break;
3521 }
3522
3523 result.SetStatus (eReturnStatusFailed);
3524 return false;
3525 }
3526
Jim Inghamda26bd22012-06-08 21:56:10 +00003527protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003528 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003529 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003530 CommandReturnObject &result)
3531 {
3532 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3533 if (target == NULL)
3534 {
3535 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3536 result.SetStatus (eReturnStatusFailed);
3537 return false;
3538 }
3539 else
3540 {
3541 bool syntax_error = false;
3542 uint32_t i;
3543 uint32_t num_successful_lookups = 0;
3544 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3545 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3546 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3547 // Dump all sections for all modules images
3548
3549 if (command.GetArgumentCount() == 0)
3550 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003551 ModuleSP current_module;
3552
3553 // Where it is possible to look in the current symbol context
3554 // first, try that. If this search was successful and --all
3555 // was not passed, don't print anything else.
3556 if (LookupHere (m_interpreter, result, syntax_error))
3557 {
3558 result.GetOutputStream().EOL();
3559 num_successful_lookups++;
3560 if (!m_options.m_print_all)
3561 {
3562 result.SetStatus (eReturnStatusSuccessFinishResult);
3563 return result.Succeeded();
3564 }
3565 }
3566
3567 // Dump all sections for all other modules
3568
Jim Ingham93367902012-05-30 02:19:25 +00003569 ModuleList &target_modules = target->GetImages();
3570 Mutex::Locker modules_locker(target_modules.GetMutex());
3571 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003572 if (num_modules > 0)
3573 {
3574 for (i = 0; i<num_modules && syntax_error == false; ++i)
3575 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003576 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3577
3578 if (module_pointer != current_module.get() &&
3579 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003580 {
3581 result.GetOutputStream().EOL();
3582 num_successful_lookups++;
3583 }
3584 }
3585 }
3586 else
3587 {
3588 result.AppendError ("the target has no associated executable images");
3589 result.SetStatus (eReturnStatusFailed);
3590 return false;
3591 }
3592 }
3593 else
3594 {
3595 // Dump specified images (by basename or fullpath)
3596 const char *arg_cstr;
3597 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3598 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003599 ModuleList module_list;
3600 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3601 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003602 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003603 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003604 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003605 Module *module = module_list.GetModulePointerAtIndex(i);
3606 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003607 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003608 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003609 {
3610 result.GetOutputStream().EOL();
3611 num_successful_lookups++;
3612 }
3613 }
3614 }
3615 }
3616 else
3617 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3618 }
3619 }
3620
3621 if (num_successful_lookups > 0)
3622 result.SetStatus (eReturnStatusSuccessFinishResult);
3623 else
3624 result.SetStatus (eReturnStatusFailed);
3625 }
3626 return result.Succeeded();
3627 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003628
3629 CommandOptions m_options;
3630};
3631
3632OptionDefinition
3633CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3634{
3635 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3636 { 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."},
Greg Clayton2ad894b2012-05-15 18:43:44 +00003637 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3638 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Jim Inghame7a91c12011-06-20 23:38:11 +00003639 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003640 { 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 +00003641 { 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."},
3642 { 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)."},
Jim Inghamf081dab2012-06-04 22:47:34 +00003643 { LLDB_OPT_SET_FROM_TO(3,5),
3644 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
Greg Clayton2ad894b2012-05-15 18:43:44 +00003645 { LLDB_OPT_SET_4, true, "function", 'F', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
3646 { LLDB_OPT_SET_5, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function or symbol by name in one or more target modules."},
3647 { LLDB_OPT_SET_6, true, "type", 't', required_argument, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003648 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
Sean Callanan56d31ec2012-06-06 20:49:55 +00003649 { LLDB_OPT_SET_ALL, false, "all", 'A', no_argument, NULL, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available."},
Greg Clayton2ad894b2012-05-15 18:43:44 +00003650 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003651};
Chris Lattner24943d22010-06-08 16:52:24 +00003652
3653
Jim Inghamd60d94a2011-03-11 03:53:59 +00003654#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003655
3656//-------------------------------------------------------------------------
3657// CommandObjectMultiwordImageSearchPaths
3658//-------------------------------------------------------------------------
3659
Greg Claytone1f50b92011-05-03 22:09:39 +00003660class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003661{
3662public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003663
3664 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3665 CommandObjectMultiword (interpreter,
3666 "target modules search-paths",
3667 "A set of commands for operating on debugger target image search paths.",
3668 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003669 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003670 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3671 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3672 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3673 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3674 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003675 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003676
3677 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003678 {
3679 }
3680};
3681
Greg Claytone1f50b92011-05-03 22:09:39 +00003682
3683
3684#pragma mark CommandObjectTargetModules
3685
3686//-------------------------------------------------------------------------
3687// CommandObjectTargetModules
3688//-------------------------------------------------------------------------
3689
3690class CommandObjectTargetModules : public CommandObjectMultiword
3691{
3692public:
3693 //------------------------------------------------------------------
3694 // Constructors and Destructors
3695 //------------------------------------------------------------------
3696 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3697 CommandObjectMultiword (interpreter,
3698 "target modules",
3699 "A set of commands for accessing information for one or more target modules.",
3700 "target modules <sub-command> ...")
3701 {
3702 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3703 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3704 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3705 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3706 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3707 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3708 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
3709
3710 }
3711 virtual
3712 ~CommandObjectTargetModules()
3713 {
3714 }
3715
3716private:
3717 //------------------------------------------------------------------
3718 // For CommandObjectTargetModules only
3719 //------------------------------------------------------------------
3720 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3721};
3722
3723
Greg Clayton3508c382012-02-24 01:59:29 +00003724
Jim Inghamda26bd22012-06-08 21:56:10 +00003725class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00003726{
3727public:
3728 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003729 CommandObjectParsed (interpreter,
3730 "target symbols add",
3731 "Add a debug symbol file to one of the target's current modules.",
3732 "target symbols add [<symfile>]")
Greg Clayton3508c382012-02-24 01:59:29 +00003733 {
3734 }
3735
3736 virtual
3737 ~CommandObjectTargetSymbolsAdd ()
3738 {
3739 }
3740
Jim Inghamda26bd22012-06-08 21:56:10 +00003741 int
3742 HandleArgumentCompletion (Args &input,
3743 int &cursor_index,
3744 int &cursor_char_position,
3745 OptionElementVector &opt_element_vector,
3746 int match_start_point,
3747 int max_return_elements,
3748 bool &word_complete,
3749 StringList &matches)
3750 {
3751 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
3752 completion_str.erase (cursor_char_position);
3753
3754 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
3755 CommandCompletions::eDiskFileCompletion,
3756 completion_str.c_str(),
3757 match_start_point,
3758 max_return_elements,
3759 NULL,
3760 word_complete,
3761 matches);
3762 return matches.GetSize();
3763 }
3764
3765protected:
Greg Clayton3508c382012-02-24 01:59:29 +00003766 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003767 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00003768 CommandReturnObject &result)
3769 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003770 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3771 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton3508c382012-02-24 01:59:29 +00003772 if (target == NULL)
3773 {
3774 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3775 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003776 }
3777 else
3778 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003779 bool flush = false;
Greg Clayton3508c382012-02-24 01:59:29 +00003780 const size_t argc = args.GetArgumentCount();
3781 if (argc == 0)
3782 {
3783 result.AppendError ("one or more symbol file paths must be specified");
3784 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003785 }
3786 else
3787 {
3788 for (size_t i=0; i<argc; ++i)
3789 {
3790 const char *symfile_path = args.GetArgumentAtIndex(i);
3791 if (symfile_path)
3792 {
3793 FileSpec symfile_spec(symfile_path, true);
3794 ArchSpec arch;
3795 if (symfile_spec.Exists())
3796 {
3797 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
3798 if (symfile_module_sp)
3799 {
3800 // We now have a module that represents a symbol file
3801 // that can be used for a module that might exist in the
3802 // current target, so we need to find that module in the
3803 // target
3804
3805 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_module_sp->GetUUID()));
3806 if (old_module_sp)
3807 {
Greg Claytond4f16c82012-03-29 21:43:25 +00003808 // The module has not yet created its symbol vendor, we can just
3809 // give the existing target module the symfile path to use for
3810 // when it decides to create it!
3811 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
Greg Clayton3508c382012-02-24 01:59:29 +00003812
Greg Claytond4f16c82012-03-29 21:43:25 +00003813 // Let clients know something changed in the module
3814 // if it is currently loaded
3815 ModuleList module_list;
3816 module_list.Append (old_module_sp);
3817 target->ModulesDidLoad (module_list);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003818 flush = true;
Greg Clayton3508c382012-02-24 01:59:29 +00003819 }
3820 }
3821 else
3822 {
3823 result.AppendError ("one or more executable image paths must be specified");
3824 result.SetStatus (eReturnStatusFailed);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003825 break;
Greg Clayton3508c382012-02-24 01:59:29 +00003826 }
3827 result.SetStatus (eReturnStatusSuccessFinishResult);
3828 }
3829 else
3830 {
3831 char resolved_symfile_path[PATH_MAX];
3832 result.SetStatus (eReturnStatusFailed);
3833 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
3834 {
3835 if (strcmp (resolved_symfile_path, symfile_path) != 0)
3836 {
3837 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
3838 break;
3839 }
3840 }
3841 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
3842 break;
3843 }
3844 }
3845 }
3846 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00003847
3848 if (flush)
3849 {
3850 Process *process = exe_ctx.GetProcessPtr();
3851 if (process)
3852 process->Flush();
3853 }
Greg Clayton3508c382012-02-24 01:59:29 +00003854 }
3855 return result.Succeeded();
3856 }
3857
Greg Clayton3508c382012-02-24 01:59:29 +00003858};
3859
3860
3861#pragma mark CommandObjectTargetSymbols
3862
3863//-------------------------------------------------------------------------
3864// CommandObjectTargetSymbols
3865//-------------------------------------------------------------------------
3866
3867class CommandObjectTargetSymbols : public CommandObjectMultiword
3868{
3869public:
3870 //------------------------------------------------------------------
3871 // Constructors and Destructors
3872 //------------------------------------------------------------------
3873 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
3874 CommandObjectMultiword (interpreter,
3875 "target symbols",
3876 "A set of commands for adding and managing debug symbol files.",
3877 "target symbols <sub-command> ...")
3878 {
3879 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
3880
3881 }
3882 virtual
3883 ~CommandObjectTargetSymbols()
3884 {
3885 }
3886
3887private:
3888 //------------------------------------------------------------------
3889 // For CommandObjectTargetModules only
3890 //------------------------------------------------------------------
3891 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
3892};
3893
3894
Jim Inghamd60d94a2011-03-11 03:53:59 +00003895#pragma mark CommandObjectTargetStopHookAdd
3896
3897//-------------------------------------------------------------------------
3898// CommandObjectTargetStopHookAdd
3899//-------------------------------------------------------------------------
3900
Jim Inghamda26bd22012-06-08 21:56:10 +00003901class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00003902{
3903public:
3904
3905 class CommandOptions : public Options
3906 {
3907 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00003908 CommandOptions (CommandInterpreter &interpreter) :
3909 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00003910 m_line_start(0),
3911 m_line_end (UINT_MAX),
3912 m_func_name_type_mask (eFunctionNameTypeAuto),
3913 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00003914 m_thread_specified (false),
3915 m_use_one_liner (false),
3916 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003917 {
3918 }
3919
3920 ~CommandOptions () {}
3921
Greg Claytonb3448432011-03-24 21:19:54 +00003922 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00003923 GetDefinitions ()
3924 {
3925 return g_option_table;
3926 }
3927
3928 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00003929 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003930 {
3931 Error error;
3932 char short_option = (char) m_getopt_table[option_idx].val;
3933 bool success;
3934
3935 switch (short_option)
3936 {
3937 case 'c':
3938 m_class_name = option_arg;
3939 m_sym_ctx_specified = true;
3940 break;
3941
3942 case 'e':
3943 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
3944 if (!success)
3945 {
Greg Clayton9c236732011-10-26 00:56:27 +00003946 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003947 break;
3948 }
3949 m_sym_ctx_specified = true;
3950 break;
3951
3952 case 'l':
3953 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
3954 if (!success)
3955 {
Greg Clayton9c236732011-10-26 00:56:27 +00003956 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003957 break;
3958 }
3959 m_sym_ctx_specified = true;
3960 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00003961
3962 case 'i':
3963 m_no_inlines = true;
3964 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003965
3966 case 'n':
3967 m_function_name = option_arg;
3968 m_func_name_type_mask |= eFunctionNameTypeAuto;
3969 m_sym_ctx_specified = true;
3970 break;
3971
3972 case 'f':
3973 m_file_name = option_arg;
3974 m_sym_ctx_specified = true;
3975 break;
3976 case 's':
3977 m_module_name = option_arg;
3978 m_sym_ctx_specified = true;
3979 break;
3980 case 't' :
3981 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003982 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003983 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00003984 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003985 m_thread_specified = true;
3986 }
3987 break;
3988 case 'T':
3989 m_thread_name = option_arg;
3990 m_thread_specified = true;
3991 break;
3992 case 'q':
3993 m_queue_name = option_arg;
3994 m_thread_specified = true;
3995 break;
3996 case 'x':
3997 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003998 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003999 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004000 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004001 m_thread_specified = true;
4002 }
4003 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004004 case 'o':
4005 m_use_one_liner = true;
4006 m_one_liner = option_arg;
4007 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004008 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004009 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004010 break;
4011 }
4012 return error;
4013 }
4014
4015 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004016 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004017 {
4018 m_class_name.clear();
4019 m_function_name.clear();
4020 m_line_start = 0;
4021 m_line_end = UINT_MAX;
4022 m_file_name.clear();
4023 m_module_name.clear();
4024 m_func_name_type_mask = eFunctionNameTypeAuto;
4025 m_thread_id = LLDB_INVALID_THREAD_ID;
4026 m_thread_index = UINT32_MAX;
4027 m_thread_name.clear();
4028 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004029
4030 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004031 m_sym_ctx_specified = false;
4032 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004033
4034 m_use_one_liner = false;
4035 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004036 }
4037
4038
Greg Claytonb3448432011-03-24 21:19:54 +00004039 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004040
4041 std::string m_class_name;
4042 std::string m_function_name;
4043 uint32_t m_line_start;
4044 uint32_t m_line_end;
4045 std::string m_file_name;
4046 std::string m_module_name;
4047 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4048 lldb::tid_t m_thread_id;
4049 uint32_t m_thread_index;
4050 std::string m_thread_name;
4051 std::string m_queue_name;
4052 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004053 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004054 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004055 // Instance variables to hold the values for one_liner options.
4056 bool m_use_one_liner;
4057 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004058 };
4059
4060 Options *
4061 GetOptions ()
4062 {
4063 return &m_options;
4064 }
4065
4066 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004067 CommandObjectParsed (interpreter,
4068 "target stop-hook add ",
4069 "Add a hook to be executed when the target stops.",
4070 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004071 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004072 {
4073 }
4074
4075 ~CommandObjectTargetStopHookAdd ()
4076 {
4077 }
4078
4079 static size_t
4080 ReadCommandsCallbackFunction (void *baton,
4081 InputReader &reader,
4082 lldb::InputReaderAction notification,
4083 const char *bytes,
4084 size_t bytes_len)
4085 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004086 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004087 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004088 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004089 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004090
4091 switch (notification)
4092 {
4093 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004094 if (!batch_mode)
4095 {
4096 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4097 if (reader.GetPrompt())
4098 out_stream->Printf ("%s", reader.GetPrompt());
4099 out_stream->Flush();
4100 }
Jim Inghame15511a2011-05-05 01:03:36 +00004101 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004102 break;
4103
4104 case eInputReaderDeactivate:
4105 break;
4106
4107 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004108 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004109 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004110 out_stream->Printf ("%s", reader.GetPrompt());
4111 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004112 }
Jim Inghame15511a2011-05-05 01:03:36 +00004113 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004114 break;
4115
Caroline Tice4a348082011-05-02 20:41:46 +00004116 case eInputReaderAsynchronousOutputWritten:
4117 break;
4118
Jim Inghamd60d94a2011-03-11 03:53:59 +00004119 case eInputReaderGotToken:
4120 if (bytes && bytes_len && baton)
4121 {
4122 StringList *commands = new_stop_hook->GetCommandPointer();
4123 if (commands)
4124 {
4125 commands->AppendString (bytes, bytes_len);
4126 }
4127 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004128 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004129 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004130 out_stream->Printf ("%s", reader.GetPrompt());
4131 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004132 }
4133 break;
4134
4135 case eInputReaderInterrupt:
4136 {
4137 // Finish, and cancel the stop hook.
4138 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004139 if (!batch_mode)
4140 {
4141 out_stream->Printf ("Stop hook cancelled.\n");
4142 out_stream->Flush();
4143 }
4144
Jim Inghamd60d94a2011-03-11 03:53:59 +00004145 reader.SetIsDone (true);
4146 }
Jim Inghame15511a2011-05-05 01:03:36 +00004147 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004148 break;
4149
4150 case eInputReaderEndOfFile:
4151 reader.SetIsDone (true);
4152 break;
4153
4154 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004155 if (!got_interrupted && !batch_mode)
4156 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004157 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004158 out_stream->Flush();
4159 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004160 break;
4161 }
4162
4163 return bytes_len;
4164 }
4165
Jim Inghamda26bd22012-06-08 21:56:10 +00004166protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004167 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004168 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004169 {
4170 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4171 if (target)
4172 {
4173 Target::StopHookSP new_hook_sp;
4174 target->AddStopHook (new_hook_sp);
4175
4176 // First step, make the specifier.
4177 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4178 if (m_options.m_sym_ctx_specified)
4179 {
4180 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4181
4182 if (!m_options.m_module_name.empty())
4183 {
4184 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4185 }
4186
4187 if (!m_options.m_class_name.empty())
4188 {
4189 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4190 }
4191
4192 if (!m_options.m_file_name.empty())
4193 {
4194 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4195 }
4196
4197 if (m_options.m_line_start != 0)
4198 {
4199 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4200 }
4201
4202 if (m_options.m_line_end != UINT_MAX)
4203 {
4204 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4205 }
4206
4207 if (!m_options.m_function_name.empty())
4208 {
4209 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4210 }
4211 }
4212
4213 if (specifier_ap.get())
4214 new_hook_sp->SetSpecifier (specifier_ap.release());
4215
4216 // Next see if any of the thread options have been entered:
4217
4218 if (m_options.m_thread_specified)
4219 {
4220 ThreadSpec *thread_spec = new ThreadSpec();
4221
4222 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4223 {
4224 thread_spec->SetTID (m_options.m_thread_id);
4225 }
4226
4227 if (m_options.m_thread_index != UINT32_MAX)
4228 thread_spec->SetIndex (m_options.m_thread_index);
4229
4230 if (!m_options.m_thread_name.empty())
4231 thread_spec->SetName (m_options.m_thread_name.c_str());
4232
4233 if (!m_options.m_queue_name.empty())
4234 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4235
4236 new_hook_sp->SetThreadSpecifier (thread_spec);
4237
4238 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004239 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004240 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004241 // Use one-liner.
4242 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004243 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004244 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004245 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004246 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004247 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4248 // the new stop hook's command string.
4249 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4250 if (!reader_sp)
4251 {
4252 result.AppendError("out of memory\n");
4253 result.SetStatus (eReturnStatusFailed);
4254 target->RemoveStopHookByID (new_hook_sp->GetID());
4255 return false;
4256 }
4257
4258 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4259 new_hook_sp.get(), // baton
4260 eInputReaderGranularityLine, // token size, to pass to callback function
4261 "DONE", // end token
4262 "> ", // prompt
4263 true)); // echo input
4264 if (!err.Success())
4265 {
4266 result.AppendError (err.AsCString());
4267 result.SetStatus (eReturnStatusFailed);
4268 target->RemoveStopHookByID (new_hook_sp->GetID());
4269 return false;
4270 }
4271 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004272 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004273 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4274 }
4275 else
4276 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004277 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004278 result.SetStatus (eReturnStatusFailed);
4279 }
4280
4281 return result.Succeeded();
4282 }
4283private:
4284 CommandOptions m_options;
4285};
4286
Greg Claytonb3448432011-03-24 21:19:54 +00004287OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004288CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4289{
Johnny Chen60fe60e2011-05-02 23:47:55 +00004290 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
4291 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004292 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4293 "Set the module within which the stop-hook is to be run."},
4294 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
4295 "The stop hook is run only for the thread whose index matches this argument."},
4296 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
4297 "The stop hook is run only for the thread whose TID matches this argument."},
4298 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
4299 "The stop hook is run only for the thread whose thread name matches this argument."},
4300 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
4301 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4302 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4303 "Specify the source file within which the stop-hook is to be run." },
4304 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4305 "Set the start of the line range for which the stop-hook is to be run."},
4306 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4307 "Set the end of the line range for which the stop-hook is to be run."},
4308 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
4309 "Specify the class within which the stop-hook is to be run." },
4310 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4311 "Set the function name within which the stop hook will be run." },
4312 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4313};
4314
4315#pragma mark CommandObjectTargetStopHookDelete
4316
4317//-------------------------------------------------------------------------
4318// CommandObjectTargetStopHookDelete
4319//-------------------------------------------------------------------------
4320
Jim Inghamda26bd22012-06-08 21:56:10 +00004321class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004322{
4323public:
4324
4325 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004326 CommandObjectParsed (interpreter,
4327 "target stop-hook delete",
4328 "Delete a stop-hook.",
4329 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004330 {
4331 }
4332
4333 ~CommandObjectTargetStopHookDelete ()
4334 {
4335 }
4336
Jim Inghamda26bd22012-06-08 21:56:10 +00004337protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004338 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004339 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004340 {
4341 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4342 if (target)
4343 {
4344 // FIXME: see if we can use the breakpoint id style parser?
4345 size_t num_args = command.GetArgumentCount();
4346 if (num_args == 0)
4347 {
4348 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4349 {
4350 result.SetStatus (eReturnStatusFailed);
4351 return false;
4352 }
4353 else
4354 {
4355 target->RemoveAllStopHooks();
4356 }
4357 }
4358 else
4359 {
4360 bool success;
4361 for (size_t i = 0; i < num_args; i++)
4362 {
4363 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4364 if (!success)
4365 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004366 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004367 result.SetStatus(eReturnStatusFailed);
4368 return false;
4369 }
4370 success = target->RemoveStopHookByID (user_id);
4371 if (!success)
4372 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004373 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004374 result.SetStatus(eReturnStatusFailed);
4375 return false;
4376 }
4377 }
4378 }
4379 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4380 }
4381 else
4382 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004383 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004384 result.SetStatus (eReturnStatusFailed);
4385 }
4386
4387 return result.Succeeded();
4388 }
4389};
4390#pragma mark CommandObjectTargetStopHookEnableDisable
4391
4392//-------------------------------------------------------------------------
4393// CommandObjectTargetStopHookEnableDisable
4394//-------------------------------------------------------------------------
4395
Jim Inghamda26bd22012-06-08 21:56:10 +00004396class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004397{
4398public:
4399
4400 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004401 CommandObjectParsed (interpreter,
4402 name,
4403 help,
4404 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004405 m_enable (enable)
4406 {
4407 }
4408
4409 ~CommandObjectTargetStopHookEnableDisable ()
4410 {
4411 }
4412
Jim Inghamda26bd22012-06-08 21:56:10 +00004413protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004414 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004415 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004416 {
4417 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4418 if (target)
4419 {
4420 // FIXME: see if we can use the breakpoint id style parser?
4421 size_t num_args = command.GetArgumentCount();
4422 bool success;
4423
4424 if (num_args == 0)
4425 {
4426 target->SetAllStopHooksActiveState (m_enable);
4427 }
4428 else
4429 {
4430 for (size_t i = 0; i < num_args; i++)
4431 {
4432 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4433 if (!success)
4434 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004435 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004436 result.SetStatus(eReturnStatusFailed);
4437 return false;
4438 }
4439 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4440 if (!success)
4441 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004442 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004443 result.SetStatus(eReturnStatusFailed);
4444 return false;
4445 }
4446 }
4447 }
4448 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4449 }
4450 else
4451 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004452 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004453 result.SetStatus (eReturnStatusFailed);
4454 }
4455 return result.Succeeded();
4456 }
4457private:
4458 bool m_enable;
4459};
4460
4461#pragma mark CommandObjectTargetStopHookList
4462
4463//-------------------------------------------------------------------------
4464// CommandObjectTargetStopHookList
4465//-------------------------------------------------------------------------
4466
Jim Inghamda26bd22012-06-08 21:56:10 +00004467class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004468{
4469public:
4470
4471 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004472 CommandObjectParsed (interpreter,
4473 "target stop-hook list",
4474 "List all stop-hooks.",
4475 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004476 {
4477 }
4478
4479 ~CommandObjectTargetStopHookList ()
4480 {
4481 }
4482
Jim Inghamda26bd22012-06-08 21:56:10 +00004483protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004484 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004485 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004486 {
4487 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004488 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004489 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004490 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004491 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004492 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004493 }
4494
4495 size_t num_hooks = target->GetNumStopHooks ();
4496 if (num_hooks == 0)
4497 {
4498 result.GetOutputStream().PutCString ("No stop hooks.\n");
4499 }
4500 else
4501 {
4502 for (size_t i = 0; i < num_hooks; i++)
4503 {
4504 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4505 if (i > 0)
4506 result.GetOutputStream().PutCString ("\n");
4507 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4508 }
4509 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004510 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004511 return result.Succeeded();
4512 }
4513};
4514
4515#pragma mark CommandObjectMultiwordTargetStopHooks
4516//-------------------------------------------------------------------------
4517// CommandObjectMultiwordTargetStopHooks
4518//-------------------------------------------------------------------------
4519
4520class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4521{
4522public:
4523
4524 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4525 CommandObjectMultiword (interpreter,
4526 "target stop-hook",
4527 "A set of commands for operating on debugger target stop-hooks.",
4528 "target stop-hook <subcommand> [<subcommand-options>]")
4529 {
4530 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4531 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4532 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4533 false,
4534 "target stop-hook disable [<id>]",
4535 "Disable a stop-hook.",
4536 "target stop-hook disable")));
4537 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4538 true,
4539 "target stop-hook enable [<id>]",
4540 "Enable a stop-hook.",
4541 "target stop-hook enable")));
4542 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4543 }
4544
4545 ~CommandObjectMultiwordTargetStopHooks()
4546 {
4547 }
4548};
4549
4550
Chris Lattner24943d22010-06-08 16:52:24 +00004551
4552#pragma mark CommandObjectMultiwordTarget
4553
4554//-------------------------------------------------------------------------
4555// CommandObjectMultiwordTarget
4556//-------------------------------------------------------------------------
4557
Greg Clayton63094e02010-06-23 01:19:29 +00004558CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004559 CommandObjectMultiword (interpreter,
4560 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004561 "A set of commands for operating on debugger targets.",
4562 "target <subcommand> [<subcommand-options>]")
4563{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004564
4565 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004566 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004567 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4568 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004569 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004570 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00004571 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004572 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004573}
4574
4575CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4576{
4577}
4578
Greg Claytonabe0fed2011-04-18 08:33:37 +00004579