blob: e193c486d692c398ff857dedf033bc9919a766b2 [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();
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000477 // Bail out if don't have any targets.
478 if (num_targets == 0) {
479 result.AppendError("no targets to delete");
480 result.SetStatus(eReturnStatusFailed);
481 success = false;
482 }
483
Greg Clayton153ccd72011-08-10 02:10:13 +0000484 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
485 {
486 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
487 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
488 if (success)
489 {
490 if (target_idx < num_targets)
491 {
492 target_sp = target_list.GetTargetAtIndex (target_idx);
493 if (target_sp)
494 {
495 delete_target_list.push_back (target_sp);
496 continue;
497 }
498 }
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000499 if (num_targets > 1)
500 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
501 target_idx,
502 num_targets - 1);
503 else
504 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
505 target_idx);
506
Greg Clayton153ccd72011-08-10 02:10:13 +0000507 result.SetStatus (eReturnStatusFailed);
508 success = false;
509 }
510 else
511 {
512 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
513 result.SetStatus (eReturnStatusFailed);
514 success = false;
515 }
516 }
517
518 }
519 else
520 {
521 target_sp = target_list.GetSelectedTarget();
522 if (target_sp)
523 {
524 delete_target_list.push_back (target_sp);
525 }
526 else
527 {
528 result.AppendErrorWithFormat("no target is currently selected\n");
529 result.SetStatus (eReturnStatusFailed);
530 success = false;
531 }
532 }
533 if (success)
534 {
535 const size_t num_targets_to_delete = delete_target_list.size();
536 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
537 {
538 target_sp = delete_target_list[idx];
539 target_list.DeleteTarget(target_sp);
540 target_sp->Destroy();
541 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000542 // If "--clean" was specified, prune any orphaned shared modules from
543 // the global shared module list
544 if (m_cleanup_option.GetOptionValue ())
545 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000546 const bool mandatory = true;
547 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000548 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000549 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
550 result.SetStatus(eReturnStatusSuccessFinishResult);
551 }
552
553 return result.Succeeded();
554 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000555
Greg Clayton5beb99d2011-08-11 02:48:45 +0000556 OptionGroupOptions m_option_group;
557 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000558};
559
Greg Claytonabe0fed2011-04-18 08:33:37 +0000560
Greg Clayton801417e2011-07-07 01:59:51 +0000561#pragma mark CommandObjectTargetVariable
562
563//----------------------------------------------------------------------
564// "target variable"
565//----------------------------------------------------------------------
566
Jim Inghamda26bd22012-06-08 21:56:10 +0000567class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000568{
569public:
570 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000571 CommandObjectParsed (interpreter,
572 "target variable",
573 "Read global variable(s) prior to running your binary.",
574 NULL,
575 0),
Greg Clayton801417e2011-07-07 01:59:51 +0000576 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000577 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000578 m_option_format (eFormatDefault),
Greg Clayton801417e2011-07-07 01:59:51 +0000579 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."),
580 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."),
581 m_varobj_options()
582 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000583 CommandArgumentEntry arg;
584 CommandArgumentData var_name_arg;
585
586 // Define the first (and only) variant of this arg.
587 var_name_arg.arg_type = eArgTypeVarName;
588 var_name_arg.arg_repetition = eArgRepeatPlus;
589
590 // There is only one variant this argument could be; put it into the argument entry.
591 arg.push_back (var_name_arg);
592
593 // Push the data for the first argument into the m_arguments vector.
594 m_arguments.push_back (arg);
595
Greg Clayton801417e2011-07-07 01:59:51 +0000596 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000597 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000598 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 +0000599 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
600 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
601 m_option_group.Finalize();
602 }
603
604 virtual
605 ~CommandObjectTargetVariable ()
606 {
607 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000608
609 void
610 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
611 {
Enrico Granata19030d82011-08-15 18:01:31 +0000612 ValueObject::DumpValueObjectOptions options;
613
Enrico Granata3069c622012-03-01 04:24:26 +0000614 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000615 .SetMaximumDepth(m_varobj_options.max_depth)
616 .SetShowTypes(m_varobj_options.show_types)
617 .SetShowLocation(m_varobj_options.show_location)
618 .SetUseObjectiveC(m_varobj_options.use_objc)
619 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000620 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000621 .SetFlatOutput(m_varobj_options.flat_output)
622 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
623 .SetIgnoreCap(m_varobj_options.ignore_cap);
624
Greg Clayton5d81f492011-07-08 21:46:14 +0000625 switch (var_sp->GetScope())
626 {
627 case eValueTypeVariableGlobal:
628 if (m_option_variable.show_scope)
629 s.PutCString("GLOBAL: ");
630 break;
631
632 case eValueTypeVariableStatic:
633 if (m_option_variable.show_scope)
634 s.PutCString("STATIC: ");
635 break;
636
637 case eValueTypeVariableArgument:
638 if (m_option_variable.show_scope)
639 s.PutCString(" ARG: ");
640 break;
641
642 case eValueTypeVariableLocal:
643 if (m_option_variable.show_scope)
644 s.PutCString(" LOCAL: ");
645 break;
646
647 default:
648 break;
649 }
650
Greg Claytonfb816422011-07-10 19:21:23 +0000651 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000652 {
Greg Claytonfb816422011-07-10 19:21:23 +0000653 bool show_fullpaths = false;
654 bool show_module = true;
655 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
656 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000657 }
658
Greg Claytona42880a2011-10-25 06:44:01 +0000659 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000660 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000661 options.SetFormat(format);
662
663 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000664
665 ValueObject::DumpValueObject (s,
666 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000667 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000668
669 }
Greg Clayton801417e2011-07-07 01:59:51 +0000670
Greg Clayton5d81f492011-07-08 21:46:14 +0000671
672 static uint32_t GetVariableCallback (void *baton,
673 const char *name,
674 VariableList &variable_list)
675 {
676 Target *target = static_cast<Target *>(baton);
677 if (target)
678 {
679 return target->GetImages().FindGlobalVariables (ConstString(name),
680 true,
681 UINT32_MAX,
682 variable_list);
683 }
684 return 0;
685 }
686
687
688
Jim Inghamda26bd22012-06-08 21:56:10 +0000689 Options *
690 GetOptions ()
691 {
692 return &m_option_group;
693 }
694
695protected:
Greg Clayton801417e2011-07-07 01:59:51 +0000696 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000697 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000698 {
699 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000700 Target *target = exe_ctx.GetTargetPtr();
701 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000702 {
703 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000704 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000705 if (argc > 0)
706 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000707
Greg Clayton801417e2011-07-07 01:59:51 +0000708 for (size_t idx = 0; idx < argc; ++idx)
709 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000710 VariableList variable_list;
711 ValueObjectList valobj_list;
712
Greg Clayton368f8222011-07-07 04:38:25 +0000713 const char *arg = args.GetArgumentAtIndex(idx);
714 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000715 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000716 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000717 {
Greg Clayton368f8222011-07-07 04:38:25 +0000718 RegularExpression regex(arg);
719 if (!regex.IsValid ())
720 {
721 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
722 result.SetStatus (eReturnStatusFailed);
723 return false;
724 }
Greg Claytonfb816422011-07-10 19:21:23 +0000725 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000726 matches = target->GetImages().FindGlobalVariables (regex,
727 true,
728 UINT32_MAX,
729 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000730 }
731 else
732 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000733 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000734 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000735 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000736 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000737 variable_list,
738 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000739 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000740 }
741
742 if (matches == 0)
743 {
744 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
745 result.SetStatus (eReturnStatusFailed);
746 return false;
747 }
748 else
749 {
Greg Clayton801417e2011-07-07 01:59:51 +0000750 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
751 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000752 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000753 if (var_sp)
754 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000755 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
756 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000757 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000758
759 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000760 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000761 }
762 }
763 }
764 }
765 }
766 else
767 {
Greg Claytonfac93882011-10-05 22:17:32 +0000768 bool success = false;
769 StackFrame *frame = exe_ctx.GetFramePtr();
770 CompileUnit *comp_unit = NULL;
771 if (frame)
772 {
773 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
774 if (comp_unit)
775 {
776 const bool can_create = true;
777 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
778 if (comp_unit_varlist_sp)
779 {
780 size_t count = comp_unit_varlist_sp->GetSize();
781 if (count > 0)
782 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000783 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000784 comp_unit->GetDirectory().GetCString(),
785 comp_unit->GetFilename().GetCString());
786
787 success = true;
788 for (uint32_t i=0; i<count; ++i)
789 {
790 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
791 if (var_sp)
792 {
793 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
794
795 if (valobj_sp)
796 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
797 }
798 }
799 }
800 }
801 }
802 }
803 if (!success)
804 {
805 if (frame)
806 {
807 if (comp_unit)
808 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
809 comp_unit->GetDirectory().GetCString(),
810 comp_unit->GetFilename().GetCString());
811 else
812 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
813 }
814 else
815 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
816 result.SetStatus (eReturnStatusFailed);
817 }
Greg Clayton801417e2011-07-07 01:59:51 +0000818 }
819 }
820 else
821 {
822 result.AppendError ("invalid target, create a debug target using the 'target create' command");
823 result.SetStatus (eReturnStatusFailed);
824 return false;
825 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000826
827 if (m_interpreter.TruncationWarningNecessary())
828 {
829 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
830 m_cmd_name.c_str());
831 m_interpreter.TruncationWarningGiven();
832 }
833
Greg Clayton801417e2011-07-07 01:59:51 +0000834 return result.Succeeded();
835 }
836
Greg Clayton801417e2011-07-07 01:59:51 +0000837 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000838 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000839 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000840 OptionGroupFileList m_option_compile_units;
841 OptionGroupFileList m_option_shared_libraries;
842 OptionGroupValueObjectDisplay m_varobj_options;
843
844};
845
846
Greg Claytone1f50b92011-05-03 22:09:39 +0000847#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000848
Jim Inghamda26bd22012-06-08 21:56:10 +0000849class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000850{
851public:
852
Greg Claytone1f50b92011-05-03 22:09:39 +0000853 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000854 CommandObjectParsed (interpreter,
855 "target modules search-paths add",
856 "Add new image search paths substitution pairs to the current target.",
857 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000858 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000859 CommandArgumentEntry arg;
860 CommandArgumentData old_prefix_arg;
861 CommandArgumentData new_prefix_arg;
862
863 // Define the first variant of this arg pair.
864 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
865 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
866
867 // Define the first variant of this arg pair.
868 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
869 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
870
871 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
872 // must always occur together, they are treated as two variants of one argument rather than two independent
873 // arguments. Push them both into the first argument position for m_arguments...
874
875 arg.push_back (old_prefix_arg);
876 arg.push_back (new_prefix_arg);
877
878 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000879 }
880
Greg Claytone1f50b92011-05-03 22:09:39 +0000881 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000882 {
883 }
884
Jim Inghamda26bd22012-06-08 21:56:10 +0000885protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000886 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000887 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000888 CommandReturnObject &result)
889 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000890 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000891 if (target)
892 {
893 uint32_t argc = command.GetArgumentCount();
894 if (argc & 1)
895 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000896 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000897 result.SetStatus (eReturnStatusFailed);
898 }
899 else
900 {
901 for (uint32_t i=0; i<argc; i+=2)
902 {
903 const char *from = command.GetArgumentAtIndex(i);
904 const char *to = command.GetArgumentAtIndex(i+1);
905
906 if (from[0] && to[0])
907 {
908 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000909 target->GetImageSearchPathList().Append (ConstString(from),
910 ConstString(to),
911 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000912 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000913 }
914 else
915 {
916 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000917 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000918 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000919 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000920 result.SetStatus (eReturnStatusFailed);
921 }
922 }
923 }
924 }
925 else
926 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000927 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000928 result.SetStatus (eReturnStatusFailed);
929 }
930 return result.Succeeded();
931 }
932};
933
Greg Claytone1f50b92011-05-03 22:09:39 +0000934#pragma mark CommandObjectTargetModulesSearchPathsClear
935
Jim Inghamda26bd22012-06-08 21:56:10 +0000936class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000937{
938public:
939
Greg Claytone1f50b92011-05-03 22:09:39 +0000940 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000941 CommandObjectParsed (interpreter,
942 "target modules search-paths clear",
943 "Clear all current image search path substitution pairs from the current target.",
944 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000945 {
946 }
947
Greg Claytone1f50b92011-05-03 22:09:39 +0000948 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000949 {
950 }
951
Jim Inghamda26bd22012-06-08 21:56:10 +0000952protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000953 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000954 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000955 CommandReturnObject &result)
956 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000957 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000958 if (target)
959 {
960 bool notify = true;
961 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000962 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000963 }
964 else
965 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000966 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000967 result.SetStatus (eReturnStatusFailed);
968 }
969 return result.Succeeded();
970 }
971};
972
Greg Claytone1f50b92011-05-03 22:09:39 +0000973#pragma mark CommandObjectTargetModulesSearchPathsInsert
974
Jim Inghamda26bd22012-06-08 21:56:10 +0000975class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000976{
977public:
978
Greg Claytone1f50b92011-05-03 22:09:39 +0000979 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000980 CommandObjectParsed (interpreter,
981 "target modules search-paths insert",
982 "Insert a new image search path substitution pair into the current target at the specified index.",
983 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000984 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000985 CommandArgumentEntry arg1;
986 CommandArgumentEntry arg2;
987 CommandArgumentData index_arg;
988 CommandArgumentData old_prefix_arg;
989 CommandArgumentData new_prefix_arg;
990
991 // Define the first and only variant of this arg.
992 index_arg.arg_type = eArgTypeIndex;
993 index_arg.arg_repetition = eArgRepeatPlain;
994
995 // Put the one and only variant into the first arg for m_arguments:
996 arg1.push_back (index_arg);
997
998 // Define the first variant of this arg pair.
999 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1000 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1001
1002 // Define the first variant of this arg pair.
1003 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1004 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1005
1006 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1007 // must always occur together, they are treated as two variants of one argument rather than two independent
1008 // arguments. Push them both into the same argument position for m_arguments...
1009
1010 arg2.push_back (old_prefix_arg);
1011 arg2.push_back (new_prefix_arg);
1012
1013 // Add arguments to m_arguments.
1014 m_arguments.push_back (arg1);
1015 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001016 }
1017
Greg Claytone1f50b92011-05-03 22:09:39 +00001018 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001019 {
1020 }
1021
Jim Inghamda26bd22012-06-08 21:56:10 +00001022protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001023 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001024 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001025 CommandReturnObject &result)
1026 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001027 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001028 if (target)
1029 {
1030 uint32_t argc = command.GetArgumentCount();
1031 // check for at least 3 arguments and an odd nubmer of parameters
1032 if (argc >= 3 && argc & 1)
1033 {
1034 bool success = false;
1035
1036 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1037
1038 if (!success)
1039 {
1040 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1041 result.SetStatus (eReturnStatusFailed);
1042 return result.Succeeded();
1043 }
1044
1045 // shift off the index
1046 command.Shift();
1047 argc = command.GetArgumentCount();
1048
1049 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1050 {
1051 const char *from = command.GetArgumentAtIndex(i);
1052 const char *to = command.GetArgumentAtIndex(i+1);
1053
1054 if (from[0] && to[0])
1055 {
1056 bool last_pair = ((argc - i) == 2);
1057 target->GetImageSearchPathList().Insert (ConstString(from),
1058 ConstString(to),
1059 insert_idx,
1060 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001061 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001062 }
1063 else
1064 {
1065 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001066 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001067 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001068 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001069 result.SetStatus (eReturnStatusFailed);
1070 return false;
1071 }
1072 }
1073 }
1074 else
1075 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001076 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001077 result.SetStatus (eReturnStatusFailed);
1078 return result.Succeeded();
1079 }
1080
1081 }
1082 else
1083 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001084 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001085 result.SetStatus (eReturnStatusFailed);
1086 }
1087 return result.Succeeded();
1088 }
1089};
1090
Greg Claytone1f50b92011-05-03 22:09:39 +00001091
1092#pragma mark CommandObjectTargetModulesSearchPathsList
1093
1094
Jim Inghamda26bd22012-06-08 21:56:10 +00001095class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001096{
1097public:
1098
Greg Claytone1f50b92011-05-03 22:09:39 +00001099 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001100 CommandObjectParsed (interpreter,
1101 "target modules search-paths list",
1102 "List all current image search path substitution pairs in the current target.",
1103 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001104 {
1105 }
1106
Greg Claytone1f50b92011-05-03 22:09:39 +00001107 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001108 {
1109 }
1110
Jim Inghamda26bd22012-06-08 21:56:10 +00001111protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001112 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001113 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001114 CommandReturnObject &result)
1115 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001116 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001117 if (target)
1118 {
1119 if (command.GetArgumentCount() != 0)
1120 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001121 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001122 result.SetStatus (eReturnStatusFailed);
1123 return result.Succeeded();
1124 }
1125
1126 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001127 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001128 }
1129 else
1130 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001131 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001132 result.SetStatus (eReturnStatusFailed);
1133 }
1134 return result.Succeeded();
1135 }
1136};
1137
Greg Claytone1f50b92011-05-03 22:09:39 +00001138#pragma mark CommandObjectTargetModulesSearchPathsQuery
1139
Jim Inghamda26bd22012-06-08 21:56:10 +00001140class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001141{
1142public:
1143
Greg Claytone1f50b92011-05-03 22:09:39 +00001144 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001145 CommandObjectParsed (interpreter,
1146 "target modules search-paths query",
1147 "Transform a path using the first applicable image search path.",
1148 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001149 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001150 CommandArgumentEntry arg;
1151 CommandArgumentData path_arg;
1152
1153 // Define the first (and only) variant of this arg.
1154 path_arg.arg_type = eArgTypePath;
1155 path_arg.arg_repetition = eArgRepeatPlain;
1156
1157 // There is only one variant this argument could be; put it into the argument entry.
1158 arg.push_back (path_arg);
1159
1160 // Push the data for the first argument into the m_arguments vector.
1161 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001162 }
1163
Greg Claytone1f50b92011-05-03 22:09:39 +00001164 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001165 {
1166 }
1167
Jim Inghamda26bd22012-06-08 21:56:10 +00001168protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001169 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001170 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001171 CommandReturnObject &result)
1172 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001173 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001174 if (target)
1175 {
1176 if (command.GetArgumentCount() != 1)
1177 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001178 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001179 result.SetStatus (eReturnStatusFailed);
1180 return result.Succeeded();
1181 }
1182
1183 ConstString orig(command.GetArgumentAtIndex(0));
1184 ConstString transformed;
1185 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1186 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1187 else
1188 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001189
1190 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001191 }
1192 else
1193 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001194 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001195 result.SetStatus (eReturnStatusFailed);
1196 }
1197 return result.Succeeded();
1198 }
1199};
1200
Greg Claytone1f50b92011-05-03 22:09:39 +00001201//----------------------------------------------------------------------
1202// Static Helper functions
1203//----------------------------------------------------------------------
1204static void
1205DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1206{
1207 if (module)
1208 {
1209 const char *arch_cstr;
1210 if (full_triple)
1211 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1212 else
1213 arch_cstr = module->GetArchitecture().GetArchitectureName();
1214 if (width)
1215 strm.Printf("%-*s", width, arch_cstr);
1216 else
1217 strm.PutCString(arch_cstr);
1218 }
1219}
1220
1221static void
1222DumpModuleUUID (Stream &strm, Module *module)
1223{
Greg Clayton153ccd72011-08-10 02:10:13 +00001224 if (module->GetUUID().IsValid())
1225 module->GetUUID().Dump (&strm);
1226 else
1227 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001228}
1229
1230static uint32_t
1231DumpCompileUnitLineTable
1232(
1233 CommandInterpreter &interpreter,
1234 Stream &strm,
1235 Module *module,
1236 const FileSpec &file_spec,
1237 bool load_addresses
1238 )
1239{
1240 uint32_t num_matches = 0;
1241 if (module)
1242 {
1243 SymbolContextList sc_list;
1244 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1245 0,
1246 false,
1247 eSymbolContextCompUnit,
1248 sc_list);
1249
1250 for (uint32_t i=0; i<num_matches; ++i)
1251 {
1252 SymbolContext sc;
1253 if (sc_list.GetContextAtIndex(i, sc))
1254 {
1255 if (i > 0)
1256 strm << "\n\n";
1257
1258 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1259 << module->GetFileSpec().GetFilename() << "\n";
1260 LineTable *line_table = sc.comp_unit->GetLineTable();
1261 if (line_table)
1262 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001263 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001264 lldb::eDescriptionLevelBrief);
1265 else
1266 strm << "No line table";
1267 }
1268 }
1269 }
1270 return num_matches;
1271}
1272
1273static void
1274DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1275{
1276 if (file_spec_ptr)
1277 {
1278 if (width > 0)
1279 {
1280 char fullpath[PATH_MAX];
1281 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1282 {
1283 strm.Printf("%-*s", width, fullpath);
1284 return;
1285 }
1286 }
1287 else
1288 {
1289 file_spec_ptr->Dump(&strm);
1290 return;
1291 }
1292 }
1293 // Keep the width spacing correct if things go wrong...
1294 if (width > 0)
1295 strm.Printf("%-*s", width, "");
1296}
1297
1298static void
1299DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1300{
1301 if (file_spec_ptr)
1302 {
1303 if (width > 0)
1304 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1305 else
1306 file_spec_ptr->GetDirectory().Dump(&strm);
1307 return;
1308 }
1309 // Keep the width spacing correct if things go wrong...
1310 if (width > 0)
1311 strm.Printf("%-*s", width, "");
1312}
1313
1314static void
1315DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1316{
1317 if (file_spec_ptr)
1318 {
1319 if (width > 0)
1320 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1321 else
1322 file_spec_ptr->GetFilename().Dump(&strm);
1323 return;
1324 }
1325 // Keep the width spacing correct if things go wrong...
1326 if (width > 0)
1327 strm.Printf("%-*s", width, "");
1328}
1329
1330
1331static void
1332DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1333{
1334 if (module)
1335 {
1336 ObjectFile *objfile = module->GetObjectFile ();
1337 if (objfile)
1338 {
1339 Symtab *symtab = objfile->GetSymtab();
1340 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001341 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001342 }
1343 }
1344}
1345
1346static void
1347DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1348{
1349 if (module)
1350 {
1351 ObjectFile *objfile = module->GetObjectFile ();
1352 if (objfile)
1353 {
1354 SectionList *section_list = objfile->GetSectionList();
1355 if (section_list)
1356 {
1357 strm.PutCString ("Sections for '");
1358 strm << module->GetFileSpec();
1359 if (module->GetObjectName())
1360 strm << '(' << module->GetObjectName() << ')';
1361 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1362 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001363 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001364 strm.IndentLess();
1365 }
1366 }
1367 }
1368}
1369
1370static bool
1371DumpModuleSymbolVendor (Stream &strm, Module *module)
1372{
1373 if (module)
1374 {
1375 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1376 if (symbol_vendor)
1377 {
1378 symbol_vendor->Dump(&strm);
1379 return true;
1380 }
1381 }
1382 return false;
1383}
1384
Greg Clayton2ad894b2012-05-15 18:43:44 +00001385static void
1386DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1387{
1388 strm.IndentMore();
1389 strm.Indent (" Address: ");
1390 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1391 strm.PutCString (" (");
1392 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1393 strm.PutCString (")\n");
1394 strm.Indent (" Summary: ");
1395 const uint32_t save_indent = strm.GetIndentLevel ();
1396 strm.SetIndentLevel (save_indent + 13);
1397 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1398 strm.SetIndentLevel (save_indent);
1399 // Print out detailed address information when verbose is enabled
1400 if (verbose)
1401 {
1402 strm.EOL();
1403 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1404 }
1405 strm.IndentLess();
1406}
1407
Greg Claytone1f50b92011-05-03 22:09:39 +00001408static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001409LookupAddressInModule (CommandInterpreter &interpreter,
1410 Stream &strm,
1411 Module *module,
1412 uint32_t resolve_mask,
1413 lldb::addr_t raw_addr,
1414 lldb::addr_t offset,
1415 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001416{
1417 if (module)
1418 {
1419 lldb::addr_t addr = raw_addr - offset;
1420 Address so_addr;
1421 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001422 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001423 if (target && !target->GetSectionLoadList().IsEmpty())
1424 {
1425 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1426 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001427 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001428 return false;
1429 }
1430 else
1431 {
1432 if (!module->ResolveFileAddress (addr, so_addr))
1433 return false;
1434 }
1435
Greg Claytone1f50b92011-05-03 22:09:39 +00001436 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001437 DumpAddress (exe_scope, so_addr, verbose, strm);
1438// strm.IndentMore();
1439// strm.Indent (" Address: ");
1440// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1441// strm.PutCString (" (");
1442// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1443// strm.PutCString (")\n");
1444// strm.Indent (" Summary: ");
1445// const uint32_t save_indent = strm.GetIndentLevel ();
1446// strm.SetIndentLevel (save_indent + 13);
1447// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1448// strm.SetIndentLevel (save_indent);
1449// // Print out detailed address information when verbose is enabled
1450// if (verbose)
1451// {
1452// strm.EOL();
1453// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1454// }
1455// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001456 return true;
1457 }
1458
1459 return false;
1460}
1461
1462static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001463LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001464{
1465 if (module)
1466 {
1467 SymbolContext sc;
1468
1469 ObjectFile *objfile = module->GetObjectFile ();
1470 if (objfile)
1471 {
1472 Symtab *symtab = objfile->GetSymtab();
1473 if (symtab)
1474 {
1475 uint32_t i;
1476 std::vector<uint32_t> match_indexes;
1477 ConstString symbol_name (name);
1478 uint32_t num_matches = 0;
1479 if (name_is_regex)
1480 {
1481 RegularExpression name_regexp(name);
1482 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1483 eSymbolTypeAny,
1484 match_indexes);
1485 }
1486 else
1487 {
1488 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1489 }
1490
1491
1492 if (num_matches > 0)
1493 {
1494 strm.Indent ();
1495 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1496 name_is_regex ? "the regular expression " : "", name);
1497 DumpFullpath (strm, &module->GetFileSpec(), 0);
1498 strm.PutCString(":\n");
1499 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001500 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001501 for (i=0; i < num_matches; ++i)
1502 {
1503 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001504 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1505 symbol->GetAddress(),
1506 verbose,
1507 strm);
1508
1509// strm.Indent ();
1510// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001511 }
1512 strm.IndentLess ();
1513 return num_matches;
1514 }
1515 }
1516 }
1517 }
1518 return 0;
1519}
1520
1521
1522static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001523DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001524{
1525 strm.IndentMore ();
1526 uint32_t i;
1527 const uint32_t num_matches = sc_list.GetSize();
1528
1529 for (i=0; i<num_matches; ++i)
1530 {
1531 SymbolContext sc;
1532 if (sc_list.GetContextAtIndex(i, sc))
1533 {
Sean Callanand7793d22012-02-11 00:24:04 +00001534 AddressRange range;
1535
1536 sc.GetAddressRange(eSymbolContextEverything,
1537 0,
1538 true,
1539 range);
1540
Greg Clayton2ad894b2012-05-15 18:43:44 +00001541 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001542 }
1543 }
1544 strm.IndentLess ();
1545}
1546
1547static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001548LookupFunctionInModule (CommandInterpreter &interpreter,
1549 Stream &strm,
1550 Module *module,
1551 const char *name,
1552 bool name_is_regex,
1553 bool include_inlines,
1554 bool include_symbols,
1555 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001556{
1557 if (module && name && name[0])
1558 {
1559 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001560 const bool append = true;
1561 uint32_t num_matches = 0;
1562 if (name_is_regex)
1563 {
1564 RegularExpression function_name_regex (name);
1565 num_matches = module->FindFunctions (function_name_regex,
1566 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001567 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001568 append,
1569 sc_list);
1570 }
1571 else
1572 {
1573 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001574 num_matches = module->FindFunctions (function_name,
1575 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001576 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1577 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001578 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001579 append,
1580 sc_list);
1581 }
1582
1583 if (num_matches)
1584 {
1585 strm.Indent ();
1586 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1587 DumpFullpath (strm, &module->GetFileSpec(), 0);
1588 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001589 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001590 }
1591 return num_matches;
1592 }
1593 return 0;
1594}
1595
1596static uint32_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001597LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001598 Stream &strm,
1599 Module *module,
1600 const char *name_cstr,
1601 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001602{
1603 if (module && name_cstr && name_cstr[0])
1604 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001605 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001606 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001607 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001608 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001609 SymbolContext sc;
1610
1611 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001612 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001613
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001614 if (num_matches)
1615 {
1616 strm.Indent ();
1617 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1618 DumpFullpath (strm, &module->GetFileSpec(), 0);
1619 strm.PutCString(":\n");
1620 const uint32_t num_types = type_list.GetSize();
1621 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001622 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001623 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1624 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001625 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001626 // Resolve the clang type so that any forward references
1627 // to types that haven't yet been parsed will get parsed.
1628 type_sp->GetClangFullType ();
1629 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001630 // Print all typedef chains
1631 TypeSP typedef_type_sp (type_sp);
1632 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1633 while (typedefed_type_sp)
1634 {
1635 strm.EOL();
1636 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1637 typedefed_type_sp->GetClangFullType ();
1638 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1639 typedef_type_sp = typedefed_type_sp;
1640 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1641 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001642 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001643 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001644 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001645 }
1646 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001647 }
1648 return 0;
1649}
1650
1651static uint32_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001652LookupTypeHere (CommandInterpreter &interpreter,
1653 Stream &strm,
1654 const SymbolContext &sym_ctx,
1655 const char *name_cstr,
1656 bool name_is_regex)
1657{
1658 if (!sym_ctx.module_sp)
1659 return 0;
1660
1661 TypeList type_list;
1662 const uint32_t max_num_matches = UINT32_MAX;
1663 uint32_t num_matches = 1;
1664 bool name_is_fully_qualified = false;
1665
1666 ConstString name(name_cstr);
1667 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1668
1669 if (num_matches)
1670 {
1671 strm.Indent ();
1672 strm.PutCString("Best match found in ");
1673 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1674 strm.PutCString(":\n");
1675
1676 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1677 if (type_sp)
1678 {
1679 // Resolve the clang type so that any forward references
1680 // to types that haven't yet been parsed will get parsed.
1681 type_sp->GetClangFullType ();
1682 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1683 // Print all typedef chains
1684 TypeSP typedef_type_sp (type_sp);
1685 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1686 while (typedefed_type_sp)
1687 {
1688 strm.EOL();
1689 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1690 typedefed_type_sp->GetClangFullType ();
1691 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1692 typedef_type_sp = typedefed_type_sp;
1693 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1694 }
1695 }
1696 strm.EOL();
1697 }
1698 return num_matches;
1699}
1700
1701static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001702LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001703 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001704 Module *module,
1705 const FileSpec &file_spec,
1706 uint32_t line,
1707 bool check_inlines,
1708 bool verbose)
1709{
1710 if (module && file_spec)
1711 {
1712 SymbolContextList sc_list;
1713 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1714 eSymbolContextEverything, sc_list);
1715 if (num_matches > 0)
1716 {
1717 strm.Indent ();
1718 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1719 strm << file_spec;
1720 if (line > 0)
1721 strm.Printf (":%u", line);
1722 strm << " in ";
1723 DumpFullpath (strm, &module->GetFileSpec(), 0);
1724 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001725 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001726 return num_matches;
1727 }
1728 }
1729 return 0;
1730
1731}
1732
Greg Clayton91048ef2011-11-10 01:18:58 +00001733
1734static size_t
1735FindModulesByName (Target *target,
1736 const char *module_name,
1737 ModuleList &module_list,
1738 bool check_global_list)
1739{
1740// Dump specified images (by basename or fullpath)
1741 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001742 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001743
1744 const size_t initial_size = module_list.GetSize ();
1745
1746 size_t num_matches = 0;
1747
1748 if (target)
1749 {
Greg Clayton444fe992012-02-26 05:51:37 +00001750 num_matches = target->GetImages().FindModules (module_spec, module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001751
1752 // Not found in our module list for our target, check the main
1753 // shared module list in case it is a extra file used somewhere
1754 // else
1755 if (num_matches == 0)
Greg Clayton444fe992012-02-26 05:51:37 +00001756 {
1757 module_spec.GetArchitecture() = target->GetArchitecture();
1758 num_matches = ModuleList::FindSharedModules (module_spec, module_list);
1759 }
Greg Clayton91048ef2011-11-10 01:18:58 +00001760 }
1761 else
1762 {
Greg Clayton444fe992012-02-26 05:51:37 +00001763 num_matches = ModuleList::FindSharedModules (module_spec,module_list);
Greg Clayton91048ef2011-11-10 01:18:58 +00001764 }
1765
1766 if (check_global_list && num_matches == 0)
1767 {
1768 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001769 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001770 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1771 ModuleSP module_sp;
1772 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1773 {
1774 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1775
1776 if (module)
1777 {
Greg Clayton444fe992012-02-26 05:51:37 +00001778 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001779 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001780 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001781 module_list.AppendIfNeeded(module_sp);
1782 }
1783 }
1784 }
1785 }
1786 return module_list.GetSize () - initial_size;
1787}
1788
Greg Claytone1f50b92011-05-03 22:09:39 +00001789#pragma mark CommandObjectTargetModulesModuleAutoComplete
1790
1791//----------------------------------------------------------------------
1792// A base command object class that can auto complete with module file
1793// paths
1794//----------------------------------------------------------------------
1795
Jim Inghamda26bd22012-06-08 21:56:10 +00001796class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001797{
1798public:
1799
1800 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1801 const char *name,
1802 const char *help,
1803 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001804 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001805 {
1806 CommandArgumentEntry arg;
1807 CommandArgumentData file_arg;
1808
1809 // Define the first (and only) variant of this arg.
1810 file_arg.arg_type = eArgTypeFilename;
1811 file_arg.arg_repetition = eArgRepeatStar;
1812
1813 // There is only one variant this argument could be; put it into the argument entry.
1814 arg.push_back (file_arg);
1815
1816 // Push the data for the first argument into the m_arguments vector.
1817 m_arguments.push_back (arg);
1818 }
1819
1820 virtual
1821 ~CommandObjectTargetModulesModuleAutoComplete ()
1822 {
1823 }
1824
1825 virtual int
1826 HandleArgumentCompletion (Args &input,
1827 int &cursor_index,
1828 int &cursor_char_position,
1829 OptionElementVector &opt_element_vector,
1830 int match_start_point,
1831 int max_return_elements,
1832 bool &word_complete,
1833 StringList &matches)
1834 {
1835 // Arguments are the standard module completer.
1836 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1837 completion_str.erase (cursor_char_position);
1838
1839 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1840 CommandCompletions::eModuleCompletion,
1841 completion_str.c_str(),
1842 match_start_point,
1843 max_return_elements,
1844 NULL,
1845 word_complete,
1846 matches);
1847 return matches.GetSize();
1848 }
1849};
1850
1851#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1852
1853//----------------------------------------------------------------------
1854// A base command object class that can auto complete with module source
1855// file paths
1856//----------------------------------------------------------------------
1857
Jim Inghamda26bd22012-06-08 21:56:10 +00001858class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001859{
1860public:
1861
1862 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1863 const char *name,
1864 const char *help,
1865 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001866 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001867 {
1868 CommandArgumentEntry arg;
1869 CommandArgumentData source_file_arg;
1870
1871 // Define the first (and only) variant of this arg.
1872 source_file_arg.arg_type = eArgTypeSourceFile;
1873 source_file_arg.arg_repetition = eArgRepeatPlus;
1874
1875 // There is only one variant this argument could be; put it into the argument entry.
1876 arg.push_back (source_file_arg);
1877
1878 // Push the data for the first argument into the m_arguments vector.
1879 m_arguments.push_back (arg);
1880 }
1881
1882 virtual
1883 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1884 {
1885 }
1886
1887 virtual int
1888 HandleArgumentCompletion (Args &input,
1889 int &cursor_index,
1890 int &cursor_char_position,
1891 OptionElementVector &opt_element_vector,
1892 int match_start_point,
1893 int max_return_elements,
1894 bool &word_complete,
1895 StringList &matches)
1896 {
1897 // Arguments are the standard source file completer.
1898 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1899 completion_str.erase (cursor_char_position);
1900
1901 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1902 CommandCompletions::eSourceFileCompletion,
1903 completion_str.c_str(),
1904 match_start_point,
1905 max_return_elements,
1906 NULL,
1907 word_complete,
1908 matches);
1909 return matches.GetSize();
1910 }
1911};
1912
1913
1914#pragma mark CommandObjectTargetModulesDumpSymtab
1915
1916
1917class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1918{
1919public:
1920 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1921 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1922 "target modules dump symtab",
1923 "Dump the symbol table from one or more target modules.",
1924 NULL),
1925 m_options (interpreter)
1926 {
1927 }
1928
1929 virtual
1930 ~CommandObjectTargetModulesDumpSymtab ()
1931 {
1932 }
1933
Jim Inghamda26bd22012-06-08 21:56:10 +00001934 virtual Options *
1935 GetOptions ()
1936 {
1937 return &m_options;
1938 }
1939
1940 class CommandOptions : public Options
1941 {
1942 public:
1943
1944 CommandOptions (CommandInterpreter &interpreter) :
1945 Options(interpreter),
1946 m_sort_order (eSortOrderNone)
1947 {
1948 }
1949
1950 virtual
1951 ~CommandOptions ()
1952 {
1953 }
1954
1955 virtual Error
1956 SetOptionValue (uint32_t option_idx, const char *option_arg)
1957 {
1958 Error error;
1959 char short_option = (char) m_getopt_table[option_idx].val;
1960
1961 switch (short_option)
1962 {
1963 case 's':
1964 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1965 g_option_table[option_idx].enum_values,
1966 eSortOrderNone,
1967 error);
1968 break;
1969
1970 default:
1971 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1972 break;
1973
1974 }
1975 return error;
1976 }
1977
1978 void
1979 OptionParsingStarting ()
1980 {
1981 m_sort_order = eSortOrderNone;
1982 }
1983
1984 const OptionDefinition*
1985 GetDefinitions ()
1986 {
1987 return g_option_table;
1988 }
1989
1990 // Options table: Required for subclasses of Options.
1991 static OptionDefinition g_option_table[];
1992
1993 SortOrder m_sort_order;
1994 };
1995
1996protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00001997 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001998 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00001999 CommandReturnObject &result)
2000 {
2001 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2002 if (target == NULL)
2003 {
2004 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2005 result.SetStatus (eReturnStatusFailed);
2006 return false;
2007 }
2008 else
2009 {
2010 uint32_t num_dumped = 0;
2011
2012 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2013 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2014 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2015
2016 if (command.GetArgumentCount() == 0)
2017 {
2018 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002019 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002020 const uint32_t num_modules = target->GetImages().GetSize();
2021 if (num_modules > 0)
2022 {
2023 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2024 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2025 {
2026 if (num_dumped > 0)
2027 {
2028 result.GetOutputStream().EOL();
2029 result.GetOutputStream().EOL();
2030 }
2031 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002032 DumpModuleSymtab (m_interpreter,
2033 result.GetOutputStream(),
2034 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2035 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002036 }
2037 }
2038 else
2039 {
2040 result.AppendError ("the target has no associated executable images");
2041 result.SetStatus (eReturnStatusFailed);
2042 return false;
2043 }
2044 }
2045 else
2046 {
2047 // Dump specified images (by basename or fullpath)
2048 const char *arg_cstr;
2049 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2050 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002051 ModuleList module_list;
2052 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2053 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002054 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002055 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002056 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002057 Module *module = module_list.GetModulePointerAtIndex(i);
2058 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002059 {
2060 if (num_dumped > 0)
2061 {
2062 result.GetOutputStream().EOL();
2063 result.GetOutputStream().EOL();
2064 }
2065 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002066 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002067 }
2068 }
2069 }
2070 else
2071 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2072 }
2073 }
2074
2075 if (num_dumped > 0)
2076 result.SetStatus (eReturnStatusSuccessFinishResult);
2077 else
2078 {
2079 result.AppendError ("no matching executable images found");
2080 result.SetStatus (eReturnStatusFailed);
2081 }
2082 }
2083 return result.Succeeded();
2084 }
2085
Greg Claytone1f50b92011-05-03 22:09:39 +00002086
2087 CommandOptions m_options;
2088};
2089
2090static OptionEnumValueElement
2091g_sort_option_enumeration[4] =
2092{
2093 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2094 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2095 { eSortOrderByName, "name", "Sort output by symbol name."},
2096 { 0, NULL, NULL }
2097};
2098
2099
2100OptionDefinition
2101CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2102{
2103 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2104 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2105};
2106
2107#pragma mark CommandObjectTargetModulesDumpSections
2108
2109//----------------------------------------------------------------------
2110// Image section dumping command
2111//----------------------------------------------------------------------
2112
2113class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2114{
2115public:
2116 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2117 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2118 "target modules dump sections",
2119 "Dump the sections from one or more target modules.",
2120 //"target modules dump sections [<file1> ...]")
2121 NULL)
2122 {
2123 }
2124
2125 virtual
2126 ~CommandObjectTargetModulesDumpSections ()
2127 {
2128 }
2129
Jim Inghamda26bd22012-06-08 21:56:10 +00002130protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002131 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002132 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002133 CommandReturnObject &result)
2134 {
2135 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2136 if (target == NULL)
2137 {
2138 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2139 result.SetStatus (eReturnStatusFailed);
2140 return false;
2141 }
2142 else
2143 {
2144 uint32_t num_dumped = 0;
2145
2146 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2147 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2148 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2149
2150 if (command.GetArgumentCount() == 0)
2151 {
2152 // Dump all sections for all modules images
2153 const uint32_t num_modules = target->GetImages().GetSize();
2154 if (num_modules > 0)
2155 {
2156 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2157 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2158 {
2159 num_dumped++;
2160 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2161 }
2162 }
2163 else
2164 {
2165 result.AppendError ("the target has no associated executable images");
2166 result.SetStatus (eReturnStatusFailed);
2167 return false;
2168 }
2169 }
2170 else
2171 {
2172 // Dump specified images (by basename or fullpath)
2173 const char *arg_cstr;
2174 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2175 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002176 ModuleList module_list;
2177 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2178 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002179 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002180 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002181 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002182 Module *module = module_list.GetModulePointerAtIndex(i);
2183 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002184 {
2185 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002186 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002187 }
2188 }
2189 }
2190 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002191 {
2192 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002193 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002194
Greg Claytone1f50b92011-05-03 22:09:39 +00002195 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002196 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002197 }
2198 }
2199
2200 if (num_dumped > 0)
2201 result.SetStatus (eReturnStatusSuccessFinishResult);
2202 else
2203 {
2204 result.AppendError ("no matching executable images found");
2205 result.SetStatus (eReturnStatusFailed);
2206 }
2207 }
2208 return result.Succeeded();
2209 }
2210};
2211
2212
2213#pragma mark CommandObjectTargetModulesDumpSymfile
2214
2215//----------------------------------------------------------------------
2216// Image debug symbol dumping command
2217//----------------------------------------------------------------------
2218
2219class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2220{
2221public:
2222 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2223 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2224 "target modules dump symfile",
2225 "Dump the debug symbol file for one or more target modules.",
2226 //"target modules dump symfile [<file1> ...]")
2227 NULL)
2228 {
2229 }
2230
2231 virtual
2232 ~CommandObjectTargetModulesDumpSymfile ()
2233 {
2234 }
2235
Jim Inghamda26bd22012-06-08 21:56:10 +00002236protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002237 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002238 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002239 CommandReturnObject &result)
2240 {
2241 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2242 if (target == NULL)
2243 {
2244 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2245 result.SetStatus (eReturnStatusFailed);
2246 return false;
2247 }
2248 else
2249 {
2250 uint32_t num_dumped = 0;
2251
2252 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2253 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2254 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2255
2256 if (command.GetArgumentCount() == 0)
2257 {
2258 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002259 ModuleList &target_modules = target->GetImages();
2260 Mutex::Locker modules_locker (target_modules.GetMutex());
2261 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002262 if (num_modules > 0)
2263 {
2264 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2265 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2266 {
Jim Ingham93367902012-05-30 02:19:25 +00002267 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002268 num_dumped++;
2269 }
2270 }
2271 else
2272 {
2273 result.AppendError ("the target has no associated executable images");
2274 result.SetStatus (eReturnStatusFailed);
2275 return false;
2276 }
2277 }
2278 else
2279 {
2280 // Dump specified images (by basename or fullpath)
2281 const char *arg_cstr;
2282 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2283 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002284 ModuleList module_list;
2285 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2286 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002287 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002288 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002289 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002290 Module *module = module_list.GetModulePointerAtIndex(i);
2291 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002292 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002293 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002294 num_dumped++;
2295 }
2296 }
2297 }
2298 else
2299 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2300 }
2301 }
2302
2303 if (num_dumped > 0)
2304 result.SetStatus (eReturnStatusSuccessFinishResult);
2305 else
2306 {
2307 result.AppendError ("no matching executable images found");
2308 result.SetStatus (eReturnStatusFailed);
2309 }
2310 }
2311 return result.Succeeded();
2312 }
2313};
2314
2315
2316#pragma mark CommandObjectTargetModulesDumpLineTable
2317
2318//----------------------------------------------------------------------
2319// Image debug line table dumping command
2320//----------------------------------------------------------------------
2321
2322class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2323{
2324public:
2325 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2326 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2327 "target modules dump line-table",
2328 "Dump the debug symbol file for one or more target modules.",
2329 NULL)
2330 {
2331 }
2332
2333 virtual
2334 ~CommandObjectTargetModulesDumpLineTable ()
2335 {
2336 }
2337
Jim Inghamda26bd22012-06-08 21:56:10 +00002338protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002339 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002340 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002341 CommandReturnObject &result)
2342 {
2343 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2344 if (target == NULL)
2345 {
2346 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2347 result.SetStatus (eReturnStatusFailed);
2348 return false;
2349 }
2350 else
2351 {
2352 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2353 uint32_t total_num_dumped = 0;
2354
2355 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2356 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2357 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2358
2359 if (command.GetArgumentCount() == 0)
2360 {
2361 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2362 result.SetStatus (eReturnStatusFailed);
2363 }
2364 else
2365 {
2366 // Dump specified images (by basename or fullpath)
2367 const char *arg_cstr;
2368 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2369 {
2370 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002371
2372 ModuleList &target_modules = target->GetImages();
2373 Mutex::Locker modules_locker(target_modules.GetMutex());
2374 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002375 if (num_modules > 0)
2376 {
2377 uint32_t num_dumped = 0;
2378 for (uint32_t i = 0; i<num_modules; ++i)
2379 {
2380 if (DumpCompileUnitLineTable (m_interpreter,
2381 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002382 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002383 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002384 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002385 num_dumped++;
2386 }
2387 if (num_dumped == 0)
2388 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2389 else
2390 total_num_dumped += num_dumped;
2391 }
2392 }
2393 }
2394
2395 if (total_num_dumped > 0)
2396 result.SetStatus (eReturnStatusSuccessFinishResult);
2397 else
2398 {
2399 result.AppendError ("no source filenames matched any command arguments");
2400 result.SetStatus (eReturnStatusFailed);
2401 }
2402 }
2403 return result.Succeeded();
2404 }
2405};
2406
2407
2408#pragma mark CommandObjectTargetModulesDump
2409
2410//----------------------------------------------------------------------
2411// Dump multi-word command for target modules
2412//----------------------------------------------------------------------
2413
2414class CommandObjectTargetModulesDump : public CommandObjectMultiword
2415{
2416public:
2417
2418 //------------------------------------------------------------------
2419 // Constructors and Destructors
2420 //------------------------------------------------------------------
2421 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2422 CommandObjectMultiword (interpreter,
2423 "target modules dump",
2424 "A set of commands for dumping information about one or more target modules.",
2425 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2426 {
2427 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2428 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2429 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2430 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2431 }
2432
2433 virtual
2434 ~CommandObjectTargetModulesDump()
2435 {
2436 }
2437};
2438
Jim Inghamda26bd22012-06-08 21:56:10 +00002439class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002440{
2441public:
2442 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002443 CommandObjectParsed (interpreter,
2444 "target modules add",
2445 "Add a new module to the current target's modules.",
2446 "target modules add [<module>]")
Greg Claytone1f50b92011-05-03 22:09:39 +00002447 {
2448 }
2449
2450 virtual
2451 ~CommandObjectTargetModulesAdd ()
2452 {
2453 }
2454
Jim Inghamda26bd22012-06-08 21:56:10 +00002455 int
2456 HandleArgumentCompletion (Args &input,
2457 int &cursor_index,
2458 int &cursor_char_position,
2459 OptionElementVector &opt_element_vector,
2460 int match_start_point,
2461 int max_return_elements,
2462 bool &word_complete,
2463 StringList &matches)
2464 {
2465 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2466 completion_str.erase (cursor_char_position);
2467
2468 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2469 CommandCompletions::eDiskFileCompletion,
2470 completion_str.c_str(),
2471 match_start_point,
2472 max_return_elements,
2473 NULL,
2474 word_complete,
2475 matches);
2476 return matches.GetSize();
2477 }
2478
2479protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002480 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002481 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002482 CommandReturnObject &result)
2483 {
2484 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2485 if (target == NULL)
2486 {
2487 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2488 result.SetStatus (eReturnStatusFailed);
2489 return false;
2490 }
2491 else
2492 {
2493 const size_t argc = args.GetArgumentCount();
2494 if (argc == 0)
2495 {
2496 result.AppendError ("one or more executable image paths must be specified");
2497 result.SetStatus (eReturnStatusFailed);
2498 return false;
2499 }
2500 else
2501 {
2502 for (size_t i=0; i<argc; ++i)
2503 {
2504 const char *path = args.GetArgumentAtIndex(i);
2505 if (path)
2506 {
2507 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002508 if (file_spec.Exists())
2509 {
Greg Clayton444fe992012-02-26 05:51:37 +00002510 ModuleSpec module_spec (file_spec);
2511 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002512 if (!module_sp)
2513 {
2514 result.AppendError ("one or more executable image paths must be specified");
2515 result.SetStatus (eReturnStatusFailed);
2516 return false;
2517 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002518 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002519 }
2520 else
2521 {
2522 char resolved_path[PATH_MAX];
2523 result.SetStatus (eReturnStatusFailed);
2524 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2525 {
2526 if (strcmp (resolved_path, path) != 0)
2527 {
2528 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2529 break;
2530 }
2531 }
2532 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2533 break;
2534 }
2535 }
2536 }
2537 }
2538 }
2539 return result.Succeeded();
2540 }
2541
Greg Claytone1f50b92011-05-03 22:09:39 +00002542};
2543
2544class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2545{
2546public:
2547 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2548 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2549 "target modules load",
2550 "Set the load addresses for one or more sections in a target module.",
2551 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2552 m_option_group (interpreter),
2553 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2554 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)
2555 {
2556 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2557 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2558 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2559 m_option_group.Finalize();
2560 }
2561
2562 virtual
2563 ~CommandObjectTargetModulesLoad ()
2564 {
2565 }
2566
Jim Inghamda26bd22012-06-08 21:56:10 +00002567 virtual Options *
2568 GetOptions ()
2569 {
2570 return &m_option_group;
2571 }
2572
2573protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002574 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002575 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002576 CommandReturnObject &result)
2577 {
2578 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2579 if (target == NULL)
2580 {
2581 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2582 result.SetStatus (eReturnStatusFailed);
2583 return false;
2584 }
2585 else
2586 {
2587 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002588 ModuleSpec module_spec;
2589 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002590 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002591 {
2592 search_using_module_spec = true;
2593 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2594 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002595
2596 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002597 {
2598 search_using_module_spec = true;
2599 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2600 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002601
Greg Clayton444fe992012-02-26 05:51:37 +00002602 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002603 {
2604
2605 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002606 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002607
2608 char path[PATH_MAX];
2609 if (num_matches == 1)
2610 {
2611 Module *module = matching_modules.GetModulePointerAtIndex(0);
2612 if (module)
2613 {
2614 ObjectFile *objfile = module->GetObjectFile();
2615 if (objfile)
2616 {
2617 SectionList *section_list = objfile->GetSectionList();
2618 if (section_list)
2619 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002620 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002621 if (argc == 0)
2622 {
2623 if (m_slide_option.GetOptionValue().OptionWasSet())
2624 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002625 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2626 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002627 }
2628 else
2629 {
2630 result.AppendError ("one or more section name + load address pair must be specified");
2631 result.SetStatus (eReturnStatusFailed);
2632 return false;
2633 }
2634 }
2635 else
2636 {
2637 if (m_slide_option.GetOptionValue().OptionWasSet())
2638 {
2639 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2640 result.SetStatus (eReturnStatusFailed);
2641 return false;
2642 }
2643
2644 for (size_t i=0; i<argc; i += 2)
2645 {
2646 const char *sect_name = args.GetArgumentAtIndex(i);
2647 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2648 if (sect_name && load_addr_cstr)
2649 {
2650 ConstString const_sect_name(sect_name);
2651 bool success = false;
2652 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2653 if (success)
2654 {
2655 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2656 if (section_sp)
2657 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002658 if (section_sp->IsThreadSpecific())
2659 {
2660 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2661 result.SetStatus (eReturnStatusFailed);
2662 break;
2663 }
2664 else
2665 {
Greg Clayton545762f2012-07-07 01:24:12 +00002666 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002667 changed = true;
2668 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2669 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002670 }
2671 else
2672 {
2673 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2674 result.SetStatus (eReturnStatusFailed);
2675 break;
2676 }
2677 }
2678 else
2679 {
2680 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2681 result.SetStatus (eReturnStatusFailed);
2682 break;
2683 }
2684 }
2685 else
2686 {
2687 if (sect_name)
2688 result.AppendError ("section names must be followed by a load address.\n");
2689 else
2690 result.AppendError ("one or more section name + load address pair must be specified.\n");
2691 result.SetStatus (eReturnStatusFailed);
2692 break;
2693 }
2694 }
2695 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002696
2697 if (changed)
2698 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002699 }
2700 else
2701 {
2702 module->GetFileSpec().GetPath (path, sizeof(path));
2703 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2704 result.SetStatus (eReturnStatusFailed);
2705 }
2706 }
2707 else
2708 {
2709 module->GetFileSpec().GetPath (path, sizeof(path));
2710 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2711 result.SetStatus (eReturnStatusFailed);
2712 }
2713 }
2714 else
2715 {
2716 module->GetFileSpec().GetPath (path, sizeof(path));
2717 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2718 result.SetStatus (eReturnStatusFailed);
2719 }
2720 }
2721 else
2722 {
2723 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002724
2725 if (module_spec.GetFileSpec())
2726 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002727 else
2728 path[0] = '\0';
2729
Greg Clayton444fe992012-02-26 05:51:37 +00002730 if (module_spec.GetUUIDPtr())
2731 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002732 else
2733 uuid_cstr[0] = '\0';
2734 if (num_matches > 1)
2735 {
2736 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2737 path[0] ? " file=" : "",
2738 path,
2739 uuid_cstr[0] ? " uuid=" : "",
2740 uuid_cstr);
2741 for (size_t i=0; i<num_matches; ++i)
2742 {
2743 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2744 result.AppendMessageWithFormat("%s\n", path);
2745 }
2746 }
2747 else
2748 {
2749 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2750 path[0] ? " file=" : "",
2751 path,
2752 uuid_cstr[0] ? " uuid=" : "",
2753 uuid_cstr);
2754 }
2755 result.SetStatus (eReturnStatusFailed);
2756 }
2757 }
2758 else
2759 {
2760 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2761 result.SetStatus (eReturnStatusFailed);
2762 return false;
2763 }
2764 }
2765 return result.Succeeded();
2766 }
2767
Greg Claytone1f50b92011-05-03 22:09:39 +00002768 OptionGroupOptions m_option_group;
2769 OptionGroupUUID m_uuid_option_group;
2770 OptionGroupFile m_file_option;
2771 OptionGroupUInt64 m_slide_option;
2772};
2773
2774//----------------------------------------------------------------------
2775// List images with associated information
2776//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002777class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002778{
2779public:
2780
2781 class CommandOptions : public Options
2782 {
2783 public:
2784
2785 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002786 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002787 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002788 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002789 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002790 {
2791 }
2792
2793 virtual
2794 ~CommandOptions ()
2795 {
2796 }
2797
2798 virtual Error
2799 SetOptionValue (uint32_t option_idx, const char *option_arg)
2800 {
2801 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002802 if (short_option == 'g')
2803 {
2804 m_use_global_module_list = true;
2805 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002806 else if (short_option == 'a')
2807 {
2808 bool success;
2809 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2810 if (!success)
2811 {
2812 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002813 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002814 }
2815 }
Greg Clayton899025f2011-08-09 00:01:09 +00002816 else
2817 {
2818 uint32_t width = 0;
2819 if (option_arg)
2820 width = strtoul (option_arg, NULL, 0);
2821 m_format_array.push_back(std::make_pair(short_option, width));
2822 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002823 Error error;
2824 return error;
2825 }
2826
2827 void
2828 OptionParsingStarting ()
2829 {
2830 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002831 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002832 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002833 }
2834
2835 const OptionDefinition*
2836 GetDefinitions ()
2837 {
2838 return g_option_table;
2839 }
2840
2841 // Options table: Required for subclasses of Options.
2842
2843 static OptionDefinition g_option_table[];
2844
2845 // Instance variables to hold the values for command options.
2846 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2847 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002848 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002849 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002850 };
2851
2852 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002853 CommandObjectParsed (interpreter,
2854 "target modules list",
2855 "List current executable and dependent shared library images.",
2856 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002857 m_options (interpreter)
2858 {
2859 }
2860
2861 virtual
2862 ~CommandObjectTargetModulesList ()
2863 {
2864 }
2865
2866 virtual
2867 Options *
2868 GetOptions ()
2869 {
2870 return &m_options;
2871 }
2872
Jim Inghamda26bd22012-06-08 21:56:10 +00002873protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002874 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002875 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002876 CommandReturnObject &result)
2877 {
2878 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002879 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00002880 // Define a local module list here to ensure it lives longer than any "locker"
2881 // object which might lock its contents below (through the "module_list_ptr"
2882 // variable).
2883 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00002884 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002885 {
2886 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2887 result.SetStatus (eReturnStatusFailed);
2888 return false;
2889 }
2890 else
2891 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002892 if (target)
2893 {
2894 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2895 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2896 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2897 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002898 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002899 Stream &strm = result.GetOutputStream();
2900
2901 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2902 {
2903 if (target)
2904 {
2905 Address module_address;
2906 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2907 {
Greg Clayton3508c382012-02-24 01:59:29 +00002908 ModuleSP module_sp (module_address.GetModule());
2909 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002910 {
Greg Clayton3508c382012-02-24 01:59:29 +00002911 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002912 result.SetStatus (eReturnStatusSuccessFinishResult);
2913 }
2914 else
2915 {
2916 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2917 result.SetStatus (eReturnStatusFailed);
2918 }
2919 }
2920 else
2921 {
2922 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2923 result.SetStatus (eReturnStatusFailed);
2924 }
2925 }
2926 else
2927 {
2928 result.AppendError ("Can only look up modules by address with a valid target.");
2929 result.SetStatus (eReturnStatusFailed);
2930 }
2931 return result.Succeeded();
2932 }
2933
Jim Ingham93367902012-05-30 02:19:25 +00002934 uint32_t num_modules = 0;
2935 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
2936 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
2937 // the global module list directly.
Greg Clayton2ad894b2012-05-15 18:43:44 +00002938 ModuleList *module_list_ptr = NULL;
2939 const size_t argc = command.GetArgumentCount();
2940 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00002941 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002942 if (use_global_module_list)
2943 {
2944 locker.Lock (Module::GetAllocationModuleCollectionMutex());
2945 num_modules = Module::GetNumberAllocatedModules();
2946 }
2947 else
2948 {
2949 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00002950 }
Greg Clayton899025f2011-08-09 00:01:09 +00002951 }
2952 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002953 {
2954 for (size_t i=0; i<argc; ++i)
2955 {
2956 // Dump specified images (by basename or fullpath)
2957 const char *arg_cstr = command.GetArgumentAtIndex(i);
2958 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2959 if (num_matches == 0)
2960 {
2961 if (argc == 1)
2962 {
2963 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
2964 result.SetStatus (eReturnStatusFailed);
2965 return false;
2966 }
2967 }
2968 }
2969
Greg Clayton2ad894b2012-05-15 18:43:44 +00002970 module_list_ptr = &module_list;
2971 }
Jim Ingham93367902012-05-30 02:19:25 +00002972
2973 if (module_list_ptr != NULL)
2974 {
2975 locker.Lock(module_list_ptr->GetMutex());
2976 num_modules = module_list_ptr->GetSize();
2977 }
Greg Clayton899025f2011-08-09 00:01:09 +00002978
Greg Claytone1f50b92011-05-03 22:09:39 +00002979 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002980 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002981 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2982 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002983 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002984 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00002985 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00002986 {
Jim Ingham93367902012-05-30 02:19:25 +00002987 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00002988 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002989 }
2990 else
2991 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002992 module = Module::GetAllocatedModuleAtIndex(image_idx);
2993 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00002994 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002995
Greg Claytonb5a8f142012-02-05 02:38:54 +00002996 int indent = strm.Printf("[%3u] ", image_idx);
2997 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00002998
Greg Claytone1f50b92011-05-03 22:09:39 +00002999 }
3000 result.SetStatus (eReturnStatusSuccessFinishResult);
3001 }
3002 else
3003 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003004 if (argc)
3005 {
3006 if (use_global_module_list)
3007 result.AppendError ("the global module list has no matching modules");
3008 else
3009 result.AppendError ("the target has no matching modules");
3010 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003011 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003012 {
3013 if (use_global_module_list)
3014 result.AppendError ("the global module list is empty");
3015 else
3016 result.AppendError ("the target has no associated executable images");
3017 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003018 result.SetStatus (eReturnStatusFailed);
3019 return false;
3020 }
3021 }
3022 return result.Succeeded();
3023 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003024
3025 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003026 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003027 {
3028
3029 bool dump_object_name = false;
3030 if (m_options.m_format_array.empty())
3031 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003032 m_options.m_format_array.push_back(std::make_pair('u', 0));
3033 m_options.m_format_array.push_back(std::make_pair('h', 0));
3034 m_options.m_format_array.push_back(std::make_pair('f', 0));
3035 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003036 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003037 const size_t num_entries = m_options.m_format_array.size();
3038 bool print_space = false;
3039 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003040 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003041 if (print_space)
3042 strm.PutChar(' ');
3043 print_space = true;
3044 const char format_char = m_options.m_format_array[i].first;
3045 uint32_t width = m_options.m_format_array[i].second;
3046 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003047 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003048 case 'A':
3049 DumpModuleArchitecture (strm, module, false, width);
3050 break;
3051
3052 case 't':
3053 DumpModuleArchitecture (strm, module, true, width);
3054 break;
3055
3056 case 'f':
3057 DumpFullpath (strm, &module->GetFileSpec(), width);
3058 dump_object_name = true;
3059 break;
3060
3061 case 'd':
3062 DumpDirectory (strm, &module->GetFileSpec(), width);
3063 break;
3064
3065 case 'b':
3066 DumpBasename (strm, &module->GetFileSpec(), width);
3067 dump_object_name = true;
3068 break;
3069
3070 case 'h':
3071 case 'o':
3072 // Image header address
3073 {
3074 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003075
Greg Claytonb5a8f142012-02-05 02:38:54 +00003076 ObjectFile *objfile = module->GetObjectFile ();
3077 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003078 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003079 Address header_addr(objfile->GetHeaderAddress());
3080 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003081 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003082 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003083 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003084 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3085 if (header_load_addr == LLDB_INVALID_ADDRESS)
3086 {
3087 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3088 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003089 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003090 {
3091 if (format_char == 'o')
3092 {
3093 // Show the offset of slide for the image
3094 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
3095 }
3096 else
3097 {
3098 // Show the load address of the image
3099 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
3100 }
3101 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003102 break;
3103 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003104 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3105 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3106 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003107 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003108 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003109 strm.Printf ("%*s", addr_nibble_width + 2, "");
3110 }
3111 break;
3112 case 'r':
3113 {
3114 uint32_t ref_count = 0;
3115 ModuleSP module_sp (module->shared_from_this());
3116 if (module_sp)
3117 {
3118 // Take one away to make sure we don't count our local "module_sp"
3119 ref_count = module_sp.use_count() - 1;
3120 }
3121 if (width)
3122 strm.Printf("{%*u}", width, ref_count);
3123 else
3124 strm.Printf("{%u}", ref_count);
3125 }
3126 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003127
Greg Claytonb5a8f142012-02-05 02:38:54 +00003128 case 's':
3129 case 'S':
3130 {
3131 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3132 if (symbol_vendor)
3133 {
3134 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3135 if (symbol_file)
3136 {
3137 if (format_char == 'S')
3138 {
3139 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3140 // Dump symbol file only if different from module file
3141 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3142 {
3143 print_space = false;
3144 break;
3145 }
3146 // Add a newline and indent past the index
3147 strm.Printf ("\n%*s", indent, "");
3148 }
3149 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3150 dump_object_name = true;
3151 break;
3152 }
3153 }
3154 strm.Printf("%.*s", width, "<NONE>");
3155 }
3156 break;
3157
3158 case 'm':
3159 module->GetModificationTime().Dump(&strm, width);
3160 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003161
Greg Claytonb5a8f142012-02-05 02:38:54 +00003162 case 'p':
3163 strm.Printf("%p", module);
3164 break;
3165
3166 case 'u':
3167 DumpModuleUUID(strm, module);
3168 break;
3169
3170 default:
3171 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003172 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003173
3174 }
3175 if (dump_object_name)
3176 {
3177 const char *object_name = module->GetObjectName().GetCString();
3178 if (object_name)
3179 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003180 }
3181 strm.EOL();
3182 }
3183
Greg Claytone1f50b92011-05-03 22:09:39 +00003184 CommandOptions m_options;
3185};
3186
3187OptionDefinition
3188CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3189{
Jim Ingham6bdea822011-10-24 18:36:33 +00003190 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3191 { 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 +00003192 { 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 +00003193 { 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."},
3194 { 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 +00003195 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3196 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3197 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3198 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3199 { 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 +00003200 { 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 +00003201 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3202 { 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."},
3203 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003204 { 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 +00003205 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3206};
3207
3208
3209
3210//----------------------------------------------------------------------
3211// Lookup information in images
3212//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003213class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003214{
3215public:
3216
3217 enum
3218 {
3219 eLookupTypeInvalid = -1,
3220 eLookupTypeAddress = 0,
3221 eLookupTypeSymbol,
3222 eLookupTypeFileLine, // Line is optional
3223 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003224 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003225 eLookupTypeType,
3226 kNumLookupTypes
3227 };
3228
3229 class CommandOptions : public Options
3230 {
3231 public:
3232
3233 CommandOptions (CommandInterpreter &interpreter) :
3234 Options(interpreter)
3235 {
3236 OptionParsingStarting();
3237 }
3238
3239 virtual
3240 ~CommandOptions ()
3241 {
3242 }
3243
3244 virtual Error
3245 SetOptionValue (uint32_t option_idx, const char *option_arg)
3246 {
3247 Error error;
3248
3249 char short_option = (char) m_getopt_table[option_idx].val;
3250
3251 switch (short_option)
3252 {
3253 case 'a':
3254 m_type = eLookupTypeAddress;
3255 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3256 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003257 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003258 break;
3259
3260 case 'o':
3261 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3262 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003263 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003264 break;
3265
3266 case 's':
3267 m_str = option_arg;
3268 m_type = eLookupTypeSymbol;
3269 break;
3270
3271 case 'f':
3272 m_file.SetFile (option_arg, false);
3273 m_type = eLookupTypeFileLine;
3274 break;
3275
3276 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003277 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003278 break;
3279
3280 case 'l':
3281 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3282 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003283 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003284 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003285 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003286 m_type = eLookupTypeFileLine;
3287 break;
3288
Greg Clayton2ad894b2012-05-15 18:43:44 +00003289 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003290 m_str = option_arg;
3291 m_type = eLookupTypeFunction;
3292 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003293
3294 case 'n':
3295 m_str = option_arg;
3296 m_type = eLookupTypeFunctionOrSymbol;
3297 break;
3298
Greg Claytone1f50b92011-05-03 22:09:39 +00003299 case 't':
3300 m_str = option_arg;
3301 m_type = eLookupTypeType;
3302 break;
3303
3304 case 'v':
3305 m_verbose = 1;
3306 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003307
3308 case 'A':
3309 m_print_all = true;
3310 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003311
3312 case 'r':
3313 m_use_regex = true;
3314 break;
3315 }
3316
3317 return error;
3318 }
3319
3320 void
3321 OptionParsingStarting ()
3322 {
3323 m_type = eLookupTypeInvalid;
3324 m_str.clear();
3325 m_file.Clear();
3326 m_addr = LLDB_INVALID_ADDRESS;
3327 m_offset = 0;
3328 m_line_number = 0;
3329 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003330 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003331 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003332 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003333 }
3334
3335 const OptionDefinition*
3336 GetDefinitions ()
3337 {
3338 return g_option_table;
3339 }
3340
3341 // Options table: Required for subclasses of Options.
3342
3343 static OptionDefinition g_option_table[];
3344 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3345 std::string m_str; // Holds name lookup
3346 FileSpec m_file; // Files for file lookups
3347 lldb::addr_t m_addr; // Holds the address to lookup
3348 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3349 uint32_t m_line_number; // Line number for file+line lookups
3350 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003351 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003352 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003353 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003354
3355 };
3356
3357 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003358 CommandObjectParsed (interpreter,
3359 "target modules lookup",
3360 "Look up information within executable and dependent shared library images.",
3361 NULL),
3362 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003363 {
3364 CommandArgumentEntry arg;
3365 CommandArgumentData file_arg;
3366
3367 // Define the first (and only) variant of this arg.
3368 file_arg.arg_type = eArgTypeFilename;
3369 file_arg.arg_repetition = eArgRepeatStar;
3370
3371 // There is only one variant this argument could be; put it into the argument entry.
3372 arg.push_back (file_arg);
3373
3374 // Push the data for the first argument into the m_arguments vector.
3375 m_arguments.push_back (arg);
3376 }
3377
3378 virtual
3379 ~CommandObjectTargetModulesLookup ()
3380 {
3381 }
3382
3383 virtual Options *
3384 GetOptions ()
3385 {
3386 return &m_options;
3387 }
3388
Sean Callanan56d31ec2012-06-06 20:49:55 +00003389 bool
3390 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3391 {
3392 switch (m_options.m_type)
3393 {
3394 case eLookupTypeAddress:
3395 case eLookupTypeFileLine:
3396 case eLookupTypeFunction:
3397 case eLookupTypeFunctionOrSymbol:
3398 case eLookupTypeSymbol:
3399 default:
3400 return false;
3401 case eLookupTypeType:
3402 break;
3403 }
3404
3405 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3406
3407 StackFrameSP frame = exe_ctx.GetFrameSP();
3408
3409 if (!frame)
3410 return false;
3411
3412 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3413
3414 if (!sym_ctx.module_sp)
3415 return false;
3416
3417 switch (m_options.m_type)
3418 {
3419 default:
3420 return false;
3421 case eLookupTypeType:
3422 if (!m_options.m_str.empty())
3423 {
3424 if (LookupTypeHere (m_interpreter,
3425 result.GetOutputStream(),
3426 sym_ctx,
3427 m_options.m_str.c_str(),
3428 m_options.m_use_regex))
3429 {
3430 result.SetStatus(eReturnStatusSuccessFinishResult);
3431 return true;
3432 }
3433 }
3434 break;
3435 }
3436
3437 return true;
3438 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003439
3440 bool
3441 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3442 {
3443 switch (m_options.m_type)
3444 {
3445 case eLookupTypeAddress:
3446 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3447 {
3448 if (LookupAddressInModule (m_interpreter,
3449 result.GetOutputStream(),
3450 module,
3451 eSymbolContextEverything,
3452 m_options.m_addr,
3453 m_options.m_offset,
3454 m_options.m_verbose))
3455 {
3456 result.SetStatus(eReturnStatusSuccessFinishResult);
3457 return true;
3458 }
3459 }
3460 break;
3461
3462 case eLookupTypeSymbol:
3463 if (!m_options.m_str.empty())
3464 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003465 if (LookupSymbolInModule (m_interpreter,
3466 result.GetOutputStream(),
3467 module,
3468 m_options.m_str.c_str(),
3469 m_options.m_use_regex,
3470 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003471 {
3472 result.SetStatus(eReturnStatusSuccessFinishResult);
3473 return true;
3474 }
3475 }
3476 break;
3477
3478 case eLookupTypeFileLine:
3479 if (m_options.m_file)
3480 {
3481
3482 if (LookupFileAndLineInModule (m_interpreter,
3483 result.GetOutputStream(),
3484 module,
3485 m_options.m_file,
3486 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003487 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003488 m_options.m_verbose))
3489 {
3490 result.SetStatus(eReturnStatusSuccessFinishResult);
3491 return true;
3492 }
3493 }
3494 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003495
3496 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003497 case eLookupTypeFunction:
3498 if (!m_options.m_str.empty())
3499 {
3500 if (LookupFunctionInModule (m_interpreter,
3501 result.GetOutputStream(),
3502 module,
3503 m_options.m_str.c_str(),
3504 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003505 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003506 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003507 m_options.m_verbose))
3508 {
3509 result.SetStatus(eReturnStatusSuccessFinishResult);
3510 return true;
3511 }
3512 }
3513 break;
3514
Greg Clayton2ad894b2012-05-15 18:43:44 +00003515
Greg Claytone1f50b92011-05-03 22:09:39 +00003516 case eLookupTypeType:
3517 if (!m_options.m_str.empty())
3518 {
3519 if (LookupTypeInModule (m_interpreter,
3520 result.GetOutputStream(),
3521 module,
3522 m_options.m_str.c_str(),
3523 m_options.m_use_regex))
3524 {
3525 result.SetStatus(eReturnStatusSuccessFinishResult);
3526 return true;
3527 }
3528 }
3529 break;
3530
3531 default:
3532 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3533 syntax_error = true;
3534 break;
3535 }
3536
3537 result.SetStatus (eReturnStatusFailed);
3538 return false;
3539 }
3540
Jim Inghamda26bd22012-06-08 21:56:10 +00003541protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003542 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003543 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003544 CommandReturnObject &result)
3545 {
3546 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3547 if (target == NULL)
3548 {
3549 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3550 result.SetStatus (eReturnStatusFailed);
3551 return false;
3552 }
3553 else
3554 {
3555 bool syntax_error = false;
3556 uint32_t i;
3557 uint32_t num_successful_lookups = 0;
3558 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3559 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3560 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3561 // Dump all sections for all modules images
3562
3563 if (command.GetArgumentCount() == 0)
3564 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003565 ModuleSP current_module;
3566
3567 // Where it is possible to look in the current symbol context
3568 // first, try that. If this search was successful and --all
3569 // was not passed, don't print anything else.
3570 if (LookupHere (m_interpreter, result, syntax_error))
3571 {
3572 result.GetOutputStream().EOL();
3573 num_successful_lookups++;
3574 if (!m_options.m_print_all)
3575 {
3576 result.SetStatus (eReturnStatusSuccessFinishResult);
3577 return result.Succeeded();
3578 }
3579 }
3580
3581 // Dump all sections for all other modules
3582
Jim Ingham93367902012-05-30 02:19:25 +00003583 ModuleList &target_modules = target->GetImages();
3584 Mutex::Locker modules_locker(target_modules.GetMutex());
3585 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003586 if (num_modules > 0)
3587 {
3588 for (i = 0; i<num_modules && syntax_error == false; ++i)
3589 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003590 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3591
3592 if (module_pointer != current_module.get() &&
3593 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003594 {
3595 result.GetOutputStream().EOL();
3596 num_successful_lookups++;
3597 }
3598 }
3599 }
3600 else
3601 {
3602 result.AppendError ("the target has no associated executable images");
3603 result.SetStatus (eReturnStatusFailed);
3604 return false;
3605 }
3606 }
3607 else
3608 {
3609 // Dump specified images (by basename or fullpath)
3610 const char *arg_cstr;
3611 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3612 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003613 ModuleList module_list;
3614 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3615 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003616 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003617 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003618 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003619 Module *module = module_list.GetModulePointerAtIndex(i);
3620 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003621 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003622 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003623 {
3624 result.GetOutputStream().EOL();
3625 num_successful_lookups++;
3626 }
3627 }
3628 }
3629 }
3630 else
3631 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3632 }
3633 }
3634
3635 if (num_successful_lookups > 0)
3636 result.SetStatus (eReturnStatusSuccessFinishResult);
3637 else
3638 result.SetStatus (eReturnStatusFailed);
3639 }
3640 return result.Succeeded();
3641 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003642
3643 CommandOptions m_options;
3644};
3645
3646OptionDefinition
3647CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3648{
3649 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3650 { 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 +00003651 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3652 /* 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 +00003653 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003654 { 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 +00003655 { 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."},
3656 { 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 +00003657 { LLDB_OPT_SET_FROM_TO(3,5),
3658 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 +00003659 { 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."},
3660 { 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."},
3661 { 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 +00003662 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
Sean Callanan56d31ec2012-06-06 20:49:55 +00003663 { 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 +00003664 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003665};
Chris Lattner24943d22010-06-08 16:52:24 +00003666
3667
Jim Inghamd60d94a2011-03-11 03:53:59 +00003668#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003669
3670//-------------------------------------------------------------------------
3671// CommandObjectMultiwordImageSearchPaths
3672//-------------------------------------------------------------------------
3673
Greg Claytone1f50b92011-05-03 22:09:39 +00003674class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003675{
3676public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003677
3678 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3679 CommandObjectMultiword (interpreter,
3680 "target modules search-paths",
3681 "A set of commands for operating on debugger target image search paths.",
3682 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003683 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003684 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3685 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3686 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3687 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3688 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003689 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003690
3691 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003692 {
3693 }
3694};
3695
Greg Claytone1f50b92011-05-03 22:09:39 +00003696
3697
3698#pragma mark CommandObjectTargetModules
3699
3700//-------------------------------------------------------------------------
3701// CommandObjectTargetModules
3702//-------------------------------------------------------------------------
3703
3704class CommandObjectTargetModules : public CommandObjectMultiword
3705{
3706public:
3707 //------------------------------------------------------------------
3708 // Constructors and Destructors
3709 //------------------------------------------------------------------
3710 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3711 CommandObjectMultiword (interpreter,
3712 "target modules",
3713 "A set of commands for accessing information for one or more target modules.",
3714 "target modules <sub-command> ...")
3715 {
3716 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3717 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3718 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3719 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3720 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3721 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3722 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
3723
3724 }
3725 virtual
3726 ~CommandObjectTargetModules()
3727 {
3728 }
3729
3730private:
3731 //------------------------------------------------------------------
3732 // For CommandObjectTargetModules only
3733 //------------------------------------------------------------------
3734 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3735};
3736
3737
Greg Clayton3508c382012-02-24 01:59:29 +00003738
Jim Inghamda26bd22012-06-08 21:56:10 +00003739class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00003740{
3741public:
3742 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003743 CommandObjectParsed (interpreter,
3744 "target symbols add",
3745 "Add a debug symbol file to one of the target's current modules.",
3746 "target symbols add [<symfile>]")
Greg Clayton3508c382012-02-24 01:59:29 +00003747 {
3748 }
3749
3750 virtual
3751 ~CommandObjectTargetSymbolsAdd ()
3752 {
3753 }
3754
Jim Inghamda26bd22012-06-08 21:56:10 +00003755 int
3756 HandleArgumentCompletion (Args &input,
3757 int &cursor_index,
3758 int &cursor_char_position,
3759 OptionElementVector &opt_element_vector,
3760 int match_start_point,
3761 int max_return_elements,
3762 bool &word_complete,
3763 StringList &matches)
3764 {
3765 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
3766 completion_str.erase (cursor_char_position);
3767
3768 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
3769 CommandCompletions::eDiskFileCompletion,
3770 completion_str.c_str(),
3771 match_start_point,
3772 max_return_elements,
3773 NULL,
3774 word_complete,
3775 matches);
3776 return matches.GetSize();
3777 }
3778
3779protected:
Greg Clayton3508c382012-02-24 01:59:29 +00003780 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003781 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00003782 CommandReturnObject &result)
3783 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003784 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3785 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton3508c382012-02-24 01:59:29 +00003786 if (target == NULL)
3787 {
3788 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3789 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003790 }
3791 else
3792 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003793 bool flush = false;
Greg Clayton3508c382012-02-24 01:59:29 +00003794 const size_t argc = args.GetArgumentCount();
3795 if (argc == 0)
3796 {
3797 result.AppendError ("one or more symbol file paths must be specified");
3798 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003799 }
3800 else
3801 {
3802 for (size_t i=0; i<argc; ++i)
3803 {
3804 const char *symfile_path = args.GetArgumentAtIndex(i);
3805 if (symfile_path)
3806 {
3807 FileSpec symfile_spec(symfile_path, true);
3808 ArchSpec arch;
3809 if (symfile_spec.Exists())
3810 {
3811 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
3812 if (symfile_module_sp)
3813 {
3814 // We now have a module that represents a symbol file
3815 // that can be used for a module that might exist in the
3816 // current target, so we need to find that module in the
3817 // target
3818
3819 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_module_sp->GetUUID()));
3820 if (old_module_sp)
3821 {
Greg Claytond4f16c82012-03-29 21:43:25 +00003822 // The module has not yet created its symbol vendor, we can just
3823 // give the existing target module the symfile path to use for
3824 // when it decides to create it!
3825 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
Greg Clayton3508c382012-02-24 01:59:29 +00003826
Greg Claytond4f16c82012-03-29 21:43:25 +00003827 // Let clients know something changed in the module
3828 // if it is currently loaded
3829 ModuleList module_list;
3830 module_list.Append (old_module_sp);
3831 target->ModulesDidLoad (module_list);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003832 flush = true;
Greg Clayton3508c382012-02-24 01:59:29 +00003833 }
3834 }
3835 else
3836 {
3837 result.AppendError ("one or more executable image paths must be specified");
3838 result.SetStatus (eReturnStatusFailed);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003839 break;
Greg Clayton3508c382012-02-24 01:59:29 +00003840 }
3841 result.SetStatus (eReturnStatusSuccessFinishResult);
3842 }
3843 else
3844 {
3845 char resolved_symfile_path[PATH_MAX];
3846 result.SetStatus (eReturnStatusFailed);
3847 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
3848 {
3849 if (strcmp (resolved_symfile_path, symfile_path) != 0)
3850 {
3851 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
3852 break;
3853 }
3854 }
3855 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
3856 break;
3857 }
3858 }
3859 }
3860 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00003861
3862 if (flush)
3863 {
3864 Process *process = exe_ctx.GetProcessPtr();
3865 if (process)
3866 process->Flush();
3867 }
Greg Clayton3508c382012-02-24 01:59:29 +00003868 }
3869 return result.Succeeded();
3870 }
3871
Greg Clayton3508c382012-02-24 01:59:29 +00003872};
3873
3874
3875#pragma mark CommandObjectTargetSymbols
3876
3877//-------------------------------------------------------------------------
3878// CommandObjectTargetSymbols
3879//-------------------------------------------------------------------------
3880
3881class CommandObjectTargetSymbols : public CommandObjectMultiword
3882{
3883public:
3884 //------------------------------------------------------------------
3885 // Constructors and Destructors
3886 //------------------------------------------------------------------
3887 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
3888 CommandObjectMultiword (interpreter,
3889 "target symbols",
3890 "A set of commands for adding and managing debug symbol files.",
3891 "target symbols <sub-command> ...")
3892 {
3893 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
3894
3895 }
3896 virtual
3897 ~CommandObjectTargetSymbols()
3898 {
3899 }
3900
3901private:
3902 //------------------------------------------------------------------
3903 // For CommandObjectTargetModules only
3904 //------------------------------------------------------------------
3905 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
3906};
3907
3908
Jim Inghamd60d94a2011-03-11 03:53:59 +00003909#pragma mark CommandObjectTargetStopHookAdd
3910
3911//-------------------------------------------------------------------------
3912// CommandObjectTargetStopHookAdd
3913//-------------------------------------------------------------------------
3914
Jim Inghamda26bd22012-06-08 21:56:10 +00003915class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00003916{
3917public:
3918
3919 class CommandOptions : public Options
3920 {
3921 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00003922 CommandOptions (CommandInterpreter &interpreter) :
3923 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00003924 m_line_start(0),
3925 m_line_end (UINT_MAX),
3926 m_func_name_type_mask (eFunctionNameTypeAuto),
3927 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00003928 m_thread_specified (false),
3929 m_use_one_liner (false),
3930 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003931 {
3932 }
3933
3934 ~CommandOptions () {}
3935
Greg Claytonb3448432011-03-24 21:19:54 +00003936 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00003937 GetDefinitions ()
3938 {
3939 return g_option_table;
3940 }
3941
3942 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00003943 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003944 {
3945 Error error;
3946 char short_option = (char) m_getopt_table[option_idx].val;
3947 bool success;
3948
3949 switch (short_option)
3950 {
3951 case 'c':
3952 m_class_name = option_arg;
3953 m_sym_ctx_specified = true;
3954 break;
3955
3956 case 'e':
3957 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
3958 if (!success)
3959 {
Greg Clayton9c236732011-10-26 00:56:27 +00003960 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003961 break;
3962 }
3963 m_sym_ctx_specified = true;
3964 break;
3965
3966 case 'l':
3967 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
3968 if (!success)
3969 {
Greg Clayton9c236732011-10-26 00:56:27 +00003970 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003971 break;
3972 }
3973 m_sym_ctx_specified = true;
3974 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00003975
3976 case 'i':
3977 m_no_inlines = true;
3978 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003979
3980 case 'n':
3981 m_function_name = option_arg;
3982 m_func_name_type_mask |= eFunctionNameTypeAuto;
3983 m_sym_ctx_specified = true;
3984 break;
3985
3986 case 'f':
3987 m_file_name = option_arg;
3988 m_sym_ctx_specified = true;
3989 break;
3990 case 's':
3991 m_module_name = option_arg;
3992 m_sym_ctx_specified = true;
3993 break;
3994 case 't' :
3995 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003996 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003997 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00003998 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003999 m_thread_specified = true;
4000 }
4001 break;
4002 case 'T':
4003 m_thread_name = option_arg;
4004 m_thread_specified = true;
4005 break;
4006 case 'q':
4007 m_queue_name = option_arg;
4008 m_thread_specified = true;
4009 break;
4010 case 'x':
4011 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004012 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004013 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004014 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004015 m_thread_specified = true;
4016 }
4017 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004018 case 'o':
4019 m_use_one_liner = true;
4020 m_one_liner = option_arg;
4021 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004022 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004023 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004024 break;
4025 }
4026 return error;
4027 }
4028
4029 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004030 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004031 {
4032 m_class_name.clear();
4033 m_function_name.clear();
4034 m_line_start = 0;
4035 m_line_end = UINT_MAX;
4036 m_file_name.clear();
4037 m_module_name.clear();
4038 m_func_name_type_mask = eFunctionNameTypeAuto;
4039 m_thread_id = LLDB_INVALID_THREAD_ID;
4040 m_thread_index = UINT32_MAX;
4041 m_thread_name.clear();
4042 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004043
4044 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004045 m_sym_ctx_specified = false;
4046 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004047
4048 m_use_one_liner = false;
4049 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004050 }
4051
4052
Greg Claytonb3448432011-03-24 21:19:54 +00004053 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004054
4055 std::string m_class_name;
4056 std::string m_function_name;
4057 uint32_t m_line_start;
4058 uint32_t m_line_end;
4059 std::string m_file_name;
4060 std::string m_module_name;
4061 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4062 lldb::tid_t m_thread_id;
4063 uint32_t m_thread_index;
4064 std::string m_thread_name;
4065 std::string m_queue_name;
4066 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004067 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004068 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004069 // Instance variables to hold the values for one_liner options.
4070 bool m_use_one_liner;
4071 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004072 };
4073
4074 Options *
4075 GetOptions ()
4076 {
4077 return &m_options;
4078 }
4079
4080 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004081 CommandObjectParsed (interpreter,
4082 "target stop-hook add ",
4083 "Add a hook to be executed when the target stops.",
4084 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004085 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004086 {
4087 }
4088
4089 ~CommandObjectTargetStopHookAdd ()
4090 {
4091 }
4092
4093 static size_t
4094 ReadCommandsCallbackFunction (void *baton,
4095 InputReader &reader,
4096 lldb::InputReaderAction notification,
4097 const char *bytes,
4098 size_t bytes_len)
4099 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004100 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004101 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004102 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004103 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004104
4105 switch (notification)
4106 {
4107 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004108 if (!batch_mode)
4109 {
4110 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4111 if (reader.GetPrompt())
4112 out_stream->Printf ("%s", reader.GetPrompt());
4113 out_stream->Flush();
4114 }
Jim Inghame15511a2011-05-05 01:03:36 +00004115 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004116 break;
4117
4118 case eInputReaderDeactivate:
4119 break;
4120
4121 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004122 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004123 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004124 out_stream->Printf ("%s", reader.GetPrompt());
4125 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004126 }
Jim Inghame15511a2011-05-05 01:03:36 +00004127 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004128 break;
4129
Caroline Tice4a348082011-05-02 20:41:46 +00004130 case eInputReaderAsynchronousOutputWritten:
4131 break;
4132
Jim Inghamd60d94a2011-03-11 03:53:59 +00004133 case eInputReaderGotToken:
4134 if (bytes && bytes_len && baton)
4135 {
4136 StringList *commands = new_stop_hook->GetCommandPointer();
4137 if (commands)
4138 {
4139 commands->AppendString (bytes, bytes_len);
4140 }
4141 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004142 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004143 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004144 out_stream->Printf ("%s", reader.GetPrompt());
4145 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004146 }
4147 break;
4148
4149 case eInputReaderInterrupt:
4150 {
4151 // Finish, and cancel the stop hook.
4152 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004153 if (!batch_mode)
4154 {
4155 out_stream->Printf ("Stop hook cancelled.\n");
4156 out_stream->Flush();
4157 }
4158
Jim Inghamd60d94a2011-03-11 03:53:59 +00004159 reader.SetIsDone (true);
4160 }
Jim Inghame15511a2011-05-05 01:03:36 +00004161 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004162 break;
4163
4164 case eInputReaderEndOfFile:
4165 reader.SetIsDone (true);
4166 break;
4167
4168 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004169 if (!got_interrupted && !batch_mode)
4170 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004171 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004172 out_stream->Flush();
4173 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004174 break;
4175 }
4176
4177 return bytes_len;
4178 }
4179
Jim Inghamda26bd22012-06-08 21:56:10 +00004180protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004181 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004182 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004183 {
4184 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4185 if (target)
4186 {
4187 Target::StopHookSP new_hook_sp;
4188 target->AddStopHook (new_hook_sp);
4189
4190 // First step, make the specifier.
4191 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4192 if (m_options.m_sym_ctx_specified)
4193 {
4194 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4195
4196 if (!m_options.m_module_name.empty())
4197 {
4198 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4199 }
4200
4201 if (!m_options.m_class_name.empty())
4202 {
4203 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4204 }
4205
4206 if (!m_options.m_file_name.empty())
4207 {
4208 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4209 }
4210
4211 if (m_options.m_line_start != 0)
4212 {
4213 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4214 }
4215
4216 if (m_options.m_line_end != UINT_MAX)
4217 {
4218 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4219 }
4220
4221 if (!m_options.m_function_name.empty())
4222 {
4223 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4224 }
4225 }
4226
4227 if (specifier_ap.get())
4228 new_hook_sp->SetSpecifier (specifier_ap.release());
4229
4230 // Next see if any of the thread options have been entered:
4231
4232 if (m_options.m_thread_specified)
4233 {
4234 ThreadSpec *thread_spec = new ThreadSpec();
4235
4236 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4237 {
4238 thread_spec->SetTID (m_options.m_thread_id);
4239 }
4240
4241 if (m_options.m_thread_index != UINT32_MAX)
4242 thread_spec->SetIndex (m_options.m_thread_index);
4243
4244 if (!m_options.m_thread_name.empty())
4245 thread_spec->SetName (m_options.m_thread_name.c_str());
4246
4247 if (!m_options.m_queue_name.empty())
4248 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4249
4250 new_hook_sp->SetThreadSpecifier (thread_spec);
4251
4252 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004253 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004254 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004255 // Use one-liner.
4256 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004257 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004258 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004259 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004260 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004261 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4262 // the new stop hook's command string.
4263 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4264 if (!reader_sp)
4265 {
4266 result.AppendError("out of memory\n");
4267 result.SetStatus (eReturnStatusFailed);
4268 target->RemoveStopHookByID (new_hook_sp->GetID());
4269 return false;
4270 }
4271
4272 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4273 new_hook_sp.get(), // baton
4274 eInputReaderGranularityLine, // token size, to pass to callback function
4275 "DONE", // end token
4276 "> ", // prompt
4277 true)); // echo input
4278 if (!err.Success())
4279 {
4280 result.AppendError (err.AsCString());
4281 result.SetStatus (eReturnStatusFailed);
4282 target->RemoveStopHookByID (new_hook_sp->GetID());
4283 return false;
4284 }
4285 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004286 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004287 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4288 }
4289 else
4290 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004291 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004292 result.SetStatus (eReturnStatusFailed);
4293 }
4294
4295 return result.Succeeded();
4296 }
4297private:
4298 CommandOptions m_options;
4299};
4300
Greg Claytonb3448432011-03-24 21:19:54 +00004301OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004302CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4303{
Johnny Chen60fe60e2011-05-02 23:47:55 +00004304 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
4305 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004306 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4307 "Set the module within which the stop-hook is to be run."},
4308 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
4309 "The stop hook is run only for the thread whose index matches this argument."},
4310 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
4311 "The stop hook is run only for the thread whose TID matches this argument."},
4312 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
4313 "The stop hook is run only for the thread whose thread name matches this argument."},
4314 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
4315 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4316 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4317 "Specify the source file within which the stop-hook is to be run." },
4318 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4319 "Set the start of the line range for which the stop-hook is to be run."},
4320 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4321 "Set the end of the line range for which the stop-hook is to be run."},
4322 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
4323 "Specify the class within which the stop-hook is to be run." },
4324 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4325 "Set the function name within which the stop hook will be run." },
4326 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4327};
4328
4329#pragma mark CommandObjectTargetStopHookDelete
4330
4331//-------------------------------------------------------------------------
4332// CommandObjectTargetStopHookDelete
4333//-------------------------------------------------------------------------
4334
Jim Inghamda26bd22012-06-08 21:56:10 +00004335class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004336{
4337public:
4338
4339 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004340 CommandObjectParsed (interpreter,
4341 "target stop-hook delete",
4342 "Delete a stop-hook.",
4343 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004344 {
4345 }
4346
4347 ~CommandObjectTargetStopHookDelete ()
4348 {
4349 }
4350
Jim Inghamda26bd22012-06-08 21:56:10 +00004351protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004352 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004353 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004354 {
4355 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4356 if (target)
4357 {
4358 // FIXME: see if we can use the breakpoint id style parser?
4359 size_t num_args = command.GetArgumentCount();
4360 if (num_args == 0)
4361 {
4362 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4363 {
4364 result.SetStatus (eReturnStatusFailed);
4365 return false;
4366 }
4367 else
4368 {
4369 target->RemoveAllStopHooks();
4370 }
4371 }
4372 else
4373 {
4374 bool success;
4375 for (size_t i = 0; i < num_args; i++)
4376 {
4377 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4378 if (!success)
4379 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004380 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004381 result.SetStatus(eReturnStatusFailed);
4382 return false;
4383 }
4384 success = target->RemoveStopHookByID (user_id);
4385 if (!success)
4386 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004387 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004388 result.SetStatus(eReturnStatusFailed);
4389 return false;
4390 }
4391 }
4392 }
4393 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4394 }
4395 else
4396 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004397 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004398 result.SetStatus (eReturnStatusFailed);
4399 }
4400
4401 return result.Succeeded();
4402 }
4403};
4404#pragma mark CommandObjectTargetStopHookEnableDisable
4405
4406//-------------------------------------------------------------------------
4407// CommandObjectTargetStopHookEnableDisable
4408//-------------------------------------------------------------------------
4409
Jim Inghamda26bd22012-06-08 21:56:10 +00004410class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004411{
4412public:
4413
4414 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004415 CommandObjectParsed (interpreter,
4416 name,
4417 help,
4418 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004419 m_enable (enable)
4420 {
4421 }
4422
4423 ~CommandObjectTargetStopHookEnableDisable ()
4424 {
4425 }
4426
Jim Inghamda26bd22012-06-08 21:56:10 +00004427protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004428 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004429 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004430 {
4431 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4432 if (target)
4433 {
4434 // FIXME: see if we can use the breakpoint id style parser?
4435 size_t num_args = command.GetArgumentCount();
4436 bool success;
4437
4438 if (num_args == 0)
4439 {
4440 target->SetAllStopHooksActiveState (m_enable);
4441 }
4442 else
4443 {
4444 for (size_t i = 0; i < num_args; i++)
4445 {
4446 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4447 if (!success)
4448 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004449 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004450 result.SetStatus(eReturnStatusFailed);
4451 return false;
4452 }
4453 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4454 if (!success)
4455 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004456 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004457 result.SetStatus(eReturnStatusFailed);
4458 return false;
4459 }
4460 }
4461 }
4462 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4463 }
4464 else
4465 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004466 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004467 result.SetStatus (eReturnStatusFailed);
4468 }
4469 return result.Succeeded();
4470 }
4471private:
4472 bool m_enable;
4473};
4474
4475#pragma mark CommandObjectTargetStopHookList
4476
4477//-------------------------------------------------------------------------
4478// CommandObjectTargetStopHookList
4479//-------------------------------------------------------------------------
4480
Jim Inghamda26bd22012-06-08 21:56:10 +00004481class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004482{
4483public:
4484
4485 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004486 CommandObjectParsed (interpreter,
4487 "target stop-hook list",
4488 "List all stop-hooks.",
4489 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004490 {
4491 }
4492
4493 ~CommandObjectTargetStopHookList ()
4494 {
4495 }
4496
Jim Inghamda26bd22012-06-08 21:56:10 +00004497protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004498 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004499 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004500 {
4501 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004502 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004503 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004504 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004505 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004506 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004507 }
4508
4509 size_t num_hooks = target->GetNumStopHooks ();
4510 if (num_hooks == 0)
4511 {
4512 result.GetOutputStream().PutCString ("No stop hooks.\n");
4513 }
4514 else
4515 {
4516 for (size_t i = 0; i < num_hooks; i++)
4517 {
4518 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4519 if (i > 0)
4520 result.GetOutputStream().PutCString ("\n");
4521 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4522 }
4523 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004524 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004525 return result.Succeeded();
4526 }
4527};
4528
4529#pragma mark CommandObjectMultiwordTargetStopHooks
4530//-------------------------------------------------------------------------
4531// CommandObjectMultiwordTargetStopHooks
4532//-------------------------------------------------------------------------
4533
4534class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4535{
4536public:
4537
4538 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4539 CommandObjectMultiword (interpreter,
4540 "target stop-hook",
4541 "A set of commands for operating on debugger target stop-hooks.",
4542 "target stop-hook <subcommand> [<subcommand-options>]")
4543 {
4544 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4545 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4546 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4547 false,
4548 "target stop-hook disable [<id>]",
4549 "Disable a stop-hook.",
4550 "target stop-hook disable")));
4551 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4552 true,
4553 "target stop-hook enable [<id>]",
4554 "Enable a stop-hook.",
4555 "target stop-hook enable")));
4556 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4557 }
4558
4559 ~CommandObjectMultiwordTargetStopHooks()
4560 {
4561 }
4562};
4563
4564
Chris Lattner24943d22010-06-08 16:52:24 +00004565
4566#pragma mark CommandObjectMultiwordTarget
4567
4568//-------------------------------------------------------------------------
4569// CommandObjectMultiwordTarget
4570//-------------------------------------------------------------------------
4571
Greg Clayton63094e02010-06-23 01:19:29 +00004572CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004573 CommandObjectMultiword (interpreter,
4574 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004575 "A set of commands for operating on debugger targets.",
4576 "target <subcommand> [<subcommand-options>]")
4577{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004578
4579 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004580 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004581 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4582 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004583 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004584 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00004585 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004586 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004587}
4588
4589CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4590{
4591}
4592
Greg Claytonabe0fed2011-04-18 08:33:37 +00004593