blob: 679c5ddbe94408223ac1920d946d77f768ed055e [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"
Greg Claytonb924eb62012-09-27 03:13:55 +000027#include "lldb/Host/Symbols.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Interpreter/CommandInterpreter.h"
29#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000030#include "lldb/Interpreter/Options.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000031#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Clayton5beb99d2011-08-11 02:48:45 +000032#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000033#include "lldb/Interpreter/OptionGroupFile.h"
Greg Claytona42880a2011-10-25 06:44:01 +000034#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton368f8222011-07-07 04:38:25 +000035#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000036#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000037#include "lldb/Interpreter/OptionGroupUInt64.h"
38#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton801417e2011-07-07 01:59:51 +000039#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000040#include "lldb/Symbol/CompileUnit.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000041#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000042#include "lldb/Symbol/LineTable.h"
43#include "lldb/Symbol/ObjectFile.h"
44#include "lldb/Symbol/SymbolFile.h"
45#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000046#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton801417e2011-07-07 01:59:51 +000047#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000048#include "lldb/Target/Process.h"
49#include "lldb/Target/StackFrame.h"
50#include "lldb/Target/Thread.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000051#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Greg Claytonabe0fed2011-04-18 08:33:37 +000056
57
58static void
59DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
60{
Greg Clayton52c8b6e2011-04-19 04:19:37 +000061 const ArchSpec &target_arch = target->GetArchitecture();
Greg Claytonabe0fed2011-04-18 08:33:37 +000062
Greg Clayton5beb99d2011-08-11 02:48:45 +000063 Module *exe_module = target->GetExecutableModulePointer();
Greg Claytonabe0fed2011-04-18 08:33:37 +000064 char exe_path[PATH_MAX];
65 bool exe_valid = false;
Greg Clayton5beb99d2011-08-11 02:48:45 +000066 if (exe_module)
67 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Greg Claytonabe0fed2011-04-18 08:33:37 +000068
69 if (!exe_valid)
70 ::strcpy (exe_path, "<none>");
71
72 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
73
74 uint32_t properties = 0;
75 if (target_arch.IsValid())
76 {
77 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
78 properties++;
79 }
80 PlatformSP platform_sp (target->GetPlatform());
81 if (platform_sp)
82 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName());
83
84 ProcessSP process_sp (target->GetProcessSP());
85 bool show_process_status = false;
86 if (process_sp)
87 {
88 lldb::pid_t pid = process_sp->GetID();
89 StateType state = process_sp->GetState();
90 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000091 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000092 const char *state_cstr = StateAsCString (state);
93 if (pid != LLDB_INVALID_PROCESS_ID)
Greg Claytond9919d32011-12-01 23:28:38 +000094 strm.Printf ("%spid=%llu", properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000095 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
96 }
97 if (properties > 0)
98 strm.PutCString (" )\n");
99 else
100 strm.EOL();
101 if (show_process_status)
102 {
103 const bool only_threads_with_stop_reason = true;
104 const uint32_t start_frame = 0;
105 const uint32_t num_frames = 1;
106 const uint32_t num_frames_with_source = 1;
107 process_sp->GetStatus (strm);
108 process_sp->GetThreadStatus (strm,
109 only_threads_with_stop_reason,
110 start_frame,
111 num_frames,
112 num_frames_with_source);
113
114 }
115}
116
117static uint32_t
118DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
119{
120 const uint32_t num_targets = target_list.GetNumTargets();
121 if (num_targets)
122 {
123 TargetSP selected_target_sp (target_list.GetSelectedTarget());
124 strm.PutCString ("Current targets:\n");
125 for (uint32_t i=0; i<num_targets; ++i)
126 {
127 TargetSP target_sp (target_list.GetTargetAtIndex (i));
128 if (target_sp)
129 {
130 bool is_selected = target_sp.get() == selected_target_sp.get();
131 DumpTargetInfo (i,
132 target_sp.get(),
133 is_selected ? "* " : " ",
134 show_stopped_process_status,
135 strm);
136 }
137 }
138 }
139 return num_targets;
140}
141#pragma mark CommandObjectTargetCreate
142
143//-------------------------------------------------------------------------
144// "target create"
145//-------------------------------------------------------------------------
146
Jim Inghamda26bd22012-06-08 21:56:10 +0000147class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000148{
149public:
150 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000151 CommandObjectParsed (interpreter,
152 "target create",
153 "Create a target using the argument as the main executable.",
154 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000155 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000156 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000157 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Sean Callanan9a91ef62012-10-24 01:12:14 +0000158 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target.")
Greg Claytonabe0fed2011-04-18 08:33:37 +0000159 {
160 CommandArgumentEntry arg;
161 CommandArgumentData file_arg;
162
163 // Define the first (and only) variant of this arg.
164 file_arg.arg_type = eArgTypeFilename;
165 file_arg.arg_repetition = eArgRepeatPlain;
166
167 // There is only one variant this argument could be; put it into the argument entry.
168 arg.push_back (file_arg);
169
170 // Push the data for the first argument into the m_arguments vector.
171 m_arguments.push_back (arg);
172
Greg Clayton801417e2011-07-07 01:59:51 +0000173 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000174 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000175 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000176 m_option_group.Finalize();
177 }
178
179 ~CommandObjectTargetCreate ()
180 {
181 }
182
183 Options *
184 GetOptions ()
185 {
186 return &m_option_group;
187 }
188
Jim Inghamda26bd22012-06-08 21:56:10 +0000189 int
190 HandleArgumentCompletion (Args &input,
191 int &cursor_index,
192 int &cursor_char_position,
193 OptionElementVector &opt_element_vector,
194 int match_start_point,
195 int max_return_elements,
196 bool &word_complete,
197 StringList &matches)
198 {
199 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
200 completion_str.erase (cursor_char_position);
201
202 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
203 CommandCompletions::eDiskFileCompletion,
204 completion_str.c_str(),
205 match_start_point,
206 max_return_elements,
207 NULL,
208 word_complete,
209 matches);
210 return matches.GetSize();
211 }
212
213protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000214 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000215 DoExecute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000216 {
217 const int argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000218 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
219
220 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000221 {
222 const char *file_path = command.GetArgumentAtIndex(0);
223 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000224 TargetSP target_sp;
225 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000226 const char *arch_cstr = m_arch_option.GetArchitectureName();
227 const bool get_dependent_files = true;
228 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000229 file_path,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000230 arch_cstr,
231 get_dependent_files,
232 &m_platform_options,
233 target_sp));
234
Greg Claytonabe0fed2011-04-18 08:33:37 +0000235 if (target_sp)
236 {
237 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000238 if (core_file)
239 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000240 char core_path[PATH_MAX];
241 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000242 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000243 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000244 FileSpec core_file_dir;
245 core_file_dir.GetDirectory() = core_file.GetDirectory();
246 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000247
Greg Clayton9ce95382012-02-13 23:10:39 +0000248 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
249
250 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000251 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000252 // Seems wierd that we Launch a core file, but that is
253 // what we do!
254 error = process_sp->LoadCore();
255
256 if (error.Fail())
257 {
258 result.AppendError(error.AsCString("can't find plug-in for core file"));
259 result.SetStatus (eReturnStatusFailed);
260 return false;
261 }
262 else
263 {
264 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
265 result.SetStatus (eReturnStatusSuccessFinishNoResult);
266 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000267 }
268 else
269 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000270 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
271 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000272 }
273 }
274 else
275 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000276 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000277 result.SetStatus (eReturnStatusFailed);
278 }
279 }
280 else
281 {
282 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
283 result.SetStatus (eReturnStatusSuccessFinishNoResult);
284 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000285 }
286 else
287 {
288 result.AppendError(error.AsCString());
289 result.SetStatus (eReturnStatusFailed);
290 }
291 }
292 else
293 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000294 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 +0000295 result.SetStatus (eReturnStatusFailed);
296 }
297 return result.Succeeded();
298
299 }
300
Greg Claytonabe0fed2011-04-18 08:33:37 +0000301private:
302 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000303 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000304 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000305 OptionGroupFile m_core_file;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000306
307};
308
309#pragma mark CommandObjectTargetList
310
311//----------------------------------------------------------------------
312// "target list"
313//----------------------------------------------------------------------
314
Jim Inghamda26bd22012-06-08 21:56:10 +0000315class CommandObjectTargetList : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000316{
317public:
318 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000319 CommandObjectParsed (interpreter,
320 "target list",
321 "List all current targets in the current debug session.",
322 NULL,
323 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000324 {
325 }
326
327 virtual
328 ~CommandObjectTargetList ()
329 {
330 }
331
Jim Inghamda26bd22012-06-08 21:56:10 +0000332protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000333 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000334 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000335 {
336 if (args.GetArgumentCount() == 0)
337 {
338 Stream &strm = result.GetOutputStream();
339
340 bool show_stopped_process_status = false;
341 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
342 {
343 strm.PutCString ("No targets.\n");
344 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000345 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000346 }
347 else
348 {
349 result.AppendError ("the 'target list' command takes no arguments\n");
350 result.SetStatus (eReturnStatusFailed);
351 }
352 return result.Succeeded();
353 }
354};
355
356
357#pragma mark CommandObjectTargetSelect
358
359//----------------------------------------------------------------------
360// "target select"
361//----------------------------------------------------------------------
362
Jim Inghamda26bd22012-06-08 21:56:10 +0000363class CommandObjectTargetSelect : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000364{
365public:
366 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000367 CommandObjectParsed (interpreter,
368 "target select",
369 "Select a target as the current target by target index.",
370 NULL,
371 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000372 {
373 }
374
375 virtual
376 ~CommandObjectTargetSelect ()
377 {
378 }
379
Jim Inghamda26bd22012-06-08 21:56:10 +0000380protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000381 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000382 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000383 {
384 if (args.GetArgumentCount() == 1)
385 {
386 bool success = false;
387 const char *target_idx_arg = args.GetArgumentAtIndex(0);
388 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
389 if (success)
390 {
391 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
392 const uint32_t num_targets = target_list.GetNumTargets();
393 if (target_idx < num_targets)
394 {
395 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
396 if (target_sp)
397 {
398 Stream &strm = result.GetOutputStream();
399 target_list.SetSelectedTarget (target_sp.get());
400 bool show_stopped_process_status = false;
401 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000402 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000403 }
404 else
405 {
406 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
407 result.SetStatus (eReturnStatusFailed);
408 }
409 }
410 else
411 {
412 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
413 target_idx,
414 num_targets - 1);
415 result.SetStatus (eReturnStatusFailed);
416 }
417 }
418 else
419 {
420 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
421 result.SetStatus (eReturnStatusFailed);
422 }
423 }
424 else
425 {
426 result.AppendError ("'target select' takes a single argument: a target index\n");
427 result.SetStatus (eReturnStatusFailed);
428 }
429 return result.Succeeded();
430 }
431};
432
Greg Clayton153ccd72011-08-10 02:10:13 +0000433#pragma mark CommandObjectTargetSelect
434
435//----------------------------------------------------------------------
436// "target delete"
437//----------------------------------------------------------------------
438
Jim Inghamda26bd22012-06-08 21:56:10 +0000439class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton153ccd72011-08-10 02:10:13 +0000440{
441public:
442 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000443 CommandObjectParsed (interpreter,
444 "target delete",
445 "Delete one or more targets by target index.",
446 NULL,
447 0),
Greg Clayton5beb99d2011-08-11 02:48:45 +0000448 m_option_group (interpreter),
Greg Clayton437b5bc2012-09-27 22:26:11 +0000449 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', "Perform extra cleanup to minimize memory consumption after deleting the target.", false, false)
Greg Clayton153ccd72011-08-10 02:10:13 +0000450 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000451 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
452 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000453 }
454
455 virtual
456 ~CommandObjectTargetDelete ()
457 {
458 }
459
Jim Inghamda26bd22012-06-08 21:56:10 +0000460 Options *
461 GetOptions ()
462 {
463 return &m_option_group;
464 }
465
466protected:
Greg Clayton153ccd72011-08-10 02:10:13 +0000467 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000468 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton153ccd72011-08-10 02:10:13 +0000469 {
470 const size_t argc = args.GetArgumentCount();
471 std::vector<TargetSP> delete_target_list;
472 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
473 bool success = true;
474 TargetSP target_sp;
475 if (argc > 0)
476 {
477 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000478 // Bail out if don't have any targets.
479 if (num_targets == 0) {
480 result.AppendError("no targets to delete");
481 result.SetStatus(eReturnStatusFailed);
482 success = false;
483 }
484
Greg Clayton153ccd72011-08-10 02:10:13 +0000485 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
486 {
487 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
488 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
489 if (success)
490 {
491 if (target_idx < num_targets)
492 {
493 target_sp = target_list.GetTargetAtIndex (target_idx);
494 if (target_sp)
495 {
496 delete_target_list.push_back (target_sp);
497 continue;
498 }
499 }
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000500 if (num_targets > 1)
501 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
502 target_idx,
503 num_targets - 1);
504 else
505 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
506 target_idx);
507
Greg Clayton153ccd72011-08-10 02:10:13 +0000508 result.SetStatus (eReturnStatusFailed);
509 success = false;
510 }
511 else
512 {
513 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
514 result.SetStatus (eReturnStatusFailed);
515 success = false;
516 }
517 }
518
519 }
520 else
521 {
522 target_sp = target_list.GetSelectedTarget();
523 if (target_sp)
524 {
525 delete_target_list.push_back (target_sp);
526 }
527 else
528 {
529 result.AppendErrorWithFormat("no target is currently selected\n");
530 result.SetStatus (eReturnStatusFailed);
531 success = false;
532 }
533 }
534 if (success)
535 {
536 const size_t num_targets_to_delete = delete_target_list.size();
537 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
538 {
539 target_sp = delete_target_list[idx];
540 target_list.DeleteTarget(target_sp);
541 target_sp->Destroy();
542 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000543 // If "--clean" was specified, prune any orphaned shared modules from
544 // the global shared module list
545 if (m_cleanup_option.GetOptionValue ())
546 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000547 const bool mandatory = true;
548 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000549 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000550 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
551 result.SetStatus(eReturnStatusSuccessFinishResult);
552 }
553
554 return result.Succeeded();
555 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000556
Greg Clayton5beb99d2011-08-11 02:48:45 +0000557 OptionGroupOptions m_option_group;
558 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000559};
560
Greg Claytonabe0fed2011-04-18 08:33:37 +0000561
Greg Clayton801417e2011-07-07 01:59:51 +0000562#pragma mark CommandObjectTargetVariable
563
564//----------------------------------------------------------------------
565// "target variable"
566//----------------------------------------------------------------------
567
Jim Inghamda26bd22012-06-08 21:56:10 +0000568class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000569{
570public:
571 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000572 CommandObjectParsed (interpreter,
573 "target variable",
Greg Claytonf72bd8b2012-11-03 00:10:22 +0000574 "Read global variable(s) prior to, or while running your binary.",
Jim Inghamda26bd22012-06-08 21:56:10 +0000575 NULL,
576 0),
Greg Clayton801417e2011-07-07 01:59:51 +0000577 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000578 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000579 m_option_format (eFormatDefault),
Sean Callanan9a91ef62012-10-24 01:12:14 +0000580 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeFilename, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
581 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'s', 0, eArgTypeFilename, "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
Greg Clayton801417e2011-07-07 01:59:51 +0000582 m_varobj_options()
583 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000584 CommandArgumentEntry arg;
585 CommandArgumentData var_name_arg;
586
587 // Define the first (and only) variant of this arg.
588 var_name_arg.arg_type = eArgTypeVarName;
589 var_name_arg.arg_repetition = eArgRepeatPlus;
590
591 // There is only one variant this argument could be; put it into the argument entry.
592 arg.push_back (var_name_arg);
593
594 // Push the data for the first argument into the m_arguments vector.
595 m_arguments.push_back (arg);
596
Greg Clayton801417e2011-07-07 01:59:51 +0000597 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000598 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000599 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 +0000600 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
601 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
602 m_option_group.Finalize();
603 }
604
605 virtual
606 ~CommandObjectTargetVariable ()
607 {
608 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000609
610 void
611 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
612 {
Enrico Granata19030d82011-08-15 18:01:31 +0000613 ValueObject::DumpValueObjectOptions options;
614
Enrico Granata3069c622012-03-01 04:24:26 +0000615 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000616 .SetMaximumDepth(m_varobj_options.max_depth)
617 .SetShowTypes(m_varobj_options.show_types)
618 .SetShowLocation(m_varobj_options.show_location)
619 .SetUseObjectiveC(m_varobj_options.use_objc)
620 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000621 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000622 .SetFlatOutput(m_varobj_options.flat_output)
623 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
624 .SetIgnoreCap(m_varobj_options.ignore_cap);
625
Greg Clayton5d81f492011-07-08 21:46:14 +0000626 switch (var_sp->GetScope())
627 {
628 case eValueTypeVariableGlobal:
629 if (m_option_variable.show_scope)
630 s.PutCString("GLOBAL: ");
631 break;
632
633 case eValueTypeVariableStatic:
634 if (m_option_variable.show_scope)
635 s.PutCString("STATIC: ");
636 break;
637
638 case eValueTypeVariableArgument:
639 if (m_option_variable.show_scope)
640 s.PutCString(" ARG: ");
641 break;
642
643 case eValueTypeVariableLocal:
644 if (m_option_variable.show_scope)
645 s.PutCString(" LOCAL: ");
646 break;
647
648 default:
649 break;
650 }
651
Greg Claytonfb816422011-07-10 19:21:23 +0000652 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000653 {
Greg Claytonfb816422011-07-10 19:21:23 +0000654 bool show_fullpaths = false;
655 bool show_module = true;
656 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
657 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000658 }
659
Greg Claytona42880a2011-10-25 06:44:01 +0000660 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000661 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000662 options.SetFormat(format);
663
664 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000665
666 ValueObject::DumpValueObject (s,
667 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000668 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000669
670 }
Greg Clayton801417e2011-07-07 01:59:51 +0000671
Greg Clayton5d81f492011-07-08 21:46:14 +0000672
673 static uint32_t GetVariableCallback (void *baton,
674 const char *name,
675 VariableList &variable_list)
676 {
677 Target *target = static_cast<Target *>(baton);
678 if (target)
679 {
680 return target->GetImages().FindGlobalVariables (ConstString(name),
681 true,
682 UINT32_MAX,
683 variable_list);
684 }
685 return 0;
686 }
687
688
689
Jim Inghamda26bd22012-06-08 21:56:10 +0000690 Options *
691 GetOptions ()
692 {
693 return &m_option_group;
694 }
695
696protected:
Greg Clayton801417e2011-07-07 01:59:51 +0000697 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000698 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000699 {
700 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000701 Target *target = exe_ctx.GetTargetPtr();
702 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000703 {
704 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000705 Stream &s = result.GetOutputStream();
Greg Clayton801417e2011-07-07 01:59:51 +0000706 if (argc > 0)
707 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000708
Greg Clayton801417e2011-07-07 01:59:51 +0000709 for (size_t idx = 0; idx < argc; ++idx)
710 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000711 VariableList variable_list;
712 ValueObjectList valobj_list;
713
Greg Clayton368f8222011-07-07 04:38:25 +0000714 const char *arg = args.GetArgumentAtIndex(idx);
715 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000716 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000717 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000718 {
Greg Clayton368f8222011-07-07 04:38:25 +0000719 RegularExpression regex(arg);
720 if (!regex.IsValid ())
721 {
722 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
723 result.SetStatus (eReturnStatusFailed);
724 return false;
725 }
Greg Claytonfb816422011-07-10 19:21:23 +0000726 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000727 matches = target->GetImages().FindGlobalVariables (regex,
728 true,
729 UINT32_MAX,
730 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000731 }
732 else
733 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000734 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000735 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000736 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000737 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000738 variable_list,
739 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000740 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000741 }
742
743 if (matches == 0)
744 {
745 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
746 result.SetStatus (eReturnStatusFailed);
747 return false;
748 }
749 else
750 {
Greg Clayton801417e2011-07-07 01:59:51 +0000751 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
752 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000753 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000754 if (var_sp)
755 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000756 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
757 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000758 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000759
760 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000761 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000762 }
763 }
764 }
765 }
766 }
767 else
768 {
Greg Claytonfac93882011-10-05 22:17:32 +0000769 bool success = false;
770 StackFrame *frame = exe_ctx.GetFramePtr();
771 CompileUnit *comp_unit = NULL;
772 if (frame)
773 {
774 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
775 if (comp_unit)
776 {
777 const bool can_create = true;
778 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create));
779 if (comp_unit_varlist_sp)
780 {
781 size_t count = comp_unit_varlist_sp->GetSize();
782 if (count > 0)
783 {
Greg Claytona1b9a902011-11-13 04:15:56 +0000784 s.Printf ("Global variables for %s/%s:\n",
Greg Claytonfac93882011-10-05 22:17:32 +0000785 comp_unit->GetDirectory().GetCString(),
786 comp_unit->GetFilename().GetCString());
787
788 success = true;
789 for (uint32_t i=0; i<count; ++i)
790 {
791 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i));
792 if (var_sp)
793 {
794 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
795
796 if (valobj_sp)
797 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
798 }
799 }
800 }
801 }
802 }
803 }
804 if (!success)
805 {
806 if (frame)
807 {
808 if (comp_unit)
809 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
810 comp_unit->GetDirectory().GetCString(),
811 comp_unit->GetFilename().GetCString());
812 else
813 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
814 }
815 else
816 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
817 result.SetStatus (eReturnStatusFailed);
818 }
Greg Clayton801417e2011-07-07 01:59:51 +0000819 }
820 }
821 else
822 {
823 result.AppendError ("invalid target, create a debug target using the 'target create' command");
824 result.SetStatus (eReturnStatusFailed);
825 return false;
826 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000827
828 if (m_interpreter.TruncationWarningNecessary())
829 {
830 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
831 m_cmd_name.c_str());
832 m_interpreter.TruncationWarningGiven();
833 }
834
Greg Clayton801417e2011-07-07 01:59:51 +0000835 return result.Succeeded();
836 }
837
Greg Clayton801417e2011-07-07 01:59:51 +0000838 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000839 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000840 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000841 OptionGroupFileList m_option_compile_units;
842 OptionGroupFileList m_option_shared_libraries;
843 OptionGroupValueObjectDisplay m_varobj_options;
844
845};
846
847
Greg Claytone1f50b92011-05-03 22:09:39 +0000848#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000849
Jim Inghamda26bd22012-06-08 21:56:10 +0000850class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000851{
852public:
853
Greg Claytone1f50b92011-05-03 22:09:39 +0000854 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000855 CommandObjectParsed (interpreter,
856 "target modules search-paths add",
857 "Add new image search paths substitution pairs to the current target.",
858 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000859 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000860 CommandArgumentEntry arg;
861 CommandArgumentData old_prefix_arg;
862 CommandArgumentData new_prefix_arg;
863
864 // Define the first variant of this arg pair.
865 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
866 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
867
868 // Define the first variant of this arg pair.
869 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
870 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
871
872 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
873 // must always occur together, they are treated as two variants of one argument rather than two independent
874 // arguments. Push them both into the first argument position for m_arguments...
875
876 arg.push_back (old_prefix_arg);
877 arg.push_back (new_prefix_arg);
878
879 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000880 }
881
Greg Claytone1f50b92011-05-03 22:09:39 +0000882 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000883 {
884 }
885
Jim Inghamda26bd22012-06-08 21:56:10 +0000886protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000887 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000888 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000889 CommandReturnObject &result)
890 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000891 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000892 if (target)
893 {
894 uint32_t argc = command.GetArgumentCount();
895 if (argc & 1)
896 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000897 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000898 result.SetStatus (eReturnStatusFailed);
899 }
900 else
901 {
902 for (uint32_t i=0; i<argc; i+=2)
903 {
904 const char *from = command.GetArgumentAtIndex(i);
905 const char *to = command.GetArgumentAtIndex(i+1);
906
907 if (from[0] && to[0])
908 {
909 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +0000910 target->GetImageSearchPathList().Append (ConstString(from),
911 ConstString(to),
912 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +0000913 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000914 }
915 else
916 {
917 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +0000918 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000919 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000920 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000921 result.SetStatus (eReturnStatusFailed);
922 }
923 }
924 }
925 }
926 else
927 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000928 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000929 result.SetStatus (eReturnStatusFailed);
930 }
931 return result.Succeeded();
932 }
933};
934
Greg Claytone1f50b92011-05-03 22:09:39 +0000935#pragma mark CommandObjectTargetModulesSearchPathsClear
936
Jim Inghamda26bd22012-06-08 21:56:10 +0000937class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000938{
939public:
940
Greg Claytone1f50b92011-05-03 22:09:39 +0000941 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000942 CommandObjectParsed (interpreter,
943 "target modules search-paths clear",
944 "Clear all current image search path substitution pairs from the current target.",
945 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +0000946 {
947 }
948
Greg Claytone1f50b92011-05-03 22:09:39 +0000949 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +0000950 {
951 }
952
Jim Inghamda26bd22012-06-08 21:56:10 +0000953protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000954 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000955 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000956 CommandReturnObject &result)
957 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000958 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000959 if (target)
960 {
961 bool notify = true;
962 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +0000963 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000964 }
965 else
966 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000967 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000968 result.SetStatus (eReturnStatusFailed);
969 }
970 return result.Succeeded();
971 }
972};
973
Greg Claytone1f50b92011-05-03 22:09:39 +0000974#pragma mark CommandObjectTargetModulesSearchPathsInsert
975
Jim Inghamda26bd22012-06-08 21:56:10 +0000976class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000977{
978public:
979
Greg Claytone1f50b92011-05-03 22:09:39 +0000980 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000981 CommandObjectParsed (interpreter,
982 "target modules search-paths insert",
983 "Insert a new image search path substitution pair into the current target at the specified index.",
984 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000985 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000986 CommandArgumentEntry arg1;
987 CommandArgumentEntry arg2;
988 CommandArgumentData index_arg;
989 CommandArgumentData old_prefix_arg;
990 CommandArgumentData new_prefix_arg;
991
992 // Define the first and only variant of this arg.
993 index_arg.arg_type = eArgTypeIndex;
994 index_arg.arg_repetition = eArgRepeatPlain;
995
996 // Put the one and only variant into the first arg for m_arguments:
997 arg1.push_back (index_arg);
998
999 // Define the first variant of this arg pair.
1000 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1001 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1002
1003 // Define the first variant of this arg pair.
1004 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1005 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1006
1007 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1008 // must always occur together, they are treated as two variants of one argument rather than two independent
1009 // arguments. Push them both into the same argument position for m_arguments...
1010
1011 arg2.push_back (old_prefix_arg);
1012 arg2.push_back (new_prefix_arg);
1013
1014 // Add arguments to m_arguments.
1015 m_arguments.push_back (arg1);
1016 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001017 }
1018
Greg Claytone1f50b92011-05-03 22:09:39 +00001019 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001020 {
1021 }
1022
Jim Inghamda26bd22012-06-08 21:56:10 +00001023protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001024 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001025 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001026 CommandReturnObject &result)
1027 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001028 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001029 if (target)
1030 {
1031 uint32_t argc = command.GetArgumentCount();
1032 // check for at least 3 arguments and an odd nubmer of parameters
1033 if (argc >= 3 && argc & 1)
1034 {
1035 bool success = false;
1036
1037 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1038
1039 if (!success)
1040 {
1041 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1042 result.SetStatus (eReturnStatusFailed);
1043 return result.Succeeded();
1044 }
1045
1046 // shift off the index
1047 command.Shift();
1048 argc = command.GetArgumentCount();
1049
1050 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1051 {
1052 const char *from = command.GetArgumentAtIndex(i);
1053 const char *to = command.GetArgumentAtIndex(i+1);
1054
1055 if (from[0] && to[0])
1056 {
1057 bool last_pair = ((argc - i) == 2);
1058 target->GetImageSearchPathList().Insert (ConstString(from),
1059 ConstString(to),
1060 insert_idx,
1061 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001062 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001063 }
1064 else
1065 {
1066 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001067 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001068 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001069 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001070 result.SetStatus (eReturnStatusFailed);
1071 return false;
1072 }
1073 }
1074 }
1075 else
1076 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001077 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001078 result.SetStatus (eReturnStatusFailed);
1079 return result.Succeeded();
1080 }
1081
1082 }
1083 else
1084 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001085 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001086 result.SetStatus (eReturnStatusFailed);
1087 }
1088 return result.Succeeded();
1089 }
1090};
1091
Greg Claytone1f50b92011-05-03 22:09:39 +00001092
1093#pragma mark CommandObjectTargetModulesSearchPathsList
1094
1095
Jim Inghamda26bd22012-06-08 21:56:10 +00001096class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001097{
1098public:
1099
Greg Claytone1f50b92011-05-03 22:09:39 +00001100 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001101 CommandObjectParsed (interpreter,
1102 "target modules search-paths list",
1103 "List all current image search path substitution pairs in the current target.",
1104 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001105 {
1106 }
1107
Greg Claytone1f50b92011-05-03 22:09:39 +00001108 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001109 {
1110 }
1111
Jim Inghamda26bd22012-06-08 21:56:10 +00001112protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001113 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001114 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001115 CommandReturnObject &result)
1116 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001117 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001118 if (target)
1119 {
1120 if (command.GetArgumentCount() != 0)
1121 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001122 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001123 result.SetStatus (eReturnStatusFailed);
1124 return result.Succeeded();
1125 }
1126
1127 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001128 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001129 }
1130 else
1131 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001132 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001133 result.SetStatus (eReturnStatusFailed);
1134 }
1135 return result.Succeeded();
1136 }
1137};
1138
Greg Claytone1f50b92011-05-03 22:09:39 +00001139#pragma mark CommandObjectTargetModulesSearchPathsQuery
1140
Jim Inghamda26bd22012-06-08 21:56:10 +00001141class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001142{
1143public:
1144
Greg Claytone1f50b92011-05-03 22:09:39 +00001145 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001146 CommandObjectParsed (interpreter,
1147 "target modules search-paths query",
1148 "Transform a path using the first applicable image search path.",
1149 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001150 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001151 CommandArgumentEntry arg;
1152 CommandArgumentData path_arg;
1153
1154 // Define the first (and only) variant of this arg.
Sean Callanan9a91ef62012-10-24 01:12:14 +00001155 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice43b014a2010-10-04 22:28:36 +00001156 path_arg.arg_repetition = eArgRepeatPlain;
1157
1158 // There is only one variant this argument could be; put it into the argument entry.
1159 arg.push_back (path_arg);
1160
1161 // Push the data for the first argument into the m_arguments vector.
1162 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001163 }
1164
Greg Claytone1f50b92011-05-03 22:09:39 +00001165 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001166 {
1167 }
1168
Jim Inghamda26bd22012-06-08 21:56:10 +00001169protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001170 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001171 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001172 CommandReturnObject &result)
1173 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001174 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001175 if (target)
1176 {
1177 if (command.GetArgumentCount() != 1)
1178 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001179 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001180 result.SetStatus (eReturnStatusFailed);
1181 return result.Succeeded();
1182 }
1183
1184 ConstString orig(command.GetArgumentAtIndex(0));
1185 ConstString transformed;
1186 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1187 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1188 else
1189 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001190
1191 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001192 }
1193 else
1194 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001195 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001196 result.SetStatus (eReturnStatusFailed);
1197 }
1198 return result.Succeeded();
1199 }
1200};
1201
Greg Claytone1f50b92011-05-03 22:09:39 +00001202//----------------------------------------------------------------------
1203// Static Helper functions
1204//----------------------------------------------------------------------
1205static void
1206DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1207{
1208 if (module)
1209 {
1210 const char *arch_cstr;
1211 if (full_triple)
1212 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1213 else
1214 arch_cstr = module->GetArchitecture().GetArchitectureName();
1215 if (width)
1216 strm.Printf("%-*s", width, arch_cstr);
1217 else
1218 strm.PutCString(arch_cstr);
1219 }
1220}
1221
1222static void
1223DumpModuleUUID (Stream &strm, Module *module)
1224{
Jim Ingham6f01c932012-10-12 17:34:26 +00001225 if (module && module->GetUUID().IsValid())
Greg Clayton153ccd72011-08-10 02:10:13 +00001226 module->GetUUID().Dump (&strm);
1227 else
1228 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001229}
1230
1231static uint32_t
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001232DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1233 Stream &strm,
1234 Module *module,
1235 const FileSpec &file_spec,
1236 bool load_addresses)
Greg Claytone1f50b92011-05-03 22:09:39 +00001237{
1238 uint32_t num_matches = 0;
1239 if (module)
1240 {
1241 SymbolContextList sc_list;
1242 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1243 0,
1244 false,
1245 eSymbolContextCompUnit,
1246 sc_list);
1247
1248 for (uint32_t i=0; i<num_matches; ++i)
1249 {
1250 SymbolContext sc;
1251 if (sc_list.GetContextAtIndex(i, sc))
1252 {
1253 if (i > 0)
1254 strm << "\n\n";
1255
1256 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1257 << module->GetFileSpec().GetFilename() << "\n";
1258 LineTable *line_table = sc.comp_unit->GetLineTable();
1259 if (line_table)
1260 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001261 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001262 lldb::eDescriptionLevelBrief);
1263 else
1264 strm << "No line table";
1265 }
1266 }
1267 }
1268 return num_matches;
1269}
1270
1271static void
1272DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1273{
1274 if (file_spec_ptr)
1275 {
1276 if (width > 0)
1277 {
1278 char fullpath[PATH_MAX];
1279 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1280 {
1281 strm.Printf("%-*s", width, fullpath);
1282 return;
1283 }
1284 }
1285 else
1286 {
1287 file_spec_ptr->Dump(&strm);
1288 return;
1289 }
1290 }
1291 // Keep the width spacing correct if things go wrong...
1292 if (width > 0)
1293 strm.Printf("%-*s", width, "");
1294}
1295
1296static void
1297DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1298{
1299 if (file_spec_ptr)
1300 {
1301 if (width > 0)
1302 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1303 else
1304 file_spec_ptr->GetDirectory().Dump(&strm);
1305 return;
1306 }
1307 // Keep the width spacing correct if things go wrong...
1308 if (width > 0)
1309 strm.Printf("%-*s", width, "");
1310}
1311
1312static void
1313DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1314{
1315 if (file_spec_ptr)
1316 {
1317 if (width > 0)
1318 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1319 else
1320 file_spec_ptr->GetFilename().Dump(&strm);
1321 return;
1322 }
1323 // Keep the width spacing correct if things go wrong...
1324 if (width > 0)
1325 strm.Printf("%-*s", width, "");
1326}
1327
1328
1329static void
1330DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1331{
1332 if (module)
1333 {
1334 ObjectFile *objfile = module->GetObjectFile ();
1335 if (objfile)
1336 {
1337 Symtab *symtab = objfile->GetSymtab();
1338 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001339 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001340 }
1341 }
1342}
1343
1344static void
1345DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1346{
1347 if (module)
1348 {
1349 ObjectFile *objfile = module->GetObjectFile ();
1350 if (objfile)
1351 {
1352 SectionList *section_list = objfile->GetSectionList();
1353 if (section_list)
1354 {
1355 strm.PutCString ("Sections for '");
1356 strm << module->GetFileSpec();
1357 if (module->GetObjectName())
1358 strm << '(' << module->GetObjectName() << ')';
1359 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1360 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001361 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001362 strm.IndentLess();
1363 }
1364 }
1365 }
1366}
1367
1368static bool
1369DumpModuleSymbolVendor (Stream &strm, Module *module)
1370{
1371 if (module)
1372 {
1373 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1374 if (symbol_vendor)
1375 {
1376 symbol_vendor->Dump(&strm);
1377 return true;
1378 }
1379 }
1380 return false;
1381}
1382
Greg Clayton2ad894b2012-05-15 18:43:44 +00001383static void
1384DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1385{
1386 strm.IndentMore();
1387 strm.Indent (" Address: ");
1388 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1389 strm.PutCString (" (");
1390 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1391 strm.PutCString (")\n");
1392 strm.Indent (" Summary: ");
1393 const uint32_t save_indent = strm.GetIndentLevel ();
1394 strm.SetIndentLevel (save_indent + 13);
1395 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1396 strm.SetIndentLevel (save_indent);
1397 // Print out detailed address information when verbose is enabled
1398 if (verbose)
1399 {
1400 strm.EOL();
1401 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1402 }
1403 strm.IndentLess();
1404}
1405
Greg Claytone1f50b92011-05-03 22:09:39 +00001406static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001407LookupAddressInModule (CommandInterpreter &interpreter,
1408 Stream &strm,
1409 Module *module,
1410 uint32_t resolve_mask,
1411 lldb::addr_t raw_addr,
1412 lldb::addr_t offset,
1413 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001414{
1415 if (module)
1416 {
1417 lldb::addr_t addr = raw_addr - offset;
1418 Address so_addr;
1419 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001420 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001421 if (target && !target->GetSectionLoadList().IsEmpty())
1422 {
1423 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1424 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001425 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001426 return false;
1427 }
1428 else
1429 {
1430 if (!module->ResolveFileAddress (addr, so_addr))
1431 return false;
1432 }
1433
Greg Claytone1f50b92011-05-03 22:09:39 +00001434 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001435 DumpAddress (exe_scope, so_addr, verbose, strm);
1436// strm.IndentMore();
1437// strm.Indent (" Address: ");
1438// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1439// strm.PutCString (" (");
1440// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1441// strm.PutCString (")\n");
1442// strm.Indent (" Summary: ");
1443// const uint32_t save_indent = strm.GetIndentLevel ();
1444// strm.SetIndentLevel (save_indent + 13);
1445// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1446// strm.SetIndentLevel (save_indent);
1447// // Print out detailed address information when verbose is enabled
1448// if (verbose)
1449// {
1450// strm.EOL();
1451// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1452// }
1453// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001454 return true;
1455 }
1456
1457 return false;
1458}
1459
1460static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001461LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001462{
1463 if (module)
1464 {
1465 SymbolContext sc;
1466
1467 ObjectFile *objfile = module->GetObjectFile ();
1468 if (objfile)
1469 {
1470 Symtab *symtab = objfile->GetSymtab();
1471 if (symtab)
1472 {
1473 uint32_t i;
1474 std::vector<uint32_t> match_indexes;
1475 ConstString symbol_name (name);
1476 uint32_t num_matches = 0;
1477 if (name_is_regex)
1478 {
1479 RegularExpression name_regexp(name);
1480 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1481 eSymbolTypeAny,
1482 match_indexes);
1483 }
1484 else
1485 {
1486 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1487 }
1488
1489
1490 if (num_matches > 0)
1491 {
1492 strm.Indent ();
1493 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1494 name_is_regex ? "the regular expression " : "", name);
1495 DumpFullpath (strm, &module->GetFileSpec(), 0);
1496 strm.PutCString(":\n");
1497 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001498 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001499 for (i=0; i < num_matches; ++i)
1500 {
1501 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001502 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1503 symbol->GetAddress(),
1504 verbose,
1505 strm);
1506
1507// strm.Indent ();
1508// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001509 }
1510 strm.IndentLess ();
1511 return num_matches;
1512 }
1513 }
1514 }
1515 }
1516 return 0;
1517}
1518
1519
1520static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001521DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001522{
1523 strm.IndentMore ();
1524 uint32_t i;
1525 const uint32_t num_matches = sc_list.GetSize();
1526
1527 for (i=0; i<num_matches; ++i)
1528 {
1529 SymbolContext sc;
1530 if (sc_list.GetContextAtIndex(i, sc))
1531 {
Sean Callanand7793d22012-02-11 00:24:04 +00001532 AddressRange range;
1533
1534 sc.GetAddressRange(eSymbolContextEverything,
1535 0,
1536 true,
1537 range);
1538
Greg Clayton2ad894b2012-05-15 18:43:44 +00001539 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001540 }
1541 }
1542 strm.IndentLess ();
1543}
1544
1545static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001546LookupFunctionInModule (CommandInterpreter &interpreter,
1547 Stream &strm,
1548 Module *module,
1549 const char *name,
1550 bool name_is_regex,
1551 bool include_inlines,
1552 bool include_symbols,
1553 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001554{
1555 if (module && name && name[0])
1556 {
1557 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001558 const bool append = true;
1559 uint32_t num_matches = 0;
1560 if (name_is_regex)
1561 {
1562 RegularExpression function_name_regex (name);
1563 num_matches = module->FindFunctions (function_name_regex,
1564 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001565 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001566 append,
1567 sc_list);
1568 }
1569 else
1570 {
1571 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001572 num_matches = module->FindFunctions (function_name,
1573 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001574 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1575 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001576 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001577 append,
1578 sc_list);
1579 }
1580
1581 if (num_matches)
1582 {
1583 strm.Indent ();
1584 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1585 DumpFullpath (strm, &module->GetFileSpec(), 0);
1586 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001587 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001588 }
1589 return num_matches;
1590 }
1591 return 0;
1592}
1593
1594static uint32_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001595LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001596 Stream &strm,
1597 Module *module,
1598 const char *name_cstr,
1599 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001600{
1601 if (module && name_cstr && name_cstr[0])
1602 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001603 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001604 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001605 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001606 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001607 SymbolContext sc;
1608
1609 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001610 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001611
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001612 if (num_matches)
1613 {
1614 strm.Indent ();
1615 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1616 DumpFullpath (strm, &module->GetFileSpec(), 0);
1617 strm.PutCString(":\n");
1618 const uint32_t num_types = type_list.GetSize();
1619 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001620 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001621 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1622 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001623 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001624 // Resolve the clang type so that any forward references
1625 // to types that haven't yet been parsed will get parsed.
1626 type_sp->GetClangFullType ();
1627 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001628 // Print all typedef chains
1629 TypeSP typedef_type_sp (type_sp);
1630 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1631 while (typedefed_type_sp)
1632 {
1633 strm.EOL();
1634 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1635 typedefed_type_sp->GetClangFullType ();
1636 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1637 typedef_type_sp = typedefed_type_sp;
1638 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1639 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001640 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001641 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001642 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001643 }
1644 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001645 }
1646 return 0;
1647}
1648
1649static uint32_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001650LookupTypeHere (CommandInterpreter &interpreter,
1651 Stream &strm,
1652 const SymbolContext &sym_ctx,
1653 const char *name_cstr,
1654 bool name_is_regex)
1655{
1656 if (!sym_ctx.module_sp)
1657 return 0;
1658
1659 TypeList type_list;
1660 const uint32_t max_num_matches = UINT32_MAX;
1661 uint32_t num_matches = 1;
1662 bool name_is_fully_qualified = false;
1663
1664 ConstString name(name_cstr);
1665 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1666
1667 if (num_matches)
1668 {
1669 strm.Indent ();
1670 strm.PutCString("Best match found in ");
1671 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1672 strm.PutCString(":\n");
1673
1674 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1675 if (type_sp)
1676 {
1677 // Resolve the clang type so that any forward references
1678 // to types that haven't yet been parsed will get parsed.
1679 type_sp->GetClangFullType ();
1680 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1681 // Print all typedef chains
1682 TypeSP typedef_type_sp (type_sp);
1683 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1684 while (typedefed_type_sp)
1685 {
1686 strm.EOL();
1687 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1688 typedefed_type_sp->GetClangFullType ();
1689 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1690 typedef_type_sp = typedefed_type_sp;
1691 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1692 }
1693 }
1694 strm.EOL();
1695 }
1696 return num_matches;
1697}
1698
1699static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001700LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001701 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001702 Module *module,
1703 const FileSpec &file_spec,
1704 uint32_t line,
1705 bool check_inlines,
1706 bool verbose)
1707{
1708 if (module && file_spec)
1709 {
1710 SymbolContextList sc_list;
1711 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1712 eSymbolContextEverything, sc_list);
1713 if (num_matches > 0)
1714 {
1715 strm.Indent ();
1716 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1717 strm << file_spec;
1718 if (line > 0)
1719 strm.Printf (":%u", line);
1720 strm << " in ";
1721 DumpFullpath (strm, &module->GetFileSpec(), 0);
1722 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001723 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001724 return num_matches;
1725 }
1726 }
1727 return 0;
1728
1729}
1730
Greg Clayton91048ef2011-11-10 01:18:58 +00001731
1732static size_t
1733FindModulesByName (Target *target,
1734 const char *module_name,
1735 ModuleList &module_list,
1736 bool check_global_list)
1737{
1738// Dump specified images (by basename or fullpath)
1739 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001740 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001741
1742 const size_t initial_size = module_list.GetSize ();
1743
Greg Clayton316f57f2012-07-11 20:46:47 +00001744 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001745 {
1746 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001747 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001748 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1749 ModuleSP module_sp;
1750 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1751 {
1752 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1753
1754 if (module)
1755 {
Greg Clayton444fe992012-02-26 05:51:37 +00001756 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001757 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001758 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001759 module_list.AppendIfNeeded(module_sp);
1760 }
1761 }
1762 }
1763 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001764 else
1765 {
1766 if (target)
1767 {
1768 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1769
1770 // Not found in our module list for our target, check the main
1771 // shared module list in case it is a extra file used somewhere
1772 // else
1773 if (num_matches == 0)
1774 {
1775 module_spec.GetArchitecture() = target->GetArchitecture();
1776 ModuleList::FindSharedModules (module_spec, module_list);
1777 }
1778 }
1779 else
1780 {
1781 ModuleList::FindSharedModules (module_spec,module_list);
1782 }
1783 }
1784
Greg Clayton91048ef2011-11-10 01:18:58 +00001785 return module_list.GetSize () - initial_size;
1786}
1787
Greg Claytone1f50b92011-05-03 22:09:39 +00001788#pragma mark CommandObjectTargetModulesModuleAutoComplete
1789
1790//----------------------------------------------------------------------
1791// A base command object class that can auto complete with module file
1792// paths
1793//----------------------------------------------------------------------
1794
Jim Inghamda26bd22012-06-08 21:56:10 +00001795class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001796{
1797public:
1798
1799 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1800 const char *name,
1801 const char *help,
1802 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001803 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001804 {
1805 CommandArgumentEntry arg;
1806 CommandArgumentData file_arg;
1807
1808 // Define the first (and only) variant of this arg.
1809 file_arg.arg_type = eArgTypeFilename;
1810 file_arg.arg_repetition = eArgRepeatStar;
1811
1812 // There is only one variant this argument could be; put it into the argument entry.
1813 arg.push_back (file_arg);
1814
1815 // Push the data for the first argument into the m_arguments vector.
1816 m_arguments.push_back (arg);
1817 }
1818
1819 virtual
1820 ~CommandObjectTargetModulesModuleAutoComplete ()
1821 {
1822 }
1823
1824 virtual int
1825 HandleArgumentCompletion (Args &input,
1826 int &cursor_index,
1827 int &cursor_char_position,
1828 OptionElementVector &opt_element_vector,
1829 int match_start_point,
1830 int max_return_elements,
1831 bool &word_complete,
1832 StringList &matches)
1833 {
1834 // Arguments are the standard module completer.
1835 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1836 completion_str.erase (cursor_char_position);
1837
1838 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1839 CommandCompletions::eModuleCompletion,
1840 completion_str.c_str(),
1841 match_start_point,
1842 max_return_elements,
1843 NULL,
1844 word_complete,
1845 matches);
1846 return matches.GetSize();
1847 }
1848};
1849
1850#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1851
1852//----------------------------------------------------------------------
1853// A base command object class that can auto complete with module source
1854// file paths
1855//----------------------------------------------------------------------
1856
Jim Inghamda26bd22012-06-08 21:56:10 +00001857class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001858{
1859public:
1860
1861 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
1862 const char *name,
1863 const char *help,
1864 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001865 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001866 {
1867 CommandArgumentEntry arg;
1868 CommandArgumentData source_file_arg;
1869
1870 // Define the first (and only) variant of this arg.
1871 source_file_arg.arg_type = eArgTypeSourceFile;
1872 source_file_arg.arg_repetition = eArgRepeatPlus;
1873
1874 // There is only one variant this argument could be; put it into the argument entry.
1875 arg.push_back (source_file_arg);
1876
1877 // Push the data for the first argument into the m_arguments vector.
1878 m_arguments.push_back (arg);
1879 }
1880
1881 virtual
1882 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1883 {
1884 }
1885
1886 virtual int
1887 HandleArgumentCompletion (Args &input,
1888 int &cursor_index,
1889 int &cursor_char_position,
1890 OptionElementVector &opt_element_vector,
1891 int match_start_point,
1892 int max_return_elements,
1893 bool &word_complete,
1894 StringList &matches)
1895 {
1896 // Arguments are the standard source file completer.
1897 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1898 completion_str.erase (cursor_char_position);
1899
1900 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1901 CommandCompletions::eSourceFileCompletion,
1902 completion_str.c_str(),
1903 match_start_point,
1904 max_return_elements,
1905 NULL,
1906 word_complete,
1907 matches);
1908 return matches.GetSize();
1909 }
1910};
1911
1912
1913#pragma mark CommandObjectTargetModulesDumpSymtab
1914
1915
1916class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
1917{
1918public:
1919 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
1920 CommandObjectTargetModulesModuleAutoComplete (interpreter,
1921 "target modules dump symtab",
1922 "Dump the symbol table from one or more target modules.",
1923 NULL),
1924 m_options (interpreter)
1925 {
1926 }
1927
1928 virtual
1929 ~CommandObjectTargetModulesDumpSymtab ()
1930 {
1931 }
1932
Jim Inghamda26bd22012-06-08 21:56:10 +00001933 virtual Options *
1934 GetOptions ()
1935 {
1936 return &m_options;
1937 }
1938
1939 class CommandOptions : public Options
1940 {
1941 public:
1942
1943 CommandOptions (CommandInterpreter &interpreter) :
1944 Options(interpreter),
1945 m_sort_order (eSortOrderNone)
1946 {
1947 }
1948
1949 virtual
1950 ~CommandOptions ()
1951 {
1952 }
1953
1954 virtual Error
1955 SetOptionValue (uint32_t option_idx, const char *option_arg)
1956 {
1957 Error error;
1958 char short_option = (char) m_getopt_table[option_idx].val;
1959
1960 switch (short_option)
1961 {
1962 case 's':
1963 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
1964 g_option_table[option_idx].enum_values,
1965 eSortOrderNone,
1966 error);
1967 break;
1968
1969 default:
1970 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
1971 break;
1972
1973 }
1974 return error;
1975 }
1976
1977 void
1978 OptionParsingStarting ()
1979 {
1980 m_sort_order = eSortOrderNone;
1981 }
1982
1983 const OptionDefinition*
1984 GetDefinitions ()
1985 {
1986 return g_option_table;
1987 }
1988
1989 // Options table: Required for subclasses of Options.
1990 static OptionDefinition g_option_table[];
1991
1992 SortOrder m_sort_order;
1993 };
1994
1995protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00001996 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001997 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00001998 CommandReturnObject &result)
1999 {
2000 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2001 if (target == NULL)
2002 {
2003 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2004 result.SetStatus (eReturnStatusFailed);
2005 return false;
2006 }
2007 else
2008 {
2009 uint32_t num_dumped = 0;
2010
2011 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2012 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2013 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2014
2015 if (command.GetArgumentCount() == 0)
2016 {
2017 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002018 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002019 const uint32_t num_modules = target->GetImages().GetSize();
2020 if (num_modules > 0)
2021 {
2022 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2023 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2024 {
2025 if (num_dumped > 0)
2026 {
2027 result.GetOutputStream().EOL();
2028 result.GetOutputStream().EOL();
2029 }
2030 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002031 DumpModuleSymtab (m_interpreter,
2032 result.GetOutputStream(),
2033 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2034 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002035 }
2036 }
2037 else
2038 {
2039 result.AppendError ("the target has no associated executable images");
2040 result.SetStatus (eReturnStatusFailed);
2041 return false;
2042 }
2043 }
2044 else
2045 {
2046 // Dump specified images (by basename or fullpath)
2047 const char *arg_cstr;
2048 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2049 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002050 ModuleList module_list;
2051 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2052 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002053 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002054 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002055 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002056 Module *module = module_list.GetModulePointerAtIndex(i);
2057 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002058 {
2059 if (num_dumped > 0)
2060 {
2061 result.GetOutputStream().EOL();
2062 result.GetOutputStream().EOL();
2063 }
2064 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002065 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002066 }
2067 }
2068 }
2069 else
2070 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2071 }
2072 }
2073
2074 if (num_dumped > 0)
2075 result.SetStatus (eReturnStatusSuccessFinishResult);
2076 else
2077 {
2078 result.AppendError ("no matching executable images found");
2079 result.SetStatus (eReturnStatusFailed);
2080 }
2081 }
2082 return result.Succeeded();
2083 }
2084
Greg Claytone1f50b92011-05-03 22:09:39 +00002085
2086 CommandOptions m_options;
2087};
2088
2089static OptionEnumValueElement
2090g_sort_option_enumeration[4] =
2091{
2092 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2093 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2094 { eSortOrderByName, "name", "Sort output by symbol name."},
2095 { 0, NULL, NULL }
2096};
2097
2098
2099OptionDefinition
2100CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2101{
2102 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2103 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2104};
2105
2106#pragma mark CommandObjectTargetModulesDumpSections
2107
2108//----------------------------------------------------------------------
2109// Image section dumping command
2110//----------------------------------------------------------------------
2111
2112class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2113{
2114public:
2115 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2116 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2117 "target modules dump sections",
2118 "Dump the sections from one or more target modules.",
2119 //"target modules dump sections [<file1> ...]")
2120 NULL)
2121 {
2122 }
2123
2124 virtual
2125 ~CommandObjectTargetModulesDumpSections ()
2126 {
2127 }
2128
Jim Inghamda26bd22012-06-08 21:56:10 +00002129protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002130 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002131 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002132 CommandReturnObject &result)
2133 {
2134 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2135 if (target == NULL)
2136 {
2137 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2138 result.SetStatus (eReturnStatusFailed);
2139 return false;
2140 }
2141 else
2142 {
2143 uint32_t num_dumped = 0;
2144
2145 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2146 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2147 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2148
2149 if (command.GetArgumentCount() == 0)
2150 {
2151 // Dump all sections for all modules images
2152 const uint32_t num_modules = target->GetImages().GetSize();
2153 if (num_modules > 0)
2154 {
2155 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2156 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2157 {
2158 num_dumped++;
2159 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2160 }
2161 }
2162 else
2163 {
2164 result.AppendError ("the target has no associated executable images");
2165 result.SetStatus (eReturnStatusFailed);
2166 return false;
2167 }
2168 }
2169 else
2170 {
2171 // Dump specified images (by basename or fullpath)
2172 const char *arg_cstr;
2173 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2174 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002175 ModuleList module_list;
2176 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2177 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002178 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002179 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002180 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002181 Module *module = module_list.GetModulePointerAtIndex(i);
2182 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002183 {
2184 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002185 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002186 }
2187 }
2188 }
2189 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002190 {
2191 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002192 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002193
Greg Claytone1f50b92011-05-03 22:09:39 +00002194 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002195 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002196 }
2197 }
2198
2199 if (num_dumped > 0)
2200 result.SetStatus (eReturnStatusSuccessFinishResult);
2201 else
2202 {
2203 result.AppendError ("no matching executable images found");
2204 result.SetStatus (eReturnStatusFailed);
2205 }
2206 }
2207 return result.Succeeded();
2208 }
2209};
2210
2211
2212#pragma mark CommandObjectTargetModulesDumpSymfile
2213
2214//----------------------------------------------------------------------
2215// Image debug symbol dumping command
2216//----------------------------------------------------------------------
2217
2218class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2219{
2220public:
2221 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2222 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2223 "target modules dump symfile",
2224 "Dump the debug symbol file for one or more target modules.",
2225 //"target modules dump symfile [<file1> ...]")
2226 NULL)
2227 {
2228 }
2229
2230 virtual
2231 ~CommandObjectTargetModulesDumpSymfile ()
2232 {
2233 }
2234
Jim Inghamda26bd22012-06-08 21:56:10 +00002235protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002236 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002237 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002238 CommandReturnObject &result)
2239 {
2240 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2241 if (target == NULL)
2242 {
2243 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2244 result.SetStatus (eReturnStatusFailed);
2245 return false;
2246 }
2247 else
2248 {
2249 uint32_t num_dumped = 0;
2250
2251 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2252 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2253 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2254
2255 if (command.GetArgumentCount() == 0)
2256 {
2257 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002258 ModuleList &target_modules = target->GetImages();
2259 Mutex::Locker modules_locker (target_modules.GetMutex());
2260 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002261 if (num_modules > 0)
2262 {
2263 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2264 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2265 {
Jim Ingham93367902012-05-30 02:19:25 +00002266 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002267 num_dumped++;
2268 }
2269 }
2270 else
2271 {
2272 result.AppendError ("the target has no associated executable images");
2273 result.SetStatus (eReturnStatusFailed);
2274 return false;
2275 }
2276 }
2277 else
2278 {
2279 // Dump specified images (by basename or fullpath)
2280 const char *arg_cstr;
2281 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2282 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002283 ModuleList module_list;
2284 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2285 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002286 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002287 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002288 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002289 Module *module = module_list.GetModulePointerAtIndex(i);
2290 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002291 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002292 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002293 num_dumped++;
2294 }
2295 }
2296 }
2297 else
2298 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2299 }
2300 }
2301
2302 if (num_dumped > 0)
2303 result.SetStatus (eReturnStatusSuccessFinishResult);
2304 else
2305 {
2306 result.AppendError ("no matching executable images found");
2307 result.SetStatus (eReturnStatusFailed);
2308 }
2309 }
2310 return result.Succeeded();
2311 }
2312};
2313
2314
2315#pragma mark CommandObjectTargetModulesDumpLineTable
2316
2317//----------------------------------------------------------------------
2318// Image debug line table dumping command
2319//----------------------------------------------------------------------
2320
2321class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2322{
2323public:
2324 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2325 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2326 "target modules dump line-table",
2327 "Dump the debug symbol file for one or more target modules.",
2328 NULL)
2329 {
2330 }
2331
2332 virtual
2333 ~CommandObjectTargetModulesDumpLineTable ()
2334 {
2335 }
2336
Jim Inghamda26bd22012-06-08 21:56:10 +00002337protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002338 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002339 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002340 CommandReturnObject &result)
2341 {
2342 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2343 if (target == NULL)
2344 {
2345 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2346 result.SetStatus (eReturnStatusFailed);
2347 return false;
2348 }
2349 else
2350 {
2351 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2352 uint32_t total_num_dumped = 0;
2353
2354 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2355 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2356 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2357
2358 if (command.GetArgumentCount() == 0)
2359 {
2360 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2361 result.SetStatus (eReturnStatusFailed);
2362 }
2363 else
2364 {
2365 // Dump specified images (by basename or fullpath)
2366 const char *arg_cstr;
2367 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2368 {
2369 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002370
2371 ModuleList &target_modules = target->GetImages();
2372 Mutex::Locker modules_locker(target_modules.GetMutex());
2373 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002374 if (num_modules > 0)
2375 {
2376 uint32_t num_dumped = 0;
2377 for (uint32_t i = 0; i<num_modules; ++i)
2378 {
2379 if (DumpCompileUnitLineTable (m_interpreter,
2380 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002381 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002382 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002383 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002384 num_dumped++;
2385 }
2386 if (num_dumped == 0)
2387 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2388 else
2389 total_num_dumped += num_dumped;
2390 }
2391 }
2392 }
2393
2394 if (total_num_dumped > 0)
2395 result.SetStatus (eReturnStatusSuccessFinishResult);
2396 else
2397 {
2398 result.AppendError ("no source filenames matched any command arguments");
2399 result.SetStatus (eReturnStatusFailed);
2400 }
2401 }
2402 return result.Succeeded();
2403 }
2404};
2405
2406
2407#pragma mark CommandObjectTargetModulesDump
2408
2409//----------------------------------------------------------------------
2410// Dump multi-word command for target modules
2411//----------------------------------------------------------------------
2412
2413class CommandObjectTargetModulesDump : public CommandObjectMultiword
2414{
2415public:
2416
2417 //------------------------------------------------------------------
2418 // Constructors and Destructors
2419 //------------------------------------------------------------------
2420 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2421 CommandObjectMultiword (interpreter,
2422 "target modules dump",
2423 "A set of commands for dumping information about one or more target modules.",
2424 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2425 {
2426 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2427 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2428 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2429 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2430 }
2431
2432 virtual
2433 ~CommandObjectTargetModulesDump()
2434 {
2435 }
2436};
2437
Jim Inghamda26bd22012-06-08 21:56:10 +00002438class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002439{
2440public:
2441 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002442 CommandObjectParsed (interpreter,
2443 "target modules add",
2444 "Add a new module to the current target's modules.",
2445 "target modules add [<module>]")
Greg Claytone1f50b92011-05-03 22:09:39 +00002446 {
2447 }
2448
2449 virtual
2450 ~CommandObjectTargetModulesAdd ()
2451 {
2452 }
2453
Jim Inghamda26bd22012-06-08 21:56:10 +00002454 int
2455 HandleArgumentCompletion (Args &input,
2456 int &cursor_index,
2457 int &cursor_char_position,
2458 OptionElementVector &opt_element_vector,
2459 int match_start_point,
2460 int max_return_elements,
2461 bool &word_complete,
2462 StringList &matches)
2463 {
2464 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2465 completion_str.erase (cursor_char_position);
2466
2467 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2468 CommandCompletions::eDiskFileCompletion,
2469 completion_str.c_str(),
2470 match_start_point,
2471 max_return_elements,
2472 NULL,
2473 word_complete,
2474 matches);
2475 return matches.GetSize();
2476 }
2477
2478protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002479 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002480 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002481 CommandReturnObject &result)
2482 {
2483 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2484 if (target == NULL)
2485 {
2486 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2487 result.SetStatus (eReturnStatusFailed);
2488 return false;
2489 }
2490 else
2491 {
2492 const size_t argc = args.GetArgumentCount();
2493 if (argc == 0)
2494 {
2495 result.AppendError ("one or more executable image paths must be specified");
2496 result.SetStatus (eReturnStatusFailed);
2497 return false;
2498 }
2499 else
2500 {
2501 for (size_t i=0; i<argc; ++i)
2502 {
2503 const char *path = args.GetArgumentAtIndex(i);
2504 if (path)
2505 {
2506 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002507 if (file_spec.Exists())
2508 {
Greg Clayton444fe992012-02-26 05:51:37 +00002509 ModuleSpec module_spec (file_spec);
2510 ModuleSP module_sp (target->GetSharedModule (module_spec));
Greg Claytone1f50b92011-05-03 22:09:39 +00002511 if (!module_sp)
2512 {
2513 result.AppendError ("one or more executable image paths must be specified");
2514 result.SetStatus (eReturnStatusFailed);
2515 return false;
2516 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002517 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002518 }
2519 else
2520 {
2521 char resolved_path[PATH_MAX];
2522 result.SetStatus (eReturnStatusFailed);
2523 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2524 {
2525 if (strcmp (resolved_path, path) != 0)
2526 {
2527 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2528 break;
2529 }
2530 }
2531 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2532 break;
2533 }
2534 }
2535 }
2536 }
2537 }
2538 return result.Succeeded();
2539 }
2540
Greg Claytone1f50b92011-05-03 22:09:39 +00002541};
2542
2543class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2544{
2545public:
2546 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2547 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2548 "target modules load",
2549 "Set the load addresses for one or more sections in a target module.",
2550 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2551 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002552 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeFilename, "Fullpath or basename for module to load."),
Greg Claytone1f50b92011-05-03 22:09:39 +00002553 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)
2554 {
2555 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2556 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2557 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2558 m_option_group.Finalize();
2559 }
2560
2561 virtual
2562 ~CommandObjectTargetModulesLoad ()
2563 {
2564 }
2565
Jim Inghamda26bd22012-06-08 21:56:10 +00002566 virtual Options *
2567 GetOptions ()
2568 {
2569 return &m_option_group;
2570 }
2571
2572protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002573 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002574 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002575 CommandReturnObject &result)
2576 {
2577 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2578 if (target == NULL)
2579 {
2580 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2581 result.SetStatus (eReturnStatusFailed);
2582 return false;
2583 }
2584 else
2585 {
2586 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002587 ModuleSpec module_spec;
2588 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002589 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002590 {
2591 search_using_module_spec = true;
2592 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2593 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002594
2595 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002596 {
2597 search_using_module_spec = true;
2598 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2599 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002600
Greg Clayton444fe992012-02-26 05:51:37 +00002601 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002602 {
2603
2604 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002605 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002606
2607 char path[PATH_MAX];
2608 if (num_matches == 1)
2609 {
2610 Module *module = matching_modules.GetModulePointerAtIndex(0);
2611 if (module)
2612 {
2613 ObjectFile *objfile = module->GetObjectFile();
2614 if (objfile)
2615 {
2616 SectionList *section_list = objfile->GetSectionList();
2617 if (section_list)
2618 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002619 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002620 if (argc == 0)
2621 {
2622 if (m_slide_option.GetOptionValue().OptionWasSet())
2623 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002624 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2625 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002626 }
2627 else
2628 {
2629 result.AppendError ("one or more section name + load address pair must be specified");
2630 result.SetStatus (eReturnStatusFailed);
2631 return false;
2632 }
2633 }
2634 else
2635 {
2636 if (m_slide_option.GetOptionValue().OptionWasSet())
2637 {
2638 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2639 result.SetStatus (eReturnStatusFailed);
2640 return false;
2641 }
2642
2643 for (size_t i=0; i<argc; i += 2)
2644 {
2645 const char *sect_name = args.GetArgumentAtIndex(i);
2646 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2647 if (sect_name && load_addr_cstr)
2648 {
2649 ConstString const_sect_name(sect_name);
2650 bool success = false;
2651 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2652 if (success)
2653 {
2654 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2655 if (section_sp)
2656 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002657 if (section_sp->IsThreadSpecific())
2658 {
2659 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2660 result.SetStatus (eReturnStatusFailed);
2661 break;
2662 }
2663 else
2664 {
Greg Clayton545762f2012-07-07 01:24:12 +00002665 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002666 changed = true;
2667 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr);
2668 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002669 }
2670 else
2671 {
2672 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2673 result.SetStatus (eReturnStatusFailed);
2674 break;
2675 }
2676 }
2677 else
2678 {
2679 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2680 result.SetStatus (eReturnStatusFailed);
2681 break;
2682 }
2683 }
2684 else
2685 {
2686 if (sect_name)
2687 result.AppendError ("section names must be followed by a load address.\n");
2688 else
2689 result.AppendError ("one or more section name + load address pair must be specified.\n");
2690 result.SetStatus (eReturnStatusFailed);
2691 break;
2692 }
2693 }
2694 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002695
2696 if (changed)
2697 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002698 }
2699 else
2700 {
2701 module->GetFileSpec().GetPath (path, sizeof(path));
2702 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2703 result.SetStatus (eReturnStatusFailed);
2704 }
2705 }
2706 else
2707 {
2708 module->GetFileSpec().GetPath (path, sizeof(path));
2709 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2710 result.SetStatus (eReturnStatusFailed);
2711 }
2712 }
2713 else
2714 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002715 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2716 if (module_spec_file)
2717 {
2718 module_spec_file->GetPath (path, sizeof(path));
2719 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2720 }
2721 else
2722 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002723 result.SetStatus (eReturnStatusFailed);
2724 }
2725 }
2726 else
2727 {
2728 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002729
2730 if (module_spec.GetFileSpec())
2731 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002732 else
2733 path[0] = '\0';
2734
Greg Clayton444fe992012-02-26 05:51:37 +00002735 if (module_spec.GetUUIDPtr())
2736 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002737 else
2738 uuid_cstr[0] = '\0';
2739 if (num_matches > 1)
2740 {
2741 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2742 path[0] ? " file=" : "",
2743 path,
2744 uuid_cstr[0] ? " uuid=" : "",
2745 uuid_cstr);
2746 for (size_t i=0; i<num_matches; ++i)
2747 {
2748 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2749 result.AppendMessageWithFormat("%s\n", path);
2750 }
2751 }
2752 else
2753 {
2754 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2755 path[0] ? " file=" : "",
2756 path,
2757 uuid_cstr[0] ? " uuid=" : "",
2758 uuid_cstr);
2759 }
2760 result.SetStatus (eReturnStatusFailed);
2761 }
2762 }
2763 else
2764 {
2765 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2766 result.SetStatus (eReturnStatusFailed);
2767 return false;
2768 }
2769 }
2770 return result.Succeeded();
2771 }
2772
Greg Claytone1f50b92011-05-03 22:09:39 +00002773 OptionGroupOptions m_option_group;
2774 OptionGroupUUID m_uuid_option_group;
2775 OptionGroupFile m_file_option;
2776 OptionGroupUInt64 m_slide_option;
2777};
2778
2779//----------------------------------------------------------------------
2780// List images with associated information
2781//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002782class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002783{
2784public:
2785
2786 class CommandOptions : public Options
2787 {
2788 public:
2789
2790 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002791 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002792 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002793 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002794 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002795 {
2796 }
2797
2798 virtual
2799 ~CommandOptions ()
2800 {
2801 }
2802
2803 virtual Error
2804 SetOptionValue (uint32_t option_idx, const char *option_arg)
2805 {
2806 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002807 if (short_option == 'g')
2808 {
2809 m_use_global_module_list = true;
2810 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002811 else if (short_option == 'a')
2812 {
2813 bool success;
2814 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2815 if (!success)
2816 {
2817 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002818 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002819 }
2820 }
Greg Clayton899025f2011-08-09 00:01:09 +00002821 else
2822 {
2823 uint32_t width = 0;
2824 if (option_arg)
2825 width = strtoul (option_arg, NULL, 0);
2826 m_format_array.push_back(std::make_pair(short_option, width));
2827 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002828 Error error;
2829 return error;
2830 }
2831
2832 void
2833 OptionParsingStarting ()
2834 {
2835 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002836 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002837 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002838 }
2839
2840 const OptionDefinition*
2841 GetDefinitions ()
2842 {
2843 return g_option_table;
2844 }
2845
2846 // Options table: Required for subclasses of Options.
2847
2848 static OptionDefinition g_option_table[];
2849
2850 // Instance variables to hold the values for command options.
2851 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2852 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002853 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002854 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002855 };
2856
2857 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002858 CommandObjectParsed (interpreter,
2859 "target modules list",
2860 "List current executable and dependent shared library images.",
2861 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002862 m_options (interpreter)
2863 {
2864 }
2865
2866 virtual
2867 ~CommandObjectTargetModulesList ()
2868 {
2869 }
2870
2871 virtual
2872 Options *
2873 GetOptions ()
2874 {
2875 return &m_options;
2876 }
2877
Jim Inghamda26bd22012-06-08 21:56:10 +00002878protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002879 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002880 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002881 CommandReturnObject &result)
2882 {
2883 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002884 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00002885 // Define a local module list here to ensure it lives longer than any "locker"
2886 // object which might lock its contents below (through the "module_list_ptr"
2887 // variable).
2888 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00002889 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002890 {
2891 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2892 result.SetStatus (eReturnStatusFailed);
2893 return false;
2894 }
2895 else
2896 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002897 if (target)
2898 {
2899 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2900 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2901 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2902 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002903 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002904 Stream &strm = result.GetOutputStream();
2905
2906 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2907 {
2908 if (target)
2909 {
2910 Address module_address;
2911 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2912 {
Greg Clayton3508c382012-02-24 01:59:29 +00002913 ModuleSP module_sp (module_address.GetModule());
2914 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002915 {
Greg Clayton3508c382012-02-24 01:59:29 +00002916 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002917 result.SetStatus (eReturnStatusSuccessFinishResult);
2918 }
2919 else
2920 {
2921 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr);
2922 result.SetStatus (eReturnStatusFailed);
2923 }
2924 }
2925 else
2926 {
2927 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr);
2928 result.SetStatus (eReturnStatusFailed);
2929 }
2930 }
2931 else
2932 {
2933 result.AppendError ("Can only look up modules by address with a valid target.");
2934 result.SetStatus (eReturnStatusFailed);
2935 }
2936 return result.Succeeded();
2937 }
2938
Jim Ingham93367902012-05-30 02:19:25 +00002939 uint32_t num_modules = 0;
2940 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
2941 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
2942 // the global module list directly.
Greg Clayton2ad894b2012-05-15 18:43:44 +00002943 ModuleList *module_list_ptr = NULL;
2944 const size_t argc = command.GetArgumentCount();
2945 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00002946 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002947 if (use_global_module_list)
2948 {
2949 locker.Lock (Module::GetAllocationModuleCollectionMutex());
2950 num_modules = Module::GetNumberAllocatedModules();
2951 }
2952 else
2953 {
2954 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00002955 }
Greg Clayton899025f2011-08-09 00:01:09 +00002956 }
2957 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00002958 {
2959 for (size_t i=0; i<argc; ++i)
2960 {
2961 // Dump specified images (by basename or fullpath)
2962 const char *arg_cstr = command.GetArgumentAtIndex(i);
2963 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2964 if (num_matches == 0)
2965 {
2966 if (argc == 1)
2967 {
2968 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
2969 result.SetStatus (eReturnStatusFailed);
2970 return false;
2971 }
2972 }
2973 }
2974
Greg Clayton2ad894b2012-05-15 18:43:44 +00002975 module_list_ptr = &module_list;
2976 }
Jim Ingham93367902012-05-30 02:19:25 +00002977
2978 if (module_list_ptr != NULL)
2979 {
2980 locker.Lock(module_list_ptr->GetMutex());
2981 num_modules = module_list_ptr->GetSize();
2982 }
Greg Clayton899025f2011-08-09 00:01:09 +00002983
Greg Claytone1f50b92011-05-03 22:09:39 +00002984 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00002985 {
Greg Claytone1f50b92011-05-03 22:09:39 +00002986 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2987 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002988 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00002989 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00002990 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00002991 {
Jim Ingham93367902012-05-30 02:19:25 +00002992 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00002993 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00002994 }
2995 else
2996 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00002997 module = Module::GetAllocatedModuleAtIndex(image_idx);
2998 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00002999 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003000
Greg Claytonb5a8f142012-02-05 02:38:54 +00003001 int indent = strm.Printf("[%3u] ", image_idx);
3002 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003003
Greg Claytone1f50b92011-05-03 22:09:39 +00003004 }
3005 result.SetStatus (eReturnStatusSuccessFinishResult);
3006 }
3007 else
3008 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003009 if (argc)
3010 {
3011 if (use_global_module_list)
3012 result.AppendError ("the global module list has no matching modules");
3013 else
3014 result.AppendError ("the target has no matching modules");
3015 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003016 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003017 {
3018 if (use_global_module_list)
3019 result.AppendError ("the global module list is empty");
3020 else
3021 result.AppendError ("the target has no associated executable images");
3022 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003023 result.SetStatus (eReturnStatusFailed);
3024 return false;
3025 }
3026 }
3027 return result.Succeeded();
3028 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003029
3030 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003031 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003032 {
3033
Jim Ingham6f01c932012-10-12 17:34:26 +00003034 if (module == NULL)
3035 {
3036 strm.PutCString("Null module");
3037 return;
3038 }
3039
Jim Ingham6bdea822011-10-24 18:36:33 +00003040 bool dump_object_name = false;
3041 if (m_options.m_format_array.empty())
3042 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003043 m_options.m_format_array.push_back(std::make_pair('u', 0));
3044 m_options.m_format_array.push_back(std::make_pair('h', 0));
3045 m_options.m_format_array.push_back(std::make_pair('f', 0));
3046 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003047 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003048 const size_t num_entries = m_options.m_format_array.size();
3049 bool print_space = false;
3050 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003051 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003052 if (print_space)
3053 strm.PutChar(' ');
3054 print_space = true;
3055 const char format_char = m_options.m_format_array[i].first;
3056 uint32_t width = m_options.m_format_array[i].second;
3057 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003058 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003059 case 'A':
3060 DumpModuleArchitecture (strm, module, false, width);
3061 break;
3062
3063 case 't':
3064 DumpModuleArchitecture (strm, module, true, width);
3065 break;
3066
3067 case 'f':
3068 DumpFullpath (strm, &module->GetFileSpec(), width);
3069 dump_object_name = true;
3070 break;
3071
3072 case 'd':
3073 DumpDirectory (strm, &module->GetFileSpec(), width);
3074 break;
3075
3076 case 'b':
3077 DumpBasename (strm, &module->GetFileSpec(), width);
3078 dump_object_name = true;
3079 break;
3080
3081 case 'h':
3082 case 'o':
3083 // Image header address
3084 {
3085 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003086
Greg Claytonb5a8f142012-02-05 02:38:54 +00003087 ObjectFile *objfile = module->GetObjectFile ();
3088 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003089 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003090 Address header_addr(objfile->GetHeaderAddress());
3091 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003092 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003093 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003094 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003095 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3096 if (header_load_addr == LLDB_INVALID_ADDRESS)
3097 {
3098 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3099 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003100 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003101 {
3102 if (format_char == 'o')
3103 {
3104 // Show the offset of slide for the image
3105 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
3106 }
3107 else
3108 {
3109 // Show the load address of the image
3110 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr);
3111 }
3112 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003113 break;
3114 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003115 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3116 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3117 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003118 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003119 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003120 strm.Printf ("%*s", addr_nibble_width + 2, "");
3121 }
3122 break;
3123 case 'r':
3124 {
3125 uint32_t ref_count = 0;
3126 ModuleSP module_sp (module->shared_from_this());
3127 if (module_sp)
3128 {
3129 // Take one away to make sure we don't count our local "module_sp"
3130 ref_count = module_sp.use_count() - 1;
3131 }
3132 if (width)
3133 strm.Printf("{%*u}", width, ref_count);
3134 else
3135 strm.Printf("{%u}", ref_count);
3136 }
3137 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003138
Greg Claytonb5a8f142012-02-05 02:38:54 +00003139 case 's':
3140 case 'S':
3141 {
3142 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3143 if (symbol_vendor)
3144 {
3145 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3146 if (symbol_file)
3147 {
3148 if (format_char == 'S')
3149 {
3150 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3151 // Dump symbol file only if different from module file
3152 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3153 {
3154 print_space = false;
3155 break;
3156 }
3157 // Add a newline and indent past the index
3158 strm.Printf ("\n%*s", indent, "");
3159 }
3160 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3161 dump_object_name = true;
3162 break;
3163 }
3164 }
3165 strm.Printf("%.*s", width, "<NONE>");
3166 }
3167 break;
3168
3169 case 'm':
3170 module->GetModificationTime().Dump(&strm, width);
3171 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003172
Greg Claytonb5a8f142012-02-05 02:38:54 +00003173 case 'p':
3174 strm.Printf("%p", module);
3175 break;
3176
3177 case 'u':
3178 DumpModuleUUID(strm, module);
3179 break;
3180
3181 default:
3182 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003183 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003184
3185 }
3186 if (dump_object_name)
3187 {
3188 const char *object_name = module->GetObjectName().GetCString();
3189 if (object_name)
3190 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003191 }
3192 strm.EOL();
3193 }
3194
Greg Claytone1f50b92011-05-03 22:09:39 +00003195 CommandOptions m_options;
3196};
3197
3198OptionDefinition
3199CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3200{
Jim Ingham6bdea822011-10-24 18:36:33 +00003201 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3202 { 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 +00003203 { 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 +00003204 { 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."},
3205 { 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 +00003206 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3207 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3208 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3209 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3210 { 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 +00003211 { 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 +00003212 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3213 { 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."},
3214 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003215 { 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 +00003216 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3217};
3218
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003219#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003220
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003221//----------------------------------------------------------------------
3222// Lookup unwind information in images
3223//----------------------------------------------------------------------
3224
3225class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3226{
3227public:
3228
3229 enum
3230 {
3231 eLookupTypeInvalid = -1,
3232 eLookupTypeAddress = 0,
3233 eLookupTypeSymbol,
3234 eLookupTypeFunction,
3235 eLookupTypeFunctionOrSymbol,
3236 kNumLookupTypes
3237 };
3238
3239 class CommandOptions : public Options
3240 {
3241 public:
3242
3243 CommandOptions (CommandInterpreter &interpreter) :
3244 Options(interpreter),
3245 m_type(eLookupTypeInvalid),
3246 m_str(),
3247 m_addr(LLDB_INVALID_ADDRESS)
3248 {
3249 }
3250
3251 virtual
3252 ~CommandOptions ()
3253 {
3254 }
3255
3256 virtual Error
3257 SetOptionValue (uint32_t option_idx, const char *option_arg)
3258 {
3259 Error error;
3260
3261 char short_option = (char) m_getopt_table[option_idx].val;
3262
3263 switch (short_option)
3264 {
3265 case 'a':
3266 m_type = eLookupTypeAddress;
3267 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3268 if (m_addr == LLDB_INVALID_ADDRESS)
3269 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3270 break;
3271
3272 case 'n':
3273 m_str = option_arg;
3274 m_type = eLookupTypeFunctionOrSymbol;
3275 break;
3276 }
3277
3278 return error;
3279 }
3280
3281 void
3282 OptionParsingStarting ()
3283 {
3284 m_type = eLookupTypeInvalid;
3285 m_str.clear();
3286 m_addr = LLDB_INVALID_ADDRESS;
3287 }
3288
3289 const OptionDefinition*
3290 GetDefinitions ()
3291 {
3292 return g_option_table;
3293 }
3294
3295 // Options table: Required for subclasses of Options.
3296
3297 static OptionDefinition g_option_table[];
3298
3299 // Instance variables to hold the values for command options.
3300
3301 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3302 std::string m_str; // Holds name lookup
3303 lldb::addr_t m_addr; // Holds the address to lookup
3304 };
3305
3306 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3307 CommandObjectParsed (interpreter,
3308 "target modules show-unwind",
3309 "Show synthesized unwind instructions for a function.",
3310 NULL),
3311 m_options (interpreter)
3312 {
3313 }
3314
3315 virtual
3316 ~CommandObjectTargetModulesShowUnwind ()
3317 {
3318 }
3319
3320 virtual
3321 Options *
3322 GetOptions ()
3323 {
3324 return &m_options;
3325 }
3326
3327protected:
3328 bool
3329 DoExecute (Args& command,
3330 CommandReturnObject &result)
3331 {
3332 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3333 if (!target)
3334 {
3335 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3336 result.SetStatus (eReturnStatusFailed);
3337 return false;
3338 }
3339
3340 ExecutionContext exe_ctx = m_interpreter.GetDebugger().GetSelectedExecutionContext();
3341 Process *process = exe_ctx.GetProcessPtr();
3342 ABI *abi = NULL;
3343 if (process)
3344 abi = process->GetABI().get();
3345
3346 if (process == NULL)
3347 {
3348 result.AppendError ("You must have a process running to use this command.");
3349 result.SetStatus (eReturnStatusFailed);
3350 return false;
3351 }
3352
3353 ThreadList threads(process->GetThreadList());
3354 if (threads.GetSize() == 0)
3355 {
3356 result.AppendError ("The process must be paused to use this command.");
3357 result.SetStatus (eReturnStatusFailed);
3358 return false;
3359 }
3360
3361 ThreadSP thread(threads.GetThreadAtIndex(0));
3362 if (thread.get() == NULL)
3363 {
3364 result.AppendError ("The process must be paused to use this command.");
3365 result.SetStatus (eReturnStatusFailed);
3366 return false;
3367 }
3368
3369 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3370 {
3371 SymbolContextList sc_list;
3372 uint32_t num_matches;
3373 ConstString function_name (m_options.m_str.c_str());
3374 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3375 for (uint32_t idx = 0; idx < num_matches; idx++)
3376 {
3377 SymbolContext sc;
3378 sc_list.GetContextAtIndex(idx, sc);
3379 if (sc.symbol == NULL && sc.function == NULL)
3380 continue;
3381 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3382 continue;
3383 AddressRange range;
3384 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3385 continue;
3386 if (!range.GetBaseAddress().IsValid())
3387 continue;
3388 ConstString funcname(sc.GetFunctionName());
3389 if (funcname.IsEmpty())
3390 continue;
3391 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3392 if (abi)
3393 start_addr = abi->FixCodeAddress(start_addr);
3394
3395 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3396 if (func_unwinders_sp.get() == NULL)
3397 continue;
3398
3399 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3400 if (first_non_prologue_insn.IsValid())
3401 {
3402 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);
3403 result.GetOutputStream().Printf ("\n");
3404 }
3405
3406 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3407 if (non_callsite_unwind_plan.get())
3408 {
3409 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);
3410 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3411 result.GetOutputStream().Printf ("\n");
3412 }
3413
3414 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3415 if (callsite_unwind_plan.get())
3416 {
3417 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);
3418 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3419 result.GetOutputStream().Printf ("\n");
3420 }
3421
3422 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3423 if (arch_default_unwind_plan.get())
3424 {
3425 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3426 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3427 result.GetOutputStream().Printf ("\n");
3428 }
3429
3430 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3431 if (fast_unwind_plan.get())
3432 {
3433 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%llx):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3434 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3435 result.GetOutputStream().Printf ("\n");
3436 }
3437
3438
3439 result.GetOutputStream().Printf ("\n");
3440 }
3441 }
3442 return result.Succeeded();
3443 }
3444
3445 CommandOptions m_options;
3446};
3447
3448OptionDefinition
3449CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3450{
3451 { 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."},
3452 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3453};
Greg Claytone1f50b92011-05-03 22:09:39 +00003454
3455//----------------------------------------------------------------------
3456// Lookup information in images
3457//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003458class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003459{
3460public:
3461
3462 enum
3463 {
3464 eLookupTypeInvalid = -1,
3465 eLookupTypeAddress = 0,
3466 eLookupTypeSymbol,
3467 eLookupTypeFileLine, // Line is optional
3468 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003469 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003470 eLookupTypeType,
3471 kNumLookupTypes
3472 };
3473
3474 class CommandOptions : public Options
3475 {
3476 public:
3477
3478 CommandOptions (CommandInterpreter &interpreter) :
3479 Options(interpreter)
3480 {
3481 OptionParsingStarting();
3482 }
3483
3484 virtual
3485 ~CommandOptions ()
3486 {
3487 }
3488
3489 virtual Error
3490 SetOptionValue (uint32_t option_idx, const char *option_arg)
3491 {
3492 Error error;
3493
3494 char short_option = (char) m_getopt_table[option_idx].val;
3495
3496 switch (short_option)
3497 {
3498 case 'a':
3499 m_type = eLookupTypeAddress;
3500 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3501 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003502 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003503 break;
3504
3505 case 'o':
3506 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3507 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003508 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003509 break;
3510
3511 case 's':
3512 m_str = option_arg;
3513 m_type = eLookupTypeSymbol;
3514 break;
3515
3516 case 'f':
3517 m_file.SetFile (option_arg, false);
3518 m_type = eLookupTypeFileLine;
3519 break;
3520
3521 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003522 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003523 break;
3524
3525 case 'l':
3526 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3527 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003528 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003529 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003530 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003531 m_type = eLookupTypeFileLine;
3532 break;
3533
Greg Clayton2ad894b2012-05-15 18:43:44 +00003534 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003535 m_str = option_arg;
3536 m_type = eLookupTypeFunction;
3537 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003538
3539 case 'n':
3540 m_str = option_arg;
3541 m_type = eLookupTypeFunctionOrSymbol;
3542 break;
3543
Greg Claytone1f50b92011-05-03 22:09:39 +00003544 case 't':
3545 m_str = option_arg;
3546 m_type = eLookupTypeType;
3547 break;
3548
3549 case 'v':
3550 m_verbose = 1;
3551 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003552
3553 case 'A':
3554 m_print_all = true;
3555 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003556
3557 case 'r':
3558 m_use_regex = true;
3559 break;
3560 }
3561
3562 return error;
3563 }
3564
3565 void
3566 OptionParsingStarting ()
3567 {
3568 m_type = eLookupTypeInvalid;
3569 m_str.clear();
3570 m_file.Clear();
3571 m_addr = LLDB_INVALID_ADDRESS;
3572 m_offset = 0;
3573 m_line_number = 0;
3574 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003575 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003576 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003577 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003578 }
3579
3580 const OptionDefinition*
3581 GetDefinitions ()
3582 {
3583 return g_option_table;
3584 }
3585
3586 // Options table: Required for subclasses of Options.
3587
3588 static OptionDefinition g_option_table[];
3589 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3590 std::string m_str; // Holds name lookup
3591 FileSpec m_file; // Files for file lookups
3592 lldb::addr_t m_addr; // Holds the address to lookup
3593 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3594 uint32_t m_line_number; // Line number for file+line lookups
3595 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003596 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003597 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003598 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003599
3600 };
3601
3602 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003603 CommandObjectParsed (interpreter,
3604 "target modules lookup",
3605 "Look up information within executable and dependent shared library images.",
3606 NULL),
3607 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003608 {
3609 CommandArgumentEntry arg;
3610 CommandArgumentData file_arg;
3611
3612 // Define the first (and only) variant of this arg.
3613 file_arg.arg_type = eArgTypeFilename;
3614 file_arg.arg_repetition = eArgRepeatStar;
3615
3616 // There is only one variant this argument could be; put it into the argument entry.
3617 arg.push_back (file_arg);
3618
3619 // Push the data for the first argument into the m_arguments vector.
3620 m_arguments.push_back (arg);
3621 }
3622
3623 virtual
3624 ~CommandObjectTargetModulesLookup ()
3625 {
3626 }
3627
3628 virtual Options *
3629 GetOptions ()
3630 {
3631 return &m_options;
3632 }
3633
Sean Callanan56d31ec2012-06-06 20:49:55 +00003634 bool
3635 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3636 {
3637 switch (m_options.m_type)
3638 {
3639 case eLookupTypeAddress:
3640 case eLookupTypeFileLine:
3641 case eLookupTypeFunction:
3642 case eLookupTypeFunctionOrSymbol:
3643 case eLookupTypeSymbol:
3644 default:
3645 return false;
3646 case eLookupTypeType:
3647 break;
3648 }
3649
3650 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3651
3652 StackFrameSP frame = exe_ctx.GetFrameSP();
3653
3654 if (!frame)
3655 return false;
3656
3657 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3658
3659 if (!sym_ctx.module_sp)
3660 return false;
3661
3662 switch (m_options.m_type)
3663 {
3664 default:
3665 return false;
3666 case eLookupTypeType:
3667 if (!m_options.m_str.empty())
3668 {
3669 if (LookupTypeHere (m_interpreter,
3670 result.GetOutputStream(),
3671 sym_ctx,
3672 m_options.m_str.c_str(),
3673 m_options.m_use_regex))
3674 {
3675 result.SetStatus(eReturnStatusSuccessFinishResult);
3676 return true;
3677 }
3678 }
3679 break;
3680 }
3681
3682 return true;
3683 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003684
3685 bool
3686 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3687 {
3688 switch (m_options.m_type)
3689 {
3690 case eLookupTypeAddress:
3691 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3692 {
3693 if (LookupAddressInModule (m_interpreter,
3694 result.GetOutputStream(),
3695 module,
3696 eSymbolContextEverything,
3697 m_options.m_addr,
3698 m_options.m_offset,
3699 m_options.m_verbose))
3700 {
3701 result.SetStatus(eReturnStatusSuccessFinishResult);
3702 return true;
3703 }
3704 }
3705 break;
3706
3707 case eLookupTypeSymbol:
3708 if (!m_options.m_str.empty())
3709 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003710 if (LookupSymbolInModule (m_interpreter,
3711 result.GetOutputStream(),
3712 module,
3713 m_options.m_str.c_str(),
3714 m_options.m_use_regex,
3715 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003716 {
3717 result.SetStatus(eReturnStatusSuccessFinishResult);
3718 return true;
3719 }
3720 }
3721 break;
3722
3723 case eLookupTypeFileLine:
3724 if (m_options.m_file)
3725 {
3726
3727 if (LookupFileAndLineInModule (m_interpreter,
3728 result.GetOutputStream(),
3729 module,
3730 m_options.m_file,
3731 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003732 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003733 m_options.m_verbose))
3734 {
3735 result.SetStatus(eReturnStatusSuccessFinishResult);
3736 return true;
3737 }
3738 }
3739 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003740
3741 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003742 case eLookupTypeFunction:
3743 if (!m_options.m_str.empty())
3744 {
3745 if (LookupFunctionInModule (m_interpreter,
3746 result.GetOutputStream(),
3747 module,
3748 m_options.m_str.c_str(),
3749 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003750 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003751 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003752 m_options.m_verbose))
3753 {
3754 result.SetStatus(eReturnStatusSuccessFinishResult);
3755 return true;
3756 }
3757 }
3758 break;
3759
Greg Clayton2ad894b2012-05-15 18:43:44 +00003760
Greg Claytone1f50b92011-05-03 22:09:39 +00003761 case eLookupTypeType:
3762 if (!m_options.m_str.empty())
3763 {
3764 if (LookupTypeInModule (m_interpreter,
3765 result.GetOutputStream(),
3766 module,
3767 m_options.m_str.c_str(),
3768 m_options.m_use_regex))
3769 {
3770 result.SetStatus(eReturnStatusSuccessFinishResult);
3771 return true;
3772 }
3773 }
3774 break;
3775
3776 default:
3777 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3778 syntax_error = true;
3779 break;
3780 }
3781
3782 result.SetStatus (eReturnStatusFailed);
3783 return false;
3784 }
3785
Jim Inghamda26bd22012-06-08 21:56:10 +00003786protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003787 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003788 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003789 CommandReturnObject &result)
3790 {
3791 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3792 if (target == NULL)
3793 {
3794 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3795 result.SetStatus (eReturnStatusFailed);
3796 return false;
3797 }
3798 else
3799 {
3800 bool syntax_error = false;
3801 uint32_t i;
3802 uint32_t num_successful_lookups = 0;
3803 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3804 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3805 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3806 // Dump all sections for all modules images
3807
3808 if (command.GetArgumentCount() == 0)
3809 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003810 ModuleSP current_module;
3811
3812 // Where it is possible to look in the current symbol context
3813 // first, try that. If this search was successful and --all
3814 // was not passed, don't print anything else.
3815 if (LookupHere (m_interpreter, result, syntax_error))
3816 {
3817 result.GetOutputStream().EOL();
3818 num_successful_lookups++;
3819 if (!m_options.m_print_all)
3820 {
3821 result.SetStatus (eReturnStatusSuccessFinishResult);
3822 return result.Succeeded();
3823 }
3824 }
3825
3826 // Dump all sections for all other modules
3827
Jim Ingham93367902012-05-30 02:19:25 +00003828 ModuleList &target_modules = target->GetImages();
3829 Mutex::Locker modules_locker(target_modules.GetMutex());
3830 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003831 if (num_modules > 0)
3832 {
3833 for (i = 0; i<num_modules && syntax_error == false; ++i)
3834 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003835 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3836
3837 if (module_pointer != current_module.get() &&
3838 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003839 {
3840 result.GetOutputStream().EOL();
3841 num_successful_lookups++;
3842 }
3843 }
3844 }
3845 else
3846 {
3847 result.AppendError ("the target has no associated executable images");
3848 result.SetStatus (eReturnStatusFailed);
3849 return false;
3850 }
3851 }
3852 else
3853 {
3854 // Dump specified images (by basename or fullpath)
3855 const char *arg_cstr;
3856 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3857 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003858 ModuleList module_list;
3859 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3860 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003861 {
Jason Molendabf41e192012-10-04 22:47:07 +00003862 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00003863 {
Jason Molendabf41e192012-10-04 22:47:07 +00003864 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00003865 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003866 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003867 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003868 {
3869 result.GetOutputStream().EOL();
3870 num_successful_lookups++;
3871 }
3872 }
3873 }
3874 }
3875 else
3876 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3877 }
3878 }
3879
3880 if (num_successful_lookups > 0)
3881 result.SetStatus (eReturnStatusSuccessFinishResult);
3882 else
3883 result.SetStatus (eReturnStatusFailed);
3884 }
3885 return result.Succeeded();
3886 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003887
3888 CommandOptions m_options;
3889};
3890
3891OptionDefinition
3892CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3893{
Sean Callanan3bfaad62012-09-13 21:11:40 +00003894 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3895 { 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 +00003896 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3897 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Sean Callanan3bfaad62012-09-13 21:11:40 +00003898 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
3899 { 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."},
3900 { 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."},
3901 { 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 +00003902 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00003903 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
3904 { 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."},
3905 { LLDB_OPT_SET_5, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules."},
3906 { 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."},
3907 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
3908 { 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."},
3909 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003910};
Chris Lattner24943d22010-06-08 16:52:24 +00003911
3912
Jim Inghamd60d94a2011-03-11 03:53:59 +00003913#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003914
3915//-------------------------------------------------------------------------
3916// CommandObjectMultiwordImageSearchPaths
3917//-------------------------------------------------------------------------
3918
Greg Claytone1f50b92011-05-03 22:09:39 +00003919class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003920{
3921public:
Greg Claytone1f50b92011-05-03 22:09:39 +00003922
3923 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
3924 CommandObjectMultiword (interpreter,
3925 "target modules search-paths",
3926 "A set of commands for operating on debugger target image search paths.",
3927 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00003928 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003929 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
3930 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
3931 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
3932 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
3933 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00003934 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003935
3936 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00003937 {
3938 }
3939};
3940
Greg Claytone1f50b92011-05-03 22:09:39 +00003941
3942
3943#pragma mark CommandObjectTargetModules
3944
3945//-------------------------------------------------------------------------
3946// CommandObjectTargetModules
3947//-------------------------------------------------------------------------
3948
3949class CommandObjectTargetModules : public CommandObjectMultiword
3950{
3951public:
3952 //------------------------------------------------------------------
3953 // Constructors and Destructors
3954 //------------------------------------------------------------------
3955 CommandObjectTargetModules(CommandInterpreter &interpreter) :
3956 CommandObjectMultiword (interpreter,
3957 "target modules",
3958 "A set of commands for accessing information for one or more target modules.",
3959 "target modules <sub-command> ...")
3960 {
3961 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
3962 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
3963 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
3964 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
3965 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
3966 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
3967 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003968 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00003969
3970 }
3971 virtual
3972 ~CommandObjectTargetModules()
3973 {
3974 }
3975
3976private:
3977 //------------------------------------------------------------------
3978 // For CommandObjectTargetModules only
3979 //------------------------------------------------------------------
3980 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
3981};
3982
3983
Greg Clayton3508c382012-02-24 01:59:29 +00003984
Jim Inghamda26bd22012-06-08 21:56:10 +00003985class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00003986{
3987public:
3988 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003989 CommandObjectParsed (interpreter,
3990 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00003991 "Add a debug symbol file to one of the target's current modules by specifying a path to a debug symbols file, or using the options to specify a module to download symbols for.",
3992 "target symbols add [<symfile>]"),
3993 m_option_group (interpreter),
3994 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
3995 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
3996
Greg Clayton3508c382012-02-24 01:59:29 +00003997 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00003998 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3999 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4000 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4001 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004002 }
4003
4004 virtual
4005 ~CommandObjectTargetSymbolsAdd ()
4006 {
4007 }
4008
Jim Inghamda26bd22012-06-08 21:56:10 +00004009 int
4010 HandleArgumentCompletion (Args &input,
4011 int &cursor_index,
4012 int &cursor_char_position,
4013 OptionElementVector &opt_element_vector,
4014 int match_start_point,
4015 int max_return_elements,
4016 bool &word_complete,
4017 StringList &matches)
4018 {
4019 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4020 completion_str.erase (cursor_char_position);
4021
4022 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4023 CommandCompletions::eDiskFileCompletion,
4024 completion_str.c_str(),
4025 match_start_point,
4026 max_return_elements,
4027 NULL,
4028 word_complete,
4029 matches);
4030 return matches.GetSize();
4031 }
4032
Greg Clayton437b5bc2012-09-27 22:26:11 +00004033 virtual Options *
4034 GetOptions ()
4035 {
4036 return &m_option_group;
4037 }
4038
4039
Jim Inghamda26bd22012-06-08 21:56:10 +00004040protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004041
4042 bool
4043 AddModuleSymbols (Target *target,
4044 const FileSpec &symfile_spec,
4045 bool &flush,
4046 CommandReturnObject &result)
4047 {
4048 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
4049 const UUID &symfile_uuid = symfile_module_sp->GetUUID();
4050 StreamString ss_symfile_uuid;
4051 symfile_uuid.Dump(&ss_symfile_uuid);
4052
4053 if (symfile_module_sp)
4054 {
4055 char symfile_path[PATH_MAX];
4056 symfile_spec.GetPath (symfile_path, sizeof(symfile_path));
4057 // We now have a module that represents a symbol file
4058 // that can be used for a module that might exist in the
4059 // current target, so we need to find that module in the
4060 // target
4061
4062 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_uuid));
4063 if (old_module_sp)
4064 {
4065 // The module has not yet created its symbol vendor, we can just
4066 // give the existing target module the symfile path to use for
4067 // when it decides to create it!
4068 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
4069
4070 // Provide feedback that the symfile has been successfully added.
4071 const FileSpec &module_fs = old_module_sp->GetFileSpec();
4072 result.AppendMessageWithFormat("symbol file '%s' with UUID %s has been successfully added to the '%s/%s' module\n",
4073 symfile_path, ss_symfile_uuid.GetData(),
4074 module_fs.GetDirectory().AsCString(), module_fs.GetFilename().AsCString());
4075
4076 // Let clients know something changed in the module
4077 // if it is currently loaded
4078 ModuleList module_list;
4079 module_list.Append (old_module_sp);
4080 target->ModulesDidLoad (module_list);
4081 flush = true;
4082 }
4083 else
4084 {
4085 result.AppendErrorWithFormat ("symbol file '%s' with UUID %s does not match any existing module%s\n",
4086 symfile_path, ss_symfile_uuid.GetData(),
4087 (symfile_spec.GetFileType() != FileSpec::eFileTypeRegular)
4088 ? "\n please specify the full path to the symbol file"
4089 : "");
4090 return false;
4091 }
4092 }
4093 else
4094 {
4095 result.AppendError ("one or more executable image paths must be specified");
4096 result.SetStatus (eReturnStatusFailed);
4097 return false;
4098 }
4099 result.SetStatus (eReturnStatusSuccessFinishResult);
4100 return true;
4101 }
4102
Greg Clayton3508c382012-02-24 01:59:29 +00004103 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004104 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004105 CommandReturnObject &result)
4106 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004107 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
4108 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004109 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004110 if (target == NULL)
4111 {
4112 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Greg Clayton3508c382012-02-24 01:59:29 +00004113 }
4114 else
4115 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004116 bool flush = false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004117 ModuleSpec sym_spec;
4118 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4119 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4120 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
4121
Greg Clayton3508c382012-02-24 01:59:29 +00004122 const size_t argc = args.GetArgumentCount();
4123 if (argc == 0)
4124 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004125 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004126 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004127 bool success = false;
4128 bool error_set = false;
4129 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004130 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004131 Process *process = exe_ctx.GetProcessPtr();
4132 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004133 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004134 const StateType process_state = process->GetState();
4135 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004136 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004137 StackFrame *frame = exe_ctx.GetFramePtr();
4138 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004139 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004140 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4141 if (frame_module_sp)
4142 {
4143 if (frame_module_sp->GetPlatformFileSpec().Exists())
4144 {
4145 sym_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4146 sym_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
4147 }
4148 sym_spec.GetUUID() = frame_module_sp->GetUUID();
4149 success = sym_spec.GetUUID().IsValid() || sym_spec.GetFileSpec();
4150 }
4151 else
4152 {
4153 result.AppendError ("frame has no module");
4154 error_set = true;
4155 }
Greg Clayton3508c382012-02-24 01:59:29 +00004156 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004157 else
4158 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004159 result.AppendError ("invalid current frame");
4160 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004161 }
Greg Clayton3508c382012-02-24 01:59:29 +00004162 }
4163 else
4164 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004165 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
4166 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004167 }
Greg Clayton3508c382012-02-24 01:59:29 +00004168 }
4169 else
4170 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004171 result.AppendError ("a process must exist in order to use the --frame option");
4172 error_set = true;
4173 }
4174 }
4175 else
4176 {
4177 if (uuid_option_set)
4178 {
4179 sym_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4180 success |= sym_spec.GetUUID().IsValid();
4181 }
4182 else if (file_option_set)
4183 {
4184 sym_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4185 ModuleSP module_sp (target->GetImages().FindFirstModule(sym_spec));
4186 if (module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004187 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004188 sym_spec.GetFileSpec() = module_sp->GetFileSpec();
4189 sym_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4190 sym_spec.GetUUID() = module_sp->GetUUID();
4191 sym_spec.GetArchitecture() = module_sp->GetArchitecture();
Greg Clayton3508c382012-02-24 01:59:29 +00004192 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004193 else
4194 {
4195 sym_spec.GetArchitecture() = target->GetArchitecture();
4196 }
4197 success |= sym_spec.GetFileSpec().Exists();
4198 }
4199 }
4200
4201 if (success)
4202 {
4203 if (Symbols::DownloadObjectAndSymbolFile (sym_spec))
4204 {
4205 if (sym_spec.GetSymbolFileSpec())
4206 success = AddModuleSymbols (target, sym_spec.GetSymbolFileSpec(), flush, result);
4207 }
4208 }
4209
4210 if (!success && !error_set)
4211 {
4212 StreamString error_strm;
4213 if (uuid_option_set)
4214 {
4215 error_strm.PutCString("unable to find debug symbols for UUID ");
4216 sym_spec.GetUUID().Dump (&error_strm);
4217 }
4218 else if (file_option_set)
4219 {
4220 error_strm.PutCString("unable to find debug symbols for the executable file ");
4221 error_strm << sym_spec.GetFileSpec();
4222 }
4223 else if (frame_option_set)
4224 {
4225 error_strm.PutCString("unable to find debug symbols for the current frame");
4226 }
4227 result.AppendError (error_strm.GetData());
4228 }
4229 }
4230 else
4231 {
4232 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4233 }
4234 }
4235 else
4236 {
4237 if (uuid_option_set)
4238 {
4239 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4240 }
4241 else if (file_option_set)
4242 {
4243 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4244 }
4245 else if (frame_option_set)
4246 {
4247 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4248 }
4249 else
4250 {
4251 PlatformSP platform_sp (target->GetPlatform());
4252
4253 for (size_t i=0; i<argc; ++i)
4254 {
4255 const char *symfile_path = args.GetArgumentAtIndex(i);
4256 if (symfile_path)
4257 {
4258 FileSpec symfile_spec;
4259 sym_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4260 if (platform_sp)
4261 platform_sp->ResolveSymbolFile(*target, sym_spec, symfile_spec);
4262 else
4263 symfile_spec.SetFile(symfile_path, true);
4264
4265 ArchSpec arch;
4266 bool symfile_exists = symfile_spec.Exists();
4267
4268 if (symfile_exists)
4269 {
4270 if (!AddModuleSymbols (target, symfile_spec, flush, result))
4271 break;
4272 }
4273 else
4274 {
4275 char resolved_symfile_path[PATH_MAX];
4276 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4277 {
4278 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4279 {
4280 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4281 break;
4282 }
4283 }
4284 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4285 break;
4286 }
Greg Clayton3508c382012-02-24 01:59:29 +00004287 }
4288 }
4289 }
4290 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004291
4292 if (flush)
4293 {
4294 Process *process = exe_ctx.GetProcessPtr();
4295 if (process)
4296 process->Flush();
4297 }
Greg Clayton3508c382012-02-24 01:59:29 +00004298 }
4299 return result.Succeeded();
4300 }
4301
Greg Clayton437b5bc2012-09-27 22:26:11 +00004302 OptionGroupOptions m_option_group;
4303 OptionGroupUUID m_uuid_option_group;
4304 OptionGroupFile m_file_option;
4305 OptionGroupBoolean m_current_frame_option;
4306
4307
Greg Clayton3508c382012-02-24 01:59:29 +00004308};
4309
4310
4311#pragma mark CommandObjectTargetSymbols
4312
4313//-------------------------------------------------------------------------
4314// CommandObjectTargetSymbols
4315//-------------------------------------------------------------------------
4316
4317class CommandObjectTargetSymbols : public CommandObjectMultiword
4318{
4319public:
4320 //------------------------------------------------------------------
4321 // Constructors and Destructors
4322 //------------------------------------------------------------------
4323 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4324 CommandObjectMultiword (interpreter,
4325 "target symbols",
4326 "A set of commands for adding and managing debug symbol files.",
4327 "target symbols <sub-command> ...")
4328 {
4329 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4330
4331 }
4332 virtual
4333 ~CommandObjectTargetSymbols()
4334 {
4335 }
4336
4337private:
4338 //------------------------------------------------------------------
4339 // For CommandObjectTargetModules only
4340 //------------------------------------------------------------------
4341 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4342};
4343
4344
Jim Inghamd60d94a2011-03-11 03:53:59 +00004345#pragma mark CommandObjectTargetStopHookAdd
4346
4347//-------------------------------------------------------------------------
4348// CommandObjectTargetStopHookAdd
4349//-------------------------------------------------------------------------
4350
Jim Inghamda26bd22012-06-08 21:56:10 +00004351class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004352{
4353public:
4354
4355 class CommandOptions : public Options
4356 {
4357 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004358 CommandOptions (CommandInterpreter &interpreter) :
4359 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004360 m_line_start(0),
4361 m_line_end (UINT_MAX),
4362 m_func_name_type_mask (eFunctionNameTypeAuto),
4363 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004364 m_thread_specified (false),
4365 m_use_one_liner (false),
4366 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004367 {
4368 }
4369
4370 ~CommandOptions () {}
4371
Greg Claytonb3448432011-03-24 21:19:54 +00004372 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004373 GetDefinitions ()
4374 {
4375 return g_option_table;
4376 }
4377
4378 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004379 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004380 {
4381 Error error;
4382 char short_option = (char) m_getopt_table[option_idx].val;
4383 bool success;
4384
4385 switch (short_option)
4386 {
4387 case 'c':
4388 m_class_name = option_arg;
4389 m_sym_ctx_specified = true;
4390 break;
4391
4392 case 'e':
4393 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4394 if (!success)
4395 {
Greg Clayton9c236732011-10-26 00:56:27 +00004396 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004397 break;
4398 }
4399 m_sym_ctx_specified = true;
4400 break;
4401
4402 case 'l':
4403 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4404 if (!success)
4405 {
Greg Clayton9c236732011-10-26 00:56:27 +00004406 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004407 break;
4408 }
4409 m_sym_ctx_specified = true;
4410 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004411
4412 case 'i':
4413 m_no_inlines = true;
4414 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004415
4416 case 'n':
4417 m_function_name = option_arg;
4418 m_func_name_type_mask |= eFunctionNameTypeAuto;
4419 m_sym_ctx_specified = true;
4420 break;
4421
4422 case 'f':
4423 m_file_name = option_arg;
4424 m_sym_ctx_specified = true;
4425 break;
4426 case 's':
4427 m_module_name = option_arg;
4428 m_sym_ctx_specified = true;
4429 break;
4430 case 't' :
4431 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004432 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004433 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004434 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004435 m_thread_specified = true;
4436 }
4437 break;
4438 case 'T':
4439 m_thread_name = option_arg;
4440 m_thread_specified = true;
4441 break;
4442 case 'q':
4443 m_queue_name = option_arg;
4444 m_thread_specified = true;
4445 break;
4446 case 'x':
4447 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004448 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004449 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004450 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004451 m_thread_specified = true;
4452 }
4453 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004454 case 'o':
4455 m_use_one_liner = true;
4456 m_one_liner = option_arg;
4457 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004458 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004459 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004460 break;
4461 }
4462 return error;
4463 }
4464
4465 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004466 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004467 {
4468 m_class_name.clear();
4469 m_function_name.clear();
4470 m_line_start = 0;
4471 m_line_end = UINT_MAX;
4472 m_file_name.clear();
4473 m_module_name.clear();
4474 m_func_name_type_mask = eFunctionNameTypeAuto;
4475 m_thread_id = LLDB_INVALID_THREAD_ID;
4476 m_thread_index = UINT32_MAX;
4477 m_thread_name.clear();
4478 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004479
4480 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004481 m_sym_ctx_specified = false;
4482 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004483
4484 m_use_one_liner = false;
4485 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004486 }
4487
4488
Greg Claytonb3448432011-03-24 21:19:54 +00004489 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004490
4491 std::string m_class_name;
4492 std::string m_function_name;
4493 uint32_t m_line_start;
4494 uint32_t m_line_end;
4495 std::string m_file_name;
4496 std::string m_module_name;
4497 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4498 lldb::tid_t m_thread_id;
4499 uint32_t m_thread_index;
4500 std::string m_thread_name;
4501 std::string m_queue_name;
4502 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004503 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004504 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004505 // Instance variables to hold the values for one_liner options.
4506 bool m_use_one_liner;
4507 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004508 };
4509
4510 Options *
4511 GetOptions ()
4512 {
4513 return &m_options;
4514 }
4515
4516 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004517 CommandObjectParsed (interpreter,
4518 "target stop-hook add ",
4519 "Add a hook to be executed when the target stops.",
4520 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004521 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004522 {
4523 }
4524
4525 ~CommandObjectTargetStopHookAdd ()
4526 {
4527 }
4528
4529 static size_t
4530 ReadCommandsCallbackFunction (void *baton,
4531 InputReader &reader,
4532 lldb::InputReaderAction notification,
4533 const char *bytes,
4534 size_t bytes_len)
4535 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004536 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004537 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004538 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004539 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004540
4541 switch (notification)
4542 {
4543 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004544 if (!batch_mode)
4545 {
4546 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4547 if (reader.GetPrompt())
4548 out_stream->Printf ("%s", reader.GetPrompt());
4549 out_stream->Flush();
4550 }
Jim Inghame15511a2011-05-05 01:03:36 +00004551 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004552 break;
4553
4554 case eInputReaderDeactivate:
4555 break;
4556
4557 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004558 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004559 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004560 out_stream->Printf ("%s", reader.GetPrompt());
4561 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004562 }
Jim Inghame15511a2011-05-05 01:03:36 +00004563 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004564 break;
4565
Caroline Tice4a348082011-05-02 20:41:46 +00004566 case eInputReaderAsynchronousOutputWritten:
4567 break;
4568
Jim Inghamd60d94a2011-03-11 03:53:59 +00004569 case eInputReaderGotToken:
4570 if (bytes && bytes_len && baton)
4571 {
4572 StringList *commands = new_stop_hook->GetCommandPointer();
4573 if (commands)
4574 {
4575 commands->AppendString (bytes, bytes_len);
4576 }
4577 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004578 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004579 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004580 out_stream->Printf ("%s", reader.GetPrompt());
4581 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004582 }
4583 break;
4584
4585 case eInputReaderInterrupt:
4586 {
4587 // Finish, and cancel the stop hook.
4588 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004589 if (!batch_mode)
4590 {
4591 out_stream->Printf ("Stop hook cancelled.\n");
4592 out_stream->Flush();
4593 }
4594
Jim Inghamd60d94a2011-03-11 03:53:59 +00004595 reader.SetIsDone (true);
4596 }
Jim Inghame15511a2011-05-05 01:03:36 +00004597 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004598 break;
4599
4600 case eInputReaderEndOfFile:
4601 reader.SetIsDone (true);
4602 break;
4603
4604 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004605 if (!got_interrupted && !batch_mode)
4606 {
Greg Clayton444e35b2011-10-19 18:09:39 +00004607 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004608 out_stream->Flush();
4609 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004610 break;
4611 }
4612
4613 return bytes_len;
4614 }
4615
Jim Inghamda26bd22012-06-08 21:56:10 +00004616protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004617 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004618 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004619 {
4620 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4621 if (target)
4622 {
4623 Target::StopHookSP new_hook_sp;
4624 target->AddStopHook (new_hook_sp);
4625
4626 // First step, make the specifier.
4627 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4628 if (m_options.m_sym_ctx_specified)
4629 {
4630 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4631
4632 if (!m_options.m_module_name.empty())
4633 {
4634 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4635 }
4636
4637 if (!m_options.m_class_name.empty())
4638 {
4639 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4640 }
4641
4642 if (!m_options.m_file_name.empty())
4643 {
4644 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4645 }
4646
4647 if (m_options.m_line_start != 0)
4648 {
4649 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4650 }
4651
4652 if (m_options.m_line_end != UINT_MAX)
4653 {
4654 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4655 }
4656
4657 if (!m_options.m_function_name.empty())
4658 {
4659 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4660 }
4661 }
4662
4663 if (specifier_ap.get())
4664 new_hook_sp->SetSpecifier (specifier_ap.release());
4665
4666 // Next see if any of the thread options have been entered:
4667
4668 if (m_options.m_thread_specified)
4669 {
4670 ThreadSpec *thread_spec = new ThreadSpec();
4671
4672 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4673 {
4674 thread_spec->SetTID (m_options.m_thread_id);
4675 }
4676
4677 if (m_options.m_thread_index != UINT32_MAX)
4678 thread_spec->SetIndex (m_options.m_thread_index);
4679
4680 if (!m_options.m_thread_name.empty())
4681 thread_spec->SetName (m_options.m_thread_name.c_str());
4682
4683 if (!m_options.m_queue_name.empty())
4684 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4685
4686 new_hook_sp->SetThreadSpecifier (thread_spec);
4687
4688 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004689 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004690 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004691 // Use one-liner.
4692 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Greg Clayton444e35b2011-10-19 18:09:39 +00004693 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004694 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004695 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004696 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004697 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4698 // the new stop hook's command string.
4699 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4700 if (!reader_sp)
4701 {
4702 result.AppendError("out of memory\n");
4703 result.SetStatus (eReturnStatusFailed);
4704 target->RemoveStopHookByID (new_hook_sp->GetID());
4705 return false;
4706 }
4707
4708 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4709 new_hook_sp.get(), // baton
4710 eInputReaderGranularityLine, // token size, to pass to callback function
4711 "DONE", // end token
4712 "> ", // prompt
4713 true)); // echo input
4714 if (!err.Success())
4715 {
4716 result.AppendError (err.AsCString());
4717 result.SetStatus (eReturnStatusFailed);
4718 target->RemoveStopHookByID (new_hook_sp->GetID());
4719 return false;
4720 }
4721 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004722 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004723 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4724 }
4725 else
4726 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004727 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004728 result.SetStatus (eReturnStatusFailed);
4729 }
4730
4731 return result.Succeeded();
4732 }
4733private:
4734 CommandOptions m_options;
4735};
4736
Greg Claytonb3448432011-03-24 21:19:54 +00004737OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004738CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4739{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004740 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00004741 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004742 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4743 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004744 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004745 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004746 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004747 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004748 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004749 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004750 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004751 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4752 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4753 "Specify the source file within which the stop-hook is to be run." },
4754 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4755 "Set the start of the line range for which the stop-hook is to be run."},
4756 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4757 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004758 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004759 "Specify the class within which the stop-hook is to be run." },
4760 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4761 "Set the function name within which the stop hook will be run." },
4762 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4763};
4764
4765#pragma mark CommandObjectTargetStopHookDelete
4766
4767//-------------------------------------------------------------------------
4768// CommandObjectTargetStopHookDelete
4769//-------------------------------------------------------------------------
4770
Jim Inghamda26bd22012-06-08 21:56:10 +00004771class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004772{
4773public:
4774
4775 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004776 CommandObjectParsed (interpreter,
4777 "target stop-hook delete",
4778 "Delete a stop-hook.",
4779 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004780 {
4781 }
4782
4783 ~CommandObjectTargetStopHookDelete ()
4784 {
4785 }
4786
Jim Inghamda26bd22012-06-08 21:56:10 +00004787protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004788 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004789 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004790 {
4791 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4792 if (target)
4793 {
4794 // FIXME: see if we can use the breakpoint id style parser?
4795 size_t num_args = command.GetArgumentCount();
4796 if (num_args == 0)
4797 {
4798 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4799 {
4800 result.SetStatus (eReturnStatusFailed);
4801 return false;
4802 }
4803 else
4804 {
4805 target->RemoveAllStopHooks();
4806 }
4807 }
4808 else
4809 {
4810 bool success;
4811 for (size_t i = 0; i < num_args; i++)
4812 {
4813 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4814 if (!success)
4815 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004816 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004817 result.SetStatus(eReturnStatusFailed);
4818 return false;
4819 }
4820 success = target->RemoveStopHookByID (user_id);
4821 if (!success)
4822 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004823 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004824 result.SetStatus(eReturnStatusFailed);
4825 return false;
4826 }
4827 }
4828 }
4829 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4830 }
4831 else
4832 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004833 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004834 result.SetStatus (eReturnStatusFailed);
4835 }
4836
4837 return result.Succeeded();
4838 }
4839};
4840#pragma mark CommandObjectTargetStopHookEnableDisable
4841
4842//-------------------------------------------------------------------------
4843// CommandObjectTargetStopHookEnableDisable
4844//-------------------------------------------------------------------------
4845
Jim Inghamda26bd22012-06-08 21:56:10 +00004846class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004847{
4848public:
4849
4850 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004851 CommandObjectParsed (interpreter,
4852 name,
4853 help,
4854 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004855 m_enable (enable)
4856 {
4857 }
4858
4859 ~CommandObjectTargetStopHookEnableDisable ()
4860 {
4861 }
4862
Jim Inghamda26bd22012-06-08 21:56:10 +00004863protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004864 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004865 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004866 {
4867 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4868 if (target)
4869 {
4870 // FIXME: see if we can use the breakpoint id style parser?
4871 size_t num_args = command.GetArgumentCount();
4872 bool success;
4873
4874 if (num_args == 0)
4875 {
4876 target->SetAllStopHooksActiveState (m_enable);
4877 }
4878 else
4879 {
4880 for (size_t i = 0; i < num_args; i++)
4881 {
4882 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4883 if (!success)
4884 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004885 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004886 result.SetStatus(eReturnStatusFailed);
4887 return false;
4888 }
4889 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4890 if (!success)
4891 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004892 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004893 result.SetStatus(eReturnStatusFailed);
4894 return false;
4895 }
4896 }
4897 }
4898 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4899 }
4900 else
4901 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004902 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004903 result.SetStatus (eReturnStatusFailed);
4904 }
4905 return result.Succeeded();
4906 }
4907private:
4908 bool m_enable;
4909};
4910
4911#pragma mark CommandObjectTargetStopHookList
4912
4913//-------------------------------------------------------------------------
4914// CommandObjectTargetStopHookList
4915//-------------------------------------------------------------------------
4916
Jim Inghamda26bd22012-06-08 21:56:10 +00004917class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004918{
4919public:
4920
4921 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004922 CommandObjectParsed (interpreter,
4923 "target stop-hook list",
4924 "List all stop-hooks.",
4925 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004926 {
4927 }
4928
4929 ~CommandObjectTargetStopHookList ()
4930 {
4931 }
4932
Jim Inghamda26bd22012-06-08 21:56:10 +00004933protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004934 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004935 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004936 {
4937 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00004938 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004939 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004940 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004941 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00004942 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004943 }
4944
4945 size_t num_hooks = target->GetNumStopHooks ();
4946 if (num_hooks == 0)
4947 {
4948 result.GetOutputStream().PutCString ("No stop hooks.\n");
4949 }
4950 else
4951 {
4952 for (size_t i = 0; i < num_hooks; i++)
4953 {
4954 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
4955 if (i > 0)
4956 result.GetOutputStream().PutCString ("\n");
4957 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
4958 }
4959 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00004960 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004961 return result.Succeeded();
4962 }
4963};
4964
4965#pragma mark CommandObjectMultiwordTargetStopHooks
4966//-------------------------------------------------------------------------
4967// CommandObjectMultiwordTargetStopHooks
4968//-------------------------------------------------------------------------
4969
4970class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
4971{
4972public:
4973
4974 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
4975 CommandObjectMultiword (interpreter,
4976 "target stop-hook",
4977 "A set of commands for operating on debugger target stop-hooks.",
4978 "target stop-hook <subcommand> [<subcommand-options>]")
4979 {
4980 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
4981 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
4982 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4983 false,
4984 "target stop-hook disable [<id>]",
4985 "Disable a stop-hook.",
4986 "target stop-hook disable")));
4987 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
4988 true,
4989 "target stop-hook enable [<id>]",
4990 "Enable a stop-hook.",
4991 "target stop-hook enable")));
4992 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
4993 }
4994
4995 ~CommandObjectMultiwordTargetStopHooks()
4996 {
4997 }
4998};
4999
5000
Chris Lattner24943d22010-06-08 16:52:24 +00005001
5002#pragma mark CommandObjectMultiwordTarget
5003
5004//-------------------------------------------------------------------------
5005// CommandObjectMultiwordTarget
5006//-------------------------------------------------------------------------
5007
Greg Clayton63094e02010-06-23 01:19:29 +00005008CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005009 CommandObjectMultiword (interpreter,
5010 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005011 "A set of commands for operating on debugger targets.",
5012 "target <subcommand> [<subcommand-options>]")
5013{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005014
5015 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005016 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005017 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5018 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005019 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005020 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005021 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005022 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005023}
5024
5025CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5026{
5027}
5028
Greg Claytonabe0fed2011-04-18 08:33:37 +00005029