blob: e697645ccd1d3d4d6f1924338c89269959e14852 [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 Claytonc3776bf2012-02-09 06:16:32 +0000163 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true
Greg Clayton1c5f1862012-11-30 19:05:35 +0000164 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 +0000165 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 +0000166 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 +0000167 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 +0000168 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 +0000169 {
170 CommandArgumentEntry arg;
171 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000172
Greg Clayton7260f622011-04-18 08:33:37 +0000173 // Define the first (and only) variant of this arg.
174 file_arg.arg_type = eArgTypeFilename;
175 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000176
Greg Clayton7260f622011-04-18 08:33:37 +0000177 // There is only one variant this argument could be; put it into the argument entry.
178 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000179
Greg Clayton7260f622011-04-18 08:33:37 +0000180 // Push the data for the first argument into the m_arguments vector.
181 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000182
Greg Clayton644247c2011-07-07 01:59:51 +0000183 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000184 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000185 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000186 m_option_group.Append (&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000187 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Jim Inghambf22b962013-07-11 01:47:46 +0000188 m_option_group.Append (&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000189 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000190 m_option_group.Finalize();
191 }
192
193 ~CommandObjectTargetCreate ()
194 {
195 }
196
197 Options *
198 GetOptions ()
199 {
200 return &m_option_group;
201 }
202
Greg Claytonc7bece562013-01-25 18:06:21 +0000203 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +0000204 HandleArgumentCompletion (Args &input,
205 int &cursor_index,
206 int &cursor_char_position,
207 OptionElementVector &opt_element_vector,
208 int match_start_point,
209 int max_return_elements,
210 bool &word_complete,
211 StringList &matches)
212 {
213 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
214 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000215
Jim Ingham5a988412012-06-08 21:56:10 +0000216 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
217 CommandCompletions::eDiskFileCompletion,
218 completion_str.c_str(),
219 match_start_point,
220 max_return_elements,
221 NULL,
222 word_complete,
223 matches);
224 return matches.GetSize();
225 }
226
227protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000228 bool
Jim Ingham5a988412012-06-08 21:56:10 +0000229 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000230 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000231 const size_t argc = command.GetArgumentCount();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000232 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
Jim Inghambf22b962013-07-11 01:47:46 +0000233 FileSpec remote_file (m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000234
Greg Clayton5acc1252014-08-15 18:00:45 +0000235 if (core_file)
236 {
237 if (!core_file.Exists())
238 {
239 result.AppendErrorWithFormat("core file '%s' doesn't exist", core_file.GetPath().c_str());
240 result.SetStatus (eReturnStatusFailed);
241 return false;
242
243 }
244 if (!core_file.Readable())
245 {
246 result.AppendErrorWithFormat("core file '%s' is not readable", core_file.GetPath().c_str());
247 result.SetStatus (eReturnStatusFailed);
248 return false;
249 }
250 }
251
Daniel Maleae0f8f572013-08-26 23:57:52 +0000252 if (argc == 1 || core_file || remote_file)
Greg Clayton7260f622011-04-18 08:33:37 +0000253 {
Greg Clayton1c5f1862012-11-30 19:05:35 +0000254 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
255 if (symfile)
256 {
Greg Clayton5acc1252014-08-15 18:00:45 +0000257 if (symfile.Exists())
258 {
259 if (!symfile.Readable())
260 {
261 result.AppendErrorWithFormat("symbol file '%s' is not readable", core_file.GetPath().c_str());
262 result.SetStatus (eReturnStatusFailed);
263 return false;
264 }
265 }
266 else
Greg Clayton1c5f1862012-11-30 19:05:35 +0000267 {
268 char symfile_path[PATH_MAX];
269 symfile.GetPath(symfile_path, sizeof(symfile_path));
270 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
271 result.SetStatus (eReturnStatusFailed);
272 return false;
273 }
274 }
275
Greg Clayton7260f622011-04-18 08:33:37 +0000276 const char *file_path = command.GetArgumentAtIndex(0);
277 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000278 FileSpec file_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279
Daniel Maleae0f8f572013-08-26 23:57:52 +0000280 if (file_path)
281 file_spec.SetFile (file_path, true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282
Daniel Maleae0f8f572013-08-26 23:57:52 +0000283 bool must_set_platform_path = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000284
Greg Clayton7260f622011-04-18 08:33:37 +0000285 Debugger &debugger = m_interpreter.GetDebugger();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000286 PlatformSP platform_sp(debugger.GetPlatformList().GetSelectedPlatform ());
287
288 if (remote_file)
289 {
290 // I have a remote file.. two possible cases
291 if (file_spec && file_spec.Exists())
292 {
293 // if the remote file does not exist, push it there
294 if (!platform_sp->GetFileExists (remote_file))
295 {
296 Error err = platform_sp->PutFile(file_spec, remote_file);
297 if (err.Fail())
298 {
299 result.AppendError(err.AsCString());
300 result.SetStatus (eReturnStatusFailed);
301 return false;
302 }
303 }
304 }
305 else
306 {
307 // there is no local file and we need one
308 // in order to make the remote ---> local transfer we need a platform
309 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
310 if (!platform_sp)
311 {
312 result.AppendError("unable to perform remote debugging without a platform");
313 result.SetStatus (eReturnStatusFailed);
314 return false;
315 }
316 if (file_path)
317 {
318 // copy the remote file to the local file
319 Error err = platform_sp->GetFile(remote_file, file_spec);
320 if (err.Fail())
321 {
322 result.AppendError(err.AsCString());
323 result.SetStatus (eReturnStatusFailed);
324 return false;
325 }
326 }
327 else
328 {
329 // make up a local file
330 result.AppendError("remote --> local transfer without local path is not implemented yet");
331 result.SetStatus (eReturnStatusFailed);
332 return false;
333 }
334 }
335 }
336
337 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000338 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Clayton1c5f1862012-11-30 19:05:35 +0000339 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000340 Error error (debugger.GetTargetList().CreateTarget (debugger,
Daniel Maleae0f8f572013-08-26 23:57:52 +0000341// remote_file ? remote_file : file_spec,
Greg Claytona0ca6602012-10-18 16:33:33 +0000342 file_path,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000343 arch_cstr,
344 get_dependent_files,
345 &m_platform_options,
346 target_sp));
347
Greg Clayton7260f622011-04-18 08:33:37 +0000348 if (target_sp)
349 {
Jim Inghambf22b962013-07-11 01:47:46 +0000350 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000351 {
352 ModuleSP module_sp (target_sp->GetExecutableModule());
353 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000354 {
355 if (symfile)
356 module_sp->SetSymbolFileFileSpec(symfile);
357 if (remote_file)
358 {
359 std::string remote_path = remote_file.GetPath();
360 target_sp->SetArg0(remote_path.c_str());
361 module_sp->SetPlatformFileSpec(remote_file);
362 }
363 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000364 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000365
Greg Clayton7260f622011-04-18 08:33:37 +0000366 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000367 if (must_set_platform_path)
368 {
369 ModuleSpec main_module_spec(file_spec);
370 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
371 if (module_sp)
372 module_sp->SetPlatformFileSpec(remote_file);
373 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000374 if (core_file)
375 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000376 char core_path[PATH_MAX];
377 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000378 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000379 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000380 FileSpec core_file_dir;
381 core_file_dir.GetDirectory() = core_file.GetDirectory();
382 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000383
Greg Claytonc859e2d2012-02-13 23:10:39 +0000384 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
385
386 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000387 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000388 // Seems wierd that we Launch a core file, but that is
389 // what we do!
390 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000391
Greg Claytonc859e2d2012-02-13 23:10:39 +0000392 if (error.Fail())
393 {
394 result.AppendError(error.AsCString("can't find plug-in for core file"));
395 result.SetStatus (eReturnStatusFailed);
396 return false;
397 }
398 else
399 {
400 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
401 result.SetStatus (eReturnStatusSuccessFinishNoResult);
402 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000403 }
404 else
405 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000406 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
407 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000408 }
409 }
410 else
411 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000412 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000413 result.SetStatus (eReturnStatusFailed);
414 }
415 }
416 else
417 {
418 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
419 result.SetStatus (eReturnStatusSuccessFinishNoResult);
420 }
Greg Clayton7260f622011-04-18 08:33:37 +0000421 }
422 else
423 {
424 result.AppendError(error.AsCString());
425 result.SetStatus (eReturnStatusFailed);
426 }
427 }
428 else
429 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000430 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core-file option.\n", m_cmd_name.c_str());
Greg Clayton7260f622011-04-18 08:33:37 +0000431 result.SetStatus (eReturnStatusFailed);
432 }
433 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000434 }
435
Greg Clayton7260f622011-04-18 08:33:37 +0000436private:
437 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000438 OptionGroupArchitecture m_arch_option;
Greg Clayton7260f622011-04-18 08:33:37 +0000439 OptionGroupPlatform m_platform_options;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000440 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000441 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000442 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000443 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000444 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000445};
446
447#pragma mark CommandObjectTargetList
448
449//----------------------------------------------------------------------
450// "target list"
451//----------------------------------------------------------------------
452
Jim Ingham5a988412012-06-08 21:56:10 +0000453class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000454{
455public:
456 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000457 CommandObjectParsed (interpreter,
458 "target list",
459 "List all current targets in the current debug session.",
460 NULL,
461 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000462 {
463 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000464
Greg Clayton7260f622011-04-18 08:33:37 +0000465 virtual
466 ~CommandObjectTargetList ()
467 {
468 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000469
Jim Ingham5a988412012-06-08 21:56:10 +0000470protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000471 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000472 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000473 {
474 if (args.GetArgumentCount() == 0)
475 {
476 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000477
Greg Clayton7260f622011-04-18 08:33:37 +0000478 bool show_stopped_process_status = false;
479 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
480 {
481 strm.PutCString ("No targets.\n");
482 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000483 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000484 }
485 else
486 {
487 result.AppendError ("the 'target list' command takes no arguments\n");
488 result.SetStatus (eReturnStatusFailed);
489 }
490 return result.Succeeded();
491 }
492};
493
494
495#pragma mark CommandObjectTargetSelect
496
497//----------------------------------------------------------------------
498// "target select"
499//----------------------------------------------------------------------
500
Jim Ingham5a988412012-06-08 21:56:10 +0000501class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000502{
503public:
504 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000505 CommandObjectParsed (interpreter,
506 "target select",
507 "Select a target as the current target by target index.",
508 NULL,
509 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000510 {
511 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000512
Greg Clayton7260f622011-04-18 08:33:37 +0000513 virtual
514 ~CommandObjectTargetSelect ()
515 {
516 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000517
Jim Ingham5a988412012-06-08 21:56:10 +0000518protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000519 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000520 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000521 {
522 if (args.GetArgumentCount() == 1)
523 {
524 bool success = false;
525 const char *target_idx_arg = args.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000526 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton7260f622011-04-18 08:33:37 +0000527 if (success)
528 {
529 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
530 const uint32_t num_targets = target_list.GetNumTargets();
531 if (target_idx < num_targets)
532 {
533 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
534 if (target_sp)
535 {
536 Stream &strm = result.GetOutputStream();
537 target_list.SetSelectedTarget (target_sp.get());
538 bool show_stopped_process_status = false;
539 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000540 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000541 }
542 else
543 {
544 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
545 result.SetStatus (eReturnStatusFailed);
546 }
547 }
548 else
549 {
Todd Fiala352237d2014-08-29 20:14:21 +0000550 if (num_targets > 0)
551 {
552 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
553 target_idx,
554 num_targets - 1);
555 } else
556 {
557 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
558 target_idx);
559 }
Greg Clayton7260f622011-04-18 08:33:37 +0000560 result.SetStatus (eReturnStatusFailed);
561 }
562 }
563 else
564 {
565 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
566 result.SetStatus (eReturnStatusFailed);
567 }
568 }
569 else
570 {
571 result.AppendError ("'target select' takes a single argument: a target index\n");
572 result.SetStatus (eReturnStatusFailed);
573 }
574 return result.Succeeded();
575 }
576};
577
Greg Clayton3418c852011-08-10 02:10:13 +0000578#pragma mark CommandObjectTargetSelect
579
580//----------------------------------------------------------------------
581// "target delete"
582//----------------------------------------------------------------------
583
Jim Ingham5a988412012-06-08 21:56:10 +0000584class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000585{
586public:
587 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000588 CommandObjectParsed (interpreter,
589 "target delete",
590 "Delete one or more targets by target index.",
591 NULL,
592 0),
Greg Claytonaa149cb2011-08-11 02:48:45 +0000593 m_option_group (interpreter),
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000594 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 +0000595 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000596 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
597 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000598 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000599
Greg Clayton3418c852011-08-10 02:10:13 +0000600 virtual
601 ~CommandObjectTargetDelete ()
602 {
603 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000604
Jim Ingham5a988412012-06-08 21:56:10 +0000605 Options *
606 GetOptions ()
607 {
608 return &m_option_group;
609 }
610
611protected:
Greg Clayton3418c852011-08-10 02:10:13 +0000612 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000613 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton3418c852011-08-10 02:10:13 +0000614 {
615 const size_t argc = args.GetArgumentCount();
616 std::vector<TargetSP> delete_target_list;
617 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
618 bool success = true;
619 TargetSP target_sp;
620 if (argc > 0)
621 {
622 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000623 // Bail out if don't have any targets.
624 if (num_targets == 0) {
625 result.AppendError("no targets to delete");
626 result.SetStatus(eReturnStatusFailed);
627 success = false;
628 }
629
Greg Clayton3418c852011-08-10 02:10:13 +0000630 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
631 {
632 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
Vince Harron5275aaa2015-01-15 20:08:35 +0000633 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton3418c852011-08-10 02:10:13 +0000634 if (success)
635 {
636 if (target_idx < num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000637 {
Greg Clayton3418c852011-08-10 02:10:13 +0000638 target_sp = target_list.GetTargetAtIndex (target_idx);
639 if (target_sp)
640 {
641 delete_target_list.push_back (target_sp);
642 continue;
643 }
644 }
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000645 if (num_targets > 1)
646 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
647 target_idx,
648 num_targets - 1);
649 else
650 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
651 target_idx);
652
Greg Clayton3418c852011-08-10 02:10:13 +0000653 result.SetStatus (eReturnStatusFailed);
654 success = false;
655 }
656 else
657 {
658 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
659 result.SetStatus (eReturnStatusFailed);
660 success = false;
661 }
662 }
Greg Clayton3418c852011-08-10 02:10:13 +0000663 }
664 else
665 {
666 target_sp = target_list.GetSelectedTarget();
667 if (target_sp)
668 {
669 delete_target_list.push_back (target_sp);
670 }
671 else
672 {
673 result.AppendErrorWithFormat("no target is currently selected\n");
674 result.SetStatus (eReturnStatusFailed);
675 success = false;
676 }
677 }
678 if (success)
679 {
680 const size_t num_targets_to_delete = delete_target_list.size();
681 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
682 {
683 target_sp = delete_target_list[idx];
684 target_list.DeleteTarget(target_sp);
685 target_sp->Destroy();
686 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000687 // If "--clean" was specified, prune any orphaned shared modules from
688 // the global shared module list
689 if (m_cleanup_option.GetOptionValue ())
690 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000691 const bool mandatory = true;
692 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000693 }
Greg Clayton3418c852011-08-10 02:10:13 +0000694 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
695 result.SetStatus(eReturnStatusSuccessFinishResult);
696 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000697
Greg Clayton3418c852011-08-10 02:10:13 +0000698 return result.Succeeded();
699 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000700
Greg Claytonaa149cb2011-08-11 02:48:45 +0000701 OptionGroupOptions m_option_group;
702 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000703};
704
Greg Clayton7260f622011-04-18 08:33:37 +0000705
Greg Clayton644247c2011-07-07 01:59:51 +0000706#pragma mark CommandObjectTargetVariable
707
708//----------------------------------------------------------------------
709// "target variable"
710//----------------------------------------------------------------------
711
Jim Ingham5a988412012-06-08 21:56:10 +0000712class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000713{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000714 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
715 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
716
Greg Clayton644247c2011-07-07 01:59:51 +0000717public:
718 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000719 CommandObjectParsed (interpreter,
720 "target variable",
Greg Claytondfdd1eb2012-11-03 00:10:22 +0000721 "Read global variable(s) prior to, or while running your binary.",
Jim Ingham5a988412012-06-08 21:56:10 +0000722 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000723 eFlagRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000724 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000725 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000726 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000727 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
728 SHORT_OPTION_FILE, 0, eArgTypeFilename,
729 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
730 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
731 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
732 "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 +0000733 m_varobj_options()
734 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000735 CommandArgumentEntry arg;
736 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000737
Johnny Chen81ab3f52011-08-22 22:22:00 +0000738 // Define the first (and only) variant of this arg.
739 var_name_arg.arg_type = eArgTypeVarName;
740 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000741
Johnny Chen81ab3f52011-08-22 22:22:00 +0000742 // There is only one variant this argument could be; put it into the argument entry.
743 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000744
Johnny Chen81ab3f52011-08-22 22:22:00 +0000745 // Push the data for the first argument into the m_arguments vector.
746 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000747
Greg Clayton644247c2011-07-07 01:59:51 +0000748 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000749 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000750 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 +0000751 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
752 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
753 m_option_group.Finalize();
754 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000755
Greg Clayton644247c2011-07-07 01:59:51 +0000756 virtual
757 ~CommandObjectTargetVariable ()
758 {
759 }
Greg Clayton884fb692011-07-08 21:46:14 +0000760
761 void
762 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
763 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000764 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000765
Greg Clayton884fb692011-07-08 21:46:14 +0000766 switch (var_sp->GetScope())
767 {
768 case eValueTypeVariableGlobal:
769 if (m_option_variable.show_scope)
770 s.PutCString("GLOBAL: ");
771 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000772
Greg Clayton884fb692011-07-08 21:46:14 +0000773 case eValueTypeVariableStatic:
774 if (m_option_variable.show_scope)
775 s.PutCString("STATIC: ");
776 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000777
Greg Clayton884fb692011-07-08 21:46:14 +0000778 case eValueTypeVariableArgument:
779 if (m_option_variable.show_scope)
780 s.PutCString(" ARG: ");
781 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000782
Greg Clayton884fb692011-07-08 21:46:14 +0000783 case eValueTypeVariableLocal:
784 if (m_option_variable.show_scope)
785 s.PutCString(" LOCAL: ");
786 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000787
Greg Clayton884fb692011-07-08 21:46:14 +0000788 default:
789 break;
790 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000791
Greg Clayton45ba8542011-07-10 19:21:23 +0000792 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000793 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000794 bool show_fullpaths = false;
795 bool show_module = true;
796 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
797 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000798 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000799
Greg Clayton1deb7962011-10-25 06:44:01 +0000800 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000801 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000802 options.SetFormat(format);
803
804 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000805
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000806 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000807 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000808
Greg Claytonc7bece562013-01-25 18:06:21 +0000809 static size_t GetVariableCallback (void *baton,
810 const char *name,
811 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000812 {
813 Target *target = static_cast<Target *>(baton);
814 if (target)
815 {
816 return target->GetImages().FindGlobalVariables (ConstString(name),
817 true,
818 UINT32_MAX,
819 variable_list);
820 }
821 return 0;
822 }
Greg Clayton884fb692011-07-08 21:46:14 +0000823
Jim Ingham5a988412012-06-08 21:56:10 +0000824 Options *
825 GetOptions ()
826 {
827 return &m_option_group;
828 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000829
Jim Ingham5a988412012-06-08 21:56:10 +0000830protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000831 void
832 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
833 {
834 size_t count = variable_list.GetSize();
835 if (count > 0)
836 {
837 if (sc.module_sp)
838 {
839 if (sc.comp_unit)
840 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000841 s.Printf ("Global variables for %s in %s:\n",
842 sc.comp_unit->GetPath().c_str(),
843 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000844 }
845 else
846 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000847 s.Printf ("Global variables for %s\n",
848 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000849 }
850 }
851 else if (sc.comp_unit)
852 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000853 s.Printf ("Global variables for %s\n",
854 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000855 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000856
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000857 for (uint32_t i=0; i<count; ++i)
858 {
859 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
860 if (var_sp)
861 {
862 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000863
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000864 if (valobj_sp)
865 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
866 }
867 }
868 }
869
870 }
Greg Clayton644247c2011-07-07 01:59:51 +0000871 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000872 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton644247c2011-07-07 01:59:51 +0000873 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000874 Target *target = m_exe_ctx.GetTargetPtr();
875 const size_t argc = args.GetArgumentCount();
876 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000877
Greg Claytonf9fc6092013-01-09 19:44:40 +0000878 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000879 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000880
881 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000882 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000883 VariableList variable_list;
884 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000885
Greg Claytonf9fc6092013-01-09 19:44:40 +0000886 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000887 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000888 bool use_var_name = false;
889 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000890 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000891 RegularExpression regex(arg);
892 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000893 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000894 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000895 result.SetStatus (eReturnStatusFailed);
896 return false;
897 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000898 use_var_name = true;
899 matches = target->GetImages().FindGlobalVariables (regex,
900 true,
901 UINT32_MAX,
902 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000903 }
904 else
905 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000906 Error error (Variable::GetValuesForVariableExpressionPath (arg,
907 m_exe_ctx.GetBestExecutionContextScope(),
908 GetVariableCallback,
909 target,
910 variable_list,
911 valobj_list));
912 matches = variable_list.GetSize();
913 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000914
Greg Claytonf9fc6092013-01-09 19:44:40 +0000915 if (matches == 0)
916 {
917 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
918 result.SetStatus (eReturnStatusFailed);
919 return false;
920 }
921 else
922 {
923 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000924 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000925 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
926 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000927 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000928 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
929 if (!valobj_sp)
930 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000931
Greg Claytonf9fc6092013-01-09 19:44:40 +0000932 if (valobj_sp)
933 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000934 }
935 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000936 }
Greg Clayton644247c2011-07-07 01:59:51 +0000937 }
938 }
939 else
940 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000941 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
942 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
943 SymbolContextList sc_list;
944 const size_t num_compile_units = compile_units.GetSize();
945 const size_t num_shlibs = shlibs.GetSize();
946 if (num_compile_units == 0 && num_shlibs == 0)
947 {
948 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000949 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000950 CompileUnit *comp_unit = NULL;
951 if (frame)
952 {
953 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
954 if (sc.comp_unit)
955 {
956 const bool can_create = true;
957 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
958 if (comp_unit_varlist_sp)
959 {
960 size_t count = comp_unit_varlist_sp->GetSize();
961 if (count > 0)
962 {
963 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
964 success = true;
965 }
966 }
967 }
968 }
969 if (!success)
970 {
971 if (frame)
972 {
973 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000974 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
975 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000976 else
977 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000978 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000979 else
980 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
981 result.SetStatus (eReturnStatusFailed);
982 }
983 }
984 else
985 {
986 SymbolContextList sc_list;
987 const bool append = true;
988 // We have one or more compile unit or shlib
989 if (num_shlibs > 0)
990 {
991 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
992 {
993 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
994 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000995
Greg Claytonf9fc6092013-01-09 19:44:40 +0000996 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
997 if (module_sp)
998 {
999 if (num_compile_units > 0)
1000 {
1001 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1002 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1003 }
1004 else
1005 {
1006 SymbolContext sc;
1007 sc.module_sp = module_sp;
1008 sc_list.Append(sc);
1009 }
1010 }
1011 else
1012 {
1013 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001014 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1015 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001016 }
1017 }
1018 }
1019 else
1020 {
1021 // No shared libraries, we just want to find globals for the compile units files that were specified
1022 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1023 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1024 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001025
Greg Claytonf9fc6092013-01-09 19:44:40 +00001026 const uint32_t num_scs = sc_list.GetSize();
1027 if (num_scs > 0)
1028 {
1029 SymbolContext sc;
1030 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1031 {
1032 if (sc_list.GetContextAtIndex(sc_idx, sc))
1033 {
1034 if (sc.comp_unit)
1035 {
1036 const bool can_create = true;
1037 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1038 if (comp_unit_varlist_sp)
1039 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1040 }
1041 else if (sc.module_sp)
1042 {
1043 // Get all global variables for this module
1044 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1045 VariableList variable_list;
1046 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1047 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1048 }
1049 }
1050 }
1051 }
1052 }
Greg Clayton644247c2011-07-07 01:59:51 +00001053 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001054
Enrico Granata61a80ba2011-08-12 16:42:31 +00001055 if (m_interpreter.TruncationWarningNecessary())
1056 {
1057 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1058 m_cmd_name.c_str());
1059 m_interpreter.TruncationWarningGiven();
1060 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001061
Greg Clayton644247c2011-07-07 01:59:51 +00001062 return result.Succeeded();
1063 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001064
Greg Clayton644247c2011-07-07 01:59:51 +00001065 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001066 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001067 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001068 OptionGroupFileList m_option_compile_units;
1069 OptionGroupFileList m_option_shared_libraries;
1070 OptionGroupValueObjectDisplay m_varobj_options;
1071
1072};
1073
1074
Greg Claytoneffe5c92011-05-03 22:09:39 +00001075#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076
Jim Ingham5a988412012-06-08 21:56:10 +00001077class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078{
1079public:
1080
Greg Claytoneffe5c92011-05-03 22:09:39 +00001081 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001082 CommandObjectParsed (interpreter,
1083 "target modules search-paths add",
1084 "Add new image search paths substitution pairs to the current target.",
1085 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 {
Caroline Tice405fe672010-10-04 22:28:36 +00001087 CommandArgumentEntry arg;
1088 CommandArgumentData old_prefix_arg;
1089 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001090
Caroline Tice405fe672010-10-04 22:28:36 +00001091 // Define the first variant of this arg pair.
1092 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1093 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001094
Caroline Tice405fe672010-10-04 22:28:36 +00001095 // Define the first variant of this arg pair.
1096 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1097 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001098
Caroline Tice405fe672010-10-04 22:28:36 +00001099 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1100 // must always occur together, they are treated as two variants of one argument rather than two independent
1101 // arguments. Push them both into the first argument position for m_arguments...
1102
1103 arg.push_back (old_prefix_arg);
1104 arg.push_back (new_prefix_arg);
1105
1106 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 }
1108
Greg Claytoneffe5c92011-05-03 22:09:39 +00001109 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 {
1111 }
1112
Jim Ingham5a988412012-06-08 21:56:10 +00001113protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001115 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 CommandReturnObject &result)
1117 {
Greg Claytona7015092010-09-18 01:14:36 +00001118 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 if (target)
1120 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001121 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 if (argc & 1)
1123 {
Greg Clayton7260f622011-04-18 08:33:37 +00001124 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125 result.SetStatus (eReturnStatusFailed);
1126 }
1127 else
1128 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001129 for (size_t i=0; i<argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130 {
1131 const char *from = command.GetArgumentAtIndex(i);
1132 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (from[0] && to[0])
1135 {
1136 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001137 target->GetImageSearchPathList().Append (ConstString(from),
1138 ConstString(to),
1139 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001140 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 }
1142 else
1143 {
1144 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001145 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 else
Greg Clayton7260f622011-04-18 08:33:37 +00001147 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 result.SetStatus (eReturnStatusFailed);
1149 }
1150 }
1151 }
1152 }
1153 else
1154 {
Greg Clayton7260f622011-04-18 08:33:37 +00001155 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 result.SetStatus (eReturnStatusFailed);
1157 }
1158 return result.Succeeded();
1159 }
1160};
1161
Greg Claytoneffe5c92011-05-03 22:09:39 +00001162#pragma mark CommandObjectTargetModulesSearchPathsClear
1163
Jim Ingham5a988412012-06-08 21:56:10 +00001164class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165{
1166public:
1167
Greg Claytoneffe5c92011-05-03 22:09:39 +00001168 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001169 CommandObjectParsed (interpreter,
1170 "target modules search-paths clear",
1171 "Clear all current image search path substitution pairs from the current target.",
1172 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 {
1174 }
1175
Greg Claytoneffe5c92011-05-03 22:09:39 +00001176 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 {
1178 }
1179
Jim Ingham5a988412012-06-08 21:56:10 +00001180protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001182 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 CommandReturnObject &result)
1184 {
Greg Claytona7015092010-09-18 01:14:36 +00001185 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 if (target)
1187 {
1188 bool notify = true;
1189 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001190 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 }
1192 else
1193 {
Greg Clayton7260f622011-04-18 08:33:37 +00001194 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 result.SetStatus (eReturnStatusFailed);
1196 }
1197 return result.Succeeded();
1198 }
1199};
1200
Greg Claytoneffe5c92011-05-03 22:09:39 +00001201#pragma mark CommandObjectTargetModulesSearchPathsInsert
1202
Jim Ingham5a988412012-06-08 21:56:10 +00001203class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204{
1205public:
1206
Greg Claytoneffe5c92011-05-03 22:09:39 +00001207 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001208 CommandObjectParsed (interpreter,
1209 "target modules search-paths insert",
1210 "Insert a new image search path substitution pair into the current target at the specified index.",
1211 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 {
Caroline Tice405fe672010-10-04 22:28:36 +00001213 CommandArgumentEntry arg1;
1214 CommandArgumentEntry arg2;
1215 CommandArgumentData index_arg;
1216 CommandArgumentData old_prefix_arg;
1217 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001218
Caroline Tice405fe672010-10-04 22:28:36 +00001219 // Define the first and only variant of this arg.
1220 index_arg.arg_type = eArgTypeIndex;
1221 index_arg.arg_repetition = eArgRepeatPlain;
1222
1223 // Put the one and only variant into the first arg for m_arguments:
1224 arg1.push_back (index_arg);
1225
1226 // Define the first variant of this arg pair.
1227 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1228 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001229
Caroline Tice405fe672010-10-04 22:28:36 +00001230 // Define the first variant of this arg pair.
1231 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1232 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001233
Caroline Tice405fe672010-10-04 22:28:36 +00001234 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1235 // must always occur together, they are treated as two variants of one argument rather than two independent
1236 // arguments. Push them both into the same argument position for m_arguments...
1237
1238 arg2.push_back (old_prefix_arg);
1239 arg2.push_back (new_prefix_arg);
1240
1241 // Add arguments to m_arguments.
1242 m_arguments.push_back (arg1);
1243 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 }
1245
Greg Claytoneffe5c92011-05-03 22:09:39 +00001246 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247 {
1248 }
1249
Jim Ingham5a988412012-06-08 21:56:10 +00001250protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001252 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 CommandReturnObject &result)
1254 {
Greg Claytona7015092010-09-18 01:14:36 +00001255 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 if (target)
1257 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001258 size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 // check for at least 3 arguments and an odd nubmer of parameters
1260 if (argc >= 3 && argc & 1)
1261 {
1262 bool success = false;
1263
Vince Harron5275aaa2015-01-15 20:08:35 +00001264 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265
1266 if (!success)
1267 {
1268 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1269 result.SetStatus (eReturnStatusFailed);
1270 return result.Succeeded();
1271 }
1272
1273 // shift off the index
1274 command.Shift();
1275 argc = command.GetArgumentCount();
1276
1277 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1278 {
1279 const char *from = command.GetArgumentAtIndex(i);
1280 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 if (from[0] && to[0])
1283 {
1284 bool last_pair = ((argc - i) == 2);
1285 target->GetImageSearchPathList().Insert (ConstString(from),
1286 ConstString(to),
1287 insert_idx,
1288 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001289 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 }
1291 else
1292 {
1293 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001294 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295 else
Greg Clayton7260f622011-04-18 08:33:37 +00001296 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 result.SetStatus (eReturnStatusFailed);
1298 return false;
1299 }
1300 }
1301 }
1302 else
1303 {
Greg Clayton7260f622011-04-18 08:33:37 +00001304 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305 result.SetStatus (eReturnStatusFailed);
1306 return result.Succeeded();
1307 }
1308
1309 }
1310 else
1311 {
Greg Clayton7260f622011-04-18 08:33:37 +00001312 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 result.SetStatus (eReturnStatusFailed);
1314 }
1315 return result.Succeeded();
1316 }
1317};
1318
Greg Claytoneffe5c92011-05-03 22:09:39 +00001319
1320#pragma mark CommandObjectTargetModulesSearchPathsList
1321
1322
Jim Ingham5a988412012-06-08 21:56:10 +00001323class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324{
1325public:
1326
Greg Claytoneffe5c92011-05-03 22:09:39 +00001327 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001328 CommandObjectParsed (interpreter,
1329 "target modules search-paths list",
1330 "List all current image search path substitution pairs in the current target.",
1331 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332 {
1333 }
1334
Greg Claytoneffe5c92011-05-03 22:09:39 +00001335 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 {
1337 }
1338
Jim Ingham5a988412012-06-08 21:56:10 +00001339protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001341 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 CommandReturnObject &result)
1343 {
Greg Claytona7015092010-09-18 01:14:36 +00001344 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 if (target)
1346 {
1347 if (command.GetArgumentCount() != 0)
1348 {
Greg Clayton7260f622011-04-18 08:33:37 +00001349 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350 result.SetStatus (eReturnStatusFailed);
1351 return result.Succeeded();
1352 }
1353
1354 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001355 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 }
1357 else
1358 {
Greg Clayton7260f622011-04-18 08:33:37 +00001359 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360 result.SetStatus (eReturnStatusFailed);
1361 }
1362 return result.Succeeded();
1363 }
1364};
1365
Greg Claytoneffe5c92011-05-03 22:09:39 +00001366#pragma mark CommandObjectTargetModulesSearchPathsQuery
1367
Jim Ingham5a988412012-06-08 21:56:10 +00001368class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369{
1370public:
1371
Greg Claytoneffe5c92011-05-03 22:09:39 +00001372 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001373 CommandObjectParsed (interpreter,
1374 "target modules search-paths query",
1375 "Transform a path using the first applicable image search path.",
1376 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377 {
Caroline Tice405fe672010-10-04 22:28:36 +00001378 CommandArgumentEntry arg;
1379 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001380
Caroline Tice405fe672010-10-04 22:28:36 +00001381 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001382 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001383 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001384
Caroline Tice405fe672010-10-04 22:28:36 +00001385 // There is only one variant this argument could be; put it into the argument entry.
1386 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001387
Caroline Tice405fe672010-10-04 22:28:36 +00001388 // Push the data for the first argument into the m_arguments vector.
1389 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 }
1391
Greg Claytoneffe5c92011-05-03 22:09:39 +00001392 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 {
1394 }
1395
Jim Ingham5a988412012-06-08 21:56:10 +00001396protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001398 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 CommandReturnObject &result)
1400 {
Greg Claytona7015092010-09-18 01:14:36 +00001401 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 if (target)
1403 {
1404 if (command.GetArgumentCount() != 1)
1405 {
Greg Clayton7260f622011-04-18 08:33:37 +00001406 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407 result.SetStatus (eReturnStatusFailed);
1408 return result.Succeeded();
1409 }
1410
1411 ConstString orig(command.GetArgumentAtIndex(0));
1412 ConstString transformed;
1413 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1414 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1415 else
1416 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001417
1418 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419 }
1420 else
1421 {
Greg Clayton7260f622011-04-18 08:33:37 +00001422 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 result.SetStatus (eReturnStatusFailed);
1424 }
1425 return result.Succeeded();
1426 }
1427};
1428
Greg Claytoneffe5c92011-05-03 22:09:39 +00001429//----------------------------------------------------------------------
1430// Static Helper functions
1431//----------------------------------------------------------------------
1432static void
1433DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1434{
1435 if (module)
1436 {
1437 const char *arch_cstr;
1438 if (full_triple)
1439 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1440 else
1441 arch_cstr = module->GetArchitecture().GetArchitectureName();
1442 if (width)
1443 strm.Printf("%-*s", width, arch_cstr);
1444 else
1445 strm.PutCString(arch_cstr);
1446 }
1447}
1448
1449static void
1450DumpModuleUUID (Stream &strm, Module *module)
1451{
Jim Ingham28eb5712012-10-12 17:34:26 +00001452 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001453 module->GetUUID().Dump (&strm);
1454 else
1455 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001456}
1457
1458static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001459DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1460 Stream &strm,
1461 Module *module,
1462 const FileSpec &file_spec,
1463 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001464{
1465 uint32_t num_matches = 0;
1466 if (module)
1467 {
1468 SymbolContextList sc_list;
1469 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1470 0,
1471 false,
1472 eSymbolContextCompUnit,
1473 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001474
Greg Claytoneffe5c92011-05-03 22:09:39 +00001475 for (uint32_t i=0; i<num_matches; ++i)
1476 {
1477 SymbolContext sc;
1478 if (sc_list.GetContextAtIndex(i, sc))
1479 {
1480 if (i > 0)
1481 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001482
Greg Claytoneffe5c92011-05-03 22:09:39 +00001483 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1484 << module->GetFileSpec().GetFilename() << "\n";
1485 LineTable *line_table = sc.comp_unit->GetLineTable();
1486 if (line_table)
1487 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001488 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001489 lldb::eDescriptionLevelBrief);
1490 else
1491 strm << "No line table";
1492 }
1493 }
1494 }
1495 return num_matches;
1496}
1497
1498static void
1499DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1500{
1501 if (file_spec_ptr)
1502 {
1503 if (width > 0)
1504 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001505 std::string fullpath = file_spec_ptr->GetPath();
1506 strm.Printf("%-*s", width, fullpath.c_str());
1507 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001508 }
1509 else
1510 {
1511 file_spec_ptr->Dump(&strm);
1512 return;
1513 }
1514 }
1515 // Keep the width spacing correct if things go wrong...
1516 if (width > 0)
1517 strm.Printf("%-*s", width, "");
1518}
1519
1520static void
1521DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1522{
1523 if (file_spec_ptr)
1524 {
1525 if (width > 0)
1526 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1527 else
1528 file_spec_ptr->GetDirectory().Dump(&strm);
1529 return;
1530 }
1531 // Keep the width spacing correct if things go wrong...
1532 if (width > 0)
1533 strm.Printf("%-*s", width, "");
1534}
1535
1536static void
1537DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1538{
1539 if (file_spec_ptr)
1540 {
1541 if (width > 0)
1542 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1543 else
1544 file_spec_ptr->GetFilename().Dump(&strm);
1545 return;
1546 }
1547 // Keep the width spacing correct if things go wrong...
1548 if (width > 0)
1549 strm.Printf("%-*s", width, "");
1550}
1551
1552
1553static void
1554DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1555{
1556 if (module)
1557 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001558 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1559 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001560 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001561 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001562 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001563 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001564 }
1565 }
1566}
1567
1568static void
1569DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1570{
1571 if (module)
1572 {
Greg Clayton3046e662013-07-10 01:23:25 +00001573 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001574 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001575 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001576 strm.Printf ("Sections for '%s' (%s):\n",
1577 module->GetSpecificationDescription().c_str(),
1578 module->GetArchitecture().GetArchitectureName());
1579 strm.IndentMore();
1580 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1581 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001582 }
1583 }
1584}
1585
1586static bool
1587DumpModuleSymbolVendor (Stream &strm, Module *module)
1588{
1589 if (module)
1590 {
1591 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1592 if (symbol_vendor)
1593 {
1594 symbol_vendor->Dump(&strm);
1595 return true;
1596 }
1597 }
1598 return false;
1599}
1600
Greg Claytonc4a8a762012-05-15 18:43:44 +00001601static void
1602DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1603{
1604 strm.IndentMore();
1605 strm.Indent (" Address: ");
1606 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1607 strm.PutCString (" (");
1608 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1609 strm.PutCString (")\n");
1610 strm.Indent (" Summary: ");
1611 const uint32_t save_indent = strm.GetIndentLevel ();
1612 strm.SetIndentLevel (save_indent + 13);
1613 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1614 strm.SetIndentLevel (save_indent);
1615 // Print out detailed address information when verbose is enabled
1616 if (verbose)
1617 {
1618 strm.EOL();
1619 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1620 }
1621 strm.IndentLess();
1622}
1623
Greg Claytoneffe5c92011-05-03 22:09:39 +00001624static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001625LookupAddressInModule (CommandInterpreter &interpreter,
1626 Stream &strm,
1627 Module *module,
1628 uint32_t resolve_mask,
1629 lldb::addr_t raw_addr,
1630 lldb::addr_t offset,
1631 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001632{
1633 if (module)
1634 {
1635 lldb::addr_t addr = raw_addr - offset;
1636 Address so_addr;
1637 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001638 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001639 if (target && !target->GetSectionLoadList().IsEmpty())
1640 {
1641 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1642 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001643 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001644 return false;
1645 }
1646 else
1647 {
1648 if (!module->ResolveFileAddress (addr, so_addr))
1649 return false;
1650 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001651
Greg Claytoneffe5c92011-05-03 22:09:39 +00001652 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001653 DumpAddress (exe_scope, so_addr, verbose, strm);
1654// strm.IndentMore();
1655// strm.Indent (" Address: ");
1656// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1657// strm.PutCString (" (");
1658// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1659// strm.PutCString (")\n");
1660// strm.Indent (" Summary: ");
1661// const uint32_t save_indent = strm.GetIndentLevel ();
1662// strm.SetIndentLevel (save_indent + 13);
1663// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1664// strm.SetIndentLevel (save_indent);
1665// // Print out detailed address information when verbose is enabled
1666// if (verbose)
1667// {
1668// strm.EOL();
1669// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1670// }
1671// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001672 return true;
1673 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001674
Greg Claytoneffe5c92011-05-03 22:09:39 +00001675 return false;
1676}
1677
1678static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001679LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001680{
1681 if (module)
1682 {
1683 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001684
Michael Sartaina7499c92013-07-01 19:45:50 +00001685 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1686 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001687 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001688 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001689 if (symtab)
1690 {
1691 uint32_t i;
1692 std::vector<uint32_t> match_indexes;
1693 ConstString symbol_name (name);
1694 uint32_t num_matches = 0;
1695 if (name_is_regex)
1696 {
1697 RegularExpression name_regexp(name);
1698 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1699 eSymbolTypeAny,
1700 match_indexes);
1701 }
1702 else
1703 {
1704 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1705 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001706
Greg Claytoneffe5c92011-05-03 22:09:39 +00001707 if (num_matches > 0)
1708 {
1709 strm.Indent ();
1710 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1711 name_is_regex ? "the regular expression " : "", name);
1712 DumpFullpath (strm, &module->GetFileSpec(), 0);
1713 strm.PutCString(":\n");
1714 strm.IndentMore ();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001715 //Symtab::DumpSymbolHeader (&strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001716 for (i=0; i < num_matches; ++i)
1717 {
1718 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Claytonc4a8a762012-05-15 18:43:44 +00001719 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1720 symbol->GetAddress(),
1721 verbose,
1722 strm);
1723
1724// strm.Indent ();
1725// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001726 }
1727 strm.IndentLess ();
1728 return num_matches;
1729 }
1730 }
1731 }
1732 }
1733 return 0;
1734}
1735
1736
1737static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001738DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001739{
1740 strm.IndentMore ();
1741 uint32_t i;
1742 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001743
Greg Claytoneffe5c92011-05-03 22:09:39 +00001744 for (i=0; i<num_matches; ++i)
1745 {
1746 SymbolContext sc;
1747 if (sc_list.GetContextAtIndex(i, sc))
1748 {
Sean Callananf6172c22012-02-11 00:24:04 +00001749 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001750
Sean Callananf6172c22012-02-11 00:24:04 +00001751 sc.GetAddressRange(eSymbolContextEverything,
1752 0,
1753 true,
1754 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001755
Greg Claytonc4a8a762012-05-15 18:43:44 +00001756 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001757 }
1758 }
1759 strm.IndentLess ();
1760}
1761
Greg Claytonc7bece562013-01-25 18:06:21 +00001762static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001763LookupFunctionInModule (CommandInterpreter &interpreter,
1764 Stream &strm,
1765 Module *module,
1766 const char *name,
1767 bool name_is_regex,
1768 bool include_inlines,
1769 bool include_symbols,
1770 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001771{
1772 if (module && name && name[0])
1773 {
1774 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001775 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001776 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001777 if (name_is_regex)
1778 {
1779 RegularExpression function_name_regex (name);
1780 num_matches = module->FindFunctions (function_name_regex,
1781 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001782 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001783 append,
1784 sc_list);
1785 }
1786 else
1787 {
1788 ConstString function_name (name);
Sean Callananb6d70eb2011-10-12 02:08:07 +00001789 num_matches = module->FindFunctions (function_name,
1790 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001791 eFunctionNameTypeAuto,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001792 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001793 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001794 append,
1795 sc_list);
1796 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001797
Greg Claytoneffe5c92011-05-03 22:09:39 +00001798 if (num_matches)
1799 {
1800 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001801 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001802 DumpFullpath (strm, &module->GetFileSpec(), 0);
1803 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001804 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001805 }
1806 return num_matches;
1807 }
1808 return 0;
1809}
1810
Greg Claytonc7bece562013-01-25 18:06:21 +00001811static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001812LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001813 Stream &strm,
1814 Module *module,
1815 const char *name_cstr,
1816 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001817{
1818 if (module && name_cstr && name_cstr[0])
1819 {
Greg Claytond1767f02011-12-08 02:13:16 +00001820 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001821 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001822 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001823 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001824 SymbolContext sc;
1825
1826 ConstString name(name_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001827 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001828
Greg Claytond1767f02011-12-08 02:13:16 +00001829 if (num_matches)
1830 {
1831 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001832 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001833 DumpFullpath (strm, &module->GetFileSpec(), 0);
1834 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001835 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001836 {
Greg Claytond1767f02011-12-08 02:13:16 +00001837 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001838 {
Greg Claytond1767f02011-12-08 02:13:16 +00001839 // Resolve the clang type so that any forward references
1840 // to types that haven't yet been parsed will get parsed.
1841 type_sp->GetClangFullType ();
1842 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001843 // Print all typedef chains
1844 TypeSP typedef_type_sp (type_sp);
1845 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1846 while (typedefed_type_sp)
1847 {
1848 strm.EOL();
1849 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1850 typedefed_type_sp->GetClangFullType ();
1851 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1852 typedef_type_sp = typedefed_type_sp;
1853 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1854 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001855 }
Greg Claytond1767f02011-12-08 02:13:16 +00001856 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001857 }
Greg Claytond1767f02011-12-08 02:13:16 +00001858 }
1859 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001860 }
1861 return 0;
1862}
1863
Greg Claytonc7bece562013-01-25 18:06:21 +00001864static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001865LookupTypeHere (CommandInterpreter &interpreter,
1866 Stream &strm,
1867 const SymbolContext &sym_ctx,
1868 const char *name_cstr,
1869 bool name_is_regex)
1870{
1871 if (!sym_ctx.module_sp)
1872 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001873
Sean Callanand38b4a92012-06-06 20:49:55 +00001874 TypeList type_list;
1875 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001876 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001877 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001878
Sean Callanand38b4a92012-06-06 20:49:55 +00001879 ConstString name(name_cstr);
1880 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 +00001881
Sean Callanand38b4a92012-06-06 20:49:55 +00001882 if (num_matches)
1883 {
1884 strm.Indent ();
1885 strm.PutCString("Best match found in ");
1886 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1887 strm.PutCString(":\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001888
Sean Callanand38b4a92012-06-06 20:49:55 +00001889 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1890 if (type_sp)
1891 {
1892 // Resolve the clang type so that any forward references
1893 // to types that haven't yet been parsed will get parsed.
1894 type_sp->GetClangFullType ();
1895 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1896 // Print all typedef chains
1897 TypeSP typedef_type_sp (type_sp);
1898 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1899 while (typedefed_type_sp)
1900 {
1901 strm.EOL();
1902 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1903 typedefed_type_sp->GetClangFullType ();
1904 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1905 typedef_type_sp = typedefed_type_sp;
1906 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1907 }
1908 }
1909 strm.EOL();
1910 }
1911 return num_matches;
1912}
1913
1914static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001915LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001916 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001917 Module *module,
1918 const FileSpec &file_spec,
1919 uint32_t line,
1920 bool check_inlines,
1921 bool verbose)
1922{
1923 if (module && file_spec)
1924 {
1925 SymbolContextList sc_list;
1926 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1927 eSymbolContextEverything, sc_list);
1928 if (num_matches > 0)
1929 {
1930 strm.Indent ();
1931 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1932 strm << file_spec;
1933 if (line > 0)
1934 strm.Printf (":%u", line);
1935 strm << " in ";
1936 DumpFullpath (strm, &module->GetFileSpec(), 0);
1937 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001938 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001939 return num_matches;
1940 }
1941 }
1942 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001943}
1944
Greg Clayton8ee64382011-11-10 01:18:58 +00001945
1946static size_t
1947FindModulesByName (Target *target,
1948 const char *module_name,
1949 ModuleList &module_list,
1950 bool check_global_list)
1951{
1952// Dump specified images (by basename or fullpath)
1953 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001954 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001955
Greg Clayton8ee64382011-11-10 01:18:58 +00001956 const size_t initial_size = module_list.GetSize ();
1957
Greg Claytonf3156262012-07-11 20:46:47 +00001958 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001959 {
1960 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001961 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001962 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001963 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001964 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001965 {
1966 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001967
Greg Clayton8ee64382011-11-10 01:18:58 +00001968 if (module)
1969 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001970 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001971 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001972 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001973 module_list.AppendIfNeeded(module_sp);
1974 }
1975 }
1976 }
1977 }
Greg Claytonf3156262012-07-11 20:46:47 +00001978 else
1979 {
1980 if (target)
1981 {
1982 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001983
Greg Claytonf3156262012-07-11 20:46:47 +00001984 // Not found in our module list for our target, check the main
1985 // shared module list in case it is a extra file used somewhere
1986 // else
1987 if (num_matches == 0)
1988 {
1989 module_spec.GetArchitecture() = target->GetArchitecture();
1990 ModuleList::FindSharedModules (module_spec, module_list);
1991 }
1992 }
1993 else
1994 {
1995 ModuleList::FindSharedModules (module_spec,module_list);
1996 }
1997 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001998
Greg Clayton8ee64382011-11-10 01:18:58 +00001999 return module_list.GetSize () - initial_size;
2000}
2001
Greg Claytoneffe5c92011-05-03 22:09:39 +00002002#pragma mark CommandObjectTargetModulesModuleAutoComplete
2003
2004//----------------------------------------------------------------------
2005// A base command object class that can auto complete with module file
2006// paths
2007//----------------------------------------------------------------------
2008
Jim Ingham5a988412012-06-08 21:56:10 +00002009class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002010{
2011public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002012 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2013 const char *name,
2014 const char *help,
2015 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002016 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002017 {
2018 CommandArgumentEntry arg;
2019 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002020
Greg Claytoneffe5c92011-05-03 22:09:39 +00002021 // Define the first (and only) variant of this arg.
2022 file_arg.arg_type = eArgTypeFilename;
2023 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002024
Greg Claytoneffe5c92011-05-03 22:09:39 +00002025 // There is only one variant this argument could be; put it into the argument entry.
2026 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002027
Greg Claytoneffe5c92011-05-03 22:09:39 +00002028 // Push the data for the first argument into the m_arguments vector.
2029 m_arguments.push_back (arg);
2030 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002031
Greg Claytoneffe5c92011-05-03 22:09:39 +00002032 virtual
2033 ~CommandObjectTargetModulesModuleAutoComplete ()
2034 {
2035 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002036
Greg Claytoneffe5c92011-05-03 22:09:39 +00002037 virtual int
2038 HandleArgumentCompletion (Args &input,
2039 int &cursor_index,
2040 int &cursor_char_position,
2041 OptionElementVector &opt_element_vector,
2042 int match_start_point,
2043 int max_return_elements,
2044 bool &word_complete,
2045 StringList &matches)
2046 {
2047 // Arguments are the standard module completer.
2048 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2049 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002050
Greg Claytoneffe5c92011-05-03 22:09:39 +00002051 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2052 CommandCompletions::eModuleCompletion,
2053 completion_str.c_str(),
2054 match_start_point,
2055 max_return_elements,
2056 NULL,
2057 word_complete,
2058 matches);
2059 return matches.GetSize();
2060 }
2061};
2062
2063#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2064
2065//----------------------------------------------------------------------
2066// A base command object class that can auto complete with module source
2067// file paths
2068//----------------------------------------------------------------------
2069
Jim Ingham5a988412012-06-08 21:56:10 +00002070class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002071{
2072public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002073 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002074 const char *name,
2075 const char *help,
2076 const char *syntax,
2077 uint32_t flags) :
2078 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002079 {
2080 CommandArgumentEntry arg;
2081 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002082
Greg Claytoneffe5c92011-05-03 22:09:39 +00002083 // Define the first (and only) variant of this arg.
2084 source_file_arg.arg_type = eArgTypeSourceFile;
2085 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002086
Greg Claytoneffe5c92011-05-03 22:09:39 +00002087 // There is only one variant this argument could be; put it into the argument entry.
2088 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002089
Greg Claytoneffe5c92011-05-03 22:09:39 +00002090 // Push the data for the first argument into the m_arguments vector.
2091 m_arguments.push_back (arg);
2092 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002093
Greg Claytoneffe5c92011-05-03 22:09:39 +00002094 virtual
2095 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2096 {
2097 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002098
Greg Claytoneffe5c92011-05-03 22:09:39 +00002099 virtual int
2100 HandleArgumentCompletion (Args &input,
2101 int &cursor_index,
2102 int &cursor_char_position,
2103 OptionElementVector &opt_element_vector,
2104 int match_start_point,
2105 int max_return_elements,
2106 bool &word_complete,
2107 StringList &matches)
2108 {
2109 // Arguments are the standard source file completer.
2110 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2111 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002112
Greg Claytoneffe5c92011-05-03 22:09:39 +00002113 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2114 CommandCompletions::eSourceFileCompletion,
2115 completion_str.c_str(),
2116 match_start_point,
2117 max_return_elements,
2118 NULL,
2119 word_complete,
2120 matches);
2121 return matches.GetSize();
2122 }
2123};
2124
2125
2126#pragma mark CommandObjectTargetModulesDumpSymtab
2127
2128
2129class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2130{
2131public:
2132 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2133 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2134 "target modules dump symtab",
2135 "Dump the symbol table from one or more target modules.",
2136 NULL),
2137 m_options (interpreter)
2138 {
2139 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002140
Greg Claytoneffe5c92011-05-03 22:09:39 +00002141 virtual
2142 ~CommandObjectTargetModulesDumpSymtab ()
2143 {
2144 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002145
Jim Ingham5a988412012-06-08 21:56:10 +00002146 virtual Options *
2147 GetOptions ()
2148 {
2149 return &m_options;
2150 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002151
Jim Ingham5a988412012-06-08 21:56:10 +00002152 class CommandOptions : public Options
2153 {
2154 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002155 CommandOptions (CommandInterpreter &interpreter) :
2156 Options(interpreter),
2157 m_sort_order (eSortOrderNone)
2158 {
2159 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002160
Jim Ingham5a988412012-06-08 21:56:10 +00002161 virtual
2162 ~CommandOptions ()
2163 {
2164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002165
Jim Ingham5a988412012-06-08 21:56:10 +00002166 virtual Error
2167 SetOptionValue (uint32_t option_idx, const char *option_arg)
2168 {
2169 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002170 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002171
Jim Ingham5a988412012-06-08 21:56:10 +00002172 switch (short_option)
2173 {
2174 case 's':
2175 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2176 g_option_table[option_idx].enum_values,
2177 eSortOrderNone,
2178 error);
2179 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002180
Jim Ingham5a988412012-06-08 21:56:10 +00002181 default:
2182 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2183 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002184
Jim Ingham5a988412012-06-08 21:56:10 +00002185 }
2186 return error;
2187 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002188
Jim Ingham5a988412012-06-08 21:56:10 +00002189 void
2190 OptionParsingStarting ()
2191 {
2192 m_sort_order = eSortOrderNone;
2193 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002194
Jim Ingham5a988412012-06-08 21:56:10 +00002195 const OptionDefinition*
2196 GetDefinitions ()
2197 {
2198 return g_option_table;
2199 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002200
Jim Ingham5a988412012-06-08 21:56:10 +00002201 // Options table: Required for subclasses of Options.
2202 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002203
Jim Ingham5a988412012-06-08 21:56:10 +00002204 SortOrder m_sort_order;
2205 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002206
Jim Ingham5a988412012-06-08 21:56:10 +00002207protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002208 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002209 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002210 CommandReturnObject &result)
2211 {
2212 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2213 if (target == NULL)
2214 {
2215 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2216 result.SetStatus (eReturnStatusFailed);
2217 return false;
2218 }
2219 else
2220 {
2221 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002222
Greg Claytoneffe5c92011-05-03 22:09:39 +00002223 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2224 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2225 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002226
Greg Claytoneffe5c92011-05-03 22:09:39 +00002227 if (command.GetArgumentCount() == 0)
2228 {
2229 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002230 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002231 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002232 if (num_modules > 0)
2233 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002234 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002235 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002236 {
2237 if (num_dumped > 0)
2238 {
2239 result.GetOutputStream().EOL();
2240 result.GetOutputStream().EOL();
2241 }
2242 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002243 DumpModuleSymtab (m_interpreter,
2244 result.GetOutputStream(),
2245 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2246 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002247 }
2248 }
2249 else
2250 {
2251 result.AppendError ("the target has no associated executable images");
2252 result.SetStatus (eReturnStatusFailed);
2253 return false;
2254 }
2255 }
2256 else
2257 {
2258 // Dump specified images (by basename or fullpath)
2259 const char *arg_cstr;
2260 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2261 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002262 ModuleList module_list;
2263 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2264 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002265 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002266 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002267 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002268 Module *module = module_list.GetModulePointerAtIndex(i);
2269 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002270 {
2271 if (num_dumped > 0)
2272 {
2273 result.GetOutputStream().EOL();
2274 result.GetOutputStream().EOL();
2275 }
2276 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002277 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002278 }
2279 }
2280 }
2281 else
2282 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2283 }
2284 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002285
Greg Claytoneffe5c92011-05-03 22:09:39 +00002286 if (num_dumped > 0)
2287 result.SetStatus (eReturnStatusSuccessFinishResult);
2288 else
2289 {
2290 result.AppendError ("no matching executable images found");
2291 result.SetStatus (eReturnStatusFailed);
2292 }
2293 }
2294 return result.Succeeded();
2295 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002296
Greg Claytoneffe5c92011-05-03 22:09:39 +00002297 CommandOptions m_options;
2298};
2299
2300static OptionEnumValueElement
2301g_sort_option_enumeration[4] =
2302{
2303 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2304 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2305 { eSortOrderByName, "name", "Sort output by symbol name."},
2306 { 0, NULL, NULL }
2307};
2308
2309
2310OptionDefinition
2311CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2312{
Zachary Turnerd37221d2014-07-09 16:31:49 +00002313 { 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."},
2314 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002315};
2316
2317#pragma mark CommandObjectTargetModulesDumpSections
2318
2319//----------------------------------------------------------------------
2320// Image section dumping command
2321//----------------------------------------------------------------------
2322
2323class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2324{
2325public:
2326 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2327 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2328 "target modules dump sections",
2329 "Dump the sections from one or more target modules.",
2330 //"target modules dump sections [<file1> ...]")
2331 NULL)
2332 {
2333 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002334
Greg Claytoneffe5c92011-05-03 22:09:39 +00002335 virtual
2336 ~CommandObjectTargetModulesDumpSections ()
2337 {
2338 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002339
Jim Ingham5a988412012-06-08 21:56:10 +00002340protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002341 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002342 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002343 CommandReturnObject &result)
2344 {
2345 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2346 if (target == NULL)
2347 {
2348 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2349 result.SetStatus (eReturnStatusFailed);
2350 return false;
2351 }
2352 else
2353 {
2354 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002355
Greg Claytoneffe5c92011-05-03 22:09:39 +00002356 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2357 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2358 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002359
Greg Claytoneffe5c92011-05-03 22:09:39 +00002360 if (command.GetArgumentCount() == 0)
2361 {
2362 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002363 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002364 if (num_modules > 0)
2365 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002366 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002367 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002368 {
2369 num_dumped++;
2370 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2371 }
2372 }
2373 else
2374 {
2375 result.AppendError ("the target has no associated executable images");
2376 result.SetStatus (eReturnStatusFailed);
2377 return false;
2378 }
2379 }
2380 else
2381 {
2382 // Dump specified images (by basename or fullpath)
2383 const char *arg_cstr;
2384 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2385 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002386 ModuleList module_list;
2387 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2388 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002389 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002390 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002391 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002392 Module *module = module_list.GetModulePointerAtIndex(i);
2393 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002394 {
2395 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002396 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002397 }
2398 }
2399 }
2400 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002401 {
2402 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002403 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002404
Greg Claytoneffe5c92011-05-03 22:09:39 +00002405 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002406 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002407 }
2408 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002409
Greg Claytoneffe5c92011-05-03 22:09:39 +00002410 if (num_dumped > 0)
2411 result.SetStatus (eReturnStatusSuccessFinishResult);
2412 else
2413 {
2414 result.AppendError ("no matching executable images found");
2415 result.SetStatus (eReturnStatusFailed);
2416 }
2417 }
2418 return result.Succeeded();
2419 }
2420};
2421
2422
2423#pragma mark CommandObjectTargetModulesDumpSymfile
2424
2425//----------------------------------------------------------------------
2426// Image debug symbol dumping command
2427//----------------------------------------------------------------------
2428
2429class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2430{
2431public:
2432 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2433 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2434 "target modules dump symfile",
2435 "Dump the debug symbol file for one or more target modules.",
2436 //"target modules dump symfile [<file1> ...]")
2437 NULL)
2438 {
2439 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002440
Greg Claytoneffe5c92011-05-03 22:09:39 +00002441 virtual
2442 ~CommandObjectTargetModulesDumpSymfile ()
2443 {
2444 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002445
Jim Ingham5a988412012-06-08 21:56:10 +00002446protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002447 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002448 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002449 CommandReturnObject &result)
2450 {
2451 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2452 if (target == NULL)
2453 {
2454 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2455 result.SetStatus (eReturnStatusFailed);
2456 return false;
2457 }
2458 else
2459 {
2460 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002461
Greg Claytoneffe5c92011-05-03 22:09:39 +00002462 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2463 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2464 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002465
Greg Claytoneffe5c92011-05-03 22:09:39 +00002466 if (command.GetArgumentCount() == 0)
2467 {
2468 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002469 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002470 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002471 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002472 if (num_modules > 0)
2473 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002474 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002475 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2476 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002477 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002478 num_dumped++;
2479 }
2480 }
2481 else
2482 {
2483 result.AppendError ("the target has no associated executable images");
2484 result.SetStatus (eReturnStatusFailed);
2485 return false;
2486 }
2487 }
2488 else
2489 {
2490 // Dump specified images (by basename or fullpath)
2491 const char *arg_cstr;
2492 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2493 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002494 ModuleList module_list;
2495 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2496 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002497 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002498 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002499 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002500 Module *module = module_list.GetModulePointerAtIndex(i);
2501 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002502 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002503 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002504 num_dumped++;
2505 }
2506 }
2507 }
2508 else
2509 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2510 }
2511 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002512
Greg Claytoneffe5c92011-05-03 22:09:39 +00002513 if (num_dumped > 0)
2514 result.SetStatus (eReturnStatusSuccessFinishResult);
2515 else
2516 {
2517 result.AppendError ("no matching executable images found");
2518 result.SetStatus (eReturnStatusFailed);
2519 }
2520 }
2521 return result.Succeeded();
2522 }
2523};
2524
2525
2526#pragma mark CommandObjectTargetModulesDumpLineTable
2527
2528//----------------------------------------------------------------------
2529// Image debug line table dumping command
2530//----------------------------------------------------------------------
2531
2532class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2533{
2534public:
2535 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2536 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002537 "target modules dump line-table",
Jim Inghamcc0273d2013-06-18 20:27:11 +00002538 "Dump the line table for one or more compilation units.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00002539 NULL,
2540 eFlagRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002541 {
2542 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002543
Greg Claytoneffe5c92011-05-03 22:09:39 +00002544 virtual
2545 ~CommandObjectTargetModulesDumpLineTable ()
2546 {
2547 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002548
Jim Ingham5a988412012-06-08 21:56:10 +00002549protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002550 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002551 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002552 CommandReturnObject &result)
2553 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002554 Target *target = m_exe_ctx.GetTargetPtr();
2555 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002556
Greg Claytonf9fc6092013-01-09 19:44:40 +00002557 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2558 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2559 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002560
Greg Claytonf9fc6092013-01-09 19:44:40 +00002561 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002562 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002563 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00002564 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002565 }
2566 else
2567 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002568 // Dump specified images (by basename or fullpath)
2569 const char *arg_cstr;
2570 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002571 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002572 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002573
Greg Claytonf9fc6092013-01-09 19:44:40 +00002574 const ModuleList &target_modules = target->GetImages();
2575 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002576 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002577 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002578 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002579 uint32_t num_dumped = 0;
2580 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002581 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002582 if (DumpCompileUnitLineTable (m_interpreter,
2583 result.GetOutputStream(),
2584 target_modules.GetModulePointerAtIndexUnlocked(i),
2585 file_spec,
2586 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2587 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002588 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002589 if (num_dumped == 0)
2590 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2591 else
2592 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002593 }
2594 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002595 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002596
Greg Claytonf9fc6092013-01-09 19:44:40 +00002597 if (total_num_dumped > 0)
2598 result.SetStatus (eReturnStatusSuccessFinishResult);
2599 else
2600 {
2601 result.AppendError ("no source filenames matched any command arguments");
2602 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002603 }
2604 return result.Succeeded();
2605 }
2606};
2607
2608
2609#pragma mark CommandObjectTargetModulesDump
2610
2611//----------------------------------------------------------------------
2612// Dump multi-word command for target modules
2613//----------------------------------------------------------------------
2614
2615class CommandObjectTargetModulesDump : public CommandObjectMultiword
2616{
2617public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002618 //------------------------------------------------------------------
2619 // Constructors and Destructors
2620 //------------------------------------------------------------------
2621 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2622 CommandObjectMultiword (interpreter,
2623 "target modules dump",
2624 "A set of commands for dumping information about one or more target modules.",
2625 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2626 {
2627 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2628 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2629 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2630 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2631 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002632
Greg Claytoneffe5c92011-05-03 22:09:39 +00002633 virtual
2634 ~CommandObjectTargetModulesDump()
2635 {
2636 }
2637};
2638
Jim Ingham5a988412012-06-08 21:56:10 +00002639class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002640{
2641public:
2642 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002643 CommandObjectParsed (interpreter,
2644 "target modules add",
2645 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002646 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002647 m_option_group (interpreter),
2648 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 +00002649 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002650 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002651 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002652 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002653 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002654
Greg Claytoneffe5c92011-05-03 22:09:39 +00002655 virtual
2656 ~CommandObjectTargetModulesAdd ()
2657 {
2658 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002659
Greg Clayton50a24bd2012-11-29 22:16:27 +00002660 virtual Options *
2661 GetOptions ()
2662 {
2663 return &m_option_group;
2664 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002665
Greg Claytonc7bece562013-01-25 18:06:21 +00002666 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00002667 HandleArgumentCompletion (Args &input,
2668 int &cursor_index,
2669 int &cursor_char_position,
2670 OptionElementVector &opt_element_vector,
2671 int match_start_point,
2672 int max_return_elements,
2673 bool &word_complete,
2674 StringList &matches)
2675 {
2676 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2677 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002678
Jim Ingham5a988412012-06-08 21:56:10 +00002679 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2680 CommandCompletions::eDiskFileCompletion,
2681 completion_str.c_str(),
2682 match_start_point,
2683 max_return_elements,
2684 NULL,
2685 word_complete,
2686 matches);
2687 return matches.GetSize();
2688 }
2689
2690protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002691 OptionGroupOptions m_option_group;
2692 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002693 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002694
Greg Claytoneffe5c92011-05-03 22:09:39 +00002695 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002696 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002697 CommandReturnObject &result)
2698 {
2699 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2700 if (target == NULL)
2701 {
2702 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2703 result.SetStatus (eReturnStatusFailed);
2704 return false;
2705 }
2706 else
2707 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002708 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002709
Greg Claytoneffe5c92011-05-03 22:09:39 +00002710 const size_t argc = args.GetArgumentCount();
2711 if (argc == 0)
2712 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002713 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2714 {
2715 // We are given a UUID only, go locate the file
2716 ModuleSpec module_spec;
2717 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002718 if (m_symbol_file.GetOptionValue().OptionWasSet())
2719 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002720 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2721 {
2722 ModuleSP module_sp (target->GetSharedModule (module_spec));
2723 if (module_sp)
2724 {
2725 result.SetStatus (eReturnStatusSuccessFinishResult);
2726 return true;
2727 }
2728 else
2729 {
2730 StreamString strm;
2731 module_spec.GetUUID().Dump (&strm);
2732 if (module_spec.GetFileSpec())
2733 {
2734 if (module_spec.GetSymbolFileSpec())
2735 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002736 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 +00002737 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002738 module_spec.GetFileSpec().GetPath().c_str(),
2739 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002740 }
2741 else
2742 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002743 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002744 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002745 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002746 }
2747 }
2748 else
2749 {
2750 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2751 strm.GetString().c_str());
2752 }
2753 result.SetStatus (eReturnStatusFailed);
2754 return false;
2755 }
2756 }
2757 else
2758 {
2759 StreamString strm;
2760 module_spec.GetUUID().Dump (&strm);
2761 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2762 result.SetStatus (eReturnStatusFailed);
2763 return false;
2764 }
2765 }
2766 else
2767 {
2768 result.AppendError ("one or more executable image paths must be specified");
2769 result.SetStatus (eReturnStatusFailed);
2770 return false;
2771 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002772 }
2773 else
2774 {
2775 for (size_t i=0; i<argc; ++i)
2776 {
2777 const char *path = args.GetArgumentAtIndex(i);
2778 if (path)
2779 {
2780 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002781 if (file_spec.Exists())
2782 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002783 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002784 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2785 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002786 if (m_symbol_file.GetOptionValue().OptionWasSet())
2787 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002788 if (!module_spec.GetArchitecture().IsValid())
2789 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002790 Error error;
2791 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002792 if (!module_sp)
2793 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002794 const char *error_cstr = error.AsCString();
2795 if (error_cstr)
2796 result.AppendError (error_cstr);
2797 else
2798 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002799 result.SetStatus (eReturnStatusFailed);
2800 return false;
2801 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002802 else
2803 {
2804 flush = true;
2805 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002806 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002807 }
2808 else
2809 {
2810 char resolved_path[PATH_MAX];
2811 result.SetStatus (eReturnStatusFailed);
2812 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2813 {
2814 if (strcmp (resolved_path, path) != 0)
2815 {
2816 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2817 break;
2818 }
2819 }
2820 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2821 break;
2822 }
2823 }
2824 }
2825 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002826
Sean Callananb36c6c02012-12-13 01:39:39 +00002827 if (flush)
2828 {
2829 ProcessSP process = target->GetProcessSP();
2830 if (process)
2831 process->Flush();
2832 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002833 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002834
Greg Claytoneffe5c92011-05-03 22:09:39 +00002835 return result.Succeeded();
2836 }
2837
Greg Claytoneffe5c92011-05-03 22:09:39 +00002838};
2839
2840class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2841{
2842public:
2843 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2844 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2845 "target modules load",
2846 "Set the load addresses for one or more sections in a target module.",
2847 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2848 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002849 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 +00002850 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)
2851 {
2852 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2853 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2854 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2855 m_option_group.Finalize();
2856 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002857
Greg Claytoneffe5c92011-05-03 22:09:39 +00002858 virtual
2859 ~CommandObjectTargetModulesLoad ()
2860 {
2861 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002862
Jim Ingham5a988412012-06-08 21:56:10 +00002863 virtual Options *
2864 GetOptions ()
2865 {
2866 return &m_option_group;
2867 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002868
Jim Ingham5a988412012-06-08 21:56:10 +00002869protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002870 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002871 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002872 CommandReturnObject &result)
2873 {
2874 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2875 if (target == NULL)
2876 {
2877 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2878 result.SetStatus (eReturnStatusFailed);
2879 return false;
2880 }
2881 else
2882 {
2883 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002884 ModuleSpec module_spec;
2885 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002886 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002887 {
2888 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002889 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2890 const bool use_global_module_list = true;
2891 ModuleList module_list;
2892 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2893 if (num_matches == 1)
2894 {
2895 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2896 }
2897 else if (num_matches > 1 )
2898 {
2899 search_using_module_spec = false;
2900 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2901 result.SetStatus (eReturnStatusFailed);
2902 }
2903 else
2904 {
2905 search_using_module_spec = false;
2906 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2907 result.SetStatus (eReturnStatusFailed);
2908 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002909 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002910
Greg Claytoneffe5c92011-05-03 22:09:39 +00002911 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002912 {
2913 search_using_module_spec = true;
2914 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2915 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002916
Greg Claytonb9a01b32012-02-26 05:51:37 +00002917 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002918 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002919 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002920 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002921
2922 char path[PATH_MAX];
2923 if (num_matches == 1)
2924 {
2925 Module *module = matching_modules.GetModulePointerAtIndex(0);
2926 if (module)
2927 {
2928 ObjectFile *objfile = module->GetObjectFile();
2929 if (objfile)
2930 {
Greg Clayton3046e662013-07-10 01:23:25 +00002931 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002932 if (section_list)
2933 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002934 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002935 if (argc == 0)
2936 {
2937 if (m_slide_option.GetOptionValue().OptionWasSet())
2938 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002939 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002940 const bool slide_is_offset = true;
2941 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002942 }
2943 else
2944 {
2945 result.AppendError ("one or more section name + load address pair must be specified");
2946 result.SetStatus (eReturnStatusFailed);
2947 return false;
2948 }
2949 }
2950 else
2951 {
2952 if (m_slide_option.GetOptionValue().OptionWasSet())
2953 {
2954 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2955 result.SetStatus (eReturnStatusFailed);
2956 return false;
2957 }
2958
2959 for (size_t i=0; i<argc; i += 2)
2960 {
2961 const char *sect_name = args.GetArgumentAtIndex(i);
2962 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2963 if (sect_name && load_addr_cstr)
2964 {
2965 ConstString const_sect_name(sect_name);
2966 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00002967 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002968 if (success)
2969 {
2970 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2971 if (section_sp)
2972 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002973 if (section_sp->IsThreadSpecific())
2974 {
2975 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2976 result.SetStatus (eReturnStatusFailed);
2977 break;
2978 }
2979 else
2980 {
Greg Clayton7820bd12012-07-07 01:24:12 +00002981 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00002982 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00002983 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00002984 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002985 }
2986 else
2987 {
2988 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2989 result.SetStatus (eReturnStatusFailed);
2990 break;
2991 }
2992 }
2993 else
2994 {
2995 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2996 result.SetStatus (eReturnStatusFailed);
2997 break;
2998 }
2999 }
3000 else
3001 {
3002 if (sect_name)
3003 result.AppendError ("section names must be followed by a load address.\n");
3004 else
3005 result.AppendError ("one or more section name + load address pair must be specified.\n");
3006 result.SetStatus (eReturnStatusFailed);
3007 break;
3008 }
3009 }
3010 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003011
Greg Clayton741f3f92012-03-27 21:10:07 +00003012 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003013 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003014 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003015 Process *process = m_exe_ctx.GetProcessPtr();
3016 if (process)
3017 process->Flush();
3018 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003019 }
3020 else
3021 {
3022 module->GetFileSpec().GetPath (path, sizeof(path));
3023 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3024 result.SetStatus (eReturnStatusFailed);
3025 }
3026 }
3027 else
3028 {
3029 module->GetFileSpec().GetPath (path, sizeof(path));
3030 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3031 result.SetStatus (eReturnStatusFailed);
3032 }
3033 }
3034 else
3035 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003036 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3037 if (module_spec_file)
3038 {
3039 module_spec_file->GetPath (path, sizeof(path));
3040 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3041 }
3042 else
3043 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003044 result.SetStatus (eReturnStatusFailed);
3045 }
3046 }
3047 else
3048 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003049 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003050
Greg Claytonb9a01b32012-02-26 05:51:37 +00003051 if (module_spec.GetFileSpec())
3052 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003053 else
3054 path[0] = '\0';
3055
Greg Claytonb9a01b32012-02-26 05:51:37 +00003056 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003057 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003058 if (num_matches > 1)
3059 {
3060 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3061 path[0] ? " file=" : "",
3062 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003063 !uuid_str.empty() ? " uuid=" : "",
3064 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003065 for (size_t i=0; i<num_matches; ++i)
3066 {
3067 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3068 result.AppendMessageWithFormat("%s\n", path);
3069 }
3070 }
3071 else
3072 {
3073 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3074 path[0] ? " file=" : "",
3075 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003076 !uuid_str.empty() ? " uuid=" : "",
3077 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003078 }
3079 result.SetStatus (eReturnStatusFailed);
3080 }
3081 }
3082 else
3083 {
3084 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3085 result.SetStatus (eReturnStatusFailed);
3086 return false;
3087 }
3088 }
3089 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003090 }
3091
Greg Claytoneffe5c92011-05-03 22:09:39 +00003092 OptionGroupOptions m_option_group;
3093 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003094 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003095 OptionGroupUInt64 m_slide_option;
3096};
3097
3098//----------------------------------------------------------------------
3099// List images with associated information
3100//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003101class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003102{
3103public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003104 class CommandOptions : public Options
3105 {
3106 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003107 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003108 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003109 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003110 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003111 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003112 {
3113 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003114
Greg Claytoneffe5c92011-05-03 22:09:39 +00003115 virtual
3116 ~CommandOptions ()
3117 {
3118 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003119
Greg Claytoneffe5c92011-05-03 22:09:39 +00003120 virtual Error
3121 SetOptionValue (uint32_t option_idx, const char *option_arg)
3122 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003123 Error error;
3124
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003125 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003126 if (short_option == 'g')
3127 {
3128 m_use_global_module_list = true;
3129 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003130 else if (short_option == 'a')
3131 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003132 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3133 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003134 }
Greg Clayton65a03992011-08-09 00:01:09 +00003135 else
3136 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003137 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003138 if (option_arg)
3139 width = strtoul (option_arg, NULL, 0);
3140 m_format_array.push_back(std::make_pair(short_option, width));
3141 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003142 return error;
3143 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003144
Greg Claytoneffe5c92011-05-03 22:09:39 +00003145 void
3146 OptionParsingStarting ()
3147 {
3148 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003149 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003150 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003151 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003152
Greg Claytoneffe5c92011-05-03 22:09:39 +00003153 const OptionDefinition*
3154 GetDefinitions ()
3155 {
3156 return g_option_table;
3157 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003158
Greg Claytoneffe5c92011-05-03 22:09:39 +00003159 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003160
Greg Claytoneffe5c92011-05-03 22:09:39 +00003161 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003162
Greg Claytoneffe5c92011-05-03 22:09:39 +00003163 // Instance variables to hold the values for command options.
3164 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3165 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003166 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003167 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003168 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003169
Greg Claytoneffe5c92011-05-03 22:09:39 +00003170 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003171 CommandObjectParsed (interpreter,
3172 "target modules list",
3173 "List current executable and dependent shared library images.",
3174 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003175 m_options (interpreter)
3176 {
3177 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003178
Greg Claytoneffe5c92011-05-03 22:09:39 +00003179 virtual
3180 ~CommandObjectTargetModulesList ()
3181 {
3182 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003183
Greg Claytoneffe5c92011-05-03 22:09:39 +00003184 virtual
3185 Options *
3186 GetOptions ()
3187 {
3188 return &m_options;
3189 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003190
Jim Ingham5a988412012-06-08 21:56:10 +00003191protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003192 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00003193 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003194 CommandReturnObject &result)
3195 {
3196 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003197 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003198 // Define a local module list here to ensure it lives longer than any "locker"
3199 // object which might lock its contents below (through the "module_list_ptr"
3200 // variable).
3201 ModuleList module_list;
Greg Clayton3418c852011-08-10 02:10:13 +00003202 if (target == NULL && use_global_module_list == false)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003203 {
3204 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3205 result.SetStatus (eReturnStatusFailed);
3206 return false;
3207 }
3208 else
3209 {
Greg Clayton3418c852011-08-10 02:10:13 +00003210 if (target)
3211 {
3212 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3213 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3214 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3215 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003216 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003217 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003218
Jim Inghamc10312c2011-10-24 18:36:33 +00003219 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3220 {
3221 if (target)
3222 {
3223 Address module_address;
3224 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3225 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003226 ModuleSP module_sp (module_address.GetModule());
3227 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003228 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003229 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003230 result.SetStatus (eReturnStatusSuccessFinishResult);
3231 }
3232 else
3233 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003234 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003235 result.SetStatus (eReturnStatusFailed);
3236 }
3237 }
3238 else
3239 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003240 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003241 result.SetStatus (eReturnStatusFailed);
3242 }
3243 }
3244 else
3245 {
3246 result.AppendError ("Can only look up modules by address with a valid target.");
3247 result.SetStatus (eReturnStatusFailed);
3248 }
3249 return result.Succeeded();
3250 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003251
Greg Claytonc7bece562013-01-25 18:06:21 +00003252 size_t num_modules = 0;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003253 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3254 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3255 // the global module list directly.
Enrico Granata17598482012-11-08 02:22:02 +00003256 const ModuleList *module_list_ptr = NULL;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003257 const size_t argc = command.GetArgumentCount();
3258 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003259 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003260 if (use_global_module_list)
3261 {
3262 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3263 num_modules = Module::GetNumberAllocatedModules();
3264 }
3265 else
3266 {
3267 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003268 }
Greg Clayton65a03992011-08-09 00:01:09 +00003269 }
3270 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003271 {
3272 for (size_t i=0; i<argc; ++i)
3273 {
3274 // Dump specified images (by basename or fullpath)
3275 const char *arg_cstr = command.GetArgumentAtIndex(i);
3276 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3277 if (num_matches == 0)
3278 {
3279 if (argc == 1)
3280 {
3281 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3282 result.SetStatus (eReturnStatusFailed);
3283 return false;
3284 }
3285 }
3286 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003287
Greg Claytonc4a8a762012-05-15 18:43:44 +00003288 module_list_ptr = &module_list;
3289 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003290
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003291 if (module_list_ptr != NULL)
3292 {
3293 locker.Lock(module_list_ptr->GetMutex());
3294 num_modules = module_list_ptr->GetSize();
3295 }
Greg Clayton65a03992011-08-09 00:01:09 +00003296
Greg Claytoneffe5c92011-05-03 22:09:39 +00003297 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003298 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003299 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3300 {
Greg Clayton3418c852011-08-10 02:10:13 +00003301 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003302 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003303 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003304 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003305 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003306 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003307 }
3308 else
3309 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003310 module = Module::GetAllocatedModuleAtIndex(image_idx);
3311 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003312 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003313
Greg Claytonc7bece562013-01-25 18:06:21 +00003314 const size_t indent = strm.Printf("[%3u] ", image_idx);
3315 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003316
Greg Claytoneffe5c92011-05-03 22:09:39 +00003317 }
3318 result.SetStatus (eReturnStatusSuccessFinishResult);
3319 }
3320 else
3321 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003322 if (argc)
3323 {
3324 if (use_global_module_list)
3325 result.AppendError ("the global module list has no matching modules");
3326 else
3327 result.AppendError ("the target has no matching modules");
3328 }
Greg Clayton3418c852011-08-10 02:10:13 +00003329 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003330 {
3331 if (use_global_module_list)
3332 result.AppendError ("the global module list is empty");
3333 else
3334 result.AppendError ("the target has no associated executable images");
3335 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003336 result.SetStatus (eReturnStatusFailed);
3337 return false;
3338 }
3339 }
3340 return result.Succeeded();
3341 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003342
3343 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003344 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003345 {
3346
Jim Ingham28eb5712012-10-12 17:34:26 +00003347 if (module == NULL)
3348 {
3349 strm.PutCString("Null module");
3350 return;
3351 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003352
Jim Inghamc10312c2011-10-24 18:36:33 +00003353 bool dump_object_name = false;
3354 if (m_options.m_format_array.empty())
3355 {
Greg Claytonc9660542012-02-05 02:38:54 +00003356 m_options.m_format_array.push_back(std::make_pair('u', 0));
3357 m_options.m_format_array.push_back(std::make_pair('h', 0));
3358 m_options.m_format_array.push_back(std::make_pair('f', 0));
3359 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003360 }
Greg Claytonc9660542012-02-05 02:38:54 +00003361 const size_t num_entries = m_options.m_format_array.size();
3362 bool print_space = false;
3363 for (size_t i=0; i<num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003364 {
Greg Claytonc9660542012-02-05 02:38:54 +00003365 if (print_space)
3366 strm.PutChar(' ');
3367 print_space = true;
3368 const char format_char = m_options.m_format_array[i].first;
3369 uint32_t width = m_options.m_format_array[i].second;
3370 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003371 {
Greg Claytonc9660542012-02-05 02:38:54 +00003372 case 'A':
3373 DumpModuleArchitecture (strm, module, false, width);
3374 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003375
Greg Claytonc9660542012-02-05 02:38:54 +00003376 case 't':
3377 DumpModuleArchitecture (strm, module, true, width);
3378 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003379
Greg Claytonc9660542012-02-05 02:38:54 +00003380 case 'f':
3381 DumpFullpath (strm, &module->GetFileSpec(), width);
3382 dump_object_name = true;
3383 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003384
Greg Claytonc9660542012-02-05 02:38:54 +00003385 case 'd':
3386 DumpDirectory (strm, &module->GetFileSpec(), width);
3387 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003388
Greg Claytonc9660542012-02-05 02:38:54 +00003389 case 'b':
3390 DumpBasename (strm, &module->GetFileSpec(), width);
3391 dump_object_name = true;
3392 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003393
Greg Claytonc9660542012-02-05 02:38:54 +00003394 case 'h':
3395 case 'o':
3396 // Image header address
3397 {
3398 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003399
Greg Claytonc9660542012-02-05 02:38:54 +00003400 ObjectFile *objfile = module->GetObjectFile ();
3401 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003402 {
Greg Claytonc9660542012-02-05 02:38:54 +00003403 Address header_addr(objfile->GetHeaderAddress());
3404 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003405 {
Greg Claytonc9660542012-02-05 02:38:54 +00003406 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003407 {
Greg Claytonc9660542012-02-05 02:38:54 +00003408 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3409 if (header_load_addr == LLDB_INVALID_ADDRESS)
3410 {
3411 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3412 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003413 else
Greg Claytonc9660542012-02-05 02:38:54 +00003414 {
3415 if (format_char == 'o')
3416 {
3417 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003418 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003419 }
3420 else
3421 {
3422 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003423 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003424 }
3425 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003426 break;
3427 }
Greg Claytonc9660542012-02-05 02:38:54 +00003428 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3429 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3430 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003431 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003432 }
Greg Claytonc9660542012-02-05 02:38:54 +00003433 strm.Printf ("%*s", addr_nibble_width + 2, "");
3434 }
3435 break;
3436 case 'r':
3437 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003438 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003439 ModuleSP module_sp (module->shared_from_this());
3440 if (module_sp)
3441 {
3442 // Take one away to make sure we don't count our local "module_sp"
3443 ref_count = module_sp.use_count() - 1;
3444 }
3445 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003446 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003447 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003448 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003449 }
3450 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003451
Greg Claytonc9660542012-02-05 02:38:54 +00003452 case 's':
3453 case 'S':
3454 {
3455 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3456 if (symbol_vendor)
3457 {
3458 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3459 if (symbol_file)
3460 {
3461 if (format_char == 'S')
3462 {
3463 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3464 // Dump symbol file only if different from module file
3465 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3466 {
3467 print_space = false;
3468 break;
3469 }
3470 // Add a newline and indent past the index
3471 strm.Printf ("\n%*s", indent, "");
3472 }
3473 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3474 dump_object_name = true;
3475 break;
3476 }
3477 }
3478 strm.Printf("%.*s", width, "<NONE>");
3479 }
3480 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003481
Greg Claytonc9660542012-02-05 02:38:54 +00003482 case 'm':
3483 module->GetModificationTime().Dump(&strm, width);
3484 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003485
Greg Claytonc9660542012-02-05 02:38:54 +00003486 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003487 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003488 break;
3489
3490 case 'u':
3491 DumpModuleUUID(strm, module);
3492 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003493
Greg Claytonc9660542012-02-05 02:38:54 +00003494 default:
3495 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003496 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003497
Greg Claytonc9660542012-02-05 02:38:54 +00003498 }
3499 if (dump_object_name)
3500 {
3501 const char *object_name = module->GetObjectName().GetCString();
3502 if (object_name)
3503 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003504 }
3505 strm.EOL();
3506 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003507
Greg Claytoneffe5c92011-05-03 22:09:39 +00003508 CommandOptions m_options;
3509};
3510
3511OptionDefinition
3512CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3513{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003514 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3515 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
3516 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
3517 { 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."},
3518 { 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)."},
3519 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3520 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3521 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3522 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3523 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3524 { 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."},
3525 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3526 { 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."},
3527 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeNone, "Display the module pointer."},
3528 { 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."},
3529 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003530};
3531
Jason Molenda380241a2012-07-12 00:20:07 +00003532#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003533
Jason Molenda380241a2012-07-12 00:20:07 +00003534//----------------------------------------------------------------------
3535// Lookup unwind information in images
3536//----------------------------------------------------------------------
3537
3538class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3539{
3540public:
3541
3542 enum
3543 {
3544 eLookupTypeInvalid = -1,
3545 eLookupTypeAddress = 0,
3546 eLookupTypeSymbol,
3547 eLookupTypeFunction,
3548 eLookupTypeFunctionOrSymbol,
3549 kNumLookupTypes
3550 };
3551
3552 class CommandOptions : public Options
3553 {
3554 public:
3555
3556 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003557 Options(interpreter),
3558 m_type(eLookupTypeInvalid),
3559 m_str(),
3560 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003561 {
3562 }
3563
3564 virtual
3565 ~CommandOptions ()
3566 {
3567 }
3568
3569 virtual Error
3570 SetOptionValue (uint32_t option_idx, const char *option_arg)
3571 {
3572 Error error;
3573
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003574 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003575
3576 switch (short_option)
3577 {
3578 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003579 {
3580 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003581 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003582 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003583 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003584 if (m_addr == LLDB_INVALID_ADDRESS)
3585 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3586 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003587 }
Jason Molenda380241a2012-07-12 00:20:07 +00003588
3589 case 'n':
Jason Molenda535ab862013-04-23 04:30:57 +00003590 {
Jason Molenda380241a2012-07-12 00:20:07 +00003591 m_str = option_arg;
3592 m_type = eLookupTypeFunctionOrSymbol;
3593 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003594 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003595
3596 default:
3597 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3598 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003599 }
3600
3601 return error;
3602 }
3603
3604 void
3605 OptionParsingStarting ()
3606 {
3607 m_type = eLookupTypeInvalid;
3608 m_str.clear();
3609 m_addr = LLDB_INVALID_ADDRESS;
3610 }
3611
3612 const OptionDefinition*
3613 GetDefinitions ()
3614 {
3615 return g_option_table;
3616 }
3617
3618 // Options table: Required for subclasses of Options.
3619
3620 static OptionDefinition g_option_table[];
3621
3622 // Instance variables to hold the values for command options.
3623
3624 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3625 std::string m_str; // Holds name lookup
3626 lldb::addr_t m_addr; // Holds the address to lookup
3627 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003628
Jason Molenda380241a2012-07-12 00:20:07 +00003629 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3630 CommandObjectParsed (interpreter,
3631 "target modules show-unwind",
3632 "Show synthesized unwind instructions for a function.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003633 NULL,
3634 eFlagRequiresTarget |
3635 eFlagRequiresProcess |
3636 eFlagProcessMustBeLaunched |
3637 eFlagProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003638 m_options (interpreter)
3639 {
3640 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003641
Jason Molenda380241a2012-07-12 00:20:07 +00003642 virtual
3643 ~CommandObjectTargetModulesShowUnwind ()
3644 {
3645 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003646
Jason Molenda380241a2012-07-12 00:20:07 +00003647 virtual
3648 Options *
3649 GetOptions ()
3650 {
3651 return &m_options;
3652 }
3653
3654protected:
3655 bool
3656 DoExecute (Args& command,
3657 CommandReturnObject &result)
3658 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003659 Target *target = m_exe_ctx.GetTargetPtr();
3660 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda380241a2012-07-12 00:20:07 +00003661 ABI *abi = NULL;
3662 if (process)
3663 abi = process->GetABI().get();
3664
3665 if (process == NULL)
3666 {
3667 result.AppendError ("You must have a process running to use this command.");
3668 result.SetStatus (eReturnStatusFailed);
3669 return false;
3670 }
3671
3672 ThreadList threads(process->GetThreadList());
3673 if (threads.GetSize() == 0)
3674 {
3675 result.AppendError ("The process must be paused to use this command.");
3676 result.SetStatus (eReturnStatusFailed);
3677 return false;
3678 }
3679
3680 ThreadSP thread(threads.GetThreadAtIndex(0));
3681 if (thread.get() == NULL)
3682 {
3683 result.AppendError ("The process must be paused to use this command.");
3684 result.SetStatus (eReturnStatusFailed);
3685 return false;
3686 }
3687
Jason Molenda535ab862013-04-23 04:30:57 +00003688 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003689
Jason Molenda380241a2012-07-12 00:20:07 +00003690 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3691 {
Jason Molenda380241a2012-07-12 00:20:07 +00003692 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003693 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3694 }
3695 else if (m_options.m_type == eLookupTypeAddress && target)
3696 {
3697 Address addr;
3698 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003699 {
3700 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003701 ModuleSP module_sp (addr.GetModule());
3702 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3703 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003704 {
Jason Molenda535ab862013-04-23 04:30:57 +00003705 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003706 }
Jason Molenda535ab862013-04-23 04:30:57 +00003707 }
3708 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003709 else
3710 {
3711 result.AppendError ("address-expression or function name option must be specified.");
3712 result.SetStatus (eReturnStatusFailed);
3713 return false;
3714 }
Jason Molenda380241a2012-07-12 00:20:07 +00003715
Jason Molenda535ab862013-04-23 04:30:57 +00003716 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003717 if (num_matches == 0)
3718 {
3719 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3720 result.SetStatus (eReturnStatusFailed);
3721 return false;
3722 }
3723
Jason Molenda535ab862013-04-23 04:30:57 +00003724 for (uint32_t idx = 0; idx < num_matches; idx++)
3725 {
3726 SymbolContext sc;
3727 sc_list.GetContextAtIndex(idx, sc);
3728 if (sc.symbol == NULL && sc.function == NULL)
3729 continue;
3730 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3731 continue;
3732 AddressRange range;
3733 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3734 continue;
3735 if (!range.GetBaseAddress().IsValid())
3736 continue;
3737 ConstString funcname(sc.GetFunctionName());
3738 if (funcname.IsEmpty())
3739 continue;
3740 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3741 if (abi)
3742 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003743
Jason Molenda535ab862013-04-23 04:30:57 +00003744 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3745 if (func_unwinders_sp.get() == NULL)
3746 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003747
Jason Molenda34549b82015-01-13 06:04:04 +00003748 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 +00003749
Todd Fiala05625242014-08-25 20:29:09 +00003750 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread.get(), -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003751 if (non_callsite_unwind_plan.get())
3752 {
Jason Molenda34549b82015-01-13 06:04:04 +00003753 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 +00003754 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003755 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003756 if (callsite_unwind_plan.get())
3757 {
Jason Molenda34549b82015-01-13 06:04:04 +00003758 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003759 }
Jason Molenda535ab862013-04-23 04:30:57 +00003760 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3761 if (fast_unwind_plan.get())
3762 {
Jason Molenda34549b82015-01-13 06:04:04 +00003763 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003764 }
3765
Jason Molenda34549b82015-01-13 06:04:04 +00003766 result.GetOutputStream().Printf("\n");
3767
3768 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread.get(), 0);
3769 if (assembly_sp)
3770 {
3771 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3772 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3773 result.GetOutputStream().Printf("\n");
3774 }
3775
3776
3777 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3778 if (ehframe_sp)
3779 {
3780 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3781 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3782 result.GetOutputStream().Printf("\n");
3783 }
3784
3785 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread.get(), 0);
3786 if (ehframe_augmented_sp)
3787 {
3788 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3789 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3790 result.GetOutputStream().Printf("\n");
3791 }
3792
3793 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3794 if (compact_unwind_sp)
3795 {
3796 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3797 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3798 result.GetOutputStream().Printf("\n");
3799 }
3800
3801 if (fast_unwind_plan)
3802 {
3803 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3804 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3805 result.GetOutputStream().Printf("\n");
3806 }
3807
3808 ABISP abi_sp = process->GetABI();
3809 if (abi_sp)
3810 {
3811 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3812 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3813 {
3814 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3815 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3816 result.GetOutputStream().Printf("\n");
3817 }
3818
3819 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3820 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3821 {
3822 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3823 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3824 result.GetOutputStream().Printf("\n");
3825 }
3826 }
Jason Molenda535ab862013-04-23 04:30:57 +00003827
3828 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003829 }
3830 return result.Succeeded();
3831 }
3832
3833 CommandOptions m_options;
3834};
3835
3836OptionDefinition
3837CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3838{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003839 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3840 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3841 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jason Molenda380241a2012-07-12 00:20:07 +00003842};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003843
3844//----------------------------------------------------------------------
3845// Lookup information in images
3846//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003847class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003848{
3849public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003850 enum
3851 {
3852 eLookupTypeInvalid = -1,
3853 eLookupTypeAddress = 0,
3854 eLookupTypeSymbol,
3855 eLookupTypeFileLine, // Line is optional
3856 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003857 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003858 eLookupTypeType,
3859 kNumLookupTypes
3860 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003861
Greg Claytoneffe5c92011-05-03 22:09:39 +00003862 class CommandOptions : public Options
3863 {
3864 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003865 CommandOptions (CommandInterpreter &interpreter) :
3866 Options(interpreter)
3867 {
3868 OptionParsingStarting();
3869 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003870
Greg Claytoneffe5c92011-05-03 22:09:39 +00003871 virtual
3872 ~CommandOptions ()
3873 {
3874 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003875
Greg Claytoneffe5c92011-05-03 22:09:39 +00003876 virtual Error
3877 SetOptionValue (uint32_t option_idx, const char *option_arg)
3878 {
3879 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003880
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003881 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003882
Greg Claytoneffe5c92011-05-03 22:09:39 +00003883 switch (short_option)
3884 {
3885 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003886 {
3887 m_type = eLookupTypeAddress;
3888 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3889 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3890 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003891 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003892
Greg Claytoneffe5c92011-05-03 22:09:39 +00003893 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003894 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003895 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003896 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003897 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003898
Greg Claytoneffe5c92011-05-03 22:09:39 +00003899 case 's':
3900 m_str = option_arg;
3901 m_type = eLookupTypeSymbol;
3902 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003903
Greg Claytoneffe5c92011-05-03 22:09:39 +00003904 case 'f':
3905 m_file.SetFile (option_arg, false);
3906 m_type = eLookupTypeFileLine;
3907 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003908
Greg Claytoneffe5c92011-05-03 22:09:39 +00003909 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003910 m_include_inlines = false;
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 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003914 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003915 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003916 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003917 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003918 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003919 m_type = eLookupTypeFileLine;
3920 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003921
Greg Claytonc4a8a762012-05-15 18:43:44 +00003922 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003923 m_str = option_arg;
3924 m_type = eLookupTypeFunction;
3925 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003926
Greg Claytonc4a8a762012-05-15 18:43:44 +00003927 case 'n':
3928 m_str = option_arg;
3929 m_type = eLookupTypeFunctionOrSymbol;
3930 break;
3931
Greg Claytoneffe5c92011-05-03 22:09:39 +00003932 case 't':
3933 m_str = option_arg;
3934 m_type = eLookupTypeType;
3935 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003936
Greg Claytoneffe5c92011-05-03 22:09:39 +00003937 case 'v':
3938 m_verbose = 1;
3939 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003940
Sean Callanand38b4a92012-06-06 20:49:55 +00003941 case 'A':
3942 m_print_all = true;
3943 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003944
Greg Claytoneffe5c92011-05-03 22:09:39 +00003945 case 'r':
3946 m_use_regex = true;
3947 break;
3948 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003949
Greg Claytoneffe5c92011-05-03 22:09:39 +00003950 return error;
3951 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003952
Greg Claytoneffe5c92011-05-03 22:09:39 +00003953 void
3954 OptionParsingStarting ()
3955 {
3956 m_type = eLookupTypeInvalid;
3957 m_str.clear();
3958 m_file.Clear();
3959 m_addr = LLDB_INVALID_ADDRESS;
3960 m_offset = 0;
3961 m_line_number = 0;
3962 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003963 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003964 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003965 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003966 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003967
Greg Claytoneffe5c92011-05-03 22:09:39 +00003968 const OptionDefinition*
3969 GetDefinitions ()
3970 {
3971 return g_option_table;
3972 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003973
Greg Claytoneffe5c92011-05-03 22:09:39 +00003974 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003975
Greg Claytoneffe5c92011-05-03 22:09:39 +00003976 static OptionDefinition g_option_table[];
3977 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3978 std::string m_str; // Holds name lookup
3979 FileSpec m_file; // Files for file lookups
3980 lldb::addr_t m_addr; // Holds the address to lookup
3981 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3982 uint32_t m_line_number; // Line number for file+line lookups
3983 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00003984 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003985 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00003986 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003987 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003988
Greg Claytoneffe5c92011-05-03 22:09:39 +00003989 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003990 CommandObjectParsed (interpreter,
3991 "target modules lookup",
3992 "Look up information within executable and dependent shared library images.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003993 NULL,
3994 eFlagRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00003995 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003996 {
3997 CommandArgumentEntry arg;
3998 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003999
Greg Claytoneffe5c92011-05-03 22:09:39 +00004000 // Define the first (and only) variant of this arg.
4001 file_arg.arg_type = eArgTypeFilename;
4002 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004003
Greg Claytoneffe5c92011-05-03 22:09:39 +00004004 // There is only one variant this argument could be; put it into the argument entry.
4005 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004006
Greg Claytoneffe5c92011-05-03 22:09:39 +00004007 // Push the data for the first argument into the m_arguments vector.
4008 m_arguments.push_back (arg);
4009 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004010
Greg Claytoneffe5c92011-05-03 22:09:39 +00004011 virtual
4012 ~CommandObjectTargetModulesLookup ()
4013 {
4014 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004015
Greg Claytoneffe5c92011-05-03 22:09:39 +00004016 virtual Options *
4017 GetOptions ()
4018 {
4019 return &m_options;
4020 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004021
Sean Callanand38b4a92012-06-06 20:49:55 +00004022 bool
4023 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4024 {
4025 switch (m_options.m_type)
4026 {
4027 case eLookupTypeAddress:
4028 case eLookupTypeFileLine:
4029 case eLookupTypeFunction:
4030 case eLookupTypeFunctionOrSymbol:
4031 case eLookupTypeSymbol:
4032 default:
4033 return false;
4034 case eLookupTypeType:
4035 break;
4036 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004037
Jason Molendab57e4a12013-11-04 09:33:30 +00004038 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004039
Sean Callanand38b4a92012-06-06 20:49:55 +00004040 if (!frame)
4041 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004042
Sean Callanand38b4a92012-06-06 20:49:55 +00004043 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004044
Sean Callanand38b4a92012-06-06 20:49:55 +00004045 if (!sym_ctx.module_sp)
4046 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004047
Sean Callanand38b4a92012-06-06 20:49:55 +00004048 switch (m_options.m_type)
4049 {
4050 default:
4051 return false;
4052 case eLookupTypeType:
4053 if (!m_options.m_str.empty())
4054 {
4055 if (LookupTypeHere (m_interpreter,
4056 result.GetOutputStream(),
4057 sym_ctx,
4058 m_options.m_str.c_str(),
4059 m_options.m_use_regex))
4060 {
4061 result.SetStatus(eReturnStatusSuccessFinishResult);
4062 return true;
4063 }
4064 }
4065 break;
4066 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004067
Sean Callanand38b4a92012-06-06 20:49:55 +00004068 return true;
4069 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004070
Greg Claytoneffe5c92011-05-03 22:09:39 +00004071 bool
4072 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4073 {
4074 switch (m_options.m_type)
4075 {
4076 case eLookupTypeAddress:
4077 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4078 {
4079 if (LookupAddressInModule (m_interpreter,
4080 result.GetOutputStream(),
4081 module,
Greg Clayton2501e5e2015-01-15 02:59:20 +00004082 eSymbolContextEverything | (m_options.m_verbose ? eSymbolContextVariable : 0),
Greg Claytoneffe5c92011-05-03 22:09:39 +00004083 m_options.m_addr,
4084 m_options.m_offset,
4085 m_options.m_verbose))
4086 {
4087 result.SetStatus(eReturnStatusSuccessFinishResult);
4088 return true;
4089 }
4090 }
4091 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004092
Greg Claytoneffe5c92011-05-03 22:09:39 +00004093 case eLookupTypeSymbol:
4094 if (!m_options.m_str.empty())
4095 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004096 if (LookupSymbolInModule (m_interpreter,
4097 result.GetOutputStream(),
4098 module,
4099 m_options.m_str.c_str(),
4100 m_options.m_use_regex,
4101 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004102 {
4103 result.SetStatus(eReturnStatusSuccessFinishResult);
4104 return true;
4105 }
4106 }
4107 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004108
Greg Claytoneffe5c92011-05-03 22:09:39 +00004109 case eLookupTypeFileLine:
4110 if (m_options.m_file)
4111 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004112 if (LookupFileAndLineInModule (m_interpreter,
4113 result.GetOutputStream(),
4114 module,
4115 m_options.m_file,
4116 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004117 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004118 m_options.m_verbose))
4119 {
4120 result.SetStatus(eReturnStatusSuccessFinishResult);
4121 return true;
4122 }
4123 }
4124 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004125
4126 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004127 case eLookupTypeFunction:
4128 if (!m_options.m_str.empty())
4129 {
4130 if (LookupFunctionInModule (m_interpreter,
4131 result.GetOutputStream(),
4132 module,
4133 m_options.m_str.c_str(),
4134 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004135 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004136 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004137 m_options.m_verbose))
4138 {
4139 result.SetStatus(eReturnStatusSuccessFinishResult);
4140 return true;
4141 }
4142 }
4143 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004144
Greg Claytoneffe5c92011-05-03 22:09:39 +00004145 case eLookupTypeType:
4146 if (!m_options.m_str.empty())
4147 {
4148 if (LookupTypeInModule (m_interpreter,
4149 result.GetOutputStream(),
4150 module,
4151 m_options.m_str.c_str(),
4152 m_options.m_use_regex))
4153 {
4154 result.SetStatus(eReturnStatusSuccessFinishResult);
4155 return true;
4156 }
4157 }
4158 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004159
Greg Claytoneffe5c92011-05-03 22:09:39 +00004160 default:
4161 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4162 syntax_error = true;
4163 break;
4164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004165
Greg Claytoneffe5c92011-05-03 22:09:39 +00004166 result.SetStatus (eReturnStatusFailed);
4167 return false;
4168 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004169
Jim Ingham5a988412012-06-08 21:56:10 +00004170protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004171 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004172 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004173 CommandReturnObject &result)
4174 {
4175 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4176 if (target == NULL)
4177 {
4178 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4179 result.SetStatus (eReturnStatusFailed);
4180 return false;
4181 }
4182 else
4183 {
4184 bool syntax_error = false;
4185 uint32_t i;
4186 uint32_t num_successful_lookups = 0;
4187 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4188 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4189 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4190 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004191
Greg Claytoneffe5c92011-05-03 22:09:39 +00004192 if (command.GetArgumentCount() == 0)
4193 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004194 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004195
Sean Callanand38b4a92012-06-06 20:49:55 +00004196 // Where it is possible to look in the current symbol context
4197 // first, try that. If this search was successful and --all
4198 // was not passed, don't print anything else.
4199 if (LookupHere (m_interpreter, result, syntax_error))
4200 {
4201 result.GetOutputStream().EOL();
4202 num_successful_lookups++;
4203 if (!m_options.m_print_all)
4204 {
4205 result.SetStatus (eReturnStatusSuccessFinishResult);
4206 return result.Succeeded();
4207 }
4208 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004209
Sean Callanand38b4a92012-06-06 20:49:55 +00004210 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004211
Enrico Granata17598482012-11-08 02:22:02 +00004212 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004213 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004214 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004215 if (num_modules > 0)
4216 {
4217 for (i = 0; i<num_modules && syntax_error == false; ++i)
4218 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004219 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004220
Sean Callanand38b4a92012-06-06 20:49:55 +00004221 if (module_pointer != current_module.get() &&
4222 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004223 {
4224 result.GetOutputStream().EOL();
4225 num_successful_lookups++;
4226 }
4227 }
4228 }
4229 else
4230 {
4231 result.AppendError ("the target has no associated executable images");
4232 result.SetStatus (eReturnStatusFailed);
4233 return false;
4234 }
4235 }
4236 else
4237 {
4238 // Dump specified images (by basename or fullpath)
4239 const char *arg_cstr;
4240 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4241 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004242 ModuleList module_list;
4243 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4244 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004245 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004246 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004247 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004248 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004249 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004250 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004251 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004252 {
4253 result.GetOutputStream().EOL();
4254 num_successful_lookups++;
4255 }
4256 }
4257 }
4258 }
4259 else
4260 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4261 }
4262 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004263
Greg Claytoneffe5c92011-05-03 22:09:39 +00004264 if (num_successful_lookups > 0)
4265 result.SetStatus (eReturnStatusSuccessFinishResult);
4266 else
4267 result.SetStatus (eReturnStatusFailed);
4268 }
4269 return result.Succeeded();
4270 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004271
Greg Claytoneffe5c92011-05-03 22:09:39 +00004272 CommandOptions m_options;
4273};
4274
4275OptionDefinition
4276CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4277{
Zachary Turnerd37221d2014-07-09 16:31:49 +00004278 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4279 { 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 +00004280 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4281 /* 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 +00004282 false, "regex", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4283 { 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."},
4284 { 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."},
4285 { 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 +00004286 { LLDB_OPT_SET_FROM_TO(3,5),
Zachary Turnerd37221d2014-07-09 16:31:49 +00004287 false, "no-inlines", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4288 { 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."},
4289 { 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."},
4290 { 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."},
4291 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4292 { 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."},
4293 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004294};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004295
4296
Jim Ingham9575d842011-03-11 03:53:59 +00004297#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004298
4299//-------------------------------------------------------------------------
4300// CommandObjectMultiwordImageSearchPaths
4301//-------------------------------------------------------------------------
4302
Greg Claytoneffe5c92011-05-03 22:09:39 +00004303class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004304{
4305public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004306 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4307 CommandObjectMultiword (interpreter,
4308 "target modules search-paths",
4309 "A set of commands for operating on debugger target image search paths.",
4310 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004311 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004312 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4313 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4314 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4315 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4316 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004317 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004318
Greg Claytoneffe5c92011-05-03 22:09:39 +00004319 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004320 {
4321 }
4322};
4323
Greg Claytoneffe5c92011-05-03 22:09:39 +00004324
4325
4326#pragma mark CommandObjectTargetModules
4327
4328//-------------------------------------------------------------------------
4329// CommandObjectTargetModules
4330//-------------------------------------------------------------------------
4331
4332class CommandObjectTargetModules : public CommandObjectMultiword
4333{
4334public:
4335 //------------------------------------------------------------------
4336 // Constructors and Destructors
4337 //------------------------------------------------------------------
4338 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4339 CommandObjectMultiword (interpreter,
4340 "target modules",
4341 "A set of commands for accessing information for one or more target modules.",
4342 "target modules <sub-command> ...")
4343 {
4344 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4345 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004346 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4347 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4348 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4349 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004350 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004351
4352 }
4353 virtual
4354 ~CommandObjectTargetModules()
4355 {
4356 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004357
Greg Claytoneffe5c92011-05-03 22:09:39 +00004358private:
4359 //------------------------------------------------------------------
4360 // For CommandObjectTargetModules only
4361 //------------------------------------------------------------------
4362 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4363};
4364
4365
Greg Claytone72dfb32012-02-24 01:59:29 +00004366
Jim Ingham5a988412012-06-08 21:56:10 +00004367class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004368{
4369public:
4370 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004371 CommandObjectParsed (interpreter,
4372 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004373 "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 +00004374 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004375 m_option_group (interpreter),
4376 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4377 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4378
Greg Claytone72dfb32012-02-24 01:59:29 +00004379 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004380 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4381 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4382 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4383 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004384 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004385
Greg Claytone72dfb32012-02-24 01:59:29 +00004386 virtual
4387 ~CommandObjectTargetSymbolsAdd ()
4388 {
4389 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004390
Greg Claytonc7bece562013-01-25 18:06:21 +00004391 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00004392 HandleArgumentCompletion (Args &input,
4393 int &cursor_index,
4394 int &cursor_char_position,
4395 OptionElementVector &opt_element_vector,
4396 int match_start_point,
4397 int max_return_elements,
4398 bool &word_complete,
4399 StringList &matches)
4400 {
4401 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4402 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004403
Jim Ingham5a988412012-06-08 21:56:10 +00004404 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4405 CommandCompletions::eDiskFileCompletion,
4406 completion_str.c_str(),
4407 match_start_point,
4408 max_return_elements,
4409 NULL,
4410 word_complete,
4411 matches);
4412 return matches.GetSize();
4413 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004414
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004415 virtual Options *
4416 GetOptions ()
4417 {
4418 return &m_option_group;
4419 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004420
Jim Ingham5a988412012-06-08 21:56:10 +00004421protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004422 bool
4423 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004424 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004425 bool &flush,
4426 CommandReturnObject &result)
4427 {
Greg Clayton89deb062012-12-12 01:15:30 +00004428 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4429 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004430 {
4431 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004432 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004433
Greg Clayton89deb062012-12-12 01:15:30 +00004434 if (!module_spec.GetUUID().IsValid())
4435 {
4436 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4437 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4438 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004439 // We now have a module that represents a symbol file
4440 // that can be used for a module that might exist in the
4441 // current target, so we need to find that module in the
4442 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004443 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004444
Greg Clayton9aae0a12013-05-15 19:52:08 +00004445 size_t num_matches = 0;
4446 // First extract all module specs from the symbol file
4447 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004448 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004449 {
4450 // Now extract the module spec that matches the target architecture
4451 ModuleSpec target_arch_module_spec;
4452 ModuleSpec symfile_module_spec;
4453 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4454 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4455 {
4456 // See if it has a UUID?
4457 if (symfile_module_spec.GetUUID().IsValid())
4458 {
4459 // It has a UUID, look for this UUID in the target modules
4460 ModuleSpec symfile_uuid_module_spec;
4461 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4462 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4463 }
4464 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004465
Greg Clayton9aae0a12013-05-15 19:52:08 +00004466 if (num_matches == 0)
4467 {
4468 // No matches yet, iterate through the module specs to find a UUID value that
4469 // we can match up to an image in our target
4470 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4471 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4472 {
4473 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4474 {
4475 if (symfile_module_spec.GetUUID().IsValid())
4476 {
4477 // It has a UUID, look for this UUID in the target modules
4478 ModuleSpec symfile_uuid_module_spec;
4479 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4480 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004481 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004482 }
4483 }
4484 }
4485 }
4486
4487 // Just try to match up the file by basename if we have no matches at this point
4488 if (num_matches == 0)
4489 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004490
Greg Clayton91c0e742013-01-11 23:44:27 +00004491 while (num_matches == 0)
4492 {
4493 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4494 // Empty string returned, lets bail
4495 if (!filename_no_extension)
4496 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004497
Greg Clayton91c0e742013-01-11 23:44:27 +00004498 // Check if there was no extension to strip and the basename is the same
4499 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4500 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004501
Greg Clayton91c0e742013-01-11 23:44:27 +00004502 // Replace basename with one less extension
4503 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004504
Greg Clayton91c0e742013-01-11 23:44:27 +00004505 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4506 }
4507
Greg Clayton89deb062012-12-12 01:15:30 +00004508 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004509 {
Greg Clayton89deb062012-12-12 01:15:30 +00004510 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4511 }
4512 else if (num_matches == 1)
4513 {
4514 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004515
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004516 // The module has not yet created its symbol vendor, we can just
4517 // give the existing target module the symfile path to use for
4518 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004519 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004520
Greg Clayton136dff82012-12-14 02:15:00 +00004521 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004522 if (symbol_vendor)
4523 {
4524 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004525
Greg Clayton89deb062012-12-12 01:15:30 +00004526 if (symbol_file)
4527 {
4528 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004529
Greg Clayton89deb062012-12-12 01:15:30 +00004530 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4531 {
4532 // Provide feedback that the symfile has been successfully added.
4533 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004534 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004535 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004536 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004537
Greg Clayton89deb062012-12-12 01:15:30 +00004538 // Let clients know something changed in the module
4539 // if it is currently loaded
4540 ModuleList module_list;
4541 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004542 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004543
Greg Clayton91c0e742013-01-11 23:44:27 +00004544 // Make sure we load any scripting resources that may be embedded
4545 // in the debug info files in case the platform supports that.
4546 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004547 StreamString feedback_stream;
4548 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004549 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004550 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4551 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4552 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004553 else if (feedback_stream.GetSize())
4554 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004555
Greg Clayton89deb062012-12-12 01:15:30 +00004556 flush = true;
4557 result.SetStatus (eReturnStatusSuccessFinishResult);
4558 return true;
4559 }
4560 }
4561 }
4562 // Clear the symbol file spec if anything went wrong
4563 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004564 }
4565
4566 if (module_spec.GetUUID().IsValid())
4567 {
4568 StreamString ss_symfile_uuid;
4569 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4570 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4571 symfile_path,
4572 ss_symfile_uuid.GetData(),
4573 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4574 ? "\n please specify the full path to the symbol file"
4575 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004576 }
4577 else
4578 {
Greg Clayton89deb062012-12-12 01:15:30 +00004579 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4580 symfile_path,
4581 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4582 ? "\n please specify the full path to the symbol file"
4583 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004584 }
4585 }
4586 else
4587 {
4588 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004589 }
Greg Clayton89deb062012-12-12 01:15:30 +00004590 result.SetStatus (eReturnStatusFailed);
4591 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004592 }
4593
Greg Claytone72dfb32012-02-24 01:59:29 +00004594 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004595 DoExecute (Args& args,
Greg Claytone72dfb32012-02-24 01:59:29 +00004596 CommandReturnObject &result)
4597 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004598 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004599 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004600 bool flush = false;
4601 ModuleSpec module_spec;
4602 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4603 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4604 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004605
Greg Claytonf9fc6092013-01-09 19:44:40 +00004606 const size_t argc = args.GetArgumentCount();
4607 if (argc == 0)
4608 {
4609 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004610 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004611 bool success = false;
4612 bool error_set = false;
4613 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004614 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004615 Process *process = m_exe_ctx.GetProcessPtr();
4616 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004617 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004618 const StateType process_state = process->GetState();
4619 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004620 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004621 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004622 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004623 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004624 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4625 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004626 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004627 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004628 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004629 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4630 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004631 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004632 module_spec.GetUUID() = frame_module_sp->GetUUID();
4633 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004634 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004635 else
4636 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004637 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004638 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004639 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004640 }
4641 else
4642 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004643 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004644 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004645 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004646 }
4647 else
4648 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004649 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004650 error_set = true;
4651 }
4652 }
4653 else
4654 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004655 result.AppendError ("a process must exist in order to use the --frame option");
4656 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004657 }
4658 }
4659 else
4660 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004661 if (uuid_option_set)
4662 {
4663 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4664 success |= module_spec.GetUUID().IsValid();
4665 }
4666 else if (file_option_set)
4667 {
4668 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4669 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4670 if (module_sp)
4671 {
4672 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4673 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4674 module_spec.GetUUID() = module_sp->GetUUID();
4675 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4676 }
4677 else
4678 {
4679 module_spec.GetArchitecture() = target->GetArchitecture();
4680 }
4681 success |= module_spec.GetFileSpec().Exists();
4682 }
4683 }
4684
4685 if (success)
4686 {
4687 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4688 {
4689 if (module_spec.GetSymbolFileSpec())
4690 success = AddModuleSymbols (target, module_spec, flush, result);
4691 }
4692 }
4693
4694 if (!success && !error_set)
4695 {
4696 StreamString error_strm;
4697 if (uuid_option_set)
4698 {
4699 error_strm.PutCString("unable to find debug symbols for UUID ");
4700 module_spec.GetUUID().Dump (&error_strm);
4701 }
4702 else if (file_option_set)
4703 {
4704 error_strm.PutCString("unable to find debug symbols for the executable file ");
4705 error_strm << module_spec.GetFileSpec();
4706 }
4707 else if (frame_option_set)
4708 {
4709 error_strm.PutCString("unable to find debug symbols for the current frame");
4710 }
4711 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004712 }
4713 }
4714 else
4715 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004716 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4717 }
4718 }
4719 else
4720 {
4721 if (uuid_option_set)
4722 {
4723 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4724 }
4725 else if (file_option_set)
4726 {
4727 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4728 }
4729 else if (frame_option_set)
4730 {
4731 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4732 }
4733 else
4734 {
4735 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004736
Greg Claytonf9fc6092013-01-09 19:44:40 +00004737 for (size_t i=0; i<argc; ++i)
4738 {
4739 const char *symfile_path = args.GetArgumentAtIndex(i);
4740 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004741 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004742 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4743 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004744 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004745 FileSpec symfile_spec;
4746 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4747 module_spec.GetSymbolFileSpec() = symfile_spec;
4748 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004749
Greg Claytonf9fc6092013-01-09 19:44:40 +00004750 ArchSpec arch;
4751 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004752
Greg Claytonf9fc6092013-01-09 19:44:40 +00004753 if (symfile_exists)
4754 {
4755 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004756 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004757 }
4758 else
4759 {
4760 char resolved_symfile_path[PATH_MAX];
4761 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4762 {
4763 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4764 {
4765 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4766 break;
4767 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004768 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004769 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4770 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004771 }
4772 }
4773 }
4774 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004775 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004776
Greg Claytonf9fc6092013-01-09 19:44:40 +00004777 if (flush)
4778 {
4779 Process *process = m_exe_ctx.GetProcessPtr();
4780 if (process)
4781 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004782 }
4783 return result.Succeeded();
4784 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004785
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004786 OptionGroupOptions m_option_group;
4787 OptionGroupUUID m_uuid_option_group;
4788 OptionGroupFile m_file_option;
4789 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004790};
4791
4792
4793#pragma mark CommandObjectTargetSymbols
4794
4795//-------------------------------------------------------------------------
4796// CommandObjectTargetSymbols
4797//-------------------------------------------------------------------------
4798
4799class CommandObjectTargetSymbols : public CommandObjectMultiword
4800{
4801public:
4802 //------------------------------------------------------------------
4803 // Constructors and Destructors
4804 //------------------------------------------------------------------
4805 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4806 CommandObjectMultiword (interpreter,
4807 "target symbols",
4808 "A set of commands for adding and managing debug symbol files.",
4809 "target symbols <sub-command> ...")
4810 {
4811 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004812
Greg Claytone72dfb32012-02-24 01:59:29 +00004813 }
4814 virtual
4815 ~CommandObjectTargetSymbols()
4816 {
4817 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004818
Greg Claytone72dfb32012-02-24 01:59:29 +00004819private:
4820 //------------------------------------------------------------------
4821 // For CommandObjectTargetModules only
4822 //------------------------------------------------------------------
4823 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4824};
4825
4826
Jim Ingham9575d842011-03-11 03:53:59 +00004827#pragma mark CommandObjectTargetStopHookAdd
4828
4829//-------------------------------------------------------------------------
4830// CommandObjectTargetStopHookAdd
4831//-------------------------------------------------------------------------
4832
Greg Clayton44d93782014-01-27 23:43:24 +00004833class CommandObjectTargetStopHookAdd :
4834 public CommandObjectParsed,
4835 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004836{
4837public:
4838
4839 class CommandOptions : public Options
4840 {
4841 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004842 CommandOptions (CommandInterpreter &interpreter) :
4843 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004844 m_line_start(0),
4845 m_line_end (UINT_MAX),
4846 m_func_name_type_mask (eFunctionNameTypeAuto),
4847 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004848 m_thread_specified (false),
4849 m_use_one_liner (false),
4850 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004851 {
4852 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004853
Jim Ingham9575d842011-03-11 03:53:59 +00004854 ~CommandOptions () {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004855
Greg Claytone0d378b2011-03-24 21:19:54 +00004856 const OptionDefinition*
Jim Ingham9575d842011-03-11 03:53:59 +00004857 GetDefinitions ()
4858 {
4859 return g_option_table;
4860 }
4861
4862 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +00004863 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham9575d842011-03-11 03:53:59 +00004864 {
4865 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004866 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004867 bool success;
4868
4869 switch (short_option)
4870 {
4871 case 'c':
4872 m_class_name = option_arg;
4873 m_sym_ctx_specified = true;
4874 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004875
Jim Ingham9575d842011-03-11 03:53:59 +00004876 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004877 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004878 if (!success)
4879 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004880 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004881 break;
4882 }
4883 m_sym_ctx_specified = true;
4884 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004885
Jim Ingham9575d842011-03-11 03:53:59 +00004886 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004887 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004888 if (!success)
4889 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004890 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004891 break;
4892 }
4893 m_sym_ctx_specified = true;
4894 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004895
Sean Callanand4a7c122012-02-11 01:22:21 +00004896 case 'i':
4897 m_no_inlines = true;
4898 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004899
Jim Ingham9575d842011-03-11 03:53:59 +00004900 case 'n':
4901 m_function_name = option_arg;
4902 m_func_name_type_mask |= eFunctionNameTypeAuto;
4903 m_sym_ctx_specified = true;
4904 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004905
Jim Ingham9575d842011-03-11 03:53:59 +00004906 case 'f':
4907 m_file_name = option_arg;
4908 m_sym_ctx_specified = true;
4909 break;
4910 case 's':
4911 m_module_name = option_arg;
4912 m_sym_ctx_specified = true;
4913 break;
4914 case 't' :
4915 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004916 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004917 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004918 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004919 m_thread_specified = true;
4920 }
4921 break;
4922 case 'T':
4923 m_thread_name = option_arg;
4924 m_thread_specified = true;
4925 break;
4926 case 'q':
4927 m_queue_name = option_arg;
4928 m_thread_specified = true;
4929 break;
4930 case 'x':
4931 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004932 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004933 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004934 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004935 m_thread_specified = true;
4936 }
4937 break;
Johnny Chenb1372c02011-05-02 23:47:55 +00004938 case 'o':
4939 m_use_one_liner = true;
4940 m_one_liner = option_arg;
4941 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004942 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004943 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Ingham9575d842011-03-11 03:53:59 +00004944 break;
4945 }
4946 return error;
4947 }
4948
4949 void
Greg Claytonf6b8b582011-04-13 00:18:08 +00004950 OptionParsingStarting ()
Jim Ingham9575d842011-03-11 03:53:59 +00004951 {
4952 m_class_name.clear();
4953 m_function_name.clear();
4954 m_line_start = 0;
4955 m_line_end = UINT_MAX;
4956 m_file_name.clear();
4957 m_module_name.clear();
4958 m_func_name_type_mask = eFunctionNameTypeAuto;
4959 m_thread_id = LLDB_INVALID_THREAD_ID;
4960 m_thread_index = UINT32_MAX;
4961 m_thread_name.clear();
4962 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004963
Sean Callanand4a7c122012-02-11 01:22:21 +00004964 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004965 m_sym_ctx_specified = false;
4966 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004967
4968 m_use_one_liner = false;
4969 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004970 }
4971
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004972
Greg Claytone0d378b2011-03-24 21:19:54 +00004973 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004974
Jim Ingham9575d842011-03-11 03:53:59 +00004975 std::string m_class_name;
4976 std::string m_function_name;
4977 uint32_t m_line_start;
4978 uint32_t m_line_end;
4979 std::string m_file_name;
4980 std::string m_module_name;
4981 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4982 lldb::tid_t m_thread_id;
4983 uint32_t m_thread_index;
4984 std::string m_thread_name;
4985 std::string m_queue_name;
4986 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004987 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004988 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00004989 // Instance variables to hold the values for one_liner options.
4990 bool m_use_one_liner;
4991 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00004992 };
4993
4994 Options *
4995 GetOptions ()
4996 {
4997 return &m_options;
4998 }
4999
5000 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005001 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00005002 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005003 "Add a hook to be executed when the target stops.",
5004 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005005 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005006 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005007 {
5008 }
5009
5010 ~CommandObjectTargetStopHookAdd ()
5011 {
5012 }
5013
Jim Ingham5a988412012-06-08 21:56:10 +00005014protected:
Greg Clayton44d93782014-01-27 23:43:24 +00005015 virtual void
5016 IOHandlerActivated (IOHandler &io_handler)
5017 {
5018 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5019 if (output_sp)
5020 {
5021 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5022 output_sp->Flush();
5023 }
5024 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005025
Greg Clayton44d93782014-01-27 23:43:24 +00005026 virtual void
5027 IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
5028 {
5029 if (m_stop_hook_sp)
5030 {
5031 if (line.empty())
5032 {
5033 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5034 if (error_sp)
5035 {
5036 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5037 error_sp->Flush();
5038 }
5039 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5040 if (target)
5041 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5042 }
5043 else
5044 {
5045 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5046 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5047 if (output_sp)
5048 {
5049 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5050 output_sp->Flush();
5051 }
5052 }
5053 m_stop_hook_sp.reset();
5054 }
5055 io_handler.SetIsDone(true);
5056 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005057
Jim Ingham9575d842011-03-11 03:53:59 +00005058 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005059 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005060 {
Greg Clayton44d93782014-01-27 23:43:24 +00005061 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005062
Jim Ingham893c9322014-11-22 01:42:44 +00005063 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005064 if (target)
5065 {
Greg Clayton44d93782014-01-27 23:43:24 +00005066 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005067
5068 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005069 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005070 if (m_options.m_sym_ctx_specified)
5071 {
5072 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005073
Jim Ingham9575d842011-03-11 03:53:59 +00005074 if (!m_options.m_module_name.empty())
5075 {
5076 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5077 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005078
Jim Ingham9575d842011-03-11 03:53:59 +00005079 if (!m_options.m_class_name.empty())
5080 {
5081 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5082 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005083
Jim Ingham9575d842011-03-11 03:53:59 +00005084 if (!m_options.m_file_name.empty())
5085 {
5086 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5087 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005088
Jim Ingham9575d842011-03-11 03:53:59 +00005089 if (m_options.m_line_start != 0)
5090 {
5091 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5092 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005093
Jim Ingham9575d842011-03-11 03:53:59 +00005094 if (m_options.m_line_end != UINT_MAX)
5095 {
5096 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5097 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005098
Jim Ingham9575d842011-03-11 03:53:59 +00005099 if (!m_options.m_function_name.empty())
5100 {
5101 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5102 }
5103 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005104
Jim Ingham9575d842011-03-11 03:53:59 +00005105 if (specifier_ap.get())
5106 new_hook_sp->SetSpecifier (specifier_ap.release());
5107
5108 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005109
Jim Ingham9575d842011-03-11 03:53:59 +00005110 if (m_options.m_thread_specified)
5111 {
5112 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005113
Jim Ingham9575d842011-03-11 03:53:59 +00005114 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5115 {
5116 thread_spec->SetTID (m_options.m_thread_id);
5117 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005118
Jim Ingham9575d842011-03-11 03:53:59 +00005119 if (m_options.m_thread_index != UINT32_MAX)
5120 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005121
Jim Ingham9575d842011-03-11 03:53:59 +00005122 if (!m_options.m_thread_name.empty())
5123 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005124
Jim Ingham9575d842011-03-11 03:53:59 +00005125 if (!m_options.m_queue_name.empty())
5126 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005127
Jim Ingham9575d842011-03-11 03:53:59 +00005128 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005129
Jim Ingham9575d842011-03-11 03:53:59 +00005130 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005131 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005132 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005133 // Use one-liner.
5134 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005135 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005136 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005137 else
Jim Ingham9575d842011-03-11 03:53:59 +00005138 {
Greg Clayton44d93782014-01-27 23:43:24 +00005139 m_stop_hook_sp = new_hook_sp;
5140 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
5141 *this, // IOHandlerDelegate
5142 true, // Run IOHandler in async mode
5143 NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
5144
Jim Ingham9575d842011-03-11 03:53:59 +00005145 }
Jim Ingham9575d842011-03-11 03:53:59 +00005146 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5147 }
5148 else
5149 {
Greg Clayton7260f622011-04-18 08:33:37 +00005150 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005151 result.SetStatus (eReturnStatusFailed);
5152 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005153
Jim Ingham9575d842011-03-11 03:53:59 +00005154 return result.Succeeded();
5155 }
5156private:
5157 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005158 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005159};
5160
Greg Claytone0d378b2011-03-24 21:19:54 +00005161OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005162CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5163{
Zachary Turnerd37221d2014-07-09 16:31:49 +00005164 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005165 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005166 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005167 "Set the module within which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005168 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005169 "The stop hook is run only for the thread whose index matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005170 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005171 "The stop hook is run only for the thread whose TID matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005172 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005173 "The stop hook is run only for the thread whose thread name matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005174 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005175 "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 +00005176 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005177 "Specify the source file within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005178 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005179 "Set the start of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005180 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005181 "Set the end of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005182 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005183 "Specify the class within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005184 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005185 "Set the function name within which the stop hook will be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005186 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jim Ingham9575d842011-03-11 03:53:59 +00005187};
5188
5189#pragma mark CommandObjectTargetStopHookDelete
5190
5191//-------------------------------------------------------------------------
5192// CommandObjectTargetStopHookDelete
5193//-------------------------------------------------------------------------
5194
Jim Ingham5a988412012-06-08 21:56:10 +00005195class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005196{
5197public:
5198
5199 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005200 CommandObjectParsed (interpreter,
5201 "target stop-hook delete",
5202 "Delete a stop-hook.",
5203 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005204 {
5205 }
5206
5207 ~CommandObjectTargetStopHookDelete ()
5208 {
5209 }
5210
Jim Ingham5a988412012-06-08 21:56:10 +00005211protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005212 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005213 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005214 {
Jim Ingham893c9322014-11-22 01:42:44 +00005215 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005216 if (target)
5217 {
5218 // FIXME: see if we can use the breakpoint id style parser?
5219 size_t num_args = command.GetArgumentCount();
5220 if (num_args == 0)
5221 {
5222 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5223 {
5224 result.SetStatus (eReturnStatusFailed);
5225 return false;
5226 }
5227 else
5228 {
5229 target->RemoveAllStopHooks();
5230 }
5231 }
5232 else
5233 {
5234 bool success;
5235 for (size_t i = 0; i < num_args; i++)
5236 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005237 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005238 if (!success)
5239 {
Greg Clayton7260f622011-04-18 08:33:37 +00005240 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005241 result.SetStatus(eReturnStatusFailed);
5242 return false;
5243 }
5244 success = target->RemoveStopHookByID (user_id);
5245 if (!success)
5246 {
Greg Clayton7260f622011-04-18 08:33:37 +00005247 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005248 result.SetStatus(eReturnStatusFailed);
5249 return false;
5250 }
5251 }
5252 }
5253 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5254 }
5255 else
5256 {
Greg Clayton7260f622011-04-18 08:33:37 +00005257 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005258 result.SetStatus (eReturnStatusFailed);
5259 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005260
Jim Ingham9575d842011-03-11 03:53:59 +00005261 return result.Succeeded();
5262 }
5263};
5264#pragma mark CommandObjectTargetStopHookEnableDisable
5265
5266//-------------------------------------------------------------------------
5267// CommandObjectTargetStopHookEnableDisable
5268//-------------------------------------------------------------------------
5269
Jim Ingham5a988412012-06-08 21:56:10 +00005270class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005271{
5272public:
5273
5274 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005275 CommandObjectParsed (interpreter,
5276 name,
5277 help,
5278 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005279 m_enable (enable)
5280 {
5281 }
5282
5283 ~CommandObjectTargetStopHookEnableDisable ()
5284 {
5285 }
5286
Jim Ingham5a988412012-06-08 21:56:10 +00005287protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005288 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005289 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005290 {
Jim Ingham893c9322014-11-22 01:42:44 +00005291 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005292 if (target)
5293 {
5294 // FIXME: see if we can use the breakpoint id style parser?
5295 size_t num_args = command.GetArgumentCount();
5296 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005297
Jim Ingham9575d842011-03-11 03:53:59 +00005298 if (num_args == 0)
5299 {
5300 target->SetAllStopHooksActiveState (m_enable);
5301 }
5302 else
5303 {
5304 for (size_t i = 0; i < num_args; i++)
5305 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005306 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005307 if (!success)
5308 {
Greg Clayton7260f622011-04-18 08:33:37 +00005309 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005310 result.SetStatus(eReturnStatusFailed);
5311 return false;
5312 }
5313 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5314 if (!success)
5315 {
Greg Clayton7260f622011-04-18 08:33:37 +00005316 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005317 result.SetStatus(eReturnStatusFailed);
5318 return false;
5319 }
5320 }
5321 }
5322 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5323 }
5324 else
5325 {
Greg Clayton7260f622011-04-18 08:33:37 +00005326 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005327 result.SetStatus (eReturnStatusFailed);
5328 }
5329 return result.Succeeded();
5330 }
5331private:
5332 bool m_enable;
5333};
5334
5335#pragma mark CommandObjectTargetStopHookList
5336
5337//-------------------------------------------------------------------------
5338// CommandObjectTargetStopHookList
5339//-------------------------------------------------------------------------
5340
Jim Ingham5a988412012-06-08 21:56:10 +00005341class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005342{
5343public:
5344
5345 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005346 CommandObjectParsed (interpreter,
5347 "target stop-hook list",
5348 "List all stop-hooks.",
5349 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005350 {
5351 }
5352
5353 ~CommandObjectTargetStopHookList ()
5354 {
5355 }
5356
Jim Ingham5a988412012-06-08 21:56:10 +00005357protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005358 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005359 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005360 {
Jim Ingham893c9322014-11-22 01:42:44 +00005361 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005362 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005363 {
Greg Clayton7260f622011-04-18 08:33:37 +00005364 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005365 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005366 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005367 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005368
Jim Ingham9575d842011-03-11 03:53:59 +00005369 size_t num_hooks = target->GetNumStopHooks ();
5370 if (num_hooks == 0)
5371 {
5372 result.GetOutputStream().PutCString ("No stop hooks.\n");
5373 }
5374 else
5375 {
5376 for (size_t i = 0; i < num_hooks; i++)
5377 {
5378 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5379 if (i > 0)
5380 result.GetOutputStream().PutCString ("\n");
5381 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5382 }
5383 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005384 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005385 return result.Succeeded();
5386 }
5387};
5388
5389#pragma mark CommandObjectMultiwordTargetStopHooks
5390//-------------------------------------------------------------------------
5391// CommandObjectMultiwordTargetStopHooks
5392//-------------------------------------------------------------------------
5393
5394class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5395{
5396public:
5397
5398 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5399 CommandObjectMultiword (interpreter,
5400 "target stop-hook",
5401 "A set of commands for operating on debugger target stop-hooks.",
5402 "target stop-hook <subcommand> [<subcommand-options>]")
5403 {
5404 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5405 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5406 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5407 false,
5408 "target stop-hook disable [<id>]",
5409 "Disable a stop-hook.",
5410 "target stop-hook disable")));
5411 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5412 true,
5413 "target stop-hook enable [<id>]",
5414 "Enable a stop-hook.",
5415 "target stop-hook enable")));
5416 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5417 }
5418
5419 ~CommandObjectMultiwordTargetStopHooks()
5420 {
5421 }
5422};
5423
5424
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005425
5426#pragma mark CommandObjectMultiwordTarget
5427
5428//-------------------------------------------------------------------------
5429// CommandObjectMultiwordTarget
5430//-------------------------------------------------------------------------
5431
Greg Clayton66111032010-06-23 01:19:29 +00005432CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005433 CommandObjectMultiword (interpreter,
5434 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005435 "A set of commands for operating on debugger targets.",
5436 "target <subcommand> [<subcommand-options>]")
5437{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005438
Greg Clayton7260f622011-04-18 08:33:37 +00005439 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005440 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005441 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5442 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005443 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005444 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005445 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005446 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005447}
5448
5449CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5450{
5451}
5452
Greg Clayton7260f622011-04-18 08:33:37 +00005453