blob: 4286d9686c0729210756c55209dc5b74eb3dacb4 [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 {
2647 const size_t argc = args.GetArgumentCount();
2648 if (argc == 0)
2649 {
Greg Clayton1649a722012-11-29 22:16:27 +00002650 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2651 {
2652 // We are given a UUID only, go locate the file
2653 ModuleSpec module_spec;
2654 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002655 if (m_symbol_file.GetOptionValue().OptionWasSet())
2656 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002657 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2658 {
2659 ModuleSP module_sp (target->GetSharedModule (module_spec));
2660 if (module_sp)
2661 {
2662 result.SetStatus (eReturnStatusSuccessFinishResult);
2663 return true;
2664 }
2665 else
2666 {
2667 StreamString strm;
2668 module_spec.GetUUID().Dump (&strm);
2669 if (module_spec.GetFileSpec())
2670 {
2671 if (module_spec.GetSymbolFileSpec())
2672 {
2673 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s and symbol file %s/%s",
2674 strm.GetString().c_str(),
2675 module_spec.GetFileSpec().GetDirectory().GetCString(),
2676 module_spec.GetFileSpec().GetFilename().GetCString(),
2677 module_spec.GetSymbolFileSpec().GetDirectory().GetCString(),
2678 module_spec.GetSymbolFileSpec().GetFilename().GetCString());
2679 }
2680 else
2681 {
2682 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s/%s",
2683 strm.GetString().c_str(),
2684 module_spec.GetFileSpec().GetDirectory().GetCString(),
2685 module_spec.GetFileSpec().GetFilename().GetCString());
2686 }
2687 }
2688 else
2689 {
2690 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2691 strm.GetString().c_str());
2692 }
2693 result.SetStatus (eReturnStatusFailed);
2694 return false;
2695 }
2696 }
2697 else
2698 {
2699 StreamString strm;
2700 module_spec.GetUUID().Dump (&strm);
2701 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2702 result.SetStatus (eReturnStatusFailed);
2703 return false;
2704 }
2705 }
2706 else
2707 {
2708 result.AppendError ("one or more executable image paths must be specified");
2709 result.SetStatus (eReturnStatusFailed);
2710 return false;
2711 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002712 }
2713 else
2714 {
2715 for (size_t i=0; i<argc; ++i)
2716 {
2717 const char *path = args.GetArgumentAtIndex(i);
2718 if (path)
2719 {
2720 FileSpec file_spec(path, true);
Greg Claytone1f50b92011-05-03 22:09:39 +00002721 if (file_spec.Exists())
2722 {
Greg Clayton444fe992012-02-26 05:51:37 +00002723 ModuleSpec module_spec (file_spec);
Greg Clayton1649a722012-11-29 22:16:27 +00002724 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2725 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Claytonec9c2d22012-11-30 19:05:35 +00002726 if (m_symbol_file.GetOptionValue().OptionWasSet())
2727 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton1649a722012-11-29 22:16:27 +00002728 Error error;
2729 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytone1f50b92011-05-03 22:09:39 +00002730 if (!module_sp)
2731 {
Greg Clayton1649a722012-11-29 22:16:27 +00002732 const char *error_cstr = error.AsCString();
2733 if (error_cstr)
2734 result.AppendError (error_cstr);
2735 else
2736 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytone1f50b92011-05-03 22:09:39 +00002737 result.SetStatus (eReturnStatusFailed);
2738 return false;
2739 }
Jason Molenda36f6fb92011-08-02 23:28:55 +00002740 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytone1f50b92011-05-03 22:09:39 +00002741 }
2742 else
2743 {
2744 char resolved_path[PATH_MAX];
2745 result.SetStatus (eReturnStatusFailed);
2746 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2747 {
2748 if (strcmp (resolved_path, path) != 0)
2749 {
2750 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2751 break;
2752 }
2753 }
2754 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2755 break;
2756 }
2757 }
2758 }
2759 }
2760 }
2761 return result.Succeeded();
2762 }
2763
Greg Claytone1f50b92011-05-03 22:09:39 +00002764};
2765
2766class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2767{
2768public:
2769 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2770 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2771 "target modules load",
2772 "Set the load addresses for one or more sections in a target module.",
2773 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2774 m_option_group (interpreter),
Sean Callanan9a91ef62012-10-24 01:12:14 +00002775 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 +00002776 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)
2777 {
2778 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2779 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2780 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2781 m_option_group.Finalize();
2782 }
2783
2784 virtual
2785 ~CommandObjectTargetModulesLoad ()
2786 {
2787 }
2788
Jim Inghamda26bd22012-06-08 21:56:10 +00002789 virtual Options *
2790 GetOptions ()
2791 {
2792 return &m_option_group;
2793 }
2794
2795protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00002796 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00002797 DoExecute (Args& args,
Greg Claytone1f50b92011-05-03 22:09:39 +00002798 CommandReturnObject &result)
2799 {
2800 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2801 if (target == NULL)
2802 {
2803 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2804 result.SetStatus (eReturnStatusFailed);
2805 return false;
2806 }
2807 else
2808 {
2809 const size_t argc = args.GetArgumentCount();
Greg Clayton444fe992012-02-26 05:51:37 +00002810 ModuleSpec module_spec;
2811 bool search_using_module_spec = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002812 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002813 {
2814 search_using_module_spec = true;
2815 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2816 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002817
2818 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Clayton444fe992012-02-26 05:51:37 +00002819 {
2820 search_using_module_spec = true;
2821 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2822 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002823
Greg Clayton444fe992012-02-26 05:51:37 +00002824 if (search_using_module_spec)
Greg Claytone1f50b92011-05-03 22:09:39 +00002825 {
2826
2827 ModuleList matching_modules;
Greg Clayton444fe992012-02-26 05:51:37 +00002828 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002829
2830 char path[PATH_MAX];
2831 if (num_matches == 1)
2832 {
2833 Module *module = matching_modules.GetModulePointerAtIndex(0);
2834 if (module)
2835 {
2836 ObjectFile *objfile = module->GetObjectFile();
2837 if (objfile)
2838 {
2839 SectionList *section_list = objfile->GetSectionList();
2840 if (section_list)
2841 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002842 bool changed = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00002843 if (argc == 0)
2844 {
2845 if (m_slide_option.GetOptionValue().OptionWasSet())
2846 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002847 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
2848 module->SetLoadAddress (*target, slide, changed);
Greg Claytone1f50b92011-05-03 22:09:39 +00002849 }
2850 else
2851 {
2852 result.AppendError ("one or more section name + load address pair must be specified");
2853 result.SetStatus (eReturnStatusFailed);
2854 return false;
2855 }
2856 }
2857 else
2858 {
2859 if (m_slide_option.GetOptionValue().OptionWasSet())
2860 {
2861 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2862 result.SetStatus (eReturnStatusFailed);
2863 return false;
2864 }
2865
2866 for (size_t i=0; i<argc; i += 2)
2867 {
2868 const char *sect_name = args.GetArgumentAtIndex(i);
2869 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2870 if (sect_name && load_addr_cstr)
2871 {
2872 ConstString const_sect_name(sect_name);
2873 bool success = false;
2874 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2875 if (success)
2876 {
2877 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2878 if (section_sp)
2879 {
Greg Clayton9ab696e2012-03-27 21:10:07 +00002880 if (section_sp->IsThreadSpecific())
2881 {
2882 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2883 result.SetStatus (eReturnStatusFailed);
2884 break;
2885 }
2886 else
2887 {
Greg Clayton545762f2012-07-07 01:24:12 +00002888 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton9ab696e2012-03-27 21:10:07 +00002889 changed = true;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002890 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton9ab696e2012-03-27 21:10:07 +00002891 }
Greg Claytone1f50b92011-05-03 22:09:39 +00002892 }
2893 else
2894 {
2895 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2896 result.SetStatus (eReturnStatusFailed);
2897 break;
2898 }
2899 }
2900 else
2901 {
2902 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2903 result.SetStatus (eReturnStatusFailed);
2904 break;
2905 }
2906 }
2907 else
2908 {
2909 if (sect_name)
2910 result.AppendError ("section names must be followed by a load address.\n");
2911 else
2912 result.AppendError ("one or more section name + load address pair must be specified.\n");
2913 result.SetStatus (eReturnStatusFailed);
2914 break;
2915 }
2916 }
2917 }
Greg Clayton9ab696e2012-03-27 21:10:07 +00002918
2919 if (changed)
2920 target->ModulesDidLoad (matching_modules);
Greg Claytone1f50b92011-05-03 22:09:39 +00002921 }
2922 else
2923 {
2924 module->GetFileSpec().GetPath (path, sizeof(path));
2925 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
2926 result.SetStatus (eReturnStatusFailed);
2927 }
2928 }
2929 else
2930 {
2931 module->GetFileSpec().GetPath (path, sizeof(path));
2932 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
2933 result.SetStatus (eReturnStatusFailed);
2934 }
2935 }
2936 else
2937 {
Jim Ingham6f01c932012-10-12 17:34:26 +00002938 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2939 if (module_spec_file)
2940 {
2941 module_spec_file->GetPath (path, sizeof(path));
2942 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
2943 }
2944 else
2945 result.AppendError ("no module spec");
Greg Claytone1f50b92011-05-03 22:09:39 +00002946 result.SetStatus (eReturnStatusFailed);
2947 }
2948 }
2949 else
2950 {
2951 char uuid_cstr[64];
Greg Clayton444fe992012-02-26 05:51:37 +00002952
2953 if (module_spec.GetFileSpec())
2954 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytone1f50b92011-05-03 22:09:39 +00002955 else
2956 path[0] = '\0';
2957
Greg Clayton444fe992012-02-26 05:51:37 +00002958 if (module_spec.GetUUIDPtr())
2959 module_spec.GetUUID().GetAsCString(uuid_cstr, sizeof(uuid_cstr));
Greg Claytone1f50b92011-05-03 22:09:39 +00002960 else
2961 uuid_cstr[0] = '\0';
2962 if (num_matches > 1)
2963 {
2964 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
2965 path[0] ? " file=" : "",
2966 path,
2967 uuid_cstr[0] ? " uuid=" : "",
2968 uuid_cstr);
2969 for (size_t i=0; i<num_matches; ++i)
2970 {
2971 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
2972 result.AppendMessageWithFormat("%s\n", path);
2973 }
2974 }
2975 else
2976 {
2977 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
2978 path[0] ? " file=" : "",
2979 path,
2980 uuid_cstr[0] ? " uuid=" : "",
2981 uuid_cstr);
2982 }
2983 result.SetStatus (eReturnStatusFailed);
2984 }
2985 }
2986 else
2987 {
2988 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
2989 result.SetStatus (eReturnStatusFailed);
2990 return false;
2991 }
2992 }
2993 return result.Succeeded();
2994 }
2995
Greg Claytone1f50b92011-05-03 22:09:39 +00002996 OptionGroupOptions m_option_group;
2997 OptionGroupUUID m_uuid_option_group;
2998 OptionGroupFile m_file_option;
2999 OptionGroupUInt64 m_slide_option;
3000};
3001
3002//----------------------------------------------------------------------
3003// List images with associated information
3004//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003005class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003006{
3007public:
3008
3009 class CommandOptions : public Options
3010 {
3011 public:
3012
3013 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton899025f2011-08-09 00:01:09 +00003014 Options(interpreter),
Jim Ingham6bdea822011-10-24 18:36:33 +00003015 m_format_array(),
Daniel Dunbar97c89572011-10-31 22:50:49 +00003016 m_use_global_module_list (false),
Jim Ingham6bdea822011-10-24 18:36:33 +00003017 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytone1f50b92011-05-03 22:09:39 +00003018 {
3019 }
3020
3021 virtual
3022 ~CommandOptions ()
3023 {
3024 }
3025
3026 virtual Error
3027 SetOptionValue (uint32_t option_idx, const char *option_arg)
3028 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003029 Error error;
3030
Greg Clayton6475c422012-12-04 00:32:51 +00003031 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton899025f2011-08-09 00:01:09 +00003032 if (short_option == 'g')
3033 {
3034 m_use_global_module_list = true;
3035 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003036 else if (short_option == 'a')
3037 {
Greg Clayton49d888d2012-12-06 22:49:16 +00003038 m_module_addr = Args::StringToAddress(NULL, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Ingham6bdea822011-10-24 18:36:33 +00003039 }
Greg Clayton899025f2011-08-09 00:01:09 +00003040 else
3041 {
3042 uint32_t width = 0;
3043 if (option_arg)
3044 width = strtoul (option_arg, NULL, 0);
3045 m_format_array.push_back(std::make_pair(short_option, width));
3046 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003047 return error;
3048 }
3049
3050 void
3051 OptionParsingStarting ()
3052 {
3053 m_format_array.clear();
Greg Clayton899025f2011-08-09 00:01:09 +00003054 m_use_global_module_list = false;
Jim Ingham6bdea822011-10-24 18:36:33 +00003055 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytone1f50b92011-05-03 22:09:39 +00003056 }
3057
3058 const OptionDefinition*
3059 GetDefinitions ()
3060 {
3061 return g_option_table;
3062 }
3063
3064 // Options table: Required for subclasses of Options.
3065
3066 static OptionDefinition g_option_table[];
3067
3068 // Instance variables to hold the values for command options.
3069 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3070 FormatWidthCollection m_format_array;
Greg Clayton899025f2011-08-09 00:01:09 +00003071 bool m_use_global_module_list;
Jim Ingham6bdea822011-10-24 18:36:33 +00003072 lldb::addr_t m_module_addr;
Greg Claytone1f50b92011-05-03 22:09:39 +00003073 };
3074
3075 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003076 CommandObjectParsed (interpreter,
3077 "target modules list",
3078 "List current executable and dependent shared library images.",
3079 "target modules list [<cmd-options>]"),
Greg Claytone1f50b92011-05-03 22:09:39 +00003080 m_options (interpreter)
3081 {
3082 }
3083
3084 virtual
3085 ~CommandObjectTargetModulesList ()
3086 {
3087 }
3088
3089 virtual
3090 Options *
3091 GetOptions ()
3092 {
3093 return &m_options;
3094 }
3095
Jim Inghamda26bd22012-06-08 21:56:10 +00003096protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00003097 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00003098 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00003099 CommandReturnObject &result)
3100 {
3101 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton153ccd72011-08-10 02:10:13 +00003102 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton11fb9212012-06-27 20:26:19 +00003103 // Define a local module list here to ensure it lives longer than any "locker"
3104 // object which might lock its contents below (through the "module_list_ptr"
3105 // variable).
3106 ModuleList module_list;
Greg Clayton153ccd72011-08-10 02:10:13 +00003107 if (target == NULL && use_global_module_list == false)
Greg Claytone1f50b92011-05-03 22:09:39 +00003108 {
3109 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3110 result.SetStatus (eReturnStatusFailed);
3111 return false;
3112 }
3113 else
3114 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003115 if (target)
3116 {
3117 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3118 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3119 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3120 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003121 // Dump all sections for all modules images
Jim Ingham6bdea822011-10-24 18:36:33 +00003122 Stream &strm = result.GetOutputStream();
3123
3124 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3125 {
3126 if (target)
3127 {
3128 Address module_address;
3129 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3130 {
Greg Clayton3508c382012-02-24 01:59:29 +00003131 ModuleSP module_sp (module_address.GetModule());
3132 if (module_sp)
Jim Ingham6bdea822011-10-24 18:36:33 +00003133 {
Greg Clayton3508c382012-02-24 01:59:29 +00003134 PrintModule (target, module_sp.get(), UINT32_MAX, 0, strm);
Jim Ingham6bdea822011-10-24 18:36:33 +00003135 result.SetStatus (eReturnStatusSuccessFinishResult);
3136 }
3137 else
3138 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003139 result.AppendError ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003140 result.SetStatus (eReturnStatusFailed);
3141 }
3142 }
3143 else
3144 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003145 result.AppendError ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Ingham6bdea822011-10-24 18:36:33 +00003146 result.SetStatus (eReturnStatusFailed);
3147 }
3148 }
3149 else
3150 {
3151 result.AppendError ("Can only look up modules by address with a valid target.");
3152 result.SetStatus (eReturnStatusFailed);
3153 }
3154 return result.Succeeded();
3155 }
3156
Jim Ingham93367902012-05-30 02:19:25 +00003157 uint32_t num_modules = 0;
3158 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3159 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3160 // the global module list directly.
Enrico Granata146d9522012-11-08 02:22:02 +00003161 const ModuleList *module_list_ptr = NULL;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003162 const size_t argc = command.GetArgumentCount();
3163 if (argc == 0)
Greg Clayton899025f2011-08-09 00:01:09 +00003164 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003165 if (use_global_module_list)
3166 {
3167 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3168 num_modules = Module::GetNumberAllocatedModules();
3169 }
3170 else
3171 {
3172 module_list_ptr = &target->GetImages();
Greg Clayton2ad894b2012-05-15 18:43:44 +00003173 }
Greg Clayton899025f2011-08-09 00:01:09 +00003174 }
3175 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003176 {
3177 for (size_t i=0; i<argc; ++i)
3178 {
3179 // Dump specified images (by basename or fullpath)
3180 const char *arg_cstr = command.GetArgumentAtIndex(i);
3181 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3182 if (num_matches == 0)
3183 {
3184 if (argc == 1)
3185 {
3186 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3187 result.SetStatus (eReturnStatusFailed);
3188 return false;
3189 }
3190 }
3191 }
3192
Greg Clayton2ad894b2012-05-15 18:43:44 +00003193 module_list_ptr = &module_list;
3194 }
Jim Ingham93367902012-05-30 02:19:25 +00003195
3196 if (module_list_ptr != NULL)
3197 {
3198 locker.Lock(module_list_ptr->GetMutex());
3199 num_modules = module_list_ptr->GetSize();
3200 }
Greg Clayton899025f2011-08-09 00:01:09 +00003201
Greg Claytone1f50b92011-05-03 22:09:39 +00003202 if (num_modules > 0)
Jim Ingham6bdea822011-10-24 18:36:33 +00003203 {
Greg Claytone1f50b92011-05-03 22:09:39 +00003204 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3205 {
Greg Clayton153ccd72011-08-10 02:10:13 +00003206 ModuleSP module_sp;
Greg Clayton899025f2011-08-09 00:01:09 +00003207 Module *module;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003208 if (module_list_ptr)
Greg Clayton899025f2011-08-09 00:01:09 +00003209 {
Jim Ingham93367902012-05-30 02:19:25 +00003210 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Clayton2ad894b2012-05-15 18:43:44 +00003211 module = module_sp.get();
Greg Clayton899025f2011-08-09 00:01:09 +00003212 }
3213 else
3214 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003215 module = Module::GetAllocatedModuleAtIndex(image_idx);
3216 module_sp = module->shared_from_this();
Greg Clayton899025f2011-08-09 00:01:09 +00003217 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003218
Greg Claytonb5a8f142012-02-05 02:38:54 +00003219 int indent = strm.Printf("[%3u] ", image_idx);
3220 PrintModule (target, module, image_idx, indent, strm);
Greg Clayton153ccd72011-08-10 02:10:13 +00003221
Greg Claytone1f50b92011-05-03 22:09:39 +00003222 }
3223 result.SetStatus (eReturnStatusSuccessFinishResult);
3224 }
3225 else
3226 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003227 if (argc)
3228 {
3229 if (use_global_module_list)
3230 result.AppendError ("the global module list has no matching modules");
3231 else
3232 result.AppendError ("the target has no matching modules");
3233 }
Greg Clayton153ccd72011-08-10 02:10:13 +00003234 else
Greg Clayton2ad894b2012-05-15 18:43:44 +00003235 {
3236 if (use_global_module_list)
3237 result.AppendError ("the global module list is empty");
3238 else
3239 result.AppendError ("the target has no associated executable images");
3240 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003241 result.SetStatus (eReturnStatusFailed);
3242 return false;
3243 }
3244 }
3245 return result.Succeeded();
3246 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003247
3248 void
Greg Claytonb5a8f142012-02-05 02:38:54 +00003249 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm)
Jim Ingham6bdea822011-10-24 18:36:33 +00003250 {
3251
Jim Ingham6f01c932012-10-12 17:34:26 +00003252 if (module == NULL)
3253 {
3254 strm.PutCString("Null module");
3255 return;
3256 }
3257
Jim Ingham6bdea822011-10-24 18:36:33 +00003258 bool dump_object_name = false;
3259 if (m_options.m_format_array.empty())
3260 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003261 m_options.m_format_array.push_back(std::make_pair('u', 0));
3262 m_options.m_format_array.push_back(std::make_pair('h', 0));
3263 m_options.m_format_array.push_back(std::make_pair('f', 0));
3264 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Ingham6bdea822011-10-24 18:36:33 +00003265 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003266 const size_t num_entries = m_options.m_format_array.size();
3267 bool print_space = false;
3268 for (size_t i=0; i<num_entries; ++i)
Jim Ingham6bdea822011-10-24 18:36:33 +00003269 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003270 if (print_space)
3271 strm.PutChar(' ');
3272 print_space = true;
3273 const char format_char = m_options.m_format_array[i].first;
3274 uint32_t width = m_options.m_format_array[i].second;
3275 switch (format_char)
Jim Ingham6bdea822011-10-24 18:36:33 +00003276 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003277 case 'A':
3278 DumpModuleArchitecture (strm, module, false, width);
3279 break;
3280
3281 case 't':
3282 DumpModuleArchitecture (strm, module, true, width);
3283 break;
3284
3285 case 'f':
3286 DumpFullpath (strm, &module->GetFileSpec(), width);
3287 dump_object_name = true;
3288 break;
3289
3290 case 'd':
3291 DumpDirectory (strm, &module->GetFileSpec(), width);
3292 break;
3293
3294 case 'b':
3295 DumpBasename (strm, &module->GetFileSpec(), width);
3296 dump_object_name = true;
3297 break;
3298
3299 case 'h':
3300 case 'o':
3301 // Image header address
3302 {
3303 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Ingham6bdea822011-10-24 18:36:33 +00003304
Greg Claytonb5a8f142012-02-05 02:38:54 +00003305 ObjectFile *objfile = module->GetObjectFile ();
3306 if (objfile)
Jim Ingham6bdea822011-10-24 18:36:33 +00003307 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003308 Address header_addr(objfile->GetHeaderAddress());
3309 if (header_addr.IsValid())
Jim Ingham6bdea822011-10-24 18:36:33 +00003310 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003311 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Ingham6bdea822011-10-24 18:36:33 +00003312 {
Greg Claytonb5a8f142012-02-05 02:38:54 +00003313 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3314 if (header_load_addr == LLDB_INVALID_ADDRESS)
3315 {
3316 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3317 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003318 else
Greg Claytonb5a8f142012-02-05 02:38:54 +00003319 {
3320 if (format_char == 'o')
3321 {
3322 // Show the offset of slide for the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003323 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonb5a8f142012-02-05 02:38:54 +00003324 }
3325 else
3326 {
3327 // Show the load address of the image
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003328 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonb5a8f142012-02-05 02:38:54 +00003329 }
3330 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003331 break;
3332 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003333 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3334 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3335 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003336 }
Jim Ingham6bdea822011-10-24 18:36:33 +00003337 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003338 strm.Printf ("%*s", addr_nibble_width + 2, "");
3339 }
3340 break;
3341 case 'r':
3342 {
3343 uint32_t ref_count = 0;
3344 ModuleSP module_sp (module->shared_from_this());
3345 if (module_sp)
3346 {
3347 // Take one away to make sure we don't count our local "module_sp"
3348 ref_count = module_sp.use_count() - 1;
3349 }
3350 if (width)
3351 strm.Printf("{%*u}", width, ref_count);
3352 else
3353 strm.Printf("{%u}", ref_count);
3354 }
3355 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003356
Greg Claytonb5a8f142012-02-05 02:38:54 +00003357 case 's':
3358 case 'S':
3359 {
3360 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3361 if (symbol_vendor)
3362 {
3363 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3364 if (symbol_file)
3365 {
3366 if (format_char == 'S')
3367 {
3368 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3369 // Dump symbol file only if different from module file
3370 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3371 {
3372 print_space = false;
3373 break;
3374 }
3375 // Add a newline and indent past the index
3376 strm.Printf ("\n%*s", indent, "");
3377 }
3378 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3379 dump_object_name = true;
3380 break;
3381 }
3382 }
3383 strm.Printf("%.*s", width, "<NONE>");
3384 }
3385 break;
3386
3387 case 'm':
3388 module->GetModificationTime().Dump(&strm, width);
3389 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003390
Greg Claytonb5a8f142012-02-05 02:38:54 +00003391 case 'p':
3392 strm.Printf("%p", module);
3393 break;
3394
3395 case 'u':
3396 DumpModuleUUID(strm, module);
3397 break;
3398
3399 default:
3400 break;
Jim Ingham6bdea822011-10-24 18:36:33 +00003401 }
Greg Claytonb5a8f142012-02-05 02:38:54 +00003402
3403 }
3404 if (dump_object_name)
3405 {
3406 const char *object_name = module->GetObjectName().GetCString();
3407 if (object_name)
3408 strm.Printf ("(%s)", object_name);
Jim Ingham6bdea822011-10-24 18:36:33 +00003409 }
3410 strm.EOL();
3411 }
3412
Greg Claytone1f50b92011-05-03 22:09:39 +00003413 CommandOptions m_options;
3414};
3415
3416OptionDefinition
3417CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3418{
Jim Ingham6bdea822011-10-24 18:36:33 +00003419 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."},
3420 { 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 +00003421 { 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 +00003422 { 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."},
3423 { 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 +00003424 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3425 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3426 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3427 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3428 { 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 +00003429 { 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 +00003430 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3431 { 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."},
3432 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."},
Greg Clayton899025f2011-08-09 00:01:09 +00003433 { 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 +00003434 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3435};
3436
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003437#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytone1f50b92011-05-03 22:09:39 +00003438
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003439//----------------------------------------------------------------------
3440// Lookup unwind information in images
3441//----------------------------------------------------------------------
3442
3443class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3444{
3445public:
3446
3447 enum
3448 {
3449 eLookupTypeInvalid = -1,
3450 eLookupTypeAddress = 0,
3451 eLookupTypeSymbol,
3452 eLookupTypeFunction,
3453 eLookupTypeFunctionOrSymbol,
3454 kNumLookupTypes
3455 };
3456
3457 class CommandOptions : public Options
3458 {
3459 public:
3460
3461 CommandOptions (CommandInterpreter &interpreter) :
3462 Options(interpreter),
3463 m_type(eLookupTypeInvalid),
3464 m_str(),
3465 m_addr(LLDB_INVALID_ADDRESS)
3466 {
3467 }
3468
3469 virtual
3470 ~CommandOptions ()
3471 {
3472 }
3473
3474 virtual Error
3475 SetOptionValue (uint32_t option_idx, const char *option_arg)
3476 {
3477 Error error;
3478
Greg Clayton6475c422012-12-04 00:32:51 +00003479 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003480
3481 switch (short_option)
3482 {
3483 case 'a':
3484 m_type = eLookupTypeAddress;
3485 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3486 if (m_addr == LLDB_INVALID_ADDRESS)
3487 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3488 break;
3489
3490 case 'n':
3491 m_str = option_arg;
3492 m_type = eLookupTypeFunctionOrSymbol;
3493 break;
3494 }
3495
3496 return error;
3497 }
3498
3499 void
3500 OptionParsingStarting ()
3501 {
3502 m_type = eLookupTypeInvalid;
3503 m_str.clear();
3504 m_addr = LLDB_INVALID_ADDRESS;
3505 }
3506
3507 const OptionDefinition*
3508 GetDefinitions ()
3509 {
3510 return g_option_table;
3511 }
3512
3513 // Options table: Required for subclasses of Options.
3514
3515 static OptionDefinition g_option_table[];
3516
3517 // Instance variables to hold the values for command options.
3518
3519 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3520 std::string m_str; // Holds name lookup
3521 lldb::addr_t m_addr; // Holds the address to lookup
3522 };
3523
3524 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3525 CommandObjectParsed (interpreter,
3526 "target modules show-unwind",
3527 "Show synthesized unwind instructions for a function.",
3528 NULL),
3529 m_options (interpreter)
3530 {
3531 }
3532
3533 virtual
3534 ~CommandObjectTargetModulesShowUnwind ()
3535 {
3536 }
3537
3538 virtual
3539 Options *
3540 GetOptions ()
3541 {
3542 return &m_options;
3543 }
3544
3545protected:
3546 bool
3547 DoExecute (Args& command,
3548 CommandReturnObject &result)
3549 {
3550 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
3551 if (!target)
3552 {
3553 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3554 result.SetStatus (eReturnStatusFailed);
3555 return false;
3556 }
3557
3558 ExecutionContext exe_ctx = m_interpreter.GetDebugger().GetSelectedExecutionContext();
3559 Process *process = exe_ctx.GetProcessPtr();
3560 ABI *abi = NULL;
3561 if (process)
3562 abi = process->GetABI().get();
3563
3564 if (process == NULL)
3565 {
3566 result.AppendError ("You must have a process running to use this command.");
3567 result.SetStatus (eReturnStatusFailed);
3568 return false;
3569 }
3570
3571 ThreadList threads(process->GetThreadList());
3572 if (threads.GetSize() == 0)
3573 {
3574 result.AppendError ("The process must be paused to use this command.");
3575 result.SetStatus (eReturnStatusFailed);
3576 return false;
3577 }
3578
3579 ThreadSP thread(threads.GetThreadAtIndex(0));
3580 if (thread.get() == NULL)
3581 {
3582 result.AppendError ("The process must be paused to use this command.");
3583 result.SetStatus (eReturnStatusFailed);
3584 return false;
3585 }
3586
3587 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3588 {
3589 SymbolContextList sc_list;
3590 uint32_t num_matches;
3591 ConstString function_name (m_options.m_str.c_str());
3592 num_matches = target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3593 for (uint32_t idx = 0; idx < num_matches; idx++)
3594 {
3595 SymbolContext sc;
3596 sc_list.GetContextAtIndex(idx, sc);
3597 if (sc.symbol == NULL && sc.function == NULL)
3598 continue;
3599 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3600 continue;
3601 AddressRange range;
3602 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3603 continue;
3604 if (!range.GetBaseAddress().IsValid())
3605 continue;
3606 ConstString funcname(sc.GetFunctionName());
3607 if (funcname.IsEmpty())
3608 continue;
3609 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3610 if (abi)
3611 start_addr = abi->FixCodeAddress(start_addr);
3612
3613 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3614 if (func_unwinders_sp.get() == NULL)
3615 continue;
3616
3617 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3618 if (first_non_prologue_insn.IsValid())
3619 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003620 result.GetOutputStream().Printf("First non-prologue instruction is at address 0x%" PRIx64 " or offset %" PRId64 " into the function.\n", first_non_prologue_insn.GetLoadAddress(target), first_non_prologue_insn.GetLoadAddress(target) - start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003621 result.GetOutputStream().Printf ("\n");
3622 }
3623
3624 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*thread.get());
3625 if (non_callsite_unwind_plan.get())
3626 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003627 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003628 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3629 result.GetOutputStream().Printf ("\n");
3630 }
3631
3632 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3633 if (callsite_unwind_plan.get())
3634 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003635 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003636 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3637 result.GetOutputStream().Printf ("\n");
3638 }
3639
3640 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3641 if (arch_default_unwind_plan.get())
3642 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003643 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003644 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3645 result.GetOutputStream().Printf ("\n");
3646 }
3647
3648 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3649 if (fast_unwind_plan.get())
3650 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00003651 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda5b0afcc2012-07-12 00:20:07 +00003652 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3653 result.GetOutputStream().Printf ("\n");
3654 }
3655
3656
3657 result.GetOutputStream().Printf ("\n");
3658 }
3659 }
3660 return result.Succeeded();
3661 }
3662
3663 CommandOptions m_options;
3664};
3665
3666OptionDefinition
3667CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3668{
3669 { LLDB_OPT_SET_1, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function or symbol by name in one or more target modules."},
3670 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
3671};
Greg Claytone1f50b92011-05-03 22:09:39 +00003672
3673//----------------------------------------------------------------------
3674// Lookup information in images
3675//----------------------------------------------------------------------
Jim Inghamda26bd22012-06-08 21:56:10 +00003676class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytone1f50b92011-05-03 22:09:39 +00003677{
3678public:
3679
3680 enum
3681 {
3682 eLookupTypeInvalid = -1,
3683 eLookupTypeAddress = 0,
3684 eLookupTypeSymbol,
3685 eLookupTypeFileLine, // Line is optional
3686 eLookupTypeFunction,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003687 eLookupTypeFunctionOrSymbol,
Greg Claytone1f50b92011-05-03 22:09:39 +00003688 eLookupTypeType,
3689 kNumLookupTypes
3690 };
3691
3692 class CommandOptions : public Options
3693 {
3694 public:
3695
3696 CommandOptions (CommandInterpreter &interpreter) :
3697 Options(interpreter)
3698 {
3699 OptionParsingStarting();
3700 }
3701
3702 virtual
3703 ~CommandOptions ()
3704 {
3705 }
3706
3707 virtual Error
3708 SetOptionValue (uint32_t option_idx, const char *option_arg)
3709 {
3710 Error error;
3711
Greg Clayton6475c422012-12-04 00:32:51 +00003712 const int short_option = m_getopt_table[option_idx].val;
Greg Claytone1f50b92011-05-03 22:09:39 +00003713
3714 switch (short_option)
3715 {
3716 case 'a':
3717 m_type = eLookupTypeAddress;
3718 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3719 if (m_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003720 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003721 break;
3722
3723 case 'o':
3724 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3725 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +00003726 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003727 break;
3728
3729 case 's':
3730 m_str = option_arg;
3731 m_type = eLookupTypeSymbol;
3732 break;
3733
3734 case 'f':
3735 m_file.SetFile (option_arg, false);
3736 m_type = eLookupTypeFileLine;
3737 break;
3738
3739 case 'i':
Sean Callanan9ad19532012-02-11 01:22:21 +00003740 m_include_inlines = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003741 break;
3742
3743 case 'l':
3744 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3745 if (m_line_number == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00003746 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytone1f50b92011-05-03 22:09:39 +00003747 else if (m_line_number == 0)
Greg Clayton9c236732011-10-26 00:56:27 +00003748 error.SetErrorString ("zero is an invalid line number");
Greg Claytone1f50b92011-05-03 22:09:39 +00003749 m_type = eLookupTypeFileLine;
3750 break;
3751
Greg Clayton2ad894b2012-05-15 18:43:44 +00003752 case 'F':
Greg Claytone1f50b92011-05-03 22:09:39 +00003753 m_str = option_arg;
3754 m_type = eLookupTypeFunction;
3755 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003756
3757 case 'n':
3758 m_str = option_arg;
3759 m_type = eLookupTypeFunctionOrSymbol;
3760 break;
3761
Greg Claytone1f50b92011-05-03 22:09:39 +00003762 case 't':
3763 m_str = option_arg;
3764 m_type = eLookupTypeType;
3765 break;
3766
3767 case 'v':
3768 m_verbose = 1;
3769 break;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003770
3771 case 'A':
3772 m_print_all = true;
3773 break;
Greg Claytone1f50b92011-05-03 22:09:39 +00003774
3775 case 'r':
3776 m_use_regex = true;
3777 break;
3778 }
3779
3780 return error;
3781 }
3782
3783 void
3784 OptionParsingStarting ()
3785 {
3786 m_type = eLookupTypeInvalid;
3787 m_str.clear();
3788 m_file.Clear();
3789 m_addr = LLDB_INVALID_ADDRESS;
3790 m_offset = 0;
3791 m_line_number = 0;
3792 m_use_regex = false;
Sean Callanan9ad19532012-02-11 01:22:21 +00003793 m_include_inlines = true;
Greg Claytone1f50b92011-05-03 22:09:39 +00003794 m_verbose = false;
Sean Callanan56d31ec2012-06-06 20:49:55 +00003795 m_print_all = false;
Greg Claytone1f50b92011-05-03 22:09:39 +00003796 }
3797
3798 const OptionDefinition*
3799 GetDefinitions ()
3800 {
3801 return g_option_table;
3802 }
3803
3804 // Options table: Required for subclasses of Options.
3805
3806 static OptionDefinition g_option_table[];
3807 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3808 std::string m_str; // Holds name lookup
3809 FileSpec m_file; // Files for file lookups
3810 lldb::addr_t m_addr; // Holds the address to lookup
3811 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3812 uint32_t m_line_number; // Line number for file+line lookups
3813 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanan9ad19532012-02-11 01:22:21 +00003814 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytone1f50b92011-05-03 22:09:39 +00003815 bool m_verbose; // Enable verbose lookup info
Sean Callanan56d31ec2012-06-06 20:49:55 +00003816 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytone1f50b92011-05-03 22:09:39 +00003817
3818 };
3819
3820 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00003821 CommandObjectParsed (interpreter,
3822 "target modules lookup",
3823 "Look up information within executable and dependent shared library images.",
3824 NULL),
3825 m_options (interpreter)
Greg Claytone1f50b92011-05-03 22:09:39 +00003826 {
3827 CommandArgumentEntry arg;
3828 CommandArgumentData file_arg;
3829
3830 // Define the first (and only) variant of this arg.
3831 file_arg.arg_type = eArgTypeFilename;
3832 file_arg.arg_repetition = eArgRepeatStar;
3833
3834 // There is only one variant this argument could be; put it into the argument entry.
3835 arg.push_back (file_arg);
3836
3837 // Push the data for the first argument into the m_arguments vector.
3838 m_arguments.push_back (arg);
3839 }
3840
3841 virtual
3842 ~CommandObjectTargetModulesLookup ()
3843 {
3844 }
3845
3846 virtual Options *
3847 GetOptions ()
3848 {
3849 return &m_options;
3850 }
3851
Sean Callanan56d31ec2012-06-06 20:49:55 +00003852 bool
3853 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3854 {
3855 switch (m_options.m_type)
3856 {
3857 case eLookupTypeAddress:
3858 case eLookupTypeFileLine:
3859 case eLookupTypeFunction:
3860 case eLookupTypeFunctionOrSymbol:
3861 case eLookupTypeSymbol:
3862 default:
3863 return false;
3864 case eLookupTypeType:
3865 break;
3866 }
3867
3868 ExecutionContext exe_ctx = interpreter.GetDebugger().GetSelectedExecutionContext();
3869
3870 StackFrameSP frame = exe_ctx.GetFrameSP();
3871
3872 if (!frame)
3873 return false;
3874
3875 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3876
3877 if (!sym_ctx.module_sp)
3878 return false;
3879
3880 switch (m_options.m_type)
3881 {
3882 default:
3883 return false;
3884 case eLookupTypeType:
3885 if (!m_options.m_str.empty())
3886 {
3887 if (LookupTypeHere (m_interpreter,
3888 result.GetOutputStream(),
3889 sym_ctx,
3890 m_options.m_str.c_str(),
3891 m_options.m_use_regex))
3892 {
3893 result.SetStatus(eReturnStatusSuccessFinishResult);
3894 return true;
3895 }
3896 }
3897 break;
3898 }
3899
3900 return true;
3901 }
Greg Claytone1f50b92011-05-03 22:09:39 +00003902
3903 bool
3904 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
3905 {
3906 switch (m_options.m_type)
3907 {
3908 case eLookupTypeAddress:
3909 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
3910 {
3911 if (LookupAddressInModule (m_interpreter,
3912 result.GetOutputStream(),
3913 module,
3914 eSymbolContextEverything,
3915 m_options.m_addr,
3916 m_options.m_offset,
3917 m_options.m_verbose))
3918 {
3919 result.SetStatus(eReturnStatusSuccessFinishResult);
3920 return true;
3921 }
3922 }
3923 break;
3924
3925 case eLookupTypeSymbol:
3926 if (!m_options.m_str.empty())
3927 {
Greg Clayton2ad894b2012-05-15 18:43:44 +00003928 if (LookupSymbolInModule (m_interpreter,
3929 result.GetOutputStream(),
3930 module,
3931 m_options.m_str.c_str(),
3932 m_options.m_use_regex,
3933 m_options.m_verbose))
Greg Claytone1f50b92011-05-03 22:09:39 +00003934 {
3935 result.SetStatus(eReturnStatusSuccessFinishResult);
3936 return true;
3937 }
3938 }
3939 break;
3940
3941 case eLookupTypeFileLine:
3942 if (m_options.m_file)
3943 {
3944
3945 if (LookupFileAndLineInModule (m_interpreter,
3946 result.GetOutputStream(),
3947 module,
3948 m_options.m_file,
3949 m_options.m_line_number,
Sean Callanan9ad19532012-02-11 01:22:21 +00003950 m_options.m_include_inlines,
Greg Claytone1f50b92011-05-03 22:09:39 +00003951 m_options.m_verbose))
3952 {
3953 result.SetStatus(eReturnStatusSuccessFinishResult);
3954 return true;
3955 }
3956 }
3957 break;
Greg Clayton2ad894b2012-05-15 18:43:44 +00003958
3959 case eLookupTypeFunctionOrSymbol:
Greg Claytone1f50b92011-05-03 22:09:39 +00003960 case eLookupTypeFunction:
3961 if (!m_options.m_str.empty())
3962 {
3963 if (LookupFunctionInModule (m_interpreter,
3964 result.GetOutputStream(),
3965 module,
3966 m_options.m_str.c_str(),
3967 m_options.m_use_regex,
Sean Callanan9ad19532012-02-11 01:22:21 +00003968 m_options.m_include_inlines,
Greg Clayton2ad894b2012-05-15 18:43:44 +00003969 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytone1f50b92011-05-03 22:09:39 +00003970 m_options.m_verbose))
3971 {
3972 result.SetStatus(eReturnStatusSuccessFinishResult);
3973 return true;
3974 }
3975 }
3976 break;
3977
Greg Clayton2ad894b2012-05-15 18:43:44 +00003978
Greg Claytone1f50b92011-05-03 22:09:39 +00003979 case eLookupTypeType:
3980 if (!m_options.m_str.empty())
3981 {
3982 if (LookupTypeInModule (m_interpreter,
3983 result.GetOutputStream(),
3984 module,
3985 m_options.m_str.c_str(),
3986 m_options.m_use_regex))
3987 {
3988 result.SetStatus(eReturnStatusSuccessFinishResult);
3989 return true;
3990 }
3991 }
3992 break;
3993
3994 default:
3995 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
3996 syntax_error = true;
3997 break;
3998 }
3999
4000 result.SetStatus (eReturnStatusFailed);
4001 return false;
4002 }
4003
Jim Inghamda26bd22012-06-08 21:56:10 +00004004protected:
Greg Claytone1f50b92011-05-03 22:09:39 +00004005 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004006 DoExecute (Args& command,
Greg Claytone1f50b92011-05-03 22:09:39 +00004007 CommandReturnObject &result)
4008 {
4009 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4010 if (target == NULL)
4011 {
4012 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4013 result.SetStatus (eReturnStatusFailed);
4014 return false;
4015 }
4016 else
4017 {
4018 bool syntax_error = false;
4019 uint32_t i;
4020 uint32_t num_successful_lookups = 0;
4021 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4022 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4023 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4024 // Dump all sections for all modules images
4025
4026 if (command.GetArgumentCount() == 0)
4027 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004028 ModuleSP current_module;
4029
4030 // Where it is possible to look in the current symbol context
4031 // first, try that. If this search was successful and --all
4032 // was not passed, don't print anything else.
4033 if (LookupHere (m_interpreter, result, syntax_error))
4034 {
4035 result.GetOutputStream().EOL();
4036 num_successful_lookups++;
4037 if (!m_options.m_print_all)
4038 {
4039 result.SetStatus (eReturnStatusSuccessFinishResult);
4040 return result.Succeeded();
4041 }
4042 }
4043
4044 // Dump all sections for all other modules
4045
Enrico Granata146d9522012-11-08 02:22:02 +00004046 const ModuleList &target_modules = target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00004047 Mutex::Locker modules_locker(target_modules.GetMutex());
4048 const uint32_t num_modules = target_modules.GetSize();
Greg Claytone1f50b92011-05-03 22:09:39 +00004049 if (num_modules > 0)
4050 {
4051 for (i = 0; i<num_modules && syntax_error == false; ++i)
4052 {
Sean Callanan56d31ec2012-06-06 20:49:55 +00004053 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
4054
4055 if (module_pointer != current_module.get() &&
4056 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004057 {
4058 result.GetOutputStream().EOL();
4059 num_successful_lookups++;
4060 }
4061 }
4062 }
4063 else
4064 {
4065 result.AppendError ("the target has no associated executable images");
4066 result.SetStatus (eReturnStatusFailed);
4067 return false;
4068 }
4069 }
4070 else
4071 {
4072 // Dump specified images (by basename or fullpath)
4073 const char *arg_cstr;
4074 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4075 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004076 ModuleList module_list;
4077 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4078 if (num_matches > 0)
Greg Claytone1f50b92011-05-03 22:09:39 +00004079 {
Jason Molendabf41e192012-10-04 22:47:07 +00004080 for (size_t j=0; j<num_matches; ++j)
Greg Claytone1f50b92011-05-03 22:09:39 +00004081 {
Jason Molendabf41e192012-10-04 22:47:07 +00004082 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton91048ef2011-11-10 01:18:58 +00004083 if (module)
Greg Claytone1f50b92011-05-03 22:09:39 +00004084 {
Greg Clayton91048ef2011-11-10 01:18:58 +00004085 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytone1f50b92011-05-03 22:09:39 +00004086 {
4087 result.GetOutputStream().EOL();
4088 num_successful_lookups++;
4089 }
4090 }
4091 }
4092 }
4093 else
4094 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4095 }
4096 }
4097
4098 if (num_successful_lookups > 0)
4099 result.SetStatus (eReturnStatusSuccessFinishResult);
4100 else
4101 result.SetStatus (eReturnStatusFailed);
4102 }
4103 return result.Succeeded();
4104 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004105
4106 CommandOptions m_options;
4107};
4108
4109OptionDefinition
4110CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4111{
Sean Callanan3bfaad62012-09-13 21:11:40 +00004112 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."},
4113 { LLDB_OPT_SET_1, false, "offset", 'o', required_argument, NULL, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
Greg Clayton2ad894b2012-05-15 18:43:44 +00004114 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4115 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Sean Callanan3bfaad62012-09-13 21:11:40 +00004116 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4117 { LLDB_OPT_SET_2, true, "symbol", 's', required_argument, NULL, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules."},
4118 { LLDB_OPT_SET_3, true, "file", 'f', required_argument, NULL, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules."},
4119 { LLDB_OPT_SET_3, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
Jim Inghamf081dab2012-06-04 22:47:34 +00004120 { LLDB_OPT_SET_FROM_TO(3,5),
Sean Callanan3bfaad62012-09-13 21:11:40 +00004121 false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4122 { LLDB_OPT_SET_4, true, "function", 'F', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
4123 { LLDB_OPT_SET_5, true, "name", 'n', required_argument, NULL, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules."},
4124 { LLDB_OPT_SET_6, true, "type", 't', required_argument, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
4125 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4126 { LLDB_OPT_SET_ALL, false, "all", 'A', no_argument, NULL, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available."},
4127 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Greg Claytone1f50b92011-05-03 22:09:39 +00004128};
Chris Lattner24943d22010-06-08 16:52:24 +00004129
4130
Jim Inghamd60d94a2011-03-11 03:53:59 +00004131#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner24943d22010-06-08 16:52:24 +00004132
4133//-------------------------------------------------------------------------
4134// CommandObjectMultiwordImageSearchPaths
4135//-------------------------------------------------------------------------
4136
Greg Claytone1f50b92011-05-03 22:09:39 +00004137class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner24943d22010-06-08 16:52:24 +00004138{
4139public:
Greg Claytone1f50b92011-05-03 22:09:39 +00004140
4141 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4142 CommandObjectMultiword (interpreter,
4143 "target modules search-paths",
4144 "A set of commands for operating on debugger target image search paths.",
4145 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00004146 {
Greg Claytone1f50b92011-05-03 22:09:39 +00004147 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4148 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4149 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4150 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4151 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00004152 }
Greg Claytone1f50b92011-05-03 22:09:39 +00004153
4154 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner24943d22010-06-08 16:52:24 +00004155 {
4156 }
4157};
4158
Greg Claytone1f50b92011-05-03 22:09:39 +00004159
4160
4161#pragma mark CommandObjectTargetModules
4162
4163//-------------------------------------------------------------------------
4164// CommandObjectTargetModules
4165//-------------------------------------------------------------------------
4166
4167class CommandObjectTargetModules : public CommandObjectMultiword
4168{
4169public:
4170 //------------------------------------------------------------------
4171 // Constructors and Destructors
4172 //------------------------------------------------------------------
4173 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4174 CommandObjectMultiword (interpreter,
4175 "target modules",
4176 "A set of commands for accessing information for one or more target modules.",
4177 "target modules <sub-command> ...")
4178 {
4179 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4180 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
4181 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter)));
4182 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4183 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4184 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4185 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda5b0afcc2012-07-12 00:20:07 +00004186 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00004187
4188 }
4189 virtual
4190 ~CommandObjectTargetModules()
4191 {
4192 }
4193
4194private:
4195 //------------------------------------------------------------------
4196 // For CommandObjectTargetModules only
4197 //------------------------------------------------------------------
4198 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4199};
4200
4201
Greg Clayton3508c382012-02-24 01:59:29 +00004202
Jim Inghamda26bd22012-06-08 21:56:10 +00004203class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Clayton3508c382012-02-24 01:59:29 +00004204{
4205public:
4206 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004207 CommandObjectParsed (interpreter,
4208 "target symbols add",
Greg Clayton437b5bc2012-09-27 22:26:11 +00004209 "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.",
4210 "target symbols add [<symfile>]"),
4211 m_option_group (interpreter),
4212 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4213 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4214
Greg Clayton3508c382012-02-24 01:59:29 +00004215 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004216 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4217 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4218 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4219 m_option_group.Finalize();
Greg Clayton3508c382012-02-24 01:59:29 +00004220 }
4221
4222 virtual
4223 ~CommandObjectTargetSymbolsAdd ()
4224 {
4225 }
4226
Jim Inghamda26bd22012-06-08 21:56:10 +00004227 int
4228 HandleArgumentCompletion (Args &input,
4229 int &cursor_index,
4230 int &cursor_char_position,
4231 OptionElementVector &opt_element_vector,
4232 int match_start_point,
4233 int max_return_elements,
4234 bool &word_complete,
4235 StringList &matches)
4236 {
4237 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4238 completion_str.erase (cursor_char_position);
4239
4240 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4241 CommandCompletions::eDiskFileCompletion,
4242 completion_str.c_str(),
4243 match_start_point,
4244 max_return_elements,
4245 NULL,
4246 word_complete,
4247 matches);
4248 return matches.GetSize();
4249 }
4250
Greg Clayton437b5bc2012-09-27 22:26:11 +00004251 virtual Options *
4252 GetOptions ()
4253 {
4254 return &m_option_group;
4255 }
4256
4257
Jim Inghamda26bd22012-06-08 21:56:10 +00004258protected:
Greg Clayton437b5bc2012-09-27 22:26:11 +00004259
4260 bool
4261 AddModuleSymbols (Target *target,
Greg Claytonc92fded2012-12-12 01:15:30 +00004262 ModuleSpec &module_spec,
Greg Clayton437b5bc2012-09-27 22:26:11 +00004263 bool &flush,
4264 CommandReturnObject &result)
4265 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004266 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4267 if (symbol_fspec)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004268 {
4269 char symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004270 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
4271
4272 if (!module_spec.GetUUID().IsValid())
4273 {
4274 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4275 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4276 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004277 // We now have a module that represents a symbol file
4278 // that can be used for a module that might exist in the
4279 // current target, so we need to find that module in the
4280 // target
Greg Claytonc92fded2012-12-12 01:15:30 +00004281 ModuleList matching_module_list;
4282 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4283 if (num_matches > 1)
Greg Clayton437b5bc2012-09-27 22:26:11 +00004284 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004285 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4286 }
4287 else if (num_matches == 1)
4288 {
4289 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
4290
Greg Clayton437b5bc2012-09-27 22:26:11 +00004291 // The module has not yet created its symbol vendor, we can just
4292 // give the existing target module the symfile path to use for
4293 // when it decides to create it!
Greg Claytonc92fded2012-12-12 01:15:30 +00004294 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004295
Greg Claytonc92fded2012-12-12 01:15:30 +00004296 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
4297 if (symbol_vendor)
4298 {
4299 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
4300
4301 if (symbol_file)
4302 {
4303 ObjectFile *object_file = symbol_file->GetObjectFile();
4304
4305 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4306 {
4307 // Provide feedback that the symfile has been successfully added.
4308 const FileSpec &module_fs = module_sp->GetFileSpec();
4309 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s/%s'\n",
4310 symfile_path,
4311 module_fs.GetDirectory().AsCString(),
4312 module_fs.GetFilename().AsCString());
4313
4314 // Let clients know something changed in the module
4315 // if it is currently loaded
4316 ModuleList module_list;
4317 module_list.Append (module_sp);
4318 target->ModulesDidLoad (module_list);
4319 flush = true;
4320 result.SetStatus (eReturnStatusSuccessFinishResult);
4321 return true;
4322 }
4323 }
4324 }
4325 // Clear the symbol file spec if anything went wrong
4326 module_sp->SetSymbolFileFileSpec (FileSpec());
4327
4328 }
4329
4330 if (module_spec.GetUUID().IsValid())
4331 {
4332 StreamString ss_symfile_uuid;
4333 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4334 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4335 symfile_path,
4336 ss_symfile_uuid.GetData(),
4337 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4338 ? "\n please specify the full path to the symbol file"
4339 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004340 }
4341 else
4342 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004343 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4344 symfile_path,
4345 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4346 ? "\n please specify the full path to the symbol file"
4347 : "");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004348 }
4349 }
4350 else
4351 {
4352 result.AppendError ("one or more executable image paths must be specified");
Greg Clayton437b5bc2012-09-27 22:26:11 +00004353 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004354 result.SetStatus (eReturnStatusFailed);
4355 return false;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004356 }
4357
Greg Clayton3508c382012-02-24 01:59:29 +00004358 virtual bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004359 DoExecute (Args& args,
Greg Clayton3508c382012-02-24 01:59:29 +00004360 CommandReturnObject &result)
4361 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004362 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
4363 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004364 result.SetStatus (eReturnStatusFailed);
Greg Clayton3508c382012-02-24 01:59:29 +00004365 if (target == NULL)
4366 {
4367 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Greg Clayton3508c382012-02-24 01:59:29 +00004368 }
4369 else
4370 {
Greg Claytoncf5927e2012-05-18 02:38:05 +00004371 bool flush = false;
Greg Claytonc92fded2012-12-12 01:15:30 +00004372 ModuleSpec module_spec;
Greg Clayton437b5bc2012-09-27 22:26:11 +00004373 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4374 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4375 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
4376
Greg Clayton3508c382012-02-24 01:59:29 +00004377 const size_t argc = args.GetArgumentCount();
4378 if (argc == 0)
4379 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004380 if (uuid_option_set || file_option_set || frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004381 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004382 bool success = false;
4383 bool error_set = false;
4384 if (frame_option_set)
Greg Clayton3508c382012-02-24 01:59:29 +00004385 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004386 Process *process = exe_ctx.GetProcessPtr();
4387 if (process)
Greg Clayton3508c382012-02-24 01:59:29 +00004388 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004389 const StateType process_state = process->GetState();
4390 if (StateIsStoppedState (process_state, true))
Greg Clayton3508c382012-02-24 01:59:29 +00004391 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004392 StackFrame *frame = exe_ctx.GetFramePtr();
4393 if (frame)
Greg Clayton3508c382012-02-24 01:59:29 +00004394 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004395 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4396 if (frame_module_sp)
4397 {
4398 if (frame_module_sp->GetPlatformFileSpec().Exists())
4399 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004400 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4401 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004402 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004403 module_spec.GetUUID() = frame_module_sp->GetUUID();
4404 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004405 }
4406 else
4407 {
4408 result.AppendError ("frame has no module");
4409 error_set = true;
4410 }
Greg Clayton3508c382012-02-24 01:59:29 +00004411 }
Johnny Chen9262cd52012-08-22 00:18:43 +00004412 else
4413 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004414 result.AppendError ("invalid current frame");
4415 error_set = true;
Johnny Chen9262cd52012-08-22 00:18:43 +00004416 }
Greg Clayton3508c382012-02-24 01:59:29 +00004417 }
4418 else
4419 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004420 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
4421 error_set = true;
Greg Clayton3508c382012-02-24 01:59:29 +00004422 }
Greg Clayton3508c382012-02-24 01:59:29 +00004423 }
4424 else
4425 {
Greg Clayton437b5bc2012-09-27 22:26:11 +00004426 result.AppendError ("a process must exist in order to use the --frame option");
4427 error_set = true;
4428 }
4429 }
4430 else
4431 {
4432 if (uuid_option_set)
4433 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004434 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4435 success |= module_spec.GetUUID().IsValid();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004436 }
4437 else if (file_option_set)
4438 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004439 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4440 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
Greg Clayton437b5bc2012-09-27 22:26:11 +00004441 if (module_sp)
Greg Clayton3508c382012-02-24 01:59:29 +00004442 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004443 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4444 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4445 module_spec.GetUUID() = module_sp->GetUUID();
4446 module_spec.GetArchitecture() = module_sp->GetArchitecture();
Greg Clayton3508c382012-02-24 01:59:29 +00004447 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004448 else
4449 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004450 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004451 }
Greg Claytonc92fded2012-12-12 01:15:30 +00004452 success |= module_spec.GetFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004453 }
4454 }
4455
4456 if (success)
4457 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004458 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004459 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004460 if (module_spec.GetSymbolFileSpec())
4461 success = AddModuleSymbols (target, module_spec, flush, result);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004462 }
4463 }
4464
4465 if (!success && !error_set)
4466 {
4467 StreamString error_strm;
4468 if (uuid_option_set)
4469 {
4470 error_strm.PutCString("unable to find debug symbols for UUID ");
Greg Claytonc92fded2012-12-12 01:15:30 +00004471 module_spec.GetUUID().Dump (&error_strm);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004472 }
4473 else if (file_option_set)
4474 {
4475 error_strm.PutCString("unable to find debug symbols for the executable file ");
Greg Claytonc92fded2012-12-12 01:15:30 +00004476 error_strm << module_spec.GetFileSpec();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004477 }
4478 else if (frame_option_set)
4479 {
4480 error_strm.PutCString("unable to find debug symbols for the current frame");
4481 }
4482 result.AppendError (error_strm.GetData());
4483 }
4484 }
4485 else
4486 {
4487 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4488 }
4489 }
4490 else
4491 {
4492 if (uuid_option_set)
4493 {
4494 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4495 }
4496 else if (file_option_set)
4497 {
4498 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4499 }
4500 else if (frame_option_set)
4501 {
4502 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4503 }
4504 else
4505 {
4506 PlatformSP platform_sp (target->GetPlatform());
4507
4508 for (size_t i=0; i<argc; ++i)
4509 {
4510 const char *symfile_path = args.GetArgumentAtIndex(i);
4511 if (symfile_path)
4512 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004513 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
Greg Clayton437b5bc2012-09-27 22:26:11 +00004514 if (platform_sp)
Greg Claytonc92fded2012-12-12 01:15:30 +00004515 {
4516 FileSpec symfile_spec;
4517 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4518 module_spec.GetSymbolFileSpec() = symfile_spec;
4519 }
Greg Clayton437b5bc2012-09-27 22:26:11 +00004520
4521 ArchSpec arch;
Greg Claytonc92fded2012-12-12 01:15:30 +00004522 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Clayton437b5bc2012-09-27 22:26:11 +00004523
4524 if (symfile_exists)
4525 {
Greg Claytonc92fded2012-12-12 01:15:30 +00004526 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004527 break;
4528 }
4529 else
4530 {
4531 char resolved_symfile_path[PATH_MAX];
Greg Claytonc92fded2012-12-12 01:15:30 +00004532 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
Greg Clayton437b5bc2012-09-27 22:26:11 +00004533 {
4534 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4535 {
4536 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4537 break;
4538 }
4539 }
4540 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4541 break;
4542 }
Greg Clayton3508c382012-02-24 01:59:29 +00004543 }
4544 }
4545 }
4546 }
Greg Claytoncf5927e2012-05-18 02:38:05 +00004547
4548 if (flush)
4549 {
4550 Process *process = exe_ctx.GetProcessPtr();
4551 if (process)
4552 process->Flush();
4553 }
Greg Clayton3508c382012-02-24 01:59:29 +00004554 }
4555 return result.Succeeded();
4556 }
4557
Greg Clayton437b5bc2012-09-27 22:26:11 +00004558 OptionGroupOptions m_option_group;
4559 OptionGroupUUID m_uuid_option_group;
4560 OptionGroupFile m_file_option;
4561 OptionGroupBoolean m_current_frame_option;
4562
4563
Greg Clayton3508c382012-02-24 01:59:29 +00004564};
4565
4566
4567#pragma mark CommandObjectTargetSymbols
4568
4569//-------------------------------------------------------------------------
4570// CommandObjectTargetSymbols
4571//-------------------------------------------------------------------------
4572
4573class CommandObjectTargetSymbols : public CommandObjectMultiword
4574{
4575public:
4576 //------------------------------------------------------------------
4577 // Constructors and Destructors
4578 //------------------------------------------------------------------
4579 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4580 CommandObjectMultiword (interpreter,
4581 "target symbols",
4582 "A set of commands for adding and managing debug symbol files.",
4583 "target symbols <sub-command> ...")
4584 {
4585 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
4586
4587 }
4588 virtual
4589 ~CommandObjectTargetSymbols()
4590 {
4591 }
4592
4593private:
4594 //------------------------------------------------------------------
4595 // For CommandObjectTargetModules only
4596 //------------------------------------------------------------------
4597 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4598};
4599
4600
Jim Inghamd60d94a2011-03-11 03:53:59 +00004601#pragma mark CommandObjectTargetStopHookAdd
4602
4603//-------------------------------------------------------------------------
4604// CommandObjectTargetStopHookAdd
4605//-------------------------------------------------------------------------
4606
Jim Inghamda26bd22012-06-08 21:56:10 +00004607class CommandObjectTargetStopHookAdd : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00004608{
4609public:
4610
4611 class CommandOptions : public Options
4612 {
4613 public:
Greg Claytonf15996e2011-04-07 22:46:35 +00004614 CommandOptions (CommandInterpreter &interpreter) :
4615 Options(interpreter),
Jim Inghamd60d94a2011-03-11 03:53:59 +00004616 m_line_start(0),
4617 m_line_end (UINT_MAX),
4618 m_func_name_type_mask (eFunctionNameTypeAuto),
4619 m_sym_ctx_specified (false),
Johnny Chen60fe60e2011-05-02 23:47:55 +00004620 m_thread_specified (false),
4621 m_use_one_liner (false),
4622 m_one_liner()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004623 {
4624 }
4625
4626 ~CommandOptions () {}
4627
Greg Claytonb3448432011-03-24 21:19:54 +00004628 const OptionDefinition*
Jim Inghamd60d94a2011-03-11 03:53:59 +00004629 GetDefinitions ()
4630 {
4631 return g_option_table;
4632 }
4633
4634 virtual Error
Greg Clayton143fcc32011-04-13 00:18:08 +00004635 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004636 {
4637 Error error;
Greg Clayton6475c422012-12-04 00:32:51 +00004638 const int short_option = m_getopt_table[option_idx].val;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004639 bool success;
4640
4641 switch (short_option)
4642 {
4643 case 'c':
4644 m_class_name = option_arg;
4645 m_sym_ctx_specified = true;
4646 break;
4647
4648 case 'e':
4649 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4650 if (!success)
4651 {
Greg Clayton9c236732011-10-26 00:56:27 +00004652 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004653 break;
4654 }
4655 m_sym_ctx_specified = true;
4656 break;
4657
4658 case 'l':
4659 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4660 if (!success)
4661 {
Greg Clayton9c236732011-10-26 00:56:27 +00004662 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004663 break;
4664 }
4665 m_sym_ctx_specified = true;
4666 break;
Sean Callanan9ad19532012-02-11 01:22:21 +00004667
4668 case 'i':
4669 m_no_inlines = true;
4670 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004671
4672 case 'n':
4673 m_function_name = option_arg;
4674 m_func_name_type_mask |= eFunctionNameTypeAuto;
4675 m_sym_ctx_specified = true;
4676 break;
4677
4678 case 'f':
4679 m_file_name = option_arg;
4680 m_sym_ctx_specified = true;
4681 break;
4682 case 's':
4683 m_module_name = option_arg;
4684 m_sym_ctx_specified = true;
4685 break;
4686 case 't' :
4687 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004688 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004689 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton9c236732011-10-26 00:56:27 +00004690 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004691 m_thread_specified = true;
4692 }
4693 break;
4694 case 'T':
4695 m_thread_name = option_arg;
4696 m_thread_specified = true;
4697 break;
4698 case 'q':
4699 m_queue_name = option_arg;
4700 m_thread_specified = true;
4701 break;
4702 case 'x':
4703 {
Jim Ingham7a4c8ea2011-03-22 01:53:33 +00004704 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004705 if (m_thread_id == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +00004706 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004707 m_thread_specified = true;
4708 }
4709 break;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004710 case 'o':
4711 m_use_one_liner = true;
4712 m_one_liner = option_arg;
4713 break;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004714 default:
Greg Clayton9c236732011-10-26 00:56:27 +00004715 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004716 break;
4717 }
4718 return error;
4719 }
4720
4721 void
Greg Clayton143fcc32011-04-13 00:18:08 +00004722 OptionParsingStarting ()
Jim Inghamd60d94a2011-03-11 03:53:59 +00004723 {
4724 m_class_name.clear();
4725 m_function_name.clear();
4726 m_line_start = 0;
4727 m_line_end = UINT_MAX;
4728 m_file_name.clear();
4729 m_module_name.clear();
4730 m_func_name_type_mask = eFunctionNameTypeAuto;
4731 m_thread_id = LLDB_INVALID_THREAD_ID;
4732 m_thread_index = UINT32_MAX;
4733 m_thread_name.clear();
4734 m_queue_name.clear();
Sean Callanan9ad19532012-02-11 01:22:21 +00004735
4736 m_no_inlines = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004737 m_sym_ctx_specified = false;
4738 m_thread_specified = false;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004739
4740 m_use_one_liner = false;
4741 m_one_liner.clear();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004742 }
4743
4744
Greg Claytonb3448432011-03-24 21:19:54 +00004745 static OptionDefinition g_option_table[];
Jim Inghamd60d94a2011-03-11 03:53:59 +00004746
4747 std::string m_class_name;
4748 std::string m_function_name;
4749 uint32_t m_line_start;
4750 uint32_t m_line_end;
4751 std::string m_file_name;
4752 std::string m_module_name;
4753 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4754 lldb::tid_t m_thread_id;
4755 uint32_t m_thread_index;
4756 std::string m_thread_name;
4757 std::string m_queue_name;
4758 bool m_sym_ctx_specified;
Sean Callanan9ad19532012-02-11 01:22:21 +00004759 bool m_no_inlines;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004760 bool m_thread_specified;
Johnny Chen60fe60e2011-05-02 23:47:55 +00004761 // Instance variables to hold the values for one_liner options.
4762 bool m_use_one_liner;
4763 std::string m_one_liner;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004764 };
4765
4766 Options *
4767 GetOptions ()
4768 {
4769 return &m_options;
4770 }
4771
4772 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00004773 CommandObjectParsed (interpreter,
4774 "target stop-hook add ",
4775 "Add a hook to be executed when the target stops.",
4776 "target stop-hook add"),
Greg Claytonf15996e2011-04-07 22:46:35 +00004777 m_options (interpreter)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004778 {
4779 }
4780
4781 ~CommandObjectTargetStopHookAdd ()
4782 {
4783 }
4784
4785 static size_t
4786 ReadCommandsCallbackFunction (void *baton,
4787 InputReader &reader,
4788 lldb::InputReaderAction notification,
4789 const char *bytes,
4790 size_t bytes_len)
4791 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004792 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004793 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton);
Jim Inghame15511a2011-05-05 01:03:36 +00004794 static bool got_interrupted;
Caroline Tice892fadd2011-06-16 16:27:19 +00004795 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004796
4797 switch (notification)
4798 {
4799 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004800 if (!batch_mode)
4801 {
4802 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end.");
4803 if (reader.GetPrompt())
4804 out_stream->Printf ("%s", reader.GetPrompt());
4805 out_stream->Flush();
4806 }
Jim Inghame15511a2011-05-05 01:03:36 +00004807 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004808 break;
4809
4810 case eInputReaderDeactivate:
4811 break;
4812
4813 case eInputReaderReactivate:
Caroline Tice892fadd2011-06-16 16:27:19 +00004814 if (reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004815 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004816 out_stream->Printf ("%s", reader.GetPrompt());
4817 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004818 }
Jim Inghame15511a2011-05-05 01:03:36 +00004819 got_interrupted = false;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004820 break;
4821
Caroline Tice4a348082011-05-02 20:41:46 +00004822 case eInputReaderAsynchronousOutputWritten:
4823 break;
4824
Jim Inghamd60d94a2011-03-11 03:53:59 +00004825 case eInputReaderGotToken:
4826 if (bytes && bytes_len && baton)
4827 {
4828 StringList *commands = new_stop_hook->GetCommandPointer();
4829 if (commands)
4830 {
4831 commands->AppendString (bytes, bytes_len);
4832 }
4833 }
Caroline Tice892fadd2011-06-16 16:27:19 +00004834 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004835 {
Caroline Tice892fadd2011-06-16 16:27:19 +00004836 out_stream->Printf ("%s", reader.GetPrompt());
4837 out_stream->Flush();
Jim Inghamd60d94a2011-03-11 03:53:59 +00004838 }
4839 break;
4840
4841 case eInputReaderInterrupt:
4842 {
4843 // Finish, and cancel the stop hook.
4844 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004845 if (!batch_mode)
4846 {
4847 out_stream->Printf ("Stop hook cancelled.\n");
4848 out_stream->Flush();
4849 }
4850
Jim Inghamd60d94a2011-03-11 03:53:59 +00004851 reader.SetIsDone (true);
4852 }
Jim Inghame15511a2011-05-05 01:03:36 +00004853 got_interrupted = true;
Jim Inghamd60d94a2011-03-11 03:53:59 +00004854 break;
4855
4856 case eInputReaderEndOfFile:
4857 reader.SetIsDone (true);
4858 break;
4859
4860 case eInputReaderDone:
Caroline Tice892fadd2011-06-16 16:27:19 +00004861 if (!got_interrupted && !batch_mode)
4862 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004863 out_stream->Printf ("Stop hook #%" PRIu64 " added.\n", new_stop_hook->GetID());
Caroline Tice892fadd2011-06-16 16:27:19 +00004864 out_stream->Flush();
4865 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004866 break;
4867 }
4868
4869 return bytes_len;
4870 }
4871
Jim Inghamda26bd22012-06-08 21:56:10 +00004872protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00004873 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00004874 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004875 {
4876 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4877 if (target)
4878 {
4879 Target::StopHookSP new_hook_sp;
4880 target->AddStopHook (new_hook_sp);
4881
4882 // First step, make the specifier.
4883 std::auto_ptr<SymbolContextSpecifier> specifier_ap;
4884 if (m_options.m_sym_ctx_specified)
4885 {
4886 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
4887
4888 if (!m_options.m_module_name.empty())
4889 {
4890 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
4891 }
4892
4893 if (!m_options.m_class_name.empty())
4894 {
4895 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
4896 }
4897
4898 if (!m_options.m_file_name.empty())
4899 {
4900 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
4901 }
4902
4903 if (m_options.m_line_start != 0)
4904 {
4905 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
4906 }
4907
4908 if (m_options.m_line_end != UINT_MAX)
4909 {
4910 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4911 }
4912
4913 if (!m_options.m_function_name.empty())
4914 {
4915 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
4916 }
4917 }
4918
4919 if (specifier_ap.get())
4920 new_hook_sp->SetSpecifier (specifier_ap.release());
4921
4922 // Next see if any of the thread options have been entered:
4923
4924 if (m_options.m_thread_specified)
4925 {
4926 ThreadSpec *thread_spec = new ThreadSpec();
4927
4928 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
4929 {
4930 thread_spec->SetTID (m_options.m_thread_id);
4931 }
4932
4933 if (m_options.m_thread_index != UINT32_MAX)
4934 thread_spec->SetIndex (m_options.m_thread_index);
4935
4936 if (!m_options.m_thread_name.empty())
4937 thread_spec->SetName (m_options.m_thread_name.c_str());
4938
4939 if (!m_options.m_queue_name.empty())
4940 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
4941
4942 new_hook_sp->SetThreadSpecifier (thread_spec);
4943
4944 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004945 if (m_options.m_use_one_liner)
Jim Inghamd60d94a2011-03-11 03:53:59 +00004946 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004947 // Use one-liner.
4948 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malea5f35a4b2012-11-29 21:49:15 +00004949 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Inghamd60d94a2011-03-11 03:53:59 +00004950 }
Johnny Chen60fe60e2011-05-02 23:47:55 +00004951 else
Jim Inghamd60d94a2011-03-11 03:53:59 +00004952 {
Johnny Chen60fe60e2011-05-02 23:47:55 +00004953 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into
4954 // the new stop hook's command string.
4955 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
4956 if (!reader_sp)
4957 {
4958 result.AppendError("out of memory\n");
4959 result.SetStatus (eReturnStatusFailed);
4960 target->RemoveStopHookByID (new_hook_sp->GetID());
4961 return false;
4962 }
4963
4964 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction,
4965 new_hook_sp.get(), // baton
4966 eInputReaderGranularityLine, // token size, to pass to callback function
4967 "DONE", // end token
4968 "> ", // prompt
4969 true)); // echo input
4970 if (!err.Success())
4971 {
4972 result.AppendError (err.AsCString());
4973 result.SetStatus (eReturnStatusFailed);
4974 target->RemoveStopHookByID (new_hook_sp->GetID());
4975 return false;
4976 }
4977 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Jim Inghamd60d94a2011-03-11 03:53:59 +00004978 }
Jim Inghamd60d94a2011-03-11 03:53:59 +00004979 result.SetStatus (eReturnStatusSuccessFinishNoResult);
4980 }
4981 else
4982 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00004983 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00004984 result.SetStatus (eReturnStatusFailed);
4985 }
4986
4987 return result.Succeeded();
4988 }
4989private:
4990 CommandOptions m_options;
4991};
4992
Greg Claytonb3448432011-03-24 21:19:54 +00004993OptionDefinition
Jim Inghamd60d94a2011-03-11 03:53:59 +00004994CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
4995{
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00004996 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, 0, eArgTypeOneLiner,
Johnny Chen60fe60e2011-05-02 23:47:55 +00004997 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Jim Inghamd60d94a2011-03-11 03:53:59 +00004998 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4999 "Set the module within which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005000 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005001 "The stop hook is run only for the thread whose index matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005002 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005003 "The stop hook is run only for the thread whose TID matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005004 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005005 "The stop hook is run only for the thread whose thread name matches this argument."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005006 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005007 "The stop hook is run only for threads in the queue whose name is given by this argument."},
5008 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
5009 "Specify the source file within which the stop-hook is to be run." },
5010 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
5011 "Set the start of the line range for which the stop-hook is to be run."},
5012 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum,
5013 "Set the end of the line range for which the stop-hook is to be run."},
Filipe Cabecinhas560c5142012-09-11 16:09:27 +00005014 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, 0, eArgTypeClassName,
Jim Inghamd60d94a2011-03-11 03:53:59 +00005015 "Specify the class within which the stop-hook is to be run." },
5016 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
5017 "Set the function name within which the stop hook will be run." },
5018 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
5019};
5020
5021#pragma mark CommandObjectTargetStopHookDelete
5022
5023//-------------------------------------------------------------------------
5024// CommandObjectTargetStopHookDelete
5025//-------------------------------------------------------------------------
5026
Jim Inghamda26bd22012-06-08 21:56:10 +00005027class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005028{
5029public:
5030
5031 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005032 CommandObjectParsed (interpreter,
5033 "target stop-hook delete",
5034 "Delete a stop-hook.",
5035 "target stop-hook delete [<idx>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005036 {
5037 }
5038
5039 ~CommandObjectTargetStopHookDelete ()
5040 {
5041 }
5042
Jim Inghamda26bd22012-06-08 21:56:10 +00005043protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005044 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005045 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005046 {
5047 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5048 if (target)
5049 {
5050 // FIXME: see if we can use the breakpoint id style parser?
5051 size_t num_args = command.GetArgumentCount();
5052 if (num_args == 0)
5053 {
5054 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5055 {
5056 result.SetStatus (eReturnStatusFailed);
5057 return false;
5058 }
5059 else
5060 {
5061 target->RemoveAllStopHooks();
5062 }
5063 }
5064 else
5065 {
5066 bool success;
5067 for (size_t i = 0; i < num_args; i++)
5068 {
5069 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5070 if (!success)
5071 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005072 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005073 result.SetStatus(eReturnStatusFailed);
5074 return false;
5075 }
5076 success = target->RemoveStopHookByID (user_id);
5077 if (!success)
5078 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005079 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005080 result.SetStatus(eReturnStatusFailed);
5081 return false;
5082 }
5083 }
5084 }
5085 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5086 }
5087 else
5088 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005089 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005090 result.SetStatus (eReturnStatusFailed);
5091 }
5092
5093 return result.Succeeded();
5094 }
5095};
5096#pragma mark CommandObjectTargetStopHookEnableDisable
5097
5098//-------------------------------------------------------------------------
5099// CommandObjectTargetStopHookEnableDisable
5100//-------------------------------------------------------------------------
5101
Jim Inghamda26bd22012-06-08 21:56:10 +00005102class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005103{
5104public:
5105
5106 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005107 CommandObjectParsed (interpreter,
5108 name,
5109 help,
5110 syntax),
Jim Inghamd60d94a2011-03-11 03:53:59 +00005111 m_enable (enable)
5112 {
5113 }
5114
5115 ~CommandObjectTargetStopHookEnableDisable ()
5116 {
5117 }
5118
Jim Inghamda26bd22012-06-08 21:56:10 +00005119protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005120 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005121 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005122 {
5123 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5124 if (target)
5125 {
5126 // FIXME: see if we can use the breakpoint id style parser?
5127 size_t num_args = command.GetArgumentCount();
5128 bool success;
5129
5130 if (num_args == 0)
5131 {
5132 target->SetAllStopHooksActiveState (m_enable);
5133 }
5134 else
5135 {
5136 for (size_t i = 0; i < num_args; i++)
5137 {
5138 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5139 if (!success)
5140 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005141 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005142 result.SetStatus(eReturnStatusFailed);
5143 return false;
5144 }
5145 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5146 if (!success)
5147 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005148 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005149 result.SetStatus(eReturnStatusFailed);
5150 return false;
5151 }
5152 }
5153 }
5154 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5155 }
5156 else
5157 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005158 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005159 result.SetStatus (eReturnStatusFailed);
5160 }
5161 return result.Succeeded();
5162 }
5163private:
5164 bool m_enable;
5165};
5166
5167#pragma mark CommandObjectTargetStopHookList
5168
5169//-------------------------------------------------------------------------
5170// CommandObjectTargetStopHookList
5171//-------------------------------------------------------------------------
5172
Jim Inghamda26bd22012-06-08 21:56:10 +00005173class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Inghamd60d94a2011-03-11 03:53:59 +00005174{
5175public:
5176
5177 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +00005178 CommandObjectParsed (interpreter,
5179 "target stop-hook list",
5180 "List all stop-hooks.",
5181 "target stop-hook list [<type>]")
Jim Inghamd60d94a2011-03-11 03:53:59 +00005182 {
5183 }
5184
5185 ~CommandObjectTargetStopHookList ()
5186 {
5187 }
5188
Jim Inghamda26bd22012-06-08 21:56:10 +00005189protected:
Jim Inghamd60d94a2011-03-11 03:53:59 +00005190 bool
Jim Inghamda26bd22012-06-08 21:56:10 +00005191 DoExecute (Args& command, CommandReturnObject &result)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005192 {
5193 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen9fc16922011-11-29 23:56:14 +00005194 if (!target)
Jim Inghamd60d94a2011-03-11 03:53:59 +00005195 {
Greg Claytonabe0fed2011-04-18 08:33:37 +00005196 result.AppendError ("invalid target\n");
Jim Inghamd60d94a2011-03-11 03:53:59 +00005197 result.SetStatus (eReturnStatusFailed);
Jason Molenda6e3a2412011-09-23 21:15:42 +00005198 return result.Succeeded();
Jim Inghamd60d94a2011-03-11 03:53:59 +00005199 }
5200
5201 size_t num_hooks = target->GetNumStopHooks ();
5202 if (num_hooks == 0)
5203 {
5204 result.GetOutputStream().PutCString ("No stop hooks.\n");
5205 }
5206 else
5207 {
5208 for (size_t i = 0; i < num_hooks; i++)
5209 {
5210 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5211 if (i > 0)
5212 result.GetOutputStream().PutCString ("\n");
5213 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5214 }
5215 }
Johnny Chen6c7c3902011-11-30 19:09:20 +00005216 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Inghamd60d94a2011-03-11 03:53:59 +00005217 return result.Succeeded();
5218 }
5219};
5220
5221#pragma mark CommandObjectMultiwordTargetStopHooks
5222//-------------------------------------------------------------------------
5223// CommandObjectMultiwordTargetStopHooks
5224//-------------------------------------------------------------------------
5225
5226class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5227{
5228public:
5229
5230 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5231 CommandObjectMultiword (interpreter,
5232 "target stop-hook",
5233 "A set of commands for operating on debugger target stop-hooks.",
5234 "target stop-hook <subcommand> [<subcommand-options>]")
5235 {
5236 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5237 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5238 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5239 false,
5240 "target stop-hook disable [<id>]",
5241 "Disable a stop-hook.",
5242 "target stop-hook disable")));
5243 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5244 true,
5245 "target stop-hook enable [<id>]",
5246 "Enable a stop-hook.",
5247 "target stop-hook enable")));
5248 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5249 }
5250
5251 ~CommandObjectMultiwordTargetStopHooks()
5252 {
5253 }
5254};
5255
5256
Chris Lattner24943d22010-06-08 16:52:24 +00005257
5258#pragma mark CommandObjectMultiwordTarget
5259
5260//-------------------------------------------------------------------------
5261// CommandObjectMultiwordTarget
5262//-------------------------------------------------------------------------
5263
Greg Clayton63094e02010-06-23 01:19:29 +00005264CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00005265 CommandObjectMultiword (interpreter,
5266 "target",
Chris Lattner24943d22010-06-08 16:52:24 +00005267 "A set of commands for operating on debugger targets.",
5268 "target <subcommand> [<subcommand-options>]")
5269{
Greg Claytonabe0fed2011-04-18 08:33:37 +00005270
5271 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton153ccd72011-08-10 02:10:13 +00005272 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Claytonabe0fed2011-04-18 08:33:37 +00005273 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5274 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Inghamd60d94a2011-03-11 03:53:59 +00005275 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytone1f50b92011-05-03 22:09:39 +00005276 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Clayton3508c382012-02-24 01:59:29 +00005277 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton801417e2011-07-07 01:59:51 +00005278 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00005279}
5280
5281CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5282{
5283}
5284
Greg Claytonabe0fed2011-04-18 08:33:37 +00005285