blob: 028bce569c580a674e974bd50f2fa44ea8174455 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectTarget.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectTarget.h"
13
14// C Includes
15#include <errno.h>
Greg Clayton81040f42011-02-01 01:13:32 +000016
Chris Lattner24943d22010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
19// Project includes
Jim Ingham84cdc152010-06-15 19:49:27 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/Debugger.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000022#include "lldb/Core/InputReader.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000025#include "lldb/Core/Section.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000026#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton801417e2011-07-07 01:59:51 +000028#include "lldb/Core/ValueObjectVariable.h"
Greg Claytonb924eb62012-09-27 03:13:55 +000029#include "lldb/Host/Symbols.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Interpreter/CommandInterpreter.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000032#include "lldb/Interpreter/Options.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000033#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Clayton5beb99d2011-08-11 02:48:45 +000034#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000035#include "lldb/Interpreter/OptionGroupFile.h"
Greg Claytona42880a2011-10-25 06:44:01 +000036#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton368f8222011-07-07 04:38:25 +000037#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000038#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000039#include "lldb/Interpreter/OptionGroupUInt64.h"
40#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton801417e2011-07-07 01:59:51 +000041#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000042#include "lldb/Symbol/CompileUnit.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000043#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytone1f50b92011-05-03 22:09:39 +000044#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolFile.h"
47#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda5b0afcc2012-07-12 00:20:07 +000048#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton801417e2011-07-07 01:59:51 +000049#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000050#include "lldb/Target/Process.h"
51#include "lldb/Target/StackFrame.h"
52#include "lldb/Target/Thread.h"
Jim Inghamd60d94a2011-03-11 03:53:59 +000053#include "lldb/Target/ThreadSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000054
55using namespace lldb;
56using namespace lldb_private;
57
Greg Claytonabe0fed2011-04-18 08:33:37 +000058
59
60static void
61DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
62{
Greg Clayton52c8b6e2011-04-19 04:19:37 +000063 const ArchSpec &target_arch = target->GetArchitecture();
Greg Claytonabe0fed2011-04-18 08:33:37 +000064
Greg Clayton5beb99d2011-08-11 02:48:45 +000065 Module *exe_module = target->GetExecutableModulePointer();
Greg Claytonabe0fed2011-04-18 08:33:37 +000066 char exe_path[PATH_MAX];
67 bool exe_valid = false;
Greg Clayton5beb99d2011-08-11 02:48:45 +000068 if (exe_module)
69 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Greg Claytonabe0fed2011-04-18 08:33:37 +000070
71 if (!exe_valid)
72 ::strcpy (exe_path, "<none>");
73
74 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
75
76 uint32_t properties = 0;
77 if (target_arch.IsValid())
78 {
79 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
80 properties++;
81 }
82 PlatformSP platform_sp (target->GetPlatform());
83 if (platform_sp)
84 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName());
85
86 ProcessSP process_sp (target->GetProcessSP());
87 bool show_process_status = false;
88 if (process_sp)
89 {
90 lldb::pid_t pid = process_sp->GetID();
91 StateType state = process_sp->GetState();
92 if (show_stopped_process_status)
Greg Clayton20206082011-11-17 01:23:07 +000093 show_process_status = StateIsStoppedState(state, true);
Greg Claytonabe0fed2011-04-18 08:33:37 +000094 const char *state_cstr = StateAsCString (state);
95 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malea5f35a4b2012-11-29 21:49:15 +000096 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Claytonabe0fed2011-04-18 08:33:37 +000097 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
98 }
99 if (properties > 0)
100 strm.PutCString (" )\n");
101 else
102 strm.EOL();
103 if (show_process_status)
104 {
105 const bool only_threads_with_stop_reason = true;
106 const uint32_t start_frame = 0;
107 const uint32_t num_frames = 1;
108 const uint32_t num_frames_with_source = 1;
109 process_sp->GetStatus (strm);
110 process_sp->GetThreadStatus (strm,
111 only_threads_with_stop_reason,
112 start_frame,
113 num_frames,
114 num_frames_with_source);
115
116 }
117}
118
119static uint32_t
120DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
121{
122 const uint32_t num_targets = target_list.GetNumTargets();
123 if (num_targets)
124 {
125 TargetSP selected_target_sp (target_list.GetSelectedTarget());
126 strm.PutCString ("Current targets:\n");
127 for (uint32_t i=0; i<num_targets; ++i)
128 {
129 TargetSP target_sp (target_list.GetTargetAtIndex (i));
130 if (target_sp)
131 {
132 bool is_selected = target_sp.get() == selected_target_sp.get();
133 DumpTargetInfo (i,
134 target_sp.get(),
135 is_selected ? "* " : " ",
136 show_stopped_process_status,
137 strm);
138 }
139 }
140 }
141 return num_targets;
142}
143#pragma mark CommandObjectTargetCreate
144
145//-------------------------------------------------------------------------
146// "target create"
147//-------------------------------------------------------------------------
148
Jim Inghamda26bd22012-06-08 21:56:10 +0000149class CommandObjectTargetCreate : public CommandObjectParsed
Greg Claytonabe0fed2011-04-18 08:33:37 +0000150{
151public:
152 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000153 CommandObjectParsed (interpreter,
154 "target create",
155 "Create a target using the argument as the main executable.",
156 NULL),
Greg Claytonabe0fed2011-04-18 08:33:37 +0000157 m_option_group (interpreter),
Greg Clayton801417e2011-07-07 01:59:51 +0000158 m_arch_option (),
Greg Clayton46c9a352012-02-09 06:16:32 +0000159 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Greg Claytonec9c2d22012-11-30 19:05:35 +0000160 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
161 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
162 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000163 {
164 CommandArgumentEntry arg;
165 CommandArgumentData file_arg;
166
167 // Define the first (and only) variant of this arg.
168 file_arg.arg_type = eArgTypeFilename;
169 file_arg.arg_repetition = eArgRepeatPlain;
170
171 // There is only one variant this argument could be; put it into the argument entry.
172 arg.push_back (file_arg);
173
174 // Push the data for the first argument into the m_arguments vector.
175 m_arguments.push_back (arg);
176
Greg Clayton801417e2011-07-07 01:59:51 +0000177 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000178 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton46c9a352012-02-09 06:16:32 +0000179 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +0000180 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
181 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000182 m_option_group.Finalize();
183 }
184
185 ~CommandObjectTargetCreate ()
186 {
187 }
188
189 Options *
190 GetOptions ()
191 {
192 return &m_option_group;
193 }
194
Jim Inghamda26bd22012-06-08 21:56:10 +0000195 int
196 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 {
223 const int 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,
603 0),
Greg Clayton801417e2011-07-07 01:59:51 +0000604 m_option_group (interpreter),
Greg Clayton368f8222011-07-07 04:38:25 +0000605 m_option_variable (false), // Don't include frame options
Greg Claytona42880a2011-10-25 06:44:01 +0000606 m_option_format (eFormatDefault),
Greg Clayton6475c422012-12-04 00:32:51 +0000607 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'file', 0, eArgTypeFilename, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
608 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'shlb', 0, eArgTypeFilename, "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
Greg Clayton801417e2011-07-07 01:59:51 +0000609 m_varobj_options()
610 {
Johnny Chen24b81e32011-08-22 22:22:00 +0000611 CommandArgumentEntry arg;
612 CommandArgumentData var_name_arg;
613
614 // Define the first (and only) variant of this arg.
615 var_name_arg.arg_type = eArgTypeVarName;
616 var_name_arg.arg_repetition = eArgRepeatPlus;
617
618 // There is only one variant this argument could be; put it into the argument entry.
619 arg.push_back (var_name_arg);
620
621 // Push the data for the first argument into the m_arguments vector.
622 m_arguments.push_back (arg);
623
Greg Clayton801417e2011-07-07 01:59:51 +0000624 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton368f8222011-07-07 04:38:25 +0000625 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton24a6bd92011-10-27 17:55:14 +0000626 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton801417e2011-07-07 01:59:51 +0000627 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
628 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
629 m_option_group.Finalize();
630 }
631
632 virtual
633 ~CommandObjectTargetVariable ()
634 {
635 }
Greg Clayton5d81f492011-07-08 21:46:14 +0000636
637 void
638 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
639 {
Enrico Granata19030d82011-08-15 18:01:31 +0000640 ValueObject::DumpValueObjectOptions options;
641
Enrico Granata3069c622012-03-01 04:24:26 +0000642 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
Enrico Granata19030d82011-08-15 18:01:31 +0000643 .SetMaximumDepth(m_varobj_options.max_depth)
644 .SetShowTypes(m_varobj_options.show_types)
645 .SetShowLocation(m_varobj_options.show_location)
646 .SetUseObjectiveC(m_varobj_options.use_objc)
647 .SetUseDynamicType(m_varobj_options.use_dynamic)
Enrico Granatacf09f882012-03-19 22:58:49 +0000648 .SetUseSyntheticValue(m_varobj_options.use_synth)
Enrico Granata19030d82011-08-15 18:01:31 +0000649 .SetFlatOutput(m_varobj_options.flat_output)
650 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
651 .SetIgnoreCap(m_varobj_options.ignore_cap);
652
Greg Clayton5d81f492011-07-08 21:46:14 +0000653 switch (var_sp->GetScope())
654 {
655 case eValueTypeVariableGlobal:
656 if (m_option_variable.show_scope)
657 s.PutCString("GLOBAL: ");
658 break;
659
660 case eValueTypeVariableStatic:
661 if (m_option_variable.show_scope)
662 s.PutCString("STATIC: ");
663 break;
664
665 case eValueTypeVariableArgument:
666 if (m_option_variable.show_scope)
667 s.PutCString(" ARG: ");
668 break;
669
670 case eValueTypeVariableLocal:
671 if (m_option_variable.show_scope)
672 s.PutCString(" LOCAL: ");
673 break;
674
675 default:
676 break;
677 }
678
Greg Claytonfb816422011-07-10 19:21:23 +0000679 if (m_option_variable.show_decl)
Greg Clayton5d81f492011-07-08 21:46:14 +0000680 {
Greg Claytonfb816422011-07-10 19:21:23 +0000681 bool show_fullpaths = false;
682 bool show_module = true;
683 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
684 s.PutCString (": ");
Greg Clayton5d81f492011-07-08 21:46:14 +0000685 }
686
Greg Claytona42880a2011-10-25 06:44:01 +0000687 const Format format = m_option_format.GetFormat();
Greg Clayton5d81f492011-07-08 21:46:14 +0000688 if (format != eFormatDefault)
Enrico Granata3069c622012-03-01 04:24:26 +0000689 options.SetFormat(format);
690
691 options.SetRootValueObjectName(root_name);
Greg Clayton5d81f492011-07-08 21:46:14 +0000692
693 ValueObject::DumpValueObject (s,
694 valobj_sp.get(),
Enrico Granata3069c622012-03-01 04:24:26 +0000695 options);
Greg Clayton5d81f492011-07-08 21:46:14 +0000696
697 }
Greg Clayton801417e2011-07-07 01:59:51 +0000698
Greg Clayton5d81f492011-07-08 21:46:14 +0000699
700 static uint32_t GetVariableCallback (void *baton,
701 const char *name,
702 VariableList &variable_list)
703 {
704 Target *target = static_cast<Target *>(baton);
705 if (target)
706 {
707 return target->GetImages().FindGlobalVariables (ConstString(name),
708 true,
709 UINT32_MAX,
710 variable_list);
711 }
712 return 0;
713 }
714
715
716
Jim Inghamda26bd22012-06-08 21:56:10 +0000717 Options *
718 GetOptions ()
719 {
720 return &m_option_group;
721 }
722
723protected:
Greg Clayton6475c422012-12-04 00:32:51 +0000724
725 void
726 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
727 {
728 size_t count = variable_list.GetSize();
729 if (count > 0)
730 {
731 if (sc.module_sp)
732 {
733 if (sc.comp_unit)
734 {
735 s.Printf ("Global variables for %s/%s in %s/%s:\n",
736 sc.comp_unit->GetDirectory().GetCString(),
737 sc.comp_unit->GetFilename().GetCString(),
738 sc.module_sp->GetFileSpec().GetDirectory().GetCString(),
739 sc.module_sp->GetFileSpec().GetFilename().GetCString());
740 }
741 else
742 {
743 s.Printf ("Global variables for %s/%s\n",
744 sc.module_sp->GetFileSpec().GetDirectory().GetCString(),
745 sc.module_sp->GetFileSpec().GetFilename().GetCString());
746 }
747 }
748 else if (sc.comp_unit)
749 {
750 s.Printf ("Global variables for %s/%s\n",
751 sc.comp_unit->GetDirectory().GetCString(),
752 sc.comp_unit->GetFilename().GetCString());
753 }
754
755 for (uint32_t i=0; i<count; ++i)
756 {
757 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
758 if (var_sp)
759 {
760 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
761
762 if (valobj_sp)
763 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
764 }
765 }
766 }
767
768 }
Greg Clayton801417e2011-07-07 01:59:51 +0000769 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000770 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton801417e2011-07-07 01:59:51 +0000771 {
772 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Greg Clayton567e7f32011-09-22 04:58:26 +0000773 Target *target = exe_ctx.GetTargetPtr();
774 if (target)
Greg Clayton801417e2011-07-07 01:59:51 +0000775 {
776 const size_t argc = args.GetArgumentCount();
Greg Claytonfac93882011-10-05 22:17:32 +0000777 Stream &s = result.GetOutputStream();
Greg Clayton6475c422012-12-04 00:32:51 +0000778
Greg Clayton801417e2011-07-07 01:59:51 +0000779 if (argc > 0)
780 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000781
Greg Clayton801417e2011-07-07 01:59:51 +0000782 for (size_t idx = 0; idx < argc; ++idx)
783 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000784 VariableList variable_list;
785 ValueObjectList valobj_list;
786
Greg Clayton368f8222011-07-07 04:38:25 +0000787 const char *arg = args.GetArgumentAtIndex(idx);
788 uint32_t matches = 0;
Greg Claytonfb816422011-07-10 19:21:23 +0000789 bool use_var_name = false;
Greg Clayton368f8222011-07-07 04:38:25 +0000790 if (m_option_variable.use_regex)
Greg Clayton801417e2011-07-07 01:59:51 +0000791 {
Greg Clayton368f8222011-07-07 04:38:25 +0000792 RegularExpression regex(arg);
793 if (!regex.IsValid ())
794 {
795 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
796 result.SetStatus (eReturnStatusFailed);
797 return false;
798 }
Greg Claytonfb816422011-07-10 19:21:23 +0000799 use_var_name = true;
Greg Clayton567e7f32011-09-22 04:58:26 +0000800 matches = target->GetImages().FindGlobalVariables (regex,
801 true,
802 UINT32_MAX,
803 variable_list);
Greg Clayton801417e2011-07-07 01:59:51 +0000804 }
805 else
806 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000807 Error error (Variable::GetValuesForVariableExpressionPath (arg,
Greg Clayton24b03102011-07-09 20:12:33 +0000808 exe_ctx.GetBestExecutionContextScope(),
Greg Clayton5d81f492011-07-08 21:46:14 +0000809 GetVariableCallback,
Greg Clayton567e7f32011-09-22 04:58:26 +0000810 target,
Greg Clayton5d81f492011-07-08 21:46:14 +0000811 variable_list,
812 valobj_list));
Greg Clayton5d81f492011-07-08 21:46:14 +0000813 matches = variable_list.GetSize();
Greg Clayton368f8222011-07-07 04:38:25 +0000814 }
815
816 if (matches == 0)
817 {
818 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
819 result.SetStatus (eReturnStatusFailed);
820 return false;
821 }
822 else
823 {
Greg Clayton801417e2011-07-07 01:59:51 +0000824 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
825 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000826 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
Greg Clayton801417e2011-07-07 01:59:51 +0000827 if (var_sp)
828 {
Greg Clayton5d81f492011-07-08 21:46:14 +0000829 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
830 if (!valobj_sp)
Greg Claytonfb816422011-07-10 19:21:23 +0000831 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp);
Greg Clayton801417e2011-07-07 01:59:51 +0000832
833 if (valobj_sp)
Greg Claytonb304a742011-10-13 18:31:02 +0000834 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton801417e2011-07-07 01:59:51 +0000835 }
836 }
837 }
838 }
839 }
840 else
841 {
Greg Clayton6475c422012-12-04 00:32:51 +0000842 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
843 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
844 SymbolContextList sc_list;
845 const size_t num_compile_units = compile_units.GetSize();
846 const size_t num_shlibs = shlibs.GetSize();
847 if (num_compile_units == 0 && num_shlibs == 0)
Greg Claytonfac93882011-10-05 22:17:32 +0000848 {
Greg Clayton6475c422012-12-04 00:32:51 +0000849 bool success = false;
850 StackFrame *frame = exe_ctx.GetFramePtr();
851 CompileUnit *comp_unit = NULL;
852 if (frame)
Greg Claytonfac93882011-10-05 22:17:32 +0000853 {
Greg Clayton6475c422012-12-04 00:32:51 +0000854 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
855 if (sc.comp_unit)
Greg Claytonfac93882011-10-05 22:17:32 +0000856 {
Greg Clayton6475c422012-12-04 00:32:51 +0000857 const bool can_create = true;
858 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
859 if (comp_unit_varlist_sp)
Greg Claytonfac93882011-10-05 22:17:32 +0000860 {
Greg Clayton6475c422012-12-04 00:32:51 +0000861 size_t count = comp_unit_varlist_sp->GetSize();
862 if (count > 0)
Greg Claytonfac93882011-10-05 22:17:32 +0000863 {
Greg Clayton6475c422012-12-04 00:32:51 +0000864 DumpGlobalVariableList(exe_ctx, sc, *comp_unit_varlist_sp, s);
865 success = true;
Greg Claytonfac93882011-10-05 22:17:32 +0000866 }
867 }
868 }
869 }
Greg Clayton6475c422012-12-04 00:32:51 +0000870 if (!success)
Greg Claytonfac93882011-10-05 22:17:32 +0000871 {
Greg Clayton6475c422012-12-04 00:32:51 +0000872 if (frame)
873 {
874 if (comp_unit)
875 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n",
876 comp_unit->GetDirectory().GetCString(),
877 comp_unit->GetFilename().GetCString());
878 else
879 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex());
880 }
Greg Claytonfac93882011-10-05 22:17:32 +0000881 else
Greg Clayton6475c422012-12-04 00:32:51 +0000882 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
883 result.SetStatus (eReturnStatusFailed);
884 }
885 }
886 else
887 {
888 SymbolContextList sc_list;
889 const bool append = true;
890 // We have one or more compile unit or shlib
891 if (num_shlibs > 0)
892 {
893 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
894 {
895 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
896 ModuleSpec module_spec (module_file);
897
898 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
899 if (module_sp)
900 {
901 if (num_compile_units > 0)
902 {
903 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
904 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
905 }
906 else
907 {
908 SymbolContext sc;
909 sc.module_sp = module_sp;
910 sc_list.Append(sc);
911 }
912 }
913 else
914 {
915 // Didn't find matching shlib/module in target...
916 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s%s%s\n",
917 module_file.GetDirectory().GetCString(),
918 module_file.GetDirectory() ? "/" : "",
919 module_file.GetFilename().GetCString());
920 }
921 }
922 }
Greg Claytonfac93882011-10-05 22:17:32 +0000923 else
Greg Clayton6475c422012-12-04 00:32:51 +0000924 {
925 // No shared libraries, we just want to find globals for the compile units files that were specified
926 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
927 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
928 }
929
930 const uint32_t num_scs = sc_list.GetSize();
931 if (num_scs > 0)
932 {
933 SymbolContext sc;
934 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
935 {
936 if (sc_list.GetContextAtIndex(sc_idx, sc))
937 {
938 if (sc.comp_unit)
939 {
940 const bool can_create = true;
941 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
942 if (comp_unit_varlist_sp)
943 DumpGlobalVariableList(exe_ctx, sc, *comp_unit_varlist_sp, s);
944 }
945 else if (sc.module_sp)
946 {
947 // Get all global variables for this module
948 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
949 VariableList variable_list;
950 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
951 DumpGlobalVariableList(exe_ctx, sc, variable_list, s);
952 }
953 }
954 }
955 }
Greg Claytonfac93882011-10-05 22:17:32 +0000956 }
Greg Clayton801417e2011-07-07 01:59:51 +0000957 }
958 }
959 else
960 {
961 result.AppendError ("invalid target, create a debug target using the 'target create' command");
962 result.SetStatus (eReturnStatusFailed);
963 return false;
964 }
Enrico Granatadb64d952011-08-12 16:42:31 +0000965
966 if (m_interpreter.TruncationWarningNecessary())
967 {
968 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
969 m_cmd_name.c_str());
970 m_interpreter.TruncationWarningGiven();
971 }
972
Greg Clayton801417e2011-07-07 01:59:51 +0000973 return result.Succeeded();
974 }
975
Greg Clayton801417e2011-07-07 01:59:51 +0000976 OptionGroupOptions m_option_group;
Greg Clayton368f8222011-07-07 04:38:25 +0000977 OptionGroupVariable m_option_variable;
Greg Claytona42880a2011-10-25 06:44:01 +0000978 OptionGroupFormat m_option_format;
Greg Clayton801417e2011-07-07 01:59:51 +0000979 OptionGroupFileList m_option_compile_units;
980 OptionGroupFileList m_option_shared_libraries;
981 OptionGroupValueObjectDisplay m_varobj_options;
982
983};
984
985
Greg Claytone1f50b92011-05-03 22:09:39 +0000986#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner24943d22010-06-08 16:52:24 +0000987
Jim Inghamda26bd22012-06-08 21:56:10 +0000988class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +0000989{
990public:
991
Greg Claytone1f50b92011-05-03 22:09:39 +0000992 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000993 CommandObjectParsed (interpreter,
994 "target modules search-paths add",
995 "Add new image search paths substitution pairs to the current target.",
996 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000997 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000998 CommandArgumentEntry arg;
999 CommandArgumentData old_prefix_arg;
1000 CommandArgumentData new_prefix_arg;
1001
1002 // Define the first variant of this arg pair.
1003 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1004 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1005
1006 // Define the first variant of this arg pair.
1007 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1008 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1009
1010 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1011 // must always occur together, they are treated as two variants of one argument rather than two independent
1012 // arguments. Push them both into the first argument position for m_arguments...
1013
1014 arg.push_back (old_prefix_arg);
1015 arg.push_back (new_prefix_arg);
1016
1017 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001018 }
1019
Greg Claytone1f50b92011-05-03 22:09:39 +00001020 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner24943d22010-06-08 16:52:24 +00001021 {
1022 }
1023
Jim Inghamda26bd22012-06-08 21:56:10 +00001024protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001025 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001026 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001027 CommandReturnObject &result)
1028 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001029 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001030 if (target)
1031 {
1032 uint32_t argc = command.GetArgumentCount();
1033 if (argc & 1)
1034 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001035 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001036 result.SetStatus (eReturnStatusFailed);
1037 }
1038 else
1039 {
1040 for (uint32_t i=0; i<argc; i+=2)
1041 {
1042 const char *from = command.GetArgumentAtIndex(i);
1043 const char *to = command.GetArgumentAtIndex(i+1);
1044
1045 if (from[0] && to[0])
1046 {
1047 bool last_pair = ((argc - i) == 2);
Greg Clayton63094e02010-06-23 01:19:29 +00001048 target->GetImageSearchPathList().Append (ConstString(from),
1049 ConstString(to),
1050 last_pair); // Notify if this is the last pair
Johnny Chen4d661352011-02-03 00:30:19 +00001051 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001052 }
1053 else
1054 {
1055 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001056 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001057 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001058 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001059 result.SetStatus (eReturnStatusFailed);
1060 }
1061 }
1062 }
1063 }
1064 else
1065 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001066 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001067 result.SetStatus (eReturnStatusFailed);
1068 }
1069 return result.Succeeded();
1070 }
1071};
1072
Greg Claytone1f50b92011-05-03 22:09:39 +00001073#pragma mark CommandObjectTargetModulesSearchPathsClear
1074
Jim Inghamda26bd22012-06-08 21:56:10 +00001075class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001076{
1077public:
1078
Greg Claytone1f50b92011-05-03 22:09:39 +00001079 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001080 CommandObjectParsed (interpreter,
1081 "target modules search-paths clear",
1082 "Clear all current image search path substitution pairs from the current target.",
1083 "target modules search-paths clear")
Chris Lattner24943d22010-06-08 16:52:24 +00001084 {
1085 }
1086
Greg Claytone1f50b92011-05-03 22:09:39 +00001087 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner24943d22010-06-08 16:52:24 +00001088 {
1089 }
1090
Jim Inghamda26bd22012-06-08 21:56:10 +00001091protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001092 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001093 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001094 CommandReturnObject &result)
1095 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001096 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001097 if (target)
1098 {
1099 bool notify = true;
1100 target->GetImageSearchPathList().Clear(notify);
Johnny Chen4d661352011-02-03 00:30:19 +00001101 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001102 }
1103 else
1104 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001105 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001106 result.SetStatus (eReturnStatusFailed);
1107 }
1108 return result.Succeeded();
1109 }
1110};
1111
Greg Claytone1f50b92011-05-03 22:09:39 +00001112#pragma mark CommandObjectTargetModulesSearchPathsInsert
1113
Jim Inghamda26bd22012-06-08 21:56:10 +00001114class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001115{
1116public:
1117
Greg Claytone1f50b92011-05-03 22:09:39 +00001118 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001119 CommandObjectParsed (interpreter,
1120 "target modules search-paths insert",
1121 "Insert a new image search path substitution pair into the current target at the specified index.",
1122 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001123 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001124 CommandArgumentEntry arg1;
1125 CommandArgumentEntry arg2;
1126 CommandArgumentData index_arg;
1127 CommandArgumentData old_prefix_arg;
1128 CommandArgumentData new_prefix_arg;
1129
1130 // Define the first and only variant of this arg.
1131 index_arg.arg_type = eArgTypeIndex;
1132 index_arg.arg_repetition = eArgRepeatPlain;
1133
1134 // Put the one and only variant into the first arg for m_arguments:
1135 arg1.push_back (index_arg);
1136
1137 // Define the first variant of this arg pair.
1138 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1139 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1140
1141 // Define the first variant of this arg pair.
1142 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1143 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1144
1145 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1146 // must always occur together, they are treated as two variants of one argument rather than two independent
1147 // arguments. Push them both into the same argument position for m_arguments...
1148
1149 arg2.push_back (old_prefix_arg);
1150 arg2.push_back (new_prefix_arg);
1151
1152 // Add arguments to m_arguments.
1153 m_arguments.push_back (arg1);
1154 m_arguments.push_back (arg2);
Chris Lattner24943d22010-06-08 16:52:24 +00001155 }
1156
Greg Claytone1f50b92011-05-03 22:09:39 +00001157 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner24943d22010-06-08 16:52:24 +00001158 {
1159 }
1160
Jim Inghamda26bd22012-06-08 21:56:10 +00001161protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001162 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001163 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001164 CommandReturnObject &result)
1165 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001166 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001167 if (target)
1168 {
1169 uint32_t argc = command.GetArgumentCount();
1170 // check for at least 3 arguments and an odd nubmer of parameters
1171 if (argc >= 3 && argc & 1)
1172 {
1173 bool success = false;
1174
1175 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1176
1177 if (!success)
1178 {
1179 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1180 result.SetStatus (eReturnStatusFailed);
1181 return result.Succeeded();
1182 }
1183
1184 // shift off the index
1185 command.Shift();
1186 argc = command.GetArgumentCount();
1187
1188 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1189 {
1190 const char *from = command.GetArgumentAtIndex(i);
1191 const char *to = command.GetArgumentAtIndex(i+1);
1192
1193 if (from[0] && to[0])
1194 {
1195 bool last_pair = ((argc - i) == 2);
1196 target->GetImageSearchPathList().Insert (ConstString(from),
1197 ConstString(to),
1198 insert_idx,
1199 last_pair);
Johnny Chen4d661352011-02-03 00:30:19 +00001200 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001201 }
1202 else
1203 {
1204 if (from[0])
Greg Claytonabe0fed2011-04-18 08:33:37 +00001205 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001206 else
Greg Claytonabe0fed2011-04-18 08:33:37 +00001207 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001208 result.SetStatus (eReturnStatusFailed);
1209 return false;
1210 }
1211 }
1212 }
1213 else
1214 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001215 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001216 result.SetStatus (eReturnStatusFailed);
1217 return result.Succeeded();
1218 }
1219
1220 }
1221 else
1222 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001223 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001224 result.SetStatus (eReturnStatusFailed);
1225 }
1226 return result.Succeeded();
1227 }
1228};
1229
Greg Claytone1f50b92011-05-03 22:09:39 +00001230
1231#pragma mark CommandObjectTargetModulesSearchPathsList
1232
1233
Jim Inghamda26bd22012-06-08 21:56:10 +00001234class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001235{
1236public:
1237
Greg Claytone1f50b92011-05-03 22:09:39 +00001238 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001239 CommandObjectParsed (interpreter,
1240 "target modules search-paths list",
1241 "List all current image search path substitution pairs in the current target.",
1242 "target modules search-paths list")
Chris Lattner24943d22010-06-08 16:52:24 +00001243 {
1244 }
1245
Greg Claytone1f50b92011-05-03 22:09:39 +00001246 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner24943d22010-06-08 16:52:24 +00001247 {
1248 }
1249
Jim Inghamda26bd22012-06-08 21:56:10 +00001250protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001251 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001252 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001253 CommandReturnObject &result)
1254 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001255 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001256 if (target)
1257 {
1258 if (command.GetArgumentCount() != 0)
1259 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001260 result.AppendError ("list takes no arguments\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001261 result.SetStatus (eReturnStatusFailed);
1262 return result.Succeeded();
1263 }
1264
1265 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen4d661352011-02-03 00:30:19 +00001266 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001267 }
1268 else
1269 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001270 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001271 result.SetStatus (eReturnStatusFailed);
1272 }
1273 return result.Succeeded();
1274 }
1275};
1276
Greg Claytone1f50b92011-05-03 22:09:39 +00001277#pragma mark CommandObjectTargetModulesSearchPathsQuery
1278
Jim Inghamda26bd22012-06-08 21:56:10 +00001279class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner24943d22010-06-08 16:52:24 +00001280{
1281public:
1282
Greg Claytone1f50b92011-05-03 22:09:39 +00001283 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001284 CommandObjectParsed (interpreter,
1285 "target modules search-paths query",
1286 "Transform a path using the first applicable image search path.",
1287 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001288 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001289 CommandArgumentEntry arg;
1290 CommandArgumentData path_arg;
1291
1292 // Define the first (and only) variant of this arg.
Sean Callanan9a91ef62012-10-24 01:12:14 +00001293 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice43b014a2010-10-04 22:28:36 +00001294 path_arg.arg_repetition = eArgRepeatPlain;
1295
1296 // There is only one variant this argument could be; put it into the argument entry.
1297 arg.push_back (path_arg);
1298
1299 // Push the data for the first argument into the m_arguments vector.
1300 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001301 }
1302
Greg Claytone1f50b92011-05-03 22:09:39 +00001303 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner24943d22010-06-08 16:52:24 +00001304 {
1305 }
1306
Jim Inghamda26bd22012-06-08 21:56:10 +00001307protected:
Chris Lattner24943d22010-06-08 16:52:24 +00001308 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00001309 DoExecute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001310 CommandReturnObject &result)
1311 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001312 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001313 if (target)
1314 {
1315 if (command.GetArgumentCount() != 1)
1316 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001317 result.AppendError ("query requires one argument\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001318 result.SetStatus (eReturnStatusFailed);
1319 return result.Succeeded();
1320 }
1321
1322 ConstString orig(command.GetArgumentAtIndex(0));
1323 ConstString transformed;
1324 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1325 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1326 else
1327 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen4d661352011-02-03 00:30:19 +00001328
1329 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +00001330 }
1331 else
1332 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00001333 result.AppendError ("invalid target\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001334 result.SetStatus (eReturnStatusFailed);
1335 }
1336 return result.Succeeded();
1337 }
1338};
1339
Greg Claytone1f50b92011-05-03 22:09:39 +00001340//----------------------------------------------------------------------
1341// Static Helper functions
1342//----------------------------------------------------------------------
1343static void
1344DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1345{
1346 if (module)
1347 {
1348 const char *arch_cstr;
1349 if (full_triple)
1350 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1351 else
1352 arch_cstr = module->GetArchitecture().GetArchitectureName();
1353 if (width)
1354 strm.Printf("%-*s", width, arch_cstr);
1355 else
1356 strm.PutCString(arch_cstr);
1357 }
1358}
1359
1360static void
1361DumpModuleUUID (Stream &strm, Module *module)
1362{
Jim Ingham6f01c932012-10-12 17:34:26 +00001363 if (module && module->GetUUID().IsValid())
Greg Clayton153ccd72011-08-10 02:10:13 +00001364 module->GetUUID().Dump (&strm);
1365 else
1366 strm.PutCString(" ");
Greg Claytone1f50b92011-05-03 22:09:39 +00001367}
1368
1369static uint32_t
Greg Claytoned0a0fb2012-10-18 16:33:33 +00001370DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1371 Stream &strm,
1372 Module *module,
1373 const FileSpec &file_spec,
1374 bool load_addresses)
Greg Claytone1f50b92011-05-03 22:09:39 +00001375{
1376 uint32_t num_matches = 0;
1377 if (module)
1378 {
1379 SymbolContextList sc_list;
1380 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1381 0,
1382 false,
1383 eSymbolContextCompUnit,
1384 sc_list);
1385
1386 for (uint32_t i=0; i<num_matches; ++i)
1387 {
1388 SymbolContext sc;
1389 if (sc_list.GetContextAtIndex(i, sc))
1390 {
1391 if (i > 0)
1392 strm << "\n\n";
1393
1394 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1395 << module->GetFileSpec().GetFilename() << "\n";
1396 LineTable *line_table = sc.comp_unit->GetLineTable();
1397 if (line_table)
1398 line_table->GetDescription (&strm,
Greg Clayton567e7f32011-09-22 04:58:26 +00001399 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytone1f50b92011-05-03 22:09:39 +00001400 lldb::eDescriptionLevelBrief);
1401 else
1402 strm << "No line table";
1403 }
1404 }
1405 }
1406 return num_matches;
1407}
1408
1409static void
1410DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1411{
1412 if (file_spec_ptr)
1413 {
1414 if (width > 0)
1415 {
1416 char fullpath[PATH_MAX];
1417 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
1418 {
1419 strm.Printf("%-*s", width, fullpath);
1420 return;
1421 }
1422 }
1423 else
1424 {
1425 file_spec_ptr->Dump(&strm);
1426 return;
1427 }
1428 }
1429 // Keep the width spacing correct if things go wrong...
1430 if (width > 0)
1431 strm.Printf("%-*s", width, "");
1432}
1433
1434static void
1435DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1436{
1437 if (file_spec_ptr)
1438 {
1439 if (width > 0)
1440 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1441 else
1442 file_spec_ptr->GetDirectory().Dump(&strm);
1443 return;
1444 }
1445 // Keep the width spacing correct if things go wrong...
1446 if (width > 0)
1447 strm.Printf("%-*s", width, "");
1448}
1449
1450static void
1451DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1452{
1453 if (file_spec_ptr)
1454 {
1455 if (width > 0)
1456 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1457 else
1458 file_spec_ptr->GetFilename().Dump(&strm);
1459 return;
1460 }
1461 // Keep the width spacing correct if things go wrong...
1462 if (width > 0)
1463 strm.Printf("%-*s", width, "");
1464}
1465
1466
1467static void
1468DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1469{
1470 if (module)
1471 {
1472 ObjectFile *objfile = module->GetObjectFile ();
1473 if (objfile)
1474 {
1475 Symtab *symtab = objfile->GetSymtab();
1476 if (symtab)
Greg Clayton567e7f32011-09-22 04:58:26 +00001477 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00001478 }
1479 }
1480}
1481
1482static void
1483DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1484{
1485 if (module)
1486 {
1487 ObjectFile *objfile = module->GetObjectFile ();
1488 if (objfile)
1489 {
1490 SectionList *section_list = objfile->GetSectionList();
1491 if (section_list)
1492 {
1493 strm.PutCString ("Sections for '");
1494 strm << module->GetFileSpec();
1495 if (module->GetObjectName())
1496 strm << '(' << module->GetObjectName() << ')';
1497 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName());
1498 strm.IndentMore();
Greg Clayton567e7f32011-09-22 04:58:26 +00001499 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
Greg Claytone1f50b92011-05-03 22:09:39 +00001500 strm.IndentLess();
1501 }
1502 }
1503 }
1504}
1505
1506static bool
1507DumpModuleSymbolVendor (Stream &strm, Module *module)
1508{
1509 if (module)
1510 {
1511 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1512 if (symbol_vendor)
1513 {
1514 symbol_vendor->Dump(&strm);
1515 return true;
1516 }
1517 }
1518 return false;
1519}
1520
Greg Clayton2ad894b2012-05-15 18:43:44 +00001521static void
1522DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1523{
1524 strm.IndentMore();
1525 strm.Indent (" Address: ");
1526 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1527 strm.PutCString (" (");
1528 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1529 strm.PutCString (")\n");
1530 strm.Indent (" Summary: ");
1531 const uint32_t save_indent = strm.GetIndentLevel ();
1532 strm.SetIndentLevel (save_indent + 13);
1533 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1534 strm.SetIndentLevel (save_indent);
1535 // Print out detailed address information when verbose is enabled
1536 if (verbose)
1537 {
1538 strm.EOL();
1539 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1540 }
1541 strm.IndentLess();
1542}
1543
Greg Claytone1f50b92011-05-03 22:09:39 +00001544static bool
Greg Clayton3508c382012-02-24 01:59:29 +00001545LookupAddressInModule (CommandInterpreter &interpreter,
1546 Stream &strm,
1547 Module *module,
1548 uint32_t resolve_mask,
1549 lldb::addr_t raw_addr,
1550 lldb::addr_t offset,
1551 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001552{
1553 if (module)
1554 {
1555 lldb::addr_t addr = raw_addr - offset;
1556 Address so_addr;
1557 SymbolContext sc;
Greg Clayton567e7f32011-09-22 04:58:26 +00001558 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytone1f50b92011-05-03 22:09:39 +00001559 if (target && !target->GetSectionLoadList().IsEmpty())
1560 {
1561 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1562 return false;
Greg Clayton3508c382012-02-24 01:59:29 +00001563 else if (so_addr.GetModule().get() != module)
Greg Claytone1f50b92011-05-03 22:09:39 +00001564 return false;
1565 }
1566 else
1567 {
1568 if (!module->ResolveFileAddress (addr, so_addr))
1569 return false;
1570 }
1571
Greg Claytone1f50b92011-05-03 22:09:39 +00001572 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001573 DumpAddress (exe_scope, so_addr, verbose, strm);
1574// strm.IndentMore();
1575// strm.Indent (" Address: ");
1576// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1577// strm.PutCString (" (");
1578// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1579// strm.PutCString (")\n");
1580// strm.Indent (" Summary: ");
1581// const uint32_t save_indent = strm.GetIndentLevel ();
1582// strm.SetIndentLevel (save_indent + 13);
1583// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1584// strm.SetIndentLevel (save_indent);
1585// // Print out detailed address information when verbose is enabled
1586// if (verbose)
1587// {
1588// strm.EOL();
1589// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1590// }
1591// strm.IndentLess();
Greg Claytone1f50b92011-05-03 22:09:39 +00001592 return true;
1593 }
1594
1595 return false;
1596}
1597
1598static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001599LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001600{
1601 if (module)
1602 {
1603 SymbolContext sc;
1604
1605 ObjectFile *objfile = module->GetObjectFile ();
1606 if (objfile)
1607 {
1608 Symtab *symtab = objfile->GetSymtab();
1609 if (symtab)
1610 {
1611 uint32_t i;
1612 std::vector<uint32_t> match_indexes;
1613 ConstString symbol_name (name);
1614 uint32_t num_matches = 0;
1615 if (name_is_regex)
1616 {
1617 RegularExpression name_regexp(name);
1618 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1619 eSymbolTypeAny,
1620 match_indexes);
1621 }
1622 else
1623 {
1624 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1625 }
1626
1627
1628 if (num_matches > 0)
1629 {
1630 strm.Indent ();
1631 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1632 name_is_regex ? "the regular expression " : "", name);
1633 DumpFullpath (strm, &module->GetFileSpec(), 0);
1634 strm.PutCString(":\n");
1635 strm.IndentMore ();
Greg Clayton2ad894b2012-05-15 18:43:44 +00001636 //Symtab::DumpSymbolHeader (&strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001637 for (i=0; i < num_matches; ++i)
1638 {
1639 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton2ad894b2012-05-15 18:43:44 +00001640 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1641 symbol->GetAddress(),
1642 verbose,
1643 strm);
1644
1645// strm.Indent ();
1646// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytone1f50b92011-05-03 22:09:39 +00001647 }
1648 strm.IndentLess ();
1649 return num_matches;
1650 }
1651 }
1652 }
1653 }
1654 return 0;
1655}
1656
1657
1658static void
Greg Clayton2ad894b2012-05-15 18:43:44 +00001659DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001660{
1661 strm.IndentMore ();
1662 uint32_t i;
1663 const uint32_t num_matches = sc_list.GetSize();
1664
1665 for (i=0; i<num_matches; ++i)
1666 {
1667 SymbolContext sc;
1668 if (sc_list.GetContextAtIndex(i, sc))
1669 {
Sean Callanand7793d22012-02-11 00:24:04 +00001670 AddressRange range;
1671
1672 sc.GetAddressRange(eSymbolContextEverything,
1673 0,
1674 true,
1675 range);
1676
Greg Clayton2ad894b2012-05-15 18:43:44 +00001677 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytone1f50b92011-05-03 22:09:39 +00001678 }
1679 }
1680 strm.IndentLess ();
1681}
1682
1683static uint32_t
Greg Clayton2ad894b2012-05-15 18:43:44 +00001684LookupFunctionInModule (CommandInterpreter &interpreter,
1685 Stream &strm,
1686 Module *module,
1687 const char *name,
1688 bool name_is_regex,
1689 bool include_inlines,
1690 bool include_symbols,
1691 bool verbose)
Greg Claytone1f50b92011-05-03 22:09:39 +00001692{
1693 if (module && name && name[0])
1694 {
1695 SymbolContextList sc_list;
Greg Claytone1f50b92011-05-03 22:09:39 +00001696 const bool append = true;
1697 uint32_t num_matches = 0;
1698 if (name_is_regex)
1699 {
1700 RegularExpression function_name_regex (name);
1701 num_matches = module->FindFunctions (function_name_regex,
1702 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001703 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001704 append,
1705 sc_list);
1706 }
1707 else
1708 {
1709 ConstString function_name (name);
Sean Callanan3e80cd92011-10-12 02:08:07 +00001710 num_matches = module->FindFunctions (function_name,
1711 NULL,
Greg Claytone1f50b92011-05-03 22:09:39 +00001712 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector,
1713 include_symbols,
Sean Callanan302d78c2012-02-10 22:52:19 +00001714 include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00001715 append,
1716 sc_list);
1717 }
1718
1719 if (num_matches)
1720 {
1721 strm.Indent ();
1722 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1723 DumpFullpath (strm, &module->GetFileSpec(), 0);
1724 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001725 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001726 }
1727 return num_matches;
1728 }
1729 return 0;
1730}
1731
1732static uint32_t
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001733LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton801417e2011-07-07 01:59:51 +00001734 Stream &strm,
1735 Module *module,
1736 const char *name_cstr,
1737 bool name_is_regex)
Greg Claytone1f50b92011-05-03 22:09:39 +00001738{
1739 if (module && name_cstr && name_cstr[0])
1740 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001741 TypeList type_list;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001742 const uint32_t max_num_matches = UINT32_MAX;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001743 uint32_t num_matches = 0;
Greg Claytondc0a38c2012-03-26 23:03:23 +00001744 bool name_is_fully_qualified = false;
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001745 SymbolContext sc;
1746
1747 ConstString name(name_cstr);
Greg Claytondc0a38c2012-03-26 23:03:23 +00001748 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Greg Claytone1f50b92011-05-03 22:09:39 +00001749
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001750 if (num_matches)
1751 {
1752 strm.Indent ();
1753 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1754 DumpFullpath (strm, &module->GetFileSpec(), 0);
1755 strm.PutCString(":\n");
1756 const uint32_t num_types = type_list.GetSize();
1757 for (uint32_t i=0; i<num_types; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00001758 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001759 TypeSP type_sp (type_list.GetTypeAtIndex(i));
1760 if (type_sp)
Greg Claytone1f50b92011-05-03 22:09:39 +00001761 {
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001762 // Resolve the clang type so that any forward references
1763 // to types that haven't yet been parsed will get parsed.
1764 type_sp->GetClangFullType ();
1765 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Clayton0cbaacd2012-05-15 19:26:12 +00001766 // Print all typedef chains
1767 TypeSP typedef_type_sp (type_sp);
1768 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1769 while (typedefed_type_sp)
1770 {
1771 strm.EOL();
1772 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1773 typedefed_type_sp->GetClangFullType ();
1774 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1775 typedef_type_sp = typedefed_type_sp;
1776 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1777 }
Greg Claytone1f50b92011-05-03 22:09:39 +00001778 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001779 strm.EOL();
Greg Claytone1f50b92011-05-03 22:09:39 +00001780 }
Greg Clayton37bb8dd2011-12-08 02:13:16 +00001781 }
1782 return num_matches;
Greg Claytone1f50b92011-05-03 22:09:39 +00001783 }
1784 return 0;
1785}
1786
1787static uint32_t
Sean Callanan56d31ec2012-06-06 20:49:55 +00001788LookupTypeHere (CommandInterpreter &interpreter,
1789 Stream &strm,
1790 const SymbolContext &sym_ctx,
1791 const char *name_cstr,
1792 bool name_is_regex)
1793{
1794 if (!sym_ctx.module_sp)
1795 return 0;
1796
1797 TypeList type_list;
1798 const uint32_t max_num_matches = UINT32_MAX;
1799 uint32_t num_matches = 1;
1800 bool name_is_fully_qualified = false;
1801
1802 ConstString name(name_cstr);
1803 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
1804
1805 if (num_matches)
1806 {
1807 strm.Indent ();
1808 strm.PutCString("Best match found in ");
1809 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1810 strm.PutCString(":\n");
1811
1812 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1813 if (type_sp)
1814 {
1815 // Resolve the clang type so that any forward references
1816 // to types that haven't yet been parsed will get parsed.
1817 type_sp->GetClangFullType ();
1818 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1819 // Print all typedef chains
1820 TypeSP typedef_type_sp (type_sp);
1821 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1822 while (typedefed_type_sp)
1823 {
1824 strm.EOL();
1825 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1826 typedefed_type_sp->GetClangFullType ();
1827 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1828 typedef_type_sp = typedefed_type_sp;
1829 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1830 }
1831 }
1832 strm.EOL();
1833 }
1834 return num_matches;
1835}
1836
1837static uint32_t
Greg Claytone1f50b92011-05-03 22:09:39 +00001838LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanan56d31ec2012-06-06 20:49:55 +00001839 Stream &strm,
Greg Claytone1f50b92011-05-03 22:09:39 +00001840 Module *module,
1841 const FileSpec &file_spec,
1842 uint32_t line,
1843 bool check_inlines,
1844 bool verbose)
1845{
1846 if (module && file_spec)
1847 {
1848 SymbolContextList sc_list;
1849 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1850 eSymbolContextEverything, sc_list);
1851 if (num_matches > 0)
1852 {
1853 strm.Indent ();
1854 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1855 strm << file_spec;
1856 if (line > 0)
1857 strm.Printf (":%u", line);
1858 strm << " in ";
1859 DumpFullpath (strm, &module->GetFileSpec(), 0);
1860 strm.PutCString(":\n");
Greg Clayton2ad894b2012-05-15 18:43:44 +00001861 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytone1f50b92011-05-03 22:09:39 +00001862 return num_matches;
1863 }
1864 }
1865 return 0;
1866
1867}
1868
Greg Clayton91048ef2011-11-10 01:18:58 +00001869
1870static size_t
1871FindModulesByName (Target *target,
1872 const char *module_name,
1873 ModuleList &module_list,
1874 bool check_global_list)
1875{
1876// Dump specified images (by basename or fullpath)
1877 FileSpec module_file_spec(module_name, false);
Greg Clayton444fe992012-02-26 05:51:37 +00001878 ModuleSpec module_spec (module_file_spec);
Greg Clayton91048ef2011-11-10 01:18:58 +00001879
1880 const size_t initial_size = module_list.GetSize ();
1881
Greg Clayton316f57f2012-07-11 20:46:47 +00001882 if (check_global_list)
Greg Clayton91048ef2011-11-10 01:18:58 +00001883 {
1884 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00001885 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00001886 const uint32_t num_modules = Module::GetNumberAllocatedModules();
1887 ModuleSP module_sp;
1888 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1889 {
1890 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1891
1892 if (module)
1893 {
Greg Clayton444fe992012-02-26 05:51:37 +00001894 if (module->MatchesModuleSpec (module_spec))
Greg Clayton91048ef2011-11-10 01:18:58 +00001895 {
Greg Clayton13d24fb2012-01-29 20:56:30 +00001896 module_sp = module->shared_from_this();
Greg Clayton91048ef2011-11-10 01:18:58 +00001897 module_list.AppendIfNeeded(module_sp);
1898 }
1899 }
1900 }
1901 }
Greg Clayton316f57f2012-07-11 20:46:47 +00001902 else
1903 {
1904 if (target)
1905 {
1906 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
1907
1908 // Not found in our module list for our target, check the main
1909 // shared module list in case it is a extra file used somewhere
1910 // else
1911 if (num_matches == 0)
1912 {
1913 module_spec.GetArchitecture() = target->GetArchitecture();
1914 ModuleList::FindSharedModules (module_spec, module_list);
1915 }
1916 }
1917 else
1918 {
1919 ModuleList::FindSharedModules (module_spec,module_list);
1920 }
1921 }
1922
Greg Clayton91048ef2011-11-10 01:18:58 +00001923 return module_list.GetSize () - initial_size;
1924}
1925
Greg Claytone1f50b92011-05-03 22:09:39 +00001926#pragma mark CommandObjectTargetModulesModuleAutoComplete
1927
1928//----------------------------------------------------------------------
1929// A base command object class that can auto complete with module file
1930// paths
1931//----------------------------------------------------------------------
1932
Jim Inghamda26bd22012-06-08 21:56:10 +00001933class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001934{
1935public:
1936
1937 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
1938 const char *name,
1939 const char *help,
1940 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00001941 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00001942 {
1943 CommandArgumentEntry arg;
1944 CommandArgumentData file_arg;
1945
1946 // Define the first (and only) variant of this arg.
1947 file_arg.arg_type = eArgTypeFilename;
1948 file_arg.arg_repetition = eArgRepeatStar;
1949
1950 // There is only one variant this argument could be; put it into the argument entry.
1951 arg.push_back (file_arg);
1952
1953 // Push the data for the first argument into the m_arguments vector.
1954 m_arguments.push_back (arg);
1955 }
1956
1957 virtual
1958 ~CommandObjectTargetModulesModuleAutoComplete ()
1959 {
1960 }
1961
1962 virtual int
1963 HandleArgumentCompletion (Args &input,
1964 int &cursor_index,
1965 int &cursor_char_position,
1966 OptionElementVector &opt_element_vector,
1967 int match_start_point,
1968 int max_return_elements,
1969 bool &word_complete,
1970 StringList &matches)
1971 {
1972 // Arguments are the standard module completer.
1973 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
1974 completion_str.erase (cursor_char_position);
1975
1976 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
1977 CommandCompletions::eModuleCompletion,
1978 completion_str.c_str(),
1979 match_start_point,
1980 max_return_elements,
1981 NULL,
1982 word_complete,
1983 matches);
1984 return matches.GetSize();
1985 }
1986};
1987
1988#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1989
1990//----------------------------------------------------------------------
1991// A base command object class that can auto complete with module source
1992// file paths
1993//----------------------------------------------------------------------
1994
Jim Inghamda26bd22012-06-08 21:56:10 +00001995class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00001996{
1997public:
1998
1999 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
2000 const char *name,
2001 const char *help,
2002 const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002003 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytone1f50b92011-05-03 22:09:39 +00002004 {
2005 CommandArgumentEntry arg;
2006 CommandArgumentData source_file_arg;
2007
2008 // Define the first (and only) variant of this arg.
2009 source_file_arg.arg_type = eArgTypeSourceFile;
2010 source_file_arg.arg_repetition = eArgRepeatPlus;
2011
2012 // There is only one variant this argument could be; put it into the argument entry.
2013 arg.push_back (source_file_arg);
2014
2015 // Push the data for the first argument into the m_arguments vector.
2016 m_arguments.push_back (arg);
2017 }
2018
2019 virtual
2020 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2021 {
2022 }
2023
2024 virtual int
2025 HandleArgumentCompletion (Args &input,
2026 int &cursor_index,
2027 int &cursor_char_position,
2028 OptionElementVector &opt_element_vector,
2029 int match_start_point,
2030 int max_return_elements,
2031 bool &word_complete,
2032 StringList &matches)
2033 {
2034 // Arguments are the standard source file completer.
2035 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2036 completion_str.erase (cursor_char_position);
2037
2038 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2039 CommandCompletions::eSourceFileCompletion,
2040 completion_str.c_str(),
2041 match_start_point,
2042 max_return_elements,
2043 NULL,
2044 word_complete,
2045 matches);
2046 return matches.GetSize();
2047 }
2048};
2049
2050
2051#pragma mark CommandObjectTargetModulesDumpSymtab
2052
2053
2054class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2055{
2056public:
2057 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2058 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2059 "target modules dump symtab",
2060 "Dump the symbol table from one or more target modules.",
2061 NULL),
2062 m_options (interpreter)
2063 {
2064 }
2065
2066 virtual
2067 ~CommandObjectTargetModulesDumpSymtab ()
2068 {
2069 }
2070
Jim Inghamda26bd22012-06-08 21:56:10 +00002071 virtual Options *
2072 GetOptions ()
2073 {
2074 return &m_options;
2075 }
2076
2077 class CommandOptions : public Options
2078 {
2079 public:
2080
2081 CommandOptions (CommandInterpreter &interpreter) :
2082 Options(interpreter),
2083 m_sort_order (eSortOrderNone)
2084 {
2085 }
2086
2087 virtual
2088 ~CommandOptions ()
2089 {
2090 }
2091
2092 virtual Error
2093 SetOptionValue (uint32_t option_idx, const char *option_arg)
2094 {
2095 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00002096 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamda26bd22012-06-08 21:56:10 +00002097
2098 switch (short_option)
2099 {
2100 case 's':
2101 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2102 g_option_table[option_idx].enum_values,
2103 eSortOrderNone,
2104 error);
2105 break;
2106
2107 default:
2108 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2109 break;
2110
2111 }
2112 return error;
2113 }
2114
2115 void
2116 OptionParsingStarting ()
2117 {
2118 m_sort_order = eSortOrderNone;
2119 }
2120
2121 const OptionDefinition*
2122 GetDefinitions ()
2123 {
2124 return g_option_table;
2125 }
2126
2127 // Options table: Required for subclasses of Options.
2128 static OptionDefinition g_option_table[];
2129
2130 SortOrder m_sort_order;
2131 };
2132
2133protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002134 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002135 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002136 CommandReturnObject &result)
2137 {
2138 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2139 if (target == NULL)
2140 {
2141 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2142 result.SetStatus (eReturnStatusFailed);
2143 return false;
2144 }
2145 else
2146 {
2147 uint32_t num_dumped = 0;
2148
2149 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2150 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2151 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2152
2153 if (command.GetArgumentCount() == 0)
2154 {
2155 // Dump all sections for all modules images
Jim Ingham93367902012-05-30 02:19:25 +00002156 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytone1f50b92011-05-03 22:09:39 +00002157 const uint32_t num_modules = target->GetImages().GetSize();
2158 if (num_modules > 0)
2159 {
2160 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
2161 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2162 {
2163 if (num_dumped > 0)
2164 {
2165 result.GetOutputStream().EOL();
2166 result.GetOutputStream().EOL();
2167 }
2168 num_dumped++;
Jim Ingham93367902012-05-30 02:19:25 +00002169 DumpModuleSymtab (m_interpreter,
2170 result.GetOutputStream(),
2171 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2172 m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002173 }
2174 }
2175 else
2176 {
2177 result.AppendError ("the target has no associated executable images");
2178 result.SetStatus (eReturnStatusFailed);
2179 return false;
2180 }
2181 }
2182 else
2183 {
2184 // Dump specified images (by basename or fullpath)
2185 const char *arg_cstr;
2186 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2187 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002188 ModuleList module_list;
2189 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2190 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002191 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002192 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002193 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002194 Module *module = module_list.GetModulePointerAtIndex(i);
2195 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002196 {
2197 if (num_dumped > 0)
2198 {
2199 result.GetOutputStream().EOL();
2200 result.GetOutputStream().EOL();
2201 }
2202 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002203 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytone1f50b92011-05-03 22:09:39 +00002204 }
2205 }
2206 }
2207 else
2208 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2209 }
2210 }
2211
2212 if (num_dumped > 0)
2213 result.SetStatus (eReturnStatusSuccessFinishResult);
2214 else
2215 {
2216 result.AppendError ("no matching executable images found");
2217 result.SetStatus (eReturnStatusFailed);
2218 }
2219 }
2220 return result.Succeeded();
2221 }
2222
Greg Claytone1f50b92011-05-03 22:09:39 +00002223
2224 CommandOptions m_options;
2225};
2226
2227static OptionEnumValueElement
2228g_sort_option_enumeration[4] =
2229{
2230 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2231 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2232 { eSortOrderByName, "name", "Sort output by symbol name."},
2233 { 0, NULL, NULL }
2234};
2235
2236
2237OptionDefinition
2238CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2239{
2240 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2241 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
2242};
2243
2244#pragma mark CommandObjectTargetModulesDumpSections
2245
2246//----------------------------------------------------------------------
2247// Image section dumping command
2248//----------------------------------------------------------------------
2249
2250class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2251{
2252public:
2253 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2254 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2255 "target modules dump sections",
2256 "Dump the sections from one or more target modules.",
2257 //"target modules dump sections [<file1> ...]")
2258 NULL)
2259 {
2260 }
2261
2262 virtual
2263 ~CommandObjectTargetModulesDumpSections ()
2264 {
2265 }
2266
Jim Inghamda26bd22012-06-08 21:56:10 +00002267protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002268 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002269 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002270 CommandReturnObject &result)
2271 {
2272 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2273 if (target == NULL)
2274 {
2275 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2276 result.SetStatus (eReturnStatusFailed);
2277 return false;
2278 }
2279 else
2280 {
2281 uint32_t num_dumped = 0;
2282
2283 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2284 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2285 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2286
2287 if (command.GetArgumentCount() == 0)
2288 {
2289 // Dump all sections for all modules images
2290 const uint32_t num_modules = target->GetImages().GetSize();
2291 if (num_modules > 0)
2292 {
2293 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
2294 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2295 {
2296 num_dumped++;
2297 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2298 }
2299 }
2300 else
2301 {
2302 result.AppendError ("the target has no associated executable images");
2303 result.SetStatus (eReturnStatusFailed);
2304 return false;
2305 }
2306 }
2307 else
2308 {
2309 // Dump specified images (by basename or fullpath)
2310 const char *arg_cstr;
2311 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2312 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002313 ModuleList module_list;
2314 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2315 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002316 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002317 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002318 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002319 Module *module = module_list.GetModulePointerAtIndex(i);
2320 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002321 {
2322 num_dumped++;
Greg Clayton91048ef2011-11-10 01:18:58 +00002323 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytone1f50b92011-05-03 22:09:39 +00002324 }
2325 }
2326 }
2327 else
Greg Clayton91048ef2011-11-10 01:18:58 +00002328 {
2329 // Check the global list
Greg Claytonc149c8b2012-01-27 18:08:35 +00002330 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton91048ef2011-11-10 01:18:58 +00002331
Greg Claytone1f50b92011-05-03 22:09:39 +00002332 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton91048ef2011-11-10 01:18:58 +00002333 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002334 }
2335 }
2336
2337 if (num_dumped > 0)
2338 result.SetStatus (eReturnStatusSuccessFinishResult);
2339 else
2340 {
2341 result.AppendError ("no matching executable images found");
2342 result.SetStatus (eReturnStatusFailed);
2343 }
2344 }
2345 return result.Succeeded();
2346 }
2347};
2348
2349
2350#pragma mark CommandObjectTargetModulesDumpSymfile
2351
2352//----------------------------------------------------------------------
2353// Image debug symbol dumping command
2354//----------------------------------------------------------------------
2355
2356class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2357{
2358public:
2359 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2360 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2361 "target modules dump symfile",
2362 "Dump the debug symbol file for one or more target modules.",
2363 //"target modules dump symfile [<file1> ...]")
2364 NULL)
2365 {
2366 }
2367
2368 virtual
2369 ~CommandObjectTargetModulesDumpSymfile ()
2370 {
2371 }
2372
Jim Inghamda26bd22012-06-08 21:56:10 +00002373protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002374 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002375 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002376 CommandReturnObject &result)
2377 {
2378 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2379 if (target == NULL)
2380 {
2381 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2382 result.SetStatus (eReturnStatusFailed);
2383 return false;
2384 }
2385 else
2386 {
2387 uint32_t num_dumped = 0;
2388
2389 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2390 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2391 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2392
2393 if (command.GetArgumentCount() == 0)
2394 {
2395 // Dump all sections for all modules images
Enrico Granata146d9522012-11-08 02:22:02 +00002396 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002397 Mutex::Locker modules_locker (target_modules.GetMutex());
2398 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002399 if (num_modules > 0)
2400 {
2401 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
2402 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2403 {
Jim Ingham93367902012-05-30 02:19:25 +00002404 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytone1f50b92011-05-03 22:09:39 +00002405 num_dumped++;
2406 }
2407 }
2408 else
2409 {
2410 result.AppendError ("the target has no associated executable images");
2411 result.SetStatus (eReturnStatusFailed);
2412 return false;
2413 }
2414 }
2415 else
2416 {
2417 // Dump specified images (by basename or fullpath)
2418 const char *arg_cstr;
2419 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2420 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002421 ModuleList module_list;
2422 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2423 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00002424 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002425 for (size_t i=0; i<num_matches; ++i)
Greg Claytone1f50b92011-05-03 22:09:39 +00002426 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002427 Module *module = module_list.GetModulePointerAtIndex(i);
2428 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00002429 {
Greg Clayton91048ef2011-11-10 01:18:58 +00002430 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytone1f50b92011-05-03 22:09:39 +00002431 num_dumped++;
2432 }
2433 }
2434 }
2435 else
2436 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2437 }
2438 }
2439
2440 if (num_dumped > 0)
2441 result.SetStatus (eReturnStatusSuccessFinishResult);
2442 else
2443 {
2444 result.AppendError ("no matching executable images found");
2445 result.SetStatus (eReturnStatusFailed);
2446 }
2447 }
2448 return result.Succeeded();
2449 }
2450};
2451
2452
2453#pragma mark CommandObjectTargetModulesDumpLineTable
2454
2455//----------------------------------------------------------------------
2456// Image debug line table dumping command
2457//----------------------------------------------------------------------
2458
2459class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2460{
2461public:
2462 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2463 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
2464 "target modules dump line-table",
2465 "Dump the debug symbol file for one or more target modules.",
2466 NULL)
2467 {
2468 }
2469
2470 virtual
2471 ~CommandObjectTargetModulesDumpLineTable ()
2472 {
2473 }
2474
Jim Inghamda26bd22012-06-08 21:56:10 +00002475protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002476 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002477 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00002478 CommandReturnObject &result)
2479 {
2480 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2481 if (target == NULL)
2482 {
2483 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2484 result.SetStatus (eReturnStatusFailed);
2485 return false;
2486 }
2487 else
2488 {
2489 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
2490 uint32_t total_num_dumped = 0;
2491
2492 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2493 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2494 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2495
2496 if (command.GetArgumentCount() == 0)
2497 {
2498 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
2499 result.SetStatus (eReturnStatusFailed);
2500 }
2501 else
2502 {
2503 // Dump specified images (by basename or fullpath)
2504 const char *arg_cstr;
2505 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2506 {
2507 FileSpec file_spec(arg_cstr, false);
Jim Ingham93367902012-05-30 02:19:25 +00002508
Enrico Granata146d9522012-11-08 02:22:02 +00002509 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00002510 Mutex::Locker modules_locker(target_modules.GetMutex());
2511 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002512 if (num_modules > 0)
2513 {
2514 uint32_t num_dumped = 0;
2515 for (uint32_t i = 0; i<num_modules; ++i)
2516 {
2517 if (DumpCompileUnitLineTable (m_interpreter,
2518 result.GetOutputStream(),
Jim Ingham93367902012-05-30 02:19:25 +00002519 target_modules.GetModulePointerAtIndexUnlocked(i),
Greg Claytone1f50b92011-05-03 22:09:39 +00002520 file_spec,
Greg Clayton567e7f32011-09-22 04:58:26 +00002521 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive()))
Greg Claytone1f50b92011-05-03 22:09:39 +00002522 num_dumped++;
2523 }
2524 if (num_dumped == 0)
2525 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2526 else
2527 total_num_dumped += num_dumped;
2528 }
2529 }
2530 }
2531
2532 if (total_num_dumped > 0)
2533 result.SetStatus (eReturnStatusSuccessFinishResult);
2534 else
2535 {
2536 result.AppendError ("no source filenames matched any command arguments");
2537 result.SetStatus (eReturnStatusFailed);
2538 }
2539 }
2540 return result.Succeeded();
2541 }
2542};
2543
2544
2545#pragma mark CommandObjectTargetModulesDump
2546
2547//----------------------------------------------------------------------
2548// Dump multi-word command for target modules
2549//----------------------------------------------------------------------
2550
2551class CommandObjectTargetModulesDump : public CommandObjectMultiword
2552{
2553public:
2554
2555 //------------------------------------------------------------------
2556 // Constructors and Destructors
2557 //------------------------------------------------------------------
2558 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2559 CommandObjectMultiword (interpreter,
2560 "target modules dump",
2561 "A set of commands for dumping information about one or more target modules.",
2562 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2563 {
2564 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2565 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2566 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2567 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2568 }
2569
2570 virtual
2571 ~CommandObjectTargetModulesDump()
2572 {
2573 }
2574};
2575
Jim Inghamda26bd22012-06-08 21:56:10 +00002576class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00002577{
2578public:
2579 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00002580 CommandObjectParsed (interpreter,
2581 "target modules add",
2582 "Add a new module to the current target's modules.",
Greg Clayton1649a722012-11-29 22:16:27 +00002583 "target modules add [<module>]"),
Greg Claytonec9c2d22012-11-30 19:05:35 +00002584 m_option_group (interpreter),
2585 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 +00002586 {
Greg Clayton1649a722012-11-29 22:16:27 +00002587 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonec9c2d22012-11-30 19:05:35 +00002588 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1649a722012-11-29 22:16:27 +00002589 m_option_group.Finalize();
Greg Claytone1f50b92011-05-03 22:09:39 +00002590 }
2591
2592 virtual
2593 ~CommandObjectTargetModulesAdd ()
2594 {
2595 }
Greg Clayton1649a722012-11-29 22:16:27 +00002596
2597 virtual Options *
2598 GetOptions ()
2599 {
2600 return &m_option_group;
2601 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002602
Jim Inghamda26bd22012-06-08 21:56:10 +00002603 int
2604 HandleArgumentCompletion (Args &input,
2605 int &cursor_index,
2606 int &cursor_char_position,
2607 OptionElementVector &opt_element_vector,
2608 int match_start_point,
2609 int max_return_elements,
2610 bool &word_complete,
2611 StringList &matches)
2612 {
2613 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2614 completion_str.erase (cursor_char_position);
2615
2616 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2617 CommandCompletions::eDiskFileCompletion,
2618 completion_str.c_str(),
2619 match_start_point,
2620 max_return_elements,
2621 NULL,
2622 word_complete,
2623 matches);
2624 return matches.GetSize();
2625 }
2626
2627protected:
Greg Clayton1649a722012-11-29 22:16:27 +00002628
2629 OptionGroupOptions m_option_group;
2630 OptionGroupUUID m_uuid_option_group;
Greg Claytonec9c2d22012-11-30 19:05:35 +00002631 OptionGroupFile m_symbol_file;
Greg Clayton1649a722012-11-29 22:16:27 +00002632
2633
Greg Claytone1f50b92011-05-03 22:09:39 +00002634 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002635 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002636 CommandReturnObject &result)
2637 {
2638 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2639 if (target == NULL)
2640 {
2641 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2642 result.SetStatus (eReturnStatusFailed);
2643 return false;
2644 }
2645 else
2646 {
Sean Callanane3f06612012-12-13 01:39:39 +00002647 bool flush = false;
2648
Greg Claytone1f50b92011-05-03 22:09:39 +00002649 const size_t argc = args.GetArgumentCount();
2650 if (argc == 0)
2651 {
Greg Clayton1649a722012-11-29 22:16:27 +00002652 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2653 {
2654 // We are given a UUID only, go locate the file
2655 ModuleSpec module_spec;
2656 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002657 if (m_symbol_file.GetOptionValue().OptionWasSet())
2658 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002659 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2660 {
2661 ModuleSP module_sp (target->GetSharedModule (module_spec));
2662 if (module_sp)
2663 {
2664 result.SetStatus (eReturnStatusSuccessFinishResult);
2665 return true;
2666 }
2667 else
2668 {
Sean Callanane3f06612012-12-13 01:39:39 +00002669 flush = true;
2670
Greg Clayton1649a722012-11-29 22:16:27 +00002671 StreamString strm;
2672 module_spec.GetUUID().Dump (&strm);
2673 if (module_spec.GetFileSpec())
2674 {
2675 if (module_spec.GetSymbolFileSpec())
2676 {
2677 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s and symbol file %s/%s",
2678 strm.GetString().c_str(),
2679 module_spec.GetFileSpec().GetDirectory().GetCString(),
2680 module_spec.GetFileSpec().GetFilename().GetCString(),
2681 module_spec.GetSymbolFileSpec().GetDirectory().GetCString(),
2682 module_spec.GetSymbolFileSpec().GetFilename().GetCString());
2683 }
2684 else
2685 {
2686 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s",
2687 strm.GetString().c_str(),
2688 module_spec.GetFileSpec().GetDirectory().GetCString(),
2689 module_spec.GetFileSpec().GetFilename().GetCString());
2690 }
2691 }
2692 else
2693 {
2694 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2695 strm.GetString().c_str());
2696 }
2697 result.SetStatus (eReturnStatusFailed);
2698 return false;
2699 }
2700 }
2701 else
2702 {
2703 StreamString strm;
2704 module_spec.GetUUID().Dump (&strm);
2705 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2706 result.SetStatus (eReturnStatusFailed);
2707 return false;
2708 }
2709 }
2710 else
2711 {
2712 result.AppendError ("one or more executable image paths must be specified");
2713 result.SetStatus (eReturnStatusFailed);
2714 return false;
2715 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002716 }
2717 else
2718 {
2719 for (size_t i=0; i<argc; ++i)
2720 {
2721 const char *path = args.GetArgumentAtIndex(i);
2722 if (path)
2723 {
2724 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002725 if (file_spec.Exists())
2726 {
Greg Clayton444fe992012-02-26 05:51:37 +00002727 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002728 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2729 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002730 if (m_symbol_file.GetOptionValue().OptionWasSet())
2731 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002732 Error error;
2733 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002734 if (!module_sp)
2735 {
Greg Clayton1649a722012-11-29 22:16:27 +00002736 const char *error_cstr = error.AsCString();
2737 if (error_cstr)
2738 result.AppendError (error_cstr);
2739 else
2740 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002741 result.SetStatus (eReturnStatusFailed);
2742 return false;
2743 }
Sean Callanane3f06612012-12-13 01:39:39 +00002744 else
2745 {
2746 flush = true;
2747 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002748 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002749 }
2750 else
2751 {
2752 char resolved_path[PATH_MAX];
2753 result.SetStatus (eReturnStatusFailed);
2754 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2755 {
2756 if (strcmp (resolved_path, path) != 0)
2757 {
2758 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2759 break;
2760 }
2761 }
2762 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2763 break;
2764 }
2765 }
2766 }
2767 }
Sean Callanane3f06612012-12-13 01:39:39 +00002768
2769 if (flush)
2770 {
2771 ProcessSP process = target->GetProcessSP();
2772 if (process)
2773 process->Flush();
2774 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002775 }
Sean Callanane3f06612012-12-13 01:39:39 +00002776
Greg Claytone1f50b92011-05-03 22:09:39 +00002777 return result.Succeeded();
2778 }
2779
Greg Claytone1f50b92011-05-03 22:09:39 +00002780};
2781
2782class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2783{
2784public:
2785 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2786 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2787 "target modules load",
2788 "Set the load addresses for one or more sections in a target module.",
2789 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2790 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002791 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 +00002792 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)
2793 {
2794 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2795 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2796 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2797 m_option_group.Finalize();
2798 }
2799
2800 virtual
2801 ~CommandObjectTargetModulesLoad ()
2802 {
2803 }
2804
Jim Inghamda26bd22012-06-08 21:56:10 +00002805 virtual Options *
2806 GetOptions ()
2807 {
2808 return &m_option_group;
2809 }
2810
2811protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002812 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002813 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002814 CommandReturnObject &result)
2815 {
2816 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2817 if (target == NULL)
2818 {
2819 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2820 result.SetStatus (eReturnStatusFailed);
2821 return false;
2822 }
2823 else
2824 {
2825 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002826 ModuleSpec module_spec;
2827 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002828 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002829 {
2830 search_using_module_spec = true;
2831 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2832 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002833
2834 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002835 {
2836 search_using_module_spec = true;
2837 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2838 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002839
Greg Clayton444fe992012-02-26 05:51:37 +00002840 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002841 {
2842
2843 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002844 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002845
2846 char path[PATH_MAX];
2847 if (num_matches == 1)
2848 {
2849 Module *module = matching_modules.GetModulePointerAtIndex(0);
2850 if (module)
2851 {
2852 ObjectFile *objfile = module->GetObjectFile();
2853 if (objfile)
2854 {
2855 SectionList *section_list = objfile->GetSectionList();
2856 if (section_list)
2857 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002858 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002859 if (argc == 0)
2860 {
2861 if (m_slide_option.GetOptionValue().OptionWasSet())
2862 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002863 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2864 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002865 }
2866 else
2867 {
2868 result.AppendError ("one or more section name + load address pair must be specified");
2869 result.SetStatus (eReturnStatusFailed);
2870 return false;
2871 }
2872 }
2873 else
2874 {
2875 if (m_slide_option.GetOptionValue().OptionWasSet())
2876 {
2877 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2878 result.SetStatus (eReturnStatusFailed);
2879 return false;
2880 }
2881
2882 for (size_t i=0; i<argc; i += 2)
2883 {
2884 const char *sect_name = args.GetArgumentAtIndex(i);
2885 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2886 if (sect_name && load_addr_cstr)
2887 {
2888 ConstString const_sect_name(sect_name);
2889 bool success = false;
2890 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2891 if (success)
2892 {
2893 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2894 if (section_sp)
2895 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002896 if (section_sp->IsThreadSpecific())
2897 {
2898 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2899 result.SetStatus (eReturnStatusFailed);
2900 break;
2901 }
2902 else
2903 {
Greg Clayton545762f2012-07-07 01:24:12 +00002904 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002905 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002906 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002907 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002908 }
2909 else
2910 {
2911 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2912 result.SetStatus (eReturnStatusFailed);
2913 break;
2914 }
2915 }
2916 else
2917 {
2918 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2919 result.SetStatus (eReturnStatusFailed);
2920 break;
2921 }
2922 }
2923 else
2924 {
2925 if (sect_name)
2926 result.AppendError ("section names must be followed by a load address.\n");
2927 else
2928 result.AppendError ("one or more section name + load address pair must be specified.\n");
2929 result.SetStatus (eReturnStatusFailed);
2930 break;
2931 }
2932 }
2933 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002934
2935 if (changed)
2936 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002937 }
2938 else
2939 {
2940 module->GetFileSpec().GetPath (path, sizeof(path));
2941 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2942 result.SetStatus (eReturnStatusFailed);
2943 }
2944 }
2945 else
2946 {
2947 module->GetFileSpec().GetPath (path, sizeof(path));
2948 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2949 result.SetStatus (eReturnStatusFailed);
2950 }
2951 }
2952 else
2953 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002954 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2955 if (module_spec_file)
2956 {
2957 module_spec_file->GetPath (path, sizeof(path));
2958 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2959 }
2960 else
2961 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002962 result.SetStatus (eReturnStatusFailed);
2963 }
2964 }
2965 else
2966 {
2967 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002968
2969 if (module_spec.GetFileSpec())
2970 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002971 else
2972 path[0] = '\0';
2973
Greg Clayton444fe992012-02-26 05:51:37 +00002974 if (module_spec.GetUUIDPtr())
2975 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002976 else
2977 uuid_cstr[0] = '\0';
2978 if (num_matches > 1)
2979 {
2980 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2981 path[0] ? " file=" : "",
2982 path,
2983 uuid_cstr[0] ? " uuid=" : "",
2984 uuid_cstr);
2985 for (size_t i=0; i<num_matches; ++i)
2986 {
2987 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2988 result.AppendMessageWithFormat("%s\n", path);
2989 }
2990 }
2991 else
2992 {
2993 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2994 path[0] ? " file=" : "",
2995 path,
2996 uuid_cstr[0] ? " uuid=" : "",
2997 uuid_cstr);
2998 }
2999 result.SetStatus (eReturnStatusFailed);
3000 }
3001 }
3002 else
3003 {
3004 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3005 result.SetStatus (eReturnStatusFailed);
3006 return false;
3007 }
3008 }
3009 return result.Succeeded();
3010 }
3011
Greg Claytone1f50b92011-05-03 22:09:39 +00003012 OptionGroupOptions m_option_group;
3013 OptionGroupUUID m_uuid_option_group;
3014 OptionGroupFile m_file_option;
3015 OptionGroupUInt64 m_slide_option;
3016};
3017
3018//----------------------------------------------------------------------
3019// List images with associated information
3020//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003021class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003022{
3023public:
3024
3025 class CommandOptions : public Options
3026 {
3027 public:
3028
3029 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00003030 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00003031 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00003032 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00003033 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00003034 {
3035 }
3036
3037 virtual
3038 ~CommandOptions ()
3039 {
3040 }
3041
3042 virtual Error
3043 SetOptionValue (uint32_t option_idx, const char *option_arg)
3044 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003045 Error error;
3046
Greg Clayton6475c422012-12-04 00:32:51 +00003047 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00003048 if (short_option == 'g')
3049 {
3050 m_use_global_module_list = true;
3051 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003052 else if (short_option == 'a')
3053 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003054 m_module_addr = Args::StringToAddress(NULL, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Ingham6bdea822011-10-24 18:36:33 +00003055 }
Greg Clayton899025f2011-08-09 00:01:09 +00003056 else
3057 {
3058 uint32_t width = 0;
3059 if (option_arg)
3060 width = strtoul (option_arg, NULL, 0);
3061 m_format_array.push_back(std::make_pair(short_option, width));
3062 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003063 return error;
3064 }
3065
3066 void
3067 OptionParsingStarting ()
3068 {
3069 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00003070 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00003071 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00003072 }
3073
3074 const OptionDefinition*
3075 GetDefinitions ()
3076 {
3077 return g_option_table;
3078 }
3079
3080 // Options table: Required for subclasses of Options.
3081
3082 static OptionDefinition g_option_table[];
3083
3084 // Instance variables to hold the values for command options.
3085 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3086 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00003087 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00003088 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00003089 };
3090
3091 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003092 CommandObjectParsed (interpreter,
3093 "target modules list",
3094 "List current executable and dependent shared library images.",
3095 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00003096 m_options (interpreter)
3097 {
3098 }
3099
3100 virtual
3101 ~CommandObjectTargetModulesList ()
3102 {
3103 }
3104
3105 virtual
3106 Options *
3107 GetOptions ()
3108 {
3109 return &m_options;
3110 }
3111
Jim Inghamda26bd22012-06-08 21:56:10 +00003112protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003113 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003114 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003115 CommandReturnObject &result)
3116 {
3117 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00003118 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00003119 // Define a local module list here to ensure it lives longer than any "locker"
3120 // object which might lock its contents below (through the "module_list_ptr"
3121 // variable).
3122 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00003123 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00003124 {
3125 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3126 result.SetStatus (eReturnStatusFailed);
3127 return false;
3128 }
3129 else
3130 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003131 if (target)
3132 {
3133 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3134 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3135 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3136 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003137 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00003138 Stream &strm = result.GetOutputStream();
3139
3140 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3141 {
3142 if (target)
3143 {
3144 Address module_address;
3145 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3146 {
Greg Clayton3508c382012-02-24 01:59:29 +00003147 ModuleSP module_sp (module_address.GetModule());
3148 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00003149 {
Greg Clayton3508c382012-02-24 01:59:29 +00003150 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00003151 result.SetStatus (eReturnStatusSuccessFinishResult);
3152 }
3153 else
3154 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003155 result.AppendError ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003156 result.SetStatus (eReturnStatusFailed);
3157 }
3158 }
3159 else
3160 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003161 result.AppendError ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003162 result.SetStatus (eReturnStatusFailed);
3163 }
3164 }
3165 else
3166 {
3167 result.AppendError ("Can only look up modules by address with a valid target.");
3168 result.SetStatus (eReturnStatusFailed);
3169 }
3170 return result.Succeeded();
3171 }
3172
Jim Ingham93367902012-05-30 02:19:25 +00003173 uint32_t num_modules = 0;
3174 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3175 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3176 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003177 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003178 const size_t argc = command.GetArgumentCount();
3179 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003180 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003181 if (use_global_module_list)
3182 {
3183 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3184 num_modules = Module::GetNumberAllocatedModules();
3185 }
3186 else
3187 {
3188 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003189 }
Greg Clayton899025f2011-08-09 00:01:09 +00003190 }
3191 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003192 {
3193 for (size_t i=0; i<argc; ++i)
3194 {
3195 // Dump specified images (by basename or fullpath)
3196 const char *arg_cstr = command.GetArgumentAtIndex(i);
3197 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3198 if (num_matches == 0)
3199 {
3200 if (argc == 1)
3201 {
3202 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3203 result.SetStatus (eReturnStatusFailed);
3204 return false;
3205 }
3206 }
3207 }
3208
Greg Clayton2ad894b2012-05-15 18:43:44 +00003209 module_list_ptr = &module_list;
3210 }
Jim Ingham93367902012-05-30 02:19:25 +00003211
3212 if (module_list_ptr != NULL)
3213 {
3214 locker.Lock(module_list_ptr->GetMutex());
3215 num_modules = module_list_ptr->GetSize();
3216 }
Greg Clayton899025f2011-08-09 00:01:09 +00003217
Greg Claytone1f50b92011-05-03 22:09:39 +00003218 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003219 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003220 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3221 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003222 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003223 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003224 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003225 {
Jim Ingham93367902012-05-30 02:19:25 +00003226 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003227 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003228 }
3229 else
3230 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003231 module = Module::GetAllocatedModuleAtIndex(image_idx);
3232 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003233 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003234
Greg Claytonb5a8f142012-02-05 02:38:54 +00003235 int indent = strm.Printf("[%3u] ", image_idx);
3236 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003237
Greg Claytone1f50b92011-05-03 22:09:39 +00003238 }
3239 result.SetStatus (eReturnStatusSuccessFinishResult);
3240 }
3241 else
3242 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003243 if (argc)
3244 {
3245 if (use_global_module_list)
3246 result.AppendError ("the global module list has no matching modules");
3247 else
3248 result.AppendError ("the target has no matching modules");
3249 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003250 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003251 {
3252 if (use_global_module_list)
3253 result.AppendError ("the global module list is empty");
3254 else
3255 result.AppendError ("the target has no associated executable images");
3256 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003257 result.SetStatus (eReturnStatusFailed);
3258 return false;
3259 }
3260 }
3261 return result.Succeeded();
3262 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003263
3264 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003265 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003266 {
3267
Jim Ingham6f01c932012-10-12 17:34:26 +00003268 if (module == NULL)
3269 {
3270 strm.PutCString("Null module");
3271 return;
3272 }
3273
Jim Ingham6bdea822011-10-24 18:36:33 +00003274 bool dump_object_name = false;
3275 if (m_options.m_format_array.empty())
3276 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003277 m_options.m_format_array.push_back(std::make_pair('u', 0));
3278 m_options.m_format_array.push_back(std::make_pair('h', 0));
3279 m_options.m_format_array.push_back(std::make_pair('f', 0));
3280 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003281 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003282 const size_t num_entries = m_options.m_format_array.size();
3283 bool print_space = false;
3284 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003285 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003286 if (print_space)
3287 strm.PutChar(' ');
3288 print_space = true;
3289 const char format_char = m_options.m_format_array[i].first;
3290 uint32_t width = m_options.m_format_array[i].second;
3291 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003292 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003293 case 'A':
3294 DumpModuleArchitecture (strm, module, false, width);
3295 break;
3296
3297 case 't':
3298 DumpModuleArchitecture (strm, module, true, width);
3299 break;
3300
3301 case 'f':
3302 DumpFullpath (strm, &module->GetFileSpec(), width);
3303 dump_object_name = true;
3304 break;
3305
3306 case 'd':
3307 DumpDirectory (strm, &module->GetFileSpec(), width);
3308 break;
3309
3310 case 'b':
3311 DumpBasename (strm, &module->GetFileSpec(), width);
3312 dump_object_name = true;
3313 break;
3314
3315 case 'h':
3316 case 'o':
3317 // Image header address
3318 {
3319 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003320
Greg Claytonb5a8f142012-02-05 02:38:54 +00003321 ObjectFile *objfile = module->GetObjectFile ();
3322 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003323 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003324 Address header_addr(objfile->GetHeaderAddress());
3325 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003326 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003327 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003328 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003329 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3330 if (header_load_addr == LLDB_INVALID_ADDRESS)
3331 {
3332 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3333 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003334 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003335 {
3336 if (format_char == 'o')
3337 {
3338 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003339 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003340 }
3341 else
3342 {
3343 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003344 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003345 }
3346 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003347 break;
3348 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003349 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3350 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3351 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003352 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003353 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003354 strm.Printf ("%*s", addr_nibble_width + 2, "");
3355 }
3356 break;
3357 case 'r':
3358 {
3359 uint32_t ref_count = 0;
3360 ModuleSP module_sp (module->shared_from_this());
3361 if (module_sp)
3362 {
3363 // Take one away to make sure we don't count our local "module_sp"
3364 ref_count = module_sp.use_count() - 1;
3365 }
3366 if (width)
3367 strm.Printf("{%*u}", width, ref_count);
3368 else
3369 strm.Printf("{%u}", ref_count);
3370 }
3371 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003372
Greg Claytonb5a8f142012-02-05 02:38:54 +00003373 case 's':
3374 case 'S':
3375 {
3376 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3377 if (symbol_vendor)
3378 {
3379 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3380 if (symbol_file)
3381 {
3382 if (format_char == 'S')
3383 {
3384 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3385 // Dump symbol file only if different from module file
3386 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3387 {
3388 print_space = false;
3389 break;
3390 }
3391 // Add a newline and indent past the index
3392 strm.Printf ("\n%*s", indent, "");
3393 }
3394 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3395 dump_object_name = true;
3396 break;
3397 }
3398 }
3399 strm.Printf("%.*s", width, "<NONE>");
3400 }
3401 break;
3402
3403 case 'm':
3404 module->GetModificationTime().Dump(&strm, width);
3405 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003406
Greg Claytonb5a8f142012-02-05 02:38:54 +00003407 case 'p':
3408 strm.Printf("%p", module);
3409 break;
3410
3411 case 'u':
3412 DumpModuleUUID(strm, module);
3413 break;
3414
3415 default:
3416 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003417 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003418
3419 }
3420 if (dump_object_name)
3421 {
3422 const char *object_name = module->GetObjectName().GetCString();
3423 if (object_name)
3424 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003425 }
3426 strm.EOL();
3427 }
3428
Greg Claytone1f50b92011-05-03 22:09:39 +00003429 CommandOptions m_options;
3430};
3431
3432OptionDefinition
3433CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3434{
Jim Ingham6bdea822011-10-24 18:36:33 +00003435 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3436 { 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 +00003437 { 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 +00003438 { 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."},
3439 { 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 +00003440 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3441 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3442 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3443 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3444 { 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 +00003445 { 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 +00003446 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3447 { 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."},
3448 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003449 { 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 +00003450 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3451};
3452
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003453#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003454
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003455//----------------------------------------------------------------------
3456// Lookup unwind information in images
3457//----------------------------------------------------------------------
3458
3459class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3460{
3461public:
3462
3463 enum
3464 {
3465 eLookupTypeInvalid = -1,
3466 eLookupTypeAddress = 0,
3467 eLookupTypeSymbol,
3468 eLookupTypeFunction,
3469 eLookupTypeFunctionOrSymbol,
3470 kNumLookupTypes
3471 };
3472
3473 class CommandOptions : public Options
3474 {
3475 public:
3476
3477 CommandOptions (CommandInterpreter &interpreter) :
3478 Options(interpreter),
3479 m_type(eLookupTypeInvalid),
3480 m_str(),
3481 m_addr(LLDB_INVALID_ADDRESS)
3482 {
3483 }
3484
3485 virtual
3486 ~CommandOptions ()
3487 {
3488 }
3489
3490 virtual Error
3491 SetOptionValue (uint32_t option_idx, const char *option_arg)
3492 {
3493 Error error;
3494
Greg Clayton6475c422012-12-04 00:32:51 +00003495 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003496
3497 switch (short_option)
3498 {
3499 case 'a':
3500 m_type = eLookupTypeAddress;
3501 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3502 if (m_addr == LLDB_INVALID_ADDRESS)
3503 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3504 break;
3505
3506 case 'n':
3507 m_str = option_arg;
3508 m_type = eLookupTypeFunctionOrSymbol;
3509 break;
3510 }
3511
3512 return error;
3513 }
3514
3515 void
3516 OptionParsingStarting ()
3517 {
3518 m_type = eLookupTypeInvalid;
3519 m_str.clear();
3520 m_addr = LLDB_INVALID_ADDRESS;
3521 }
3522
3523 const OptionDefinition*
3524 GetDefinitions ()
3525 {
3526 return g_option_table;
3527 }
3528
3529 // Options table: Required for subclasses of Options.
3530
3531 static OptionDefinition g_option_table[];
3532
3533 // Instance variables to hold the values for command options.
3534
3535 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3536 std::string m_str; // Holds name lookup
3537 lldb::addr_t m_addr; // Holds the address to lookup
3538 };
3539
3540 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3541 CommandObjectParsed (interpreter,
3542 "target modules show-unwind",
3543 "Show synthesized unwind instructions for a function.",
3544 NULL),
3545 m_options (interpreter)
3546 {
3547 }
3548
3549 virtual
3550 ~CommandObjectTargetModulesShowUnwind ()
3551 {
3552 }
3553
3554 virtual
3555 Options *
3556 GetOptions ()
3557 {
3558 return &m_options;
3559 }
3560
3561protected:
3562 bool
3563 DoExecute (Args& command,
3564 CommandReturnObject &result)
3565 {
3566 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3567 if (!target)
3568 {
3569 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3570 result.SetStatus (eReturnStatusFailed);
3571 return false;
3572 }
3573
3574 ExecutionContext exe_ctx = m_interpreter.GetDebugger().GetSelectedExecutionContext();
3575 Process *process = exe_ctx.GetProcessPtr();
3576 ABI *abi = NULL;
3577 if (process)
3578 abi = process->GetABI().get();
3579
3580 if (process == NULL)
3581 {
3582 result.AppendError ("You must have a process running to use this command.");
3583 result.SetStatus (eReturnStatusFailed);
3584 return false;
3585 }
3586
3587 ThreadList threads(process->GetThreadList());
3588 if (threads.GetSize() == 0)
3589 {
3590 result.AppendError ("The process must be paused to use this command.");
3591 result.SetStatus (eReturnStatusFailed);
3592 return false;
3593 }
3594
3595 ThreadSP thread(threads.GetThreadAtIndex(0));
3596 if (thread.get() == NULL)
3597 {
3598 result.AppendError ("The process must be paused to use this command.");
3599 result.SetStatus (eReturnStatusFailed);
3600 return false;
3601 }
3602
3603 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3604 {
3605 SymbolContextList sc_list;
3606 uint32_t num_matches;
3607 ConstString function_name (m_options.m_str.c_str());
3608 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3609 for (uint32_t idx = 0; idx < num_matches; idx++)
3610 {
3611 SymbolContext sc;
3612 sc_list.GetContextAtIndex(idx, sc);
3613 if (sc.symbol == NULL && sc.function == NULL)
3614 continue;
3615 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3616 continue;
3617 AddressRange range;
3618 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3619 continue;
3620 if (!range.GetBaseAddress().IsValid())
3621 continue;
3622 ConstString funcname(sc.GetFunctionName());
3623 if (funcname.IsEmpty())
3624 continue;
3625 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3626 if (abi)
3627 start_addr = abi->FixCodeAddress(start_addr);
3628
3629 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3630 if (func_unwinders_sp.get() == NULL)
3631 continue;
3632
3633 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3634 if (first_non_prologue_insn.IsValid())
3635 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003636 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 +00003637 result.GetOutputStream().Printf ("\n");
3638 }
3639
3640 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3641 if (non_callsite_unwind_plan.get())
3642 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003643 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003644 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3645 result.GetOutputStream().Printf ("\n");
3646 }
3647
3648 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3649 if (callsite_unwind_plan.get())
3650 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003651 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003652 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3653 result.GetOutputStream().Printf ("\n");
3654 }
3655
3656 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3657 if (arch_default_unwind_plan.get())
3658 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003659 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003660 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3661 result.GetOutputStream().Printf ("\n");
3662 }
3663
3664 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3665 if (fast_unwind_plan.get())
3666 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003667 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003668 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3669 result.GetOutputStream().Printf ("\n");
3670 }
3671
3672
3673 result.GetOutputStream().Printf ("\n");
3674 }
3675 }
3676 return result.Succeeded();
3677 }
3678
3679 CommandOptions m_options;
3680};
3681
3682OptionDefinition
3683CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3684{
3685 { LLDB_OPT_SET_1, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function or symbol by name in one or more target modules."},
3686 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3687};
Greg Claytone1f50b92011-05-03 22:09:39 +00003688
3689//----------------------------------------------------------------------
3690// Lookup information in images
3691//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003692class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003693{
3694public:
3695
3696 enum
3697 {
3698 eLookupTypeInvalid = -1,
3699 eLookupTypeAddress = 0,
3700 eLookupTypeSymbol,
3701 eLookupTypeFileLine, // Line is optional
3702 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003703 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003704 eLookupTypeType,
3705 kNumLookupTypes
3706 };
3707
3708 class CommandOptions : public Options
3709 {
3710 public:
3711
3712 CommandOptions (CommandInterpreter &interpreter) :
3713 Options(interpreter)
3714 {
3715 OptionParsingStarting();
3716 }
3717
3718 virtual
3719 ~CommandOptions ()
3720 {
3721 }
3722
3723 virtual Error
3724 SetOptionValue (uint32_t option_idx, const char *option_arg)
3725 {
3726 Error error;
3727
Greg Clayton6475c422012-12-04 00:32:51 +00003728 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone1f50b92011-05-03 22:09:39 +00003729
3730 switch (short_option)
3731 {
3732 case 'a':
3733 m_type = eLookupTypeAddress;
3734 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3735 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003736 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003737 break;
3738
3739 case 'o':
3740 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3741 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003742 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003743 break;
3744
3745 case 's':
3746 m_str = option_arg;
3747 m_type = eLookupTypeSymbol;
3748 break;
3749
3750 case 'f':
3751 m_file.SetFile (option_arg, false);
3752 m_type = eLookupTypeFileLine;
3753 break;
3754
3755 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003756 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003757 break;
3758
3759 case 'l':
3760 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3761 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003762 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003763 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003764 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003765 m_type = eLookupTypeFileLine;
3766 break;
3767
Greg Clayton2ad894b2012-05-15 18:43:44 +00003768 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003769 m_str = option_arg;
3770 m_type = eLookupTypeFunction;
3771 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003772
3773 case 'n':
3774 m_str = option_arg;
3775 m_type = eLookupTypeFunctionOrSymbol;
3776 break;
3777
Greg Claytone1f50b92011-05-03 22:09:39 +00003778 case 't':
3779 m_str = option_arg;
3780 m_type = eLookupTypeType;
3781 break;
3782
3783 case 'v':
3784 m_verbose = 1;
3785 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003786
3787 case 'A':
3788 m_print_all = true;
3789 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003790
3791 case 'r':
3792 m_use_regex = true;
3793 break;
3794 }
3795
3796 return error;
3797 }
3798
3799 void
3800 OptionParsingStarting ()
3801 {
3802 m_type = eLookupTypeInvalid;
3803 m_str.clear();
3804 m_file.Clear();
3805 m_addr = LLDB_INVALID_ADDRESS;
3806 m_offset = 0;
3807 m_line_number = 0;
3808 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003809 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003810 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003811 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003812 }
3813
3814 const OptionDefinition*
3815 GetDefinitions ()
3816 {
3817 return g_option_table;
3818 }
3819
3820 // Options table: Required for subclasses of Options.
3821
3822 static OptionDefinition g_option_table[];
3823 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3824 std::string m_str; // Holds name lookup
3825 FileSpec m_file; // Files for file lookups
3826 lldb::addr_t m_addr; // Holds the address to lookup
3827 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3828 uint32_t m_line_number; // Line number for file+line lookups
3829 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003830 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003831 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003832 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003833
3834 };
3835
3836 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003837 CommandObjectParsed (interpreter,
3838 "target modules lookup",
3839 "Look up information within executable and dependent shared library images.",
3840 NULL),
3841 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003842 {
3843 CommandArgumentEntry arg;
3844 CommandArgumentData file_arg;
3845
3846 // Define the first (and only) variant of this arg.
3847 file_arg.arg_type = eArgTypeFilename;
3848 file_arg.arg_repetition = eArgRepeatStar;
3849
3850 // There is only one variant this argument could be; put it into the argument entry.
3851 arg.push_back (file_arg);
3852
3853 // Push the data for the first argument into the m_arguments vector.
3854 m_arguments.push_back (arg);
3855 }
3856
3857 virtual
3858 ~CommandObjectTargetModulesLookup ()
3859 {
3860 }
3861
3862 virtual Options *
3863 GetOptions ()
3864 {
3865 return &m_options;
3866 }
3867
Sean Callanan56d31ec2012-06-06 20:49:55 +00003868 bool
3869 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3870 {
3871 switch (m_options.m_type)
3872 {
3873 case eLookupTypeAddress:
3874 case eLookupTypeFileLine:
3875 case eLookupTypeFunction:
3876 case eLookupTypeFunctionOrSymbol:
3877 case eLookupTypeSymbol:
3878 default:
3879 return false;
3880 case eLookupTypeType:
3881 break;
3882 }
3883
3884 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3885
3886 StackFrameSP frame = exe_ctx.GetFrameSP();
3887
3888 if (!frame)
3889 return false;
3890
3891 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3892
3893 if (!sym_ctx.module_sp)
3894 return false;
3895
3896 switch (m_options.m_type)
3897 {
3898 default:
3899 return false;
3900 case eLookupTypeType:
3901 if (!m_options.m_str.empty())
3902 {
3903 if (LookupTypeHere (m_interpreter,
3904 result.GetOutputStream(),
3905 sym_ctx,
3906 m_options.m_str.c_str(),
3907 m_options.m_use_regex))
3908 {
3909 result.SetStatus(eReturnStatusSuccessFinishResult);
3910 return true;
3911 }
3912 }
3913 break;
3914 }
3915
3916 return true;
3917 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003918
3919 bool
3920 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3921 {
3922 switch (m_options.m_type)
3923 {
3924 case eLookupTypeAddress:
3925 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3926 {
3927 if (LookupAddressInModule (m_interpreter,
3928 result.GetOutputStream(),
3929 module,
3930 eSymbolContextEverything,
3931 m_options.m_addr,
3932 m_options.m_offset,
3933 m_options.m_verbose))
3934 {
3935 result.SetStatus(eReturnStatusSuccessFinishResult);
3936 return true;
3937 }
3938 }
3939 break;
3940
3941 case eLookupTypeSymbol:
3942 if (!m_options.m_str.empty())
3943 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003944 if (LookupSymbolInModule (m_interpreter,
3945 result.GetOutputStream(),
3946 module,
3947 m_options.m_str.c_str(),
3948 m_options.m_use_regex,
3949 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003950 {
3951 result.SetStatus(eReturnStatusSuccessFinishResult);
3952 return true;
3953 }
3954 }
3955 break;
3956
3957 case eLookupTypeFileLine:
3958 if (m_options.m_file)
3959 {
3960
3961 if (LookupFileAndLineInModule (m_interpreter,
3962 result.GetOutputStream(),
3963 module,
3964 m_options.m_file,
3965 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003966 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003967 m_options.m_verbose))
3968 {
3969 result.SetStatus(eReturnStatusSuccessFinishResult);
3970 return true;
3971 }
3972 }
3973 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003974
3975 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003976 case eLookupTypeFunction:
3977 if (!m_options.m_str.empty())
3978 {
3979 if (LookupFunctionInModule (m_interpreter,
3980 result.GetOutputStream(),
3981 module,
3982 m_options.m_str.c_str(),
3983 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003984 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003985 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003986 m_options.m_verbose))
3987 {
3988 result.SetStatus(eReturnStatusSuccessFinishResult);
3989 return true;
3990 }
3991 }
3992 break;
3993
Greg Clayton2ad894b2012-05-15 18:43:44 +00003994
Greg Claytone1f50b92011-05-03 22:09:39 +00003995 case eLookupTypeType:
3996 if (!m_options.m_str.empty())
3997 {
3998 if (LookupTypeInModule (m_interpreter,
3999 result.GetOutputStream(),
4000 module,
4001 m_options.m_str.c_str(),
4002 m_options.m_use_regex))
4003 {
4004 result.SetStatus(eReturnStatusSuccessFinishResult);
4005 return true;
4006 }
4007 }
4008 break;
4009
4010 default:
4011 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4012 syntax_error = true;
4013 break;
4014 }
4015
4016 result.SetStatus (eReturnStatusFailed);
4017 return false;
4018 }
4019
Jim Inghamda26bd22012-06-08 21:56:10 +00004020protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00004021 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004022 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00004023 CommandReturnObject &result)
4024 {
4025 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4026 if (target == NULL)
4027 {
4028 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4029 result.SetStatus (eReturnStatusFailed);
4030 return false;
4031 }
4032 else
4033 {
4034 bool syntax_error = false;
4035 uint32_t i;
4036 uint32_t num_successful_lookups = 0;
4037 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4038 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4039 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4040 // Dump all sections for all modules images
4041
4042 if (command.GetArgumentCount() == 0)
4043 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004044 ModuleSP current_module;
4045
4046 // Where it is possible to look in the current symbol context
4047 // first, try that. If this search was successful and --all
4048 // was not passed, don't print anything else.
4049 if (LookupHere (m_interpreter, result, syntax_error))
4050 {
4051 result.GetOutputStream().EOL();
4052 num_successful_lookups++;
4053 if (!m_options.m_print_all)
4054 {
4055 result.SetStatus (eReturnStatusSuccessFinishResult);
4056 return result.Succeeded();
4057 }
4058 }
4059
4060 // Dump all sections for all other modules
4061
Enrico Granata146d9522012-11-08 02:22:02 +00004062 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00004063 Mutex::Locker modules_locker(target_modules.GetMutex());
4064 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00004065 if (num_modules > 0)
4066 {
4067 for (i = 0; i<num_modules && syntax_error == false; ++i)
4068 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004069 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
4070
4071 if (module_pointer != current_module.get() &&
4072 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004073 {
4074 result.GetOutputStream().EOL();
4075 num_successful_lookups++;
4076 }
4077 }
4078 }
4079 else
4080 {
4081 result.AppendError ("the target has no associated executable images");
4082 result.SetStatus (eReturnStatusFailed);
4083 return false;
4084 }
4085 }
4086 else
4087 {
4088 // Dump specified images (by basename or fullpath)
4089 const char *arg_cstr;
4090 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4091 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004092 ModuleList module_list;
4093 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4094 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00004095 {
Jason Molendabf41e192012-10-04 22:47:07 +00004096 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00004097 {
Jason Molendabf41e192012-10-04 22:47:07 +00004098 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00004099 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00004100 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004101 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004102 {
4103 result.GetOutputStream().EOL();
4104 num_successful_lookups++;
4105 }
4106 }
4107 }
4108 }
4109 else
4110 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4111 }
4112 }
4113
4114 if (num_successful_lookups > 0)
4115 result.SetStatus (eReturnStatusSuccessFinishResult);
4116 else
4117 result.SetStatus (eReturnStatusFailed);
4118 }
4119 return result.Succeeded();
4120 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004121
4122 CommandOptions m_options;
4123};
4124
4125OptionDefinition
4126CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4127{
Sean Callanan3bfaad62012-09-13 21:11:40 +00004128 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
4129 { 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 +00004130 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4131 /* 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 +00004132 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4133 { 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."},
4134 { 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."},
4135 { 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 +00004136 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00004137 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4138 { 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."},
4139 { 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."},
4140 { 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."},
4141 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4142 { 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."},
4143 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00004144};
Chris Lattner24943d22010-06-08 16:52:24 +00004145
4146
Jim Inghamd60d94a2011-03-11 03:53:59 +00004147#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00004148
4149//-------------------------------------------------------------------------
4150// CommandObjectMultiwordImageSearchPaths
4151//-------------------------------------------------------------------------
4152
Greg Claytone1f50b92011-05-03 22:09:39 +00004153class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00004154{
4155public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004156
4157 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4158 CommandObjectMultiword (interpreter,
4159 "target modules search-paths",
4160 "A set of commands for operating on debugger target image search paths.",
4161 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004162 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004163 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4164 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4165 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4166 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4167 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004168 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004169
4170 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004171 {
4172 }
4173};
4174
Greg Claytone1f50b92011-05-03 22:09:39 +00004175
4176
4177#pragma mark CommandObjectTargetModules
4178
4179//-------------------------------------------------------------------------
4180// CommandObjectTargetModules
4181//-------------------------------------------------------------------------
4182
4183class CommandObjectTargetModules : public CommandObjectMultiword
4184{
4185public:
4186 //------------------------------------------------------------------
4187 // Constructors and Destructors
4188 //------------------------------------------------------------------
4189 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4190 CommandObjectMultiword (interpreter,
4191 "target modules",
4192 "A set of commands for accessing information for one or more target modules.",
4193 "target modules <sub-command> ...")
4194 {
4195 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4196 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
4197 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
4198 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4199 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4200 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4201 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004202 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004203
4204 }
4205 virtual
4206 ~CommandObjectTargetModules()
4207 {
4208 }
4209
4210private:
4211 //------------------------------------------------------------------
4212 // For CommandObjectTargetModules only
4213 //------------------------------------------------------------------
4214 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4215};
4216
4217
Greg Clayton3508c382012-02-24 01:59:29 +00004218
Jim Inghamda26bd22012-06-08 21:56:10 +00004219class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004220{
4221public:
4222 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004223 CommandObjectParsed (interpreter,
4224 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004225 "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.",
4226 "target symbols add [<symfile>]"),
4227 m_option_group (interpreter),
4228 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4229 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4230
Greg Clayton3508c382012-02-24 01:59:29 +00004231 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004232 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4233 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4234 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4235 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004236 }
4237
4238 virtual
4239 ~CommandObjectTargetSymbolsAdd ()
4240 {
4241 }
4242
Jim Inghamda26bd22012-06-08 21:56:10 +00004243 int
4244 HandleArgumentCompletion (Args &input,
4245 int &cursor_index,
4246 int &cursor_char_position,
4247 OptionElementVector &opt_element_vector,
4248 int match_start_point,
4249 int max_return_elements,
4250 bool &word_complete,
4251 StringList &matches)
4252 {
4253 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4254 completion_str.erase (cursor_char_position);
4255
4256 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4257 CommandCompletions::eDiskFileCompletion,
4258 completion_str.c_str(),
4259 match_start_point,
4260 max_return_elements,
4261 NULL,
4262 word_complete,
4263 matches);
4264 return matches.GetSize();
4265 }
4266
Greg Clayton437b5bc2012-09-27 22:26:11 +00004267 virtual Options *
4268 GetOptions ()
4269 {
4270 return &m_option_group;
4271 }
4272
4273
Jim Inghamda26bd22012-06-08 21:56:10 +00004274protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004275
4276 bool
4277 AddModuleSymbols (Target *target,
Greg Claytonc92fded2012-12-12 01:15:30 +00004278 ModuleSpec &module_spec,
Greg Clayton437b5bc2012-09-27 22:26:11 +00004279 bool &flush,
4280 CommandReturnObject &result)
4281 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004282 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4283 if (symbol_fspec)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004284 {
4285 char symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004286 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
4287
4288 if (!module_spec.GetUUID().IsValid())
4289 {
4290 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4291 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4292 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004293 // We now have a module that represents a symbol file
4294 // that can be used for a module that might exist in the
4295 // current target, so we need to find that module in the
4296 // target
Greg Claytonc92fded2012-12-12 01:15:30 +00004297 ModuleList matching_module_list;
4298 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4299 if (num_matches > 1)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004300 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004301 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4302 }
4303 else if (num_matches == 1)
4304 {
4305 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
4306
Greg Clayton437b5bc2012-09-27 22:26:11 +00004307 // The module has not yet created its symbol vendor, we can just
4308 // give the existing target module the symfile path to use for
4309 // when it decides to create it!
Greg Claytonc92fded2012-12-12 01:15:30 +00004310 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004311
Greg Claytonc92fded2012-12-12 01:15:30 +00004312 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
4313 if (symbol_vendor)
4314 {
4315 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4316
4317 if (symbol_file)
4318 {
4319 ObjectFile *object_file = symbol_file->GetObjectFile();
4320
4321 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4322 {
4323 // Provide feedback that the symfile has been successfully added.
4324 const FileSpec &module_fs = module_sp->GetFileSpec();
4325 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s/%s'\n",
4326 symfile_path,
4327 module_fs.GetDirectory().AsCString(),
4328 module_fs.GetFilename().AsCString());
4329
4330 // Let clients know something changed in the module
4331 // if it is currently loaded
4332 ModuleList module_list;
4333 module_list.Append (module_sp);
4334 target->ModulesDidLoad (module_list);
4335 flush = true;
4336 result.SetStatus (eReturnStatusSuccessFinishResult);
4337 return true;
4338 }
4339 }
4340 }
4341 // Clear the symbol file spec if anything went wrong
4342 module_sp->SetSymbolFileFileSpec (FileSpec());
4343
4344 }
4345
4346 if (module_spec.GetUUID().IsValid())
4347 {
4348 StreamString ss_symfile_uuid;
4349 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4350 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4351 symfile_path,
4352 ss_symfile_uuid.GetData(),
4353 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4354 ? "\n please specify the full path to the symbol file"
4355 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004356 }
4357 else
4358 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004359 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4360 symfile_path,
4361 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4362 ? "\n please specify the full path to the symbol file"
4363 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004364 }
4365 }
4366 else
4367 {
4368 result.AppendError ("one or more executable image paths must be specified");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004369 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004370 result.SetStatus (eReturnStatusFailed);
4371 return false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004372 }
4373
Greg Clayton3508c382012-02-24 01:59:29 +00004374 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004375 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004376 CommandReturnObject &result)
4377 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004378 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
4379 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004380 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004381 if (target == NULL)
4382 {
4383 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Greg Clayton3508c382012-02-24 01:59:29 +00004384 }
4385 else
4386 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004387 bool flush = false;
Greg Claytonc92fded2012-12-12 01:15:30 +00004388 ModuleSpec module_spec;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004389 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4390 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4391 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
4392
Greg Clayton3508c382012-02-24 01:59:29 +00004393 const size_t argc = args.GetArgumentCount();
4394 if (argc == 0)
4395 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004396 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004397 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004398 bool success = false;
4399 bool error_set = false;
4400 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004401 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004402 Process *process = exe_ctx.GetProcessPtr();
4403 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004404 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004405 const StateType process_state = process->GetState();
4406 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004407 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004408 StackFrame *frame = exe_ctx.GetFramePtr();
4409 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004410 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004411 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4412 if (frame_module_sp)
4413 {
4414 if (frame_module_sp->GetPlatformFileSpec().Exists())
4415 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004416 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4417 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004418 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004419 module_spec.GetUUID() = frame_module_sp->GetUUID();
4420 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004421 }
4422 else
4423 {
4424 result.AppendError ("frame has no module");
4425 error_set = true;
4426 }
Greg Clayton3508c382012-02-24 01:59:29 +00004427 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004428 else
4429 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004430 result.AppendError ("invalid current frame");
4431 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004432 }
Greg Clayton3508c382012-02-24 01:59:29 +00004433 }
4434 else
4435 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004436 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
4437 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004438 }
Greg Clayton3508c382012-02-24 01:59:29 +00004439 }
4440 else
4441 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004442 result.AppendError ("a process must exist in order to use the --frame option");
4443 error_set = true;
4444 }
4445 }
4446 else
4447 {
4448 if (uuid_option_set)
4449 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004450 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4451 success |= module_spec.GetUUID().IsValid();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004452 }
4453 else if (file_option_set)
4454 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004455 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4456 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
Greg Clayton437b5bc2012-09-27 22:26:11 +00004457 if (module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004458 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004459 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4460 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4461 module_spec.GetUUID() = module_sp->GetUUID();
4462 module_spec.GetArchitecture() = module_sp->GetArchitecture();
Greg Clayton3508c382012-02-24 01:59:29 +00004463 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004464 else
4465 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004466 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004467 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004468 success |= module_spec.GetFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004469 }
4470 }
4471
4472 if (success)
4473 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004474 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004475 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004476 if (module_spec.GetSymbolFileSpec())
4477 success = AddModuleSymbols (target, module_spec, flush, result);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004478 }
4479 }
4480
4481 if (!success && !error_set)
4482 {
4483 StreamString error_strm;
4484 if (uuid_option_set)
4485 {
4486 error_strm.PutCString("unable to find debug symbols for UUID ");
Greg Claytonc92fded2012-12-12 01:15:30 +00004487 module_spec.GetUUID().Dump (&error_strm);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004488 }
4489 else if (file_option_set)
4490 {
4491 error_strm.PutCString("unable to find debug symbols for the executable file ");
Greg Claytonc92fded2012-12-12 01:15:30 +00004492 error_strm << module_spec.GetFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004493 }
4494 else if (frame_option_set)
4495 {
4496 error_strm.PutCString("unable to find debug symbols for the current frame");
4497 }
4498 result.AppendError (error_strm.GetData());
4499 }
4500 }
4501 else
4502 {
4503 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4504 }
4505 }
4506 else
4507 {
4508 if (uuid_option_set)
4509 {
4510 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4511 }
4512 else if (file_option_set)
4513 {
4514 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4515 }
4516 else if (frame_option_set)
4517 {
4518 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4519 }
4520 else
4521 {
4522 PlatformSP platform_sp (target->GetPlatform());
4523
4524 for (size_t i=0; i<argc; ++i)
4525 {
4526 const char *symfile_path = args.GetArgumentAtIndex(i);
4527 if (symfile_path)
4528 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004529 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004530 if (platform_sp)
Greg Claytonc92fded2012-12-12 01:15:30 +00004531 {
4532 FileSpec symfile_spec;
4533 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4534 module_spec.GetSymbolFileSpec() = symfile_spec;
4535 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004536
4537 ArchSpec arch;
Greg Claytonc92fded2012-12-12 01:15:30 +00004538 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004539
4540 if (symfile_exists)
4541 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004542 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004543 break;
4544 }
4545 else
4546 {
4547 char resolved_symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004548 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004549 {
4550 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4551 {
4552 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4553 break;
4554 }
4555 }
4556 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4557 break;
4558 }
Greg Clayton3508c382012-02-24 01:59:29 +00004559 }
4560 }
4561 }
4562 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004563
4564 if (flush)
4565 {
4566 Process *process = exe_ctx.GetProcessPtr();
4567 if (process)
4568 process->Flush();
4569 }
Greg Clayton3508c382012-02-24 01:59:29 +00004570 }
4571 return result.Succeeded();
4572 }
4573
Greg Clayton437b5bc2012-09-27 22:26:11 +00004574 OptionGroupOptions m_option_group;
4575 OptionGroupUUID m_uuid_option_group;
4576 OptionGroupFile m_file_option;
4577 OptionGroupBoolean m_current_frame_option;
4578
4579
Greg Clayton3508c382012-02-24 01:59:29 +00004580};
4581
4582
4583#pragma mark CommandObjectTargetSymbols
4584
4585//-------------------------------------------------------------------------
4586// CommandObjectTargetSymbols
4587//-------------------------------------------------------------------------
4588
4589class CommandObjectTargetSymbols : public CommandObjectMultiword
4590{
4591public:
4592 //------------------------------------------------------------------
4593 // Constructors and Destructors
4594 //------------------------------------------------------------------
4595 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4596 CommandObjectMultiword (interpreter,
4597 "target symbols",
4598 "A set of commands for adding and managing debug symbol files.",
4599 "target symbols <sub-command> ...")
4600 {
4601 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4602
4603 }
4604 virtual
4605 ~CommandObjectTargetSymbols()
4606 {
4607 }
4608
4609private:
4610 //------------------------------------------------------------------
4611 // For CommandObjectTargetModules only
4612 //------------------------------------------------------------------
4613 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4614};
4615
4616
Jim Inghamd60d94a2011-03-11 03:53:59 +00004617#pragma mark CommandObjectTargetStopHookAdd
4618
4619//-------------------------------------------------------------------------
4620// CommandObjectTargetStopHookAdd
4621//-------------------------------------------------------------------------
4622
Jim Inghamda26bd22012-06-08 21:56:10 +00004623class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004624{
4625public:
4626
4627 class CommandOptions : public Options
4628 {
4629 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004630 CommandOptions (CommandInterpreter &interpreter) :
4631 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004632 m_line_start(0),
4633 m_line_end (UINT_MAX),
4634 m_func_name_type_mask (eFunctionNameTypeAuto),
4635 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004636 m_thread_specified (false),
4637 m_use_one_liner (false),
4638 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004639 {
4640 }
4641
4642 ~CommandOptions () {}
4643
Greg Claytonb3448432011-03-24 21:19:54 +00004644 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004645 GetDefinitions ()
4646 {
4647 return g_option_table;
4648 }
4649
4650 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004651 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004652 {
4653 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00004654 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004655 bool success;
4656
4657 switch (short_option)
4658 {
4659 case 'c':
4660 m_class_name = option_arg;
4661 m_sym_ctx_specified = true;
4662 break;
4663
4664 case 'e':
4665 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4666 if (!success)
4667 {
Greg Clayton9c236732011-10-26 00:56:27 +00004668 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004669 break;
4670 }
4671 m_sym_ctx_specified = true;
4672 break;
4673
4674 case 'l':
4675 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4676 if (!success)
4677 {
Greg Clayton9c236732011-10-26 00:56:27 +00004678 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004679 break;
4680 }
4681 m_sym_ctx_specified = true;
4682 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004683
4684 case 'i':
4685 m_no_inlines = true;
4686 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004687
4688 case 'n':
4689 m_function_name = option_arg;
4690 m_func_name_type_mask |= eFunctionNameTypeAuto;
4691 m_sym_ctx_specified = true;
4692 break;
4693
4694 case 'f':
4695 m_file_name = option_arg;
4696 m_sym_ctx_specified = true;
4697 break;
4698 case 's':
4699 m_module_name = option_arg;
4700 m_sym_ctx_specified = true;
4701 break;
4702 case 't' :
4703 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004704 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004705 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004706 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004707 m_thread_specified = true;
4708 }
4709 break;
4710 case 'T':
4711 m_thread_name = option_arg;
4712 m_thread_specified = true;
4713 break;
4714 case 'q':
4715 m_queue_name = option_arg;
4716 m_thread_specified = true;
4717 break;
4718 case 'x':
4719 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004720 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004721 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004722 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004723 m_thread_specified = true;
4724 }
4725 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004726 case 'o':
4727 m_use_one_liner = true;
4728 m_one_liner = option_arg;
4729 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004730 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004731 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004732 break;
4733 }
4734 return error;
4735 }
4736
4737 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004738 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004739 {
4740 m_class_name.clear();
4741 m_function_name.clear();
4742 m_line_start = 0;
4743 m_line_end = UINT_MAX;
4744 m_file_name.clear();
4745 m_module_name.clear();
4746 m_func_name_type_mask = eFunctionNameTypeAuto;
4747 m_thread_id = LLDB_INVALID_THREAD_ID;
4748 m_thread_index = UINT32_MAX;
4749 m_thread_name.clear();
4750 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004751
4752 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004753 m_sym_ctx_specified = false;
4754 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004755
4756 m_use_one_liner = false;
4757 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004758 }
4759
4760
Greg Claytonb3448432011-03-24 21:19:54 +00004761 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004762
4763 std::string m_class_name;
4764 std::string m_function_name;
4765 uint32_t m_line_start;
4766 uint32_t m_line_end;
4767 std::string m_file_name;
4768 std::string m_module_name;
4769 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4770 lldb::tid_t m_thread_id;
4771 uint32_t m_thread_index;
4772 std::string m_thread_name;
4773 std::string m_queue_name;
4774 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004775 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004776 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004777 // Instance variables to hold the values for one_liner options.
4778 bool m_use_one_liner;
4779 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004780 };
4781
4782 Options *
4783 GetOptions ()
4784 {
4785 return &m_options;
4786 }
4787
4788 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004789 CommandObjectParsed (interpreter,
4790 "target stop-hook add ",
4791 "Add a hook to be executed when the target stops.",
4792 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004793 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004794 {
4795 }
4796
4797 ~CommandObjectTargetStopHookAdd ()
4798 {
4799 }
4800
4801 static size_t
4802 ReadCommandsCallbackFunction (void *baton,
4803 InputReader &reader,
4804 lldb::InputReaderAction notification,
4805 const char *bytes,
4806 size_t bytes_len)
4807 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004808 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004809 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004810 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004811 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004812
4813 switch (notification)
4814 {
4815 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004816 if (!batch_mode)
4817 {
4818 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4819 if (reader.GetPrompt())
4820 out_stream->Printf ("%s", reader.GetPrompt());
4821 out_stream->Flush();
4822 }
Jim Inghame15511a2011-05-05 01:03:36 +00004823 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004824 break;
4825
4826 case eInputReaderDeactivate:
4827 break;
4828
4829 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004830 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004831 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004832 out_stream->Printf ("%s", reader.GetPrompt());
4833 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004834 }
Jim Inghame15511a2011-05-05 01:03:36 +00004835 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004836 break;
4837
Caroline Tice4a348082011-05-02 20:41:46 +00004838 case eInputReaderAsynchronousOutputWritten:
4839 break;
4840
Jim Inghamd60d94a2011-03-11 03:53:59 +00004841 case eInputReaderGotToken:
4842 if (bytes && bytes_len && baton)
4843 {
4844 StringList *commands = new_stop_hook->GetCommandPointer();
4845 if (commands)
4846 {
4847 commands->AppendString (bytes, bytes_len);
4848 }
4849 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004850 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004851 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004852 out_stream->Printf ("%s", reader.GetPrompt());
4853 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004854 }
4855 break;
4856
4857 case eInputReaderInterrupt:
4858 {
4859 // Finish, and cancel the stop hook.
4860 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004861 if (!batch_mode)
4862 {
4863 out_stream->Printf ("Stop hook cancelled.\n");
4864 out_stream->Flush();
4865 }
4866
Jim Inghamd60d94a2011-03-11 03:53:59 +00004867 reader.SetIsDone (true);
4868 }
Jim Inghame15511a2011-05-05 01:03:36 +00004869 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004870 break;
4871
4872 case eInputReaderEndOfFile:
4873 reader.SetIsDone (true);
4874 break;
4875
4876 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004877 if (!got_interrupted && !batch_mode)
4878 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004879 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004880 out_stream->Flush();
4881 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004882 break;
4883 }
4884
4885 return bytes_len;
4886 }
4887
Jim Inghamda26bd22012-06-08 21:56:10 +00004888protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004889 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004890 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004891 {
4892 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4893 if (target)
4894 {
4895 Target::StopHookSP new_hook_sp;
4896 target->AddStopHook (new_hook_sp);
4897
4898 // First step, make the specifier.
4899 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4900 if (m_options.m_sym_ctx_specified)
4901 {
4902 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4903
4904 if (!m_options.m_module_name.empty())
4905 {
4906 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4907 }
4908
4909 if (!m_options.m_class_name.empty())
4910 {
4911 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4912 }
4913
4914 if (!m_options.m_file_name.empty())
4915 {
4916 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4917 }
4918
4919 if (m_options.m_line_start != 0)
4920 {
4921 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4922 }
4923
4924 if (m_options.m_line_end != UINT_MAX)
4925 {
4926 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4927 }
4928
4929 if (!m_options.m_function_name.empty())
4930 {
4931 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4932 }
4933 }
4934
4935 if (specifier_ap.get())
4936 new_hook_sp->SetSpecifier (specifier_ap.release());
4937
4938 // Next see if any of the thread options have been entered:
4939
4940 if (m_options.m_thread_specified)
4941 {
4942 ThreadSpec *thread_spec = new ThreadSpec();
4943
4944 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4945 {
4946 thread_spec->SetTID (m_options.m_thread_id);
4947 }
4948
4949 if (m_options.m_thread_index != UINT32_MAX)
4950 thread_spec->SetIndex (m_options.m_thread_index);
4951
4952 if (!m_options.m_thread_name.empty())
4953 thread_spec->SetName (m_options.m_thread_name.c_str());
4954
4955 if (!m_options.m_queue_name.empty())
4956 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4957
4958 new_hook_sp->SetThreadSpecifier (thread_spec);
4959
4960 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004961 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004962 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004963 // Use one-liner.
4964 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004965 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004966 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004967 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004968 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004969 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4970 // the new stop hook's command string.
4971 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4972 if (!reader_sp)
4973 {
4974 result.AppendError("out of memory\n");
4975 result.SetStatus (eReturnStatusFailed);
4976 target->RemoveStopHookByID (new_hook_sp->GetID());
4977 return false;
4978 }
4979
4980 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4981 new_hook_sp.get(), // baton
4982 eInputReaderGranularityLine, // token size, to pass to callback function
4983 "DONE", // end token
4984 "> ", // prompt
4985 true)); // echo input
4986 if (!err.Success())
4987 {
4988 result.AppendError (err.AsCString());
4989 result.SetStatus (eReturnStatusFailed);
4990 target->RemoveStopHookByID (new_hook_sp->GetID());
4991 return false;
4992 }
4993 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004994 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004995 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4996 }
4997 else
4998 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004999 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005000 result.SetStatus (eReturnStatusFailed);
5001 }
5002
5003 return result.Succeeded();
5004 }
5005private:
5006 CommandOptions m_options;
5007};
5008
Greg Claytonb3448432011-03-24 21:19:54 +00005009OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00005010CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5011{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005012 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00005013 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00005014 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
5015 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005016 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005017 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005018 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005019 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005020 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005021 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005022 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005023 "The stop hook is run only for threads in the queue whose name is given by this argument."},
5024 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
5025 "Specify the source file within which the stop-hook is to be run." },
5026 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
5027 "Set the start of the line range for which the stop-hook is to be run."},
5028 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
5029 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005030 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005031 "Specify the class within which the stop-hook is to be run." },
5032 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
5033 "Set the function name within which the stop hook will be run." },
5034 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5035};
5036
5037#pragma mark CommandObjectTargetStopHookDelete
5038
5039//-------------------------------------------------------------------------
5040// CommandObjectTargetStopHookDelete
5041//-------------------------------------------------------------------------
5042
Jim Inghamda26bd22012-06-08 21:56:10 +00005043class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005044{
5045public:
5046
5047 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005048 CommandObjectParsed (interpreter,
5049 "target stop-hook delete",
5050 "Delete a stop-hook.",
5051 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005052 {
5053 }
5054
5055 ~CommandObjectTargetStopHookDelete ()
5056 {
5057 }
5058
Jim Inghamda26bd22012-06-08 21:56:10 +00005059protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005060 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005061 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005062 {
5063 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5064 if (target)
5065 {
5066 // FIXME: see if we can use the breakpoint id style parser?
5067 size_t num_args = command.GetArgumentCount();
5068 if (num_args == 0)
5069 {
5070 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5071 {
5072 result.SetStatus (eReturnStatusFailed);
5073 return false;
5074 }
5075 else
5076 {
5077 target->RemoveAllStopHooks();
5078 }
5079 }
5080 else
5081 {
5082 bool success;
5083 for (size_t i = 0; i < num_args; i++)
5084 {
5085 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5086 if (!success)
5087 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005088 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005089 result.SetStatus(eReturnStatusFailed);
5090 return false;
5091 }
5092 success = target->RemoveStopHookByID (user_id);
5093 if (!success)
5094 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005095 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005096 result.SetStatus(eReturnStatusFailed);
5097 return false;
5098 }
5099 }
5100 }
5101 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5102 }
5103 else
5104 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005105 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005106 result.SetStatus (eReturnStatusFailed);
5107 }
5108
5109 return result.Succeeded();
5110 }
5111};
5112#pragma mark CommandObjectTargetStopHookEnableDisable
5113
5114//-------------------------------------------------------------------------
5115// CommandObjectTargetStopHookEnableDisable
5116//-------------------------------------------------------------------------
5117
Jim Inghamda26bd22012-06-08 21:56:10 +00005118class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005119{
5120public:
5121
5122 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005123 CommandObjectParsed (interpreter,
5124 name,
5125 help,
5126 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00005127 m_enable (enable)
5128 {
5129 }
5130
5131 ~CommandObjectTargetStopHookEnableDisable ()
5132 {
5133 }
5134
Jim Inghamda26bd22012-06-08 21:56:10 +00005135protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005136 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005137 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005138 {
5139 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5140 if (target)
5141 {
5142 // FIXME: see if we can use the breakpoint id style parser?
5143 size_t num_args = command.GetArgumentCount();
5144 bool success;
5145
5146 if (num_args == 0)
5147 {
5148 target->SetAllStopHooksActiveState (m_enable);
5149 }
5150 else
5151 {
5152 for (size_t i = 0; i < num_args; i++)
5153 {
5154 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5155 if (!success)
5156 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005157 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005158 result.SetStatus(eReturnStatusFailed);
5159 return false;
5160 }
5161 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5162 if (!success)
5163 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005164 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005165 result.SetStatus(eReturnStatusFailed);
5166 return false;
5167 }
5168 }
5169 }
5170 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5171 }
5172 else
5173 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005174 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005175 result.SetStatus (eReturnStatusFailed);
5176 }
5177 return result.Succeeded();
5178 }
5179private:
5180 bool m_enable;
5181};
5182
5183#pragma mark CommandObjectTargetStopHookList
5184
5185//-------------------------------------------------------------------------
5186// CommandObjectTargetStopHookList
5187//-------------------------------------------------------------------------
5188
Jim Inghamda26bd22012-06-08 21:56:10 +00005189class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005190{
5191public:
5192
5193 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005194 CommandObjectParsed (interpreter,
5195 "target stop-hook list",
5196 "List all stop-hooks.",
5197 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005198 {
5199 }
5200
5201 ~CommandObjectTargetStopHookList ()
5202 {
5203 }
5204
Jim Inghamda26bd22012-06-08 21:56:10 +00005205protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005206 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005207 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005208 {
5209 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005210 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005211 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005212 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005213 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005214 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005215 }
5216
5217 size_t num_hooks = target->GetNumStopHooks ();
5218 if (num_hooks == 0)
5219 {
5220 result.GetOutputStream().PutCString ("No stop hooks.\n");
5221 }
5222 else
5223 {
5224 for (size_t i = 0; i < num_hooks; i++)
5225 {
5226 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5227 if (i > 0)
5228 result.GetOutputStream().PutCString ("\n");
5229 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5230 }
5231 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005232 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005233 return result.Succeeded();
5234 }
5235};
5236
5237#pragma mark CommandObjectMultiwordTargetStopHooks
5238//-------------------------------------------------------------------------
5239// CommandObjectMultiwordTargetStopHooks
5240//-------------------------------------------------------------------------
5241
5242class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5243{
5244public:
5245
5246 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5247 CommandObjectMultiword (interpreter,
5248 "target stop-hook",
5249 "A set of commands for operating on debugger target stop-hooks.",
5250 "target stop-hook <subcommand> [<subcommand-options>]")
5251 {
5252 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5253 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5254 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5255 false,
5256 "target stop-hook disable [<id>]",
5257 "Disable a stop-hook.",
5258 "target stop-hook disable")));
5259 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5260 true,
5261 "target stop-hook enable [<id>]",
5262 "Enable a stop-hook.",
5263 "target stop-hook enable")));
5264 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5265 }
5266
5267 ~CommandObjectMultiwordTargetStopHooks()
5268 {
5269 }
5270};
5271
5272
Chris Lattner24943d22010-06-08 16:52:24 +00005273
5274#pragma mark CommandObjectMultiwordTarget
5275
5276//-------------------------------------------------------------------------
5277// CommandObjectMultiwordTarget
5278//-------------------------------------------------------------------------
5279
Greg Clayton63094e02010-06-23 01:19:29 +00005280CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005281 CommandObjectMultiword (interpreter,
5282 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005283 "A set of commands for operating on debugger targets.",
5284 "target <subcommand> [<subcommand-options>]")
5285{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005286
5287 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005288 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005289 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5290 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005291 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005292 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005293 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005294 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005295}
5296
5297CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5298{
5299}
5300
Greg Claytonabe0fed2011-04-18 08:33:37 +00005301