blob: 3869efb08b998dce4910e593723e550fdba0d83c [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 Granata19030d82011-08-15 18:01:31 +0000640 ValueObject::DumpValueObjectOptions options;
641
Enrico Granata3069c622012-03-01 04:24:26 +0000642 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000643 .SetMaximumDepth(m_varobj_options.max_depth)
644 .SetShowTypes(m_varobj_options.show_types)
645 .SetShowLocation(m_varobj_options.show_location)
646 .SetUseObjectiveC(m_varobj_options.use_objc)
647 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000648 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000649 .SetFlatOutput(m_varobj_options.flat_output)
650 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
651 .SetIgnoreCap(m_varobj_options.ignore_cap);
652
Greg Clayton5d81f492011-07-08 21:46:14 +0000653 switch (var_sp->GetScope())
654 {
655 case eValueTypeVariableGlobal:
656 if (m_option_variable.show_scope)
657 s.PutCString("GLOBAL: ");
658 break;
659
660 case eValueTypeVariableStatic:
661 if (m_option_variable.show_scope)
662 s.PutCString("STATIC: ");
663 break;
664
665 case eValueTypeVariableArgument:
666 if (m_option_variable.show_scope)
667 s.PutCString(" ARG: ");
668 break;
669
670 case eValueTypeVariableLocal:
671 if (m_option_variable.show_scope)
672 s.PutCString(" LOCAL: ");
673 break;
674
675 default:
676 break;
677 }
678
Greg Claytonfb816422011-07-10 19:21:23 +0000679 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000680 {
Greg Claytonfb816422011-07-10 19:21:23 +0000681 bool show_fullpaths = false;
682 bool show_module = true;
683 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
684 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000685 }
686
Greg Claytona42880a2011-10-25 06:44:01 +0000687 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000688 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000689 options.SetFormat(format);
690
691 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000692
693 ValueObject::DumpValueObject (s,
694 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000695 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000696
697 }
Greg Clayton801417e2011-07-07 01:59:51 +0000698
Greg Clayton5d81f492011-07-08 21:46:14 +0000699
Greg Clayton36da2aa2013-01-25 18:06:21 +0000700 static size_t GetVariableCallback (void *baton,
701 const char *name,
702 VariableList &variable_list)
Greg Clayton5d81f492011-07-08 21:46:14 +0000703 {
704 Target *target = static_cast<Target *>(baton);
705 if (target)
706 {
707 return target->GetImages().FindGlobalVariables (ConstString(name),
708 true,
709 UINT32_MAX,
710 variable_list);
711 }
712 return 0;
713 }
714
715
716
Jim Inghamda26bd22012-06-08 21:56:10 +0000717 Options *
718 GetOptions ()
719 {
720 return &m_option_group;
721 }
722
723protected:
Greg Clayton6475c422012-12-04 00:32:51 +0000724
725 void
726 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
727 {
728 size_t count = variable_list.GetSize();
729 if (count > 0)
730 {
731 if (sc.module_sp)
732 {
733 if (sc.comp_unit)
734 {
735 s.Printf ("Global variables for %s/%s in %s/%s:\n",
736 sc.comp_unit->GetDirectory().GetCString(),
737 sc.comp_unit->GetFilename().GetCString(),
738 sc.module_sp->GetFileSpec().GetDirectory().GetCString(),
739 sc.module_sp->GetFileSpec().GetFilename().GetCString());
740 }
741 else
742 {
743 s.Printf ("Global variables for %s/%s\n",
744 sc.module_sp->GetFileSpec().GetDirectory().GetCString(),
745 sc.module_sp->GetFileSpec().GetFilename().GetCString());
746 }
747 }
748 else if (sc.comp_unit)
749 {
750 s.Printf ("Global variables for %s/%s\n",
751 sc.comp_unit->GetDirectory().GetCString(),
752 sc.comp_unit->GetFilename().GetCString());
753 }
754
755 for (uint32_t i=0; i<count; ++i)
756 {
757 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
758 if (var_sp)
759 {
760 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
761
762 if (valobj_sp)
763 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
764 }
765 }
766 }
767
768 }
Greg Clayton801417e2011-07-07 01:59:51 +0000769 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000770 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000771 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000772 Target *target = m_exe_ctx.GetTargetPtr();
773 const size_t argc = args.GetArgumentCount();
774 Stream &s = result.GetOutputStream();
775
776 if (argc > 0)
Greg Clayton801417e2011-07-07 01:59:51 +0000777 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000778
779 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton801417e2011-07-07 01:59:51 +0000780 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000781 VariableList variable_list;
782 ValueObjectList valobj_list;
Greg Clayton5d81f492011-07-08 21:46:14 +0000783
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000784 const char *arg = args.GetArgumentAtIndex(idx);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000785 size_t matches = 0;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000786 bool use_var_name = false;
787 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000788 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000789 RegularExpression regex(arg);
790 if (!regex.IsValid ())
Greg Clayton801417e2011-07-07 01:59:51 +0000791 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000792 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton368f8222011-07-07 04:38:25 +0000793 result.SetStatus (eReturnStatusFailed);
794 return false;
795 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000796 use_var_name = true;
797 matches = target->GetImages().FindGlobalVariables (regex,
798 true,
799 UINT32_MAX,
800 variable_list);
Greg Clayton6475c422012-12-04 00:32:51 +0000801 }
802 else
803 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000804 Error error (Variable::GetValuesForVariableExpressionPath (arg,
805 m_exe_ctx.GetBestExecutionContextScope(),
806 GetVariableCallback,
807 target,
808 variable_list,
809 valobj_list));
810 matches = variable_list.GetSize();
811 }
812
813 if (matches == 0)
814 {
815 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
816 result.SetStatus (eReturnStatusFailed);
817 return false;
818 }
819 else
820 {
821 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton6475c422012-12-04 00:32:51 +0000822 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000823 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
824 if (var_sp)
Greg Clayton6475c422012-12-04 00:32:51 +0000825 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000826 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
827 if (!valobj_sp)
828 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton6475c422012-12-04 00:32:51 +0000829
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000830 if (valobj_sp)
831 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton6475c422012-12-04 00:32:51 +0000832 }
833 }
Greg Claytonfac93882011-10-05 22:17:32 +0000834 }
Greg Clayton801417e2011-07-07 01:59:51 +0000835 }
836 }
837 else
838 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000839 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
840 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
841 SymbolContextList sc_list;
842 const size_t num_compile_units = compile_units.GetSize();
843 const size_t num_shlibs = shlibs.GetSize();
844 if (num_compile_units == 0 && num_shlibs == 0)
845 {
846 bool success = false;
847 StackFrame *frame = m_exe_ctx.GetFramePtr();
848 CompileUnit *comp_unit = NULL;
849 if (frame)
850 {
851 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
852 if (sc.comp_unit)
853 {
854 const bool can_create = true;
855 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
856 if (comp_unit_varlist_sp)
857 {
858 size_t count = comp_unit_varlist_sp->GetSize();
859 if (count > 0)
860 {
861 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
862 success = true;
863 }
864 }
865 }
866 }
867 if (!success)
868 {
869 if (frame)
870 {
871 if (comp_unit)
872 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
873 comp_unit->GetDirectory().GetCString(),
874 comp_unit->GetFilename().GetCString());
875 else
876 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
877 }
878 else
879 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
880 result.SetStatus (eReturnStatusFailed);
881 }
882 }
883 else
884 {
885 SymbolContextList sc_list;
886 const bool append = true;
887 // We have one or more compile unit or shlib
888 if (num_shlibs > 0)
889 {
890 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
891 {
892 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
893 ModuleSpec module_spec (module_file);
894
895 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
896 if (module_sp)
897 {
898 if (num_compile_units > 0)
899 {
900 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
901 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
902 }
903 else
904 {
905 SymbolContext sc;
906 sc.module_sp = module_sp;
907 sc_list.Append(sc);
908 }
909 }
910 else
911 {
912 // Didn't find matching shlib/module in target...
913 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s%s%s\n",
914 module_file.GetDirectory().GetCString(),
915 module_file.GetDirectory() ? "/" : "",
916 module_file.GetFilename().GetCString());
917 }
918 }
919 }
920 else
921 {
922 // No shared libraries, we just want to find globals for the compile units files that were specified
923 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
924 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
925 }
926
927 const uint32_t num_scs = sc_list.GetSize();
928 if (num_scs > 0)
929 {
930 SymbolContext sc;
931 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
932 {
933 if (sc_list.GetContextAtIndex(sc_idx, sc))
934 {
935 if (sc.comp_unit)
936 {
937 const bool can_create = true;
938 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
939 if (comp_unit_varlist_sp)
940 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
941 }
942 else if (sc.module_sp)
943 {
944 // Get all global variables for this module
945 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
946 VariableList variable_list;
947 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
948 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
949 }
950 }
951 }
952 }
953 }
Greg Clayton801417e2011-07-07 01:59:51 +0000954 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000955
Enrico Granatadb64d952011-08-12 16:42:31 +0000956 if (m_interpreter.TruncationWarningNecessary())
957 {
958 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
959 m_cmd_name.c_str());
960 m_interpreter.TruncationWarningGiven();
961 }
962
Greg Clayton801417e2011-07-07 01:59:51 +0000963 return result.Succeeded();
964 }
965
Greg Clayton801417e2011-07-07 01:59:51 +0000966 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000967 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000968 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000969 OptionGroupFileList m_option_compile_units;
970 OptionGroupFileList m_option_shared_libraries;
971 OptionGroupValueObjectDisplay m_varobj_options;
972
973};
974
975
Greg Claytone1f50b92011-05-03 22:09:39 +0000976#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000977
Jim Inghamda26bd22012-06-08 21:56:10 +0000978class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000979{
980public:
981
Greg Claytone1f50b92011-05-03 22:09:39 +0000982 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000983 CommandObjectParsed (interpreter,
984 "target modules search-paths add",
985 "Add new image search paths substitution pairs to the current target.",
986 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000987 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000988 CommandArgumentEntry arg;
989 CommandArgumentData old_prefix_arg;
990 CommandArgumentData new_prefix_arg;
991
992 // Define the first variant of this arg pair.
993 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
994 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
995
996 // Define the first variant of this arg pair.
997 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
998 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
999
1000 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1001 // must always occur together, they are treated as two variants of one argument rather than two independent
1002 // arguments. Push them both into the first argument position for m_arguments...
1003
1004 arg.push_back (old_prefix_arg);
1005 arg.push_back (new_prefix_arg);
1006
1007 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001008 }
1009
Greg Claytone1f50b92011-05-03 22:09:39 +00001010 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +00001011 {
1012 }
1013
Jim Inghamda26bd22012-06-08 21:56:10 +00001014protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001015 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001016 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001017 CommandReturnObject &result)
1018 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001019 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001020 if (target)
1021 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001022 const size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001023 if (argc & 1)
1024 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001025 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001026 result.SetStatus (eReturnStatusFailed);
1027 }
1028 else
1029 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001030 for (size_t i=0; i<argc; i+=2)
Chris Lattner24943d22010-06-08 16:52:24 +00001031 {
1032 const char *from = command.GetArgumentAtIndex(i);
1033 const char *to = command.GetArgumentAtIndex(i+1);
1034
1035 if (from[0] && to[0])
1036 {
1037 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +00001038 target->GetImageSearchPathList().Append (ConstString(from),
1039 ConstString(to),
1040 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +00001041 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001042 }
1043 else
1044 {
1045 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001046 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001047 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001048 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001049 result.SetStatus (eReturnStatusFailed);
1050 }
1051 }
1052 }
1053 }
1054 else
1055 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001056 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001057 result.SetStatus (eReturnStatusFailed);
1058 }
1059 return result.Succeeded();
1060 }
1061};
1062
Greg Claytone1f50b92011-05-03 22:09:39 +00001063#pragma mark CommandObjectTargetModulesSearchPathsClear
1064
Jim Inghamda26bd22012-06-08 21:56:10 +00001065class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001066{
1067public:
1068
Greg Claytone1f50b92011-05-03 22:09:39 +00001069 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001070 CommandObjectParsed (interpreter,
1071 "target modules search-paths clear",
1072 "Clear all current image search path substitution pairs from the current target.",
1073 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +00001074 {
1075 }
1076
Greg Claytone1f50b92011-05-03 22:09:39 +00001077 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +00001078 {
1079 }
1080
Jim Inghamda26bd22012-06-08 21:56:10 +00001081protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001082 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001083 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001084 CommandReturnObject &result)
1085 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001086 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001087 if (target)
1088 {
1089 bool notify = true;
1090 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +00001091 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001092 }
1093 else
1094 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001095 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001096 result.SetStatus (eReturnStatusFailed);
1097 }
1098 return result.Succeeded();
1099 }
1100};
1101
Greg Claytone1f50b92011-05-03 22:09:39 +00001102#pragma mark CommandObjectTargetModulesSearchPathsInsert
1103
Jim Inghamda26bd22012-06-08 21:56:10 +00001104class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001105{
1106public:
1107
Greg Claytone1f50b92011-05-03 22:09:39 +00001108 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001109 CommandObjectParsed (interpreter,
1110 "target modules search-paths insert",
1111 "Insert a new image search path substitution pair into the current target at the specified index.",
1112 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001113 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001114 CommandArgumentEntry arg1;
1115 CommandArgumentEntry arg2;
1116 CommandArgumentData index_arg;
1117 CommandArgumentData old_prefix_arg;
1118 CommandArgumentData new_prefix_arg;
1119
1120 // Define the first and only variant of this arg.
1121 index_arg.arg_type = eArgTypeIndex;
1122 index_arg.arg_repetition = eArgRepeatPlain;
1123
1124 // Put the one and only variant into the first arg for m_arguments:
1125 arg1.push_back (index_arg);
1126
1127 // Define the first variant of this arg pair.
1128 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1129 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1130
1131 // Define the first variant of this arg pair.
1132 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1133 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1134
1135 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1136 // must always occur together, they are treated as two variants of one argument rather than two independent
1137 // arguments. Push them both into the same argument position for m_arguments...
1138
1139 arg2.push_back (old_prefix_arg);
1140 arg2.push_back (new_prefix_arg);
1141
1142 // Add arguments to m_arguments.
1143 m_arguments.push_back (arg1);
1144 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001145 }
1146
Greg Claytone1f50b92011-05-03 22:09:39 +00001147 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001148 {
1149 }
1150
Jim Inghamda26bd22012-06-08 21:56:10 +00001151protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001152 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001153 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001154 CommandReturnObject &result)
1155 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001156 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001157 if (target)
1158 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001159 size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001160 // check for at least 3 arguments and an odd nubmer of parameters
1161 if (argc >= 3 && argc & 1)
1162 {
1163 bool success = false;
1164
1165 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1166
1167 if (!success)
1168 {
1169 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1170 result.SetStatus (eReturnStatusFailed);
1171 return result.Succeeded();
1172 }
1173
1174 // shift off the index
1175 command.Shift();
1176 argc = command.GetArgumentCount();
1177
1178 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1179 {
1180 const char *from = command.GetArgumentAtIndex(i);
1181 const char *to = command.GetArgumentAtIndex(i+1);
1182
1183 if (from[0] && to[0])
1184 {
1185 bool last_pair = ((argc - i) == 2);
1186 target->GetImageSearchPathList().Insert (ConstString(from),
1187 ConstString(to),
1188 insert_idx,
1189 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001190 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001191 }
1192 else
1193 {
1194 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001195 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001196 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001197 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001198 result.SetStatus (eReturnStatusFailed);
1199 return false;
1200 }
1201 }
1202 }
1203 else
1204 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001205 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001206 result.SetStatus (eReturnStatusFailed);
1207 return result.Succeeded();
1208 }
1209
1210 }
1211 else
1212 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001213 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001214 result.SetStatus (eReturnStatusFailed);
1215 }
1216 return result.Succeeded();
1217 }
1218};
1219
Greg Claytone1f50b92011-05-03 22:09:39 +00001220
1221#pragma mark CommandObjectTargetModulesSearchPathsList
1222
1223
Jim Inghamda26bd22012-06-08 21:56:10 +00001224class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001225{
1226public:
1227
Greg Claytone1f50b92011-05-03 22:09:39 +00001228 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001229 CommandObjectParsed (interpreter,
1230 "target modules search-paths list",
1231 "List all current image search path substitution pairs in the current target.",
1232 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001233 {
1234 }
1235
Greg Claytone1f50b92011-05-03 22:09:39 +00001236 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001237 {
1238 }
1239
Jim Inghamda26bd22012-06-08 21:56:10 +00001240protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001241 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001242 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001243 CommandReturnObject &result)
1244 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001245 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001246 if (target)
1247 {
1248 if (command.GetArgumentCount() != 0)
1249 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001250 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001251 result.SetStatus (eReturnStatusFailed);
1252 return result.Succeeded();
1253 }
1254
1255 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001256 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001257 }
1258 else
1259 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001260 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001261 result.SetStatus (eReturnStatusFailed);
1262 }
1263 return result.Succeeded();
1264 }
1265};
1266
Greg Claytone1f50b92011-05-03 22:09:39 +00001267#pragma mark CommandObjectTargetModulesSearchPathsQuery
1268
Jim Inghamda26bd22012-06-08 21:56:10 +00001269class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001270{
1271public:
1272
Greg Claytone1f50b92011-05-03 22:09:39 +00001273 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001274 CommandObjectParsed (interpreter,
1275 "target modules search-paths query",
1276 "Transform a path using the first applicable image search path.",
1277 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001278 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001279 CommandArgumentEntry arg;
1280 CommandArgumentData path_arg;
1281
1282 // Define the first (and only) variant of this arg.
Sean Callanan9a91ef62012-10-24 01:12:14 +00001283 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice43b014a2010-10-04 22:28:36 +00001284 path_arg.arg_repetition = eArgRepeatPlain;
1285
1286 // There is only one variant this argument could be; put it into the argument entry.
1287 arg.push_back (path_arg);
1288
1289 // Push the data for the first argument into the m_arguments vector.
1290 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001291 }
1292
Greg Claytone1f50b92011-05-03 22:09:39 +00001293 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001294 {
1295 }
1296
Jim Inghamda26bd22012-06-08 21:56:10 +00001297protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001298 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001299 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001300 CommandReturnObject &result)
1301 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001302 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001303 if (target)
1304 {
1305 if (command.GetArgumentCount() != 1)
1306 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001307 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001308 result.SetStatus (eReturnStatusFailed);
1309 return result.Succeeded();
1310 }
1311
1312 ConstString orig(command.GetArgumentAtIndex(0));
1313 ConstString transformed;
1314 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1315 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1316 else
1317 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001318
1319 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001320 }
1321 else
1322 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001323 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001324 result.SetStatus (eReturnStatusFailed);
1325 }
1326 return result.Succeeded();
1327 }
1328};
1329
Greg Claytone1f50b92011-05-03 22:09:39 +00001330//----------------------------------------------------------------------
1331// Static Helper functions
1332//----------------------------------------------------------------------
1333static void
1334DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1335{
1336 if (module)
1337 {
1338 const char *arch_cstr;
1339 if (full_triple)
1340 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1341 else
1342 arch_cstr = module->GetArchitecture().GetArchitectureName();
1343 if (width)
1344 strm.Printf("%-*s", width, arch_cstr);
1345 else
1346 strm.PutCString(arch_cstr);
1347 }
1348}
1349
1350static void
1351DumpModuleUUID (Stream &strm, Module *module)
1352{
Jim Ingham6f01c932012-10-12 17:34:26 +00001353 if (module && module->GetUUID().IsValid())
Greg Clayton153ccd72011-08-10 02:10:13 +00001354 module->GetUUID().Dump (&strm);
1355 else
1356 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001357}
1358
1359static uint32_t
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001360DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1361 Stream &strm,
1362 Module *module,
1363 const FileSpec &file_spec,
1364 bool load_addresses)
Greg Claytone1f50b92011-05-03 22:09:39 +00001365{
1366 uint32_t num_matches = 0;
1367 if (module)
1368 {
1369 SymbolContextList sc_list;
1370 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1371 0,
1372 false,
1373 eSymbolContextCompUnit,
1374 sc_list);
1375
1376 for (uint32_t i=0; i<num_matches; ++i)
1377 {
1378 SymbolContext sc;
1379 if (sc_list.GetContextAtIndex(i, sc))
1380 {
1381 if (i > 0)
1382 strm << "\n\n";
1383
1384 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1385 << module->GetFileSpec().GetFilename() << "\n";
1386 LineTable *line_table = sc.comp_unit->GetLineTable();
1387 if (line_table)
1388 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001389 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001390 lldb::eDescriptionLevelBrief);
1391 else
1392 strm << "No line table";
1393 }
1394 }
1395 }
1396 return num_matches;
1397}
1398
1399static void
1400DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1401{
1402 if (file_spec_ptr)
1403 {
1404 if (width > 0)
1405 {
1406 char fullpath[PATH_MAX];
1407 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1408 {
1409 strm.Printf("%-*s", width, fullpath);
1410 return;
1411 }
1412 }
1413 else
1414 {
1415 file_spec_ptr->Dump(&strm);
1416 return;
1417 }
1418 }
1419 // Keep the width spacing correct if things go wrong...
1420 if (width > 0)
1421 strm.Printf("%-*s", width, "");
1422}
1423
1424static void
1425DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1426{
1427 if (file_spec_ptr)
1428 {
1429 if (width > 0)
1430 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1431 else
1432 file_spec_ptr->GetDirectory().Dump(&strm);
1433 return;
1434 }
1435 // Keep the width spacing correct if things go wrong...
1436 if (width > 0)
1437 strm.Printf("%-*s", width, "");
1438}
1439
1440static void
1441DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1442{
1443 if (file_spec_ptr)
1444 {
1445 if (width > 0)
1446 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1447 else
1448 file_spec_ptr->GetFilename().Dump(&strm);
1449 return;
1450 }
1451 // Keep the width spacing correct if things go wrong...
1452 if (width > 0)
1453 strm.Printf("%-*s", width, "");
1454}
1455
1456
1457static void
1458DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1459{
1460 if (module)
1461 {
1462 ObjectFile *objfile = module->GetObjectFile ();
1463 if (objfile)
1464 {
1465 Symtab *symtab = objfile->GetSymtab();
1466 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001467 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001468 }
1469 }
1470}
1471
1472static void
1473DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1474{
1475 if (module)
1476 {
1477 ObjectFile *objfile = module->GetObjectFile ();
1478 if (objfile)
1479 {
1480 SectionList *section_list = objfile->GetSectionList();
1481 if (section_list)
1482 {
1483 strm.PutCString ("Sections for '");
1484 strm << module->GetFileSpec();
1485 if (module->GetObjectName())
1486 strm << '(' << module->GetObjectName() << ')';
1487 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1488 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001489 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001490 strm.IndentLess();
1491 }
1492 }
1493 }
1494}
1495
1496static bool
1497DumpModuleSymbolVendor (Stream &strm, Module *module)
1498{
1499 if (module)
1500 {
1501 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1502 if (symbol_vendor)
1503 {
1504 symbol_vendor->Dump(&strm);
1505 return true;
1506 }
1507 }
1508 return false;
1509}
1510
Greg Clayton2ad894b2012-05-15 18:43:44 +00001511static void
1512DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1513{
1514 strm.IndentMore();
1515 strm.Indent (" Address: ");
1516 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1517 strm.PutCString (" (");
1518 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1519 strm.PutCString (")\n");
1520 strm.Indent (" Summary: ");
1521 const uint32_t save_indent = strm.GetIndentLevel ();
1522 strm.SetIndentLevel (save_indent + 13);
1523 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1524 strm.SetIndentLevel (save_indent);
1525 // Print out detailed address information when verbose is enabled
1526 if (verbose)
1527 {
1528 strm.EOL();
1529 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1530 }
1531 strm.IndentLess();
1532}
1533
Greg Claytone1f50b92011-05-03 22:09:39 +00001534static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001535LookupAddressInModule (CommandInterpreter &interpreter,
1536 Stream &strm,
1537 Module *module,
1538 uint32_t resolve_mask,
1539 lldb::addr_t raw_addr,
1540 lldb::addr_t offset,
1541 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001542{
1543 if (module)
1544 {
1545 lldb::addr_t addr = raw_addr - offset;
1546 Address so_addr;
1547 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001548 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001549 if (target && !target->GetSectionLoadList().IsEmpty())
1550 {
1551 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1552 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001553 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001554 return false;
1555 }
1556 else
1557 {
1558 if (!module->ResolveFileAddress (addr, so_addr))
1559 return false;
1560 }
1561
Greg Claytone1f50b92011-05-03 22:09:39 +00001562 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001563 DumpAddress (exe_scope, so_addr, verbose, strm);
1564// strm.IndentMore();
1565// strm.Indent (" Address: ");
1566// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1567// strm.PutCString (" (");
1568// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1569// strm.PutCString (")\n");
1570// strm.Indent (" Summary: ");
1571// const uint32_t save_indent = strm.GetIndentLevel ();
1572// strm.SetIndentLevel (save_indent + 13);
1573// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1574// strm.SetIndentLevel (save_indent);
1575// // Print out detailed address information when verbose is enabled
1576// if (verbose)
1577// {
1578// strm.EOL();
1579// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1580// }
1581// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001582 return true;
1583 }
1584
1585 return false;
1586}
1587
1588static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001589LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001590{
1591 if (module)
1592 {
1593 SymbolContext sc;
1594
1595 ObjectFile *objfile = module->GetObjectFile ();
1596 if (objfile)
1597 {
1598 Symtab *symtab = objfile->GetSymtab();
1599 if (symtab)
1600 {
1601 uint32_t i;
1602 std::vector<uint32_t> match_indexes;
1603 ConstString symbol_name (name);
1604 uint32_t num_matches = 0;
1605 if (name_is_regex)
1606 {
1607 RegularExpression name_regexp(name);
1608 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1609 eSymbolTypeAny,
1610 match_indexes);
1611 }
1612 else
1613 {
1614 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1615 }
1616
1617
1618 if (num_matches > 0)
1619 {
1620 strm.Indent ();
1621 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1622 name_is_regex ? "the regular expression " : "", name);
1623 DumpFullpath (strm, &module->GetFileSpec(), 0);
1624 strm.PutCString(":\n");
1625 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001626 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001627 for (i=0; i < num_matches; ++i)
1628 {
1629 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001630 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1631 symbol->GetAddress(),
1632 verbose,
1633 strm);
1634
1635// strm.Indent ();
1636// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001637 }
1638 strm.IndentLess ();
1639 return num_matches;
1640 }
1641 }
1642 }
1643 }
1644 return 0;
1645}
1646
1647
1648static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001649DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001650{
1651 strm.IndentMore ();
1652 uint32_t i;
1653 const uint32_t num_matches = sc_list.GetSize();
1654
1655 for (i=0; i<num_matches; ++i)
1656 {
1657 SymbolContext sc;
1658 if (sc_list.GetContextAtIndex(i, sc))
1659 {
Sean Callanand7793d22012-02-11 00:24:04 +00001660 AddressRange range;
1661
1662 sc.GetAddressRange(eSymbolContextEverything,
1663 0,
1664 true,
1665 range);
1666
Greg Clayton2ad894b2012-05-15 18:43:44 +00001667 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001668 }
1669 }
1670 strm.IndentLess ();
1671}
1672
Greg Clayton36da2aa2013-01-25 18:06:21 +00001673static size_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001674LookupFunctionInModule (CommandInterpreter &interpreter,
1675 Stream &strm,
1676 Module *module,
1677 const char *name,
1678 bool name_is_regex,
1679 bool include_inlines,
1680 bool include_symbols,
1681 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001682{
1683 if (module && name && name[0])
1684 {
1685 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001686 const bool append = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001687 size_t num_matches = 0;
Greg Claytone1f50b92011-05-03 22:09:39 +00001688 if (name_is_regex)
1689 {
1690 RegularExpression function_name_regex (name);
1691 num_matches = module->FindFunctions (function_name_regex,
1692 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001693 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001694 append,
1695 sc_list);
1696 }
1697 else
1698 {
1699 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001700 num_matches = module->FindFunctions (function_name,
1701 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001702 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1703 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001704 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001705 append,
1706 sc_list);
1707 }
1708
1709 if (num_matches)
1710 {
1711 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001712 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Claytone1f50b92011-05-03 22:09:39 +00001713 DumpFullpath (strm, &module->GetFileSpec(), 0);
1714 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001715 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001716 }
1717 return num_matches;
1718 }
1719 return 0;
1720}
1721
Greg Clayton36da2aa2013-01-25 18:06:21 +00001722static size_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001723LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001724 Stream &strm,
1725 Module *module,
1726 const char *name_cstr,
1727 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001728{
1729 if (module && name_cstr && name_cstr[0])
1730 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001731 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001732 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001733 size_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001734 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001735 SymbolContext sc;
1736
1737 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001738 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001739
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001740 if (num_matches)
1741 {
1742 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001743 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001744 DumpFullpath (strm, &module->GetFileSpec(), 0);
1745 strm.PutCString(":\n");
1746 const uint32_t num_types = type_list.GetSize();
1747 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001748 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001749 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1750 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001751 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001752 // Resolve the clang type so that any forward references
1753 // to types that haven't yet been parsed will get parsed.
1754 type_sp->GetClangFullType ();
1755 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001756 // Print all typedef chains
1757 TypeSP typedef_type_sp (type_sp);
1758 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1759 while (typedefed_type_sp)
1760 {
1761 strm.EOL();
1762 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1763 typedefed_type_sp->GetClangFullType ();
1764 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1765 typedef_type_sp = typedefed_type_sp;
1766 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1767 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001768 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001769 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001770 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001771 }
1772 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001773 }
1774 return 0;
1775}
1776
Greg Clayton36da2aa2013-01-25 18:06:21 +00001777static size_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001778LookupTypeHere (CommandInterpreter &interpreter,
1779 Stream &strm,
1780 const SymbolContext &sym_ctx,
1781 const char *name_cstr,
1782 bool name_is_regex)
1783{
1784 if (!sym_ctx.module_sp)
1785 return 0;
1786
1787 TypeList type_list;
1788 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001789 size_t num_matches = 1;
Sean Callanan56d31ec2012-06-06 20:49:55 +00001790 bool name_is_fully_qualified = false;
1791
1792 ConstString name(name_cstr);
1793 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1794
1795 if (num_matches)
1796 {
1797 strm.Indent ();
1798 strm.PutCString("Best match found in ");
1799 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1800 strm.PutCString(":\n");
1801
1802 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1803 if (type_sp)
1804 {
1805 // Resolve the clang type so that any forward references
1806 // to types that haven't yet been parsed will get parsed.
1807 type_sp->GetClangFullType ();
1808 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1809 // Print all typedef chains
1810 TypeSP typedef_type_sp (type_sp);
1811 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1812 while (typedefed_type_sp)
1813 {
1814 strm.EOL();
1815 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1816 typedefed_type_sp->GetClangFullType ();
1817 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1818 typedef_type_sp = typedefed_type_sp;
1819 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1820 }
1821 }
1822 strm.EOL();
1823 }
1824 return num_matches;
1825}
1826
1827static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001828LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001829 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001830 Module *module,
1831 const FileSpec &file_spec,
1832 uint32_t line,
1833 bool check_inlines,
1834 bool verbose)
1835{
1836 if (module && file_spec)
1837 {
1838 SymbolContextList sc_list;
1839 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1840 eSymbolContextEverything, sc_list);
1841 if (num_matches > 0)
1842 {
1843 strm.Indent ();
1844 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1845 strm << file_spec;
1846 if (line > 0)
1847 strm.Printf (":%u", line);
1848 strm << " in ";
1849 DumpFullpath (strm, &module->GetFileSpec(), 0);
1850 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001851 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001852 return num_matches;
1853 }
1854 }
1855 return 0;
1856
1857}
1858
Greg Clayton91048ef2011-11-10 01:18:58 +00001859
1860static size_t
1861FindModulesByName (Target *target,
1862 const char *module_name,
1863 ModuleList &module_list,
1864 bool check_global_list)
1865{
1866// Dump specified images (by basename or fullpath)
1867 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001868 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001869
1870 const size_t initial_size = module_list.GetSize ();
1871
Greg Clayton316f57f2012-07-11 20:46:47 +00001872 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001873 {
1874 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001875 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00001876 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton91048ef2011-11-10 01:18:58 +00001877 ModuleSP module_sp;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001878 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton91048ef2011-11-10 01:18:58 +00001879 {
1880 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1881
1882 if (module)
1883 {
Greg Clayton444fe992012-02-26 05:51:37 +00001884 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001885 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001886 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001887 module_list.AppendIfNeeded(module_sp);
1888 }
1889 }
1890 }
1891 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001892 else
1893 {
1894 if (target)
1895 {
1896 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1897
1898 // Not found in our module list for our target, check the main
1899 // shared module list in case it is a extra file used somewhere
1900 // else
1901 if (num_matches == 0)
1902 {
1903 module_spec.GetArchitecture() = target->GetArchitecture();
1904 ModuleList::FindSharedModules (module_spec, module_list);
1905 }
1906 }
1907 else
1908 {
1909 ModuleList::FindSharedModules (module_spec,module_list);
1910 }
1911 }
1912
Greg Clayton91048ef2011-11-10 01:18:58 +00001913 return module_list.GetSize () - initial_size;
1914}
1915
Greg Claytone1f50b92011-05-03 22:09:39 +00001916#pragma mark CommandObjectTargetModulesModuleAutoComplete
1917
1918//----------------------------------------------------------------------
1919// A base command object class that can auto complete with module file
1920// paths
1921//----------------------------------------------------------------------
1922
Jim Inghamda26bd22012-06-08 21:56:10 +00001923class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001924{
1925public:
1926
1927 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1928 const char *name,
1929 const char *help,
1930 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001931 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001932 {
1933 CommandArgumentEntry arg;
1934 CommandArgumentData file_arg;
1935
1936 // Define the first (and only) variant of this arg.
1937 file_arg.arg_type = eArgTypeFilename;
1938 file_arg.arg_repetition = eArgRepeatStar;
1939
1940 // There is only one variant this argument could be; put it into the argument entry.
1941 arg.push_back (file_arg);
1942
1943 // Push the data for the first argument into the m_arguments vector.
1944 m_arguments.push_back (arg);
1945 }
1946
1947 virtual
1948 ~CommandObjectTargetModulesModuleAutoComplete ()
1949 {
1950 }
1951
1952 virtual int
1953 HandleArgumentCompletion (Args &input,
1954 int &cursor_index,
1955 int &cursor_char_position,
1956 OptionElementVector &opt_element_vector,
1957 int match_start_point,
1958 int max_return_elements,
1959 bool &word_complete,
1960 StringList &matches)
1961 {
1962 // Arguments are the standard module completer.
1963 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1964 completion_str.erase (cursor_char_position);
1965
1966 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1967 CommandCompletions::eModuleCompletion,
1968 completion_str.c_str(),
1969 match_start_point,
1970 max_return_elements,
1971 NULL,
1972 word_complete,
1973 matches);
1974 return matches.GetSize();
1975 }
1976};
1977
1978#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1979
1980//----------------------------------------------------------------------
1981// A base command object class that can auto complete with module source
1982// file paths
1983//----------------------------------------------------------------------
1984
Jim Inghamda26bd22012-06-08 21:56:10 +00001985class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001986{
1987public:
1988
1989 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001990 const char *name,
1991 const char *help,
1992 const char *syntax,
1993 uint32_t flags) :
1994 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytone1f50b92011-05-03 22:09:39 +00001995 {
1996 CommandArgumentEntry arg;
1997 CommandArgumentData source_file_arg;
1998
1999 // Define the first (and only) variant of this arg.
2000 source_file_arg.arg_type = eArgTypeSourceFile;
2001 source_file_arg.arg_repetition = eArgRepeatPlus;
2002
2003 // There is only one variant this argument could be; put it into the argument entry.
2004 arg.push_back (source_file_arg);
2005
2006 // Push the data for the first argument into the m_arguments vector.
2007 m_arguments.push_back (arg);
2008 }
2009
2010 virtual
2011 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2012 {
2013 }
2014
2015 virtual int
2016 HandleArgumentCompletion (Args &input,
2017 int &cursor_index,
2018 int &cursor_char_position,
2019 OptionElementVector &opt_element_vector,
2020 int match_start_point,
2021 int max_return_elements,
2022 bool &word_complete,
2023 StringList &matches)
2024 {
2025 // Arguments are the standard source file completer.
2026 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2027 completion_str.erase (cursor_char_position);
2028
2029 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2030 CommandCompletions::eSourceFileCompletion,
2031 completion_str.c_str(),
2032 match_start_point,
2033 max_return_elements,
2034 NULL,
2035 word_complete,
2036 matches);
2037 return matches.GetSize();
2038 }
2039};
2040
2041
2042#pragma mark CommandObjectTargetModulesDumpSymtab
2043
2044
2045class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2046{
2047public:
2048 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2049 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2050 "target modules dump symtab",
2051 "Dump the symbol table from one or more target modules.",
2052 NULL),
2053 m_options (interpreter)
2054 {
2055 }
2056
2057 virtual
2058 ~CommandObjectTargetModulesDumpSymtab ()
2059 {
2060 }
2061
Jim Inghamda26bd22012-06-08 21:56:10 +00002062 virtual Options *
2063 GetOptions ()
2064 {
2065 return &m_options;
2066 }
2067
2068 class CommandOptions : public Options
2069 {
2070 public:
2071
2072 CommandOptions (CommandInterpreter &interpreter) :
2073 Options(interpreter),
2074 m_sort_order (eSortOrderNone)
2075 {
2076 }
2077
2078 virtual
2079 ~CommandOptions ()
2080 {
2081 }
2082
2083 virtual Error
2084 SetOptionValue (uint32_t option_idx, const char *option_arg)
2085 {
2086 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00002087 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamda26bd22012-06-08 21:56:10 +00002088
2089 switch (short_option)
2090 {
2091 case 's':
2092 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2093 g_option_table[option_idx].enum_values,
2094 eSortOrderNone,
2095 error);
2096 break;
2097
2098 default:
2099 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2100 break;
2101
2102 }
2103 return error;
2104 }
2105
2106 void
2107 OptionParsingStarting ()
2108 {
2109 m_sort_order = eSortOrderNone;
2110 }
2111
2112 const OptionDefinition*
2113 GetDefinitions ()
2114 {
2115 return g_option_table;
2116 }
2117
2118 // Options table: Required for subclasses of Options.
2119 static OptionDefinition g_option_table[];
2120
2121 SortOrder m_sort_order;
2122 };
2123
2124protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002125 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002126 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002127 CommandReturnObject &result)
2128 {
2129 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2130 if (target == NULL)
2131 {
2132 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2133 result.SetStatus (eReturnStatusFailed);
2134 return false;
2135 }
2136 else
2137 {
2138 uint32_t num_dumped = 0;
2139
2140 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2141 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2142 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2143
2144 if (command.GetArgumentCount() == 0)
2145 {
2146 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002147 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002148 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002149 if (num_modules > 0)
2150 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002151 result.GetOutputStream().Printf("Dumping symbol table for %zu modules.\n", num_modules);
2152 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002153 {
2154 if (num_dumped > 0)
2155 {
2156 result.GetOutputStream().EOL();
2157 result.GetOutputStream().EOL();
2158 }
2159 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002160 DumpModuleSymtab (m_interpreter,
2161 result.GetOutputStream(),
2162 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2163 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002164 }
2165 }
2166 else
2167 {
2168 result.AppendError ("the target has no associated executable images");
2169 result.SetStatus (eReturnStatusFailed);
2170 return false;
2171 }
2172 }
2173 else
2174 {
2175 // Dump specified images (by basename or fullpath)
2176 const char *arg_cstr;
2177 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2178 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002179 ModuleList module_list;
2180 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2181 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002182 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002183 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002184 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002185 Module *module = module_list.GetModulePointerAtIndex(i);
2186 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002187 {
2188 if (num_dumped > 0)
2189 {
2190 result.GetOutputStream().EOL();
2191 result.GetOutputStream().EOL();
2192 }
2193 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002194 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002195 }
2196 }
2197 }
2198 else
2199 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2200 }
2201 }
2202
2203 if (num_dumped > 0)
2204 result.SetStatus (eReturnStatusSuccessFinishResult);
2205 else
2206 {
2207 result.AppendError ("no matching executable images found");
2208 result.SetStatus (eReturnStatusFailed);
2209 }
2210 }
2211 return result.Succeeded();
2212 }
2213
Greg Claytone1f50b92011-05-03 22:09:39 +00002214
2215 CommandOptions m_options;
2216};
2217
2218static OptionEnumValueElement
2219g_sort_option_enumeration[4] =
2220{
2221 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2222 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2223 { eSortOrderByName, "name", "Sort output by symbol name."},
2224 { 0, NULL, NULL }
2225};
2226
2227
2228OptionDefinition
2229CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2230{
2231 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2232 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2233};
2234
2235#pragma mark CommandObjectTargetModulesDumpSections
2236
2237//----------------------------------------------------------------------
2238// Image section dumping command
2239//----------------------------------------------------------------------
2240
2241class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2242{
2243public:
2244 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2245 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2246 "target modules dump sections",
2247 "Dump the sections from one or more target modules.",
2248 //"target modules dump sections [<file1> ...]")
2249 NULL)
2250 {
2251 }
2252
2253 virtual
2254 ~CommandObjectTargetModulesDumpSections ()
2255 {
2256 }
2257
Jim Inghamda26bd22012-06-08 21:56:10 +00002258protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002259 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002260 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002261 CommandReturnObject &result)
2262 {
2263 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2264 if (target == NULL)
2265 {
2266 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2267 result.SetStatus (eReturnStatusFailed);
2268 return false;
2269 }
2270 else
2271 {
2272 uint32_t num_dumped = 0;
2273
2274 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2275 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2276 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2277
2278 if (command.GetArgumentCount() == 0)
2279 {
2280 // Dump all sections for all modules images
Greg Clayton36da2aa2013-01-25 18:06:21 +00002281 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002282 if (num_modules > 0)
2283 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002284 result.GetOutputStream().Printf("Dumping sections for %zu modules.\n", num_modules);
2285 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002286 {
2287 num_dumped++;
2288 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2289 }
2290 }
2291 else
2292 {
2293 result.AppendError ("the target has no associated executable images");
2294 result.SetStatus (eReturnStatusFailed);
2295 return false;
2296 }
2297 }
2298 else
2299 {
2300 // Dump specified images (by basename or fullpath)
2301 const char *arg_cstr;
2302 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2303 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002304 ModuleList module_list;
2305 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2306 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002307 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002308 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002309 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002310 Module *module = module_list.GetModulePointerAtIndex(i);
2311 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002312 {
2313 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002314 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002315 }
2316 }
2317 }
2318 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002319 {
2320 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002321 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002322
Greg Claytone1f50b92011-05-03 22:09:39 +00002323 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002324 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002325 }
2326 }
2327
2328 if (num_dumped > 0)
2329 result.SetStatus (eReturnStatusSuccessFinishResult);
2330 else
2331 {
2332 result.AppendError ("no matching executable images found");
2333 result.SetStatus (eReturnStatusFailed);
2334 }
2335 }
2336 return result.Succeeded();
2337 }
2338};
2339
2340
2341#pragma mark CommandObjectTargetModulesDumpSymfile
2342
2343//----------------------------------------------------------------------
2344// Image debug symbol dumping command
2345//----------------------------------------------------------------------
2346
2347class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2348{
2349public:
2350 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2351 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2352 "target modules dump symfile",
2353 "Dump the debug symbol file for one or more target modules.",
2354 //"target modules dump symfile [<file1> ...]")
2355 NULL)
2356 {
2357 }
2358
2359 virtual
2360 ~CommandObjectTargetModulesDumpSymfile ()
2361 {
2362 }
2363
Jim Inghamda26bd22012-06-08 21:56:10 +00002364protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002365 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002366 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002367 CommandReturnObject &result)
2368 {
2369 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2370 if (target == NULL)
2371 {
2372 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2373 result.SetStatus (eReturnStatusFailed);
2374 return false;
2375 }
2376 else
2377 {
2378 uint32_t num_dumped = 0;
2379
2380 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2381 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2382 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2383
2384 if (command.GetArgumentCount() == 0)
2385 {
2386 // Dump all sections for all modules images
Enrico Granata146d9522012-11-08 02:22:02 +00002387 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002388 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002389 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002390 if (num_modules > 0)
2391 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002392 result.GetOutputStream().Printf("Dumping debug symbols for %zu modules.\n", num_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002393 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2394 {
Jim Ingham93367902012-05-30 02:19:25 +00002395 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002396 num_dumped++;
2397 }
2398 }
2399 else
2400 {
2401 result.AppendError ("the target has no associated executable images");
2402 result.SetStatus (eReturnStatusFailed);
2403 return false;
2404 }
2405 }
2406 else
2407 {
2408 // Dump specified images (by basename or fullpath)
2409 const char *arg_cstr;
2410 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2411 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002412 ModuleList module_list;
2413 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2414 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002415 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002416 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002417 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002418 Module *module = module_list.GetModulePointerAtIndex(i);
2419 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002420 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002421 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002422 num_dumped++;
2423 }
2424 }
2425 }
2426 else
2427 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2428 }
2429 }
2430
2431 if (num_dumped > 0)
2432 result.SetStatus (eReturnStatusSuccessFinishResult);
2433 else
2434 {
2435 result.AppendError ("no matching executable images found");
2436 result.SetStatus (eReturnStatusFailed);
2437 }
2438 }
2439 return result.Succeeded();
2440 }
2441};
2442
2443
2444#pragma mark CommandObjectTargetModulesDumpLineTable
2445
2446//----------------------------------------------------------------------
2447// Image debug line table dumping command
2448//----------------------------------------------------------------------
2449
2450class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2451{
2452public:
2453 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2454 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002455 "target modules dump line-table",
2456 "Dump the debug symbol file for one or more target modules.",
2457 NULL,
2458 eFlagRequiresTarget)
Greg Claytone1f50b92011-05-03 22:09:39 +00002459 {
2460 }
2461
2462 virtual
2463 ~CommandObjectTargetModulesDumpLineTable ()
2464 {
2465 }
2466
Jim Inghamda26bd22012-06-08 21:56:10 +00002467protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002468 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002469 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002470 CommandReturnObject &result)
2471 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002472 Target *target = m_exe_ctx.GetTargetPtr();
2473 uint32_t total_num_dumped = 0;
2474
2475 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2476 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2477 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2478
2479 if (command.GetArgumentCount() == 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002480 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002481 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytone1f50b92011-05-03 22:09:39 +00002482 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002483 }
2484 else
2485 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002486 // Dump specified images (by basename or fullpath)
2487 const char *arg_cstr;
2488 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002489 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002490 FileSpec file_spec(arg_cstr, false);
2491
2492 const ModuleList &target_modules = target->GetImages();
2493 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002494 const size_t num_modules = target_modules.GetSize();
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002495 if (num_modules > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002496 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002497 uint32_t num_dumped = 0;
2498 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002499 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002500 if (DumpCompileUnitLineTable (m_interpreter,
2501 result.GetOutputStream(),
2502 target_modules.GetModulePointerAtIndexUnlocked(i),
2503 file_spec,
2504 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2505 num_dumped++;
Greg Claytone1f50b92011-05-03 22:09:39 +00002506 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002507 if (num_dumped == 0)
2508 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2509 else
2510 total_num_dumped += num_dumped;
Greg Claytone1f50b92011-05-03 22:09:39 +00002511 }
2512 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002513 }
2514
2515 if (total_num_dumped > 0)
2516 result.SetStatus (eReturnStatusSuccessFinishResult);
2517 else
2518 {
2519 result.AppendError ("no source filenames matched any command arguments");
2520 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002521 }
2522 return result.Succeeded();
2523 }
2524};
2525
2526
2527#pragma mark CommandObjectTargetModulesDump
2528
2529//----------------------------------------------------------------------
2530// Dump multi-word command for target modules
2531//----------------------------------------------------------------------
2532
2533class CommandObjectTargetModulesDump : public CommandObjectMultiword
2534{
2535public:
2536
2537 //------------------------------------------------------------------
2538 // Constructors and Destructors
2539 //------------------------------------------------------------------
2540 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2541 CommandObjectMultiword (interpreter,
2542 "target modules dump",
2543 "A set of commands for dumping information about one or more target modules.",
2544 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2545 {
2546 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2547 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2548 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2549 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2550 }
2551
2552 virtual
2553 ~CommandObjectTargetModulesDump()
2554 {
2555 }
2556};
2557
Jim Inghamda26bd22012-06-08 21:56:10 +00002558class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002559{
2560public:
2561 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002562 CommandObjectParsed (interpreter,
2563 "target modules add",
2564 "Add a new module to the current target's modules.",
Greg Clayton1649a722012-11-29 22:16:27 +00002565 "target modules add [<module>]"),
Greg Claytonec9c2d22012-11-30 19:05:35 +00002566 m_option_group (interpreter),
2567 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 +00002568 {
Greg Clayton1649a722012-11-29 22:16:27 +00002569 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +00002570 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1649a722012-11-29 22:16:27 +00002571 m_option_group.Finalize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002572 }
2573
2574 virtual
2575 ~CommandObjectTargetModulesAdd ()
2576 {
2577 }
Greg Clayton1649a722012-11-29 22:16:27 +00002578
2579 virtual Options *
2580 GetOptions ()
2581 {
2582 return &m_option_group;
2583 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002584
Greg Clayton36da2aa2013-01-25 18:06:21 +00002585 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00002586 HandleArgumentCompletion (Args &input,
2587 int &cursor_index,
2588 int &cursor_char_position,
2589 OptionElementVector &opt_element_vector,
2590 int match_start_point,
2591 int max_return_elements,
2592 bool &word_complete,
2593 StringList &matches)
2594 {
2595 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2596 completion_str.erase (cursor_char_position);
2597
2598 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2599 CommandCompletions::eDiskFileCompletion,
2600 completion_str.c_str(),
2601 match_start_point,
2602 max_return_elements,
2603 NULL,
2604 word_complete,
2605 matches);
2606 return matches.GetSize();
2607 }
2608
2609protected:
Greg Clayton1649a722012-11-29 22:16:27 +00002610
2611 OptionGroupOptions m_option_group;
2612 OptionGroupUUID m_uuid_option_group;
Greg Claytonec9c2d22012-11-30 19:05:35 +00002613 OptionGroupFile m_symbol_file;
Greg Clayton1649a722012-11-29 22:16:27 +00002614
2615
Greg Claytone1f50b92011-05-03 22:09:39 +00002616 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002617 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002618 CommandReturnObject &result)
2619 {
2620 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2621 if (target == NULL)
2622 {
2623 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2624 result.SetStatus (eReturnStatusFailed);
2625 return false;
2626 }
2627 else
2628 {
Sean Callanane3f06612012-12-13 01:39:39 +00002629 bool flush = false;
2630
Greg Claytone1f50b92011-05-03 22:09:39 +00002631 const size_t argc = args.GetArgumentCount();
2632 if (argc == 0)
2633 {
Greg Clayton1649a722012-11-29 22:16:27 +00002634 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2635 {
2636 // We are given a UUID only, go locate the file
2637 ModuleSpec module_spec;
2638 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002639 if (m_symbol_file.GetOptionValue().OptionWasSet())
2640 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002641 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2642 {
2643 ModuleSP module_sp (target->GetSharedModule (module_spec));
2644 if (module_sp)
2645 {
2646 result.SetStatus (eReturnStatusSuccessFinishResult);
2647 return true;
2648 }
2649 else
2650 {
Sean Callanane3f06612012-12-13 01:39:39 +00002651 flush = true;
2652
Greg Clayton1649a722012-11-29 22:16:27 +00002653 StreamString strm;
2654 module_spec.GetUUID().Dump (&strm);
2655 if (module_spec.GetFileSpec())
2656 {
2657 if (module_spec.GetSymbolFileSpec())
2658 {
2659 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s and symbol file %s/%s",
2660 strm.GetString().c_str(),
2661 module_spec.GetFileSpec().GetDirectory().GetCString(),
2662 module_spec.GetFileSpec().GetFilename().GetCString(),
2663 module_spec.GetSymbolFileSpec().GetDirectory().GetCString(),
2664 module_spec.GetSymbolFileSpec().GetFilename().GetCString());
2665 }
2666 else
2667 {
2668 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s",
2669 strm.GetString().c_str(),
2670 module_spec.GetFileSpec().GetDirectory().GetCString(),
2671 module_spec.GetFileSpec().GetFilename().GetCString());
2672 }
2673 }
2674 else
2675 {
2676 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2677 strm.GetString().c_str());
2678 }
2679 result.SetStatus (eReturnStatusFailed);
2680 return false;
2681 }
2682 }
2683 else
2684 {
2685 StreamString strm;
2686 module_spec.GetUUID().Dump (&strm);
2687 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2688 result.SetStatus (eReturnStatusFailed);
2689 return false;
2690 }
2691 }
2692 else
2693 {
2694 result.AppendError ("one or more executable image paths must be specified");
2695 result.SetStatus (eReturnStatusFailed);
2696 return false;
2697 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002698 }
2699 else
2700 {
2701 for (size_t i=0; i<argc; ++i)
2702 {
2703 const char *path = args.GetArgumentAtIndex(i);
2704 if (path)
2705 {
2706 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002707 if (file_spec.Exists())
2708 {
Greg Clayton444fe992012-02-26 05:51:37 +00002709 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002710 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2711 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002712 if (m_symbol_file.GetOptionValue().OptionWasSet())
2713 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002714 Error error;
2715 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002716 if (!module_sp)
2717 {
Greg Clayton1649a722012-11-29 22:16:27 +00002718 const char *error_cstr = error.AsCString();
2719 if (error_cstr)
2720 result.AppendError (error_cstr);
2721 else
2722 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002723 result.SetStatus (eReturnStatusFailed);
2724 return false;
2725 }
Sean Callanane3f06612012-12-13 01:39:39 +00002726 else
2727 {
2728 flush = true;
2729 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002730 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002731 }
2732 else
2733 {
2734 char resolved_path[PATH_MAX];
2735 result.SetStatus (eReturnStatusFailed);
2736 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2737 {
2738 if (strcmp (resolved_path, path) != 0)
2739 {
2740 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2741 break;
2742 }
2743 }
2744 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2745 break;
2746 }
2747 }
2748 }
2749 }
Sean Callanane3f06612012-12-13 01:39:39 +00002750
2751 if (flush)
2752 {
2753 ProcessSP process = target->GetProcessSP();
2754 if (process)
2755 process->Flush();
2756 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002757 }
Sean Callanane3f06612012-12-13 01:39:39 +00002758
Greg Claytone1f50b92011-05-03 22:09:39 +00002759 return result.Succeeded();
2760 }
2761
Greg Claytone1f50b92011-05-03 22:09:39 +00002762};
2763
2764class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2765{
2766public:
2767 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2768 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2769 "target modules load",
2770 "Set the load addresses for one or more sections in a target module.",
2771 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2772 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002773 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 +00002774 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)
2775 {
2776 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2777 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2778 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2779 m_option_group.Finalize();
2780 }
2781
2782 virtual
2783 ~CommandObjectTargetModulesLoad ()
2784 {
2785 }
2786
Jim Inghamda26bd22012-06-08 21:56:10 +00002787 virtual Options *
2788 GetOptions ()
2789 {
2790 return &m_option_group;
2791 }
2792
2793protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002794 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002795 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002796 CommandReturnObject &result)
2797 {
2798 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2799 if (target == NULL)
2800 {
2801 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2802 result.SetStatus (eReturnStatusFailed);
2803 return false;
2804 }
2805 else
2806 {
2807 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002808 ModuleSpec module_spec;
2809 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002810 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002811 {
2812 search_using_module_spec = true;
2813 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2814 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002815
2816 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002817 {
2818 search_using_module_spec = true;
2819 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2820 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002821
Greg Clayton444fe992012-02-26 05:51:37 +00002822 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002823 {
2824
2825 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002826 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002827
2828 char path[PATH_MAX];
2829 if (num_matches == 1)
2830 {
2831 Module *module = matching_modules.GetModulePointerAtIndex(0);
2832 if (module)
2833 {
2834 ObjectFile *objfile = module->GetObjectFile();
2835 if (objfile)
2836 {
2837 SectionList *section_list = objfile->GetSectionList();
2838 if (section_list)
2839 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002840 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002841 if (argc == 0)
2842 {
2843 if (m_slide_option.GetOptionValue().OptionWasSet())
2844 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002845 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2846 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002847 }
2848 else
2849 {
2850 result.AppendError ("one or more section name + load address pair must be specified");
2851 result.SetStatus (eReturnStatusFailed);
2852 return false;
2853 }
2854 }
2855 else
2856 {
2857 if (m_slide_option.GetOptionValue().OptionWasSet())
2858 {
2859 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2860 result.SetStatus (eReturnStatusFailed);
2861 return false;
2862 }
2863
2864 for (size_t i=0; i<argc; i += 2)
2865 {
2866 const char *sect_name = args.GetArgumentAtIndex(i);
2867 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2868 if (sect_name && load_addr_cstr)
2869 {
2870 ConstString const_sect_name(sect_name);
2871 bool success = false;
2872 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2873 if (success)
2874 {
2875 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2876 if (section_sp)
2877 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002878 if (section_sp->IsThreadSpecific())
2879 {
2880 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2881 result.SetStatus (eReturnStatusFailed);
2882 break;
2883 }
2884 else
2885 {
Greg Clayton545762f2012-07-07 01:24:12 +00002886 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002887 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002888 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002889 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002890 }
2891 else
2892 {
2893 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2894 result.SetStatus (eReturnStatusFailed);
2895 break;
2896 }
2897 }
2898 else
2899 {
2900 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2901 result.SetStatus (eReturnStatusFailed);
2902 break;
2903 }
2904 }
2905 else
2906 {
2907 if (sect_name)
2908 result.AppendError ("section names must be followed by a load address.\n");
2909 else
2910 result.AppendError ("one or more section name + load address pair must be specified.\n");
2911 result.SetStatus (eReturnStatusFailed);
2912 break;
2913 }
2914 }
2915 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002916
2917 if (changed)
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002918 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002919 target->ModulesDidLoad (matching_modules);
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002920 Process *process = m_exe_ctx.GetProcessPtr();
2921 if (process)
2922 process->Flush();
2923 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002924 }
2925 else
2926 {
2927 module->GetFileSpec().GetPath (path, sizeof(path));
2928 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2929 result.SetStatus (eReturnStatusFailed);
2930 }
2931 }
2932 else
2933 {
2934 module->GetFileSpec().GetPath (path, sizeof(path));
2935 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2936 result.SetStatus (eReturnStatusFailed);
2937 }
2938 }
2939 else
2940 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002941 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2942 if (module_spec_file)
2943 {
2944 module_spec_file->GetPath (path, sizeof(path));
2945 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2946 }
2947 else
2948 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002949 result.SetStatus (eReturnStatusFailed);
2950 }
2951 }
2952 else
2953 {
2954 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002955
2956 if (module_spec.GetFileSpec())
2957 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002958 else
2959 path[0] = '\0';
2960
Greg Clayton444fe992012-02-26 05:51:37 +00002961 if (module_spec.GetUUIDPtr())
2962 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002963 else
2964 uuid_cstr[0] = '\0';
2965 if (num_matches > 1)
2966 {
2967 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2968 path[0] ? " file=" : "",
2969 path,
2970 uuid_cstr[0] ? " uuid=" : "",
2971 uuid_cstr);
2972 for (size_t i=0; i<num_matches; ++i)
2973 {
2974 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2975 result.AppendMessageWithFormat("%s\n", path);
2976 }
2977 }
2978 else
2979 {
2980 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2981 path[0] ? " file=" : "",
2982 path,
2983 uuid_cstr[0] ? " uuid=" : "",
2984 uuid_cstr);
2985 }
2986 result.SetStatus (eReturnStatusFailed);
2987 }
2988 }
2989 else
2990 {
2991 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2992 result.SetStatus (eReturnStatusFailed);
2993 return false;
2994 }
2995 }
2996 return result.Succeeded();
2997 }
2998
Greg Claytone1f50b92011-05-03 22:09:39 +00002999 OptionGroupOptions m_option_group;
3000 OptionGroupUUID m_uuid_option_group;
3001 OptionGroupFile m_file_option;
3002 OptionGroupUInt64 m_slide_option;
3003};
3004
3005//----------------------------------------------------------------------
3006// List images with associated information
3007//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003008class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003009{
3010public:
3011
3012 class CommandOptions : public Options
3013 {
3014 public:
3015
3016 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00003017 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00003018 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00003019 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00003020 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00003021 {
3022 }
3023
3024 virtual
3025 ~CommandOptions ()
3026 {
3027 }
3028
3029 virtual Error
3030 SetOptionValue (uint32_t option_idx, const char *option_arg)
3031 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003032 Error error;
3033
Greg Clayton6475c422012-12-04 00:32:51 +00003034 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00003035 if (short_option == 'g')
3036 {
3037 m_use_global_module_list = true;
3038 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003039 else if (short_option == 'a')
3040 {
Jim Inghamd86cf812013-03-15 23:09:19 +00003041 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3042 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Ingham6bdea822011-10-24 18:36:33 +00003043 }
Greg Clayton899025f2011-08-09 00:01:09 +00003044 else
3045 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003046 unsigned long width = 0;
Greg Clayton899025f2011-08-09 00:01:09 +00003047 if (option_arg)
3048 width = strtoul (option_arg, NULL, 0);
3049 m_format_array.push_back(std::make_pair(short_option, width));
3050 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003051 return error;
3052 }
3053
3054 void
3055 OptionParsingStarting ()
3056 {
3057 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00003058 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00003059 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00003060 }
3061
3062 const OptionDefinition*
3063 GetDefinitions ()
3064 {
3065 return g_option_table;
3066 }
3067
3068 // Options table: Required for subclasses of Options.
3069
3070 static OptionDefinition g_option_table[];
3071
3072 // Instance variables to hold the values for command options.
3073 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3074 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00003075 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00003076 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00003077 };
3078
3079 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003080 CommandObjectParsed (interpreter,
3081 "target modules list",
3082 "List current executable and dependent shared library images.",
3083 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00003084 m_options (interpreter)
3085 {
3086 }
3087
3088 virtual
3089 ~CommandObjectTargetModulesList ()
3090 {
3091 }
3092
3093 virtual
3094 Options *
3095 GetOptions ()
3096 {
3097 return &m_options;
3098 }
3099
Jim Inghamda26bd22012-06-08 21:56:10 +00003100protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003101 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003102 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003103 CommandReturnObject &result)
3104 {
3105 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00003106 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00003107 // Define a local module list here to ensure it lives longer than any "locker"
3108 // object which might lock its contents below (through the "module_list_ptr"
3109 // variable).
3110 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00003111 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00003112 {
3113 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3114 result.SetStatus (eReturnStatusFailed);
3115 return false;
3116 }
3117 else
3118 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003119 if (target)
3120 {
3121 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3122 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3123 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3124 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003125 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00003126 Stream &strm = result.GetOutputStream();
3127
3128 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3129 {
3130 if (target)
3131 {
3132 Address module_address;
3133 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3134 {
Greg Clayton3508c382012-02-24 01:59:29 +00003135 ModuleSP module_sp (module_address.GetModule());
3136 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00003137 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003138 PrintModule (target, module_sp.get(), 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00003139 result.SetStatus (eReturnStatusSuccessFinishResult);
3140 }
3141 else
3142 {
Jim Inghambb04be12012-12-15 02:40:54 +00003143 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003144 result.SetStatus (eReturnStatusFailed);
3145 }
3146 }
3147 else
3148 {
Jim Inghambb04be12012-12-15 02:40:54 +00003149 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003150 result.SetStatus (eReturnStatusFailed);
3151 }
3152 }
3153 else
3154 {
3155 result.AppendError ("Can only look up modules by address with a valid target.");
3156 result.SetStatus (eReturnStatusFailed);
3157 }
3158 return result.Succeeded();
3159 }
3160
Greg Clayton36da2aa2013-01-25 18:06:21 +00003161 size_t num_modules = 0;
Jim Ingham93367902012-05-30 02:19:25 +00003162 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3163 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3164 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003165 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003166 const size_t argc = command.GetArgumentCount();
3167 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003168 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003169 if (use_global_module_list)
3170 {
3171 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3172 num_modules = Module::GetNumberAllocatedModules();
3173 }
3174 else
3175 {
3176 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003177 }
Greg Clayton899025f2011-08-09 00:01:09 +00003178 }
3179 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003180 {
3181 for (size_t i=0; i<argc; ++i)
3182 {
3183 // Dump specified images (by basename or fullpath)
3184 const char *arg_cstr = command.GetArgumentAtIndex(i);
3185 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3186 if (num_matches == 0)
3187 {
3188 if (argc == 1)
3189 {
3190 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3191 result.SetStatus (eReturnStatusFailed);
3192 return false;
3193 }
3194 }
3195 }
3196
Greg Clayton2ad894b2012-05-15 18:43:44 +00003197 module_list_ptr = &module_list;
3198 }
Jim Ingham93367902012-05-30 02:19:25 +00003199
3200 if (module_list_ptr != NULL)
3201 {
3202 locker.Lock(module_list_ptr->GetMutex());
3203 num_modules = module_list_ptr->GetSize();
3204 }
Greg Clayton899025f2011-08-09 00:01:09 +00003205
Greg Claytone1f50b92011-05-03 22:09:39 +00003206 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003207 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003208 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3209 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003210 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003211 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003212 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003213 {
Jim Ingham93367902012-05-30 02:19:25 +00003214 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003215 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003216 }
3217 else
3218 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003219 module = Module::GetAllocatedModuleAtIndex(image_idx);
3220 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003221 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003222
Greg Clayton36da2aa2013-01-25 18:06:21 +00003223 const size_t indent = strm.Printf("[%3u] ", image_idx);
3224 PrintModule (target, module, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003225
Greg Claytone1f50b92011-05-03 22:09:39 +00003226 }
3227 result.SetStatus (eReturnStatusSuccessFinishResult);
3228 }
3229 else
3230 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003231 if (argc)
3232 {
3233 if (use_global_module_list)
3234 result.AppendError ("the global module list has no matching modules");
3235 else
3236 result.AppendError ("the target has no matching modules");
3237 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003238 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003239 {
3240 if (use_global_module_list)
3241 result.AppendError ("the global module list is empty");
3242 else
3243 result.AppendError ("the target has no associated executable images");
3244 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003245 result.SetStatus (eReturnStatusFailed);
3246 return false;
3247 }
3248 }
3249 return result.Succeeded();
3250 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003251
3252 void
Greg Clayton36da2aa2013-01-25 18:06:21 +00003253 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003254 {
3255
Jim Ingham6f01c932012-10-12 17:34:26 +00003256 if (module == NULL)
3257 {
3258 strm.PutCString("Null module");
3259 return;
3260 }
3261
Jim Ingham6bdea822011-10-24 18:36:33 +00003262 bool dump_object_name = false;
3263 if (m_options.m_format_array.empty())
3264 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003265 m_options.m_format_array.push_back(std::make_pair('u', 0));
3266 m_options.m_format_array.push_back(std::make_pair('h', 0));
3267 m_options.m_format_array.push_back(std::make_pair('f', 0));
3268 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003269 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003270 const size_t num_entries = m_options.m_format_array.size();
3271 bool print_space = false;
3272 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003273 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003274 if (print_space)
3275 strm.PutChar(' ');
3276 print_space = true;
3277 const char format_char = m_options.m_format_array[i].first;
3278 uint32_t width = m_options.m_format_array[i].second;
3279 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003280 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003281 case 'A':
3282 DumpModuleArchitecture (strm, module, false, width);
3283 break;
3284
3285 case 't':
3286 DumpModuleArchitecture (strm, module, true, width);
3287 break;
3288
3289 case 'f':
3290 DumpFullpath (strm, &module->GetFileSpec(), width);
3291 dump_object_name = true;
3292 break;
3293
3294 case 'd':
3295 DumpDirectory (strm, &module->GetFileSpec(), width);
3296 break;
3297
3298 case 'b':
3299 DumpBasename (strm, &module->GetFileSpec(), width);
3300 dump_object_name = true;
3301 break;
3302
3303 case 'h':
3304 case 'o':
3305 // Image header address
3306 {
3307 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003308
Greg Claytonb5a8f142012-02-05 02:38:54 +00003309 ObjectFile *objfile = module->GetObjectFile ();
3310 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003311 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003312 Address header_addr(objfile->GetHeaderAddress());
3313 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003314 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003315 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003316 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003317 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3318 if (header_load_addr == LLDB_INVALID_ADDRESS)
3319 {
3320 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3321 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003322 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003323 {
3324 if (format_char == 'o')
3325 {
3326 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003327 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003328 }
3329 else
3330 {
3331 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003332 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003333 }
3334 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003335 break;
3336 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003337 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3338 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3339 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003340 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003341 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003342 strm.Printf ("%*s", addr_nibble_width + 2, "");
3343 }
3344 break;
3345 case 'r':
3346 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003347 size_t ref_count = 0;
Greg Claytonb5a8f142012-02-05 02:38:54 +00003348 ModuleSP module_sp (module->shared_from_this());
3349 if (module_sp)
3350 {
3351 // Take one away to make sure we don't count our local "module_sp"
3352 ref_count = module_sp.use_count() - 1;
3353 }
3354 if (width)
Greg Clayton36da2aa2013-01-25 18:06:21 +00003355 strm.Printf("{%*zu}", width, ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003356 else
Greg Clayton36da2aa2013-01-25 18:06:21 +00003357 strm.Printf("{%zu}", ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003358 }
3359 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003360
Greg Claytonb5a8f142012-02-05 02:38:54 +00003361 case 's':
3362 case 'S':
3363 {
3364 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3365 if (symbol_vendor)
3366 {
3367 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3368 if (symbol_file)
3369 {
3370 if (format_char == 'S')
3371 {
3372 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3373 // Dump symbol file only if different from module file
3374 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3375 {
3376 print_space = false;
3377 break;
3378 }
3379 // Add a newline and indent past the index
3380 strm.Printf ("\n%*s", indent, "");
3381 }
3382 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3383 dump_object_name = true;
3384 break;
3385 }
3386 }
3387 strm.Printf("%.*s", width, "<NONE>");
3388 }
3389 break;
3390
3391 case 'm':
3392 module->GetModificationTime().Dump(&strm, width);
3393 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003394
Greg Claytonb5a8f142012-02-05 02:38:54 +00003395 case 'p':
3396 strm.Printf("%p", module);
3397 break;
3398
3399 case 'u':
3400 DumpModuleUUID(strm, module);
3401 break;
3402
3403 default:
3404 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003405 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003406
3407 }
3408 if (dump_object_name)
3409 {
3410 const char *object_name = module->GetObjectName().GetCString();
3411 if (object_name)
3412 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003413 }
3414 strm.EOL();
3415 }
3416
Greg Claytone1f50b92011-05-03 22:09:39 +00003417 CommandOptions m_options;
3418};
3419
3420OptionDefinition
3421CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3422{
Enrico Granata4968ad82013-01-29 01:48:30 +00003423 { 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 +00003424 { 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 +00003425 { 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 +00003426 { 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."},
3427 { 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 +00003428 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3429 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3430 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3431 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3432 { 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 +00003433 { 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 +00003434 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3435 { 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."},
3436 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003437 { 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 +00003438 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3439};
3440
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003441#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003442
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003443//----------------------------------------------------------------------
3444// Lookup unwind information in images
3445//----------------------------------------------------------------------
3446
3447class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3448{
3449public:
3450
3451 enum
3452 {
3453 eLookupTypeInvalid = -1,
3454 eLookupTypeAddress = 0,
3455 eLookupTypeSymbol,
3456 eLookupTypeFunction,
3457 eLookupTypeFunctionOrSymbol,
3458 kNumLookupTypes
3459 };
3460
3461 class CommandOptions : public Options
3462 {
3463 public:
3464
3465 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003466 Options(interpreter),
3467 m_type(eLookupTypeInvalid),
3468 m_str(),
3469 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003470 {
3471 }
3472
3473 virtual
3474 ~CommandOptions ()
3475 {
3476 }
3477
3478 virtual Error
3479 SetOptionValue (uint32_t option_idx, const char *option_arg)
3480 {
3481 Error error;
3482
Greg Clayton6475c422012-12-04 00:32:51 +00003483 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003484
3485 switch (short_option)
3486 {
3487 case 'a':
3488 m_type = eLookupTypeAddress;
3489 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3490 if (m_addr == LLDB_INVALID_ADDRESS)
3491 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3492 break;
3493
3494 case 'n':
3495 m_str = option_arg;
3496 m_type = eLookupTypeFunctionOrSymbol;
3497 break;
3498 }
3499
3500 return error;
3501 }
3502
3503 void
3504 OptionParsingStarting ()
3505 {
3506 m_type = eLookupTypeInvalid;
3507 m_str.clear();
3508 m_addr = LLDB_INVALID_ADDRESS;
3509 }
3510
3511 const OptionDefinition*
3512 GetDefinitions ()
3513 {
3514 return g_option_table;
3515 }
3516
3517 // Options table: Required for subclasses of Options.
3518
3519 static OptionDefinition g_option_table[];
3520
3521 // Instance variables to hold the values for command options.
3522
3523 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3524 std::string m_str; // Holds name lookup
3525 lldb::addr_t m_addr; // Holds the address to lookup
3526 };
3527
3528 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3529 CommandObjectParsed (interpreter,
3530 "target modules show-unwind",
3531 "Show synthesized unwind instructions for a function.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003532 NULL,
3533 eFlagRequiresTarget |
3534 eFlagRequiresProcess |
3535 eFlagProcessMustBeLaunched |
3536 eFlagProcessMustBePaused ),
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003537 m_options (interpreter)
3538 {
3539 }
3540
3541 virtual
3542 ~CommandObjectTargetModulesShowUnwind ()
3543 {
3544 }
3545
3546 virtual
3547 Options *
3548 GetOptions ()
3549 {
3550 return &m_options;
3551 }
3552
3553protected:
3554 bool
3555 DoExecute (Args& command,
3556 CommandReturnObject &result)
3557 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003558 Target *target = m_exe_ctx.GetTargetPtr();
3559 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003560 ABI *abi = NULL;
3561 if (process)
3562 abi = process->GetABI().get();
3563
3564 if (process == NULL)
3565 {
3566 result.AppendError ("You must have a process running to use this command.");
3567 result.SetStatus (eReturnStatusFailed);
3568 return false;
3569 }
3570
3571 ThreadList threads(process->GetThreadList());
3572 if (threads.GetSize() == 0)
3573 {
3574 result.AppendError ("The process must be paused to use this command.");
3575 result.SetStatus (eReturnStatusFailed);
3576 return false;
3577 }
3578
3579 ThreadSP thread(threads.GetThreadAtIndex(0));
3580 if (thread.get() == NULL)
3581 {
3582 result.AppendError ("The process must be paused to use this command.");
3583 result.SetStatus (eReturnStatusFailed);
3584 return false;
3585 }
3586
3587 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3588 {
3589 SymbolContextList sc_list;
Greg Clayton36da2aa2013-01-25 18:06:21 +00003590 size_t num_matches;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003591 ConstString function_name (m_options.m_str.c_str());
3592 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3593 for (uint32_t idx = 0; idx < num_matches; idx++)
3594 {
3595 SymbolContext sc;
3596 sc_list.GetContextAtIndex(idx, sc);
3597 if (sc.symbol == NULL && sc.function == NULL)
3598 continue;
3599 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3600 continue;
3601 AddressRange range;
3602 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3603 continue;
3604 if (!range.GetBaseAddress().IsValid())
3605 continue;
3606 ConstString funcname(sc.GetFunctionName());
3607 if (funcname.IsEmpty())
3608 continue;
3609 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3610 if (abi)
3611 start_addr = abi->FixCodeAddress(start_addr);
3612
3613 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3614 if (func_unwinders_sp.get() == NULL)
3615 continue;
3616
3617 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3618 if (first_non_prologue_insn.IsValid())
3619 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003620 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 +00003621 result.GetOutputStream().Printf ("\n");
3622 }
3623
3624 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3625 if (non_callsite_unwind_plan.get())
3626 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003627 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003628 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3629 result.GetOutputStream().Printf ("\n");
3630 }
3631
3632 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3633 if (callsite_unwind_plan.get())
3634 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003635 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003636 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3637 result.GetOutputStream().Printf ("\n");
3638 }
3639
3640 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3641 if (arch_default_unwind_plan.get())
3642 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003643 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003644 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3645 result.GetOutputStream().Printf ("\n");
3646 }
3647
3648 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3649 if (fast_unwind_plan.get())
3650 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003651 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003652 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3653 result.GetOutputStream().Printf ("\n");
3654 }
3655
3656
3657 result.GetOutputStream().Printf ("\n");
3658 }
3659 }
3660 return result.Succeeded();
3661 }
3662
3663 CommandOptions m_options;
3664};
3665
3666OptionDefinition
3667CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3668{
3669 { LLDB_OPT_SET_1, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function or symbol by name in one or more target modules."},
3670 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3671};
Greg Claytone1f50b92011-05-03 22:09:39 +00003672
3673//----------------------------------------------------------------------
3674// Lookup information in images
3675//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003676class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003677{
3678public:
3679
3680 enum
3681 {
3682 eLookupTypeInvalid = -1,
3683 eLookupTypeAddress = 0,
3684 eLookupTypeSymbol,
3685 eLookupTypeFileLine, // Line is optional
3686 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003687 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003688 eLookupTypeType,
3689 kNumLookupTypes
3690 };
3691
3692 class CommandOptions : public Options
3693 {
3694 public:
3695
3696 CommandOptions (CommandInterpreter &interpreter) :
3697 Options(interpreter)
3698 {
3699 OptionParsingStarting();
3700 }
3701
3702 virtual
3703 ~CommandOptions ()
3704 {
3705 }
3706
3707 virtual Error
3708 SetOptionValue (uint32_t option_idx, const char *option_arg)
3709 {
3710 Error error;
3711
Greg Clayton6475c422012-12-04 00:32:51 +00003712 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone1f50b92011-05-03 22:09:39 +00003713
3714 switch (short_option)
3715 {
3716 case 'a':
Jim Inghamd86cf812013-03-15 23:09:19 +00003717 {
3718 m_type = eLookupTypeAddress;
3719 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3720 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3721 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003722 break;
3723
3724 case 'o':
3725 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3726 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003727 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003728 break;
3729
3730 case 's':
3731 m_str = option_arg;
3732 m_type = eLookupTypeSymbol;
3733 break;
3734
3735 case 'f':
3736 m_file.SetFile (option_arg, false);
3737 m_type = eLookupTypeFileLine;
3738 break;
3739
3740 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003741 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003742 break;
3743
3744 case 'l':
3745 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3746 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003747 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003748 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003749 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003750 m_type = eLookupTypeFileLine;
3751 break;
3752
Greg Clayton2ad894b2012-05-15 18:43:44 +00003753 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003754 m_str = option_arg;
3755 m_type = eLookupTypeFunction;
3756 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003757
3758 case 'n':
3759 m_str = option_arg;
3760 m_type = eLookupTypeFunctionOrSymbol;
3761 break;
3762
Greg Claytone1f50b92011-05-03 22:09:39 +00003763 case 't':
3764 m_str = option_arg;
3765 m_type = eLookupTypeType;
3766 break;
3767
3768 case 'v':
3769 m_verbose = 1;
3770 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003771
3772 case 'A':
3773 m_print_all = true;
3774 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003775
3776 case 'r':
3777 m_use_regex = true;
3778 break;
3779 }
3780
3781 return error;
3782 }
3783
3784 void
3785 OptionParsingStarting ()
3786 {
3787 m_type = eLookupTypeInvalid;
3788 m_str.clear();
3789 m_file.Clear();
3790 m_addr = LLDB_INVALID_ADDRESS;
3791 m_offset = 0;
3792 m_line_number = 0;
3793 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003794 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003795 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003796 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003797 }
3798
3799 const OptionDefinition*
3800 GetDefinitions ()
3801 {
3802 return g_option_table;
3803 }
3804
3805 // Options table: Required for subclasses of Options.
3806
3807 static OptionDefinition g_option_table[];
3808 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3809 std::string m_str; // Holds name lookup
3810 FileSpec m_file; // Files for file lookups
3811 lldb::addr_t m_addr; // Holds the address to lookup
3812 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3813 uint32_t m_line_number; // Line number for file+line lookups
3814 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003815 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003816 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003817 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003818
3819 };
3820
3821 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003822 CommandObjectParsed (interpreter,
3823 "target modules lookup",
3824 "Look up information within executable and dependent shared library images.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003825 NULL,
3826 eFlagRequiresTarget),
Jim Inghamda26bd22012-06-08 21:56:10 +00003827 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003828 {
3829 CommandArgumentEntry arg;
3830 CommandArgumentData file_arg;
3831
3832 // Define the first (and only) variant of this arg.
3833 file_arg.arg_type = eArgTypeFilename;
3834 file_arg.arg_repetition = eArgRepeatStar;
3835
3836 // There is only one variant this argument could be; put it into the argument entry.
3837 arg.push_back (file_arg);
3838
3839 // Push the data for the first argument into the m_arguments vector.
3840 m_arguments.push_back (arg);
3841 }
3842
3843 virtual
3844 ~CommandObjectTargetModulesLookup ()
3845 {
3846 }
3847
3848 virtual Options *
3849 GetOptions ()
3850 {
3851 return &m_options;
3852 }
3853
Sean Callanan56d31ec2012-06-06 20:49:55 +00003854 bool
3855 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3856 {
3857 switch (m_options.m_type)
3858 {
3859 case eLookupTypeAddress:
3860 case eLookupTypeFileLine:
3861 case eLookupTypeFunction:
3862 case eLookupTypeFunctionOrSymbol:
3863 case eLookupTypeSymbol:
3864 default:
3865 return false;
3866 case eLookupTypeType:
3867 break;
3868 }
3869
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003870 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Sean Callanan56d31ec2012-06-06 20:49:55 +00003871
3872 if (!frame)
3873 return false;
3874
3875 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3876
3877 if (!sym_ctx.module_sp)
3878 return false;
3879
3880 switch (m_options.m_type)
3881 {
3882 default:
3883 return false;
3884 case eLookupTypeType:
3885 if (!m_options.m_str.empty())
3886 {
3887 if (LookupTypeHere (m_interpreter,
3888 result.GetOutputStream(),
3889 sym_ctx,
3890 m_options.m_str.c_str(),
3891 m_options.m_use_regex))
3892 {
3893 result.SetStatus(eReturnStatusSuccessFinishResult);
3894 return true;
3895 }
3896 }
3897 break;
3898 }
3899
3900 return true;
3901 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003902
3903 bool
3904 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3905 {
3906 switch (m_options.m_type)
3907 {
3908 case eLookupTypeAddress:
3909 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3910 {
3911 if (LookupAddressInModule (m_interpreter,
3912 result.GetOutputStream(),
3913 module,
3914 eSymbolContextEverything,
3915 m_options.m_addr,
3916 m_options.m_offset,
3917 m_options.m_verbose))
3918 {
3919 result.SetStatus(eReturnStatusSuccessFinishResult);
3920 return true;
3921 }
3922 }
3923 break;
3924
3925 case eLookupTypeSymbol:
3926 if (!m_options.m_str.empty())
3927 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003928 if (LookupSymbolInModule (m_interpreter,
3929 result.GetOutputStream(),
3930 module,
3931 m_options.m_str.c_str(),
3932 m_options.m_use_regex,
3933 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003934 {
3935 result.SetStatus(eReturnStatusSuccessFinishResult);
3936 return true;
3937 }
3938 }
3939 break;
3940
3941 case eLookupTypeFileLine:
3942 if (m_options.m_file)
3943 {
3944
3945 if (LookupFileAndLineInModule (m_interpreter,
3946 result.GetOutputStream(),
3947 module,
3948 m_options.m_file,
3949 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003950 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003951 m_options.m_verbose))
3952 {
3953 result.SetStatus(eReturnStatusSuccessFinishResult);
3954 return true;
3955 }
3956 }
3957 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003958
3959 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003960 case eLookupTypeFunction:
3961 if (!m_options.m_str.empty())
3962 {
3963 if (LookupFunctionInModule (m_interpreter,
3964 result.GetOutputStream(),
3965 module,
3966 m_options.m_str.c_str(),
3967 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003968 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003969 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003970 m_options.m_verbose))
3971 {
3972 result.SetStatus(eReturnStatusSuccessFinishResult);
3973 return true;
3974 }
3975 }
3976 break;
3977
Greg Clayton2ad894b2012-05-15 18:43:44 +00003978
Greg Claytone1f50b92011-05-03 22:09:39 +00003979 case eLookupTypeType:
3980 if (!m_options.m_str.empty())
3981 {
3982 if (LookupTypeInModule (m_interpreter,
3983 result.GetOutputStream(),
3984 module,
3985 m_options.m_str.c_str(),
3986 m_options.m_use_regex))
3987 {
3988 result.SetStatus(eReturnStatusSuccessFinishResult);
3989 return true;
3990 }
3991 }
3992 break;
3993
3994 default:
3995 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3996 syntax_error = true;
3997 break;
3998 }
3999
4000 result.SetStatus (eReturnStatusFailed);
4001 return false;
4002 }
4003
Jim Inghamda26bd22012-06-08 21:56:10 +00004004protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00004005 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004006 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00004007 CommandReturnObject &result)
4008 {
4009 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4010 if (target == NULL)
4011 {
4012 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4013 result.SetStatus (eReturnStatusFailed);
4014 return false;
4015 }
4016 else
4017 {
4018 bool syntax_error = false;
4019 uint32_t i;
4020 uint32_t num_successful_lookups = 0;
4021 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4022 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4023 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4024 // Dump all sections for all modules images
4025
4026 if (command.GetArgumentCount() == 0)
4027 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004028 ModuleSP current_module;
4029
4030 // Where it is possible to look in the current symbol context
4031 // first, try that. If this search was successful and --all
4032 // was not passed, don't print anything else.
4033 if (LookupHere (m_interpreter, result, syntax_error))
4034 {
4035 result.GetOutputStream().EOL();
4036 num_successful_lookups++;
4037 if (!m_options.m_print_all)
4038 {
4039 result.SetStatus (eReturnStatusSuccessFinishResult);
4040 return result.Succeeded();
4041 }
4042 }
4043
4044 // Dump all sections for all other modules
4045
Enrico Granata146d9522012-11-08 02:22:02 +00004046 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00004047 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00004048 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00004049 if (num_modules > 0)
4050 {
4051 for (i = 0; i<num_modules && syntax_error == false; ++i)
4052 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004053 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
4054
4055 if (module_pointer != current_module.get() &&
4056 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004057 {
4058 result.GetOutputStream().EOL();
4059 num_successful_lookups++;
4060 }
4061 }
4062 }
4063 else
4064 {
4065 result.AppendError ("the target has no associated executable images");
4066 result.SetStatus (eReturnStatusFailed);
4067 return false;
4068 }
4069 }
4070 else
4071 {
4072 // Dump specified images (by basename or fullpath)
4073 const char *arg_cstr;
4074 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4075 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004076 ModuleList module_list;
4077 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4078 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00004079 {
Jason Molendabf41e192012-10-04 22:47:07 +00004080 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00004081 {
Jason Molendabf41e192012-10-04 22:47:07 +00004082 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00004083 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00004084 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004085 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004086 {
4087 result.GetOutputStream().EOL();
4088 num_successful_lookups++;
4089 }
4090 }
4091 }
4092 }
4093 else
4094 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4095 }
4096 }
4097
4098 if (num_successful_lookups > 0)
4099 result.SetStatus (eReturnStatusSuccessFinishResult);
4100 else
4101 result.SetStatus (eReturnStatusFailed);
4102 }
4103 return result.Succeeded();
4104 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004105
4106 CommandOptions m_options;
4107};
4108
4109OptionDefinition
4110CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4111{
Jim Inghamd86cf812013-03-15 23:09:19 +00004112 { 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 +00004113 { 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 +00004114 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4115 /* 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 +00004116 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4117 { 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."},
4118 { 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."},
4119 { 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 +00004120 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00004121 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4122 { 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."},
4123 { 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."},
4124 { 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."},
4125 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4126 { 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."},
4127 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00004128};
Chris Lattner24943d22010-06-08 16:52:24 +00004129
4130
Jim Inghamd60d94a2011-03-11 03:53:59 +00004131#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00004132
4133//-------------------------------------------------------------------------
4134// CommandObjectMultiwordImageSearchPaths
4135//-------------------------------------------------------------------------
4136
Greg Claytone1f50b92011-05-03 22:09:39 +00004137class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00004138{
4139public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004140
4141 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4142 CommandObjectMultiword (interpreter,
4143 "target modules search-paths",
4144 "A set of commands for operating on debugger target image search paths.",
4145 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004146 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004147 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4148 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4149 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4150 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4151 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004152 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004153
4154 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004155 {
4156 }
4157};
4158
Greg Claytone1f50b92011-05-03 22:09:39 +00004159
4160
4161#pragma mark CommandObjectTargetModules
4162
4163//-------------------------------------------------------------------------
4164// CommandObjectTargetModules
4165//-------------------------------------------------------------------------
4166
4167class CommandObjectTargetModules : public CommandObjectMultiword
4168{
4169public:
4170 //------------------------------------------------------------------
4171 // Constructors and Destructors
4172 //------------------------------------------------------------------
4173 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4174 CommandObjectMultiword (interpreter,
4175 "target modules",
4176 "A set of commands for accessing information for one or more target modules.",
4177 "target modules <sub-command> ...")
4178 {
4179 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4180 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004181 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4182 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4183 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4184 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004185 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004186
4187 }
4188 virtual
4189 ~CommandObjectTargetModules()
4190 {
4191 }
4192
4193private:
4194 //------------------------------------------------------------------
4195 // For CommandObjectTargetModules only
4196 //------------------------------------------------------------------
4197 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4198};
4199
4200
Greg Clayton3508c382012-02-24 01:59:29 +00004201
Jim Inghamda26bd22012-06-08 21:56:10 +00004202class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004203{
4204public:
4205 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004206 CommandObjectParsed (interpreter,
4207 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004208 "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 +00004209 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Clayton437b5bc2012-09-27 22:26:11 +00004210 m_option_group (interpreter),
4211 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4212 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4213
Greg Clayton3508c382012-02-24 01:59:29 +00004214 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004215 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4216 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4217 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4218 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004219 }
4220
4221 virtual
4222 ~CommandObjectTargetSymbolsAdd ()
4223 {
4224 }
4225
Greg Clayton36da2aa2013-01-25 18:06:21 +00004226 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00004227 HandleArgumentCompletion (Args &input,
4228 int &cursor_index,
4229 int &cursor_char_position,
4230 OptionElementVector &opt_element_vector,
4231 int match_start_point,
4232 int max_return_elements,
4233 bool &word_complete,
4234 StringList &matches)
4235 {
4236 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4237 completion_str.erase (cursor_char_position);
4238
4239 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4240 CommandCompletions::eDiskFileCompletion,
4241 completion_str.c_str(),
4242 match_start_point,
4243 max_return_elements,
4244 NULL,
4245 word_complete,
4246 matches);
4247 return matches.GetSize();
4248 }
4249
Greg Clayton437b5bc2012-09-27 22:26:11 +00004250 virtual Options *
4251 GetOptions ()
4252 {
4253 return &m_option_group;
4254 }
4255
4256
Jim Inghamda26bd22012-06-08 21:56:10 +00004257protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004258
4259 bool
4260 AddModuleSymbols (Target *target,
Greg Claytonc92fded2012-12-12 01:15:30 +00004261 ModuleSpec &module_spec,
Greg Clayton437b5bc2012-09-27 22:26:11 +00004262 bool &flush,
4263 CommandReturnObject &result)
4264 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004265 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4266 if (symbol_fspec)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004267 {
4268 char symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004269 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
4270
4271 if (!module_spec.GetUUID().IsValid())
4272 {
4273 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4274 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4275 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004276 // We now have a module that represents a symbol file
4277 // that can be used for a module that might exist in the
4278 // current target, so we need to find that module in the
4279 // target
Greg Claytonc92fded2012-12-12 01:15:30 +00004280 ModuleList matching_module_list;
Greg Claytond9735a12013-01-11 23:44:27 +00004281 size_t num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4282 while (num_matches == 0)
4283 {
4284 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4285 // Empty string returned, lets bail
4286 if (!filename_no_extension)
4287 break;
4288
4289 // Check if there was no extension to strip and the basename is the same
4290 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4291 break;
4292
4293 // Replace basename with one less extension
4294 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4295
4296 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4297 }
4298
Greg Claytonc92fded2012-12-12 01:15:30 +00004299 if (num_matches > 1)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004300 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004301 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4302 }
4303 else if (num_matches == 1)
4304 {
4305 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
4306
Greg Clayton437b5bc2012-09-27 22:26:11 +00004307 // The module has not yet created its symbol vendor, we can just
4308 // give the existing target module the symfile path to use for
4309 // when it decides to create it!
Greg Claytonc92fded2012-12-12 01:15:30 +00004310 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004311
Greg Clayton18809182012-12-14 02:15:00 +00004312 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Claytonc92fded2012-12-12 01:15:30 +00004313 if (symbol_vendor)
4314 {
4315 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4316
4317 if (symbol_file)
4318 {
4319 ObjectFile *object_file = symbol_file->GetObjectFile();
4320
4321 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4322 {
4323 // Provide feedback that the symfile has been successfully added.
4324 const FileSpec &module_fs = module_sp->GetFileSpec();
4325 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s/%s'\n",
4326 symfile_path,
4327 module_fs.GetDirectory().AsCString(),
4328 module_fs.GetFilename().AsCString());
4329
4330 // Let clients know something changed in the module
4331 // if it is currently loaded
4332 ModuleList module_list;
4333 module_list.Append (module_sp);
4334 target->ModulesDidLoad (module_list);
Greg Claytond9735a12013-01-11 23:44:27 +00004335
4336 // Make sure we load any scripting resources that may be embedded
4337 // in the debug info files in case the platform supports that.
4338 Error error;
4339 module_sp->LoadScriptingResourceInTarget (target, error);
4340
Greg Claytonc92fded2012-12-12 01:15:30 +00004341 flush = true;
4342 result.SetStatus (eReturnStatusSuccessFinishResult);
4343 return true;
4344 }
4345 }
4346 }
4347 // Clear the symbol file spec if anything went wrong
4348 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Claytonc92fded2012-12-12 01:15:30 +00004349 }
4350
4351 if (module_spec.GetUUID().IsValid())
4352 {
4353 StreamString ss_symfile_uuid;
4354 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4355 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4356 symfile_path,
4357 ss_symfile_uuid.GetData(),
4358 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4359 ? "\n please specify the full path to the symbol file"
4360 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004361 }
4362 else
4363 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004364 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4365 symfile_path,
4366 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4367 ? "\n please specify the full path to the symbol file"
4368 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004369 }
4370 }
4371 else
4372 {
4373 result.AppendError ("one or more executable image paths must be specified");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004374 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004375 result.SetStatus (eReturnStatusFailed);
4376 return false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004377 }
4378
Greg Clayton3508c382012-02-24 01:59:29 +00004379 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004380 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004381 CommandReturnObject &result)
4382 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004383 Target *target = m_exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004384 result.SetStatus (eReturnStatusFailed);
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004385 bool flush = false;
4386 ModuleSpec module_spec;
4387 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4388 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4389 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004390
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004391 const size_t argc = args.GetArgumentCount();
4392 if (argc == 0)
4393 {
4394 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004395 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004396 bool success = false;
4397 bool error_set = false;
4398 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004399 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004400 Process *process = m_exe_ctx.GetProcessPtr();
4401 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004402 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004403 const StateType process_state = process->GetState();
4404 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004405 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004406 StackFrame *frame = m_exe_ctx.GetFramePtr();
4407 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004408 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004409 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4410 if (frame_module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004411 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004412 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Clayton437b5bc2012-09-27 22:26:11 +00004413 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004414 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4415 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004416 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004417 module_spec.GetUUID() = frame_module_sp->GetUUID();
4418 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Clayton3508c382012-02-24 01:59:29 +00004419 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004420 else
4421 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004422 result.AppendError ("frame has no module");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004423 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004424 }
Greg Clayton3508c382012-02-24 01:59:29 +00004425 }
4426 else
4427 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004428 result.AppendError ("invalid current frame");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004429 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004430 }
Greg Clayton3508c382012-02-24 01:59:29 +00004431 }
4432 else
4433 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004434 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Clayton437b5bc2012-09-27 22:26:11 +00004435 error_set = true;
4436 }
4437 }
4438 else
4439 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004440 result.AppendError ("a process must exist in order to use the --frame option");
4441 error_set = true;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004442 }
4443 }
4444 else
4445 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004446 if (uuid_option_set)
4447 {
4448 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4449 success |= module_spec.GetUUID().IsValid();
4450 }
4451 else if (file_option_set)
4452 {
4453 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4454 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4455 if (module_sp)
4456 {
4457 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4458 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4459 module_spec.GetUUID() = module_sp->GetUUID();
4460 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4461 }
4462 else
4463 {
4464 module_spec.GetArchitecture() = target->GetArchitecture();
4465 }
4466 success |= module_spec.GetFileSpec().Exists();
4467 }
4468 }
4469
4470 if (success)
4471 {
4472 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4473 {
4474 if (module_spec.GetSymbolFileSpec())
4475 success = AddModuleSymbols (target, module_spec, flush, result);
4476 }
4477 }
4478
4479 if (!success && !error_set)
4480 {
4481 StreamString error_strm;
4482 if (uuid_option_set)
4483 {
4484 error_strm.PutCString("unable to find debug symbols for UUID ");
4485 module_spec.GetUUID().Dump (&error_strm);
4486 }
4487 else if (file_option_set)
4488 {
4489 error_strm.PutCString("unable to find debug symbols for the executable file ");
4490 error_strm << module_spec.GetFileSpec();
4491 }
4492 else if (frame_option_set)
4493 {
4494 error_strm.PutCString("unable to find debug symbols for the current frame");
4495 }
4496 result.AppendError (error_strm.GetData());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004497 }
4498 }
4499 else
4500 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004501 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4502 }
4503 }
4504 else
4505 {
4506 if (uuid_option_set)
4507 {
4508 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4509 }
4510 else if (file_option_set)
4511 {
4512 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4513 }
4514 else if (frame_option_set)
4515 {
4516 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4517 }
4518 else
4519 {
4520 PlatformSP platform_sp (target->GetPlatform());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004521
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004522 for (size_t i=0; i<argc; ++i)
4523 {
4524 const char *symfile_path = args.GetArgumentAtIndex(i);
4525 if (symfile_path)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004526 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004527 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4528 if (platform_sp)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004529 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004530 FileSpec symfile_spec;
4531 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4532 module_spec.GetSymbolFileSpec() = symfile_spec;
4533 }
4534
4535 ArchSpec arch;
4536 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004537
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004538 if (symfile_exists)
4539 {
4540 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004541 break;
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004542 }
4543 else
4544 {
4545 char resolved_symfile_path[PATH_MAX];
4546 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4547 {
4548 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4549 {
4550 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4551 break;
4552 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004553 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004554 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4555 break;
Greg Clayton3508c382012-02-24 01:59:29 +00004556 }
4557 }
4558 }
4559 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004560 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004561
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004562 if (flush)
4563 {
4564 Process *process = m_exe_ctx.GetProcessPtr();
4565 if (process)
4566 process->Flush();
Greg Clayton3508c382012-02-24 01:59:29 +00004567 }
4568 return result.Succeeded();
4569 }
4570
Greg Clayton437b5bc2012-09-27 22:26:11 +00004571 OptionGroupOptions m_option_group;
4572 OptionGroupUUID m_uuid_option_group;
4573 OptionGroupFile m_file_option;
4574 OptionGroupBoolean m_current_frame_option;
4575
4576
Greg Clayton3508c382012-02-24 01:59:29 +00004577};
4578
4579
4580#pragma mark CommandObjectTargetSymbols
4581
4582//-------------------------------------------------------------------------
4583// CommandObjectTargetSymbols
4584//-------------------------------------------------------------------------
4585
4586class CommandObjectTargetSymbols : public CommandObjectMultiword
4587{
4588public:
4589 //------------------------------------------------------------------
4590 // Constructors and Destructors
4591 //------------------------------------------------------------------
4592 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4593 CommandObjectMultiword (interpreter,
4594 "target symbols",
4595 "A set of commands for adding and managing debug symbol files.",
4596 "target symbols <sub-command> ...")
4597 {
4598 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4599
4600 }
4601 virtual
4602 ~CommandObjectTargetSymbols()
4603 {
4604 }
4605
4606private:
4607 //------------------------------------------------------------------
4608 // For CommandObjectTargetModules only
4609 //------------------------------------------------------------------
4610 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4611};
4612
4613
Jim Inghamd60d94a2011-03-11 03:53:59 +00004614#pragma mark CommandObjectTargetStopHookAdd
4615
4616//-------------------------------------------------------------------------
4617// CommandObjectTargetStopHookAdd
4618//-------------------------------------------------------------------------
4619
Jim Inghamda26bd22012-06-08 21:56:10 +00004620class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004621{
4622public:
4623
4624 class CommandOptions : public Options
4625 {
4626 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004627 CommandOptions (CommandInterpreter &interpreter) :
4628 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004629 m_line_start(0),
4630 m_line_end (UINT_MAX),
4631 m_func_name_type_mask (eFunctionNameTypeAuto),
4632 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004633 m_thread_specified (false),
4634 m_use_one_liner (false),
4635 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004636 {
4637 }
4638
4639 ~CommandOptions () {}
4640
Greg Claytonb3448432011-03-24 21:19:54 +00004641 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004642 GetDefinitions ()
4643 {
4644 return g_option_table;
4645 }
4646
4647 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004648 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004649 {
4650 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00004651 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004652 bool success;
4653
4654 switch (short_option)
4655 {
4656 case 'c':
4657 m_class_name = option_arg;
4658 m_sym_ctx_specified = true;
4659 break;
4660
4661 case 'e':
4662 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4663 if (!success)
4664 {
Greg Clayton9c236732011-10-26 00:56:27 +00004665 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004666 break;
4667 }
4668 m_sym_ctx_specified = true;
4669 break;
4670
4671 case 'l':
4672 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4673 if (!success)
4674 {
Greg Clayton9c236732011-10-26 00:56:27 +00004675 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004676 break;
4677 }
4678 m_sym_ctx_specified = true;
4679 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004680
4681 case 'i':
4682 m_no_inlines = true;
4683 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004684
4685 case 'n':
4686 m_function_name = option_arg;
4687 m_func_name_type_mask |= eFunctionNameTypeAuto;
4688 m_sym_ctx_specified = true;
4689 break;
4690
4691 case 'f':
4692 m_file_name = option_arg;
4693 m_sym_ctx_specified = true;
4694 break;
4695 case 's':
4696 m_module_name = option_arg;
4697 m_sym_ctx_specified = true;
4698 break;
4699 case 't' :
4700 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004701 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004702 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004703 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004704 m_thread_specified = true;
4705 }
4706 break;
4707 case 'T':
4708 m_thread_name = option_arg;
4709 m_thread_specified = true;
4710 break;
4711 case 'q':
4712 m_queue_name = option_arg;
4713 m_thread_specified = true;
4714 break;
4715 case 'x':
4716 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004717 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004718 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004719 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004720 m_thread_specified = true;
4721 }
4722 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004723 case 'o':
4724 m_use_one_liner = true;
4725 m_one_liner = option_arg;
4726 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004727 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004728 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004729 break;
4730 }
4731 return error;
4732 }
4733
4734 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004735 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004736 {
4737 m_class_name.clear();
4738 m_function_name.clear();
4739 m_line_start = 0;
4740 m_line_end = UINT_MAX;
4741 m_file_name.clear();
4742 m_module_name.clear();
4743 m_func_name_type_mask = eFunctionNameTypeAuto;
4744 m_thread_id = LLDB_INVALID_THREAD_ID;
4745 m_thread_index = UINT32_MAX;
4746 m_thread_name.clear();
4747 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004748
4749 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004750 m_sym_ctx_specified = false;
4751 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004752
4753 m_use_one_liner = false;
4754 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004755 }
4756
4757
Greg Claytonb3448432011-03-24 21:19:54 +00004758 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004759
4760 std::string m_class_name;
4761 std::string m_function_name;
4762 uint32_t m_line_start;
4763 uint32_t m_line_end;
4764 std::string m_file_name;
4765 std::string m_module_name;
4766 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4767 lldb::tid_t m_thread_id;
4768 uint32_t m_thread_index;
4769 std::string m_thread_name;
4770 std::string m_queue_name;
4771 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004772 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004773 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004774 // Instance variables to hold the values for one_liner options.
4775 bool m_use_one_liner;
4776 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004777 };
4778
4779 Options *
4780 GetOptions ()
4781 {
4782 return &m_options;
4783 }
4784
4785 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004786 CommandObjectParsed (interpreter,
4787 "target stop-hook add ",
4788 "Add a hook to be executed when the target stops.",
4789 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004790 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004791 {
4792 }
4793
4794 ~CommandObjectTargetStopHookAdd ()
4795 {
4796 }
4797
4798 static size_t
4799 ReadCommandsCallbackFunction (void *baton,
4800 InputReader &reader,
4801 lldb::InputReaderAction notification,
4802 const char *bytes,
4803 size_t bytes_len)
4804 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004805 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004806 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004807 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004808 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004809
4810 switch (notification)
4811 {
4812 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004813 if (!batch_mode)
4814 {
4815 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4816 if (reader.GetPrompt())
4817 out_stream->Printf ("%s", reader.GetPrompt());
4818 out_stream->Flush();
4819 }
Jim Inghame15511a2011-05-05 01:03:36 +00004820 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004821 break;
4822
4823 case eInputReaderDeactivate:
4824 break;
4825
4826 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004827 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004828 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004829 out_stream->Printf ("%s", reader.GetPrompt());
4830 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004831 }
Jim Inghame15511a2011-05-05 01:03:36 +00004832 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004833 break;
4834
Caroline Tice4a348082011-05-02 20:41:46 +00004835 case eInputReaderAsynchronousOutputWritten:
4836 break;
4837
Jim Inghamd60d94a2011-03-11 03:53:59 +00004838 case eInputReaderGotToken:
4839 if (bytes && bytes_len && baton)
4840 {
4841 StringList *commands = new_stop_hook->GetCommandPointer();
4842 if (commands)
4843 {
4844 commands->AppendString (bytes, bytes_len);
4845 }
4846 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004847 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004848 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004849 out_stream->Printf ("%s", reader.GetPrompt());
4850 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004851 }
4852 break;
4853
4854 case eInputReaderInterrupt:
4855 {
4856 // Finish, and cancel the stop hook.
4857 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004858 if (!batch_mode)
4859 {
4860 out_stream->Printf ("Stop hook cancelled.\n");
4861 out_stream->Flush();
4862 }
4863
Jim Inghamd60d94a2011-03-11 03:53:59 +00004864 reader.SetIsDone (true);
4865 }
Jim Inghame15511a2011-05-05 01:03:36 +00004866 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004867 break;
4868
4869 case eInputReaderEndOfFile:
4870 reader.SetIsDone (true);
4871 break;
4872
4873 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004874 if (!got_interrupted && !batch_mode)
4875 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004876 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004877 out_stream->Flush();
4878 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004879 break;
4880 }
4881
4882 return bytes_len;
4883 }
4884
Jim Inghamda26bd22012-06-08 21:56:10 +00004885protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004886 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004887 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004888 {
4889 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4890 if (target)
4891 {
4892 Target::StopHookSP new_hook_sp;
4893 target->AddStopHook (new_hook_sp);
4894
4895 // First step, make the specifier.
4896 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4897 if (m_options.m_sym_ctx_specified)
4898 {
4899 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4900
4901 if (!m_options.m_module_name.empty())
4902 {
4903 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4904 }
4905
4906 if (!m_options.m_class_name.empty())
4907 {
4908 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4909 }
4910
4911 if (!m_options.m_file_name.empty())
4912 {
4913 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4914 }
4915
4916 if (m_options.m_line_start != 0)
4917 {
4918 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4919 }
4920
4921 if (m_options.m_line_end != UINT_MAX)
4922 {
4923 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4924 }
4925
4926 if (!m_options.m_function_name.empty())
4927 {
4928 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4929 }
4930 }
4931
4932 if (specifier_ap.get())
4933 new_hook_sp->SetSpecifier (specifier_ap.release());
4934
4935 // Next see if any of the thread options have been entered:
4936
4937 if (m_options.m_thread_specified)
4938 {
4939 ThreadSpec *thread_spec = new ThreadSpec();
4940
4941 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4942 {
4943 thread_spec->SetTID (m_options.m_thread_id);
4944 }
4945
4946 if (m_options.m_thread_index != UINT32_MAX)
4947 thread_spec->SetIndex (m_options.m_thread_index);
4948
4949 if (!m_options.m_thread_name.empty())
4950 thread_spec->SetName (m_options.m_thread_name.c_str());
4951
4952 if (!m_options.m_queue_name.empty())
4953 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4954
4955 new_hook_sp->SetThreadSpecifier (thread_spec);
4956
4957 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004958 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004959 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004960 // Use one-liner.
4961 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004962 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004963 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004964 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004965 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004966 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4967 // the new stop hook's command string.
4968 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4969 if (!reader_sp)
4970 {
4971 result.AppendError("out of memory\n");
4972 result.SetStatus (eReturnStatusFailed);
4973 target->RemoveStopHookByID (new_hook_sp->GetID());
4974 return false;
4975 }
4976
4977 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4978 new_hook_sp.get(), // baton
4979 eInputReaderGranularityLine, // token size, to pass to callback function
4980 "DONE", // end token
4981 "> ", // prompt
4982 true)); // echo input
4983 if (!err.Success())
4984 {
4985 result.AppendError (err.AsCString());
4986 result.SetStatus (eReturnStatusFailed);
4987 target->RemoveStopHookByID (new_hook_sp->GetID());
4988 return false;
4989 }
4990 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004991 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004992 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4993 }
4994 else
4995 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004996 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004997 result.SetStatus (eReturnStatusFailed);
4998 }
4999
5000 return result.Succeeded();
5001 }
5002private:
5003 CommandOptions m_options;
5004};
5005
Greg Claytonb3448432011-03-24 21:19:54 +00005006OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00005007CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5008{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005009 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00005010 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00005011 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
5012 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005013 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005014 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005015 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005016 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005017 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005018 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005019 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005020 "The stop hook is run only for threads in the queue whose name is given by this argument."},
5021 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
5022 "Specify the source file within which the stop-hook is to be run." },
5023 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
5024 "Set the start of the line range for which the stop-hook is to be run."},
5025 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
5026 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005027 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005028 "Specify the class within which the stop-hook is to be run." },
5029 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
5030 "Set the function name within which the stop hook will be run." },
5031 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5032};
5033
5034#pragma mark CommandObjectTargetStopHookDelete
5035
5036//-------------------------------------------------------------------------
5037// CommandObjectTargetStopHookDelete
5038//-------------------------------------------------------------------------
5039
Jim Inghamda26bd22012-06-08 21:56:10 +00005040class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005041{
5042public:
5043
5044 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005045 CommandObjectParsed (interpreter,
5046 "target stop-hook delete",
5047 "Delete a stop-hook.",
5048 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005049 {
5050 }
5051
5052 ~CommandObjectTargetStopHookDelete ()
5053 {
5054 }
5055
Jim Inghamda26bd22012-06-08 21:56:10 +00005056protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005057 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005058 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005059 {
5060 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5061 if (target)
5062 {
5063 // FIXME: see if we can use the breakpoint id style parser?
5064 size_t num_args = command.GetArgumentCount();
5065 if (num_args == 0)
5066 {
5067 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5068 {
5069 result.SetStatus (eReturnStatusFailed);
5070 return false;
5071 }
5072 else
5073 {
5074 target->RemoveAllStopHooks();
5075 }
5076 }
5077 else
5078 {
5079 bool success;
5080 for (size_t i = 0; i < num_args; i++)
5081 {
5082 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5083 if (!success)
5084 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005085 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005086 result.SetStatus(eReturnStatusFailed);
5087 return false;
5088 }
5089 success = target->RemoveStopHookByID (user_id);
5090 if (!success)
5091 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005092 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005093 result.SetStatus(eReturnStatusFailed);
5094 return false;
5095 }
5096 }
5097 }
5098 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5099 }
5100 else
5101 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005102 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005103 result.SetStatus (eReturnStatusFailed);
5104 }
5105
5106 return result.Succeeded();
5107 }
5108};
5109#pragma mark CommandObjectTargetStopHookEnableDisable
5110
5111//-------------------------------------------------------------------------
5112// CommandObjectTargetStopHookEnableDisable
5113//-------------------------------------------------------------------------
5114
Jim Inghamda26bd22012-06-08 21:56:10 +00005115class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005116{
5117public:
5118
5119 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005120 CommandObjectParsed (interpreter,
5121 name,
5122 help,
5123 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00005124 m_enable (enable)
5125 {
5126 }
5127
5128 ~CommandObjectTargetStopHookEnableDisable ()
5129 {
5130 }
5131
Jim Inghamda26bd22012-06-08 21:56:10 +00005132protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005133 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005134 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005135 {
5136 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5137 if (target)
5138 {
5139 // FIXME: see if we can use the breakpoint id style parser?
5140 size_t num_args = command.GetArgumentCount();
5141 bool success;
5142
5143 if (num_args == 0)
5144 {
5145 target->SetAllStopHooksActiveState (m_enable);
5146 }
5147 else
5148 {
5149 for (size_t i = 0; i < num_args; i++)
5150 {
5151 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5152 if (!success)
5153 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005154 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005155 result.SetStatus(eReturnStatusFailed);
5156 return false;
5157 }
5158 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5159 if (!success)
5160 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005161 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005162 result.SetStatus(eReturnStatusFailed);
5163 return false;
5164 }
5165 }
5166 }
5167 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5168 }
5169 else
5170 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005171 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005172 result.SetStatus (eReturnStatusFailed);
5173 }
5174 return result.Succeeded();
5175 }
5176private:
5177 bool m_enable;
5178};
5179
5180#pragma mark CommandObjectTargetStopHookList
5181
5182//-------------------------------------------------------------------------
5183// CommandObjectTargetStopHookList
5184//-------------------------------------------------------------------------
5185
Jim Inghamda26bd22012-06-08 21:56:10 +00005186class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005187{
5188public:
5189
5190 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005191 CommandObjectParsed (interpreter,
5192 "target stop-hook list",
5193 "List all stop-hooks.",
5194 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005195 {
5196 }
5197
5198 ~CommandObjectTargetStopHookList ()
5199 {
5200 }
5201
Jim Inghamda26bd22012-06-08 21:56:10 +00005202protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005203 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005204 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005205 {
5206 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005207 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005208 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005209 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005210 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005211 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005212 }
5213
5214 size_t num_hooks = target->GetNumStopHooks ();
5215 if (num_hooks == 0)
5216 {
5217 result.GetOutputStream().PutCString ("No stop hooks.\n");
5218 }
5219 else
5220 {
5221 for (size_t i = 0; i < num_hooks; i++)
5222 {
5223 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5224 if (i > 0)
5225 result.GetOutputStream().PutCString ("\n");
5226 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5227 }
5228 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005229 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005230 return result.Succeeded();
5231 }
5232};
5233
5234#pragma mark CommandObjectMultiwordTargetStopHooks
5235//-------------------------------------------------------------------------
5236// CommandObjectMultiwordTargetStopHooks
5237//-------------------------------------------------------------------------
5238
5239class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5240{
5241public:
5242
5243 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5244 CommandObjectMultiword (interpreter,
5245 "target stop-hook",
5246 "A set of commands for operating on debugger target stop-hooks.",
5247 "target stop-hook <subcommand> [<subcommand-options>]")
5248 {
5249 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5250 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5251 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5252 false,
5253 "target stop-hook disable [<id>]",
5254 "Disable a stop-hook.",
5255 "target stop-hook disable")));
5256 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5257 true,
5258 "target stop-hook enable [<id>]",
5259 "Enable a stop-hook.",
5260 "target stop-hook enable")));
5261 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5262 }
5263
5264 ~CommandObjectMultiwordTargetStopHooks()
5265 {
5266 }
5267};
5268
5269
Chris Lattner24943d22010-06-08 16:52:24 +00005270
5271#pragma mark CommandObjectMultiwordTarget
5272
5273//-------------------------------------------------------------------------
5274// CommandObjectMultiwordTarget
5275//-------------------------------------------------------------------------
5276
Greg Clayton63094e02010-06-23 01:19:29 +00005277CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005278 CommandObjectMultiword (interpreter,
5279 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005280 "A set of commands for operating on debugger targets.",
5281 "target <subcommand> [<subcommand-options>]")
5282{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005283
5284 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005285 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005286 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5287 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005288 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005289 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005290 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005291 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005292}
5293
5294CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5295{
5296}
5297
Greg Claytonabe0fed2011-04-18 08:33:37 +00005298