blob: 0ba1dd4751d3f52b79ff37e82b3e9c2e4ab37245 [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)
Greg Clayton0e191602013-05-10 21:47:16 +000084 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Greg Claytonabe0fed2011-04-18 08:33:37 +000085
86 ProcessSP process_sp (target->GetProcessSP());
87 bool show_process_status = false;
88 if (process_sp)
89 {
90 lldb::pid_t pid = process_sp->GetID();
91 StateType state = process_sp->GetState();
92 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000093 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000094 const char *state_cstr = StateAsCString (state);
95 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malea5f35a4b2012-11-29 21:49:15 +000096 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000097 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
98 }
99 if (properties > 0)
100 strm.PutCString (" )\n");
101 else
102 strm.EOL();
103 if (show_process_status)
104 {
105 const bool only_threads_with_stop_reason = true;
106 const uint32_t start_frame = 0;
107 const uint32_t num_frames = 1;
108 const uint32_t num_frames_with_source = 1;
109 process_sp->GetStatus (strm);
110 process_sp->GetThreadStatus (strm,
111 only_threads_with_stop_reason,
112 start_frame,
113 num_frames,
114 num_frames_with_source);
115
116 }
117}
118
119static uint32_t
120DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
121{
122 const uint32_t num_targets = target_list.GetNumTargets();
123 if (num_targets)
124 {
125 TargetSP selected_target_sp (target_list.GetSelectedTarget());
126 strm.PutCString ("Current targets:\n");
127 for (uint32_t i=0; i<num_targets; ++i)
128 {
129 TargetSP target_sp (target_list.GetTargetAtIndex (i));
130 if (target_sp)
131 {
132 bool is_selected = target_sp.get() == selected_target_sp.get();
133 DumpTargetInfo (i,
134 target_sp.get(),
135 is_selected ? "* " : " ",
136 show_stopped_process_status,
137 strm);
138 }
139 }
140 }
141 return num_targets;
142}
143#pragma mark CommandObjectTargetCreate
144
145//-------------------------------------------------------------------------
146// "target create"
147//-------------------------------------------------------------------------
148
Jim Inghamda26bd22012-06-08 21:56:10 +0000149class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000150{
151public:
152 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000153 CommandObjectParsed (interpreter,
154 "target create",
155 "Create a target using the argument as the main executable.",
156 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000157 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000158 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000159 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Greg Claytonec9c2d22012-11-30 19:05:35 +0000160 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
161 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
162 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000163 {
164 CommandArgumentEntry arg;
165 CommandArgumentData file_arg;
166
167 // Define the first (and only) variant of this arg.
168 file_arg.arg_type = eArgTypeFilename;
169 file_arg.arg_repetition = eArgRepeatPlain;
170
171 // There is only one variant this argument could be; put it into the argument entry.
172 arg.push_back (file_arg);
173
174 // Push the data for the first argument into the m_arguments vector.
175 m_arguments.push_back (arg);
176
Greg Clayton801417e2011-07-07 01:59:51 +0000177 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000178 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000179 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +0000180 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
181 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000182 m_option_group.Finalize();
183 }
184
185 ~CommandObjectTargetCreate ()
186 {
187 }
188
189 Options *
190 GetOptions ()
191 {
192 return &m_option_group;
193 }
194
Greg Clayton36da2aa2013-01-25 18:06:21 +0000195 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +0000196 HandleArgumentCompletion (Args &input,
197 int &cursor_index,
198 int &cursor_char_position,
199 OptionElementVector &opt_element_vector,
200 int match_start_point,
201 int max_return_elements,
202 bool &word_complete,
203 StringList &matches)
204 {
205 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
206 completion_str.erase (cursor_char_position);
207
208 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
209 CommandCompletions::eDiskFileCompletion,
210 completion_str.c_str(),
211 match_start_point,
212 max_return_elements,
213 NULL,
214 word_complete,
215 matches);
216 return matches.GetSize();
217 }
218
219protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000220 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000221 DoExecute (Args& command, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000222 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000223 const size_t argc = command.GetArgumentCount();
Greg Clayton46c9a352012-02-09 06:16:32 +0000224 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
225
226 if (argc == 1 || core_file)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000227 {
Greg Claytonec9c2d22012-11-30 19:05:35 +0000228 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
229 if (symfile)
230 {
231 if (!symfile.Exists())
232 {
233 char symfile_path[PATH_MAX];
234 symfile.GetPath(symfile_path, sizeof(symfile_path));
235 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
236 result.SetStatus (eReturnStatusFailed);
237 return false;
238 }
239 }
240
Greg Claytonabe0fed2011-04-18 08:33:37 +0000241 const char *file_path = command.GetArgumentAtIndex(0);
242 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000243 TargetSP target_sp;
244 Debugger &debugger = m_interpreter.GetDebugger();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000245 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Claytonec9c2d22012-11-30 19:05:35 +0000246 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000247 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytoned0a0fb2012-10-18 16:33:33 +0000248 file_path,
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000249 arch_cstr,
250 get_dependent_files,
251 &m_platform_options,
252 target_sp));
253
Greg Claytonabe0fed2011-04-18 08:33:37 +0000254 if (target_sp)
255 {
Greg Claytonec9c2d22012-11-30 19:05:35 +0000256 if (symfile)
257 {
258 ModuleSP module_sp (target_sp->GetExecutableModule());
259 if (module_sp)
260 module_sp->SetSymbolFileFileSpec(symfile);
261 }
262
Greg Claytonabe0fed2011-04-18 08:33:37 +0000263 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Greg Clayton46c9a352012-02-09 06:16:32 +0000264 if (core_file)
265 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000266 char core_path[PATH_MAX];
267 core_file.GetPath(core_path, sizeof(core_path));
Greg Clayton9ce95382012-02-13 23:10:39 +0000268 if (core_file.Exists())
Greg Clayton46c9a352012-02-09 06:16:32 +0000269 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000270 FileSpec core_file_dir;
271 core_file_dir.GetDirectory() = core_file.GetDirectory();
272 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Greg Clayton46c9a352012-02-09 06:16:32 +0000273
Greg Clayton9ce95382012-02-13 23:10:39 +0000274 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
275
276 if (process_sp)
Greg Clayton46c9a352012-02-09 06:16:32 +0000277 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000278 // Seems wierd that we Launch a core file, but that is
279 // what we do!
280 error = process_sp->LoadCore();
281
282 if (error.Fail())
283 {
284 result.AppendError(error.AsCString("can't find plug-in for core file"));
285 result.SetStatus (eReturnStatusFailed);
286 return false;
287 }
288 else
289 {
290 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
291 result.SetStatus (eReturnStatusSuccessFinishNoResult);
292 }
Greg Clayton46c9a352012-02-09 06:16:32 +0000293 }
294 else
295 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000296 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
297 result.SetStatus (eReturnStatusFailed);
Greg Clayton46c9a352012-02-09 06:16:32 +0000298 }
299 }
300 else
301 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000302 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Clayton46c9a352012-02-09 06:16:32 +0000303 result.SetStatus (eReturnStatusFailed);
304 }
305 }
306 else
307 {
308 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
309 result.SetStatus (eReturnStatusSuccessFinishNoResult);
310 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000311 }
312 else
313 {
314 result.AppendError(error.AsCString());
315 result.SetStatus (eReturnStatusFailed);
316 }
317 }
318 else
319 {
Greg Clayton46c9a352012-02-09 06:16:32 +0000320 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core-file option.\n", m_cmd_name.c_str());
Greg Claytonabe0fed2011-04-18 08:33:37 +0000321 result.SetStatus (eReturnStatusFailed);
322 }
323 return result.Succeeded();
324
325 }
326
Greg Claytonabe0fed2011-04-18 08:33:37 +0000327private:
328 OptionGroupOptions m_option_group;
Greg Clayton801417e2011-07-07 01:59:51 +0000329 OptionGroupArchitecture m_arch_option;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000330 OptionGroupPlatform m_platform_options;
Greg Clayton46c9a352012-02-09 06:16:32 +0000331 OptionGroupFile m_core_file;
Greg Claytonec9c2d22012-11-30 19:05:35 +0000332 OptionGroupFile m_symbol_file;
333 OptionGroupBoolean m_add_dependents;
Greg Claytonabe0fed2011-04-18 08:33:37 +0000334};
335
336#pragma mark CommandObjectTargetList
337
338//----------------------------------------------------------------------
339// "target list"
340//----------------------------------------------------------------------
341
Jim Inghamda26bd22012-06-08 21:56:10 +0000342class CommandObjectTargetList : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000343{
344public:
345 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000346 CommandObjectParsed (interpreter,
347 "target list",
348 "List all current targets in the current debug session.",
349 NULL,
350 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000351 {
352 }
353
354 virtual
355 ~CommandObjectTargetList ()
356 {
357 }
358
Jim Inghamda26bd22012-06-08 21:56:10 +0000359protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000360 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000361 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000362 {
363 if (args.GetArgumentCount() == 0)
364 {
365 Stream &strm = result.GetOutputStream();
366
367 bool show_stopped_process_status = false;
368 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
369 {
370 strm.PutCString ("No targets.\n");
371 }
Johnny Chen44dc9d32011-04-18 21:08:05 +0000372 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000373 }
374 else
375 {
376 result.AppendError ("the 'target list' command takes no arguments\n");
377 result.SetStatus (eReturnStatusFailed);
378 }
379 return result.Succeeded();
380 }
381};
382
383
384#pragma mark CommandObjectTargetSelect
385
386//----------------------------------------------------------------------
387// "target select"
388//----------------------------------------------------------------------
389
Jim Inghamda26bd22012-06-08 21:56:10 +0000390class CommandObjectTargetSelect : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000391{
392public:
393 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000394 CommandObjectParsed (interpreter,
395 "target select",
396 "Select a target as the current target by target index.",
397 NULL,
398 0)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000399 {
400 }
401
402 virtual
403 ~CommandObjectTargetSelect ()
404 {
405 }
406
Jim Inghamda26bd22012-06-08 21:56:10 +0000407protected:
Greg Claytonabe0fed2011-04-18 08:33:37 +0000408 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000409 DoExecute (Args& args, CommandReturnObject &result)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000410 {
411 if (args.GetArgumentCount() == 1)
412 {
413 bool success = false;
414 const char *target_idx_arg = args.GetArgumentAtIndex(0);
415 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
416 if (success)
417 {
418 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
419 const uint32_t num_targets = target_list.GetNumTargets();
420 if (target_idx < num_targets)
421 {
422 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
423 if (target_sp)
424 {
425 Stream &strm = result.GetOutputStream();
426 target_list.SetSelectedTarget (target_sp.get());
427 bool show_stopped_process_status = false;
428 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen44dc9d32011-04-18 21:08:05 +0000429 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000430 }
431 else
432 {
433 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
434 result.SetStatus (eReturnStatusFailed);
435 }
436 }
437 else
438 {
439 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
440 target_idx,
441 num_targets - 1);
442 result.SetStatus (eReturnStatusFailed);
443 }
444 }
445 else
446 {
447 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
448 result.SetStatus (eReturnStatusFailed);
449 }
450 }
451 else
452 {
453 result.AppendError ("'target select' takes a single argument: a target index\n");
454 result.SetStatus (eReturnStatusFailed);
455 }
456 return result.Succeeded();
457 }
458};
459
Greg Clayton153ccd72011-08-10 02:10:13 +0000460#pragma mark CommandObjectTargetSelect
461
462//----------------------------------------------------------------------
463// "target delete"
464//----------------------------------------------------------------------
465
Jim Inghamda26bd22012-06-08 21:56:10 +0000466class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton153ccd72011-08-10 02:10:13 +0000467{
468public:
469 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000470 CommandObjectParsed (interpreter,
471 "target delete",
472 "Delete one or more targets by target index.",
473 NULL,
474 0),
Greg Clayton5beb99d2011-08-11 02:48:45 +0000475 m_option_group (interpreter),
Greg Clayton437b5bc2012-09-27 22:26:11 +0000476 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', "Perform extra cleanup to minimize memory consumption after deleting the target.", false, false)
Greg Clayton153ccd72011-08-10 02:10:13 +0000477 {
Greg Clayton5beb99d2011-08-11 02:48:45 +0000478 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
479 m_option_group.Finalize();
Greg Clayton153ccd72011-08-10 02:10:13 +0000480 }
481
482 virtual
483 ~CommandObjectTargetDelete ()
484 {
485 }
486
Jim Inghamda26bd22012-06-08 21:56:10 +0000487 Options *
488 GetOptions ()
489 {
490 return &m_option_group;
491 }
492
493protected:
Greg Clayton153ccd72011-08-10 02:10:13 +0000494 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000495 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton153ccd72011-08-10 02:10:13 +0000496 {
497 const size_t argc = args.GetArgumentCount();
498 std::vector<TargetSP> delete_target_list;
499 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
500 bool success = true;
501 TargetSP target_sp;
502 if (argc > 0)
503 {
504 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000505 // Bail out if don't have any targets.
506 if (num_targets == 0) {
507 result.AppendError("no targets to delete");
508 result.SetStatus(eReturnStatusFailed);
509 success = false;
510 }
511
Greg Clayton153ccd72011-08-10 02:10:13 +0000512 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
513 {
514 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
515 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
516 if (success)
517 {
518 if (target_idx < num_targets)
519 {
520 target_sp = target_list.GetTargetAtIndex (target_idx);
521 if (target_sp)
522 {
523 delete_target_list.push_back (target_sp);
524 continue;
525 }
526 }
Filipe Cabecinhasaa3d89e2012-07-09 13:02:17 +0000527 if (num_targets > 1)
528 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
529 target_idx,
530 num_targets - 1);
531 else
532 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
533 target_idx);
534
Greg Clayton153ccd72011-08-10 02:10:13 +0000535 result.SetStatus (eReturnStatusFailed);
536 success = false;
537 }
538 else
539 {
540 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
541 result.SetStatus (eReturnStatusFailed);
542 success = false;
543 }
544 }
545
546 }
547 else
548 {
549 target_sp = target_list.GetSelectedTarget();
550 if (target_sp)
551 {
552 delete_target_list.push_back (target_sp);
553 }
554 else
555 {
556 result.AppendErrorWithFormat("no target is currently selected\n");
557 result.SetStatus (eReturnStatusFailed);
558 success = false;
559 }
560 }
561 if (success)
562 {
563 const size_t num_targets_to_delete = delete_target_list.size();
564 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
565 {
566 target_sp = delete_target_list[idx];
567 target_list.DeleteTarget(target_sp);
568 target_sp->Destroy();
569 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000570 // If "--clean" was specified, prune any orphaned shared modules from
571 // the global shared module list
572 if (m_cleanup_option.GetOptionValue ())
573 {
Greg Clayton860b9ea2012-04-09 20:22:01 +0000574 const bool mandatory = true;
575 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Clayton5beb99d2011-08-11 02:48:45 +0000576 }
Greg Clayton153ccd72011-08-10 02:10:13 +0000577 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
578 result.SetStatus(eReturnStatusSuccessFinishResult);
579 }
580
581 return result.Succeeded();
582 }
Greg Clayton5beb99d2011-08-11 02:48:45 +0000583
Greg Clayton5beb99d2011-08-11 02:48:45 +0000584 OptionGroupOptions m_option_group;
585 OptionGroupBoolean m_cleanup_option;
Greg Clayton153ccd72011-08-10 02:10:13 +0000586};
587
Greg Claytonabe0fed2011-04-18 08:33:37 +0000588
Greg Clayton801417e2011-07-07 01:59:51 +0000589#pragma mark CommandObjectTargetVariable
590
591//----------------------------------------------------------------------
592// "target variable"
593//----------------------------------------------------------------------
594
Jim Inghamda26bd22012-06-08 21:56:10 +0000595class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton801417e2011-07-07 01:59:51 +0000596{
597public:
598 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000599 CommandObjectParsed (interpreter,
600 "target variable",
Greg Claytonf72bd8b2012-11-03 00:10:22 +0000601 "Read global variable(s) prior to, or while running your binary.",
Jim Inghamda26bd22012-06-08 21:56:10 +0000602 NULL,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000603 eFlagRequiresTarget),
Greg Clayton801417e2011-07-07 01:59:51 +0000604 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000605 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000606 m_option_format (eFormatDefault),
Greg Clayton6475c422012-12-04 00:32:51 +0000607 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'file', 0, eArgTypeFilename, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
608 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'shlb', 0, eArgTypeFilename, "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
Greg Clayton801417e2011-07-07 01:59:51 +0000609 m_varobj_options()
610 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000611 CommandArgumentEntry arg;
612 CommandArgumentData var_name_arg;
613
614 // Define the first (and only) variant of this arg.
615 var_name_arg.arg_type = eArgTypeVarName;
616 var_name_arg.arg_repetition = eArgRepeatPlus;
617
618 // There is only one variant this argument could be; put it into the argument entry.
619 arg.push_back (var_name_arg);
620
621 // Push the data for the first argument into the m_arguments vector.
622 m_arguments.push_back (arg);
623
Greg Clayton801417e2011-07-07 01:59:51 +0000624 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000625 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000626 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton801417e2011-07-07 01:59:51 +0000627 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
628 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
629 m_option_group.Finalize();
630 }
631
632 virtual
633 ~CommandObjectTargetVariable ()
634 {
635 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000636
637 void
638 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
639 {
Enrico Granatac3f5cd82013-03-26 18:04:53 +0000640 ValueObject::DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Enrico Granata19030d82011-08-15 18:01:31 +0000641
Greg Clayton5d81f492011-07-08 21:46:14 +0000642 switch (var_sp->GetScope())
643 {
644 case eValueTypeVariableGlobal:
645 if (m_option_variable.show_scope)
646 s.PutCString("GLOBAL: ");
647 break;
648
649 case eValueTypeVariableStatic:
650 if (m_option_variable.show_scope)
651 s.PutCString("STATIC: ");
652 break;
653
654 case eValueTypeVariableArgument:
655 if (m_option_variable.show_scope)
656 s.PutCString(" ARG: ");
657 break;
658
659 case eValueTypeVariableLocal:
660 if (m_option_variable.show_scope)
661 s.PutCString(" LOCAL: ");
662 break;
663
664 default:
665 break;
666 }
667
Greg Claytonfb816422011-07-10 19:21:23 +0000668 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000669 {
Greg Claytonfb816422011-07-10 19:21:23 +0000670 bool show_fullpaths = false;
671 bool show_module = true;
672 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
673 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000674 }
675
Greg Claytona42880a2011-10-25 06:44:01 +0000676 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000677 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000678 options.SetFormat(format);
679
680 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000681
682 ValueObject::DumpValueObject (s,
683 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000684 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000685
686 }
Greg Clayton801417e2011-07-07 01:59:51 +0000687
Greg Clayton5d81f492011-07-08 21:46:14 +0000688
Greg Clayton36da2aa2013-01-25 18:06:21 +0000689 static size_t GetVariableCallback (void *baton,
690 const char *name,
691 VariableList &variable_list)
Greg Clayton5d81f492011-07-08 21:46:14 +0000692 {
693 Target *target = static_cast<Target *>(baton);
694 if (target)
695 {
696 return target->GetImages().FindGlobalVariables (ConstString(name),
697 true,
698 UINT32_MAX,
699 variable_list);
700 }
701 return 0;
702 }
703
704
705
Jim Inghamda26bd22012-06-08 21:56:10 +0000706 Options *
707 GetOptions ()
708 {
709 return &m_option_group;
710 }
711
712protected:
Greg Clayton6475c422012-12-04 00:32:51 +0000713
714 void
715 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
716 {
717 size_t count = variable_list.GetSize();
718 if (count > 0)
719 {
720 if (sc.module_sp)
721 {
722 if (sc.comp_unit)
723 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000724 s.Printf ("Global variables for %s in %s:\n",
725 sc.comp_unit->GetPath().c_str(),
726 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000727 }
728 else
729 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000730 s.Printf ("Global variables for %s\n",
731 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000732 }
733 }
734 else if (sc.comp_unit)
735 {
Greg Clayton97a19b22013-04-29 17:25:54 +0000736 s.Printf ("Global variables for %s\n",
737 sc.comp_unit->GetPath().c_str());
Greg Clayton6475c422012-12-04 00:32:51 +0000738 }
739
740 for (uint32_t i=0; i<count; ++i)
741 {
742 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
743 if (var_sp)
744 {
745 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
746
747 if (valobj_sp)
748 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
749 }
750 }
751 }
752
753 }
Greg Clayton801417e2011-07-07 01:59:51 +0000754 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000755 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000756 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000757 Target *target = m_exe_ctx.GetTargetPtr();
758 const size_t argc = args.GetArgumentCount();
759 Stream &s = result.GetOutputStream();
760
761 if (argc > 0)
Greg Clayton801417e2011-07-07 01:59:51 +0000762 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000763
764 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton801417e2011-07-07 01:59:51 +0000765 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000766 VariableList variable_list;
767 ValueObjectList valobj_list;
Greg Clayton5d81f492011-07-08 21:46:14 +0000768
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000769 const char *arg = args.GetArgumentAtIndex(idx);
Greg Clayton36da2aa2013-01-25 18:06:21 +0000770 size_t matches = 0;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000771 bool use_var_name = false;
772 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000773 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000774 RegularExpression regex(arg);
775 if (!regex.IsValid ())
Greg Clayton801417e2011-07-07 01:59:51 +0000776 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000777 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton368f8222011-07-07 04:38:25 +0000778 result.SetStatus (eReturnStatusFailed);
779 return false;
780 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000781 use_var_name = true;
782 matches = target->GetImages().FindGlobalVariables (regex,
783 true,
784 UINT32_MAX,
785 variable_list);
Greg Clayton6475c422012-12-04 00:32:51 +0000786 }
787 else
788 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000789 Error error (Variable::GetValuesForVariableExpressionPath (arg,
790 m_exe_ctx.GetBestExecutionContextScope(),
791 GetVariableCallback,
792 target,
793 variable_list,
794 valobj_list));
795 matches = variable_list.GetSize();
796 }
797
798 if (matches == 0)
799 {
800 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
801 result.SetStatus (eReturnStatusFailed);
802 return false;
803 }
804 else
805 {
806 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton6475c422012-12-04 00:32:51 +0000807 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000808 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
809 if (var_sp)
Greg Clayton6475c422012-12-04 00:32:51 +0000810 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000811 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
812 if (!valobj_sp)
813 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton6475c422012-12-04 00:32:51 +0000814
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000815 if (valobj_sp)
816 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton6475c422012-12-04 00:32:51 +0000817 }
818 }
Greg Claytonfac93882011-10-05 22:17:32 +0000819 }
Greg Clayton801417e2011-07-07 01:59:51 +0000820 }
821 }
822 else
823 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000824 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
825 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
826 SymbolContextList sc_list;
827 const size_t num_compile_units = compile_units.GetSize();
828 const size_t num_shlibs = shlibs.GetSize();
829 if (num_compile_units == 0 && num_shlibs == 0)
830 {
831 bool success = false;
832 StackFrame *frame = m_exe_ctx.GetFramePtr();
833 CompileUnit *comp_unit = NULL;
834 if (frame)
835 {
836 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
837 if (sc.comp_unit)
838 {
839 const bool can_create = true;
840 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
841 if (comp_unit_varlist_sp)
842 {
843 size_t count = comp_unit_varlist_sp->GetSize();
844 if (count > 0)
845 {
846 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
847 success = true;
848 }
849 }
850 }
851 }
852 if (!success)
853 {
854 if (frame)
855 {
856 if (comp_unit)
Greg Clayton97a19b22013-04-29 17:25:54 +0000857 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
858 comp_unit->GetPath().c_str());
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000859 else
860 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
861 }
862 else
863 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
864 result.SetStatus (eReturnStatusFailed);
865 }
866 }
867 else
868 {
869 SymbolContextList sc_list;
870 const bool append = true;
871 // We have one or more compile unit or shlib
872 if (num_shlibs > 0)
873 {
874 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
875 {
876 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
877 ModuleSpec module_spec (module_file);
878
879 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
880 if (module_sp)
881 {
882 if (num_compile_units > 0)
883 {
884 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
885 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
886 }
887 else
888 {
889 SymbolContext sc;
890 sc.module_sp = module_sp;
891 sc_list.Append(sc);
892 }
893 }
894 else
895 {
896 // Didn't find matching shlib/module in target...
Greg Clayton97a19b22013-04-29 17:25:54 +0000897 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
898 module_file.GetPath().c_str());
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000899 }
900 }
901 }
902 else
903 {
904 // No shared libraries, we just want to find globals for the compile units files that were specified
905 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
906 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
907 }
908
909 const uint32_t num_scs = sc_list.GetSize();
910 if (num_scs > 0)
911 {
912 SymbolContext sc;
913 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
914 {
915 if (sc_list.GetContextAtIndex(sc_idx, sc))
916 {
917 if (sc.comp_unit)
918 {
919 const bool can_create = true;
920 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
921 if (comp_unit_varlist_sp)
922 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
923 }
924 else if (sc.module_sp)
925 {
926 // Get all global variables for this module
927 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
928 VariableList variable_list;
929 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
930 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
931 }
932 }
933 }
934 }
935 }
Greg Clayton801417e2011-07-07 01:59:51 +0000936 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000937
Enrico Granatadb64d952011-08-12 16:42:31 +0000938 if (m_interpreter.TruncationWarningNecessary())
939 {
940 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
941 m_cmd_name.c_str());
942 m_interpreter.TruncationWarningGiven();
943 }
944
Greg Clayton801417e2011-07-07 01:59:51 +0000945 return result.Succeeded();
946 }
947
Greg Clayton801417e2011-07-07 01:59:51 +0000948 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000949 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000950 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000951 OptionGroupFileList m_option_compile_units;
952 OptionGroupFileList m_option_shared_libraries;
953 OptionGroupValueObjectDisplay m_varobj_options;
954
955};
956
957
Greg Claytone1f50b92011-05-03 22:09:39 +0000958#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000959
Jim Inghamda26bd22012-06-08 21:56:10 +0000960class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000961{
962public:
963
Greg Claytone1f50b92011-05-03 22:09:39 +0000964 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000965 CommandObjectParsed (interpreter,
966 "target modules search-paths add",
967 "Add new image search paths substitution pairs to the current target.",
968 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000969 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000970 CommandArgumentEntry arg;
971 CommandArgumentData old_prefix_arg;
972 CommandArgumentData new_prefix_arg;
973
974 // Define the first variant of this arg pair.
975 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
976 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
977
978 // Define the first variant of this arg pair.
979 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
980 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
981
982 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
983 // must always occur together, they are treated as two variants of one argument rather than two independent
984 // arguments. Push them both into the first argument position for m_arguments...
985
986 arg.push_back (old_prefix_arg);
987 arg.push_back (new_prefix_arg);
988
989 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000990 }
991
Greg Claytone1f50b92011-05-03 22:09:39 +0000992 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +0000993 {
994 }
995
Jim Inghamda26bd22012-06-08 21:56:10 +0000996protected:
Chris Lattner24943d22010-06-08 16:52:24 +0000997 bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000998 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000999 CommandReturnObject &result)
1000 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001001 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001002 if (target)
1003 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001004 const size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001005 if (argc & 1)
1006 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001007 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001008 result.SetStatus (eReturnStatusFailed);
1009 }
1010 else
1011 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001012 for (size_t i=0; i<argc; i+=2)
Chris Lattner24943d22010-06-08 16:52:24 +00001013 {
1014 const char *from = command.GetArgumentAtIndex(i);
1015 const char *to = command.GetArgumentAtIndex(i+1);
1016
1017 if (from[0] && to[0])
1018 {
1019 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +00001020 target->GetImageSearchPathList().Append (ConstString(from),
1021 ConstString(to),
1022 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +00001023 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001024 }
1025 else
1026 {
1027 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001028 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001029 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001030 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001031 result.SetStatus (eReturnStatusFailed);
1032 }
1033 }
1034 }
1035 }
1036 else
1037 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001038 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001039 result.SetStatus (eReturnStatusFailed);
1040 }
1041 return result.Succeeded();
1042 }
1043};
1044
Greg Claytone1f50b92011-05-03 22:09:39 +00001045#pragma mark CommandObjectTargetModulesSearchPathsClear
1046
Jim Inghamda26bd22012-06-08 21:56:10 +00001047class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001048{
1049public:
1050
Greg Claytone1f50b92011-05-03 22:09:39 +00001051 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001052 CommandObjectParsed (interpreter,
1053 "target modules search-paths clear",
1054 "Clear all current image search path substitution pairs from the current target.",
1055 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +00001056 {
1057 }
1058
Greg Claytone1f50b92011-05-03 22:09:39 +00001059 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +00001060 {
1061 }
1062
Jim Inghamda26bd22012-06-08 21:56:10 +00001063protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001064 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001065 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001066 CommandReturnObject &result)
1067 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001068 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001069 if (target)
1070 {
1071 bool notify = true;
1072 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +00001073 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001074 }
1075 else
1076 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001077 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001078 result.SetStatus (eReturnStatusFailed);
1079 }
1080 return result.Succeeded();
1081 }
1082};
1083
Greg Claytone1f50b92011-05-03 22:09:39 +00001084#pragma mark CommandObjectTargetModulesSearchPathsInsert
1085
Jim Inghamda26bd22012-06-08 21:56:10 +00001086class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001087{
1088public:
1089
Greg Claytone1f50b92011-05-03 22:09:39 +00001090 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001091 CommandObjectParsed (interpreter,
1092 "target modules search-paths insert",
1093 "Insert a new image search path substitution pair into the current target at the specified index.",
1094 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001095 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001096 CommandArgumentEntry arg1;
1097 CommandArgumentEntry arg2;
1098 CommandArgumentData index_arg;
1099 CommandArgumentData old_prefix_arg;
1100 CommandArgumentData new_prefix_arg;
1101
1102 // Define the first and only variant of this arg.
1103 index_arg.arg_type = eArgTypeIndex;
1104 index_arg.arg_repetition = eArgRepeatPlain;
1105
1106 // Put the one and only variant into the first arg for m_arguments:
1107 arg1.push_back (index_arg);
1108
1109 // Define the first variant of this arg pair.
1110 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1111 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1112
1113 // Define the first variant of this arg pair.
1114 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1115 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1116
1117 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1118 // must always occur together, they are treated as two variants of one argument rather than two independent
1119 // arguments. Push them both into the same argument position for m_arguments...
1120
1121 arg2.push_back (old_prefix_arg);
1122 arg2.push_back (new_prefix_arg);
1123
1124 // Add arguments to m_arguments.
1125 m_arguments.push_back (arg1);
1126 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001127 }
1128
Greg Claytone1f50b92011-05-03 22:09:39 +00001129 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001130 {
1131 }
1132
Jim Inghamda26bd22012-06-08 21:56:10 +00001133protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001134 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001135 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001136 CommandReturnObject &result)
1137 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001138 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001139 if (target)
1140 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00001141 size_t argc = command.GetArgumentCount();
Chris Lattner24943d22010-06-08 16:52:24 +00001142 // check for at least 3 arguments and an odd nubmer of parameters
1143 if (argc >= 3 && argc & 1)
1144 {
1145 bool success = false;
1146
1147 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1148
1149 if (!success)
1150 {
1151 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1152 result.SetStatus (eReturnStatusFailed);
1153 return result.Succeeded();
1154 }
1155
1156 // shift off the index
1157 command.Shift();
1158 argc = command.GetArgumentCount();
1159
1160 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1161 {
1162 const char *from = command.GetArgumentAtIndex(i);
1163 const char *to = command.GetArgumentAtIndex(i+1);
1164
1165 if (from[0] && to[0])
1166 {
1167 bool last_pair = ((argc - i) == 2);
1168 target->GetImageSearchPathList().Insert (ConstString(from),
1169 ConstString(to),
1170 insert_idx,
1171 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001172 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001173 }
1174 else
1175 {
1176 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001177 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001178 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001179 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001180 result.SetStatus (eReturnStatusFailed);
1181 return false;
1182 }
1183 }
1184 }
1185 else
1186 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001187 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001188 result.SetStatus (eReturnStatusFailed);
1189 return result.Succeeded();
1190 }
1191
1192 }
1193 else
1194 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001195 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001196 result.SetStatus (eReturnStatusFailed);
1197 }
1198 return result.Succeeded();
1199 }
1200};
1201
Greg Claytone1f50b92011-05-03 22:09:39 +00001202
1203#pragma mark CommandObjectTargetModulesSearchPathsList
1204
1205
Jim Inghamda26bd22012-06-08 21:56:10 +00001206class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001207{
1208public:
1209
Greg Claytone1f50b92011-05-03 22:09:39 +00001210 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001211 CommandObjectParsed (interpreter,
1212 "target modules search-paths list",
1213 "List all current image search path substitution pairs in the current target.",
1214 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001215 {
1216 }
1217
Greg Claytone1f50b92011-05-03 22:09:39 +00001218 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001219 {
1220 }
1221
Jim Inghamda26bd22012-06-08 21:56:10 +00001222protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001223 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001224 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001225 CommandReturnObject &result)
1226 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001227 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001228 if (target)
1229 {
1230 if (command.GetArgumentCount() != 0)
1231 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001232 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001233 result.SetStatus (eReturnStatusFailed);
1234 return result.Succeeded();
1235 }
1236
1237 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001238 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001239 }
1240 else
1241 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001242 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001243 result.SetStatus (eReturnStatusFailed);
1244 }
1245 return result.Succeeded();
1246 }
1247};
1248
Greg Claytone1f50b92011-05-03 22:09:39 +00001249#pragma mark CommandObjectTargetModulesSearchPathsQuery
1250
Jim Inghamda26bd22012-06-08 21:56:10 +00001251class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001252{
1253public:
1254
Greg Claytone1f50b92011-05-03 22:09:39 +00001255 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001256 CommandObjectParsed (interpreter,
1257 "target modules search-paths query",
1258 "Transform a path using the first applicable image search path.",
1259 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001260 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001261 CommandArgumentEntry arg;
1262 CommandArgumentData path_arg;
1263
1264 // Define the first (and only) variant of this arg.
Sean Callanan9a91ef62012-10-24 01:12:14 +00001265 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice43b014a2010-10-04 22:28:36 +00001266 path_arg.arg_repetition = eArgRepeatPlain;
1267
1268 // There is only one variant this argument could be; put it into the argument entry.
1269 arg.push_back (path_arg);
1270
1271 // Push the data for the first argument into the m_arguments vector.
1272 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001273 }
1274
Greg Claytone1f50b92011-05-03 22:09:39 +00001275 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001276 {
1277 }
1278
Jim Inghamda26bd22012-06-08 21:56:10 +00001279protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001280 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001281 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001282 CommandReturnObject &result)
1283 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001284 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001285 if (target)
1286 {
1287 if (command.GetArgumentCount() != 1)
1288 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001289 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001290 result.SetStatus (eReturnStatusFailed);
1291 return result.Succeeded();
1292 }
1293
1294 ConstString orig(command.GetArgumentAtIndex(0));
1295 ConstString transformed;
1296 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1297 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1298 else
1299 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001300
1301 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001302 }
1303 else
1304 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001305 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001306 result.SetStatus (eReturnStatusFailed);
1307 }
1308 return result.Succeeded();
1309 }
1310};
1311
Greg Claytone1f50b92011-05-03 22:09:39 +00001312//----------------------------------------------------------------------
1313// Static Helper functions
1314//----------------------------------------------------------------------
1315static void
1316DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1317{
1318 if (module)
1319 {
1320 const char *arch_cstr;
1321 if (full_triple)
1322 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1323 else
1324 arch_cstr = module->GetArchitecture().GetArchitectureName();
1325 if (width)
1326 strm.Printf("%-*s", width, arch_cstr);
1327 else
1328 strm.PutCString(arch_cstr);
1329 }
1330}
1331
1332static void
1333DumpModuleUUID (Stream &strm, Module *module)
1334{
Jim Ingham6f01c932012-10-12 17:34:26 +00001335 if (module && module->GetUUID().IsValid())
Greg Clayton153ccd72011-08-10 02:10:13 +00001336 module->GetUUID().Dump (&strm);
1337 else
1338 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001339}
1340
1341static uint32_t
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001342DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1343 Stream &strm,
1344 Module *module,
1345 const FileSpec &file_spec,
1346 bool load_addresses)
Greg Claytone1f50b92011-05-03 22:09:39 +00001347{
1348 uint32_t num_matches = 0;
1349 if (module)
1350 {
1351 SymbolContextList sc_list;
1352 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1353 0,
1354 false,
1355 eSymbolContextCompUnit,
1356 sc_list);
1357
1358 for (uint32_t i=0; i<num_matches; ++i)
1359 {
1360 SymbolContext sc;
1361 if (sc_list.GetContextAtIndex(i, sc))
1362 {
1363 if (i > 0)
1364 strm << "\n\n";
1365
1366 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1367 << module->GetFileSpec().GetFilename() << "\n";
1368 LineTable *line_table = sc.comp_unit->GetLineTable();
1369 if (line_table)
1370 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001371 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001372 lldb::eDescriptionLevelBrief);
1373 else
1374 strm << "No line table";
1375 }
1376 }
1377 }
1378 return num_matches;
1379}
1380
1381static void
1382DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1383{
1384 if (file_spec_ptr)
1385 {
1386 if (width > 0)
1387 {
Jason Molenda5581a7e2013-05-06 10:21:11 +00001388 std::string fullpath = file_spec_ptr->GetPath();
1389 strm.Printf("%-*s", width, fullpath.c_str());
1390 return;
Greg Claytone1f50b92011-05-03 22:09:39 +00001391 }
1392 else
1393 {
1394 file_spec_ptr->Dump(&strm);
1395 return;
1396 }
1397 }
1398 // Keep the width spacing correct if things go wrong...
1399 if (width > 0)
1400 strm.Printf("%-*s", width, "");
1401}
1402
1403static void
1404DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1405{
1406 if (file_spec_ptr)
1407 {
1408 if (width > 0)
1409 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1410 else
1411 file_spec_ptr->GetDirectory().Dump(&strm);
1412 return;
1413 }
1414 // Keep the width spacing correct if things go wrong...
1415 if (width > 0)
1416 strm.Printf("%-*s", width, "");
1417}
1418
1419static void
1420DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1421{
1422 if (file_spec_ptr)
1423 {
1424 if (width > 0)
1425 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1426 else
1427 file_spec_ptr->GetFilename().Dump(&strm);
1428 return;
1429 }
1430 // Keep the width spacing correct if things go wrong...
1431 if (width > 0)
1432 strm.Printf("%-*s", width, "");
1433}
1434
1435
1436static void
1437DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1438{
1439 if (module)
1440 {
Michael Sartaina807cee2013-07-01 19:45:50 +00001441 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1442 if (sym_vendor)
Greg Claytone1f50b92011-05-03 22:09:39 +00001443 {
Michael Sartaina807cee2013-07-01 19:45:50 +00001444 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytone1f50b92011-05-03 22:09:39 +00001445 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001446 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001447 }
1448 }
1449}
1450
1451static void
1452DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1453{
1454 if (module)
1455 {
Michael Sartaina807cee2013-07-01 19:45:50 +00001456 SectionList *section_list = module->GetUnifiedSectionList();
1457 if (section_list)
Greg Claytone1f50b92011-05-03 22:09:39 +00001458 {
Michael Sartaina807cee2013-07-01 19:45:50 +00001459 strm.Printf ("Sections for '%s' (%s):\n",
1460 module->GetSpecificationDescription().c_str(),
1461 module->GetArchitecture().GetArchitectureName());
1462 strm.IndentMore();
1463 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1464 strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001465 }
1466 }
1467}
1468
1469static bool
1470DumpModuleSymbolVendor (Stream &strm, Module *module)
1471{
1472 if (module)
1473 {
1474 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1475 if (symbol_vendor)
1476 {
1477 symbol_vendor->Dump(&strm);
1478 return true;
1479 }
1480 }
1481 return false;
1482}
1483
Greg Clayton2ad894b2012-05-15 18:43:44 +00001484static void
1485DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1486{
1487 strm.IndentMore();
1488 strm.Indent (" Address: ");
1489 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1490 strm.PutCString (" (");
1491 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1492 strm.PutCString (")\n");
1493 strm.Indent (" Summary: ");
1494 const uint32_t save_indent = strm.GetIndentLevel ();
1495 strm.SetIndentLevel (save_indent + 13);
1496 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1497 strm.SetIndentLevel (save_indent);
1498 // Print out detailed address information when verbose is enabled
1499 if (verbose)
1500 {
1501 strm.EOL();
1502 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1503 }
1504 strm.IndentLess();
1505}
1506
Greg Claytone1f50b92011-05-03 22:09:39 +00001507static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001508LookupAddressInModule (CommandInterpreter &interpreter,
1509 Stream &strm,
1510 Module *module,
1511 uint32_t resolve_mask,
1512 lldb::addr_t raw_addr,
1513 lldb::addr_t offset,
1514 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001515{
1516 if (module)
1517 {
1518 lldb::addr_t addr = raw_addr - offset;
1519 Address so_addr;
1520 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001521 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001522 if (target && !target->GetSectionLoadList().IsEmpty())
1523 {
1524 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1525 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001526 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001527 return false;
1528 }
1529 else
1530 {
1531 if (!module->ResolveFileAddress (addr, so_addr))
1532 return false;
1533 }
1534
Greg Claytone1f50b92011-05-03 22:09:39 +00001535 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001536 DumpAddress (exe_scope, so_addr, verbose, strm);
1537// strm.IndentMore();
1538// strm.Indent (" Address: ");
1539// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1540// strm.PutCString (" (");
1541// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1542// strm.PutCString (")\n");
1543// strm.Indent (" Summary: ");
1544// const uint32_t save_indent = strm.GetIndentLevel ();
1545// strm.SetIndentLevel (save_indent + 13);
1546// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1547// strm.SetIndentLevel (save_indent);
1548// // Print out detailed address information when verbose is enabled
1549// if (verbose)
1550// {
1551// strm.EOL();
1552// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1553// }
1554// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001555 return true;
1556 }
1557
1558 return false;
1559}
1560
1561static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001562LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001563{
1564 if (module)
1565 {
1566 SymbolContext sc;
1567
Michael Sartaina807cee2013-07-01 19:45:50 +00001568 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1569 if (sym_vendor)
Greg Claytone1f50b92011-05-03 22:09:39 +00001570 {
Michael Sartaina807cee2013-07-01 19:45:50 +00001571 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytone1f50b92011-05-03 22:09:39 +00001572 if (symtab)
1573 {
1574 uint32_t i;
1575 std::vector<uint32_t> match_indexes;
1576 ConstString symbol_name (name);
1577 uint32_t num_matches = 0;
1578 if (name_is_regex)
1579 {
1580 RegularExpression name_regexp(name);
1581 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1582 eSymbolTypeAny,
1583 match_indexes);
1584 }
1585 else
1586 {
1587 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1588 }
1589
1590
1591 if (num_matches > 0)
1592 {
1593 strm.Indent ();
1594 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1595 name_is_regex ? "the regular expression " : "", name);
1596 DumpFullpath (strm, &module->GetFileSpec(), 0);
1597 strm.PutCString(":\n");
1598 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001599 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001600 for (i=0; i < num_matches; ++i)
1601 {
1602 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001603 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1604 symbol->GetAddress(),
1605 verbose,
1606 strm);
1607
1608// strm.Indent ();
1609// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001610 }
1611 strm.IndentLess ();
1612 return num_matches;
1613 }
1614 }
1615 }
1616 }
1617 return 0;
1618}
1619
1620
1621static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001622DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001623{
1624 strm.IndentMore ();
1625 uint32_t i;
1626 const uint32_t num_matches = sc_list.GetSize();
1627
1628 for (i=0; i<num_matches; ++i)
1629 {
1630 SymbolContext sc;
1631 if (sc_list.GetContextAtIndex(i, sc))
1632 {
Sean Callanand7793d22012-02-11 00:24:04 +00001633 AddressRange range;
1634
1635 sc.GetAddressRange(eSymbolContextEverything,
1636 0,
1637 true,
1638 range);
1639
Greg Clayton2ad894b2012-05-15 18:43:44 +00001640 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001641 }
1642 }
1643 strm.IndentLess ();
1644}
1645
Greg Clayton36da2aa2013-01-25 18:06:21 +00001646static size_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001647LookupFunctionInModule (CommandInterpreter &interpreter,
1648 Stream &strm,
1649 Module *module,
1650 const char *name,
1651 bool name_is_regex,
1652 bool include_inlines,
1653 bool include_symbols,
1654 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001655{
1656 if (module && name && name[0])
1657 {
1658 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001659 const bool append = true;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001660 size_t num_matches = 0;
Greg Claytone1f50b92011-05-03 22:09:39 +00001661 if (name_is_regex)
1662 {
1663 RegularExpression function_name_regex (name);
1664 num_matches = module->FindFunctions (function_name_regex,
1665 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001666 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001667 append,
1668 sc_list);
1669 }
1670 else
1671 {
1672 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001673 num_matches = module->FindFunctions (function_name,
1674 NULL,
Greg Clayton83d90c52013-05-18 00:11:21 +00001675 eFunctionNameTypeAuto,
Greg Claytone1f50b92011-05-03 22:09:39 +00001676 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001677 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001678 append,
1679 sc_list);
1680 }
1681
1682 if (num_matches)
1683 {
1684 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001685 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Claytone1f50b92011-05-03 22:09:39 +00001686 DumpFullpath (strm, &module->GetFileSpec(), 0);
1687 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001688 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001689 }
1690 return num_matches;
1691 }
1692 return 0;
1693}
1694
Greg Clayton36da2aa2013-01-25 18:06:21 +00001695static size_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001696LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001697 Stream &strm,
1698 Module *module,
1699 const char *name_cstr,
1700 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001701{
1702 if (module && name_cstr && name_cstr[0])
1703 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001704 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001705 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001706 size_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001707 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001708 SymbolContext sc;
1709
1710 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001711 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001712
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001713 if (num_matches)
1714 {
1715 strm.Indent ();
Greg Clayton36da2aa2013-01-25 18:06:21 +00001716 strm.Printf("%zu match%s found in ", num_matches, num_matches > 1 ? "es" : "");
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001717 DumpFullpath (strm, &module->GetFileSpec(), 0);
1718 strm.PutCString(":\n");
1719 const uint32_t num_types = type_list.GetSize();
1720 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001721 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001722 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1723 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001724 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001725 // Resolve the clang type so that any forward references
1726 // to types that haven't yet been parsed will get parsed.
1727 type_sp->GetClangFullType ();
1728 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001729 // Print all typedef chains
1730 TypeSP typedef_type_sp (type_sp);
1731 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1732 while (typedefed_type_sp)
1733 {
1734 strm.EOL();
1735 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1736 typedefed_type_sp->GetClangFullType ();
1737 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1738 typedef_type_sp = typedefed_type_sp;
1739 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1740 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001741 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001742 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001743 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001744 }
1745 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001746 }
1747 return 0;
1748}
1749
Greg Clayton36da2aa2013-01-25 18:06:21 +00001750static size_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001751LookupTypeHere (CommandInterpreter &interpreter,
1752 Stream &strm,
1753 const SymbolContext &sym_ctx,
1754 const char *name_cstr,
1755 bool name_is_regex)
1756{
1757 if (!sym_ctx.module_sp)
1758 return 0;
1759
1760 TypeList type_list;
1761 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001762 size_t num_matches = 1;
Sean Callanan56d31ec2012-06-06 20:49:55 +00001763 bool name_is_fully_qualified = false;
1764
1765 ConstString name(name_cstr);
1766 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1767
1768 if (num_matches)
1769 {
1770 strm.Indent ();
1771 strm.PutCString("Best match found in ");
1772 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1773 strm.PutCString(":\n");
1774
1775 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1776 if (type_sp)
1777 {
1778 // Resolve the clang type so that any forward references
1779 // to types that haven't yet been parsed will get parsed.
1780 type_sp->GetClangFullType ();
1781 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1782 // Print all typedef chains
1783 TypeSP typedef_type_sp (type_sp);
1784 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1785 while (typedefed_type_sp)
1786 {
1787 strm.EOL();
1788 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1789 typedefed_type_sp->GetClangFullType ();
1790 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1791 typedef_type_sp = typedefed_type_sp;
1792 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1793 }
1794 }
1795 strm.EOL();
1796 }
1797 return num_matches;
1798}
1799
1800static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001801LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001802 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001803 Module *module,
1804 const FileSpec &file_spec,
1805 uint32_t line,
1806 bool check_inlines,
1807 bool verbose)
1808{
1809 if (module && file_spec)
1810 {
1811 SymbolContextList sc_list;
1812 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1813 eSymbolContextEverything, sc_list);
1814 if (num_matches > 0)
1815 {
1816 strm.Indent ();
1817 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1818 strm << file_spec;
1819 if (line > 0)
1820 strm.Printf (":%u", line);
1821 strm << " in ";
1822 DumpFullpath (strm, &module->GetFileSpec(), 0);
1823 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001824 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001825 return num_matches;
1826 }
1827 }
1828 return 0;
1829
1830}
1831
Greg Clayton91048ef2011-11-10 01:18:58 +00001832
1833static size_t
1834FindModulesByName (Target *target,
1835 const char *module_name,
1836 ModuleList &module_list,
1837 bool check_global_list)
1838{
1839// Dump specified images (by basename or fullpath)
1840 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001841 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001842
1843 const size_t initial_size = module_list.GetSize ();
1844
Greg Clayton316f57f2012-07-11 20:46:47 +00001845 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001846 {
1847 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001848 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00001849 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton91048ef2011-11-10 01:18:58 +00001850 ModuleSP module_sp;
Greg Clayton36da2aa2013-01-25 18:06:21 +00001851 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton91048ef2011-11-10 01:18:58 +00001852 {
1853 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1854
1855 if (module)
1856 {
Greg Clayton444fe992012-02-26 05:51:37 +00001857 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001858 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001859 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001860 module_list.AppendIfNeeded(module_sp);
1861 }
1862 }
1863 }
1864 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001865 else
1866 {
1867 if (target)
1868 {
1869 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1870
1871 // Not found in our module list for our target, check the main
1872 // shared module list in case it is a extra file used somewhere
1873 // else
1874 if (num_matches == 0)
1875 {
1876 module_spec.GetArchitecture() = target->GetArchitecture();
1877 ModuleList::FindSharedModules (module_spec, module_list);
1878 }
1879 }
1880 else
1881 {
1882 ModuleList::FindSharedModules (module_spec,module_list);
1883 }
1884 }
1885
Greg Clayton91048ef2011-11-10 01:18:58 +00001886 return module_list.GetSize () - initial_size;
1887}
1888
Greg Claytone1f50b92011-05-03 22:09:39 +00001889#pragma mark CommandObjectTargetModulesModuleAutoComplete
1890
1891//----------------------------------------------------------------------
1892// A base command object class that can auto complete with module file
1893// paths
1894//----------------------------------------------------------------------
1895
Jim Inghamda26bd22012-06-08 21:56:10 +00001896class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001897{
1898public:
1899
1900 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1901 const char *name,
1902 const char *help,
1903 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001904 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001905 {
1906 CommandArgumentEntry arg;
1907 CommandArgumentData file_arg;
1908
1909 // Define the first (and only) variant of this arg.
1910 file_arg.arg_type = eArgTypeFilename;
1911 file_arg.arg_repetition = eArgRepeatStar;
1912
1913 // There is only one variant this argument could be; put it into the argument entry.
1914 arg.push_back (file_arg);
1915
1916 // Push the data for the first argument into the m_arguments vector.
1917 m_arguments.push_back (arg);
1918 }
1919
1920 virtual
1921 ~CommandObjectTargetModulesModuleAutoComplete ()
1922 {
1923 }
1924
1925 virtual int
1926 HandleArgumentCompletion (Args &input,
1927 int &cursor_index,
1928 int &cursor_char_position,
1929 OptionElementVector &opt_element_vector,
1930 int match_start_point,
1931 int max_return_elements,
1932 bool &word_complete,
1933 StringList &matches)
1934 {
1935 // Arguments are the standard module completer.
1936 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1937 completion_str.erase (cursor_char_position);
1938
1939 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1940 CommandCompletions::eModuleCompletion,
1941 completion_str.c_str(),
1942 match_start_point,
1943 max_return_elements,
1944 NULL,
1945 word_complete,
1946 matches);
1947 return matches.GetSize();
1948 }
1949};
1950
1951#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1952
1953//----------------------------------------------------------------------
1954// A base command object class that can auto complete with module source
1955// file paths
1956//----------------------------------------------------------------------
1957
Jim Inghamda26bd22012-06-08 21:56:10 +00001958class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001959{
1960public:
1961
1962 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00001963 const char *name,
1964 const char *help,
1965 const char *syntax,
1966 uint32_t flags) :
1967 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytone1f50b92011-05-03 22:09:39 +00001968 {
1969 CommandArgumentEntry arg;
1970 CommandArgumentData source_file_arg;
1971
1972 // Define the first (and only) variant of this arg.
1973 source_file_arg.arg_type = eArgTypeSourceFile;
1974 source_file_arg.arg_repetition = eArgRepeatPlus;
1975
1976 // There is only one variant this argument could be; put it into the argument entry.
1977 arg.push_back (source_file_arg);
1978
1979 // Push the data for the first argument into the m_arguments vector.
1980 m_arguments.push_back (arg);
1981 }
1982
1983 virtual
1984 ~CommandObjectTargetModulesSourceFileAutoComplete ()
1985 {
1986 }
1987
1988 virtual int
1989 HandleArgumentCompletion (Args &input,
1990 int &cursor_index,
1991 int &cursor_char_position,
1992 OptionElementVector &opt_element_vector,
1993 int match_start_point,
1994 int max_return_elements,
1995 bool &word_complete,
1996 StringList &matches)
1997 {
1998 // Arguments are the standard source file completer.
1999 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2000 completion_str.erase (cursor_char_position);
2001
2002 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2003 CommandCompletions::eSourceFileCompletion,
2004 completion_str.c_str(),
2005 match_start_point,
2006 max_return_elements,
2007 NULL,
2008 word_complete,
2009 matches);
2010 return matches.GetSize();
2011 }
2012};
2013
2014
2015#pragma mark CommandObjectTargetModulesDumpSymtab
2016
2017
2018class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2019{
2020public:
2021 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2022 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2023 "target modules dump symtab",
2024 "Dump the symbol table from one or more target modules.",
2025 NULL),
2026 m_options (interpreter)
2027 {
2028 }
2029
2030 virtual
2031 ~CommandObjectTargetModulesDumpSymtab ()
2032 {
2033 }
2034
Jim Inghamda26bd22012-06-08 21:56:10 +00002035 virtual Options *
2036 GetOptions ()
2037 {
2038 return &m_options;
2039 }
2040
2041 class CommandOptions : public Options
2042 {
2043 public:
2044
2045 CommandOptions (CommandInterpreter &interpreter) :
2046 Options(interpreter),
2047 m_sort_order (eSortOrderNone)
2048 {
2049 }
2050
2051 virtual
2052 ~CommandOptions ()
2053 {
2054 }
2055
2056 virtual Error
2057 SetOptionValue (uint32_t option_idx, const char *option_arg)
2058 {
2059 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00002060 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamda26bd22012-06-08 21:56:10 +00002061
2062 switch (short_option)
2063 {
2064 case 's':
2065 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2066 g_option_table[option_idx].enum_values,
2067 eSortOrderNone,
2068 error);
2069 break;
2070
2071 default:
2072 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2073 break;
2074
2075 }
2076 return error;
2077 }
2078
2079 void
2080 OptionParsingStarting ()
2081 {
2082 m_sort_order = eSortOrderNone;
2083 }
2084
2085 const OptionDefinition*
2086 GetDefinitions ()
2087 {
2088 return g_option_table;
2089 }
2090
2091 // Options table: Required for subclasses of Options.
2092 static OptionDefinition g_option_table[];
2093
2094 SortOrder m_sort_order;
2095 };
2096
2097protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002098 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002099 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002100 CommandReturnObject &result)
2101 {
2102 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2103 if (target == NULL)
2104 {
2105 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2106 result.SetStatus (eReturnStatusFailed);
2107 return false;
2108 }
2109 else
2110 {
2111 uint32_t num_dumped = 0;
2112
2113 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2114 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2115 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2116
2117 if (command.GetArgumentCount() == 0)
2118 {
2119 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002120 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002121 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002122 if (num_modules > 0)
2123 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002124 result.GetOutputStream().Printf("Dumping symbol table for %zu modules.\n", num_modules);
2125 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002126 {
2127 if (num_dumped > 0)
2128 {
2129 result.GetOutputStream().EOL();
2130 result.GetOutputStream().EOL();
2131 }
2132 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002133 DumpModuleSymtab (m_interpreter,
2134 result.GetOutputStream(),
2135 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2136 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002137 }
2138 }
2139 else
2140 {
2141 result.AppendError ("the target has no associated executable images");
2142 result.SetStatus (eReturnStatusFailed);
2143 return false;
2144 }
2145 }
2146 else
2147 {
2148 // Dump specified images (by basename or fullpath)
2149 const char *arg_cstr;
2150 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2151 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002152 ModuleList module_list;
2153 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2154 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002155 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002156 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002157 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002158 Module *module = module_list.GetModulePointerAtIndex(i);
2159 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002160 {
2161 if (num_dumped > 0)
2162 {
2163 result.GetOutputStream().EOL();
2164 result.GetOutputStream().EOL();
2165 }
2166 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002167 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002168 }
2169 }
2170 }
2171 else
2172 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2173 }
2174 }
2175
2176 if (num_dumped > 0)
2177 result.SetStatus (eReturnStatusSuccessFinishResult);
2178 else
2179 {
2180 result.AppendError ("no matching executable images found");
2181 result.SetStatus (eReturnStatusFailed);
2182 }
2183 }
2184 return result.Succeeded();
2185 }
2186
Greg Claytone1f50b92011-05-03 22:09:39 +00002187
2188 CommandOptions m_options;
2189};
2190
2191static OptionEnumValueElement
2192g_sort_option_enumeration[4] =
2193{
2194 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2195 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2196 { eSortOrderByName, "name", "Sort output by symbol name."},
2197 { 0, NULL, NULL }
2198};
2199
2200
2201OptionDefinition
2202CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2203{
2204 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2205 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2206};
2207
2208#pragma mark CommandObjectTargetModulesDumpSections
2209
2210//----------------------------------------------------------------------
2211// Image section dumping command
2212//----------------------------------------------------------------------
2213
2214class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2215{
2216public:
2217 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2218 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2219 "target modules dump sections",
2220 "Dump the sections from one or more target modules.",
2221 //"target modules dump sections [<file1> ...]")
2222 NULL)
2223 {
2224 }
2225
2226 virtual
2227 ~CommandObjectTargetModulesDumpSections ()
2228 {
2229 }
2230
Jim Inghamda26bd22012-06-08 21:56:10 +00002231protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002232 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002233 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002234 CommandReturnObject &result)
2235 {
2236 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2237 if (target == NULL)
2238 {
2239 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2240 result.SetStatus (eReturnStatusFailed);
2241 return false;
2242 }
2243 else
2244 {
2245 uint32_t num_dumped = 0;
2246
2247 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2248 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2249 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2250
2251 if (command.GetArgumentCount() == 0)
2252 {
2253 // Dump all sections for all modules images
Greg Clayton36da2aa2013-01-25 18:06:21 +00002254 const size_t num_modules = target->GetImages().GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002255 if (num_modules > 0)
2256 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002257 result.GetOutputStream().Printf("Dumping sections for %zu modules.\n", num_modules);
2258 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002259 {
2260 num_dumped++;
2261 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2262 }
2263 }
2264 else
2265 {
2266 result.AppendError ("the target has no associated executable images");
2267 result.SetStatus (eReturnStatusFailed);
2268 return false;
2269 }
2270 }
2271 else
2272 {
2273 // Dump specified images (by basename or fullpath)
2274 const char *arg_cstr;
2275 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2276 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002277 ModuleList module_list;
2278 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2279 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002280 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002281 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002282 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002283 Module *module = module_list.GetModulePointerAtIndex(i);
2284 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002285 {
2286 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002287 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002288 }
2289 }
2290 }
2291 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002292 {
2293 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002294 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002295
Greg Claytone1f50b92011-05-03 22:09:39 +00002296 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002297 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002298 }
2299 }
2300
2301 if (num_dumped > 0)
2302 result.SetStatus (eReturnStatusSuccessFinishResult);
2303 else
2304 {
2305 result.AppendError ("no matching executable images found");
2306 result.SetStatus (eReturnStatusFailed);
2307 }
2308 }
2309 return result.Succeeded();
2310 }
2311};
2312
2313
2314#pragma mark CommandObjectTargetModulesDumpSymfile
2315
2316//----------------------------------------------------------------------
2317// Image debug symbol dumping command
2318//----------------------------------------------------------------------
2319
2320class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2321{
2322public:
2323 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2324 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2325 "target modules dump symfile",
2326 "Dump the debug symbol file for one or more target modules.",
2327 //"target modules dump symfile [<file1> ...]")
2328 NULL)
2329 {
2330 }
2331
2332 virtual
2333 ~CommandObjectTargetModulesDumpSymfile ()
2334 {
2335 }
2336
Jim Inghamda26bd22012-06-08 21:56:10 +00002337protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002338 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002339 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002340 CommandReturnObject &result)
2341 {
2342 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2343 if (target == NULL)
2344 {
2345 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2346 result.SetStatus (eReturnStatusFailed);
2347 return false;
2348 }
2349 else
2350 {
2351 uint32_t num_dumped = 0;
2352
2353 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2354 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2355 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2356
2357 if (command.GetArgumentCount() == 0)
2358 {
2359 // Dump all sections for all modules images
Enrico Granata146d9522012-11-08 02:22:02 +00002360 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002361 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002362 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002363 if (num_modules > 0)
2364 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00002365 result.GetOutputStream().Printf("Dumping debug symbols for %zu modules.\n", num_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002366 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2367 {
Jim Ingham93367902012-05-30 02:19:25 +00002368 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002369 num_dumped++;
2370 }
2371 }
2372 else
2373 {
2374 result.AppendError ("the target has no associated executable images");
2375 result.SetStatus (eReturnStatusFailed);
2376 return false;
2377 }
2378 }
2379 else
2380 {
2381 // Dump specified images (by basename or fullpath)
2382 const char *arg_cstr;
2383 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2384 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002385 ModuleList module_list;
2386 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2387 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002388 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002389 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002390 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002391 Module *module = module_list.GetModulePointerAtIndex(i);
2392 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002393 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002394 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002395 num_dumped++;
2396 }
2397 }
2398 }
2399 else
2400 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2401 }
2402 }
2403
2404 if (num_dumped > 0)
2405 result.SetStatus (eReturnStatusSuccessFinishResult);
2406 else
2407 {
2408 result.AppendError ("no matching executable images found");
2409 result.SetStatus (eReturnStatusFailed);
2410 }
2411 }
2412 return result.Succeeded();
2413 }
2414};
2415
2416
2417#pragma mark CommandObjectTargetModulesDumpLineTable
2418
2419//----------------------------------------------------------------------
2420// Image debug line table dumping command
2421//----------------------------------------------------------------------
2422
2423class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2424{
2425public:
2426 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2427 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002428 "target modules dump line-table",
Jim Inghame5150762013-06-18 20:27:11 +00002429 "Dump the line table for one or more compilation units.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002430 NULL,
2431 eFlagRequiresTarget)
Greg Claytone1f50b92011-05-03 22:09:39 +00002432 {
2433 }
2434
2435 virtual
2436 ~CommandObjectTargetModulesDumpLineTable ()
2437 {
2438 }
2439
Jim Inghamda26bd22012-06-08 21:56:10 +00002440protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002441 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002442 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002443 CommandReturnObject &result)
2444 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002445 Target *target = m_exe_ctx.GetTargetPtr();
2446 uint32_t total_num_dumped = 0;
2447
2448 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2449 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2450 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2451
2452 if (command.GetArgumentCount() == 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002453 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002454 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytone1f50b92011-05-03 22:09:39 +00002455 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002456 }
2457 else
2458 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002459 // Dump specified images (by basename or fullpath)
2460 const char *arg_cstr;
2461 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytone1f50b92011-05-03 22:09:39 +00002462 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002463 FileSpec file_spec(arg_cstr, false);
2464
2465 const ModuleList &target_modules = target->GetImages();
2466 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00002467 const size_t num_modules = target_modules.GetSize();
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002468 if (num_modules > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002469 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002470 uint32_t num_dumped = 0;
2471 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002472 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002473 if (DumpCompileUnitLineTable (m_interpreter,
2474 result.GetOutputStream(),
2475 target_modules.GetModulePointerAtIndexUnlocked(i),
2476 file_spec,
2477 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2478 num_dumped++;
Greg Claytone1f50b92011-05-03 22:09:39 +00002479 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002480 if (num_dumped == 0)
2481 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2482 else
2483 total_num_dumped += num_dumped;
Greg Claytone1f50b92011-05-03 22:09:39 +00002484 }
2485 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00002486 }
2487
2488 if (total_num_dumped > 0)
2489 result.SetStatus (eReturnStatusSuccessFinishResult);
2490 else
2491 {
2492 result.AppendError ("no source filenames matched any command arguments");
2493 result.SetStatus (eReturnStatusFailed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002494 }
2495 return result.Succeeded();
2496 }
2497};
2498
2499
2500#pragma mark CommandObjectTargetModulesDump
2501
2502//----------------------------------------------------------------------
2503// Dump multi-word command for target modules
2504//----------------------------------------------------------------------
2505
2506class CommandObjectTargetModulesDump : public CommandObjectMultiword
2507{
2508public:
2509
2510 //------------------------------------------------------------------
2511 // Constructors and Destructors
2512 //------------------------------------------------------------------
2513 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2514 CommandObjectMultiword (interpreter,
2515 "target modules dump",
2516 "A set of commands for dumping information about one or more target modules.",
2517 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2518 {
2519 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2520 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2521 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2522 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2523 }
2524
2525 virtual
2526 ~CommandObjectTargetModulesDump()
2527 {
2528 }
2529};
2530
Jim Inghamda26bd22012-06-08 21:56:10 +00002531class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002532{
2533public:
2534 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002535 CommandObjectParsed (interpreter,
2536 "target modules add",
2537 "Add a new module to the current target's modules.",
Greg Clayton1649a722012-11-29 22:16:27 +00002538 "target modules add [<module>]"),
Greg Claytonec9c2d22012-11-30 19:05:35 +00002539 m_option_group (interpreter),
2540 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 +00002541 {
Greg Clayton1649a722012-11-29 22:16:27 +00002542 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +00002543 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1649a722012-11-29 22:16:27 +00002544 m_option_group.Finalize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002545 }
2546
2547 virtual
2548 ~CommandObjectTargetModulesAdd ()
2549 {
2550 }
Greg Clayton1649a722012-11-29 22:16:27 +00002551
2552 virtual Options *
2553 GetOptions ()
2554 {
2555 return &m_option_group;
2556 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002557
Greg Clayton36da2aa2013-01-25 18:06:21 +00002558 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00002559 HandleArgumentCompletion (Args &input,
2560 int &cursor_index,
2561 int &cursor_char_position,
2562 OptionElementVector &opt_element_vector,
2563 int match_start_point,
2564 int max_return_elements,
2565 bool &word_complete,
2566 StringList &matches)
2567 {
2568 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2569 completion_str.erase (cursor_char_position);
2570
2571 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2572 CommandCompletions::eDiskFileCompletion,
2573 completion_str.c_str(),
2574 match_start_point,
2575 max_return_elements,
2576 NULL,
2577 word_complete,
2578 matches);
2579 return matches.GetSize();
2580 }
2581
2582protected:
Greg Clayton1649a722012-11-29 22:16:27 +00002583
2584 OptionGroupOptions m_option_group;
2585 OptionGroupUUID m_uuid_option_group;
Greg Claytonec9c2d22012-11-30 19:05:35 +00002586 OptionGroupFile m_symbol_file;
Greg Clayton1649a722012-11-29 22:16:27 +00002587
2588
Greg Claytone1f50b92011-05-03 22:09:39 +00002589 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002590 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002591 CommandReturnObject &result)
2592 {
2593 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2594 if (target == NULL)
2595 {
2596 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2597 result.SetStatus (eReturnStatusFailed);
2598 return false;
2599 }
2600 else
2601 {
Sean Callanane3f06612012-12-13 01:39:39 +00002602 bool flush = false;
2603
Greg Claytone1f50b92011-05-03 22:09:39 +00002604 const size_t argc = args.GetArgumentCount();
2605 if (argc == 0)
2606 {
Greg Clayton1649a722012-11-29 22:16:27 +00002607 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2608 {
2609 // We are given a UUID only, go locate the file
2610 ModuleSpec module_spec;
2611 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002612 if (m_symbol_file.GetOptionValue().OptionWasSet())
2613 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002614 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2615 {
2616 ModuleSP module_sp (target->GetSharedModule (module_spec));
2617 if (module_sp)
2618 {
2619 result.SetStatus (eReturnStatusSuccessFinishResult);
2620 return true;
2621 }
2622 else
2623 {
Sean Callanane3f06612012-12-13 01:39:39 +00002624 flush = true;
2625
Greg Clayton1649a722012-11-29 22:16:27 +00002626 StreamString strm;
2627 module_spec.GetUUID().Dump (&strm);
2628 if (module_spec.GetFileSpec())
2629 {
2630 if (module_spec.GetSymbolFileSpec())
2631 {
Greg Clayton97a19b22013-04-29 17:25:54 +00002632 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s and symbol file %s",
Greg Clayton1649a722012-11-29 22:16:27 +00002633 strm.GetString().c_str(),
Greg Clayton97a19b22013-04-29 17:25:54 +00002634 module_spec.GetFileSpec().GetPath().c_str(),
2635 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton1649a722012-11-29 22:16:27 +00002636 }
2637 else
2638 {
Greg Clayton97a19b22013-04-29 17:25:54 +00002639 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton1649a722012-11-29 22:16:27 +00002640 strm.GetString().c_str(),
Greg Clayton97a19b22013-04-29 17:25:54 +00002641 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton1649a722012-11-29 22:16:27 +00002642 }
2643 }
2644 else
2645 {
2646 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2647 strm.GetString().c_str());
2648 }
2649 result.SetStatus (eReturnStatusFailed);
2650 return false;
2651 }
2652 }
2653 else
2654 {
2655 StreamString strm;
2656 module_spec.GetUUID().Dump (&strm);
2657 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2658 result.SetStatus (eReturnStatusFailed);
2659 return false;
2660 }
2661 }
2662 else
2663 {
2664 result.AppendError ("one or more executable image paths must be specified");
2665 result.SetStatus (eReturnStatusFailed);
2666 return false;
2667 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002668 }
2669 else
2670 {
2671 for (size_t i=0; i<argc; ++i)
2672 {
2673 const char *path = args.GetArgumentAtIndex(i);
2674 if (path)
2675 {
2676 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002677 if (file_spec.Exists())
2678 {
Greg Clayton444fe992012-02-26 05:51:37 +00002679 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002680 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2681 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002682 if (m_symbol_file.GetOptionValue().OptionWasSet())
2683 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002684 Error error;
2685 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002686 if (!module_sp)
2687 {
Greg Clayton1649a722012-11-29 22:16:27 +00002688 const char *error_cstr = error.AsCString();
2689 if (error_cstr)
2690 result.AppendError (error_cstr);
2691 else
2692 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002693 result.SetStatus (eReturnStatusFailed);
2694 return false;
2695 }
Sean Callanane3f06612012-12-13 01:39:39 +00002696 else
2697 {
2698 flush = true;
2699 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002700 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002701 }
2702 else
2703 {
2704 char resolved_path[PATH_MAX];
2705 result.SetStatus (eReturnStatusFailed);
2706 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2707 {
2708 if (strcmp (resolved_path, path) != 0)
2709 {
2710 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2711 break;
2712 }
2713 }
2714 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2715 break;
2716 }
2717 }
2718 }
2719 }
Sean Callanane3f06612012-12-13 01:39:39 +00002720
2721 if (flush)
2722 {
2723 ProcessSP process = target->GetProcessSP();
2724 if (process)
2725 process->Flush();
2726 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002727 }
Sean Callanane3f06612012-12-13 01:39:39 +00002728
Greg Claytone1f50b92011-05-03 22:09:39 +00002729 return result.Succeeded();
2730 }
2731
Greg Claytone1f50b92011-05-03 22:09:39 +00002732};
2733
2734class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2735{
2736public:
2737 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2738 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2739 "target modules load",
2740 "Set the load addresses for one or more sections in a target module.",
2741 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2742 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002743 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 +00002744 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)
2745 {
2746 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2747 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2748 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2749 m_option_group.Finalize();
2750 }
2751
2752 virtual
2753 ~CommandObjectTargetModulesLoad ()
2754 {
2755 }
2756
Jim Inghamda26bd22012-06-08 21:56:10 +00002757 virtual Options *
2758 GetOptions ()
2759 {
2760 return &m_option_group;
2761 }
2762
2763protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002764 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002765 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002766 CommandReturnObject &result)
2767 {
2768 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2769 if (target == NULL)
2770 {
2771 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2772 result.SetStatus (eReturnStatusFailed);
2773 return false;
2774 }
2775 else
2776 {
2777 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002778 ModuleSpec module_spec;
2779 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002780 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002781 {
2782 search_using_module_spec = true;
2783 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2784 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002785
2786 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002787 {
2788 search_using_module_spec = true;
2789 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2790 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002791
Greg Clayton444fe992012-02-26 05:51:37 +00002792 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002793 {
2794
2795 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002796 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002797
2798 char path[PATH_MAX];
2799 if (num_matches == 1)
2800 {
2801 Module *module = matching_modules.GetModulePointerAtIndex(0);
2802 if (module)
2803 {
2804 ObjectFile *objfile = module->GetObjectFile();
2805 if (objfile)
2806 {
2807 SectionList *section_list = objfile->GetSectionList();
2808 if (section_list)
2809 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002810 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002811 if (argc == 0)
2812 {
2813 if (m_slide_option.GetOptionValue().OptionWasSet())
2814 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002815 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2816 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002817 }
2818 else
2819 {
2820 result.AppendError ("one or more section name + load address pair must be specified");
2821 result.SetStatus (eReturnStatusFailed);
2822 return false;
2823 }
2824 }
2825 else
2826 {
2827 if (m_slide_option.GetOptionValue().OptionWasSet())
2828 {
2829 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2830 result.SetStatus (eReturnStatusFailed);
2831 return false;
2832 }
2833
2834 for (size_t i=0; i<argc; i += 2)
2835 {
2836 const char *sect_name = args.GetArgumentAtIndex(i);
2837 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2838 if (sect_name && load_addr_cstr)
2839 {
2840 ConstString const_sect_name(sect_name);
2841 bool success = false;
2842 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2843 if (success)
2844 {
2845 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2846 if (section_sp)
2847 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002848 if (section_sp->IsThreadSpecific())
2849 {
2850 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2851 result.SetStatus (eReturnStatusFailed);
2852 break;
2853 }
2854 else
2855 {
Greg Clayton545762f2012-07-07 01:24:12 +00002856 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002857 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002858 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002859 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002860 }
2861 else
2862 {
2863 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2864 result.SetStatus (eReturnStatusFailed);
2865 break;
2866 }
2867 }
2868 else
2869 {
2870 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2871 result.SetStatus (eReturnStatusFailed);
2872 break;
2873 }
2874 }
2875 else
2876 {
2877 if (sect_name)
2878 result.AppendError ("section names must be followed by a load address.\n");
2879 else
2880 result.AppendError ("one or more section name + load address pair must be specified.\n");
2881 result.SetStatus (eReturnStatusFailed);
2882 break;
2883 }
2884 }
2885 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002886
2887 if (changed)
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002888 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002889 target->ModulesDidLoad (matching_modules);
Greg Clayton1f71bcf2013-01-29 01:17:09 +00002890 Process *process = m_exe_ctx.GetProcessPtr();
2891 if (process)
2892 process->Flush();
2893 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002894 }
2895 else
2896 {
2897 module->GetFileSpec().GetPath (path, sizeof(path));
2898 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2899 result.SetStatus (eReturnStatusFailed);
2900 }
2901 }
2902 else
2903 {
2904 module->GetFileSpec().GetPath (path, sizeof(path));
2905 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2906 result.SetStatus (eReturnStatusFailed);
2907 }
2908 }
2909 else
2910 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002911 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2912 if (module_spec_file)
2913 {
2914 module_spec_file->GetPath (path, sizeof(path));
2915 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2916 }
2917 else
2918 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002919 result.SetStatus (eReturnStatusFailed);
2920 }
2921 }
2922 else
2923 {
Jason Molenda42b336c2013-05-03 23:56:12 +00002924 std::string uuid_str;
Greg Clayton444fe992012-02-26 05:51:37 +00002925
2926 if (module_spec.GetFileSpec())
2927 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002928 else
2929 path[0] = '\0';
2930
Greg Clayton444fe992012-02-26 05:51:37 +00002931 if (module_spec.GetUUIDPtr())
Jason Molenda42b336c2013-05-03 23:56:12 +00002932 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytone1f50b92011-05-03 22:09:39 +00002933 if (num_matches > 1)
2934 {
2935 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2936 path[0] ? " file=" : "",
2937 path,
Jason Molenda42b336c2013-05-03 23:56:12 +00002938 !uuid_str.empty() ? " uuid=" : "",
2939 uuid_str.c_str());
Greg Claytone1f50b92011-05-03 22:09:39 +00002940 for (size_t i=0; i<num_matches; ++i)
2941 {
2942 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2943 result.AppendMessageWithFormat("%s\n", path);
2944 }
2945 }
2946 else
2947 {
2948 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2949 path[0] ? " file=" : "",
2950 path,
Jason Molenda42b336c2013-05-03 23:56:12 +00002951 !uuid_str.empty() ? " uuid=" : "",
2952 uuid_str.c_str());
Greg Claytone1f50b92011-05-03 22:09:39 +00002953 }
2954 result.SetStatus (eReturnStatusFailed);
2955 }
2956 }
2957 else
2958 {
2959 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2960 result.SetStatus (eReturnStatusFailed);
2961 return false;
2962 }
2963 }
2964 return result.Succeeded();
2965 }
2966
Greg Claytone1f50b92011-05-03 22:09:39 +00002967 OptionGroupOptions m_option_group;
2968 OptionGroupUUID m_uuid_option_group;
2969 OptionGroupFile m_file_option;
2970 OptionGroupUInt64 m_slide_option;
2971};
2972
2973//----------------------------------------------------------------------
2974// List images with associated information
2975//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00002976class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002977{
2978public:
2979
2980 class CommandOptions : public Options
2981 {
2982 public:
2983
2984 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00002985 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00002986 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00002987 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00002988 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00002989 {
2990 }
2991
2992 virtual
2993 ~CommandOptions ()
2994 {
2995 }
2996
2997 virtual Error
2998 SetOptionValue (uint32_t option_idx, const char *option_arg)
2999 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003000 Error error;
3001
Greg Clayton6475c422012-12-04 00:32:51 +00003002 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00003003 if (short_option == 'g')
3004 {
3005 m_use_global_module_list = true;
3006 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003007 else if (short_option == 'a')
3008 {
Jim Inghamd86cf812013-03-15 23:09:19 +00003009 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3010 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Ingham6bdea822011-10-24 18:36:33 +00003011 }
Greg Clayton899025f2011-08-09 00:01:09 +00003012 else
3013 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003014 unsigned long width = 0;
Greg Clayton899025f2011-08-09 00:01:09 +00003015 if (option_arg)
3016 width = strtoul (option_arg, NULL, 0);
3017 m_format_array.push_back(std::make_pair(short_option, width));
3018 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003019 return error;
3020 }
3021
3022 void
3023 OptionParsingStarting ()
3024 {
3025 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00003026 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00003027 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00003028 }
3029
3030 const OptionDefinition*
3031 GetDefinitions ()
3032 {
3033 return g_option_table;
3034 }
3035
3036 // Options table: Required for subclasses of Options.
3037
3038 static OptionDefinition g_option_table[];
3039
3040 // Instance variables to hold the values for command options.
3041 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3042 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00003043 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00003044 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00003045 };
3046
3047 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003048 CommandObjectParsed (interpreter,
3049 "target modules list",
3050 "List current executable and dependent shared library images.",
3051 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00003052 m_options (interpreter)
3053 {
3054 }
3055
3056 virtual
3057 ~CommandObjectTargetModulesList ()
3058 {
3059 }
3060
3061 virtual
3062 Options *
3063 GetOptions ()
3064 {
3065 return &m_options;
3066 }
3067
Jim Inghamda26bd22012-06-08 21:56:10 +00003068protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003069 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003070 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003071 CommandReturnObject &result)
3072 {
3073 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00003074 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00003075 // Define a local module list here to ensure it lives longer than any "locker"
3076 // object which might lock its contents below (through the "module_list_ptr"
3077 // variable).
3078 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00003079 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00003080 {
3081 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3082 result.SetStatus (eReturnStatusFailed);
3083 return false;
3084 }
3085 else
3086 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003087 if (target)
3088 {
3089 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3090 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3091 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3092 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003093 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00003094 Stream &strm = result.GetOutputStream();
3095
3096 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3097 {
3098 if (target)
3099 {
3100 Address module_address;
3101 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3102 {
Greg Clayton3508c382012-02-24 01:59:29 +00003103 ModuleSP module_sp (module_address.GetModule());
3104 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00003105 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003106 PrintModule (target, module_sp.get(), 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00003107 result.SetStatus (eReturnStatusSuccessFinishResult);
3108 }
3109 else
3110 {
Jim Inghambb04be12012-12-15 02:40:54 +00003111 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003112 result.SetStatus (eReturnStatusFailed);
3113 }
3114 }
3115 else
3116 {
Jim Inghambb04be12012-12-15 02:40:54 +00003117 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003118 result.SetStatus (eReturnStatusFailed);
3119 }
3120 }
3121 else
3122 {
3123 result.AppendError ("Can only look up modules by address with a valid target.");
3124 result.SetStatus (eReturnStatusFailed);
3125 }
3126 return result.Succeeded();
3127 }
3128
Greg Clayton36da2aa2013-01-25 18:06:21 +00003129 size_t num_modules = 0;
Jim Ingham93367902012-05-30 02:19:25 +00003130 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3131 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3132 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003133 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003134 const size_t argc = command.GetArgumentCount();
3135 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003136 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003137 if (use_global_module_list)
3138 {
3139 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3140 num_modules = Module::GetNumberAllocatedModules();
3141 }
3142 else
3143 {
3144 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003145 }
Greg Clayton899025f2011-08-09 00:01:09 +00003146 }
3147 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003148 {
3149 for (size_t i=0; i<argc; ++i)
3150 {
3151 // Dump specified images (by basename or fullpath)
3152 const char *arg_cstr = command.GetArgumentAtIndex(i);
3153 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3154 if (num_matches == 0)
3155 {
3156 if (argc == 1)
3157 {
3158 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3159 result.SetStatus (eReturnStatusFailed);
3160 return false;
3161 }
3162 }
3163 }
3164
Greg Clayton2ad894b2012-05-15 18:43:44 +00003165 module_list_ptr = &module_list;
3166 }
Jim Ingham93367902012-05-30 02:19:25 +00003167
3168 if (module_list_ptr != NULL)
3169 {
3170 locker.Lock(module_list_ptr->GetMutex());
3171 num_modules = module_list_ptr->GetSize();
3172 }
Greg Clayton899025f2011-08-09 00:01:09 +00003173
Greg Claytone1f50b92011-05-03 22:09:39 +00003174 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003175 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003176 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3177 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003178 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003179 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003180 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003181 {
Jim Ingham93367902012-05-30 02:19:25 +00003182 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003183 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003184 }
3185 else
3186 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003187 module = Module::GetAllocatedModuleAtIndex(image_idx);
3188 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003189 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003190
Greg Clayton36da2aa2013-01-25 18:06:21 +00003191 const size_t indent = strm.Printf("[%3u] ", image_idx);
3192 PrintModule (target, module, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003193
Greg Claytone1f50b92011-05-03 22:09:39 +00003194 }
3195 result.SetStatus (eReturnStatusSuccessFinishResult);
3196 }
3197 else
3198 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003199 if (argc)
3200 {
3201 if (use_global_module_list)
3202 result.AppendError ("the global module list has no matching modules");
3203 else
3204 result.AppendError ("the target has no matching modules");
3205 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003206 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003207 {
3208 if (use_global_module_list)
3209 result.AppendError ("the global module list is empty");
3210 else
3211 result.AppendError ("the target has no associated executable images");
3212 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003213 result.SetStatus (eReturnStatusFailed);
3214 return false;
3215 }
3216 }
3217 return result.Succeeded();
3218 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003219
3220 void
Greg Clayton36da2aa2013-01-25 18:06:21 +00003221 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003222 {
3223
Jim Ingham6f01c932012-10-12 17:34:26 +00003224 if (module == NULL)
3225 {
3226 strm.PutCString("Null module");
3227 return;
3228 }
3229
Jim Ingham6bdea822011-10-24 18:36:33 +00003230 bool dump_object_name = false;
3231 if (m_options.m_format_array.empty())
3232 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003233 m_options.m_format_array.push_back(std::make_pair('u', 0));
3234 m_options.m_format_array.push_back(std::make_pair('h', 0));
3235 m_options.m_format_array.push_back(std::make_pair('f', 0));
3236 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003237 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003238 const size_t num_entries = m_options.m_format_array.size();
3239 bool print_space = false;
3240 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003241 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003242 if (print_space)
3243 strm.PutChar(' ');
3244 print_space = true;
3245 const char format_char = m_options.m_format_array[i].first;
3246 uint32_t width = m_options.m_format_array[i].second;
3247 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003248 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003249 case 'A':
3250 DumpModuleArchitecture (strm, module, false, width);
3251 break;
3252
3253 case 't':
3254 DumpModuleArchitecture (strm, module, true, width);
3255 break;
3256
3257 case 'f':
3258 DumpFullpath (strm, &module->GetFileSpec(), width);
3259 dump_object_name = true;
3260 break;
3261
3262 case 'd':
3263 DumpDirectory (strm, &module->GetFileSpec(), width);
3264 break;
3265
3266 case 'b':
3267 DumpBasename (strm, &module->GetFileSpec(), width);
3268 dump_object_name = true;
3269 break;
3270
3271 case 'h':
3272 case 'o':
3273 // Image header address
3274 {
3275 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003276
Greg Claytonb5a8f142012-02-05 02:38:54 +00003277 ObjectFile *objfile = module->GetObjectFile ();
3278 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003279 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003280 Address header_addr(objfile->GetHeaderAddress());
3281 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003282 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003283 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003284 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003285 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3286 if (header_load_addr == LLDB_INVALID_ADDRESS)
3287 {
3288 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3289 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003290 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003291 {
3292 if (format_char == 'o')
3293 {
3294 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003295 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003296 }
3297 else
3298 {
3299 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003300 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003301 }
3302 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003303 break;
3304 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003305 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3306 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3307 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003308 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003309 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003310 strm.Printf ("%*s", addr_nibble_width + 2, "");
3311 }
3312 break;
3313 case 'r':
3314 {
Greg Clayton36da2aa2013-01-25 18:06:21 +00003315 size_t ref_count = 0;
Greg Claytonb5a8f142012-02-05 02:38:54 +00003316 ModuleSP module_sp (module->shared_from_this());
3317 if (module_sp)
3318 {
3319 // Take one away to make sure we don't count our local "module_sp"
3320 ref_count = module_sp.use_count() - 1;
3321 }
3322 if (width)
Greg Clayton36da2aa2013-01-25 18:06:21 +00003323 strm.Printf("{%*zu}", width, ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003324 else
Greg Clayton36da2aa2013-01-25 18:06:21 +00003325 strm.Printf("{%zu}", ref_count);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003326 }
3327 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003328
Greg Claytonb5a8f142012-02-05 02:38:54 +00003329 case 's':
3330 case 'S':
3331 {
3332 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3333 if (symbol_vendor)
3334 {
3335 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3336 if (symbol_file)
3337 {
3338 if (format_char == 'S')
3339 {
3340 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3341 // Dump symbol file only if different from module file
3342 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3343 {
3344 print_space = false;
3345 break;
3346 }
3347 // Add a newline and indent past the index
3348 strm.Printf ("\n%*s", indent, "");
3349 }
3350 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3351 dump_object_name = true;
3352 break;
3353 }
3354 }
3355 strm.Printf("%.*s", width, "<NONE>");
3356 }
3357 break;
3358
3359 case 'm':
3360 module->GetModificationTime().Dump(&strm, width);
3361 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003362
Greg Claytonb5a8f142012-02-05 02:38:54 +00003363 case 'p':
3364 strm.Printf("%p", module);
3365 break;
3366
3367 case 'u':
3368 DumpModuleUUID(strm, module);
3369 break;
3370
3371 default:
3372 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003373 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003374
3375 }
3376 if (dump_object_name)
3377 {
3378 const char *object_name = module->GetObjectName().GetCString();
3379 if (object_name)
3380 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003381 }
3382 strm.EOL();
3383 }
3384
Greg Claytone1f50b92011-05-03 22:09:39 +00003385 CommandOptions m_options;
3386};
3387
3388OptionDefinition
3389CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3390{
Enrico Granata4968ad82013-01-29 01:48:30 +00003391 { 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 +00003392 { 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 +00003393 { 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 +00003394 { 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."},
3395 { 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 +00003396 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3397 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3398 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3399 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3400 { 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 +00003401 { 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 +00003402 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3403 { 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."},
3404 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003405 { 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 +00003406 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3407};
3408
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003409#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003410
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003411//----------------------------------------------------------------------
3412// Lookup unwind information in images
3413//----------------------------------------------------------------------
3414
3415class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3416{
3417public:
3418
3419 enum
3420 {
3421 eLookupTypeInvalid = -1,
3422 eLookupTypeAddress = 0,
3423 eLookupTypeSymbol,
3424 eLookupTypeFunction,
3425 eLookupTypeFunctionOrSymbol,
3426 kNumLookupTypes
3427 };
3428
3429 class CommandOptions : public Options
3430 {
3431 public:
3432
3433 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003434 Options(interpreter),
3435 m_type(eLookupTypeInvalid),
3436 m_str(),
3437 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003438 {
3439 }
3440
3441 virtual
3442 ~CommandOptions ()
3443 {
3444 }
3445
3446 virtual Error
3447 SetOptionValue (uint32_t option_idx, const char *option_arg)
3448 {
3449 Error error;
3450
Greg Clayton6475c422012-12-04 00:32:51 +00003451 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003452
3453 switch (short_option)
3454 {
3455 case 'a':
Jason Molenda5a559062013-04-23 04:30:57 +00003456 {
3457 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003458 m_type = eLookupTypeAddress;
Jason Molenda5a559062013-04-23 04:30:57 +00003459 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003460 if (m_addr == LLDB_INVALID_ADDRESS)
3461 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3462 break;
Jason Molenda5a559062013-04-23 04:30:57 +00003463 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003464
3465 case 'n':
Jason Molenda5a559062013-04-23 04:30:57 +00003466 {
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003467 m_str = option_arg;
3468 m_type = eLookupTypeFunctionOrSymbol;
3469 break;
Jason Molenda5a559062013-04-23 04:30:57 +00003470 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003471 }
3472
3473 return error;
3474 }
3475
3476 void
3477 OptionParsingStarting ()
3478 {
3479 m_type = eLookupTypeInvalid;
3480 m_str.clear();
3481 m_addr = LLDB_INVALID_ADDRESS;
3482 }
3483
3484 const OptionDefinition*
3485 GetDefinitions ()
3486 {
3487 return g_option_table;
3488 }
3489
3490 // Options table: Required for subclasses of Options.
3491
3492 static OptionDefinition g_option_table[];
3493
3494 // Instance variables to hold the values for command options.
3495
3496 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3497 std::string m_str; // Holds name lookup
3498 lldb::addr_t m_addr; // Holds the address to lookup
3499 };
3500
3501 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3502 CommandObjectParsed (interpreter,
3503 "target modules show-unwind",
3504 "Show synthesized unwind instructions for a function.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003505 NULL,
3506 eFlagRequiresTarget |
3507 eFlagRequiresProcess |
3508 eFlagProcessMustBeLaunched |
3509 eFlagProcessMustBePaused ),
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003510 m_options (interpreter)
3511 {
3512 }
3513
3514 virtual
3515 ~CommandObjectTargetModulesShowUnwind ()
3516 {
3517 }
3518
3519 virtual
3520 Options *
3521 GetOptions ()
3522 {
3523 return &m_options;
3524 }
3525
3526protected:
3527 bool
3528 DoExecute (Args& command,
3529 CommandReturnObject &result)
3530 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003531 Target *target = m_exe_ctx.GetTargetPtr();
3532 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003533 ABI *abi = NULL;
3534 if (process)
3535 abi = process->GetABI().get();
3536
3537 if (process == NULL)
3538 {
3539 result.AppendError ("You must have a process running to use this command.");
3540 result.SetStatus (eReturnStatusFailed);
3541 return false;
3542 }
3543
3544 ThreadList threads(process->GetThreadList());
3545 if (threads.GetSize() == 0)
3546 {
3547 result.AppendError ("The process must be paused to use this command.");
3548 result.SetStatus (eReturnStatusFailed);
3549 return false;
3550 }
3551
3552 ThreadSP thread(threads.GetThreadAtIndex(0));
3553 if (thread.get() == NULL)
3554 {
3555 result.AppendError ("The process must be paused to use this command.");
3556 result.SetStatus (eReturnStatusFailed);
3557 return false;
3558 }
3559
Jason Molenda5a559062013-04-23 04:30:57 +00003560 SymbolContextList sc_list;
3561
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003562 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3563 {
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003564 ConstString function_name (m_options.m_str.c_str());
Jason Molenda5a559062013-04-23 04:30:57 +00003565 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3566 }
3567 else if (m_options.m_type == eLookupTypeAddress && target)
3568 {
3569 Address addr;
3570 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003571 {
3572 SymbolContext sc;
Jason Molenda5a559062013-04-23 04:30:57 +00003573 ModuleSP module_sp (addr.GetModule());
3574 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3575 if (sc.function || sc.symbol)
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003576 {
Jason Molenda5a559062013-04-23 04:30:57 +00003577 sc_list.Append(sc);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003578 }
Jason Molenda5a559062013-04-23 04:30:57 +00003579 }
3580 }
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003581
Jason Molenda5a559062013-04-23 04:30:57 +00003582 size_t num_matches = sc_list.GetSize();
3583 for (uint32_t idx = 0; idx < num_matches; idx++)
3584 {
3585 SymbolContext sc;
3586 sc_list.GetContextAtIndex(idx, sc);
3587 if (sc.symbol == NULL && sc.function == NULL)
3588 continue;
3589 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3590 continue;
3591 AddressRange range;
3592 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3593 continue;
3594 if (!range.GetBaseAddress().IsValid())
3595 continue;
3596 ConstString funcname(sc.GetFunctionName());
3597 if (funcname.IsEmpty())
3598 continue;
3599 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3600 if (abi)
3601 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003602
Jason Molenda5a559062013-04-23 04:30:57 +00003603 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3604 if (func_unwinders_sp.get() == NULL)
3605 continue;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003606
Jason Molenda5a559062013-04-23 04:30:57 +00003607 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3608 if (first_non_prologue_insn.IsValid())
3609 {
3610 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 +00003611 result.GetOutputStream().Printf ("\n");
3612 }
Jason Molenda5a559062013-04-23 04:30:57 +00003613
3614 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3615 if (non_callsite_unwind_plan.get())
3616 {
3617 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);
3618 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3619 result.GetOutputStream().Printf ("\n");
3620 }
3621
3622 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3623 if (callsite_unwind_plan.get())
3624 {
3625 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);
3626 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3627 result.GetOutputStream().Printf ("\n");
3628 }
3629
3630 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3631 if (arch_default_unwind_plan.get())
3632 {
3633 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3634 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3635 result.GetOutputStream().Printf ("\n");
3636 }
3637
3638 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3639 if (fast_unwind_plan.get())
3640 {
3641 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3642 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3643 result.GetOutputStream().Printf ("\n");
3644 }
3645
3646
3647 result.GetOutputStream().Printf ("\n");
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003648 }
3649 return result.Succeeded();
3650 }
3651
3652 CommandOptions m_options;
3653};
3654
3655OptionDefinition
3656CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3657{
Jason Molenda5a559062013-04-23 04:30:57 +00003658 { LLDB_OPT_SET_1, false, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3659 { LLDB_OPT_SET_2, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003660 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3661};
Greg Claytone1f50b92011-05-03 22:09:39 +00003662
3663//----------------------------------------------------------------------
3664// Lookup information in images
3665//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003666class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003667{
3668public:
3669
3670 enum
3671 {
3672 eLookupTypeInvalid = -1,
3673 eLookupTypeAddress = 0,
3674 eLookupTypeSymbol,
3675 eLookupTypeFileLine, // Line is optional
3676 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003677 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003678 eLookupTypeType,
3679 kNumLookupTypes
3680 };
3681
3682 class CommandOptions : public Options
3683 {
3684 public:
3685
3686 CommandOptions (CommandInterpreter &interpreter) :
3687 Options(interpreter)
3688 {
3689 OptionParsingStarting();
3690 }
3691
3692 virtual
3693 ~CommandOptions ()
3694 {
3695 }
3696
3697 virtual Error
3698 SetOptionValue (uint32_t option_idx, const char *option_arg)
3699 {
3700 Error error;
3701
Greg Clayton6475c422012-12-04 00:32:51 +00003702 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone1f50b92011-05-03 22:09:39 +00003703
3704 switch (short_option)
3705 {
3706 case 'a':
Jim Inghamd86cf812013-03-15 23:09:19 +00003707 {
3708 m_type = eLookupTypeAddress;
3709 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3710 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3711 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003712 break;
3713
3714 case 'o':
3715 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3716 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003717 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003718 break;
3719
3720 case 's':
3721 m_str = option_arg;
3722 m_type = eLookupTypeSymbol;
3723 break;
3724
3725 case 'f':
3726 m_file.SetFile (option_arg, false);
3727 m_type = eLookupTypeFileLine;
3728 break;
3729
3730 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003731 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003732 break;
3733
3734 case 'l':
3735 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3736 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003737 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003738 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003739 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003740 m_type = eLookupTypeFileLine;
3741 break;
3742
Greg Clayton2ad894b2012-05-15 18:43:44 +00003743 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003744 m_str = option_arg;
3745 m_type = eLookupTypeFunction;
3746 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003747
3748 case 'n':
3749 m_str = option_arg;
3750 m_type = eLookupTypeFunctionOrSymbol;
3751 break;
3752
Greg Claytone1f50b92011-05-03 22:09:39 +00003753 case 't':
3754 m_str = option_arg;
3755 m_type = eLookupTypeType;
3756 break;
3757
3758 case 'v':
3759 m_verbose = 1;
3760 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003761
3762 case 'A':
3763 m_print_all = true;
3764 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003765
3766 case 'r':
3767 m_use_regex = true;
3768 break;
3769 }
3770
3771 return error;
3772 }
3773
3774 void
3775 OptionParsingStarting ()
3776 {
3777 m_type = eLookupTypeInvalid;
3778 m_str.clear();
3779 m_file.Clear();
3780 m_addr = LLDB_INVALID_ADDRESS;
3781 m_offset = 0;
3782 m_line_number = 0;
3783 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003784 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003785 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003786 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003787 }
3788
3789 const OptionDefinition*
3790 GetDefinitions ()
3791 {
3792 return g_option_table;
3793 }
3794
3795 // Options table: Required for subclasses of Options.
3796
3797 static OptionDefinition g_option_table[];
3798 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3799 std::string m_str; // Holds name lookup
3800 FileSpec m_file; // Files for file lookups
3801 lldb::addr_t m_addr; // Holds the address to lookup
3802 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3803 uint32_t m_line_number; // Line number for file+line lookups
3804 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003805 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003806 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003807 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003808
3809 };
3810
3811 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003812 CommandObjectParsed (interpreter,
3813 "target modules lookup",
3814 "Look up information within executable and dependent shared library images.",
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003815 NULL,
3816 eFlagRequiresTarget),
Jim Inghamda26bd22012-06-08 21:56:10 +00003817 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003818 {
3819 CommandArgumentEntry arg;
3820 CommandArgumentData file_arg;
3821
3822 // Define the first (and only) variant of this arg.
3823 file_arg.arg_type = eArgTypeFilename;
3824 file_arg.arg_repetition = eArgRepeatStar;
3825
3826 // There is only one variant this argument could be; put it into the argument entry.
3827 arg.push_back (file_arg);
3828
3829 // Push the data for the first argument into the m_arguments vector.
3830 m_arguments.push_back (arg);
3831 }
3832
3833 virtual
3834 ~CommandObjectTargetModulesLookup ()
3835 {
3836 }
3837
3838 virtual Options *
3839 GetOptions ()
3840 {
3841 return &m_options;
3842 }
3843
Sean Callanan56d31ec2012-06-06 20:49:55 +00003844 bool
3845 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3846 {
3847 switch (m_options.m_type)
3848 {
3849 case eLookupTypeAddress:
3850 case eLookupTypeFileLine:
3851 case eLookupTypeFunction:
3852 case eLookupTypeFunctionOrSymbol:
3853 case eLookupTypeSymbol:
3854 default:
3855 return false;
3856 case eLookupTypeType:
3857 break;
3858 }
3859
Greg Claytonea0bb4d2013-01-09 19:44:40 +00003860 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Sean Callanan56d31ec2012-06-06 20:49:55 +00003861
3862 if (!frame)
3863 return false;
3864
3865 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3866
3867 if (!sym_ctx.module_sp)
3868 return false;
3869
3870 switch (m_options.m_type)
3871 {
3872 default:
3873 return false;
3874 case eLookupTypeType:
3875 if (!m_options.m_str.empty())
3876 {
3877 if (LookupTypeHere (m_interpreter,
3878 result.GetOutputStream(),
3879 sym_ctx,
3880 m_options.m_str.c_str(),
3881 m_options.m_use_regex))
3882 {
3883 result.SetStatus(eReturnStatusSuccessFinishResult);
3884 return true;
3885 }
3886 }
3887 break;
3888 }
3889
3890 return true;
3891 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003892
3893 bool
3894 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3895 {
3896 switch (m_options.m_type)
3897 {
3898 case eLookupTypeAddress:
3899 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3900 {
3901 if (LookupAddressInModule (m_interpreter,
3902 result.GetOutputStream(),
3903 module,
3904 eSymbolContextEverything,
3905 m_options.m_addr,
3906 m_options.m_offset,
3907 m_options.m_verbose))
3908 {
3909 result.SetStatus(eReturnStatusSuccessFinishResult);
3910 return true;
3911 }
3912 }
3913 break;
3914
3915 case eLookupTypeSymbol:
3916 if (!m_options.m_str.empty())
3917 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003918 if (LookupSymbolInModule (m_interpreter,
3919 result.GetOutputStream(),
3920 module,
3921 m_options.m_str.c_str(),
3922 m_options.m_use_regex,
3923 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003924 {
3925 result.SetStatus(eReturnStatusSuccessFinishResult);
3926 return true;
3927 }
3928 }
3929 break;
3930
3931 case eLookupTypeFileLine:
3932 if (m_options.m_file)
3933 {
3934
3935 if (LookupFileAndLineInModule (m_interpreter,
3936 result.GetOutputStream(),
3937 module,
3938 m_options.m_file,
3939 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003940 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003941 m_options.m_verbose))
3942 {
3943 result.SetStatus(eReturnStatusSuccessFinishResult);
3944 return true;
3945 }
3946 }
3947 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003948
3949 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003950 case eLookupTypeFunction:
3951 if (!m_options.m_str.empty())
3952 {
3953 if (LookupFunctionInModule (m_interpreter,
3954 result.GetOutputStream(),
3955 module,
3956 m_options.m_str.c_str(),
3957 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003958 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003959 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003960 m_options.m_verbose))
3961 {
3962 result.SetStatus(eReturnStatusSuccessFinishResult);
3963 return true;
3964 }
3965 }
3966 break;
3967
Greg Clayton2ad894b2012-05-15 18:43:44 +00003968
Greg Claytone1f50b92011-05-03 22:09:39 +00003969 case eLookupTypeType:
3970 if (!m_options.m_str.empty())
3971 {
3972 if (LookupTypeInModule (m_interpreter,
3973 result.GetOutputStream(),
3974 module,
3975 m_options.m_str.c_str(),
3976 m_options.m_use_regex))
3977 {
3978 result.SetStatus(eReturnStatusSuccessFinishResult);
3979 return true;
3980 }
3981 }
3982 break;
3983
3984 default:
3985 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3986 syntax_error = true;
3987 break;
3988 }
3989
3990 result.SetStatus (eReturnStatusFailed);
3991 return false;
3992 }
3993
Jim Inghamda26bd22012-06-08 21:56:10 +00003994protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003995 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003996 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003997 CommandReturnObject &result)
3998 {
3999 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4000 if (target == NULL)
4001 {
4002 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4003 result.SetStatus (eReturnStatusFailed);
4004 return false;
4005 }
4006 else
4007 {
4008 bool syntax_error = false;
4009 uint32_t i;
4010 uint32_t num_successful_lookups = 0;
4011 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4012 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4013 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4014 // Dump all sections for all modules images
4015
4016 if (command.GetArgumentCount() == 0)
4017 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004018 ModuleSP current_module;
4019
4020 // Where it is possible to look in the current symbol context
4021 // first, try that. If this search was successful and --all
4022 // was not passed, don't print anything else.
4023 if (LookupHere (m_interpreter, result, syntax_error))
4024 {
4025 result.GetOutputStream().EOL();
4026 num_successful_lookups++;
4027 if (!m_options.m_print_all)
4028 {
4029 result.SetStatus (eReturnStatusSuccessFinishResult);
4030 return result.Succeeded();
4031 }
4032 }
4033
4034 // Dump all sections for all other modules
4035
Enrico Granata146d9522012-11-08 02:22:02 +00004036 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00004037 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Clayton36da2aa2013-01-25 18:06:21 +00004038 const size_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00004039 if (num_modules > 0)
4040 {
4041 for (i = 0; i<num_modules && syntax_error == false; ++i)
4042 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004043 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
4044
4045 if (module_pointer != current_module.get() &&
4046 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004047 {
4048 result.GetOutputStream().EOL();
4049 num_successful_lookups++;
4050 }
4051 }
4052 }
4053 else
4054 {
4055 result.AppendError ("the target has no associated executable images");
4056 result.SetStatus (eReturnStatusFailed);
4057 return false;
4058 }
4059 }
4060 else
4061 {
4062 // Dump specified images (by basename or fullpath)
4063 const char *arg_cstr;
4064 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4065 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004066 ModuleList module_list;
4067 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4068 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00004069 {
Jason Molendabf41e192012-10-04 22:47:07 +00004070 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00004071 {
Jason Molendabf41e192012-10-04 22:47:07 +00004072 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00004073 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00004074 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004075 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004076 {
4077 result.GetOutputStream().EOL();
4078 num_successful_lookups++;
4079 }
4080 }
4081 }
4082 }
4083 else
4084 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4085 }
4086 }
4087
4088 if (num_successful_lookups > 0)
4089 result.SetStatus (eReturnStatusSuccessFinishResult);
4090 else
4091 result.SetStatus (eReturnStatusFailed);
4092 }
4093 return result.Succeeded();
4094 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004095
4096 CommandOptions m_options;
4097};
4098
4099OptionDefinition
4100CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4101{
Jim Inghamd86cf812013-03-15 23:09:19 +00004102 { 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 +00004103 { 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 +00004104 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4105 /* 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 +00004106 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4107 { 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."},
4108 { 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."},
4109 { 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 +00004110 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00004111 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4112 { 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."},
4113 { 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."},
4114 { 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."},
4115 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4116 { 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."},
4117 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00004118};
Chris Lattner24943d22010-06-08 16:52:24 +00004119
4120
Jim Inghamd60d94a2011-03-11 03:53:59 +00004121#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00004122
4123//-------------------------------------------------------------------------
4124// CommandObjectMultiwordImageSearchPaths
4125//-------------------------------------------------------------------------
4126
Greg Claytone1f50b92011-05-03 22:09:39 +00004127class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00004128{
4129public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004130
4131 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4132 CommandObjectMultiword (interpreter,
4133 "target modules search-paths",
4134 "A set of commands for operating on debugger target image search paths.",
4135 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004136 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004137 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4138 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4139 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4140 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4141 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004142 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004143
4144 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004145 {
4146 }
4147};
4148
Greg Claytone1f50b92011-05-03 22:09:39 +00004149
4150
4151#pragma mark CommandObjectTargetModules
4152
4153//-------------------------------------------------------------------------
4154// CommandObjectTargetModules
4155//-------------------------------------------------------------------------
4156
4157class CommandObjectTargetModules : public CommandObjectMultiword
4158{
4159public:
4160 //------------------------------------------------------------------
4161 // Constructors and Destructors
4162 //------------------------------------------------------------------
4163 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4164 CommandObjectMultiword (interpreter,
4165 "target modules",
4166 "A set of commands for accessing information for one or more target modules.",
4167 "target modules <sub-command> ...")
4168 {
4169 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4170 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004171 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4172 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4173 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4174 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004175 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004176
4177 }
4178 virtual
4179 ~CommandObjectTargetModules()
4180 {
4181 }
4182
4183private:
4184 //------------------------------------------------------------------
4185 // For CommandObjectTargetModules only
4186 //------------------------------------------------------------------
4187 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4188};
4189
4190
Greg Clayton3508c382012-02-24 01:59:29 +00004191
Jim Inghamda26bd22012-06-08 21:56:10 +00004192class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004193{
4194public:
4195 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004196 CommandObjectParsed (interpreter,
4197 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004198 "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 +00004199 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Clayton437b5bc2012-09-27 22:26:11 +00004200 m_option_group (interpreter),
4201 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4202 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4203
Greg Clayton3508c382012-02-24 01:59:29 +00004204 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004205 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4206 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4207 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4208 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004209 }
4210
4211 virtual
4212 ~CommandObjectTargetSymbolsAdd ()
4213 {
4214 }
4215
Greg Clayton36da2aa2013-01-25 18:06:21 +00004216 virtual int
Jim Inghamda26bd22012-06-08 21:56:10 +00004217 HandleArgumentCompletion (Args &input,
4218 int &cursor_index,
4219 int &cursor_char_position,
4220 OptionElementVector &opt_element_vector,
4221 int match_start_point,
4222 int max_return_elements,
4223 bool &word_complete,
4224 StringList &matches)
4225 {
4226 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4227 completion_str.erase (cursor_char_position);
4228
4229 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4230 CommandCompletions::eDiskFileCompletion,
4231 completion_str.c_str(),
4232 match_start_point,
4233 max_return_elements,
4234 NULL,
4235 word_complete,
4236 matches);
4237 return matches.GetSize();
4238 }
4239
Greg Clayton437b5bc2012-09-27 22:26:11 +00004240 virtual Options *
4241 GetOptions ()
4242 {
4243 return &m_option_group;
4244 }
4245
4246
Jim Inghamda26bd22012-06-08 21:56:10 +00004247protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004248
4249 bool
4250 AddModuleSymbols (Target *target,
Greg Claytonc92fded2012-12-12 01:15:30 +00004251 ModuleSpec &module_spec,
Greg Clayton437b5bc2012-09-27 22:26:11 +00004252 bool &flush,
4253 CommandReturnObject &result)
4254 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004255 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4256 if (symbol_fspec)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004257 {
4258 char symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004259 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
4260
4261 if (!module_spec.GetUUID().IsValid())
4262 {
4263 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4264 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4265 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004266 // We now have a module that represents a symbol file
4267 // that can be used for a module that might exist in the
4268 // current target, so we need to find that module in the
4269 // target
Greg Claytonc92fded2012-12-12 01:15:30 +00004270 ModuleList matching_module_list;
Greg Clayton95fd2852013-05-15 19:52:08 +00004271
4272 size_t num_matches = 0;
4273 // First extract all module specs from the symbol file
4274 lldb_private::ModuleSpecList symfile_module_specs;
4275 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, symfile_module_specs))
4276 {
4277 // Now extract the module spec that matches the target architecture
4278 ModuleSpec target_arch_module_spec;
4279 ModuleSpec symfile_module_spec;
4280 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4281 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4282 {
4283 // See if it has a UUID?
4284 if (symfile_module_spec.GetUUID().IsValid())
4285 {
4286 // It has a UUID, look for this UUID in the target modules
4287 ModuleSpec symfile_uuid_module_spec;
4288 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4289 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4290 }
4291 }
4292
4293 if (num_matches == 0)
4294 {
4295 // No matches yet, iterate through the module specs to find a UUID value that
4296 // we can match up to an image in our target
4297 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4298 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4299 {
4300 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4301 {
4302 if (symfile_module_spec.GetUUID().IsValid())
4303 {
4304 // It has a UUID, look for this UUID in the target modules
4305 ModuleSpec symfile_uuid_module_spec;
4306 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4307 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4308 }
4309 }
4310 }
4311 }
4312 }
4313
4314 // Just try to match up the file by basename if we have no matches at this point
4315 if (num_matches == 0)
4316 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4317
Greg Claytond9735a12013-01-11 23:44:27 +00004318 while (num_matches == 0)
4319 {
4320 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4321 // Empty string returned, lets bail
4322 if (!filename_no_extension)
4323 break;
4324
4325 // Check if there was no extension to strip and the basename is the same
4326 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4327 break;
4328
4329 // Replace basename with one less extension
4330 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4331
4332 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Greg Clayton95fd2852013-05-15 19:52:08 +00004333
Greg Claytond9735a12013-01-11 23:44:27 +00004334 }
4335
Greg Claytonc92fded2012-12-12 01:15:30 +00004336 if (num_matches > 1)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004337 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004338 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4339 }
4340 else if (num_matches == 1)
4341 {
4342 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
4343
Greg Clayton437b5bc2012-09-27 22:26:11 +00004344 // The module has not yet created its symbol vendor, we can just
4345 // give the existing target module the symfile path to use for
4346 // when it decides to create it!
Greg Claytonc92fded2012-12-12 01:15:30 +00004347 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004348
Greg Clayton18809182012-12-14 02:15:00 +00004349 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Claytonc92fded2012-12-12 01:15:30 +00004350 if (symbol_vendor)
4351 {
4352 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4353
4354 if (symbol_file)
4355 {
4356 ObjectFile *object_file = symbol_file->GetObjectFile();
4357
4358 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4359 {
4360 // Provide feedback that the symfile has been successfully added.
4361 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Clayton97a19b22013-04-29 17:25:54 +00004362 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Claytonc92fded2012-12-12 01:15:30 +00004363 symfile_path,
Greg Clayton97a19b22013-04-29 17:25:54 +00004364 module_fs.GetPath().c_str());
Greg Claytonc92fded2012-12-12 01:15:30 +00004365
4366 // Let clients know something changed in the module
4367 // if it is currently loaded
4368 ModuleList module_list;
4369 module_list.Append (module_sp);
Enrico Granata12fbcf52013-04-05 18:49:06 +00004370 target->SymbolsDidLoad (module_list);
Greg Claytond9735a12013-01-11 23:44:27 +00004371
4372 // Make sure we load any scripting resources that may be embedded
4373 // in the debug info files in case the platform supports that.
4374 Error error;
Enrico Granata02af4942013-05-21 00:00:30 +00004375 StreamString feedback_stream;
4376 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granata37c43e42013-05-20 22:40:54 +00004377 if (error.Fail() && error.AsCString())
Enrico Granata1d3db0a2013-05-13 17:03:52 +00004378 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4379 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4380 error.AsCString());
Enrico Granata02af4942013-05-21 00:00:30 +00004381 else if (feedback_stream.GetSize())
4382 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Claytond9735a12013-01-11 23:44:27 +00004383
Greg Claytonc92fded2012-12-12 01:15:30 +00004384 flush = true;
4385 result.SetStatus (eReturnStatusSuccessFinishResult);
4386 return true;
4387 }
4388 }
4389 }
4390 // Clear the symbol file spec if anything went wrong
4391 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Claytonc92fded2012-12-12 01:15:30 +00004392 }
4393
4394 if (module_spec.GetUUID().IsValid())
4395 {
4396 StreamString ss_symfile_uuid;
4397 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4398 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4399 symfile_path,
4400 ss_symfile_uuid.GetData(),
4401 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4402 ? "\n please specify the full path to the symbol file"
4403 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004404 }
4405 else
4406 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004407 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4408 symfile_path,
4409 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4410 ? "\n please specify the full path to the symbol file"
4411 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004412 }
4413 }
4414 else
4415 {
4416 result.AppendError ("one or more executable image paths must be specified");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004417 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004418 result.SetStatus (eReturnStatusFailed);
4419 return false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004420 }
4421
Greg Clayton3508c382012-02-24 01:59:29 +00004422 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004423 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004424 CommandReturnObject &result)
4425 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004426 Target *target = m_exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004427 result.SetStatus (eReturnStatusFailed);
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004428 bool flush = false;
4429 ModuleSpec module_spec;
4430 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4431 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4432 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004433
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004434 const size_t argc = args.GetArgumentCount();
4435 if (argc == 0)
4436 {
4437 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004438 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004439 bool success = false;
4440 bool error_set = false;
4441 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004442 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004443 Process *process = m_exe_ctx.GetProcessPtr();
4444 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004445 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004446 const StateType process_state = process->GetState();
4447 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004448 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004449 StackFrame *frame = m_exe_ctx.GetFramePtr();
4450 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004451 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004452 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4453 if (frame_module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004454 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004455 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Clayton437b5bc2012-09-27 22:26:11 +00004456 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004457 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4458 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004459 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004460 module_spec.GetUUID() = frame_module_sp->GetUUID();
4461 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Clayton3508c382012-02-24 01:59:29 +00004462 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004463 else
4464 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004465 result.AppendError ("frame has no module");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004466 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004467 }
Greg Clayton3508c382012-02-24 01:59:29 +00004468 }
4469 else
4470 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004471 result.AppendError ("invalid current frame");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004472 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004473 }
Greg Clayton3508c382012-02-24 01:59:29 +00004474 }
4475 else
4476 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004477 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Clayton437b5bc2012-09-27 22:26:11 +00004478 error_set = true;
4479 }
4480 }
4481 else
4482 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004483 result.AppendError ("a process must exist in order to use the --frame option");
4484 error_set = true;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004485 }
4486 }
4487 else
4488 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004489 if (uuid_option_set)
4490 {
4491 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4492 success |= module_spec.GetUUID().IsValid();
4493 }
4494 else if (file_option_set)
4495 {
4496 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4497 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4498 if (module_sp)
4499 {
4500 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4501 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4502 module_spec.GetUUID() = module_sp->GetUUID();
4503 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4504 }
4505 else
4506 {
4507 module_spec.GetArchitecture() = target->GetArchitecture();
4508 }
4509 success |= module_spec.GetFileSpec().Exists();
4510 }
4511 }
4512
4513 if (success)
4514 {
4515 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4516 {
4517 if (module_spec.GetSymbolFileSpec())
4518 success = AddModuleSymbols (target, module_spec, flush, result);
4519 }
4520 }
4521
4522 if (!success && !error_set)
4523 {
4524 StreamString error_strm;
4525 if (uuid_option_set)
4526 {
4527 error_strm.PutCString("unable to find debug symbols for UUID ");
4528 module_spec.GetUUID().Dump (&error_strm);
4529 }
4530 else if (file_option_set)
4531 {
4532 error_strm.PutCString("unable to find debug symbols for the executable file ");
4533 error_strm << module_spec.GetFileSpec();
4534 }
4535 else if (frame_option_set)
4536 {
4537 error_strm.PutCString("unable to find debug symbols for the current frame");
4538 }
4539 result.AppendError (error_strm.GetData());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004540 }
4541 }
4542 else
4543 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004544 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4545 }
4546 }
4547 else
4548 {
4549 if (uuid_option_set)
4550 {
4551 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4552 }
4553 else if (file_option_set)
4554 {
4555 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4556 }
4557 else if (frame_option_set)
4558 {
4559 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4560 }
4561 else
4562 {
4563 PlatformSP platform_sp (target->GetPlatform());
Greg Clayton437b5bc2012-09-27 22:26:11 +00004564
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004565 for (size_t i=0; i<argc; ++i)
4566 {
4567 const char *symfile_path = args.GetArgumentAtIndex(i);
4568 if (symfile_path)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004569 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004570 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4571 if (platform_sp)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004572 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004573 FileSpec symfile_spec;
4574 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4575 module_spec.GetSymbolFileSpec() = symfile_spec;
4576 }
4577
4578 ArchSpec arch;
4579 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004580
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004581 if (symfile_exists)
4582 {
4583 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004584 break;
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004585 }
4586 else
4587 {
4588 char resolved_symfile_path[PATH_MAX];
4589 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4590 {
4591 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4592 {
4593 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4594 break;
4595 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004596 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004597 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4598 break;
Greg Clayton3508c382012-02-24 01:59:29 +00004599 }
4600 }
4601 }
4602 }
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004603 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004604
Greg Claytonea0bb4d2013-01-09 19:44:40 +00004605 if (flush)
4606 {
4607 Process *process = m_exe_ctx.GetProcessPtr();
4608 if (process)
4609 process->Flush();
Greg Clayton3508c382012-02-24 01:59:29 +00004610 }
4611 return result.Succeeded();
4612 }
4613
Greg Clayton437b5bc2012-09-27 22:26:11 +00004614 OptionGroupOptions m_option_group;
4615 OptionGroupUUID m_uuid_option_group;
4616 OptionGroupFile m_file_option;
4617 OptionGroupBoolean m_current_frame_option;
4618
4619
Greg Clayton3508c382012-02-24 01:59:29 +00004620};
4621
4622
4623#pragma mark CommandObjectTargetSymbols
4624
4625//-------------------------------------------------------------------------
4626// CommandObjectTargetSymbols
4627//-------------------------------------------------------------------------
4628
4629class CommandObjectTargetSymbols : public CommandObjectMultiword
4630{
4631public:
4632 //------------------------------------------------------------------
4633 // Constructors and Destructors
4634 //------------------------------------------------------------------
4635 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4636 CommandObjectMultiword (interpreter,
4637 "target symbols",
4638 "A set of commands for adding and managing debug symbol files.",
4639 "target symbols <sub-command> ...")
4640 {
4641 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4642
4643 }
4644 virtual
4645 ~CommandObjectTargetSymbols()
4646 {
4647 }
4648
4649private:
4650 //------------------------------------------------------------------
4651 // For CommandObjectTargetModules only
4652 //------------------------------------------------------------------
4653 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4654};
4655
4656
Jim Inghamd60d94a2011-03-11 03:53:59 +00004657#pragma mark CommandObjectTargetStopHookAdd
4658
4659//-------------------------------------------------------------------------
4660// CommandObjectTargetStopHookAdd
4661//-------------------------------------------------------------------------
4662
Jim Inghamda26bd22012-06-08 21:56:10 +00004663class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004664{
4665public:
4666
4667 class CommandOptions : public Options
4668 {
4669 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004670 CommandOptions (CommandInterpreter &interpreter) :
4671 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004672 m_line_start(0),
4673 m_line_end (UINT_MAX),
4674 m_func_name_type_mask (eFunctionNameTypeAuto),
4675 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004676 m_thread_specified (false),
4677 m_use_one_liner (false),
4678 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004679 {
4680 }
4681
4682 ~CommandOptions () {}
4683
Greg Claytonb3448432011-03-24 21:19:54 +00004684 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004685 GetDefinitions ()
4686 {
4687 return g_option_table;
4688 }
4689
4690 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004691 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004692 {
4693 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00004694 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004695 bool success;
4696
4697 switch (short_option)
4698 {
4699 case 'c':
4700 m_class_name = option_arg;
4701 m_sym_ctx_specified = true;
4702 break;
4703
4704 case 'e':
4705 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4706 if (!success)
4707 {
Greg Clayton9c236732011-10-26 00:56:27 +00004708 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004709 break;
4710 }
4711 m_sym_ctx_specified = true;
4712 break;
4713
4714 case 'l':
4715 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4716 if (!success)
4717 {
Greg Clayton9c236732011-10-26 00:56:27 +00004718 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004719 break;
4720 }
4721 m_sym_ctx_specified = true;
4722 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004723
4724 case 'i':
4725 m_no_inlines = true;
4726 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004727
4728 case 'n':
4729 m_function_name = option_arg;
4730 m_func_name_type_mask |= eFunctionNameTypeAuto;
4731 m_sym_ctx_specified = true;
4732 break;
4733
4734 case 'f':
4735 m_file_name = option_arg;
4736 m_sym_ctx_specified = true;
4737 break;
4738 case 's':
4739 m_module_name = option_arg;
4740 m_sym_ctx_specified = true;
4741 break;
4742 case 't' :
4743 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004744 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004745 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004746 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004747 m_thread_specified = true;
4748 }
4749 break;
4750 case 'T':
4751 m_thread_name = option_arg;
4752 m_thread_specified = true;
4753 break;
4754 case 'q':
4755 m_queue_name = option_arg;
4756 m_thread_specified = true;
4757 break;
4758 case 'x':
4759 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004760 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004761 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004762 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004763 m_thread_specified = true;
4764 }
4765 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004766 case 'o':
4767 m_use_one_liner = true;
4768 m_one_liner = option_arg;
4769 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004770 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004771 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004772 break;
4773 }
4774 return error;
4775 }
4776
4777 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004778 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004779 {
4780 m_class_name.clear();
4781 m_function_name.clear();
4782 m_line_start = 0;
4783 m_line_end = UINT_MAX;
4784 m_file_name.clear();
4785 m_module_name.clear();
4786 m_func_name_type_mask = eFunctionNameTypeAuto;
4787 m_thread_id = LLDB_INVALID_THREAD_ID;
4788 m_thread_index = UINT32_MAX;
4789 m_thread_name.clear();
4790 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004791
4792 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004793 m_sym_ctx_specified = false;
4794 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004795
4796 m_use_one_liner = false;
4797 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004798 }
4799
4800
Greg Claytonb3448432011-03-24 21:19:54 +00004801 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004802
4803 std::string m_class_name;
4804 std::string m_function_name;
4805 uint32_t m_line_start;
4806 uint32_t m_line_end;
4807 std::string m_file_name;
4808 std::string m_module_name;
4809 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4810 lldb::tid_t m_thread_id;
4811 uint32_t m_thread_index;
4812 std::string m_thread_name;
4813 std::string m_queue_name;
4814 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004815 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004816 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004817 // Instance variables to hold the values for one_liner options.
4818 bool m_use_one_liner;
4819 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004820 };
4821
4822 Options *
4823 GetOptions ()
4824 {
4825 return &m_options;
4826 }
4827
4828 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004829 CommandObjectParsed (interpreter,
4830 "target stop-hook add ",
4831 "Add a hook to be executed when the target stops.",
4832 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004833 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004834 {
4835 }
4836
4837 ~CommandObjectTargetStopHookAdd ()
4838 {
4839 }
4840
4841 static size_t
4842 ReadCommandsCallbackFunction (void *baton,
4843 InputReader &reader,
4844 lldb::InputReaderAction notification,
4845 const char *bytes,
4846 size_t bytes_len)
4847 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004848 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004849 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004850 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004851 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004852
4853 switch (notification)
4854 {
4855 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004856 if (!batch_mode)
4857 {
4858 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4859 if (reader.GetPrompt())
4860 out_stream->Printf ("%s", reader.GetPrompt());
4861 out_stream->Flush();
4862 }
Jim Inghame15511a2011-05-05 01:03:36 +00004863 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004864 break;
4865
4866 case eInputReaderDeactivate:
4867 break;
4868
4869 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004870 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004871 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004872 out_stream->Printf ("%s", reader.GetPrompt());
4873 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004874 }
Jim Inghame15511a2011-05-05 01:03:36 +00004875 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004876 break;
4877
Caroline Tice4a348082011-05-02 20:41:46 +00004878 case eInputReaderAsynchronousOutputWritten:
4879 break;
4880
Jim Inghamd60d94a2011-03-11 03:53:59 +00004881 case eInputReaderGotToken:
4882 if (bytes && bytes_len && baton)
4883 {
4884 StringList *commands = new_stop_hook->GetCommandPointer();
4885 if (commands)
4886 {
4887 commands->AppendString (bytes, bytes_len);
4888 }
4889 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004890 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004891 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004892 out_stream->Printf ("%s", reader.GetPrompt());
4893 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004894 }
4895 break;
4896
4897 case eInputReaderInterrupt:
4898 {
4899 // Finish, and cancel the stop hook.
4900 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004901 if (!batch_mode)
4902 {
4903 out_stream->Printf ("Stop hook cancelled.\n");
4904 out_stream->Flush();
4905 }
4906
Jim Inghamd60d94a2011-03-11 03:53:59 +00004907 reader.SetIsDone (true);
4908 }
Jim Inghame15511a2011-05-05 01:03:36 +00004909 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004910 break;
4911
4912 case eInputReaderEndOfFile:
4913 reader.SetIsDone (true);
4914 break;
4915
4916 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004917 if (!got_interrupted && !batch_mode)
4918 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004919 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004920 out_stream->Flush();
4921 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004922 break;
4923 }
4924
4925 return bytes_len;
4926 }
4927
Jim Inghamda26bd22012-06-08 21:56:10 +00004928protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004929 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004930 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004931 {
4932 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4933 if (target)
4934 {
4935 Target::StopHookSP new_hook_sp;
4936 target->AddStopHook (new_hook_sp);
4937
4938 // First step, make the specifier.
Greg Clayton102b2c22013-04-18 22:45:39 +00004939 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004940 if (m_options.m_sym_ctx_specified)
4941 {
4942 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4943
4944 if (!m_options.m_module_name.empty())
4945 {
4946 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4947 }
4948
4949 if (!m_options.m_class_name.empty())
4950 {
4951 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4952 }
4953
4954 if (!m_options.m_file_name.empty())
4955 {
4956 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4957 }
4958
4959 if (m_options.m_line_start != 0)
4960 {
4961 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4962 }
4963
4964 if (m_options.m_line_end != UINT_MAX)
4965 {
4966 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4967 }
4968
4969 if (!m_options.m_function_name.empty())
4970 {
4971 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4972 }
4973 }
4974
4975 if (specifier_ap.get())
4976 new_hook_sp->SetSpecifier (specifier_ap.release());
4977
4978 // Next see if any of the thread options have been entered:
4979
4980 if (m_options.m_thread_specified)
4981 {
4982 ThreadSpec *thread_spec = new ThreadSpec();
4983
4984 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4985 {
4986 thread_spec->SetTID (m_options.m_thread_id);
4987 }
4988
4989 if (m_options.m_thread_index != UINT32_MAX)
4990 thread_spec->SetIndex (m_options.m_thread_index);
4991
4992 if (!m_options.m_thread_name.empty())
4993 thread_spec->SetName (m_options.m_thread_name.c_str());
4994
4995 if (!m_options.m_queue_name.empty())
4996 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4997
4998 new_hook_sp->SetThreadSpecifier (thread_spec);
4999
5000 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00005001 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005002 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00005003 // Use one-liner.
5004 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00005005 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00005006 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00005007 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00005008 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00005009 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
5010 // the new stop hook's command string.
5011 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
5012 if (!reader_sp)
5013 {
5014 result.AppendError("out of memory\n");
5015 result.SetStatus (eReturnStatusFailed);
5016 target->RemoveStopHookByID (new_hook_sp->GetID());
5017 return false;
5018 }
5019
5020 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
5021 new_hook_sp.get(), // baton
5022 eInputReaderGranularityLine, // token size, to pass to callback function
5023 "DONE", // end token
5024 "> ", // prompt
5025 true)); // echo input
5026 if (!err.Success())
5027 {
5028 result.AppendError (err.AsCString());
5029 result.SetStatus (eReturnStatusFailed);
5030 target->RemoveStopHookByID (new_hook_sp->GetID());
5031 return false;
5032 }
5033 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005034 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00005035 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5036 }
5037 else
5038 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005039 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005040 result.SetStatus (eReturnStatusFailed);
5041 }
5042
5043 return result.Succeeded();
5044 }
5045private:
5046 CommandOptions m_options;
5047};
5048
Greg Claytonb3448432011-03-24 21:19:54 +00005049OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00005050CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5051{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005052 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00005053 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00005054 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
5055 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005056 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005057 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005058 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005059 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005060 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005061 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005062 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005063 "The stop hook is run only for threads in the queue whose name is given by this argument."},
5064 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
5065 "Specify the source file within which the stop-hook is to be run." },
5066 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
5067 "Set the start of the line range for which the stop-hook is to be run."},
5068 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
5069 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005070 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005071 "Specify the class within which the stop-hook is to be run." },
5072 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
5073 "Set the function name within which the stop hook will be run." },
5074 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5075};
5076
5077#pragma mark CommandObjectTargetStopHookDelete
5078
5079//-------------------------------------------------------------------------
5080// CommandObjectTargetStopHookDelete
5081//-------------------------------------------------------------------------
5082
Jim Inghamda26bd22012-06-08 21:56:10 +00005083class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005084{
5085public:
5086
5087 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005088 CommandObjectParsed (interpreter,
5089 "target stop-hook delete",
5090 "Delete a stop-hook.",
5091 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005092 {
5093 }
5094
5095 ~CommandObjectTargetStopHookDelete ()
5096 {
5097 }
5098
Jim Inghamda26bd22012-06-08 21:56:10 +00005099protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005100 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005101 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005102 {
5103 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5104 if (target)
5105 {
5106 // FIXME: see if we can use the breakpoint id style parser?
5107 size_t num_args = command.GetArgumentCount();
5108 if (num_args == 0)
5109 {
5110 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5111 {
5112 result.SetStatus (eReturnStatusFailed);
5113 return false;
5114 }
5115 else
5116 {
5117 target->RemoveAllStopHooks();
5118 }
5119 }
5120 else
5121 {
5122 bool success;
5123 for (size_t i = 0; i < num_args; i++)
5124 {
5125 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5126 if (!success)
5127 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005128 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005129 result.SetStatus(eReturnStatusFailed);
5130 return false;
5131 }
5132 success = target->RemoveStopHookByID (user_id);
5133 if (!success)
5134 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005135 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005136 result.SetStatus(eReturnStatusFailed);
5137 return false;
5138 }
5139 }
5140 }
5141 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5142 }
5143 else
5144 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005145 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005146 result.SetStatus (eReturnStatusFailed);
5147 }
5148
5149 return result.Succeeded();
5150 }
5151};
5152#pragma mark CommandObjectTargetStopHookEnableDisable
5153
5154//-------------------------------------------------------------------------
5155// CommandObjectTargetStopHookEnableDisable
5156//-------------------------------------------------------------------------
5157
Jim Inghamda26bd22012-06-08 21:56:10 +00005158class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005159{
5160public:
5161
5162 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005163 CommandObjectParsed (interpreter,
5164 name,
5165 help,
5166 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00005167 m_enable (enable)
5168 {
5169 }
5170
5171 ~CommandObjectTargetStopHookEnableDisable ()
5172 {
5173 }
5174
Jim Inghamda26bd22012-06-08 21:56:10 +00005175protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005176 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005177 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005178 {
5179 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5180 if (target)
5181 {
5182 // FIXME: see if we can use the breakpoint id style parser?
5183 size_t num_args = command.GetArgumentCount();
5184 bool success;
5185
5186 if (num_args == 0)
5187 {
5188 target->SetAllStopHooksActiveState (m_enable);
5189 }
5190 else
5191 {
5192 for (size_t i = 0; i < num_args; i++)
5193 {
5194 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5195 if (!success)
5196 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005197 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005198 result.SetStatus(eReturnStatusFailed);
5199 return false;
5200 }
5201 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5202 if (!success)
5203 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005204 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005205 result.SetStatus(eReturnStatusFailed);
5206 return false;
5207 }
5208 }
5209 }
5210 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5211 }
5212 else
5213 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005214 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005215 result.SetStatus (eReturnStatusFailed);
5216 }
5217 return result.Succeeded();
5218 }
5219private:
5220 bool m_enable;
5221};
5222
5223#pragma mark CommandObjectTargetStopHookList
5224
5225//-------------------------------------------------------------------------
5226// CommandObjectTargetStopHookList
5227//-------------------------------------------------------------------------
5228
Jim Inghamda26bd22012-06-08 21:56:10 +00005229class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005230{
5231public:
5232
5233 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005234 CommandObjectParsed (interpreter,
5235 "target stop-hook list",
5236 "List all stop-hooks.",
5237 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005238 {
5239 }
5240
5241 ~CommandObjectTargetStopHookList ()
5242 {
5243 }
5244
Jim Inghamda26bd22012-06-08 21:56:10 +00005245protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005246 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005247 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005248 {
5249 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005250 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005251 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005252 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005253 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005254 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005255 }
5256
5257 size_t num_hooks = target->GetNumStopHooks ();
5258 if (num_hooks == 0)
5259 {
5260 result.GetOutputStream().PutCString ("No stop hooks.\n");
5261 }
5262 else
5263 {
5264 for (size_t i = 0; i < num_hooks; i++)
5265 {
5266 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5267 if (i > 0)
5268 result.GetOutputStream().PutCString ("\n");
5269 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5270 }
5271 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005272 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005273 return result.Succeeded();
5274 }
5275};
5276
5277#pragma mark CommandObjectMultiwordTargetStopHooks
5278//-------------------------------------------------------------------------
5279// CommandObjectMultiwordTargetStopHooks
5280//-------------------------------------------------------------------------
5281
5282class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5283{
5284public:
5285
5286 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5287 CommandObjectMultiword (interpreter,
5288 "target stop-hook",
5289 "A set of commands for operating on debugger target stop-hooks.",
5290 "target stop-hook <subcommand> [<subcommand-options>]")
5291 {
5292 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5293 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5294 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5295 false,
5296 "target stop-hook disable [<id>]",
5297 "Disable a stop-hook.",
5298 "target stop-hook disable")));
5299 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5300 true,
5301 "target stop-hook enable [<id>]",
5302 "Enable a stop-hook.",
5303 "target stop-hook enable")));
5304 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5305 }
5306
5307 ~CommandObjectMultiwordTargetStopHooks()
5308 {
5309 }
5310};
5311
5312
Chris Lattner24943d22010-06-08 16:52:24 +00005313
5314#pragma mark CommandObjectMultiwordTarget
5315
5316//-------------------------------------------------------------------------
5317// CommandObjectMultiwordTarget
5318//-------------------------------------------------------------------------
5319
Greg Clayton63094e02010-06-23 01:19:29 +00005320CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005321 CommandObjectMultiword (interpreter,
5322 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005323 "A set of commands for operating on debugger targets.",
5324 "target <subcommand> [<subcommand-options>]")
5325{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005326
5327 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005328 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005329 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5330 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005331 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005332 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005333 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005334 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005335}
5336
5337CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5338{
5339}
5340
Greg Claytonabe0fed2011-04-18 08:33:37 +00005341