blob: 62343789007d88360c5e77be8afab6bbb2840e50 [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectTarget.h"
13
14// C Includes
15#include <errno.h>
Greg Clayton81040f42011-02-01 01:13:32 +000016
Chris Lattner24943d22010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
19// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Debugger.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000022#include "lldb/Core/InputReader.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000025#include "lldb/Core/Section.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000026#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton801417e2011-07-07 01:59:51 +000028#include "lldb/Core/ValueObjectVariable.h"
Greg Claytonb924eb62012-09-27 03:13:55 +000029#include "lldb/Host/Symbols.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Interpreter/CommandInterpreter.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000032#include "lldb/Interpreter/Options.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000033#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Clayton5beb99d2011-08-11 02:48:45 +000034#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000035#include "lldb/Interpreter/OptionGroupFile.h"
Greg Claytona42880a2011-10-25 06:44:01 +000036#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton368f8222011-07-07 04:38:25 +000037#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000038#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000039#include "lldb/Interpreter/OptionGroupUInt64.h"
40#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton801417e2011-07-07 01:59:51 +000041#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000042#include "lldb/Symbol/CompileUnit.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000043#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000044#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolFile.h"
47#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000048#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton801417e2011-07-07 01:59:51 +000049#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000050#include "lldb/Target/Process.h"
51#include "lldb/Target/StackFrame.h"
52#include "lldb/Target/Thread.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000053#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000054
55using namespace lldb;
56using namespace lldb_private;
57
Greg Claytonabe0fed2011-04-18 08:33:37 +000058
59
60static void
61DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
62{
Greg Clayton52c8b6e2011-04-19 04:19:37 +000063 const ArchSpec &target_arch = target->GetArchitecture();
Greg Claytonabe0fed2011-04-18 08:33:37 +000064
Greg Clayton5beb99d2011-08-11 02:48:45 +000065 Module *exe_module = target->GetExecutableModulePointer();
Greg Claytonabe0fed2011-04-18 08:33:37 +000066 char exe_path[PATH_MAX];
67 bool exe_valid = false;
Greg Clayton5beb99d2011-08-11 02:48:45 +000068 if (exe_module)
69 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Greg Claytonabe0fed2011-04-18 08:33:37 +000070
71 if (!exe_valid)
72 ::strcpy (exe_path, "<none>");
73
74 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
75
76 uint32_t properties = 0;
77 if (target_arch.IsValid())
78 {
79 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
80 properties++;
81 }
82 PlatformSP platform_sp (target->GetPlatform());
83 if (platform_sp)
84 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName());
85
86 ProcessSP process_sp (target->GetProcessSP());
87 bool show_process_status = false;
88 if (process_sp)
89 {
90 lldb::pid_t pid = process_sp->GetID();
91 StateType state = process_sp->GetState();
92 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000093 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000094 const char *state_cstr = StateAsCString (state);
95 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malea5f35a4b2012-11-29 21:49:15 +000096 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000097 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
98 }
99 if (properties > 0)
100 strm.PutCString (" )\n");
101 else
102 strm.EOL();
103 if (show_process_status)
104 {
105 const bool only_threads_with_stop_reason = true;
106 const uint32_t start_frame = 0;
107 const uint32_t num_frames = 1;
108 const uint32_t num_frames_with_source = 1;
109 process_sp->GetStatus (strm);
110 process_sp->GetThreadStatus (strm,
111 only_threads_with_stop_reason,
112 start_frame,
113 num_frames,
114 num_frames_with_source);
115
116 }
117}
118
119static uint32_t
120DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
121{
122 const uint32_t num_targets = target_list.GetNumTargets();
123 if (num_targets)
124 {
125 TargetSP selected_target_sp (target_list.GetSelectedTarget());
126 strm.PutCString ("Current targets:\n");
127 for (uint32_t i=0; i<num_targets; ++i)
128 {
129 TargetSP target_sp (target_list.GetTargetAtIndex (i));
130 if (target_sp)
131 {
132 bool is_selected = target_sp.get() == selected_target_sp.get();
133 DumpTargetInfo (i,
134 target_sp.get(),
135 is_selected ? "* " : " ",
136 show_stopped_process_status,
137 strm);
138 }
139 }
140 }
141 return num_targets;
142}
143#pragma mark CommandObjectTargetCreate
144
145//-------------------------------------------------------------------------
146// "target create"
147//-------------------------------------------------------------------------
148
Jim Inghamda26bd22012-06-08 21:56:10 +0000149class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000150{
151public:
152 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000153 CommandObjectParsed (interpreter,
154 "target create",
155 "Create a target using the argument as the main executable.",
156 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000157 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000158 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000159 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Greg Claytonec9c2d22012-11-30 19:05:35 +0000160 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
161 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
162 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000163 {
164 CommandArgumentEntry arg;
165 CommandArgumentData file_arg;
166
167 // Define the first (and only) variant of this arg.
168 file_arg.arg_type = eArgTypeFilename;
169 file_arg.arg_repetition = eArgRepeatPlain;
170
171 // There is only one variant this argument could be; put it into the argument entry.
172 arg.push_back (file_arg);
173
174 // Push the data for the first argument into the m_arguments vector.
175 m_arguments.push_back (arg);
176
Greg Clayton801417e2011-07-07 01:59:51 +0000177 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000178 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000179 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +0000180 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
181 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000182 m_option_group.Finalize();
183 }
184
185 ~CommandObjectTargetCreate ()
186 {
187 }
188
189 Options *
190 GetOptions ()
191 {
192 return &m_option_group;
193 }
194
Greg Clayton36da2aa2013-01-25 18:06:21 +0000195 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +0000196 HandleArgumentCompletion (Args &input,
197 int &cursor_index,
198 int &cursor_char_position,
199 OptionElementVector &opt_element_vector,
200 int match_start_point,
201 int max_return_elements,
202 bool &word_complete,
203 StringList &matches)
204 {
205 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
206 completion_str.erase (cursor_char_position);
207
208 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
209 CommandCompletions::eDiskFileCompletion,
210 completion_str.c_str(),
211 match_start_point,
212 max_return_elements,
213 NULL,
214 word_complete,
215 matches);
216 return matches.GetSize();
217 }
218
219protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000220 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000221 DoExecute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000222 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000223 const size_t argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000224 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
225
226 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000227 {
Greg Claytonec9c2d22012-11-30 19:05:35 +0000228 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
229 if (symfile)
230 {
231 if (!symfile.Exists())
232 {
233 char symfile_path[PATH_MAX];
234 symfile.GetPath(symfile_path, sizeof(symfile_path));
235 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
236 result.SetStatus (eReturnStatusFailed);
237 return false;
238 }
239 }
240
Greg Claytonabe0fed2011-04-18 08:33:37 +0000241 const char *file_path = command.GetArgumentAtIndex(0);
242 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000243 TargetSP target_sp;
244 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000245 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Claytonec9c2d22012-11-30 19:05:35 +0000246 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000247 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000248 file_path,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000249 arch_cstr,
250 get_dependent_files,
251 &m_platform_options,
252 target_sp));
253
Greg Claytonabe0fed2011-04-18 08:33:37 +0000254 if (target_sp)
255 {
Greg Claytonec9c2d22012-11-30 19:05:35 +0000256 if (symfile)
257 {
258 ModuleSP module_sp (target_sp->GetExecutableModule());
259 if (module_sp)
260 module_sp->SetSymbolFileFileSpec(symfile);
261 }
262
Greg Claytonabe0fed2011-04-18 08:33:37 +0000263 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000264 if (core_file)
265 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000266 char core_path[PATH_MAX];
267 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000268 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000269 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000270 FileSpec core_file_dir;
271 core_file_dir.GetDirectory() = core_file.GetDirectory();
272 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000273
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
275
276 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000277 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000278 // Seems wierd that we Launch a core file, but that is
279 // what we do!
280 error = process_sp->LoadCore();
281
282 if (error.Fail())
283 {
284 result.AppendError(error.AsCString("can't find plug-in for core file"));
285 result.SetStatus (eReturnStatusFailed);
286 return false;
287 }
288 else
289 {
290 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
291 result.SetStatus (eReturnStatusSuccessFinishNoResult);
292 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000293 }
294 else
295 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000296 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
297 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000298 }
299 }
300 else
301 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000302 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000303 result.SetStatus (eReturnStatusFailed);
304 }
305 }
306 else
307 {
308 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
309 result.SetStatus (eReturnStatusSuccessFinishNoResult);
310 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000311 }
312 else
313 {
314 result.AppendError(error.AsCString());
315 result.SetStatus (eReturnStatusFailed);
316 }
317 }
318 else
319 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000320 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 +0000321 result.SetStatus (eReturnStatusFailed);
322 }
323 return result.Succeeded();
324
325 }
326
Greg Claytonabe0fed2011-04-18 08:33:37 +0000327private:
328 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000329 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000330 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000331 OptionGroupFile m_core_file;
Greg Claytonec9c2d22012-11-30 19:05:35 +0000332 OptionGroupFile m_symbol_file;
333 OptionGroupBoolean m_add_dependents;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000334};
335
336#pragma mark CommandObjectTargetList
337
338//----------------------------------------------------------------------
339// "target list"
340//----------------------------------------------------------------------
341
Jim Inghamda26bd22012-06-08 21:56:10 +0000342class CommandObjectTargetList : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000343{
344public:
345 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000346 CommandObjectParsed (interpreter,
347 "target list",
348 "List all current targets in the current debug session.",
349 NULL,
350 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000351 {
352 }
353
354 virtual
355 ~CommandObjectTargetList ()
356 {
357 }
358
Jim Inghamda26bd22012-06-08 21:56:10 +0000359protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000360 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000361 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000362 {
363 if (args.GetArgumentCount() == 0)
364 {
365 Stream &strm = result.GetOutputStream();
366
367 bool show_stopped_process_status = false;
368 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
369 {
370 strm.PutCString ("No targets.\n");
371 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000372 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000373 }
374 else
375 {
376 result.AppendError ("the 'target list' command takes no arguments\n");
377 result.SetStatus (eReturnStatusFailed);
378 }
379 return result.Succeeded();
380 }
381};
382
383
384#pragma mark CommandObjectTargetSelect
385
386//----------------------------------------------------------------------
387// "target select"
388//----------------------------------------------------------------------
389
Jim Inghamda26bd22012-06-08 21:56:10 +0000390class CommandObjectTargetSelect : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000391{
392public:
393 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000394 CommandObjectParsed (interpreter,
395 "target select",
396 "Select a target as the current target by target index.",
397 NULL,
398 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000399 {
400 }
401
402 virtual
403 ~CommandObjectTargetSelect ()
404 {
405 }
406
Jim Inghamda26bd22012-06-08 21:56:10 +0000407protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000408 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000409 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000410 {
411 if (args.GetArgumentCount() == 1)
412 {
413 bool success = false;
414 const char *target_idx_arg = args.GetArgumentAtIndex(0);
415 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
416 if (success)
417 {
418 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
419 const uint32_t num_targets = target_list.GetNumTargets();
420 if (target_idx < num_targets)
421 {
422 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
423 if (target_sp)
424 {
425 Stream &strm = result.GetOutputStream();
426 target_list.SetSelectedTarget (target_sp.get());
427 bool show_stopped_process_status = false;
428 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000429 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000430 }
431 else
432 {
433 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
434 result.SetStatus (eReturnStatusFailed);
435 }
436 }
437 else
438 {
439 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
440 target_idx,
441 num_targets - 1);
442 result.SetStatus (eReturnStatusFailed);
443 }
444 }
445 else
446 {
447 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
448 result.SetStatus (eReturnStatusFailed);
449 }
450 }
451 else
452 {
453 result.AppendError ("'target select' takes a single argument: a target index\n");
454 result.SetStatus (eReturnStatusFailed);
455 }
456 return result.Succeeded();
457 }
458};
459
Greg Clayton153ccd72011-08-10 02:10:13 +0000460#pragma mark CommandObjectTargetSelect
461
462//----------------------------------------------------------------------
463// "target delete"
464//----------------------------------------------------------------------
465
Jim Inghamda26bd22012-06-08 21:56:10 +0000466class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton153ccd72011-08-10 02:10:13 +0000467{
468public:
469 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000470 CommandObjectParsed (interpreter,
471 "target delete",
472 "Delete one or more targets by target index.",
473 NULL,
474 0),
Greg Clayton5beb99d2011-08-11 02:48:45 +0000475 m_option_group (interpreter),
Greg Clayton437b5bc2012-09-27 22:26:11 +0000476 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 +0000477 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000478 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
479 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000480 }
481
482 virtual
483 ~CommandObjectTargetDelete ()
484 {
485 }
486
Jim Inghamda26bd22012-06-08 21:56:10 +0000487 Options *
488 GetOptions ()
489 {
490 return &m_option_group;
491 }
492
493protected:
Greg Clayton153ccd72011-08-10 02:10:13 +0000494 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000495 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton153ccd72011-08-10 02:10:13 +0000496 {
497 const size_t argc = args.GetArgumentCount();
498 std::vector<TargetSP> delete_target_list;
499 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
500 bool success = true;
501 TargetSP target_sp;
502 if (argc > 0)
503 {
504 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000505 // Bail out if don't have any targets.
506 if (num_targets == 0) {
507 result.AppendError("no targets to delete");
508 result.SetStatus(eReturnStatusFailed);
509 success = false;
510 }
511
Greg Clayton153ccd72011-08-10 02:10:13 +0000512 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
513 {
514 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
515 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
516 if (success)
517 {
518 if (target_idx < num_targets)
519 {
520 target_sp = target_list.GetTargetAtIndex (target_idx);
521 if (target_sp)
522 {
523 delete_target_list.push_back (target_sp);
524 continue;
525 }
526 }
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000527 if (num_targets > 1)
528 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
529 target_idx,
530 num_targets - 1);
531 else
532 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
533 target_idx);
534
Greg Clayton153ccd72011-08-10 02:10:13 +0000535 result.SetStatus (eReturnStatusFailed);
536 success = false;
537 }
538 else
539 {
540 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
541 result.SetStatus (eReturnStatusFailed);
542 success = false;
543 }
544 }
545
546 }
547 else
548 {
549 target_sp = target_list.GetSelectedTarget();
550 if (target_sp)
551 {
552 delete_target_list.push_back (target_sp);
553 }
554 else
555 {
556 result.AppendErrorWithFormat("no target is currently selected\n");
557 result.SetStatus (eReturnStatusFailed);
558 success = false;
559 }
560 }
561 if (success)
562 {
563 const size_t num_targets_to_delete = delete_target_list.size();
564 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
565 {
566 target_sp = delete_target_list[idx];
567 target_list.DeleteTarget(target_sp);
568 target_sp->Destroy();
569 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000570 // If "--clean" was specified, prune any orphaned shared modules from
571 // the global shared module list
572 if (m_cleanup_option.GetOptionValue ())
573 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000574 const bool mandatory = true;
575 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000576 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000577 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
578 result.SetStatus(eReturnStatusSuccessFinishResult);
579 }
580
581 return result.Succeeded();
582 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000583
Greg Clayton5beb99d2011-08-11 02:48:45 +0000584 OptionGroupOptions m_option_group;
585 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000586};
587
Greg Claytonabe0fed2011-04-18 08:33:37 +0000588
Greg Clayton801417e2011-07-07 01:59:51 +0000589#pragma mark CommandObjectTargetVariable
590
591//----------------------------------------------------------------------
592// "target variable"
593//----------------------------------------------------------------------
594
Jim Inghamda26bd22012-06-08 21:56:10 +0000595class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000596{
597public:
598 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000599 CommandObjectParsed (interpreter,
600 "target variable",
Greg Claytonf72bd8b2012-11-03 00:10:22 +0000601 "Read global variable(s) prior to, or while running your binary.",
Jim Inghamda26bd22012-06-08 21:56:10 +0000602 NULL,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000603 eFlagRequiresTarget),
Greg Clayton801417e2011-07-07 01:59:51 +0000604 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000605 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000606 m_option_format (eFormatDefault),
Greg Clayton6475c422012-12-04 00:32:51 +0000607 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'file', 0, eArgTypeFilename, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
608 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'shlb', 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 +0000609 m_varobj_options()
610 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000611 CommandArgumentEntry arg;
612 CommandArgumentData var_name_arg;
613
614 // Define the first (and only) variant of this arg.
615 var_name_arg.arg_type = eArgTypeVarName;
616 var_name_arg.arg_repetition = eArgRepeatPlus;
617
618 // There is only one variant this argument could be; put it into the argument entry.
619 arg.push_back (var_name_arg);
620
621 // Push the data for the first argument into the m_arguments vector.
622 m_arguments.push_back (arg);
623
Greg Clayton801417e2011-07-07 01:59:51 +0000624 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000625 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000626 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 +0000627 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
628 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
629 m_option_group.Finalize();
630 }
631
632 virtual
633 ~CommandObjectTargetVariable ()
634 {
635 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000636
637 void
638 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
639 {
Enrico Granatac3f5cd82013-03-26 18:04:53 +0000640 ValueObject::DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Enrico Granata19030d82011-08-15 18:01:31 +0000641
Greg Clayton5d81f492011-07-08 21:46:14 +0000642 switch (var_sp->GetScope())
643 {
644 case eValueTypeVariableGlobal:
645 if (m_option_variable.show_scope)
646 s.PutCString("GLOBAL: ");
647 break;
648
649 case eValueTypeVariableStatic:
650 if (m_option_variable.show_scope)
651 s.PutCString("STATIC: ");
652 break;
653
654 case eValueTypeVariableArgument:
655 if (m_option_variable.show_scope)
656 s.PutCString(" ARG: ");
657 break;
658
659 case eValueTypeVariableLocal:
660 if (m_option_variable.show_scope)
661 s.PutCString(" LOCAL: ");
662 break;
663
664 default:
665 break;
666 }
667
Greg Claytonfb816422011-07-10 19:21:23 +0000668 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000669 {
Greg Claytonfb816422011-07-10 19:21:23 +0000670 bool show_fullpaths = false;
671 bool show_module = true;
672 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
673 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000674 }
675
Greg Claytona42880a2011-10-25 06:44:01 +0000676 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000677 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000678 options.SetFormat(format);
679
680 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000681
682 ValueObject::DumpValueObject (s,
683 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000684 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000685
686 }
Greg Clayton801417e2011-07-07 01:59:51 +0000687
Greg Clayton5d81f492011-07-08 21:46:14 +0000688
Greg Clayton36da2aa2013-01-25 18:06:21 +0000689 static size_t GetVariableCallback (void *baton,
690 const char *name,
691 VariableList &variable_list)
Greg Clayton5d81f492011-07-08 21:46:14 +0000692 {
693 Target *target = static_cast<Target *>(baton);
694 if (target)
695 {
696 return target->GetImages().FindGlobalVariables (ConstString(name),
697 true,
698 UINT32_MAX,
699 variable_list);
700 }
701 return 0;
702 }
703
704
705
Jim Inghamda26bd22012-06-08 21:56:10 +0000706 Options *
707 GetOptions ()
708 {
709 return &m_option_group;
710 }
711
712protected:
Greg Clayton6475c422012-12-04 00:32:51 +0000713
714 void
715 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
716 {
717 size_t count = variable_list.GetSize();
718 if (count > 0)
719 {
720 if (sc.module_sp)
721 {
722 if (sc.comp_unit)
723 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000724 s.Printf ("Global variables for %s in %s:\n",
725 sc.comp_unit->GetPath().c_str(),
726 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000727 }
728 else
729 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000730 s.Printf ("Global variables for %s\n",
731 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000732 }
733 }
734 else if (sc.comp_unit)
735 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000736 s.Printf ("Global variables for %s\n",
737 sc.comp_unit->GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000738 }
739
740 for (uint32_t i=0; i<count; ++i)
741 {
742 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
743 if (var_sp)
744 {
745 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
746
747 if (valobj_sp)
748 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
749 }
750 }
751 }
752
753 }
Greg Clayton801417e2011-07-07 01:59:51 +0000754 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000755 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000756 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000757 Target *target = m_exe_ctx.GetTargetPtr();
758 const size_t argc = args.GetArgumentCount();
759 Stream &s = result.GetOutputStream();
760
761 if (argc > 0)
Greg Clayton801417e2011-07-07 01:59:51 +0000762 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000763
764 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton801417e2011-07-07 01:59:51 +0000765 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000766 VariableList variable_list;
767 ValueObjectList valobj_list;
Greg Clayton5d81f492011-07-08 21:46:14 +0000768
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000769 const char *arg = args.GetArgumentAtIndex(idx);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000770 size_t matches = 0;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000771 bool use_var_name = false;
772 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000773 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000774 RegularExpression regex(arg);
775 if (!regex.IsValid ())
Greg Clayton801417e2011-07-07 01:59:51 +0000776 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000777 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton368f8222011-07-07 04:38:25 +0000778 result.SetStatus (eReturnStatusFailed);
779 return false;
780 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000781 use_var_name = true;
782 matches = target->GetImages().FindGlobalVariables (regex,
783 true,
784 UINT32_MAX,
785 variable_list);
Greg Clayton6475c422012-12-04 00:32:51 +0000786 }
787 else
788 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000789 Error error (Variable::GetValuesForVariableExpressionPath (arg,
790 m_exe_ctx.GetBestExecutionContextScope(),
791 GetVariableCallback,
792 target,
793 variable_list,
794 valobj_list));
795 matches = variable_list.GetSize();
796 }
797
798 if (matches == 0)
799 {
800 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
801 result.SetStatus (eReturnStatusFailed);
802 return false;
803 }
804 else
805 {
806 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton6475c422012-12-04 00:32:51 +0000807 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000808 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
809 if (var_sp)
Greg Clayton6475c422012-12-04 00:32:51 +0000810 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000811 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
812 if (!valobj_sp)
813 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton6475c422012-12-04 00:32:51 +0000814
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000815 if (valobj_sp)
816 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton6475c422012-12-04 00:32:51 +0000817 }
818 }
Greg Claytonfac93882011-10-05 22:17:32 +0000819 }
Greg Clayton801417e2011-07-07 01:59:51 +0000820 }
821 }
822 else
823 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000824 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
825 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
826 SymbolContextList sc_list;
827 const size_t num_compile_units = compile_units.GetSize();
828 const size_t num_shlibs = shlibs.GetSize();
829 if (num_compile_units == 0 && num_shlibs == 0)
830 {
831 bool success = false;
832 StackFrame *frame = m_exe_ctx.GetFramePtr();
833 CompileUnit *comp_unit = NULL;
834 if (frame)
835 {
836 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
837 if (sc.comp_unit)
838 {
839 const bool can_create = true;
840 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
841 if (comp_unit_varlist_sp)
842 {
843 size_t count = comp_unit_varlist_sp->GetSize();
844 if (count > 0)
845 {
846 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
847 success = true;
848 }
849 }
850 }
851 }
852 if (!success)
853 {
854 if (frame)
855 {
856 if (comp_unit)
Greg Clayton97a19b22013-04-29 17:25:54 +0000857 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
858 comp_unit->GetPath().c_str());
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000859 else
860 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
861 }
862 else
863 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
864 result.SetStatus (eReturnStatusFailed);
865 }
866 }
867 else
868 {
869 SymbolContextList sc_list;
870 const bool append = true;
871 // We have one or more compile unit or shlib
872 if (num_shlibs > 0)
873 {
874 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
875 {
876 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
877 ModuleSpec module_spec (module_file);
878
879 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
880 if (module_sp)
881 {
882 if (num_compile_units > 0)
883 {
884 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
885 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
886 }
887 else
888 {
889 SymbolContext sc;
890 sc.module_sp = module_sp;
891 sc_list.Append(sc);
892 }
893 }
894 else
895 {
896 // Didn't find matching shlib/module in target...
Greg Clayton97a19b22013-04-29 17:25:54 +0000897 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
898 module_file.GetPath().c_str());
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000899 }
900 }
901 }
902 else
903 {
904 // No shared libraries, we just want to find globals for the compile units files that were specified
905 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
906 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
907 }
908
909 const uint32_t num_scs = sc_list.GetSize();
910 if (num_scs > 0)
911 {
912 SymbolContext sc;
913 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
914 {
915 if (sc_list.GetContextAtIndex(sc_idx, sc))
916 {
917 if (sc.comp_unit)
918 {
919 const bool can_create = true;
920 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
921 if (comp_unit_varlist_sp)
922 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
923 }
924 else if (sc.module_sp)
925 {
926 // Get all global variables for this module
927 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
928 VariableList variable_list;
929 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
930 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
931 }
932 }
933 }
934 }
935 }
Greg Clayton801417e2011-07-07 01:59:51 +0000936 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000937
Enrico Granatadb64d952011-08-12 16:42:31 +0000938 if (m_interpreter.TruncationWarningNecessary())
939 {
940 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
941 m_cmd_name.c_str());
942 m_interpreter.TruncationWarningGiven();
943 }
944
Greg Clayton801417e2011-07-07 01:59:51 +0000945 return result.Succeeded();
946 }
947
Greg Clayton801417e2011-07-07 01:59:51 +0000948 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000949 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000950 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000951 OptionGroupFileList m_option_compile_units;
952 OptionGroupFileList m_option_shared_libraries;
953 OptionGroupValueObjectDisplay m_varobj_options;
954
955};
956
957
Greg Claytone1f50b92011-05-03 22:09:39 +0000958#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000959
Jim Inghamda26bd22012-06-08 21:56:10 +0000960class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000961{
962public:
963
Greg Claytone1f50b92011-05-03 22:09:39 +0000964 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000965 CommandObjectParsed (interpreter,
966 "target modules search-paths add",
967 "Add new image search paths substitution pairs to the current target.",
968 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000969 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000970 CommandArgumentEntry arg;
971 CommandArgumentData old_prefix_arg;
972 CommandArgumentData new_prefix_arg;
973
974 // Define the first variant of this arg pair.
975 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
976 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
977
978 // Define the first variant of this arg pair.
979 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
980 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
981
982 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
983 // must always occur together, they are treated as two variants of one argument rather than two independent
984 // arguments. Push them both into the first argument position for m_arguments...
985
986 arg.push_back (old_prefix_arg);
987 arg.push_back (new_prefix_arg);
988
989 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000990 }
991
Greg Claytone1f50b92011-05-03 22:09:39 +0000992 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000993 {
994 }
995
Jim Inghamda26bd22012-06-08 21:56:10 +0000996protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000997 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000998 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000999 CommandReturnObject &result)
1000 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001001 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001002 if (target)
1003 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001004 const size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001005 if (argc & 1)
1006 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001007 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001008 result.SetStatus (eReturnStatusFailed);
1009 }
1010 else
1011 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001012 for (size_t i=0; i<argc; i+=2)
Chris Lattner24943d22010-06-08 16:52:24 +00001013 {
1014 const char *from = command.GetArgumentAtIndex(i);
1015 const char *to = command.GetArgumentAtIndex(i+1);
1016
1017 if (from[0] && to[0])
1018 {
1019 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +00001020 target->GetImageSearchPathList().Append (ConstString(from),
1021 ConstString(to),
1022 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +00001023 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001024 }
1025 else
1026 {
1027 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001028 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001029 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001030 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001031 result.SetStatus (eReturnStatusFailed);
1032 }
1033 }
1034 }
1035 }
1036 else
1037 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001038 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001039 result.SetStatus (eReturnStatusFailed);
1040 }
1041 return result.Succeeded();
1042 }
1043};
1044
Greg Claytone1f50b92011-05-03 22:09:39 +00001045#pragma mark CommandObjectTargetModulesSearchPathsClear
1046
Jim Inghamda26bd22012-06-08 21:56:10 +00001047class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001048{
1049public:
1050
Greg Claytone1f50b92011-05-03 22:09:39 +00001051 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001052 CommandObjectParsed (interpreter,
1053 "target modules search-paths clear",
1054 "Clear all current image search path substitution pairs from the current target.",
1055 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +00001056 {
1057 }
1058
Greg Claytone1f50b92011-05-03 22:09:39 +00001059 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +00001060 {
1061 }
1062
Jim Inghamda26bd22012-06-08 21:56:10 +00001063protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001064 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001065 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001066 CommandReturnObject &result)
1067 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001068 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001069 if (target)
1070 {
1071 bool notify = true;
1072 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +00001073 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001074 }
1075 else
1076 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001077 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001078 result.SetStatus (eReturnStatusFailed);
1079 }
1080 return result.Succeeded();
1081 }
1082};
1083
Greg Claytone1f50b92011-05-03 22:09:39 +00001084#pragma mark CommandObjectTargetModulesSearchPathsInsert
1085
Jim Inghamda26bd22012-06-08 21:56:10 +00001086class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001087{
1088public:
1089
Greg Claytone1f50b92011-05-03 22:09:39 +00001090 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001091 CommandObjectParsed (interpreter,
1092 "target modules search-paths insert",
1093 "Insert a new image search path substitution pair into the current target at the specified index.",
1094 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001095 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001096 CommandArgumentEntry arg1;
1097 CommandArgumentEntry arg2;
1098 CommandArgumentData index_arg;
1099 CommandArgumentData old_prefix_arg;
1100 CommandArgumentData new_prefix_arg;
1101
1102 // Define the first and only variant of this arg.
1103 index_arg.arg_type = eArgTypeIndex;
1104 index_arg.arg_repetition = eArgRepeatPlain;
1105
1106 // Put the one and only variant into the first arg for m_arguments:
1107 arg1.push_back (index_arg);
1108
1109 // Define the first variant of this arg pair.
1110 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1111 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1112
1113 // Define the first variant of this arg pair.
1114 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1115 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1116
1117 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1118 // must always occur together, they are treated as two variants of one argument rather than two independent
1119 // arguments. Push them both into the same argument position for m_arguments...
1120
1121 arg2.push_back (old_prefix_arg);
1122 arg2.push_back (new_prefix_arg);
1123
1124 // Add arguments to m_arguments.
1125 m_arguments.push_back (arg1);
1126 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001127 }
1128
Greg Claytone1f50b92011-05-03 22:09:39 +00001129 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001130 {
1131 }
1132
Jim Inghamda26bd22012-06-08 21:56:10 +00001133protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001134 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001135 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001136 CommandReturnObject &result)
1137 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001138 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001139 if (target)
1140 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001141 size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001142 // check for at least 3 arguments and an odd nubmer of parameters
1143 if (argc >= 3 && argc & 1)
1144 {
1145 bool success = false;
1146
1147 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1148
1149 if (!success)
1150 {
1151 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1152 result.SetStatus (eReturnStatusFailed);
1153 return result.Succeeded();
1154 }
1155
1156 // shift off the index
1157 command.Shift();
1158 argc = command.GetArgumentCount();
1159
1160 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1161 {
1162 const char *from = command.GetArgumentAtIndex(i);
1163 const char *to = command.GetArgumentAtIndex(i+1);
1164
1165 if (from[0] && to[0])
1166 {
1167 bool last_pair = ((argc - i) == 2);
1168 target->GetImageSearchPathList().Insert (ConstString(from),
1169 ConstString(to),
1170 insert_idx,
1171 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001172 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001173 }
1174 else
1175 {
1176 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001177 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001178 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001179 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001180 result.SetStatus (eReturnStatusFailed);
1181 return false;
1182 }
1183 }
1184 }
1185 else
1186 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001187 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001188 result.SetStatus (eReturnStatusFailed);
1189 return result.Succeeded();
1190 }
1191
1192 }
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#pragma mark CommandObjectTargetModulesSearchPathsList
1204
1205
Jim Inghamda26bd22012-06-08 21:56:10 +00001206class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001207{
1208public:
1209
Greg Claytone1f50b92011-05-03 22:09:39 +00001210 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001211 CommandObjectParsed (interpreter,
1212 "target modules search-paths list",
1213 "List all current image search path substitution pairs in the current target.",
1214 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001215 {
1216 }
1217
Greg Claytone1f50b92011-05-03 22:09:39 +00001218 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001219 {
1220 }
1221
Jim Inghamda26bd22012-06-08 21:56:10 +00001222protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001223 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001224 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001225 CommandReturnObject &result)
1226 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001227 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001228 if (target)
1229 {
1230 if (command.GetArgumentCount() != 0)
1231 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001232 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001233 result.SetStatus (eReturnStatusFailed);
1234 return result.Succeeded();
1235 }
1236
1237 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001238 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001239 }
1240 else
1241 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001242 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001243 result.SetStatus (eReturnStatusFailed);
1244 }
1245 return result.Succeeded();
1246 }
1247};
1248
Greg Claytone1f50b92011-05-03 22:09:39 +00001249#pragma mark CommandObjectTargetModulesSearchPathsQuery
1250
Jim Inghamda26bd22012-06-08 21:56:10 +00001251class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001252{
1253public:
1254
Greg Claytone1f50b92011-05-03 22:09:39 +00001255 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001256 CommandObjectParsed (interpreter,
1257 "target modules search-paths query",
1258 "Transform a path using the first applicable image search path.",
1259 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001260 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001261 CommandArgumentEntry arg;
1262 CommandArgumentData path_arg;
1263
1264 // Define the first (and only) variant of this arg.
Sean Callanan9a91ef62012-10-24 01:12:14 +00001265 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice43b014a2010-10-04 22:28:36 +00001266 path_arg.arg_repetition = eArgRepeatPlain;
1267
1268 // There is only one variant this argument could be; put it into the argument entry.
1269 arg.push_back (path_arg);
1270
1271 // Push the data for the first argument into the m_arguments vector.
1272 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001273 }
1274
Greg Claytone1f50b92011-05-03 22:09:39 +00001275 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001276 {
1277 }
1278
Jim Inghamda26bd22012-06-08 21:56:10 +00001279protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001280 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001281 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001282 CommandReturnObject &result)
1283 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001284 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001285 if (target)
1286 {
1287 if (command.GetArgumentCount() != 1)
1288 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001289 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001290 result.SetStatus (eReturnStatusFailed);
1291 return result.Succeeded();
1292 }
1293
1294 ConstString orig(command.GetArgumentAtIndex(0));
1295 ConstString transformed;
1296 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1297 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1298 else
1299 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001300
1301 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001302 }
1303 else
1304 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001305 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001306 result.SetStatus (eReturnStatusFailed);
1307 }
1308 return result.Succeeded();
1309 }
1310};
1311
Greg Claytone1f50b92011-05-03 22:09:39 +00001312//----------------------------------------------------------------------
1313// Static Helper functions
1314//----------------------------------------------------------------------
1315static void
1316DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1317{
1318 if (module)
1319 {
1320 const char *arch_cstr;
1321 if (full_triple)
1322 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1323 else
1324 arch_cstr = module->GetArchitecture().GetArchitectureName();
1325 if (width)
1326 strm.Printf("%-*s", width, arch_cstr);
1327 else
1328 strm.PutCString(arch_cstr);
1329 }
1330}
1331
1332static void
1333DumpModuleUUID (Stream &strm, Module *module)
1334{
Jim Ingham6f01c932012-10-12 17:34:26 +00001335 if (module && module->GetUUID().IsValid())
Greg Clayton153ccd72011-08-10 02:10:13 +00001336 module->GetUUID().Dump (&strm);
1337 else
1338 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001339}
1340
1341static uint32_t
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001342DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1343 Stream &strm,
1344 Module *module,
1345 const FileSpec &file_spec,
1346 bool load_addresses)
Greg Claytone1f50b92011-05-03 22:09:39 +00001347{
1348 uint32_t num_matches = 0;
1349 if (module)
1350 {
1351 SymbolContextList sc_list;
1352 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1353 0,
1354 false,
1355 eSymbolContextCompUnit,
1356 sc_list);
1357
1358 for (uint32_t i=0; i<num_matches; ++i)
1359 {
1360 SymbolContext sc;
1361 if (sc_list.GetContextAtIndex(i, sc))
1362 {
1363 if (i > 0)
1364 strm << "\n\n";
1365
1366 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1367 << module->GetFileSpec().GetFilename() << "\n";
1368 LineTable *line_table = sc.comp_unit->GetLineTable();
1369 if (line_table)
1370 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001371 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001372 lldb::eDescriptionLevelBrief);
1373 else
1374 strm << "No line table";
1375 }
1376 }
1377 }
1378 return num_matches;
1379}
1380
1381static void
1382DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1383{
1384 if (file_spec_ptr)
1385 {
1386 if (width > 0)
1387 {
1388 char fullpath[PATH_MAX];
1389 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1390 {
1391 strm.Printf("%-*s", width, fullpath);
1392 return;
1393 }
1394 }
1395 else
1396 {
1397 file_spec_ptr->Dump(&strm);
1398 return;
1399 }
1400 }
1401 // Keep the width spacing correct if things go wrong...
1402 if (width > 0)
1403 strm.Printf("%-*s", width, "");
1404}
1405
1406static void
1407DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1408{
1409 if (file_spec_ptr)
1410 {
1411 if (width > 0)
1412 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1413 else
1414 file_spec_ptr->GetDirectory().Dump(&strm);
1415 return;
1416 }
1417 // Keep the width spacing correct if things go wrong...
1418 if (width > 0)
1419 strm.Printf("%-*s", width, "");
1420}
1421
1422static void
1423DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1424{
1425 if (file_spec_ptr)
1426 {
1427 if (width > 0)
1428 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1429 else
1430 file_spec_ptr->GetFilename().Dump(&strm);
1431 return;
1432 }
1433 // Keep the width spacing correct if things go wrong...
1434 if (width > 0)
1435 strm.Printf("%-*s", width, "");
1436}
1437
1438
1439static void
1440DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1441{
1442 if (module)
1443 {
1444 ObjectFile *objfile = module->GetObjectFile ();
1445 if (objfile)
1446 {
1447 Symtab *symtab = objfile->GetSymtab();
1448 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001449 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001450 }
1451 }
1452}
1453
1454static void
1455DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1456{
1457 if (module)
1458 {
1459 ObjectFile *objfile = module->GetObjectFile ();
1460 if (objfile)
1461 {
1462 SectionList *section_list = objfile->GetSectionList();
1463 if (section_list)
1464 {
Greg Clayton97a19b22013-04-29 17:25:54 +00001465 strm.Printf ("Sections for '%s' (%s):\n",
1466 module->GetSpecificationDescription().c_str(),
1467 module->GetArchitecture().GetArchitectureName());
Greg Claytone1f50b92011-05-03 22:09:39 +00001468 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001469 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001470 strm.IndentLess();
1471 }
1472 }
1473 }
1474}
1475
1476static bool
1477DumpModuleSymbolVendor (Stream &strm, Module *module)
1478{
1479 if (module)
1480 {
1481 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1482 if (symbol_vendor)
1483 {
1484 symbol_vendor->Dump(&strm);
1485 return true;
1486 }
1487 }
1488 return false;
1489}
1490
Greg Clayton2ad894b2012-05-15 18:43:44 +00001491static void
1492DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1493{
1494 strm.IndentMore();
1495 strm.Indent (" Address: ");
1496 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1497 strm.PutCString (" (");
1498 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1499 strm.PutCString (")\n");
1500 strm.Indent (" Summary: ");
1501 const uint32_t save_indent = strm.GetIndentLevel ();
1502 strm.SetIndentLevel (save_indent + 13);
1503 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1504 strm.SetIndentLevel (save_indent);
1505 // Print out detailed address information when verbose is enabled
1506 if (verbose)
1507 {
1508 strm.EOL();
1509 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1510 }
1511 strm.IndentLess();
1512}
1513
Greg Claytone1f50b92011-05-03 22:09:39 +00001514static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001515LookupAddressInModule (CommandInterpreter &interpreter,
1516 Stream &strm,
1517 Module *module,
1518 uint32_t resolve_mask,
1519 lldb::addr_t raw_addr,
1520 lldb::addr_t offset,
1521 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001522{
1523 if (module)
1524 {
1525 lldb::addr_t addr = raw_addr - offset;
1526 Address so_addr;
1527 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001528 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001529 if (target && !target->GetSectionLoadList().IsEmpty())
1530 {
1531 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1532 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001533 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001534 return false;
1535 }
1536 else
1537 {
1538 if (!module->ResolveFileAddress (addr, so_addr))
1539 return false;
1540 }
1541
Greg Claytone1f50b92011-05-03 22:09:39 +00001542 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001543 DumpAddress (exe_scope, so_addr, verbose, strm);
1544// strm.IndentMore();
1545// strm.Indent (" Address: ");
1546// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1547// strm.PutCString (" (");
1548// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1549// strm.PutCString (")\n");
1550// strm.Indent (" Summary: ");
1551// const uint32_t save_indent = strm.GetIndentLevel ();
1552// strm.SetIndentLevel (save_indent + 13);
1553// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1554// strm.SetIndentLevel (save_indent);
1555// // Print out detailed address information when verbose is enabled
1556// if (verbose)
1557// {
1558// strm.EOL();
1559// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1560// }
1561// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001562 return true;
1563 }
1564
1565 return false;
1566}
1567
1568static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001569LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001570{
1571 if (module)
1572 {
1573 SymbolContext sc;
1574
1575 ObjectFile *objfile = module->GetObjectFile ();
1576 if (objfile)
1577 {
1578 Symtab *symtab = objfile->GetSymtab();
1579 if (symtab)
1580 {
1581 uint32_t i;
1582 std::vector<uint32_t> match_indexes;
1583 ConstString symbol_name (name);
1584 uint32_t num_matches = 0;
1585 if (name_is_regex)
1586 {
1587 RegularExpression name_regexp(name);
1588 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1589 eSymbolTypeAny,
1590 match_indexes);
1591 }
1592 else
1593 {
1594 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1595 }
1596
1597
1598 if (num_matches > 0)
1599 {
1600 strm.Indent ();
1601 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1602 name_is_regex ? "the regular expression " : "", name);
1603 DumpFullpath (strm, &module->GetFileSpec(), 0);
1604 strm.PutCString(":\n");
1605 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001606 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001607 for (i=0; i < num_matches; ++i)
1608 {
1609 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001610 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1611 symbol->GetAddress(),
1612 verbose,
1613 strm);
1614
1615// strm.Indent ();
1616// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001617 }
1618 strm.IndentLess ();
1619 return num_matches;
1620 }
1621 }
1622 }
1623 }
1624 return 0;
1625}
1626
1627
1628static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001629DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001630{
1631 strm.IndentMore ();
1632 uint32_t i;
1633 const uint32_t num_matches = sc_list.GetSize();
1634
1635 for (i=0; i<num_matches; ++i)
1636 {
1637 SymbolContext sc;
1638 if (sc_list.GetContextAtIndex(i, sc))
1639 {
Sean Callanand7793d22012-02-11 00:24:04 +00001640 AddressRange range;
1641
1642 sc.GetAddressRange(eSymbolContextEverything,
1643 0,
1644 true,
1645 range);
1646
Greg Clayton2ad894b2012-05-15 18:43:44 +00001647 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001648 }
1649 }
1650 strm.IndentLess ();
1651}
1652
Greg Clayton36da2aa2013-01-25 18:06:21 +00001653static size_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001654LookupFunctionInModule (CommandInterpreter &interpreter,
1655 Stream &strm,
1656 Module *module,
1657 const char *name,
1658 bool name_is_regex,
1659 bool include_inlines,
1660 bool include_symbols,
1661 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001662{
1663 if (module && name && name[0])
1664 {
1665 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001666 const bool append = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001667 size_t num_matches = 0;
Greg Claytone1f50b92011-05-03 22:09:39 +00001668 if (name_is_regex)
1669 {
1670 RegularExpression function_name_regex (name);
1671 num_matches = module->FindFunctions (function_name_regex,
1672 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001673 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001674 append,
1675 sc_list);
1676 }
1677 else
1678 {
1679 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001680 num_matches = module->FindFunctions (function_name,
1681 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001682 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1683 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001684 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001685 append,
1686 sc_list);
1687 }
1688
1689 if (num_matches)
1690 {
1691 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001692 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Claytone1f50b92011-05-03 22:09:39 +00001693 DumpFullpath (strm, &module->GetFileSpec(), 0);
1694 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001695 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001696 }
1697 return num_matches;
1698 }
1699 return 0;
1700}
1701
Greg Clayton36da2aa2013-01-25 18:06:21 +00001702static size_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001703LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001704 Stream &strm,
1705 Module *module,
1706 const char *name_cstr,
1707 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001708{
1709 if (module && name_cstr && name_cstr[0])
1710 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001711 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001712 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001713 size_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001714 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001715 SymbolContext sc;
1716
1717 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001718 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001719
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001720 if (num_matches)
1721 {
1722 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001723 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001724 DumpFullpath (strm, &module->GetFileSpec(), 0);
1725 strm.PutCString(":\n");
1726 const uint32_t num_types = type_list.GetSize();
1727 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001728 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001729 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1730 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001731 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001732 // Resolve the clang type so that any forward references
1733 // to types that haven't yet been parsed will get parsed.
1734 type_sp->GetClangFullType ();
1735 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001736 // Print all typedef chains
1737 TypeSP typedef_type_sp (type_sp);
1738 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1739 while (typedefed_type_sp)
1740 {
1741 strm.EOL();
1742 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1743 typedefed_type_sp->GetClangFullType ();
1744 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1745 typedef_type_sp = typedefed_type_sp;
1746 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1747 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001748 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001749 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001750 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001751 }
1752 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001753 }
1754 return 0;
1755}
1756
Greg Clayton36da2aa2013-01-25 18:06:21 +00001757static size_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001758LookupTypeHere (CommandInterpreter &interpreter,
1759 Stream &strm,
1760 const SymbolContext &sym_ctx,
1761 const char *name_cstr,
1762 bool name_is_regex)
1763{
1764 if (!sym_ctx.module_sp)
1765 return 0;
1766
1767 TypeList type_list;
1768 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001769 size_t num_matches = 1;
Sean Callanan56d31ec2012-06-06 20:49:55 +00001770 bool name_is_fully_qualified = false;
1771
1772 ConstString name(name_cstr);
1773 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1774
1775 if (num_matches)
1776 {
1777 strm.Indent ();
1778 strm.PutCString("Best match found in ");
1779 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1780 strm.PutCString(":\n");
1781
1782 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1783 if (type_sp)
1784 {
1785 // Resolve the clang type so that any forward references
1786 // to types that haven't yet been parsed will get parsed.
1787 type_sp->GetClangFullType ();
1788 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1789 // Print all typedef chains
1790 TypeSP typedef_type_sp (type_sp);
1791 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1792 while (typedefed_type_sp)
1793 {
1794 strm.EOL();
1795 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1796 typedefed_type_sp->GetClangFullType ();
1797 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1798 typedef_type_sp = typedefed_type_sp;
1799 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1800 }
1801 }
1802 strm.EOL();
1803 }
1804 return num_matches;
1805}
1806
1807static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001808LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001809 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001810 Module *module,
1811 const FileSpec &file_spec,
1812 uint32_t line,
1813 bool check_inlines,
1814 bool verbose)
1815{
1816 if (module && file_spec)
1817 {
1818 SymbolContextList sc_list;
1819 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1820 eSymbolContextEverything, sc_list);
1821 if (num_matches > 0)
1822 {
1823 strm.Indent ();
1824 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1825 strm << file_spec;
1826 if (line > 0)
1827 strm.Printf (":%u", line);
1828 strm << " in ";
1829 DumpFullpath (strm, &module->GetFileSpec(), 0);
1830 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001831 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001832 return num_matches;
1833 }
1834 }
1835 return 0;
1836
1837}
1838
Greg Clayton91048ef2011-11-10 01:18:58 +00001839
1840static size_t
1841FindModulesByName (Target *target,
1842 const char *module_name,
1843 ModuleList &module_list,
1844 bool check_global_list)
1845{
1846// Dump specified images (by basename or fullpath)
1847 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001848 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001849
1850 const size_t initial_size = module_list.GetSize ();
1851
Greg Clayton316f57f2012-07-11 20:46:47 +00001852 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001853 {
1854 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001855 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00001856 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton91048ef2011-11-10 01:18:58 +00001857 ModuleSP module_sp;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001858 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton91048ef2011-11-10 01:18:58 +00001859 {
1860 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1861
1862 if (module)
1863 {
Greg Clayton444fe992012-02-26 05:51:37 +00001864 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001865 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001866 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001867 module_list.AppendIfNeeded(module_sp);
1868 }
1869 }
1870 }
1871 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001872 else
1873 {
1874 if (target)
1875 {
1876 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1877
1878 // Not found in our module list for our target, check the main
1879 // shared module list in case it is a extra file used somewhere
1880 // else
1881 if (num_matches == 0)
1882 {
1883 module_spec.GetArchitecture() = target->GetArchitecture();
1884 ModuleList::FindSharedModules (module_spec, module_list);
1885 }
1886 }
1887 else
1888 {
1889 ModuleList::FindSharedModules (module_spec,module_list);
1890 }
1891 }
1892
Greg Clayton91048ef2011-11-10 01:18:58 +00001893 return module_list.GetSize () - initial_size;
1894}
1895
Greg Claytone1f50b92011-05-03 22:09:39 +00001896#pragma mark CommandObjectTargetModulesModuleAutoComplete
1897
1898//----------------------------------------------------------------------
1899// A base command object class that can auto complete with module file
1900// paths
1901//----------------------------------------------------------------------
1902
Jim Inghamda26bd22012-06-08 21:56:10 +00001903class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001904{
1905public:
1906
1907 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1908 const char *name,
1909 const char *help,
1910 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001911 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001912 {
1913 CommandArgumentEntry arg;
1914 CommandArgumentData file_arg;
1915
1916 // Define the first (and only) variant of this arg.
1917 file_arg.arg_type = eArgTypeFilename;
1918 file_arg.arg_repetition = eArgRepeatStar;
1919
1920 // There is only one variant this argument could be; put it into the argument entry.
1921 arg.push_back (file_arg);
1922
1923 // Push the data for the first argument into the m_arguments vector.
1924 m_arguments.push_back (arg);
1925 }
1926
1927 virtual
1928 ~CommandObjectTargetModulesModuleAutoComplete ()
1929 {
1930 }
1931
1932 virtual int
1933 HandleArgumentCompletion (Args &input,
1934 int &cursor_index,
1935 int &cursor_char_position,
1936 OptionElementVector &opt_element_vector,
1937 int match_start_point,
1938 int max_return_elements,
1939 bool &word_complete,
1940 StringList &matches)
1941 {
1942 // Arguments are the standard module completer.
1943 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1944 completion_str.erase (cursor_char_position);
1945
1946 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1947 CommandCompletions::eModuleCompletion,
1948 completion_str.c_str(),
1949 match_start_point,
1950 max_return_elements,
1951 NULL,
1952 word_complete,
1953 matches);
1954 return matches.GetSize();
1955 }
1956};
1957
1958#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1959
1960//----------------------------------------------------------------------
1961// A base command object class that can auto complete with module source
1962// file paths
1963//----------------------------------------------------------------------
1964
Jim Inghamda26bd22012-06-08 21:56:10 +00001965class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001966{
1967public:
1968
1969 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001970 const char *name,
1971 const char *help,
1972 const char *syntax,
1973 uint32_t flags) :
1974 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytone1f50b92011-05-03 22:09:39 +00001975 {
1976 CommandArgumentEntry arg;
1977 CommandArgumentData source_file_arg;
1978
1979 // Define the first (and only) variant of this arg.
1980 source_file_arg.arg_type = eArgTypeSourceFile;
1981 source_file_arg.arg_repetition = eArgRepeatPlus;
1982
1983 // There is only one variant this argument could be; put it into the argument entry.
1984 arg.push_back (source_file_arg);
1985
1986 // Push the data for the first argument into the m_arguments vector.
1987 m_arguments.push_back (arg);
1988 }
1989
1990 virtual
1991 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1992 {
1993 }
1994
1995 virtual int
1996 HandleArgumentCompletion (Args &input,
1997 int &cursor_index,
1998 int &cursor_char_position,
1999 OptionElementVector &opt_element_vector,
2000 int match_start_point,
2001 int max_return_elements,
2002 bool &word_complete,
2003 StringList &matches)
2004 {
2005 // Arguments are the standard source file completer.
2006 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2007 completion_str.erase (cursor_char_position);
2008
2009 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2010 CommandCompletions::eSourceFileCompletion,
2011 completion_str.c_str(),
2012 match_start_point,
2013 max_return_elements,
2014 NULL,
2015 word_complete,
2016 matches);
2017 return matches.GetSize();
2018 }
2019};
2020
2021
2022#pragma mark CommandObjectTargetModulesDumpSymtab
2023
2024
2025class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2026{
2027public:
2028 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2029 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2030 "target modules dump symtab",
2031 "Dump the symbol table from one or more target modules.",
2032 NULL),
2033 m_options (interpreter)
2034 {
2035 }
2036
2037 virtual
2038 ~CommandObjectTargetModulesDumpSymtab ()
2039 {
2040 }
2041
Jim Inghamda26bd22012-06-08 21:56:10 +00002042 virtual Options *
2043 GetOptions ()
2044 {
2045 return &m_options;
2046 }
2047
2048 class CommandOptions : public Options
2049 {
2050 public:
2051
2052 CommandOptions (CommandInterpreter &interpreter) :
2053 Options(interpreter),
2054 m_sort_order (eSortOrderNone)
2055 {
2056 }
2057
2058 virtual
2059 ~CommandOptions ()
2060 {
2061 }
2062
2063 virtual Error
2064 SetOptionValue (uint32_t option_idx, const char *option_arg)
2065 {
2066 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00002067 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamda26bd22012-06-08 21:56:10 +00002068
2069 switch (short_option)
2070 {
2071 case 's':
2072 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2073 g_option_table[option_idx].enum_values,
2074 eSortOrderNone,
2075 error);
2076 break;
2077
2078 default:
2079 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2080 break;
2081
2082 }
2083 return error;
2084 }
2085
2086 void
2087 OptionParsingStarting ()
2088 {
2089 m_sort_order = eSortOrderNone;
2090 }
2091
2092 const OptionDefinition*
2093 GetDefinitions ()
2094 {
2095 return g_option_table;
2096 }
2097
2098 // Options table: Required for subclasses of Options.
2099 static OptionDefinition g_option_table[];
2100
2101 SortOrder m_sort_order;
2102 };
2103
2104protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002105 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002106 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002107 CommandReturnObject &result)
2108 {
2109 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2110 if (target == NULL)
2111 {
2112 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2113 result.SetStatus (eReturnStatusFailed);
2114 return false;
2115 }
2116 else
2117 {
2118 uint32_t num_dumped = 0;
2119
2120 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2121 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2122 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2123
2124 if (command.GetArgumentCount() == 0)
2125 {
2126 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002127 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002128 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002129 if (num_modules > 0)
2130 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002131 result.GetOutputStream().Printf("Dumping symbol table for %zu modules.\n", num_modules);
2132 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002133 {
2134 if (num_dumped > 0)
2135 {
2136 result.GetOutputStream().EOL();
2137 result.GetOutputStream().EOL();
2138 }
2139 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002140 DumpModuleSymtab (m_interpreter,
2141 result.GetOutputStream(),
2142 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2143 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002144 }
2145 }
2146 else
2147 {
2148 result.AppendError ("the target has no associated executable images");
2149 result.SetStatus (eReturnStatusFailed);
2150 return false;
2151 }
2152 }
2153 else
2154 {
2155 // Dump specified images (by basename or fullpath)
2156 const char *arg_cstr;
2157 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2158 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002159 ModuleList module_list;
2160 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2161 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002162 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002163 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002164 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002165 Module *module = module_list.GetModulePointerAtIndex(i);
2166 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002167 {
2168 if (num_dumped > 0)
2169 {
2170 result.GetOutputStream().EOL();
2171 result.GetOutputStream().EOL();
2172 }
2173 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002174 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002175 }
2176 }
2177 }
2178 else
2179 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2180 }
2181 }
2182
2183 if (num_dumped > 0)
2184 result.SetStatus (eReturnStatusSuccessFinishResult);
2185 else
2186 {
2187 result.AppendError ("no matching executable images found");
2188 result.SetStatus (eReturnStatusFailed);
2189 }
2190 }
2191 return result.Succeeded();
2192 }
2193
Greg Claytone1f50b92011-05-03 22:09:39 +00002194
2195 CommandOptions m_options;
2196};
2197
2198static OptionEnumValueElement
2199g_sort_option_enumeration[4] =
2200{
2201 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2202 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2203 { eSortOrderByName, "name", "Sort output by symbol name."},
2204 { 0, NULL, NULL }
2205};
2206
2207
2208OptionDefinition
2209CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2210{
2211 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2212 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2213};
2214
2215#pragma mark CommandObjectTargetModulesDumpSections
2216
2217//----------------------------------------------------------------------
2218// Image section dumping command
2219//----------------------------------------------------------------------
2220
2221class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2222{
2223public:
2224 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2225 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2226 "target modules dump sections",
2227 "Dump the sections from one or more target modules.",
2228 //"target modules dump sections [<file1> ...]")
2229 NULL)
2230 {
2231 }
2232
2233 virtual
2234 ~CommandObjectTargetModulesDumpSections ()
2235 {
2236 }
2237
Jim Inghamda26bd22012-06-08 21:56:10 +00002238protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002239 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002240 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002241 CommandReturnObject &result)
2242 {
2243 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2244 if (target == NULL)
2245 {
2246 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2247 result.SetStatus (eReturnStatusFailed);
2248 return false;
2249 }
2250 else
2251 {
2252 uint32_t num_dumped = 0;
2253
2254 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2255 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2256 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2257
2258 if (command.GetArgumentCount() == 0)
2259 {
2260 // Dump all sections for all modules images
Greg Clayton36da2aa2013-01-25 18:06:21 +00002261 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002262 if (num_modules > 0)
2263 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002264 result.GetOutputStream().Printf("Dumping sections for %zu modules.\n", num_modules);
2265 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002266 {
2267 num_dumped++;
2268 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2269 }
2270 }
2271 else
2272 {
2273 result.AppendError ("the target has no associated executable images");
2274 result.SetStatus (eReturnStatusFailed);
2275 return false;
2276 }
2277 }
2278 else
2279 {
2280 // Dump specified images (by basename or fullpath)
2281 const char *arg_cstr;
2282 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2283 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002284 ModuleList module_list;
2285 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2286 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002287 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002288 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002289 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002290 Module *module = module_list.GetModulePointerAtIndex(i);
2291 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002292 {
2293 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002294 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002295 }
2296 }
2297 }
2298 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002299 {
2300 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002301 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002302
Greg Claytone1f50b92011-05-03 22:09:39 +00002303 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002304 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002305 }
2306 }
2307
2308 if (num_dumped > 0)
2309 result.SetStatus (eReturnStatusSuccessFinishResult);
2310 else
2311 {
2312 result.AppendError ("no matching executable images found");
2313 result.SetStatus (eReturnStatusFailed);
2314 }
2315 }
2316 return result.Succeeded();
2317 }
2318};
2319
2320
2321#pragma mark CommandObjectTargetModulesDumpSymfile
2322
2323//----------------------------------------------------------------------
2324// Image debug symbol dumping command
2325//----------------------------------------------------------------------
2326
2327class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2328{
2329public:
2330 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2331 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2332 "target modules dump symfile",
2333 "Dump the debug symbol file for one or more target modules.",
2334 //"target modules dump symfile [<file1> ...]")
2335 NULL)
2336 {
2337 }
2338
2339 virtual
2340 ~CommandObjectTargetModulesDumpSymfile ()
2341 {
2342 }
2343
Jim Inghamda26bd22012-06-08 21:56:10 +00002344protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002345 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002346 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002347 CommandReturnObject &result)
2348 {
2349 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2350 if (target == NULL)
2351 {
2352 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2353 result.SetStatus (eReturnStatusFailed);
2354 return false;
2355 }
2356 else
2357 {
2358 uint32_t num_dumped = 0;
2359
2360 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2361 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2362 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2363
2364 if (command.GetArgumentCount() == 0)
2365 {
2366 // Dump all sections for all modules images
Enrico Granata146d9522012-11-08 02:22:02 +00002367 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002368 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002369 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002370 if (num_modules > 0)
2371 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002372 result.GetOutputStream().Printf("Dumping debug symbols for %zu modules.\n", num_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002373 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2374 {
Jim Ingham93367902012-05-30 02:19:25 +00002375 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002376 num_dumped++;
2377 }
2378 }
2379 else
2380 {
2381 result.AppendError ("the target has no associated executable images");
2382 result.SetStatus (eReturnStatusFailed);
2383 return false;
2384 }
2385 }
2386 else
2387 {
2388 // Dump specified images (by basename or fullpath)
2389 const char *arg_cstr;
2390 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2391 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002392 ModuleList module_list;
2393 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2394 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002395 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002396 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002397 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002398 Module *module = module_list.GetModulePointerAtIndex(i);
2399 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002400 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002401 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002402 num_dumped++;
2403 }
2404 }
2405 }
2406 else
2407 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2408 }
2409 }
2410
2411 if (num_dumped > 0)
2412 result.SetStatus (eReturnStatusSuccessFinishResult);
2413 else
2414 {
2415 result.AppendError ("no matching executable images found");
2416 result.SetStatus (eReturnStatusFailed);
2417 }
2418 }
2419 return result.Succeeded();
2420 }
2421};
2422
2423
2424#pragma mark CommandObjectTargetModulesDumpLineTable
2425
2426//----------------------------------------------------------------------
2427// Image debug line table dumping command
2428//----------------------------------------------------------------------
2429
2430class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2431{
2432public:
2433 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2434 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002435 "target modules dump line-table",
2436 "Dump the debug symbol file for one or more target modules.",
2437 NULL,
2438 eFlagRequiresTarget)
Greg Claytone1f50b92011-05-03 22:09:39 +00002439 {
2440 }
2441
2442 virtual
2443 ~CommandObjectTargetModulesDumpLineTable ()
2444 {
2445 }
2446
Jim Inghamda26bd22012-06-08 21:56:10 +00002447protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002448 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002449 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002450 CommandReturnObject &result)
2451 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002452 Target *target = m_exe_ctx.GetTargetPtr();
2453 uint32_t total_num_dumped = 0;
2454
2455 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2456 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2457 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2458
2459 if (command.GetArgumentCount() == 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002460 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002461 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytone1f50b92011-05-03 22:09:39 +00002462 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002463 }
2464 else
2465 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002466 // Dump specified images (by basename or fullpath)
2467 const char *arg_cstr;
2468 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002469 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002470 FileSpec file_spec(arg_cstr, false);
2471
2472 const ModuleList &target_modules = target->GetImages();
2473 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002474 const size_t num_modules = target_modules.GetSize();
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002475 if (num_modules > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002476 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002477 uint32_t num_dumped = 0;
2478 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002479 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002480 if (DumpCompileUnitLineTable (m_interpreter,
2481 result.GetOutputStream(),
2482 target_modules.GetModulePointerAtIndexUnlocked(i),
2483 file_spec,
2484 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2485 num_dumped++;
Greg Claytone1f50b92011-05-03 22:09:39 +00002486 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002487 if (num_dumped == 0)
2488 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2489 else
2490 total_num_dumped += num_dumped;
Greg Claytone1f50b92011-05-03 22:09:39 +00002491 }
2492 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002493 }
2494
2495 if (total_num_dumped > 0)
2496 result.SetStatus (eReturnStatusSuccessFinishResult);
2497 else
2498 {
2499 result.AppendError ("no source filenames matched any command arguments");
2500 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002501 }
2502 return result.Succeeded();
2503 }
2504};
2505
2506
2507#pragma mark CommandObjectTargetModulesDump
2508
2509//----------------------------------------------------------------------
2510// Dump multi-word command for target modules
2511//----------------------------------------------------------------------
2512
2513class CommandObjectTargetModulesDump : public CommandObjectMultiword
2514{
2515public:
2516
2517 //------------------------------------------------------------------
2518 // Constructors and Destructors
2519 //------------------------------------------------------------------
2520 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2521 CommandObjectMultiword (interpreter,
2522 "target modules dump",
2523 "A set of commands for dumping information about one or more target modules.",
2524 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2525 {
2526 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2527 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2528 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2529 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2530 }
2531
2532 virtual
2533 ~CommandObjectTargetModulesDump()
2534 {
2535 }
2536};
2537
Jim Inghamda26bd22012-06-08 21:56:10 +00002538class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002539{
2540public:
2541 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002542 CommandObjectParsed (interpreter,
2543 "target modules add",
2544 "Add a new module to the current target's modules.",
Greg Clayton1649a722012-11-29 22:16:27 +00002545 "target modules add [<module>]"),
Greg Claytonec9c2d22012-11-30 19:05:35 +00002546 m_option_group (interpreter),
2547 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable.")
Greg Claytone1f50b92011-05-03 22:09:39 +00002548 {
Greg Clayton1649a722012-11-29 22:16:27 +00002549 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +00002550 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1649a722012-11-29 22:16:27 +00002551 m_option_group.Finalize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002552 }
2553
2554 virtual
2555 ~CommandObjectTargetModulesAdd ()
2556 {
2557 }
Greg Clayton1649a722012-11-29 22:16:27 +00002558
2559 virtual Options *
2560 GetOptions ()
2561 {
2562 return &m_option_group;
2563 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002564
Greg Clayton36da2aa2013-01-25 18:06:21 +00002565 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00002566 HandleArgumentCompletion (Args &input,
2567 int &cursor_index,
2568 int &cursor_char_position,
2569 OptionElementVector &opt_element_vector,
2570 int match_start_point,
2571 int max_return_elements,
2572 bool &word_complete,
2573 StringList &matches)
2574 {
2575 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2576 completion_str.erase (cursor_char_position);
2577
2578 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2579 CommandCompletions::eDiskFileCompletion,
2580 completion_str.c_str(),
2581 match_start_point,
2582 max_return_elements,
2583 NULL,
2584 word_complete,
2585 matches);
2586 return matches.GetSize();
2587 }
2588
2589protected:
Greg Clayton1649a722012-11-29 22:16:27 +00002590
2591 OptionGroupOptions m_option_group;
2592 OptionGroupUUID m_uuid_option_group;
Greg Claytonec9c2d22012-11-30 19:05:35 +00002593 OptionGroupFile m_symbol_file;
Greg Clayton1649a722012-11-29 22:16:27 +00002594
2595
Greg Claytone1f50b92011-05-03 22:09:39 +00002596 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002597 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002598 CommandReturnObject &result)
2599 {
2600 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2601 if (target == NULL)
2602 {
2603 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2604 result.SetStatus (eReturnStatusFailed);
2605 return false;
2606 }
2607 else
2608 {
Sean Callanane3f06612012-12-13 01:39:39 +00002609 bool flush = false;
2610
Greg Claytone1f50b92011-05-03 22:09:39 +00002611 const size_t argc = args.GetArgumentCount();
2612 if (argc == 0)
2613 {
Greg Clayton1649a722012-11-29 22:16:27 +00002614 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2615 {
2616 // We are given a UUID only, go locate the file
2617 ModuleSpec module_spec;
2618 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002619 if (m_symbol_file.GetOptionValue().OptionWasSet())
2620 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002621 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2622 {
2623 ModuleSP module_sp (target->GetSharedModule (module_spec));
2624 if (module_sp)
2625 {
2626 result.SetStatus (eReturnStatusSuccessFinishResult);
2627 return true;
2628 }
2629 else
2630 {
Sean Callanane3f06612012-12-13 01:39:39 +00002631 flush = true;
2632
Greg Clayton1649a722012-11-29 22:16:27 +00002633 StreamString strm;
2634 module_spec.GetUUID().Dump (&strm);
2635 if (module_spec.GetFileSpec())
2636 {
2637 if (module_spec.GetSymbolFileSpec())
2638 {
Greg Clayton97a19b22013-04-29 17:25:54 +00002639 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s and symbol file %s",
Greg Clayton1649a722012-11-29 22:16:27 +00002640 strm.GetString().c_str(),
Greg Clayton97a19b22013-04-29 17:25:54 +00002641 module_spec.GetFileSpec().GetPath().c_str(),
2642 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton1649a722012-11-29 22:16:27 +00002643 }
2644 else
2645 {
Greg Clayton97a19b22013-04-29 17:25:54 +00002646 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton1649a722012-11-29 22:16:27 +00002647 strm.GetString().c_str(),
Greg Clayton97a19b22013-04-29 17:25:54 +00002648 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton1649a722012-11-29 22:16:27 +00002649 }
2650 }
2651 else
2652 {
2653 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2654 strm.GetString().c_str());
2655 }
2656 result.SetStatus (eReturnStatusFailed);
2657 return false;
2658 }
2659 }
2660 else
2661 {
2662 StreamString strm;
2663 module_spec.GetUUID().Dump (&strm);
2664 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2665 result.SetStatus (eReturnStatusFailed);
2666 return false;
2667 }
2668 }
2669 else
2670 {
2671 result.AppendError ("one or more executable image paths must be specified");
2672 result.SetStatus (eReturnStatusFailed);
2673 return false;
2674 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002675 }
2676 else
2677 {
2678 for (size_t i=0; i<argc; ++i)
2679 {
2680 const char *path = args.GetArgumentAtIndex(i);
2681 if (path)
2682 {
2683 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002684 if (file_spec.Exists())
2685 {
Greg Clayton444fe992012-02-26 05:51:37 +00002686 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002687 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2688 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002689 if (m_symbol_file.GetOptionValue().OptionWasSet())
2690 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002691 Error error;
2692 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002693 if (!module_sp)
2694 {
Greg Clayton1649a722012-11-29 22:16:27 +00002695 const char *error_cstr = error.AsCString();
2696 if (error_cstr)
2697 result.AppendError (error_cstr);
2698 else
2699 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002700 result.SetStatus (eReturnStatusFailed);
2701 return false;
2702 }
Sean Callanane3f06612012-12-13 01:39:39 +00002703 else
2704 {
2705 flush = true;
2706 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002707 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002708 }
2709 else
2710 {
2711 char resolved_path[PATH_MAX];
2712 result.SetStatus (eReturnStatusFailed);
2713 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2714 {
2715 if (strcmp (resolved_path, path) != 0)
2716 {
2717 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2718 break;
2719 }
2720 }
2721 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2722 break;
2723 }
2724 }
2725 }
2726 }
Sean Callanane3f06612012-12-13 01:39:39 +00002727
2728 if (flush)
2729 {
2730 ProcessSP process = target->GetProcessSP();
2731 if (process)
2732 process->Flush();
2733 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002734 }
Sean Callanane3f06612012-12-13 01:39:39 +00002735
Greg Claytone1f50b92011-05-03 22:09:39 +00002736 return result.Succeeded();
2737 }
2738
Greg Claytone1f50b92011-05-03 22:09:39 +00002739};
2740
2741class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2742{
2743public:
2744 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2745 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2746 "target modules load",
2747 "Set the load addresses for one or more sections in a target module.",
2748 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2749 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002750 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 +00002751 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)
2752 {
2753 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2754 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2755 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2756 m_option_group.Finalize();
2757 }
2758
2759 virtual
2760 ~CommandObjectTargetModulesLoad ()
2761 {
2762 }
2763
Jim Inghamda26bd22012-06-08 21:56:10 +00002764 virtual Options *
2765 GetOptions ()
2766 {
2767 return &m_option_group;
2768 }
2769
2770protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002771 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002772 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002773 CommandReturnObject &result)
2774 {
2775 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2776 if (target == NULL)
2777 {
2778 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2779 result.SetStatus (eReturnStatusFailed);
2780 return false;
2781 }
2782 else
2783 {
2784 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002785 ModuleSpec module_spec;
2786 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002787 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002788 {
2789 search_using_module_spec = true;
2790 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2791 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002792
2793 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002794 {
2795 search_using_module_spec = true;
2796 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2797 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002798
Greg Clayton444fe992012-02-26 05:51:37 +00002799 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002800 {
2801
2802 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002803 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002804
2805 char path[PATH_MAX];
2806 if (num_matches == 1)
2807 {
2808 Module *module = matching_modules.GetModulePointerAtIndex(0);
2809 if (module)
2810 {
2811 ObjectFile *objfile = module->GetObjectFile();
2812 if (objfile)
2813 {
2814 SectionList *section_list = objfile->GetSectionList();
2815 if (section_list)
2816 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002817 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002818 if (argc == 0)
2819 {
2820 if (m_slide_option.GetOptionValue().OptionWasSet())
2821 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002822 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2823 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002824 }
2825 else
2826 {
2827 result.AppendError ("one or more section name + load address pair must be specified");
2828 result.SetStatus (eReturnStatusFailed);
2829 return false;
2830 }
2831 }
2832 else
2833 {
2834 if (m_slide_option.GetOptionValue().OptionWasSet())
2835 {
2836 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2837 result.SetStatus (eReturnStatusFailed);
2838 return false;
2839 }
2840
2841 for (size_t i=0; i<argc; i += 2)
2842 {
2843 const char *sect_name = args.GetArgumentAtIndex(i);
2844 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2845 if (sect_name && load_addr_cstr)
2846 {
2847 ConstString const_sect_name(sect_name);
2848 bool success = false;
2849 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2850 if (success)
2851 {
2852 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2853 if (section_sp)
2854 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002855 if (section_sp->IsThreadSpecific())
2856 {
2857 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2858 result.SetStatus (eReturnStatusFailed);
2859 break;
2860 }
2861 else
2862 {
Greg Clayton545762f2012-07-07 01:24:12 +00002863 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002864 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002865 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002866 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002867 }
2868 else
2869 {
2870 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2871 result.SetStatus (eReturnStatusFailed);
2872 break;
2873 }
2874 }
2875 else
2876 {
2877 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2878 result.SetStatus (eReturnStatusFailed);
2879 break;
2880 }
2881 }
2882 else
2883 {
2884 if (sect_name)
2885 result.AppendError ("section names must be followed by a load address.\n");
2886 else
2887 result.AppendError ("one or more section name + load address pair must be specified.\n");
2888 result.SetStatus (eReturnStatusFailed);
2889 break;
2890 }
2891 }
2892 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002893
2894 if (changed)
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002895 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002896 target->ModulesDidLoad (matching_modules);
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002897 Process *process = m_exe_ctx.GetProcessPtr();
2898 if (process)
2899 process->Flush();
2900 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002901 }
2902 else
2903 {
2904 module->GetFileSpec().GetPath (path, sizeof(path));
2905 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2906 result.SetStatus (eReturnStatusFailed);
2907 }
2908 }
2909 else
2910 {
2911 module->GetFileSpec().GetPath (path, sizeof(path));
2912 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2913 result.SetStatus (eReturnStatusFailed);
2914 }
2915 }
2916 else
2917 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002918 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2919 if (module_spec_file)
2920 {
2921 module_spec_file->GetPath (path, sizeof(path));
2922 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2923 }
2924 else
2925 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002926 result.SetStatus (eReturnStatusFailed);
2927 }
2928 }
2929 else
2930 {
2931 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002932
2933 if (module_spec.GetFileSpec())
2934 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002935 else
2936 path[0] = '\0';
2937
Greg Clayton444fe992012-02-26 05:51:37 +00002938 if (module_spec.GetUUIDPtr())
2939 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002940 else
2941 uuid_cstr[0] = '\0';
2942 if (num_matches > 1)
2943 {
2944 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2945 path[0] ? " file=" : "",
2946 path,
2947 uuid_cstr[0] ? " uuid=" : "",
2948 uuid_cstr);
2949 for (size_t i=0; i<num_matches; ++i)
2950 {
2951 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2952 result.AppendMessageWithFormat("%s\n", path);
2953 }
2954 }
2955 else
2956 {
2957 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2958 path[0] ? " file=" : "",
2959 path,
2960 uuid_cstr[0] ? " uuid=" : "",
2961 uuid_cstr);
2962 }
2963 result.SetStatus (eReturnStatusFailed);
2964 }
2965 }
2966 else
2967 {
2968 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2969 result.SetStatus (eReturnStatusFailed);
2970 return false;
2971 }
2972 }
2973 return result.Succeeded();
2974 }
2975
Greg Claytone1f50b92011-05-03 22:09:39 +00002976 OptionGroupOptions m_option_group;
2977 OptionGroupUUID m_uuid_option_group;
2978 OptionGroupFile m_file_option;
2979 OptionGroupUInt64 m_slide_option;
2980};
2981
2982//----------------------------------------------------------------------
2983// List images with associated information
2984//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002985class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002986{
2987public:
2988
2989 class CommandOptions : public Options
2990 {
2991 public:
2992
2993 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002994 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002995 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002996 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002997 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002998 {
2999 }
3000
3001 virtual
3002 ~CommandOptions ()
3003 {
3004 }
3005
3006 virtual Error
3007 SetOptionValue (uint32_t option_idx, const char *option_arg)
3008 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003009 Error error;
3010
Greg Clayton6475c422012-12-04 00:32:51 +00003011 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00003012 if (short_option == 'g')
3013 {
3014 m_use_global_module_list = true;
3015 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003016 else if (short_option == 'a')
3017 {
Jim Inghamd86cf812013-03-15 23:09:19 +00003018 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3019 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Ingham6bdea822011-10-24 18:36:33 +00003020 }
Greg Clayton899025f2011-08-09 00:01:09 +00003021 else
3022 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003023 unsigned long width = 0;
Greg Clayton899025f2011-08-09 00:01:09 +00003024 if (option_arg)
3025 width = strtoul (option_arg, NULL, 0);
3026 m_format_array.push_back(std::make_pair(short_option, width));
3027 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003028 return error;
3029 }
3030
3031 void
3032 OptionParsingStarting ()
3033 {
3034 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00003035 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00003036 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00003037 }
3038
3039 const OptionDefinition*
3040 GetDefinitions ()
3041 {
3042 return g_option_table;
3043 }
3044
3045 // Options table: Required for subclasses of Options.
3046
3047 static OptionDefinition g_option_table[];
3048
3049 // Instance variables to hold the values for command options.
3050 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3051 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00003052 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00003053 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00003054 };
3055
3056 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003057 CommandObjectParsed (interpreter,
3058 "target modules list",
3059 "List current executable and dependent shared library images.",
3060 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00003061 m_options (interpreter)
3062 {
3063 }
3064
3065 virtual
3066 ~CommandObjectTargetModulesList ()
3067 {
3068 }
3069
3070 virtual
3071 Options *
3072 GetOptions ()
3073 {
3074 return &m_options;
3075 }
3076
Jim Inghamda26bd22012-06-08 21:56:10 +00003077protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003078 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003079 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003080 CommandReturnObject &result)
3081 {
3082 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00003083 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00003084 // Define a local module list here to ensure it lives longer than any "locker"
3085 // object which might lock its contents below (through the "module_list_ptr"
3086 // variable).
3087 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00003088 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00003089 {
3090 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3091 result.SetStatus (eReturnStatusFailed);
3092 return false;
3093 }
3094 else
3095 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003096 if (target)
3097 {
3098 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3099 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3100 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3101 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003102 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00003103 Stream &strm = result.GetOutputStream();
3104
3105 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3106 {
3107 if (target)
3108 {
3109 Address module_address;
3110 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3111 {
Greg Clayton3508c382012-02-24 01:59:29 +00003112 ModuleSP module_sp (module_address.GetModule());
3113 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00003114 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003115 PrintModule (target, module_sp.get(), 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00003116 result.SetStatus (eReturnStatusSuccessFinishResult);
3117 }
3118 else
3119 {
Jim Inghambb04be12012-12-15 02:40:54 +00003120 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003121 result.SetStatus (eReturnStatusFailed);
3122 }
3123 }
3124 else
3125 {
Jim Inghambb04be12012-12-15 02:40:54 +00003126 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003127 result.SetStatus (eReturnStatusFailed);
3128 }
3129 }
3130 else
3131 {
3132 result.AppendError ("Can only look up modules by address with a valid target.");
3133 result.SetStatus (eReturnStatusFailed);
3134 }
3135 return result.Succeeded();
3136 }
3137
Greg Clayton36da2aa2013-01-25 18:06:21 +00003138 size_t num_modules = 0;
Jim Ingham93367902012-05-30 02:19:25 +00003139 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3140 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3141 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003142 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003143 const size_t argc = command.GetArgumentCount();
3144 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003145 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003146 if (use_global_module_list)
3147 {
3148 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3149 num_modules = Module::GetNumberAllocatedModules();
3150 }
3151 else
3152 {
3153 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003154 }
Greg Clayton899025f2011-08-09 00:01:09 +00003155 }
3156 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003157 {
3158 for (size_t i=0; i<argc; ++i)
3159 {
3160 // Dump specified images (by basename or fullpath)
3161 const char *arg_cstr = command.GetArgumentAtIndex(i);
3162 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3163 if (num_matches == 0)
3164 {
3165 if (argc == 1)
3166 {
3167 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3168 result.SetStatus (eReturnStatusFailed);
3169 return false;
3170 }
3171 }
3172 }
3173
Greg Clayton2ad894b2012-05-15 18:43:44 +00003174 module_list_ptr = &module_list;
3175 }
Jim Ingham93367902012-05-30 02:19:25 +00003176
3177 if (module_list_ptr != NULL)
3178 {
3179 locker.Lock(module_list_ptr->GetMutex());
3180 num_modules = module_list_ptr->GetSize();
3181 }
Greg Clayton899025f2011-08-09 00:01:09 +00003182
Greg Claytone1f50b92011-05-03 22:09:39 +00003183 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003184 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003185 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3186 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003187 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003188 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003189 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003190 {
Jim Ingham93367902012-05-30 02:19:25 +00003191 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003192 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003193 }
3194 else
3195 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003196 module = Module::GetAllocatedModuleAtIndex(image_idx);
3197 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003198 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003199
Greg Clayton36da2aa2013-01-25 18:06:21 +00003200 const size_t indent = strm.Printf("[%3u] ", image_idx);
3201 PrintModule (target, module, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003202
Greg Claytone1f50b92011-05-03 22:09:39 +00003203 }
3204 result.SetStatus (eReturnStatusSuccessFinishResult);
3205 }
3206 else
3207 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003208 if (argc)
3209 {
3210 if (use_global_module_list)
3211 result.AppendError ("the global module list has no matching modules");
3212 else
3213 result.AppendError ("the target has no matching modules");
3214 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003215 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003216 {
3217 if (use_global_module_list)
3218 result.AppendError ("the global module list is empty");
3219 else
3220 result.AppendError ("the target has no associated executable images");
3221 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003222 result.SetStatus (eReturnStatusFailed);
3223 return false;
3224 }
3225 }
3226 return result.Succeeded();
3227 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003228
3229 void
Greg Clayton36da2aa2013-01-25 18:06:21 +00003230 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003231 {
3232
Jim Ingham6f01c932012-10-12 17:34:26 +00003233 if (module == NULL)
3234 {
3235 strm.PutCString("Null module");
3236 return;
3237 }
3238
Jim Ingham6bdea822011-10-24 18:36:33 +00003239 bool dump_object_name = false;
3240 if (m_options.m_format_array.empty())
3241 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003242 m_options.m_format_array.push_back(std::make_pair('u', 0));
3243 m_options.m_format_array.push_back(std::make_pair('h', 0));
3244 m_options.m_format_array.push_back(std::make_pair('f', 0));
3245 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003246 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003247 const size_t num_entries = m_options.m_format_array.size();
3248 bool print_space = false;
3249 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003250 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003251 if (print_space)
3252 strm.PutChar(' ');
3253 print_space = true;
3254 const char format_char = m_options.m_format_array[i].first;
3255 uint32_t width = m_options.m_format_array[i].second;
3256 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003257 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003258 case 'A':
3259 DumpModuleArchitecture (strm, module, false, width);
3260 break;
3261
3262 case 't':
3263 DumpModuleArchitecture (strm, module, true, width);
3264 break;
3265
3266 case 'f':
3267 DumpFullpath (strm, &module->GetFileSpec(), width);
3268 dump_object_name = true;
3269 break;
3270
3271 case 'd':
3272 DumpDirectory (strm, &module->GetFileSpec(), width);
3273 break;
3274
3275 case 'b':
3276 DumpBasename (strm, &module->GetFileSpec(), width);
3277 dump_object_name = true;
3278 break;
3279
3280 case 'h':
3281 case 'o':
3282 // Image header address
3283 {
3284 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003285
Greg Claytonb5a8f142012-02-05 02:38:54 +00003286 ObjectFile *objfile = module->GetObjectFile ();
3287 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003288 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003289 Address header_addr(objfile->GetHeaderAddress());
3290 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003291 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003292 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003293 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003294 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3295 if (header_load_addr == LLDB_INVALID_ADDRESS)
3296 {
3297 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3298 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003299 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003300 {
3301 if (format_char == 'o')
3302 {
3303 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003304 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003305 }
3306 else
3307 {
3308 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003309 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003310 }
3311 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003312 break;
3313 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003314 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3315 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3316 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003317 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003318 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003319 strm.Printf ("%*s", addr_nibble_width + 2, "");
3320 }
3321 break;
3322 case 'r':
3323 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003324 size_t ref_count = 0;
Greg Claytonb5a8f142012-02-05 02:38:54 +00003325 ModuleSP module_sp (module->shared_from_this());
3326 if (module_sp)
3327 {
3328 // Take one away to make sure we don't count our local "module_sp"
3329 ref_count = module_sp.use_count() - 1;
3330 }
3331 if (width)
Greg Clayton36da2aa2013-01-25 18:06:21 +00003332 strm.Printf("{%*zu}", width, ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003333 else
Greg Clayton36da2aa2013-01-25 18:06:21 +00003334 strm.Printf("{%zu}", ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003335 }
3336 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003337
Greg Claytonb5a8f142012-02-05 02:38:54 +00003338 case 's':
3339 case 'S':
3340 {
3341 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3342 if (symbol_vendor)
3343 {
3344 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3345 if (symbol_file)
3346 {
3347 if (format_char == 'S')
3348 {
3349 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3350 // Dump symbol file only if different from module file
3351 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3352 {
3353 print_space = false;
3354 break;
3355 }
3356 // Add a newline and indent past the index
3357 strm.Printf ("\n%*s", indent, "");
3358 }
3359 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3360 dump_object_name = true;
3361 break;
3362 }
3363 }
3364 strm.Printf("%.*s", width, "<NONE>");
3365 }
3366 break;
3367
3368 case 'm':
3369 module->GetModificationTime().Dump(&strm, width);
3370 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003371
Greg Claytonb5a8f142012-02-05 02:38:54 +00003372 case 'p':
3373 strm.Printf("%p", module);
3374 break;
3375
3376 case 'u':
3377 DumpModuleUUID(strm, module);
3378 break;
3379
3380 default:
3381 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003382 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003383
3384 }
3385 if (dump_object_name)
3386 {
3387 const char *object_name = module->GetObjectName().GetCString();
3388 if (object_name)
3389 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003390 }
3391 strm.EOL();
3392 }
3393
Greg Claytone1f50b92011-05-03 22:09:39 +00003394 CommandOptions m_options;
3395};
3396
3397OptionDefinition
3398CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3399{
Enrico Granata4968ad82013-01-29 01:48:30 +00003400 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
Jim Ingham6bdea822011-10-24 18:36:33 +00003401 { 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 +00003402 { 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 +00003403 { 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."},
3404 { 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 +00003405 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3406 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3407 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3408 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3409 { 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 +00003410 { 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 +00003411 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3412 { 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."},
3413 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003414 { 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 +00003415 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3416};
3417
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003418#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003419
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003420//----------------------------------------------------------------------
3421// Lookup unwind information in images
3422//----------------------------------------------------------------------
3423
3424class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3425{
3426public:
3427
3428 enum
3429 {
3430 eLookupTypeInvalid = -1,
3431 eLookupTypeAddress = 0,
3432 eLookupTypeSymbol,
3433 eLookupTypeFunction,
3434 eLookupTypeFunctionOrSymbol,
3435 kNumLookupTypes
3436 };
3437
3438 class CommandOptions : public Options
3439 {
3440 public:
3441
3442 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003443 Options(interpreter),
3444 m_type(eLookupTypeInvalid),
3445 m_str(),
3446 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003447 {
3448 }
3449
3450 virtual
3451 ~CommandOptions ()
3452 {
3453 }
3454
3455 virtual Error
3456 SetOptionValue (uint32_t option_idx, const char *option_arg)
3457 {
3458 Error error;
3459
Greg Clayton6475c422012-12-04 00:32:51 +00003460 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003461
3462 switch (short_option)
3463 {
3464 case 'a':
Jason Molenda5a559062013-04-23 04:30:57 +00003465 {
3466 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003467 m_type = eLookupTypeAddress;
Jason Molenda5a559062013-04-23 04:30:57 +00003468 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003469 if (m_addr == LLDB_INVALID_ADDRESS)
3470 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3471 break;
Jason Molenda5a559062013-04-23 04:30:57 +00003472 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003473
3474 case 'n':
Jason Molenda5a559062013-04-23 04:30:57 +00003475 {
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003476 m_str = option_arg;
3477 m_type = eLookupTypeFunctionOrSymbol;
3478 break;
Jason Molenda5a559062013-04-23 04:30:57 +00003479 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003480 }
3481
3482 return error;
3483 }
3484
3485 void
3486 OptionParsingStarting ()
3487 {
3488 m_type = eLookupTypeInvalid;
3489 m_str.clear();
3490 m_addr = LLDB_INVALID_ADDRESS;
3491 }
3492
3493 const OptionDefinition*
3494 GetDefinitions ()
3495 {
3496 return g_option_table;
3497 }
3498
3499 // Options table: Required for subclasses of Options.
3500
3501 static OptionDefinition g_option_table[];
3502
3503 // Instance variables to hold the values for command options.
3504
3505 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3506 std::string m_str; // Holds name lookup
3507 lldb::addr_t m_addr; // Holds the address to lookup
3508 };
3509
3510 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3511 CommandObjectParsed (interpreter,
3512 "target modules show-unwind",
3513 "Show synthesized unwind instructions for a function.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003514 NULL,
3515 eFlagRequiresTarget |
3516 eFlagRequiresProcess |
3517 eFlagProcessMustBeLaunched |
3518 eFlagProcessMustBePaused ),
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003519 m_options (interpreter)
3520 {
3521 }
3522
3523 virtual
3524 ~CommandObjectTargetModulesShowUnwind ()
3525 {
3526 }
3527
3528 virtual
3529 Options *
3530 GetOptions ()
3531 {
3532 return &m_options;
3533 }
3534
3535protected:
3536 bool
3537 DoExecute (Args& command,
3538 CommandReturnObject &result)
3539 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003540 Target *target = m_exe_ctx.GetTargetPtr();
3541 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003542 ABI *abi = NULL;
3543 if (process)
3544 abi = process->GetABI().get();
3545
3546 if (process == NULL)
3547 {
3548 result.AppendError ("You must have a process running to use this command.");
3549 result.SetStatus (eReturnStatusFailed);
3550 return false;
3551 }
3552
3553 ThreadList threads(process->GetThreadList());
3554 if (threads.GetSize() == 0)
3555 {
3556 result.AppendError ("The process must be paused to use this command.");
3557 result.SetStatus (eReturnStatusFailed);
3558 return false;
3559 }
3560
3561 ThreadSP thread(threads.GetThreadAtIndex(0));
3562 if (thread.get() == NULL)
3563 {
3564 result.AppendError ("The process must be paused to use this command.");
3565 result.SetStatus (eReturnStatusFailed);
3566 return false;
3567 }
3568
Jason Molenda5a559062013-04-23 04:30:57 +00003569 SymbolContextList sc_list;
3570
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003571 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3572 {
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003573 ConstString function_name (m_options.m_str.c_str());
Jason Molenda5a559062013-04-23 04:30:57 +00003574 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3575 }
3576 else if (m_options.m_type == eLookupTypeAddress && target)
3577 {
3578 Address addr;
3579 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003580 {
3581 SymbolContext sc;
Jason Molenda5a559062013-04-23 04:30:57 +00003582 ModuleSP module_sp (addr.GetModule());
3583 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3584 if (sc.function || sc.symbol)
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003585 {
Jason Molenda5a559062013-04-23 04:30:57 +00003586 sc_list.Append(sc);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003587 }
Jason Molenda5a559062013-04-23 04:30:57 +00003588 }
3589 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003590
Jason Molenda5a559062013-04-23 04:30:57 +00003591 size_t num_matches = sc_list.GetSize();
3592 for (uint32_t idx = 0; idx < num_matches; idx++)
3593 {
3594 SymbolContext sc;
3595 sc_list.GetContextAtIndex(idx, sc);
3596 if (sc.symbol == NULL && sc.function == NULL)
3597 continue;
3598 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3599 continue;
3600 AddressRange range;
3601 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3602 continue;
3603 if (!range.GetBaseAddress().IsValid())
3604 continue;
3605 ConstString funcname(sc.GetFunctionName());
3606 if (funcname.IsEmpty())
3607 continue;
3608 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3609 if (abi)
3610 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003611
Jason Molenda5a559062013-04-23 04:30:57 +00003612 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3613 if (func_unwinders_sp.get() == NULL)
3614 continue;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003615
Jason Molenda5a559062013-04-23 04:30:57 +00003616 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3617 if (first_non_prologue_insn.IsValid())
3618 {
3619 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 +00003620 result.GetOutputStream().Printf ("\n");
3621 }
Jason Molenda5a559062013-04-23 04:30:57 +00003622
3623 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3624 if (non_callsite_unwind_plan.get())
3625 {
3626 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);
3627 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3628 result.GetOutputStream().Printf ("\n");
3629 }
3630
3631 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3632 if (callsite_unwind_plan.get())
3633 {
3634 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);
3635 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3636 result.GetOutputStream().Printf ("\n");
3637 }
3638
3639 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3640 if (arch_default_unwind_plan.get())
3641 {
3642 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3643 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3644 result.GetOutputStream().Printf ("\n");
3645 }
3646
3647 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3648 if (fast_unwind_plan.get())
3649 {
3650 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3651 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3652 result.GetOutputStream().Printf ("\n");
3653 }
3654
3655
3656 result.GetOutputStream().Printf ("\n");
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003657 }
3658 return result.Succeeded();
3659 }
3660
3661 CommandOptions m_options;
3662};
3663
3664OptionDefinition
3665CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3666{
Jason Molenda5a559062013-04-23 04:30:57 +00003667 { LLDB_OPT_SET_1, false, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3668 { LLDB_OPT_SET_2, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003669 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3670};
Greg Claytone1f50b92011-05-03 22:09:39 +00003671
3672//----------------------------------------------------------------------
3673// Lookup information in images
3674//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003675class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003676{
3677public:
3678
3679 enum
3680 {
3681 eLookupTypeInvalid = -1,
3682 eLookupTypeAddress = 0,
3683 eLookupTypeSymbol,
3684 eLookupTypeFileLine, // Line is optional
3685 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003686 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003687 eLookupTypeType,
3688 kNumLookupTypes
3689 };
3690
3691 class CommandOptions : public Options
3692 {
3693 public:
3694
3695 CommandOptions (CommandInterpreter &interpreter) :
3696 Options(interpreter)
3697 {
3698 OptionParsingStarting();
3699 }
3700
3701 virtual
3702 ~CommandOptions ()
3703 {
3704 }
3705
3706 virtual Error
3707 SetOptionValue (uint32_t option_idx, const char *option_arg)
3708 {
3709 Error error;
3710
Greg Clayton6475c422012-12-04 00:32:51 +00003711 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone1f50b92011-05-03 22:09:39 +00003712
3713 switch (short_option)
3714 {
3715 case 'a':
Jim Inghamd86cf812013-03-15 23:09:19 +00003716 {
3717 m_type = eLookupTypeAddress;
3718 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3719 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3720 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003721 break;
3722
3723 case 'o':
3724 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3725 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003726 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003727 break;
3728
3729 case 's':
3730 m_str = option_arg;
3731 m_type = eLookupTypeSymbol;
3732 break;
3733
3734 case 'f':
3735 m_file.SetFile (option_arg, false);
3736 m_type = eLookupTypeFileLine;
3737 break;
3738
3739 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003740 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003741 break;
3742
3743 case 'l':
3744 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3745 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003746 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003747 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003748 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003749 m_type = eLookupTypeFileLine;
3750 break;
3751
Greg Clayton2ad894b2012-05-15 18:43:44 +00003752 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003753 m_str = option_arg;
3754 m_type = eLookupTypeFunction;
3755 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003756
3757 case 'n':
3758 m_str = option_arg;
3759 m_type = eLookupTypeFunctionOrSymbol;
3760 break;
3761
Greg Claytone1f50b92011-05-03 22:09:39 +00003762 case 't':
3763 m_str = option_arg;
3764 m_type = eLookupTypeType;
3765 break;
3766
3767 case 'v':
3768 m_verbose = 1;
3769 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003770
3771 case 'A':
3772 m_print_all = true;
3773 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003774
3775 case 'r':
3776 m_use_regex = true;
3777 break;
3778 }
3779
3780 return error;
3781 }
3782
3783 void
3784 OptionParsingStarting ()
3785 {
3786 m_type = eLookupTypeInvalid;
3787 m_str.clear();
3788 m_file.Clear();
3789 m_addr = LLDB_INVALID_ADDRESS;
3790 m_offset = 0;
3791 m_line_number = 0;
3792 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003793 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003794 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003795 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003796 }
3797
3798 const OptionDefinition*
3799 GetDefinitions ()
3800 {
3801 return g_option_table;
3802 }
3803
3804 // Options table: Required for subclasses of Options.
3805
3806 static OptionDefinition g_option_table[];
3807 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3808 std::string m_str; // Holds name lookup
3809 FileSpec m_file; // Files for file lookups
3810 lldb::addr_t m_addr; // Holds the address to lookup
3811 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3812 uint32_t m_line_number; // Line number for file+line lookups
3813 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003814 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003815 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003816 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003817
3818 };
3819
3820 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003821 CommandObjectParsed (interpreter,
3822 "target modules lookup",
3823 "Look up information within executable and dependent shared library images.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003824 NULL,
3825 eFlagRequiresTarget),
Jim Inghamda26bd22012-06-08 21:56:10 +00003826 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003827 {
3828 CommandArgumentEntry arg;
3829 CommandArgumentData file_arg;
3830
3831 // Define the first (and only) variant of this arg.
3832 file_arg.arg_type = eArgTypeFilename;
3833 file_arg.arg_repetition = eArgRepeatStar;
3834
3835 // There is only one variant this argument could be; put it into the argument entry.
3836 arg.push_back (file_arg);
3837
3838 // Push the data for the first argument into the m_arguments vector.
3839 m_arguments.push_back (arg);
3840 }
3841
3842 virtual
3843 ~CommandObjectTargetModulesLookup ()
3844 {
3845 }
3846
3847 virtual Options *
3848 GetOptions ()
3849 {
3850 return &m_options;
3851 }
3852
Sean Callanan56d31ec2012-06-06 20:49:55 +00003853 bool
3854 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3855 {
3856 switch (m_options.m_type)
3857 {
3858 case eLookupTypeAddress:
3859 case eLookupTypeFileLine:
3860 case eLookupTypeFunction:
3861 case eLookupTypeFunctionOrSymbol:
3862 case eLookupTypeSymbol:
3863 default:
3864 return false;
3865 case eLookupTypeType:
3866 break;
3867 }
3868
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003869 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Sean Callanan56d31ec2012-06-06 20:49:55 +00003870
3871 if (!frame)
3872 return false;
3873
3874 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3875
3876 if (!sym_ctx.module_sp)
3877 return false;
3878
3879 switch (m_options.m_type)
3880 {
3881 default:
3882 return false;
3883 case eLookupTypeType:
3884 if (!m_options.m_str.empty())
3885 {
3886 if (LookupTypeHere (m_interpreter,
3887 result.GetOutputStream(),
3888 sym_ctx,
3889 m_options.m_str.c_str(),
3890 m_options.m_use_regex))
3891 {
3892 result.SetStatus(eReturnStatusSuccessFinishResult);
3893 return true;
3894 }
3895 }
3896 break;
3897 }
3898
3899 return true;
3900 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003901
3902 bool
3903 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3904 {
3905 switch (m_options.m_type)
3906 {
3907 case eLookupTypeAddress:
3908 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3909 {
3910 if (LookupAddressInModule (m_interpreter,
3911 result.GetOutputStream(),
3912 module,
3913 eSymbolContextEverything,
3914 m_options.m_addr,
3915 m_options.m_offset,
3916 m_options.m_verbose))
3917 {
3918 result.SetStatus(eReturnStatusSuccessFinishResult);
3919 return true;
3920 }
3921 }
3922 break;
3923
3924 case eLookupTypeSymbol:
3925 if (!m_options.m_str.empty())
3926 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003927 if (LookupSymbolInModule (m_interpreter,
3928 result.GetOutputStream(),
3929 module,
3930 m_options.m_str.c_str(),
3931 m_options.m_use_regex,
3932 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003933 {
3934 result.SetStatus(eReturnStatusSuccessFinishResult);
3935 return true;
3936 }
3937 }
3938 break;
3939
3940 case eLookupTypeFileLine:
3941 if (m_options.m_file)
3942 {
3943
3944 if (LookupFileAndLineInModule (m_interpreter,
3945 result.GetOutputStream(),
3946 module,
3947 m_options.m_file,
3948 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003949 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003950 m_options.m_verbose))
3951 {
3952 result.SetStatus(eReturnStatusSuccessFinishResult);
3953 return true;
3954 }
3955 }
3956 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003957
3958 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003959 case eLookupTypeFunction:
3960 if (!m_options.m_str.empty())
3961 {
3962 if (LookupFunctionInModule (m_interpreter,
3963 result.GetOutputStream(),
3964 module,
3965 m_options.m_str.c_str(),
3966 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003967 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003968 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003969 m_options.m_verbose))
3970 {
3971 result.SetStatus(eReturnStatusSuccessFinishResult);
3972 return true;
3973 }
3974 }
3975 break;
3976
Greg Clayton2ad894b2012-05-15 18:43:44 +00003977
Greg Claytone1f50b92011-05-03 22:09:39 +00003978 case eLookupTypeType:
3979 if (!m_options.m_str.empty())
3980 {
3981 if (LookupTypeInModule (m_interpreter,
3982 result.GetOutputStream(),
3983 module,
3984 m_options.m_str.c_str(),
3985 m_options.m_use_regex))
3986 {
3987 result.SetStatus(eReturnStatusSuccessFinishResult);
3988 return true;
3989 }
3990 }
3991 break;
3992
3993 default:
3994 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3995 syntax_error = true;
3996 break;
3997 }
3998
3999 result.SetStatus (eReturnStatusFailed);
4000 return false;
4001 }
4002
Jim Inghamda26bd22012-06-08 21:56:10 +00004003protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00004004 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004005 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00004006 CommandReturnObject &result)
4007 {
4008 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4009 if (target == NULL)
4010 {
4011 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4012 result.SetStatus (eReturnStatusFailed);
4013 return false;
4014 }
4015 else
4016 {
4017 bool syntax_error = false;
4018 uint32_t i;
4019 uint32_t num_successful_lookups = 0;
4020 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4021 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4022 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4023 // Dump all sections for all modules images
4024
4025 if (command.GetArgumentCount() == 0)
4026 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004027 ModuleSP current_module;
4028
4029 // Where it is possible to look in the current symbol context
4030 // first, try that. If this search was successful and --all
4031 // was not passed, don't print anything else.
4032 if (LookupHere (m_interpreter, result, syntax_error))
4033 {
4034 result.GetOutputStream().EOL();
4035 num_successful_lookups++;
4036 if (!m_options.m_print_all)
4037 {
4038 result.SetStatus (eReturnStatusSuccessFinishResult);
4039 return result.Succeeded();
4040 }
4041 }
4042
4043 // Dump all sections for all other modules
4044
Enrico Granata146d9522012-11-08 02:22:02 +00004045 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00004046 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00004047 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00004048 if (num_modules > 0)
4049 {
4050 for (i = 0; i<num_modules && syntax_error == false; ++i)
4051 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004052 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
4053
4054 if (module_pointer != current_module.get() &&
4055 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004056 {
4057 result.GetOutputStream().EOL();
4058 num_successful_lookups++;
4059 }
4060 }
4061 }
4062 else
4063 {
4064 result.AppendError ("the target has no associated executable images");
4065 result.SetStatus (eReturnStatusFailed);
4066 return false;
4067 }
4068 }
4069 else
4070 {
4071 // Dump specified images (by basename or fullpath)
4072 const char *arg_cstr;
4073 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4074 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004075 ModuleList module_list;
4076 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4077 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00004078 {
Jason Molendabf41e192012-10-04 22:47:07 +00004079 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00004080 {
Jason Molendabf41e192012-10-04 22:47:07 +00004081 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00004082 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00004083 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004084 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004085 {
4086 result.GetOutputStream().EOL();
4087 num_successful_lookups++;
4088 }
4089 }
4090 }
4091 }
4092 else
4093 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4094 }
4095 }
4096
4097 if (num_successful_lookups > 0)
4098 result.SetStatus (eReturnStatusSuccessFinishResult);
4099 else
4100 result.SetStatus (eReturnStatusFailed);
4101 }
4102 return result.Succeeded();
4103 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004104
4105 CommandOptions m_options;
4106};
4107
4108OptionDefinition
4109CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4110{
Jim Inghamd86cf812013-03-15 23:09:19 +00004111 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
Sean Callanan3bfaad62012-09-13 21:11:40 +00004112 { 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 +00004113 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4114 /* 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 +00004115 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4116 { 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."},
4117 { 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."},
4118 { 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 +00004119 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00004120 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4121 { 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."},
4122 { 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."},
4123 { 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."},
4124 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4125 { 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."},
4126 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00004127};
Chris Lattner24943d22010-06-08 16:52:24 +00004128
4129
Jim Inghamd60d94a2011-03-11 03:53:59 +00004130#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00004131
4132//-------------------------------------------------------------------------
4133// CommandObjectMultiwordImageSearchPaths
4134//-------------------------------------------------------------------------
4135
Greg Claytone1f50b92011-05-03 22:09:39 +00004136class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00004137{
4138public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004139
4140 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4141 CommandObjectMultiword (interpreter,
4142 "target modules search-paths",
4143 "A set of commands for operating on debugger target image search paths.",
4144 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004145 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004146 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4147 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4148 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4149 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4150 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004151 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004152
4153 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004154 {
4155 }
4156};
4157
Greg Claytone1f50b92011-05-03 22:09:39 +00004158
4159
4160#pragma mark CommandObjectTargetModules
4161
4162//-------------------------------------------------------------------------
4163// CommandObjectTargetModules
4164//-------------------------------------------------------------------------
4165
4166class CommandObjectTargetModules : public CommandObjectMultiword
4167{
4168public:
4169 //------------------------------------------------------------------
4170 // Constructors and Destructors
4171 //------------------------------------------------------------------
4172 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4173 CommandObjectMultiword (interpreter,
4174 "target modules",
4175 "A set of commands for accessing information for one or more target modules.",
4176 "target modules <sub-command> ...")
4177 {
4178 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4179 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004180 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4181 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4182 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4183 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004184 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004185
4186 }
4187 virtual
4188 ~CommandObjectTargetModules()
4189 {
4190 }
4191
4192private:
4193 //------------------------------------------------------------------
4194 // For CommandObjectTargetModules only
4195 //------------------------------------------------------------------
4196 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4197};
4198
4199
Greg Clayton3508c382012-02-24 01:59:29 +00004200
Jim Inghamda26bd22012-06-08 21:56:10 +00004201class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004202{
4203public:
4204 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004205 CommandObjectParsed (interpreter,
4206 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004207 "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.",
Jason Molenda1ab639c2013-02-02 06:00:36 +00004208 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Clayton437b5bc2012-09-27 22:26:11 +00004209 m_option_group (interpreter),
4210 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4211 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4212
Greg Clayton3508c382012-02-24 01:59:29 +00004213 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004214 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4215 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4216 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4217 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004218 }
4219
4220 virtual
4221 ~CommandObjectTargetSymbolsAdd ()
4222 {
4223 }
4224
Greg Clayton36da2aa2013-01-25 18:06:21 +00004225 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00004226 HandleArgumentCompletion (Args &input,
4227 int &cursor_index,
4228 int &cursor_char_position,
4229 OptionElementVector &opt_element_vector,
4230 int match_start_point,
4231 int max_return_elements,
4232 bool &word_complete,
4233 StringList &matches)
4234 {
4235 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4236 completion_str.erase (cursor_char_position);
4237
4238 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4239 CommandCompletions::eDiskFileCompletion,
4240 completion_str.c_str(),
4241 match_start_point,
4242 max_return_elements,
4243 NULL,
4244 word_complete,
4245 matches);
4246 return matches.GetSize();
4247 }
4248
Greg Clayton437b5bc2012-09-27 22:26:11 +00004249 virtual Options *
4250 GetOptions ()
4251 {
4252 return &m_option_group;
4253 }
4254
4255
Jim Inghamda26bd22012-06-08 21:56:10 +00004256protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004257
4258 bool
4259 AddModuleSymbols (Target *target,
Greg Claytonc92fded2012-12-12 01:15:30 +00004260 ModuleSpec &module_spec,
Greg Clayton437b5bc2012-09-27 22:26:11 +00004261 bool &flush,
4262 CommandReturnObject &result)
4263 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004264 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4265 if (symbol_fspec)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004266 {
4267 char symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004268 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
4269
4270 if (!module_spec.GetUUID().IsValid())
4271 {
4272 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4273 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4274 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004275 // We now have a module that represents a symbol file
4276 // that can be used for a module that might exist in the
4277 // current target, so we need to find that module in the
4278 // target
Greg Claytonc92fded2012-12-12 01:15:30 +00004279 ModuleList matching_module_list;
Greg Claytond9735a12013-01-11 23:44:27 +00004280 size_t num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4281 while (num_matches == 0)
4282 {
4283 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4284 // Empty string returned, lets bail
4285 if (!filename_no_extension)
4286 break;
4287
4288 // Check if there was no extension to strip and the basename is the same
4289 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4290 break;
4291
4292 // Replace basename with one less extension
4293 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4294
4295 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4296 }
4297
Greg Claytonc92fded2012-12-12 01:15:30 +00004298 if (num_matches > 1)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004299 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004300 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4301 }
4302 else if (num_matches == 1)
4303 {
4304 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
4305
Greg Clayton437b5bc2012-09-27 22:26:11 +00004306 // The module has not yet created its symbol vendor, we can just
4307 // give the existing target module the symfile path to use for
4308 // when it decides to create it!
Greg Claytonc92fded2012-12-12 01:15:30 +00004309 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004310
Greg Clayton18809182012-12-14 02:15:00 +00004311 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Claytonc92fded2012-12-12 01:15:30 +00004312 if (symbol_vendor)
4313 {
4314 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4315
4316 if (symbol_file)
4317 {
4318 ObjectFile *object_file = symbol_file->GetObjectFile();
4319
4320 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4321 {
4322 // Provide feedback that the symfile has been successfully added.
4323 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Clayton97a19b22013-04-29 17:25:54 +00004324 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Claytonc92fded2012-12-12 01:15:30 +00004325 symfile_path,
Greg Clayton97a19b22013-04-29 17:25:54 +00004326 module_fs.GetPath().c_str());
Greg Claytonc92fded2012-12-12 01:15:30 +00004327
4328 // Let clients know something changed in the module
4329 // if it is currently loaded
4330 ModuleList module_list;
4331 module_list.Append (module_sp);
Enrico Granata12fbcf52013-04-05 18:49:06 +00004332 target->SymbolsDidLoad (module_list);
Greg Claytond9735a12013-01-11 23:44:27 +00004333
4334 // Make sure we load any scripting resources that may be embedded
4335 // in the debug info files in case the platform supports that.
4336 Error error;
4337 module_sp->LoadScriptingResourceInTarget (target, error);
4338
Greg Claytonc92fded2012-12-12 01:15:30 +00004339 flush = true;
4340 result.SetStatus (eReturnStatusSuccessFinishResult);
4341 return true;
4342 }
4343 }
4344 }
4345 // Clear the symbol file spec if anything went wrong
4346 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Claytonc92fded2012-12-12 01:15:30 +00004347 }
4348
4349 if (module_spec.GetUUID().IsValid())
4350 {
4351 StreamString ss_symfile_uuid;
4352 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4353 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4354 symfile_path,
4355 ss_symfile_uuid.GetData(),
4356 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4357 ? "\n please specify the full path to the symbol file"
4358 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004359 }
4360 else
4361 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004362 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4363 symfile_path,
4364 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4365 ? "\n please specify the full path to the symbol file"
4366 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004367 }
4368 }
4369 else
4370 {
4371 result.AppendError ("one or more executable image paths must be specified");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004372 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004373 result.SetStatus (eReturnStatusFailed);
4374 return false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004375 }
4376
Greg Clayton3508c382012-02-24 01:59:29 +00004377 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004378 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004379 CommandReturnObject &result)
4380 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004381 Target *target = m_exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004382 result.SetStatus (eReturnStatusFailed);
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004383 bool flush = false;
4384 ModuleSpec module_spec;
4385 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4386 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4387 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004388
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004389 const size_t argc = args.GetArgumentCount();
4390 if (argc == 0)
4391 {
4392 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004393 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004394 bool success = false;
4395 bool error_set = false;
4396 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004397 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004398 Process *process = m_exe_ctx.GetProcessPtr();
4399 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004400 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004401 const StateType process_state = process->GetState();
4402 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004403 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004404 StackFrame *frame = m_exe_ctx.GetFramePtr();
4405 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004406 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004407 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4408 if (frame_module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004409 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004410 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Clayton437b5bc2012-09-27 22:26:11 +00004411 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004412 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4413 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004414 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004415 module_spec.GetUUID() = frame_module_sp->GetUUID();
4416 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Clayton3508c382012-02-24 01:59:29 +00004417 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004418 else
4419 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004420 result.AppendError ("frame has no module");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004421 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004422 }
Greg Clayton3508c382012-02-24 01:59:29 +00004423 }
4424 else
4425 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004426 result.AppendError ("invalid current frame");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004427 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004428 }
Greg Clayton3508c382012-02-24 01:59:29 +00004429 }
4430 else
4431 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004432 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Clayton437b5bc2012-09-27 22:26:11 +00004433 error_set = true;
4434 }
4435 }
4436 else
4437 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004438 result.AppendError ("a process must exist in order to use the --frame option");
4439 error_set = true;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004440 }
4441 }
4442 else
4443 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004444 if (uuid_option_set)
4445 {
4446 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4447 success |= module_spec.GetUUID().IsValid();
4448 }
4449 else if (file_option_set)
4450 {
4451 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4452 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4453 if (module_sp)
4454 {
4455 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4456 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4457 module_spec.GetUUID() = module_sp->GetUUID();
4458 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4459 }
4460 else
4461 {
4462 module_spec.GetArchitecture() = target->GetArchitecture();
4463 }
4464 success |= module_spec.GetFileSpec().Exists();
4465 }
4466 }
4467
4468 if (success)
4469 {
4470 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4471 {
4472 if (module_spec.GetSymbolFileSpec())
4473 success = AddModuleSymbols (target, module_spec, flush, result);
4474 }
4475 }
4476
4477 if (!success && !error_set)
4478 {
4479 StreamString error_strm;
4480 if (uuid_option_set)
4481 {
4482 error_strm.PutCString("unable to find debug symbols for UUID ");
4483 module_spec.GetUUID().Dump (&error_strm);
4484 }
4485 else if (file_option_set)
4486 {
4487 error_strm.PutCString("unable to find debug symbols for the executable file ");
4488 error_strm << module_spec.GetFileSpec();
4489 }
4490 else if (frame_option_set)
4491 {
4492 error_strm.PutCString("unable to find debug symbols for the current frame");
4493 }
4494 result.AppendError (error_strm.GetData());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004495 }
4496 }
4497 else
4498 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004499 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4500 }
4501 }
4502 else
4503 {
4504 if (uuid_option_set)
4505 {
4506 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4507 }
4508 else if (file_option_set)
4509 {
4510 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4511 }
4512 else if (frame_option_set)
4513 {
4514 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4515 }
4516 else
4517 {
4518 PlatformSP platform_sp (target->GetPlatform());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004519
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004520 for (size_t i=0; i<argc; ++i)
4521 {
4522 const char *symfile_path = args.GetArgumentAtIndex(i);
4523 if (symfile_path)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004524 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004525 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4526 if (platform_sp)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004527 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004528 FileSpec symfile_spec;
4529 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4530 module_spec.GetSymbolFileSpec() = symfile_spec;
4531 }
4532
4533 ArchSpec arch;
4534 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004535
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004536 if (symfile_exists)
4537 {
4538 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004539 break;
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004540 }
4541 else
4542 {
4543 char resolved_symfile_path[PATH_MAX];
4544 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4545 {
4546 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4547 {
4548 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4549 break;
4550 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004551 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004552 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4553 break;
Greg Clayton3508c382012-02-24 01:59:29 +00004554 }
4555 }
4556 }
4557 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004558 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004559
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004560 if (flush)
4561 {
4562 Process *process = m_exe_ctx.GetProcessPtr();
4563 if (process)
4564 process->Flush();
Greg Clayton3508c382012-02-24 01:59:29 +00004565 }
4566 return result.Succeeded();
4567 }
4568
Greg Clayton437b5bc2012-09-27 22:26:11 +00004569 OptionGroupOptions m_option_group;
4570 OptionGroupUUID m_uuid_option_group;
4571 OptionGroupFile m_file_option;
4572 OptionGroupBoolean m_current_frame_option;
4573
4574
Greg Clayton3508c382012-02-24 01:59:29 +00004575};
4576
4577
4578#pragma mark CommandObjectTargetSymbols
4579
4580//-------------------------------------------------------------------------
4581// CommandObjectTargetSymbols
4582//-------------------------------------------------------------------------
4583
4584class CommandObjectTargetSymbols : public CommandObjectMultiword
4585{
4586public:
4587 //------------------------------------------------------------------
4588 // Constructors and Destructors
4589 //------------------------------------------------------------------
4590 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4591 CommandObjectMultiword (interpreter,
4592 "target symbols",
4593 "A set of commands for adding and managing debug symbol files.",
4594 "target symbols <sub-command> ...")
4595 {
4596 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4597
4598 }
4599 virtual
4600 ~CommandObjectTargetSymbols()
4601 {
4602 }
4603
4604private:
4605 //------------------------------------------------------------------
4606 // For CommandObjectTargetModules only
4607 //------------------------------------------------------------------
4608 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4609};
4610
4611
Jim Inghamd60d94a2011-03-11 03:53:59 +00004612#pragma mark CommandObjectTargetStopHookAdd
4613
4614//-------------------------------------------------------------------------
4615// CommandObjectTargetStopHookAdd
4616//-------------------------------------------------------------------------
4617
Jim Inghamda26bd22012-06-08 21:56:10 +00004618class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004619{
4620public:
4621
4622 class CommandOptions : public Options
4623 {
4624 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004625 CommandOptions (CommandInterpreter &interpreter) :
4626 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004627 m_line_start(0),
4628 m_line_end (UINT_MAX),
4629 m_func_name_type_mask (eFunctionNameTypeAuto),
4630 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004631 m_thread_specified (false),
4632 m_use_one_liner (false),
4633 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004634 {
4635 }
4636
4637 ~CommandOptions () {}
4638
Greg Claytonb3448432011-03-24 21:19:54 +00004639 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004640 GetDefinitions ()
4641 {
4642 return g_option_table;
4643 }
4644
4645 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004646 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004647 {
4648 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00004649 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004650 bool success;
4651
4652 switch (short_option)
4653 {
4654 case 'c':
4655 m_class_name = option_arg;
4656 m_sym_ctx_specified = true;
4657 break;
4658
4659 case 'e':
4660 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4661 if (!success)
4662 {
Greg Clayton9c236732011-10-26 00:56:27 +00004663 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004664 break;
4665 }
4666 m_sym_ctx_specified = true;
4667 break;
4668
4669 case 'l':
4670 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4671 if (!success)
4672 {
Greg Clayton9c236732011-10-26 00:56:27 +00004673 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004674 break;
4675 }
4676 m_sym_ctx_specified = true;
4677 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004678
4679 case 'i':
4680 m_no_inlines = true;
4681 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004682
4683 case 'n':
4684 m_function_name = option_arg;
4685 m_func_name_type_mask |= eFunctionNameTypeAuto;
4686 m_sym_ctx_specified = true;
4687 break;
4688
4689 case 'f':
4690 m_file_name = option_arg;
4691 m_sym_ctx_specified = true;
4692 break;
4693 case 's':
4694 m_module_name = option_arg;
4695 m_sym_ctx_specified = true;
4696 break;
4697 case 't' :
4698 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004699 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004700 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004701 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004702 m_thread_specified = true;
4703 }
4704 break;
4705 case 'T':
4706 m_thread_name = option_arg;
4707 m_thread_specified = true;
4708 break;
4709 case 'q':
4710 m_queue_name = option_arg;
4711 m_thread_specified = true;
4712 break;
4713 case 'x':
4714 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004715 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004716 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004717 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004718 m_thread_specified = true;
4719 }
4720 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004721 case 'o':
4722 m_use_one_liner = true;
4723 m_one_liner = option_arg;
4724 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004725 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004726 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004727 break;
4728 }
4729 return error;
4730 }
4731
4732 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004733 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004734 {
4735 m_class_name.clear();
4736 m_function_name.clear();
4737 m_line_start = 0;
4738 m_line_end = UINT_MAX;
4739 m_file_name.clear();
4740 m_module_name.clear();
4741 m_func_name_type_mask = eFunctionNameTypeAuto;
4742 m_thread_id = LLDB_INVALID_THREAD_ID;
4743 m_thread_index = UINT32_MAX;
4744 m_thread_name.clear();
4745 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004746
4747 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004748 m_sym_ctx_specified = false;
4749 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004750
4751 m_use_one_liner = false;
4752 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004753 }
4754
4755
Greg Claytonb3448432011-03-24 21:19:54 +00004756 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004757
4758 std::string m_class_name;
4759 std::string m_function_name;
4760 uint32_t m_line_start;
4761 uint32_t m_line_end;
4762 std::string m_file_name;
4763 std::string m_module_name;
4764 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4765 lldb::tid_t m_thread_id;
4766 uint32_t m_thread_index;
4767 std::string m_thread_name;
4768 std::string m_queue_name;
4769 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004770 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004771 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004772 // Instance variables to hold the values for one_liner options.
4773 bool m_use_one_liner;
4774 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004775 };
4776
4777 Options *
4778 GetOptions ()
4779 {
4780 return &m_options;
4781 }
4782
4783 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004784 CommandObjectParsed (interpreter,
4785 "target stop-hook add ",
4786 "Add a hook to be executed when the target stops.",
4787 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004788 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004789 {
4790 }
4791
4792 ~CommandObjectTargetStopHookAdd ()
4793 {
4794 }
4795
4796 static size_t
4797 ReadCommandsCallbackFunction (void *baton,
4798 InputReader &reader,
4799 lldb::InputReaderAction notification,
4800 const char *bytes,
4801 size_t bytes_len)
4802 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004803 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004804 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004805 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004806 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004807
4808 switch (notification)
4809 {
4810 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004811 if (!batch_mode)
4812 {
4813 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4814 if (reader.GetPrompt())
4815 out_stream->Printf ("%s", reader.GetPrompt());
4816 out_stream->Flush();
4817 }
Jim Inghame15511a2011-05-05 01:03:36 +00004818 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004819 break;
4820
4821 case eInputReaderDeactivate:
4822 break;
4823
4824 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004825 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004826 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004827 out_stream->Printf ("%s", reader.GetPrompt());
4828 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004829 }
Jim Inghame15511a2011-05-05 01:03:36 +00004830 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004831 break;
4832
Caroline Tice4a348082011-05-02 20:41:46 +00004833 case eInputReaderAsynchronousOutputWritten:
4834 break;
4835
Jim Inghamd60d94a2011-03-11 03:53:59 +00004836 case eInputReaderGotToken:
4837 if (bytes && bytes_len && baton)
4838 {
4839 StringList *commands = new_stop_hook->GetCommandPointer();
4840 if (commands)
4841 {
4842 commands->AppendString (bytes, bytes_len);
4843 }
4844 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004845 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004846 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004847 out_stream->Printf ("%s", reader.GetPrompt());
4848 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004849 }
4850 break;
4851
4852 case eInputReaderInterrupt:
4853 {
4854 // Finish, and cancel the stop hook.
4855 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004856 if (!batch_mode)
4857 {
4858 out_stream->Printf ("Stop hook cancelled.\n");
4859 out_stream->Flush();
4860 }
4861
Jim Inghamd60d94a2011-03-11 03:53:59 +00004862 reader.SetIsDone (true);
4863 }
Jim Inghame15511a2011-05-05 01:03:36 +00004864 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004865 break;
4866
4867 case eInputReaderEndOfFile:
4868 reader.SetIsDone (true);
4869 break;
4870
4871 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004872 if (!got_interrupted && !batch_mode)
4873 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004874 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004875 out_stream->Flush();
4876 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004877 break;
4878 }
4879
4880 return bytes_len;
4881 }
4882
Jim Inghamda26bd22012-06-08 21:56:10 +00004883protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004884 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004885 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004886 {
4887 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4888 if (target)
4889 {
4890 Target::StopHookSP new_hook_sp;
4891 target->AddStopHook (new_hook_sp);
4892
4893 // First step, make the specifier.
Greg Clayton102b2c22013-04-18 22:45:39 +00004894 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004895 if (m_options.m_sym_ctx_specified)
4896 {
4897 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4898
4899 if (!m_options.m_module_name.empty())
4900 {
4901 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4902 }
4903
4904 if (!m_options.m_class_name.empty())
4905 {
4906 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4907 }
4908
4909 if (!m_options.m_file_name.empty())
4910 {
4911 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4912 }
4913
4914 if (m_options.m_line_start != 0)
4915 {
4916 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4917 }
4918
4919 if (m_options.m_line_end != UINT_MAX)
4920 {
4921 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4922 }
4923
4924 if (!m_options.m_function_name.empty())
4925 {
4926 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4927 }
4928 }
4929
4930 if (specifier_ap.get())
4931 new_hook_sp->SetSpecifier (specifier_ap.release());
4932
4933 // Next see if any of the thread options have been entered:
4934
4935 if (m_options.m_thread_specified)
4936 {
4937 ThreadSpec *thread_spec = new ThreadSpec();
4938
4939 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4940 {
4941 thread_spec->SetTID (m_options.m_thread_id);
4942 }
4943
4944 if (m_options.m_thread_index != UINT32_MAX)
4945 thread_spec->SetIndex (m_options.m_thread_index);
4946
4947 if (!m_options.m_thread_name.empty())
4948 thread_spec->SetName (m_options.m_thread_name.c_str());
4949
4950 if (!m_options.m_queue_name.empty())
4951 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4952
4953 new_hook_sp->SetThreadSpecifier (thread_spec);
4954
4955 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004956 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004957 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004958 // Use one-liner.
4959 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004960 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004961 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004962 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004963 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004964 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4965 // the new stop hook's command string.
4966 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4967 if (!reader_sp)
4968 {
4969 result.AppendError("out of memory\n");
4970 result.SetStatus (eReturnStatusFailed);
4971 target->RemoveStopHookByID (new_hook_sp->GetID());
4972 return false;
4973 }
4974
4975 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4976 new_hook_sp.get(), // baton
4977 eInputReaderGranularityLine, // token size, to pass to callback function
4978 "DONE", // end token
4979 "> ", // prompt
4980 true)); // echo input
4981 if (!err.Success())
4982 {
4983 result.AppendError (err.AsCString());
4984 result.SetStatus (eReturnStatusFailed);
4985 target->RemoveStopHookByID (new_hook_sp->GetID());
4986 return false;
4987 }
4988 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004989 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004990 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4991 }
4992 else
4993 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004994 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004995 result.SetStatus (eReturnStatusFailed);
4996 }
4997
4998 return result.Succeeded();
4999 }
5000private:
5001 CommandOptions m_options;
5002};
5003
Greg Claytonb3448432011-03-24 21:19:54 +00005004OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00005005CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5006{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005007 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00005008 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00005009 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
5010 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005011 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005012 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005013 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005014 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005015 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005016 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005017 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005018 "The stop hook is run only for threads in the queue whose name is given by this argument."},
5019 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
5020 "Specify the source file within which the stop-hook is to be run." },
5021 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
5022 "Set the start of the line range for which the stop-hook is to be run."},
5023 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
5024 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005025 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005026 "Specify the class within which the stop-hook is to be run." },
5027 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
5028 "Set the function name within which the stop hook will be run." },
5029 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5030};
5031
5032#pragma mark CommandObjectTargetStopHookDelete
5033
5034//-------------------------------------------------------------------------
5035// CommandObjectTargetStopHookDelete
5036//-------------------------------------------------------------------------
5037
Jim Inghamda26bd22012-06-08 21:56:10 +00005038class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005039{
5040public:
5041
5042 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005043 CommandObjectParsed (interpreter,
5044 "target stop-hook delete",
5045 "Delete a stop-hook.",
5046 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005047 {
5048 }
5049
5050 ~CommandObjectTargetStopHookDelete ()
5051 {
5052 }
5053
Jim Inghamda26bd22012-06-08 21:56:10 +00005054protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005055 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005056 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005057 {
5058 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5059 if (target)
5060 {
5061 // FIXME: see if we can use the breakpoint id style parser?
5062 size_t num_args = command.GetArgumentCount();
5063 if (num_args == 0)
5064 {
5065 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5066 {
5067 result.SetStatus (eReturnStatusFailed);
5068 return false;
5069 }
5070 else
5071 {
5072 target->RemoveAllStopHooks();
5073 }
5074 }
5075 else
5076 {
5077 bool success;
5078 for (size_t i = 0; i < num_args; i++)
5079 {
5080 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5081 if (!success)
5082 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005083 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005084 result.SetStatus(eReturnStatusFailed);
5085 return false;
5086 }
5087 success = target->RemoveStopHookByID (user_id);
5088 if (!success)
5089 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005090 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005091 result.SetStatus(eReturnStatusFailed);
5092 return false;
5093 }
5094 }
5095 }
5096 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5097 }
5098 else
5099 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005100 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005101 result.SetStatus (eReturnStatusFailed);
5102 }
5103
5104 return result.Succeeded();
5105 }
5106};
5107#pragma mark CommandObjectTargetStopHookEnableDisable
5108
5109//-------------------------------------------------------------------------
5110// CommandObjectTargetStopHookEnableDisable
5111//-------------------------------------------------------------------------
5112
Jim Inghamda26bd22012-06-08 21:56:10 +00005113class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005114{
5115public:
5116
5117 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005118 CommandObjectParsed (interpreter,
5119 name,
5120 help,
5121 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00005122 m_enable (enable)
5123 {
5124 }
5125
5126 ~CommandObjectTargetStopHookEnableDisable ()
5127 {
5128 }
5129
Jim Inghamda26bd22012-06-08 21:56:10 +00005130protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005131 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005132 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005133 {
5134 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5135 if (target)
5136 {
5137 // FIXME: see if we can use the breakpoint id style parser?
5138 size_t num_args = command.GetArgumentCount();
5139 bool success;
5140
5141 if (num_args == 0)
5142 {
5143 target->SetAllStopHooksActiveState (m_enable);
5144 }
5145 else
5146 {
5147 for (size_t i = 0; i < num_args; i++)
5148 {
5149 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5150 if (!success)
5151 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005152 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005153 result.SetStatus(eReturnStatusFailed);
5154 return false;
5155 }
5156 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5157 if (!success)
5158 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005159 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005160 result.SetStatus(eReturnStatusFailed);
5161 return false;
5162 }
5163 }
5164 }
5165 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5166 }
5167 else
5168 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005169 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005170 result.SetStatus (eReturnStatusFailed);
5171 }
5172 return result.Succeeded();
5173 }
5174private:
5175 bool m_enable;
5176};
5177
5178#pragma mark CommandObjectTargetStopHookList
5179
5180//-------------------------------------------------------------------------
5181// CommandObjectTargetStopHookList
5182//-------------------------------------------------------------------------
5183
Jim Inghamda26bd22012-06-08 21:56:10 +00005184class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005185{
5186public:
5187
5188 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005189 CommandObjectParsed (interpreter,
5190 "target stop-hook list",
5191 "List all stop-hooks.",
5192 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005193 {
5194 }
5195
5196 ~CommandObjectTargetStopHookList ()
5197 {
5198 }
5199
Jim Inghamda26bd22012-06-08 21:56:10 +00005200protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005201 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005202 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005203 {
5204 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005205 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005206 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005207 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005208 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005209 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005210 }
5211
5212 size_t num_hooks = target->GetNumStopHooks ();
5213 if (num_hooks == 0)
5214 {
5215 result.GetOutputStream().PutCString ("No stop hooks.\n");
5216 }
5217 else
5218 {
5219 for (size_t i = 0; i < num_hooks; i++)
5220 {
5221 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5222 if (i > 0)
5223 result.GetOutputStream().PutCString ("\n");
5224 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5225 }
5226 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005227 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005228 return result.Succeeded();
5229 }
5230};
5231
5232#pragma mark CommandObjectMultiwordTargetStopHooks
5233//-------------------------------------------------------------------------
5234// CommandObjectMultiwordTargetStopHooks
5235//-------------------------------------------------------------------------
5236
5237class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5238{
5239public:
5240
5241 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5242 CommandObjectMultiword (interpreter,
5243 "target stop-hook",
5244 "A set of commands for operating on debugger target stop-hooks.",
5245 "target stop-hook <subcommand> [<subcommand-options>]")
5246 {
5247 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5248 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5249 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5250 false,
5251 "target stop-hook disable [<id>]",
5252 "Disable a stop-hook.",
5253 "target stop-hook disable")));
5254 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5255 true,
5256 "target stop-hook enable [<id>]",
5257 "Enable a stop-hook.",
5258 "target stop-hook enable")));
5259 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5260 }
5261
5262 ~CommandObjectMultiwordTargetStopHooks()
5263 {
5264 }
5265};
5266
5267
Chris Lattner24943d22010-06-08 16:52:24 +00005268
5269#pragma mark CommandObjectMultiwordTarget
5270
5271//-------------------------------------------------------------------------
5272// CommandObjectMultiwordTarget
5273//-------------------------------------------------------------------------
5274
Greg Clayton63094e02010-06-23 01:19:29 +00005275CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005276 CommandObjectMultiword (interpreter,
5277 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005278 "A set of commands for operating on debugger targets.",
5279 "target <subcommand> [<subcommand-options>]")
5280{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005281
5282 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005283 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005284 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5285 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005286 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005287 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005288 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005289 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005290}
5291
5292CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5293{
5294}
5295
Greg Claytonabe0fed2011-04-18 08:33:37 +00005296