blob: 024f7b5a041565150f05dc7dabf1af499b99e538 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectTarget.h"
13
14// C Includes
15#include <errno.h>
Greg Claytonc4a99bc2011-02-01 01:13:32 +000016
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
19// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000022#include "lldb/Core/IOHandler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000025#include "lldb/Core/Section.h"
Greg Clayton7260f622011-04-18 08:33:37 +000026#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton644247c2011-07-07 01:59:51 +000028#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000029#include "lldb/DataFormatters/ValueObjectPrinter.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000030#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000033#include "lldb/Interpreter/Options.h"
Greg Clayton7260f622011-04-18 08:33:37 +000034#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000035#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000036#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000037#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton715c2362011-07-07 04:38:25 +000038#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Clayton7260f622011-04-18 08:33:37 +000039#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000040#include "lldb/Interpreter/OptionGroupUInt64.h"
41#include "lldb/Interpreter/OptionGroupUUID.h"
Greg Clayton644247c2011-07-07 01:59:51 +000042#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000044#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000045#include "lldb/Symbol/LineTable.h"
46#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Symbol/SymbolFile.h"
48#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000049#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000050#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000052#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000053#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000055#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
57using namespace lldb;
58using namespace lldb_private;
59
Greg Clayton7260f622011-04-18 08:33:37 +000060
61
62static void
63DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
64{
Greg Clayton176761e2011-04-19 04:19:37 +000065 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000066
Greg Claytonaa149cb2011-08-11 02:48:45 +000067 Module *exe_module = target->GetExecutableModulePointer();
Greg Clayton7260f622011-04-18 08:33:37 +000068 char exe_path[PATH_MAX];
69 bool exe_valid = false;
Greg Claytonaa149cb2011-08-11 02:48:45 +000070 if (exe_module)
71 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000072
Greg Clayton7260f622011-04-18 08:33:37 +000073 if (!exe_valid)
74 ::strcpy (exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000075
Greg Clayton7260f622011-04-18 08:33:37 +000076 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000077
Greg Clayton7260f622011-04-18 08:33:37 +000078 uint32_t properties = 0;
79 if (target_arch.IsValid())
80 {
81 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
82 properties++;
83 }
84 PlatformSP platform_sp (target->GetPlatform());
85 if (platform_sp)
Greg Clayton57abc5d2013-05-10 21:47:16 +000086 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000087
Greg Clayton7260f622011-04-18 08:33:37 +000088 ProcessSP process_sp (target->GetProcessSP());
89 bool show_process_status = false;
90 if (process_sp)
91 {
92 lldb::pid_t pid = process_sp->GetID();
93 StateType state = process_sp->GetState();
94 if (show_stopped_process_status)
Greg Clayton2637f822011-11-17 01:23:07 +000095 show_process_status = StateIsStoppedState(state, true);
Greg Clayton7260f622011-04-18 08:33:37 +000096 const char *state_cstr = StateAsCString (state);
97 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +000098 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Clayton7260f622011-04-18 08:33:37 +000099 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
100 }
101 if (properties > 0)
102 strm.PutCString (" )\n");
103 else
104 strm.EOL();
105 if (show_process_status)
106 {
107 const bool only_threads_with_stop_reason = true;
108 const uint32_t start_frame = 0;
109 const uint32_t num_frames = 1;
110 const uint32_t num_frames_with_source = 1;
111 process_sp->GetStatus (strm);
112 process_sp->GetThreadStatus (strm,
113 only_threads_with_stop_reason,
114 start_frame,
115 num_frames,
116 num_frames_with_source);
117
118 }
119}
120
121static uint32_t
122DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
123{
124 const uint32_t num_targets = target_list.GetNumTargets();
125 if (num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000126 {
Greg Clayton7260f622011-04-18 08:33:37 +0000127 TargetSP selected_target_sp (target_list.GetSelectedTarget());
128 strm.PutCString ("Current targets:\n");
129 for (uint32_t i=0; i<num_targets; ++i)
130 {
131 TargetSP target_sp (target_list.GetTargetAtIndex (i));
132 if (target_sp)
133 {
134 bool is_selected = target_sp.get() == selected_target_sp.get();
135 DumpTargetInfo (i,
136 target_sp.get(),
137 is_selected ? "* " : " ",
138 show_stopped_process_status,
139 strm);
140 }
141 }
142 }
143 return num_targets;
144}
145#pragma mark CommandObjectTargetCreate
146
147//-------------------------------------------------------------------------
148// "target create"
149//-------------------------------------------------------------------------
150
Jim Ingham5a988412012-06-08 21:56:10 +0000151class CommandObjectTargetCreate : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000152{
153public:
154 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000155 CommandObjectParsed (interpreter,
156 "target create",
157 "Create a target using the argument as the main executable.",
158 NULL),
Greg Clayton7260f622011-04-18 08:33:37 +0000159 m_option_group (interpreter),
Greg Clayton644247c2011-07-07 01:59:51 +0000160 m_arch_option (),
Greg Claytonc3776bf2012-02-09 06:16:32 +0000161 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Greg Clayton1c5f1862012-11-30 19:05:35 +0000162 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000163 m_platform_path (LLDB_OPT_SET_1, false, "platform-path", 'P', 0, eArgTypePath, "Path to the remote file to use for this target."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000164 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."),
Jim Inghambf22b962013-07-11 01:47:46 +0000165 m_remote_file (LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename, "Fullpath to the file on the remote host if debugging remotely."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000166 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 Clayton7260f622011-04-18 08:33:37 +0000167 {
168 CommandArgumentEntry arg;
169 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170
Greg Clayton7260f622011-04-18 08:33:37 +0000171 // Define the first (and only) variant of this arg.
172 file_arg.arg_type = eArgTypeFilename;
173 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000174
Greg Clayton7260f622011-04-18 08:33:37 +0000175 // There is only one variant this argument could be; put it into the argument entry.
176 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000177
Greg Clayton7260f622011-04-18 08:33:37 +0000178 // Push the data for the first argument into the m_arguments vector.
179 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000180
Greg Clayton644247c2011-07-07 01:59:51 +0000181 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000182 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000183 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000184 m_option_group.Append (&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000185 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Jim Inghambf22b962013-07-11 01:47:46 +0000186 m_option_group.Append (&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000187 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000188 m_option_group.Finalize();
189 }
190
191 ~CommandObjectTargetCreate ()
192 {
193 }
194
195 Options *
196 GetOptions ()
197 {
198 return &m_option_group;
199 }
200
Greg Claytonc7bece562013-01-25 18:06:21 +0000201 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +0000202 HandleArgumentCompletion (Args &input,
203 int &cursor_index,
204 int &cursor_char_position,
205 OptionElementVector &opt_element_vector,
206 int match_start_point,
207 int max_return_elements,
208 bool &word_complete,
209 StringList &matches)
210 {
211 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
212 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000213
Jim Ingham5a988412012-06-08 21:56:10 +0000214 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
215 CommandCompletions::eDiskFileCompletion,
216 completion_str.c_str(),
217 match_start_point,
218 max_return_elements,
219 NULL,
220 word_complete,
221 matches);
222 return matches.GetSize();
223 }
224
225protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000226 bool
Jim Ingham5a988412012-06-08 21:56:10 +0000227 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000228 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000229 const size_t argc = command.GetArgumentCount();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000230 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
Jim Inghambf22b962013-07-11 01:47:46 +0000231 FileSpec remote_file (m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000232
Greg Clayton5acc1252014-08-15 18:00:45 +0000233 if (core_file)
234 {
235 if (!core_file.Exists())
236 {
237 result.AppendErrorWithFormat("core file '%s' doesn't exist", core_file.GetPath().c_str());
238 result.SetStatus (eReturnStatusFailed);
239 return false;
240
241 }
242 if (!core_file.Readable())
243 {
244 result.AppendErrorWithFormat("core file '%s' is not readable", core_file.GetPath().c_str());
245 result.SetStatus (eReturnStatusFailed);
246 return false;
247 }
248 }
249
Daniel Maleae0f8f572013-08-26 23:57:52 +0000250 if (argc == 1 || core_file || remote_file)
Greg Clayton7260f622011-04-18 08:33:37 +0000251 {
Greg Clayton1c5f1862012-11-30 19:05:35 +0000252 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
253 if (symfile)
254 {
Greg Clayton5acc1252014-08-15 18:00:45 +0000255 if (symfile.Exists())
256 {
257 if (!symfile.Readable())
258 {
259 result.AppendErrorWithFormat("symbol file '%s' is not readable", core_file.GetPath().c_str());
260 result.SetStatus (eReturnStatusFailed);
261 return false;
262 }
263 }
264 else
Greg Clayton1c5f1862012-11-30 19:05:35 +0000265 {
266 char symfile_path[PATH_MAX];
267 symfile.GetPath(symfile_path, sizeof(symfile_path));
268 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
269 result.SetStatus (eReturnStatusFailed);
270 return false;
271 }
272 }
273
Greg Clayton7260f622011-04-18 08:33:37 +0000274 const char *file_path = command.GetArgumentAtIndex(0);
275 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000276 FileSpec file_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277
Daniel Maleae0f8f572013-08-26 23:57:52 +0000278 if (file_path)
279 file_spec.SetFile (file_path, true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000280
Daniel Maleae0f8f572013-08-26 23:57:52 +0000281 bool must_set_platform_path = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282
Greg Clayton7260f622011-04-18 08:33:37 +0000283 Debugger &debugger = m_interpreter.GetDebugger();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000284 PlatformSP platform_sp(debugger.GetPlatformList().GetSelectedPlatform ());
285
286 if (remote_file)
287 {
288 // I have a remote file.. two possible cases
289 if (file_spec && file_spec.Exists())
290 {
291 // if the remote file does not exist, push it there
292 if (!platform_sp->GetFileExists (remote_file))
293 {
294 Error err = platform_sp->PutFile(file_spec, remote_file);
295 if (err.Fail())
296 {
297 result.AppendError(err.AsCString());
298 result.SetStatus (eReturnStatusFailed);
299 return false;
300 }
301 }
302 }
303 else
304 {
305 // there is no local file and we need one
306 // in order to make the remote ---> local transfer we need a platform
307 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
308 if (!platform_sp)
309 {
310 result.AppendError("unable to perform remote debugging without a platform");
311 result.SetStatus (eReturnStatusFailed);
312 return false;
313 }
314 if (file_path)
315 {
316 // copy the remote file to the local file
317 Error err = platform_sp->GetFile(remote_file, file_spec);
318 if (err.Fail())
319 {
320 result.AppendError(err.AsCString());
321 result.SetStatus (eReturnStatusFailed);
322 return false;
323 }
324 }
325 else
326 {
327 // make up a local file
328 result.AppendError("remote --> local transfer without local path is not implemented yet");
329 result.SetStatus (eReturnStatusFailed);
330 return false;
331 }
332 }
333 }
334
335 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000336 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Clayton1c5f1862012-11-30 19:05:35 +0000337 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000338 Error error (debugger.GetTargetList().CreateTarget (debugger,
Daniel Maleae0f8f572013-08-26 23:57:52 +0000339// remote_file ? remote_file : file_spec,
Greg Claytona0ca6602012-10-18 16:33:33 +0000340 file_path,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000341 arch_cstr,
342 get_dependent_files,
343 &m_platform_options,
344 target_sp));
345
Greg Clayton7260f622011-04-18 08:33:37 +0000346 if (target_sp)
347 {
Jim Inghambf22b962013-07-11 01:47:46 +0000348 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000349 {
350 ModuleSP module_sp (target_sp->GetExecutableModule());
351 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000352 {
353 if (symfile)
354 module_sp->SetSymbolFileFileSpec(symfile);
355 if (remote_file)
356 {
357 std::string remote_path = remote_file.GetPath();
358 target_sp->SetArg0(remote_path.c_str());
359 module_sp->SetPlatformFileSpec(remote_file);
360 }
361 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000362 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000363
Greg Clayton7260f622011-04-18 08:33:37 +0000364 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000365 if (must_set_platform_path)
366 {
367 ModuleSpec main_module_spec(file_spec);
368 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
369 if (module_sp)
370 module_sp->SetPlatformFileSpec(remote_file);
371 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000372 if (core_file)
373 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000374 char core_path[PATH_MAX];
375 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000376 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000377 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000378 FileSpec core_file_dir;
379 core_file_dir.GetDirectory() = core_file.GetDirectory();
380 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000381
Greg Claytonc859e2d2012-02-13 23:10:39 +0000382 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
383
384 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000385 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000386 // Seems wierd that we Launch a core file, but that is
387 // what we do!
388 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000389
Greg Claytonc859e2d2012-02-13 23:10:39 +0000390 if (error.Fail())
391 {
392 result.AppendError(error.AsCString("can't find plug-in for core file"));
393 result.SetStatus (eReturnStatusFailed);
394 return false;
395 }
396 else
397 {
398 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
399 result.SetStatus (eReturnStatusSuccessFinishNoResult);
400 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000401 }
402 else
403 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000404 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
405 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000406 }
407 }
408 else
409 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000410 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000411 result.SetStatus (eReturnStatusFailed);
412 }
413 }
414 else
415 {
416 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
417 result.SetStatus (eReturnStatusSuccessFinishNoResult);
418 }
Greg Clayton7260f622011-04-18 08:33:37 +0000419 }
420 else
421 {
422 result.AppendError(error.AsCString());
423 result.SetStatus (eReturnStatusFailed);
424 }
425 }
426 else
427 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000428 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core-file option.\n", m_cmd_name.c_str());
Greg Clayton7260f622011-04-18 08:33:37 +0000429 result.SetStatus (eReturnStatusFailed);
430 }
431 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000432 }
433
Greg Clayton7260f622011-04-18 08:33:37 +0000434private:
435 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000436 OptionGroupArchitecture m_arch_option;
Greg Clayton7260f622011-04-18 08:33:37 +0000437 OptionGroupPlatform m_platform_options;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000438 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000439 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000440 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000441 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000442 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000443};
444
445#pragma mark CommandObjectTargetList
446
447//----------------------------------------------------------------------
448// "target list"
449//----------------------------------------------------------------------
450
Jim Ingham5a988412012-06-08 21:56:10 +0000451class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000452{
453public:
454 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000455 CommandObjectParsed (interpreter,
456 "target list",
457 "List all current targets in the current debug session.",
458 NULL,
459 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000460 {
461 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000462
Greg Clayton7260f622011-04-18 08:33:37 +0000463 virtual
464 ~CommandObjectTargetList ()
465 {
466 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000467
Jim Ingham5a988412012-06-08 21:56:10 +0000468protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000469 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000470 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000471 {
472 if (args.GetArgumentCount() == 0)
473 {
474 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000475
Greg Clayton7260f622011-04-18 08:33:37 +0000476 bool show_stopped_process_status = false;
477 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
478 {
479 strm.PutCString ("No targets.\n");
480 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000481 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000482 }
483 else
484 {
485 result.AppendError ("the 'target list' command takes no arguments\n");
486 result.SetStatus (eReturnStatusFailed);
487 }
488 return result.Succeeded();
489 }
490};
491
492
493#pragma mark CommandObjectTargetSelect
494
495//----------------------------------------------------------------------
496// "target select"
497//----------------------------------------------------------------------
498
Jim Ingham5a988412012-06-08 21:56:10 +0000499class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000500{
501public:
502 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000503 CommandObjectParsed (interpreter,
504 "target select",
505 "Select a target as the current target by target index.",
506 NULL,
507 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000508 {
509 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000510
Greg Clayton7260f622011-04-18 08:33:37 +0000511 virtual
512 ~CommandObjectTargetSelect ()
513 {
514 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000515
Jim Ingham5a988412012-06-08 21:56:10 +0000516protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000517 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000518 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000519 {
520 if (args.GetArgumentCount() == 1)
521 {
522 bool success = false;
523 const char *target_idx_arg = args.GetArgumentAtIndex(0);
524 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
525 if (success)
526 {
527 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
528 const uint32_t num_targets = target_list.GetNumTargets();
529 if (target_idx < num_targets)
530 {
531 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
532 if (target_sp)
533 {
534 Stream &strm = result.GetOutputStream();
535 target_list.SetSelectedTarget (target_sp.get());
536 bool show_stopped_process_status = false;
537 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000538 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000539 }
540 else
541 {
542 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
543 result.SetStatus (eReturnStatusFailed);
544 }
545 }
546 else
547 {
Todd Fiala352237d2014-08-29 20:14:21 +0000548 if (num_targets > 0)
549 {
550 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
551 target_idx,
552 num_targets - 1);
553 } else
554 {
555 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
556 target_idx);
557 }
Greg Clayton7260f622011-04-18 08:33:37 +0000558 result.SetStatus (eReturnStatusFailed);
559 }
560 }
561 else
562 {
563 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
564 result.SetStatus (eReturnStatusFailed);
565 }
566 }
567 else
568 {
569 result.AppendError ("'target select' takes a single argument: a target index\n");
570 result.SetStatus (eReturnStatusFailed);
571 }
572 return result.Succeeded();
573 }
574};
575
Greg Clayton3418c852011-08-10 02:10:13 +0000576#pragma mark CommandObjectTargetSelect
577
578//----------------------------------------------------------------------
579// "target delete"
580//----------------------------------------------------------------------
581
Jim Ingham5a988412012-06-08 21:56:10 +0000582class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000583{
584public:
585 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000586 CommandObjectParsed (interpreter,
587 "target delete",
588 "Delete one or more targets by target index.",
589 NULL,
590 0),
Greg Claytonaa149cb2011-08-11 02:48:45 +0000591 m_option_group (interpreter),
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000592 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', "Perform extra cleanup to minimize memory consumption after deleting the target.", false, false)
Greg Clayton3418c852011-08-10 02:10:13 +0000593 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000594 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
595 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000596 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000597
Greg Clayton3418c852011-08-10 02:10:13 +0000598 virtual
599 ~CommandObjectTargetDelete ()
600 {
601 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000602
Jim Ingham5a988412012-06-08 21:56:10 +0000603 Options *
604 GetOptions ()
605 {
606 return &m_option_group;
607 }
608
609protected:
Greg Clayton3418c852011-08-10 02:10:13 +0000610 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000611 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton3418c852011-08-10 02:10:13 +0000612 {
613 const size_t argc = args.GetArgumentCount();
614 std::vector<TargetSP> delete_target_list;
615 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
616 bool success = true;
617 TargetSP target_sp;
618 if (argc > 0)
619 {
620 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000621 // Bail out if don't have any targets.
622 if (num_targets == 0) {
623 result.AppendError("no targets to delete");
624 result.SetStatus(eReturnStatusFailed);
625 success = false;
626 }
627
Greg Clayton3418c852011-08-10 02:10:13 +0000628 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
629 {
630 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
631 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
632 if (success)
633 {
634 if (target_idx < num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000635 {
Greg Clayton3418c852011-08-10 02:10:13 +0000636 target_sp = target_list.GetTargetAtIndex (target_idx);
637 if (target_sp)
638 {
639 delete_target_list.push_back (target_sp);
640 continue;
641 }
642 }
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000643 if (num_targets > 1)
644 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
645 target_idx,
646 num_targets - 1);
647 else
648 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
649 target_idx);
650
Greg Clayton3418c852011-08-10 02:10:13 +0000651 result.SetStatus (eReturnStatusFailed);
652 success = false;
653 }
654 else
655 {
656 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
657 result.SetStatus (eReturnStatusFailed);
658 success = false;
659 }
660 }
Greg Clayton3418c852011-08-10 02:10:13 +0000661 }
662 else
663 {
664 target_sp = target_list.GetSelectedTarget();
665 if (target_sp)
666 {
667 delete_target_list.push_back (target_sp);
668 }
669 else
670 {
671 result.AppendErrorWithFormat("no target is currently selected\n");
672 result.SetStatus (eReturnStatusFailed);
673 success = false;
674 }
675 }
676 if (success)
677 {
678 const size_t num_targets_to_delete = delete_target_list.size();
679 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
680 {
681 target_sp = delete_target_list[idx];
682 target_list.DeleteTarget(target_sp);
683 target_sp->Destroy();
684 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000685 // If "--clean" was specified, prune any orphaned shared modules from
686 // the global shared module list
687 if (m_cleanup_option.GetOptionValue ())
688 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000689 const bool mandatory = true;
690 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000691 }
Greg Clayton3418c852011-08-10 02:10:13 +0000692 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
693 result.SetStatus(eReturnStatusSuccessFinishResult);
694 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000695
Greg Clayton3418c852011-08-10 02:10:13 +0000696 return result.Succeeded();
697 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000698
Greg Claytonaa149cb2011-08-11 02:48:45 +0000699 OptionGroupOptions m_option_group;
700 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000701};
702
Greg Clayton7260f622011-04-18 08:33:37 +0000703
Greg Clayton644247c2011-07-07 01:59:51 +0000704#pragma mark CommandObjectTargetVariable
705
706//----------------------------------------------------------------------
707// "target variable"
708//----------------------------------------------------------------------
709
Jim Ingham5a988412012-06-08 21:56:10 +0000710class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000711{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000712 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
713 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
714
Greg Clayton644247c2011-07-07 01:59:51 +0000715public:
716 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000717 CommandObjectParsed (interpreter,
718 "target variable",
Greg Claytondfdd1eb2012-11-03 00:10:22 +0000719 "Read global variable(s) prior to, or while running your binary.",
Jim Ingham5a988412012-06-08 21:56:10 +0000720 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000721 eFlagRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000722 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000723 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000724 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000725 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
726 SHORT_OPTION_FILE, 0, eArgTypeFilename,
727 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
728 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
729 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
730 "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."),
Greg Clayton644247c2011-07-07 01:59:51 +0000731 m_varobj_options()
732 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000733 CommandArgumentEntry arg;
734 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000735
Johnny Chen81ab3f52011-08-22 22:22:00 +0000736 // Define the first (and only) variant of this arg.
737 var_name_arg.arg_type = eArgTypeVarName;
738 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000739
Johnny Chen81ab3f52011-08-22 22:22:00 +0000740 // There is only one variant this argument could be; put it into the argument entry.
741 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000742
Johnny Chen81ab3f52011-08-22 22:22:00 +0000743 // Push the data for the first argument into the m_arguments vector.
744 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000745
Greg Clayton644247c2011-07-07 01:59:51 +0000746 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000747 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000748 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton644247c2011-07-07 01:59:51 +0000749 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
750 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
751 m_option_group.Finalize();
752 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000753
Greg Clayton644247c2011-07-07 01:59:51 +0000754 virtual
755 ~CommandObjectTargetVariable ()
756 {
757 }
Greg Clayton884fb692011-07-08 21:46:14 +0000758
759 void
760 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
761 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000762 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000763
Greg Clayton884fb692011-07-08 21:46:14 +0000764 switch (var_sp->GetScope())
765 {
766 case eValueTypeVariableGlobal:
767 if (m_option_variable.show_scope)
768 s.PutCString("GLOBAL: ");
769 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000770
Greg Clayton884fb692011-07-08 21:46:14 +0000771 case eValueTypeVariableStatic:
772 if (m_option_variable.show_scope)
773 s.PutCString("STATIC: ");
774 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000775
Greg Clayton884fb692011-07-08 21:46:14 +0000776 case eValueTypeVariableArgument:
777 if (m_option_variable.show_scope)
778 s.PutCString(" ARG: ");
779 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000780
Greg Clayton884fb692011-07-08 21:46:14 +0000781 case eValueTypeVariableLocal:
782 if (m_option_variable.show_scope)
783 s.PutCString(" LOCAL: ");
784 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000785
Greg Clayton884fb692011-07-08 21:46:14 +0000786 default:
787 break;
788 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000789
Greg Clayton45ba8542011-07-10 19:21:23 +0000790 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000791 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000792 bool show_fullpaths = false;
793 bool show_module = true;
794 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
795 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000796 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797
Greg Clayton1deb7962011-10-25 06:44:01 +0000798 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000799 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000800 options.SetFormat(format);
801
802 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000803
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000804 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000805 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000806
Greg Claytonc7bece562013-01-25 18:06:21 +0000807 static size_t GetVariableCallback (void *baton,
808 const char *name,
809 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000810 {
811 Target *target = static_cast<Target *>(baton);
812 if (target)
813 {
814 return target->GetImages().FindGlobalVariables (ConstString(name),
815 true,
816 UINT32_MAX,
817 variable_list);
818 }
819 return 0;
820 }
Greg Clayton884fb692011-07-08 21:46:14 +0000821
Jim Ingham5a988412012-06-08 21:56:10 +0000822 Options *
823 GetOptions ()
824 {
825 return &m_option_group;
826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000827
Jim Ingham5a988412012-06-08 21:56:10 +0000828protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000829 void
830 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
831 {
832 size_t count = variable_list.GetSize();
833 if (count > 0)
834 {
835 if (sc.module_sp)
836 {
837 if (sc.comp_unit)
838 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000839 s.Printf ("Global variables for %s in %s:\n",
840 sc.comp_unit->GetPath().c_str(),
841 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000842 }
843 else
844 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000845 s.Printf ("Global variables for %s\n",
846 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000847 }
848 }
849 else if (sc.comp_unit)
850 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000851 s.Printf ("Global variables for %s\n",
852 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000853 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000854
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000855 for (uint32_t i=0; i<count; ++i)
856 {
857 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
858 if (var_sp)
859 {
860 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000861
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000862 if (valobj_sp)
863 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
864 }
865 }
866 }
867
868 }
Greg Clayton644247c2011-07-07 01:59:51 +0000869 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000870 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton644247c2011-07-07 01:59:51 +0000871 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000872 Target *target = m_exe_ctx.GetTargetPtr();
873 const size_t argc = args.GetArgumentCount();
874 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000875
Greg Claytonf9fc6092013-01-09 19:44:40 +0000876 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000877 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000878
879 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000880 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000881 VariableList variable_list;
882 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000883
Greg Claytonf9fc6092013-01-09 19:44:40 +0000884 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000885 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000886 bool use_var_name = false;
887 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000888 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000889 RegularExpression regex(arg);
890 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000891 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000892 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000893 result.SetStatus (eReturnStatusFailed);
894 return false;
895 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000896 use_var_name = true;
897 matches = target->GetImages().FindGlobalVariables (regex,
898 true,
899 UINT32_MAX,
900 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000901 }
902 else
903 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000904 Error error (Variable::GetValuesForVariableExpressionPath (arg,
905 m_exe_ctx.GetBestExecutionContextScope(),
906 GetVariableCallback,
907 target,
908 variable_list,
909 valobj_list));
910 matches = variable_list.GetSize();
911 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000912
Greg Claytonf9fc6092013-01-09 19:44:40 +0000913 if (matches == 0)
914 {
915 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
916 result.SetStatus (eReturnStatusFailed);
917 return false;
918 }
919 else
920 {
921 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000922 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000923 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
924 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000925 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000926 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
927 if (!valobj_sp)
928 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000929
Greg Claytonf9fc6092013-01-09 19:44:40 +0000930 if (valobj_sp)
931 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000932 }
933 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000934 }
Greg Clayton644247c2011-07-07 01:59:51 +0000935 }
936 }
937 else
938 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000939 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
940 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
941 SymbolContextList sc_list;
942 const size_t num_compile_units = compile_units.GetSize();
943 const size_t num_shlibs = shlibs.GetSize();
944 if (num_compile_units == 0 && num_shlibs == 0)
945 {
946 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000947 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000948 CompileUnit *comp_unit = NULL;
949 if (frame)
950 {
951 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
952 if (sc.comp_unit)
953 {
954 const bool can_create = true;
955 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
956 if (comp_unit_varlist_sp)
957 {
958 size_t count = comp_unit_varlist_sp->GetSize();
959 if (count > 0)
960 {
961 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
962 success = true;
963 }
964 }
965 }
966 }
967 if (!success)
968 {
969 if (frame)
970 {
971 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000972 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
973 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000974 else
975 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000976 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000977 else
978 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
979 result.SetStatus (eReturnStatusFailed);
980 }
981 }
982 else
983 {
984 SymbolContextList sc_list;
985 const bool append = true;
986 // We have one or more compile unit or shlib
987 if (num_shlibs > 0)
988 {
989 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
990 {
991 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
992 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000993
Greg Claytonf9fc6092013-01-09 19:44:40 +0000994 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
995 if (module_sp)
996 {
997 if (num_compile_units > 0)
998 {
999 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1000 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1001 }
1002 else
1003 {
1004 SymbolContext sc;
1005 sc.module_sp = module_sp;
1006 sc_list.Append(sc);
1007 }
1008 }
1009 else
1010 {
1011 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001012 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1013 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001014 }
1015 }
1016 }
1017 else
1018 {
1019 // No shared libraries, we just want to find globals for the compile units files that were specified
1020 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1021 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1022 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001023
Greg Claytonf9fc6092013-01-09 19:44:40 +00001024 const uint32_t num_scs = sc_list.GetSize();
1025 if (num_scs > 0)
1026 {
1027 SymbolContext sc;
1028 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1029 {
1030 if (sc_list.GetContextAtIndex(sc_idx, sc))
1031 {
1032 if (sc.comp_unit)
1033 {
1034 const bool can_create = true;
1035 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1036 if (comp_unit_varlist_sp)
1037 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1038 }
1039 else if (sc.module_sp)
1040 {
1041 // Get all global variables for this module
1042 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1043 VariableList variable_list;
1044 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1045 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1046 }
1047 }
1048 }
1049 }
1050 }
Greg Clayton644247c2011-07-07 01:59:51 +00001051 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001052
Enrico Granata61a80ba2011-08-12 16:42:31 +00001053 if (m_interpreter.TruncationWarningNecessary())
1054 {
1055 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1056 m_cmd_name.c_str());
1057 m_interpreter.TruncationWarningGiven();
1058 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001059
Greg Clayton644247c2011-07-07 01:59:51 +00001060 return result.Succeeded();
1061 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001062
Greg Clayton644247c2011-07-07 01:59:51 +00001063 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001064 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001065 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001066 OptionGroupFileList m_option_compile_units;
1067 OptionGroupFileList m_option_shared_libraries;
1068 OptionGroupValueObjectDisplay m_varobj_options;
1069
1070};
1071
1072
Greg Claytoneffe5c92011-05-03 22:09:39 +00001073#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074
Jim Ingham5a988412012-06-08 21:56:10 +00001075class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076{
1077public:
1078
Greg Claytoneffe5c92011-05-03 22:09:39 +00001079 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001080 CommandObjectParsed (interpreter,
1081 "target modules search-paths add",
1082 "Add new image search paths substitution pairs to the current target.",
1083 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 {
Caroline Tice405fe672010-10-04 22:28:36 +00001085 CommandArgumentEntry arg;
1086 CommandArgumentData old_prefix_arg;
1087 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001088
Caroline Tice405fe672010-10-04 22:28:36 +00001089 // Define the first variant of this arg pair.
1090 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1091 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001092
Caroline Tice405fe672010-10-04 22:28:36 +00001093 // Define the first variant of this arg pair.
1094 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1095 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001096
Caroline Tice405fe672010-10-04 22:28:36 +00001097 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1098 // must always occur together, they are treated as two variants of one argument rather than two independent
1099 // arguments. Push them both into the first argument position for m_arguments...
1100
1101 arg.push_back (old_prefix_arg);
1102 arg.push_back (new_prefix_arg);
1103
1104 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 }
1106
Greg Claytoneffe5c92011-05-03 22:09:39 +00001107 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 {
1109 }
1110
Jim Ingham5a988412012-06-08 21:56:10 +00001111protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001113 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 CommandReturnObject &result)
1115 {
Greg Claytona7015092010-09-18 01:14:36 +00001116 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117 if (target)
1118 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001119 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 if (argc & 1)
1121 {
Greg Clayton7260f622011-04-18 08:33:37 +00001122 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 result.SetStatus (eReturnStatusFailed);
1124 }
1125 else
1126 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001127 for (size_t i=0; i<argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 {
1129 const char *from = command.GetArgumentAtIndex(i);
1130 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001131
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 if (from[0] && to[0])
1133 {
1134 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001135 target->GetImageSearchPathList().Append (ConstString(from),
1136 ConstString(to),
1137 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001138 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 }
1140 else
1141 {
1142 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001143 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 else
Greg Clayton7260f622011-04-18 08:33:37 +00001145 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 result.SetStatus (eReturnStatusFailed);
1147 }
1148 }
1149 }
1150 }
1151 else
1152 {
Greg Clayton7260f622011-04-18 08:33:37 +00001153 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 result.SetStatus (eReturnStatusFailed);
1155 }
1156 return result.Succeeded();
1157 }
1158};
1159
Greg Claytoneffe5c92011-05-03 22:09:39 +00001160#pragma mark CommandObjectTargetModulesSearchPathsClear
1161
Jim Ingham5a988412012-06-08 21:56:10 +00001162class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163{
1164public:
1165
Greg Claytoneffe5c92011-05-03 22:09:39 +00001166 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001167 CommandObjectParsed (interpreter,
1168 "target modules search-paths clear",
1169 "Clear all current image search path substitution pairs from the current target.",
1170 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 {
1172 }
1173
Greg Claytoneffe5c92011-05-03 22:09:39 +00001174 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175 {
1176 }
1177
Jim Ingham5a988412012-06-08 21:56:10 +00001178protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001180 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 CommandReturnObject &result)
1182 {
Greg Claytona7015092010-09-18 01:14:36 +00001183 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 if (target)
1185 {
1186 bool notify = true;
1187 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001188 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189 }
1190 else
1191 {
Greg Clayton7260f622011-04-18 08:33:37 +00001192 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 result.SetStatus (eReturnStatusFailed);
1194 }
1195 return result.Succeeded();
1196 }
1197};
1198
Greg Claytoneffe5c92011-05-03 22:09:39 +00001199#pragma mark CommandObjectTargetModulesSearchPathsInsert
1200
Jim Ingham5a988412012-06-08 21:56:10 +00001201class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202{
1203public:
1204
Greg Claytoneffe5c92011-05-03 22:09:39 +00001205 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001206 CommandObjectParsed (interpreter,
1207 "target modules search-paths insert",
1208 "Insert a new image search path substitution pair into the current target at the specified index.",
1209 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 {
Caroline Tice405fe672010-10-04 22:28:36 +00001211 CommandArgumentEntry arg1;
1212 CommandArgumentEntry arg2;
1213 CommandArgumentData index_arg;
1214 CommandArgumentData old_prefix_arg;
1215 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001216
Caroline Tice405fe672010-10-04 22:28:36 +00001217 // Define the first and only variant of this arg.
1218 index_arg.arg_type = eArgTypeIndex;
1219 index_arg.arg_repetition = eArgRepeatPlain;
1220
1221 // Put the one and only variant into the first arg for m_arguments:
1222 arg1.push_back (index_arg);
1223
1224 // Define the first variant of this arg pair.
1225 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1226 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001227
Caroline Tice405fe672010-10-04 22:28:36 +00001228 // Define the first variant of this arg pair.
1229 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1230 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001231
Caroline Tice405fe672010-10-04 22:28:36 +00001232 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1233 // must always occur together, they are treated as two variants of one argument rather than two independent
1234 // arguments. Push them both into the same argument position for m_arguments...
1235
1236 arg2.push_back (old_prefix_arg);
1237 arg2.push_back (new_prefix_arg);
1238
1239 // Add arguments to m_arguments.
1240 m_arguments.push_back (arg1);
1241 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242 }
1243
Greg Claytoneffe5c92011-05-03 22:09:39 +00001244 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245 {
1246 }
1247
Jim Ingham5a988412012-06-08 21:56:10 +00001248protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001250 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 CommandReturnObject &result)
1252 {
Greg Claytona7015092010-09-18 01:14:36 +00001253 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 if (target)
1255 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001256 size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257 // check for at least 3 arguments and an odd nubmer of parameters
1258 if (argc >= 3 && argc & 1)
1259 {
1260 bool success = false;
1261
1262 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1263
1264 if (!success)
1265 {
1266 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1267 result.SetStatus (eReturnStatusFailed);
1268 return result.Succeeded();
1269 }
1270
1271 // shift off the index
1272 command.Shift();
1273 argc = command.GetArgumentCount();
1274
1275 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1276 {
1277 const char *from = command.GetArgumentAtIndex(i);
1278 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001279
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280 if (from[0] && to[0])
1281 {
1282 bool last_pair = ((argc - i) == 2);
1283 target->GetImageSearchPathList().Insert (ConstString(from),
1284 ConstString(to),
1285 insert_idx,
1286 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001287 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288 }
1289 else
1290 {
1291 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001292 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293 else
Greg Clayton7260f622011-04-18 08:33:37 +00001294 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295 result.SetStatus (eReturnStatusFailed);
1296 return false;
1297 }
1298 }
1299 }
1300 else
1301 {
Greg Clayton7260f622011-04-18 08:33:37 +00001302 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303 result.SetStatus (eReturnStatusFailed);
1304 return result.Succeeded();
1305 }
1306
1307 }
1308 else
1309 {
Greg Clayton7260f622011-04-18 08:33:37 +00001310 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 result.SetStatus (eReturnStatusFailed);
1312 }
1313 return result.Succeeded();
1314 }
1315};
1316
Greg Claytoneffe5c92011-05-03 22:09:39 +00001317
1318#pragma mark CommandObjectTargetModulesSearchPathsList
1319
1320
Jim Ingham5a988412012-06-08 21:56:10 +00001321class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322{
1323public:
1324
Greg Claytoneffe5c92011-05-03 22:09:39 +00001325 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001326 CommandObjectParsed (interpreter,
1327 "target modules search-paths list",
1328 "List all current image search path substitution pairs in the current target.",
1329 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 {
1331 }
1332
Greg Claytoneffe5c92011-05-03 22:09:39 +00001333 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 {
1335 }
1336
Jim Ingham5a988412012-06-08 21:56:10 +00001337protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001339 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 CommandReturnObject &result)
1341 {
Greg Claytona7015092010-09-18 01:14:36 +00001342 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 if (target)
1344 {
1345 if (command.GetArgumentCount() != 0)
1346 {
Greg Clayton7260f622011-04-18 08:33:37 +00001347 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 result.SetStatus (eReturnStatusFailed);
1349 return result.Succeeded();
1350 }
1351
1352 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001353 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
1355 else
1356 {
Greg Clayton7260f622011-04-18 08:33:37 +00001357 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 result.SetStatus (eReturnStatusFailed);
1359 }
1360 return result.Succeeded();
1361 }
1362};
1363
Greg Claytoneffe5c92011-05-03 22:09:39 +00001364#pragma mark CommandObjectTargetModulesSearchPathsQuery
1365
Jim Ingham5a988412012-06-08 21:56:10 +00001366class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367{
1368public:
1369
Greg Claytoneffe5c92011-05-03 22:09:39 +00001370 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001371 CommandObjectParsed (interpreter,
1372 "target modules search-paths query",
1373 "Transform a path using the first applicable image search path.",
1374 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 {
Caroline Tice405fe672010-10-04 22:28:36 +00001376 CommandArgumentEntry arg;
1377 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001378
Caroline Tice405fe672010-10-04 22:28:36 +00001379 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001380 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001381 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001382
Caroline Tice405fe672010-10-04 22:28:36 +00001383 // There is only one variant this argument could be; put it into the argument entry.
1384 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001385
Caroline Tice405fe672010-10-04 22:28:36 +00001386 // Push the data for the first argument into the m_arguments vector.
1387 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 }
1389
Greg Claytoneffe5c92011-05-03 22:09:39 +00001390 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 {
1392 }
1393
Jim Ingham5a988412012-06-08 21:56:10 +00001394protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001396 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 CommandReturnObject &result)
1398 {
Greg Claytona7015092010-09-18 01:14:36 +00001399 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400 if (target)
1401 {
1402 if (command.GetArgumentCount() != 1)
1403 {
Greg Clayton7260f622011-04-18 08:33:37 +00001404 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405 result.SetStatus (eReturnStatusFailed);
1406 return result.Succeeded();
1407 }
1408
1409 ConstString orig(command.GetArgumentAtIndex(0));
1410 ConstString transformed;
1411 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1412 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1413 else
1414 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001415
1416 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417 }
1418 else
1419 {
Greg Clayton7260f622011-04-18 08:33:37 +00001420 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421 result.SetStatus (eReturnStatusFailed);
1422 }
1423 return result.Succeeded();
1424 }
1425};
1426
Greg Claytoneffe5c92011-05-03 22:09:39 +00001427//----------------------------------------------------------------------
1428// Static Helper functions
1429//----------------------------------------------------------------------
1430static void
1431DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1432{
1433 if (module)
1434 {
1435 const char *arch_cstr;
1436 if (full_triple)
1437 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1438 else
1439 arch_cstr = module->GetArchitecture().GetArchitectureName();
1440 if (width)
1441 strm.Printf("%-*s", width, arch_cstr);
1442 else
1443 strm.PutCString(arch_cstr);
1444 }
1445}
1446
1447static void
1448DumpModuleUUID (Stream &strm, Module *module)
1449{
Jim Ingham28eb5712012-10-12 17:34:26 +00001450 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001451 module->GetUUID().Dump (&strm);
1452 else
1453 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001454}
1455
1456static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001457DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1458 Stream &strm,
1459 Module *module,
1460 const FileSpec &file_spec,
1461 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001462{
1463 uint32_t num_matches = 0;
1464 if (module)
1465 {
1466 SymbolContextList sc_list;
1467 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1468 0,
1469 false,
1470 eSymbolContextCompUnit,
1471 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001472
Greg Claytoneffe5c92011-05-03 22:09:39 +00001473 for (uint32_t i=0; i<num_matches; ++i)
1474 {
1475 SymbolContext sc;
1476 if (sc_list.GetContextAtIndex(i, sc))
1477 {
1478 if (i > 0)
1479 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001480
Greg Claytoneffe5c92011-05-03 22:09:39 +00001481 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1482 << module->GetFileSpec().GetFilename() << "\n";
1483 LineTable *line_table = sc.comp_unit->GetLineTable();
1484 if (line_table)
1485 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001486 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001487 lldb::eDescriptionLevelBrief);
1488 else
1489 strm << "No line table";
1490 }
1491 }
1492 }
1493 return num_matches;
1494}
1495
1496static void
1497DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1498{
1499 if (file_spec_ptr)
1500 {
1501 if (width > 0)
1502 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001503 std::string fullpath = file_spec_ptr->GetPath();
1504 strm.Printf("%-*s", width, fullpath.c_str());
1505 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001506 }
1507 else
1508 {
1509 file_spec_ptr->Dump(&strm);
1510 return;
1511 }
1512 }
1513 // Keep the width spacing correct if things go wrong...
1514 if (width > 0)
1515 strm.Printf("%-*s", width, "");
1516}
1517
1518static void
1519DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1520{
1521 if (file_spec_ptr)
1522 {
1523 if (width > 0)
1524 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1525 else
1526 file_spec_ptr->GetDirectory().Dump(&strm);
1527 return;
1528 }
1529 // Keep the width spacing correct if things go wrong...
1530 if (width > 0)
1531 strm.Printf("%-*s", width, "");
1532}
1533
1534static void
1535DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1536{
1537 if (file_spec_ptr)
1538 {
1539 if (width > 0)
1540 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1541 else
1542 file_spec_ptr->GetFilename().Dump(&strm);
1543 return;
1544 }
1545 // Keep the width spacing correct if things go wrong...
1546 if (width > 0)
1547 strm.Printf("%-*s", width, "");
1548}
1549
1550
1551static void
1552DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1553{
1554 if (module)
1555 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001556 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1557 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001558 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001559 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001560 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001561 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001562 }
1563 }
1564}
1565
1566static void
1567DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1568{
1569 if (module)
1570 {
Greg Clayton3046e662013-07-10 01:23:25 +00001571 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001572 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001573 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001574 strm.Printf ("Sections for '%s' (%s):\n",
1575 module->GetSpecificationDescription().c_str(),
1576 module->GetArchitecture().GetArchitectureName());
1577 strm.IndentMore();
1578 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1579 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001580 }
1581 }
1582}
1583
1584static bool
1585DumpModuleSymbolVendor (Stream &strm, Module *module)
1586{
1587 if (module)
1588 {
1589 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1590 if (symbol_vendor)
1591 {
1592 symbol_vendor->Dump(&strm);
1593 return true;
1594 }
1595 }
1596 return false;
1597}
1598
Greg Claytonc4a8a762012-05-15 18:43:44 +00001599static void
1600DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1601{
1602 strm.IndentMore();
1603 strm.Indent (" Address: ");
1604 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1605 strm.PutCString (" (");
1606 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1607 strm.PutCString (")\n");
1608 strm.Indent (" Summary: ");
1609 const uint32_t save_indent = strm.GetIndentLevel ();
1610 strm.SetIndentLevel (save_indent + 13);
1611 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1612 strm.SetIndentLevel (save_indent);
1613 // Print out detailed address information when verbose is enabled
1614 if (verbose)
1615 {
1616 strm.EOL();
1617 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1618 }
1619 strm.IndentLess();
1620}
1621
Greg Claytoneffe5c92011-05-03 22:09:39 +00001622static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001623LookupAddressInModule (CommandInterpreter &interpreter,
1624 Stream &strm,
1625 Module *module,
1626 uint32_t resolve_mask,
1627 lldb::addr_t raw_addr,
1628 lldb::addr_t offset,
1629 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001630{
1631 if (module)
1632 {
1633 lldb::addr_t addr = raw_addr - offset;
1634 Address so_addr;
1635 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001636 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001637 if (target && !target->GetSectionLoadList().IsEmpty())
1638 {
1639 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1640 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001641 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001642 return false;
1643 }
1644 else
1645 {
1646 if (!module->ResolveFileAddress (addr, so_addr))
1647 return false;
1648 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001649
Greg Claytoneffe5c92011-05-03 22:09:39 +00001650 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001651 DumpAddress (exe_scope, so_addr, verbose, strm);
1652// strm.IndentMore();
1653// strm.Indent (" Address: ");
1654// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1655// strm.PutCString (" (");
1656// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1657// strm.PutCString (")\n");
1658// strm.Indent (" Summary: ");
1659// const uint32_t save_indent = strm.GetIndentLevel ();
1660// strm.SetIndentLevel (save_indent + 13);
1661// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1662// strm.SetIndentLevel (save_indent);
1663// // Print out detailed address information when verbose is enabled
1664// if (verbose)
1665// {
1666// strm.EOL();
1667// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1668// }
1669// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001670 return true;
1671 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001672
Greg Claytoneffe5c92011-05-03 22:09:39 +00001673 return false;
1674}
1675
1676static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001677LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001678{
1679 if (module)
1680 {
1681 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001682
Michael Sartaina7499c92013-07-01 19:45:50 +00001683 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1684 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001685 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001686 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001687 if (symtab)
1688 {
1689 uint32_t i;
1690 std::vector<uint32_t> match_indexes;
1691 ConstString symbol_name (name);
1692 uint32_t num_matches = 0;
1693 if (name_is_regex)
1694 {
1695 RegularExpression name_regexp(name);
1696 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1697 eSymbolTypeAny,
1698 match_indexes);
1699 }
1700 else
1701 {
1702 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1703 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001704
Greg Claytoneffe5c92011-05-03 22:09:39 +00001705 if (num_matches > 0)
1706 {
1707 strm.Indent ();
1708 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1709 name_is_regex ? "the regular expression " : "", name);
1710 DumpFullpath (strm, &module->GetFileSpec(), 0);
1711 strm.PutCString(":\n");
1712 strm.IndentMore ();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001713 //Symtab::DumpSymbolHeader (&strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001714 for (i=0; i < num_matches; ++i)
1715 {
1716 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Claytonc4a8a762012-05-15 18:43:44 +00001717 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1718 symbol->GetAddress(),
1719 verbose,
1720 strm);
1721
1722// strm.Indent ();
1723// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001724 }
1725 strm.IndentLess ();
1726 return num_matches;
1727 }
1728 }
1729 }
1730 }
1731 return 0;
1732}
1733
1734
1735static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001736DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001737{
1738 strm.IndentMore ();
1739 uint32_t i;
1740 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001741
Greg Claytoneffe5c92011-05-03 22:09:39 +00001742 for (i=0; i<num_matches; ++i)
1743 {
1744 SymbolContext sc;
1745 if (sc_list.GetContextAtIndex(i, sc))
1746 {
Sean Callananf6172c22012-02-11 00:24:04 +00001747 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001748
Sean Callananf6172c22012-02-11 00:24:04 +00001749 sc.GetAddressRange(eSymbolContextEverything,
1750 0,
1751 true,
1752 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001753
Greg Claytonc4a8a762012-05-15 18:43:44 +00001754 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001755 }
1756 }
1757 strm.IndentLess ();
1758}
1759
Greg Claytonc7bece562013-01-25 18:06:21 +00001760static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001761LookupFunctionInModule (CommandInterpreter &interpreter,
1762 Stream &strm,
1763 Module *module,
1764 const char *name,
1765 bool name_is_regex,
1766 bool include_inlines,
1767 bool include_symbols,
1768 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001769{
1770 if (module && name && name[0])
1771 {
1772 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001773 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001774 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001775 if (name_is_regex)
1776 {
1777 RegularExpression function_name_regex (name);
1778 num_matches = module->FindFunctions (function_name_regex,
1779 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001780 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001781 append,
1782 sc_list);
1783 }
1784 else
1785 {
1786 ConstString function_name (name);
Sean Callananb6d70eb2011-10-12 02:08:07 +00001787 num_matches = module->FindFunctions (function_name,
1788 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001789 eFunctionNameTypeAuto,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001790 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001791 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001792 append,
1793 sc_list);
1794 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001795
Greg Claytoneffe5c92011-05-03 22:09:39 +00001796 if (num_matches)
1797 {
1798 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001799 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001800 DumpFullpath (strm, &module->GetFileSpec(), 0);
1801 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001802 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001803 }
1804 return num_matches;
1805 }
1806 return 0;
1807}
1808
Greg Claytonc7bece562013-01-25 18:06:21 +00001809static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001810LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001811 Stream &strm,
1812 Module *module,
1813 const char *name_cstr,
1814 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001815{
1816 if (module && name_cstr && name_cstr[0])
1817 {
Greg Claytond1767f02011-12-08 02:13:16 +00001818 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001819 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001820 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001821 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001822 SymbolContext sc;
1823
1824 ConstString name(name_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001825 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001826
Greg Claytond1767f02011-12-08 02:13:16 +00001827 if (num_matches)
1828 {
1829 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001830 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001831 DumpFullpath (strm, &module->GetFileSpec(), 0);
1832 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001833 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001834 {
Greg Claytond1767f02011-12-08 02:13:16 +00001835 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001836 {
Greg Claytond1767f02011-12-08 02:13:16 +00001837 // Resolve the clang type so that any forward references
1838 // to types that haven't yet been parsed will get parsed.
1839 type_sp->GetClangFullType ();
1840 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001841 // Print all typedef chains
1842 TypeSP typedef_type_sp (type_sp);
1843 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1844 while (typedefed_type_sp)
1845 {
1846 strm.EOL();
1847 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1848 typedefed_type_sp->GetClangFullType ();
1849 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1850 typedef_type_sp = typedefed_type_sp;
1851 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1852 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001853 }
Greg Claytond1767f02011-12-08 02:13:16 +00001854 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001855 }
Greg Claytond1767f02011-12-08 02:13:16 +00001856 }
1857 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001858 }
1859 return 0;
1860}
1861
Greg Claytonc7bece562013-01-25 18:06:21 +00001862static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001863LookupTypeHere (CommandInterpreter &interpreter,
1864 Stream &strm,
1865 const SymbolContext &sym_ctx,
1866 const char *name_cstr,
1867 bool name_is_regex)
1868{
1869 if (!sym_ctx.module_sp)
1870 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001871
Sean Callanand38b4a92012-06-06 20:49:55 +00001872 TypeList type_list;
1873 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001874 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001875 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001876
Sean Callanand38b4a92012-06-06 20:49:55 +00001877 ConstString name(name_cstr);
1878 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001879
Sean Callanand38b4a92012-06-06 20:49:55 +00001880 if (num_matches)
1881 {
1882 strm.Indent ();
1883 strm.PutCString("Best match found in ");
1884 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1885 strm.PutCString(":\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001886
Sean Callanand38b4a92012-06-06 20:49:55 +00001887 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1888 if (type_sp)
1889 {
1890 // Resolve the clang type so that any forward references
1891 // to types that haven't yet been parsed will get parsed.
1892 type_sp->GetClangFullType ();
1893 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1894 // Print all typedef chains
1895 TypeSP typedef_type_sp (type_sp);
1896 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1897 while (typedefed_type_sp)
1898 {
1899 strm.EOL();
1900 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1901 typedefed_type_sp->GetClangFullType ();
1902 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1903 typedef_type_sp = typedefed_type_sp;
1904 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1905 }
1906 }
1907 strm.EOL();
1908 }
1909 return num_matches;
1910}
1911
1912static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001913LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001914 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001915 Module *module,
1916 const FileSpec &file_spec,
1917 uint32_t line,
1918 bool check_inlines,
1919 bool verbose)
1920{
1921 if (module && file_spec)
1922 {
1923 SymbolContextList sc_list;
1924 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1925 eSymbolContextEverything, sc_list);
1926 if (num_matches > 0)
1927 {
1928 strm.Indent ();
1929 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1930 strm << file_spec;
1931 if (line > 0)
1932 strm.Printf (":%u", line);
1933 strm << " in ";
1934 DumpFullpath (strm, &module->GetFileSpec(), 0);
1935 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001936 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001937 return num_matches;
1938 }
1939 }
1940 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001941}
1942
Greg Clayton8ee64382011-11-10 01:18:58 +00001943
1944static size_t
1945FindModulesByName (Target *target,
1946 const char *module_name,
1947 ModuleList &module_list,
1948 bool check_global_list)
1949{
1950// Dump specified images (by basename or fullpath)
1951 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001952 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001953
Greg Clayton8ee64382011-11-10 01:18:58 +00001954 const size_t initial_size = module_list.GetSize ();
1955
Greg Claytonf3156262012-07-11 20:46:47 +00001956 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001957 {
1958 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001959 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001960 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001961 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001962 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001963 {
1964 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001965
Greg Clayton8ee64382011-11-10 01:18:58 +00001966 if (module)
1967 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001968 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001969 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001970 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001971 module_list.AppendIfNeeded(module_sp);
1972 }
1973 }
1974 }
1975 }
Greg Claytonf3156262012-07-11 20:46:47 +00001976 else
1977 {
1978 if (target)
1979 {
1980 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001981
Greg Claytonf3156262012-07-11 20:46:47 +00001982 // Not found in our module list for our target, check the main
1983 // shared module list in case it is a extra file used somewhere
1984 // else
1985 if (num_matches == 0)
1986 {
1987 module_spec.GetArchitecture() = target->GetArchitecture();
1988 ModuleList::FindSharedModules (module_spec, module_list);
1989 }
1990 }
1991 else
1992 {
1993 ModuleList::FindSharedModules (module_spec,module_list);
1994 }
1995 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001996
Greg Clayton8ee64382011-11-10 01:18:58 +00001997 return module_list.GetSize () - initial_size;
1998}
1999
Greg Claytoneffe5c92011-05-03 22:09:39 +00002000#pragma mark CommandObjectTargetModulesModuleAutoComplete
2001
2002//----------------------------------------------------------------------
2003// A base command object class that can auto complete with module file
2004// paths
2005//----------------------------------------------------------------------
2006
Jim Ingham5a988412012-06-08 21:56:10 +00002007class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002008{
2009public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002010 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2011 const char *name,
2012 const char *help,
2013 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002014 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002015 {
2016 CommandArgumentEntry arg;
2017 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002018
Greg Claytoneffe5c92011-05-03 22:09:39 +00002019 // Define the first (and only) variant of this arg.
2020 file_arg.arg_type = eArgTypeFilename;
2021 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002022
Greg Claytoneffe5c92011-05-03 22:09:39 +00002023 // There is only one variant this argument could be; put it into the argument entry.
2024 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002025
Greg Claytoneffe5c92011-05-03 22:09:39 +00002026 // Push the data for the first argument into the m_arguments vector.
2027 m_arguments.push_back (arg);
2028 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002029
Greg Claytoneffe5c92011-05-03 22:09:39 +00002030 virtual
2031 ~CommandObjectTargetModulesModuleAutoComplete ()
2032 {
2033 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002034
Greg Claytoneffe5c92011-05-03 22:09:39 +00002035 virtual int
2036 HandleArgumentCompletion (Args &input,
2037 int &cursor_index,
2038 int &cursor_char_position,
2039 OptionElementVector &opt_element_vector,
2040 int match_start_point,
2041 int max_return_elements,
2042 bool &word_complete,
2043 StringList &matches)
2044 {
2045 // Arguments are the standard module completer.
2046 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2047 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002048
Greg Claytoneffe5c92011-05-03 22:09:39 +00002049 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2050 CommandCompletions::eModuleCompletion,
2051 completion_str.c_str(),
2052 match_start_point,
2053 max_return_elements,
2054 NULL,
2055 word_complete,
2056 matches);
2057 return matches.GetSize();
2058 }
2059};
2060
2061#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2062
2063//----------------------------------------------------------------------
2064// A base command object class that can auto complete with module source
2065// file paths
2066//----------------------------------------------------------------------
2067
Jim Ingham5a988412012-06-08 21:56:10 +00002068class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002069{
2070public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002071 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002072 const char *name,
2073 const char *help,
2074 const char *syntax,
2075 uint32_t flags) :
2076 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002077 {
2078 CommandArgumentEntry arg;
2079 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002080
Greg Claytoneffe5c92011-05-03 22:09:39 +00002081 // Define the first (and only) variant of this arg.
2082 source_file_arg.arg_type = eArgTypeSourceFile;
2083 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002084
Greg Claytoneffe5c92011-05-03 22:09:39 +00002085 // There is only one variant this argument could be; put it into the argument entry.
2086 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002087
Greg Claytoneffe5c92011-05-03 22:09:39 +00002088 // Push the data for the first argument into the m_arguments vector.
2089 m_arguments.push_back (arg);
2090 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002091
Greg Claytoneffe5c92011-05-03 22:09:39 +00002092 virtual
2093 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2094 {
2095 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002096
Greg Claytoneffe5c92011-05-03 22:09:39 +00002097 virtual int
2098 HandleArgumentCompletion (Args &input,
2099 int &cursor_index,
2100 int &cursor_char_position,
2101 OptionElementVector &opt_element_vector,
2102 int match_start_point,
2103 int max_return_elements,
2104 bool &word_complete,
2105 StringList &matches)
2106 {
2107 // Arguments are the standard source file completer.
2108 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2109 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002110
Greg Claytoneffe5c92011-05-03 22:09:39 +00002111 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2112 CommandCompletions::eSourceFileCompletion,
2113 completion_str.c_str(),
2114 match_start_point,
2115 max_return_elements,
2116 NULL,
2117 word_complete,
2118 matches);
2119 return matches.GetSize();
2120 }
2121};
2122
2123
2124#pragma mark CommandObjectTargetModulesDumpSymtab
2125
2126
2127class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2128{
2129public:
2130 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2131 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2132 "target modules dump symtab",
2133 "Dump the symbol table from one or more target modules.",
2134 NULL),
2135 m_options (interpreter)
2136 {
2137 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002138
Greg Claytoneffe5c92011-05-03 22:09:39 +00002139 virtual
2140 ~CommandObjectTargetModulesDumpSymtab ()
2141 {
2142 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002143
Jim Ingham5a988412012-06-08 21:56:10 +00002144 virtual Options *
2145 GetOptions ()
2146 {
2147 return &m_options;
2148 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002149
Jim Ingham5a988412012-06-08 21:56:10 +00002150 class CommandOptions : public Options
2151 {
2152 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002153 CommandOptions (CommandInterpreter &interpreter) :
2154 Options(interpreter),
2155 m_sort_order (eSortOrderNone)
2156 {
2157 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002158
Jim Ingham5a988412012-06-08 21:56:10 +00002159 virtual
2160 ~CommandOptions ()
2161 {
2162 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002163
Jim Ingham5a988412012-06-08 21:56:10 +00002164 virtual Error
2165 SetOptionValue (uint32_t option_idx, const char *option_arg)
2166 {
2167 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002168 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002169
Jim Ingham5a988412012-06-08 21:56:10 +00002170 switch (short_option)
2171 {
2172 case 's':
2173 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2174 g_option_table[option_idx].enum_values,
2175 eSortOrderNone,
2176 error);
2177 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002178
Jim Ingham5a988412012-06-08 21:56:10 +00002179 default:
2180 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2181 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002182
Jim Ingham5a988412012-06-08 21:56:10 +00002183 }
2184 return error;
2185 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002186
Jim Ingham5a988412012-06-08 21:56:10 +00002187 void
2188 OptionParsingStarting ()
2189 {
2190 m_sort_order = eSortOrderNone;
2191 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002192
Jim Ingham5a988412012-06-08 21:56:10 +00002193 const OptionDefinition*
2194 GetDefinitions ()
2195 {
2196 return g_option_table;
2197 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002198
Jim Ingham5a988412012-06-08 21:56:10 +00002199 // Options table: Required for subclasses of Options.
2200 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002201
Jim Ingham5a988412012-06-08 21:56:10 +00002202 SortOrder m_sort_order;
2203 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002204
Jim Ingham5a988412012-06-08 21:56:10 +00002205protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002206 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002207 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002208 CommandReturnObject &result)
2209 {
2210 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2211 if (target == NULL)
2212 {
2213 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2214 result.SetStatus (eReturnStatusFailed);
2215 return false;
2216 }
2217 else
2218 {
2219 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002220
Greg Claytoneffe5c92011-05-03 22:09:39 +00002221 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2222 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2223 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002224
Greg Claytoneffe5c92011-05-03 22:09:39 +00002225 if (command.GetArgumentCount() == 0)
2226 {
2227 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002228 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002229 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002230 if (num_modules > 0)
2231 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002232 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002233 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002234 {
2235 if (num_dumped > 0)
2236 {
2237 result.GetOutputStream().EOL();
2238 result.GetOutputStream().EOL();
2239 }
2240 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002241 DumpModuleSymtab (m_interpreter,
2242 result.GetOutputStream(),
2243 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2244 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002245 }
2246 }
2247 else
2248 {
2249 result.AppendError ("the target has no associated executable images");
2250 result.SetStatus (eReturnStatusFailed);
2251 return false;
2252 }
2253 }
2254 else
2255 {
2256 // Dump specified images (by basename or fullpath)
2257 const char *arg_cstr;
2258 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2259 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002260 ModuleList module_list;
2261 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2262 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002263 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002264 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002265 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002266 Module *module = module_list.GetModulePointerAtIndex(i);
2267 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002268 {
2269 if (num_dumped > 0)
2270 {
2271 result.GetOutputStream().EOL();
2272 result.GetOutputStream().EOL();
2273 }
2274 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002275 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002276 }
2277 }
2278 }
2279 else
2280 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2281 }
2282 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002283
Greg Claytoneffe5c92011-05-03 22:09:39 +00002284 if (num_dumped > 0)
2285 result.SetStatus (eReturnStatusSuccessFinishResult);
2286 else
2287 {
2288 result.AppendError ("no matching executable images found");
2289 result.SetStatus (eReturnStatusFailed);
2290 }
2291 }
2292 return result.Succeeded();
2293 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002294
Greg Claytoneffe5c92011-05-03 22:09:39 +00002295 CommandOptions m_options;
2296};
2297
2298static OptionEnumValueElement
2299g_sort_option_enumeration[4] =
2300{
2301 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2302 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2303 { eSortOrderByName, "name", "Sort output by symbol name."},
2304 { 0, NULL, NULL }
2305};
2306
2307
2308OptionDefinition
2309CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2310{
Zachary Turnerd37221d2014-07-09 16:31:49 +00002311 { LLDB_OPT_SET_1, false, "sort", 's', OptionParser::eRequiredArgument, NULL, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2312 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002313};
2314
2315#pragma mark CommandObjectTargetModulesDumpSections
2316
2317//----------------------------------------------------------------------
2318// Image section dumping command
2319//----------------------------------------------------------------------
2320
2321class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2322{
2323public:
2324 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2325 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2326 "target modules dump sections",
2327 "Dump the sections from one or more target modules.",
2328 //"target modules dump sections [<file1> ...]")
2329 NULL)
2330 {
2331 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002332
Greg Claytoneffe5c92011-05-03 22:09:39 +00002333 virtual
2334 ~CommandObjectTargetModulesDumpSections ()
2335 {
2336 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002337
Jim Ingham5a988412012-06-08 21:56:10 +00002338protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002339 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002340 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002341 CommandReturnObject &result)
2342 {
2343 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2344 if (target == NULL)
2345 {
2346 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2347 result.SetStatus (eReturnStatusFailed);
2348 return false;
2349 }
2350 else
2351 {
2352 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002353
Greg Claytoneffe5c92011-05-03 22:09:39 +00002354 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2355 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2356 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002357
Greg Claytoneffe5c92011-05-03 22:09:39 +00002358 if (command.GetArgumentCount() == 0)
2359 {
2360 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002361 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002362 if (num_modules > 0)
2363 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002364 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002365 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002366 {
2367 num_dumped++;
2368 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2369 }
2370 }
2371 else
2372 {
2373 result.AppendError ("the target has no associated executable images");
2374 result.SetStatus (eReturnStatusFailed);
2375 return false;
2376 }
2377 }
2378 else
2379 {
2380 // Dump specified images (by basename or fullpath)
2381 const char *arg_cstr;
2382 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2383 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002384 ModuleList module_list;
2385 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2386 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002387 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002388 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002389 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002390 Module *module = module_list.GetModulePointerAtIndex(i);
2391 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002392 {
2393 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002394 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002395 }
2396 }
2397 }
2398 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002399 {
2400 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002401 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002402
Greg Claytoneffe5c92011-05-03 22:09:39 +00002403 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002404 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002405 }
2406 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002407
Greg Claytoneffe5c92011-05-03 22:09:39 +00002408 if (num_dumped > 0)
2409 result.SetStatus (eReturnStatusSuccessFinishResult);
2410 else
2411 {
2412 result.AppendError ("no matching executable images found");
2413 result.SetStatus (eReturnStatusFailed);
2414 }
2415 }
2416 return result.Succeeded();
2417 }
2418};
2419
2420
2421#pragma mark CommandObjectTargetModulesDumpSymfile
2422
2423//----------------------------------------------------------------------
2424// Image debug symbol dumping command
2425//----------------------------------------------------------------------
2426
2427class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2428{
2429public:
2430 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2431 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2432 "target modules dump symfile",
2433 "Dump the debug symbol file for one or more target modules.",
2434 //"target modules dump symfile [<file1> ...]")
2435 NULL)
2436 {
2437 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002438
Greg Claytoneffe5c92011-05-03 22:09:39 +00002439 virtual
2440 ~CommandObjectTargetModulesDumpSymfile ()
2441 {
2442 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002443
Jim Ingham5a988412012-06-08 21:56:10 +00002444protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002445 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002446 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002447 CommandReturnObject &result)
2448 {
2449 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2450 if (target == NULL)
2451 {
2452 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2453 result.SetStatus (eReturnStatusFailed);
2454 return false;
2455 }
2456 else
2457 {
2458 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002459
Greg Claytoneffe5c92011-05-03 22:09:39 +00002460 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2461 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2462 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002463
Greg Claytoneffe5c92011-05-03 22:09:39 +00002464 if (command.GetArgumentCount() == 0)
2465 {
2466 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002467 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002468 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002469 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002470 if (num_modules > 0)
2471 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002472 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002473 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2474 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002475 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002476 num_dumped++;
2477 }
2478 }
2479 else
2480 {
2481 result.AppendError ("the target has no associated executable images");
2482 result.SetStatus (eReturnStatusFailed);
2483 return false;
2484 }
2485 }
2486 else
2487 {
2488 // Dump specified images (by basename or fullpath)
2489 const char *arg_cstr;
2490 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2491 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002492 ModuleList module_list;
2493 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2494 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002495 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002496 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002497 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002498 Module *module = module_list.GetModulePointerAtIndex(i);
2499 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002500 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002501 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002502 num_dumped++;
2503 }
2504 }
2505 }
2506 else
2507 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2508 }
2509 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002510
Greg Claytoneffe5c92011-05-03 22:09:39 +00002511 if (num_dumped > 0)
2512 result.SetStatus (eReturnStatusSuccessFinishResult);
2513 else
2514 {
2515 result.AppendError ("no matching executable images found");
2516 result.SetStatus (eReturnStatusFailed);
2517 }
2518 }
2519 return result.Succeeded();
2520 }
2521};
2522
2523
2524#pragma mark CommandObjectTargetModulesDumpLineTable
2525
2526//----------------------------------------------------------------------
2527// Image debug line table dumping command
2528//----------------------------------------------------------------------
2529
2530class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2531{
2532public:
2533 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2534 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002535 "target modules dump line-table",
Jim Inghamcc0273d2013-06-18 20:27:11 +00002536 "Dump the line table for one or more compilation units.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00002537 NULL,
2538 eFlagRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002539 {
2540 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002541
Greg Claytoneffe5c92011-05-03 22:09:39 +00002542 virtual
2543 ~CommandObjectTargetModulesDumpLineTable ()
2544 {
2545 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002546
Jim Ingham5a988412012-06-08 21:56:10 +00002547protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002548 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002549 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002550 CommandReturnObject &result)
2551 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002552 Target *target = m_exe_ctx.GetTargetPtr();
2553 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002554
Greg Claytonf9fc6092013-01-09 19:44:40 +00002555 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2556 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2557 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002558
Greg Claytonf9fc6092013-01-09 19:44:40 +00002559 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002560 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002561 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00002562 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002563 }
2564 else
2565 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002566 // Dump specified images (by basename or fullpath)
2567 const char *arg_cstr;
2568 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002569 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002570 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002571
Greg Claytonf9fc6092013-01-09 19:44:40 +00002572 const ModuleList &target_modules = target->GetImages();
2573 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002574 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002575 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002576 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002577 uint32_t num_dumped = 0;
2578 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002579 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002580 if (DumpCompileUnitLineTable (m_interpreter,
2581 result.GetOutputStream(),
2582 target_modules.GetModulePointerAtIndexUnlocked(i),
2583 file_spec,
2584 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2585 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002586 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002587 if (num_dumped == 0)
2588 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2589 else
2590 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002591 }
2592 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002593 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002594
Greg Claytonf9fc6092013-01-09 19:44:40 +00002595 if (total_num_dumped > 0)
2596 result.SetStatus (eReturnStatusSuccessFinishResult);
2597 else
2598 {
2599 result.AppendError ("no source filenames matched any command arguments");
2600 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002601 }
2602 return result.Succeeded();
2603 }
2604};
2605
2606
2607#pragma mark CommandObjectTargetModulesDump
2608
2609//----------------------------------------------------------------------
2610// Dump multi-word command for target modules
2611//----------------------------------------------------------------------
2612
2613class CommandObjectTargetModulesDump : public CommandObjectMultiword
2614{
2615public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002616 //------------------------------------------------------------------
2617 // Constructors and Destructors
2618 //------------------------------------------------------------------
2619 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2620 CommandObjectMultiword (interpreter,
2621 "target modules dump",
2622 "A set of commands for dumping information about one or more target modules.",
2623 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2624 {
2625 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2626 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2627 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2628 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2629 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002630
Greg Claytoneffe5c92011-05-03 22:09:39 +00002631 virtual
2632 ~CommandObjectTargetModulesDump()
2633 {
2634 }
2635};
2636
Jim Ingham5a988412012-06-08 21:56:10 +00002637class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002638{
2639public:
2640 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002641 CommandObjectParsed (interpreter,
2642 "target modules add",
2643 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002644 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002645 m_option_group (interpreter),
2646 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 Claytoneffe5c92011-05-03 22:09:39 +00002647 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002648 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002649 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002650 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002651 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002652
Greg Claytoneffe5c92011-05-03 22:09:39 +00002653 virtual
2654 ~CommandObjectTargetModulesAdd ()
2655 {
2656 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002657
Greg Clayton50a24bd2012-11-29 22:16:27 +00002658 virtual Options *
2659 GetOptions ()
2660 {
2661 return &m_option_group;
2662 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002663
Greg Claytonc7bece562013-01-25 18:06:21 +00002664 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00002665 HandleArgumentCompletion (Args &input,
2666 int &cursor_index,
2667 int &cursor_char_position,
2668 OptionElementVector &opt_element_vector,
2669 int match_start_point,
2670 int max_return_elements,
2671 bool &word_complete,
2672 StringList &matches)
2673 {
2674 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2675 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002676
Jim Ingham5a988412012-06-08 21:56:10 +00002677 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2678 CommandCompletions::eDiskFileCompletion,
2679 completion_str.c_str(),
2680 match_start_point,
2681 max_return_elements,
2682 NULL,
2683 word_complete,
2684 matches);
2685 return matches.GetSize();
2686 }
2687
2688protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002689 OptionGroupOptions m_option_group;
2690 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002691 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002692
Greg Claytoneffe5c92011-05-03 22:09:39 +00002693 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002694 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002695 CommandReturnObject &result)
2696 {
2697 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2698 if (target == NULL)
2699 {
2700 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2701 result.SetStatus (eReturnStatusFailed);
2702 return false;
2703 }
2704 else
2705 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002706 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002707
Greg Claytoneffe5c92011-05-03 22:09:39 +00002708 const size_t argc = args.GetArgumentCount();
2709 if (argc == 0)
2710 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002711 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2712 {
2713 // We are given a UUID only, go locate the file
2714 ModuleSpec module_spec;
2715 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002716 if (m_symbol_file.GetOptionValue().OptionWasSet())
2717 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002718 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2719 {
2720 ModuleSP module_sp (target->GetSharedModule (module_spec));
2721 if (module_sp)
2722 {
2723 result.SetStatus (eReturnStatusSuccessFinishResult);
2724 return true;
2725 }
2726 else
2727 {
2728 StreamString strm;
2729 module_spec.GetUUID().Dump (&strm);
2730 if (module_spec.GetFileSpec())
2731 {
2732 if (module_spec.GetSymbolFileSpec())
2733 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002734 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s and symbol file %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002735 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002736 module_spec.GetFileSpec().GetPath().c_str(),
2737 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002738 }
2739 else
2740 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002741 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002742 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002743 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002744 }
2745 }
2746 else
2747 {
2748 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2749 strm.GetString().c_str());
2750 }
2751 result.SetStatus (eReturnStatusFailed);
2752 return false;
2753 }
2754 }
2755 else
2756 {
2757 StreamString strm;
2758 module_spec.GetUUID().Dump (&strm);
2759 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2760 result.SetStatus (eReturnStatusFailed);
2761 return false;
2762 }
2763 }
2764 else
2765 {
2766 result.AppendError ("one or more executable image paths must be specified");
2767 result.SetStatus (eReturnStatusFailed);
2768 return false;
2769 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002770 }
2771 else
2772 {
2773 for (size_t i=0; i<argc; ++i)
2774 {
2775 const char *path = args.GetArgumentAtIndex(i);
2776 if (path)
2777 {
2778 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002779 if (file_spec.Exists())
2780 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002781 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002782 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2783 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002784 if (m_symbol_file.GetOptionValue().OptionWasSet())
2785 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002786 if (!module_spec.GetArchitecture().IsValid())
2787 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002788 Error error;
2789 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002790 if (!module_sp)
2791 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002792 const char *error_cstr = error.AsCString();
2793 if (error_cstr)
2794 result.AppendError (error_cstr);
2795 else
2796 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002797 result.SetStatus (eReturnStatusFailed);
2798 return false;
2799 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002800 else
2801 {
2802 flush = true;
2803 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002804 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002805 }
2806 else
2807 {
2808 char resolved_path[PATH_MAX];
2809 result.SetStatus (eReturnStatusFailed);
2810 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2811 {
2812 if (strcmp (resolved_path, path) != 0)
2813 {
2814 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2815 break;
2816 }
2817 }
2818 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2819 break;
2820 }
2821 }
2822 }
2823 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002824
Sean Callananb36c6c02012-12-13 01:39:39 +00002825 if (flush)
2826 {
2827 ProcessSP process = target->GetProcessSP();
2828 if (process)
2829 process->Flush();
2830 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002831 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002832
Greg Claytoneffe5c92011-05-03 22:09:39 +00002833 return result.Succeeded();
2834 }
2835
Greg Claytoneffe5c92011-05-03 22:09:39 +00002836};
2837
2838class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2839{
2840public:
2841 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2842 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2843 "target modules load",
2844 "Set the load addresses for one or more sections in a target module.",
2845 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2846 m_option_group (interpreter),
Sean Callanan31542552012-10-24 01:12:14 +00002847 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeFilename, "Fullpath or basename for module to load."),
Greg Claytoneffe5c92011-05-03 22:09:39 +00002848 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)
2849 {
2850 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2851 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2852 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2853 m_option_group.Finalize();
2854 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002855
Greg Claytoneffe5c92011-05-03 22:09:39 +00002856 virtual
2857 ~CommandObjectTargetModulesLoad ()
2858 {
2859 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002860
Jim Ingham5a988412012-06-08 21:56:10 +00002861 virtual Options *
2862 GetOptions ()
2863 {
2864 return &m_option_group;
2865 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002866
Jim Ingham5a988412012-06-08 21:56:10 +00002867protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002868 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002869 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002870 CommandReturnObject &result)
2871 {
2872 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2873 if (target == NULL)
2874 {
2875 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2876 result.SetStatus (eReturnStatusFailed);
2877 return false;
2878 }
2879 else
2880 {
2881 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002882 ModuleSpec module_spec;
2883 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002884 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002885 {
2886 search_using_module_spec = true;
2887 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
2888 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002889
Greg Claytoneffe5c92011-05-03 22:09:39 +00002890 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002891 {
2892 search_using_module_spec = true;
2893 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2894 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002895
Greg Claytonb9a01b32012-02-26 05:51:37 +00002896 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002897 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002898 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002899 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002900
2901 char path[PATH_MAX];
2902 if (num_matches == 1)
2903 {
2904 Module *module = matching_modules.GetModulePointerAtIndex(0);
2905 if (module)
2906 {
2907 ObjectFile *objfile = module->GetObjectFile();
2908 if (objfile)
2909 {
Greg Clayton3046e662013-07-10 01:23:25 +00002910 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002911 if (section_list)
2912 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002913 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002914 if (argc == 0)
2915 {
2916 if (m_slide_option.GetOptionValue().OptionWasSet())
2917 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002918 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002919 const bool slide_is_offset = true;
2920 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002921 }
2922 else
2923 {
2924 result.AppendError ("one or more section name + load address pair must be specified");
2925 result.SetStatus (eReturnStatusFailed);
2926 return false;
2927 }
2928 }
2929 else
2930 {
2931 if (m_slide_option.GetOptionValue().OptionWasSet())
2932 {
2933 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2934 result.SetStatus (eReturnStatusFailed);
2935 return false;
2936 }
2937
2938 for (size_t i=0; i<argc; i += 2)
2939 {
2940 const char *sect_name = args.GetArgumentAtIndex(i);
2941 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2942 if (sect_name && load_addr_cstr)
2943 {
2944 ConstString const_sect_name(sect_name);
2945 bool success = false;
2946 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2947 if (success)
2948 {
2949 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2950 if (section_sp)
2951 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002952 if (section_sp->IsThreadSpecific())
2953 {
2954 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2955 result.SetStatus (eReturnStatusFailed);
2956 break;
2957 }
2958 else
2959 {
Greg Clayton7820bd12012-07-07 01:24:12 +00002960 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00002961 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00002962 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00002963 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002964 }
2965 else
2966 {
2967 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2968 result.SetStatus (eReturnStatusFailed);
2969 break;
2970 }
2971 }
2972 else
2973 {
2974 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2975 result.SetStatus (eReturnStatusFailed);
2976 break;
2977 }
2978 }
2979 else
2980 {
2981 if (sect_name)
2982 result.AppendError ("section names must be followed by a load address.\n");
2983 else
2984 result.AppendError ("one or more section name + load address pair must be specified.\n");
2985 result.SetStatus (eReturnStatusFailed);
2986 break;
2987 }
2988 }
2989 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002990
Greg Clayton741f3f92012-03-27 21:10:07 +00002991 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00002992 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002993 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00002994 Process *process = m_exe_ctx.GetProcessPtr();
2995 if (process)
2996 process->Flush();
2997 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002998 }
2999 else
3000 {
3001 module->GetFileSpec().GetPath (path, sizeof(path));
3002 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3003 result.SetStatus (eReturnStatusFailed);
3004 }
3005 }
3006 else
3007 {
3008 module->GetFileSpec().GetPath (path, sizeof(path));
3009 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3010 result.SetStatus (eReturnStatusFailed);
3011 }
3012 }
3013 else
3014 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003015 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3016 if (module_spec_file)
3017 {
3018 module_spec_file->GetPath (path, sizeof(path));
3019 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3020 }
3021 else
3022 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003023 result.SetStatus (eReturnStatusFailed);
3024 }
3025 }
3026 else
3027 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003028 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003029
Greg Claytonb9a01b32012-02-26 05:51:37 +00003030 if (module_spec.GetFileSpec())
3031 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003032 else
3033 path[0] = '\0';
3034
Greg Claytonb9a01b32012-02-26 05:51:37 +00003035 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003036 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003037 if (num_matches > 1)
3038 {
3039 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3040 path[0] ? " file=" : "",
3041 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003042 !uuid_str.empty() ? " uuid=" : "",
3043 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003044 for (size_t i=0; i<num_matches; ++i)
3045 {
3046 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3047 result.AppendMessageWithFormat("%s\n", path);
3048 }
3049 }
3050 else
3051 {
3052 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3053 path[0] ? " file=" : "",
3054 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003055 !uuid_str.empty() ? " uuid=" : "",
3056 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003057 }
3058 result.SetStatus (eReturnStatusFailed);
3059 }
3060 }
3061 else
3062 {
3063 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3064 result.SetStatus (eReturnStatusFailed);
3065 return false;
3066 }
3067 }
3068 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003069 }
3070
Greg Claytoneffe5c92011-05-03 22:09:39 +00003071 OptionGroupOptions m_option_group;
3072 OptionGroupUUID m_uuid_option_group;
3073 OptionGroupFile m_file_option;
3074 OptionGroupUInt64 m_slide_option;
3075};
3076
3077//----------------------------------------------------------------------
3078// List images with associated information
3079//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003080class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003081{
3082public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003083 class CommandOptions : public Options
3084 {
3085 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003086 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003087 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003088 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003089 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003090 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003091 {
3092 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003093
Greg Claytoneffe5c92011-05-03 22:09:39 +00003094 virtual
3095 ~CommandOptions ()
3096 {
3097 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003098
Greg Claytoneffe5c92011-05-03 22:09:39 +00003099 virtual Error
3100 SetOptionValue (uint32_t option_idx, const char *option_arg)
3101 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003102 Error error;
3103
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003104 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003105 if (short_option == 'g')
3106 {
3107 m_use_global_module_list = true;
3108 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003109 else if (short_option == 'a')
3110 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003111 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3112 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003113 }
Greg Clayton65a03992011-08-09 00:01:09 +00003114 else
3115 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003116 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003117 if (option_arg)
3118 width = strtoul (option_arg, NULL, 0);
3119 m_format_array.push_back(std::make_pair(short_option, width));
3120 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003121 return error;
3122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003123
Greg Claytoneffe5c92011-05-03 22:09:39 +00003124 void
3125 OptionParsingStarting ()
3126 {
3127 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003128 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003129 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003130 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003131
Greg Claytoneffe5c92011-05-03 22:09:39 +00003132 const OptionDefinition*
3133 GetDefinitions ()
3134 {
3135 return g_option_table;
3136 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003137
Greg Claytoneffe5c92011-05-03 22:09:39 +00003138 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003139
Greg Claytoneffe5c92011-05-03 22:09:39 +00003140 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003141
Greg Claytoneffe5c92011-05-03 22:09:39 +00003142 // Instance variables to hold the values for command options.
3143 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3144 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003145 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003146 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003147 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003148
Greg Claytoneffe5c92011-05-03 22:09:39 +00003149 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003150 CommandObjectParsed (interpreter,
3151 "target modules list",
3152 "List current executable and dependent shared library images.",
3153 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003154 m_options (interpreter)
3155 {
3156 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003157
Greg Claytoneffe5c92011-05-03 22:09:39 +00003158 virtual
3159 ~CommandObjectTargetModulesList ()
3160 {
3161 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003162
Greg Claytoneffe5c92011-05-03 22:09:39 +00003163 virtual
3164 Options *
3165 GetOptions ()
3166 {
3167 return &m_options;
3168 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003169
Jim Ingham5a988412012-06-08 21:56:10 +00003170protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003171 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00003172 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003173 CommandReturnObject &result)
3174 {
3175 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003176 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003177 // Define a local module list here to ensure it lives longer than any "locker"
3178 // object which might lock its contents below (through the "module_list_ptr"
3179 // variable).
3180 ModuleList module_list;
Greg Clayton3418c852011-08-10 02:10:13 +00003181 if (target == NULL && use_global_module_list == false)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003182 {
3183 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3184 result.SetStatus (eReturnStatusFailed);
3185 return false;
3186 }
3187 else
3188 {
Greg Clayton3418c852011-08-10 02:10:13 +00003189 if (target)
3190 {
3191 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3192 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3193 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3194 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003195 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003196 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003197
Jim Inghamc10312c2011-10-24 18:36:33 +00003198 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3199 {
3200 if (target)
3201 {
3202 Address module_address;
3203 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3204 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003205 ModuleSP module_sp (module_address.GetModule());
3206 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003207 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003208 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003209 result.SetStatus (eReturnStatusSuccessFinishResult);
3210 }
3211 else
3212 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003213 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003214 result.SetStatus (eReturnStatusFailed);
3215 }
3216 }
3217 else
3218 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003219 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003220 result.SetStatus (eReturnStatusFailed);
3221 }
3222 }
3223 else
3224 {
3225 result.AppendError ("Can only look up modules by address with a valid target.");
3226 result.SetStatus (eReturnStatusFailed);
3227 }
3228 return result.Succeeded();
3229 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003230
Greg Claytonc7bece562013-01-25 18:06:21 +00003231 size_t num_modules = 0;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003232 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3233 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3234 // the global module list directly.
Enrico Granata17598482012-11-08 02:22:02 +00003235 const ModuleList *module_list_ptr = NULL;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003236 const size_t argc = command.GetArgumentCount();
3237 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003238 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003239 if (use_global_module_list)
3240 {
3241 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3242 num_modules = Module::GetNumberAllocatedModules();
3243 }
3244 else
3245 {
3246 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003247 }
Greg Clayton65a03992011-08-09 00:01:09 +00003248 }
3249 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003250 {
3251 for (size_t i=0; i<argc; ++i)
3252 {
3253 // Dump specified images (by basename or fullpath)
3254 const char *arg_cstr = command.GetArgumentAtIndex(i);
3255 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3256 if (num_matches == 0)
3257 {
3258 if (argc == 1)
3259 {
3260 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3261 result.SetStatus (eReturnStatusFailed);
3262 return false;
3263 }
3264 }
3265 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003266
Greg Claytonc4a8a762012-05-15 18:43:44 +00003267 module_list_ptr = &module_list;
3268 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003269
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003270 if (module_list_ptr != NULL)
3271 {
3272 locker.Lock(module_list_ptr->GetMutex());
3273 num_modules = module_list_ptr->GetSize();
3274 }
Greg Clayton65a03992011-08-09 00:01:09 +00003275
Greg Claytoneffe5c92011-05-03 22:09:39 +00003276 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003277 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003278 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3279 {
Greg Clayton3418c852011-08-10 02:10:13 +00003280 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003281 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003282 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003283 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003284 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003285 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003286 }
3287 else
3288 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003289 module = Module::GetAllocatedModuleAtIndex(image_idx);
3290 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003291 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003292
Greg Claytonc7bece562013-01-25 18:06:21 +00003293 const size_t indent = strm.Printf("[%3u] ", image_idx);
3294 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003295
Greg Claytoneffe5c92011-05-03 22:09:39 +00003296 }
3297 result.SetStatus (eReturnStatusSuccessFinishResult);
3298 }
3299 else
3300 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003301 if (argc)
3302 {
3303 if (use_global_module_list)
3304 result.AppendError ("the global module list has no matching modules");
3305 else
3306 result.AppendError ("the target has no matching modules");
3307 }
Greg Clayton3418c852011-08-10 02:10:13 +00003308 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003309 {
3310 if (use_global_module_list)
3311 result.AppendError ("the global module list is empty");
3312 else
3313 result.AppendError ("the target has no associated executable images");
3314 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003315 result.SetStatus (eReturnStatusFailed);
3316 return false;
3317 }
3318 }
3319 return result.Succeeded();
3320 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003321
3322 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003323 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003324 {
3325
Jim Ingham28eb5712012-10-12 17:34:26 +00003326 if (module == NULL)
3327 {
3328 strm.PutCString("Null module");
3329 return;
3330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003331
Jim Inghamc10312c2011-10-24 18:36:33 +00003332 bool dump_object_name = false;
3333 if (m_options.m_format_array.empty())
3334 {
Greg Claytonc9660542012-02-05 02:38:54 +00003335 m_options.m_format_array.push_back(std::make_pair('u', 0));
3336 m_options.m_format_array.push_back(std::make_pair('h', 0));
3337 m_options.m_format_array.push_back(std::make_pair('f', 0));
3338 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003339 }
Greg Claytonc9660542012-02-05 02:38:54 +00003340 const size_t num_entries = m_options.m_format_array.size();
3341 bool print_space = false;
3342 for (size_t i=0; i<num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003343 {
Greg Claytonc9660542012-02-05 02:38:54 +00003344 if (print_space)
3345 strm.PutChar(' ');
3346 print_space = true;
3347 const char format_char = m_options.m_format_array[i].first;
3348 uint32_t width = m_options.m_format_array[i].second;
3349 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003350 {
Greg Claytonc9660542012-02-05 02:38:54 +00003351 case 'A':
3352 DumpModuleArchitecture (strm, module, false, width);
3353 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003354
Greg Claytonc9660542012-02-05 02:38:54 +00003355 case 't':
3356 DumpModuleArchitecture (strm, module, true, width);
3357 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003358
Greg Claytonc9660542012-02-05 02:38:54 +00003359 case 'f':
3360 DumpFullpath (strm, &module->GetFileSpec(), width);
3361 dump_object_name = true;
3362 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003363
Greg Claytonc9660542012-02-05 02:38:54 +00003364 case 'd':
3365 DumpDirectory (strm, &module->GetFileSpec(), width);
3366 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003367
Greg Claytonc9660542012-02-05 02:38:54 +00003368 case 'b':
3369 DumpBasename (strm, &module->GetFileSpec(), width);
3370 dump_object_name = true;
3371 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003372
Greg Claytonc9660542012-02-05 02:38:54 +00003373 case 'h':
3374 case 'o':
3375 // Image header address
3376 {
3377 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003378
Greg Claytonc9660542012-02-05 02:38:54 +00003379 ObjectFile *objfile = module->GetObjectFile ();
3380 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003381 {
Greg Claytonc9660542012-02-05 02:38:54 +00003382 Address header_addr(objfile->GetHeaderAddress());
3383 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003384 {
Greg Claytonc9660542012-02-05 02:38:54 +00003385 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003386 {
Greg Claytonc9660542012-02-05 02:38:54 +00003387 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3388 if (header_load_addr == LLDB_INVALID_ADDRESS)
3389 {
3390 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3391 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003392 else
Greg Claytonc9660542012-02-05 02:38:54 +00003393 {
3394 if (format_char == 'o')
3395 {
3396 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003397 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003398 }
3399 else
3400 {
3401 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003402 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003403 }
3404 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003405 break;
3406 }
Greg Claytonc9660542012-02-05 02:38:54 +00003407 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3408 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3409 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003410 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003411 }
Greg Claytonc9660542012-02-05 02:38:54 +00003412 strm.Printf ("%*s", addr_nibble_width + 2, "");
3413 }
3414 break;
3415 case 'r':
3416 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003417 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003418 ModuleSP module_sp (module->shared_from_this());
3419 if (module_sp)
3420 {
3421 // Take one away to make sure we don't count our local "module_sp"
3422 ref_count = module_sp.use_count() - 1;
3423 }
3424 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003425 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003426 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003427 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003428 }
3429 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003430
Greg Claytonc9660542012-02-05 02:38:54 +00003431 case 's':
3432 case 'S':
3433 {
3434 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3435 if (symbol_vendor)
3436 {
3437 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3438 if (symbol_file)
3439 {
3440 if (format_char == 'S')
3441 {
3442 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3443 // Dump symbol file only if different from module file
3444 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3445 {
3446 print_space = false;
3447 break;
3448 }
3449 // Add a newline and indent past the index
3450 strm.Printf ("\n%*s", indent, "");
3451 }
3452 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3453 dump_object_name = true;
3454 break;
3455 }
3456 }
3457 strm.Printf("%.*s", width, "<NONE>");
3458 }
3459 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003460
Greg Claytonc9660542012-02-05 02:38:54 +00003461 case 'm':
3462 module->GetModificationTime().Dump(&strm, width);
3463 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003464
Greg Claytonc9660542012-02-05 02:38:54 +00003465 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003466 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003467 break;
3468
3469 case 'u':
3470 DumpModuleUUID(strm, module);
3471 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003472
Greg Claytonc9660542012-02-05 02:38:54 +00003473 default:
3474 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003475 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003476
Greg Claytonc9660542012-02-05 02:38:54 +00003477 }
3478 if (dump_object_name)
3479 {
3480 const char *object_name = module->GetObjectName().GetCString();
3481 if (object_name)
3482 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003483 }
3484 strm.EOL();
3485 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003486
Greg Claytoneffe5c92011-05-03 22:09:39 +00003487 CommandOptions m_options;
3488};
3489
3490OptionDefinition
3491CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3492{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003493 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3494 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
3495 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
3496 { LLDB_OPT_SET_1, false, "header", 'h', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the image header address as a load address if debugging, a file address otherwise."},
3497 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the image header address offset from the header file address (the slide amount)."},
3498 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3499 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3500 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3501 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3502 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3503 { LLDB_OPT_SET_1, false, "symfile-unique", 'S', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the symbol file with optional width only if it is different from the executable object file."},
3504 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3505 { LLDB_OPT_SET_1, false, "ref-count", 'r', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the reference count if the module is still in the shared module cache."},
3506 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeNone, "Display the module pointer."},
3507 { LLDB_OPT_SET_1, false, "global", 'g', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the modules from the global module list, not just the current target."},
3508 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003509};
3510
Jason Molenda380241a2012-07-12 00:20:07 +00003511#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003512
Jason Molenda380241a2012-07-12 00:20:07 +00003513//----------------------------------------------------------------------
3514// Lookup unwind information in images
3515//----------------------------------------------------------------------
3516
3517class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3518{
3519public:
3520
3521 enum
3522 {
3523 eLookupTypeInvalid = -1,
3524 eLookupTypeAddress = 0,
3525 eLookupTypeSymbol,
3526 eLookupTypeFunction,
3527 eLookupTypeFunctionOrSymbol,
3528 kNumLookupTypes
3529 };
3530
3531 class CommandOptions : public Options
3532 {
3533 public:
3534
3535 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003536 Options(interpreter),
3537 m_type(eLookupTypeInvalid),
3538 m_str(),
3539 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003540 {
3541 }
3542
3543 virtual
3544 ~CommandOptions ()
3545 {
3546 }
3547
3548 virtual Error
3549 SetOptionValue (uint32_t option_idx, const char *option_arg)
3550 {
3551 Error error;
3552
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003553 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003554
3555 switch (short_option)
3556 {
3557 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003558 {
3559 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003560 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003561 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003562 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003563 if (m_addr == LLDB_INVALID_ADDRESS)
3564 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3565 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003566 }
Jason Molenda380241a2012-07-12 00:20:07 +00003567
3568 case 'n':
Jason Molenda535ab862013-04-23 04:30:57 +00003569 {
Jason Molenda380241a2012-07-12 00:20:07 +00003570 m_str = option_arg;
3571 m_type = eLookupTypeFunctionOrSymbol;
3572 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003573 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003574
3575 default:
3576 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3577 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003578 }
3579
3580 return error;
3581 }
3582
3583 void
3584 OptionParsingStarting ()
3585 {
3586 m_type = eLookupTypeInvalid;
3587 m_str.clear();
3588 m_addr = LLDB_INVALID_ADDRESS;
3589 }
3590
3591 const OptionDefinition*
3592 GetDefinitions ()
3593 {
3594 return g_option_table;
3595 }
3596
3597 // Options table: Required for subclasses of Options.
3598
3599 static OptionDefinition g_option_table[];
3600
3601 // Instance variables to hold the values for command options.
3602
3603 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3604 std::string m_str; // Holds name lookup
3605 lldb::addr_t m_addr; // Holds the address to lookup
3606 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003607
Jason Molenda380241a2012-07-12 00:20:07 +00003608 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3609 CommandObjectParsed (interpreter,
3610 "target modules show-unwind",
3611 "Show synthesized unwind instructions for a function.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003612 NULL,
3613 eFlagRequiresTarget |
3614 eFlagRequiresProcess |
3615 eFlagProcessMustBeLaunched |
3616 eFlagProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003617 m_options (interpreter)
3618 {
3619 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003620
Jason Molenda380241a2012-07-12 00:20:07 +00003621 virtual
3622 ~CommandObjectTargetModulesShowUnwind ()
3623 {
3624 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003625
Jason Molenda380241a2012-07-12 00:20:07 +00003626 virtual
3627 Options *
3628 GetOptions ()
3629 {
3630 return &m_options;
3631 }
3632
3633protected:
3634 bool
3635 DoExecute (Args& command,
3636 CommandReturnObject &result)
3637 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003638 Target *target = m_exe_ctx.GetTargetPtr();
3639 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda380241a2012-07-12 00:20:07 +00003640 ABI *abi = NULL;
3641 if (process)
3642 abi = process->GetABI().get();
3643
3644 if (process == NULL)
3645 {
3646 result.AppendError ("You must have a process running to use this command.");
3647 result.SetStatus (eReturnStatusFailed);
3648 return false;
3649 }
3650
3651 ThreadList threads(process->GetThreadList());
3652 if (threads.GetSize() == 0)
3653 {
3654 result.AppendError ("The process must be paused to use this command.");
3655 result.SetStatus (eReturnStatusFailed);
3656 return false;
3657 }
3658
3659 ThreadSP thread(threads.GetThreadAtIndex(0));
3660 if (thread.get() == NULL)
3661 {
3662 result.AppendError ("The process must be paused to use this command.");
3663 result.SetStatus (eReturnStatusFailed);
3664 return false;
3665 }
3666
Jason Molenda535ab862013-04-23 04:30:57 +00003667 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003668
Jason Molenda380241a2012-07-12 00:20:07 +00003669 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3670 {
Jason Molenda380241a2012-07-12 00:20:07 +00003671 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003672 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3673 }
3674 else if (m_options.m_type == eLookupTypeAddress && target)
3675 {
3676 Address addr;
3677 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003678 {
3679 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003680 ModuleSP module_sp (addr.GetModule());
3681 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3682 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003683 {
Jason Molenda535ab862013-04-23 04:30:57 +00003684 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003685 }
Jason Molenda535ab862013-04-23 04:30:57 +00003686 }
3687 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003688 else
3689 {
3690 result.AppendError ("address-expression or function name option must be specified.");
3691 result.SetStatus (eReturnStatusFailed);
3692 return false;
3693 }
Jason Molenda380241a2012-07-12 00:20:07 +00003694
Jason Molenda535ab862013-04-23 04:30:57 +00003695 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003696 if (num_matches == 0)
3697 {
3698 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3699 result.SetStatus (eReturnStatusFailed);
3700 return false;
3701 }
3702
Jason Molenda535ab862013-04-23 04:30:57 +00003703 for (uint32_t idx = 0; idx < num_matches; idx++)
3704 {
3705 SymbolContext sc;
3706 sc_list.GetContextAtIndex(idx, sc);
3707 if (sc.symbol == NULL && sc.function == NULL)
3708 continue;
3709 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3710 continue;
3711 AddressRange range;
3712 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3713 continue;
3714 if (!range.GetBaseAddress().IsValid())
3715 continue;
3716 ConstString funcname(sc.GetFunctionName());
3717 if (funcname.IsEmpty())
3718 continue;
3719 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3720 if (abi)
3721 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003722
Jason Molenda535ab862013-04-23 04:30:57 +00003723 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3724 if (func_unwinders_sp.get() == NULL)
3725 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003726
Jason Molenda535ab862013-04-23 04:30:57 +00003727 Address first_non_prologue_insn (func_unwinders_sp->GetFirstNonPrologueInsn(*target));
3728 if (first_non_prologue_insn.IsValid())
3729 {
3730 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 Molenda380241a2012-07-12 00:20:07 +00003731 result.GetOutputStream().Printf ("\n");
3732 }
Jason Molenda535ab862013-04-23 04:30:57 +00003733
Todd Fiala05625242014-08-25 20:29:09 +00003734 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread.get(), -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003735 if (non_callsite_unwind_plan.get())
3736 {
3737 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);
3738 non_callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3739 result.GetOutputStream().Printf ("\n");
3740 }
3741
3742 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(-1);
3743 if (callsite_unwind_plan.get())
3744 {
3745 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);
3746 callsite_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3747 result.GetOutputStream().Printf ("\n");
3748 }
3749
3750 UnwindPlanSP arch_default_unwind_plan = func_unwinders_sp->GetUnwindPlanArchitectureDefault(*thread.get());
3751 if (arch_default_unwind_plan.get())
3752 {
3753 result.GetOutputStream().Printf("Architecture default UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3754 arch_default_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3755 result.GetOutputStream().Printf ("\n");
3756 }
3757
3758 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3759 if (fast_unwind_plan.get())
3760 {
3761 result.GetOutputStream().Printf("Fast UnwindPlan for %s`%s (start addr 0x%" PRIx64 "):\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
3762 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3763 result.GetOutputStream().Printf ("\n");
3764 }
3765
3766
3767 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003768 }
3769 return result.Succeeded();
3770 }
3771
3772 CommandOptions m_options;
3773};
3774
3775OptionDefinition
3776CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3777{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003778 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3779 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3780 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jason Molenda380241a2012-07-12 00:20:07 +00003781};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003782
3783//----------------------------------------------------------------------
3784// Lookup information in images
3785//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003786class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003787{
3788public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003789 enum
3790 {
3791 eLookupTypeInvalid = -1,
3792 eLookupTypeAddress = 0,
3793 eLookupTypeSymbol,
3794 eLookupTypeFileLine, // Line is optional
3795 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003796 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003797 eLookupTypeType,
3798 kNumLookupTypes
3799 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003800
Greg Claytoneffe5c92011-05-03 22:09:39 +00003801 class CommandOptions : public Options
3802 {
3803 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003804 CommandOptions (CommandInterpreter &interpreter) :
3805 Options(interpreter)
3806 {
3807 OptionParsingStarting();
3808 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003809
Greg Claytoneffe5c92011-05-03 22:09:39 +00003810 virtual
3811 ~CommandOptions ()
3812 {
3813 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003814
Greg Claytoneffe5c92011-05-03 22:09:39 +00003815 virtual Error
3816 SetOptionValue (uint32_t option_idx, const char *option_arg)
3817 {
3818 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003819
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003820 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003821
Greg Claytoneffe5c92011-05-03 22:09:39 +00003822 switch (short_option)
3823 {
3824 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003825 {
3826 m_type = eLookupTypeAddress;
3827 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3828 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3829 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003830 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003831
Greg Claytoneffe5c92011-05-03 22:09:39 +00003832 case 'o':
3833 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
3834 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003835 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003836 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003837
Greg Claytoneffe5c92011-05-03 22:09:39 +00003838 case 's':
3839 m_str = option_arg;
3840 m_type = eLookupTypeSymbol;
3841 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003842
Greg Claytoneffe5c92011-05-03 22:09:39 +00003843 case 'f':
3844 m_file.SetFile (option_arg, false);
3845 m_type = eLookupTypeFileLine;
3846 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003847
Greg Claytoneffe5c92011-05-03 22:09:39 +00003848 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003849 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003850 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003851
Greg Claytoneffe5c92011-05-03 22:09:39 +00003852 case 'l':
3853 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
3854 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003855 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003856 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003857 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003858 m_type = eLookupTypeFileLine;
3859 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003860
Greg Claytonc4a8a762012-05-15 18:43:44 +00003861 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003862 m_str = option_arg;
3863 m_type = eLookupTypeFunction;
3864 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003865
Greg Claytonc4a8a762012-05-15 18:43:44 +00003866 case 'n':
3867 m_str = option_arg;
3868 m_type = eLookupTypeFunctionOrSymbol;
3869 break;
3870
Greg Claytoneffe5c92011-05-03 22:09:39 +00003871 case 't':
3872 m_str = option_arg;
3873 m_type = eLookupTypeType;
3874 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003875
Greg Claytoneffe5c92011-05-03 22:09:39 +00003876 case 'v':
3877 m_verbose = 1;
3878 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003879
Sean Callanand38b4a92012-06-06 20:49:55 +00003880 case 'A':
3881 m_print_all = true;
3882 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003883
Greg Claytoneffe5c92011-05-03 22:09:39 +00003884 case 'r':
3885 m_use_regex = true;
3886 break;
3887 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003888
Greg Claytoneffe5c92011-05-03 22:09:39 +00003889 return error;
3890 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003891
Greg Claytoneffe5c92011-05-03 22:09:39 +00003892 void
3893 OptionParsingStarting ()
3894 {
3895 m_type = eLookupTypeInvalid;
3896 m_str.clear();
3897 m_file.Clear();
3898 m_addr = LLDB_INVALID_ADDRESS;
3899 m_offset = 0;
3900 m_line_number = 0;
3901 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003902 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003903 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003904 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003905 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003906
Greg Claytoneffe5c92011-05-03 22:09:39 +00003907 const OptionDefinition*
3908 GetDefinitions ()
3909 {
3910 return g_option_table;
3911 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003912
Greg Claytoneffe5c92011-05-03 22:09:39 +00003913 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003914
Greg Claytoneffe5c92011-05-03 22:09:39 +00003915 static OptionDefinition g_option_table[];
3916 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3917 std::string m_str; // Holds name lookup
3918 FileSpec m_file; // Files for file lookups
3919 lldb::addr_t m_addr; // Holds the address to lookup
3920 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3921 uint32_t m_line_number; // Line number for file+line lookups
3922 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00003923 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003924 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00003925 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003926 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003927
Greg Claytoneffe5c92011-05-03 22:09:39 +00003928 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003929 CommandObjectParsed (interpreter,
3930 "target modules lookup",
3931 "Look up information within executable and dependent shared library images.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003932 NULL,
3933 eFlagRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00003934 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003935 {
3936 CommandArgumentEntry arg;
3937 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003938
Greg Claytoneffe5c92011-05-03 22:09:39 +00003939 // Define the first (and only) variant of this arg.
3940 file_arg.arg_type = eArgTypeFilename;
3941 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003942
Greg Claytoneffe5c92011-05-03 22:09:39 +00003943 // There is only one variant this argument could be; put it into the argument entry.
3944 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003945
Greg Claytoneffe5c92011-05-03 22:09:39 +00003946 // Push the data for the first argument into the m_arguments vector.
3947 m_arguments.push_back (arg);
3948 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003949
Greg Claytoneffe5c92011-05-03 22:09:39 +00003950 virtual
3951 ~CommandObjectTargetModulesLookup ()
3952 {
3953 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003954
Greg Claytoneffe5c92011-05-03 22:09:39 +00003955 virtual Options *
3956 GetOptions ()
3957 {
3958 return &m_options;
3959 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003960
Sean Callanand38b4a92012-06-06 20:49:55 +00003961 bool
3962 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
3963 {
3964 switch (m_options.m_type)
3965 {
3966 case eLookupTypeAddress:
3967 case eLookupTypeFileLine:
3968 case eLookupTypeFunction:
3969 case eLookupTypeFunctionOrSymbol:
3970 case eLookupTypeSymbol:
3971 default:
3972 return false;
3973 case eLookupTypeType:
3974 break;
3975 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003976
Jason Molendab57e4a12013-11-04 09:33:30 +00003977 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003978
Sean Callanand38b4a92012-06-06 20:49:55 +00003979 if (!frame)
3980 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003981
Sean Callanand38b4a92012-06-06 20:49:55 +00003982 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003983
Sean Callanand38b4a92012-06-06 20:49:55 +00003984 if (!sym_ctx.module_sp)
3985 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003986
Sean Callanand38b4a92012-06-06 20:49:55 +00003987 switch (m_options.m_type)
3988 {
3989 default:
3990 return false;
3991 case eLookupTypeType:
3992 if (!m_options.m_str.empty())
3993 {
3994 if (LookupTypeHere (m_interpreter,
3995 result.GetOutputStream(),
3996 sym_ctx,
3997 m_options.m_str.c_str(),
3998 m_options.m_use_regex))
3999 {
4000 result.SetStatus(eReturnStatusSuccessFinishResult);
4001 return true;
4002 }
4003 }
4004 break;
4005 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004006
Sean Callanand38b4a92012-06-06 20:49:55 +00004007 return true;
4008 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004009
Greg Claytoneffe5c92011-05-03 22:09:39 +00004010 bool
4011 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4012 {
4013 switch (m_options.m_type)
4014 {
4015 case eLookupTypeAddress:
4016 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4017 {
4018 if (LookupAddressInModule (m_interpreter,
4019 result.GetOutputStream(),
4020 module,
4021 eSymbolContextEverything,
4022 m_options.m_addr,
4023 m_options.m_offset,
4024 m_options.m_verbose))
4025 {
4026 result.SetStatus(eReturnStatusSuccessFinishResult);
4027 return true;
4028 }
4029 }
4030 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004031
Greg Claytoneffe5c92011-05-03 22:09:39 +00004032 case eLookupTypeSymbol:
4033 if (!m_options.m_str.empty())
4034 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004035 if (LookupSymbolInModule (m_interpreter,
4036 result.GetOutputStream(),
4037 module,
4038 m_options.m_str.c_str(),
4039 m_options.m_use_regex,
4040 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004041 {
4042 result.SetStatus(eReturnStatusSuccessFinishResult);
4043 return true;
4044 }
4045 }
4046 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004047
Greg Claytoneffe5c92011-05-03 22:09:39 +00004048 case eLookupTypeFileLine:
4049 if (m_options.m_file)
4050 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004051 if (LookupFileAndLineInModule (m_interpreter,
4052 result.GetOutputStream(),
4053 module,
4054 m_options.m_file,
4055 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004056 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004057 m_options.m_verbose))
4058 {
4059 result.SetStatus(eReturnStatusSuccessFinishResult);
4060 return true;
4061 }
4062 }
4063 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004064
4065 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004066 case eLookupTypeFunction:
4067 if (!m_options.m_str.empty())
4068 {
4069 if (LookupFunctionInModule (m_interpreter,
4070 result.GetOutputStream(),
4071 module,
4072 m_options.m_str.c_str(),
4073 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004074 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004075 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004076 m_options.m_verbose))
4077 {
4078 result.SetStatus(eReturnStatusSuccessFinishResult);
4079 return true;
4080 }
4081 }
4082 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004083
Greg Claytoneffe5c92011-05-03 22:09:39 +00004084 case eLookupTypeType:
4085 if (!m_options.m_str.empty())
4086 {
4087 if (LookupTypeInModule (m_interpreter,
4088 result.GetOutputStream(),
4089 module,
4090 m_options.m_str.c_str(),
4091 m_options.m_use_regex))
4092 {
4093 result.SetStatus(eReturnStatusSuccessFinishResult);
4094 return true;
4095 }
4096 }
4097 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004098
Greg Claytoneffe5c92011-05-03 22:09:39 +00004099 default:
4100 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4101 syntax_error = true;
4102 break;
4103 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004104
Greg Claytoneffe5c92011-05-03 22:09:39 +00004105 result.SetStatus (eReturnStatusFailed);
4106 return false;
4107 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004108
Jim Ingham5a988412012-06-08 21:56:10 +00004109protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004110 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004111 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004112 CommandReturnObject &result)
4113 {
4114 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4115 if (target == NULL)
4116 {
4117 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4118 result.SetStatus (eReturnStatusFailed);
4119 return false;
4120 }
4121 else
4122 {
4123 bool syntax_error = false;
4124 uint32_t i;
4125 uint32_t num_successful_lookups = 0;
4126 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4127 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4128 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4129 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004130
Greg Claytoneffe5c92011-05-03 22:09:39 +00004131 if (command.GetArgumentCount() == 0)
4132 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004133 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004134
Sean Callanand38b4a92012-06-06 20:49:55 +00004135 // Where it is possible to look in the current symbol context
4136 // first, try that. If this search was successful and --all
4137 // was not passed, don't print anything else.
4138 if (LookupHere (m_interpreter, result, syntax_error))
4139 {
4140 result.GetOutputStream().EOL();
4141 num_successful_lookups++;
4142 if (!m_options.m_print_all)
4143 {
4144 result.SetStatus (eReturnStatusSuccessFinishResult);
4145 return result.Succeeded();
4146 }
4147 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004148
Sean Callanand38b4a92012-06-06 20:49:55 +00004149 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004150
Enrico Granata17598482012-11-08 02:22:02 +00004151 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004152 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004153 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004154 if (num_modules > 0)
4155 {
4156 for (i = 0; i<num_modules && syntax_error == false; ++i)
4157 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004158 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004159
Sean Callanand38b4a92012-06-06 20:49:55 +00004160 if (module_pointer != current_module.get() &&
4161 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004162 {
4163 result.GetOutputStream().EOL();
4164 num_successful_lookups++;
4165 }
4166 }
4167 }
4168 else
4169 {
4170 result.AppendError ("the target has no associated executable images");
4171 result.SetStatus (eReturnStatusFailed);
4172 return false;
4173 }
4174 }
4175 else
4176 {
4177 // Dump specified images (by basename or fullpath)
4178 const char *arg_cstr;
4179 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4180 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004181 ModuleList module_list;
4182 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4183 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004184 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004185 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004186 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004187 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004188 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004189 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004190 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004191 {
4192 result.GetOutputStream().EOL();
4193 num_successful_lookups++;
4194 }
4195 }
4196 }
4197 }
4198 else
4199 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4200 }
4201 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004202
Greg Claytoneffe5c92011-05-03 22:09:39 +00004203 if (num_successful_lookups > 0)
4204 result.SetStatus (eReturnStatusSuccessFinishResult);
4205 else
4206 result.SetStatus (eReturnStatusFailed);
4207 }
4208 return result.Succeeded();
4209 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004210
Greg Claytoneffe5c92011-05-03 22:09:39 +00004211 CommandOptions m_options;
4212};
4213
4214OptionDefinition
4215CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4216{
Zachary Turnerd37221d2014-07-09 16:31:49 +00004217 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4218 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
Greg Claytonc4a8a762012-05-15 18:43:44 +00004219 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4220 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Zachary Turnerd37221d2014-07-09 16:31:49 +00004221 false, "regex", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4222 { LLDB_OPT_SET_2, true, "symbol", 's', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules."},
4223 { LLDB_OPT_SET_3, true, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules."},
4224 { LLDB_OPT_SET_3, false, "line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
Jim Inghama8f55662012-06-04 22:47:34 +00004225 { LLDB_OPT_SET_FROM_TO(3,5),
Zachary Turnerd37221d2014-07-09 16:31:49 +00004226 false, "no-inlines", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4227 { LLDB_OPT_SET_4, true, "function", 'F', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
4228 { LLDB_OPT_SET_5, true, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules."},
4229 { LLDB_OPT_SET_6, true, "type", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
4230 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4231 { LLDB_OPT_SET_ALL, false, "all", 'A', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available."},
4232 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004233};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004234
4235
Jim Ingham9575d842011-03-11 03:53:59 +00004236#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004237
4238//-------------------------------------------------------------------------
4239// CommandObjectMultiwordImageSearchPaths
4240//-------------------------------------------------------------------------
4241
Greg Claytoneffe5c92011-05-03 22:09:39 +00004242class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004243{
4244public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004245 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4246 CommandObjectMultiword (interpreter,
4247 "target modules search-paths",
4248 "A set of commands for operating on debugger target image search paths.",
4249 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004250 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004251 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4252 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4253 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4254 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4255 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004256 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004257
Greg Claytoneffe5c92011-05-03 22:09:39 +00004258 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004259 {
4260 }
4261};
4262
Greg Claytoneffe5c92011-05-03 22:09:39 +00004263
4264
4265#pragma mark CommandObjectTargetModules
4266
4267//-------------------------------------------------------------------------
4268// CommandObjectTargetModules
4269//-------------------------------------------------------------------------
4270
4271class CommandObjectTargetModules : public CommandObjectMultiword
4272{
4273public:
4274 //------------------------------------------------------------------
4275 // Constructors and Destructors
4276 //------------------------------------------------------------------
4277 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4278 CommandObjectMultiword (interpreter,
4279 "target modules",
4280 "A set of commands for accessing information for one or more target modules.",
4281 "target modules <sub-command> ...")
4282 {
4283 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4284 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004285 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4286 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4287 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4288 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004289 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004290
4291 }
4292 virtual
4293 ~CommandObjectTargetModules()
4294 {
4295 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004296
Greg Claytoneffe5c92011-05-03 22:09:39 +00004297private:
4298 //------------------------------------------------------------------
4299 // For CommandObjectTargetModules only
4300 //------------------------------------------------------------------
4301 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4302};
4303
4304
Greg Claytone72dfb32012-02-24 01:59:29 +00004305
Jim Ingham5a988412012-06-08 21:56:10 +00004306class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004307{
4308public:
4309 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004310 CommandObjectParsed (interpreter,
4311 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004312 "Add a debug symbol file to one of the target's current modules by specifying a path to a debug symbols file, or using the options to specify a module to download symbols for.",
Jason Molendab9c9f9b2013-02-02 06:00:36 +00004313 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004314 m_option_group (interpreter),
4315 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4316 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4317
Greg Claytone72dfb32012-02-24 01:59:29 +00004318 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004319 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4320 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4321 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4322 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004323 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004324
Greg Claytone72dfb32012-02-24 01:59:29 +00004325 virtual
4326 ~CommandObjectTargetSymbolsAdd ()
4327 {
4328 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004329
Greg Claytonc7bece562013-01-25 18:06:21 +00004330 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00004331 HandleArgumentCompletion (Args &input,
4332 int &cursor_index,
4333 int &cursor_char_position,
4334 OptionElementVector &opt_element_vector,
4335 int match_start_point,
4336 int max_return_elements,
4337 bool &word_complete,
4338 StringList &matches)
4339 {
4340 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4341 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004342
Jim Ingham5a988412012-06-08 21:56:10 +00004343 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4344 CommandCompletions::eDiskFileCompletion,
4345 completion_str.c_str(),
4346 match_start_point,
4347 max_return_elements,
4348 NULL,
4349 word_complete,
4350 matches);
4351 return matches.GetSize();
4352 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004353
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004354 virtual Options *
4355 GetOptions ()
4356 {
4357 return &m_option_group;
4358 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004359
Jim Ingham5a988412012-06-08 21:56:10 +00004360protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004361 bool
4362 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004363 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004364 bool &flush,
4365 CommandReturnObject &result)
4366 {
Greg Clayton89deb062012-12-12 01:15:30 +00004367 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4368 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004369 {
4370 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004371 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004372
Greg Clayton89deb062012-12-12 01:15:30 +00004373 if (!module_spec.GetUUID().IsValid())
4374 {
4375 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4376 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4377 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004378 // We now have a module that represents a symbol file
4379 // that can be used for a module that might exist in the
4380 // current target, so we need to find that module in the
4381 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004382 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004383
Greg Clayton9aae0a12013-05-15 19:52:08 +00004384 size_t num_matches = 0;
4385 // First extract all module specs from the symbol file
4386 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004387 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004388 {
4389 // Now extract the module spec that matches the target architecture
4390 ModuleSpec target_arch_module_spec;
4391 ModuleSpec symfile_module_spec;
4392 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4393 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4394 {
4395 // See if it has a UUID?
4396 if (symfile_module_spec.GetUUID().IsValid())
4397 {
4398 // It has a UUID, look for this UUID in the target modules
4399 ModuleSpec symfile_uuid_module_spec;
4400 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4401 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4402 }
4403 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004404
Greg Clayton9aae0a12013-05-15 19:52:08 +00004405 if (num_matches == 0)
4406 {
4407 // No matches yet, iterate through the module specs to find a UUID value that
4408 // we can match up to an image in our target
4409 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4410 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4411 {
4412 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4413 {
4414 if (symfile_module_spec.GetUUID().IsValid())
4415 {
4416 // It has a UUID, look for this UUID in the target modules
4417 ModuleSpec symfile_uuid_module_spec;
4418 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4419 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004420 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004421 }
4422 }
4423 }
4424 }
4425
4426 // Just try to match up the file by basename if we have no matches at this point
4427 if (num_matches == 0)
4428 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004429
Greg Clayton91c0e742013-01-11 23:44:27 +00004430 while (num_matches == 0)
4431 {
4432 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4433 // Empty string returned, lets bail
4434 if (!filename_no_extension)
4435 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004436
Greg Clayton91c0e742013-01-11 23:44:27 +00004437 // Check if there was no extension to strip and the basename is the same
4438 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4439 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004440
Greg Clayton91c0e742013-01-11 23:44:27 +00004441 // Replace basename with one less extension
4442 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004443
Greg Clayton91c0e742013-01-11 23:44:27 +00004444 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4445 }
4446
Greg Clayton89deb062012-12-12 01:15:30 +00004447 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004448 {
Greg Clayton89deb062012-12-12 01:15:30 +00004449 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4450 }
4451 else if (num_matches == 1)
4452 {
4453 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004454
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004455 // The module has not yet created its symbol vendor, we can just
4456 // give the existing target module the symfile path to use for
4457 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004458 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004459
Greg Clayton136dff82012-12-14 02:15:00 +00004460 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004461 if (symbol_vendor)
4462 {
4463 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004464
Greg Clayton89deb062012-12-12 01:15:30 +00004465 if (symbol_file)
4466 {
4467 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004468
Greg Clayton89deb062012-12-12 01:15:30 +00004469 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4470 {
4471 // Provide feedback that the symfile has been successfully added.
4472 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004473 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004474 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004475 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004476
Greg Clayton89deb062012-12-12 01:15:30 +00004477 // Let clients know something changed in the module
4478 // if it is currently loaded
4479 ModuleList module_list;
4480 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004481 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004482
Greg Clayton91c0e742013-01-11 23:44:27 +00004483 // Make sure we load any scripting resources that may be embedded
4484 // in the debug info files in case the platform supports that.
4485 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004486 StreamString feedback_stream;
4487 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004488 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004489 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4490 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4491 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004492 else if (feedback_stream.GetSize())
4493 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004494
Greg Clayton89deb062012-12-12 01:15:30 +00004495 flush = true;
4496 result.SetStatus (eReturnStatusSuccessFinishResult);
4497 return true;
4498 }
4499 }
4500 }
4501 // Clear the symbol file spec if anything went wrong
4502 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004503 }
4504
4505 if (module_spec.GetUUID().IsValid())
4506 {
4507 StreamString ss_symfile_uuid;
4508 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4509 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4510 symfile_path,
4511 ss_symfile_uuid.GetData(),
4512 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4513 ? "\n please specify the full path to the symbol file"
4514 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004515 }
4516 else
4517 {
Greg Clayton89deb062012-12-12 01:15:30 +00004518 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4519 symfile_path,
4520 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4521 ? "\n please specify the full path to the symbol file"
4522 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004523 }
4524 }
4525 else
4526 {
4527 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004528 }
Greg Clayton89deb062012-12-12 01:15:30 +00004529 result.SetStatus (eReturnStatusFailed);
4530 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004531 }
4532
Greg Claytone72dfb32012-02-24 01:59:29 +00004533 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004534 DoExecute (Args& args,
Greg Claytone72dfb32012-02-24 01:59:29 +00004535 CommandReturnObject &result)
4536 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004537 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004538 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004539 bool flush = false;
4540 ModuleSpec module_spec;
4541 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4542 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4543 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004544
Greg Claytonf9fc6092013-01-09 19:44:40 +00004545 const size_t argc = args.GetArgumentCount();
4546 if (argc == 0)
4547 {
4548 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004549 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004550 bool success = false;
4551 bool error_set = false;
4552 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004553 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004554 Process *process = m_exe_ctx.GetProcessPtr();
4555 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004556 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004557 const StateType process_state = process->GetState();
4558 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004559 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004560 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004561 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004562 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004563 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4564 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004565 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004566 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004567 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004568 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4569 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004570 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004571 module_spec.GetUUID() = frame_module_sp->GetUUID();
4572 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004573 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004574 else
4575 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004576 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004577 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004578 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004579 }
4580 else
4581 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004582 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004583 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004584 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004585 }
4586 else
4587 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004588 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004589 error_set = true;
4590 }
4591 }
4592 else
4593 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004594 result.AppendError ("a process must exist in order to use the --frame option");
4595 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004596 }
4597 }
4598 else
4599 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004600 if (uuid_option_set)
4601 {
4602 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4603 success |= module_spec.GetUUID().IsValid();
4604 }
4605 else if (file_option_set)
4606 {
4607 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4608 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4609 if (module_sp)
4610 {
4611 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4612 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4613 module_spec.GetUUID() = module_sp->GetUUID();
4614 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4615 }
4616 else
4617 {
4618 module_spec.GetArchitecture() = target->GetArchitecture();
4619 }
4620 success |= module_spec.GetFileSpec().Exists();
4621 }
4622 }
4623
4624 if (success)
4625 {
4626 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4627 {
4628 if (module_spec.GetSymbolFileSpec())
4629 success = AddModuleSymbols (target, module_spec, flush, result);
4630 }
4631 }
4632
4633 if (!success && !error_set)
4634 {
4635 StreamString error_strm;
4636 if (uuid_option_set)
4637 {
4638 error_strm.PutCString("unable to find debug symbols for UUID ");
4639 module_spec.GetUUID().Dump (&error_strm);
4640 }
4641 else if (file_option_set)
4642 {
4643 error_strm.PutCString("unable to find debug symbols for the executable file ");
4644 error_strm << module_spec.GetFileSpec();
4645 }
4646 else if (frame_option_set)
4647 {
4648 error_strm.PutCString("unable to find debug symbols for the current frame");
4649 }
4650 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004651 }
4652 }
4653 else
4654 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004655 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4656 }
4657 }
4658 else
4659 {
4660 if (uuid_option_set)
4661 {
4662 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4663 }
4664 else if (file_option_set)
4665 {
4666 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4667 }
4668 else if (frame_option_set)
4669 {
4670 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4671 }
4672 else
4673 {
4674 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004675
Greg Claytonf9fc6092013-01-09 19:44:40 +00004676 for (size_t i=0; i<argc; ++i)
4677 {
4678 const char *symfile_path = args.GetArgumentAtIndex(i);
4679 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004680 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004681 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4682 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004683 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004684 FileSpec symfile_spec;
4685 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4686 module_spec.GetSymbolFileSpec() = symfile_spec;
4687 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004688
Greg Claytonf9fc6092013-01-09 19:44:40 +00004689 ArchSpec arch;
4690 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004691
Greg Claytonf9fc6092013-01-09 19:44:40 +00004692 if (symfile_exists)
4693 {
4694 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004695 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004696 }
4697 else
4698 {
4699 char resolved_symfile_path[PATH_MAX];
4700 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4701 {
4702 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4703 {
4704 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4705 break;
4706 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004707 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004708 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4709 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004710 }
4711 }
4712 }
4713 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004714 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004715
Greg Claytonf9fc6092013-01-09 19:44:40 +00004716 if (flush)
4717 {
4718 Process *process = m_exe_ctx.GetProcessPtr();
4719 if (process)
4720 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004721 }
4722 return result.Succeeded();
4723 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004724
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004725 OptionGroupOptions m_option_group;
4726 OptionGroupUUID m_uuid_option_group;
4727 OptionGroupFile m_file_option;
4728 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004729};
4730
4731
4732#pragma mark CommandObjectTargetSymbols
4733
4734//-------------------------------------------------------------------------
4735// CommandObjectTargetSymbols
4736//-------------------------------------------------------------------------
4737
4738class CommandObjectTargetSymbols : public CommandObjectMultiword
4739{
4740public:
4741 //------------------------------------------------------------------
4742 // Constructors and Destructors
4743 //------------------------------------------------------------------
4744 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4745 CommandObjectMultiword (interpreter,
4746 "target symbols",
4747 "A set of commands for adding and managing debug symbol files.",
4748 "target symbols <sub-command> ...")
4749 {
4750 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004751
Greg Claytone72dfb32012-02-24 01:59:29 +00004752 }
4753 virtual
4754 ~CommandObjectTargetSymbols()
4755 {
4756 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004757
Greg Claytone72dfb32012-02-24 01:59:29 +00004758private:
4759 //------------------------------------------------------------------
4760 // For CommandObjectTargetModules only
4761 //------------------------------------------------------------------
4762 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4763};
4764
4765
Jim Ingham9575d842011-03-11 03:53:59 +00004766#pragma mark CommandObjectTargetStopHookAdd
4767
4768//-------------------------------------------------------------------------
4769// CommandObjectTargetStopHookAdd
4770//-------------------------------------------------------------------------
4771
Greg Clayton44d93782014-01-27 23:43:24 +00004772class CommandObjectTargetStopHookAdd :
4773 public CommandObjectParsed,
4774 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004775{
4776public:
4777
4778 class CommandOptions : public Options
4779 {
4780 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004781 CommandOptions (CommandInterpreter &interpreter) :
4782 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004783 m_line_start(0),
4784 m_line_end (UINT_MAX),
4785 m_func_name_type_mask (eFunctionNameTypeAuto),
4786 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004787 m_thread_specified (false),
4788 m_use_one_liner (false),
4789 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004790 {
4791 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004792
Jim Ingham9575d842011-03-11 03:53:59 +00004793 ~CommandOptions () {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004794
Greg Claytone0d378b2011-03-24 21:19:54 +00004795 const OptionDefinition*
Jim Ingham9575d842011-03-11 03:53:59 +00004796 GetDefinitions ()
4797 {
4798 return g_option_table;
4799 }
4800
4801 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +00004802 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham9575d842011-03-11 03:53:59 +00004803 {
4804 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004805 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004806 bool success;
4807
4808 switch (short_option)
4809 {
4810 case 'c':
4811 m_class_name = option_arg;
4812 m_sym_ctx_specified = true;
4813 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004814
Jim Ingham9575d842011-03-11 03:53:59 +00004815 case 'e':
4816 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success);
4817 if (!success)
4818 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004819 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004820 break;
4821 }
4822 m_sym_ctx_specified = true;
4823 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004824
Jim Ingham9575d842011-03-11 03:53:59 +00004825 case 'l':
4826 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success);
4827 if (!success)
4828 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004829 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004830 break;
4831 }
4832 m_sym_ctx_specified = true;
4833 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004834
Sean Callanand4a7c122012-02-11 01:22:21 +00004835 case 'i':
4836 m_no_inlines = true;
4837 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004838
Jim Ingham9575d842011-03-11 03:53:59 +00004839 case 'n':
4840 m_function_name = option_arg;
4841 m_func_name_type_mask |= eFunctionNameTypeAuto;
4842 m_sym_ctx_specified = true;
4843 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004844
Jim Ingham9575d842011-03-11 03:53:59 +00004845 case 'f':
4846 m_file_name = option_arg;
4847 m_sym_ctx_specified = true;
4848 break;
4849 case 's':
4850 m_module_name = option_arg;
4851 m_sym_ctx_specified = true;
4852 break;
4853 case 't' :
4854 {
Jim Ingham0292f4a2011-03-22 01:53:33 +00004855 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004856 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004857 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004858 m_thread_specified = true;
4859 }
4860 break;
4861 case 'T':
4862 m_thread_name = option_arg;
4863 m_thread_specified = true;
4864 break;
4865 case 'q':
4866 m_queue_name = option_arg;
4867 m_thread_specified = true;
4868 break;
4869 case 'x':
4870 {
Jim Ingham0292f4a2011-03-22 01:53:33 +00004871 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004872 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004873 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004874 m_thread_specified = true;
4875 }
4876 break;
Johnny Chenb1372c02011-05-02 23:47:55 +00004877 case 'o':
4878 m_use_one_liner = true;
4879 m_one_liner = option_arg;
4880 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004881 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004882 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Ingham9575d842011-03-11 03:53:59 +00004883 break;
4884 }
4885 return error;
4886 }
4887
4888 void
Greg Claytonf6b8b582011-04-13 00:18:08 +00004889 OptionParsingStarting ()
Jim Ingham9575d842011-03-11 03:53:59 +00004890 {
4891 m_class_name.clear();
4892 m_function_name.clear();
4893 m_line_start = 0;
4894 m_line_end = UINT_MAX;
4895 m_file_name.clear();
4896 m_module_name.clear();
4897 m_func_name_type_mask = eFunctionNameTypeAuto;
4898 m_thread_id = LLDB_INVALID_THREAD_ID;
4899 m_thread_index = UINT32_MAX;
4900 m_thread_name.clear();
4901 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004902
Sean Callanand4a7c122012-02-11 01:22:21 +00004903 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004904 m_sym_ctx_specified = false;
4905 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004906
4907 m_use_one_liner = false;
4908 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004909 }
4910
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004911
Greg Claytone0d378b2011-03-24 21:19:54 +00004912 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004913
Jim Ingham9575d842011-03-11 03:53:59 +00004914 std::string m_class_name;
4915 std::string m_function_name;
4916 uint32_t m_line_start;
4917 uint32_t m_line_end;
4918 std::string m_file_name;
4919 std::string m_module_name;
4920 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4921 lldb::tid_t m_thread_id;
4922 uint32_t m_thread_index;
4923 std::string m_thread_name;
4924 std::string m_queue_name;
4925 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004926 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004927 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00004928 // Instance variables to hold the values for one_liner options.
4929 bool m_use_one_liner;
4930 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00004931 };
4932
4933 Options *
4934 GetOptions ()
4935 {
4936 return &m_options;
4937 }
4938
4939 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004940 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00004941 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00004942 "Add a hook to be executed when the target stops.",
4943 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00004944 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00004945 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00004946 {
4947 }
4948
4949 ~CommandObjectTargetStopHookAdd ()
4950 {
4951 }
4952
Jim Ingham5a988412012-06-08 21:56:10 +00004953protected:
Greg Clayton44d93782014-01-27 23:43:24 +00004954 virtual void
4955 IOHandlerActivated (IOHandler &io_handler)
4956 {
4957 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
4958 if (output_sp)
4959 {
4960 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
4961 output_sp->Flush();
4962 }
4963 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004964
Greg Clayton44d93782014-01-27 23:43:24 +00004965 virtual void
4966 IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
4967 {
4968 if (m_stop_hook_sp)
4969 {
4970 if (line.empty())
4971 {
4972 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
4973 if (error_sp)
4974 {
4975 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
4976 error_sp->Flush();
4977 }
4978 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4979 if (target)
4980 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
4981 }
4982 else
4983 {
4984 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
4985 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
4986 if (output_sp)
4987 {
4988 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
4989 output_sp->Flush();
4990 }
4991 }
4992 m_stop_hook_sp.reset();
4993 }
4994 io_handler.SetIsDone(true);
4995 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004996
Jim Ingham9575d842011-03-11 03:53:59 +00004997 bool
Jim Ingham5a988412012-06-08 21:56:10 +00004998 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00004999 {
Greg Clayton44d93782014-01-27 23:43:24 +00005000 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005001
Jim Ingham9575d842011-03-11 03:53:59 +00005002 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5003 if (target)
5004 {
Greg Clayton44d93782014-01-27 23:43:24 +00005005 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005006
5007 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005008 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005009 if (m_options.m_sym_ctx_specified)
5010 {
5011 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005012
Jim Ingham9575d842011-03-11 03:53:59 +00005013 if (!m_options.m_module_name.empty())
5014 {
5015 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5016 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005017
Jim Ingham9575d842011-03-11 03:53:59 +00005018 if (!m_options.m_class_name.empty())
5019 {
5020 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5021 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005022
Jim Ingham9575d842011-03-11 03:53:59 +00005023 if (!m_options.m_file_name.empty())
5024 {
5025 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5026 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005027
Jim Ingham9575d842011-03-11 03:53:59 +00005028 if (m_options.m_line_start != 0)
5029 {
5030 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5031 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005032
Jim Ingham9575d842011-03-11 03:53:59 +00005033 if (m_options.m_line_end != UINT_MAX)
5034 {
5035 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5036 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005037
Jim Ingham9575d842011-03-11 03:53:59 +00005038 if (!m_options.m_function_name.empty())
5039 {
5040 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5041 }
5042 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005043
Jim Ingham9575d842011-03-11 03:53:59 +00005044 if (specifier_ap.get())
5045 new_hook_sp->SetSpecifier (specifier_ap.release());
5046
5047 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005048
Jim Ingham9575d842011-03-11 03:53:59 +00005049 if (m_options.m_thread_specified)
5050 {
5051 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005052
Jim Ingham9575d842011-03-11 03:53:59 +00005053 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5054 {
5055 thread_spec->SetTID (m_options.m_thread_id);
5056 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005057
Jim Ingham9575d842011-03-11 03:53:59 +00005058 if (m_options.m_thread_index != UINT32_MAX)
5059 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005060
Jim Ingham9575d842011-03-11 03:53:59 +00005061 if (!m_options.m_thread_name.empty())
5062 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005063
Jim Ingham9575d842011-03-11 03:53:59 +00005064 if (!m_options.m_queue_name.empty())
5065 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005066
Jim Ingham9575d842011-03-11 03:53:59 +00005067 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005068
Jim Ingham9575d842011-03-11 03:53:59 +00005069 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005070 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005071 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005072 // Use one-liner.
5073 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005074 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005075 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005076 else
Jim Ingham9575d842011-03-11 03:53:59 +00005077 {
Greg Clayton44d93782014-01-27 23:43:24 +00005078 m_stop_hook_sp = new_hook_sp;
5079 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
5080 *this, // IOHandlerDelegate
5081 true, // Run IOHandler in async mode
5082 NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
5083
Jim Ingham9575d842011-03-11 03:53:59 +00005084 }
Jim Ingham9575d842011-03-11 03:53:59 +00005085 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5086 }
5087 else
5088 {
Greg Clayton7260f622011-04-18 08:33:37 +00005089 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005090 result.SetStatus (eReturnStatusFailed);
5091 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005092
Jim Ingham9575d842011-03-11 03:53:59 +00005093 return result.Succeeded();
5094 }
5095private:
5096 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005097 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005098};
5099
Greg Claytone0d378b2011-03-24 21:19:54 +00005100OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005101CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5102{
Zachary Turnerd37221d2014-07-09 16:31:49 +00005103 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005104 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005105 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005106 "Set the module within which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005107 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005108 "The stop hook is run only for the thread whose index matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005109 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005110 "The stop hook is run only for the thread whose TID matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005111 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005112 "The stop hook is run only for the thread whose thread name matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005113 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005114 "The stop hook is run only for threads in the queue whose name is given by this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005115 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005116 "Specify the source file within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005117 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005118 "Set the start of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005119 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005120 "Set the end of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005121 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005122 "Specify the class within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005123 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005124 "Set the function name within which the stop hook will be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005125 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jim Ingham9575d842011-03-11 03:53:59 +00005126};
5127
5128#pragma mark CommandObjectTargetStopHookDelete
5129
5130//-------------------------------------------------------------------------
5131// CommandObjectTargetStopHookDelete
5132//-------------------------------------------------------------------------
5133
Jim Ingham5a988412012-06-08 21:56:10 +00005134class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005135{
5136public:
5137
5138 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005139 CommandObjectParsed (interpreter,
5140 "target stop-hook delete",
5141 "Delete a stop-hook.",
5142 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005143 {
5144 }
5145
5146 ~CommandObjectTargetStopHookDelete ()
5147 {
5148 }
5149
Jim Ingham5a988412012-06-08 21:56:10 +00005150protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005151 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005152 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005153 {
5154 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5155 if (target)
5156 {
5157 // FIXME: see if we can use the breakpoint id style parser?
5158 size_t num_args = command.GetArgumentCount();
5159 if (num_args == 0)
5160 {
5161 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5162 {
5163 result.SetStatus (eReturnStatusFailed);
5164 return false;
5165 }
5166 else
5167 {
5168 target->RemoveAllStopHooks();
5169 }
5170 }
5171 else
5172 {
5173 bool success;
5174 for (size_t i = 0; i < num_args; i++)
5175 {
5176 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5177 if (!success)
5178 {
Greg Clayton7260f622011-04-18 08:33:37 +00005179 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005180 result.SetStatus(eReturnStatusFailed);
5181 return false;
5182 }
5183 success = target->RemoveStopHookByID (user_id);
5184 if (!success)
5185 {
Greg Clayton7260f622011-04-18 08:33:37 +00005186 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005187 result.SetStatus(eReturnStatusFailed);
5188 return false;
5189 }
5190 }
5191 }
5192 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5193 }
5194 else
5195 {
Greg Clayton7260f622011-04-18 08:33:37 +00005196 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005197 result.SetStatus (eReturnStatusFailed);
5198 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005199
Jim Ingham9575d842011-03-11 03:53:59 +00005200 return result.Succeeded();
5201 }
5202};
5203#pragma mark CommandObjectTargetStopHookEnableDisable
5204
5205//-------------------------------------------------------------------------
5206// CommandObjectTargetStopHookEnableDisable
5207//-------------------------------------------------------------------------
5208
Jim Ingham5a988412012-06-08 21:56:10 +00005209class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005210{
5211public:
5212
5213 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005214 CommandObjectParsed (interpreter,
5215 name,
5216 help,
5217 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005218 m_enable (enable)
5219 {
5220 }
5221
5222 ~CommandObjectTargetStopHookEnableDisable ()
5223 {
5224 }
5225
Jim Ingham5a988412012-06-08 21:56:10 +00005226protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005227 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005228 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005229 {
5230 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5231 if (target)
5232 {
5233 // FIXME: see if we can use the breakpoint id style parser?
5234 size_t num_args = command.GetArgumentCount();
5235 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005236
Jim Ingham9575d842011-03-11 03:53:59 +00005237 if (num_args == 0)
5238 {
5239 target->SetAllStopHooksActiveState (m_enable);
5240 }
5241 else
5242 {
5243 for (size_t i = 0; i < num_args; i++)
5244 {
5245 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
5246 if (!success)
5247 {
Greg Clayton7260f622011-04-18 08:33:37 +00005248 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005249 result.SetStatus(eReturnStatusFailed);
5250 return false;
5251 }
5252 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5253 if (!success)
5254 {
Greg Clayton7260f622011-04-18 08:33:37 +00005255 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005256 result.SetStatus(eReturnStatusFailed);
5257 return false;
5258 }
5259 }
5260 }
5261 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5262 }
5263 else
5264 {
Greg Clayton7260f622011-04-18 08:33:37 +00005265 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005266 result.SetStatus (eReturnStatusFailed);
5267 }
5268 return result.Succeeded();
5269 }
5270private:
5271 bool m_enable;
5272};
5273
5274#pragma mark CommandObjectTargetStopHookList
5275
5276//-------------------------------------------------------------------------
5277// CommandObjectTargetStopHookList
5278//-------------------------------------------------------------------------
5279
Jim Ingham5a988412012-06-08 21:56:10 +00005280class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005281{
5282public:
5283
5284 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005285 CommandObjectParsed (interpreter,
5286 "target stop-hook list",
5287 "List all stop-hooks.",
5288 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005289 {
5290 }
5291
5292 ~CommandObjectTargetStopHookList ()
5293 {
5294 }
5295
Jim Ingham5a988412012-06-08 21:56:10 +00005296protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005297 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005298 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005299 {
5300 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005301 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005302 {
Greg Clayton7260f622011-04-18 08:33:37 +00005303 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005304 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005305 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005306 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005307
Jim Ingham9575d842011-03-11 03:53:59 +00005308 size_t num_hooks = target->GetNumStopHooks ();
5309 if (num_hooks == 0)
5310 {
5311 result.GetOutputStream().PutCString ("No stop hooks.\n");
5312 }
5313 else
5314 {
5315 for (size_t i = 0; i < num_hooks; i++)
5316 {
5317 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5318 if (i > 0)
5319 result.GetOutputStream().PutCString ("\n");
5320 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5321 }
5322 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005323 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005324 return result.Succeeded();
5325 }
5326};
5327
5328#pragma mark CommandObjectMultiwordTargetStopHooks
5329//-------------------------------------------------------------------------
5330// CommandObjectMultiwordTargetStopHooks
5331//-------------------------------------------------------------------------
5332
5333class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5334{
5335public:
5336
5337 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5338 CommandObjectMultiword (interpreter,
5339 "target stop-hook",
5340 "A set of commands for operating on debugger target stop-hooks.",
5341 "target stop-hook <subcommand> [<subcommand-options>]")
5342 {
5343 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5344 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5345 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5346 false,
5347 "target stop-hook disable [<id>]",
5348 "Disable a stop-hook.",
5349 "target stop-hook disable")));
5350 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5351 true,
5352 "target stop-hook enable [<id>]",
5353 "Enable a stop-hook.",
5354 "target stop-hook enable")));
5355 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5356 }
5357
5358 ~CommandObjectMultiwordTargetStopHooks()
5359 {
5360 }
5361};
5362
5363
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005364
5365#pragma mark CommandObjectMultiwordTarget
5366
5367//-------------------------------------------------------------------------
5368// CommandObjectMultiwordTarget
5369//-------------------------------------------------------------------------
5370
Greg Clayton66111032010-06-23 01:19:29 +00005371CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005372 CommandObjectMultiword (interpreter,
5373 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005374 "A set of commands for operating on debugger targets.",
5375 "target <subcommand> [<subcommand-options>]")
5376{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005377
Greg Clayton7260f622011-04-18 08:33:37 +00005378 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005379 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005380 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5381 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005382 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005383 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005384 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005385 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005386}
5387
5388CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5389{
5390}
5391
Greg Clayton7260f622011-04-18 08:33:37 +00005392