blob: b9e4942caefbd6cc3a28dcae6e223fd8c08f86f3 [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"
Vince Harron5275aaa2015-01-15 20:08:35 +000030#include "lldb/Host/StringConvert.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000031#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000034#include "lldb/Interpreter/Options.h"
Greg Clayton7260f622011-04-18 08:33:37 +000035#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000036#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000037#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000038#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton715c2362011-07-07 04:38:25 +000039#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Clayton7260f622011-04-18 08:33:37 +000040#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000041#include "lldb/Interpreter/OptionGroupUInt64.h"
42#include "lldb/Interpreter/OptionGroupUUID.h"
Jason Molendac6127dd2014-11-21 02:25:15 +000043#include "lldb/Interpreter/OptionGroupString.h"
Greg Clayton644247c2011-07-07 01:59:51 +000044#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton1f746072012-08-29 21:13:06 +000045#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000046#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000047#include "lldb/Symbol/LineTable.h"
48#include "lldb/Symbol/ObjectFile.h"
49#include "lldb/Symbol/SymbolFile.h"
50#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000051#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000052#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000054#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000055#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000057#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
61
Greg Clayton7260f622011-04-18 08:33:37 +000062
63
64static void
65DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
66{
Greg Clayton176761e2011-04-19 04:19:37 +000067 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000068
Greg Claytonaa149cb2011-08-11 02:48:45 +000069 Module *exe_module = target->GetExecutableModulePointer();
Greg Clayton7260f622011-04-18 08:33:37 +000070 char exe_path[PATH_MAX];
71 bool exe_valid = false;
Greg Claytonaa149cb2011-08-11 02:48:45 +000072 if (exe_module)
73 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000074
Greg Clayton7260f622011-04-18 08:33:37 +000075 if (!exe_valid)
76 ::strcpy (exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000077
Greg Clayton7260f622011-04-18 08:33:37 +000078 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000079
Greg Clayton7260f622011-04-18 08:33:37 +000080 uint32_t properties = 0;
81 if (target_arch.IsValid())
82 {
83 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
84 properties++;
85 }
86 PlatformSP platform_sp (target->GetPlatform());
87 if (platform_sp)
Greg Clayton57abc5d2013-05-10 21:47:16 +000088 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000089
Greg Clayton7260f622011-04-18 08:33:37 +000090 ProcessSP process_sp (target->GetProcessSP());
91 bool show_process_status = false;
92 if (process_sp)
93 {
94 lldb::pid_t pid = process_sp->GetID();
95 StateType state = process_sp->GetState();
96 if (show_stopped_process_status)
Greg Clayton2637f822011-11-17 01:23:07 +000097 show_process_status = StateIsStoppedState(state, true);
Greg Clayton7260f622011-04-18 08:33:37 +000098 const char *state_cstr = StateAsCString (state);
99 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000100 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Clayton7260f622011-04-18 08:33:37 +0000101 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
102 }
103 if (properties > 0)
104 strm.PutCString (" )\n");
105 else
106 strm.EOL();
107 if (show_process_status)
108 {
109 const bool only_threads_with_stop_reason = true;
110 const uint32_t start_frame = 0;
111 const uint32_t num_frames = 1;
112 const uint32_t num_frames_with_source = 1;
113 process_sp->GetStatus (strm);
114 process_sp->GetThreadStatus (strm,
115 only_threads_with_stop_reason,
116 start_frame,
117 num_frames,
118 num_frames_with_source);
119
120 }
121}
122
123static uint32_t
124DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
125{
126 const uint32_t num_targets = target_list.GetNumTargets();
127 if (num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000128 {
Greg Clayton7260f622011-04-18 08:33:37 +0000129 TargetSP selected_target_sp (target_list.GetSelectedTarget());
130 strm.PutCString ("Current targets:\n");
131 for (uint32_t i=0; i<num_targets; ++i)
132 {
133 TargetSP target_sp (target_list.GetTargetAtIndex (i));
134 if (target_sp)
135 {
136 bool is_selected = target_sp.get() == selected_target_sp.get();
137 DumpTargetInfo (i,
138 target_sp.get(),
139 is_selected ? "* " : " ",
140 show_stopped_process_status,
141 strm);
142 }
143 }
144 }
145 return num_targets;
146}
147#pragma mark CommandObjectTargetCreate
148
149//-------------------------------------------------------------------------
150// "target create"
151//-------------------------------------------------------------------------
152
Jim Ingham5a988412012-06-08 21:56:10 +0000153class CommandObjectTargetCreate : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000154{
155public:
156 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000157 CommandObjectParsed (interpreter,
158 "target create",
159 "Create a target using the argument as the main executable.",
160 NULL),
Greg Clayton7260f622011-04-18 08:33:37 +0000161 m_option_group (interpreter),
Greg Clayton644247c2011-07-07 01:59:51 +0000162 m_arch_option (),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000163 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 +0000164 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 +0000165 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 +0000166 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 +0000167 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 +0000168 {
169 CommandArgumentEntry arg;
170 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000171
Greg Clayton7260f622011-04-18 08:33:37 +0000172 // Define the first (and only) variant of this arg.
173 file_arg.arg_type = eArgTypeFilename;
174 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000175
Greg Clayton7260f622011-04-18 08:33:37 +0000176 // There is only one variant this argument could be; put it into the argument entry.
177 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000178
Greg Clayton7260f622011-04-18 08:33:37 +0000179 // Push the data for the first argument into the m_arguments vector.
180 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000181
Greg Clayton644247c2011-07-07 01:59:51 +0000182 m_option_group.Append (&m_arch_option, 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
285 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000286 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Clayton1c5f1862012-11-30 19:05:35 +0000287 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000288 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000289 file_path,
Greg Claytond26a1e52015-01-28 22:08:17 +0000290 arch_cstr,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000291 get_dependent_files,
Greg Claytond26a1e52015-01-28 22:08:17 +0000292 NULL,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000293 target_sp));
294
Greg Clayton7260f622011-04-18 08:33:37 +0000295 if (target_sp)
296 {
Greg Claytond26a1e52015-01-28 22:08:17 +0000297 // Only get the platform after we create the target because we might have
298 // switched platforms depending on what the arguments were to CreateTarget()
299 // we can't rely on the selected platform.
300
301 PlatformSP platform_sp = target_sp->GetPlatform();
302
303 if (remote_file)
304 {
305 if (platform_sp)
306 {
307 // I have a remote file.. two possible cases
308 if (file_spec && file_spec.Exists())
309 {
310 // if the remote file does not exist, push it there
311 if (!platform_sp->GetFileExists (remote_file))
312 {
313 Error err = platform_sp->PutFile(file_spec, remote_file);
314 if (err.Fail())
315 {
316 result.AppendError(err.AsCString());
317 result.SetStatus (eReturnStatusFailed);
318 return false;
319 }
320 }
321 }
322 else
323 {
324 // there is no local file and we need one
325 // in order to make the remote ---> local transfer we need a platform
326 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
327 if (!platform_sp)
328 {
329 result.AppendError("unable to perform remote debugging without a platform");
330 result.SetStatus (eReturnStatusFailed);
331 return false;
332 }
333 if (file_path)
334 {
335 // copy the remote file to the local file
336 Error err = platform_sp->GetFile(remote_file, file_spec);
337 if (err.Fail())
338 {
339 result.AppendError(err.AsCString());
340 result.SetStatus (eReturnStatusFailed);
341 return false;
342 }
343 }
344 else
345 {
346 // make up a local file
347 result.AppendError("remote --> local transfer without local path is not implemented yet");
348 result.SetStatus (eReturnStatusFailed);
349 return false;
350 }
351 }
352 }
353 else
354 {
355 result.AppendError("no platform found for target");
356 result.SetStatus (eReturnStatusFailed);
357 return false;
358 }
359 }
360
Jim Inghambf22b962013-07-11 01:47:46 +0000361 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000362 {
363 ModuleSP module_sp (target_sp->GetExecutableModule());
364 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000365 {
366 if (symfile)
367 module_sp->SetSymbolFileFileSpec(symfile);
368 if (remote_file)
369 {
370 std::string remote_path = remote_file.GetPath();
371 target_sp->SetArg0(remote_path.c_str());
372 module_sp->SetPlatformFileSpec(remote_file);
373 }
374 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000375 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000376
Greg Clayton7260f622011-04-18 08:33:37 +0000377 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000378 if (must_set_platform_path)
379 {
380 ModuleSpec main_module_spec(file_spec);
381 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
382 if (module_sp)
383 module_sp->SetPlatformFileSpec(remote_file);
384 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000385 if (core_file)
386 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000387 char core_path[PATH_MAX];
388 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000389 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000390 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000391 FileSpec core_file_dir;
392 core_file_dir.GetDirectory() = core_file.GetDirectory();
393 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000394
Greg Claytonc859e2d2012-02-13 23:10:39 +0000395 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
396
397 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000398 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000399 // Seems wierd that we Launch a core file, but that is
400 // what we do!
401 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000402
Greg Claytonc859e2d2012-02-13 23:10:39 +0000403 if (error.Fail())
404 {
405 result.AppendError(error.AsCString("can't find plug-in for core file"));
406 result.SetStatus (eReturnStatusFailed);
407 return false;
408 }
409 else
410 {
411 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
412 result.SetStatus (eReturnStatusSuccessFinishNoResult);
413 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000414 }
415 else
416 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000417 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
418 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000419 }
420 }
421 else
422 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000423 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000424 result.SetStatus (eReturnStatusFailed);
425 }
426 }
427 else
428 {
429 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
430 result.SetStatus (eReturnStatusSuccessFinishNoResult);
431 }
Greg Clayton7260f622011-04-18 08:33:37 +0000432 }
433 else
434 {
435 result.AppendError(error.AsCString());
436 result.SetStatus (eReturnStatusFailed);
437 }
438 }
439 else
440 {
Jason Molenda65e06422015-01-20 03:06:17 +0000441 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core option.\n", m_cmd_name.c_str());
Greg Clayton7260f622011-04-18 08:33:37 +0000442 result.SetStatus (eReturnStatusFailed);
443 }
444 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000445 }
446
Greg Clayton7260f622011-04-18 08:33:37 +0000447private:
448 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000449 OptionGroupArchitecture m_arch_option;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000450 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000451 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000452 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000453 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000454 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000455};
456
457#pragma mark CommandObjectTargetList
458
459//----------------------------------------------------------------------
460// "target list"
461//----------------------------------------------------------------------
462
Jim Ingham5a988412012-06-08 21:56:10 +0000463class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000464{
465public:
466 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000467 CommandObjectParsed (interpreter,
468 "target list",
469 "List all current targets in the current debug session.",
470 NULL,
471 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000472 {
473 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000474
Greg Clayton7260f622011-04-18 08:33:37 +0000475 virtual
476 ~CommandObjectTargetList ()
477 {
478 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000479
Jim Ingham5a988412012-06-08 21:56:10 +0000480protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000481 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000482 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000483 {
484 if (args.GetArgumentCount() == 0)
485 {
486 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000487
Greg Clayton7260f622011-04-18 08:33:37 +0000488 bool show_stopped_process_status = false;
489 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
490 {
491 strm.PutCString ("No targets.\n");
492 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000493 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000494 }
495 else
496 {
497 result.AppendError ("the 'target list' command takes no arguments\n");
498 result.SetStatus (eReturnStatusFailed);
499 }
500 return result.Succeeded();
501 }
502};
503
504
505#pragma mark CommandObjectTargetSelect
506
507//----------------------------------------------------------------------
508// "target select"
509//----------------------------------------------------------------------
510
Jim Ingham5a988412012-06-08 21:56:10 +0000511class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000512{
513public:
514 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000515 CommandObjectParsed (interpreter,
516 "target select",
517 "Select a target as the current target by target index.",
518 NULL,
519 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000520 {
521 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000522
Greg Clayton7260f622011-04-18 08:33:37 +0000523 virtual
524 ~CommandObjectTargetSelect ()
525 {
526 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000527
Jim Ingham5a988412012-06-08 21:56:10 +0000528protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000529 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000530 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000531 {
532 if (args.GetArgumentCount() == 1)
533 {
534 bool success = false;
535 const char *target_idx_arg = args.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000536 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton7260f622011-04-18 08:33:37 +0000537 if (success)
538 {
539 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
540 const uint32_t num_targets = target_list.GetNumTargets();
541 if (target_idx < num_targets)
542 {
543 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
544 if (target_sp)
545 {
546 Stream &strm = result.GetOutputStream();
547 target_list.SetSelectedTarget (target_sp.get());
548 bool show_stopped_process_status = false;
549 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000550 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000551 }
552 else
553 {
554 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
555 result.SetStatus (eReturnStatusFailed);
556 }
557 }
558 else
559 {
Todd Fiala352237d2014-08-29 20:14:21 +0000560 if (num_targets > 0)
561 {
562 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
563 target_idx,
564 num_targets - 1);
565 } else
566 {
567 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
568 target_idx);
569 }
Greg Clayton7260f622011-04-18 08:33:37 +0000570 result.SetStatus (eReturnStatusFailed);
571 }
572 }
573 else
574 {
575 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
576 result.SetStatus (eReturnStatusFailed);
577 }
578 }
579 else
580 {
581 result.AppendError ("'target select' takes a single argument: a target index\n");
582 result.SetStatus (eReturnStatusFailed);
583 }
584 return result.Succeeded();
585 }
586};
587
Greg Clayton3418c852011-08-10 02:10:13 +0000588#pragma mark CommandObjectTargetSelect
589
590//----------------------------------------------------------------------
591// "target delete"
592//----------------------------------------------------------------------
593
Jim Ingham5a988412012-06-08 21:56:10 +0000594class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000595{
596public:
597 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000598 CommandObjectParsed (interpreter,
599 "target delete",
600 "Delete one or more targets by target index.",
601 NULL,
602 0),
Greg Claytonaa149cb2011-08-11 02:48:45 +0000603 m_option_group (interpreter),
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000604 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 +0000605 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000606 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
607 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000608 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000609
Greg Clayton3418c852011-08-10 02:10:13 +0000610 virtual
611 ~CommandObjectTargetDelete ()
612 {
613 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000614
Jim Ingham5a988412012-06-08 21:56:10 +0000615 Options *
616 GetOptions ()
617 {
618 return &m_option_group;
619 }
620
621protected:
Greg Clayton3418c852011-08-10 02:10:13 +0000622 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000623 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton3418c852011-08-10 02:10:13 +0000624 {
625 const size_t argc = args.GetArgumentCount();
626 std::vector<TargetSP> delete_target_list;
627 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
628 bool success = true;
629 TargetSP target_sp;
630 if (argc > 0)
631 {
632 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000633 // Bail out if don't have any targets.
634 if (num_targets == 0) {
635 result.AppendError("no targets to delete");
636 result.SetStatus(eReturnStatusFailed);
637 success = false;
638 }
639
Greg Clayton3418c852011-08-10 02:10:13 +0000640 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
641 {
642 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
Vince Harron5275aaa2015-01-15 20:08:35 +0000643 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton3418c852011-08-10 02:10:13 +0000644 if (success)
645 {
646 if (target_idx < num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000647 {
Greg Clayton3418c852011-08-10 02:10:13 +0000648 target_sp = target_list.GetTargetAtIndex (target_idx);
649 if (target_sp)
650 {
651 delete_target_list.push_back (target_sp);
652 continue;
653 }
654 }
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000655 if (num_targets > 1)
656 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
657 target_idx,
658 num_targets - 1);
659 else
660 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
661 target_idx);
662
Greg Clayton3418c852011-08-10 02:10:13 +0000663 result.SetStatus (eReturnStatusFailed);
664 success = false;
665 }
666 else
667 {
668 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
669 result.SetStatus (eReturnStatusFailed);
670 success = false;
671 }
672 }
Greg Clayton3418c852011-08-10 02:10:13 +0000673 }
674 else
675 {
676 target_sp = target_list.GetSelectedTarget();
677 if (target_sp)
678 {
679 delete_target_list.push_back (target_sp);
680 }
681 else
682 {
683 result.AppendErrorWithFormat("no target is currently selected\n");
684 result.SetStatus (eReturnStatusFailed);
685 success = false;
686 }
687 }
688 if (success)
689 {
690 const size_t num_targets_to_delete = delete_target_list.size();
691 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
692 {
693 target_sp = delete_target_list[idx];
694 target_list.DeleteTarget(target_sp);
695 target_sp->Destroy();
696 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000697 // If "--clean" was specified, prune any orphaned shared modules from
698 // the global shared module list
699 if (m_cleanup_option.GetOptionValue ())
700 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000701 const bool mandatory = true;
702 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000703 }
Greg Clayton3418c852011-08-10 02:10:13 +0000704 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
705 result.SetStatus(eReturnStatusSuccessFinishResult);
706 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000707
Greg Clayton3418c852011-08-10 02:10:13 +0000708 return result.Succeeded();
709 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000710
Greg Claytonaa149cb2011-08-11 02:48:45 +0000711 OptionGroupOptions m_option_group;
712 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000713};
714
Greg Clayton7260f622011-04-18 08:33:37 +0000715
Greg Clayton644247c2011-07-07 01:59:51 +0000716#pragma mark CommandObjectTargetVariable
717
718//----------------------------------------------------------------------
719// "target variable"
720//----------------------------------------------------------------------
721
Jim Ingham5a988412012-06-08 21:56:10 +0000722class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000723{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000724 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
725 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
726
Greg Clayton644247c2011-07-07 01:59:51 +0000727public:
728 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000729 CommandObjectParsed (interpreter,
730 "target variable",
Greg Claytondfdd1eb2012-11-03 00:10:22 +0000731 "Read global variable(s) prior to, or while running your binary.",
Jim Ingham5a988412012-06-08 21:56:10 +0000732 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000733 eFlagRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000734 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000735 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000736 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000737 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
738 SHORT_OPTION_FILE, 0, eArgTypeFilename,
739 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
740 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
741 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
742 "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 +0000743 m_varobj_options()
744 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000745 CommandArgumentEntry arg;
746 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000747
Johnny Chen81ab3f52011-08-22 22:22:00 +0000748 // Define the first (and only) variant of this arg.
749 var_name_arg.arg_type = eArgTypeVarName;
750 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000751
Johnny Chen81ab3f52011-08-22 22:22:00 +0000752 // There is only one variant this argument could be; put it into the argument entry.
753 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000754
Johnny Chen81ab3f52011-08-22 22:22:00 +0000755 // Push the data for the first argument into the m_arguments vector.
756 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000757
Greg Clayton644247c2011-07-07 01:59:51 +0000758 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000759 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000760 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 +0000761 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
762 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
763 m_option_group.Finalize();
764 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000765
Greg Clayton644247c2011-07-07 01:59:51 +0000766 virtual
767 ~CommandObjectTargetVariable ()
768 {
769 }
Greg Clayton884fb692011-07-08 21:46:14 +0000770
771 void
772 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
773 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000774 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000775
Enrico Granata560558e2015-02-11 02:35:39 +0000776 if (false == valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
777 true == valobj_sp->IsRuntimeSupportValue())
778 return;
779
Greg Clayton884fb692011-07-08 21:46:14 +0000780 switch (var_sp->GetScope())
781 {
782 case eValueTypeVariableGlobal:
783 if (m_option_variable.show_scope)
784 s.PutCString("GLOBAL: ");
785 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000786
Greg Clayton884fb692011-07-08 21:46:14 +0000787 case eValueTypeVariableStatic:
788 if (m_option_variable.show_scope)
789 s.PutCString("STATIC: ");
790 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000791
Greg Clayton884fb692011-07-08 21:46:14 +0000792 case eValueTypeVariableArgument:
793 if (m_option_variable.show_scope)
794 s.PutCString(" ARG: ");
795 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000796
Greg Clayton884fb692011-07-08 21:46:14 +0000797 case eValueTypeVariableLocal:
798 if (m_option_variable.show_scope)
799 s.PutCString(" LOCAL: ");
800 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000801
Greg Clayton884fb692011-07-08 21:46:14 +0000802 default:
803 break;
804 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000805
Greg Clayton45ba8542011-07-10 19:21:23 +0000806 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000807 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000808 bool show_fullpaths = false;
809 bool show_module = true;
810 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
811 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000812 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000813
Greg Clayton1deb7962011-10-25 06:44:01 +0000814 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000815 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000816 options.SetFormat(format);
817
818 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000819
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000820 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000821 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000822
Greg Claytonc7bece562013-01-25 18:06:21 +0000823 static size_t GetVariableCallback (void *baton,
824 const char *name,
825 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000826 {
827 Target *target = static_cast<Target *>(baton);
828 if (target)
829 {
830 return target->GetImages().FindGlobalVariables (ConstString(name),
831 true,
832 UINT32_MAX,
833 variable_list);
834 }
835 return 0;
836 }
Greg Clayton884fb692011-07-08 21:46:14 +0000837
Jim Ingham5a988412012-06-08 21:56:10 +0000838 Options *
839 GetOptions ()
840 {
841 return &m_option_group;
842 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000843
Jim Ingham5a988412012-06-08 21:56:10 +0000844protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000845 void
846 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
847 {
848 size_t count = variable_list.GetSize();
849 if (count > 0)
850 {
851 if (sc.module_sp)
852 {
853 if (sc.comp_unit)
854 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000855 s.Printf ("Global variables for %s in %s:\n",
856 sc.comp_unit->GetPath().c_str(),
857 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000858 }
859 else
860 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000861 s.Printf ("Global variables for %s\n",
862 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000863 }
864 }
865 else if (sc.comp_unit)
866 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000867 s.Printf ("Global variables for %s\n",
868 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000869 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000870
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000871 for (uint32_t i=0; i<count; ++i)
872 {
873 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
874 if (var_sp)
875 {
876 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000877
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000878 if (valobj_sp)
879 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
880 }
881 }
882 }
883
884 }
Greg Clayton644247c2011-07-07 01:59:51 +0000885 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000886 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton644247c2011-07-07 01:59:51 +0000887 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000888 Target *target = m_exe_ctx.GetTargetPtr();
889 const size_t argc = args.GetArgumentCount();
890 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000891
Greg Claytonf9fc6092013-01-09 19:44:40 +0000892 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000893 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000894
895 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000896 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000897 VariableList variable_list;
898 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000899
Greg Claytonf9fc6092013-01-09 19:44:40 +0000900 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000901 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000902 bool use_var_name = false;
903 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000904 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000905 RegularExpression regex(arg);
906 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000907 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000908 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000909 result.SetStatus (eReturnStatusFailed);
910 return false;
911 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000912 use_var_name = true;
913 matches = target->GetImages().FindGlobalVariables (regex,
914 true,
915 UINT32_MAX,
916 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000917 }
918 else
919 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000920 Error error (Variable::GetValuesForVariableExpressionPath (arg,
921 m_exe_ctx.GetBestExecutionContextScope(),
922 GetVariableCallback,
923 target,
924 variable_list,
925 valobj_list));
926 matches = variable_list.GetSize();
927 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000928
Greg Claytonf9fc6092013-01-09 19:44:40 +0000929 if (matches == 0)
930 {
931 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
932 result.SetStatus (eReturnStatusFailed);
933 return false;
934 }
935 else
936 {
937 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000938 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000939 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
940 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000941 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000942 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
943 if (!valobj_sp)
944 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000945
Greg Claytonf9fc6092013-01-09 19:44:40 +0000946 if (valobj_sp)
947 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000948 }
949 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000950 }
Greg Clayton644247c2011-07-07 01:59:51 +0000951 }
952 }
953 else
954 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000955 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
956 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
957 SymbolContextList sc_list;
958 const size_t num_compile_units = compile_units.GetSize();
959 const size_t num_shlibs = shlibs.GetSize();
960 if (num_compile_units == 0 && num_shlibs == 0)
961 {
962 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000963 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000964 CompileUnit *comp_unit = NULL;
965 if (frame)
966 {
967 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
968 if (sc.comp_unit)
969 {
970 const bool can_create = true;
971 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
972 if (comp_unit_varlist_sp)
973 {
974 size_t count = comp_unit_varlist_sp->GetSize();
975 if (count > 0)
976 {
977 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
978 success = true;
979 }
980 }
981 }
982 }
983 if (!success)
984 {
985 if (frame)
986 {
987 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000988 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
989 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000990 else
991 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000992 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000993 else
994 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
995 result.SetStatus (eReturnStatusFailed);
996 }
997 }
998 else
999 {
1000 SymbolContextList sc_list;
1001 const bool append = true;
1002 // We have one or more compile unit or shlib
1003 if (num_shlibs > 0)
1004 {
1005 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
1006 {
1007 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
1008 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001009
Greg Claytonf9fc6092013-01-09 19:44:40 +00001010 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
1011 if (module_sp)
1012 {
1013 if (num_compile_units > 0)
1014 {
1015 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1016 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1017 }
1018 else
1019 {
1020 SymbolContext sc;
1021 sc.module_sp = module_sp;
1022 sc_list.Append(sc);
1023 }
1024 }
1025 else
1026 {
1027 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001028 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1029 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001030 }
1031 }
1032 }
1033 else
1034 {
1035 // No shared libraries, we just want to find globals for the compile units files that were specified
1036 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1037 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1038 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001039
Greg Claytonf9fc6092013-01-09 19:44:40 +00001040 const uint32_t num_scs = sc_list.GetSize();
1041 if (num_scs > 0)
1042 {
1043 SymbolContext sc;
1044 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1045 {
1046 if (sc_list.GetContextAtIndex(sc_idx, sc))
1047 {
1048 if (sc.comp_unit)
1049 {
1050 const bool can_create = true;
1051 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1052 if (comp_unit_varlist_sp)
1053 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1054 }
1055 else if (sc.module_sp)
1056 {
1057 // Get all global variables for this module
1058 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1059 VariableList variable_list;
1060 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1061 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1062 }
1063 }
1064 }
1065 }
1066 }
Greg Clayton644247c2011-07-07 01:59:51 +00001067 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001068
Enrico Granata61a80ba2011-08-12 16:42:31 +00001069 if (m_interpreter.TruncationWarningNecessary())
1070 {
1071 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1072 m_cmd_name.c_str());
1073 m_interpreter.TruncationWarningGiven();
1074 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001075
Greg Clayton644247c2011-07-07 01:59:51 +00001076 return result.Succeeded();
1077 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001078
Greg Clayton644247c2011-07-07 01:59:51 +00001079 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001080 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001081 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001082 OptionGroupFileList m_option_compile_units;
1083 OptionGroupFileList m_option_shared_libraries;
1084 OptionGroupValueObjectDisplay m_varobj_options;
1085
1086};
1087
1088
Greg Claytoneffe5c92011-05-03 22:09:39 +00001089#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090
Jim Ingham5a988412012-06-08 21:56:10 +00001091class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092{
1093public:
1094
Greg Claytoneffe5c92011-05-03 22:09:39 +00001095 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001096 CommandObjectParsed (interpreter,
1097 "target modules search-paths add",
1098 "Add new image search paths substitution pairs to the current target.",
1099 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 {
Caroline Tice405fe672010-10-04 22:28:36 +00001101 CommandArgumentEntry arg;
1102 CommandArgumentData old_prefix_arg;
1103 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001104
Caroline Tice405fe672010-10-04 22:28:36 +00001105 // Define the first variant of this arg pair.
1106 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1107 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001108
Caroline Tice405fe672010-10-04 22:28:36 +00001109 // Define the first variant of this arg pair.
1110 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1111 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001112
Caroline Tice405fe672010-10-04 22:28:36 +00001113 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1114 // must always occur together, they are treated as two variants of one argument rather than two independent
1115 // arguments. Push them both into the first argument position for m_arguments...
1116
1117 arg.push_back (old_prefix_arg);
1118 arg.push_back (new_prefix_arg);
1119
1120 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 }
1122
Greg Claytoneffe5c92011-05-03 22:09:39 +00001123 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124 {
1125 }
1126
Jim Ingham5a988412012-06-08 21:56:10 +00001127protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001129 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130 CommandReturnObject &result)
1131 {
Greg Claytona7015092010-09-18 01:14:36 +00001132 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 if (target)
1134 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001135 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 if (argc & 1)
1137 {
Greg Clayton7260f622011-04-18 08:33:37 +00001138 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 result.SetStatus (eReturnStatusFailed);
1140 }
1141 else
1142 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001143 for (size_t i=0; i<argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 {
1145 const char *from = command.GetArgumentAtIndex(i);
1146 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001147
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 if (from[0] && to[0])
1149 {
1150 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001151 target->GetImageSearchPathList().Append (ConstString(from),
1152 ConstString(to),
1153 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001154 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 }
1156 else
1157 {
1158 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001159 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 else
Greg Clayton7260f622011-04-18 08:33:37 +00001161 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 result.SetStatus (eReturnStatusFailed);
1163 }
1164 }
1165 }
1166 }
1167 else
1168 {
Greg Clayton7260f622011-04-18 08:33:37 +00001169 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 result.SetStatus (eReturnStatusFailed);
1171 }
1172 return result.Succeeded();
1173 }
1174};
1175
Greg Claytoneffe5c92011-05-03 22:09:39 +00001176#pragma mark CommandObjectTargetModulesSearchPathsClear
1177
Jim Ingham5a988412012-06-08 21:56:10 +00001178class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179{
1180public:
1181
Greg Claytoneffe5c92011-05-03 22:09:39 +00001182 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001183 CommandObjectParsed (interpreter,
1184 "target modules search-paths clear",
1185 "Clear all current image search path substitution pairs from the current target.",
1186 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 {
1188 }
1189
Greg Claytoneffe5c92011-05-03 22:09:39 +00001190 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 {
1192 }
1193
Jim Ingham5a988412012-06-08 21:56:10 +00001194protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001196 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 CommandReturnObject &result)
1198 {
Greg Claytona7015092010-09-18 01:14:36 +00001199 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 if (target)
1201 {
1202 bool notify = true;
1203 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001204 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 }
1206 else
1207 {
Greg Clayton7260f622011-04-18 08:33:37 +00001208 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 result.SetStatus (eReturnStatusFailed);
1210 }
1211 return result.Succeeded();
1212 }
1213};
1214
Greg Claytoneffe5c92011-05-03 22:09:39 +00001215#pragma mark CommandObjectTargetModulesSearchPathsInsert
1216
Jim Ingham5a988412012-06-08 21:56:10 +00001217class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218{
1219public:
1220
Greg Claytoneffe5c92011-05-03 22:09:39 +00001221 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001222 CommandObjectParsed (interpreter,
1223 "target modules search-paths insert",
1224 "Insert a new image search path substitution pair into the current target at the specified index.",
1225 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 {
Caroline Tice405fe672010-10-04 22:28:36 +00001227 CommandArgumentEntry arg1;
1228 CommandArgumentEntry arg2;
1229 CommandArgumentData index_arg;
1230 CommandArgumentData old_prefix_arg;
1231 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001232
Caroline Tice405fe672010-10-04 22:28:36 +00001233 // Define the first and only variant of this arg.
1234 index_arg.arg_type = eArgTypeIndex;
1235 index_arg.arg_repetition = eArgRepeatPlain;
1236
1237 // Put the one and only variant into the first arg for m_arguments:
1238 arg1.push_back (index_arg);
1239
1240 // Define the first variant of this arg pair.
1241 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1242 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001243
Caroline Tice405fe672010-10-04 22:28:36 +00001244 // Define the first variant of this arg pair.
1245 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1246 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001247
Caroline Tice405fe672010-10-04 22:28:36 +00001248 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1249 // must always occur together, they are treated as two variants of one argument rather than two independent
1250 // arguments. Push them both into the same argument position for m_arguments...
1251
1252 arg2.push_back (old_prefix_arg);
1253 arg2.push_back (new_prefix_arg);
1254
1255 // Add arguments to m_arguments.
1256 m_arguments.push_back (arg1);
1257 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
1259
Greg Claytoneffe5c92011-05-03 22:09:39 +00001260 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261 {
1262 }
1263
Jim Ingham5a988412012-06-08 21:56:10 +00001264protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001266 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267 CommandReturnObject &result)
1268 {
Greg Claytona7015092010-09-18 01:14:36 +00001269 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 if (target)
1271 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001272 size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 // check for at least 3 arguments and an odd nubmer of parameters
1274 if (argc >= 3 && argc & 1)
1275 {
1276 bool success = false;
1277
Vince Harron5275aaa2015-01-15 20:08:35 +00001278 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279
1280 if (!success)
1281 {
1282 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1283 result.SetStatus (eReturnStatusFailed);
1284 return result.Succeeded();
1285 }
1286
1287 // shift off the index
1288 command.Shift();
1289 argc = command.GetArgumentCount();
1290
1291 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1292 {
1293 const char *from = command.GetArgumentAtIndex(i);
1294 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001295
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 if (from[0] && to[0])
1297 {
1298 bool last_pair = ((argc - i) == 2);
1299 target->GetImageSearchPathList().Insert (ConstString(from),
1300 ConstString(to),
1301 insert_idx,
1302 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001303 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 }
1305 else
1306 {
1307 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001308 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001309 else
Greg Clayton7260f622011-04-18 08:33:37 +00001310 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 result.SetStatus (eReturnStatusFailed);
1312 return false;
1313 }
1314 }
1315 }
1316 else
1317 {
Greg Clayton7260f622011-04-18 08:33:37 +00001318 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319 result.SetStatus (eReturnStatusFailed);
1320 return result.Succeeded();
1321 }
1322
1323 }
1324 else
1325 {
Greg Clayton7260f622011-04-18 08:33:37 +00001326 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327 result.SetStatus (eReturnStatusFailed);
1328 }
1329 return result.Succeeded();
1330 }
1331};
1332
Greg Claytoneffe5c92011-05-03 22:09:39 +00001333
1334#pragma mark CommandObjectTargetModulesSearchPathsList
1335
1336
Jim Ingham5a988412012-06-08 21:56:10 +00001337class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338{
1339public:
1340
Greg Claytoneffe5c92011-05-03 22:09:39 +00001341 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001342 CommandObjectParsed (interpreter,
1343 "target modules search-paths list",
1344 "List all current image search path substitution pairs in the current target.",
1345 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 {
1347 }
1348
Greg Claytoneffe5c92011-05-03 22:09:39 +00001349 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350 {
1351 }
1352
Jim Ingham5a988412012-06-08 21:56:10 +00001353protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001355 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 CommandReturnObject &result)
1357 {
Greg Claytona7015092010-09-18 01:14:36 +00001358 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 if (target)
1360 {
1361 if (command.GetArgumentCount() != 0)
1362 {
Greg Clayton7260f622011-04-18 08:33:37 +00001363 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364 result.SetStatus (eReturnStatusFailed);
1365 return result.Succeeded();
1366 }
1367
1368 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001369 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 }
1371 else
1372 {
Greg Clayton7260f622011-04-18 08:33:37 +00001373 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 result.SetStatus (eReturnStatusFailed);
1375 }
1376 return result.Succeeded();
1377 }
1378};
1379
Greg Claytoneffe5c92011-05-03 22:09:39 +00001380#pragma mark CommandObjectTargetModulesSearchPathsQuery
1381
Jim Ingham5a988412012-06-08 21:56:10 +00001382class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383{
1384public:
1385
Greg Claytoneffe5c92011-05-03 22:09:39 +00001386 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001387 CommandObjectParsed (interpreter,
1388 "target modules search-paths query",
1389 "Transform a path using the first applicable image search path.",
1390 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 {
Caroline Tice405fe672010-10-04 22:28:36 +00001392 CommandArgumentEntry arg;
1393 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001394
Caroline Tice405fe672010-10-04 22:28:36 +00001395 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001396 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001397 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001398
Caroline Tice405fe672010-10-04 22:28:36 +00001399 // There is only one variant this argument could be; put it into the argument entry.
1400 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001401
Caroline Tice405fe672010-10-04 22:28:36 +00001402 // Push the data for the first argument into the m_arguments vector.
1403 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 }
1405
Greg Claytoneffe5c92011-05-03 22:09:39 +00001406 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 {
1408 }
1409
Jim Ingham5a988412012-06-08 21:56:10 +00001410protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001412 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 CommandReturnObject &result)
1414 {
Greg Claytona7015092010-09-18 01:14:36 +00001415 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 if (target)
1417 {
1418 if (command.GetArgumentCount() != 1)
1419 {
Greg Clayton7260f622011-04-18 08:33:37 +00001420 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421 result.SetStatus (eReturnStatusFailed);
1422 return result.Succeeded();
1423 }
1424
1425 ConstString orig(command.GetArgumentAtIndex(0));
1426 ConstString transformed;
1427 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1428 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1429 else
1430 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001431
1432 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 }
1434 else
1435 {
Greg Clayton7260f622011-04-18 08:33:37 +00001436 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 result.SetStatus (eReturnStatusFailed);
1438 }
1439 return result.Succeeded();
1440 }
1441};
1442
Greg Claytoneffe5c92011-05-03 22:09:39 +00001443//----------------------------------------------------------------------
1444// Static Helper functions
1445//----------------------------------------------------------------------
1446static void
1447DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1448{
1449 if (module)
1450 {
1451 const char *arch_cstr;
1452 if (full_triple)
1453 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1454 else
1455 arch_cstr = module->GetArchitecture().GetArchitectureName();
1456 if (width)
1457 strm.Printf("%-*s", width, arch_cstr);
1458 else
1459 strm.PutCString(arch_cstr);
1460 }
1461}
1462
1463static void
1464DumpModuleUUID (Stream &strm, Module *module)
1465{
Jim Ingham28eb5712012-10-12 17:34:26 +00001466 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001467 module->GetUUID().Dump (&strm);
1468 else
1469 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001470}
1471
1472static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001473DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1474 Stream &strm,
1475 Module *module,
1476 const FileSpec &file_spec,
1477 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001478{
1479 uint32_t num_matches = 0;
1480 if (module)
1481 {
1482 SymbolContextList sc_list;
1483 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1484 0,
1485 false,
1486 eSymbolContextCompUnit,
1487 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001488
Greg Claytoneffe5c92011-05-03 22:09:39 +00001489 for (uint32_t i=0; i<num_matches; ++i)
1490 {
1491 SymbolContext sc;
1492 if (sc_list.GetContextAtIndex(i, sc))
1493 {
1494 if (i > 0)
1495 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001496
Greg Claytoneffe5c92011-05-03 22:09:39 +00001497 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1498 << module->GetFileSpec().GetFilename() << "\n";
1499 LineTable *line_table = sc.comp_unit->GetLineTable();
1500 if (line_table)
1501 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001502 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001503 lldb::eDescriptionLevelBrief);
1504 else
1505 strm << "No line table";
1506 }
1507 }
1508 }
1509 return num_matches;
1510}
1511
1512static void
1513DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1514{
1515 if (file_spec_ptr)
1516 {
1517 if (width > 0)
1518 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001519 std::string fullpath = file_spec_ptr->GetPath();
1520 strm.Printf("%-*s", width, fullpath.c_str());
1521 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001522 }
1523 else
1524 {
1525 file_spec_ptr->Dump(&strm);
1526 return;
1527 }
1528 }
1529 // Keep the width spacing correct if things go wrong...
1530 if (width > 0)
1531 strm.Printf("%-*s", width, "");
1532}
1533
1534static void
1535DumpDirectory (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->GetDirectory().AsCString(""));
1541 else
1542 file_spec_ptr->GetDirectory().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
1550static void
1551DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1552{
1553 if (file_spec_ptr)
1554 {
1555 if (width > 0)
1556 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1557 else
1558 file_spec_ptr->GetFilename().Dump(&strm);
1559 return;
1560 }
1561 // Keep the width spacing correct if things go wrong...
1562 if (width > 0)
1563 strm.Printf("%-*s", width, "");
1564}
1565
1566
1567static void
1568DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1569{
1570 if (module)
1571 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001572 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1573 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001574 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001575 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001576 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001577 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001578 }
1579 }
1580}
1581
1582static void
1583DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1584{
1585 if (module)
1586 {
Greg Clayton3046e662013-07-10 01:23:25 +00001587 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001588 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001589 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001590 strm.Printf ("Sections for '%s' (%s):\n",
1591 module->GetSpecificationDescription().c_str(),
1592 module->GetArchitecture().GetArchitectureName());
1593 strm.IndentMore();
1594 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1595 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001596 }
1597 }
1598}
1599
1600static bool
1601DumpModuleSymbolVendor (Stream &strm, Module *module)
1602{
1603 if (module)
1604 {
1605 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1606 if (symbol_vendor)
1607 {
1608 symbol_vendor->Dump(&strm);
1609 return true;
1610 }
1611 }
1612 return false;
1613}
1614
Greg Claytonc4a8a762012-05-15 18:43:44 +00001615static void
1616DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1617{
1618 strm.IndentMore();
1619 strm.Indent (" Address: ");
1620 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1621 strm.PutCString (" (");
1622 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1623 strm.PutCString (")\n");
1624 strm.Indent (" Summary: ");
1625 const uint32_t save_indent = strm.GetIndentLevel ();
1626 strm.SetIndentLevel (save_indent + 13);
1627 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1628 strm.SetIndentLevel (save_indent);
1629 // Print out detailed address information when verbose is enabled
1630 if (verbose)
1631 {
1632 strm.EOL();
1633 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1634 }
1635 strm.IndentLess();
1636}
1637
Greg Claytoneffe5c92011-05-03 22:09:39 +00001638static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001639LookupAddressInModule (CommandInterpreter &interpreter,
1640 Stream &strm,
1641 Module *module,
1642 uint32_t resolve_mask,
1643 lldb::addr_t raw_addr,
1644 lldb::addr_t offset,
1645 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001646{
1647 if (module)
1648 {
1649 lldb::addr_t addr = raw_addr - offset;
1650 Address so_addr;
1651 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001652 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001653 if (target && !target->GetSectionLoadList().IsEmpty())
1654 {
1655 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1656 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001657 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001658 return false;
1659 }
1660 else
1661 {
1662 if (!module->ResolveFileAddress (addr, so_addr))
1663 return false;
1664 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001665
Greg Claytoneffe5c92011-05-03 22:09:39 +00001666 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001667 DumpAddress (exe_scope, so_addr, verbose, strm);
1668// strm.IndentMore();
1669// strm.Indent (" Address: ");
1670// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1671// strm.PutCString (" (");
1672// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1673// strm.PutCString (")\n");
1674// strm.Indent (" Summary: ");
1675// const uint32_t save_indent = strm.GetIndentLevel ();
1676// strm.SetIndentLevel (save_indent + 13);
1677// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1678// strm.SetIndentLevel (save_indent);
1679// // Print out detailed address information when verbose is enabled
1680// if (verbose)
1681// {
1682// strm.EOL();
1683// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1684// }
1685// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001686 return true;
1687 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001688
Greg Claytoneffe5c92011-05-03 22:09:39 +00001689 return false;
1690}
1691
1692static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001693LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001694{
1695 if (module)
1696 {
1697 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001698
Michael Sartaina7499c92013-07-01 19:45:50 +00001699 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1700 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001701 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001702 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001703 if (symtab)
1704 {
1705 uint32_t i;
1706 std::vector<uint32_t> match_indexes;
1707 ConstString symbol_name (name);
1708 uint32_t num_matches = 0;
1709 if (name_is_regex)
1710 {
1711 RegularExpression name_regexp(name);
1712 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1713 eSymbolTypeAny,
1714 match_indexes);
1715 }
1716 else
1717 {
1718 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1719 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001720
Greg Claytoneffe5c92011-05-03 22:09:39 +00001721 if (num_matches > 0)
1722 {
1723 strm.Indent ();
1724 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1725 name_is_regex ? "the regular expression " : "", name);
1726 DumpFullpath (strm, &module->GetFileSpec(), 0);
1727 strm.PutCString(":\n");
1728 strm.IndentMore ();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001729 //Symtab::DumpSymbolHeader (&strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001730 for (i=0; i < num_matches; ++i)
1731 {
1732 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Claytonc4a8a762012-05-15 18:43:44 +00001733 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1734 symbol->GetAddress(),
1735 verbose,
1736 strm);
1737
1738// strm.Indent ();
1739// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001740 }
1741 strm.IndentLess ();
1742 return num_matches;
1743 }
1744 }
1745 }
1746 }
1747 return 0;
1748}
1749
1750
1751static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001752DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001753{
1754 strm.IndentMore ();
1755 uint32_t i;
1756 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001757
Greg Claytoneffe5c92011-05-03 22:09:39 +00001758 for (i=0; i<num_matches; ++i)
1759 {
1760 SymbolContext sc;
1761 if (sc_list.GetContextAtIndex(i, sc))
1762 {
Sean Callananf6172c22012-02-11 00:24:04 +00001763 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001764
Sean Callananf6172c22012-02-11 00:24:04 +00001765 sc.GetAddressRange(eSymbolContextEverything,
1766 0,
1767 true,
1768 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001769
Greg Claytonc4a8a762012-05-15 18:43:44 +00001770 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001771 }
1772 }
1773 strm.IndentLess ();
1774}
1775
Greg Claytonc7bece562013-01-25 18:06:21 +00001776static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001777LookupFunctionInModule (CommandInterpreter &interpreter,
1778 Stream &strm,
1779 Module *module,
1780 const char *name,
1781 bool name_is_regex,
1782 bool include_inlines,
1783 bool include_symbols,
1784 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001785{
1786 if (module && name && name[0])
1787 {
1788 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001789 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001790 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001791 if (name_is_regex)
1792 {
1793 RegularExpression function_name_regex (name);
1794 num_matches = module->FindFunctions (function_name_regex,
1795 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001796 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001797 append,
1798 sc_list);
1799 }
1800 else
1801 {
1802 ConstString function_name (name);
Sean Callananb6d70eb2011-10-12 02:08:07 +00001803 num_matches = module->FindFunctions (function_name,
1804 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001805 eFunctionNameTypeAuto,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001806 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001807 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001808 append,
1809 sc_list);
1810 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001811
Greg Claytoneffe5c92011-05-03 22:09:39 +00001812 if (num_matches)
1813 {
1814 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001815 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001816 DumpFullpath (strm, &module->GetFileSpec(), 0);
1817 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001818 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001819 }
1820 return num_matches;
1821 }
1822 return 0;
1823}
1824
Greg Claytonc7bece562013-01-25 18:06:21 +00001825static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001826LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001827 Stream &strm,
1828 Module *module,
1829 const char *name_cstr,
1830 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001831{
1832 if (module && name_cstr && name_cstr[0])
1833 {
Greg Claytond1767f02011-12-08 02:13:16 +00001834 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001835 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001836 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001837 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001838 SymbolContext sc;
1839
1840 ConstString name(name_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001841 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001842
Greg Claytond1767f02011-12-08 02:13:16 +00001843 if (num_matches)
1844 {
1845 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001846 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001847 DumpFullpath (strm, &module->GetFileSpec(), 0);
1848 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001849 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001850 {
Greg Claytond1767f02011-12-08 02:13:16 +00001851 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001852 {
Greg Claytond1767f02011-12-08 02:13:16 +00001853 // Resolve the clang type so that any forward references
1854 // to types that haven't yet been parsed will get parsed.
1855 type_sp->GetClangFullType ();
1856 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001857 // Print all typedef chains
1858 TypeSP typedef_type_sp (type_sp);
1859 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1860 while (typedefed_type_sp)
1861 {
1862 strm.EOL();
1863 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1864 typedefed_type_sp->GetClangFullType ();
1865 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1866 typedef_type_sp = typedefed_type_sp;
1867 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1868 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001869 }
Greg Claytond1767f02011-12-08 02:13:16 +00001870 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001871 }
Greg Claytond1767f02011-12-08 02:13:16 +00001872 }
1873 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001874 }
1875 return 0;
1876}
1877
Greg Claytonc7bece562013-01-25 18:06:21 +00001878static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001879LookupTypeHere (CommandInterpreter &interpreter,
1880 Stream &strm,
1881 const SymbolContext &sym_ctx,
1882 const char *name_cstr,
1883 bool name_is_regex)
1884{
1885 if (!sym_ctx.module_sp)
1886 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001887
Sean Callanand38b4a92012-06-06 20:49:55 +00001888 TypeList type_list;
1889 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001890 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001891 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001892
Sean Callanand38b4a92012-06-06 20:49:55 +00001893 ConstString name(name_cstr);
1894 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 +00001895
Sean Callanand38b4a92012-06-06 20:49:55 +00001896 if (num_matches)
1897 {
1898 strm.Indent ();
1899 strm.PutCString("Best match found in ");
1900 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1901 strm.PutCString(":\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001902
Sean Callanand38b4a92012-06-06 20:49:55 +00001903 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1904 if (type_sp)
1905 {
1906 // Resolve the clang type so that any forward references
1907 // to types that haven't yet been parsed will get parsed.
1908 type_sp->GetClangFullType ();
1909 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1910 // Print all typedef chains
1911 TypeSP typedef_type_sp (type_sp);
1912 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1913 while (typedefed_type_sp)
1914 {
1915 strm.EOL();
1916 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1917 typedefed_type_sp->GetClangFullType ();
1918 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1919 typedef_type_sp = typedefed_type_sp;
1920 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1921 }
1922 }
1923 strm.EOL();
1924 }
1925 return num_matches;
1926}
1927
1928static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001929LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001930 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001931 Module *module,
1932 const FileSpec &file_spec,
1933 uint32_t line,
1934 bool check_inlines,
1935 bool verbose)
1936{
1937 if (module && file_spec)
1938 {
1939 SymbolContextList sc_list;
1940 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1941 eSymbolContextEverything, sc_list);
1942 if (num_matches > 0)
1943 {
1944 strm.Indent ();
1945 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1946 strm << file_spec;
1947 if (line > 0)
1948 strm.Printf (":%u", line);
1949 strm << " in ";
1950 DumpFullpath (strm, &module->GetFileSpec(), 0);
1951 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001952 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001953 return num_matches;
1954 }
1955 }
1956 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001957}
1958
Greg Clayton8ee64382011-11-10 01:18:58 +00001959
1960static size_t
1961FindModulesByName (Target *target,
1962 const char *module_name,
1963 ModuleList &module_list,
1964 bool check_global_list)
1965{
1966// Dump specified images (by basename or fullpath)
1967 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001968 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001969
Greg Clayton8ee64382011-11-10 01:18:58 +00001970 const size_t initial_size = module_list.GetSize ();
1971
Greg Claytonf3156262012-07-11 20:46:47 +00001972 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001973 {
1974 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001975 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001976 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001977 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001978 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001979 {
1980 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001981
Greg Clayton8ee64382011-11-10 01:18:58 +00001982 if (module)
1983 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001984 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001985 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001986 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001987 module_list.AppendIfNeeded(module_sp);
1988 }
1989 }
1990 }
1991 }
Greg Claytonf3156262012-07-11 20:46:47 +00001992 else
1993 {
1994 if (target)
1995 {
1996 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001997
Greg Claytonf3156262012-07-11 20:46:47 +00001998 // Not found in our module list for our target, check the main
1999 // shared module list in case it is a extra file used somewhere
2000 // else
2001 if (num_matches == 0)
2002 {
2003 module_spec.GetArchitecture() = target->GetArchitecture();
2004 ModuleList::FindSharedModules (module_spec, module_list);
2005 }
2006 }
2007 else
2008 {
2009 ModuleList::FindSharedModules (module_spec,module_list);
2010 }
2011 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002012
Greg Clayton8ee64382011-11-10 01:18:58 +00002013 return module_list.GetSize () - initial_size;
2014}
2015
Greg Claytoneffe5c92011-05-03 22:09:39 +00002016#pragma mark CommandObjectTargetModulesModuleAutoComplete
2017
2018//----------------------------------------------------------------------
2019// A base command object class that can auto complete with module file
2020// paths
2021//----------------------------------------------------------------------
2022
Jim Ingham5a988412012-06-08 21:56:10 +00002023class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002024{
2025public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002026 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2027 const char *name,
2028 const char *help,
2029 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002030 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002031 {
2032 CommandArgumentEntry arg;
2033 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002034
Greg Claytoneffe5c92011-05-03 22:09:39 +00002035 // Define the first (and only) variant of this arg.
2036 file_arg.arg_type = eArgTypeFilename;
2037 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002038
Greg Claytoneffe5c92011-05-03 22:09:39 +00002039 // There is only one variant this argument could be; put it into the argument entry.
2040 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002041
Greg Claytoneffe5c92011-05-03 22:09:39 +00002042 // Push the data for the first argument into the m_arguments vector.
2043 m_arguments.push_back (arg);
2044 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002045
Greg Claytoneffe5c92011-05-03 22:09:39 +00002046 virtual
2047 ~CommandObjectTargetModulesModuleAutoComplete ()
2048 {
2049 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002050
Greg Claytoneffe5c92011-05-03 22:09:39 +00002051 virtual int
2052 HandleArgumentCompletion (Args &input,
2053 int &cursor_index,
2054 int &cursor_char_position,
2055 OptionElementVector &opt_element_vector,
2056 int match_start_point,
2057 int max_return_elements,
2058 bool &word_complete,
2059 StringList &matches)
2060 {
2061 // Arguments are the standard module completer.
2062 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2063 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002064
Greg Claytoneffe5c92011-05-03 22:09:39 +00002065 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2066 CommandCompletions::eModuleCompletion,
2067 completion_str.c_str(),
2068 match_start_point,
2069 max_return_elements,
2070 NULL,
2071 word_complete,
2072 matches);
2073 return matches.GetSize();
2074 }
2075};
2076
2077#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2078
2079//----------------------------------------------------------------------
2080// A base command object class that can auto complete with module source
2081// file paths
2082//----------------------------------------------------------------------
2083
Jim Ingham5a988412012-06-08 21:56:10 +00002084class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002085{
2086public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002087 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002088 const char *name,
2089 const char *help,
2090 const char *syntax,
2091 uint32_t flags) :
2092 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002093 {
2094 CommandArgumentEntry arg;
2095 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002096
Greg Claytoneffe5c92011-05-03 22:09:39 +00002097 // Define the first (and only) variant of this arg.
2098 source_file_arg.arg_type = eArgTypeSourceFile;
2099 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002100
Greg Claytoneffe5c92011-05-03 22:09:39 +00002101 // There is only one variant this argument could be; put it into the argument entry.
2102 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002103
Greg Claytoneffe5c92011-05-03 22:09:39 +00002104 // Push the data for the first argument into the m_arguments vector.
2105 m_arguments.push_back (arg);
2106 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002107
Greg Claytoneffe5c92011-05-03 22:09:39 +00002108 virtual
2109 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2110 {
2111 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002112
Greg Claytoneffe5c92011-05-03 22:09:39 +00002113 virtual int
2114 HandleArgumentCompletion (Args &input,
2115 int &cursor_index,
2116 int &cursor_char_position,
2117 OptionElementVector &opt_element_vector,
2118 int match_start_point,
2119 int max_return_elements,
2120 bool &word_complete,
2121 StringList &matches)
2122 {
2123 // Arguments are the standard source file completer.
2124 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2125 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002126
Greg Claytoneffe5c92011-05-03 22:09:39 +00002127 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2128 CommandCompletions::eSourceFileCompletion,
2129 completion_str.c_str(),
2130 match_start_point,
2131 max_return_elements,
2132 NULL,
2133 word_complete,
2134 matches);
2135 return matches.GetSize();
2136 }
2137};
2138
2139
2140#pragma mark CommandObjectTargetModulesDumpSymtab
2141
2142
2143class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2144{
2145public:
2146 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2147 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2148 "target modules dump symtab",
2149 "Dump the symbol table from one or more target modules.",
2150 NULL),
2151 m_options (interpreter)
2152 {
2153 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002154
Greg Claytoneffe5c92011-05-03 22:09:39 +00002155 virtual
2156 ~CommandObjectTargetModulesDumpSymtab ()
2157 {
2158 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002159
Jim Ingham5a988412012-06-08 21:56:10 +00002160 virtual Options *
2161 GetOptions ()
2162 {
2163 return &m_options;
2164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002165
Jim Ingham5a988412012-06-08 21:56:10 +00002166 class CommandOptions : public Options
2167 {
2168 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002169 CommandOptions (CommandInterpreter &interpreter) :
2170 Options(interpreter),
2171 m_sort_order (eSortOrderNone)
2172 {
2173 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002174
Jim Ingham5a988412012-06-08 21:56:10 +00002175 virtual
2176 ~CommandOptions ()
2177 {
2178 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002179
Jim Ingham5a988412012-06-08 21:56:10 +00002180 virtual Error
2181 SetOptionValue (uint32_t option_idx, const char *option_arg)
2182 {
2183 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002184 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002185
Jim Ingham5a988412012-06-08 21:56:10 +00002186 switch (short_option)
2187 {
2188 case 's':
2189 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2190 g_option_table[option_idx].enum_values,
2191 eSortOrderNone,
2192 error);
2193 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002194
Jim Ingham5a988412012-06-08 21:56:10 +00002195 default:
2196 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2197 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002198
Jim Ingham5a988412012-06-08 21:56:10 +00002199 }
2200 return error;
2201 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002202
Jim Ingham5a988412012-06-08 21:56:10 +00002203 void
2204 OptionParsingStarting ()
2205 {
2206 m_sort_order = eSortOrderNone;
2207 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002208
Jim Ingham5a988412012-06-08 21:56:10 +00002209 const OptionDefinition*
2210 GetDefinitions ()
2211 {
2212 return g_option_table;
2213 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002214
Jim Ingham5a988412012-06-08 21:56:10 +00002215 // Options table: Required for subclasses of Options.
2216 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002217
Jim Ingham5a988412012-06-08 21:56:10 +00002218 SortOrder m_sort_order;
2219 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002220
Jim Ingham5a988412012-06-08 21:56:10 +00002221protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002222 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002223 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002224 CommandReturnObject &result)
2225 {
2226 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2227 if (target == NULL)
2228 {
2229 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2230 result.SetStatus (eReturnStatusFailed);
2231 return false;
2232 }
2233 else
2234 {
2235 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002236
Greg Claytoneffe5c92011-05-03 22:09:39 +00002237 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2238 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2239 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002240
Greg Claytoneffe5c92011-05-03 22:09:39 +00002241 if (command.GetArgumentCount() == 0)
2242 {
2243 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002244 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002245 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002246 if (num_modules > 0)
2247 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002248 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002249 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002250 {
2251 if (num_dumped > 0)
2252 {
2253 result.GetOutputStream().EOL();
2254 result.GetOutputStream().EOL();
2255 }
2256 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002257 DumpModuleSymtab (m_interpreter,
2258 result.GetOutputStream(),
2259 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2260 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002261 }
2262 }
2263 else
2264 {
2265 result.AppendError ("the target has no associated executable images");
2266 result.SetStatus (eReturnStatusFailed);
2267 return false;
2268 }
2269 }
2270 else
2271 {
2272 // Dump specified images (by basename or fullpath)
2273 const char *arg_cstr;
2274 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2275 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002276 ModuleList module_list;
2277 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2278 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002279 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002280 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002281 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002282 Module *module = module_list.GetModulePointerAtIndex(i);
2283 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002284 {
2285 if (num_dumped > 0)
2286 {
2287 result.GetOutputStream().EOL();
2288 result.GetOutputStream().EOL();
2289 }
2290 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002291 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002292 }
2293 }
2294 }
2295 else
2296 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2297 }
2298 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002299
Greg Claytoneffe5c92011-05-03 22:09:39 +00002300 if (num_dumped > 0)
2301 result.SetStatus (eReturnStatusSuccessFinishResult);
2302 else
2303 {
2304 result.AppendError ("no matching executable images found");
2305 result.SetStatus (eReturnStatusFailed);
2306 }
2307 }
2308 return result.Succeeded();
2309 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002310
Greg Claytoneffe5c92011-05-03 22:09:39 +00002311 CommandOptions m_options;
2312};
2313
2314static OptionEnumValueElement
2315g_sort_option_enumeration[4] =
2316{
2317 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2318 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2319 { eSortOrderByName, "name", "Sort output by symbol name."},
2320 { 0, NULL, NULL }
2321};
2322
2323
2324OptionDefinition
2325CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2326{
Zachary Turnerd37221d2014-07-09 16:31:49 +00002327 { 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."},
2328 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002329};
2330
2331#pragma mark CommandObjectTargetModulesDumpSections
2332
2333//----------------------------------------------------------------------
2334// Image section dumping command
2335//----------------------------------------------------------------------
2336
2337class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2338{
2339public:
2340 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2341 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2342 "target modules dump sections",
2343 "Dump the sections from one or more target modules.",
2344 //"target modules dump sections [<file1> ...]")
2345 NULL)
2346 {
2347 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002348
Greg Claytoneffe5c92011-05-03 22:09:39 +00002349 virtual
2350 ~CommandObjectTargetModulesDumpSections ()
2351 {
2352 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002353
Jim Ingham5a988412012-06-08 21:56:10 +00002354protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002355 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002356 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002357 CommandReturnObject &result)
2358 {
2359 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2360 if (target == NULL)
2361 {
2362 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2363 result.SetStatus (eReturnStatusFailed);
2364 return false;
2365 }
2366 else
2367 {
2368 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002369
Greg Claytoneffe5c92011-05-03 22:09:39 +00002370 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2371 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2372 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002373
Greg Claytoneffe5c92011-05-03 22:09:39 +00002374 if (command.GetArgumentCount() == 0)
2375 {
2376 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002377 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002378 if (num_modules > 0)
2379 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002380 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002381 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002382 {
2383 num_dumped++;
2384 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2385 }
2386 }
2387 else
2388 {
2389 result.AppendError ("the target has no associated executable images");
2390 result.SetStatus (eReturnStatusFailed);
2391 return false;
2392 }
2393 }
2394 else
2395 {
2396 // Dump specified images (by basename or fullpath)
2397 const char *arg_cstr;
2398 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2399 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002400 ModuleList module_list;
2401 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2402 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002403 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002404 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002405 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002406 Module *module = module_list.GetModulePointerAtIndex(i);
2407 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002408 {
2409 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002410 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002411 }
2412 }
2413 }
2414 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002415 {
2416 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002417 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002418
Greg Claytoneffe5c92011-05-03 22:09:39 +00002419 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002420 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002421 }
2422 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002423
Greg Claytoneffe5c92011-05-03 22:09:39 +00002424 if (num_dumped > 0)
2425 result.SetStatus (eReturnStatusSuccessFinishResult);
2426 else
2427 {
2428 result.AppendError ("no matching executable images found");
2429 result.SetStatus (eReturnStatusFailed);
2430 }
2431 }
2432 return result.Succeeded();
2433 }
2434};
2435
2436
2437#pragma mark CommandObjectTargetModulesDumpSymfile
2438
2439//----------------------------------------------------------------------
2440// Image debug symbol dumping command
2441//----------------------------------------------------------------------
2442
2443class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2444{
2445public:
2446 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2447 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2448 "target modules dump symfile",
2449 "Dump the debug symbol file for one or more target modules.",
2450 //"target modules dump symfile [<file1> ...]")
2451 NULL)
2452 {
2453 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002454
Greg Claytoneffe5c92011-05-03 22:09:39 +00002455 virtual
2456 ~CommandObjectTargetModulesDumpSymfile ()
2457 {
2458 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002459
Jim Ingham5a988412012-06-08 21:56:10 +00002460protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002461 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002462 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002463 CommandReturnObject &result)
2464 {
2465 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2466 if (target == NULL)
2467 {
2468 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2469 result.SetStatus (eReturnStatusFailed);
2470 return false;
2471 }
2472 else
2473 {
2474 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002475
Greg Claytoneffe5c92011-05-03 22:09:39 +00002476 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2477 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2478 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002479
Greg Claytoneffe5c92011-05-03 22:09:39 +00002480 if (command.GetArgumentCount() == 0)
2481 {
2482 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002483 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002484 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002485 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002486 if (num_modules > 0)
2487 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002488 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002489 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2490 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002491 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002492 num_dumped++;
2493 }
2494 }
2495 else
2496 {
2497 result.AppendError ("the target has no associated executable images");
2498 result.SetStatus (eReturnStatusFailed);
2499 return false;
2500 }
2501 }
2502 else
2503 {
2504 // Dump specified images (by basename or fullpath)
2505 const char *arg_cstr;
2506 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2507 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002508 ModuleList module_list;
2509 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2510 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002511 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002512 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002513 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002514 Module *module = module_list.GetModulePointerAtIndex(i);
2515 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002516 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002517 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002518 num_dumped++;
2519 }
2520 }
2521 }
2522 else
2523 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2524 }
2525 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002526
Greg Claytoneffe5c92011-05-03 22:09:39 +00002527 if (num_dumped > 0)
2528 result.SetStatus (eReturnStatusSuccessFinishResult);
2529 else
2530 {
2531 result.AppendError ("no matching executable images found");
2532 result.SetStatus (eReturnStatusFailed);
2533 }
2534 }
2535 return result.Succeeded();
2536 }
2537};
2538
2539
2540#pragma mark CommandObjectTargetModulesDumpLineTable
2541
2542//----------------------------------------------------------------------
2543// Image debug line table dumping command
2544//----------------------------------------------------------------------
2545
2546class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2547{
2548public:
2549 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2550 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002551 "target modules dump line-table",
Jim Inghamcc0273d2013-06-18 20:27:11 +00002552 "Dump the line table for one or more compilation units.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00002553 NULL,
2554 eFlagRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002555 {
2556 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002557
Greg Claytoneffe5c92011-05-03 22:09:39 +00002558 virtual
2559 ~CommandObjectTargetModulesDumpLineTable ()
2560 {
2561 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002562
Jim Ingham5a988412012-06-08 21:56:10 +00002563protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002564 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002565 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002566 CommandReturnObject &result)
2567 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002568 Target *target = m_exe_ctx.GetTargetPtr();
2569 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002570
Greg Claytonf9fc6092013-01-09 19:44:40 +00002571 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2572 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2573 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002574
Greg Claytonf9fc6092013-01-09 19:44:40 +00002575 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002576 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002577 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00002578 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002579 }
2580 else
2581 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002582 // Dump specified images (by basename or fullpath)
2583 const char *arg_cstr;
2584 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002585 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002586 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002587
Greg Claytonf9fc6092013-01-09 19:44:40 +00002588 const ModuleList &target_modules = target->GetImages();
2589 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002590 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002591 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002592 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002593 uint32_t num_dumped = 0;
2594 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002595 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002596 if (DumpCompileUnitLineTable (m_interpreter,
2597 result.GetOutputStream(),
2598 target_modules.GetModulePointerAtIndexUnlocked(i),
2599 file_spec,
2600 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2601 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002602 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002603 if (num_dumped == 0)
2604 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2605 else
2606 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002607 }
2608 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002609 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002610
Greg Claytonf9fc6092013-01-09 19:44:40 +00002611 if (total_num_dumped > 0)
2612 result.SetStatus (eReturnStatusSuccessFinishResult);
2613 else
2614 {
2615 result.AppendError ("no source filenames matched any command arguments");
2616 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002617 }
2618 return result.Succeeded();
2619 }
2620};
2621
2622
2623#pragma mark CommandObjectTargetModulesDump
2624
2625//----------------------------------------------------------------------
2626// Dump multi-word command for target modules
2627//----------------------------------------------------------------------
2628
2629class CommandObjectTargetModulesDump : public CommandObjectMultiword
2630{
2631public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002632 //------------------------------------------------------------------
2633 // Constructors and Destructors
2634 //------------------------------------------------------------------
2635 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2636 CommandObjectMultiword (interpreter,
2637 "target modules dump",
2638 "A set of commands for dumping information about one or more target modules.",
2639 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2640 {
2641 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2642 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2643 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2644 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2645 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002646
Greg Claytoneffe5c92011-05-03 22:09:39 +00002647 virtual
2648 ~CommandObjectTargetModulesDump()
2649 {
2650 }
2651};
2652
Jim Ingham5a988412012-06-08 21:56:10 +00002653class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002654{
2655public:
2656 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002657 CommandObjectParsed (interpreter,
2658 "target modules add",
2659 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002660 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002661 m_option_group (interpreter),
2662 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 +00002663 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002664 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002665 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002666 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002667 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002668
Greg Claytoneffe5c92011-05-03 22:09:39 +00002669 virtual
2670 ~CommandObjectTargetModulesAdd ()
2671 {
2672 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002673
Greg Clayton50a24bd2012-11-29 22:16:27 +00002674 virtual Options *
2675 GetOptions ()
2676 {
2677 return &m_option_group;
2678 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002679
Greg Claytonc7bece562013-01-25 18:06:21 +00002680 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00002681 HandleArgumentCompletion (Args &input,
2682 int &cursor_index,
2683 int &cursor_char_position,
2684 OptionElementVector &opt_element_vector,
2685 int match_start_point,
2686 int max_return_elements,
2687 bool &word_complete,
2688 StringList &matches)
2689 {
2690 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2691 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002692
Jim Ingham5a988412012-06-08 21:56:10 +00002693 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2694 CommandCompletions::eDiskFileCompletion,
2695 completion_str.c_str(),
2696 match_start_point,
2697 max_return_elements,
2698 NULL,
2699 word_complete,
2700 matches);
2701 return matches.GetSize();
2702 }
2703
2704protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002705 OptionGroupOptions m_option_group;
2706 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002707 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002708
Greg Claytoneffe5c92011-05-03 22:09:39 +00002709 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002710 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002711 CommandReturnObject &result)
2712 {
2713 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2714 if (target == NULL)
2715 {
2716 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2717 result.SetStatus (eReturnStatusFailed);
2718 return false;
2719 }
2720 else
2721 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002722 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002723
Greg Claytoneffe5c92011-05-03 22:09:39 +00002724 const size_t argc = args.GetArgumentCount();
2725 if (argc == 0)
2726 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002727 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2728 {
2729 // We are given a UUID only, go locate the file
2730 ModuleSpec module_spec;
2731 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002732 if (m_symbol_file.GetOptionValue().OptionWasSet())
2733 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002734 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2735 {
2736 ModuleSP module_sp (target->GetSharedModule (module_spec));
2737 if (module_sp)
2738 {
2739 result.SetStatus (eReturnStatusSuccessFinishResult);
2740 return true;
2741 }
2742 else
2743 {
2744 StreamString strm;
2745 module_spec.GetUUID().Dump (&strm);
2746 if (module_spec.GetFileSpec())
2747 {
2748 if (module_spec.GetSymbolFileSpec())
2749 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002750 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 +00002751 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002752 module_spec.GetFileSpec().GetPath().c_str(),
2753 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002754 }
2755 else
2756 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002757 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002758 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002759 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002760 }
2761 }
2762 else
2763 {
2764 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2765 strm.GetString().c_str());
2766 }
2767 result.SetStatus (eReturnStatusFailed);
2768 return false;
2769 }
2770 }
2771 else
2772 {
2773 StreamString strm;
2774 module_spec.GetUUID().Dump (&strm);
2775 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2776 result.SetStatus (eReturnStatusFailed);
2777 return false;
2778 }
2779 }
2780 else
2781 {
2782 result.AppendError ("one or more executable image paths must be specified");
2783 result.SetStatus (eReturnStatusFailed);
2784 return false;
2785 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002786 }
2787 else
2788 {
2789 for (size_t i=0; i<argc; ++i)
2790 {
2791 const char *path = args.GetArgumentAtIndex(i);
2792 if (path)
2793 {
2794 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002795 if (file_spec.Exists())
2796 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002797 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002798 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2799 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002800 if (m_symbol_file.GetOptionValue().OptionWasSet())
2801 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002802 if (!module_spec.GetArchitecture().IsValid())
2803 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002804 Error error;
2805 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002806 if (!module_sp)
2807 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002808 const char *error_cstr = error.AsCString();
2809 if (error_cstr)
2810 result.AppendError (error_cstr);
2811 else
2812 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002813 result.SetStatus (eReturnStatusFailed);
2814 return false;
2815 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002816 else
2817 {
2818 flush = true;
2819 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002820 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002821 }
2822 else
2823 {
2824 char resolved_path[PATH_MAX];
2825 result.SetStatus (eReturnStatusFailed);
2826 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2827 {
2828 if (strcmp (resolved_path, path) != 0)
2829 {
2830 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2831 break;
2832 }
2833 }
2834 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2835 break;
2836 }
2837 }
2838 }
2839 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002840
Sean Callananb36c6c02012-12-13 01:39:39 +00002841 if (flush)
2842 {
2843 ProcessSP process = target->GetProcessSP();
2844 if (process)
2845 process->Flush();
2846 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002847 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002848
Greg Claytoneffe5c92011-05-03 22:09:39 +00002849 return result.Succeeded();
2850 }
2851
Greg Claytoneffe5c92011-05-03 22:09:39 +00002852};
2853
2854class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2855{
2856public:
2857 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2858 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2859 "target modules load",
2860 "Set the load addresses for one or more sections in a target module.",
2861 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2862 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002863 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName, "Fullpath or basename for module to load.", ""),
Greg Claytoneffe5c92011-05-03 22:09:39 +00002864 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)
2865 {
2866 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2867 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2868 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2869 m_option_group.Finalize();
2870 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002871
Greg Claytoneffe5c92011-05-03 22:09:39 +00002872 virtual
2873 ~CommandObjectTargetModulesLoad ()
2874 {
2875 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002876
Jim Ingham5a988412012-06-08 21:56:10 +00002877 virtual Options *
2878 GetOptions ()
2879 {
2880 return &m_option_group;
2881 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002882
Jim Ingham5a988412012-06-08 21:56:10 +00002883protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002884 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002885 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002886 CommandReturnObject &result)
2887 {
2888 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2889 if (target == NULL)
2890 {
2891 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2892 result.SetStatus (eReturnStatusFailed);
2893 return false;
2894 }
2895 else
2896 {
2897 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002898 ModuleSpec module_spec;
2899 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002900 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002901 {
2902 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002903 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2904 const bool use_global_module_list = true;
2905 ModuleList module_list;
2906 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2907 if (num_matches == 1)
2908 {
2909 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2910 }
2911 else if (num_matches > 1 )
2912 {
2913 search_using_module_spec = false;
2914 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2915 result.SetStatus (eReturnStatusFailed);
2916 }
2917 else
2918 {
2919 search_using_module_spec = false;
2920 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2921 result.SetStatus (eReturnStatusFailed);
2922 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002923 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002924
Greg Claytoneffe5c92011-05-03 22:09:39 +00002925 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002926 {
2927 search_using_module_spec = true;
2928 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2929 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002930
Greg Claytonb9a01b32012-02-26 05:51:37 +00002931 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002932 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002933 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002934 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002935
2936 char path[PATH_MAX];
2937 if (num_matches == 1)
2938 {
2939 Module *module = matching_modules.GetModulePointerAtIndex(0);
2940 if (module)
2941 {
2942 ObjectFile *objfile = module->GetObjectFile();
2943 if (objfile)
2944 {
Greg Clayton3046e662013-07-10 01:23:25 +00002945 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002946 if (section_list)
2947 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002948 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002949 if (argc == 0)
2950 {
2951 if (m_slide_option.GetOptionValue().OptionWasSet())
2952 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002953 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002954 const bool slide_is_offset = true;
2955 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002956 }
2957 else
2958 {
2959 result.AppendError ("one or more section name + load address pair must be specified");
2960 result.SetStatus (eReturnStatusFailed);
2961 return false;
2962 }
2963 }
2964 else
2965 {
2966 if (m_slide_option.GetOptionValue().OptionWasSet())
2967 {
2968 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2969 result.SetStatus (eReturnStatusFailed);
2970 return false;
2971 }
2972
2973 for (size_t i=0; i<argc; i += 2)
2974 {
2975 const char *sect_name = args.GetArgumentAtIndex(i);
2976 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2977 if (sect_name && load_addr_cstr)
2978 {
2979 ConstString const_sect_name(sect_name);
2980 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00002981 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002982 if (success)
2983 {
2984 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2985 if (section_sp)
2986 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002987 if (section_sp->IsThreadSpecific())
2988 {
2989 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2990 result.SetStatus (eReturnStatusFailed);
2991 break;
2992 }
2993 else
2994 {
Greg Clayton7820bd12012-07-07 01:24:12 +00002995 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00002996 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00002997 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00002998 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002999 }
3000 else
3001 {
3002 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
3003 result.SetStatus (eReturnStatusFailed);
3004 break;
3005 }
3006 }
3007 else
3008 {
3009 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
3010 result.SetStatus (eReturnStatusFailed);
3011 break;
3012 }
3013 }
3014 else
3015 {
3016 if (sect_name)
3017 result.AppendError ("section names must be followed by a load address.\n");
3018 else
3019 result.AppendError ("one or more section name + load address pair must be specified.\n");
3020 result.SetStatus (eReturnStatusFailed);
3021 break;
3022 }
3023 }
3024 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003025
Greg Clayton741f3f92012-03-27 21:10:07 +00003026 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003027 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003028 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003029 Process *process = m_exe_ctx.GetProcessPtr();
3030 if (process)
3031 process->Flush();
3032 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003033 }
3034 else
3035 {
3036 module->GetFileSpec().GetPath (path, sizeof(path));
3037 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3038 result.SetStatus (eReturnStatusFailed);
3039 }
3040 }
3041 else
3042 {
3043 module->GetFileSpec().GetPath (path, sizeof(path));
3044 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3045 result.SetStatus (eReturnStatusFailed);
3046 }
3047 }
3048 else
3049 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003050 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3051 if (module_spec_file)
3052 {
3053 module_spec_file->GetPath (path, sizeof(path));
3054 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3055 }
3056 else
3057 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003058 result.SetStatus (eReturnStatusFailed);
3059 }
3060 }
3061 else
3062 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003063 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003064
Greg Claytonb9a01b32012-02-26 05:51:37 +00003065 if (module_spec.GetFileSpec())
3066 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003067 else
3068 path[0] = '\0';
3069
Greg Claytonb9a01b32012-02-26 05:51:37 +00003070 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003071 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003072 if (num_matches > 1)
3073 {
3074 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3075 path[0] ? " file=" : "",
3076 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003077 !uuid_str.empty() ? " uuid=" : "",
3078 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003079 for (size_t i=0; i<num_matches; ++i)
3080 {
3081 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3082 result.AppendMessageWithFormat("%s\n", path);
3083 }
3084 }
3085 else
3086 {
3087 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3088 path[0] ? " file=" : "",
3089 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003090 !uuid_str.empty() ? " uuid=" : "",
3091 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003092 }
3093 result.SetStatus (eReturnStatusFailed);
3094 }
3095 }
3096 else
3097 {
3098 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3099 result.SetStatus (eReturnStatusFailed);
3100 return false;
3101 }
3102 }
3103 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003104 }
3105
Greg Claytoneffe5c92011-05-03 22:09:39 +00003106 OptionGroupOptions m_option_group;
3107 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003108 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003109 OptionGroupUInt64 m_slide_option;
3110};
3111
3112//----------------------------------------------------------------------
3113// List images with associated information
3114//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003115class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003116{
3117public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003118 class CommandOptions : public Options
3119 {
3120 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003121 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003122 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003123 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003124 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003125 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003126 {
3127 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003128
Greg Claytoneffe5c92011-05-03 22:09:39 +00003129 virtual
3130 ~CommandOptions ()
3131 {
3132 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003133
Greg Claytoneffe5c92011-05-03 22:09:39 +00003134 virtual Error
3135 SetOptionValue (uint32_t option_idx, const char *option_arg)
3136 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003137 Error error;
3138
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003139 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003140 if (short_option == 'g')
3141 {
3142 m_use_global_module_list = true;
3143 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003144 else if (short_option == 'a')
3145 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003146 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3147 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003148 }
Greg Clayton65a03992011-08-09 00:01:09 +00003149 else
3150 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003151 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003152 if (option_arg)
3153 width = strtoul (option_arg, NULL, 0);
3154 m_format_array.push_back(std::make_pair(short_option, width));
3155 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003156 return error;
3157 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003158
Greg Claytoneffe5c92011-05-03 22:09:39 +00003159 void
3160 OptionParsingStarting ()
3161 {
3162 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003163 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003164 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003165 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003166
Greg Claytoneffe5c92011-05-03 22:09:39 +00003167 const OptionDefinition*
3168 GetDefinitions ()
3169 {
3170 return g_option_table;
3171 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003172
Greg Claytoneffe5c92011-05-03 22:09:39 +00003173 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003174
Greg Claytoneffe5c92011-05-03 22:09:39 +00003175 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003176
Greg Claytoneffe5c92011-05-03 22:09:39 +00003177 // Instance variables to hold the values for command options.
3178 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3179 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003180 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003181 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003182 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003183
Greg Claytoneffe5c92011-05-03 22:09:39 +00003184 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003185 CommandObjectParsed (interpreter,
3186 "target modules list",
3187 "List current executable and dependent shared library images.",
3188 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003189 m_options (interpreter)
3190 {
3191 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003192
Greg Claytoneffe5c92011-05-03 22:09:39 +00003193 virtual
3194 ~CommandObjectTargetModulesList ()
3195 {
3196 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003197
Greg Claytoneffe5c92011-05-03 22:09:39 +00003198 virtual
3199 Options *
3200 GetOptions ()
3201 {
3202 return &m_options;
3203 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003204
Jim Ingham5a988412012-06-08 21:56:10 +00003205protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003206 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00003207 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003208 CommandReturnObject &result)
3209 {
3210 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003211 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003212 // Define a local module list here to ensure it lives longer than any "locker"
3213 // object which might lock its contents below (through the "module_list_ptr"
3214 // variable).
3215 ModuleList module_list;
Greg Clayton3418c852011-08-10 02:10:13 +00003216 if (target == NULL && use_global_module_list == false)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003217 {
3218 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3219 result.SetStatus (eReturnStatusFailed);
3220 return false;
3221 }
3222 else
3223 {
Greg Clayton3418c852011-08-10 02:10:13 +00003224 if (target)
3225 {
3226 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3227 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3228 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3229 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003230 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003231 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003232
Jim Inghamc10312c2011-10-24 18:36:33 +00003233 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3234 {
3235 if (target)
3236 {
3237 Address module_address;
3238 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3239 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003240 ModuleSP module_sp (module_address.GetModule());
3241 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003242 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003243 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003244 result.SetStatus (eReturnStatusSuccessFinishResult);
3245 }
3246 else
3247 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003248 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003249 result.SetStatus (eReturnStatusFailed);
3250 }
3251 }
3252 else
3253 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003254 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003255 result.SetStatus (eReturnStatusFailed);
3256 }
3257 }
3258 else
3259 {
3260 result.AppendError ("Can only look up modules by address with a valid target.");
3261 result.SetStatus (eReturnStatusFailed);
3262 }
3263 return result.Succeeded();
3264 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003265
Greg Claytonc7bece562013-01-25 18:06:21 +00003266 size_t num_modules = 0;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003267 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3268 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3269 // the global module list directly.
Enrico Granata17598482012-11-08 02:22:02 +00003270 const ModuleList *module_list_ptr = NULL;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003271 const size_t argc = command.GetArgumentCount();
3272 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003273 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003274 if (use_global_module_list)
3275 {
3276 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3277 num_modules = Module::GetNumberAllocatedModules();
3278 }
3279 else
3280 {
3281 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003282 }
Greg Clayton65a03992011-08-09 00:01:09 +00003283 }
3284 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003285 {
3286 for (size_t i=0; i<argc; ++i)
3287 {
3288 // Dump specified images (by basename or fullpath)
3289 const char *arg_cstr = command.GetArgumentAtIndex(i);
3290 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3291 if (num_matches == 0)
3292 {
3293 if (argc == 1)
3294 {
3295 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3296 result.SetStatus (eReturnStatusFailed);
3297 return false;
3298 }
3299 }
3300 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003301
Greg Claytonc4a8a762012-05-15 18:43:44 +00003302 module_list_ptr = &module_list;
3303 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003304
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003305 if (module_list_ptr != NULL)
3306 {
3307 locker.Lock(module_list_ptr->GetMutex());
3308 num_modules = module_list_ptr->GetSize();
3309 }
Greg Clayton65a03992011-08-09 00:01:09 +00003310
Greg Claytoneffe5c92011-05-03 22:09:39 +00003311 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003312 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003313 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3314 {
Greg Clayton3418c852011-08-10 02:10:13 +00003315 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003316 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003317 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003318 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003319 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003320 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003321 }
3322 else
3323 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003324 module = Module::GetAllocatedModuleAtIndex(image_idx);
3325 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003326 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003327
Greg Claytonc7bece562013-01-25 18:06:21 +00003328 const size_t indent = strm.Printf("[%3u] ", image_idx);
3329 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003330
Greg Claytoneffe5c92011-05-03 22:09:39 +00003331 }
3332 result.SetStatus (eReturnStatusSuccessFinishResult);
3333 }
3334 else
3335 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003336 if (argc)
3337 {
3338 if (use_global_module_list)
3339 result.AppendError ("the global module list has no matching modules");
3340 else
3341 result.AppendError ("the target has no matching modules");
3342 }
Greg Clayton3418c852011-08-10 02:10:13 +00003343 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003344 {
3345 if (use_global_module_list)
3346 result.AppendError ("the global module list is empty");
3347 else
3348 result.AppendError ("the target has no associated executable images");
3349 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003350 result.SetStatus (eReturnStatusFailed);
3351 return false;
3352 }
3353 }
3354 return result.Succeeded();
3355 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003356
3357 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003358 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003359 {
3360
Jim Ingham28eb5712012-10-12 17:34:26 +00003361 if (module == NULL)
3362 {
3363 strm.PutCString("Null module");
3364 return;
3365 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003366
Jim Inghamc10312c2011-10-24 18:36:33 +00003367 bool dump_object_name = false;
3368 if (m_options.m_format_array.empty())
3369 {
Greg Claytonc9660542012-02-05 02:38:54 +00003370 m_options.m_format_array.push_back(std::make_pair('u', 0));
3371 m_options.m_format_array.push_back(std::make_pair('h', 0));
3372 m_options.m_format_array.push_back(std::make_pair('f', 0));
3373 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003374 }
Greg Claytonc9660542012-02-05 02:38:54 +00003375 const size_t num_entries = m_options.m_format_array.size();
3376 bool print_space = false;
3377 for (size_t i=0; i<num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003378 {
Greg Claytonc9660542012-02-05 02:38:54 +00003379 if (print_space)
3380 strm.PutChar(' ');
3381 print_space = true;
3382 const char format_char = m_options.m_format_array[i].first;
3383 uint32_t width = m_options.m_format_array[i].second;
3384 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003385 {
Greg Claytonc9660542012-02-05 02:38:54 +00003386 case 'A':
3387 DumpModuleArchitecture (strm, module, false, width);
3388 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003389
Greg Claytonc9660542012-02-05 02:38:54 +00003390 case 't':
3391 DumpModuleArchitecture (strm, module, true, width);
3392 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003393
Greg Claytonc9660542012-02-05 02:38:54 +00003394 case 'f':
3395 DumpFullpath (strm, &module->GetFileSpec(), width);
3396 dump_object_name = true;
3397 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003398
Greg Claytonc9660542012-02-05 02:38:54 +00003399 case 'd':
3400 DumpDirectory (strm, &module->GetFileSpec(), width);
3401 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003402
Greg Claytonc9660542012-02-05 02:38:54 +00003403 case 'b':
3404 DumpBasename (strm, &module->GetFileSpec(), width);
3405 dump_object_name = true;
3406 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003407
Greg Claytonc9660542012-02-05 02:38:54 +00003408 case 'h':
3409 case 'o':
3410 // Image header address
3411 {
3412 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003413
Greg Claytonc9660542012-02-05 02:38:54 +00003414 ObjectFile *objfile = module->GetObjectFile ();
3415 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003416 {
Greg Claytonc9660542012-02-05 02:38:54 +00003417 Address header_addr(objfile->GetHeaderAddress());
3418 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003419 {
Greg Claytonc9660542012-02-05 02:38:54 +00003420 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003421 {
Greg Claytonc9660542012-02-05 02:38:54 +00003422 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3423 if (header_load_addr == LLDB_INVALID_ADDRESS)
3424 {
3425 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3426 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003427 else
Greg Claytonc9660542012-02-05 02:38:54 +00003428 {
3429 if (format_char == 'o')
3430 {
3431 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003432 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003433 }
3434 else
3435 {
3436 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003437 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003438 }
3439 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003440 break;
3441 }
Greg Claytonc9660542012-02-05 02:38:54 +00003442 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3443 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3444 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003445 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003446 }
Greg Claytonc9660542012-02-05 02:38:54 +00003447 strm.Printf ("%*s", addr_nibble_width + 2, "");
3448 }
3449 break;
3450 case 'r':
3451 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003452 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003453 ModuleSP module_sp (module->shared_from_this());
3454 if (module_sp)
3455 {
3456 // Take one away to make sure we don't count our local "module_sp"
3457 ref_count = module_sp.use_count() - 1;
3458 }
3459 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003460 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003461 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003462 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003463 }
3464 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003465
Greg Claytonc9660542012-02-05 02:38:54 +00003466 case 's':
3467 case 'S':
3468 {
3469 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3470 if (symbol_vendor)
3471 {
3472 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3473 if (symbol_file)
3474 {
3475 if (format_char == 'S')
3476 {
3477 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3478 // Dump symbol file only if different from module file
3479 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3480 {
3481 print_space = false;
3482 break;
3483 }
3484 // Add a newline and indent past the index
3485 strm.Printf ("\n%*s", indent, "");
3486 }
3487 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3488 dump_object_name = true;
3489 break;
3490 }
3491 }
3492 strm.Printf("%.*s", width, "<NONE>");
3493 }
3494 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003495
Greg Claytonc9660542012-02-05 02:38:54 +00003496 case 'm':
3497 module->GetModificationTime().Dump(&strm, width);
3498 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003499
Greg Claytonc9660542012-02-05 02:38:54 +00003500 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003501 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003502 break;
3503
3504 case 'u':
3505 DumpModuleUUID(strm, module);
3506 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003507
Greg Claytonc9660542012-02-05 02:38:54 +00003508 default:
3509 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003510 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003511
Greg Claytonc9660542012-02-05 02:38:54 +00003512 }
3513 if (dump_object_name)
3514 {
3515 const char *object_name = module->GetObjectName().GetCString();
3516 if (object_name)
3517 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003518 }
3519 strm.EOL();
3520 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003521
Greg Claytoneffe5c92011-05-03 22:09:39 +00003522 CommandOptions m_options;
3523};
3524
3525OptionDefinition
3526CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3527{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003528 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3529 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
3530 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
3531 { 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."},
3532 { 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)."},
3533 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3534 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3535 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3536 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3537 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3538 { 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."},
3539 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3540 { 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."},
3541 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeNone, "Display the module pointer."},
3542 { 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."},
3543 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003544};
3545
Jason Molenda380241a2012-07-12 00:20:07 +00003546#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003547
Jason Molenda380241a2012-07-12 00:20:07 +00003548//----------------------------------------------------------------------
3549// Lookup unwind information in images
3550//----------------------------------------------------------------------
3551
3552class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3553{
3554public:
3555
3556 enum
3557 {
3558 eLookupTypeInvalid = -1,
3559 eLookupTypeAddress = 0,
3560 eLookupTypeSymbol,
3561 eLookupTypeFunction,
3562 eLookupTypeFunctionOrSymbol,
3563 kNumLookupTypes
3564 };
3565
3566 class CommandOptions : public Options
3567 {
3568 public:
3569
3570 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003571 Options(interpreter),
3572 m_type(eLookupTypeInvalid),
3573 m_str(),
3574 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003575 {
3576 }
3577
3578 virtual
3579 ~CommandOptions ()
3580 {
3581 }
3582
3583 virtual Error
3584 SetOptionValue (uint32_t option_idx, const char *option_arg)
3585 {
3586 Error error;
3587
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003588 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003589
3590 switch (short_option)
3591 {
3592 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003593 {
3594 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003595 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003596 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003597 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003598 if (m_addr == LLDB_INVALID_ADDRESS)
3599 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3600 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003601 }
Jason Molenda380241a2012-07-12 00:20:07 +00003602
3603 case 'n':
Jason Molenda535ab862013-04-23 04:30:57 +00003604 {
Jason Molenda380241a2012-07-12 00:20:07 +00003605 m_str = option_arg;
3606 m_type = eLookupTypeFunctionOrSymbol;
3607 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003608 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003609
3610 default:
3611 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3612 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003613 }
3614
3615 return error;
3616 }
3617
3618 void
3619 OptionParsingStarting ()
3620 {
3621 m_type = eLookupTypeInvalid;
3622 m_str.clear();
3623 m_addr = LLDB_INVALID_ADDRESS;
3624 }
3625
3626 const OptionDefinition*
3627 GetDefinitions ()
3628 {
3629 return g_option_table;
3630 }
3631
3632 // Options table: Required for subclasses of Options.
3633
3634 static OptionDefinition g_option_table[];
3635
3636 // Instance variables to hold the values for command options.
3637
3638 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3639 std::string m_str; // Holds name lookup
3640 lldb::addr_t m_addr; // Holds the address to lookup
3641 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003642
Jason Molenda380241a2012-07-12 00:20:07 +00003643 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3644 CommandObjectParsed (interpreter,
3645 "target modules show-unwind",
3646 "Show synthesized unwind instructions for a function.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003647 NULL,
3648 eFlagRequiresTarget |
3649 eFlagRequiresProcess |
3650 eFlagProcessMustBeLaunched |
3651 eFlagProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003652 m_options (interpreter)
3653 {
3654 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003655
Jason Molenda380241a2012-07-12 00:20:07 +00003656 virtual
3657 ~CommandObjectTargetModulesShowUnwind ()
3658 {
3659 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003660
Jason Molenda380241a2012-07-12 00:20:07 +00003661 virtual
3662 Options *
3663 GetOptions ()
3664 {
3665 return &m_options;
3666 }
3667
3668protected:
3669 bool
3670 DoExecute (Args& command,
3671 CommandReturnObject &result)
3672 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003673 Target *target = m_exe_ctx.GetTargetPtr();
3674 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda380241a2012-07-12 00:20:07 +00003675 ABI *abi = NULL;
3676 if (process)
3677 abi = process->GetABI().get();
3678
3679 if (process == NULL)
3680 {
3681 result.AppendError ("You must have a process running to use this command.");
3682 result.SetStatus (eReturnStatusFailed);
3683 return false;
3684 }
3685
3686 ThreadList threads(process->GetThreadList());
3687 if (threads.GetSize() == 0)
3688 {
3689 result.AppendError ("The process must be paused to use this command.");
3690 result.SetStatus (eReturnStatusFailed);
3691 return false;
3692 }
3693
3694 ThreadSP thread(threads.GetThreadAtIndex(0));
3695 if (thread.get() == NULL)
3696 {
3697 result.AppendError ("The process must be paused to use this command.");
3698 result.SetStatus (eReturnStatusFailed);
3699 return false;
3700 }
3701
Jason Molenda535ab862013-04-23 04:30:57 +00003702 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003703
Jason Molenda380241a2012-07-12 00:20:07 +00003704 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3705 {
Jason Molenda380241a2012-07-12 00:20:07 +00003706 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003707 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3708 }
3709 else if (m_options.m_type == eLookupTypeAddress && target)
3710 {
3711 Address addr;
3712 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003713 {
3714 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003715 ModuleSP module_sp (addr.GetModule());
3716 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3717 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003718 {
Jason Molenda535ab862013-04-23 04:30:57 +00003719 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003720 }
Jason Molenda535ab862013-04-23 04:30:57 +00003721 }
3722 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003723 else
3724 {
3725 result.AppendError ("address-expression or function name option must be specified.");
3726 result.SetStatus (eReturnStatusFailed);
3727 return false;
3728 }
Jason Molenda380241a2012-07-12 00:20:07 +00003729
Jason Molenda535ab862013-04-23 04:30:57 +00003730 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003731 if (num_matches == 0)
3732 {
3733 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3734 result.SetStatus (eReturnStatusFailed);
3735 return false;
3736 }
3737
Jason Molenda535ab862013-04-23 04:30:57 +00003738 for (uint32_t idx = 0; idx < num_matches; idx++)
3739 {
3740 SymbolContext sc;
3741 sc_list.GetContextAtIndex(idx, sc);
3742 if (sc.symbol == NULL && sc.function == NULL)
3743 continue;
3744 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3745 continue;
3746 AddressRange range;
3747 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3748 continue;
3749 if (!range.GetBaseAddress().IsValid())
3750 continue;
3751 ConstString funcname(sc.GetFunctionName());
3752 if (funcname.IsEmpty())
3753 continue;
3754 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3755 if (abi)
3756 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003757
Jason Molenda535ab862013-04-23 04:30:57 +00003758 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3759 if (func_unwinders_sp.get() == NULL)
3760 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003761
Jason Molenda34549b82015-01-13 06:04:04 +00003762 result.GetOutputStream().Printf("UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n\n", sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), funcname.AsCString(), start_addr);
Jason Molenda535ab862013-04-23 04:30:57 +00003763
Todd Fiala05625242014-08-25 20:29:09 +00003764 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread.get(), -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003765 if (non_callsite_unwind_plan.get())
3766 {
Jason Molenda34549b82015-01-13 06:04:04 +00003767 result.GetOutputStream().Printf("Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n", non_callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003768 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003769 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003770 if (callsite_unwind_plan.get())
3771 {
Jason Molenda34549b82015-01-13 06:04:04 +00003772 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003773 }
Jason Molenda535ab862013-04-23 04:30:57 +00003774 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3775 if (fast_unwind_plan.get())
3776 {
Jason Molenda34549b82015-01-13 06:04:04 +00003777 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003778 }
3779
Jason Molenda34549b82015-01-13 06:04:04 +00003780 result.GetOutputStream().Printf("\n");
3781
3782 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread.get(), 0);
3783 if (assembly_sp)
3784 {
3785 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3786 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3787 result.GetOutputStream().Printf("\n");
3788 }
3789
3790
3791 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3792 if (ehframe_sp)
3793 {
3794 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3795 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3796 result.GetOutputStream().Printf("\n");
3797 }
3798
3799 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread.get(), 0);
3800 if (ehframe_augmented_sp)
3801 {
3802 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3803 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3804 result.GetOutputStream().Printf("\n");
3805 }
3806
3807 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3808 if (compact_unwind_sp)
3809 {
3810 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3811 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3812 result.GetOutputStream().Printf("\n");
3813 }
3814
3815 if (fast_unwind_plan)
3816 {
3817 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3818 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3819 result.GetOutputStream().Printf("\n");
3820 }
3821
3822 ABISP abi_sp = process->GetABI();
3823 if (abi_sp)
3824 {
3825 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3826 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3827 {
3828 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3829 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3830 result.GetOutputStream().Printf("\n");
3831 }
3832
3833 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3834 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3835 {
3836 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3837 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3838 result.GetOutputStream().Printf("\n");
3839 }
3840 }
Jason Molenda535ab862013-04-23 04:30:57 +00003841
3842 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003843 }
3844 return result.Succeeded();
3845 }
3846
3847 CommandOptions m_options;
3848};
3849
3850OptionDefinition
3851CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3852{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003853 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3854 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3855 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jason Molenda380241a2012-07-12 00:20:07 +00003856};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003857
3858//----------------------------------------------------------------------
3859// Lookup information in images
3860//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003861class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003862{
3863public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003864 enum
3865 {
3866 eLookupTypeInvalid = -1,
3867 eLookupTypeAddress = 0,
3868 eLookupTypeSymbol,
3869 eLookupTypeFileLine, // Line is optional
3870 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003871 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003872 eLookupTypeType,
3873 kNumLookupTypes
3874 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003875
Greg Claytoneffe5c92011-05-03 22:09:39 +00003876 class CommandOptions : public Options
3877 {
3878 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003879 CommandOptions (CommandInterpreter &interpreter) :
3880 Options(interpreter)
3881 {
3882 OptionParsingStarting();
3883 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003884
Greg Claytoneffe5c92011-05-03 22:09:39 +00003885 virtual
3886 ~CommandOptions ()
3887 {
3888 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003889
Greg Claytoneffe5c92011-05-03 22:09:39 +00003890 virtual Error
3891 SetOptionValue (uint32_t option_idx, const char *option_arg)
3892 {
3893 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003894
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003895 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003896
Greg Claytoneffe5c92011-05-03 22:09:39 +00003897 switch (short_option)
3898 {
3899 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003900 {
3901 m_type = eLookupTypeAddress;
3902 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3903 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3904 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003905 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003906
Greg Claytoneffe5c92011-05-03 22:09:39 +00003907 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003908 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003909 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003910 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003911 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003912
Greg Claytoneffe5c92011-05-03 22:09:39 +00003913 case 's':
3914 m_str = option_arg;
3915 m_type = eLookupTypeSymbol;
3916 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003917
Greg Claytoneffe5c92011-05-03 22:09:39 +00003918 case 'f':
3919 m_file.SetFile (option_arg, false);
3920 m_type = eLookupTypeFileLine;
3921 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003922
Greg Claytoneffe5c92011-05-03 22:09:39 +00003923 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003924 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003925 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003926
Greg Claytoneffe5c92011-05-03 22:09:39 +00003927 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003928 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003929 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003930 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003931 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003932 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003933 m_type = eLookupTypeFileLine;
3934 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003935
Greg Claytonc4a8a762012-05-15 18:43:44 +00003936 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003937 m_str = option_arg;
3938 m_type = eLookupTypeFunction;
3939 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003940
Greg Claytonc4a8a762012-05-15 18:43:44 +00003941 case 'n':
3942 m_str = option_arg;
3943 m_type = eLookupTypeFunctionOrSymbol;
3944 break;
3945
Greg Claytoneffe5c92011-05-03 22:09:39 +00003946 case 't':
3947 m_str = option_arg;
3948 m_type = eLookupTypeType;
3949 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003950
Greg Claytoneffe5c92011-05-03 22:09:39 +00003951 case 'v':
3952 m_verbose = 1;
3953 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003954
Sean Callanand38b4a92012-06-06 20:49:55 +00003955 case 'A':
3956 m_print_all = true;
3957 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003958
Greg Claytoneffe5c92011-05-03 22:09:39 +00003959 case 'r':
3960 m_use_regex = true;
3961 break;
3962 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003963
Greg Claytoneffe5c92011-05-03 22:09:39 +00003964 return error;
3965 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003966
Greg Claytoneffe5c92011-05-03 22:09:39 +00003967 void
3968 OptionParsingStarting ()
3969 {
3970 m_type = eLookupTypeInvalid;
3971 m_str.clear();
3972 m_file.Clear();
3973 m_addr = LLDB_INVALID_ADDRESS;
3974 m_offset = 0;
3975 m_line_number = 0;
3976 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003977 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003978 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003979 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003980 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003981
Greg Claytoneffe5c92011-05-03 22:09:39 +00003982 const OptionDefinition*
3983 GetDefinitions ()
3984 {
3985 return g_option_table;
3986 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003987
Greg Claytoneffe5c92011-05-03 22:09:39 +00003988 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003989
Greg Claytoneffe5c92011-05-03 22:09:39 +00003990 static OptionDefinition g_option_table[];
3991 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3992 std::string m_str; // Holds name lookup
3993 FileSpec m_file; // Files for file lookups
3994 lldb::addr_t m_addr; // Holds the address to lookup
3995 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3996 uint32_t m_line_number; // Line number for file+line lookups
3997 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00003998 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003999 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00004000 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00004001 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004002
Greg Claytoneffe5c92011-05-03 22:09:39 +00004003 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004004 CommandObjectParsed (interpreter,
4005 "target modules lookup",
4006 "Look up information within executable and dependent shared library images.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00004007 NULL,
4008 eFlagRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00004009 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004010 {
4011 CommandArgumentEntry arg;
4012 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004013
Greg Claytoneffe5c92011-05-03 22:09:39 +00004014 // Define the first (and only) variant of this arg.
4015 file_arg.arg_type = eArgTypeFilename;
4016 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004017
Greg Claytoneffe5c92011-05-03 22:09:39 +00004018 // There is only one variant this argument could be; put it into the argument entry.
4019 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004020
Greg Claytoneffe5c92011-05-03 22:09:39 +00004021 // Push the data for the first argument into the m_arguments vector.
4022 m_arguments.push_back (arg);
4023 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004024
Greg Claytoneffe5c92011-05-03 22:09:39 +00004025 virtual
4026 ~CommandObjectTargetModulesLookup ()
4027 {
4028 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004029
Greg Claytoneffe5c92011-05-03 22:09:39 +00004030 virtual Options *
4031 GetOptions ()
4032 {
4033 return &m_options;
4034 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004035
Sean Callanand38b4a92012-06-06 20:49:55 +00004036 bool
4037 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4038 {
4039 switch (m_options.m_type)
4040 {
4041 case eLookupTypeAddress:
4042 case eLookupTypeFileLine:
4043 case eLookupTypeFunction:
4044 case eLookupTypeFunctionOrSymbol:
4045 case eLookupTypeSymbol:
4046 default:
4047 return false;
4048 case eLookupTypeType:
4049 break;
4050 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004051
Jason Molendab57e4a12013-11-04 09:33:30 +00004052 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004053
Sean Callanand38b4a92012-06-06 20:49:55 +00004054 if (!frame)
4055 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004056
Sean Callanand38b4a92012-06-06 20:49:55 +00004057 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004058
Sean Callanand38b4a92012-06-06 20:49:55 +00004059 if (!sym_ctx.module_sp)
4060 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004061
Sean Callanand38b4a92012-06-06 20:49:55 +00004062 switch (m_options.m_type)
4063 {
4064 default:
4065 return false;
4066 case eLookupTypeType:
4067 if (!m_options.m_str.empty())
4068 {
4069 if (LookupTypeHere (m_interpreter,
4070 result.GetOutputStream(),
4071 sym_ctx,
4072 m_options.m_str.c_str(),
4073 m_options.m_use_regex))
4074 {
4075 result.SetStatus(eReturnStatusSuccessFinishResult);
4076 return true;
4077 }
4078 }
4079 break;
4080 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004081
Sean Callanand38b4a92012-06-06 20:49:55 +00004082 return true;
4083 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004084
Greg Claytoneffe5c92011-05-03 22:09:39 +00004085 bool
4086 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4087 {
4088 switch (m_options.m_type)
4089 {
4090 case eLookupTypeAddress:
4091 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4092 {
4093 if (LookupAddressInModule (m_interpreter,
4094 result.GetOutputStream(),
4095 module,
Greg Clayton2501e5e2015-01-15 02:59:20 +00004096 eSymbolContextEverything | (m_options.m_verbose ? eSymbolContextVariable : 0),
Greg Claytoneffe5c92011-05-03 22:09:39 +00004097 m_options.m_addr,
4098 m_options.m_offset,
4099 m_options.m_verbose))
4100 {
4101 result.SetStatus(eReturnStatusSuccessFinishResult);
4102 return true;
4103 }
4104 }
4105 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004106
Greg Claytoneffe5c92011-05-03 22:09:39 +00004107 case eLookupTypeSymbol:
4108 if (!m_options.m_str.empty())
4109 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004110 if (LookupSymbolInModule (m_interpreter,
4111 result.GetOutputStream(),
4112 module,
4113 m_options.m_str.c_str(),
4114 m_options.m_use_regex,
4115 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004116 {
4117 result.SetStatus(eReturnStatusSuccessFinishResult);
4118 return true;
4119 }
4120 }
4121 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004122
Greg Claytoneffe5c92011-05-03 22:09:39 +00004123 case eLookupTypeFileLine:
4124 if (m_options.m_file)
4125 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004126 if (LookupFileAndLineInModule (m_interpreter,
4127 result.GetOutputStream(),
4128 module,
4129 m_options.m_file,
4130 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004131 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004132 m_options.m_verbose))
4133 {
4134 result.SetStatus(eReturnStatusSuccessFinishResult);
4135 return true;
4136 }
4137 }
4138 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004139
4140 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004141 case eLookupTypeFunction:
4142 if (!m_options.m_str.empty())
4143 {
4144 if (LookupFunctionInModule (m_interpreter,
4145 result.GetOutputStream(),
4146 module,
4147 m_options.m_str.c_str(),
4148 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004149 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004150 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004151 m_options.m_verbose))
4152 {
4153 result.SetStatus(eReturnStatusSuccessFinishResult);
4154 return true;
4155 }
4156 }
4157 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004158
Greg Claytoneffe5c92011-05-03 22:09:39 +00004159 case eLookupTypeType:
4160 if (!m_options.m_str.empty())
4161 {
4162 if (LookupTypeInModule (m_interpreter,
4163 result.GetOutputStream(),
4164 module,
4165 m_options.m_str.c_str(),
4166 m_options.m_use_regex))
4167 {
4168 result.SetStatus(eReturnStatusSuccessFinishResult);
4169 return true;
4170 }
4171 }
4172 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004173
Greg Claytoneffe5c92011-05-03 22:09:39 +00004174 default:
4175 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4176 syntax_error = true;
4177 break;
4178 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004179
Greg Claytoneffe5c92011-05-03 22:09:39 +00004180 result.SetStatus (eReturnStatusFailed);
4181 return false;
4182 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004183
Jim Ingham5a988412012-06-08 21:56:10 +00004184protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004185 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004186 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004187 CommandReturnObject &result)
4188 {
4189 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4190 if (target == NULL)
4191 {
4192 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4193 result.SetStatus (eReturnStatusFailed);
4194 return false;
4195 }
4196 else
4197 {
4198 bool syntax_error = false;
4199 uint32_t i;
4200 uint32_t num_successful_lookups = 0;
4201 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4202 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4203 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4204 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004205
Greg Claytoneffe5c92011-05-03 22:09:39 +00004206 if (command.GetArgumentCount() == 0)
4207 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004208 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004209
Sean Callanand38b4a92012-06-06 20:49:55 +00004210 // Where it is possible to look in the current symbol context
4211 // first, try that. If this search was successful and --all
4212 // was not passed, don't print anything else.
4213 if (LookupHere (m_interpreter, result, syntax_error))
4214 {
4215 result.GetOutputStream().EOL();
4216 num_successful_lookups++;
4217 if (!m_options.m_print_all)
4218 {
4219 result.SetStatus (eReturnStatusSuccessFinishResult);
4220 return result.Succeeded();
4221 }
4222 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004223
Sean Callanand38b4a92012-06-06 20:49:55 +00004224 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004225
Enrico Granata17598482012-11-08 02:22:02 +00004226 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004227 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004228 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004229 if (num_modules > 0)
4230 {
4231 for (i = 0; i<num_modules && syntax_error == false; ++i)
4232 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004233 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004234
Sean Callanand38b4a92012-06-06 20:49:55 +00004235 if (module_pointer != current_module.get() &&
4236 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004237 {
4238 result.GetOutputStream().EOL();
4239 num_successful_lookups++;
4240 }
4241 }
4242 }
4243 else
4244 {
4245 result.AppendError ("the target has no associated executable images");
4246 result.SetStatus (eReturnStatusFailed);
4247 return false;
4248 }
4249 }
4250 else
4251 {
4252 // Dump specified images (by basename or fullpath)
4253 const char *arg_cstr;
4254 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4255 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004256 ModuleList module_list;
4257 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4258 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004259 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004260 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004261 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004262 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004263 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004264 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004265 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004266 {
4267 result.GetOutputStream().EOL();
4268 num_successful_lookups++;
4269 }
4270 }
4271 }
4272 }
4273 else
4274 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4275 }
4276 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004277
Greg Claytoneffe5c92011-05-03 22:09:39 +00004278 if (num_successful_lookups > 0)
4279 result.SetStatus (eReturnStatusSuccessFinishResult);
4280 else
4281 result.SetStatus (eReturnStatusFailed);
4282 }
4283 return result.Succeeded();
4284 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004285
Greg Claytoneffe5c92011-05-03 22:09:39 +00004286 CommandOptions m_options;
4287};
4288
4289OptionDefinition
4290CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4291{
Zachary Turnerd37221d2014-07-09 16:31:49 +00004292 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4293 { 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 +00004294 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4295 /* 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 +00004296 false, "regex", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4297 { 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."},
4298 { 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."},
4299 { 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 +00004300 { LLDB_OPT_SET_FROM_TO(3,5),
Zachary Turnerd37221d2014-07-09 16:31:49 +00004301 false, "no-inlines", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4302 { 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."},
4303 { 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."},
4304 { 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."},
4305 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4306 { 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."},
4307 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004308};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004309
4310
Jim Ingham9575d842011-03-11 03:53:59 +00004311#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004312
4313//-------------------------------------------------------------------------
4314// CommandObjectMultiwordImageSearchPaths
4315//-------------------------------------------------------------------------
4316
Greg Claytoneffe5c92011-05-03 22:09:39 +00004317class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004318{
4319public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004320 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4321 CommandObjectMultiword (interpreter,
4322 "target modules search-paths",
4323 "A set of commands for operating on debugger target image search paths.",
4324 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004325 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004326 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4327 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4328 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4329 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4330 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004331 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004332
Greg Claytoneffe5c92011-05-03 22:09:39 +00004333 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004334 {
4335 }
4336};
4337
Greg Claytoneffe5c92011-05-03 22:09:39 +00004338
4339
4340#pragma mark CommandObjectTargetModules
4341
4342//-------------------------------------------------------------------------
4343// CommandObjectTargetModules
4344//-------------------------------------------------------------------------
4345
4346class CommandObjectTargetModules : public CommandObjectMultiword
4347{
4348public:
4349 //------------------------------------------------------------------
4350 // Constructors and Destructors
4351 //------------------------------------------------------------------
4352 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4353 CommandObjectMultiword (interpreter,
4354 "target modules",
4355 "A set of commands for accessing information for one or more target modules.",
4356 "target modules <sub-command> ...")
4357 {
4358 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4359 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004360 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4361 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4362 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4363 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004364 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004365
4366 }
4367 virtual
4368 ~CommandObjectTargetModules()
4369 {
4370 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004371
Greg Claytoneffe5c92011-05-03 22:09:39 +00004372private:
4373 //------------------------------------------------------------------
4374 // For CommandObjectTargetModules only
4375 //------------------------------------------------------------------
4376 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4377};
4378
4379
Greg Claytone72dfb32012-02-24 01:59:29 +00004380
Jim Ingham5a988412012-06-08 21:56:10 +00004381class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004382{
4383public:
4384 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004385 CommandObjectParsed (interpreter,
4386 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004387 "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 +00004388 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004389 m_option_group (interpreter),
4390 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4391 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4392
Greg Claytone72dfb32012-02-24 01:59:29 +00004393 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004394 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4395 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4396 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4397 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004398 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004399
Greg Claytone72dfb32012-02-24 01:59:29 +00004400 virtual
4401 ~CommandObjectTargetSymbolsAdd ()
4402 {
4403 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004404
Greg Claytonc7bece562013-01-25 18:06:21 +00004405 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00004406 HandleArgumentCompletion (Args &input,
4407 int &cursor_index,
4408 int &cursor_char_position,
4409 OptionElementVector &opt_element_vector,
4410 int match_start_point,
4411 int max_return_elements,
4412 bool &word_complete,
4413 StringList &matches)
4414 {
4415 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4416 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004417
Jim Ingham5a988412012-06-08 21:56:10 +00004418 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4419 CommandCompletions::eDiskFileCompletion,
4420 completion_str.c_str(),
4421 match_start_point,
4422 max_return_elements,
4423 NULL,
4424 word_complete,
4425 matches);
4426 return matches.GetSize();
4427 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004428
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004429 virtual Options *
4430 GetOptions ()
4431 {
4432 return &m_option_group;
4433 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004434
Jim Ingham5a988412012-06-08 21:56:10 +00004435protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004436 bool
4437 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004438 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004439 bool &flush,
4440 CommandReturnObject &result)
4441 {
Greg Clayton89deb062012-12-12 01:15:30 +00004442 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4443 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004444 {
4445 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004446 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004447
Greg Clayton89deb062012-12-12 01:15:30 +00004448 if (!module_spec.GetUUID().IsValid())
4449 {
4450 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4451 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4452 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004453 // We now have a module that represents a symbol file
4454 // that can be used for a module that might exist in the
4455 // current target, so we need to find that module in the
4456 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004457 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004458
Greg Clayton9aae0a12013-05-15 19:52:08 +00004459 size_t num_matches = 0;
4460 // First extract all module specs from the symbol file
4461 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004462 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004463 {
4464 // Now extract the module spec that matches the target architecture
4465 ModuleSpec target_arch_module_spec;
4466 ModuleSpec symfile_module_spec;
4467 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4468 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4469 {
4470 // See if it has a UUID?
4471 if (symfile_module_spec.GetUUID().IsValid())
4472 {
4473 // It has a UUID, look for this UUID in the target modules
4474 ModuleSpec symfile_uuid_module_spec;
4475 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4476 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4477 }
4478 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004479
Greg Clayton9aae0a12013-05-15 19:52:08 +00004480 if (num_matches == 0)
4481 {
4482 // No matches yet, iterate through the module specs to find a UUID value that
4483 // we can match up to an image in our target
4484 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4485 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4486 {
4487 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4488 {
4489 if (symfile_module_spec.GetUUID().IsValid())
4490 {
4491 // It has a UUID, look for this UUID in the target modules
4492 ModuleSpec symfile_uuid_module_spec;
4493 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4494 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004495 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004496 }
4497 }
4498 }
4499 }
4500
4501 // Just try to match up the file by basename if we have no matches at this point
4502 if (num_matches == 0)
4503 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004504
Greg Clayton91c0e742013-01-11 23:44:27 +00004505 while (num_matches == 0)
4506 {
4507 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4508 // Empty string returned, lets bail
4509 if (!filename_no_extension)
4510 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004511
Greg Clayton91c0e742013-01-11 23:44:27 +00004512 // Check if there was no extension to strip and the basename is the same
4513 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4514 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004515
Greg Clayton91c0e742013-01-11 23:44:27 +00004516 // Replace basename with one less extension
4517 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004518
Greg Clayton91c0e742013-01-11 23:44:27 +00004519 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4520 }
4521
Greg Clayton89deb062012-12-12 01:15:30 +00004522 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004523 {
Greg Clayton89deb062012-12-12 01:15:30 +00004524 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4525 }
4526 else if (num_matches == 1)
4527 {
4528 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004529
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004530 // The module has not yet created its symbol vendor, we can just
4531 // give the existing target module the symfile path to use for
4532 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004533 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004534
Greg Clayton136dff82012-12-14 02:15:00 +00004535 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004536 if (symbol_vendor)
4537 {
4538 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004539
Greg Clayton89deb062012-12-12 01:15:30 +00004540 if (symbol_file)
4541 {
4542 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004543
Greg Clayton89deb062012-12-12 01:15:30 +00004544 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4545 {
4546 // Provide feedback that the symfile has been successfully added.
4547 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004548 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004549 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004550 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004551
Greg Clayton89deb062012-12-12 01:15:30 +00004552 // Let clients know something changed in the module
4553 // if it is currently loaded
4554 ModuleList module_list;
4555 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004556 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004557
Greg Clayton91c0e742013-01-11 23:44:27 +00004558 // Make sure we load any scripting resources that may be embedded
4559 // in the debug info files in case the platform supports that.
4560 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004561 StreamString feedback_stream;
4562 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004563 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004564 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4565 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4566 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004567 else if (feedback_stream.GetSize())
4568 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004569
Greg Clayton89deb062012-12-12 01:15:30 +00004570 flush = true;
4571 result.SetStatus (eReturnStatusSuccessFinishResult);
4572 return true;
4573 }
4574 }
4575 }
4576 // Clear the symbol file spec if anything went wrong
4577 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004578 }
4579
4580 if (module_spec.GetUUID().IsValid())
4581 {
4582 StreamString ss_symfile_uuid;
4583 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4584 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4585 symfile_path,
4586 ss_symfile_uuid.GetData(),
4587 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4588 ? "\n please specify the full path to the symbol file"
4589 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004590 }
4591 else
4592 {
Greg Clayton89deb062012-12-12 01:15:30 +00004593 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4594 symfile_path,
4595 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4596 ? "\n please specify the full path to the symbol file"
4597 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004598 }
4599 }
4600 else
4601 {
4602 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004603 }
Greg Clayton89deb062012-12-12 01:15:30 +00004604 result.SetStatus (eReturnStatusFailed);
4605 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004606 }
4607
Greg Claytone72dfb32012-02-24 01:59:29 +00004608 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004609 DoExecute (Args& args,
Greg Claytone72dfb32012-02-24 01:59:29 +00004610 CommandReturnObject &result)
4611 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004612 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004613 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004614 bool flush = false;
4615 ModuleSpec module_spec;
4616 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4617 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4618 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004619
Greg Claytonf9fc6092013-01-09 19:44:40 +00004620 const size_t argc = args.GetArgumentCount();
4621 if (argc == 0)
4622 {
4623 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004624 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004625 bool success = false;
4626 bool error_set = false;
4627 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004628 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004629 Process *process = m_exe_ctx.GetProcessPtr();
4630 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004631 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004632 const StateType process_state = process->GetState();
4633 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004634 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004635 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004636 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004637 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004638 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4639 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004640 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004641 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004642 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004643 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4644 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004645 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004646 module_spec.GetUUID() = frame_module_sp->GetUUID();
4647 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004648 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004649 else
4650 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004651 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004652 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004653 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004654 }
4655 else
4656 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004657 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004658 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004659 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004660 }
4661 else
4662 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004663 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004664 error_set = true;
4665 }
4666 }
4667 else
4668 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004669 result.AppendError ("a process must exist in order to use the --frame option");
4670 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004671 }
4672 }
4673 else
4674 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004675 if (uuid_option_set)
4676 {
4677 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4678 success |= module_spec.GetUUID().IsValid();
4679 }
4680 else if (file_option_set)
4681 {
4682 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4683 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4684 if (module_sp)
4685 {
4686 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4687 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4688 module_spec.GetUUID() = module_sp->GetUUID();
4689 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4690 }
4691 else
4692 {
4693 module_spec.GetArchitecture() = target->GetArchitecture();
4694 }
4695 success |= module_spec.GetFileSpec().Exists();
4696 }
4697 }
4698
4699 if (success)
4700 {
4701 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4702 {
4703 if (module_spec.GetSymbolFileSpec())
4704 success = AddModuleSymbols (target, module_spec, flush, result);
4705 }
4706 }
4707
4708 if (!success && !error_set)
4709 {
4710 StreamString error_strm;
4711 if (uuid_option_set)
4712 {
4713 error_strm.PutCString("unable to find debug symbols for UUID ");
4714 module_spec.GetUUID().Dump (&error_strm);
4715 }
4716 else if (file_option_set)
4717 {
4718 error_strm.PutCString("unable to find debug symbols for the executable file ");
4719 error_strm << module_spec.GetFileSpec();
4720 }
4721 else if (frame_option_set)
4722 {
4723 error_strm.PutCString("unable to find debug symbols for the current frame");
4724 }
4725 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004726 }
4727 }
4728 else
4729 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004730 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4731 }
4732 }
4733 else
4734 {
4735 if (uuid_option_set)
4736 {
4737 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4738 }
4739 else if (file_option_set)
4740 {
4741 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4742 }
4743 else if (frame_option_set)
4744 {
4745 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4746 }
4747 else
4748 {
4749 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004750
Greg Claytonf9fc6092013-01-09 19:44:40 +00004751 for (size_t i=0; i<argc; ++i)
4752 {
4753 const char *symfile_path = args.GetArgumentAtIndex(i);
4754 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004755 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004756 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4757 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004758 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004759 FileSpec symfile_spec;
4760 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4761 module_spec.GetSymbolFileSpec() = symfile_spec;
4762 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004763
Greg Claytonf9fc6092013-01-09 19:44:40 +00004764 ArchSpec arch;
4765 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004766
Greg Claytonf9fc6092013-01-09 19:44:40 +00004767 if (symfile_exists)
4768 {
4769 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004770 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004771 }
4772 else
4773 {
4774 char resolved_symfile_path[PATH_MAX];
4775 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4776 {
4777 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4778 {
4779 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4780 break;
4781 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004782 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004783 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4784 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004785 }
4786 }
4787 }
4788 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004789 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004790
Greg Claytonf9fc6092013-01-09 19:44:40 +00004791 if (flush)
4792 {
4793 Process *process = m_exe_ctx.GetProcessPtr();
4794 if (process)
4795 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004796 }
4797 return result.Succeeded();
4798 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004799
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004800 OptionGroupOptions m_option_group;
4801 OptionGroupUUID m_uuid_option_group;
4802 OptionGroupFile m_file_option;
4803 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004804};
4805
4806
4807#pragma mark CommandObjectTargetSymbols
4808
4809//-------------------------------------------------------------------------
4810// CommandObjectTargetSymbols
4811//-------------------------------------------------------------------------
4812
4813class CommandObjectTargetSymbols : public CommandObjectMultiword
4814{
4815public:
4816 //------------------------------------------------------------------
4817 // Constructors and Destructors
4818 //------------------------------------------------------------------
4819 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4820 CommandObjectMultiword (interpreter,
4821 "target symbols",
4822 "A set of commands for adding and managing debug symbol files.",
4823 "target symbols <sub-command> ...")
4824 {
4825 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004826
Greg Claytone72dfb32012-02-24 01:59:29 +00004827 }
4828 virtual
4829 ~CommandObjectTargetSymbols()
4830 {
4831 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004832
Greg Claytone72dfb32012-02-24 01:59:29 +00004833private:
4834 //------------------------------------------------------------------
4835 // For CommandObjectTargetModules only
4836 //------------------------------------------------------------------
4837 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4838};
4839
4840
Jim Ingham9575d842011-03-11 03:53:59 +00004841#pragma mark CommandObjectTargetStopHookAdd
4842
4843//-------------------------------------------------------------------------
4844// CommandObjectTargetStopHookAdd
4845//-------------------------------------------------------------------------
4846
Greg Clayton44d93782014-01-27 23:43:24 +00004847class CommandObjectTargetStopHookAdd :
4848 public CommandObjectParsed,
4849 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004850{
4851public:
4852
4853 class CommandOptions : public Options
4854 {
4855 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004856 CommandOptions (CommandInterpreter &interpreter) :
4857 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004858 m_line_start(0),
4859 m_line_end (UINT_MAX),
4860 m_func_name_type_mask (eFunctionNameTypeAuto),
4861 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004862 m_thread_specified (false),
4863 m_use_one_liner (false),
4864 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004865 {
4866 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004867
Jim Ingham9575d842011-03-11 03:53:59 +00004868 ~CommandOptions () {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004869
Greg Claytone0d378b2011-03-24 21:19:54 +00004870 const OptionDefinition*
Jim Ingham9575d842011-03-11 03:53:59 +00004871 GetDefinitions ()
4872 {
4873 return g_option_table;
4874 }
4875
4876 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +00004877 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham9575d842011-03-11 03:53:59 +00004878 {
4879 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004880 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004881 bool success;
4882
4883 switch (short_option)
4884 {
4885 case 'c':
4886 m_class_name = option_arg;
4887 m_sym_ctx_specified = true;
4888 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004889
Jim Ingham9575d842011-03-11 03:53:59 +00004890 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004891 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004892 if (!success)
4893 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004894 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004895 break;
4896 }
4897 m_sym_ctx_specified = true;
4898 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004899
Jim Ingham9575d842011-03-11 03:53:59 +00004900 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004901 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004902 if (!success)
4903 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004904 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004905 break;
4906 }
4907 m_sym_ctx_specified = true;
4908 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004909
Sean Callanand4a7c122012-02-11 01:22:21 +00004910 case 'i':
4911 m_no_inlines = true;
4912 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004913
Jim Ingham9575d842011-03-11 03:53:59 +00004914 case 'n':
4915 m_function_name = option_arg;
4916 m_func_name_type_mask |= eFunctionNameTypeAuto;
4917 m_sym_ctx_specified = true;
4918 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004919
Jim Ingham9575d842011-03-11 03:53:59 +00004920 case 'f':
4921 m_file_name = option_arg;
4922 m_sym_ctx_specified = true;
4923 break;
4924 case 's':
4925 m_module_name = option_arg;
4926 m_sym_ctx_specified = true;
4927 break;
4928 case 't' :
4929 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004930 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004931 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004932 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004933 m_thread_specified = true;
4934 }
4935 break;
4936 case 'T':
4937 m_thread_name = option_arg;
4938 m_thread_specified = true;
4939 break;
4940 case 'q':
4941 m_queue_name = option_arg;
4942 m_thread_specified = true;
4943 break;
4944 case 'x':
4945 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004946 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004947 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004948 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004949 m_thread_specified = true;
4950 }
4951 break;
Johnny Chenb1372c02011-05-02 23:47:55 +00004952 case 'o':
4953 m_use_one_liner = true;
4954 m_one_liner = option_arg;
4955 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004956 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004957 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Ingham9575d842011-03-11 03:53:59 +00004958 break;
4959 }
4960 return error;
4961 }
4962
4963 void
Greg Claytonf6b8b582011-04-13 00:18:08 +00004964 OptionParsingStarting ()
Jim Ingham9575d842011-03-11 03:53:59 +00004965 {
4966 m_class_name.clear();
4967 m_function_name.clear();
4968 m_line_start = 0;
4969 m_line_end = UINT_MAX;
4970 m_file_name.clear();
4971 m_module_name.clear();
4972 m_func_name_type_mask = eFunctionNameTypeAuto;
4973 m_thread_id = LLDB_INVALID_THREAD_ID;
4974 m_thread_index = UINT32_MAX;
4975 m_thread_name.clear();
4976 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004977
Sean Callanand4a7c122012-02-11 01:22:21 +00004978 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004979 m_sym_ctx_specified = false;
4980 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004981
4982 m_use_one_liner = false;
4983 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004984 }
4985
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004986
Greg Claytone0d378b2011-03-24 21:19:54 +00004987 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004988
Jim Ingham9575d842011-03-11 03:53:59 +00004989 std::string m_class_name;
4990 std::string m_function_name;
4991 uint32_t m_line_start;
4992 uint32_t m_line_end;
4993 std::string m_file_name;
4994 std::string m_module_name;
4995 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4996 lldb::tid_t m_thread_id;
4997 uint32_t m_thread_index;
4998 std::string m_thread_name;
4999 std::string m_queue_name;
5000 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00005001 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00005002 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00005003 // Instance variables to hold the values for one_liner options.
5004 bool m_use_one_liner;
5005 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00005006 };
5007
5008 Options *
5009 GetOptions ()
5010 {
5011 return &m_options;
5012 }
5013
5014 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005015 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00005016 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005017 "Add a hook to be executed when the target stops.",
5018 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005019 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005020 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005021 {
5022 }
5023
5024 ~CommandObjectTargetStopHookAdd ()
5025 {
5026 }
5027
Jim Ingham5a988412012-06-08 21:56:10 +00005028protected:
Greg Clayton44d93782014-01-27 23:43:24 +00005029 virtual void
5030 IOHandlerActivated (IOHandler &io_handler)
5031 {
5032 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5033 if (output_sp)
5034 {
5035 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5036 output_sp->Flush();
5037 }
5038 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005039
Greg Clayton44d93782014-01-27 23:43:24 +00005040 virtual void
5041 IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
5042 {
5043 if (m_stop_hook_sp)
5044 {
5045 if (line.empty())
5046 {
5047 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5048 if (error_sp)
5049 {
5050 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5051 error_sp->Flush();
5052 }
5053 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5054 if (target)
5055 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5056 }
5057 else
5058 {
5059 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5060 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5061 if (output_sp)
5062 {
5063 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5064 output_sp->Flush();
5065 }
5066 }
5067 m_stop_hook_sp.reset();
5068 }
5069 io_handler.SetIsDone(true);
5070 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005071
Jim Ingham9575d842011-03-11 03:53:59 +00005072 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005073 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005074 {
Greg Clayton44d93782014-01-27 23:43:24 +00005075 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005076
Jim Ingham893c9322014-11-22 01:42:44 +00005077 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005078 if (target)
5079 {
Greg Clayton44d93782014-01-27 23:43:24 +00005080 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005081
5082 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005083 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005084 if (m_options.m_sym_ctx_specified)
5085 {
5086 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005087
Jim Ingham9575d842011-03-11 03:53:59 +00005088 if (!m_options.m_module_name.empty())
5089 {
5090 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5091 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005092
Jim Ingham9575d842011-03-11 03:53:59 +00005093 if (!m_options.m_class_name.empty())
5094 {
5095 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5096 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005097
Jim Ingham9575d842011-03-11 03:53:59 +00005098 if (!m_options.m_file_name.empty())
5099 {
5100 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5101 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005102
Jim Ingham9575d842011-03-11 03:53:59 +00005103 if (m_options.m_line_start != 0)
5104 {
5105 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5106 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005107
Jim Ingham9575d842011-03-11 03:53:59 +00005108 if (m_options.m_line_end != UINT_MAX)
5109 {
5110 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5111 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005112
Jim Ingham9575d842011-03-11 03:53:59 +00005113 if (!m_options.m_function_name.empty())
5114 {
5115 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5116 }
5117 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005118
Jim Ingham9575d842011-03-11 03:53:59 +00005119 if (specifier_ap.get())
5120 new_hook_sp->SetSpecifier (specifier_ap.release());
5121
5122 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005123
Jim Ingham9575d842011-03-11 03:53:59 +00005124 if (m_options.m_thread_specified)
5125 {
5126 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005127
Jim Ingham9575d842011-03-11 03:53:59 +00005128 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5129 {
5130 thread_spec->SetTID (m_options.m_thread_id);
5131 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005132
Jim Ingham9575d842011-03-11 03:53:59 +00005133 if (m_options.m_thread_index != UINT32_MAX)
5134 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005135
Jim Ingham9575d842011-03-11 03:53:59 +00005136 if (!m_options.m_thread_name.empty())
5137 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005138
Jim Ingham9575d842011-03-11 03:53:59 +00005139 if (!m_options.m_queue_name.empty())
5140 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005141
Jim Ingham9575d842011-03-11 03:53:59 +00005142 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005143
Jim Ingham9575d842011-03-11 03:53:59 +00005144 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005145 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005146 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005147 // Use one-liner.
5148 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005149 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005150 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005151 else
Jim Ingham9575d842011-03-11 03:53:59 +00005152 {
Greg Clayton44d93782014-01-27 23:43:24 +00005153 m_stop_hook_sp = new_hook_sp;
5154 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
5155 *this, // IOHandlerDelegate
5156 true, // Run IOHandler in async mode
5157 NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
5158
Jim Ingham9575d842011-03-11 03:53:59 +00005159 }
Jim Ingham9575d842011-03-11 03:53:59 +00005160 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5161 }
5162 else
5163 {
Greg Clayton7260f622011-04-18 08:33:37 +00005164 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005165 result.SetStatus (eReturnStatusFailed);
5166 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005167
Jim Ingham9575d842011-03-11 03:53:59 +00005168 return result.Succeeded();
5169 }
5170private:
5171 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005172 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005173};
5174
Greg Claytone0d378b2011-03-24 21:19:54 +00005175OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005176CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5177{
Zachary Turnerd37221d2014-07-09 16:31:49 +00005178 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005179 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005180 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005181 "Set the module within which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005182 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005183 "The stop hook is run only for the thread whose index matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005184 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005185 "The stop hook is run only for the thread whose TID matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005186 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005187 "The stop hook is run only for the thread whose thread name matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005188 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005189 "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 +00005190 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005191 "Specify the source file within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005192 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005193 "Set the start of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005194 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005195 "Set the end of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005196 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005197 "Specify the class within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005198 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005199 "Set the function name within which the stop hook will be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005200 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jim Ingham9575d842011-03-11 03:53:59 +00005201};
5202
5203#pragma mark CommandObjectTargetStopHookDelete
5204
5205//-------------------------------------------------------------------------
5206// CommandObjectTargetStopHookDelete
5207//-------------------------------------------------------------------------
5208
Jim Ingham5a988412012-06-08 21:56:10 +00005209class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005210{
5211public:
5212
5213 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005214 CommandObjectParsed (interpreter,
5215 "target stop-hook delete",
5216 "Delete a stop-hook.",
5217 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005218 {
5219 }
5220
5221 ~CommandObjectTargetStopHookDelete ()
5222 {
5223 }
5224
Jim Ingham5a988412012-06-08 21:56:10 +00005225protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005226 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005227 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005228 {
Jim Ingham893c9322014-11-22 01:42:44 +00005229 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005230 if (target)
5231 {
5232 // FIXME: see if we can use the breakpoint id style parser?
5233 size_t num_args = command.GetArgumentCount();
5234 if (num_args == 0)
5235 {
5236 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5237 {
5238 result.SetStatus (eReturnStatusFailed);
5239 return false;
5240 }
5241 else
5242 {
5243 target->RemoveAllStopHooks();
5244 }
5245 }
5246 else
5247 {
5248 bool success;
5249 for (size_t i = 0; i < num_args; i++)
5250 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005251 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005252 if (!success)
5253 {
Greg Clayton7260f622011-04-18 08:33:37 +00005254 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005255 result.SetStatus(eReturnStatusFailed);
5256 return false;
5257 }
5258 success = target->RemoveStopHookByID (user_id);
5259 if (!success)
5260 {
Greg Clayton7260f622011-04-18 08:33:37 +00005261 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005262 result.SetStatus(eReturnStatusFailed);
5263 return false;
5264 }
5265 }
5266 }
5267 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5268 }
5269 else
5270 {
Greg Clayton7260f622011-04-18 08:33:37 +00005271 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005272 result.SetStatus (eReturnStatusFailed);
5273 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005274
Jim Ingham9575d842011-03-11 03:53:59 +00005275 return result.Succeeded();
5276 }
5277};
5278#pragma mark CommandObjectTargetStopHookEnableDisable
5279
5280//-------------------------------------------------------------------------
5281// CommandObjectTargetStopHookEnableDisable
5282//-------------------------------------------------------------------------
5283
Jim Ingham5a988412012-06-08 21:56:10 +00005284class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005285{
5286public:
5287
5288 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005289 CommandObjectParsed (interpreter,
5290 name,
5291 help,
5292 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005293 m_enable (enable)
5294 {
5295 }
5296
5297 ~CommandObjectTargetStopHookEnableDisable ()
5298 {
5299 }
5300
Jim Ingham5a988412012-06-08 21:56:10 +00005301protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005302 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005303 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005304 {
Jim Ingham893c9322014-11-22 01:42:44 +00005305 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005306 if (target)
5307 {
5308 // FIXME: see if we can use the breakpoint id style parser?
5309 size_t num_args = command.GetArgumentCount();
5310 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005311
Jim Ingham9575d842011-03-11 03:53:59 +00005312 if (num_args == 0)
5313 {
5314 target->SetAllStopHooksActiveState (m_enable);
5315 }
5316 else
5317 {
5318 for (size_t i = 0; i < num_args; i++)
5319 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005320 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005321 if (!success)
5322 {
Greg Clayton7260f622011-04-18 08:33:37 +00005323 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005324 result.SetStatus(eReturnStatusFailed);
5325 return false;
5326 }
5327 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5328 if (!success)
5329 {
Greg Clayton7260f622011-04-18 08:33:37 +00005330 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005331 result.SetStatus(eReturnStatusFailed);
5332 return false;
5333 }
5334 }
5335 }
5336 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5337 }
5338 else
5339 {
Greg Clayton7260f622011-04-18 08:33:37 +00005340 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005341 result.SetStatus (eReturnStatusFailed);
5342 }
5343 return result.Succeeded();
5344 }
5345private:
5346 bool m_enable;
5347};
5348
5349#pragma mark CommandObjectTargetStopHookList
5350
5351//-------------------------------------------------------------------------
5352// CommandObjectTargetStopHookList
5353//-------------------------------------------------------------------------
5354
Jim Ingham5a988412012-06-08 21:56:10 +00005355class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005356{
5357public:
5358
5359 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005360 CommandObjectParsed (interpreter,
5361 "target stop-hook list",
5362 "List all stop-hooks.",
5363 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005364 {
5365 }
5366
5367 ~CommandObjectTargetStopHookList ()
5368 {
5369 }
5370
Jim Ingham5a988412012-06-08 21:56:10 +00005371protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005372 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005373 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005374 {
Jim Ingham893c9322014-11-22 01:42:44 +00005375 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005376 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005377 {
Greg Clayton7260f622011-04-18 08:33:37 +00005378 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005379 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005380 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005381 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005382
Jim Ingham9575d842011-03-11 03:53:59 +00005383 size_t num_hooks = target->GetNumStopHooks ();
5384 if (num_hooks == 0)
5385 {
5386 result.GetOutputStream().PutCString ("No stop hooks.\n");
5387 }
5388 else
5389 {
5390 for (size_t i = 0; i < num_hooks; i++)
5391 {
5392 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5393 if (i > 0)
5394 result.GetOutputStream().PutCString ("\n");
5395 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5396 }
5397 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005398 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005399 return result.Succeeded();
5400 }
5401};
5402
5403#pragma mark CommandObjectMultiwordTargetStopHooks
5404//-------------------------------------------------------------------------
5405// CommandObjectMultiwordTargetStopHooks
5406//-------------------------------------------------------------------------
5407
5408class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5409{
5410public:
5411
5412 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5413 CommandObjectMultiword (interpreter,
5414 "target stop-hook",
5415 "A set of commands for operating on debugger target stop-hooks.",
5416 "target stop-hook <subcommand> [<subcommand-options>]")
5417 {
5418 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5419 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5420 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5421 false,
5422 "target stop-hook disable [<id>]",
5423 "Disable a stop-hook.",
5424 "target stop-hook disable")));
5425 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5426 true,
5427 "target stop-hook enable [<id>]",
5428 "Enable a stop-hook.",
5429 "target stop-hook enable")));
5430 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5431 }
5432
5433 ~CommandObjectMultiwordTargetStopHooks()
5434 {
5435 }
5436};
5437
5438
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005439
5440#pragma mark CommandObjectMultiwordTarget
5441
5442//-------------------------------------------------------------------------
5443// CommandObjectMultiwordTarget
5444//-------------------------------------------------------------------------
5445
Greg Clayton66111032010-06-23 01:19:29 +00005446CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005447 CommandObjectMultiword (interpreter,
5448 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005449 "A set of commands for operating on debugger targets.",
5450 "target <subcommand> [<subcommand-options>]")
5451{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005452
Greg Clayton7260f622011-04-18 08:33:37 +00005453 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005454 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005455 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5456 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005457 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005458 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005459 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005460 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005461}
5462
5463CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5464{
5465}
5466
Greg Clayton7260f622011-04-18 08:33:37 +00005467