blob: 829283dee9f141a51f9318ce4ca40168c9b29c4e [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)
Daniel Malea5f35a4b2012-11-29 21:49:15 +000094 strm.Printf ("%spid=%" PRIu64, 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
Enrico Granata146d9522012-11-08 02:22:02 +00002258 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002259 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
Enrico Granata146d9522012-11-08 02:22:02 +00002371 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002372 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.",
Greg Clayton1649a722012-11-29 22:16:27 +00002445 "target modules add [<module>]"),
2446 m_option_group (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00002447 {
Greg Clayton1649a722012-11-29 22:16:27 +00002448 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2449 m_option_group.Finalize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002450 }
2451
2452 virtual
2453 ~CommandObjectTargetModulesAdd ()
2454 {
2455 }
Greg Clayton1649a722012-11-29 22:16:27 +00002456
2457 virtual Options *
2458 GetOptions ()
2459 {
2460 return &m_option_group;
2461 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002462
Jim Inghamda26bd22012-06-08 21:56:10 +00002463 int
2464 HandleArgumentCompletion (Args &input,
2465 int &cursor_index,
2466 int &cursor_char_position,
2467 OptionElementVector &opt_element_vector,
2468 int match_start_point,
2469 int max_return_elements,
2470 bool &word_complete,
2471 StringList &matches)
2472 {
2473 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2474 completion_str.erase (cursor_char_position);
2475
2476 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2477 CommandCompletions::eDiskFileCompletion,
2478 completion_str.c_str(),
2479 match_start_point,
2480 max_return_elements,
2481 NULL,
2482 word_complete,
2483 matches);
2484 return matches.GetSize();
2485 }
2486
2487protected:
Greg Clayton1649a722012-11-29 22:16:27 +00002488
2489 OptionGroupOptions m_option_group;
2490 OptionGroupUUID m_uuid_option_group;
2491
2492
Greg Claytone1f50b92011-05-03 22:09:39 +00002493 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002494 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002495 CommandReturnObject &result)
2496 {
2497 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2498 if (target == NULL)
2499 {
2500 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2501 result.SetStatus (eReturnStatusFailed);
2502 return false;
2503 }
2504 else
2505 {
2506 const size_t argc = args.GetArgumentCount();
2507 if (argc == 0)
2508 {
Greg Clayton1649a722012-11-29 22:16:27 +00002509 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2510 {
2511 // We are given a UUID only, go locate the file
2512 ModuleSpec module_spec;
2513 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
2514 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2515 {
2516 ModuleSP module_sp (target->GetSharedModule (module_spec));
2517 if (module_sp)
2518 {
2519 result.SetStatus (eReturnStatusSuccessFinishResult);
2520 return true;
2521 }
2522 else
2523 {
2524 StreamString strm;
2525 module_spec.GetUUID().Dump (&strm);
2526 if (module_spec.GetFileSpec())
2527 {
2528 if (module_spec.GetSymbolFileSpec())
2529 {
2530 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s and symbol file %s/%s",
2531 strm.GetString().c_str(),
2532 module_spec.GetFileSpec().GetDirectory().GetCString(),
2533 module_spec.GetFileSpec().GetFilename().GetCString(),
2534 module_spec.GetSymbolFileSpec().GetDirectory().GetCString(),
2535 module_spec.GetSymbolFileSpec().GetFilename().GetCString());
2536 }
2537 else
2538 {
2539 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s",
2540 strm.GetString().c_str(),
2541 module_spec.GetFileSpec().GetDirectory().GetCString(),
2542 module_spec.GetFileSpec().GetFilename().GetCString());
2543 }
2544 }
2545 else
2546 {
2547 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2548 strm.GetString().c_str());
2549 }
2550 result.SetStatus (eReturnStatusFailed);
2551 return false;
2552 }
2553 }
2554 else
2555 {
2556 StreamString strm;
2557 module_spec.GetUUID().Dump (&strm);
2558 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2559 result.SetStatus (eReturnStatusFailed);
2560 return false;
2561 }
2562 }
2563 else
2564 {
2565 result.AppendError ("one or more executable image paths must be specified");
2566 result.SetStatus (eReturnStatusFailed);
2567 return false;
2568 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002569 }
2570 else
2571 {
2572 for (size_t i=0; i<argc; ++i)
2573 {
2574 const char *path = args.GetArgumentAtIndex(i);
2575 if (path)
2576 {
2577 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002578 if (file_spec.Exists())
2579 {
Greg Clayton444fe992012-02-26 05:51:37 +00002580 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002581 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2582 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
2583
2584 Error error;
2585 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002586 if (!module_sp)
2587 {
Greg Clayton1649a722012-11-29 22:16:27 +00002588 const char *error_cstr = error.AsCString();
2589 if (error_cstr)
2590 result.AppendError (error_cstr);
2591 else
2592 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002593 result.SetStatus (eReturnStatusFailed);
2594 return false;
2595 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002596 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002597 }
2598 else
2599 {
2600 char resolved_path[PATH_MAX];
2601 result.SetStatus (eReturnStatusFailed);
2602 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2603 {
2604 if (strcmp (resolved_path, path) != 0)
2605 {
2606 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2607 break;
2608 }
2609 }
2610 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2611 break;
2612 }
2613 }
2614 }
2615 }
2616 }
2617 return result.Succeeded();
2618 }
2619
Greg Claytone1f50b92011-05-03 22:09:39 +00002620};
2621
2622class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2623{
2624public:
2625 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2626 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2627 "target modules load",
2628 "Set the load addresses for one or more sections in a target module.",
2629 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2630 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002631 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 +00002632 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)
2633 {
2634 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2635 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2636 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2637 m_option_group.Finalize();
2638 }
2639
2640 virtual
2641 ~CommandObjectTargetModulesLoad ()
2642 {
2643 }
2644
Jim Inghamda26bd22012-06-08 21:56:10 +00002645 virtual Options *
2646 GetOptions ()
2647 {
2648 return &m_option_group;
2649 }
2650
2651protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002652 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002653 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002654 CommandReturnObject &result)
2655 {
2656 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2657 if (target == NULL)
2658 {
2659 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2660 result.SetStatus (eReturnStatusFailed);
2661 return false;
2662 }
2663 else
2664 {
2665 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002666 ModuleSpec module_spec;
2667 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002668 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002669 {
2670 search_using_module_spec = true;
2671 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2672 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002673
2674 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002675 {
2676 search_using_module_spec = true;
2677 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2678 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002679
Greg Clayton444fe992012-02-26 05:51:37 +00002680 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002681 {
2682
2683 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002684 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002685
2686 char path[PATH_MAX];
2687 if (num_matches == 1)
2688 {
2689 Module *module = matching_modules.GetModulePointerAtIndex(0);
2690 if (module)
2691 {
2692 ObjectFile *objfile = module->GetObjectFile();
2693 if (objfile)
2694 {
2695 SectionList *section_list = objfile->GetSectionList();
2696 if (section_list)
2697 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002698 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002699 if (argc == 0)
2700 {
2701 if (m_slide_option.GetOptionValue().OptionWasSet())
2702 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002703 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2704 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002705 }
2706 else
2707 {
2708 result.AppendError ("one or more section name + load address pair must be specified");
2709 result.SetStatus (eReturnStatusFailed);
2710 return false;
2711 }
2712 }
2713 else
2714 {
2715 if (m_slide_option.GetOptionValue().OptionWasSet())
2716 {
2717 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2718 result.SetStatus (eReturnStatusFailed);
2719 return false;
2720 }
2721
2722 for (size_t i=0; i<argc; i += 2)
2723 {
2724 const char *sect_name = args.GetArgumentAtIndex(i);
2725 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2726 if (sect_name && load_addr_cstr)
2727 {
2728 ConstString const_sect_name(sect_name);
2729 bool success = false;
2730 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2731 if (success)
2732 {
2733 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2734 if (section_sp)
2735 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002736 if (section_sp->IsThreadSpecific())
2737 {
2738 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2739 result.SetStatus (eReturnStatusFailed);
2740 break;
2741 }
2742 else
2743 {
Greg Clayton545762f2012-07-07 01:24:12 +00002744 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002745 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002746 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002747 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002748 }
2749 else
2750 {
2751 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2752 result.SetStatus (eReturnStatusFailed);
2753 break;
2754 }
2755 }
2756 else
2757 {
2758 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2759 result.SetStatus (eReturnStatusFailed);
2760 break;
2761 }
2762 }
2763 else
2764 {
2765 if (sect_name)
2766 result.AppendError ("section names must be followed by a load address.\n");
2767 else
2768 result.AppendError ("one or more section name + load address pair must be specified.\n");
2769 result.SetStatus (eReturnStatusFailed);
2770 break;
2771 }
2772 }
2773 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002774
2775 if (changed)
2776 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002777 }
2778 else
2779 {
2780 module->GetFileSpec().GetPath (path, sizeof(path));
2781 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2782 result.SetStatus (eReturnStatusFailed);
2783 }
2784 }
2785 else
2786 {
2787 module->GetFileSpec().GetPath (path, sizeof(path));
2788 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2789 result.SetStatus (eReturnStatusFailed);
2790 }
2791 }
2792 else
2793 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002794 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2795 if (module_spec_file)
2796 {
2797 module_spec_file->GetPath (path, sizeof(path));
2798 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2799 }
2800 else
2801 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002802 result.SetStatus (eReturnStatusFailed);
2803 }
2804 }
2805 else
2806 {
2807 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002808
2809 if (module_spec.GetFileSpec())
2810 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002811 else
2812 path[0] = '\0';
2813
Greg Clayton444fe992012-02-26 05:51:37 +00002814 if (module_spec.GetUUIDPtr())
2815 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002816 else
2817 uuid_cstr[0] = '\0';
2818 if (num_matches > 1)
2819 {
2820 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2821 path[0] ? " file=" : "",
2822 path,
2823 uuid_cstr[0] ? " uuid=" : "",
2824 uuid_cstr);
2825 for (size_t i=0; i<num_matches; ++i)
2826 {
2827 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2828 result.AppendMessageWithFormat("%s\n", path);
2829 }
2830 }
2831 else
2832 {
2833 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2834 path[0] ? " file=" : "",
2835 path,
2836 uuid_cstr[0] ? " uuid=" : "",
2837 uuid_cstr);
2838 }
2839 result.SetStatus (eReturnStatusFailed);
2840 }
2841 }
2842 else
2843 {
2844 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2845 result.SetStatus (eReturnStatusFailed);
2846 return false;
2847 }
2848 }
2849 return result.Succeeded();
2850 }
2851
Greg Claytone1f50b92011-05-03 22:09:39 +00002852 OptionGroupOptions m_option_group;
2853 OptionGroupUUID m_uuid_option_group;
2854 OptionGroupFile m_file_option;
2855 OptionGroupUInt64 m_slide_option;
2856};
2857
2858//----------------------------------------------------------------------
2859// List images with associated information
2860//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002861class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002862{
2863public:
2864
2865 class CommandOptions : public Options
2866 {
2867 public:
2868
2869 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002870 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002871 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002872 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002873 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002874 {
2875 }
2876
2877 virtual
2878 ~CommandOptions ()
2879 {
2880 }
2881
2882 virtual Error
2883 SetOptionValue (uint32_t option_idx, const char *option_arg)
2884 {
2885 char short_option = (char) m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00002886 if (short_option == 'g')
2887 {
2888 m_use_global_module_list = true;
2889 }
Jim Ingham6bdea822011-10-24 18:36:33 +00002890 else if (short_option == 'a')
2891 {
2892 bool success;
2893 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success);
2894 if (!success)
2895 {
2896 Error error;
Greg Clayton9c236732011-10-26 00:56:27 +00002897 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg);
Jim Ingham6bdea822011-10-24 18:36:33 +00002898 }
2899 }
Greg Clayton899025f2011-08-09 00:01:09 +00002900 else
2901 {
2902 uint32_t width = 0;
2903 if (option_arg)
2904 width = strtoul (option_arg, NULL, 0);
2905 m_format_array.push_back(std::make_pair(short_option, width));
2906 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002907 Error error;
2908 return error;
2909 }
2910
2911 void
2912 OptionParsingStarting ()
2913 {
2914 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00002915 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00002916 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00002917 }
2918
2919 const OptionDefinition*
2920 GetDefinitions ()
2921 {
2922 return g_option_table;
2923 }
2924
2925 // Options table: Required for subclasses of Options.
2926
2927 static OptionDefinition g_option_table[];
2928
2929 // Instance variables to hold the values for command options.
2930 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
2931 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00002932 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00002933 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00002934 };
2935
2936 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002937 CommandObjectParsed (interpreter,
2938 "target modules list",
2939 "List current executable and dependent shared library images.",
2940 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00002941 m_options (interpreter)
2942 {
2943 }
2944
2945 virtual
2946 ~CommandObjectTargetModulesList ()
2947 {
2948 }
2949
2950 virtual
2951 Options *
2952 GetOptions ()
2953 {
2954 return &m_options;
2955 }
2956
Jim Inghamda26bd22012-06-08 21:56:10 +00002957protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002958 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002959 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002960 CommandReturnObject &result)
2961 {
2962 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00002963 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00002964 // Define a local module list here to ensure it lives longer than any "locker"
2965 // object which might lock its contents below (through the "module_list_ptr"
2966 // variable).
2967 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00002968 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00002969 {
2970 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2971 result.SetStatus (eReturnStatusFailed);
2972 return false;
2973 }
2974 else
2975 {
Greg Clayton153ccd72011-08-10 02:10:13 +00002976 if (target)
2977 {
2978 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2979 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2980 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2981 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002982 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00002983 Stream &strm = result.GetOutputStream();
2984
2985 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
2986 {
2987 if (target)
2988 {
2989 Address module_address;
2990 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
2991 {
Greg Clayton3508c382012-02-24 01:59:29 +00002992 ModuleSP module_sp (module_address.GetModule());
2993 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00002994 {
Greg Clayton3508c382012-02-24 01:59:29 +00002995 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00002996 result.SetStatus (eReturnStatusSuccessFinishResult);
2997 }
2998 else
2999 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003000 result.AppendError ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003001 result.SetStatus (eReturnStatusFailed);
3002 }
3003 }
3004 else
3005 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003006 result.AppendError ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003007 result.SetStatus (eReturnStatusFailed);
3008 }
3009 }
3010 else
3011 {
3012 result.AppendError ("Can only look up modules by address with a valid target.");
3013 result.SetStatus (eReturnStatusFailed);
3014 }
3015 return result.Succeeded();
3016 }
3017
Jim Ingham93367902012-05-30 02:19:25 +00003018 uint32_t num_modules = 0;
3019 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3020 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3021 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003022 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003023 const size_t argc = command.GetArgumentCount();
3024 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003025 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003026 if (use_global_module_list)
3027 {
3028 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3029 num_modules = Module::GetNumberAllocatedModules();
3030 }
3031 else
3032 {
3033 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003034 }
Greg Clayton899025f2011-08-09 00:01:09 +00003035 }
3036 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003037 {
3038 for (size_t i=0; i<argc; ++i)
3039 {
3040 // Dump specified images (by basename or fullpath)
3041 const char *arg_cstr = command.GetArgumentAtIndex(i);
3042 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3043 if (num_matches == 0)
3044 {
3045 if (argc == 1)
3046 {
3047 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3048 result.SetStatus (eReturnStatusFailed);
3049 return false;
3050 }
3051 }
3052 }
3053
Greg Clayton2ad894b2012-05-15 18:43:44 +00003054 module_list_ptr = &module_list;
3055 }
Jim Ingham93367902012-05-30 02:19:25 +00003056
3057 if (module_list_ptr != NULL)
3058 {
3059 locker.Lock(module_list_ptr->GetMutex());
3060 num_modules = module_list_ptr->GetSize();
3061 }
Greg Clayton899025f2011-08-09 00:01:09 +00003062
Greg Claytone1f50b92011-05-03 22:09:39 +00003063 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003064 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003065 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3066 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003067 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003068 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003069 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003070 {
Jim Ingham93367902012-05-30 02:19:25 +00003071 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003072 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003073 }
3074 else
3075 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003076 module = Module::GetAllocatedModuleAtIndex(image_idx);
3077 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003078 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003079
Greg Claytonb5a8f142012-02-05 02:38:54 +00003080 int indent = strm.Printf("[%3u] ", image_idx);
3081 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003082
Greg Claytone1f50b92011-05-03 22:09:39 +00003083 }
3084 result.SetStatus (eReturnStatusSuccessFinishResult);
3085 }
3086 else
3087 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003088 if (argc)
3089 {
3090 if (use_global_module_list)
3091 result.AppendError ("the global module list has no matching modules");
3092 else
3093 result.AppendError ("the target has no matching modules");
3094 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003095 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003096 {
3097 if (use_global_module_list)
3098 result.AppendError ("the global module list is empty");
3099 else
3100 result.AppendError ("the target has no associated executable images");
3101 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003102 result.SetStatus (eReturnStatusFailed);
3103 return false;
3104 }
3105 }
3106 return result.Succeeded();
3107 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003108
3109 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003110 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003111 {
3112
Jim Ingham6f01c932012-10-12 17:34:26 +00003113 if (module == NULL)
3114 {
3115 strm.PutCString("Null module");
3116 return;
3117 }
3118
Jim Ingham6bdea822011-10-24 18:36:33 +00003119 bool dump_object_name = false;
3120 if (m_options.m_format_array.empty())
3121 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003122 m_options.m_format_array.push_back(std::make_pair('u', 0));
3123 m_options.m_format_array.push_back(std::make_pair('h', 0));
3124 m_options.m_format_array.push_back(std::make_pair('f', 0));
3125 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003126 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003127 const size_t num_entries = m_options.m_format_array.size();
3128 bool print_space = false;
3129 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003130 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003131 if (print_space)
3132 strm.PutChar(' ');
3133 print_space = true;
3134 const char format_char = m_options.m_format_array[i].first;
3135 uint32_t width = m_options.m_format_array[i].second;
3136 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003137 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003138 case 'A':
3139 DumpModuleArchitecture (strm, module, false, width);
3140 break;
3141
3142 case 't':
3143 DumpModuleArchitecture (strm, module, true, width);
3144 break;
3145
3146 case 'f':
3147 DumpFullpath (strm, &module->GetFileSpec(), width);
3148 dump_object_name = true;
3149 break;
3150
3151 case 'd':
3152 DumpDirectory (strm, &module->GetFileSpec(), width);
3153 break;
3154
3155 case 'b':
3156 DumpBasename (strm, &module->GetFileSpec(), width);
3157 dump_object_name = true;
3158 break;
3159
3160 case 'h':
3161 case 'o':
3162 // Image header address
3163 {
3164 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003165
Greg Claytonb5a8f142012-02-05 02:38:54 +00003166 ObjectFile *objfile = module->GetObjectFile ();
3167 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003168 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003169 Address header_addr(objfile->GetHeaderAddress());
3170 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003171 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003172 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003173 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003174 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3175 if (header_load_addr == LLDB_INVALID_ADDRESS)
3176 {
3177 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3178 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003179 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003180 {
3181 if (format_char == 'o')
3182 {
3183 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003184 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003185 }
3186 else
3187 {
3188 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003189 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003190 }
3191 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003192 break;
3193 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003194 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3195 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3196 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003197 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003198 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003199 strm.Printf ("%*s", addr_nibble_width + 2, "");
3200 }
3201 break;
3202 case 'r':
3203 {
3204 uint32_t ref_count = 0;
3205 ModuleSP module_sp (module->shared_from_this());
3206 if (module_sp)
3207 {
3208 // Take one away to make sure we don't count our local "module_sp"
3209 ref_count = module_sp.use_count() - 1;
3210 }
3211 if (width)
3212 strm.Printf("{%*u}", width, ref_count);
3213 else
3214 strm.Printf("{%u}", ref_count);
3215 }
3216 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003217
Greg Claytonb5a8f142012-02-05 02:38:54 +00003218 case 's':
3219 case 'S':
3220 {
3221 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3222 if (symbol_vendor)
3223 {
3224 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3225 if (symbol_file)
3226 {
3227 if (format_char == 'S')
3228 {
3229 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3230 // Dump symbol file only if different from module file
3231 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3232 {
3233 print_space = false;
3234 break;
3235 }
3236 // Add a newline and indent past the index
3237 strm.Printf ("\n%*s", indent, "");
3238 }
3239 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3240 dump_object_name = true;
3241 break;
3242 }
3243 }
3244 strm.Printf("%.*s", width, "<NONE>");
3245 }
3246 break;
3247
3248 case 'm':
3249 module->GetModificationTime().Dump(&strm, width);
3250 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003251
Greg Claytonb5a8f142012-02-05 02:38:54 +00003252 case 'p':
3253 strm.Printf("%p", module);
3254 break;
3255
3256 case 'u':
3257 DumpModuleUUID(strm, module);
3258 break;
3259
3260 default:
3261 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003262 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003263
3264 }
3265 if (dump_object_name)
3266 {
3267 const char *object_name = module->GetObjectName().GetCString();
3268 if (object_name)
3269 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003270 }
3271 strm.EOL();
3272 }
3273
Greg Claytone1f50b92011-05-03 22:09:39 +00003274 CommandOptions m_options;
3275};
3276
3277OptionDefinition
3278CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3279{
Jim Ingham6bdea822011-10-24 18:36:33 +00003280 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3281 { 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 +00003282 { 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 +00003283 { 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."},
3284 { 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 +00003285 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3286 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3287 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3288 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3289 { 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 +00003290 { 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 +00003291 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3292 { 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."},
3293 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003294 { 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 +00003295 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3296};
3297
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003298#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003299
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003300//----------------------------------------------------------------------
3301// Lookup unwind information in images
3302//----------------------------------------------------------------------
3303
3304class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3305{
3306public:
3307
3308 enum
3309 {
3310 eLookupTypeInvalid = -1,
3311 eLookupTypeAddress = 0,
3312 eLookupTypeSymbol,
3313 eLookupTypeFunction,
3314 eLookupTypeFunctionOrSymbol,
3315 kNumLookupTypes
3316 };
3317
3318 class CommandOptions : public Options
3319 {
3320 public:
3321
3322 CommandOptions (CommandInterpreter &interpreter) :
3323 Options(interpreter),
3324 m_type(eLookupTypeInvalid),
3325 m_str(),
3326 m_addr(LLDB_INVALID_ADDRESS)
3327 {
3328 }
3329
3330 virtual
3331 ~CommandOptions ()
3332 {
3333 }
3334
3335 virtual Error
3336 SetOptionValue (uint32_t option_idx, const char *option_arg)
3337 {
3338 Error error;
3339
3340 char short_option = (char) m_getopt_table[option_idx].val;
3341
3342 switch (short_option)
3343 {
3344 case 'a':
3345 m_type = eLookupTypeAddress;
3346 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3347 if (m_addr == LLDB_INVALID_ADDRESS)
3348 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3349 break;
3350
3351 case 'n':
3352 m_str = option_arg;
3353 m_type = eLookupTypeFunctionOrSymbol;
3354 break;
3355 }
3356
3357 return error;
3358 }
3359
3360 void
3361 OptionParsingStarting ()
3362 {
3363 m_type = eLookupTypeInvalid;
3364 m_str.clear();
3365 m_addr = LLDB_INVALID_ADDRESS;
3366 }
3367
3368 const OptionDefinition*
3369 GetDefinitions ()
3370 {
3371 return g_option_table;
3372 }
3373
3374 // Options table: Required for subclasses of Options.
3375
3376 static OptionDefinition g_option_table[];
3377
3378 // Instance variables to hold the values for command options.
3379
3380 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3381 std::string m_str; // Holds name lookup
3382 lldb::addr_t m_addr; // Holds the address to lookup
3383 };
3384
3385 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3386 CommandObjectParsed (interpreter,
3387 "target modules show-unwind",
3388 "Show synthesized unwind instructions for a function.",
3389 NULL),
3390 m_options (interpreter)
3391 {
3392 }
3393
3394 virtual
3395 ~CommandObjectTargetModulesShowUnwind ()
3396 {
3397 }
3398
3399 virtual
3400 Options *
3401 GetOptions ()
3402 {
3403 return &m_options;
3404 }
3405
3406protected:
3407 bool
3408 DoExecute (Args& command,
3409 CommandReturnObject &result)
3410 {
3411 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3412 if (!target)
3413 {
3414 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3415 result.SetStatus (eReturnStatusFailed);
3416 return false;
3417 }
3418
3419 ExecutionContext exe_ctx = m_interpreter.GetDebugger().GetSelectedExecutionContext();
3420 Process *process = exe_ctx.GetProcessPtr();
3421 ABI *abi = NULL;
3422 if (process)
3423 abi = process->GetABI().get();
3424
3425 if (process == NULL)
3426 {
3427 result.AppendError ("You must have a process running to use this command.");
3428 result.SetStatus (eReturnStatusFailed);
3429 return false;
3430 }
3431
3432 ThreadList threads(process->GetThreadList());
3433 if (threads.GetSize() == 0)
3434 {
3435 result.AppendError ("The process must be paused to use this command.");
3436 result.SetStatus (eReturnStatusFailed);
3437 return false;
3438 }
3439
3440 ThreadSP thread(threads.GetThreadAtIndex(0));
3441 if (thread.get() == NULL)
3442 {
3443 result.AppendError ("The process must be paused to use this command.");
3444 result.SetStatus (eReturnStatusFailed);
3445 return false;
3446 }
3447
3448 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3449 {
3450 SymbolContextList sc_list;
3451 uint32_t num_matches;
3452 ConstString function_name (m_options.m_str.c_str());
3453 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3454 for (uint32_t idx = 0; idx < num_matches; idx++)
3455 {
3456 SymbolContext sc;
3457 sc_list.GetContextAtIndex(idx, sc);
3458 if (sc.symbol == NULL && sc.function == NULL)
3459 continue;
3460 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3461 continue;
3462 AddressRange range;
3463 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3464 continue;
3465 if (!range.GetBaseAddress().IsValid())
3466 continue;
3467 ConstString funcname(sc.GetFunctionName());
3468 if (funcname.IsEmpty())
3469 continue;
3470 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3471 if (abi)
3472 start_addr = abi->FixCodeAddress(start_addr);
3473
3474 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3475 if (func_unwinders_sp.get() == NULL)
3476 continue;
3477
3478 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3479 if (first_non_prologue_insn.IsValid())
3480 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003481 result.GetOutputStream().Printf("First non-prologue instruction is at address 0x%" PRIx64 " or offset %" PRId64 " into the function.\n", first_non_prologue_insn.GetLoadAddress(target), first_non_prologue_insn.GetLoadAddress(target) - start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003482 result.GetOutputStream().Printf ("\n");
3483 }
3484
3485 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3486 if (non_callsite_unwind_plan.get())
3487 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003488 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003489 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3490 result.GetOutputStream().Printf ("\n");
3491 }
3492
3493 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3494 if (callsite_unwind_plan.get())
3495 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003496 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003497 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3498 result.GetOutputStream().Printf ("\n");
3499 }
3500
3501 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3502 if (arch_default_unwind_plan.get())
3503 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003504 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003505 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3506 result.GetOutputStream().Printf ("\n");
3507 }
3508
3509 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3510 if (fast_unwind_plan.get())
3511 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003512 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003513 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3514 result.GetOutputStream().Printf ("\n");
3515 }
3516
3517
3518 result.GetOutputStream().Printf ("\n");
3519 }
3520 }
3521 return result.Succeeded();
3522 }
3523
3524 CommandOptions m_options;
3525};
3526
3527OptionDefinition
3528CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3529{
3530 { 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."},
3531 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3532};
Greg Claytone1f50b92011-05-03 22:09:39 +00003533
3534//----------------------------------------------------------------------
3535// Lookup information in images
3536//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003537class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003538{
3539public:
3540
3541 enum
3542 {
3543 eLookupTypeInvalid = -1,
3544 eLookupTypeAddress = 0,
3545 eLookupTypeSymbol,
3546 eLookupTypeFileLine, // Line is optional
3547 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003548 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003549 eLookupTypeType,
3550 kNumLookupTypes
3551 };
3552
3553 class CommandOptions : public Options
3554 {
3555 public:
3556
3557 CommandOptions (CommandInterpreter &interpreter) :
3558 Options(interpreter)
3559 {
3560 OptionParsingStarting();
3561 }
3562
3563 virtual
3564 ~CommandOptions ()
3565 {
3566 }
3567
3568 virtual Error
3569 SetOptionValue (uint32_t option_idx, const char *option_arg)
3570 {
3571 Error error;
3572
3573 char short_option = (char) m_getopt_table[option_idx].val;
3574
3575 switch (short_option)
3576 {
3577 case 'a':
3578 m_type = eLookupTypeAddress;
3579 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3580 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003581 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003582 break;
3583
3584 case 'o':
3585 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3586 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003587 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003588 break;
3589
3590 case 's':
3591 m_str = option_arg;
3592 m_type = eLookupTypeSymbol;
3593 break;
3594
3595 case 'f':
3596 m_file.SetFile (option_arg, false);
3597 m_type = eLookupTypeFileLine;
3598 break;
3599
3600 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003601 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003602 break;
3603
3604 case 'l':
3605 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3606 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003607 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003608 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003609 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003610 m_type = eLookupTypeFileLine;
3611 break;
3612
Greg Clayton2ad894b2012-05-15 18:43:44 +00003613 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003614 m_str = option_arg;
3615 m_type = eLookupTypeFunction;
3616 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003617
3618 case 'n':
3619 m_str = option_arg;
3620 m_type = eLookupTypeFunctionOrSymbol;
3621 break;
3622
Greg Claytone1f50b92011-05-03 22:09:39 +00003623 case 't':
3624 m_str = option_arg;
3625 m_type = eLookupTypeType;
3626 break;
3627
3628 case 'v':
3629 m_verbose = 1;
3630 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003631
3632 case 'A':
3633 m_print_all = true;
3634 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003635
3636 case 'r':
3637 m_use_regex = true;
3638 break;
3639 }
3640
3641 return error;
3642 }
3643
3644 void
3645 OptionParsingStarting ()
3646 {
3647 m_type = eLookupTypeInvalid;
3648 m_str.clear();
3649 m_file.Clear();
3650 m_addr = LLDB_INVALID_ADDRESS;
3651 m_offset = 0;
3652 m_line_number = 0;
3653 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003654 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003655 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003656 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003657 }
3658
3659 const OptionDefinition*
3660 GetDefinitions ()
3661 {
3662 return g_option_table;
3663 }
3664
3665 // Options table: Required for subclasses of Options.
3666
3667 static OptionDefinition g_option_table[];
3668 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3669 std::string m_str; // Holds name lookup
3670 FileSpec m_file; // Files for file lookups
3671 lldb::addr_t m_addr; // Holds the address to lookup
3672 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3673 uint32_t m_line_number; // Line number for file+line lookups
3674 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003675 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003676 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003677 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003678
3679 };
3680
3681 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003682 CommandObjectParsed (interpreter,
3683 "target modules lookup",
3684 "Look up information within executable and dependent shared library images.",
3685 NULL),
3686 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003687 {
3688 CommandArgumentEntry arg;
3689 CommandArgumentData file_arg;
3690
3691 // Define the first (and only) variant of this arg.
3692 file_arg.arg_type = eArgTypeFilename;
3693 file_arg.arg_repetition = eArgRepeatStar;
3694
3695 // There is only one variant this argument could be; put it into the argument entry.
3696 arg.push_back (file_arg);
3697
3698 // Push the data for the first argument into the m_arguments vector.
3699 m_arguments.push_back (arg);
3700 }
3701
3702 virtual
3703 ~CommandObjectTargetModulesLookup ()
3704 {
3705 }
3706
3707 virtual Options *
3708 GetOptions ()
3709 {
3710 return &m_options;
3711 }
3712
Sean Callanan56d31ec2012-06-06 20:49:55 +00003713 bool
3714 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3715 {
3716 switch (m_options.m_type)
3717 {
3718 case eLookupTypeAddress:
3719 case eLookupTypeFileLine:
3720 case eLookupTypeFunction:
3721 case eLookupTypeFunctionOrSymbol:
3722 case eLookupTypeSymbol:
3723 default:
3724 return false;
3725 case eLookupTypeType:
3726 break;
3727 }
3728
3729 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3730
3731 StackFrameSP frame = exe_ctx.GetFrameSP();
3732
3733 if (!frame)
3734 return false;
3735
3736 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3737
3738 if (!sym_ctx.module_sp)
3739 return false;
3740
3741 switch (m_options.m_type)
3742 {
3743 default:
3744 return false;
3745 case eLookupTypeType:
3746 if (!m_options.m_str.empty())
3747 {
3748 if (LookupTypeHere (m_interpreter,
3749 result.GetOutputStream(),
3750 sym_ctx,
3751 m_options.m_str.c_str(),
3752 m_options.m_use_regex))
3753 {
3754 result.SetStatus(eReturnStatusSuccessFinishResult);
3755 return true;
3756 }
3757 }
3758 break;
3759 }
3760
3761 return true;
3762 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003763
3764 bool
3765 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3766 {
3767 switch (m_options.m_type)
3768 {
3769 case eLookupTypeAddress:
3770 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3771 {
3772 if (LookupAddressInModule (m_interpreter,
3773 result.GetOutputStream(),
3774 module,
3775 eSymbolContextEverything,
3776 m_options.m_addr,
3777 m_options.m_offset,
3778 m_options.m_verbose))
3779 {
3780 result.SetStatus(eReturnStatusSuccessFinishResult);
3781 return true;
3782 }
3783 }
3784 break;
3785
3786 case eLookupTypeSymbol:
3787 if (!m_options.m_str.empty())
3788 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003789 if (LookupSymbolInModule (m_interpreter,
3790 result.GetOutputStream(),
3791 module,
3792 m_options.m_str.c_str(),
3793 m_options.m_use_regex,
3794 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003795 {
3796 result.SetStatus(eReturnStatusSuccessFinishResult);
3797 return true;
3798 }
3799 }
3800 break;
3801
3802 case eLookupTypeFileLine:
3803 if (m_options.m_file)
3804 {
3805
3806 if (LookupFileAndLineInModule (m_interpreter,
3807 result.GetOutputStream(),
3808 module,
3809 m_options.m_file,
3810 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003811 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003812 m_options.m_verbose))
3813 {
3814 result.SetStatus(eReturnStatusSuccessFinishResult);
3815 return true;
3816 }
3817 }
3818 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003819
3820 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003821 case eLookupTypeFunction:
3822 if (!m_options.m_str.empty())
3823 {
3824 if (LookupFunctionInModule (m_interpreter,
3825 result.GetOutputStream(),
3826 module,
3827 m_options.m_str.c_str(),
3828 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003829 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003830 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003831 m_options.m_verbose))
3832 {
3833 result.SetStatus(eReturnStatusSuccessFinishResult);
3834 return true;
3835 }
3836 }
3837 break;
3838
Greg Clayton2ad894b2012-05-15 18:43:44 +00003839
Greg Claytone1f50b92011-05-03 22:09:39 +00003840 case eLookupTypeType:
3841 if (!m_options.m_str.empty())
3842 {
3843 if (LookupTypeInModule (m_interpreter,
3844 result.GetOutputStream(),
3845 module,
3846 m_options.m_str.c_str(),
3847 m_options.m_use_regex))
3848 {
3849 result.SetStatus(eReturnStatusSuccessFinishResult);
3850 return true;
3851 }
3852 }
3853 break;
3854
3855 default:
3856 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3857 syntax_error = true;
3858 break;
3859 }
3860
3861 result.SetStatus (eReturnStatusFailed);
3862 return false;
3863 }
3864
Jim Inghamda26bd22012-06-08 21:56:10 +00003865protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003866 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003867 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003868 CommandReturnObject &result)
3869 {
3870 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3871 if (target == NULL)
3872 {
3873 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3874 result.SetStatus (eReturnStatusFailed);
3875 return false;
3876 }
3877 else
3878 {
3879 bool syntax_error = false;
3880 uint32_t i;
3881 uint32_t num_successful_lookups = 0;
3882 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3883 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3884 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3885 // Dump all sections for all modules images
3886
3887 if (command.GetArgumentCount() == 0)
3888 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003889 ModuleSP current_module;
3890
3891 // Where it is possible to look in the current symbol context
3892 // first, try that. If this search was successful and --all
3893 // was not passed, don't print anything else.
3894 if (LookupHere (m_interpreter, result, syntax_error))
3895 {
3896 result.GetOutputStream().EOL();
3897 num_successful_lookups++;
3898 if (!m_options.m_print_all)
3899 {
3900 result.SetStatus (eReturnStatusSuccessFinishResult);
3901 return result.Succeeded();
3902 }
3903 }
3904
3905 // Dump all sections for all other modules
3906
Enrico Granata146d9522012-11-08 02:22:02 +00003907 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00003908 Mutex::Locker modules_locker(target_modules.GetMutex());
3909 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00003910 if (num_modules > 0)
3911 {
3912 for (i = 0; i<num_modules && syntax_error == false; ++i)
3913 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00003914 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
3915
3916 if (module_pointer != current_module.get() &&
3917 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003918 {
3919 result.GetOutputStream().EOL();
3920 num_successful_lookups++;
3921 }
3922 }
3923 }
3924 else
3925 {
3926 result.AppendError ("the target has no associated executable images");
3927 result.SetStatus (eReturnStatusFailed);
3928 return false;
3929 }
3930 }
3931 else
3932 {
3933 // Dump specified images (by basename or fullpath)
3934 const char *arg_cstr;
3935 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
3936 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003937 ModuleList module_list;
3938 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
3939 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00003940 {
Jason Molendabf41e192012-10-04 22:47:07 +00003941 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00003942 {
Jason Molendabf41e192012-10-04 22:47:07 +00003943 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00003944 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00003945 {
Greg Clayton91048ef2011-11-10 01:18:58 +00003946 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00003947 {
3948 result.GetOutputStream().EOL();
3949 num_successful_lookups++;
3950 }
3951 }
3952 }
3953 }
3954 else
3955 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
3956 }
3957 }
3958
3959 if (num_successful_lookups > 0)
3960 result.SetStatus (eReturnStatusSuccessFinishResult);
3961 else
3962 result.SetStatus (eReturnStatusFailed);
3963 }
3964 return result.Succeeded();
3965 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003966
3967 CommandOptions m_options;
3968};
3969
3970OptionDefinition
3971CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
3972{
Sean Callanan3bfaad62012-09-13 21:11:40 +00003973 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
3974 { 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 +00003975 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
3976 /* 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 +00003977 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
3978 { 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."},
3979 { 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."},
3980 { 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 +00003981 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00003982 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
3983 { 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."},
3984 { 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."},
3985 { 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."},
3986 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
3987 { 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."},
3988 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00003989};
Chris Lattner24943d22010-06-08 16:52:24 +00003990
3991
Jim Inghamd60d94a2011-03-11 03:53:59 +00003992#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00003993
3994//-------------------------------------------------------------------------
3995// CommandObjectMultiwordImageSearchPaths
3996//-------------------------------------------------------------------------
3997
Greg Claytone1f50b92011-05-03 22:09:39 +00003998class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00003999{
4000public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004001
4002 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4003 CommandObjectMultiword (interpreter,
4004 "target modules search-paths",
4005 "A set of commands for operating on debugger target image search paths.",
4006 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004007 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004008 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4009 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4010 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4011 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4012 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004013 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004014
4015 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004016 {
4017 }
4018};
4019
Greg Claytone1f50b92011-05-03 22:09:39 +00004020
4021
4022#pragma mark CommandObjectTargetModules
4023
4024//-------------------------------------------------------------------------
4025// CommandObjectTargetModules
4026//-------------------------------------------------------------------------
4027
4028class CommandObjectTargetModules : public CommandObjectMultiword
4029{
4030public:
4031 //------------------------------------------------------------------
4032 // Constructors and Destructors
4033 //------------------------------------------------------------------
4034 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4035 CommandObjectMultiword (interpreter,
4036 "target modules",
4037 "A set of commands for accessing information for one or more target modules.",
4038 "target modules <sub-command> ...")
4039 {
4040 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4041 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
4042 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
4043 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4044 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4045 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4046 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004047 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004048
4049 }
4050 virtual
4051 ~CommandObjectTargetModules()
4052 {
4053 }
4054
4055private:
4056 //------------------------------------------------------------------
4057 // For CommandObjectTargetModules only
4058 //------------------------------------------------------------------
4059 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4060};
4061
4062
Greg Clayton3508c382012-02-24 01:59:29 +00004063
Jim Inghamda26bd22012-06-08 21:56:10 +00004064class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004065{
4066public:
4067 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004068 CommandObjectParsed (interpreter,
4069 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004070 "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.",
4071 "target symbols add [<symfile>]"),
4072 m_option_group (interpreter),
4073 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4074 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4075
Greg Clayton3508c382012-02-24 01:59:29 +00004076 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004077 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4078 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4079 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4080 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004081 }
4082
4083 virtual
4084 ~CommandObjectTargetSymbolsAdd ()
4085 {
4086 }
4087
Jim Inghamda26bd22012-06-08 21:56:10 +00004088 int
4089 HandleArgumentCompletion (Args &input,
4090 int &cursor_index,
4091 int &cursor_char_position,
4092 OptionElementVector &opt_element_vector,
4093 int match_start_point,
4094 int max_return_elements,
4095 bool &word_complete,
4096 StringList &matches)
4097 {
4098 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4099 completion_str.erase (cursor_char_position);
4100
4101 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4102 CommandCompletions::eDiskFileCompletion,
4103 completion_str.c_str(),
4104 match_start_point,
4105 max_return_elements,
4106 NULL,
4107 word_complete,
4108 matches);
4109 return matches.GetSize();
4110 }
4111
Greg Clayton437b5bc2012-09-27 22:26:11 +00004112 virtual Options *
4113 GetOptions ()
4114 {
4115 return &m_option_group;
4116 }
4117
4118
Jim Inghamda26bd22012-06-08 21:56:10 +00004119protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004120
4121 bool
4122 AddModuleSymbols (Target *target,
4123 const FileSpec &symfile_spec,
4124 bool &flush,
4125 CommandReturnObject &result)
4126 {
4127 ModuleSP symfile_module_sp (new Module (symfile_spec, target->GetArchitecture()));
4128 const UUID &symfile_uuid = symfile_module_sp->GetUUID();
4129 StreamString ss_symfile_uuid;
4130 symfile_uuid.Dump(&ss_symfile_uuid);
4131
4132 if (symfile_module_sp)
4133 {
4134 char symfile_path[PATH_MAX];
4135 symfile_spec.GetPath (symfile_path, sizeof(symfile_path));
4136 // We now have a module that represents a symbol file
4137 // that can be used for a module that might exist in the
4138 // current target, so we need to find that module in the
4139 // target
4140
4141 ModuleSP old_module_sp (target->GetImages().FindModule (symfile_uuid));
4142 if (old_module_sp)
4143 {
4144 // The module has not yet created its symbol vendor, we can just
4145 // give the existing target module the symfile path to use for
4146 // when it decides to create it!
4147 old_module_sp->SetSymbolFileFileSpec (symfile_module_sp->GetFileSpec());
4148
4149 // Provide feedback that the symfile has been successfully added.
4150 const FileSpec &module_fs = old_module_sp->GetFileSpec();
4151 result.AppendMessageWithFormat("symbol file '%s' with UUID %s has been successfully added to the '%s/%s' module\n",
4152 symfile_path, ss_symfile_uuid.GetData(),
4153 module_fs.GetDirectory().AsCString(), module_fs.GetFilename().AsCString());
4154
4155 // Let clients know something changed in the module
4156 // if it is currently loaded
4157 ModuleList module_list;
4158 module_list.Append (old_module_sp);
4159 target->ModulesDidLoad (module_list);
4160 flush = true;
4161 }
4162 else
4163 {
4164 result.AppendErrorWithFormat ("symbol file '%s' with UUID %s does not match any existing module%s\n",
4165 symfile_path, ss_symfile_uuid.GetData(),
4166 (symfile_spec.GetFileType() != FileSpec::eFileTypeRegular)
4167 ? "\n please specify the full path to the symbol file"
4168 : "");
4169 return false;
4170 }
4171 }
4172 else
4173 {
4174 result.AppendError ("one or more executable image paths must be specified");
4175 result.SetStatus (eReturnStatusFailed);
4176 return false;
4177 }
4178 result.SetStatus (eReturnStatusSuccessFinishResult);
4179 return true;
4180 }
4181
Greg Clayton3508c382012-02-24 01:59:29 +00004182 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004183 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004184 CommandReturnObject &result)
4185 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004186 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
4187 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004188 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004189 if (target == NULL)
4190 {
4191 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Greg Clayton3508c382012-02-24 01:59:29 +00004192 }
4193 else
4194 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004195 bool flush = false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004196 ModuleSpec sym_spec;
4197 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4198 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4199 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
4200
Greg Clayton3508c382012-02-24 01:59:29 +00004201 const size_t argc = args.GetArgumentCount();
4202 if (argc == 0)
4203 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004204 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004205 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004206 bool success = false;
4207 bool error_set = false;
4208 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004209 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004210 Process *process = exe_ctx.GetProcessPtr();
4211 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004212 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004213 const StateType process_state = process->GetState();
4214 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004215 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004216 StackFrame *frame = exe_ctx.GetFramePtr();
4217 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004218 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004219 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4220 if (frame_module_sp)
4221 {
4222 if (frame_module_sp->GetPlatformFileSpec().Exists())
4223 {
4224 sym_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4225 sym_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
4226 }
4227 sym_spec.GetUUID() = frame_module_sp->GetUUID();
4228 success = sym_spec.GetUUID().IsValid() || sym_spec.GetFileSpec();
4229 }
4230 else
4231 {
4232 result.AppendError ("frame has no module");
4233 error_set = true;
4234 }
Greg Clayton3508c382012-02-24 01:59:29 +00004235 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004236 else
4237 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004238 result.AppendError ("invalid current frame");
4239 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004240 }
Greg Clayton3508c382012-02-24 01:59:29 +00004241 }
4242 else
4243 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004244 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
4245 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004246 }
Greg Clayton3508c382012-02-24 01:59:29 +00004247 }
4248 else
4249 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004250 result.AppendError ("a process must exist in order to use the --frame option");
4251 error_set = true;
4252 }
4253 }
4254 else
4255 {
4256 if (uuid_option_set)
4257 {
4258 sym_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4259 success |= sym_spec.GetUUID().IsValid();
4260 }
4261 else if (file_option_set)
4262 {
4263 sym_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4264 ModuleSP module_sp (target->GetImages().FindFirstModule(sym_spec));
4265 if (module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004266 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004267 sym_spec.GetFileSpec() = module_sp->GetFileSpec();
4268 sym_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4269 sym_spec.GetUUID() = module_sp->GetUUID();
4270 sym_spec.GetArchitecture() = module_sp->GetArchitecture();
Greg Clayton3508c382012-02-24 01:59:29 +00004271 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004272 else
4273 {
4274 sym_spec.GetArchitecture() = target->GetArchitecture();
4275 }
4276 success |= sym_spec.GetFileSpec().Exists();
4277 }
4278 }
4279
4280 if (success)
4281 {
4282 if (Symbols::DownloadObjectAndSymbolFile (sym_spec))
4283 {
4284 if (sym_spec.GetSymbolFileSpec())
4285 success = AddModuleSymbols (target, sym_spec.GetSymbolFileSpec(), flush, result);
4286 }
4287 }
4288
4289 if (!success && !error_set)
4290 {
4291 StreamString error_strm;
4292 if (uuid_option_set)
4293 {
4294 error_strm.PutCString("unable to find debug symbols for UUID ");
4295 sym_spec.GetUUID().Dump (&error_strm);
4296 }
4297 else if (file_option_set)
4298 {
4299 error_strm.PutCString("unable to find debug symbols for the executable file ");
4300 error_strm << sym_spec.GetFileSpec();
4301 }
4302 else if (frame_option_set)
4303 {
4304 error_strm.PutCString("unable to find debug symbols for the current frame");
4305 }
4306 result.AppendError (error_strm.GetData());
4307 }
4308 }
4309 else
4310 {
4311 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4312 }
4313 }
4314 else
4315 {
4316 if (uuid_option_set)
4317 {
4318 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4319 }
4320 else if (file_option_set)
4321 {
4322 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4323 }
4324 else if (frame_option_set)
4325 {
4326 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4327 }
4328 else
4329 {
4330 PlatformSP platform_sp (target->GetPlatform());
4331
4332 for (size_t i=0; i<argc; ++i)
4333 {
4334 const char *symfile_path = args.GetArgumentAtIndex(i);
4335 if (symfile_path)
4336 {
4337 FileSpec symfile_spec;
4338 sym_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4339 if (platform_sp)
4340 platform_sp->ResolveSymbolFile(*target, sym_spec, symfile_spec);
4341 else
4342 symfile_spec.SetFile(symfile_path, true);
4343
4344 ArchSpec arch;
4345 bool symfile_exists = symfile_spec.Exists();
4346
4347 if (symfile_exists)
4348 {
4349 if (!AddModuleSymbols (target, symfile_spec, flush, result))
4350 break;
4351 }
4352 else
4353 {
4354 char resolved_symfile_path[PATH_MAX];
4355 if (symfile_spec.GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4356 {
4357 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4358 {
4359 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4360 break;
4361 }
4362 }
4363 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4364 break;
4365 }
Greg Clayton3508c382012-02-24 01:59:29 +00004366 }
4367 }
4368 }
4369 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004370
4371 if (flush)
4372 {
4373 Process *process = exe_ctx.GetProcessPtr();
4374 if (process)
4375 process->Flush();
4376 }
Greg Clayton3508c382012-02-24 01:59:29 +00004377 }
4378 return result.Succeeded();
4379 }
4380
Greg Clayton437b5bc2012-09-27 22:26:11 +00004381 OptionGroupOptions m_option_group;
4382 OptionGroupUUID m_uuid_option_group;
4383 OptionGroupFile m_file_option;
4384 OptionGroupBoolean m_current_frame_option;
4385
4386
Greg Clayton3508c382012-02-24 01:59:29 +00004387};
4388
4389
4390#pragma mark CommandObjectTargetSymbols
4391
4392//-------------------------------------------------------------------------
4393// CommandObjectTargetSymbols
4394//-------------------------------------------------------------------------
4395
4396class CommandObjectTargetSymbols : public CommandObjectMultiword
4397{
4398public:
4399 //------------------------------------------------------------------
4400 // Constructors and Destructors
4401 //------------------------------------------------------------------
4402 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4403 CommandObjectMultiword (interpreter,
4404 "target symbols",
4405 "A set of commands for adding and managing debug symbol files.",
4406 "target symbols <sub-command> ...")
4407 {
4408 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4409
4410 }
4411 virtual
4412 ~CommandObjectTargetSymbols()
4413 {
4414 }
4415
4416private:
4417 //------------------------------------------------------------------
4418 // For CommandObjectTargetModules only
4419 //------------------------------------------------------------------
4420 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4421};
4422
4423
Jim Inghamd60d94a2011-03-11 03:53:59 +00004424#pragma mark CommandObjectTargetStopHookAdd
4425
4426//-------------------------------------------------------------------------
4427// CommandObjectTargetStopHookAdd
4428//-------------------------------------------------------------------------
4429
Jim Inghamda26bd22012-06-08 21:56:10 +00004430class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004431{
4432public:
4433
4434 class CommandOptions : public Options
4435 {
4436 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004437 CommandOptions (CommandInterpreter &interpreter) :
4438 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004439 m_line_start(0),
4440 m_line_end (UINT_MAX),
4441 m_func_name_type_mask (eFunctionNameTypeAuto),
4442 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004443 m_thread_specified (false),
4444 m_use_one_liner (false),
4445 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004446 {
4447 }
4448
4449 ~CommandOptions () {}
4450
Greg Claytonb3448432011-03-24 21:19:54 +00004451 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004452 GetDefinitions ()
4453 {
4454 return g_option_table;
4455 }
4456
4457 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004458 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004459 {
4460 Error error;
4461 char short_option = (char) m_getopt_table[option_idx].val;
4462 bool success;
4463
4464 switch (short_option)
4465 {
4466 case 'c':
4467 m_class_name = option_arg;
4468 m_sym_ctx_specified = true;
4469 break;
4470
4471 case 'e':
4472 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4473 if (!success)
4474 {
Greg Clayton9c236732011-10-26 00:56:27 +00004475 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004476 break;
4477 }
4478 m_sym_ctx_specified = true;
4479 break;
4480
4481 case 'l':
4482 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4483 if (!success)
4484 {
Greg Clayton9c236732011-10-26 00:56:27 +00004485 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004486 break;
4487 }
4488 m_sym_ctx_specified = true;
4489 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004490
4491 case 'i':
4492 m_no_inlines = true;
4493 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004494
4495 case 'n':
4496 m_function_name = option_arg;
4497 m_func_name_type_mask |= eFunctionNameTypeAuto;
4498 m_sym_ctx_specified = true;
4499 break;
4500
4501 case 'f':
4502 m_file_name = option_arg;
4503 m_sym_ctx_specified = true;
4504 break;
4505 case 's':
4506 m_module_name = option_arg;
4507 m_sym_ctx_specified = true;
4508 break;
4509 case 't' :
4510 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004511 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004512 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004513 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004514 m_thread_specified = true;
4515 }
4516 break;
4517 case 'T':
4518 m_thread_name = option_arg;
4519 m_thread_specified = true;
4520 break;
4521 case 'q':
4522 m_queue_name = option_arg;
4523 m_thread_specified = true;
4524 break;
4525 case 'x':
4526 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004527 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004528 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004529 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004530 m_thread_specified = true;
4531 }
4532 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004533 case 'o':
4534 m_use_one_liner = true;
4535 m_one_liner = option_arg;
4536 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004537 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004538 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004539 break;
4540 }
4541 return error;
4542 }
4543
4544 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004545 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004546 {
4547 m_class_name.clear();
4548 m_function_name.clear();
4549 m_line_start = 0;
4550 m_line_end = UINT_MAX;
4551 m_file_name.clear();
4552 m_module_name.clear();
4553 m_func_name_type_mask = eFunctionNameTypeAuto;
4554 m_thread_id = LLDB_INVALID_THREAD_ID;
4555 m_thread_index = UINT32_MAX;
4556 m_thread_name.clear();
4557 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004558
4559 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004560 m_sym_ctx_specified = false;
4561 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004562
4563 m_use_one_liner = false;
4564 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004565 }
4566
4567
Greg Claytonb3448432011-03-24 21:19:54 +00004568 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004569
4570 std::string m_class_name;
4571 std::string m_function_name;
4572 uint32_t m_line_start;
4573 uint32_t m_line_end;
4574 std::string m_file_name;
4575 std::string m_module_name;
4576 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4577 lldb::tid_t m_thread_id;
4578 uint32_t m_thread_index;
4579 std::string m_thread_name;
4580 std::string m_queue_name;
4581 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004582 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004583 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004584 // Instance variables to hold the values for one_liner options.
4585 bool m_use_one_liner;
4586 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004587 };
4588
4589 Options *
4590 GetOptions ()
4591 {
4592 return &m_options;
4593 }
4594
4595 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004596 CommandObjectParsed (interpreter,
4597 "target stop-hook add ",
4598 "Add a hook to be executed when the target stops.",
4599 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004600 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004601 {
4602 }
4603
4604 ~CommandObjectTargetStopHookAdd ()
4605 {
4606 }
4607
4608 static size_t
4609 ReadCommandsCallbackFunction (void *baton,
4610 InputReader &reader,
4611 lldb::InputReaderAction notification,
4612 const char *bytes,
4613 size_t bytes_len)
4614 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004615 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004616 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004617 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004618 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004619
4620 switch (notification)
4621 {
4622 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004623 if (!batch_mode)
4624 {
4625 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4626 if (reader.GetPrompt())
4627 out_stream->Printf ("%s", reader.GetPrompt());
4628 out_stream->Flush();
4629 }
Jim Inghame15511a2011-05-05 01:03:36 +00004630 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004631 break;
4632
4633 case eInputReaderDeactivate:
4634 break;
4635
4636 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004637 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004638 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004639 out_stream->Printf ("%s", reader.GetPrompt());
4640 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004641 }
Jim Inghame15511a2011-05-05 01:03:36 +00004642 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004643 break;
4644
Caroline Tice4a348082011-05-02 20:41:46 +00004645 case eInputReaderAsynchronousOutputWritten:
4646 break;
4647
Jim Inghamd60d94a2011-03-11 03:53:59 +00004648 case eInputReaderGotToken:
4649 if (bytes && bytes_len && baton)
4650 {
4651 StringList *commands = new_stop_hook->GetCommandPointer();
4652 if (commands)
4653 {
4654 commands->AppendString (bytes, bytes_len);
4655 }
4656 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004657 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004658 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004659 out_stream->Printf ("%s", reader.GetPrompt());
4660 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004661 }
4662 break;
4663
4664 case eInputReaderInterrupt:
4665 {
4666 // Finish, and cancel the stop hook.
4667 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004668 if (!batch_mode)
4669 {
4670 out_stream->Printf ("Stop hook cancelled.\n");
4671 out_stream->Flush();
4672 }
4673
Jim Inghamd60d94a2011-03-11 03:53:59 +00004674 reader.SetIsDone (true);
4675 }
Jim Inghame15511a2011-05-05 01:03:36 +00004676 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004677 break;
4678
4679 case eInputReaderEndOfFile:
4680 reader.SetIsDone (true);
4681 break;
4682
4683 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004684 if (!got_interrupted && !batch_mode)
4685 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004686 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004687 out_stream->Flush();
4688 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004689 break;
4690 }
4691
4692 return bytes_len;
4693 }
4694
Jim Inghamda26bd22012-06-08 21:56:10 +00004695protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004696 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004697 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004698 {
4699 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4700 if (target)
4701 {
4702 Target::StopHookSP new_hook_sp;
4703 target->AddStopHook (new_hook_sp);
4704
4705 // First step, make the specifier.
4706 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4707 if (m_options.m_sym_ctx_specified)
4708 {
4709 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4710
4711 if (!m_options.m_module_name.empty())
4712 {
4713 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4714 }
4715
4716 if (!m_options.m_class_name.empty())
4717 {
4718 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4719 }
4720
4721 if (!m_options.m_file_name.empty())
4722 {
4723 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4724 }
4725
4726 if (m_options.m_line_start != 0)
4727 {
4728 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4729 }
4730
4731 if (m_options.m_line_end != UINT_MAX)
4732 {
4733 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4734 }
4735
4736 if (!m_options.m_function_name.empty())
4737 {
4738 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4739 }
4740 }
4741
4742 if (specifier_ap.get())
4743 new_hook_sp->SetSpecifier (specifier_ap.release());
4744
4745 // Next see if any of the thread options have been entered:
4746
4747 if (m_options.m_thread_specified)
4748 {
4749 ThreadSpec *thread_spec = new ThreadSpec();
4750
4751 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4752 {
4753 thread_spec->SetTID (m_options.m_thread_id);
4754 }
4755
4756 if (m_options.m_thread_index != UINT32_MAX)
4757 thread_spec->SetIndex (m_options.m_thread_index);
4758
4759 if (!m_options.m_thread_name.empty())
4760 thread_spec->SetName (m_options.m_thread_name.c_str());
4761
4762 if (!m_options.m_queue_name.empty())
4763 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4764
4765 new_hook_sp->SetThreadSpecifier (thread_spec);
4766
4767 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004768 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004769 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004770 // Use one-liner.
4771 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004772 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004773 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004774 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004775 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004776 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4777 // the new stop hook's command string.
4778 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4779 if (!reader_sp)
4780 {
4781 result.AppendError("out of memory\n");
4782 result.SetStatus (eReturnStatusFailed);
4783 target->RemoveStopHookByID (new_hook_sp->GetID());
4784 return false;
4785 }
4786
4787 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4788 new_hook_sp.get(), // baton
4789 eInputReaderGranularityLine, // token size, to pass to callback function
4790 "DONE", // end token
4791 "> ", // prompt
4792 true)); // echo input
4793 if (!err.Success())
4794 {
4795 result.AppendError (err.AsCString());
4796 result.SetStatus (eReturnStatusFailed);
4797 target->RemoveStopHookByID (new_hook_sp->GetID());
4798 return false;
4799 }
4800 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004801 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004802 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4803 }
4804 else
4805 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004806 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004807 result.SetStatus (eReturnStatusFailed);
4808 }
4809
4810 return result.Succeeded();
4811 }
4812private:
4813 CommandOptions m_options;
4814};
4815
Greg Claytonb3448432011-03-24 21:19:54 +00004816OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004817CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4818{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004819 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00004820 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004821 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4822 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004823 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004824 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004825 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004826 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004827 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004828 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004829 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004830 "The stop hook is run only for threads in the queue whose name is given by this argument."},
4831 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
4832 "Specify the source file within which the stop-hook is to be run." },
4833 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
4834 "Set the start of the line range for which the stop-hook is to be run."},
4835 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
4836 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004837 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00004838 "Specify the class within which the stop-hook is to be run." },
4839 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
4840 "Set the function name within which the stop hook will be run." },
4841 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
4842};
4843
4844#pragma mark CommandObjectTargetStopHookDelete
4845
4846//-------------------------------------------------------------------------
4847// CommandObjectTargetStopHookDelete
4848//-------------------------------------------------------------------------
4849
Jim Inghamda26bd22012-06-08 21:56:10 +00004850class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004851{
4852public:
4853
4854 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004855 CommandObjectParsed (interpreter,
4856 "target stop-hook delete",
4857 "Delete a stop-hook.",
4858 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00004859 {
4860 }
4861
4862 ~CommandObjectTargetStopHookDelete ()
4863 {
4864 }
4865
Jim Inghamda26bd22012-06-08 21:56:10 +00004866protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004867 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004868 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004869 {
4870 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4871 if (target)
4872 {
4873 // FIXME: see if we can use the breakpoint id style parser?
4874 size_t num_args = command.GetArgumentCount();
4875 if (num_args == 0)
4876 {
4877 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
4878 {
4879 result.SetStatus (eReturnStatusFailed);
4880 return false;
4881 }
4882 else
4883 {
4884 target->RemoveAllStopHooks();
4885 }
4886 }
4887 else
4888 {
4889 bool success;
4890 for (size_t i = 0; i < num_args; i++)
4891 {
4892 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4893 if (!success)
4894 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004895 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004896 result.SetStatus(eReturnStatusFailed);
4897 return false;
4898 }
4899 success = target->RemoveStopHookByID (user_id);
4900 if (!success)
4901 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004902 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004903 result.SetStatus(eReturnStatusFailed);
4904 return false;
4905 }
4906 }
4907 }
4908 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4909 }
4910 else
4911 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004912 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004913 result.SetStatus (eReturnStatusFailed);
4914 }
4915
4916 return result.Succeeded();
4917 }
4918};
4919#pragma mark CommandObjectTargetStopHookEnableDisable
4920
4921//-------------------------------------------------------------------------
4922// CommandObjectTargetStopHookEnableDisable
4923//-------------------------------------------------------------------------
4924
Jim Inghamda26bd22012-06-08 21:56:10 +00004925class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004926{
4927public:
4928
4929 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004930 CommandObjectParsed (interpreter,
4931 name,
4932 help,
4933 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004934 m_enable (enable)
4935 {
4936 }
4937
4938 ~CommandObjectTargetStopHookEnableDisable ()
4939 {
4940 }
4941
Jim Inghamda26bd22012-06-08 21:56:10 +00004942protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004943 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004944 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004945 {
4946 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4947 if (target)
4948 {
4949 // FIXME: see if we can use the breakpoint id style parser?
4950 size_t num_args = command.GetArgumentCount();
4951 bool success;
4952
4953 if (num_args == 0)
4954 {
4955 target->SetAllStopHooksActiveState (m_enable);
4956 }
4957 else
4958 {
4959 for (size_t i = 0; i < num_args; i++)
4960 {
4961 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
4962 if (!success)
4963 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004964 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004965 result.SetStatus(eReturnStatusFailed);
4966 return false;
4967 }
4968 success = target->SetStopHookActiveStateByID (user_id, m_enable);
4969 if (!success)
4970 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004971 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00004972 result.SetStatus(eReturnStatusFailed);
4973 return false;
4974 }
4975 }
4976 }
4977 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4978 }
4979 else
4980 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004981 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004982 result.SetStatus (eReturnStatusFailed);
4983 }
4984 return result.Succeeded();
4985 }
4986private:
4987 bool m_enable;
4988};
4989
4990#pragma mark CommandObjectTargetStopHookList
4991
4992//-------------------------------------------------------------------------
4993// CommandObjectTargetStopHookList
4994//-------------------------------------------------------------------------
4995
Jim Inghamda26bd22012-06-08 21:56:10 +00004996class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004997{
4998public:
4999
5000 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005001 CommandObjectParsed (interpreter,
5002 "target stop-hook list",
5003 "List all stop-hooks.",
5004 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005005 {
5006 }
5007
5008 ~CommandObjectTargetStopHookList ()
5009 {
5010 }
5011
Jim Inghamda26bd22012-06-08 21:56:10 +00005012protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005013 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005014 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005015 {
5016 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005017 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005018 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005019 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005020 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005021 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005022 }
5023
5024 size_t num_hooks = target->GetNumStopHooks ();
5025 if (num_hooks == 0)
5026 {
5027 result.GetOutputStream().PutCString ("No stop hooks.\n");
5028 }
5029 else
5030 {
5031 for (size_t i = 0; i < num_hooks; i++)
5032 {
5033 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5034 if (i > 0)
5035 result.GetOutputStream().PutCString ("\n");
5036 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5037 }
5038 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005039 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005040 return result.Succeeded();
5041 }
5042};
5043
5044#pragma mark CommandObjectMultiwordTargetStopHooks
5045//-------------------------------------------------------------------------
5046// CommandObjectMultiwordTargetStopHooks
5047//-------------------------------------------------------------------------
5048
5049class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5050{
5051public:
5052
5053 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5054 CommandObjectMultiword (interpreter,
5055 "target stop-hook",
5056 "A set of commands for operating on debugger target stop-hooks.",
5057 "target stop-hook <subcommand> [<subcommand-options>]")
5058 {
5059 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5060 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5061 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5062 false,
5063 "target stop-hook disable [<id>]",
5064 "Disable a stop-hook.",
5065 "target stop-hook disable")));
5066 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5067 true,
5068 "target stop-hook enable [<id>]",
5069 "Enable a stop-hook.",
5070 "target stop-hook enable")));
5071 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5072 }
5073
5074 ~CommandObjectMultiwordTargetStopHooks()
5075 {
5076 }
5077};
5078
5079
Chris Lattner24943d22010-06-08 16:52:24 +00005080
5081#pragma mark CommandObjectMultiwordTarget
5082
5083//-------------------------------------------------------------------------
5084// CommandObjectMultiwordTarget
5085//-------------------------------------------------------------------------
5086
Greg Clayton63094e02010-06-23 01:19:29 +00005087CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005088 CommandObjectMultiword (interpreter,
5089 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005090 "A set of commands for operating on debugger targets.",
5091 "target <subcommand> [<subcommand-options>]")
5092{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005093
5094 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005095 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005096 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5097 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005098 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005099 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005100 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005101 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005102}
5103
5104CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5105{
5106}
5107
Greg Claytonabe0fed2011-04-18 08:33:37 +00005108