blob: 23d174290f3a8ad9a7d31319ea16e0e021739570 [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
Greg Clayton316f57f2012-07-11 20:46:47 +00001746 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001747 {
1748 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001749 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001750 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1751 ModuleSP module_sp;
1752 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1753 {
1754 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1755
1756 if (module)
1757 {
Greg Clayton444fe992012-02-26 05:51:37 +00001758 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001759 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001760 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001761 module_list.AppendIfNeeded(module_sp);
1762 }
1763 }
1764 }
1765 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001766 else
1767 {
1768 if (target)
1769 {
1770 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1771
1772 // Not found in our module list for our target, check the main
1773 // shared module list in case it is a extra file used somewhere
1774 // else
1775 if (num_matches == 0)
1776 {
1777 module_spec.GetArchitecture() = target->GetArchitecture();
1778 ModuleList::FindSharedModules (module_spec, module_list);
1779 }
1780 }
1781 else
1782 {
1783 ModuleList::FindSharedModules (module_spec,module_list);
1784 }
1785 }
1786
Greg Clayton91048ef2011-11-10 01:18:58 +00001787 return module_list.GetSize () - initial_size;
1788}
1789
Greg Claytone1f50b92011-05-03 22:09:39 +00001790#pragma mark CommandObjectTargetModulesModuleAutoComplete
1791
1792//----------------------------------------------------------------------
1793// A base command object class that can auto complete with module file
1794// paths
1795//----------------------------------------------------------------------
1796
Jim Inghamda26bd22012-06-08 21:56:10 +00001797class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001798{
1799public:
1800
1801 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1802 const char *name,
1803 const char *help,
1804 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001805 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001806 {
1807 CommandArgumentEntry arg;
1808 CommandArgumentData file_arg;
1809
1810 // Define the first (and only) variant of this arg.
1811 file_arg.arg_type = eArgTypeFilename;
1812 file_arg.arg_repetition = eArgRepeatStar;
1813
1814 // There is only one variant this argument could be; put it into the argument entry.
1815 arg.push_back (file_arg);
1816
1817 // Push the data for the first argument into the m_arguments vector.
1818 m_arguments.push_back (arg);
1819 }
1820
1821 virtual
1822 ~CommandObjectTargetModulesModuleAutoComplete ()
1823 {
1824 }
1825
1826 virtual int
1827 HandleArgumentCompletion (Args &input,
1828 int &cursor_index,
1829 int &cursor_char_position,
1830 OptionElementVector &opt_element_vector,
1831 int match_start_point,
1832 int max_return_elements,
1833 bool &word_complete,
1834 StringList &matches)
1835 {
1836 // Arguments are the standard module completer.
1837 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1838 completion_str.erase (cursor_char_position);
1839
1840 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1841 CommandCompletions::eModuleCompletion,
1842 completion_str.c_str(),
1843 match_start_point,
1844 max_return_elements,
1845 NULL,
1846 word_complete,
1847 matches);
1848 return matches.GetSize();
1849 }
1850};
1851
1852#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1853
1854//----------------------------------------------------------------------
1855// A base command object class that can auto complete with module source
1856// file paths
1857//----------------------------------------------------------------------
1858
Jim Inghamda26bd22012-06-08 21:56:10 +00001859class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001860{
1861public:
1862
1863 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1864 const char *name,
1865 const char *help,
1866 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001867 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001868 {
1869 CommandArgumentEntry arg;
1870 CommandArgumentData source_file_arg;
1871
1872 // Define the first (and only) variant of this arg.
1873 source_file_arg.arg_type = eArgTypeSourceFile;
1874 source_file_arg.arg_repetition = eArgRepeatPlus;
1875
1876 // There is only one variant this argument could be; put it into the argument entry.
1877 arg.push_back (source_file_arg);
1878
1879 // Push the data for the first argument into the m_arguments vector.
1880 m_arguments.push_back (arg);
1881 }
1882
1883 virtual
1884 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1885 {
1886 }
1887
1888 virtual int
1889 HandleArgumentCompletion (Args &input,
1890 int &cursor_index,
1891 int &cursor_char_position,
1892 OptionElementVector &opt_element_vector,
1893 int match_start_point,
1894 int max_return_elements,
1895 bool &word_complete,
1896 StringList &matches)
1897 {
1898 // Arguments are the standard source file completer.
1899 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1900 completion_str.erase (cursor_char_position);
1901
1902 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1903 CommandCompletions::eSourceFileCompletion,
1904 completion_str.c_str(),
1905 match_start_point,
1906 max_return_elements,
1907 NULL,
1908 word_complete,
1909 matches);
1910 return matches.GetSize();
1911 }
1912};
1913
1914
1915#pragma mark CommandObjectTargetModulesDumpSymtab
1916
1917
1918class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1919{
1920public:
1921 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1922 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1923 "target modules dump symtab",
1924 "Dump the symbol table from one or more target modules.",
1925 NULL),
1926 m_options (interpreter)
1927 {
1928 }
1929
1930 virtual
1931 ~CommandObjectTargetModulesDumpSymtab ()
1932 {
1933 }
1934
Jim Inghamda26bd22012-06-08 21:56:10 +00001935 virtual Options *
1936 GetOptions ()
1937 {
1938 return &m_options;
1939 }
1940
1941 class CommandOptions : public Options
1942 {
1943 public:
1944
1945 CommandOptions (CommandInterpreter &interpreter) :
1946 Options(interpreter),
1947 m_sort_order (eSortOrderNone)
1948 {
1949 }
1950
1951 virtual
1952 ~CommandOptions ()
1953 {
1954 }
1955
1956 virtual Error
1957 SetOptionValue (uint32_t option_idx, const char *option_arg)
1958 {
1959 Error error;
1960 char short_option = (char) m_getopt_table[option_idx].val;
1961
1962 switch (short_option)
1963 {
1964 case 's':
1965 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1966 g_option_table[option_idx].enum_values,
1967 eSortOrderNone,
1968 error);
1969 break;
1970
1971 default:
1972 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1973 break;
1974
1975 }
1976 return error;
1977 }
1978
1979 void
1980 OptionParsingStarting ()
1981 {
1982 m_sort_order = eSortOrderNone;
1983 }
1984
1985 const OptionDefinition*
1986 GetDefinitions ()
1987 {
1988 return g_option_table;
1989 }
1990
1991 // Options table: Required for subclasses of Options.
1992 static OptionDefinition g_option_table[];
1993
1994 SortOrder m_sort_order;
1995 };
1996
1997protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00001998 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001999 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002000 CommandReturnObject &result)
2001 {
2002 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2003 if (target == NULL)
2004 {
2005 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2006 result.SetStatus (eReturnStatusFailed);
2007 return false;
2008 }
2009 else
2010 {
2011 uint32_t num_dumped = 0;
2012
2013 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2014 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2015 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2016
2017 if (command.GetArgumentCount() == 0)
2018 {
2019 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002020 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002021 const uint32_t num_modules = target->GetImages().GetSize();
2022 if (num_modules > 0)
2023 {
2024 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2025 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2026 {
2027 if (num_dumped > 0)
2028 {
2029 result.GetOutputStream().EOL();
2030 result.GetOutputStream().EOL();
2031 }
2032 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002033 DumpModuleSymtab (m_interpreter,
2034 result.GetOutputStream(),
2035 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2036 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002037 }
2038 }
2039 else
2040 {
2041 result.AppendError ("the target has no associated executable images");
2042 result.SetStatus (eReturnStatusFailed);
2043 return false;
2044 }
2045 }
2046 else
2047 {
2048 // Dump specified images (by basename or fullpath)
2049 const char *arg_cstr;
2050 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2051 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002052 ModuleList module_list;
2053 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2054 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002055 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002056 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002057 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002058 Module *module = module_list.GetModulePointerAtIndex(i);
2059 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002060 {
2061 if (num_dumped > 0)
2062 {
2063 result.GetOutputStream().EOL();
2064 result.GetOutputStream().EOL();
2065 }
2066 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002067 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002068 }
2069 }
2070 }
2071 else
2072 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2073 }
2074 }
2075
2076 if (num_dumped > 0)
2077 result.SetStatus (eReturnStatusSuccessFinishResult);
2078 else
2079 {
2080 result.AppendError ("no matching executable images found");
2081 result.SetStatus (eReturnStatusFailed);
2082 }
2083 }
2084 return result.Succeeded();
2085 }
2086
Greg Claytone1f50b92011-05-03 22:09:39 +00002087
2088 CommandOptions m_options;
2089};
2090
2091static OptionEnumValueElement
2092g_sort_option_enumeration[4] =
2093{
2094 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2095 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2096 { eSortOrderByName, "name", "Sort output by symbol name."},
2097 { 0, NULL, NULL }
2098};
2099
2100
2101OptionDefinition
2102CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2103{
2104 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2105 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2106};
2107
2108#pragma mark CommandObjectTargetModulesDumpSections
2109
2110//----------------------------------------------------------------------
2111// Image section dumping command
2112//----------------------------------------------------------------------
2113
2114class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2115{
2116public:
2117 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2118 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2119 "target modules dump sections",
2120 "Dump the sections from one or more target modules.",
2121 //"target modules dump sections [<file1> ...]")
2122 NULL)
2123 {
2124 }
2125
2126 virtual
2127 ~CommandObjectTargetModulesDumpSections ()
2128 {
2129 }
2130
Jim Inghamda26bd22012-06-08 21:56:10 +00002131protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002132 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002133 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002134 CommandReturnObject &result)
2135 {
2136 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2137 if (target == NULL)
2138 {
2139 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2140 result.SetStatus (eReturnStatusFailed);
2141 return false;
2142 }
2143 else
2144 {
2145 uint32_t num_dumped = 0;
2146
2147 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2148 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2149 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2150
2151 if (command.GetArgumentCount() == 0)
2152 {
2153 // Dump all sections for all modules images
2154 const uint32_t num_modules = target->GetImages().GetSize();
2155 if (num_modules > 0)
2156 {
2157 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2158 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2159 {
2160 num_dumped++;
2161 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2162 }
2163 }
2164 else
2165 {
2166 result.AppendError ("the target has no associated executable images");
2167 result.SetStatus (eReturnStatusFailed);
2168 return false;
2169 }
2170 }
2171 else
2172 {
2173 // Dump specified images (by basename or fullpath)
2174 const char *arg_cstr;
2175 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2176 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002177 ModuleList module_list;
2178 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2179 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002180 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002181 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002182 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002183 Module *module = module_list.GetModulePointerAtIndex(i);
2184 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002185 {
2186 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002187 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002188 }
2189 }
2190 }
2191 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002192 {
2193 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002194 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002195
Greg Claytone1f50b92011-05-03 22:09:39 +00002196 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002197 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002198 }
2199 }
2200
2201 if (num_dumped > 0)
2202 result.SetStatus (eReturnStatusSuccessFinishResult);
2203 else
2204 {
2205 result.AppendError ("no matching executable images found");
2206 result.SetStatus (eReturnStatusFailed);
2207 }
2208 }
2209 return result.Succeeded();
2210 }
2211};
2212
2213
2214#pragma mark CommandObjectTargetModulesDumpSymfile
2215
2216//----------------------------------------------------------------------
2217// Image debug symbol dumping command
2218//----------------------------------------------------------------------
2219
2220class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2221{
2222public:
2223 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2224 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2225 "target modules dump symfile",
2226 "Dump the debug symbol file for one or more target modules.",
2227 //"target modules dump symfile [<file1> ...]")
2228 NULL)
2229 {
2230 }
2231
2232 virtual
2233 ~CommandObjectTargetModulesDumpSymfile ()
2234 {
2235 }
2236
Jim Inghamda26bd22012-06-08 21:56:10 +00002237protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002238 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002239 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002240 CommandReturnObject &result)
2241 {
2242 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2243 if (target == NULL)
2244 {
2245 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2246 result.SetStatus (eReturnStatusFailed);
2247 return false;
2248 }
2249 else
2250 {
2251 uint32_t num_dumped = 0;
2252
2253 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2254 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2255 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2256
2257 if (command.GetArgumentCount() == 0)
2258 {
2259 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002260 ModuleList &target_modules = target->GetImages();
2261 Mutex::Locker modules_locker (target_modules.GetMutex());
2262 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002263 if (num_modules > 0)
2264 {
2265 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2266 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2267 {
Jim Ingham93367902012-05-30 02:19:25 +00002268 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002269 num_dumped++;
2270 }
2271 }
2272 else
2273 {
2274 result.AppendError ("the target has no associated executable images");
2275 result.SetStatus (eReturnStatusFailed);
2276 return false;
2277 }
2278 }
2279 else
2280 {
2281 // Dump specified images (by basename or fullpath)
2282 const char *arg_cstr;
2283 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2284 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002285 ModuleList module_list;
2286 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2287 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002288 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002289 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002290 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002291 Module *module = module_list.GetModulePointerAtIndex(i);
2292 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002293 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002294 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002295 num_dumped++;
2296 }
2297 }
2298 }
2299 else
2300 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2301 }
2302 }
2303
2304 if (num_dumped > 0)
2305 result.SetStatus (eReturnStatusSuccessFinishResult);
2306 else
2307 {
2308 result.AppendError ("no matching executable images found");
2309 result.SetStatus (eReturnStatusFailed);
2310 }
2311 }
2312 return result.Succeeded();
2313 }
2314};
2315
2316
2317#pragma mark CommandObjectTargetModulesDumpLineTable
2318
2319//----------------------------------------------------------------------
2320// Image debug line table dumping command
2321//----------------------------------------------------------------------
2322
2323class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2324{
2325public:
2326 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2327 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2328 "target modules dump line-table",
2329 "Dump the debug symbol file for one or more target modules.",
2330 NULL)
2331 {
2332 }
2333
2334 virtual
2335 ~CommandObjectTargetModulesDumpLineTable ()
2336 {
2337 }
2338
Jim Inghamda26bd22012-06-08 21:56:10 +00002339protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002340 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002341 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002342 CommandReturnObject &result)
2343 {
2344 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2345 if (target == NULL)
2346 {
2347 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2348 result.SetStatus (eReturnStatusFailed);
2349 return false;
2350 }
2351 else
2352 {
2353 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2354 uint32_t total_num_dumped = 0;
2355
2356 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2357 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2358 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2359
2360 if (command.GetArgumentCount() == 0)
2361 {
2362 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2363 result.SetStatus (eReturnStatusFailed);
2364 }
2365 else
2366 {
2367 // Dump specified images (by basename or fullpath)
2368 const char *arg_cstr;
2369 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2370 {
2371 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002372
2373 ModuleList &target_modules = target->GetImages();
2374 Mutex::Locker modules_locker(target_modules.GetMutex());
2375 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002376 if (num_modules > 0)
2377 {
2378 uint32_t num_dumped = 0;
2379 for (uint32_t i = 0; i<num_modules; ++i)
2380 {
2381 if (DumpCompileUnitLineTable (m_interpreter,
2382 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002383 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002384 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002385 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002386 num_dumped++;
2387 }
2388 if (num_dumped == 0)
2389 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2390 else
2391 total_num_dumped += num_dumped;
2392 }
2393 }
2394 }
2395
2396 if (total_num_dumped > 0)
2397 result.SetStatus (eReturnStatusSuccessFinishResult);
2398 else
2399 {
2400 result.AppendError ("no source filenames matched any command arguments");
2401 result.SetStatus (eReturnStatusFailed);
2402 }
2403 }
2404 return result.Succeeded();
2405 }
2406};
2407
2408
2409#pragma mark CommandObjectTargetModulesDump
2410
2411//----------------------------------------------------------------------
2412// Dump multi-word command for target modules
2413//----------------------------------------------------------------------
2414
2415class CommandObjectTargetModulesDump : public CommandObjectMultiword
2416{
2417public:
2418
2419 //------------------------------------------------------------------
2420 // Constructors and Destructors
2421 //------------------------------------------------------------------
2422 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2423 CommandObjectMultiword (interpreter,
2424 "target modules dump",
2425 "A set of commands for dumping information about one or more target modules.",
2426 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2427 {
2428 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2429 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2430 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2431 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2432 }
2433
2434 virtual
2435 ~CommandObjectTargetModulesDump()
2436 {
2437 }
2438};
2439
Jim Inghamda26bd22012-06-08 21:56:10 +00002440class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002441{
2442public:
2443 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002444 CommandObjectParsed (interpreter,
2445 "target modules add",
2446 "Add a new module to the current target's modules.",
2447 "target modules add [<module>]")
Greg Claytone1f50b92011-05-03 22:09:39 +00002448 {
2449 }
2450
2451 virtual
2452 ~CommandObjectTargetModulesAdd ()
2453 {
2454 }
2455
Jim Inghamda26bd22012-06-08 21:56:10 +00002456 int
2457 HandleArgumentCompletion (Args &input,
2458 int &cursor_index,
2459 int &cursor_char_position,
2460 OptionElementVector &opt_element_vector,
2461 int match_start_point,
2462 int max_return_elements,
2463 bool &word_complete,
2464 StringList &matches)
2465 {
2466 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2467 completion_str.erase (cursor_char_position);
2468
2469 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2470 CommandCompletions::eDiskFileCompletion,
2471 completion_str.c_str(),
2472 match_start_point,
2473 max_return_elements,
2474 NULL,
2475 word_complete,
2476 matches);
2477 return matches.GetSize();
2478 }
2479
2480protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002481 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002482 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002483 CommandReturnObject &result)
2484 {
2485 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2486 if (target == NULL)
2487 {
2488 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2489 result.SetStatus (eReturnStatusFailed);
2490 return false;
2491 }
2492 else
2493 {
2494 const size_t argc = args.GetArgumentCount();
2495 if (argc == 0)
2496 {
2497 result.AppendError ("one or more executable image paths must be specified");
2498 result.SetStatus (eReturnStatusFailed);
2499 return false;
2500 }
2501 else
2502 {
2503 for (size_t i=0; i<argc; ++i)
2504 {
2505 const char *path = args.GetArgumentAtIndex(i);
2506 if (path)
2507 {
2508 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002509 if (file_spec.Exists())
2510 {
Greg Clayton444fe992012-02-26 05:51:37 +00002511 ModuleSpec module_spec (file_spec);
2512 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002513 if (!module_sp)
2514 {
2515 result.AppendError ("one or more executable image paths must be specified");
2516 result.SetStatus (eReturnStatusFailed);
2517 return false;
2518 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002519 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002520 }
2521 else
2522 {
2523 char resolved_path[PATH_MAX];
2524 result.SetStatus (eReturnStatusFailed);
2525 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2526 {
2527 if (strcmp (resolved_path, path) != 0)
2528 {
2529 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2530 break;
2531 }
2532 }
2533 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2534 break;
2535 }
2536 }
2537 }
2538 }
2539 }
2540 return result.Succeeded();
2541 }
2542
Greg Claytone1f50b92011-05-03 22:09:39 +00002543};
2544
2545class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2546{
2547public:
2548 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2549 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2550 "target modules load",
2551 "Set the load addresses for one or more sections in a target module.",
2552 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2553 m_option_group (interpreter),
2554 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2555 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)
2556 {
2557 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2558 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2559 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2560 m_option_group.Finalize();
2561 }
2562
2563 virtual
2564 ~CommandObjectTargetModulesLoad ()
2565 {
2566 }
2567
Jim Inghamda26bd22012-06-08 21:56:10 +00002568 virtual Options *
2569 GetOptions ()
2570 {
2571 return &m_option_group;
2572 }
2573
2574protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002575 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002576 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002577 CommandReturnObject &result)
2578 {
2579 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2580 if (target == NULL)
2581 {
2582 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2583 result.SetStatus (eReturnStatusFailed);
2584 return false;
2585 }
2586 else
2587 {
2588 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002589 ModuleSpec module_spec;
2590 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002591 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002592 {
2593 search_using_module_spec = true;
2594 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2595 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002596
2597 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002598 {
2599 search_using_module_spec = true;
2600 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2601 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002602
Greg Clayton444fe992012-02-26 05:51:37 +00002603 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002604 {
2605
2606 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002607 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002608
2609 char path[PATH_MAX];
2610 if (num_matches == 1)
2611 {
2612 Module *module = matching_modules.GetModulePointerAtIndex(0);
2613 if (module)
2614 {
2615 ObjectFile *objfile = module->GetObjectFile();
2616 if (objfile)
2617 {
2618 SectionList *section_list = objfile->GetSectionList();
2619 if (section_list)
2620 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002621 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002622 if (argc == 0)
2623 {
2624 if (m_slide_option.GetOptionValue().OptionWasSet())
2625 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002626 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2627 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002628 }
2629 else
2630 {
2631 result.AppendError ("one or more section name + load address pair must be specified");
2632 result.SetStatus (eReturnStatusFailed);
2633 return false;
2634 }
2635 }
2636 else
2637 {
2638 if (m_slide_option.GetOptionValue().OptionWasSet())
2639 {
2640 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2641 result.SetStatus (eReturnStatusFailed);
2642 return false;
2643 }
2644
2645 for (size_t i=0; i<argc; i += 2)
2646 {
2647 const char *sect_name = args.GetArgumentAtIndex(i);
2648 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2649 if (sect_name && load_addr_cstr)
2650 {
2651 ConstString const_sect_name(sect_name);
2652 bool success = false;
2653 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2654 if (success)
2655 {
2656 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2657 if (section_sp)
2658 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002659 if (section_sp->IsThreadSpecific())
2660 {
2661 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2662 result.SetStatus (eReturnStatusFailed);
2663 break;
2664 }
2665 else
2666 {
Greg Clayton545762f2012-07-07 01:24:12 +00002667 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002668 changed = true;
2669 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2670 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002671 }
2672 else
2673 {
2674 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2675 result.SetStatus (eReturnStatusFailed);
2676 break;
2677 }
2678 }
2679 else
2680 {
2681 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2682 result.SetStatus (eReturnStatusFailed);
2683 break;
2684 }
2685 }
2686 else
2687 {
2688 if (sect_name)
2689 result.AppendError ("section names must be followed by a load address.\n");
2690 else
2691 result.AppendError ("one or more section name + load address pair must be specified.\n");
2692 result.SetStatus (eReturnStatusFailed);
2693 break;
2694 }
2695 }
2696 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002697
2698 if (changed)
2699 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002700 }
2701 else
2702 {
2703 module->GetFileSpec().GetPath (path, sizeof(path));
2704 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2705 result.SetStatus (eReturnStatusFailed);
2706 }
2707 }
2708 else
2709 {
2710 module->GetFileSpec().GetPath (path, sizeof(path));
2711 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2712 result.SetStatus (eReturnStatusFailed);
2713 }
2714 }
2715 else
2716 {
2717 module->GetFileSpec().GetPath (path, sizeof(path));
2718 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2719 result.SetStatus (eReturnStatusFailed);
2720 }
2721 }
2722 else
2723 {
2724 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002725
2726 if (module_spec.GetFileSpec())
2727 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002728 else
2729 path[0] = '\0';
2730
Greg Clayton444fe992012-02-26 05:51:37 +00002731 if (module_spec.GetUUIDPtr())
2732 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002733 else
2734 uuid_cstr[0] = '\0';
2735 if (num_matches > 1)
2736 {
2737 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2738 path[0] ? " file=" : "",
2739 path,
2740 uuid_cstr[0] ? " uuid=" : "",
2741 uuid_cstr);
2742 for (size_t i=0; i<num_matches; ++i)
2743 {
2744 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2745 result.AppendMessageWithFormat("%s\n", path);
2746 }
2747 }
2748 else
2749 {
2750 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2751 path[0] ? " file=" : "",
2752 path,
2753 uuid_cstr[0] ? " uuid=" : "",
2754 uuid_cstr);
2755 }
2756 result.SetStatus (eReturnStatusFailed);
2757 }
2758 }
2759 else
2760 {
2761 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2762 result.SetStatus (eReturnStatusFailed);
2763 return false;
2764 }
2765 }
2766 return result.Succeeded();
2767 }
2768
Greg Claytone1f50b92011-05-03 22:09:39 +00002769 OptionGroupOptions m_option_group;
2770 OptionGroupUUID m_uuid_option_group;
2771 OptionGroupFile m_file_option;
2772 OptionGroupUInt64 m_slide_option;
2773};
2774
2775//----------------------------------------------------------------------
2776// List images with associated information
2777//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002778class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002779{
2780public:
2781
2782 class CommandOptions : public Options
2783 {
2784 public:
2785
2786 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002787 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002788 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002789 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002790 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002791 {
2792 }
2793
2794 virtual
2795 ~CommandOptions ()
2796 {
2797 }
2798
2799 virtual Error
2800 SetOptionValue (uint32_t option_idx, const char *option_arg)
2801 {
2802 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002803 if (short_option == 'g')
2804 {
2805 m_use_global_module_list = true;
2806 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002807 else if (short_option == 'a')
2808 {
2809 bool success;
2810 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2811 if (!success)
2812 {
2813 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002814 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002815 }
2816 }
Greg Clayton899025f2011-08-09 00:01:09 +00002817 else
2818 {
2819 uint32_t width = 0;
2820 if (option_arg)
2821 width = strtoul (option_arg, NULL, 0);
2822 m_format_array.push_back(std::make_pair(short_option, width));
2823 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002824 Error error;
2825 return error;
2826 }
2827
2828 void
2829 OptionParsingStarting ()
2830 {
2831 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002832 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002833 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002834 }
2835
2836 const OptionDefinition*
2837 GetDefinitions ()
2838 {
2839 return g_option_table;
2840 }
2841
2842 // Options table: Required for subclasses of Options.
2843
2844 static OptionDefinition g_option_table[];
2845
2846 // Instance variables to hold the values for command options.
2847 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2848 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002849 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002850 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002851 };
2852
2853 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002854 CommandObjectParsed (interpreter,
2855 "target modules list",
2856 "List current executable and dependent shared library images.",
2857 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002858 m_options (interpreter)
2859 {
2860 }
2861
2862 virtual
2863 ~CommandObjectTargetModulesList ()
2864 {
2865 }
2866
2867 virtual
2868 Options *
2869 GetOptions ()
2870 {
2871 return &m_options;
2872 }
2873
Jim Inghamda26bd22012-06-08 21:56:10 +00002874protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002875 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002876 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002877 CommandReturnObject &result)
2878 {
2879 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002880 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00002881 // Define a local module list here to ensure it lives longer than any "locker"
2882 // object which might lock its contents below (through the "module_list_ptr"
2883 // variable).
2884 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00002885 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002886 {
2887 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2888 result.SetStatus (eReturnStatusFailed);
2889 return false;
2890 }
2891 else
2892 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002893 if (target)
2894 {
2895 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2896 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2897 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2898 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002899 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002900 Stream &strm = result.GetOutputStream();
2901
2902 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2903 {
2904 if (target)
2905 {
2906 Address module_address;
2907 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2908 {
Greg Clayton3508c382012-02-24 01:59:29 +00002909 ModuleSP module_sp (module_address.GetModule());
2910 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002911 {
Greg Clayton3508c382012-02-24 01:59:29 +00002912 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002913 result.SetStatus (eReturnStatusSuccessFinishResult);
2914 }
2915 else
2916 {
2917 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2918 result.SetStatus (eReturnStatusFailed);
2919 }
2920 }
2921 else
2922 {
2923 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2924 result.SetStatus (eReturnStatusFailed);
2925 }
2926 }
2927 else
2928 {
2929 result.AppendError ("Can only look up modules by address with a valid target.");
2930 result.SetStatus (eReturnStatusFailed);
2931 }
2932 return result.Succeeded();
2933 }
2934
Jim Ingham93367902012-05-30 02:19:25 +00002935 uint32_t num_modules = 0;
2936 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
2937 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
2938 // the global module list directly.
Greg Clayton2ad894b2012-05-15 18:43:44 +00002939 ModuleList *module_list_ptr = NULL;
2940 const size_t argc = command.GetArgumentCount();
2941 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00002942 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002943 if (use_global_module_list)
2944 {
2945 locker.Lock (Module::GetAllocationModuleCollectionMutex());
2946 num_modules = Module::GetNumberAllocatedModules();
2947 }
2948 else
2949 {
2950 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00002951 }
Greg Clayton899025f2011-08-09 00:01:09 +00002952 }
2953 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002954 {
2955 for (size_t i=0; i<argc; ++i)
2956 {
2957 // Dump specified images (by basename or fullpath)
2958 const char *arg_cstr = command.GetArgumentAtIndex(i);
2959 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2960 if (num_matches == 0)
2961 {
2962 if (argc == 1)
2963 {
2964 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
2965 result.SetStatus (eReturnStatusFailed);
2966 return false;
2967 }
2968 }
2969 }
2970
Greg Clayton2ad894b2012-05-15 18:43:44 +00002971 module_list_ptr = &module_list;
2972 }
Jim Ingham93367902012-05-30 02:19:25 +00002973
2974 if (module_list_ptr != NULL)
2975 {
2976 locker.Lock(module_list_ptr->GetMutex());
2977 num_modules = module_list_ptr->GetSize();
2978 }
Greg Clayton899025f2011-08-09 00:01:09 +00002979
Greg Claytone1f50b92011-05-03 22:09:39 +00002980 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002981 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002982 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2983 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002984 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002985 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00002986 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00002987 {
Jim Ingham93367902012-05-30 02:19:25 +00002988 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00002989 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002990 }
2991 else
2992 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002993 module = Module::GetAllocatedModuleAtIndex(image_idx);
2994 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00002995 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002996
Greg Claytonb5a8f142012-02-05 02:38:54 +00002997 int indent = strm.Printf("[%3u] ", image_idx);
2998 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00002999
Greg Claytone1f50b92011-05-03 22:09:39 +00003000 }
3001 result.SetStatus (eReturnStatusSuccessFinishResult);
3002 }
3003 else
3004 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003005 if (argc)
3006 {
3007 if (use_global_module_list)
3008 result.AppendError ("the global module list has no matching modules");
3009 else
3010 result.AppendError ("the target has no matching modules");
3011 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003012 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003013 {
3014 if (use_global_module_list)
3015 result.AppendError ("the global module list is empty");
3016 else
3017 result.AppendError ("the target has no associated executable images");
3018 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003019 result.SetStatus (eReturnStatusFailed);
3020 return false;
3021 }
3022 }
3023 return result.Succeeded();
3024 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003025
3026 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003027 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003028 {
3029
3030 bool dump_object_name = false;
3031 if (m_options.m_format_array.empty())
3032 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003033 m_options.m_format_array.push_back(std::make_pair('u', 0));
3034 m_options.m_format_array.push_back(std::make_pair('h', 0));
3035 m_options.m_format_array.push_back(std::make_pair('f', 0));
3036 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003037 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003038 const size_t num_entries = m_options.m_format_array.size();
3039 bool print_space = false;
3040 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003041 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003042 if (print_space)
3043 strm.PutChar(' ');
3044 print_space = true;
3045 const char format_char = m_options.m_format_array[i].first;
3046 uint32_t width = m_options.m_format_array[i].second;
3047 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003048 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003049 case 'A':
3050 DumpModuleArchitecture (strm, module, false, width);
3051 break;
3052
3053 case 't':
3054 DumpModuleArchitecture (strm, module, true, width);
3055 break;
3056
3057 case 'f':
3058 DumpFullpath (strm, &module->GetFileSpec(), width);
3059 dump_object_name = true;
3060 break;
3061
3062 case 'd':
3063 DumpDirectory (strm, &module->GetFileSpec(), width);
3064 break;
3065
3066 case 'b':
3067 DumpBasename (strm, &module->GetFileSpec(), width);
3068 dump_object_name = true;
3069 break;
3070
3071 case 'h':
3072 case 'o':
3073 // Image header address
3074 {
3075 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003076
Greg Claytonb5a8f142012-02-05 02:38:54 +00003077 ObjectFile *objfile = module->GetObjectFile ();
3078 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003079 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003080 Address header_addr(objfile->GetHeaderAddress());
3081 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003082 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003083 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003084 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003085 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3086 if (header_load_addr == LLDB_INVALID_ADDRESS)
3087 {
3088 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3089 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003090 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003091 {
3092 if (format_char == 'o')
3093 {
3094 // Show the offset of slide for the image
3095 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
3096 }
3097 else
3098 {
3099 // Show the load address of the image
3100 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
3101 }
3102 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003103 break;
3104 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003105 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3106 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3107 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003108 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003109 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003110 strm.Printf ("%*s", addr_nibble_width + 2, "");
3111 }
3112 break;
3113 case 'r':
3114 {
3115 uint32_t ref_count = 0;
3116 ModuleSP module_sp (module->shared_from_this());
3117 if (module_sp)
3118 {
3119 // Take one away to make sure we don't count our local "module_sp"
3120 ref_count = module_sp.use_count() - 1;
3121 }
3122 if (width)
3123 strm.Printf("{%*u}", width, ref_count);
3124 else
3125 strm.Printf("{%u}", ref_count);
3126 }
3127 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003128
Greg Claytonb5a8f142012-02-05 02:38:54 +00003129 case 's':
3130 case 'S':
3131 {
3132 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3133 if (symbol_vendor)
3134 {
3135 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3136 if (symbol_file)
3137 {
3138 if (format_char == 'S')
3139 {
3140 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3141 // Dump symbol file only if different from module file
3142 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3143 {
3144 print_space = false;
3145 break;
3146 }
3147 // Add a newline and indent past the index
3148 strm.Printf ("\n%*s", indent, "");
3149 }
3150 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3151 dump_object_name = true;
3152 break;
3153 }
3154 }
3155 strm.Printf("%.*s", width, "<NONE>");
3156 }
3157 break;
3158
3159 case 'm':
3160 module->GetModificationTime().Dump(&strm, width);
3161 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003162
Greg Claytonb5a8f142012-02-05 02:38:54 +00003163 case 'p':
3164 strm.Printf("%p", module);
3165 break;
3166
3167 case 'u':
3168 DumpModuleUUID(strm, module);
3169 break;
3170
3171 default:
3172 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003173 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003174
3175 }
3176 if (dump_object_name)
3177 {
3178 const char *object_name = module->GetObjectName().GetCString();
3179 if (object_name)
3180 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003181 }
3182 strm.EOL();
3183 }
3184
Greg Claytone1f50b92011-05-03 22:09:39 +00003185 CommandOptions m_options;
3186};
3187
3188OptionDefinition
3189CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3190{
Jim Ingham6bdea822011-10-24 18:36:33 +00003191 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3192 { 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 +00003193 { 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 +00003194 { 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."},
3195 { 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 +00003196 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3197 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3198 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3199 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3200 { 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 +00003201 { 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 +00003202 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3203 { 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."},
3204 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003205 { 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 +00003206 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3207};
3208
3209
3210
3211//----------------------------------------------------------------------
3212// Lookup information in images
3213//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003214class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003215{
3216public:
3217
3218 enum
3219 {
3220 eLookupTypeInvalid = -1,
3221 eLookupTypeAddress = 0,
3222 eLookupTypeSymbol,
3223 eLookupTypeFileLine, // Line is optional
3224 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003225 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003226 eLookupTypeType,
3227 kNumLookupTypes
3228 };
3229
3230 class CommandOptions : public Options
3231 {
3232 public:
3233
3234 CommandOptions (CommandInterpreter &interpreter) :
3235 Options(interpreter)
3236 {
3237 OptionParsingStarting();
3238 }
3239
3240 virtual
3241 ~CommandOptions ()
3242 {
3243 }
3244
3245 virtual Error
3246 SetOptionValue (uint32_t option_idx, const char *option_arg)
3247 {
3248 Error error;
3249
3250 char short_option = (char) m_getopt_table[option_idx].val;
3251
3252 switch (short_option)
3253 {
3254 case 'a':
3255 m_type = eLookupTypeAddress;
3256 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3257 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003258 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003259 break;
3260
3261 case 'o':
3262 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3263 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003264 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003265 break;
3266
3267 case 's':
3268 m_str = option_arg;
3269 m_type = eLookupTypeSymbol;
3270 break;
3271
3272 case 'f':
3273 m_file.SetFile (option_arg, false);
3274 m_type = eLookupTypeFileLine;
3275 break;
3276
3277 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003278 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003279 break;
3280
3281 case 'l':
3282 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3283 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003284 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003285 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003286 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003287 m_type = eLookupTypeFileLine;
3288 break;
3289
Greg Clayton2ad894b2012-05-15 18:43:44 +00003290 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003291 m_str = option_arg;
3292 m_type = eLookupTypeFunction;
3293 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003294
3295 case 'n':
3296 m_str = option_arg;
3297 m_type = eLookupTypeFunctionOrSymbol;
3298 break;
3299
Greg Claytone1f50b92011-05-03 22:09:39 +00003300 case 't':
3301 m_str = option_arg;
3302 m_type = eLookupTypeType;
3303 break;
3304
3305 case 'v':
3306 m_verbose = 1;
3307 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003308
3309 case 'A':
3310 m_print_all = true;
3311 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003312
3313 case 'r':
3314 m_use_regex = true;
3315 break;
3316 }
3317
3318 return error;
3319 }
3320
3321 void
3322 OptionParsingStarting ()
3323 {
3324 m_type = eLookupTypeInvalid;
3325 m_str.clear();
3326 m_file.Clear();
3327 m_addr = LLDB_INVALID_ADDRESS;
3328 m_offset = 0;
3329 m_line_number = 0;
3330 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003331 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003332 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003333 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003334 }
3335
3336 const OptionDefinition*
3337 GetDefinitions ()
3338 {
3339 return g_option_table;
3340 }
3341
3342 // Options table: Required for subclasses of Options.
3343
3344 static OptionDefinition g_option_table[];
3345 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3346 std::string m_str; // Holds name lookup
3347 FileSpec m_file; // Files for file lookups
3348 lldb::addr_t m_addr; // Holds the address to lookup
3349 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3350 uint32_t m_line_number; // Line number for file+line lookups
3351 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003352 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003353 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003354 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003355
3356 };
3357
3358 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003359 CommandObjectParsed (interpreter,
3360 "target modules lookup",
3361 "Look up information within executable and dependent shared library images.",
3362 NULL),
3363 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003364 {
3365 CommandArgumentEntry arg;
3366 CommandArgumentData file_arg;
3367
3368 // Define the first (and only) variant of this arg.
3369 file_arg.arg_type = eArgTypeFilename;
3370 file_arg.arg_repetition = eArgRepeatStar;
3371
3372 // There is only one variant this argument could be; put it into the argument entry.
3373 arg.push_back (file_arg);
3374
3375 // Push the data for the first argument into the m_arguments vector.
3376 m_arguments.push_back (arg);
3377 }
3378
3379 virtual
3380 ~CommandObjectTargetModulesLookup ()
3381 {
3382 }
3383
3384 virtual Options *
3385 GetOptions ()
3386 {
3387 return &m_options;
3388 }
3389
Sean Callanan56d31ec2012-06-06 20:49:55 +00003390 bool
3391 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3392 {
3393 switch (m_options.m_type)
3394 {
3395 case eLookupTypeAddress:
3396 case eLookupTypeFileLine:
3397 case eLookupTypeFunction:
3398 case eLookupTypeFunctionOrSymbol:
3399 case eLookupTypeSymbol:
3400 default:
3401 return false;
3402 case eLookupTypeType:
3403 break;
3404 }
3405
3406 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3407
3408 StackFrameSP frame = exe_ctx.GetFrameSP();
3409
3410 if (!frame)
3411 return false;
3412
3413 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3414
3415 if (!sym_ctx.module_sp)
3416 return false;
3417
3418 switch (m_options.m_type)
3419 {
3420 default:
3421 return false;
3422 case eLookupTypeType:
3423 if (!m_options.m_str.empty())
3424 {
3425 if (LookupTypeHere (m_interpreter,
3426 result.GetOutputStream(),
3427 sym_ctx,
3428 m_options.m_str.c_str(),
3429 m_options.m_use_regex))
3430 {
3431 result.SetStatus(eReturnStatusSuccessFinishResult);
3432 return true;
3433 }
3434 }
3435 break;
3436 }
3437
3438 return true;
3439 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003440
3441 bool
3442 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3443 {
3444 switch (m_options.m_type)
3445 {
3446 case eLookupTypeAddress:
3447 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3448 {
3449 if (LookupAddressInModule (m_interpreter,
3450 result.GetOutputStream(),
3451 module,
3452 eSymbolContextEverything,
3453 m_options.m_addr,
3454 m_options.m_offset,
3455 m_options.m_verbose))
3456 {
3457 result.SetStatus(eReturnStatusSuccessFinishResult);
3458 return true;
3459 }
3460 }
3461 break;
3462
3463 case eLookupTypeSymbol:
3464 if (!m_options.m_str.empty())
3465 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003466 if (LookupSymbolInModule (m_interpreter,
3467 result.GetOutputStream(),
3468 module,
3469 m_options.m_str.c_str(),
3470 m_options.m_use_regex,
3471 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003472 {
3473 result.SetStatus(eReturnStatusSuccessFinishResult);
3474 return true;
3475 }
3476 }
3477 break;
3478
3479 case eLookupTypeFileLine:
3480 if (m_options.m_file)
3481 {
3482
3483 if (LookupFileAndLineInModule (m_interpreter,
3484 result.GetOutputStream(),
3485 module,
3486 m_options.m_file,
3487 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003488 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003489 m_options.m_verbose))
3490 {
3491 result.SetStatus(eReturnStatusSuccessFinishResult);
3492 return true;
3493 }
3494 }
3495 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003496
3497 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003498 case eLookupTypeFunction:
3499 if (!m_options.m_str.empty())
3500 {
3501 if (LookupFunctionInModule (m_interpreter,
3502 result.GetOutputStream(),
3503 module,
3504 m_options.m_str.c_str(),
3505 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003506 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003507 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003508 m_options.m_verbose))
3509 {
3510 result.SetStatus(eReturnStatusSuccessFinishResult);
3511 return true;
3512 }
3513 }
3514 break;
3515
Greg Clayton2ad894b2012-05-15 18:43:44 +00003516
Greg Claytone1f50b92011-05-03 22:09:39 +00003517 case eLookupTypeType:
3518 if (!m_options.m_str.empty())
3519 {
3520 if (LookupTypeInModule (m_interpreter,
3521 result.GetOutputStream(),
3522 module,
3523 m_options.m_str.c_str(),
3524 m_options.m_use_regex))
3525 {
3526 result.SetStatus(eReturnStatusSuccessFinishResult);
3527 return true;
3528 }
3529 }
3530 break;
3531
3532 default:
3533 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3534 syntax_error = true;
3535 break;
3536 }
3537
3538 result.SetStatus (eReturnStatusFailed);
3539 return false;
3540 }
3541
Jim Inghamda26bd22012-06-08 21:56:10 +00003542protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003543 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003544 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003545 CommandReturnObject &result)
3546 {
3547 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3548 if (target == NULL)
3549 {
3550 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3551 result.SetStatus (eReturnStatusFailed);
3552 return false;
3553 }
3554 else
3555 {
3556 bool syntax_error = false;
3557 uint32_t i;
3558 uint32_t num_successful_lookups = 0;
3559 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3560 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3561 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3562 // Dump all sections for all modules images
3563
3564 if (command.GetArgumentCount() == 0)
3565 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003566 ModuleSP current_module;
3567
3568 // Where it is possible to look in the current symbol context
3569 // first, try that. If this search was successful and --all
3570 // was not passed, don't print anything else.
3571 if (LookupHere (m_interpreter, result, syntax_error))
3572 {
3573 result.GetOutputStream().EOL();
3574 num_successful_lookups++;
3575 if (!m_options.m_print_all)
3576 {
3577 result.SetStatus (eReturnStatusSuccessFinishResult);
3578 return result.Succeeded();
3579 }
3580 }
3581
3582 // Dump all sections for all other modules
3583
Jim Ingham93367902012-05-30 02:19:25 +00003584 ModuleList &target_modules = target->GetImages();
3585 Mutex::Locker modules_locker(target_modules.GetMutex());
3586 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003587 if (num_modules > 0)
3588 {
3589 for (i = 0; i<num_modules && syntax_error == false; ++i)
3590 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003591 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3592
3593 if (module_pointer != current_module.get() &&
3594 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003595 {
3596 result.GetOutputStream().EOL();
3597 num_successful_lookups++;
3598 }
3599 }
3600 }
3601 else
3602 {
3603 result.AppendError ("the target has no associated executable images");
3604 result.SetStatus (eReturnStatusFailed);
3605 return false;
3606 }
3607 }
3608 else
3609 {
3610 // Dump specified images (by basename or fullpath)
3611 const char *arg_cstr;
3612 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3613 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003614 ModuleList module_list;
3615 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3616 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003617 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003618 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003619 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003620 Module *module = module_list.GetModulePointerAtIndex(i);
3621 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003622 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003623 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003624 {
3625 result.GetOutputStream().EOL();
3626 num_successful_lookups++;
3627 }
3628 }
3629 }
3630 }
3631 else
3632 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3633 }
3634 }
3635
3636 if (num_successful_lookups > 0)
3637 result.SetStatus (eReturnStatusSuccessFinishResult);
3638 else
3639 result.SetStatus (eReturnStatusFailed);
3640 }
3641 return result.Succeeded();
3642 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003643
3644 CommandOptions m_options;
3645};
3646
3647OptionDefinition
3648CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3649{
3650 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3651 { 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 +00003652 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3653 /* 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 +00003654 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003655 { 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 +00003656 { 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."},
3657 { 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 +00003658 { LLDB_OPT_SET_FROM_TO(3,5),
3659 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 +00003660 { 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."},
3661 { 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."},
3662 { 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 +00003663 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
Sean Callanan56d31ec2012-06-06 20:49:55 +00003664 { 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 +00003665 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003666};
Chris Lattner24943d22010-06-08 16:52:24 +00003667
3668
Jim Inghamd60d94a2011-03-11 03:53:59 +00003669#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003670
3671//-------------------------------------------------------------------------
3672// CommandObjectMultiwordImageSearchPaths
3673//-------------------------------------------------------------------------
3674
Greg Claytone1f50b92011-05-03 22:09:39 +00003675class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003676{
3677public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003678
3679 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3680 CommandObjectMultiword (interpreter,
3681 "target modules search-paths",
3682 "A set of commands for operating on debugger target image search paths.",
3683 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003684 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003685 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3686 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3687 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3688 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3689 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003690 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003691
3692 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003693 {
3694 }
3695};
3696
Greg Claytone1f50b92011-05-03 22:09:39 +00003697
3698
3699#pragma mark CommandObjectTargetModules
3700
3701//-------------------------------------------------------------------------
3702// CommandObjectTargetModules
3703//-------------------------------------------------------------------------
3704
3705class CommandObjectTargetModules : public CommandObjectMultiword
3706{
3707public:
3708 //------------------------------------------------------------------
3709 // Constructors and Destructors
3710 //------------------------------------------------------------------
3711 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3712 CommandObjectMultiword (interpreter,
3713 "target modules",
3714 "A set of commands for accessing information for one or more target modules.",
3715 "target modules <sub-command> ...")
3716 {
3717 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3718 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3719 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3720 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3721 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3722 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3723 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
3724
3725 }
3726 virtual
3727 ~CommandObjectTargetModules()
3728 {
3729 }
3730
3731private:
3732 //------------------------------------------------------------------
3733 // For CommandObjectTargetModules only
3734 //------------------------------------------------------------------
3735 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3736};
3737
3738
Greg Clayton3508c382012-02-24 01:59:29 +00003739
Jim Inghamda26bd22012-06-08 21:56:10 +00003740class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00003741{
3742public:
3743 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003744 CommandObjectParsed (interpreter,
3745 "target symbols add",
3746 "Add a debug symbol file to one of the target's current modules.",
3747 "target symbols add [<symfile>]")
Greg Clayton3508c382012-02-24 01:59:29 +00003748 {
3749 }
3750
3751 virtual
3752 ~CommandObjectTargetSymbolsAdd ()
3753 {
3754 }
3755
Jim Inghamda26bd22012-06-08 21:56:10 +00003756 int
3757 HandleArgumentCompletion (Args &input,
3758 int &cursor_index,
3759 int &cursor_char_position,
3760 OptionElementVector &opt_element_vector,
3761 int match_start_point,
3762 int max_return_elements,
3763 bool &word_complete,
3764 StringList &matches)
3765 {
3766 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
3767 completion_str.erase (cursor_char_position);
3768
3769 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
3770 CommandCompletions::eDiskFileCompletion,
3771 completion_str.c_str(),
3772 match_start_point,
3773 max_return_elements,
3774 NULL,
3775 word_complete,
3776 matches);
3777 return matches.GetSize();
3778 }
3779
3780protected:
Greg Clayton3508c382012-02-24 01:59:29 +00003781 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003782 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00003783 CommandReturnObject &result)
3784 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003785 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3786 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton3508c382012-02-24 01:59:29 +00003787 if (target == NULL)
3788 {
3789 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3790 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003791 }
3792 else
3793 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00003794 bool flush = false;
Greg Clayton3508c382012-02-24 01:59:29 +00003795 const size_t argc = args.GetArgumentCount();
3796 if (argc == 0)
3797 {
3798 result.AppendError ("one or more symbol file paths must be specified");
3799 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00003800 }
3801 else
3802 {
3803 for (size_t i=0; i<argc; ++i)
3804 {
3805 const char *symfile_path = args.GetArgumentAtIndex(i);
3806 if (symfile_path)
3807 {
3808 FileSpec symfile_spec(symfile_path, true);
3809 ArchSpec arch;
3810 if (symfile_spec.Exists())
3811 {
3812 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
3813 if (symfile_module_sp)
3814 {
3815 // We now have a module that represents a symbol file
3816 // that can be used for a module that might exist in the
3817 // current target, so we need to find that module in the
3818 // target
3819
3820 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_module_sp->GetUUID()));
3821 if (old_module_sp)
3822 {
Greg Claytond4f16c82012-03-29 21:43:25 +00003823 // The module has not yet created its symbol vendor, we can just
3824 // give the existing target module the symfile path to use for
3825 // when it decides to create it!
3826 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
Greg Clayton3508c382012-02-24 01:59:29 +00003827
Greg Claytond4f16c82012-03-29 21:43:25 +00003828 // Let clients know something changed in the module
3829 // if it is currently loaded
3830 ModuleList module_list;
3831 module_list.Append (old_module_sp);
3832 target->ModulesDidLoad (module_list);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003833 flush = true;
Greg Clayton3508c382012-02-24 01:59:29 +00003834 }
3835 }
3836 else
3837 {
3838 result.AppendError ("one or more executable image paths must be specified");
3839 result.SetStatus (eReturnStatusFailed);
Greg Claytoncf5927e2012-05-18 02:38:05 +00003840 break;
Greg Clayton3508c382012-02-24 01:59:29 +00003841 }
3842 result.SetStatus (eReturnStatusSuccessFinishResult);
3843 }
3844 else
3845 {
3846 char resolved_symfile_path[PATH_MAX];
3847 result.SetStatus (eReturnStatusFailed);
3848 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
3849 {
3850 if (strcmp (resolved_symfile_path, symfile_path) != 0)
3851 {
3852 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
3853 break;
3854 }
3855 }
3856 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
3857 break;
3858 }
3859 }
3860 }
3861 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00003862
3863 if (flush)
3864 {
3865 Process *process = exe_ctx.GetProcessPtr();
3866 if (process)
3867 process->Flush();
3868 }
Greg Clayton3508c382012-02-24 01:59:29 +00003869 }
3870 return result.Succeeded();
3871 }
3872
Greg Clayton3508c382012-02-24 01:59:29 +00003873};
3874
3875
3876#pragma mark CommandObjectTargetSymbols
3877
3878//-------------------------------------------------------------------------
3879// CommandObjectTargetSymbols
3880//-------------------------------------------------------------------------
3881
3882class CommandObjectTargetSymbols : public CommandObjectMultiword
3883{
3884public:
3885 //------------------------------------------------------------------
3886 // Constructors and Destructors
3887 //------------------------------------------------------------------
3888 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
3889 CommandObjectMultiword (interpreter,
3890 "target symbols",
3891 "A set of commands for adding and managing debug symbol files.",
3892 "target symbols <sub-command> ...")
3893 {
3894 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
3895
3896 }
3897 virtual
3898 ~CommandObjectTargetSymbols()
3899 {
3900 }
3901
3902private:
3903 //------------------------------------------------------------------
3904 // For CommandObjectTargetModules only
3905 //------------------------------------------------------------------
3906 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
3907};
3908
3909
Jim Inghamd60d94a2011-03-11 03:53:59 +00003910#pragma mark CommandObjectTargetStopHookAdd
3911
3912//-------------------------------------------------------------------------
3913// CommandObjectTargetStopHookAdd
3914//-------------------------------------------------------------------------
3915
Jim Inghamda26bd22012-06-08 21:56:10 +00003916class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00003917{
3918public:
3919
3920 class CommandOptions : public Options
3921 {
3922 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00003923 CommandOptions (CommandInterpreter &interpreter) :
3924 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00003925 m_line_start(0),
3926 m_line_end (UINT_MAX),
3927 m_func_name_type_mask (eFunctionNameTypeAuto),
3928 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00003929 m_thread_specified (false),
3930 m_use_one_liner (false),
3931 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00003932 {
3933 }
3934
3935 ~CommandOptions () {}
3936
Greg Claytonb3448432011-03-24 21:19:54 +00003937 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00003938 GetDefinitions ()
3939 {
3940 return g_option_table;
3941 }
3942
3943 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00003944 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00003945 {
3946 Error error;
3947 char short_option = (char) m_getopt_table[option_idx].val;
3948 bool success;
3949
3950 switch (short_option)
3951 {
3952 case 'c':
3953 m_class_name = option_arg;
3954 m_sym_ctx_specified = true;
3955 break;
3956
3957 case 'e':
3958 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
3959 if (!success)
3960 {
Greg Clayton9c236732011-10-26 00:56:27 +00003961 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003962 break;
3963 }
3964 m_sym_ctx_specified = true;
3965 break;
3966
3967 case 'l':
3968 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
3969 if (!success)
3970 {
Greg Clayton9c236732011-10-26 00:56:27 +00003971 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003972 break;
3973 }
3974 m_sym_ctx_specified = true;
3975 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00003976
3977 case 'i':
3978 m_no_inlines = true;
3979 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00003980
3981 case 'n':
3982 m_function_name = option_arg;
3983 m_func_name_type_mask |= eFunctionNameTypeAuto;
3984 m_sym_ctx_specified = true;
3985 break;
3986
3987 case 'f':
3988 m_file_name = option_arg;
3989 m_sym_ctx_specified = true;
3990 break;
3991 case 's':
3992 m_module_name = option_arg;
3993 m_sym_ctx_specified = true;
3994 break;
3995 case 't' :
3996 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00003997 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00003998 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00003999 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004000 m_thread_specified = true;
4001 }
4002 break;
4003 case 'T':
4004 m_thread_name = option_arg;
4005 m_thread_specified = true;
4006 break;
4007 case 'q':
4008 m_queue_name = option_arg;
4009 m_thread_specified = true;
4010 break;
4011 case 'x':
4012 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004013 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004014 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004015 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004016 m_thread_specified = true;
4017 }
4018 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004019 case 'o':
4020 m_use_one_liner = true;
4021 m_one_liner = option_arg;
4022 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004023 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004024 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004025 break;
4026 }
4027 return error;
4028 }
4029
4030 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004031 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004032 {
4033 m_class_name.clear();
4034 m_function_name.clear();
4035 m_line_start = 0;
4036 m_line_end = UINT_MAX;
4037 m_file_name.clear();
4038 m_module_name.clear();
4039 m_func_name_type_mask = eFunctionNameTypeAuto;
4040 m_thread_id = LLDB_INVALID_THREAD_ID;
4041 m_thread_index = UINT32_MAX;
4042 m_thread_name.clear();
4043 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004044
4045 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004046 m_sym_ctx_specified = false;
4047 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004048
4049 m_use_one_liner = false;
4050 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004051 }
4052
4053
Greg Claytonb3448432011-03-24 21:19:54 +00004054 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004055
4056 std::string m_class_name;
4057 std::string m_function_name;
4058 uint32_t m_line_start;
4059 uint32_t m_line_end;
4060 std::string m_file_name;
4061 std::string m_module_name;
4062 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4063 lldb::tid_t m_thread_id;
4064 uint32_t m_thread_index;
4065 std::string m_thread_name;
4066 std::string m_queue_name;
4067 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004068 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004069 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004070 // Instance variables to hold the values for one_liner options.
4071 bool m_use_one_liner;
4072 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004073 };
4074
4075 Options *
4076 GetOptions ()
4077 {
4078 return &m_options;
4079 }
4080
4081 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004082 CommandObjectParsed (interpreter,
4083 "target stop-hook add ",
4084 "Add a hook to be executed when the target stops.",
4085 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004086 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004087 {
4088 }
4089
4090 ~CommandObjectTargetStopHookAdd ()
4091 {
4092 }
4093
4094 static size_t
4095 ReadCommandsCallbackFunction (void *baton,
4096 InputReader &reader,
4097 lldb::InputReaderAction notification,
4098 const char *bytes,
4099 size_t bytes_len)
4100 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004101 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004102 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004103 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004104 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004105
4106 switch (notification)
4107 {
4108 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004109 if (!batch_mode)
4110 {
4111 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4112 if (reader.GetPrompt())
4113 out_stream->Printf ("%s", reader.GetPrompt());
4114 out_stream->Flush();
4115 }
Jim Inghame15511a2011-05-05 01:03:36 +00004116 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004117 break;
4118
4119 case eInputReaderDeactivate:
4120 break;
4121
4122 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004123 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004124 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004125 out_stream->Printf ("%s", reader.GetPrompt());
4126 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004127 }
Jim Inghame15511a2011-05-05 01:03:36 +00004128 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004129 break;
4130
Caroline Tice4a348082011-05-02 20:41:46 +00004131 case eInputReaderAsynchronousOutputWritten:
4132 break;
4133
Jim Inghamd60d94a2011-03-11 03:53:59 +00004134 case eInputReaderGotToken:
4135 if (bytes && bytes_len && baton)
4136 {
4137 StringList *commands = new_stop_hook->GetCommandPointer();
4138 if (commands)
4139 {
4140 commands->AppendString (bytes, bytes_len);
4141 }
4142 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004143 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004144 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004145 out_stream->Printf ("%s", reader.GetPrompt());
4146 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004147 }
4148 break;
4149
4150 case eInputReaderInterrupt:
4151 {
4152 // Finish, and cancel the stop hook.
4153 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004154 if (!batch_mode)
4155 {
4156 out_stream->Printf ("Stop hook cancelled.\n");
4157 out_stream->Flush();
4158 }
4159
Jim Inghamd60d94a2011-03-11 03:53:59 +00004160 reader.SetIsDone (true);
4161 }
Jim Inghame15511a2011-05-05 01:03:36 +00004162 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004163 break;
4164
4165 case eInputReaderEndOfFile:
4166 reader.SetIsDone (true);
4167 break;
4168
4169 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004170 if (!got_interrupted && !batch_mode)
4171 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004172 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004173 out_stream->Flush();
4174 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004175 break;
4176 }
4177
4178 return bytes_len;
4179 }
4180
Jim Inghamda26bd22012-06-08 21:56:10 +00004181protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004182 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004183 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004184 {
4185 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4186 if (target)
4187 {
4188 Target::StopHookSP new_hook_sp;
4189 target->AddStopHook (new_hook_sp);
4190
4191 // First step, make the specifier.
4192 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4193 if (m_options.m_sym_ctx_specified)
4194 {
4195 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4196
4197 if (!m_options.m_module_name.empty())
4198 {
4199 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4200 }
4201
4202 if (!m_options.m_class_name.empty())
4203 {
4204 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4205 }
4206
4207 if (!m_options.m_file_name.empty())
4208 {
4209 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4210 }
4211
4212 if (m_options.m_line_start != 0)
4213 {
4214 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4215 }
4216
4217 if (m_options.m_line_end != UINT_MAX)
4218 {
4219 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4220 }
4221
4222 if (!m_options.m_function_name.empty())
4223 {
4224 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4225 }
4226 }
4227
4228 if (specifier_ap.get())
4229 new_hook_sp->SetSpecifier (specifier_ap.release());
4230
4231 // Next see if any of the thread options have been entered:
4232
4233 if (m_options.m_thread_specified)
4234 {
4235 ThreadSpec *thread_spec = new ThreadSpec();
4236
4237 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4238 {
4239 thread_spec->SetTID (m_options.m_thread_id);
4240 }
4241
4242 if (m_options.m_thread_index != UINT32_MAX)
4243 thread_spec->SetIndex (m_options.m_thread_index);
4244
4245 if (!m_options.m_thread_name.empty())
4246 thread_spec->SetName (m_options.m_thread_name.c_str());
4247
4248 if (!m_options.m_queue_name.empty())
4249 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4250
4251 new_hook_sp->SetThreadSpecifier (thread_spec);
4252
4253 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004254 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004255 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004256 // Use one-liner.
4257 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004258 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004259 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004260 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004261 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004262 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4263 // the new stop hook's command string.
4264 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4265 if (!reader_sp)
4266 {
4267 result.AppendError("out of memory\n");
4268 result.SetStatus (eReturnStatusFailed);
4269 target->RemoveStopHookByID (new_hook_sp->GetID());
4270 return false;
4271 }
4272
4273 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4274 new_hook_sp.get(), // baton
4275 eInputReaderGranularityLine, // token size, to pass to callback function
4276 "DONE", // end token
4277 "> ", // prompt
4278 true)); // echo input
4279 if (!err.Success())
4280 {
4281 result.AppendError (err.AsCString());
4282 result.SetStatus (eReturnStatusFailed);
4283 target->RemoveStopHookByID (new_hook_sp->GetID());
4284 return false;
4285 }
4286 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004287 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004288 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4289 }
4290 else
4291 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004292 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004293 result.SetStatus (eReturnStatusFailed);
4294 }
4295
4296 return result.Succeeded();
4297 }
4298private:
4299 CommandOptions m_options;
4300};
4301
Greg Claytonb3448432011-03-24 21:19:54 +00004302OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004303CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4304{
Johnny Chen60fe60e2011-05-02 23:47:55 +00004305 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
4306 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004307 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4308 "Set the module within which the stop-hook is to be run."},
4309 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
4310 "The stop hook is run only for the thread whose index matches this argument."},
4311 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
4312 "The stop hook is run only for the thread whose TID matches this argument."},
4313 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
4314 "The stop hook is run only for the thread whose thread name matches this argument."},
4315 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
4316 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4317 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4318 "Specify the source file within which the stop-hook is to be run." },
4319 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4320 "Set the start of the line range for which the stop-hook is to be run."},
4321 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4322 "Set the end of the line range for which the stop-hook is to be run."},
4323 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
4324 "Specify the class within which the stop-hook is to be run." },
4325 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4326 "Set the function name within which the stop hook will be run." },
4327 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4328};
4329
4330#pragma mark CommandObjectTargetStopHookDelete
4331
4332//-------------------------------------------------------------------------
4333// CommandObjectTargetStopHookDelete
4334//-------------------------------------------------------------------------
4335
Jim Inghamda26bd22012-06-08 21:56:10 +00004336class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004337{
4338public:
4339
4340 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004341 CommandObjectParsed (interpreter,
4342 "target stop-hook delete",
4343 "Delete a stop-hook.",
4344 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004345 {
4346 }
4347
4348 ~CommandObjectTargetStopHookDelete ()
4349 {
4350 }
4351
Jim Inghamda26bd22012-06-08 21:56:10 +00004352protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004353 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004354 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004355 {
4356 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4357 if (target)
4358 {
4359 // FIXME: see if we can use the breakpoint id style parser?
4360 size_t num_args = command.GetArgumentCount();
4361 if (num_args == 0)
4362 {
4363 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4364 {
4365 result.SetStatus (eReturnStatusFailed);
4366 return false;
4367 }
4368 else
4369 {
4370 target->RemoveAllStopHooks();
4371 }
4372 }
4373 else
4374 {
4375 bool success;
4376 for (size_t i = 0; i < num_args; i++)
4377 {
4378 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4379 if (!success)
4380 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004381 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004382 result.SetStatus(eReturnStatusFailed);
4383 return false;
4384 }
4385 success = target->RemoveStopHookByID (user_id);
4386 if (!success)
4387 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004388 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004389 result.SetStatus(eReturnStatusFailed);
4390 return false;
4391 }
4392 }
4393 }
4394 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4395 }
4396 else
4397 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004398 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004399 result.SetStatus (eReturnStatusFailed);
4400 }
4401
4402 return result.Succeeded();
4403 }
4404};
4405#pragma mark CommandObjectTargetStopHookEnableDisable
4406
4407//-------------------------------------------------------------------------
4408// CommandObjectTargetStopHookEnableDisable
4409//-------------------------------------------------------------------------
4410
Jim Inghamda26bd22012-06-08 21:56:10 +00004411class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004412{
4413public:
4414
4415 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004416 CommandObjectParsed (interpreter,
4417 name,
4418 help,
4419 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004420 m_enable (enable)
4421 {
4422 }
4423
4424 ~CommandObjectTargetStopHookEnableDisable ()
4425 {
4426 }
4427
Jim Inghamda26bd22012-06-08 21:56:10 +00004428protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004429 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004430 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004431 {
4432 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4433 if (target)
4434 {
4435 // FIXME: see if we can use the breakpoint id style parser?
4436 size_t num_args = command.GetArgumentCount();
4437 bool success;
4438
4439 if (num_args == 0)
4440 {
4441 target->SetAllStopHooksActiveState (m_enable);
4442 }
4443 else
4444 {
4445 for (size_t i = 0; i < num_args; i++)
4446 {
4447 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4448 if (!success)
4449 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004450 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004451 result.SetStatus(eReturnStatusFailed);
4452 return false;
4453 }
4454 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4455 if (!success)
4456 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004457 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004458 result.SetStatus(eReturnStatusFailed);
4459 return false;
4460 }
4461 }
4462 }
4463 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4464 }
4465 else
4466 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004467 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004468 result.SetStatus (eReturnStatusFailed);
4469 }
4470 return result.Succeeded();
4471 }
4472private:
4473 bool m_enable;
4474};
4475
4476#pragma mark CommandObjectTargetStopHookList
4477
4478//-------------------------------------------------------------------------
4479// CommandObjectTargetStopHookList
4480//-------------------------------------------------------------------------
4481
Jim Inghamda26bd22012-06-08 21:56:10 +00004482class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004483{
4484public:
4485
4486 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004487 CommandObjectParsed (interpreter,
4488 "target stop-hook list",
4489 "List all stop-hooks.",
4490 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004491 {
4492 }
4493
4494 ~CommandObjectTargetStopHookList ()
4495 {
4496 }
4497
Jim Inghamda26bd22012-06-08 21:56:10 +00004498protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004499 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004500 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004501 {
4502 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004503 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004504 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004505 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004506 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004507 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004508 }
4509
4510 size_t num_hooks = target->GetNumStopHooks ();
4511 if (num_hooks == 0)
4512 {
4513 result.GetOutputStream().PutCString ("No stop hooks.\n");
4514 }
4515 else
4516 {
4517 for (size_t i = 0; i < num_hooks; i++)
4518 {
4519 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4520 if (i > 0)
4521 result.GetOutputStream().PutCString ("\n");
4522 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4523 }
4524 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004525 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004526 return result.Succeeded();
4527 }
4528};
4529
4530#pragma mark CommandObjectMultiwordTargetStopHooks
4531//-------------------------------------------------------------------------
4532// CommandObjectMultiwordTargetStopHooks
4533//-------------------------------------------------------------------------
4534
4535class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4536{
4537public:
4538
4539 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4540 CommandObjectMultiword (interpreter,
4541 "target stop-hook",
4542 "A set of commands for operating on debugger target stop-hooks.",
4543 "target stop-hook <subcommand> [<subcommand-options>]")
4544 {
4545 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4546 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4547 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4548 false,
4549 "target stop-hook disable [<id>]",
4550 "Disable a stop-hook.",
4551 "target stop-hook disable")));
4552 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4553 true,
4554 "target stop-hook enable [<id>]",
4555 "Enable a stop-hook.",
4556 "target stop-hook enable")));
4557 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4558 }
4559
4560 ~CommandObjectMultiwordTargetStopHooks()
4561 {
4562 }
4563};
4564
4565
Chris Lattner24943d22010-06-08 16:52:24 +00004566
4567#pragma mark CommandObjectMultiwordTarget
4568
4569//-------------------------------------------------------------------------
4570// CommandObjectMultiwordTarget
4571//-------------------------------------------------------------------------
4572
Greg Clayton63094e02010-06-23 01:19:29 +00004573CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004574 CommandObjectMultiword (interpreter,
4575 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004576 "A set of commands for operating on debugger targets.",
4577 "target <subcommand> [<subcommand-options>]")
4578{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004579
4580 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004581 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004582 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4583 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004584 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004585 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00004586 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004587 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004588}
4589
4590CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4591{
4592}
4593
Greg Claytonabe0fed2011-04-18 08:33:37 +00004594