blob: 15523b31f2e5a6799a06685c28c2ef6b28458659 [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 Clayton49ce8962012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
22#include "lldb/Core/ModuleSpec.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000023#include "lldb/Core/Section.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000024#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Greg Clayton801417e2011-07-07 01:59:51 +000026#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Interpreter/CommandInterpreter.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000029#include "lldb/Interpreter/Options.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000030#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Clayton5beb99d2011-08-11 02:48:45 +000031#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000032#include "lldb/Interpreter/OptionGroupFile.h"
Greg Claytona42880a2011-10-25 06:44:01 +000033#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton368f8222011-07-07 04:38:25 +000034#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000035#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000036#include "lldb/Interpreter/OptionGroupUInt64.h"
37#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton801417e2011-07-07 01:59:51 +000038#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000039#include "lldb/Symbol/CompileUnit.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000040#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000041#include "lldb/Symbol/LineTable.h"
42#include "lldb/Symbol/ObjectFile.h"
43#include "lldb/Symbol/SymbolFile.h"
44#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000045#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton801417e2011-07-07 01:59:51 +000046#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000047#include "lldb/Target/Process.h"
48#include "lldb/Target/StackFrame.h"
49#include "lldb/Target/Thread.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000050#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000051
52using namespace lldb;
53using namespace lldb_private;
54
Greg Claytonabe0fed2011-04-18 08:33:37 +000055
56
57static void
58DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
59{
Greg Clayton52c8b6e2011-04-19 04:19:37 +000060 const ArchSpec &target_arch = target->GetArchitecture();
Greg Claytonabe0fed2011-04-18 08:33:37 +000061
Greg Clayton5beb99d2011-08-11 02:48:45 +000062 Module *exe_module = target->GetExecutableModulePointer();
Greg Claytonabe0fed2011-04-18 08:33:37 +000063 char exe_path[PATH_MAX];
64 bool exe_valid = false;
Greg Clayton5beb99d2011-08-11 02:48:45 +000065 if (exe_module)
66 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Greg Claytonabe0fed2011-04-18 08:33:37 +000067
68 if (!exe_valid)
69 ::strcpy (exe_path, "<none>");
70
71 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
72
73 uint32_t properties = 0;
74 if (target_arch.IsValid())
75 {
76 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
77 properties++;
78 }
79 PlatformSP platform_sp (target->GetPlatform());
80 if (platform_sp)
81 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName());
82
83 ProcessSP process_sp (target->GetProcessSP());
84 bool show_process_status = false;
85 if (process_sp)
86 {
87 lldb::pid_t pid = process_sp->GetID();
88 StateType state = process_sp->GetState();
89 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000090 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000091 const char *state_cstr = StateAsCString (state);
92 if (pid != LLDB_INVALID_PROCESS_ID)
Greg Claytond9919d32011-12-01 23:28:38 +000093 strm.Printf ("%spid=%llu", properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000094 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
95 }
96 if (properties > 0)
97 strm.PutCString (" )\n");
98 else
99 strm.EOL();
100 if (show_process_status)
101 {
102 const bool only_threads_with_stop_reason = true;
103 const uint32_t start_frame = 0;
104 const uint32_t num_frames = 1;
105 const uint32_t num_frames_with_source = 1;
106 process_sp->GetStatus (strm);
107 process_sp->GetThreadStatus (strm,
108 only_threads_with_stop_reason,
109 start_frame,
110 num_frames,
111 num_frames_with_source);
112
113 }
114}
115
116static uint32_t
117DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
118{
119 const uint32_t num_targets = target_list.GetNumTargets();
120 if (num_targets)
121 {
122 TargetSP selected_target_sp (target_list.GetSelectedTarget());
123 strm.PutCString ("Current targets:\n");
124 for (uint32_t i=0; i<num_targets; ++i)
125 {
126 TargetSP target_sp (target_list.GetTargetAtIndex (i));
127 if (target_sp)
128 {
129 bool is_selected = target_sp.get() == selected_target_sp.get();
130 DumpTargetInfo (i,
131 target_sp.get(),
132 is_selected ? "* " : " ",
133 show_stopped_process_status,
134 strm);
135 }
136 }
137 }
138 return num_targets;
139}
140#pragma mark CommandObjectTargetCreate
141
142//-------------------------------------------------------------------------
143// "target create"
144//-------------------------------------------------------------------------
145
Jim Inghamda26bd22012-06-08 21:56:10 +0000146class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000147{
148public:
149 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000150 CommandObjectParsed (interpreter,
151 "target create",
152 "Create a target using the argument as the main executable.",
153 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000154 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000155 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000156 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Johnny Chen6c061be2012-08-15 22:10:42 +0000157 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypePath, "Fullpath to a core file to use for this target.")
Greg Claytonabe0fed2011-04-18 08:33:37 +0000158 {
159 CommandArgumentEntry arg;
160 CommandArgumentData file_arg;
161
162 // Define the first (and only) variant of this arg.
163 file_arg.arg_type = eArgTypeFilename;
164 file_arg.arg_repetition = eArgRepeatPlain;
165
166 // There is only one variant this argument could be; put it into the argument entry.
167 arg.push_back (file_arg);
168
169 // Push the data for the first argument into the m_arguments vector.
170 m_arguments.push_back (arg);
171
Greg Clayton801417e2011-07-07 01:59:51 +0000172 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000173 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000174 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000175 m_option_group.Finalize();
176 }
177
178 ~CommandObjectTargetCreate ()
179 {
180 }
181
182 Options *
183 GetOptions ()
184 {
185 return &m_option_group;
186 }
187
Jim Inghamda26bd22012-06-08 21:56:10 +0000188 int
189 HandleArgumentCompletion (Args &input,
190 int &cursor_index,
191 int &cursor_char_position,
192 OptionElementVector &opt_element_vector,
193 int match_start_point,
194 int max_return_elements,
195 bool &word_complete,
196 StringList &matches)
197 {
198 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
199 completion_str.erase (cursor_char_position);
200
201 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
202 CommandCompletions::eDiskFileCompletion,
203 completion_str.c_str(),
204 match_start_point,
205 max_return_elements,
206 NULL,
207 word_complete,
208 matches);
209 return matches.GetSize();
210 }
211
212protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000213 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000214 DoExecute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000215 {
216 const int argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000217 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
218
219 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000220 {
221 const char *file_path = command.GetArgumentAtIndex(0);
222 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000223 FileSpec file_spec;
224
225 if (file_path)
226 file_spec.SetFile (file_path, true);
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000227
Greg Claytonabe0fed2011-04-18 08:33:37 +0000228 TargetSP target_sp;
229 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000230 const char *arch_cstr = m_arch_option.GetArchitectureName();
231 const bool get_dependent_files = true;
232 Error error (debugger.GetTargetList().CreateTarget (debugger,
233 file_spec,
234 arch_cstr,
235 get_dependent_files,
236 &m_platform_options,
237 target_sp));
238
Greg Claytonabe0fed2011-04-18 08:33:37 +0000239 if (target_sp)
240 {
241 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000242 if (core_file)
243 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000244 char core_path[PATH_MAX];
245 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000246 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000247 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000248 FileSpec core_file_dir;
249 core_file_dir.GetDirectory() = core_file.GetDirectory();
250 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000251
Greg Clayton9ce95382012-02-13 23:10:39 +0000252 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
253
254 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000255 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000256 // Seems wierd that we Launch a core file, but that is
257 // what we do!
258 error = process_sp->LoadCore();
259
260 if (error.Fail())
261 {
262 result.AppendError(error.AsCString("can't find plug-in for core file"));
263 result.SetStatus (eReturnStatusFailed);
264 return false;
265 }
266 else
267 {
268 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
269 result.SetStatus (eReturnStatusSuccessFinishNoResult);
270 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000271 }
272 else
273 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
275 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000276 }
277 }
278 else
279 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000280 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000281 result.SetStatus (eReturnStatusFailed);
282 }
283 }
284 else
285 {
286 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
287 result.SetStatus (eReturnStatusSuccessFinishNoResult);
288 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000289 }
290 else
291 {
292 result.AppendError(error.AsCString());
293 result.SetStatus (eReturnStatusFailed);
294 }
295 }
296 else
297 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000298 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 +0000299 result.SetStatus (eReturnStatusFailed);
300 }
301 return result.Succeeded();
302
303 }
304
Greg Claytonabe0fed2011-04-18 08:33:37 +0000305private:
306 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000307 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000308 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000309 OptionGroupFile m_core_file;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000310
311};
312
313#pragma mark CommandObjectTargetList
314
315//----------------------------------------------------------------------
316// "target list"
317//----------------------------------------------------------------------
318
Jim Inghamda26bd22012-06-08 21:56:10 +0000319class CommandObjectTargetList : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000320{
321public:
322 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000323 CommandObjectParsed (interpreter,
324 "target list",
325 "List all current targets in the current debug session.",
326 NULL,
327 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000328 {
329 }
330
331 virtual
332 ~CommandObjectTargetList ()
333 {
334 }
335
Jim Inghamda26bd22012-06-08 21:56:10 +0000336protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000337 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000338 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000339 {
340 if (args.GetArgumentCount() == 0)
341 {
342 Stream &strm = result.GetOutputStream();
343
344 bool show_stopped_process_status = false;
345 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
346 {
347 strm.PutCString ("No targets.\n");
348 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000349 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000350 }
351 else
352 {
353 result.AppendError ("the 'target list' command takes no arguments\n");
354 result.SetStatus (eReturnStatusFailed);
355 }
356 return result.Succeeded();
357 }
358};
359
360
361#pragma mark CommandObjectTargetSelect
362
363//----------------------------------------------------------------------
364// "target select"
365//----------------------------------------------------------------------
366
Jim Inghamda26bd22012-06-08 21:56:10 +0000367class CommandObjectTargetSelect : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000368{
369public:
370 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000371 CommandObjectParsed (interpreter,
372 "target select",
373 "Select a target as the current target by target index.",
374 NULL,
375 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000376 {
377 }
378
379 virtual
380 ~CommandObjectTargetSelect ()
381 {
382 }
383
Jim Inghamda26bd22012-06-08 21:56:10 +0000384protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000385 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000386 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000387 {
388 if (args.GetArgumentCount() == 1)
389 {
390 bool success = false;
391 const char *target_idx_arg = args.GetArgumentAtIndex(0);
392 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
393 if (success)
394 {
395 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
396 const uint32_t num_targets = target_list.GetNumTargets();
397 if (target_idx < num_targets)
398 {
399 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
400 if (target_sp)
401 {
402 Stream &strm = result.GetOutputStream();
403 target_list.SetSelectedTarget (target_sp.get());
404 bool show_stopped_process_status = false;
405 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000406 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000407 }
408 else
409 {
410 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
411 result.SetStatus (eReturnStatusFailed);
412 }
413 }
414 else
415 {
416 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
417 target_idx,
418 num_targets - 1);
419 result.SetStatus (eReturnStatusFailed);
420 }
421 }
422 else
423 {
424 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
425 result.SetStatus (eReturnStatusFailed);
426 }
427 }
428 else
429 {
430 result.AppendError ("'target select' takes a single argument: a target index\n");
431 result.SetStatus (eReturnStatusFailed);
432 }
433 return result.Succeeded();
434 }
435};
436
Greg Clayton153ccd72011-08-10 02:10:13 +0000437#pragma mark CommandObjectTargetSelect
438
439//----------------------------------------------------------------------
440// "target delete"
441//----------------------------------------------------------------------
442
Jim Inghamda26bd22012-06-08 21:56:10 +0000443class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton153ccd72011-08-10 02:10:13 +0000444{
445public:
446 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000447 CommandObjectParsed (interpreter,
448 "target delete",
449 "Delete one or more targets by target index.",
450 NULL,
451 0),
Greg Clayton5beb99d2011-08-11 02:48:45 +0000452 m_option_group (interpreter),
453 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 +0000454 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000455 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
456 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000457 }
458
459 virtual
460 ~CommandObjectTargetDelete ()
461 {
462 }
463
Jim Inghamda26bd22012-06-08 21:56:10 +0000464 Options *
465 GetOptions ()
466 {
467 return &m_option_group;
468 }
469
470protected:
Greg Clayton153ccd72011-08-10 02:10:13 +0000471 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000472 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton153ccd72011-08-10 02:10:13 +0000473 {
474 const size_t argc = args.GetArgumentCount();
475 std::vector<TargetSP> delete_target_list;
476 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
477 bool success = true;
478 TargetSP target_sp;
479 if (argc > 0)
480 {
481 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000482 // Bail out if don't have any targets.
483 if (num_targets == 0) {
484 result.AppendError("no targets to delete");
485 result.SetStatus(eReturnStatusFailed);
486 success = false;
487 }
488
Greg Clayton153ccd72011-08-10 02:10:13 +0000489 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
490 {
491 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
492 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
493 if (success)
494 {
495 if (target_idx < num_targets)
496 {
497 target_sp = target_list.GetTargetAtIndex (target_idx);
498 if (target_sp)
499 {
500 delete_target_list.push_back (target_sp);
501 continue;
502 }
503 }
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000504 if (num_targets > 1)
505 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
506 target_idx,
507 num_targets - 1);
508 else
509 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
510 target_idx);
511
Greg Clayton153ccd72011-08-10 02:10:13 +0000512 result.SetStatus (eReturnStatusFailed);
513 success = false;
514 }
515 else
516 {
517 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
518 result.SetStatus (eReturnStatusFailed);
519 success = false;
520 }
521 }
522
523 }
524 else
525 {
526 target_sp = target_list.GetSelectedTarget();
527 if (target_sp)
528 {
529 delete_target_list.push_back (target_sp);
530 }
531 else
532 {
533 result.AppendErrorWithFormat("no target is currently selected\n");
534 result.SetStatus (eReturnStatusFailed);
535 success = false;
536 }
537 }
538 if (success)
539 {
540 const size_t num_targets_to_delete = delete_target_list.size();
541 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
542 {
543 target_sp = delete_target_list[idx];
544 target_list.DeleteTarget(target_sp);
545 target_sp->Destroy();
546 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000547 // If "--clean" was specified, prune any orphaned shared modules from
548 // the global shared module list
549 if (m_cleanup_option.GetOptionValue ())
550 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000551 const bool mandatory = true;
552 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000553 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000554 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
555 result.SetStatus(eReturnStatusSuccessFinishResult);
556 }
557
558 return result.Succeeded();
559 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000560
Greg Clayton5beb99d2011-08-11 02:48:45 +0000561 OptionGroupOptions m_option_group;
562 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000563};
564
Greg Claytonabe0fed2011-04-18 08:33:37 +0000565
Greg Clayton801417e2011-07-07 01:59:51 +0000566#pragma mark CommandObjectTargetVariable
567
568//----------------------------------------------------------------------
569// "target variable"
570//----------------------------------------------------------------------
571
Jim Inghamda26bd22012-06-08 21:56:10 +0000572class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000573{
574public:
575 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000576 CommandObjectParsed (interpreter,
577 "target variable",
578 "Read global variable(s) prior to running your binary.",
579 NULL,
580 0),
Greg Clayton801417e2011-07-07 01:59:51 +0000581 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000582 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000583 m_option_format (eFormatDefault),
Greg Clayton801417e2011-07-07 01:59:51 +0000584 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."),
585 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."),
586 m_varobj_options()
587 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000588 CommandArgumentEntry arg;
589 CommandArgumentData var_name_arg;
590
591 // Define the first (and only) variant of this arg.
592 var_name_arg.arg_type = eArgTypeVarName;
593 var_name_arg.arg_repetition = eArgRepeatPlus;
594
595 // There is only one variant this argument could be; put it into the argument entry.
596 arg.push_back (var_name_arg);
597
598 // Push the data for the first argument into the m_arguments vector.
599 m_arguments.push_back (arg);
600
Greg Clayton801417e2011-07-07 01:59:51 +0000601 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000602 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000603 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 +0000604 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
605 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
606 m_option_group.Finalize();
607 }
608
609 virtual
610 ~CommandObjectTargetVariable ()
611 {
612 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000613
614 void
615 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
616 {
Enrico Granata19030d82011-08-15 18:01:31 +0000617 ValueObject::DumpValueObjectOptions options;
618
Enrico Granata3069c622012-03-01 04:24:26 +0000619 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000620 .SetMaximumDepth(m_varobj_options.max_depth)
621 .SetShowTypes(m_varobj_options.show_types)
622 .SetShowLocation(m_varobj_options.show_location)
623 .SetUseObjectiveC(m_varobj_options.use_objc)
624 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000625 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000626 .SetFlatOutput(m_varobj_options.flat_output)
627 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
628 .SetIgnoreCap(m_varobj_options.ignore_cap);
629
Greg Clayton5d81f492011-07-08 21:46:14 +0000630 switch (var_sp->GetScope())
631 {
632 case eValueTypeVariableGlobal:
633 if (m_option_variable.show_scope)
634 s.PutCString("GLOBAL: ");
635 break;
636
637 case eValueTypeVariableStatic:
638 if (m_option_variable.show_scope)
639 s.PutCString("STATIC: ");
640 break;
641
642 case eValueTypeVariableArgument:
643 if (m_option_variable.show_scope)
644 s.PutCString(" ARG: ");
645 break;
646
647 case eValueTypeVariableLocal:
648 if (m_option_variable.show_scope)
649 s.PutCString(" LOCAL: ");
650 break;
651
652 default:
653 break;
654 }
655
Greg Claytonfb816422011-07-10 19:21:23 +0000656 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000657 {
Greg Claytonfb816422011-07-10 19:21:23 +0000658 bool show_fullpaths = false;
659 bool show_module = true;
660 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
661 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000662 }
663
Greg Claytona42880a2011-10-25 06:44:01 +0000664 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000665 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000666 options.SetFormat(format);
667
668 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000669
670 ValueObject::DumpValueObject (s,
671 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000672 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000673
674 }
Greg Clayton801417e2011-07-07 01:59:51 +0000675
Greg Clayton5d81f492011-07-08 21:46:14 +0000676
677 static uint32_t GetVariableCallback (void *baton,
678 const char *name,
679 VariableList &variable_list)
680 {
681 Target *target = static_cast<Target *>(baton);
682 if (target)
683 {
684 return target->GetImages().FindGlobalVariables (ConstString(name),
685 true,
686 UINT32_MAX,
687 variable_list);
688 }
689 return 0;
690 }
691
692
693
Jim Inghamda26bd22012-06-08 21:56:10 +0000694 Options *
695 GetOptions ()
696 {
697 return &m_option_group;
698 }
699
700protected:
Greg Clayton801417e2011-07-07 01:59:51 +0000701 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000702 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000703 {
704 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000705 Target *target = exe_ctx.GetTargetPtr();
706 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000707 {
708 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000709 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000710 if (argc > 0)
711 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000712
Greg Clayton801417e2011-07-07 01:59:51 +0000713 for (size_t idx = 0; idx < argc; ++idx)
714 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000715 VariableList variable_list;
716 ValueObjectList valobj_list;
717
Greg Clayton368f8222011-07-07 04:38:25 +0000718 const char *arg = args.GetArgumentAtIndex(idx);
719 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000720 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000721 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000722 {
Greg Clayton368f8222011-07-07 04:38:25 +0000723 RegularExpression regex(arg);
724 if (!regex.IsValid ())
725 {
726 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
727 result.SetStatus (eReturnStatusFailed);
728 return false;
729 }
Greg Claytonfb816422011-07-10 19:21:23 +0000730 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000731 matches = target->GetImages().FindGlobalVariables (regex,
732 true,
733 UINT32_MAX,
734 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000735 }
736 else
737 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000738 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000739 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000740 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000741 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000742 variable_list,
743 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000744 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000745 }
746
747 if (matches == 0)
748 {
749 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
750 result.SetStatus (eReturnStatusFailed);
751 return false;
752 }
753 else
754 {
Greg Clayton801417e2011-07-07 01:59:51 +0000755 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
756 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000757 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000758 if (var_sp)
759 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000760 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
761 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000762 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000763
764 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000765 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000766 }
767 }
768 }
769 }
770 }
771 else
772 {
Greg Claytonfac93882011-10-05 22:17:32 +0000773 bool success = false;
774 StackFrame *frame = exe_ctx.GetFramePtr();
775 CompileUnit *comp_unit = NULL;
776 if (frame)
777 {
778 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
779 if (comp_unit)
780 {
781 const bool can_create = true;
782 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
783 if (comp_unit_varlist_sp)
784 {
785 size_t count = comp_unit_varlist_sp->GetSize();
786 if (count > 0)
787 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000788 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000789 comp_unit->GetDirectory().GetCString(),
790 comp_unit->GetFilename().GetCString());
791
792 success = true;
793 for (uint32_t i=0; i<count; ++i)
794 {
795 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
796 if (var_sp)
797 {
798 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
799
800 if (valobj_sp)
801 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
802 }
803 }
804 }
805 }
806 }
807 }
808 if (!success)
809 {
810 if (frame)
811 {
812 if (comp_unit)
813 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
814 comp_unit->GetDirectory().GetCString(),
815 comp_unit->GetFilename().GetCString());
816 else
817 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
818 }
819 else
820 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
821 result.SetStatus (eReturnStatusFailed);
822 }
Greg Clayton801417e2011-07-07 01:59:51 +0000823 }
824 }
825 else
826 {
827 result.AppendError ("invalid target, create a debug target using the 'target create' command");
828 result.SetStatus (eReturnStatusFailed);
829 return false;
830 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000831
832 if (m_interpreter.TruncationWarningNecessary())
833 {
834 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
835 m_cmd_name.c_str());
836 m_interpreter.TruncationWarningGiven();
837 }
838
Greg Clayton801417e2011-07-07 01:59:51 +0000839 return result.Succeeded();
840 }
841
Greg Clayton801417e2011-07-07 01:59:51 +0000842 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000843 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000844 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000845 OptionGroupFileList m_option_compile_units;
846 OptionGroupFileList m_option_shared_libraries;
847 OptionGroupValueObjectDisplay m_varobj_options;
848
849};
850
851
Greg Claytone1f50b92011-05-03 22:09:39 +0000852#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000853
Jim Inghamda26bd22012-06-08 21:56:10 +0000854class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000855{
856public:
857
Greg Claytone1f50b92011-05-03 22:09:39 +0000858 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000859 CommandObjectParsed (interpreter,
860 "target modules search-paths add",
861 "Add new image search paths substitution pairs to the current target.",
862 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000863 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000864 CommandArgumentEntry arg;
865 CommandArgumentData old_prefix_arg;
866 CommandArgumentData new_prefix_arg;
867
868 // Define the first variant of this arg pair.
869 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
870 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
871
872 // Define the first variant of this arg pair.
873 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
874 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
875
876 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
877 // must always occur together, they are treated as two variants of one argument rather than two independent
878 // arguments. Push them both into the first argument position for m_arguments...
879
880 arg.push_back (old_prefix_arg);
881 arg.push_back (new_prefix_arg);
882
883 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000884 }
885
Greg Claytone1f50b92011-05-03 22:09:39 +0000886 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000887 {
888 }
889
Jim Inghamda26bd22012-06-08 21:56:10 +0000890protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000891 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000892 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000893 CommandReturnObject &result)
894 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000895 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000896 if (target)
897 {
898 uint32_t argc = command.GetArgumentCount();
899 if (argc & 1)
900 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000901 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000902 result.SetStatus (eReturnStatusFailed);
903 }
904 else
905 {
906 for (uint32_t i=0; i<argc; i+=2)
907 {
908 const char *from = command.GetArgumentAtIndex(i);
909 const char *to = command.GetArgumentAtIndex(i+1);
910
911 if (from[0] && to[0])
912 {
913 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000914 target->GetImageSearchPathList().Append (ConstString(from),
915 ConstString(to),
916 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000917 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000918 }
919 else
920 {
921 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000922 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000923 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000924 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000925 result.SetStatus (eReturnStatusFailed);
926 }
927 }
928 }
929 }
930 else
931 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000932 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000933 result.SetStatus (eReturnStatusFailed);
934 }
935 return result.Succeeded();
936 }
937};
938
Greg Claytone1f50b92011-05-03 22:09:39 +0000939#pragma mark CommandObjectTargetModulesSearchPathsClear
940
Jim Inghamda26bd22012-06-08 21:56:10 +0000941class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000942{
943public:
944
Greg Claytone1f50b92011-05-03 22:09:39 +0000945 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000946 CommandObjectParsed (interpreter,
947 "target modules search-paths clear",
948 "Clear all current image search path substitution pairs from the current target.",
949 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000950 {
951 }
952
Greg Claytone1f50b92011-05-03 22:09:39 +0000953 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000954 {
955 }
956
Jim Inghamda26bd22012-06-08 21:56:10 +0000957protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000958 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000959 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000960 CommandReturnObject &result)
961 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000962 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000963 if (target)
964 {
965 bool notify = true;
966 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000967 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000968 }
969 else
970 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000971 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000972 result.SetStatus (eReturnStatusFailed);
973 }
974 return result.Succeeded();
975 }
976};
977
Greg Claytone1f50b92011-05-03 22:09:39 +0000978#pragma mark CommandObjectTargetModulesSearchPathsInsert
979
Jim Inghamda26bd22012-06-08 21:56:10 +0000980class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000981{
982public:
983
Greg Claytone1f50b92011-05-03 22:09:39 +0000984 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000985 CommandObjectParsed (interpreter,
986 "target modules search-paths insert",
987 "Insert a new image search path substitution pair into the current target at the specified index.",
988 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000989 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000990 CommandArgumentEntry arg1;
991 CommandArgumentEntry arg2;
992 CommandArgumentData index_arg;
993 CommandArgumentData old_prefix_arg;
994 CommandArgumentData new_prefix_arg;
995
996 // Define the first and only variant of this arg.
997 index_arg.arg_type = eArgTypeIndex;
998 index_arg.arg_repetition = eArgRepeatPlain;
999
1000 // Put the one and only variant into the first arg for m_arguments:
1001 arg1.push_back (index_arg);
1002
1003 // Define the first variant of this arg pair.
1004 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1005 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1006
1007 // Define the first variant of this arg pair.
1008 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1009 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1010
1011 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1012 // must always occur together, they are treated as two variants of one argument rather than two independent
1013 // arguments. Push them both into the same argument position for m_arguments...
1014
1015 arg2.push_back (old_prefix_arg);
1016 arg2.push_back (new_prefix_arg);
1017
1018 // Add arguments to m_arguments.
1019 m_arguments.push_back (arg1);
1020 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001021 }
1022
Greg Claytone1f50b92011-05-03 22:09:39 +00001023 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001024 {
1025 }
1026
Jim Inghamda26bd22012-06-08 21:56:10 +00001027protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001028 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001029 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001030 CommandReturnObject &result)
1031 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001032 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001033 if (target)
1034 {
1035 uint32_t argc = command.GetArgumentCount();
1036 // check for at least 3 arguments and an odd nubmer of parameters
1037 if (argc >= 3 && argc & 1)
1038 {
1039 bool success = false;
1040
1041 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1042
1043 if (!success)
1044 {
1045 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1046 result.SetStatus (eReturnStatusFailed);
1047 return result.Succeeded();
1048 }
1049
1050 // shift off the index
1051 command.Shift();
1052 argc = command.GetArgumentCount();
1053
1054 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1055 {
1056 const char *from = command.GetArgumentAtIndex(i);
1057 const char *to = command.GetArgumentAtIndex(i+1);
1058
1059 if (from[0] && to[0])
1060 {
1061 bool last_pair = ((argc - i) == 2);
1062 target->GetImageSearchPathList().Insert (ConstString(from),
1063 ConstString(to),
1064 insert_idx,
1065 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001066 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001067 }
1068 else
1069 {
1070 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001071 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001072 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001073 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001074 result.SetStatus (eReturnStatusFailed);
1075 return false;
1076 }
1077 }
1078 }
1079 else
1080 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001081 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001082 result.SetStatus (eReturnStatusFailed);
1083 return result.Succeeded();
1084 }
1085
1086 }
1087 else
1088 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001089 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001090 result.SetStatus (eReturnStatusFailed);
1091 }
1092 return result.Succeeded();
1093 }
1094};
1095
Greg Claytone1f50b92011-05-03 22:09:39 +00001096
1097#pragma mark CommandObjectTargetModulesSearchPathsList
1098
1099
Jim Inghamda26bd22012-06-08 21:56:10 +00001100class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001101{
1102public:
1103
Greg Claytone1f50b92011-05-03 22:09:39 +00001104 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001105 CommandObjectParsed (interpreter,
1106 "target modules search-paths list",
1107 "List all current image search path substitution pairs in the current target.",
1108 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001109 {
1110 }
1111
Greg Claytone1f50b92011-05-03 22:09:39 +00001112 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001113 {
1114 }
1115
Jim Inghamda26bd22012-06-08 21:56:10 +00001116protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001117 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001118 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001119 CommandReturnObject &result)
1120 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001121 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001122 if (target)
1123 {
1124 if (command.GetArgumentCount() != 0)
1125 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001126 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001127 result.SetStatus (eReturnStatusFailed);
1128 return result.Succeeded();
1129 }
1130
1131 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001132 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001133 }
1134 else
1135 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001136 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001137 result.SetStatus (eReturnStatusFailed);
1138 }
1139 return result.Succeeded();
1140 }
1141};
1142
Greg Claytone1f50b92011-05-03 22:09:39 +00001143#pragma mark CommandObjectTargetModulesSearchPathsQuery
1144
Jim Inghamda26bd22012-06-08 21:56:10 +00001145class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001146{
1147public:
1148
Greg Claytone1f50b92011-05-03 22:09:39 +00001149 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001150 CommandObjectParsed (interpreter,
1151 "target modules search-paths query",
1152 "Transform a path using the first applicable image search path.",
1153 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001154 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001155 CommandArgumentEntry arg;
1156 CommandArgumentData path_arg;
1157
1158 // Define the first (and only) variant of this arg.
1159 path_arg.arg_type = eArgTypePath;
1160 path_arg.arg_repetition = eArgRepeatPlain;
1161
1162 // There is only one variant this argument could be; put it into the argument entry.
1163 arg.push_back (path_arg);
1164
1165 // Push the data for the first argument into the m_arguments vector.
1166 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001167 }
1168
Greg Claytone1f50b92011-05-03 22:09:39 +00001169 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001170 {
1171 }
1172
Jim Inghamda26bd22012-06-08 21:56:10 +00001173protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001174 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001175 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001176 CommandReturnObject &result)
1177 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001178 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001179 if (target)
1180 {
1181 if (command.GetArgumentCount() != 1)
1182 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001183 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001184 result.SetStatus (eReturnStatusFailed);
1185 return result.Succeeded();
1186 }
1187
1188 ConstString orig(command.GetArgumentAtIndex(0));
1189 ConstString transformed;
1190 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1191 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1192 else
1193 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001194
1195 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001196 }
1197 else
1198 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001199 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001200 result.SetStatus (eReturnStatusFailed);
1201 }
1202 return result.Succeeded();
1203 }
1204};
1205
Greg Claytone1f50b92011-05-03 22:09:39 +00001206//----------------------------------------------------------------------
1207// Static Helper functions
1208//----------------------------------------------------------------------
1209static void
1210DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1211{
1212 if (module)
1213 {
1214 const char *arch_cstr;
1215 if (full_triple)
1216 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1217 else
1218 arch_cstr = module->GetArchitecture().GetArchitectureName();
1219 if (width)
1220 strm.Printf("%-*s", width, arch_cstr);
1221 else
1222 strm.PutCString(arch_cstr);
1223 }
1224}
1225
1226static void
1227DumpModuleUUID (Stream &strm, Module *module)
1228{
Greg Clayton153ccd72011-08-10 02:10:13 +00001229 if (module->GetUUID().IsValid())
1230 module->GetUUID().Dump (&strm);
1231 else
1232 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001233}
1234
1235static uint32_t
1236DumpCompileUnitLineTable
1237(
1238 CommandInterpreter &interpreter,
1239 Stream &strm,
1240 Module *module,
1241 const FileSpec &file_spec,
1242 bool load_addresses
1243 )
1244{
1245 uint32_t num_matches = 0;
1246 if (module)
1247 {
1248 SymbolContextList sc_list;
1249 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1250 0,
1251 false,
1252 eSymbolContextCompUnit,
1253 sc_list);
1254
1255 for (uint32_t i=0; i<num_matches; ++i)
1256 {
1257 SymbolContext sc;
1258 if (sc_list.GetContextAtIndex(i, sc))
1259 {
1260 if (i > 0)
1261 strm << "\n\n";
1262
1263 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1264 << module->GetFileSpec().GetFilename() << "\n";
1265 LineTable *line_table = sc.comp_unit->GetLineTable();
1266 if (line_table)
1267 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001268 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001269 lldb::eDescriptionLevelBrief);
1270 else
1271 strm << "No line table";
1272 }
1273 }
1274 }
1275 return num_matches;
1276}
1277
1278static void
1279DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1280{
1281 if (file_spec_ptr)
1282 {
1283 if (width > 0)
1284 {
1285 char fullpath[PATH_MAX];
1286 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1287 {
1288 strm.Printf("%-*s", width, fullpath);
1289 return;
1290 }
1291 }
1292 else
1293 {
1294 file_spec_ptr->Dump(&strm);
1295 return;
1296 }
1297 }
1298 // Keep the width spacing correct if things go wrong...
1299 if (width > 0)
1300 strm.Printf("%-*s", width, "");
1301}
1302
1303static void
1304DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1305{
1306 if (file_spec_ptr)
1307 {
1308 if (width > 0)
1309 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1310 else
1311 file_spec_ptr->GetDirectory().Dump(&strm);
1312 return;
1313 }
1314 // Keep the width spacing correct if things go wrong...
1315 if (width > 0)
1316 strm.Printf("%-*s", width, "");
1317}
1318
1319static void
1320DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1321{
1322 if (file_spec_ptr)
1323 {
1324 if (width > 0)
1325 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1326 else
1327 file_spec_ptr->GetFilename().Dump(&strm);
1328 return;
1329 }
1330 // Keep the width spacing correct if things go wrong...
1331 if (width > 0)
1332 strm.Printf("%-*s", width, "");
1333}
1334
1335
1336static void
1337DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1338{
1339 if (module)
1340 {
1341 ObjectFile *objfile = module->GetObjectFile ();
1342 if (objfile)
1343 {
1344 Symtab *symtab = objfile->GetSymtab();
1345 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001346 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001347 }
1348 }
1349}
1350
1351static void
1352DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1353{
1354 if (module)
1355 {
1356 ObjectFile *objfile = module->GetObjectFile ();
1357 if (objfile)
1358 {
1359 SectionList *section_list = objfile->GetSectionList();
1360 if (section_list)
1361 {
1362 strm.PutCString ("Sections for '");
1363 strm << module->GetFileSpec();
1364 if (module->GetObjectName())
1365 strm << '(' << module->GetObjectName() << ')';
1366 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1367 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001368 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001369 strm.IndentLess();
1370 }
1371 }
1372 }
1373}
1374
1375static bool
1376DumpModuleSymbolVendor (Stream &strm, Module *module)
1377{
1378 if (module)
1379 {
1380 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1381 if (symbol_vendor)
1382 {
1383 symbol_vendor->Dump(&strm);
1384 return true;
1385 }
1386 }
1387 return false;
1388}
1389
Greg Clayton2ad894b2012-05-15 18:43:44 +00001390static void
1391DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1392{
1393 strm.IndentMore();
1394 strm.Indent (" Address: ");
1395 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1396 strm.PutCString (" (");
1397 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1398 strm.PutCString (")\n");
1399 strm.Indent (" Summary: ");
1400 const uint32_t save_indent = strm.GetIndentLevel ();
1401 strm.SetIndentLevel (save_indent + 13);
1402 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1403 strm.SetIndentLevel (save_indent);
1404 // Print out detailed address information when verbose is enabled
1405 if (verbose)
1406 {
1407 strm.EOL();
1408 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1409 }
1410 strm.IndentLess();
1411}
1412
Greg Claytone1f50b92011-05-03 22:09:39 +00001413static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001414LookupAddressInModule (CommandInterpreter &interpreter,
1415 Stream &strm,
1416 Module *module,
1417 uint32_t resolve_mask,
1418 lldb::addr_t raw_addr,
1419 lldb::addr_t offset,
1420 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001421{
1422 if (module)
1423 {
1424 lldb::addr_t addr = raw_addr - offset;
1425 Address so_addr;
1426 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001427 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001428 if (target && !target->GetSectionLoadList().IsEmpty())
1429 {
1430 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1431 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001432 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001433 return false;
1434 }
1435 else
1436 {
1437 if (!module->ResolveFileAddress (addr, so_addr))
1438 return false;
1439 }
1440
Greg Claytone1f50b92011-05-03 22:09:39 +00001441 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001442 DumpAddress (exe_scope, so_addr, verbose, strm);
1443// strm.IndentMore();
1444// strm.Indent (" Address: ");
1445// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1446// strm.PutCString (" (");
1447// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1448// strm.PutCString (")\n");
1449// strm.Indent (" Summary: ");
1450// const uint32_t save_indent = strm.GetIndentLevel ();
1451// strm.SetIndentLevel (save_indent + 13);
1452// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1453// strm.SetIndentLevel (save_indent);
1454// // Print out detailed address information when verbose is enabled
1455// if (verbose)
1456// {
1457// strm.EOL();
1458// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1459// }
1460// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001461 return true;
1462 }
1463
1464 return false;
1465}
1466
1467static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001468LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001469{
1470 if (module)
1471 {
1472 SymbolContext sc;
1473
1474 ObjectFile *objfile = module->GetObjectFile ();
1475 if (objfile)
1476 {
1477 Symtab *symtab = objfile->GetSymtab();
1478 if (symtab)
1479 {
1480 uint32_t i;
1481 std::vector<uint32_t> match_indexes;
1482 ConstString symbol_name (name);
1483 uint32_t num_matches = 0;
1484 if (name_is_regex)
1485 {
1486 RegularExpression name_regexp(name);
1487 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1488 eSymbolTypeAny,
1489 match_indexes);
1490 }
1491 else
1492 {
1493 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1494 }
1495
1496
1497 if (num_matches > 0)
1498 {
1499 strm.Indent ();
1500 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1501 name_is_regex ? "the regular expression " : "", name);
1502 DumpFullpath (strm, &module->GetFileSpec(), 0);
1503 strm.PutCString(":\n");
1504 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001505 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001506 for (i=0; i < num_matches; ++i)
1507 {
1508 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001509 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1510 symbol->GetAddress(),
1511 verbose,
1512 strm);
1513
1514// strm.Indent ();
1515// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001516 }
1517 strm.IndentLess ();
1518 return num_matches;
1519 }
1520 }
1521 }
1522 }
1523 return 0;
1524}
1525
1526
1527static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001528DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001529{
1530 strm.IndentMore ();
1531 uint32_t i;
1532 const uint32_t num_matches = sc_list.GetSize();
1533
1534 for (i=0; i<num_matches; ++i)
1535 {
1536 SymbolContext sc;
1537 if (sc_list.GetContextAtIndex(i, sc))
1538 {
Sean Callanand7793d22012-02-11 00:24:04 +00001539 AddressRange range;
1540
1541 sc.GetAddressRange(eSymbolContextEverything,
1542 0,
1543 true,
1544 range);
1545
Greg Clayton2ad894b2012-05-15 18:43:44 +00001546 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001547 }
1548 }
1549 strm.IndentLess ();
1550}
1551
1552static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001553LookupFunctionInModule (CommandInterpreter &interpreter,
1554 Stream &strm,
1555 Module *module,
1556 const char *name,
1557 bool name_is_regex,
1558 bool include_inlines,
1559 bool include_symbols,
1560 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001561{
1562 if (module && name && name[0])
1563 {
1564 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001565 const bool append = true;
1566 uint32_t num_matches = 0;
1567 if (name_is_regex)
1568 {
1569 RegularExpression function_name_regex (name);
1570 num_matches = module->FindFunctions (function_name_regex,
1571 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001572 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001573 append,
1574 sc_list);
1575 }
1576 else
1577 {
1578 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001579 num_matches = module->FindFunctions (function_name,
1580 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001581 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1582 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001583 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001584 append,
1585 sc_list);
1586 }
1587
1588 if (num_matches)
1589 {
1590 strm.Indent ();
1591 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1592 DumpFullpath (strm, &module->GetFileSpec(), 0);
1593 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001594 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001595 }
1596 return num_matches;
1597 }
1598 return 0;
1599}
1600
1601static uint32_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001602LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001603 Stream &strm,
1604 Module *module,
1605 const char *name_cstr,
1606 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001607{
1608 if (module && name_cstr && name_cstr[0])
1609 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001610 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001611 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001612 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001613 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001614 SymbolContext sc;
1615
1616 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001617 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001618
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001619 if (num_matches)
1620 {
1621 strm.Indent ();
1622 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1623 DumpFullpath (strm, &module->GetFileSpec(), 0);
1624 strm.PutCString(":\n");
1625 const uint32_t num_types = type_list.GetSize();
1626 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001627 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001628 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1629 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001630 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001631 // Resolve the clang type so that any forward references
1632 // to types that haven't yet been parsed will get parsed.
1633 type_sp->GetClangFullType ();
1634 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001635 // Print all typedef chains
1636 TypeSP typedef_type_sp (type_sp);
1637 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1638 while (typedefed_type_sp)
1639 {
1640 strm.EOL();
1641 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1642 typedefed_type_sp->GetClangFullType ();
1643 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1644 typedef_type_sp = typedefed_type_sp;
1645 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1646 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001647 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001648 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001649 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001650 }
1651 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001652 }
1653 return 0;
1654}
1655
1656static uint32_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001657LookupTypeHere (CommandInterpreter &interpreter,
1658 Stream &strm,
1659 const SymbolContext &sym_ctx,
1660 const char *name_cstr,
1661 bool name_is_regex)
1662{
1663 if (!sym_ctx.module_sp)
1664 return 0;
1665
1666 TypeList type_list;
1667 const uint32_t max_num_matches = UINT32_MAX;
1668 uint32_t num_matches = 1;
1669 bool name_is_fully_qualified = false;
1670
1671 ConstString name(name_cstr);
1672 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1673
1674 if (num_matches)
1675 {
1676 strm.Indent ();
1677 strm.PutCString("Best match found in ");
1678 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1679 strm.PutCString(":\n");
1680
1681 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1682 if (type_sp)
1683 {
1684 // Resolve the clang type so that any forward references
1685 // to types that haven't yet been parsed will get parsed.
1686 type_sp->GetClangFullType ();
1687 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1688 // Print all typedef chains
1689 TypeSP typedef_type_sp (type_sp);
1690 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1691 while (typedefed_type_sp)
1692 {
1693 strm.EOL();
1694 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1695 typedefed_type_sp->GetClangFullType ();
1696 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1697 typedef_type_sp = typedefed_type_sp;
1698 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1699 }
1700 }
1701 strm.EOL();
1702 }
1703 return num_matches;
1704}
1705
1706static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001707LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001708 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001709 Module *module,
1710 const FileSpec &file_spec,
1711 uint32_t line,
1712 bool check_inlines,
1713 bool verbose)
1714{
1715 if (module && file_spec)
1716 {
1717 SymbolContextList sc_list;
1718 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1719 eSymbolContextEverything, sc_list);
1720 if (num_matches > 0)
1721 {
1722 strm.Indent ();
1723 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1724 strm << file_spec;
1725 if (line > 0)
1726 strm.Printf (":%u", line);
1727 strm << " in ";
1728 DumpFullpath (strm, &module->GetFileSpec(), 0);
1729 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001730 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001731 return num_matches;
1732 }
1733 }
1734 return 0;
1735
1736}
1737
Greg Clayton91048ef2011-11-10 01:18:58 +00001738
1739static size_t
1740FindModulesByName (Target *target,
1741 const char *module_name,
1742 ModuleList &module_list,
1743 bool check_global_list)
1744{
1745// Dump specified images (by basename or fullpath)
1746 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001747 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001748
1749 const size_t initial_size = module_list.GetSize ();
1750
Greg Clayton316f57f2012-07-11 20:46:47 +00001751 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001752 {
1753 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001754 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001755 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1756 ModuleSP module_sp;
1757 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1758 {
1759 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1760
1761 if (module)
1762 {
Greg Clayton444fe992012-02-26 05:51:37 +00001763 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001764 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001765 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001766 module_list.AppendIfNeeded(module_sp);
1767 }
1768 }
1769 }
1770 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001771 else
1772 {
1773 if (target)
1774 {
1775 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1776
1777 // Not found in our module list for our target, check the main
1778 // shared module list in case it is a extra file used somewhere
1779 // else
1780 if (num_matches == 0)
1781 {
1782 module_spec.GetArchitecture() = target->GetArchitecture();
1783 ModuleList::FindSharedModules (module_spec, module_list);
1784 }
1785 }
1786 else
1787 {
1788 ModuleList::FindSharedModules (module_spec,module_list);
1789 }
1790 }
1791
Greg Clayton91048ef2011-11-10 01:18:58 +00001792 return module_list.GetSize () - initial_size;
1793}
1794
Greg Claytone1f50b92011-05-03 22:09:39 +00001795#pragma mark CommandObjectTargetModulesModuleAutoComplete
1796
1797//----------------------------------------------------------------------
1798// A base command object class that can auto complete with module file
1799// paths
1800//----------------------------------------------------------------------
1801
Jim Inghamda26bd22012-06-08 21:56:10 +00001802class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001803{
1804public:
1805
1806 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1807 const char *name,
1808 const char *help,
1809 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001810 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001811 {
1812 CommandArgumentEntry arg;
1813 CommandArgumentData file_arg;
1814
1815 // Define the first (and only) variant of this arg.
1816 file_arg.arg_type = eArgTypeFilename;
1817 file_arg.arg_repetition = eArgRepeatStar;
1818
1819 // There is only one variant this argument could be; put it into the argument entry.
1820 arg.push_back (file_arg);
1821
1822 // Push the data for the first argument into the m_arguments vector.
1823 m_arguments.push_back (arg);
1824 }
1825
1826 virtual
1827 ~CommandObjectTargetModulesModuleAutoComplete ()
1828 {
1829 }
1830
1831 virtual int
1832 HandleArgumentCompletion (Args &input,
1833 int &cursor_index,
1834 int &cursor_char_position,
1835 OptionElementVector &opt_element_vector,
1836 int match_start_point,
1837 int max_return_elements,
1838 bool &word_complete,
1839 StringList &matches)
1840 {
1841 // Arguments are the standard module completer.
1842 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1843 completion_str.erase (cursor_char_position);
1844
1845 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1846 CommandCompletions::eModuleCompletion,
1847 completion_str.c_str(),
1848 match_start_point,
1849 max_return_elements,
1850 NULL,
1851 word_complete,
1852 matches);
1853 return matches.GetSize();
1854 }
1855};
1856
1857#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1858
1859//----------------------------------------------------------------------
1860// A base command object class that can auto complete with module source
1861// file paths
1862//----------------------------------------------------------------------
1863
Jim Inghamda26bd22012-06-08 21:56:10 +00001864class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001865{
1866public:
1867
1868 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1869 const char *name,
1870 const char *help,
1871 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001872 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001873 {
1874 CommandArgumentEntry arg;
1875 CommandArgumentData source_file_arg;
1876
1877 // Define the first (and only) variant of this arg.
1878 source_file_arg.arg_type = eArgTypeSourceFile;
1879 source_file_arg.arg_repetition = eArgRepeatPlus;
1880
1881 // There is only one variant this argument could be; put it into the argument entry.
1882 arg.push_back (source_file_arg);
1883
1884 // Push the data for the first argument into the m_arguments vector.
1885 m_arguments.push_back (arg);
1886 }
1887
1888 virtual
1889 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1890 {
1891 }
1892
1893 virtual int
1894 HandleArgumentCompletion (Args &input,
1895 int &cursor_index,
1896 int &cursor_char_position,
1897 OptionElementVector &opt_element_vector,
1898 int match_start_point,
1899 int max_return_elements,
1900 bool &word_complete,
1901 StringList &matches)
1902 {
1903 // Arguments are the standard source file completer.
1904 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1905 completion_str.erase (cursor_char_position);
1906
1907 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1908 CommandCompletions::eSourceFileCompletion,
1909 completion_str.c_str(),
1910 match_start_point,
1911 max_return_elements,
1912 NULL,
1913 word_complete,
1914 matches);
1915 return matches.GetSize();
1916 }
1917};
1918
1919
1920#pragma mark CommandObjectTargetModulesDumpSymtab
1921
1922
1923class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1924{
1925public:
1926 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1927 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1928 "target modules dump symtab",
1929 "Dump the symbol table from one or more target modules.",
1930 NULL),
1931 m_options (interpreter)
1932 {
1933 }
1934
1935 virtual
1936 ~CommandObjectTargetModulesDumpSymtab ()
1937 {
1938 }
1939
Jim Inghamda26bd22012-06-08 21:56:10 +00001940 virtual Options *
1941 GetOptions ()
1942 {
1943 return &m_options;
1944 }
1945
1946 class CommandOptions : public Options
1947 {
1948 public:
1949
1950 CommandOptions (CommandInterpreter &interpreter) :
1951 Options(interpreter),
1952 m_sort_order (eSortOrderNone)
1953 {
1954 }
1955
1956 virtual
1957 ~CommandOptions ()
1958 {
1959 }
1960
1961 virtual Error
1962 SetOptionValue (uint32_t option_idx, const char *option_arg)
1963 {
1964 Error error;
1965 char short_option = (char) m_getopt_table[option_idx].val;
1966
1967 switch (short_option)
1968 {
1969 case 's':
1970 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1971 g_option_table[option_idx].enum_values,
1972 eSortOrderNone,
1973 error);
1974 break;
1975
1976 default:
1977 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1978 break;
1979
1980 }
1981 return error;
1982 }
1983
1984 void
1985 OptionParsingStarting ()
1986 {
1987 m_sort_order = eSortOrderNone;
1988 }
1989
1990 const OptionDefinition*
1991 GetDefinitions ()
1992 {
1993 return g_option_table;
1994 }
1995
1996 // Options table: Required for subclasses of Options.
1997 static OptionDefinition g_option_table[];
1998
1999 SortOrder m_sort_order;
2000 };
2001
2002protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002003 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002004 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002005 CommandReturnObject &result)
2006 {
2007 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2008 if (target == NULL)
2009 {
2010 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2011 result.SetStatus (eReturnStatusFailed);
2012 return false;
2013 }
2014 else
2015 {
2016 uint32_t num_dumped = 0;
2017
2018 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2019 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2020 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2021
2022 if (command.GetArgumentCount() == 0)
2023 {
2024 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002025 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002026 const uint32_t num_modules = target->GetImages().GetSize();
2027 if (num_modules > 0)
2028 {
2029 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2030 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2031 {
2032 if (num_dumped > 0)
2033 {
2034 result.GetOutputStream().EOL();
2035 result.GetOutputStream().EOL();
2036 }
2037 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002038 DumpModuleSymtab (m_interpreter,
2039 result.GetOutputStream(),
2040 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2041 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002042 }
2043 }
2044 else
2045 {
2046 result.AppendError ("the target has no associated executable images");
2047 result.SetStatus (eReturnStatusFailed);
2048 return false;
2049 }
2050 }
2051 else
2052 {
2053 // Dump specified images (by basename or fullpath)
2054 const char *arg_cstr;
2055 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2056 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002057 ModuleList module_list;
2058 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2059 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002060 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002061 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002062 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002063 Module *module = module_list.GetModulePointerAtIndex(i);
2064 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002065 {
2066 if (num_dumped > 0)
2067 {
2068 result.GetOutputStream().EOL();
2069 result.GetOutputStream().EOL();
2070 }
2071 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002072 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002073 }
2074 }
2075 }
2076 else
2077 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2078 }
2079 }
2080
2081 if (num_dumped > 0)
2082 result.SetStatus (eReturnStatusSuccessFinishResult);
2083 else
2084 {
2085 result.AppendError ("no matching executable images found");
2086 result.SetStatus (eReturnStatusFailed);
2087 }
2088 }
2089 return result.Succeeded();
2090 }
2091
Greg Claytone1f50b92011-05-03 22:09:39 +00002092
2093 CommandOptions m_options;
2094};
2095
2096static OptionEnumValueElement
2097g_sort_option_enumeration[4] =
2098{
2099 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2100 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2101 { eSortOrderByName, "name", "Sort output by symbol name."},
2102 { 0, NULL, NULL }
2103};
2104
2105
2106OptionDefinition
2107CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2108{
2109 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2110 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2111};
2112
2113#pragma mark CommandObjectTargetModulesDumpSections
2114
2115//----------------------------------------------------------------------
2116// Image section dumping command
2117//----------------------------------------------------------------------
2118
2119class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2120{
2121public:
2122 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2123 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2124 "target modules dump sections",
2125 "Dump the sections from one or more target modules.",
2126 //"target modules dump sections [<file1> ...]")
2127 NULL)
2128 {
2129 }
2130
2131 virtual
2132 ~CommandObjectTargetModulesDumpSections ()
2133 {
2134 }
2135
Jim Inghamda26bd22012-06-08 21:56:10 +00002136protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002137 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002138 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002139 CommandReturnObject &result)
2140 {
2141 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2142 if (target == NULL)
2143 {
2144 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2145 result.SetStatus (eReturnStatusFailed);
2146 return false;
2147 }
2148 else
2149 {
2150 uint32_t num_dumped = 0;
2151
2152 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2153 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2154 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2155
2156 if (command.GetArgumentCount() == 0)
2157 {
2158 // Dump all sections for all modules images
2159 const uint32_t num_modules = target->GetImages().GetSize();
2160 if (num_modules > 0)
2161 {
2162 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2163 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2164 {
2165 num_dumped++;
2166 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2167 }
2168 }
2169 else
2170 {
2171 result.AppendError ("the target has no associated executable images");
2172 result.SetStatus (eReturnStatusFailed);
2173 return false;
2174 }
2175 }
2176 else
2177 {
2178 // Dump specified images (by basename or fullpath)
2179 const char *arg_cstr;
2180 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2181 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002182 ModuleList module_list;
2183 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2184 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002185 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002186 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002187 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002188 Module *module = module_list.GetModulePointerAtIndex(i);
2189 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002190 {
2191 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002192 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002193 }
2194 }
2195 }
2196 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002197 {
2198 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002199 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002200
Greg Claytone1f50b92011-05-03 22:09:39 +00002201 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002202 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002203 }
2204 }
2205
2206 if (num_dumped > 0)
2207 result.SetStatus (eReturnStatusSuccessFinishResult);
2208 else
2209 {
2210 result.AppendError ("no matching executable images found");
2211 result.SetStatus (eReturnStatusFailed);
2212 }
2213 }
2214 return result.Succeeded();
2215 }
2216};
2217
2218
2219#pragma mark CommandObjectTargetModulesDumpSymfile
2220
2221//----------------------------------------------------------------------
2222// Image debug symbol dumping command
2223//----------------------------------------------------------------------
2224
2225class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2226{
2227public:
2228 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2229 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2230 "target modules dump symfile",
2231 "Dump the debug symbol file for one or more target modules.",
2232 //"target modules dump symfile [<file1> ...]")
2233 NULL)
2234 {
2235 }
2236
2237 virtual
2238 ~CommandObjectTargetModulesDumpSymfile ()
2239 {
2240 }
2241
Jim Inghamda26bd22012-06-08 21:56:10 +00002242protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002243 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002244 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002245 CommandReturnObject &result)
2246 {
2247 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2248 if (target == NULL)
2249 {
2250 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2251 result.SetStatus (eReturnStatusFailed);
2252 return false;
2253 }
2254 else
2255 {
2256 uint32_t num_dumped = 0;
2257
2258 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2259 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2260 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2261
2262 if (command.GetArgumentCount() == 0)
2263 {
2264 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002265 ModuleList &target_modules = target->GetImages();
2266 Mutex::Locker modules_locker (target_modules.GetMutex());
2267 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002268 if (num_modules > 0)
2269 {
2270 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2271 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2272 {
Jim Ingham93367902012-05-30 02:19:25 +00002273 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002274 num_dumped++;
2275 }
2276 }
2277 else
2278 {
2279 result.AppendError ("the target has no associated executable images");
2280 result.SetStatus (eReturnStatusFailed);
2281 return false;
2282 }
2283 }
2284 else
2285 {
2286 // Dump specified images (by basename or fullpath)
2287 const char *arg_cstr;
2288 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2289 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002290 ModuleList module_list;
2291 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2292 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002293 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002294 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002295 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002296 Module *module = module_list.GetModulePointerAtIndex(i);
2297 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002298 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002299 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002300 num_dumped++;
2301 }
2302 }
2303 }
2304 else
2305 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2306 }
2307 }
2308
2309 if (num_dumped > 0)
2310 result.SetStatus (eReturnStatusSuccessFinishResult);
2311 else
2312 {
2313 result.AppendError ("no matching executable images found");
2314 result.SetStatus (eReturnStatusFailed);
2315 }
2316 }
2317 return result.Succeeded();
2318 }
2319};
2320
2321
2322#pragma mark CommandObjectTargetModulesDumpLineTable
2323
2324//----------------------------------------------------------------------
2325// Image debug line table dumping command
2326//----------------------------------------------------------------------
2327
2328class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2329{
2330public:
2331 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2332 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2333 "target modules dump line-table",
2334 "Dump the debug symbol file for one or more target modules.",
2335 NULL)
2336 {
2337 }
2338
2339 virtual
2340 ~CommandObjectTargetModulesDumpLineTable ()
2341 {
2342 }
2343
Jim Inghamda26bd22012-06-08 21:56:10 +00002344protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002345 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002346 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002347 CommandReturnObject &result)
2348 {
2349 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2350 if (target == NULL)
2351 {
2352 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2353 result.SetStatus (eReturnStatusFailed);
2354 return false;
2355 }
2356 else
2357 {
2358 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2359 uint32_t total_num_dumped = 0;
2360
2361 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2362 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2363 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2364
2365 if (command.GetArgumentCount() == 0)
2366 {
2367 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2368 result.SetStatus (eReturnStatusFailed);
2369 }
2370 else
2371 {
2372 // Dump specified images (by basename or fullpath)
2373 const char *arg_cstr;
2374 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2375 {
2376 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002377
2378 ModuleList &target_modules = target->GetImages();
2379 Mutex::Locker modules_locker(target_modules.GetMutex());
2380 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002381 if (num_modules > 0)
2382 {
2383 uint32_t num_dumped = 0;
2384 for (uint32_t i = 0; i<num_modules; ++i)
2385 {
2386 if (DumpCompileUnitLineTable (m_interpreter,
2387 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002388 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002389 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002390 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002391 num_dumped++;
2392 }
2393 if (num_dumped == 0)
2394 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2395 else
2396 total_num_dumped += num_dumped;
2397 }
2398 }
2399 }
2400
2401 if (total_num_dumped > 0)
2402 result.SetStatus (eReturnStatusSuccessFinishResult);
2403 else
2404 {
2405 result.AppendError ("no source filenames matched any command arguments");
2406 result.SetStatus (eReturnStatusFailed);
2407 }
2408 }
2409 return result.Succeeded();
2410 }
2411};
2412
2413
2414#pragma mark CommandObjectTargetModulesDump
2415
2416//----------------------------------------------------------------------
2417// Dump multi-word command for target modules
2418//----------------------------------------------------------------------
2419
2420class CommandObjectTargetModulesDump : public CommandObjectMultiword
2421{
2422public:
2423
2424 //------------------------------------------------------------------
2425 // Constructors and Destructors
2426 //------------------------------------------------------------------
2427 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2428 CommandObjectMultiword (interpreter,
2429 "target modules dump",
2430 "A set of commands for dumping information about one or more target modules.",
2431 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2432 {
2433 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2434 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2435 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2436 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2437 }
2438
2439 virtual
2440 ~CommandObjectTargetModulesDump()
2441 {
2442 }
2443};
2444
Jim Inghamda26bd22012-06-08 21:56:10 +00002445class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002446{
2447public:
2448 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002449 CommandObjectParsed (interpreter,
2450 "target modules add",
2451 "Add a new module to the current target's modules.",
2452 "target modules add [<module>]")
Greg Claytone1f50b92011-05-03 22:09:39 +00002453 {
2454 }
2455
2456 virtual
2457 ~CommandObjectTargetModulesAdd ()
2458 {
2459 }
2460
Jim Inghamda26bd22012-06-08 21:56:10 +00002461 int
2462 HandleArgumentCompletion (Args &input,
2463 int &cursor_index,
2464 int &cursor_char_position,
2465 OptionElementVector &opt_element_vector,
2466 int match_start_point,
2467 int max_return_elements,
2468 bool &word_complete,
2469 StringList &matches)
2470 {
2471 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2472 completion_str.erase (cursor_char_position);
2473
2474 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2475 CommandCompletions::eDiskFileCompletion,
2476 completion_str.c_str(),
2477 match_start_point,
2478 max_return_elements,
2479 NULL,
2480 word_complete,
2481 matches);
2482 return matches.GetSize();
2483 }
2484
2485protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002486 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002487 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002488 CommandReturnObject &result)
2489 {
2490 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2491 if (target == NULL)
2492 {
2493 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2494 result.SetStatus (eReturnStatusFailed);
2495 return false;
2496 }
2497 else
2498 {
2499 const size_t argc = args.GetArgumentCount();
2500 if (argc == 0)
2501 {
2502 result.AppendError ("one or more executable image paths must be specified");
2503 result.SetStatus (eReturnStatusFailed);
2504 return false;
2505 }
2506 else
2507 {
2508 for (size_t i=0; i<argc; ++i)
2509 {
2510 const char *path = args.GetArgumentAtIndex(i);
2511 if (path)
2512 {
2513 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002514 if (file_spec.Exists())
2515 {
Greg Clayton444fe992012-02-26 05:51:37 +00002516 ModuleSpec module_spec (file_spec);
2517 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002518 if (!module_sp)
2519 {
2520 result.AppendError ("one or more executable image paths must be specified");
2521 result.SetStatus (eReturnStatusFailed);
2522 return false;
2523 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002524 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002525 }
2526 else
2527 {
2528 char resolved_path[PATH_MAX];
2529 result.SetStatus (eReturnStatusFailed);
2530 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2531 {
2532 if (strcmp (resolved_path, path) != 0)
2533 {
2534 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2535 break;
2536 }
2537 }
2538 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2539 break;
2540 }
2541 }
2542 }
2543 }
2544 }
2545 return result.Succeeded();
2546 }
2547
Greg Claytone1f50b92011-05-03 22:09:39 +00002548};
2549
2550class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2551{
2552public:
2553 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2554 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2555 "target modules load",
2556 "Set the load addresses for one or more sections in a target module.",
2557 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2558 m_option_group (interpreter),
2559 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."),
2560 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)
2561 {
2562 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2563 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2564 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2565 m_option_group.Finalize();
2566 }
2567
2568 virtual
2569 ~CommandObjectTargetModulesLoad ()
2570 {
2571 }
2572
Jim Inghamda26bd22012-06-08 21:56:10 +00002573 virtual Options *
2574 GetOptions ()
2575 {
2576 return &m_option_group;
2577 }
2578
2579protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002580 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002581 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002582 CommandReturnObject &result)
2583 {
2584 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2585 if (target == NULL)
2586 {
2587 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2588 result.SetStatus (eReturnStatusFailed);
2589 return false;
2590 }
2591 else
2592 {
2593 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002594 ModuleSpec module_spec;
2595 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002596 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002597 {
2598 search_using_module_spec = true;
2599 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2600 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002601
2602 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002603 {
2604 search_using_module_spec = true;
2605 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2606 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002607
Greg Clayton444fe992012-02-26 05:51:37 +00002608 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002609 {
2610
2611 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002612 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002613
2614 char path[PATH_MAX];
2615 if (num_matches == 1)
2616 {
2617 Module *module = matching_modules.GetModulePointerAtIndex(0);
2618 if (module)
2619 {
2620 ObjectFile *objfile = module->GetObjectFile();
2621 if (objfile)
2622 {
2623 SectionList *section_list = objfile->GetSectionList();
2624 if (section_list)
2625 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002626 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002627 if (argc == 0)
2628 {
2629 if (m_slide_option.GetOptionValue().OptionWasSet())
2630 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002631 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2632 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002633 }
2634 else
2635 {
2636 result.AppendError ("one or more section name + load address pair must be specified");
2637 result.SetStatus (eReturnStatusFailed);
2638 return false;
2639 }
2640 }
2641 else
2642 {
2643 if (m_slide_option.GetOptionValue().OptionWasSet())
2644 {
2645 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2646 result.SetStatus (eReturnStatusFailed);
2647 return false;
2648 }
2649
2650 for (size_t i=0; i<argc; i += 2)
2651 {
2652 const char *sect_name = args.GetArgumentAtIndex(i);
2653 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2654 if (sect_name && load_addr_cstr)
2655 {
2656 ConstString const_sect_name(sect_name);
2657 bool success = false;
2658 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2659 if (success)
2660 {
2661 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2662 if (section_sp)
2663 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002664 if (section_sp->IsThreadSpecific())
2665 {
2666 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2667 result.SetStatus (eReturnStatusFailed);
2668 break;
2669 }
2670 else
2671 {
Greg Clayton545762f2012-07-07 01:24:12 +00002672 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002673 changed = true;
2674 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2675 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002676 }
2677 else
2678 {
2679 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2680 result.SetStatus (eReturnStatusFailed);
2681 break;
2682 }
2683 }
2684 else
2685 {
2686 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2687 result.SetStatus (eReturnStatusFailed);
2688 break;
2689 }
2690 }
2691 else
2692 {
2693 if (sect_name)
2694 result.AppendError ("section names must be followed by a load address.\n");
2695 else
2696 result.AppendError ("one or more section name + load address pair must be specified.\n");
2697 result.SetStatus (eReturnStatusFailed);
2698 break;
2699 }
2700 }
2701 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002702
2703 if (changed)
2704 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002705 }
2706 else
2707 {
2708 module->GetFileSpec().GetPath (path, sizeof(path));
2709 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2710 result.SetStatus (eReturnStatusFailed);
2711 }
2712 }
2713 else
2714 {
2715 module->GetFileSpec().GetPath (path, sizeof(path));
2716 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2717 result.SetStatus (eReturnStatusFailed);
2718 }
2719 }
2720 else
2721 {
2722 module->GetFileSpec().GetPath (path, sizeof(path));
2723 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2724 result.SetStatus (eReturnStatusFailed);
2725 }
2726 }
2727 else
2728 {
2729 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002730
2731 if (module_spec.GetFileSpec())
2732 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002733 else
2734 path[0] = '\0';
2735
Greg Clayton444fe992012-02-26 05:51:37 +00002736 if (module_spec.GetUUIDPtr())
2737 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002738 else
2739 uuid_cstr[0] = '\0';
2740 if (num_matches > 1)
2741 {
2742 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2743 path[0] ? " file=" : "",
2744 path,
2745 uuid_cstr[0] ? " uuid=" : "",
2746 uuid_cstr);
2747 for (size_t i=0; i<num_matches; ++i)
2748 {
2749 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2750 result.AppendMessageWithFormat("%s\n", path);
2751 }
2752 }
2753 else
2754 {
2755 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2756 path[0] ? " file=" : "",
2757 path,
2758 uuid_cstr[0] ? " uuid=" : "",
2759 uuid_cstr);
2760 }
2761 result.SetStatus (eReturnStatusFailed);
2762 }
2763 }
2764 else
2765 {
2766 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2767 result.SetStatus (eReturnStatusFailed);
2768 return false;
2769 }
2770 }
2771 return result.Succeeded();
2772 }
2773
Greg Claytone1f50b92011-05-03 22:09:39 +00002774 OptionGroupOptions m_option_group;
2775 OptionGroupUUID m_uuid_option_group;
2776 OptionGroupFile m_file_option;
2777 OptionGroupUInt64 m_slide_option;
2778};
2779
2780//----------------------------------------------------------------------
2781// List images with associated information
2782//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002783class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002784{
2785public:
2786
2787 class CommandOptions : public Options
2788 {
2789 public:
2790
2791 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002792 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002793 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002794 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002795 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002796 {
2797 }
2798
2799 virtual
2800 ~CommandOptions ()
2801 {
2802 }
2803
2804 virtual Error
2805 SetOptionValue (uint32_t option_idx, const char *option_arg)
2806 {
2807 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002808 if (short_option == 'g')
2809 {
2810 m_use_global_module_list = true;
2811 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002812 else if (short_option == 'a')
2813 {
2814 bool success;
2815 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2816 if (!success)
2817 {
2818 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002819 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002820 }
2821 }
Greg Clayton899025f2011-08-09 00:01:09 +00002822 else
2823 {
2824 uint32_t width = 0;
2825 if (option_arg)
2826 width = strtoul (option_arg, NULL, 0);
2827 m_format_array.push_back(std::make_pair(short_option, width));
2828 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002829 Error error;
2830 return error;
2831 }
2832
2833 void
2834 OptionParsingStarting ()
2835 {
2836 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002837 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002838 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002839 }
2840
2841 const OptionDefinition*
2842 GetDefinitions ()
2843 {
2844 return g_option_table;
2845 }
2846
2847 // Options table: Required for subclasses of Options.
2848
2849 static OptionDefinition g_option_table[];
2850
2851 // Instance variables to hold the values for command options.
2852 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2853 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002854 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002855 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002856 };
2857
2858 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002859 CommandObjectParsed (interpreter,
2860 "target modules list",
2861 "List current executable and dependent shared library images.",
2862 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002863 m_options (interpreter)
2864 {
2865 }
2866
2867 virtual
2868 ~CommandObjectTargetModulesList ()
2869 {
2870 }
2871
2872 virtual
2873 Options *
2874 GetOptions ()
2875 {
2876 return &m_options;
2877 }
2878
Jim Inghamda26bd22012-06-08 21:56:10 +00002879protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002880 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002881 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002882 CommandReturnObject &result)
2883 {
2884 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002885 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00002886 // Define a local module list here to ensure it lives longer than any "locker"
2887 // object which might lock its contents below (through the "module_list_ptr"
2888 // variable).
2889 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00002890 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002891 {
2892 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2893 result.SetStatus (eReturnStatusFailed);
2894 return false;
2895 }
2896 else
2897 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002898 if (target)
2899 {
2900 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2901 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2902 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2903 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002904 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002905 Stream &strm = result.GetOutputStream();
2906
2907 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2908 {
2909 if (target)
2910 {
2911 Address module_address;
2912 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2913 {
Greg Clayton3508c382012-02-24 01:59:29 +00002914 ModuleSP module_sp (module_address.GetModule());
2915 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002916 {
Greg Clayton3508c382012-02-24 01:59:29 +00002917 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002918 result.SetStatus (eReturnStatusSuccessFinishResult);
2919 }
2920 else
2921 {
2922 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2923 result.SetStatus (eReturnStatusFailed);
2924 }
2925 }
2926 else
2927 {
2928 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2929 result.SetStatus (eReturnStatusFailed);
2930 }
2931 }
2932 else
2933 {
2934 result.AppendError ("Can only look up modules by address with a valid target.");
2935 result.SetStatus (eReturnStatusFailed);
2936 }
2937 return result.Succeeded();
2938 }
2939
Jim Ingham93367902012-05-30 02:19:25 +00002940 uint32_t num_modules = 0;
2941 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
2942 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
2943 // the global module list directly.
Greg Clayton2ad894b2012-05-15 18:43:44 +00002944 ModuleList *module_list_ptr = NULL;
2945 const size_t argc = command.GetArgumentCount();
2946 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00002947 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002948 if (use_global_module_list)
2949 {
2950 locker.Lock (Module::GetAllocationModuleCollectionMutex());
2951 num_modules = Module::GetNumberAllocatedModules();
2952 }
2953 else
2954 {
2955 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00002956 }
Greg Clayton899025f2011-08-09 00:01:09 +00002957 }
2958 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002959 {
2960 for (size_t i=0; i<argc; ++i)
2961 {
2962 // Dump specified images (by basename or fullpath)
2963 const char *arg_cstr = command.GetArgumentAtIndex(i);
2964 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2965 if (num_matches == 0)
2966 {
2967 if (argc == 1)
2968 {
2969 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
2970 result.SetStatus (eReturnStatusFailed);
2971 return false;
2972 }
2973 }
2974 }
2975
Greg Clayton2ad894b2012-05-15 18:43:44 +00002976 module_list_ptr = &module_list;
2977 }
Jim Ingham93367902012-05-30 02:19:25 +00002978
2979 if (module_list_ptr != NULL)
2980 {
2981 locker.Lock(module_list_ptr->GetMutex());
2982 num_modules = module_list_ptr->GetSize();
2983 }
Greg Clayton899025f2011-08-09 00:01:09 +00002984
Greg Claytone1f50b92011-05-03 22:09:39 +00002985 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002986 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002987 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2988 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002989 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002990 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00002991 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00002992 {
Jim Ingham93367902012-05-30 02:19:25 +00002993 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00002994 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002995 }
2996 else
2997 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002998 module = Module::GetAllocatedModuleAtIndex(image_idx);
2999 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003000 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003001
Greg Claytonb5a8f142012-02-05 02:38:54 +00003002 int indent = strm.Printf("[%3u] ", image_idx);
3003 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003004
Greg Claytone1f50b92011-05-03 22:09:39 +00003005 }
3006 result.SetStatus (eReturnStatusSuccessFinishResult);
3007 }
3008 else
3009 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003010 if (argc)
3011 {
3012 if (use_global_module_list)
3013 result.AppendError ("the global module list has no matching modules");
3014 else
3015 result.AppendError ("the target has no matching modules");
3016 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003017 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003018 {
3019 if (use_global_module_list)
3020 result.AppendError ("the global module list is empty");
3021 else
3022 result.AppendError ("the target has no associated executable images");
3023 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003024 result.SetStatus (eReturnStatusFailed);
3025 return false;
3026 }
3027 }
3028 return result.Succeeded();
3029 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003030
3031 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003032 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003033 {
3034
3035 bool dump_object_name = false;
3036 if (m_options.m_format_array.empty())
3037 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003038 m_options.m_format_array.push_back(std::make_pair('u', 0));
3039 m_options.m_format_array.push_back(std::make_pair('h', 0));
3040 m_options.m_format_array.push_back(std::make_pair('f', 0));
3041 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003042 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003043 const size_t num_entries = m_options.m_format_array.size();
3044 bool print_space = false;
3045 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003046 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003047 if (print_space)
3048 strm.PutChar(' ');
3049 print_space = true;
3050 const char format_char = m_options.m_format_array[i].first;
3051 uint32_t width = m_options.m_format_array[i].second;
3052 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003053 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003054 case 'A':
3055 DumpModuleArchitecture (strm, module, false, width);
3056 break;
3057
3058 case 't':
3059 DumpModuleArchitecture (strm, module, true, width);
3060 break;
3061
3062 case 'f':
3063 DumpFullpath (strm, &module->GetFileSpec(), width);
3064 dump_object_name = true;
3065 break;
3066
3067 case 'd':
3068 DumpDirectory (strm, &module->GetFileSpec(), width);
3069 break;
3070
3071 case 'b':
3072 DumpBasename (strm, &module->GetFileSpec(), width);
3073 dump_object_name = true;
3074 break;
3075
3076 case 'h':
3077 case 'o':
3078 // Image header address
3079 {
3080 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003081
Greg Claytonb5a8f142012-02-05 02:38:54 +00003082 ObjectFile *objfile = module->GetObjectFile ();
3083 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003084 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003085 Address header_addr(objfile->GetHeaderAddress());
3086 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003087 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003088 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003089 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003090 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3091 if (header_load_addr == LLDB_INVALID_ADDRESS)
3092 {
3093 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3094 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003095 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003096 {
3097 if (format_char == 'o')
3098 {
3099 // Show the offset of slide for the image
3100 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
3101 }
3102 else
3103 {
3104 // Show the load address of the image
3105 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
3106 }
3107 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003108 break;
3109 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003110 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3111 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3112 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003113 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003114 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003115 strm.Printf ("%*s", addr_nibble_width + 2, "");
3116 }
3117 break;
3118 case 'r':
3119 {
3120 uint32_t ref_count = 0;
3121 ModuleSP module_sp (module->shared_from_this());
3122 if (module_sp)
3123 {
3124 // Take one away to make sure we don't count our local "module_sp"
3125 ref_count = module_sp.use_count() - 1;
3126 }
3127 if (width)
3128 strm.Printf("{%*u}", width, ref_count);
3129 else
3130 strm.Printf("{%u}", ref_count);
3131 }
3132 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003133
Greg Claytonb5a8f142012-02-05 02:38:54 +00003134 case 's':
3135 case 'S':
3136 {
3137 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3138 if (symbol_vendor)
3139 {
3140 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3141 if (symbol_file)
3142 {
3143 if (format_char == 'S')
3144 {
3145 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3146 // Dump symbol file only if different from module file
3147 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3148 {
3149 print_space = false;
3150 break;
3151 }
3152 // Add a newline and indent past the index
3153 strm.Printf ("\n%*s", indent, "");
3154 }
3155 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3156 dump_object_name = true;
3157 break;
3158 }
3159 }
3160 strm.Printf("%.*s", width, "<NONE>");
3161 }
3162 break;
3163
3164 case 'm':
3165 module->GetModificationTime().Dump(&strm, width);
3166 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003167
Greg Claytonb5a8f142012-02-05 02:38:54 +00003168 case 'p':
3169 strm.Printf("%p", module);
3170 break;
3171
3172 case 'u':
3173 DumpModuleUUID(strm, module);
3174 break;
3175
3176 default:
3177 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003178 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003179
3180 }
3181 if (dump_object_name)
3182 {
3183 const char *object_name = module->GetObjectName().GetCString();
3184 if (object_name)
3185 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003186 }
3187 strm.EOL();
3188 }
3189
Greg Claytone1f50b92011-05-03 22:09:39 +00003190 CommandOptions m_options;
3191};
3192
3193OptionDefinition
3194CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3195{
Jim Ingham6bdea822011-10-24 18:36:33 +00003196 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3197 { 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 +00003198 { 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 +00003199 { 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."},
3200 { 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 +00003201 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3202 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3203 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3204 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3205 { 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 +00003206 { 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 +00003207 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3208 { 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."},
3209 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003210 { 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 +00003211 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3212};
3213
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003214#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003215
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003216//----------------------------------------------------------------------
3217// Lookup unwind information in images
3218//----------------------------------------------------------------------
3219
3220class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3221{
3222public:
3223
3224 enum
3225 {
3226 eLookupTypeInvalid = -1,
3227 eLookupTypeAddress = 0,
3228 eLookupTypeSymbol,
3229 eLookupTypeFunction,
3230 eLookupTypeFunctionOrSymbol,
3231 kNumLookupTypes
3232 };
3233
3234 class CommandOptions : public Options
3235 {
3236 public:
3237
3238 CommandOptions (CommandInterpreter &interpreter) :
3239 Options(interpreter),
3240 m_type(eLookupTypeInvalid),
3241 m_str(),
3242 m_addr(LLDB_INVALID_ADDRESS)
3243 {
3244 }
3245
3246 virtual
3247 ~CommandOptions ()
3248 {
3249 }
3250
3251 virtual Error
3252 SetOptionValue (uint32_t option_idx, const char *option_arg)
3253 {
3254 Error error;
3255
3256 char short_option = (char) m_getopt_table[option_idx].val;
3257
3258 switch (short_option)
3259 {
3260 case 'a':
3261 m_type = eLookupTypeAddress;
3262 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3263 if (m_addr == LLDB_INVALID_ADDRESS)
3264 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3265 break;
3266
3267 case 'n':
3268 m_str = option_arg;
3269 m_type = eLookupTypeFunctionOrSymbol;
3270 break;
3271 }
3272
3273 return error;
3274 }
3275
3276 void
3277 OptionParsingStarting ()
3278 {
3279 m_type = eLookupTypeInvalid;
3280 m_str.clear();
3281 m_addr = LLDB_INVALID_ADDRESS;
3282 }
3283
3284 const OptionDefinition*
3285 GetDefinitions ()
3286 {
3287 return g_option_table;
3288 }
3289
3290 // Options table: Required for subclasses of Options.
3291
3292 static OptionDefinition g_option_table[];
3293
3294 // Instance variables to hold the values for command options.
3295
3296 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3297 std::string m_str; // Holds name lookup
3298 lldb::addr_t m_addr; // Holds the address to lookup
3299 };
3300
3301 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3302 CommandObjectParsed (interpreter,
3303 "target modules show-unwind",
3304 "Show synthesized unwind instructions for a function.",
3305 NULL),
3306 m_options (interpreter)
3307 {
3308 }
3309
3310 virtual
3311 ~CommandObjectTargetModulesShowUnwind ()
3312 {
3313 }
3314
3315 virtual
3316 Options *
3317 GetOptions ()
3318 {
3319 return &m_options;
3320 }
3321
3322protected:
3323 bool
3324 DoExecute (Args& command,
3325 CommandReturnObject &result)
3326 {
3327 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3328 if (!target)
3329 {
3330 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3331 result.SetStatus (eReturnStatusFailed);
3332 return false;
3333 }
3334
3335 ExecutionContext exe_ctx = m_interpreter.GetDebugger().GetSelectedExecutionContext();
3336 Process *process = exe_ctx.GetProcessPtr();
3337 ABI *abi = NULL;
3338 if (process)
3339 abi = process->GetABI().get();
3340
3341 if (process == NULL)
3342 {
3343 result.AppendError ("You must have a process running to use this command.");
3344 result.SetStatus (eReturnStatusFailed);
3345 return false;
3346 }
3347
3348 ThreadList threads(process->GetThreadList());
3349 if (threads.GetSize() == 0)
3350 {
3351 result.AppendError ("The process must be paused to use this command.");
3352 result.SetStatus (eReturnStatusFailed);
3353 return false;
3354 }
3355
3356 ThreadSP thread(threads.GetThreadAtIndex(0));
3357 if (thread.get() == NULL)
3358 {
3359 result.AppendError ("The process must be paused to use this command.");
3360 result.SetStatus (eReturnStatusFailed);
3361 return false;
3362 }
3363
3364 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3365 {
3366 SymbolContextList sc_list;
3367 uint32_t num_matches;
3368 ConstString function_name (m_options.m_str.c_str());
3369 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3370 for (uint32_t idx = 0; idx < num_matches; idx++)
3371 {
3372 SymbolContext sc;
3373 sc_list.GetContextAtIndex(idx, sc);
3374 if (sc.symbol == NULL && sc.function == NULL)
3375 continue;
3376 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3377 continue;
3378 AddressRange range;
3379 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3380 continue;
3381 if (!range.GetBaseAddress().IsValid())
3382 continue;
3383 ConstString funcname(sc.GetFunctionName());
3384 if (funcname.IsEmpty())
3385 continue;
3386 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3387 if (abi)
3388 start_addr = abi->FixCodeAddress(start_addr);
3389
3390 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3391 if (func_unwinders_sp.get() == NULL)
3392 continue;
3393
3394 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3395 if (first_non_prologue_insn.IsValid())
3396 {
3397 result.GetOutputStream().Printf("First non-prologue instruction is at address 0x%llx or offset %lld into the function.\n", first_non_prologue_insn.GetLoadAddress(target), first_non_prologue_insn.GetLoadAddress(target) - start_addr);
3398 result.GetOutputStream().Printf ("\n");
3399 }
3400
3401 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3402 if (non_callsite_unwind_plan.get())
3403 {
3404 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3405 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3406 result.GetOutputStream().Printf ("\n");
3407 }
3408
3409 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3410 if (callsite_unwind_plan.get())
3411 {
3412 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3413 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3414 result.GetOutputStream().Printf ("\n");
3415 }
3416
3417 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3418 if (arch_default_unwind_plan.get())
3419 {
3420 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3421 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3422 result.GetOutputStream().Printf ("\n");
3423 }
3424
3425 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3426 if (fast_unwind_plan.get())
3427 {
3428 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3429 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3430 result.GetOutputStream().Printf ("\n");
3431 }
3432
3433
3434 result.GetOutputStream().Printf ("\n");
3435 }
3436 }
3437 return result.Succeeded();
3438 }
3439
3440 CommandOptions m_options;
3441};
3442
3443OptionDefinition
3444CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3445{
3446 { LLDB_OPT_SET_1, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function or symbol by name in one or more target modules."},
3447 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3448};
Greg Claytone1f50b92011-05-03 22:09:39 +00003449
3450//----------------------------------------------------------------------
3451// Lookup information in images
3452//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003453class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003454{
3455public:
3456
3457 enum
3458 {
3459 eLookupTypeInvalid = -1,
3460 eLookupTypeAddress = 0,
3461 eLookupTypeSymbol,
3462 eLookupTypeFileLine, // Line is optional
3463 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003464 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003465 eLookupTypeType,
3466 kNumLookupTypes
3467 };
3468
3469 class CommandOptions : public Options
3470 {
3471 public:
3472
3473 CommandOptions (CommandInterpreter &interpreter) :
3474 Options(interpreter)
3475 {
3476 OptionParsingStarting();
3477 }
3478
3479 virtual
3480 ~CommandOptions ()
3481 {
3482 }
3483
3484 virtual Error
3485 SetOptionValue (uint32_t option_idx, const char *option_arg)
3486 {
3487 Error error;
3488
3489 char short_option = (char) m_getopt_table[option_idx].val;
3490
3491 switch (short_option)
3492 {
3493 case 'a':
3494 m_type = eLookupTypeAddress;
3495 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3496 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003497 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003498 break;
3499
3500 case 'o':
3501 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3502 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003503 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003504 break;
3505
3506 case 's':
3507 m_str = option_arg;
3508 m_type = eLookupTypeSymbol;
3509 break;
3510
3511 case 'f':
3512 m_file.SetFile (option_arg, false);
3513 m_type = eLookupTypeFileLine;
3514 break;
3515
3516 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003517 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003518 break;
3519
3520 case 'l':
3521 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3522 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003523 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003524 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003525 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003526 m_type = eLookupTypeFileLine;
3527 break;
3528
Greg Clayton2ad894b2012-05-15 18:43:44 +00003529 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003530 m_str = option_arg;
3531 m_type = eLookupTypeFunction;
3532 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003533
3534 case 'n':
3535 m_str = option_arg;
3536 m_type = eLookupTypeFunctionOrSymbol;
3537 break;
3538
Greg Claytone1f50b92011-05-03 22:09:39 +00003539 case 't':
3540 m_str = option_arg;
3541 m_type = eLookupTypeType;
3542 break;
3543
3544 case 'v':
3545 m_verbose = 1;
3546 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003547
3548 case 'A':
3549 m_print_all = true;
3550 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003551
3552 case 'r':
3553 m_use_regex = true;
3554 break;
3555 }
3556
3557 return error;
3558 }
3559
3560 void
3561 OptionParsingStarting ()
3562 {
3563 m_type = eLookupTypeInvalid;
3564 m_str.clear();
3565 m_file.Clear();
3566 m_addr = LLDB_INVALID_ADDRESS;
3567 m_offset = 0;
3568 m_line_number = 0;
3569 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003570 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003571 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003572 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003573 }
3574
3575 const OptionDefinition*
3576 GetDefinitions ()
3577 {
3578 return g_option_table;
3579 }
3580
3581 // Options table: Required for subclasses of Options.
3582
3583 static OptionDefinition g_option_table[];
3584 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3585 std::string m_str; // Holds name lookup
3586 FileSpec m_file; // Files for file lookups
3587 lldb::addr_t m_addr; // Holds the address to lookup
3588 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3589 uint32_t m_line_number; // Line number for file+line lookups
3590 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003591 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003592 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003593 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003594
3595 };
3596
3597 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003598 CommandObjectParsed (interpreter,
3599 "target modules lookup",
3600 "Look up information within executable and dependent shared library images.",
3601 NULL),
3602 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003603 {
3604 CommandArgumentEntry arg;
3605 CommandArgumentData file_arg;
3606
3607 // Define the first (and only) variant of this arg.
3608 file_arg.arg_type = eArgTypeFilename;
3609 file_arg.arg_repetition = eArgRepeatStar;
3610
3611 // There is only one variant this argument could be; put it into the argument entry.
3612 arg.push_back (file_arg);
3613
3614 // Push the data for the first argument into the m_arguments vector.
3615 m_arguments.push_back (arg);
3616 }
3617
3618 virtual
3619 ~CommandObjectTargetModulesLookup ()
3620 {
3621 }
3622
3623 virtual Options *
3624 GetOptions ()
3625 {
3626 return &m_options;
3627 }
3628
Sean Callanan56d31ec2012-06-06 20:49:55 +00003629 bool
3630 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3631 {
3632 switch (m_options.m_type)
3633 {
3634 case eLookupTypeAddress:
3635 case eLookupTypeFileLine:
3636 case eLookupTypeFunction:
3637 case eLookupTypeFunctionOrSymbol:
3638 case eLookupTypeSymbol:
3639 default:
3640 return false;
3641 case eLookupTypeType:
3642 break;
3643 }
3644
3645 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3646
3647 StackFrameSP frame = exe_ctx.GetFrameSP();
3648
3649 if (!frame)
3650 return false;
3651
3652 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3653
3654 if (!sym_ctx.module_sp)
3655 return false;
3656
3657 switch (m_options.m_type)
3658 {
3659 default:
3660 return false;
3661 case eLookupTypeType:
3662 if (!m_options.m_str.empty())
3663 {
3664 if (LookupTypeHere (m_interpreter,
3665 result.GetOutputStream(),
3666 sym_ctx,
3667 m_options.m_str.c_str(),
3668 m_options.m_use_regex))
3669 {
3670 result.SetStatus(eReturnStatusSuccessFinishResult);
3671 return true;
3672 }
3673 }
3674 break;
3675 }
3676
3677 return true;
3678 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003679
3680 bool
3681 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3682 {
3683 switch (m_options.m_type)
3684 {
3685 case eLookupTypeAddress:
3686 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3687 {
3688 if (LookupAddressInModule (m_interpreter,
3689 result.GetOutputStream(),
3690 module,
3691 eSymbolContextEverything,
3692 m_options.m_addr,
3693 m_options.m_offset,
3694 m_options.m_verbose))
3695 {
3696 result.SetStatus(eReturnStatusSuccessFinishResult);
3697 return true;
3698 }
3699 }
3700 break;
3701
3702 case eLookupTypeSymbol:
3703 if (!m_options.m_str.empty())
3704 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003705 if (LookupSymbolInModule (m_interpreter,
3706 result.GetOutputStream(),
3707 module,
3708 m_options.m_str.c_str(),
3709 m_options.m_use_regex,
3710 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003711 {
3712 result.SetStatus(eReturnStatusSuccessFinishResult);
3713 return true;
3714 }
3715 }
3716 break;
3717
3718 case eLookupTypeFileLine:
3719 if (m_options.m_file)
3720 {
3721
3722 if (LookupFileAndLineInModule (m_interpreter,
3723 result.GetOutputStream(),
3724 module,
3725 m_options.m_file,
3726 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003727 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003728 m_options.m_verbose))
3729 {
3730 result.SetStatus(eReturnStatusSuccessFinishResult);
3731 return true;
3732 }
3733 }
3734 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003735
3736 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003737 case eLookupTypeFunction:
3738 if (!m_options.m_str.empty())
3739 {
3740 if (LookupFunctionInModule (m_interpreter,
3741 result.GetOutputStream(),
3742 module,
3743 m_options.m_str.c_str(),
3744 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003745 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003746 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003747 m_options.m_verbose))
3748 {
3749 result.SetStatus(eReturnStatusSuccessFinishResult);
3750 return true;
3751 }
3752 }
3753 break;
3754
Greg Clayton2ad894b2012-05-15 18:43:44 +00003755
Greg Claytone1f50b92011-05-03 22:09:39 +00003756 case eLookupTypeType:
3757 if (!m_options.m_str.empty())
3758 {
3759 if (LookupTypeInModule (m_interpreter,
3760 result.GetOutputStream(),
3761 module,
3762 m_options.m_str.c_str(),
3763 m_options.m_use_regex))
3764 {
3765 result.SetStatus(eReturnStatusSuccessFinishResult);
3766 return true;
3767 }
3768 }
3769 break;
3770
3771 default:
3772 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3773 syntax_error = true;
3774 break;
3775 }
3776
3777 result.SetStatus (eReturnStatusFailed);
3778 return false;
3779 }
3780
Jim Inghamda26bd22012-06-08 21:56:10 +00003781protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003782 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003783 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003784 CommandReturnObject &result)
3785 {
3786 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3787 if (target == NULL)
3788 {
3789 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3790 result.SetStatus (eReturnStatusFailed);
3791 return false;
3792 }
3793 else
3794 {
3795 bool syntax_error = false;
3796 uint32_t i;
3797 uint32_t num_successful_lookups = 0;
3798 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3799 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3800 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3801 // Dump all sections for all modules images
3802
3803 if (command.GetArgumentCount() == 0)
3804 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003805 ModuleSP current_module;
3806
3807 // Where it is possible to look in the current symbol context
3808 // first, try that. If this search was successful and --all
3809 // was not passed, don't print anything else.
3810 if (LookupHere (m_interpreter, result, syntax_error))
3811 {
3812 result.GetOutputStream().EOL();
3813 num_successful_lookups++;
3814 if (!m_options.m_print_all)
3815 {
3816 result.SetStatus (eReturnStatusSuccessFinishResult);
3817 return result.Succeeded();
3818 }
3819 }
3820
3821 // Dump all sections for all other modules
3822
Jim Ingham93367902012-05-30 02:19:25 +00003823 ModuleList &target_modules = target->GetImages();
3824 Mutex::Locker modules_locker(target_modules.GetMutex());
3825 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003826 if (num_modules > 0)
3827 {
3828 for (i = 0; i<num_modules && syntax_error == false; ++i)
3829 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003830 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3831
3832 if (module_pointer != current_module.get() &&
3833 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003834 {
3835 result.GetOutputStream().EOL();
3836 num_successful_lookups++;
3837 }
3838 }
3839 }
3840 else
3841 {
3842 result.AppendError ("the target has no associated executable images");
3843 result.SetStatus (eReturnStatusFailed);
3844 return false;
3845 }
3846 }
3847 else
3848 {
3849 // Dump specified images (by basename or fullpath)
3850 const char *arg_cstr;
3851 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3852 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003853 ModuleList module_list;
3854 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3855 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003856 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003857 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00003858 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003859 Module *module = module_list.GetModulePointerAtIndex(i);
3860 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003861 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003862 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003863 {
3864 result.GetOutputStream().EOL();
3865 num_successful_lookups++;
3866 }
3867 }
3868 }
3869 }
3870 else
3871 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3872 }
3873 }
3874
3875 if (num_successful_lookups > 0)
3876 result.SetStatus (eReturnStatusSuccessFinishResult);
3877 else
3878 result.SetStatus (eReturnStatusFailed);
3879 }
3880 return result.Succeeded();
3881 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003882
3883 CommandOptions m_options;
3884};
3885
3886OptionDefinition
3887CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3888{
3889 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3890 { 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 +00003891 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3892 /* 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 +00003893 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
Greg Claytone1f50b92011-05-03 22:09:39 +00003894 { 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 +00003895 { 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."},
3896 { 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 +00003897 { LLDB_OPT_SET_FROM_TO(3,5),
3898 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 +00003899 { 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."},
3900 { 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."},
3901 { 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 +00003902 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
Sean Callanan56d31ec2012-06-06 20:49:55 +00003903 { 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 +00003904 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003905};
Chris Lattner24943d22010-06-08 16:52:24 +00003906
3907
Jim Inghamd60d94a2011-03-11 03:53:59 +00003908#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003909
3910//-------------------------------------------------------------------------
3911// CommandObjectMultiwordImageSearchPaths
3912//-------------------------------------------------------------------------
3913
Greg Claytone1f50b92011-05-03 22:09:39 +00003914class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003915{
3916public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003917
3918 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3919 CommandObjectMultiword (interpreter,
3920 "target modules search-paths",
3921 "A set of commands for operating on debugger target image search paths.",
3922 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003923 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003924 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3925 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3926 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3927 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3928 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003929 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003930
3931 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003932 {
3933 }
3934};
3935
Greg Claytone1f50b92011-05-03 22:09:39 +00003936
3937
3938#pragma mark CommandObjectTargetModules
3939
3940//-------------------------------------------------------------------------
3941// CommandObjectTargetModules
3942//-------------------------------------------------------------------------
3943
3944class CommandObjectTargetModules : public CommandObjectMultiword
3945{
3946public:
3947 //------------------------------------------------------------------
3948 // Constructors and Destructors
3949 //------------------------------------------------------------------
3950 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3951 CommandObjectMultiword (interpreter,
3952 "target modules",
3953 "A set of commands for accessing information for one or more target modules.",
3954 "target modules <sub-command> ...")
3955 {
3956 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3957 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3958 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3959 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3960 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3961 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3962 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003963 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00003964
3965 }
3966 virtual
3967 ~CommandObjectTargetModules()
3968 {
3969 }
3970
3971private:
3972 //------------------------------------------------------------------
3973 // For CommandObjectTargetModules only
3974 //------------------------------------------------------------------
3975 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3976};
3977
3978
Greg Clayton3508c382012-02-24 01:59:29 +00003979
Jim Inghamda26bd22012-06-08 21:56:10 +00003980class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00003981{
3982public:
3983 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003984 CommandObjectParsed (interpreter,
3985 "target symbols add",
3986 "Add a debug symbol file to one of the target's current modules.",
3987 "target symbols add [<symfile>]")
Greg Clayton3508c382012-02-24 01:59:29 +00003988 {
3989 }
3990
3991 virtual
3992 ~CommandObjectTargetSymbolsAdd ()
3993 {
3994 }
3995
Jim Inghamda26bd22012-06-08 21:56:10 +00003996 int
3997 HandleArgumentCompletion (Args &input,
3998 int &cursor_index,
3999 int &cursor_char_position,
4000 OptionElementVector &opt_element_vector,
4001 int match_start_point,
4002 int max_return_elements,
4003 bool &word_complete,
4004 StringList &matches)
4005 {
4006 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4007 completion_str.erase (cursor_char_position);
4008
4009 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4010 CommandCompletions::eDiskFileCompletion,
4011 completion_str.c_str(),
4012 match_start_point,
4013 max_return_elements,
4014 NULL,
4015 word_complete,
4016 matches);
4017 return matches.GetSize();
4018 }
4019
4020protected:
Greg Clayton3508c382012-02-24 01:59:29 +00004021 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004022 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004023 CommandReturnObject &result)
4024 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004025 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
4026 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton3508c382012-02-24 01:59:29 +00004027 if (target == NULL)
4028 {
4029 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4030 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004031 }
4032 else
4033 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004034 bool flush = false;
Greg Clayton3508c382012-02-24 01:59:29 +00004035 const size_t argc = args.GetArgumentCount();
4036 if (argc == 0)
4037 {
4038 result.AppendError ("one or more symbol file paths must be specified");
4039 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004040 }
4041 else
4042 {
4043 for (size_t i=0; i<argc; ++i)
4044 {
4045 const char *symfile_path = args.GetArgumentAtIndex(i);
4046 if (symfile_path)
4047 {
4048 FileSpec symfile_spec(symfile_path, true);
4049 ArchSpec arch;
4050 if (symfile_spec.Exists())
4051 {
4052 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
Johnny Chen9262cd52012-08-22 00:18:43 +00004053 const UUID &symfile_uuid = symfile_module_sp->GetUUID();
4054 StreamString ss_symfile_uuid;
Johnny Chen11d6e5e2012-08-28 21:01:31 +00004055 symfile_uuid.Dump(&ss_symfile_uuid);
Johnny Chen9262cd52012-08-22 00:18:43 +00004056
Greg Clayton3508c382012-02-24 01:59:29 +00004057 if (symfile_module_sp)
4058 {
4059 // We now have a module that represents a symbol file
4060 // that can be used for a module that might exist in the
4061 // current target, so we need to find that module in the
4062 // target
4063
Johnny Chen9262cd52012-08-22 00:18:43 +00004064 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_uuid));
Greg Clayton3508c382012-02-24 01:59:29 +00004065 if (old_module_sp)
4066 {
Greg Claytond4f16c82012-03-29 21:43:25 +00004067 // The module has not yet created its symbol vendor, we can just
4068 // give the existing target module the symfile path to use for
4069 // when it decides to create it!
4070 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
Greg Clayton3508c382012-02-24 01:59:29 +00004071
Johnny Chen9262cd52012-08-22 00:18:43 +00004072 // Provide feedback that the symfile has been successfully added.
4073 const FileSpec &module_fs = old_module_sp->GetFileSpec();
4074 result.AppendMessageWithFormat("symbol file '%s' with UUID %s has been successfully added to the '%s/%s' module\n",
4075 symfile_path, ss_symfile_uuid.GetData(),
4076 module_fs.GetDirectory().AsCString(), module_fs.GetFilename().AsCString());
4077
Greg Claytond4f16c82012-03-29 21:43:25 +00004078 // Let clients know something changed in the module
4079 // if it is currently loaded
4080 ModuleList module_list;
4081 module_list.Append (old_module_sp);
4082 target->ModulesDidLoad (module_list);
Greg Claytoncf5927e2012-05-18 02:38:05 +00004083 flush = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004084 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004085 else
4086 {
4087 result.AppendErrorWithFormat ("symbol file '%s' with UUID %s does not match any existing module%s\n",
4088 symfile_path, ss_symfile_uuid.GetData(),
4089 (symfile_spec.GetFileType() != FileSpec::eFileTypeRegular)
4090 ? "\n please specify the full path to the symbol file"
4091 : "");
4092 break;
4093 }
Greg Clayton3508c382012-02-24 01:59:29 +00004094 }
4095 else
4096 {
4097 result.AppendError ("one or more executable image paths must be specified");
4098 result.SetStatus (eReturnStatusFailed);
Greg Claytoncf5927e2012-05-18 02:38:05 +00004099 break;
Greg Clayton3508c382012-02-24 01:59:29 +00004100 }
4101 result.SetStatus (eReturnStatusSuccessFinishResult);
4102 }
4103 else
4104 {
4105 char resolved_symfile_path[PATH_MAX];
4106 result.SetStatus (eReturnStatusFailed);
4107 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4108 {
4109 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4110 {
4111 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4112 break;
4113 }
4114 }
4115 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4116 break;
4117 }
4118 }
4119 }
4120 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004121
4122 if (flush)
4123 {
4124 Process *process = exe_ctx.GetProcessPtr();
4125 if (process)
4126 process->Flush();
4127 }
Greg Clayton3508c382012-02-24 01:59:29 +00004128 }
4129 return result.Succeeded();
4130 }
4131
Greg Clayton3508c382012-02-24 01:59:29 +00004132};
4133
4134
4135#pragma mark CommandObjectTargetSymbols
4136
4137//-------------------------------------------------------------------------
4138// CommandObjectTargetSymbols
4139//-------------------------------------------------------------------------
4140
4141class CommandObjectTargetSymbols : public CommandObjectMultiword
4142{
4143public:
4144 //------------------------------------------------------------------
4145 // Constructors and Destructors
4146 //------------------------------------------------------------------
4147 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4148 CommandObjectMultiword (interpreter,
4149 "target symbols",
4150 "A set of commands for adding and managing debug symbol files.",
4151 "target symbols <sub-command> ...")
4152 {
4153 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4154
4155 }
4156 virtual
4157 ~CommandObjectTargetSymbols()
4158 {
4159 }
4160
4161private:
4162 //------------------------------------------------------------------
4163 // For CommandObjectTargetModules only
4164 //------------------------------------------------------------------
4165 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4166};
4167
4168
Jim Inghamd60d94a2011-03-11 03:53:59 +00004169#pragma mark CommandObjectTargetStopHookAdd
4170
4171//-------------------------------------------------------------------------
4172// CommandObjectTargetStopHookAdd
4173//-------------------------------------------------------------------------
4174
Jim Inghamda26bd22012-06-08 21:56:10 +00004175class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004176{
4177public:
4178
4179 class CommandOptions : public Options
4180 {
4181 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004182 CommandOptions (CommandInterpreter &interpreter) :
4183 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004184 m_line_start(0),
4185 m_line_end (UINT_MAX),
4186 m_func_name_type_mask (eFunctionNameTypeAuto),
4187 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004188 m_thread_specified (false),
4189 m_use_one_liner (false),
4190 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004191 {
4192 }
4193
4194 ~CommandOptions () {}
4195
Greg Claytonb3448432011-03-24 21:19:54 +00004196 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004197 GetDefinitions ()
4198 {
4199 return g_option_table;
4200 }
4201
4202 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004203 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004204 {
4205 Error error;
4206 char short_option = (char) m_getopt_table[option_idx].val;
4207 bool success;
4208
4209 switch (short_option)
4210 {
4211 case 'c':
4212 m_class_name = option_arg;
4213 m_sym_ctx_specified = true;
4214 break;
4215
4216 case 'e':
4217 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4218 if (!success)
4219 {
Greg Clayton9c236732011-10-26 00:56:27 +00004220 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004221 break;
4222 }
4223 m_sym_ctx_specified = true;
4224 break;
4225
4226 case 'l':
4227 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4228 if (!success)
4229 {
Greg Clayton9c236732011-10-26 00:56:27 +00004230 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004231 break;
4232 }
4233 m_sym_ctx_specified = true;
4234 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004235
4236 case 'i':
4237 m_no_inlines = true;
4238 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004239
4240 case 'n':
4241 m_function_name = option_arg;
4242 m_func_name_type_mask |= eFunctionNameTypeAuto;
4243 m_sym_ctx_specified = true;
4244 break;
4245
4246 case 'f':
4247 m_file_name = option_arg;
4248 m_sym_ctx_specified = true;
4249 break;
4250 case 's':
4251 m_module_name = option_arg;
4252 m_sym_ctx_specified = true;
4253 break;
4254 case 't' :
4255 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004256 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004257 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004258 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004259 m_thread_specified = true;
4260 }
4261 break;
4262 case 'T':
4263 m_thread_name = option_arg;
4264 m_thread_specified = true;
4265 break;
4266 case 'q':
4267 m_queue_name = option_arg;
4268 m_thread_specified = true;
4269 break;
4270 case 'x':
4271 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004272 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004273 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004274 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004275 m_thread_specified = true;
4276 }
4277 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004278 case 'o':
4279 m_use_one_liner = true;
4280 m_one_liner = option_arg;
4281 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004282 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004283 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004284 break;
4285 }
4286 return error;
4287 }
4288
4289 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004290 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004291 {
4292 m_class_name.clear();
4293 m_function_name.clear();
4294 m_line_start = 0;
4295 m_line_end = UINT_MAX;
4296 m_file_name.clear();
4297 m_module_name.clear();
4298 m_func_name_type_mask = eFunctionNameTypeAuto;
4299 m_thread_id = LLDB_INVALID_THREAD_ID;
4300 m_thread_index = UINT32_MAX;
4301 m_thread_name.clear();
4302 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004303
4304 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004305 m_sym_ctx_specified = false;
4306 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004307
4308 m_use_one_liner = false;
4309 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004310 }
4311
4312
Greg Claytonb3448432011-03-24 21:19:54 +00004313 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004314
4315 std::string m_class_name;
4316 std::string m_function_name;
4317 uint32_t m_line_start;
4318 uint32_t m_line_end;
4319 std::string m_file_name;
4320 std::string m_module_name;
4321 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4322 lldb::tid_t m_thread_id;
4323 uint32_t m_thread_index;
4324 std::string m_thread_name;
4325 std::string m_queue_name;
4326 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004327 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004328 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004329 // Instance variables to hold the values for one_liner options.
4330 bool m_use_one_liner;
4331 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004332 };
4333
4334 Options *
4335 GetOptions ()
4336 {
4337 return &m_options;
4338 }
4339
4340 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004341 CommandObjectParsed (interpreter,
4342 "target stop-hook add ",
4343 "Add a hook to be executed when the target stops.",
4344 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004345 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004346 {
4347 }
4348
4349 ~CommandObjectTargetStopHookAdd ()
4350 {
4351 }
4352
4353 static size_t
4354 ReadCommandsCallbackFunction (void *baton,
4355 InputReader &reader,
4356 lldb::InputReaderAction notification,
4357 const char *bytes,
4358 size_t bytes_len)
4359 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004360 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004361 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004362 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004363 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004364
4365 switch (notification)
4366 {
4367 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004368 if (!batch_mode)
4369 {
4370 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4371 if (reader.GetPrompt())
4372 out_stream->Printf ("%s", reader.GetPrompt());
4373 out_stream->Flush();
4374 }
Jim Inghame15511a2011-05-05 01:03:36 +00004375 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004376 break;
4377
4378 case eInputReaderDeactivate:
4379 break;
4380
4381 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004382 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004383 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004384 out_stream->Printf ("%s", reader.GetPrompt());
4385 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004386 }
Jim Inghame15511a2011-05-05 01:03:36 +00004387 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004388 break;
4389
Caroline Tice4a348082011-05-02 20:41:46 +00004390 case eInputReaderAsynchronousOutputWritten:
4391 break;
4392
Jim Inghamd60d94a2011-03-11 03:53:59 +00004393 case eInputReaderGotToken:
4394 if (bytes && bytes_len && baton)
4395 {
4396 StringList *commands = new_stop_hook->GetCommandPointer();
4397 if (commands)
4398 {
4399 commands->AppendString (bytes, bytes_len);
4400 }
4401 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004402 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004403 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004404 out_stream->Printf ("%s", reader.GetPrompt());
4405 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004406 }
4407 break;
4408
4409 case eInputReaderInterrupt:
4410 {
4411 // Finish, and cancel the stop hook.
4412 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004413 if (!batch_mode)
4414 {
4415 out_stream->Printf ("Stop hook cancelled.\n");
4416 out_stream->Flush();
4417 }
4418
Jim Inghamd60d94a2011-03-11 03:53:59 +00004419 reader.SetIsDone (true);
4420 }
Jim Inghame15511a2011-05-05 01:03:36 +00004421 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004422 break;
4423
4424 case eInputReaderEndOfFile:
4425 reader.SetIsDone (true);
4426 break;
4427
4428 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004429 if (!got_interrupted && !batch_mode)
4430 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004431 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004432 out_stream->Flush();
4433 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004434 break;
4435 }
4436
4437 return bytes_len;
4438 }
4439
Jim Inghamda26bd22012-06-08 21:56:10 +00004440protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004441 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004442 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004443 {
4444 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4445 if (target)
4446 {
4447 Target::StopHookSP new_hook_sp;
4448 target->AddStopHook (new_hook_sp);
4449
4450 // First step, make the specifier.
4451 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4452 if (m_options.m_sym_ctx_specified)
4453 {
4454 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4455
4456 if (!m_options.m_module_name.empty())
4457 {
4458 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4459 }
4460
4461 if (!m_options.m_class_name.empty())
4462 {
4463 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4464 }
4465
4466 if (!m_options.m_file_name.empty())
4467 {
4468 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4469 }
4470
4471 if (m_options.m_line_start != 0)
4472 {
4473 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4474 }
4475
4476 if (m_options.m_line_end != UINT_MAX)
4477 {
4478 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4479 }
4480
4481 if (!m_options.m_function_name.empty())
4482 {
4483 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4484 }
4485 }
4486
4487 if (specifier_ap.get())
4488 new_hook_sp->SetSpecifier (specifier_ap.release());
4489
4490 // Next see if any of the thread options have been entered:
4491
4492 if (m_options.m_thread_specified)
4493 {
4494 ThreadSpec *thread_spec = new ThreadSpec();
4495
4496 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4497 {
4498 thread_spec->SetTID (m_options.m_thread_id);
4499 }
4500
4501 if (m_options.m_thread_index != UINT32_MAX)
4502 thread_spec->SetIndex (m_options.m_thread_index);
4503
4504 if (!m_options.m_thread_name.empty())
4505 thread_spec->SetName (m_options.m_thread_name.c_str());
4506
4507 if (!m_options.m_queue_name.empty())
4508 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4509
4510 new_hook_sp->SetThreadSpecifier (thread_spec);
4511
4512 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004513 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004514 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004515 // Use one-liner.
4516 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004517 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004518 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004519 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004520 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004521 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4522 // the new stop hook's command string.
4523 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4524 if (!reader_sp)
4525 {
4526 result.AppendError("out of memory\n");
4527 result.SetStatus (eReturnStatusFailed);
4528 target->RemoveStopHookByID (new_hook_sp->GetID());
4529 return false;
4530 }
4531
4532 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4533 new_hook_sp.get(), // baton
4534 eInputReaderGranularityLine, // token size, to pass to callback function
4535 "DONE", // end token
4536 "> ", // prompt
4537 true)); // echo input
4538 if (!err.Success())
4539 {
4540 result.AppendError (err.AsCString());
4541 result.SetStatus (eReturnStatusFailed);
4542 target->RemoveStopHookByID (new_hook_sp->GetID());
4543 return false;
4544 }
4545 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004546 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004547 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4548 }
4549 else
4550 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004551 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004552 result.SetStatus (eReturnStatusFailed);
4553 }
4554
4555 return result.Succeeded();
4556 }
4557private:
4558 CommandOptions m_options;
4559};
4560
Greg Claytonb3448432011-03-24 21:19:54 +00004561OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004562CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4563{
Johnny Chen60fe60e2011-05-02 23:47:55 +00004564 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner,
4565 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004566 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4567 "Set the module within which the stop-hook is to be run."},
4568 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex,
4569 "The stop hook is run only for the thread whose index matches this argument."},
4570 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID,
4571 "The stop hook is run only for the thread whose TID matches this argument."},
4572 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName,
4573 "The stop hook is run only for the thread whose thread name matches this argument."},
4574 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName,
4575 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4576 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4577 "Specify the source file within which the stop-hook is to be run." },
4578 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4579 "Set the start of the line range for which the stop-hook is to be run."},
4580 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4581 "Set the end of the line range for which the stop-hook is to be run."},
4582 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName,
4583 "Specify the class within which the stop-hook is to be run." },
4584 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4585 "Set the function name within which the stop hook will be run." },
4586 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4587};
4588
4589#pragma mark CommandObjectTargetStopHookDelete
4590
4591//-------------------------------------------------------------------------
4592// CommandObjectTargetStopHookDelete
4593//-------------------------------------------------------------------------
4594
Jim Inghamda26bd22012-06-08 21:56:10 +00004595class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004596{
4597public:
4598
4599 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004600 CommandObjectParsed (interpreter,
4601 "target stop-hook delete",
4602 "Delete a stop-hook.",
4603 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004604 {
4605 }
4606
4607 ~CommandObjectTargetStopHookDelete ()
4608 {
4609 }
4610
Jim Inghamda26bd22012-06-08 21:56:10 +00004611protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004612 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004613 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004614 {
4615 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4616 if (target)
4617 {
4618 // FIXME: see if we can use the breakpoint id style parser?
4619 size_t num_args = command.GetArgumentCount();
4620 if (num_args == 0)
4621 {
4622 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4623 {
4624 result.SetStatus (eReturnStatusFailed);
4625 return false;
4626 }
4627 else
4628 {
4629 target->RemoveAllStopHooks();
4630 }
4631 }
4632 else
4633 {
4634 bool success;
4635 for (size_t i = 0; i < num_args; i++)
4636 {
4637 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4638 if (!success)
4639 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004640 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004641 result.SetStatus(eReturnStatusFailed);
4642 return false;
4643 }
4644 success = target->RemoveStopHookByID (user_id);
4645 if (!success)
4646 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004647 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004648 result.SetStatus(eReturnStatusFailed);
4649 return false;
4650 }
4651 }
4652 }
4653 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4654 }
4655 else
4656 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004657 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004658 result.SetStatus (eReturnStatusFailed);
4659 }
4660
4661 return result.Succeeded();
4662 }
4663};
4664#pragma mark CommandObjectTargetStopHookEnableDisable
4665
4666//-------------------------------------------------------------------------
4667// CommandObjectTargetStopHookEnableDisable
4668//-------------------------------------------------------------------------
4669
Jim Inghamda26bd22012-06-08 21:56:10 +00004670class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004671{
4672public:
4673
4674 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004675 CommandObjectParsed (interpreter,
4676 name,
4677 help,
4678 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004679 m_enable (enable)
4680 {
4681 }
4682
4683 ~CommandObjectTargetStopHookEnableDisable ()
4684 {
4685 }
4686
Jim Inghamda26bd22012-06-08 21:56:10 +00004687protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004688 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004689 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004690 {
4691 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4692 if (target)
4693 {
4694 // FIXME: see if we can use the breakpoint id style parser?
4695 size_t num_args = command.GetArgumentCount();
4696 bool success;
4697
4698 if (num_args == 0)
4699 {
4700 target->SetAllStopHooksActiveState (m_enable);
4701 }
4702 else
4703 {
4704 for (size_t i = 0; i < num_args; i++)
4705 {
4706 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4707 if (!success)
4708 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004709 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004710 result.SetStatus(eReturnStatusFailed);
4711 return false;
4712 }
4713 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4714 if (!success)
4715 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004716 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004717 result.SetStatus(eReturnStatusFailed);
4718 return false;
4719 }
4720 }
4721 }
4722 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4723 }
4724 else
4725 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004726 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004727 result.SetStatus (eReturnStatusFailed);
4728 }
4729 return result.Succeeded();
4730 }
4731private:
4732 bool m_enable;
4733};
4734
4735#pragma mark CommandObjectTargetStopHookList
4736
4737//-------------------------------------------------------------------------
4738// CommandObjectTargetStopHookList
4739//-------------------------------------------------------------------------
4740
Jim Inghamda26bd22012-06-08 21:56:10 +00004741class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004742{
4743public:
4744
4745 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004746 CommandObjectParsed (interpreter,
4747 "target stop-hook list",
4748 "List all stop-hooks.",
4749 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004750 {
4751 }
4752
4753 ~CommandObjectTargetStopHookList ()
4754 {
4755 }
4756
Jim Inghamda26bd22012-06-08 21:56:10 +00004757protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004758 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004759 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004760 {
4761 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004762 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004763 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004764 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004765 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004766 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004767 }
4768
4769 size_t num_hooks = target->GetNumStopHooks ();
4770 if (num_hooks == 0)
4771 {
4772 result.GetOutputStream().PutCString ("No stop hooks.\n");
4773 }
4774 else
4775 {
4776 for (size_t i = 0; i < num_hooks; i++)
4777 {
4778 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4779 if (i > 0)
4780 result.GetOutputStream().PutCString ("\n");
4781 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4782 }
4783 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004784 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004785 return result.Succeeded();
4786 }
4787};
4788
4789#pragma mark CommandObjectMultiwordTargetStopHooks
4790//-------------------------------------------------------------------------
4791// CommandObjectMultiwordTargetStopHooks
4792//-------------------------------------------------------------------------
4793
4794class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4795{
4796public:
4797
4798 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4799 CommandObjectMultiword (interpreter,
4800 "target stop-hook",
4801 "A set of commands for operating on debugger target stop-hooks.",
4802 "target stop-hook <subcommand> [<subcommand-options>]")
4803 {
4804 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4805 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4806 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4807 false,
4808 "target stop-hook disable [<id>]",
4809 "Disable a stop-hook.",
4810 "target stop-hook disable")));
4811 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4812 true,
4813 "target stop-hook enable [<id>]",
4814 "Enable a stop-hook.",
4815 "target stop-hook enable")));
4816 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4817 }
4818
4819 ~CommandObjectMultiwordTargetStopHooks()
4820 {
4821 }
4822};
4823
4824
Chris Lattner24943d22010-06-08 16:52:24 +00004825
4826#pragma mark CommandObjectMultiwordTarget
4827
4828//-------------------------------------------------------------------------
4829// CommandObjectMultiwordTarget
4830//-------------------------------------------------------------------------
4831
Greg Clayton63094e02010-06-23 01:19:29 +00004832CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00004833 CommandObjectMultiword (interpreter,
4834 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00004835 "A set of commands for operating on debugger targets.",
4836 "target <subcommand> [<subcommand-options>]")
4837{
Greg Claytonabe0fed2011-04-18 08:33:37 +00004838
4839 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00004840 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00004841 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
4842 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004843 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004844 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00004845 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00004846 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004847}
4848
4849CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
4850{
4851}
4852
Greg Claytonabe0fed2011-04-18 08:33:37 +00004853