blob: 30eecbff7f6d51dc4c93001f423ddd0915a892a7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandObjectTarget.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectTarget.h"
13
14// C Includes
15#include <errno.h>
Greg Claytonc4a99bc2011-02-01 01:13:32 +000016
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
19// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000020#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000022#include "lldb/Core/IOHandler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
24#include "lldb/Core/ModuleSpec.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000025#include "lldb/Core/Section.h"
Greg Clayton7260f622011-04-18 08:33:37 +000026#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
Greg Clayton644247c2011-07-07 01:59:51 +000028#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000029#include "lldb/DataFormatters/ValueObjectPrinter.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000030#include "lldb/Host/StringConvert.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000031#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000034#include "lldb/Interpreter/Options.h"
Greg Clayton7260f622011-04-18 08:33:37 +000035#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000036#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000037#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000038#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton715c2362011-07-07 04:38:25 +000039#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Clayton7260f622011-04-18 08:33:37 +000040#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000041#include "lldb/Interpreter/OptionGroupUInt64.h"
42#include "lldb/Interpreter/OptionGroupUUID.h"
Jason Molendac6127dd2014-11-21 02:25:15 +000043#include "lldb/Interpreter/OptionGroupString.h"
Greg Clayton644247c2011-07-07 01:59:51 +000044#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton1f746072012-08-29 21:13:06 +000045#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000046#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000047#include "lldb/Symbol/LineTable.h"
48#include "lldb/Symbol/ObjectFile.h"
49#include "lldb/Symbol/SymbolFile.h"
50#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000051#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000052#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000054#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000055#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000057#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
61
Greg Clayton7260f622011-04-18 08:33:37 +000062
63
64static void
65DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
66{
Greg Clayton176761e2011-04-19 04:19:37 +000067 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000068
Greg Claytonaa149cb2011-08-11 02:48:45 +000069 Module *exe_module = target->GetExecutableModulePointer();
Greg Clayton7260f622011-04-18 08:33:37 +000070 char exe_path[PATH_MAX];
71 bool exe_valid = false;
Greg Claytonaa149cb2011-08-11 02:48:45 +000072 if (exe_module)
73 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000074
Greg Clayton7260f622011-04-18 08:33:37 +000075 if (!exe_valid)
76 ::strcpy (exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000077
Greg Clayton7260f622011-04-18 08:33:37 +000078 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000079
Greg Clayton7260f622011-04-18 08:33:37 +000080 uint32_t properties = 0;
81 if (target_arch.IsValid())
82 {
83 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str());
84 properties++;
85 }
86 PlatformSP platform_sp (target->GetPlatform());
87 if (platform_sp)
Greg Clayton57abc5d2013-05-10 21:47:16 +000088 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000089
Greg Clayton7260f622011-04-18 08:33:37 +000090 ProcessSP process_sp (target->GetProcessSP());
91 bool show_process_status = false;
92 if (process_sp)
93 {
94 lldb::pid_t pid = process_sp->GetID();
95 StateType state = process_sp->GetState();
96 if (show_stopped_process_status)
Greg Clayton2637f822011-11-17 01:23:07 +000097 show_process_status = StateIsStoppedState(state, true);
Greg Clayton7260f622011-04-18 08:33:37 +000098 const char *state_cstr = StateAsCString (state);
99 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +0000100 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Clayton7260f622011-04-18 08:33:37 +0000101 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
102 }
103 if (properties > 0)
104 strm.PutCString (" )\n");
105 else
106 strm.EOL();
107 if (show_process_status)
108 {
109 const bool only_threads_with_stop_reason = true;
110 const uint32_t start_frame = 0;
111 const uint32_t num_frames = 1;
112 const uint32_t num_frames_with_source = 1;
113 process_sp->GetStatus (strm);
114 process_sp->GetThreadStatus (strm,
115 only_threads_with_stop_reason,
116 start_frame,
117 num_frames,
118 num_frames_with_source);
119
120 }
121}
122
123static uint32_t
124DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
125{
126 const uint32_t num_targets = target_list.GetNumTargets();
127 if (num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000128 {
Greg Clayton7260f622011-04-18 08:33:37 +0000129 TargetSP selected_target_sp (target_list.GetSelectedTarget());
130 strm.PutCString ("Current targets:\n");
131 for (uint32_t i=0; i<num_targets; ++i)
132 {
133 TargetSP target_sp (target_list.GetTargetAtIndex (i));
134 if (target_sp)
135 {
136 bool is_selected = target_sp.get() == selected_target_sp.get();
137 DumpTargetInfo (i,
138 target_sp.get(),
139 is_selected ? "* " : " ",
140 show_stopped_process_status,
141 strm);
142 }
143 }
144 }
145 return num_targets;
146}
147#pragma mark CommandObjectTargetCreate
148
149//-------------------------------------------------------------------------
150// "target create"
151//-------------------------------------------------------------------------
152
Jim Ingham5a988412012-06-08 21:56:10 +0000153class CommandObjectTargetCreate : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000154{
155public:
156 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000157 CommandObjectParsed (interpreter,
158 "target create",
159 "Create a target using the argument as the main executable.",
160 NULL),
Greg Clayton7260f622011-04-18 08:33:37 +0000161 m_option_group (interpreter),
Greg Clayton644247c2011-07-07 01:59:51 +0000162 m_arch_option (),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000163 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000164 m_platform_path (LLDB_OPT_SET_1, false, "platform-path", 'P', 0, eArgTypePath, "Path to the remote file to use for this target."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000165 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
Jim Inghambf22b962013-07-11 01:47:46 +0000166 m_remote_file (LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename, "Fullpath to the file on the remote host if debugging remotely."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000167 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Clayton7260f622011-04-18 08:33:37 +0000168 {
169 CommandArgumentEntry arg;
170 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000171
Greg Clayton7260f622011-04-18 08:33:37 +0000172 // Define the first (and only) variant of this arg.
173 file_arg.arg_type = eArgTypeFilename;
174 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000175
Greg Clayton7260f622011-04-18 08:33:37 +0000176 // There is only one variant this argument could be; put it into the argument entry.
177 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000178
Greg Clayton7260f622011-04-18 08:33:37 +0000179 // Push the data for the first argument into the m_arguments vector.
180 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000181
Greg Clayton644247c2011-07-07 01:59:51 +0000182 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000183 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000184 m_option_group.Append (&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000185 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Jim Inghambf22b962013-07-11 01:47:46 +0000186 m_option_group.Append (&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000187 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000188 m_option_group.Finalize();
189 }
190
191 ~CommandObjectTargetCreate ()
192 {
193 }
194
195 Options *
196 GetOptions ()
197 {
198 return &m_option_group;
199 }
200
Greg Claytonc7bece562013-01-25 18:06:21 +0000201 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +0000202 HandleArgumentCompletion (Args &input,
203 int &cursor_index,
204 int &cursor_char_position,
205 OptionElementVector &opt_element_vector,
206 int match_start_point,
207 int max_return_elements,
208 bool &word_complete,
209 StringList &matches)
210 {
211 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
212 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000213
Jim Ingham5a988412012-06-08 21:56:10 +0000214 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
215 CommandCompletions::eDiskFileCompletion,
216 completion_str.c_str(),
217 match_start_point,
218 max_return_elements,
219 NULL,
220 word_complete,
221 matches);
222 return matches.GetSize();
223 }
224
225protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000226 bool
Jim Ingham5a988412012-06-08 21:56:10 +0000227 DoExecute (Args& command, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000228 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000229 const size_t argc = command.GetArgumentCount();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000230 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
Jim Inghambf22b962013-07-11 01:47:46 +0000231 FileSpec remote_file (m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000232
Greg Clayton5acc1252014-08-15 18:00:45 +0000233 if (core_file)
234 {
235 if (!core_file.Exists())
236 {
237 result.AppendErrorWithFormat("core file '%s' doesn't exist", core_file.GetPath().c_str());
238 result.SetStatus (eReturnStatusFailed);
239 return false;
240
241 }
242 if (!core_file.Readable())
243 {
244 result.AppendErrorWithFormat("core file '%s' is not readable", core_file.GetPath().c_str());
245 result.SetStatus (eReturnStatusFailed);
246 return false;
247 }
248 }
249
Daniel Maleae0f8f572013-08-26 23:57:52 +0000250 if (argc == 1 || core_file || remote_file)
Greg Clayton7260f622011-04-18 08:33:37 +0000251 {
Greg Clayton1c5f1862012-11-30 19:05:35 +0000252 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
253 if (symfile)
254 {
Greg Clayton5acc1252014-08-15 18:00:45 +0000255 if (symfile.Exists())
256 {
257 if (!symfile.Readable())
258 {
259 result.AppendErrorWithFormat("symbol file '%s' is not readable", core_file.GetPath().c_str());
260 result.SetStatus (eReturnStatusFailed);
261 return false;
262 }
263 }
264 else
Greg Clayton1c5f1862012-11-30 19:05:35 +0000265 {
266 char symfile_path[PATH_MAX];
267 symfile.GetPath(symfile_path, sizeof(symfile_path));
268 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
269 result.SetStatus (eReturnStatusFailed);
270 return false;
271 }
272 }
273
Greg Clayton7260f622011-04-18 08:33:37 +0000274 const char *file_path = command.GetArgumentAtIndex(0);
275 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000276 FileSpec file_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277
Daniel Maleae0f8f572013-08-26 23:57:52 +0000278 if (file_path)
279 file_spec.SetFile (file_path, true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000280
Daniel Maleae0f8f572013-08-26 23:57:52 +0000281 bool must_set_platform_path = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282
Greg Clayton7260f622011-04-18 08:33:37 +0000283 Debugger &debugger = m_interpreter.GetDebugger();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000284 PlatformSP platform_sp(debugger.GetPlatformList().GetSelectedPlatform ());
285
286 if (remote_file)
287 {
288 // I have a remote file.. two possible cases
289 if (file_spec && file_spec.Exists())
290 {
291 // if the remote file does not exist, push it there
292 if (!platform_sp->GetFileExists (remote_file))
293 {
294 Error err = platform_sp->PutFile(file_spec, remote_file);
295 if (err.Fail())
296 {
297 result.AppendError(err.AsCString());
298 result.SetStatus (eReturnStatusFailed);
299 return false;
300 }
301 }
302 }
303 else
304 {
305 // there is no local file and we need one
306 // in order to make the remote ---> local transfer we need a platform
307 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
308 if (!platform_sp)
309 {
310 result.AppendError("unable to perform remote debugging without a platform");
311 result.SetStatus (eReturnStatusFailed);
312 return false;
313 }
314 if (file_path)
315 {
316 // copy the remote file to the local file
317 Error err = platform_sp->GetFile(remote_file, file_spec);
318 if (err.Fail())
319 {
320 result.AppendError(err.AsCString());
321 result.SetStatus (eReturnStatusFailed);
322 return false;
323 }
324 }
325 else
326 {
327 // make up a local file
328 result.AppendError("remote --> local transfer without local path is not implemented yet");
329 result.SetStatus (eReturnStatusFailed);
330 return false;
331 }
332 }
333 }
334
335 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000336 const char *arch_cstr = m_arch_option.GetArchitectureName();
Vince Harron1b5a74e2015-01-21 22:42:49 +0000337 ArchSpec arch_spec(arch_cstr);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000338 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000339 Error error (debugger.GetTargetList().CreateTarget (debugger,
Greg Claytona0ca6602012-10-18 16:33:33 +0000340 file_path,
Vince Harron1b5a74e2015-01-21 22:42:49 +0000341 arch_spec,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000342 get_dependent_files,
Vince Harron1b5a74e2015-01-21 22:42:49 +0000343 platform_sp,
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000344 target_sp));
345
Greg Clayton7260f622011-04-18 08:33:37 +0000346 if (target_sp)
347 {
Jim Inghambf22b962013-07-11 01:47:46 +0000348 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000349 {
350 ModuleSP module_sp (target_sp->GetExecutableModule());
351 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000352 {
353 if (symfile)
354 module_sp->SetSymbolFileFileSpec(symfile);
355 if (remote_file)
356 {
357 std::string remote_path = remote_file.GetPath();
358 target_sp->SetArg0(remote_path.c_str());
359 module_sp->SetPlatformFileSpec(remote_file);
360 }
361 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000362 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000363
Greg Clayton7260f622011-04-18 08:33:37 +0000364 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000365 if (must_set_platform_path)
366 {
367 ModuleSpec main_module_spec(file_spec);
368 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
369 if (module_sp)
370 module_sp->SetPlatformFileSpec(remote_file);
371 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000372 if (core_file)
373 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000374 char core_path[PATH_MAX];
375 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000376 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000377 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000378 FileSpec core_file_dir;
379 core_file_dir.GetDirectory() = core_file.GetDirectory();
380 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000381
Greg Claytonc859e2d2012-02-13 23:10:39 +0000382 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file));
383
384 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000385 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000386 // Seems wierd that we Launch a core file, but that is
387 // what we do!
388 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000389
Greg Claytonc859e2d2012-02-13 23:10:39 +0000390 if (error.Fail())
391 {
392 result.AppendError(error.AsCString("can't find plug-in for core file"));
393 result.SetStatus (eReturnStatusFailed);
394 return false;
395 }
396 else
397 {
398 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
399 result.SetStatus (eReturnStatusSuccessFinishNoResult);
400 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000401 }
402 else
403 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000404 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
405 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000406 }
407 }
408 else
409 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000410 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000411 result.SetStatus (eReturnStatusFailed);
412 }
413 }
414 else
415 {
416 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
417 result.SetStatus (eReturnStatusSuccessFinishNoResult);
418 }
Greg Clayton7260f622011-04-18 08:33:37 +0000419 }
420 else
421 {
422 result.AppendError(error.AsCString());
423 result.SetStatus (eReturnStatusFailed);
424 }
425 }
426 else
427 {
Jason Molenda65e06422015-01-20 03:06:17 +0000428 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core option.\n", m_cmd_name.c_str());
Greg Clayton7260f622011-04-18 08:33:37 +0000429 result.SetStatus (eReturnStatusFailed);
430 }
431 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000432 }
433
Greg Clayton7260f622011-04-18 08:33:37 +0000434private:
435 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000436 OptionGroupArchitecture m_arch_option;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000437 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000438 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000439 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000440 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000441 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000442};
443
444#pragma mark CommandObjectTargetList
445
446//----------------------------------------------------------------------
447// "target list"
448//----------------------------------------------------------------------
449
Jim Ingham5a988412012-06-08 21:56:10 +0000450class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000451{
452public:
453 CommandObjectTargetList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000454 CommandObjectParsed (interpreter,
455 "target list",
456 "List all current targets in the current debug session.",
457 NULL,
458 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000459 {
460 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000461
Greg Clayton7260f622011-04-18 08:33:37 +0000462 virtual
463 ~CommandObjectTargetList ()
464 {
465 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000466
Jim Ingham5a988412012-06-08 21:56:10 +0000467protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000468 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000469 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000470 {
471 if (args.GetArgumentCount() == 0)
472 {
473 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000474
Greg Clayton7260f622011-04-18 08:33:37 +0000475 bool show_stopped_process_status = false;
476 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
477 {
478 strm.PutCString ("No targets.\n");
479 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000480 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000481 }
482 else
483 {
484 result.AppendError ("the 'target list' command takes no arguments\n");
485 result.SetStatus (eReturnStatusFailed);
486 }
487 return result.Succeeded();
488 }
489};
490
491
492#pragma mark CommandObjectTargetSelect
493
494//----------------------------------------------------------------------
495// "target select"
496//----------------------------------------------------------------------
497
Jim Ingham5a988412012-06-08 21:56:10 +0000498class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000499{
500public:
501 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000502 CommandObjectParsed (interpreter,
503 "target select",
504 "Select a target as the current target by target index.",
505 NULL,
506 0)
Greg Clayton7260f622011-04-18 08:33:37 +0000507 {
508 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000509
Greg Clayton7260f622011-04-18 08:33:37 +0000510 virtual
511 ~CommandObjectTargetSelect ()
512 {
513 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000514
Jim Ingham5a988412012-06-08 21:56:10 +0000515protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000516 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000517 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton7260f622011-04-18 08:33:37 +0000518 {
519 if (args.GetArgumentCount() == 1)
520 {
521 bool success = false;
522 const char *target_idx_arg = args.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000523 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton7260f622011-04-18 08:33:37 +0000524 if (success)
525 {
526 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
527 const uint32_t num_targets = target_list.GetNumTargets();
528 if (target_idx < num_targets)
529 {
530 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
531 if (target_sp)
532 {
533 Stream &strm = result.GetOutputStream();
534 target_list.SetSelectedTarget (target_sp.get());
535 bool show_stopped_process_status = false;
536 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000537 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000538 }
539 else
540 {
541 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
542 result.SetStatus (eReturnStatusFailed);
543 }
544 }
545 else
546 {
Todd Fiala352237d2014-08-29 20:14:21 +0000547 if (num_targets > 0)
548 {
549 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
550 target_idx,
551 num_targets - 1);
552 } else
553 {
554 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
555 target_idx);
556 }
Greg Clayton7260f622011-04-18 08:33:37 +0000557 result.SetStatus (eReturnStatusFailed);
558 }
559 }
560 else
561 {
562 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
563 result.SetStatus (eReturnStatusFailed);
564 }
565 }
566 else
567 {
568 result.AppendError ("'target select' takes a single argument: a target index\n");
569 result.SetStatus (eReturnStatusFailed);
570 }
571 return result.Succeeded();
572 }
573};
574
Greg Clayton3418c852011-08-10 02:10:13 +0000575#pragma mark CommandObjectTargetSelect
576
577//----------------------------------------------------------------------
578// "target delete"
579//----------------------------------------------------------------------
580
Jim Ingham5a988412012-06-08 21:56:10 +0000581class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000582{
583public:
584 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000585 CommandObjectParsed (interpreter,
586 "target delete",
587 "Delete one or more targets by target index.",
588 NULL,
589 0),
Greg Claytonaa149cb2011-08-11 02:48:45 +0000590 m_option_group (interpreter),
Greg Claytonb5f0fea2012-09-27 22:26:11 +0000591 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 +0000592 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000593 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
594 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000595 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000596
Greg Clayton3418c852011-08-10 02:10:13 +0000597 virtual
598 ~CommandObjectTargetDelete ()
599 {
600 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000601
Jim Ingham5a988412012-06-08 21:56:10 +0000602 Options *
603 GetOptions ()
604 {
605 return &m_option_group;
606 }
607
608protected:
Greg Clayton3418c852011-08-10 02:10:13 +0000609 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000610 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton3418c852011-08-10 02:10:13 +0000611 {
612 const size_t argc = args.GetArgumentCount();
613 std::vector<TargetSP> delete_target_list;
614 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
615 bool success = true;
616 TargetSP target_sp;
617 if (argc > 0)
618 {
619 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000620 // Bail out if don't have any targets.
621 if (num_targets == 0) {
622 result.AppendError("no targets to delete");
623 result.SetStatus(eReturnStatusFailed);
624 success = false;
625 }
626
Greg Clayton3418c852011-08-10 02:10:13 +0000627 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx)
628 {
629 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
Vince Harron5275aaa2015-01-15 20:08:35 +0000630 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton3418c852011-08-10 02:10:13 +0000631 if (success)
632 {
633 if (target_idx < num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000634 {
Greg Clayton3418c852011-08-10 02:10:13 +0000635 target_sp = target_list.GetTargetAtIndex (target_idx);
636 if (target_sp)
637 {
638 delete_target_list.push_back (target_sp);
639 continue;
640 }
641 }
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000642 if (num_targets > 1)
643 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
644 target_idx,
645 num_targets - 1);
646 else
647 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
648 target_idx);
649
Greg Clayton3418c852011-08-10 02:10:13 +0000650 result.SetStatus (eReturnStatusFailed);
651 success = false;
652 }
653 else
654 {
655 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
656 result.SetStatus (eReturnStatusFailed);
657 success = false;
658 }
659 }
Greg Clayton3418c852011-08-10 02:10:13 +0000660 }
661 else
662 {
663 target_sp = target_list.GetSelectedTarget();
664 if (target_sp)
665 {
666 delete_target_list.push_back (target_sp);
667 }
668 else
669 {
670 result.AppendErrorWithFormat("no target is currently selected\n");
671 result.SetStatus (eReturnStatusFailed);
672 success = false;
673 }
674 }
675 if (success)
676 {
677 const size_t num_targets_to_delete = delete_target_list.size();
678 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
679 {
680 target_sp = delete_target_list[idx];
681 target_list.DeleteTarget(target_sp);
682 target_sp->Destroy();
683 }
Greg Claytonaa149cb2011-08-11 02:48:45 +0000684 // If "--clean" was specified, prune any orphaned shared modules from
685 // the global shared module list
686 if (m_cleanup_option.GetOptionValue ())
687 {
Greg Clayton0cd70862012-04-09 20:22:01 +0000688 const bool mandatory = true;
689 ModuleList::RemoveOrphanSharedModules(mandatory);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000690 }
Greg Clayton3418c852011-08-10 02:10:13 +0000691 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
692 result.SetStatus(eReturnStatusSuccessFinishResult);
693 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000694
Greg Clayton3418c852011-08-10 02:10:13 +0000695 return result.Succeeded();
696 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000697
Greg Claytonaa149cb2011-08-11 02:48:45 +0000698 OptionGroupOptions m_option_group;
699 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000700};
701
Greg Clayton7260f622011-04-18 08:33:37 +0000702
Greg Clayton644247c2011-07-07 01:59:51 +0000703#pragma mark CommandObjectTargetVariable
704
705//----------------------------------------------------------------------
706// "target variable"
707//----------------------------------------------------------------------
708
Jim Ingham5a988412012-06-08 21:56:10 +0000709class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000710{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000711 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
712 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
713
Greg Clayton644247c2011-07-07 01:59:51 +0000714public:
715 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000716 CommandObjectParsed (interpreter,
717 "target variable",
Greg Claytondfdd1eb2012-11-03 00:10:22 +0000718 "Read global variable(s) prior to, or while running your binary.",
Jim Ingham5a988412012-06-08 21:56:10 +0000719 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000720 eFlagRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000721 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000722 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000723 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000724 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
725 SHORT_OPTION_FILE, 0, eArgTypeFilename,
726 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
727 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
728 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
729 "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 +0000730 m_varobj_options()
731 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000732 CommandArgumentEntry arg;
733 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000734
Johnny Chen81ab3f52011-08-22 22:22:00 +0000735 // Define the first (and only) variant of this arg.
736 var_name_arg.arg_type = eArgTypeVarName;
737 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000738
Johnny Chen81ab3f52011-08-22 22:22:00 +0000739 // There is only one variant this argument could be; put it into the argument entry.
740 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000741
Johnny Chen81ab3f52011-08-22 22:22:00 +0000742 // Push the data for the first argument into the m_arguments vector.
743 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000744
Greg Clayton644247c2011-07-07 01:59:51 +0000745 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000746 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000747 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 +0000748 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
749 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
750 m_option_group.Finalize();
751 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000752
Greg Clayton644247c2011-07-07 01:59:51 +0000753 virtual
754 ~CommandObjectTargetVariable ()
755 {
756 }
Greg Clayton884fb692011-07-08 21:46:14 +0000757
758 void
759 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
760 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000761 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000762
Greg Clayton884fb692011-07-08 21:46:14 +0000763 switch (var_sp->GetScope())
764 {
765 case eValueTypeVariableGlobal:
766 if (m_option_variable.show_scope)
767 s.PutCString("GLOBAL: ");
768 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000769
Greg Clayton884fb692011-07-08 21:46:14 +0000770 case eValueTypeVariableStatic:
771 if (m_option_variable.show_scope)
772 s.PutCString("STATIC: ");
773 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000774
Greg Clayton884fb692011-07-08 21:46:14 +0000775 case eValueTypeVariableArgument:
776 if (m_option_variable.show_scope)
777 s.PutCString(" ARG: ");
778 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000779
Greg Clayton884fb692011-07-08 21:46:14 +0000780 case eValueTypeVariableLocal:
781 if (m_option_variable.show_scope)
782 s.PutCString(" LOCAL: ");
783 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000784
Greg Clayton884fb692011-07-08 21:46:14 +0000785 default:
786 break;
787 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000788
Greg Clayton45ba8542011-07-10 19:21:23 +0000789 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000790 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000791 bool show_fullpaths = false;
792 bool show_module = true;
793 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
794 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000795 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000796
Greg Clayton1deb7962011-10-25 06:44:01 +0000797 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000798 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000799 options.SetFormat(format);
800
801 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000802
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000803 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000804 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000805
Greg Claytonc7bece562013-01-25 18:06:21 +0000806 static size_t GetVariableCallback (void *baton,
807 const char *name,
808 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000809 {
810 Target *target = static_cast<Target *>(baton);
811 if (target)
812 {
813 return target->GetImages().FindGlobalVariables (ConstString(name),
814 true,
815 UINT32_MAX,
816 variable_list);
817 }
818 return 0;
819 }
Greg Clayton884fb692011-07-08 21:46:14 +0000820
Jim Ingham5a988412012-06-08 21:56:10 +0000821 Options *
822 GetOptions ()
823 {
824 return &m_option_group;
825 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000826
Jim Ingham5a988412012-06-08 21:56:10 +0000827protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000828 void
829 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
830 {
831 size_t count = variable_list.GetSize();
832 if (count > 0)
833 {
834 if (sc.module_sp)
835 {
836 if (sc.comp_unit)
837 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000838 s.Printf ("Global variables for %s in %s:\n",
839 sc.comp_unit->GetPath().c_str(),
840 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000841 }
842 else
843 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000844 s.Printf ("Global variables for %s\n",
845 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000846 }
847 }
848 else if (sc.comp_unit)
849 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000850 s.Printf ("Global variables for %s\n",
851 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000852 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000854 for (uint32_t i=0; i<count; ++i)
855 {
856 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
857 if (var_sp)
858 {
859 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000860
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000861 if (valobj_sp)
862 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
863 }
864 }
865 }
866
867 }
Greg Clayton644247c2011-07-07 01:59:51 +0000868 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +0000869 DoExecute (Args& args, CommandReturnObject &result)
Greg Clayton644247c2011-07-07 01:59:51 +0000870 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000871 Target *target = m_exe_ctx.GetTargetPtr();
872 const size_t argc = args.GetArgumentCount();
873 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000874
Greg Claytonf9fc6092013-01-09 19:44:40 +0000875 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000876 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000877
878 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000879 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000880 VariableList variable_list;
881 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000882
Greg Claytonf9fc6092013-01-09 19:44:40 +0000883 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000884 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000885 bool use_var_name = false;
886 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000887 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000888 RegularExpression regex(arg);
889 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000890 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000891 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000892 result.SetStatus (eReturnStatusFailed);
893 return false;
894 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000895 use_var_name = true;
896 matches = target->GetImages().FindGlobalVariables (regex,
897 true,
898 UINT32_MAX,
899 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000900 }
901 else
902 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000903 Error error (Variable::GetValuesForVariableExpressionPath (arg,
904 m_exe_ctx.GetBestExecutionContextScope(),
905 GetVariableCallback,
906 target,
907 variable_list,
908 valobj_list));
909 matches = variable_list.GetSize();
910 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000911
Greg Claytonf9fc6092013-01-09 19:44:40 +0000912 if (matches == 0)
913 {
914 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
915 result.SetStatus (eReturnStatusFailed);
916 return false;
917 }
918 else
919 {
920 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000921 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000922 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
923 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000924 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000925 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
926 if (!valobj_sp)
927 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000928
Greg Claytonf9fc6092013-01-09 19:44:40 +0000929 if (valobj_sp)
930 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000931 }
932 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000933 }
Greg Clayton644247c2011-07-07 01:59:51 +0000934 }
935 }
936 else
937 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000938 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
939 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
940 SymbolContextList sc_list;
941 const size_t num_compile_units = compile_units.GetSize();
942 const size_t num_shlibs = shlibs.GetSize();
943 if (num_compile_units == 0 && num_shlibs == 0)
944 {
945 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000946 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +0000947 CompileUnit *comp_unit = NULL;
948 if (frame)
949 {
950 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
951 if (sc.comp_unit)
952 {
953 const bool can_create = true;
954 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
955 if (comp_unit_varlist_sp)
956 {
957 size_t count = comp_unit_varlist_sp->GetSize();
958 if (count > 0)
959 {
960 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
961 success = true;
962 }
963 }
964 }
965 }
966 if (!success)
967 {
968 if (frame)
969 {
970 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000971 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
972 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000973 else
974 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000975 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000976 else
977 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
978 result.SetStatus (eReturnStatusFailed);
979 }
980 }
981 else
982 {
983 SymbolContextList sc_list;
984 const bool append = true;
985 // We have one or more compile unit or shlib
986 if (num_shlibs > 0)
987 {
988 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
989 {
990 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
991 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000992
Greg Claytonf9fc6092013-01-09 19:44:40 +0000993 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
994 if (module_sp)
995 {
996 if (num_compile_units > 0)
997 {
998 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
999 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1000 }
1001 else
1002 {
1003 SymbolContext sc;
1004 sc.module_sp = module_sp;
1005 sc_list.Append(sc);
1006 }
1007 }
1008 else
1009 {
1010 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001011 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1012 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001013 }
1014 }
1015 }
1016 else
1017 {
1018 // No shared libraries, we just want to find globals for the compile units files that were specified
1019 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1020 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1021 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001022
Greg Claytonf9fc6092013-01-09 19:44:40 +00001023 const uint32_t num_scs = sc_list.GetSize();
1024 if (num_scs > 0)
1025 {
1026 SymbolContext sc;
1027 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1028 {
1029 if (sc_list.GetContextAtIndex(sc_idx, sc))
1030 {
1031 if (sc.comp_unit)
1032 {
1033 const bool can_create = true;
1034 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1035 if (comp_unit_varlist_sp)
1036 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1037 }
1038 else if (sc.module_sp)
1039 {
1040 // Get all global variables for this module
1041 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1042 VariableList variable_list;
1043 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1044 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1045 }
1046 }
1047 }
1048 }
1049 }
Greg Clayton644247c2011-07-07 01:59:51 +00001050 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001051
Enrico Granata61a80ba2011-08-12 16:42:31 +00001052 if (m_interpreter.TruncationWarningNecessary())
1053 {
1054 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1055 m_cmd_name.c_str());
1056 m_interpreter.TruncationWarningGiven();
1057 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001058
Greg Clayton644247c2011-07-07 01:59:51 +00001059 return result.Succeeded();
1060 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001061
Greg Clayton644247c2011-07-07 01:59:51 +00001062 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001063 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001064 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001065 OptionGroupFileList m_option_compile_units;
1066 OptionGroupFileList m_option_shared_libraries;
1067 OptionGroupValueObjectDisplay m_varobj_options;
1068
1069};
1070
1071
Greg Claytoneffe5c92011-05-03 22:09:39 +00001072#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
Jim Ingham5a988412012-06-08 21:56:10 +00001074class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075{
1076public:
1077
Greg Claytoneffe5c92011-05-03 22:09:39 +00001078 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001079 CommandObjectParsed (interpreter,
1080 "target modules search-paths add",
1081 "Add new image search paths substitution pairs to the current target.",
1082 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 {
Caroline Tice405fe672010-10-04 22:28:36 +00001084 CommandArgumentEntry arg;
1085 CommandArgumentData old_prefix_arg;
1086 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001087
Caroline Tice405fe672010-10-04 22:28:36 +00001088 // Define the first variant of this arg pair.
1089 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1090 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001091
Caroline Tice405fe672010-10-04 22:28:36 +00001092 // Define the first variant of this arg pair.
1093 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1094 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001095
Caroline Tice405fe672010-10-04 22:28:36 +00001096 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1097 // must always occur together, they are treated as two variants of one argument rather than two independent
1098 // arguments. Push them both into the first argument position for m_arguments...
1099
1100 arg.push_back (old_prefix_arg);
1101 arg.push_back (new_prefix_arg);
1102
1103 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104 }
1105
Greg Claytoneffe5c92011-05-03 22:09:39 +00001106 ~CommandObjectTargetModulesSearchPathsAdd ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 {
1108 }
1109
Jim Ingham5a988412012-06-08 21:56:10 +00001110protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001112 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113 CommandReturnObject &result)
1114 {
Greg Claytona7015092010-09-18 01:14:36 +00001115 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 if (target)
1117 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001118 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 if (argc & 1)
1120 {
Greg Clayton7260f622011-04-18 08:33:37 +00001121 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 result.SetStatus (eReturnStatusFailed);
1123 }
1124 else
1125 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001126 for (size_t i=0; i<argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 {
1128 const char *from = command.GetArgumentAtIndex(i);
1129 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 if (from[0] && to[0])
1132 {
1133 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001134 target->GetImageSearchPathList().Append (ConstString(from),
1135 ConstString(to),
1136 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001137 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 }
1139 else
1140 {
1141 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001142 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 else
Greg Clayton7260f622011-04-18 08:33:37 +00001144 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 result.SetStatus (eReturnStatusFailed);
1146 }
1147 }
1148 }
1149 }
1150 else
1151 {
Greg Clayton7260f622011-04-18 08:33:37 +00001152 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 result.SetStatus (eReturnStatusFailed);
1154 }
1155 return result.Succeeded();
1156 }
1157};
1158
Greg Claytoneffe5c92011-05-03 22:09:39 +00001159#pragma mark CommandObjectTargetModulesSearchPathsClear
1160
Jim Ingham5a988412012-06-08 21:56:10 +00001161class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162{
1163public:
1164
Greg Claytoneffe5c92011-05-03 22:09:39 +00001165 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001166 CommandObjectParsed (interpreter,
1167 "target modules search-paths clear",
1168 "Clear all current image search path substitution pairs from the current target.",
1169 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 {
1171 }
1172
Greg Claytoneffe5c92011-05-03 22:09:39 +00001173 ~CommandObjectTargetModulesSearchPathsClear ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 {
1175 }
1176
Jim Ingham5a988412012-06-08 21:56:10 +00001177protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001179 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 CommandReturnObject &result)
1181 {
Greg Claytona7015092010-09-18 01:14:36 +00001182 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 if (target)
1184 {
1185 bool notify = true;
1186 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001187 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 }
1189 else
1190 {
Greg Clayton7260f622011-04-18 08:33:37 +00001191 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 result.SetStatus (eReturnStatusFailed);
1193 }
1194 return result.Succeeded();
1195 }
1196};
1197
Greg Claytoneffe5c92011-05-03 22:09:39 +00001198#pragma mark CommandObjectTargetModulesSearchPathsInsert
1199
Jim Ingham5a988412012-06-08 21:56:10 +00001200class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201{
1202public:
1203
Greg Claytoneffe5c92011-05-03 22:09:39 +00001204 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001205 CommandObjectParsed (interpreter,
1206 "target modules search-paths insert",
1207 "Insert a new image search path substitution pair into the current target at the specified index.",
1208 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 {
Caroline Tice405fe672010-10-04 22:28:36 +00001210 CommandArgumentEntry arg1;
1211 CommandArgumentEntry arg2;
1212 CommandArgumentData index_arg;
1213 CommandArgumentData old_prefix_arg;
1214 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001215
Caroline Tice405fe672010-10-04 22:28:36 +00001216 // Define the first and only variant of this arg.
1217 index_arg.arg_type = eArgTypeIndex;
1218 index_arg.arg_repetition = eArgRepeatPlain;
1219
1220 // Put the one and only variant into the first arg for m_arguments:
1221 arg1.push_back (index_arg);
1222
1223 // Define the first variant of this arg pair.
1224 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1225 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001226
Caroline Tice405fe672010-10-04 22:28:36 +00001227 // Define the first variant of this arg pair.
1228 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1229 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001230
Caroline Tice405fe672010-10-04 22:28:36 +00001231 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1232 // must always occur together, they are treated as two variants of one argument rather than two independent
1233 // arguments. Push them both into the same argument position for m_arguments...
1234
1235 arg2.push_back (old_prefix_arg);
1236 arg2.push_back (new_prefix_arg);
1237
1238 // Add arguments to m_arguments.
1239 m_arguments.push_back (arg1);
1240 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 }
1242
Greg Claytoneffe5c92011-05-03 22:09:39 +00001243 ~CommandObjectTargetModulesSearchPathsInsert ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 {
1245 }
1246
Jim Ingham5a988412012-06-08 21:56:10 +00001247protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001249 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 CommandReturnObject &result)
1251 {
Greg Claytona7015092010-09-18 01:14:36 +00001252 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 if (target)
1254 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001255 size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 // check for at least 3 arguments and an odd nubmer of parameters
1257 if (argc >= 3 && argc & 1)
1258 {
1259 bool success = false;
1260
Vince Harron5275aaa2015-01-15 20:08:35 +00001261 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262
1263 if (!success)
1264 {
1265 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1266 result.SetStatus (eReturnStatusFailed);
1267 return result.Succeeded();
1268 }
1269
1270 // shift off the index
1271 command.Shift();
1272 argc = command.GetArgumentCount();
1273
1274 for (uint32_t i=0; i<argc; i+=2, ++insert_idx)
1275 {
1276 const char *from = command.GetArgumentAtIndex(i);
1277 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001278
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279 if (from[0] && to[0])
1280 {
1281 bool last_pair = ((argc - i) == 2);
1282 target->GetImageSearchPathList().Insert (ConstString(from),
1283 ConstString(to),
1284 insert_idx,
1285 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001286 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 }
1288 else
1289 {
1290 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001291 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292 else
Greg Clayton7260f622011-04-18 08:33:37 +00001293 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 result.SetStatus (eReturnStatusFailed);
1295 return false;
1296 }
1297 }
1298 }
1299 else
1300 {
Greg Clayton7260f622011-04-18 08:33:37 +00001301 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302 result.SetStatus (eReturnStatusFailed);
1303 return result.Succeeded();
1304 }
1305
1306 }
1307 else
1308 {
Greg Clayton7260f622011-04-18 08:33:37 +00001309 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 result.SetStatus (eReturnStatusFailed);
1311 }
1312 return result.Succeeded();
1313 }
1314};
1315
Greg Claytoneffe5c92011-05-03 22:09:39 +00001316
1317#pragma mark CommandObjectTargetModulesSearchPathsList
1318
1319
Jim Ingham5a988412012-06-08 21:56:10 +00001320class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321{
1322public:
1323
Greg Claytoneffe5c92011-05-03 22:09:39 +00001324 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001325 CommandObjectParsed (interpreter,
1326 "target modules search-paths list",
1327 "List all current image search path substitution pairs in the current target.",
1328 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 {
1330 }
1331
Greg Claytoneffe5c92011-05-03 22:09:39 +00001332 ~CommandObjectTargetModulesSearchPathsList ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 {
1334 }
1335
Jim Ingham5a988412012-06-08 21:56:10 +00001336protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001338 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 CommandReturnObject &result)
1340 {
Greg Claytona7015092010-09-18 01:14:36 +00001341 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 if (target)
1343 {
1344 if (command.GetArgumentCount() != 0)
1345 {
Greg Clayton7260f622011-04-18 08:33:37 +00001346 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 result.SetStatus (eReturnStatusFailed);
1348 return result.Succeeded();
1349 }
1350
1351 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001352 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353 }
1354 else
1355 {
Greg Clayton7260f622011-04-18 08:33:37 +00001356 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357 result.SetStatus (eReturnStatusFailed);
1358 }
1359 return result.Succeeded();
1360 }
1361};
1362
Greg Claytoneffe5c92011-05-03 22:09:39 +00001363#pragma mark CommandObjectTargetModulesSearchPathsQuery
1364
Jim Ingham5a988412012-06-08 21:56:10 +00001365class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366{
1367public:
1368
Greg Claytoneffe5c92011-05-03 22:09:39 +00001369 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001370 CommandObjectParsed (interpreter,
1371 "target modules search-paths query",
1372 "Transform a path using the first applicable image search path.",
1373 NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 {
Caroline Tice405fe672010-10-04 22:28:36 +00001375 CommandArgumentEntry arg;
1376 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001377
Caroline Tice405fe672010-10-04 22:28:36 +00001378 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001379 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001380 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001381
Caroline Tice405fe672010-10-04 22:28:36 +00001382 // There is only one variant this argument could be; put it into the argument entry.
1383 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001384
Caroline Tice405fe672010-10-04 22:28:36 +00001385 // Push the data for the first argument into the m_arguments vector.
1386 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 }
1388
Greg Claytoneffe5c92011-05-03 22:09:39 +00001389 ~CommandObjectTargetModulesSearchPathsQuery ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 {
1391 }
1392
Jim Ingham5a988412012-06-08 21:56:10 +00001393protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 bool
Jim Ingham5a988412012-06-08 21:56:10 +00001395 DoExecute (Args& command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 CommandReturnObject &result)
1397 {
Greg Claytona7015092010-09-18 01:14:36 +00001398 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 if (target)
1400 {
1401 if (command.GetArgumentCount() != 1)
1402 {
Greg Clayton7260f622011-04-18 08:33:37 +00001403 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 result.SetStatus (eReturnStatusFailed);
1405 return result.Succeeded();
1406 }
1407
1408 ConstString orig(command.GetArgumentAtIndex(0));
1409 ConstString transformed;
1410 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1411 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1412 else
1413 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001414
1415 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 }
1417 else
1418 {
Greg Clayton7260f622011-04-18 08:33:37 +00001419 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001420 result.SetStatus (eReturnStatusFailed);
1421 }
1422 return result.Succeeded();
1423 }
1424};
1425
Greg Claytoneffe5c92011-05-03 22:09:39 +00001426//----------------------------------------------------------------------
1427// Static Helper functions
1428//----------------------------------------------------------------------
1429static void
1430DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1431{
1432 if (module)
1433 {
1434 const char *arch_cstr;
1435 if (full_triple)
1436 arch_cstr = module->GetArchitecture().GetTriple().str().c_str();
1437 else
1438 arch_cstr = module->GetArchitecture().GetArchitectureName();
1439 if (width)
1440 strm.Printf("%-*s", width, arch_cstr);
1441 else
1442 strm.PutCString(arch_cstr);
1443 }
1444}
1445
1446static void
1447DumpModuleUUID (Stream &strm, Module *module)
1448{
Jim Ingham28eb5712012-10-12 17:34:26 +00001449 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001450 module->GetUUID().Dump (&strm);
1451 else
1452 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001453}
1454
1455static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001456DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1457 Stream &strm,
1458 Module *module,
1459 const FileSpec &file_spec,
1460 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001461{
1462 uint32_t num_matches = 0;
1463 if (module)
1464 {
1465 SymbolContextList sc_list;
1466 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1467 0,
1468 false,
1469 eSymbolContextCompUnit,
1470 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001471
Greg Claytoneffe5c92011-05-03 22:09:39 +00001472 for (uint32_t i=0; i<num_matches; ++i)
1473 {
1474 SymbolContext sc;
1475 if (sc_list.GetContextAtIndex(i, sc))
1476 {
1477 if (i > 0)
1478 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001479
Greg Claytoneffe5c92011-05-03 22:09:39 +00001480 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1481 << module->GetFileSpec().GetFilename() << "\n";
1482 LineTable *line_table = sc.comp_unit->GetLineTable();
1483 if (line_table)
1484 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001485 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001486 lldb::eDescriptionLevelBrief);
1487 else
1488 strm << "No line table";
1489 }
1490 }
1491 }
1492 return num_matches;
1493}
1494
1495static void
1496DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1497{
1498 if (file_spec_ptr)
1499 {
1500 if (width > 0)
1501 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001502 std::string fullpath = file_spec_ptr->GetPath();
1503 strm.Printf("%-*s", width, fullpath.c_str());
1504 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001505 }
1506 else
1507 {
1508 file_spec_ptr->Dump(&strm);
1509 return;
1510 }
1511 }
1512 // Keep the width spacing correct if things go wrong...
1513 if (width > 0)
1514 strm.Printf("%-*s", width, "");
1515}
1516
1517static void
1518DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1519{
1520 if (file_spec_ptr)
1521 {
1522 if (width > 0)
1523 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1524 else
1525 file_spec_ptr->GetDirectory().Dump(&strm);
1526 return;
1527 }
1528 // Keep the width spacing correct if things go wrong...
1529 if (width > 0)
1530 strm.Printf("%-*s", width, "");
1531}
1532
1533static void
1534DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1535{
1536 if (file_spec_ptr)
1537 {
1538 if (width > 0)
1539 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1540 else
1541 file_spec_ptr->GetFilename().Dump(&strm);
1542 return;
1543 }
1544 // Keep the width spacing correct if things go wrong...
1545 if (width > 0)
1546 strm.Printf("%-*s", width, "");
1547}
1548
1549
1550static void
1551DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1552{
1553 if (module)
1554 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001555 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1556 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001557 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001558 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001559 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001560 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001561 }
1562 }
1563}
1564
1565static void
1566DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1567{
1568 if (module)
1569 {
Greg Clayton3046e662013-07-10 01:23:25 +00001570 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001571 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001572 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001573 strm.Printf ("Sections for '%s' (%s):\n",
1574 module->GetSpecificationDescription().c_str(),
1575 module->GetArchitecture().GetArchitectureName());
1576 strm.IndentMore();
1577 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1578 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001579 }
1580 }
1581}
1582
1583static bool
1584DumpModuleSymbolVendor (Stream &strm, Module *module)
1585{
1586 if (module)
1587 {
1588 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1589 if (symbol_vendor)
1590 {
1591 symbol_vendor->Dump(&strm);
1592 return true;
1593 }
1594 }
1595 return false;
1596}
1597
Greg Claytonc4a8a762012-05-15 18:43:44 +00001598static void
1599DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1600{
1601 strm.IndentMore();
1602 strm.Indent (" Address: ");
1603 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1604 strm.PutCString (" (");
1605 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1606 strm.PutCString (")\n");
1607 strm.Indent (" Summary: ");
1608 const uint32_t save_indent = strm.GetIndentLevel ();
1609 strm.SetIndentLevel (save_indent + 13);
1610 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1611 strm.SetIndentLevel (save_indent);
1612 // Print out detailed address information when verbose is enabled
1613 if (verbose)
1614 {
1615 strm.EOL();
1616 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1617 }
1618 strm.IndentLess();
1619}
1620
Greg Claytoneffe5c92011-05-03 22:09:39 +00001621static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001622LookupAddressInModule (CommandInterpreter &interpreter,
1623 Stream &strm,
1624 Module *module,
1625 uint32_t resolve_mask,
1626 lldb::addr_t raw_addr,
1627 lldb::addr_t offset,
1628 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001629{
1630 if (module)
1631 {
1632 lldb::addr_t addr = raw_addr - offset;
1633 Address so_addr;
1634 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001635 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001636 if (target && !target->GetSectionLoadList().IsEmpty())
1637 {
1638 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1639 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001640 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001641 return false;
1642 }
1643 else
1644 {
1645 if (!module->ResolveFileAddress (addr, so_addr))
1646 return false;
1647 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001648
Greg Claytoneffe5c92011-05-03 22:09:39 +00001649 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001650 DumpAddress (exe_scope, so_addr, verbose, strm);
1651// strm.IndentMore();
1652// strm.Indent (" Address: ");
1653// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1654// strm.PutCString (" (");
1655// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1656// strm.PutCString (")\n");
1657// strm.Indent (" Summary: ");
1658// const uint32_t save_indent = strm.GetIndentLevel ();
1659// strm.SetIndentLevel (save_indent + 13);
1660// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1661// strm.SetIndentLevel (save_indent);
1662// // Print out detailed address information when verbose is enabled
1663// if (verbose)
1664// {
1665// strm.EOL();
1666// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1667// }
1668// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001669 return true;
1670 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001671
Greg Claytoneffe5c92011-05-03 22:09:39 +00001672 return false;
1673}
1674
1675static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001676LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001677{
1678 if (module)
1679 {
1680 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001681
Michael Sartaina7499c92013-07-01 19:45:50 +00001682 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1683 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001684 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001685 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001686 if (symtab)
1687 {
1688 uint32_t i;
1689 std::vector<uint32_t> match_indexes;
1690 ConstString symbol_name (name);
1691 uint32_t num_matches = 0;
1692 if (name_is_regex)
1693 {
1694 RegularExpression name_regexp(name);
1695 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1696 eSymbolTypeAny,
1697 match_indexes);
1698 }
1699 else
1700 {
1701 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1702 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001703
Greg Claytoneffe5c92011-05-03 22:09:39 +00001704 if (num_matches > 0)
1705 {
1706 strm.Indent ();
1707 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1708 name_is_regex ? "the regular expression " : "", name);
1709 DumpFullpath (strm, &module->GetFileSpec(), 0);
1710 strm.PutCString(":\n");
1711 strm.IndentMore ();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001712 //Symtab::DumpSymbolHeader (&strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001713 for (i=0; i < num_matches; ++i)
1714 {
1715 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Claytonc4a8a762012-05-15 18:43:44 +00001716 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1717 symbol->GetAddress(),
1718 verbose,
1719 strm);
1720
1721// strm.Indent ();
1722// symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001723 }
1724 strm.IndentLess ();
1725 return num_matches;
1726 }
1727 }
1728 }
1729 }
1730 return 0;
1731}
1732
1733
1734static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001735DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001736{
1737 strm.IndentMore ();
1738 uint32_t i;
1739 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001740
Greg Claytoneffe5c92011-05-03 22:09:39 +00001741 for (i=0; i<num_matches; ++i)
1742 {
1743 SymbolContext sc;
1744 if (sc_list.GetContextAtIndex(i, sc))
1745 {
Sean Callananf6172c22012-02-11 00:24:04 +00001746 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001747
Sean Callananf6172c22012-02-11 00:24:04 +00001748 sc.GetAddressRange(eSymbolContextEverything,
1749 0,
1750 true,
1751 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001752
Greg Claytonc4a8a762012-05-15 18:43:44 +00001753 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001754 }
1755 }
1756 strm.IndentLess ();
1757}
1758
Greg Claytonc7bece562013-01-25 18:06:21 +00001759static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001760LookupFunctionInModule (CommandInterpreter &interpreter,
1761 Stream &strm,
1762 Module *module,
1763 const char *name,
1764 bool name_is_regex,
1765 bool include_inlines,
1766 bool include_symbols,
1767 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001768{
1769 if (module && name && name[0])
1770 {
1771 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001772 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001773 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001774 if (name_is_regex)
1775 {
1776 RegularExpression function_name_regex (name);
1777 num_matches = module->FindFunctions (function_name_regex,
1778 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001779 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001780 append,
1781 sc_list);
1782 }
1783 else
1784 {
1785 ConstString function_name (name);
Sean Callananb6d70eb2011-10-12 02:08:07 +00001786 num_matches = module->FindFunctions (function_name,
1787 NULL,
Greg Clayton6ecb2322013-05-18 00:11:21 +00001788 eFunctionNameTypeAuto,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001789 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001790 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001791 append,
1792 sc_list);
1793 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001794
Greg Claytoneffe5c92011-05-03 22:09:39 +00001795 if (num_matches)
1796 {
1797 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001798 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001799 DumpFullpath (strm, &module->GetFileSpec(), 0);
1800 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001801 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001802 }
1803 return num_matches;
1804 }
1805 return 0;
1806}
1807
Greg Claytonc7bece562013-01-25 18:06:21 +00001808static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001809LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001810 Stream &strm,
1811 Module *module,
1812 const char *name_cstr,
1813 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001814{
1815 if (module && name_cstr && name_cstr[0])
1816 {
Greg Claytond1767f02011-12-08 02:13:16 +00001817 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001818 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001819 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001820 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001821 SymbolContext sc;
1822
1823 ConstString name(name_cstr);
Greg Clayton84db9102012-03-26 23:03:23 +00001824 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001825
Greg Claytond1767f02011-12-08 02:13:16 +00001826 if (num_matches)
1827 {
1828 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001829 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001830 DumpFullpath (strm, &module->GetFileSpec(), 0);
1831 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001832 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001833 {
Greg Claytond1767f02011-12-08 02:13:16 +00001834 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001835 {
Greg Claytond1767f02011-12-08 02:13:16 +00001836 // Resolve the clang type so that any forward references
1837 // to types that haven't yet been parsed will get parsed.
1838 type_sp->GetClangFullType ();
1839 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001840 // Print all typedef chains
1841 TypeSP typedef_type_sp (type_sp);
1842 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1843 while (typedefed_type_sp)
1844 {
1845 strm.EOL();
1846 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1847 typedefed_type_sp->GetClangFullType ();
1848 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1849 typedef_type_sp = typedefed_type_sp;
1850 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1851 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001852 }
Greg Claytond1767f02011-12-08 02:13:16 +00001853 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001854 }
Greg Claytond1767f02011-12-08 02:13:16 +00001855 }
1856 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001857 }
1858 return 0;
1859}
1860
Greg Claytonc7bece562013-01-25 18:06:21 +00001861static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001862LookupTypeHere (CommandInterpreter &interpreter,
1863 Stream &strm,
1864 const SymbolContext &sym_ctx,
1865 const char *name_cstr,
1866 bool name_is_regex)
1867{
1868 if (!sym_ctx.module_sp)
1869 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001870
Sean Callanand38b4a92012-06-06 20:49:55 +00001871 TypeList type_list;
1872 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001873 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001874 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001875
Sean Callanand38b4a92012-06-06 20:49:55 +00001876 ConstString name(name_cstr);
1877 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 +00001878
Sean Callanand38b4a92012-06-06 20:49:55 +00001879 if (num_matches)
1880 {
1881 strm.Indent ();
1882 strm.PutCString("Best match found in ");
1883 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1884 strm.PutCString(":\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001885
Sean Callanand38b4a92012-06-06 20:49:55 +00001886 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1887 if (type_sp)
1888 {
1889 // Resolve the clang type so that any forward references
1890 // to types that haven't yet been parsed will get parsed.
1891 type_sp->GetClangFullType ();
1892 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1893 // Print all typedef chains
1894 TypeSP typedef_type_sp (type_sp);
1895 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1896 while (typedefed_type_sp)
1897 {
1898 strm.EOL();
1899 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
1900 typedefed_type_sp->GetClangFullType ();
1901 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1902 typedef_type_sp = typedefed_type_sp;
1903 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1904 }
1905 }
1906 strm.EOL();
1907 }
1908 return num_matches;
1909}
1910
1911static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001912LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001913 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001914 Module *module,
1915 const FileSpec &file_spec,
1916 uint32_t line,
1917 bool check_inlines,
1918 bool verbose)
1919{
1920 if (module && file_spec)
1921 {
1922 SymbolContextList sc_list;
1923 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1924 eSymbolContextEverything, sc_list);
1925 if (num_matches > 0)
1926 {
1927 strm.Indent ();
1928 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1929 strm << file_spec;
1930 if (line > 0)
1931 strm.Printf (":%u", line);
1932 strm << " in ";
1933 DumpFullpath (strm, &module->GetFileSpec(), 0);
1934 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001935 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001936 return num_matches;
1937 }
1938 }
1939 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001940}
1941
Greg Clayton8ee64382011-11-10 01:18:58 +00001942
1943static size_t
1944FindModulesByName (Target *target,
1945 const char *module_name,
1946 ModuleList &module_list,
1947 bool check_global_list)
1948{
1949// Dump specified images (by basename or fullpath)
1950 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001951 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001952
Greg Clayton8ee64382011-11-10 01:18:58 +00001953 const size_t initial_size = module_list.GetSize ();
1954
Greg Claytonf3156262012-07-11 20:46:47 +00001955 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001956 {
1957 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001958 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001959 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001960 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001961 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001962 {
1963 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001964
Greg Clayton8ee64382011-11-10 01:18:58 +00001965 if (module)
1966 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001967 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001968 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001969 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001970 module_list.AppendIfNeeded(module_sp);
1971 }
1972 }
1973 }
1974 }
Greg Claytonf3156262012-07-11 20:46:47 +00001975 else
1976 {
1977 if (target)
1978 {
1979 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001980
Greg Claytonf3156262012-07-11 20:46:47 +00001981 // Not found in our module list for our target, check the main
1982 // shared module list in case it is a extra file used somewhere
1983 // else
1984 if (num_matches == 0)
1985 {
1986 module_spec.GetArchitecture() = target->GetArchitecture();
1987 ModuleList::FindSharedModules (module_spec, module_list);
1988 }
1989 }
1990 else
1991 {
1992 ModuleList::FindSharedModules (module_spec,module_list);
1993 }
1994 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001995
Greg Clayton8ee64382011-11-10 01:18:58 +00001996 return module_list.GetSize () - initial_size;
1997}
1998
Greg Claytoneffe5c92011-05-03 22:09:39 +00001999#pragma mark CommandObjectTargetModulesModuleAutoComplete
2000
2001//----------------------------------------------------------------------
2002// A base command object class that can auto complete with module file
2003// paths
2004//----------------------------------------------------------------------
2005
Jim Ingham5a988412012-06-08 21:56:10 +00002006class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002007{
2008public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002009 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2010 const char *name,
2011 const char *help,
2012 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002013 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002014 {
2015 CommandArgumentEntry arg;
2016 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002017
Greg Claytoneffe5c92011-05-03 22:09:39 +00002018 // Define the first (and only) variant of this arg.
2019 file_arg.arg_type = eArgTypeFilename;
2020 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002021
Greg Claytoneffe5c92011-05-03 22:09:39 +00002022 // There is only one variant this argument could be; put it into the argument entry.
2023 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002024
Greg Claytoneffe5c92011-05-03 22:09:39 +00002025 // Push the data for the first argument into the m_arguments vector.
2026 m_arguments.push_back (arg);
2027 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002028
Greg Claytoneffe5c92011-05-03 22:09:39 +00002029 virtual
2030 ~CommandObjectTargetModulesModuleAutoComplete ()
2031 {
2032 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002033
Greg Claytoneffe5c92011-05-03 22:09:39 +00002034 virtual int
2035 HandleArgumentCompletion (Args &input,
2036 int &cursor_index,
2037 int &cursor_char_position,
2038 OptionElementVector &opt_element_vector,
2039 int match_start_point,
2040 int max_return_elements,
2041 bool &word_complete,
2042 StringList &matches)
2043 {
2044 // Arguments are the standard module completer.
2045 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2046 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002047
Greg Claytoneffe5c92011-05-03 22:09:39 +00002048 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2049 CommandCompletions::eModuleCompletion,
2050 completion_str.c_str(),
2051 match_start_point,
2052 max_return_elements,
2053 NULL,
2054 word_complete,
2055 matches);
2056 return matches.GetSize();
2057 }
2058};
2059
2060#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2061
2062//----------------------------------------------------------------------
2063// A base command object class that can auto complete with module source
2064// file paths
2065//----------------------------------------------------------------------
2066
Jim Ingham5a988412012-06-08 21:56:10 +00002067class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002068{
2069public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002070 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002071 const char *name,
2072 const char *help,
2073 const char *syntax,
2074 uint32_t flags) :
2075 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002076 {
2077 CommandArgumentEntry arg;
2078 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002079
Greg Claytoneffe5c92011-05-03 22:09:39 +00002080 // Define the first (and only) variant of this arg.
2081 source_file_arg.arg_type = eArgTypeSourceFile;
2082 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002083
Greg Claytoneffe5c92011-05-03 22:09:39 +00002084 // There is only one variant this argument could be; put it into the argument entry.
2085 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002086
Greg Claytoneffe5c92011-05-03 22:09:39 +00002087 // Push the data for the first argument into the m_arguments vector.
2088 m_arguments.push_back (arg);
2089 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002090
Greg Claytoneffe5c92011-05-03 22:09:39 +00002091 virtual
2092 ~CommandObjectTargetModulesSourceFileAutoComplete ()
2093 {
2094 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002095
Greg Claytoneffe5c92011-05-03 22:09:39 +00002096 virtual int
2097 HandleArgumentCompletion (Args &input,
2098 int &cursor_index,
2099 int &cursor_char_position,
2100 OptionElementVector &opt_element_vector,
2101 int match_start_point,
2102 int max_return_elements,
2103 bool &word_complete,
2104 StringList &matches)
2105 {
2106 // Arguments are the standard source file completer.
2107 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2108 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002109
Greg Claytoneffe5c92011-05-03 22:09:39 +00002110 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2111 CommandCompletions::eSourceFileCompletion,
2112 completion_str.c_str(),
2113 match_start_point,
2114 max_return_elements,
2115 NULL,
2116 word_complete,
2117 matches);
2118 return matches.GetSize();
2119 }
2120};
2121
2122
2123#pragma mark CommandObjectTargetModulesDumpSymtab
2124
2125
2126class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2127{
2128public:
2129 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
2130 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2131 "target modules dump symtab",
2132 "Dump the symbol table from one or more target modules.",
2133 NULL),
2134 m_options (interpreter)
2135 {
2136 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002137
Greg Claytoneffe5c92011-05-03 22:09:39 +00002138 virtual
2139 ~CommandObjectTargetModulesDumpSymtab ()
2140 {
2141 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002142
Jim Ingham5a988412012-06-08 21:56:10 +00002143 virtual Options *
2144 GetOptions ()
2145 {
2146 return &m_options;
2147 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002148
Jim Ingham5a988412012-06-08 21:56:10 +00002149 class CommandOptions : public Options
2150 {
2151 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002152 CommandOptions (CommandInterpreter &interpreter) :
2153 Options(interpreter),
2154 m_sort_order (eSortOrderNone)
2155 {
2156 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002157
Jim Ingham5a988412012-06-08 21:56:10 +00002158 virtual
2159 ~CommandOptions ()
2160 {
2161 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002162
Jim Ingham5a988412012-06-08 21:56:10 +00002163 virtual Error
2164 SetOptionValue (uint32_t option_idx, const char *option_arg)
2165 {
2166 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002167 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002168
Jim Ingham5a988412012-06-08 21:56:10 +00002169 switch (short_option)
2170 {
2171 case 's':
2172 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2173 g_option_table[option_idx].enum_values,
2174 eSortOrderNone,
2175 error);
2176 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002177
Jim Ingham5a988412012-06-08 21:56:10 +00002178 default:
2179 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2180 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002181
Jim Ingham5a988412012-06-08 21:56:10 +00002182 }
2183 return error;
2184 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002185
Jim Ingham5a988412012-06-08 21:56:10 +00002186 void
2187 OptionParsingStarting ()
2188 {
2189 m_sort_order = eSortOrderNone;
2190 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002191
Jim Ingham5a988412012-06-08 21:56:10 +00002192 const OptionDefinition*
2193 GetDefinitions ()
2194 {
2195 return g_option_table;
2196 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002197
Jim Ingham5a988412012-06-08 21:56:10 +00002198 // Options table: Required for subclasses of Options.
2199 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002200
Jim Ingham5a988412012-06-08 21:56:10 +00002201 SortOrder m_sort_order;
2202 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002203
Jim Ingham5a988412012-06-08 21:56:10 +00002204protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002205 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002206 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002207 CommandReturnObject &result)
2208 {
2209 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2210 if (target == NULL)
2211 {
2212 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2213 result.SetStatus (eReturnStatusFailed);
2214 return false;
2215 }
2216 else
2217 {
2218 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002219
Greg Claytoneffe5c92011-05-03 22:09:39 +00002220 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2221 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2222 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002223
Greg Claytoneffe5c92011-05-03 22:09:39 +00002224 if (command.GetArgumentCount() == 0)
2225 {
2226 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002227 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002228 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002229 if (num_modules > 0)
2230 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002231 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002232 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002233 {
2234 if (num_dumped > 0)
2235 {
2236 result.GetOutputStream().EOL();
2237 result.GetOutputStream().EOL();
2238 }
2239 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002240 DumpModuleSymtab (m_interpreter,
2241 result.GetOutputStream(),
2242 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2243 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002244 }
2245 }
2246 else
2247 {
2248 result.AppendError ("the target has no associated executable images");
2249 result.SetStatus (eReturnStatusFailed);
2250 return false;
2251 }
2252 }
2253 else
2254 {
2255 // Dump specified images (by basename or fullpath)
2256 const char *arg_cstr;
2257 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2258 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002259 ModuleList module_list;
2260 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2261 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002262 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002263 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002264 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002265 Module *module = module_list.GetModulePointerAtIndex(i);
2266 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002267 {
2268 if (num_dumped > 0)
2269 {
2270 result.GetOutputStream().EOL();
2271 result.GetOutputStream().EOL();
2272 }
2273 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002274 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002275 }
2276 }
2277 }
2278 else
2279 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2280 }
2281 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002282
Greg Claytoneffe5c92011-05-03 22:09:39 +00002283 if (num_dumped > 0)
2284 result.SetStatus (eReturnStatusSuccessFinishResult);
2285 else
2286 {
2287 result.AppendError ("no matching executable images found");
2288 result.SetStatus (eReturnStatusFailed);
2289 }
2290 }
2291 return result.Succeeded();
2292 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002293
Greg Claytoneffe5c92011-05-03 22:09:39 +00002294 CommandOptions m_options;
2295};
2296
2297static OptionEnumValueElement
2298g_sort_option_enumeration[4] =
2299{
2300 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2301 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2302 { eSortOrderByName, "name", "Sort output by symbol name."},
2303 { 0, NULL, NULL }
2304};
2305
2306
2307OptionDefinition
2308CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2309{
Zachary Turnerd37221d2014-07-09 16:31:49 +00002310 { 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."},
2311 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002312};
2313
2314#pragma mark CommandObjectTargetModulesDumpSections
2315
2316//----------------------------------------------------------------------
2317// Image section dumping command
2318//----------------------------------------------------------------------
2319
2320class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2321{
2322public:
2323 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
2324 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2325 "target modules dump sections",
2326 "Dump the sections from one or more target modules.",
2327 //"target modules dump sections [<file1> ...]")
2328 NULL)
2329 {
2330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002331
Greg Claytoneffe5c92011-05-03 22:09:39 +00002332 virtual
2333 ~CommandObjectTargetModulesDumpSections ()
2334 {
2335 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002336
Jim Ingham5a988412012-06-08 21:56:10 +00002337protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002338 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002339 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002340 CommandReturnObject &result)
2341 {
2342 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2343 if (target == NULL)
2344 {
2345 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2346 result.SetStatus (eReturnStatusFailed);
2347 return false;
2348 }
2349 else
2350 {
2351 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002352
Greg Claytoneffe5c92011-05-03 22:09:39 +00002353 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2354 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2355 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002356
Greg Claytoneffe5c92011-05-03 22:09:39 +00002357 if (command.GetArgumentCount() == 0)
2358 {
2359 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002360 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002361 if (num_modules > 0)
2362 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002363 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002364 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002365 {
2366 num_dumped++;
2367 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2368 }
2369 }
2370 else
2371 {
2372 result.AppendError ("the target has no associated executable images");
2373 result.SetStatus (eReturnStatusFailed);
2374 return false;
2375 }
2376 }
2377 else
2378 {
2379 // Dump specified images (by basename or fullpath)
2380 const char *arg_cstr;
2381 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2382 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002383 ModuleList module_list;
2384 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2385 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002386 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002387 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002388 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002389 Module *module = module_list.GetModulePointerAtIndex(i);
2390 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002391 {
2392 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002393 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002394 }
2395 }
2396 }
2397 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002398 {
2399 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002400 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002401
Greg Claytoneffe5c92011-05-03 22:09:39 +00002402 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002403 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002404 }
2405 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002406
Greg Claytoneffe5c92011-05-03 22:09:39 +00002407 if (num_dumped > 0)
2408 result.SetStatus (eReturnStatusSuccessFinishResult);
2409 else
2410 {
2411 result.AppendError ("no matching executable images found");
2412 result.SetStatus (eReturnStatusFailed);
2413 }
2414 }
2415 return result.Succeeded();
2416 }
2417};
2418
2419
2420#pragma mark CommandObjectTargetModulesDumpSymfile
2421
2422//----------------------------------------------------------------------
2423// Image debug symbol dumping command
2424//----------------------------------------------------------------------
2425
2426class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2427{
2428public:
2429 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
2430 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2431 "target modules dump symfile",
2432 "Dump the debug symbol file for one or more target modules.",
2433 //"target modules dump symfile [<file1> ...]")
2434 NULL)
2435 {
2436 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002437
Greg Claytoneffe5c92011-05-03 22:09:39 +00002438 virtual
2439 ~CommandObjectTargetModulesDumpSymfile ()
2440 {
2441 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002442
Jim Ingham5a988412012-06-08 21:56:10 +00002443protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002444 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002445 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002446 CommandReturnObject &result)
2447 {
2448 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2449 if (target == NULL)
2450 {
2451 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2452 result.SetStatus (eReturnStatusFailed);
2453 return false;
2454 }
2455 else
2456 {
2457 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002458
Greg Claytoneffe5c92011-05-03 22:09:39 +00002459 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2460 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2461 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002462
Greg Claytoneffe5c92011-05-03 22:09:39 +00002463 if (command.GetArgumentCount() == 0)
2464 {
2465 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002466 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002467 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002468 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002469 if (num_modules > 0)
2470 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002471 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002472 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2473 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002474 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002475 num_dumped++;
2476 }
2477 }
2478 else
2479 {
2480 result.AppendError ("the target has no associated executable images");
2481 result.SetStatus (eReturnStatusFailed);
2482 return false;
2483 }
2484 }
2485 else
2486 {
2487 // Dump specified images (by basename or fullpath)
2488 const char *arg_cstr;
2489 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
2490 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002491 ModuleList module_list;
2492 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2493 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002494 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002495 for (size_t i=0; i<num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002496 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002497 Module *module = module_list.GetModulePointerAtIndex(i);
2498 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002499 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002500 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002501 num_dumped++;
2502 }
2503 }
2504 }
2505 else
2506 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2507 }
2508 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002509
Greg Claytoneffe5c92011-05-03 22:09:39 +00002510 if (num_dumped > 0)
2511 result.SetStatus (eReturnStatusSuccessFinishResult);
2512 else
2513 {
2514 result.AppendError ("no matching executable images found");
2515 result.SetStatus (eReturnStatusFailed);
2516 }
2517 }
2518 return result.Succeeded();
2519 }
2520};
2521
2522
2523#pragma mark CommandObjectTargetModulesDumpLineTable
2524
2525//----------------------------------------------------------------------
2526// Image debug line table dumping command
2527//----------------------------------------------------------------------
2528
2529class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2530{
2531public:
2532 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
2533 CommandObjectTargetModulesSourceFileAutoComplete (interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002534 "target modules dump line-table",
Jim Inghamcc0273d2013-06-18 20:27:11 +00002535 "Dump the line table for one or more compilation units.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00002536 NULL,
2537 eFlagRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002538 {
2539 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002540
Greg Claytoneffe5c92011-05-03 22:09:39 +00002541 virtual
2542 ~CommandObjectTargetModulesDumpLineTable ()
2543 {
2544 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002545
Jim Ingham5a988412012-06-08 21:56:10 +00002546protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002547 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002548 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002549 CommandReturnObject &result)
2550 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002551 Target *target = m_exe_ctx.GetTargetPtr();
2552 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002553
Greg Claytonf9fc6092013-01-09 19:44:40 +00002554 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2555 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2556 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002557
Greg Claytonf9fc6092013-01-09 19:44:40 +00002558 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002559 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002560 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00002561 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002562 }
2563 else
2564 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002565 // Dump specified images (by basename or fullpath)
2566 const char *arg_cstr;
2567 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002568 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002569 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002570
Greg Claytonf9fc6092013-01-09 19:44:40 +00002571 const ModuleList &target_modules = target->GetImages();
2572 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002573 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002574 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002575 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002576 uint32_t num_dumped = 0;
2577 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002578 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002579 if (DumpCompileUnitLineTable (m_interpreter,
2580 result.GetOutputStream(),
2581 target_modules.GetModulePointerAtIndexUnlocked(i),
2582 file_spec,
2583 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2584 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002585 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002586 if (num_dumped == 0)
2587 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2588 else
2589 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002590 }
2591 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002592 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002593
Greg Claytonf9fc6092013-01-09 19:44:40 +00002594 if (total_num_dumped > 0)
2595 result.SetStatus (eReturnStatusSuccessFinishResult);
2596 else
2597 {
2598 result.AppendError ("no source filenames matched any command arguments");
2599 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002600 }
2601 return result.Succeeded();
2602 }
2603};
2604
2605
2606#pragma mark CommandObjectTargetModulesDump
2607
2608//----------------------------------------------------------------------
2609// Dump multi-word command for target modules
2610//----------------------------------------------------------------------
2611
2612class CommandObjectTargetModulesDump : public CommandObjectMultiword
2613{
2614public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002615 //------------------------------------------------------------------
2616 // Constructors and Destructors
2617 //------------------------------------------------------------------
2618 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
2619 CommandObjectMultiword (interpreter,
2620 "target modules dump",
2621 "A set of commands for dumping information about one or more target modules.",
2622 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
2623 {
2624 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2625 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2626 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2627 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2628 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002629
Greg Claytoneffe5c92011-05-03 22:09:39 +00002630 virtual
2631 ~CommandObjectTargetModulesDump()
2632 {
2633 }
2634};
2635
Jim Ingham5a988412012-06-08 21:56:10 +00002636class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002637{
2638public:
2639 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002640 CommandObjectParsed (interpreter,
2641 "target modules add",
2642 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002643 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002644 m_option_group (interpreter),
2645 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 +00002646 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002647 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002648 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002649 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002650 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002651
Greg Claytoneffe5c92011-05-03 22:09:39 +00002652 virtual
2653 ~CommandObjectTargetModulesAdd ()
2654 {
2655 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002656
Greg Clayton50a24bd2012-11-29 22:16:27 +00002657 virtual Options *
2658 GetOptions ()
2659 {
2660 return &m_option_group;
2661 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002662
Greg Claytonc7bece562013-01-25 18:06:21 +00002663 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00002664 HandleArgumentCompletion (Args &input,
2665 int &cursor_index,
2666 int &cursor_char_position,
2667 OptionElementVector &opt_element_vector,
2668 int match_start_point,
2669 int max_return_elements,
2670 bool &word_complete,
2671 StringList &matches)
2672 {
2673 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2674 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002675
Jim Ingham5a988412012-06-08 21:56:10 +00002676 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
2677 CommandCompletions::eDiskFileCompletion,
2678 completion_str.c_str(),
2679 match_start_point,
2680 max_return_elements,
2681 NULL,
2682 word_complete,
2683 matches);
2684 return matches.GetSize();
2685 }
2686
2687protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002688 OptionGroupOptions m_option_group;
2689 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002690 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002691
Greg Claytoneffe5c92011-05-03 22:09:39 +00002692 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002693 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002694 CommandReturnObject &result)
2695 {
2696 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2697 if (target == NULL)
2698 {
2699 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2700 result.SetStatus (eReturnStatusFailed);
2701 return false;
2702 }
2703 else
2704 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002705 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002706
Greg Claytoneffe5c92011-05-03 22:09:39 +00002707 const size_t argc = args.GetArgumentCount();
2708 if (argc == 0)
2709 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002710 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2711 {
2712 // We are given a UUID only, go locate the file
2713 ModuleSpec module_spec;
2714 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002715 if (m_symbol_file.GetOptionValue().OptionWasSet())
2716 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002717 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2718 {
2719 ModuleSP module_sp (target->GetSharedModule (module_spec));
2720 if (module_sp)
2721 {
2722 result.SetStatus (eReturnStatusSuccessFinishResult);
2723 return true;
2724 }
2725 else
2726 {
2727 StreamString strm;
2728 module_spec.GetUUID().Dump (&strm);
2729 if (module_spec.GetFileSpec())
2730 {
2731 if (module_spec.GetSymbolFileSpec())
2732 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002733 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 +00002734 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002735 module_spec.GetFileSpec().GetPath().c_str(),
2736 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002737 }
2738 else
2739 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002740 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002741 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002742 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002743 }
2744 }
2745 else
2746 {
2747 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2748 strm.GetString().c_str());
2749 }
2750 result.SetStatus (eReturnStatusFailed);
2751 return false;
2752 }
2753 }
2754 else
2755 {
2756 StreamString strm;
2757 module_spec.GetUUID().Dump (&strm);
2758 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2759 result.SetStatus (eReturnStatusFailed);
2760 return false;
2761 }
2762 }
2763 else
2764 {
2765 result.AppendError ("one or more executable image paths must be specified");
2766 result.SetStatus (eReturnStatusFailed);
2767 return false;
2768 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002769 }
2770 else
2771 {
2772 for (size_t i=0; i<argc; ++i)
2773 {
2774 const char *path = args.GetArgumentAtIndex(i);
2775 if (path)
2776 {
2777 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002778 if (file_spec.Exists())
2779 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002780 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002781 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2782 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002783 if (m_symbol_file.GetOptionValue().OptionWasSet())
2784 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002785 if (!module_spec.GetArchitecture().IsValid())
2786 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002787 Error error;
2788 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002789 if (!module_sp)
2790 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002791 const char *error_cstr = error.AsCString();
2792 if (error_cstr)
2793 result.AppendError (error_cstr);
2794 else
2795 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002796 result.SetStatus (eReturnStatusFailed);
2797 return false;
2798 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002799 else
2800 {
2801 flush = true;
2802 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002803 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002804 }
2805 else
2806 {
2807 char resolved_path[PATH_MAX];
2808 result.SetStatus (eReturnStatusFailed);
2809 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2810 {
2811 if (strcmp (resolved_path, path) != 0)
2812 {
2813 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2814 break;
2815 }
2816 }
2817 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2818 break;
2819 }
2820 }
2821 }
2822 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002823
Sean Callananb36c6c02012-12-13 01:39:39 +00002824 if (flush)
2825 {
2826 ProcessSP process = target->GetProcessSP();
2827 if (process)
2828 process->Flush();
2829 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002830 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002831
Greg Claytoneffe5c92011-05-03 22:09:39 +00002832 return result.Succeeded();
2833 }
2834
Greg Claytoneffe5c92011-05-03 22:09:39 +00002835};
2836
2837class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2838{
2839public:
2840 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2841 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2842 "target modules load",
2843 "Set the load addresses for one or more sections in a target module.",
2844 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2845 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002846 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 +00002847 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)
2848 {
2849 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2850 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2851 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2852 m_option_group.Finalize();
2853 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002854
Greg Claytoneffe5c92011-05-03 22:09:39 +00002855 virtual
2856 ~CommandObjectTargetModulesLoad ()
2857 {
2858 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002859
Jim Ingham5a988412012-06-08 21:56:10 +00002860 virtual Options *
2861 GetOptions ()
2862 {
2863 return &m_option_group;
2864 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002865
Jim Ingham5a988412012-06-08 21:56:10 +00002866protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002867 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00002868 DoExecute (Args& args,
Greg Claytoneffe5c92011-05-03 22:09:39 +00002869 CommandReturnObject &result)
2870 {
2871 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2872 if (target == NULL)
2873 {
2874 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2875 result.SetStatus (eReturnStatusFailed);
2876 return false;
2877 }
2878 else
2879 {
2880 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002881 ModuleSpec module_spec;
2882 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002883 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002884 {
2885 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002886 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2887 const bool use_global_module_list = true;
2888 ModuleList module_list;
2889 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2890 if (num_matches == 1)
2891 {
2892 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2893 }
2894 else if (num_matches > 1 )
2895 {
2896 search_using_module_spec = false;
2897 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2898 result.SetStatus (eReturnStatusFailed);
2899 }
2900 else
2901 {
2902 search_using_module_spec = false;
2903 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2904 result.SetStatus (eReturnStatusFailed);
2905 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002906 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002907
Greg Claytoneffe5c92011-05-03 22:09:39 +00002908 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002909 {
2910 search_using_module_spec = true;
2911 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2912 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002913
Greg Claytonb9a01b32012-02-26 05:51:37 +00002914 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002915 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002916 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002917 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002918
2919 char path[PATH_MAX];
2920 if (num_matches == 1)
2921 {
2922 Module *module = matching_modules.GetModulePointerAtIndex(0);
2923 if (module)
2924 {
2925 ObjectFile *objfile = module->GetObjectFile();
2926 if (objfile)
2927 {
Greg Clayton3046e662013-07-10 01:23:25 +00002928 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002929 if (section_list)
2930 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002931 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002932 if (argc == 0)
2933 {
2934 if (m_slide_option.GetOptionValue().OptionWasSet())
2935 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002936 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002937 const bool slide_is_offset = true;
2938 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002939 }
2940 else
2941 {
2942 result.AppendError ("one or more section name + load address pair must be specified");
2943 result.SetStatus (eReturnStatusFailed);
2944 return false;
2945 }
2946 }
2947 else
2948 {
2949 if (m_slide_option.GetOptionValue().OptionWasSet())
2950 {
2951 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
2952 result.SetStatus (eReturnStatusFailed);
2953 return false;
2954 }
2955
2956 for (size_t i=0; i<argc; i += 2)
2957 {
2958 const char *sect_name = args.GetArgumentAtIndex(i);
2959 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
2960 if (sect_name && load_addr_cstr)
2961 {
2962 ConstString const_sect_name(sect_name);
2963 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00002964 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002965 if (success)
2966 {
2967 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
2968 if (section_sp)
2969 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002970 if (section_sp->IsThreadSpecific())
2971 {
2972 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
2973 result.SetStatus (eReturnStatusFailed);
2974 break;
2975 }
2976 else
2977 {
Greg Clayton7820bd12012-07-07 01:24:12 +00002978 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00002979 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00002980 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00002981 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002982 }
2983 else
2984 {
2985 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
2986 result.SetStatus (eReturnStatusFailed);
2987 break;
2988 }
2989 }
2990 else
2991 {
2992 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
2993 result.SetStatus (eReturnStatusFailed);
2994 break;
2995 }
2996 }
2997 else
2998 {
2999 if (sect_name)
3000 result.AppendError ("section names must be followed by a load address.\n");
3001 else
3002 result.AppendError ("one or more section name + load address pair must be specified.\n");
3003 result.SetStatus (eReturnStatusFailed);
3004 break;
3005 }
3006 }
3007 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003008
Greg Clayton741f3f92012-03-27 21:10:07 +00003009 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003010 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003011 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003012 Process *process = m_exe_ctx.GetProcessPtr();
3013 if (process)
3014 process->Flush();
3015 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003016 }
3017 else
3018 {
3019 module->GetFileSpec().GetPath (path, sizeof(path));
3020 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3021 result.SetStatus (eReturnStatusFailed);
3022 }
3023 }
3024 else
3025 {
3026 module->GetFileSpec().GetPath (path, sizeof(path));
3027 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3028 result.SetStatus (eReturnStatusFailed);
3029 }
3030 }
3031 else
3032 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003033 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3034 if (module_spec_file)
3035 {
3036 module_spec_file->GetPath (path, sizeof(path));
3037 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3038 }
3039 else
3040 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003041 result.SetStatus (eReturnStatusFailed);
3042 }
3043 }
3044 else
3045 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003046 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003047
Greg Claytonb9a01b32012-02-26 05:51:37 +00003048 if (module_spec.GetFileSpec())
3049 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003050 else
3051 path[0] = '\0';
3052
Greg Claytonb9a01b32012-02-26 05:51:37 +00003053 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003054 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003055 if (num_matches > 1)
3056 {
3057 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3058 path[0] ? " file=" : "",
3059 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003060 !uuid_str.empty() ? " uuid=" : "",
3061 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003062 for (size_t i=0; i<num_matches; ++i)
3063 {
3064 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3065 result.AppendMessageWithFormat("%s\n", path);
3066 }
3067 }
3068 else
3069 {
3070 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3071 path[0] ? " file=" : "",
3072 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003073 !uuid_str.empty() ? " uuid=" : "",
3074 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003075 }
3076 result.SetStatus (eReturnStatusFailed);
3077 }
3078 }
3079 else
3080 {
3081 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3082 result.SetStatus (eReturnStatusFailed);
3083 return false;
3084 }
3085 }
3086 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003087 }
3088
Greg Claytoneffe5c92011-05-03 22:09:39 +00003089 OptionGroupOptions m_option_group;
3090 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003091 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003092 OptionGroupUInt64 m_slide_option;
3093};
3094
3095//----------------------------------------------------------------------
3096// List images with associated information
3097//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003098class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003099{
3100public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003101 class CommandOptions : public Options
3102 {
3103 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003104 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003105 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003106 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003107 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003108 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003109 {
3110 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003111
Greg Claytoneffe5c92011-05-03 22:09:39 +00003112 virtual
3113 ~CommandOptions ()
3114 {
3115 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003116
Greg Claytoneffe5c92011-05-03 22:09:39 +00003117 virtual Error
3118 SetOptionValue (uint32_t option_idx, const char *option_arg)
3119 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003120 Error error;
3121
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003122 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003123 if (short_option == 'g')
3124 {
3125 m_use_global_module_list = true;
3126 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003127 else if (short_option == 'a')
3128 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003129 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3130 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003131 }
Greg Clayton65a03992011-08-09 00:01:09 +00003132 else
3133 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003134 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003135 if (option_arg)
3136 width = strtoul (option_arg, NULL, 0);
3137 m_format_array.push_back(std::make_pair(short_option, width));
3138 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003139 return error;
3140 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003141
Greg Claytoneffe5c92011-05-03 22:09:39 +00003142 void
3143 OptionParsingStarting ()
3144 {
3145 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003146 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003147 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003148 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003149
Greg Claytoneffe5c92011-05-03 22:09:39 +00003150 const OptionDefinition*
3151 GetDefinitions ()
3152 {
3153 return g_option_table;
3154 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003155
Greg Claytoneffe5c92011-05-03 22:09:39 +00003156 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003157
Greg Claytoneffe5c92011-05-03 22:09:39 +00003158 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003159
Greg Claytoneffe5c92011-05-03 22:09:39 +00003160 // Instance variables to hold the values for command options.
3161 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3162 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003163 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003164 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003165 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003166
Greg Claytoneffe5c92011-05-03 22:09:39 +00003167 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003168 CommandObjectParsed (interpreter,
3169 "target modules list",
3170 "List current executable and dependent shared library images.",
3171 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003172 m_options (interpreter)
3173 {
3174 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003175
Greg Claytoneffe5c92011-05-03 22:09:39 +00003176 virtual
3177 ~CommandObjectTargetModulesList ()
3178 {
3179 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003180
Greg Claytoneffe5c92011-05-03 22:09:39 +00003181 virtual
3182 Options *
3183 GetOptions ()
3184 {
3185 return &m_options;
3186 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003187
Jim Ingham5a988412012-06-08 21:56:10 +00003188protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003189 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00003190 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003191 CommandReturnObject &result)
3192 {
3193 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003194 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003195 // Define a local module list here to ensure it lives longer than any "locker"
3196 // object which might lock its contents below (through the "module_list_ptr"
3197 // variable).
3198 ModuleList module_list;
Greg Clayton3418c852011-08-10 02:10:13 +00003199 if (target == NULL && use_global_module_list == false)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003200 {
3201 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3202 result.SetStatus (eReturnStatusFailed);
3203 return false;
3204 }
3205 else
3206 {
Greg Clayton3418c852011-08-10 02:10:13 +00003207 if (target)
3208 {
3209 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3210 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3211 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3212 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003213 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003214 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003215
Jim Inghamc10312c2011-10-24 18:36:33 +00003216 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3217 {
3218 if (target)
3219 {
3220 Address module_address;
3221 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3222 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003223 ModuleSP module_sp (module_address.GetModule());
3224 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003225 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003226 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003227 result.SetStatus (eReturnStatusSuccessFinishResult);
3228 }
3229 else
3230 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003231 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003232 result.SetStatus (eReturnStatusFailed);
3233 }
3234 }
3235 else
3236 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003237 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003238 result.SetStatus (eReturnStatusFailed);
3239 }
3240 }
3241 else
3242 {
3243 result.AppendError ("Can only look up modules by address with a valid target.");
3244 result.SetStatus (eReturnStatusFailed);
3245 }
3246 return result.Succeeded();
3247 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003248
Greg Claytonc7bece562013-01-25 18:06:21 +00003249 size_t num_modules = 0;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003250 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-NULL.
3251 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3252 // the global module list directly.
Enrico Granata17598482012-11-08 02:22:02 +00003253 const ModuleList *module_list_ptr = NULL;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003254 const size_t argc = command.GetArgumentCount();
3255 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003256 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003257 if (use_global_module_list)
3258 {
3259 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3260 num_modules = Module::GetNumberAllocatedModules();
3261 }
3262 else
3263 {
3264 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003265 }
Greg Clayton65a03992011-08-09 00:01:09 +00003266 }
3267 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003268 {
3269 for (size_t i=0; i<argc; ++i)
3270 {
3271 // Dump specified images (by basename or fullpath)
3272 const char *arg_cstr = command.GetArgumentAtIndex(i);
3273 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3274 if (num_matches == 0)
3275 {
3276 if (argc == 1)
3277 {
3278 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3279 result.SetStatus (eReturnStatusFailed);
3280 return false;
3281 }
3282 }
3283 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003284
Greg Claytonc4a8a762012-05-15 18:43:44 +00003285 module_list_ptr = &module_list;
3286 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003287
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003288 if (module_list_ptr != NULL)
3289 {
3290 locker.Lock(module_list_ptr->GetMutex());
3291 num_modules = module_list_ptr->GetSize();
3292 }
Greg Clayton65a03992011-08-09 00:01:09 +00003293
Greg Claytoneffe5c92011-05-03 22:09:39 +00003294 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003295 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003296 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3297 {
Greg Clayton3418c852011-08-10 02:10:13 +00003298 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003299 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003300 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003301 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003302 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003303 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003304 }
3305 else
3306 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003307 module = Module::GetAllocatedModuleAtIndex(image_idx);
3308 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003309 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003310
Greg Claytonc7bece562013-01-25 18:06:21 +00003311 const size_t indent = strm.Printf("[%3u] ", image_idx);
3312 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003313
Greg Claytoneffe5c92011-05-03 22:09:39 +00003314 }
3315 result.SetStatus (eReturnStatusSuccessFinishResult);
3316 }
3317 else
3318 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003319 if (argc)
3320 {
3321 if (use_global_module_list)
3322 result.AppendError ("the global module list has no matching modules");
3323 else
3324 result.AppendError ("the target has no matching modules");
3325 }
Greg Clayton3418c852011-08-10 02:10:13 +00003326 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003327 {
3328 if (use_global_module_list)
3329 result.AppendError ("the global module list is empty");
3330 else
3331 result.AppendError ("the target has no associated executable images");
3332 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003333 result.SetStatus (eReturnStatusFailed);
3334 return false;
3335 }
3336 }
3337 return result.Succeeded();
3338 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003339
3340 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003341 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003342 {
3343
Jim Ingham28eb5712012-10-12 17:34:26 +00003344 if (module == NULL)
3345 {
3346 strm.PutCString("Null module");
3347 return;
3348 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003349
Jim Inghamc10312c2011-10-24 18:36:33 +00003350 bool dump_object_name = false;
3351 if (m_options.m_format_array.empty())
3352 {
Greg Claytonc9660542012-02-05 02:38:54 +00003353 m_options.m_format_array.push_back(std::make_pair('u', 0));
3354 m_options.m_format_array.push_back(std::make_pair('h', 0));
3355 m_options.m_format_array.push_back(std::make_pair('f', 0));
3356 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003357 }
Greg Claytonc9660542012-02-05 02:38:54 +00003358 const size_t num_entries = m_options.m_format_array.size();
3359 bool print_space = false;
3360 for (size_t i=0; i<num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003361 {
Greg Claytonc9660542012-02-05 02:38:54 +00003362 if (print_space)
3363 strm.PutChar(' ');
3364 print_space = true;
3365 const char format_char = m_options.m_format_array[i].first;
3366 uint32_t width = m_options.m_format_array[i].second;
3367 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003368 {
Greg Claytonc9660542012-02-05 02:38:54 +00003369 case 'A':
3370 DumpModuleArchitecture (strm, module, false, width);
3371 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003372
Greg Claytonc9660542012-02-05 02:38:54 +00003373 case 't':
3374 DumpModuleArchitecture (strm, module, true, width);
3375 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003376
Greg Claytonc9660542012-02-05 02:38:54 +00003377 case 'f':
3378 DumpFullpath (strm, &module->GetFileSpec(), width);
3379 dump_object_name = true;
3380 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003381
Greg Claytonc9660542012-02-05 02:38:54 +00003382 case 'd':
3383 DumpDirectory (strm, &module->GetFileSpec(), width);
3384 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003385
Greg Claytonc9660542012-02-05 02:38:54 +00003386 case 'b':
3387 DumpBasename (strm, &module->GetFileSpec(), width);
3388 dump_object_name = true;
3389 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003390
Greg Claytonc9660542012-02-05 02:38:54 +00003391 case 'h':
3392 case 'o':
3393 // Image header address
3394 {
3395 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003396
Greg Claytonc9660542012-02-05 02:38:54 +00003397 ObjectFile *objfile = module->GetObjectFile ();
3398 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003399 {
Greg Claytonc9660542012-02-05 02:38:54 +00003400 Address header_addr(objfile->GetHeaderAddress());
3401 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003402 {
Greg Claytonc9660542012-02-05 02:38:54 +00003403 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003404 {
Greg Claytonc9660542012-02-05 02:38:54 +00003405 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3406 if (header_load_addr == LLDB_INVALID_ADDRESS)
3407 {
3408 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3409 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003410 else
Greg Claytonc9660542012-02-05 02:38:54 +00003411 {
3412 if (format_char == 'o')
3413 {
3414 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003415 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003416 }
3417 else
3418 {
3419 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003420 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003421 }
3422 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003423 break;
3424 }
Greg Claytonc9660542012-02-05 02:38:54 +00003425 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3426 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3427 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003428 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003429 }
Greg Claytonc9660542012-02-05 02:38:54 +00003430 strm.Printf ("%*s", addr_nibble_width + 2, "");
3431 }
3432 break;
3433 case 'r':
3434 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003435 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003436 ModuleSP module_sp (module->shared_from_this());
3437 if (module_sp)
3438 {
3439 // Take one away to make sure we don't count our local "module_sp"
3440 ref_count = module_sp.use_count() - 1;
3441 }
3442 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003443 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003444 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003445 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003446 }
3447 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003448
Greg Claytonc9660542012-02-05 02:38:54 +00003449 case 's':
3450 case 'S':
3451 {
3452 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
3453 if (symbol_vendor)
3454 {
3455 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
3456 if (symbol_file)
3457 {
3458 if (format_char == 'S')
3459 {
3460 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec();
3461 // Dump symbol file only if different from module file
3462 if (!symfile_spec || symfile_spec == module->GetFileSpec())
3463 {
3464 print_space = false;
3465 break;
3466 }
3467 // Add a newline and indent past the index
3468 strm.Printf ("\n%*s", indent, "");
3469 }
3470 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
3471 dump_object_name = true;
3472 break;
3473 }
3474 }
3475 strm.Printf("%.*s", width, "<NONE>");
3476 }
3477 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003478
Greg Claytonc9660542012-02-05 02:38:54 +00003479 case 'm':
3480 module->GetModificationTime().Dump(&strm, width);
3481 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003482
Greg Claytonc9660542012-02-05 02:38:54 +00003483 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003484 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003485 break;
3486
3487 case 'u':
3488 DumpModuleUUID(strm, module);
3489 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003490
Greg Claytonc9660542012-02-05 02:38:54 +00003491 default:
3492 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003493 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003494
Greg Claytonc9660542012-02-05 02:38:54 +00003495 }
3496 if (dump_object_name)
3497 {
3498 const char *object_name = module->GetObjectName().GetCString();
3499 if (object_name)
3500 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003501 }
3502 strm.EOL();
3503 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003504
Greg Claytoneffe5c92011-05-03 22:09:39 +00003505 CommandOptions m_options;
3506};
3507
3508OptionDefinition
3509CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3510{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003511 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3512 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
3513 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the triple when listing images."},
3514 { 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."},
3515 { 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)."},
3516 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
3517 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3518 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3519 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3520 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3521 { 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."},
3522 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3523 { 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."},
3524 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, NULL, NULL, 0, eArgTypeNone, "Display the module pointer."},
3525 { 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."},
3526 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003527};
3528
Jason Molenda380241a2012-07-12 00:20:07 +00003529#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003530
Jason Molenda380241a2012-07-12 00:20:07 +00003531//----------------------------------------------------------------------
3532// Lookup unwind information in images
3533//----------------------------------------------------------------------
3534
3535class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3536{
3537public:
3538
3539 enum
3540 {
3541 eLookupTypeInvalid = -1,
3542 eLookupTypeAddress = 0,
3543 eLookupTypeSymbol,
3544 eLookupTypeFunction,
3545 eLookupTypeFunctionOrSymbol,
3546 kNumLookupTypes
3547 };
3548
3549 class CommandOptions : public Options
3550 {
3551 public:
3552
3553 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003554 Options(interpreter),
3555 m_type(eLookupTypeInvalid),
3556 m_str(),
3557 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003558 {
3559 }
3560
3561 virtual
3562 ~CommandOptions ()
3563 {
3564 }
3565
3566 virtual Error
3567 SetOptionValue (uint32_t option_idx, const char *option_arg)
3568 {
3569 Error error;
3570
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003571 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003572
3573 switch (short_option)
3574 {
3575 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003576 {
3577 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003578 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003579 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003580 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003581 if (m_addr == LLDB_INVALID_ADDRESS)
3582 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3583 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003584 }
Jason Molenda380241a2012-07-12 00:20:07 +00003585
3586 case 'n':
Jason Molenda535ab862013-04-23 04:30:57 +00003587 {
Jason Molenda380241a2012-07-12 00:20:07 +00003588 m_str = option_arg;
3589 m_type = eLookupTypeFunctionOrSymbol;
3590 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003591 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003592
3593 default:
3594 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3595 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003596 }
3597
3598 return error;
3599 }
3600
3601 void
3602 OptionParsingStarting ()
3603 {
3604 m_type = eLookupTypeInvalid;
3605 m_str.clear();
3606 m_addr = LLDB_INVALID_ADDRESS;
3607 }
3608
3609 const OptionDefinition*
3610 GetDefinitions ()
3611 {
3612 return g_option_table;
3613 }
3614
3615 // Options table: Required for subclasses of Options.
3616
3617 static OptionDefinition g_option_table[];
3618
3619 // Instance variables to hold the values for command options.
3620
3621 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3622 std::string m_str; // Holds name lookup
3623 lldb::addr_t m_addr; // Holds the address to lookup
3624 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003625
Jason Molenda380241a2012-07-12 00:20:07 +00003626 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
3627 CommandObjectParsed (interpreter,
3628 "target modules show-unwind",
3629 "Show synthesized unwind instructions for a function.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003630 NULL,
3631 eFlagRequiresTarget |
3632 eFlagRequiresProcess |
3633 eFlagProcessMustBeLaunched |
3634 eFlagProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003635 m_options (interpreter)
3636 {
3637 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003638
Jason Molenda380241a2012-07-12 00:20:07 +00003639 virtual
3640 ~CommandObjectTargetModulesShowUnwind ()
3641 {
3642 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003643
Jason Molenda380241a2012-07-12 00:20:07 +00003644 virtual
3645 Options *
3646 GetOptions ()
3647 {
3648 return &m_options;
3649 }
3650
3651protected:
3652 bool
3653 DoExecute (Args& command,
3654 CommandReturnObject &result)
3655 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003656 Target *target = m_exe_ctx.GetTargetPtr();
3657 Process *process = m_exe_ctx.GetProcessPtr();
Jason Molenda380241a2012-07-12 00:20:07 +00003658 ABI *abi = NULL;
3659 if (process)
3660 abi = process->GetABI().get();
3661
3662 if (process == NULL)
3663 {
3664 result.AppendError ("You must have a process running to use this command.");
3665 result.SetStatus (eReturnStatusFailed);
3666 return false;
3667 }
3668
3669 ThreadList threads(process->GetThreadList());
3670 if (threads.GetSize() == 0)
3671 {
3672 result.AppendError ("The process must be paused to use this command.");
3673 result.SetStatus (eReturnStatusFailed);
3674 return false;
3675 }
3676
3677 ThreadSP thread(threads.GetThreadAtIndex(0));
3678 if (thread.get() == NULL)
3679 {
3680 result.AppendError ("The process must be paused to use this command.");
3681 result.SetStatus (eReturnStatusFailed);
3682 return false;
3683 }
3684
Jason Molenda535ab862013-04-23 04:30:57 +00003685 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003686
Jason Molenda380241a2012-07-12 00:20:07 +00003687 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3688 {
Jason Molenda380241a2012-07-12 00:20:07 +00003689 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003690 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3691 }
3692 else if (m_options.m_type == eLookupTypeAddress && target)
3693 {
3694 Address addr;
3695 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003696 {
3697 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003698 ModuleSP module_sp (addr.GetModule());
3699 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3700 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003701 {
Jason Molenda535ab862013-04-23 04:30:57 +00003702 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003703 }
Jason Molenda535ab862013-04-23 04:30:57 +00003704 }
3705 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003706 else
3707 {
3708 result.AppendError ("address-expression or function name option must be specified.");
3709 result.SetStatus (eReturnStatusFailed);
3710 return false;
3711 }
Jason Molenda380241a2012-07-12 00:20:07 +00003712
Jason Molenda535ab862013-04-23 04:30:57 +00003713 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003714 if (num_matches == 0)
3715 {
3716 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3717 result.SetStatus (eReturnStatusFailed);
3718 return false;
3719 }
3720
Jason Molenda535ab862013-04-23 04:30:57 +00003721 for (uint32_t idx = 0; idx < num_matches; idx++)
3722 {
3723 SymbolContext sc;
3724 sc_list.GetContextAtIndex(idx, sc);
3725 if (sc.symbol == NULL && sc.function == NULL)
3726 continue;
3727 if (sc.module_sp.get() == NULL || sc.module_sp->GetObjectFile() == NULL)
3728 continue;
3729 AddressRange range;
3730 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3731 continue;
3732 if (!range.GetBaseAddress().IsValid())
3733 continue;
3734 ConstString funcname(sc.GetFunctionName());
3735 if (funcname.IsEmpty())
3736 continue;
3737 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3738 if (abi)
3739 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003740
Jason Molenda535ab862013-04-23 04:30:57 +00003741 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3742 if (func_unwinders_sp.get() == NULL)
3743 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003744
Jason Molenda34549b82015-01-13 06:04:04 +00003745 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 +00003746
Todd Fiala05625242014-08-25 20:29:09 +00003747 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread.get(), -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003748 if (non_callsite_unwind_plan.get())
3749 {
Jason Molenda34549b82015-01-13 06:04:04 +00003750 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 +00003751 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003752 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Jason Molenda535ab862013-04-23 04:30:57 +00003753 if (callsite_unwind_plan.get())
3754 {
Jason Molenda34549b82015-01-13 06:04:04 +00003755 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003756 }
Jason Molenda535ab862013-04-23 04:30:57 +00003757 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*thread.get());
3758 if (fast_unwind_plan.get())
3759 {
Jason Molenda34549b82015-01-13 06:04:04 +00003760 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003761 }
3762
Jason Molenda34549b82015-01-13 06:04:04 +00003763 result.GetOutputStream().Printf("\n");
3764
3765 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread.get(), 0);
3766 if (assembly_sp)
3767 {
3768 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3769 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3770 result.GetOutputStream().Printf("\n");
3771 }
3772
3773
3774 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3775 if (ehframe_sp)
3776 {
3777 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3778 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3779 result.GetOutputStream().Printf("\n");
3780 }
3781
3782 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread.get(), 0);
3783 if (ehframe_augmented_sp)
3784 {
3785 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3786 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3787 result.GetOutputStream().Printf("\n");
3788 }
3789
3790 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3791 if (compact_unwind_sp)
3792 {
3793 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3794 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3795 result.GetOutputStream().Printf("\n");
3796 }
3797
3798 if (fast_unwind_plan)
3799 {
3800 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3801 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3802 result.GetOutputStream().Printf("\n");
3803 }
3804
3805 ABISP abi_sp = process->GetABI();
3806 if (abi_sp)
3807 {
3808 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3809 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3810 {
3811 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3812 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3813 result.GetOutputStream().Printf("\n");
3814 }
3815
3816 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3817 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3818 {
3819 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3820 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3821 result.GetOutputStream().Printf("\n");
3822 }
3823 }
Jason Molenda535ab862013-04-23 04:30:57 +00003824
3825 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003826 }
3827 return result.Succeeded();
3828 }
3829
3830 CommandOptions m_options;
3831};
3832
3833OptionDefinition
3834CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3835{
Zachary Turnerd37221d2014-07-09 16:31:49 +00003836 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3837 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3838 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jason Molenda380241a2012-07-12 00:20:07 +00003839};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003840
3841//----------------------------------------------------------------------
3842// Lookup information in images
3843//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003844class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003845{
3846public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003847 enum
3848 {
3849 eLookupTypeInvalid = -1,
3850 eLookupTypeAddress = 0,
3851 eLookupTypeSymbol,
3852 eLookupTypeFileLine, // Line is optional
3853 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003854 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003855 eLookupTypeType,
3856 kNumLookupTypes
3857 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003858
Greg Claytoneffe5c92011-05-03 22:09:39 +00003859 class CommandOptions : public Options
3860 {
3861 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003862 CommandOptions (CommandInterpreter &interpreter) :
3863 Options(interpreter)
3864 {
3865 OptionParsingStarting();
3866 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003867
Greg Claytoneffe5c92011-05-03 22:09:39 +00003868 virtual
3869 ~CommandOptions ()
3870 {
3871 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003872
Greg Claytoneffe5c92011-05-03 22:09:39 +00003873 virtual Error
3874 SetOptionValue (uint32_t option_idx, const char *option_arg)
3875 {
3876 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003877
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003878 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003879
Greg Claytoneffe5c92011-05-03 22:09:39 +00003880 switch (short_option)
3881 {
3882 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003883 {
3884 m_type = eLookupTypeAddress;
3885 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3886 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3887 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003888 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003889
Greg Claytoneffe5c92011-05-03 22:09:39 +00003890 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003891 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003892 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003893 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003894 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003895
Greg Claytoneffe5c92011-05-03 22:09:39 +00003896 case 's':
3897 m_str = option_arg;
3898 m_type = eLookupTypeSymbol;
3899 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003900
Greg Claytoneffe5c92011-05-03 22:09:39 +00003901 case 'f':
3902 m_file.SetFile (option_arg, false);
3903 m_type = eLookupTypeFileLine;
3904 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003905
Greg Claytoneffe5c92011-05-03 22:09:39 +00003906 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003907 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003908 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003909
Greg Claytoneffe5c92011-05-03 22:09:39 +00003910 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003911 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003912 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003913 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003914 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003915 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003916 m_type = eLookupTypeFileLine;
3917 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003918
Greg Claytonc4a8a762012-05-15 18:43:44 +00003919 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003920 m_str = option_arg;
3921 m_type = eLookupTypeFunction;
3922 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003923
Greg Claytonc4a8a762012-05-15 18:43:44 +00003924 case 'n':
3925 m_str = option_arg;
3926 m_type = eLookupTypeFunctionOrSymbol;
3927 break;
3928
Greg Claytoneffe5c92011-05-03 22:09:39 +00003929 case 't':
3930 m_str = option_arg;
3931 m_type = eLookupTypeType;
3932 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003933
Greg Claytoneffe5c92011-05-03 22:09:39 +00003934 case 'v':
3935 m_verbose = 1;
3936 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003937
Sean Callanand38b4a92012-06-06 20:49:55 +00003938 case 'A':
3939 m_print_all = true;
3940 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003941
Greg Claytoneffe5c92011-05-03 22:09:39 +00003942 case 'r':
3943 m_use_regex = true;
3944 break;
3945 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003946
Greg Claytoneffe5c92011-05-03 22:09:39 +00003947 return error;
3948 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003949
Greg Claytoneffe5c92011-05-03 22:09:39 +00003950 void
3951 OptionParsingStarting ()
3952 {
3953 m_type = eLookupTypeInvalid;
3954 m_str.clear();
3955 m_file.Clear();
3956 m_addr = LLDB_INVALID_ADDRESS;
3957 m_offset = 0;
3958 m_line_number = 0;
3959 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003960 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003961 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003962 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003963 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003964
Greg Claytoneffe5c92011-05-03 22:09:39 +00003965 const OptionDefinition*
3966 GetDefinitions ()
3967 {
3968 return g_option_table;
3969 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003970
Greg Claytoneffe5c92011-05-03 22:09:39 +00003971 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003972
Greg Claytoneffe5c92011-05-03 22:09:39 +00003973 static OptionDefinition g_option_table[];
3974 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3975 std::string m_str; // Holds name lookup
3976 FileSpec m_file; // Files for file lookups
3977 lldb::addr_t m_addr; // Holds the address to lookup
3978 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
3979 uint32_t m_line_number; // Line number for file+line lookups
3980 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00003981 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003982 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00003983 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00003984 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003985
Greg Claytoneffe5c92011-05-03 22:09:39 +00003986 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003987 CommandObjectParsed (interpreter,
3988 "target modules lookup",
3989 "Look up information within executable and dependent shared library images.",
Greg Claytonf9fc6092013-01-09 19:44:40 +00003990 NULL,
3991 eFlagRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00003992 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003993 {
3994 CommandArgumentEntry arg;
3995 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003996
Greg Claytoneffe5c92011-05-03 22:09:39 +00003997 // Define the first (and only) variant of this arg.
3998 file_arg.arg_type = eArgTypeFilename;
3999 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004000
Greg Claytoneffe5c92011-05-03 22:09:39 +00004001 // There is only one variant this argument could be; put it into the argument entry.
4002 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004003
Greg Claytoneffe5c92011-05-03 22:09:39 +00004004 // Push the data for the first argument into the m_arguments vector.
4005 m_arguments.push_back (arg);
4006 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004007
Greg Claytoneffe5c92011-05-03 22:09:39 +00004008 virtual
4009 ~CommandObjectTargetModulesLookup ()
4010 {
4011 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004012
Greg Claytoneffe5c92011-05-03 22:09:39 +00004013 virtual Options *
4014 GetOptions ()
4015 {
4016 return &m_options;
4017 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004018
Sean Callanand38b4a92012-06-06 20:49:55 +00004019 bool
4020 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4021 {
4022 switch (m_options.m_type)
4023 {
4024 case eLookupTypeAddress:
4025 case eLookupTypeFileLine:
4026 case eLookupTypeFunction:
4027 case eLookupTypeFunctionOrSymbol:
4028 case eLookupTypeSymbol:
4029 default:
4030 return false;
4031 case eLookupTypeType:
4032 break;
4033 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004034
Jason Molendab57e4a12013-11-04 09:33:30 +00004035 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004036
Sean Callanand38b4a92012-06-06 20:49:55 +00004037 if (!frame)
4038 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004039
Sean Callanand38b4a92012-06-06 20:49:55 +00004040 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004041
Sean Callanand38b4a92012-06-06 20:49:55 +00004042 if (!sym_ctx.module_sp)
4043 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004044
Sean Callanand38b4a92012-06-06 20:49:55 +00004045 switch (m_options.m_type)
4046 {
4047 default:
4048 return false;
4049 case eLookupTypeType:
4050 if (!m_options.m_str.empty())
4051 {
4052 if (LookupTypeHere (m_interpreter,
4053 result.GetOutputStream(),
4054 sym_ctx,
4055 m_options.m_str.c_str(),
4056 m_options.m_use_regex))
4057 {
4058 result.SetStatus(eReturnStatusSuccessFinishResult);
4059 return true;
4060 }
4061 }
4062 break;
4063 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004064
Sean Callanand38b4a92012-06-06 20:49:55 +00004065 return true;
4066 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004067
Greg Claytoneffe5c92011-05-03 22:09:39 +00004068 bool
4069 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4070 {
4071 switch (m_options.m_type)
4072 {
4073 case eLookupTypeAddress:
4074 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4075 {
4076 if (LookupAddressInModule (m_interpreter,
4077 result.GetOutputStream(),
4078 module,
Greg Clayton2501e5e2015-01-15 02:59:20 +00004079 eSymbolContextEverything | (m_options.m_verbose ? eSymbolContextVariable : 0),
Greg Claytoneffe5c92011-05-03 22:09:39 +00004080 m_options.m_addr,
4081 m_options.m_offset,
4082 m_options.m_verbose))
4083 {
4084 result.SetStatus(eReturnStatusSuccessFinishResult);
4085 return true;
4086 }
4087 }
4088 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004089
Greg Claytoneffe5c92011-05-03 22:09:39 +00004090 case eLookupTypeSymbol:
4091 if (!m_options.m_str.empty())
4092 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004093 if (LookupSymbolInModule (m_interpreter,
4094 result.GetOutputStream(),
4095 module,
4096 m_options.m_str.c_str(),
4097 m_options.m_use_regex,
4098 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004099 {
4100 result.SetStatus(eReturnStatusSuccessFinishResult);
4101 return true;
4102 }
4103 }
4104 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004105
Greg Claytoneffe5c92011-05-03 22:09:39 +00004106 case eLookupTypeFileLine:
4107 if (m_options.m_file)
4108 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004109 if (LookupFileAndLineInModule (m_interpreter,
4110 result.GetOutputStream(),
4111 module,
4112 m_options.m_file,
4113 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004114 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004115 m_options.m_verbose))
4116 {
4117 result.SetStatus(eReturnStatusSuccessFinishResult);
4118 return true;
4119 }
4120 }
4121 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004122
4123 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004124 case eLookupTypeFunction:
4125 if (!m_options.m_str.empty())
4126 {
4127 if (LookupFunctionInModule (m_interpreter,
4128 result.GetOutputStream(),
4129 module,
4130 m_options.m_str.c_str(),
4131 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004132 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004133 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004134 m_options.m_verbose))
4135 {
4136 result.SetStatus(eReturnStatusSuccessFinishResult);
4137 return true;
4138 }
4139 }
4140 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004141
Greg Claytoneffe5c92011-05-03 22:09:39 +00004142 case eLookupTypeType:
4143 if (!m_options.m_str.empty())
4144 {
4145 if (LookupTypeInModule (m_interpreter,
4146 result.GetOutputStream(),
4147 module,
4148 m_options.m_str.c_str(),
4149 m_options.m_use_regex))
4150 {
4151 result.SetStatus(eReturnStatusSuccessFinishResult);
4152 return true;
4153 }
4154 }
4155 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004156
Greg Claytoneffe5c92011-05-03 22:09:39 +00004157 default:
4158 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4159 syntax_error = true;
4160 break;
4161 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004162
Greg Claytoneffe5c92011-05-03 22:09:39 +00004163 result.SetStatus (eReturnStatusFailed);
4164 return false;
4165 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004166
Jim Ingham5a988412012-06-08 21:56:10 +00004167protected:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004168 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004169 DoExecute (Args& command,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004170 CommandReturnObject &result)
4171 {
4172 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
4173 if (target == NULL)
4174 {
4175 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4176 result.SetStatus (eReturnStatusFailed);
4177 return false;
4178 }
4179 else
4180 {
4181 bool syntax_error = false;
4182 uint32_t i;
4183 uint32_t num_successful_lookups = 0;
4184 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4185 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4186 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4187 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004188
Greg Claytoneffe5c92011-05-03 22:09:39 +00004189 if (command.GetArgumentCount() == 0)
4190 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004191 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004192
Sean Callanand38b4a92012-06-06 20:49:55 +00004193 // Where it is possible to look in the current symbol context
4194 // first, try that. If this search was successful and --all
4195 // was not passed, don't print anything else.
4196 if (LookupHere (m_interpreter, result, syntax_error))
4197 {
4198 result.GetOutputStream().EOL();
4199 num_successful_lookups++;
4200 if (!m_options.m_print_all)
4201 {
4202 result.SetStatus (eReturnStatusSuccessFinishResult);
4203 return result.Succeeded();
4204 }
4205 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004206
Sean Callanand38b4a92012-06-06 20:49:55 +00004207 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004208
Enrico Granata17598482012-11-08 02:22:02 +00004209 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004210 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004211 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004212 if (num_modules > 0)
4213 {
4214 for (i = 0; i<num_modules && syntax_error == false; ++i)
4215 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004216 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004217
Sean Callanand38b4a92012-06-06 20:49:55 +00004218 if (module_pointer != current_module.get() &&
4219 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004220 {
4221 result.GetOutputStream().EOL();
4222 num_successful_lookups++;
4223 }
4224 }
4225 }
4226 else
4227 {
4228 result.AppendError ("the target has no associated executable images");
4229 result.SetStatus (eReturnStatusFailed);
4230 return false;
4231 }
4232 }
4233 else
4234 {
4235 // Dump specified images (by basename or fullpath)
4236 const char *arg_cstr;
4237 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
4238 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004239 ModuleList module_list;
4240 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4241 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004242 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004243 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004244 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004245 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004246 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004247 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004248 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004249 {
4250 result.GetOutputStream().EOL();
4251 num_successful_lookups++;
4252 }
4253 }
4254 }
4255 }
4256 else
4257 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4258 }
4259 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004260
Greg Claytoneffe5c92011-05-03 22:09:39 +00004261 if (num_successful_lookups > 0)
4262 result.SetStatus (eReturnStatusSuccessFinishResult);
4263 else
4264 result.SetStatus (eReturnStatusFailed);
4265 }
4266 return result.Succeeded();
4267 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004268
Greg Claytoneffe5c92011-05-03 22:09:39 +00004269 CommandOptions m_options;
4270};
4271
4272OptionDefinition
4273CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4274{
Zachary Turnerd37221d2014-07-09 16:31:49 +00004275 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4276 { 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 +00004277 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4278 /* 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 +00004279 false, "regex", 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4280 { 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."},
4281 { 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."},
4282 { 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 +00004283 { LLDB_OPT_SET_FROM_TO(3,5),
Zachary Turnerd37221d2014-07-09 16:31:49 +00004284 false, "no-inlines", 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4285 { 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."},
4286 { 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."},
4287 { 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."},
4288 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
4289 { 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."},
4290 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004291};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004292
4293
Jim Ingham9575d842011-03-11 03:53:59 +00004294#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004295
4296//-------------------------------------------------------------------------
4297// CommandObjectMultiwordImageSearchPaths
4298//-------------------------------------------------------------------------
4299
Greg Claytoneffe5c92011-05-03 22:09:39 +00004300class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301{
4302public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004303 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4304 CommandObjectMultiword (interpreter,
4305 "target modules search-paths",
4306 "A set of commands for operating on debugger target image search paths.",
4307 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004308 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004309 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4310 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4311 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4312 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4313 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004314 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004315
Greg Claytoneffe5c92011-05-03 22:09:39 +00004316 ~CommandObjectTargetModulesImageSearchPaths()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004317 {
4318 }
4319};
4320
Greg Claytoneffe5c92011-05-03 22:09:39 +00004321
4322
4323#pragma mark CommandObjectTargetModules
4324
4325//-------------------------------------------------------------------------
4326// CommandObjectTargetModules
4327//-------------------------------------------------------------------------
4328
4329class CommandObjectTargetModules : public CommandObjectMultiword
4330{
4331public:
4332 //------------------------------------------------------------------
4333 // Constructors and Destructors
4334 //------------------------------------------------------------------
4335 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4336 CommandObjectMultiword (interpreter,
4337 "target modules",
4338 "A set of commands for accessing information for one or more target modules.",
4339 "target modules <sub-command> ...")
4340 {
4341 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4342 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004343 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4344 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4345 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4346 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004347 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004348
4349 }
4350 virtual
4351 ~CommandObjectTargetModules()
4352 {
4353 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004354
Greg Claytoneffe5c92011-05-03 22:09:39 +00004355private:
4356 //------------------------------------------------------------------
4357 // For CommandObjectTargetModules only
4358 //------------------------------------------------------------------
4359 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4360};
4361
4362
Greg Claytone72dfb32012-02-24 01:59:29 +00004363
Jim Ingham5a988412012-06-08 21:56:10 +00004364class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004365{
4366public:
4367 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004368 CommandObjectParsed (interpreter,
4369 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004370 "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 +00004371 "target symbols add [<symfile>]", eFlagRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004372 m_option_group (interpreter),
4373 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4374 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4375
Greg Claytone72dfb32012-02-24 01:59:29 +00004376 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004377 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4378 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4379 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4380 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004381 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004382
Greg Claytone72dfb32012-02-24 01:59:29 +00004383 virtual
4384 ~CommandObjectTargetSymbolsAdd ()
4385 {
4386 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004387
Greg Claytonc7bece562013-01-25 18:06:21 +00004388 virtual int
Jim Ingham5a988412012-06-08 21:56:10 +00004389 HandleArgumentCompletion (Args &input,
4390 int &cursor_index,
4391 int &cursor_char_position,
4392 OptionElementVector &opt_element_vector,
4393 int match_start_point,
4394 int max_return_elements,
4395 bool &word_complete,
4396 StringList &matches)
4397 {
4398 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4399 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004400
Jim Ingham5a988412012-06-08 21:56:10 +00004401 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
4402 CommandCompletions::eDiskFileCompletion,
4403 completion_str.c_str(),
4404 match_start_point,
4405 max_return_elements,
4406 NULL,
4407 word_complete,
4408 matches);
4409 return matches.GetSize();
4410 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004411
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004412 virtual Options *
4413 GetOptions ()
4414 {
4415 return &m_option_group;
4416 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004417
Jim Ingham5a988412012-06-08 21:56:10 +00004418protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004419 bool
4420 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004421 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004422 bool &flush,
4423 CommandReturnObject &result)
4424 {
Greg Clayton89deb062012-12-12 01:15:30 +00004425 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4426 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004427 {
4428 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004429 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004430
Greg Clayton89deb062012-12-12 01:15:30 +00004431 if (!module_spec.GetUUID().IsValid())
4432 {
4433 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4434 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4435 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004436 // We now have a module that represents a symbol file
4437 // that can be used for a module that might exist in the
4438 // current target, so we need to find that module in the
4439 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004440 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004441
Greg Clayton9aae0a12013-05-15 19:52:08 +00004442 size_t num_matches = 0;
4443 // First extract all module specs from the symbol file
4444 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004445 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004446 {
4447 // Now extract the module spec that matches the target architecture
4448 ModuleSpec target_arch_module_spec;
4449 ModuleSpec symfile_module_spec;
4450 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4451 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4452 {
4453 // See if it has a UUID?
4454 if (symfile_module_spec.GetUUID().IsValid())
4455 {
4456 // It has a UUID, look for this UUID in the target modules
4457 ModuleSpec symfile_uuid_module_spec;
4458 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4459 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4460 }
4461 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004462
Greg Clayton9aae0a12013-05-15 19:52:08 +00004463 if (num_matches == 0)
4464 {
4465 // No matches yet, iterate through the module specs to find a UUID value that
4466 // we can match up to an image in our target
4467 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4468 for (size_t i=0; i<num_symfile_module_specs && num_matches == 0; ++i)
4469 {
4470 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4471 {
4472 if (symfile_module_spec.GetUUID().IsValid())
4473 {
4474 // It has a UUID, look for this UUID in the target modules
4475 ModuleSpec symfile_uuid_module_spec;
4476 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4477 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004478 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004479 }
4480 }
4481 }
4482 }
4483
4484 // Just try to match up the file by basename if we have no matches at this point
4485 if (num_matches == 0)
4486 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004487
Greg Clayton91c0e742013-01-11 23:44:27 +00004488 while (num_matches == 0)
4489 {
4490 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4491 // Empty string returned, lets bail
4492 if (!filename_no_extension)
4493 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004494
Greg Clayton91c0e742013-01-11 23:44:27 +00004495 // Check if there was no extension to strip and the basename is the same
4496 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4497 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004498
Greg Clayton91c0e742013-01-11 23:44:27 +00004499 // Replace basename with one less extension
4500 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004501
Greg Clayton91c0e742013-01-11 23:44:27 +00004502 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4503 }
4504
Greg Clayton89deb062012-12-12 01:15:30 +00004505 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004506 {
Greg Clayton89deb062012-12-12 01:15:30 +00004507 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4508 }
4509 else if (num_matches == 1)
4510 {
4511 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004512
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004513 // The module has not yet created its symbol vendor, we can just
4514 // give the existing target module the symfile path to use for
4515 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004516 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004517
Greg Clayton136dff82012-12-14 02:15:00 +00004518 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004519 if (symbol_vendor)
4520 {
4521 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004522
Greg Clayton89deb062012-12-12 01:15:30 +00004523 if (symbol_file)
4524 {
4525 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004526
Greg Clayton89deb062012-12-12 01:15:30 +00004527 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4528 {
4529 // Provide feedback that the symfile has been successfully added.
4530 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004531 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004532 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004533 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004534
Greg Clayton89deb062012-12-12 01:15:30 +00004535 // Let clients know something changed in the module
4536 // if it is currently loaded
4537 ModuleList module_list;
4538 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004539 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004540
Greg Clayton91c0e742013-01-11 23:44:27 +00004541 // Make sure we load any scripting resources that may be embedded
4542 // in the debug info files in case the platform supports that.
4543 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004544 StreamString feedback_stream;
4545 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004546 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004547 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4548 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4549 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004550 else if (feedback_stream.GetSize())
4551 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004552
Greg Clayton89deb062012-12-12 01:15:30 +00004553 flush = true;
4554 result.SetStatus (eReturnStatusSuccessFinishResult);
4555 return true;
4556 }
4557 }
4558 }
4559 // Clear the symbol file spec if anything went wrong
4560 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004561 }
4562
4563 if (module_spec.GetUUID().IsValid())
4564 {
4565 StreamString ss_symfile_uuid;
4566 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4567 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4568 symfile_path,
4569 ss_symfile_uuid.GetData(),
4570 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4571 ? "\n please specify the full path to the symbol file"
4572 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004573 }
4574 else
4575 {
Greg Clayton89deb062012-12-12 01:15:30 +00004576 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4577 symfile_path,
4578 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4579 ? "\n please specify the full path to the symbol file"
4580 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004581 }
4582 }
4583 else
4584 {
4585 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004586 }
Greg Clayton89deb062012-12-12 01:15:30 +00004587 result.SetStatus (eReturnStatusFailed);
4588 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004589 }
4590
Greg Claytone72dfb32012-02-24 01:59:29 +00004591 virtual bool
Jim Ingham5a988412012-06-08 21:56:10 +00004592 DoExecute (Args& args,
Greg Claytone72dfb32012-02-24 01:59:29 +00004593 CommandReturnObject &result)
4594 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004595 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004596 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004597 bool flush = false;
4598 ModuleSpec module_spec;
4599 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4600 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4601 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004602
Greg Claytonf9fc6092013-01-09 19:44:40 +00004603 const size_t argc = args.GetArgumentCount();
4604 if (argc == 0)
4605 {
4606 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004607 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004608 bool success = false;
4609 bool error_set = false;
4610 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004611 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004612 Process *process = m_exe_ctx.GetProcessPtr();
4613 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004614 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004615 const StateType process_state = process->GetState();
4616 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004617 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004618 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004619 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004620 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004621 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4622 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004623 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004624 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004625 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004626 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4627 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004628 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004629 module_spec.GetUUID() = frame_module_sp->GetUUID();
4630 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004631 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004632 else
4633 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004634 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004635 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004636 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004637 }
4638 else
4639 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004640 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004641 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004642 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004643 }
4644 else
4645 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004646 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004647 error_set = true;
4648 }
4649 }
4650 else
4651 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004652 result.AppendError ("a process must exist in order to use the --frame option");
4653 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004654 }
4655 }
4656 else
4657 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004658 if (uuid_option_set)
4659 {
4660 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4661 success |= module_spec.GetUUID().IsValid();
4662 }
4663 else if (file_option_set)
4664 {
4665 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4666 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4667 if (module_sp)
4668 {
4669 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4670 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4671 module_spec.GetUUID() = module_sp->GetUUID();
4672 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4673 }
4674 else
4675 {
4676 module_spec.GetArchitecture() = target->GetArchitecture();
4677 }
4678 success |= module_spec.GetFileSpec().Exists();
4679 }
4680 }
4681
4682 if (success)
4683 {
4684 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4685 {
4686 if (module_spec.GetSymbolFileSpec())
4687 success = AddModuleSymbols (target, module_spec, flush, result);
4688 }
4689 }
4690
4691 if (!success && !error_set)
4692 {
4693 StreamString error_strm;
4694 if (uuid_option_set)
4695 {
4696 error_strm.PutCString("unable to find debug symbols for UUID ");
4697 module_spec.GetUUID().Dump (&error_strm);
4698 }
4699 else if (file_option_set)
4700 {
4701 error_strm.PutCString("unable to find debug symbols for the executable file ");
4702 error_strm << module_spec.GetFileSpec();
4703 }
4704 else if (frame_option_set)
4705 {
4706 error_strm.PutCString("unable to find debug symbols for the current frame");
4707 }
4708 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004709 }
4710 }
4711 else
4712 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004713 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4714 }
4715 }
4716 else
4717 {
4718 if (uuid_option_set)
4719 {
4720 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4721 }
4722 else if (file_option_set)
4723 {
4724 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4725 }
4726 else if (frame_option_set)
4727 {
4728 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4729 }
4730 else
4731 {
4732 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004733
Greg Claytonf9fc6092013-01-09 19:44:40 +00004734 for (size_t i=0; i<argc; ++i)
4735 {
4736 const char *symfile_path = args.GetArgumentAtIndex(i);
4737 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004738 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004739 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4740 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004741 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004742 FileSpec symfile_spec;
4743 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4744 module_spec.GetSymbolFileSpec() = symfile_spec;
4745 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004746
Greg Claytonf9fc6092013-01-09 19:44:40 +00004747 ArchSpec arch;
4748 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004749
Greg Claytonf9fc6092013-01-09 19:44:40 +00004750 if (symfile_exists)
4751 {
4752 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004753 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004754 }
4755 else
4756 {
4757 char resolved_symfile_path[PATH_MAX];
4758 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4759 {
4760 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4761 {
4762 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4763 break;
4764 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004765 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004766 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4767 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004768 }
4769 }
4770 }
4771 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004772 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004773
Greg Claytonf9fc6092013-01-09 19:44:40 +00004774 if (flush)
4775 {
4776 Process *process = m_exe_ctx.GetProcessPtr();
4777 if (process)
4778 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004779 }
4780 return result.Succeeded();
4781 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004782
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004783 OptionGroupOptions m_option_group;
4784 OptionGroupUUID m_uuid_option_group;
4785 OptionGroupFile m_file_option;
4786 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004787};
4788
4789
4790#pragma mark CommandObjectTargetSymbols
4791
4792//-------------------------------------------------------------------------
4793// CommandObjectTargetSymbols
4794//-------------------------------------------------------------------------
4795
4796class CommandObjectTargetSymbols : public CommandObjectMultiword
4797{
4798public:
4799 //------------------------------------------------------------------
4800 // Constructors and Destructors
4801 //------------------------------------------------------------------
4802 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4803 CommandObjectMultiword (interpreter,
4804 "target symbols",
4805 "A set of commands for adding and managing debug symbol files.",
4806 "target symbols <sub-command> ...")
4807 {
4808 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004809
Greg Claytone72dfb32012-02-24 01:59:29 +00004810 }
4811 virtual
4812 ~CommandObjectTargetSymbols()
4813 {
4814 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004815
Greg Claytone72dfb32012-02-24 01:59:29 +00004816private:
4817 //------------------------------------------------------------------
4818 // For CommandObjectTargetModules only
4819 //------------------------------------------------------------------
4820 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4821};
4822
4823
Jim Ingham9575d842011-03-11 03:53:59 +00004824#pragma mark CommandObjectTargetStopHookAdd
4825
4826//-------------------------------------------------------------------------
4827// CommandObjectTargetStopHookAdd
4828//-------------------------------------------------------------------------
4829
Greg Clayton44d93782014-01-27 23:43:24 +00004830class CommandObjectTargetStopHookAdd :
4831 public CommandObjectParsed,
4832 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004833{
4834public:
4835
4836 class CommandOptions : public Options
4837 {
4838 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004839 CommandOptions (CommandInterpreter &interpreter) :
4840 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004841 m_line_start(0),
4842 m_line_end (UINT_MAX),
4843 m_func_name_type_mask (eFunctionNameTypeAuto),
4844 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004845 m_thread_specified (false),
4846 m_use_one_liner (false),
4847 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004848 {
4849 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004850
Jim Ingham9575d842011-03-11 03:53:59 +00004851 ~CommandOptions () {}
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004852
Greg Claytone0d378b2011-03-24 21:19:54 +00004853 const OptionDefinition*
Jim Ingham9575d842011-03-11 03:53:59 +00004854 GetDefinitions ()
4855 {
4856 return g_option_table;
4857 }
4858
4859 virtual Error
Greg Claytonf6b8b582011-04-13 00:18:08 +00004860 SetOptionValue (uint32_t option_idx, const char *option_arg)
Jim Ingham9575d842011-03-11 03:53:59 +00004861 {
4862 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004863 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004864 bool success;
4865
4866 switch (short_option)
4867 {
4868 case 'c':
4869 m_class_name = option_arg;
4870 m_sym_ctx_specified = true;
4871 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004872
Jim Ingham9575d842011-03-11 03:53:59 +00004873 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004874 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004875 if (!success)
4876 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004877 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004878 break;
4879 }
4880 m_sym_ctx_specified = true;
4881 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004882
Jim Ingham9575d842011-03-11 03:53:59 +00004883 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004884 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004885 if (!success)
4886 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004887 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004888 break;
4889 }
4890 m_sym_ctx_specified = true;
4891 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004892
Sean Callanand4a7c122012-02-11 01:22:21 +00004893 case 'i':
4894 m_no_inlines = true;
4895 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004896
Jim Ingham9575d842011-03-11 03:53:59 +00004897 case 'n':
4898 m_function_name = option_arg;
4899 m_func_name_type_mask |= eFunctionNameTypeAuto;
4900 m_sym_ctx_specified = true;
4901 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004902
Jim Ingham9575d842011-03-11 03:53:59 +00004903 case 'f':
4904 m_file_name = option_arg;
4905 m_sym_ctx_specified = true;
4906 break;
4907 case 's':
4908 m_module_name = option_arg;
4909 m_sym_ctx_specified = true;
4910 break;
4911 case 't' :
4912 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004913 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004914 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004915 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004916 m_thread_specified = true;
4917 }
4918 break;
4919 case 'T':
4920 m_thread_name = option_arg;
4921 m_thread_specified = true;
4922 break;
4923 case 'q':
4924 m_queue_name = option_arg;
4925 m_thread_specified = true;
4926 break;
4927 case 'x':
4928 {
Vince Harron5275aaa2015-01-15 20:08:35 +00004929 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004930 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004931 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004932 m_thread_specified = true;
4933 }
4934 break;
Johnny Chenb1372c02011-05-02 23:47:55 +00004935 case 'o':
4936 m_use_one_liner = true;
4937 m_one_liner = option_arg;
4938 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004939 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004940 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Jim Ingham9575d842011-03-11 03:53:59 +00004941 break;
4942 }
4943 return error;
4944 }
4945
4946 void
Greg Claytonf6b8b582011-04-13 00:18:08 +00004947 OptionParsingStarting ()
Jim Ingham9575d842011-03-11 03:53:59 +00004948 {
4949 m_class_name.clear();
4950 m_function_name.clear();
4951 m_line_start = 0;
4952 m_line_end = UINT_MAX;
4953 m_file_name.clear();
4954 m_module_name.clear();
4955 m_func_name_type_mask = eFunctionNameTypeAuto;
4956 m_thread_id = LLDB_INVALID_THREAD_ID;
4957 m_thread_index = UINT32_MAX;
4958 m_thread_name.clear();
4959 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004960
Sean Callanand4a7c122012-02-11 01:22:21 +00004961 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004962 m_sym_ctx_specified = false;
4963 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004964
4965 m_use_one_liner = false;
4966 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004967 }
4968
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004969
Greg Claytone0d378b2011-03-24 21:19:54 +00004970 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004971
Jim Ingham9575d842011-03-11 03:53:59 +00004972 std::string m_class_name;
4973 std::string m_function_name;
4974 uint32_t m_line_start;
4975 uint32_t m_line_end;
4976 std::string m_file_name;
4977 std::string m_module_name;
4978 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4979 lldb::tid_t m_thread_id;
4980 uint32_t m_thread_index;
4981 std::string m_thread_name;
4982 std::string m_queue_name;
4983 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004984 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004985 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00004986 // Instance variables to hold the values for one_liner options.
4987 bool m_use_one_liner;
4988 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00004989 };
4990
4991 Options *
4992 GetOptions ()
4993 {
4994 return &m_options;
4995 }
4996
4997 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004998 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00004999 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005000 "Add a hook to be executed when the target stops.",
5001 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005002 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005003 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005004 {
5005 }
5006
5007 ~CommandObjectTargetStopHookAdd ()
5008 {
5009 }
5010
Jim Ingham5a988412012-06-08 21:56:10 +00005011protected:
Greg Clayton44d93782014-01-27 23:43:24 +00005012 virtual void
5013 IOHandlerActivated (IOHandler &io_handler)
5014 {
5015 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5016 if (output_sp)
5017 {
5018 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5019 output_sp->Flush();
5020 }
5021 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005022
Greg Clayton44d93782014-01-27 23:43:24 +00005023 virtual void
5024 IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
5025 {
5026 if (m_stop_hook_sp)
5027 {
5028 if (line.empty())
5029 {
5030 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5031 if (error_sp)
5032 {
5033 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5034 error_sp->Flush();
5035 }
5036 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5037 if (target)
5038 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5039 }
5040 else
5041 {
5042 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5043 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5044 if (output_sp)
5045 {
5046 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5047 output_sp->Flush();
5048 }
5049 }
5050 m_stop_hook_sp.reset();
5051 }
5052 io_handler.SetIsDone(true);
5053 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005054
Jim Ingham9575d842011-03-11 03:53:59 +00005055 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005056 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005057 {
Greg Clayton44d93782014-01-27 23:43:24 +00005058 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005059
Jim Ingham893c9322014-11-22 01:42:44 +00005060 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005061 if (target)
5062 {
Greg Clayton44d93782014-01-27 23:43:24 +00005063 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005064
5065 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005066 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005067 if (m_options.m_sym_ctx_specified)
5068 {
5069 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005070
Jim Ingham9575d842011-03-11 03:53:59 +00005071 if (!m_options.m_module_name.empty())
5072 {
5073 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5074 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005075
Jim Ingham9575d842011-03-11 03:53:59 +00005076 if (!m_options.m_class_name.empty())
5077 {
5078 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5079 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005080
Jim Ingham9575d842011-03-11 03:53:59 +00005081 if (!m_options.m_file_name.empty())
5082 {
5083 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5084 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005085
Jim Ingham9575d842011-03-11 03:53:59 +00005086 if (m_options.m_line_start != 0)
5087 {
5088 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5089 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005090
Jim Ingham9575d842011-03-11 03:53:59 +00005091 if (m_options.m_line_end != UINT_MAX)
5092 {
5093 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5094 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005095
Jim Ingham9575d842011-03-11 03:53:59 +00005096 if (!m_options.m_function_name.empty())
5097 {
5098 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5099 }
5100 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005101
Jim Ingham9575d842011-03-11 03:53:59 +00005102 if (specifier_ap.get())
5103 new_hook_sp->SetSpecifier (specifier_ap.release());
5104
5105 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005106
Jim Ingham9575d842011-03-11 03:53:59 +00005107 if (m_options.m_thread_specified)
5108 {
5109 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005110
Jim Ingham9575d842011-03-11 03:53:59 +00005111 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5112 {
5113 thread_spec->SetTID (m_options.m_thread_id);
5114 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005115
Jim Ingham9575d842011-03-11 03:53:59 +00005116 if (m_options.m_thread_index != UINT32_MAX)
5117 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005118
Jim Ingham9575d842011-03-11 03:53:59 +00005119 if (!m_options.m_thread_name.empty())
5120 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005121
Jim Ingham9575d842011-03-11 03:53:59 +00005122 if (!m_options.m_queue_name.empty())
5123 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005124
Jim Ingham9575d842011-03-11 03:53:59 +00005125 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005126
Jim Ingham9575d842011-03-11 03:53:59 +00005127 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005128 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005129 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005130 // Use one-liner.
5131 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005132 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005133 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005134 else
Jim Ingham9575d842011-03-11 03:53:59 +00005135 {
Greg Clayton44d93782014-01-27 23:43:24 +00005136 m_stop_hook_sp = new_hook_sp;
5137 m_interpreter.GetLLDBCommandsFromIOHandler ("> ", // Prompt
5138 *this, // IOHandlerDelegate
5139 true, // Run IOHandler in async mode
5140 NULL); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
5141
Jim Ingham9575d842011-03-11 03:53:59 +00005142 }
Jim Ingham9575d842011-03-11 03:53:59 +00005143 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5144 }
5145 else
5146 {
Greg Clayton7260f622011-04-18 08:33:37 +00005147 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005148 result.SetStatus (eReturnStatusFailed);
5149 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005150
Jim Ingham9575d842011-03-11 03:53:59 +00005151 return result.Succeeded();
5152 }
5153private:
5154 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005155 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005156};
5157
Greg Claytone0d378b2011-03-24 21:19:54 +00005158OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005159CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5160{
Zachary Turnerd37221d2014-07-09 16:31:49 +00005161 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005162 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005163 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005164 "Set the module within which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005165 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005166 "The stop hook is run only for the thread whose index matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005167 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005168 "The stop hook is run only for the thread whose TID matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005169 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005170 "The stop hook is run only for the thread whose thread name matches this argument."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005171 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005172 "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 +00005173 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005174 "Specify the source file within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005175 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005176 "Set the start of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005177 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005178 "Set the end of the line range for which the stop-hook is to be run."},
Zachary Turnerd37221d2014-07-09 16:31:49 +00005179 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005180 "Specify the class within which the stop-hook is to be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005181 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005182 "Set the function name within which the stop hook will be run." },
Zachary Turnerd37221d2014-07-09 16:31:49 +00005183 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
Jim Ingham9575d842011-03-11 03:53:59 +00005184};
5185
5186#pragma mark CommandObjectTargetStopHookDelete
5187
5188//-------------------------------------------------------------------------
5189// CommandObjectTargetStopHookDelete
5190//-------------------------------------------------------------------------
5191
Jim Ingham5a988412012-06-08 21:56:10 +00005192class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005193{
5194public:
5195
5196 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005197 CommandObjectParsed (interpreter,
5198 "target stop-hook delete",
5199 "Delete a stop-hook.",
5200 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005201 {
5202 }
5203
5204 ~CommandObjectTargetStopHookDelete ()
5205 {
5206 }
5207
Jim Ingham5a988412012-06-08 21:56:10 +00005208protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005209 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005210 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005211 {
Jim Ingham893c9322014-11-22 01:42:44 +00005212 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005213 if (target)
5214 {
5215 // FIXME: see if we can use the breakpoint id style parser?
5216 size_t num_args = command.GetArgumentCount();
5217 if (num_args == 0)
5218 {
5219 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5220 {
5221 result.SetStatus (eReturnStatusFailed);
5222 return false;
5223 }
5224 else
5225 {
5226 target->RemoveAllStopHooks();
5227 }
5228 }
5229 else
5230 {
5231 bool success;
5232 for (size_t i = 0; i < num_args; i++)
5233 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005234 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005235 if (!success)
5236 {
Greg Clayton7260f622011-04-18 08:33:37 +00005237 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005238 result.SetStatus(eReturnStatusFailed);
5239 return false;
5240 }
5241 success = target->RemoveStopHookByID (user_id);
5242 if (!success)
5243 {
Greg Clayton7260f622011-04-18 08:33:37 +00005244 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005245 result.SetStatus(eReturnStatusFailed);
5246 return false;
5247 }
5248 }
5249 }
5250 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5251 }
5252 else
5253 {
Greg Clayton7260f622011-04-18 08:33:37 +00005254 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005255 result.SetStatus (eReturnStatusFailed);
5256 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005257
Jim Ingham9575d842011-03-11 03:53:59 +00005258 return result.Succeeded();
5259 }
5260};
5261#pragma mark CommandObjectTargetStopHookEnableDisable
5262
5263//-------------------------------------------------------------------------
5264// CommandObjectTargetStopHookEnableDisable
5265//-------------------------------------------------------------------------
5266
Jim Ingham5a988412012-06-08 21:56:10 +00005267class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005268{
5269public:
5270
5271 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005272 CommandObjectParsed (interpreter,
5273 name,
5274 help,
5275 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005276 m_enable (enable)
5277 {
5278 }
5279
5280 ~CommandObjectTargetStopHookEnableDisable ()
5281 {
5282 }
5283
Jim Ingham5a988412012-06-08 21:56:10 +00005284protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005285 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005286 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005287 {
Jim Ingham893c9322014-11-22 01:42:44 +00005288 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005289 if (target)
5290 {
5291 // FIXME: see if we can use the breakpoint id style parser?
5292 size_t num_args = command.GetArgumentCount();
5293 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005294
Jim Ingham9575d842011-03-11 03:53:59 +00005295 if (num_args == 0)
5296 {
5297 target->SetAllStopHooksActiveState (m_enable);
5298 }
5299 else
5300 {
5301 for (size_t i = 0; i < num_args; i++)
5302 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005303 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005304 if (!success)
5305 {
Greg Clayton7260f622011-04-18 08:33:37 +00005306 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005307 result.SetStatus(eReturnStatusFailed);
5308 return false;
5309 }
5310 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5311 if (!success)
5312 {
Greg Clayton7260f622011-04-18 08:33:37 +00005313 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005314 result.SetStatus(eReturnStatusFailed);
5315 return false;
5316 }
5317 }
5318 }
5319 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5320 }
5321 else
5322 {
Greg Clayton7260f622011-04-18 08:33:37 +00005323 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005324 result.SetStatus (eReturnStatusFailed);
5325 }
5326 return result.Succeeded();
5327 }
5328private:
5329 bool m_enable;
5330};
5331
5332#pragma mark CommandObjectTargetStopHookList
5333
5334//-------------------------------------------------------------------------
5335// CommandObjectTargetStopHookList
5336//-------------------------------------------------------------------------
5337
Jim Ingham5a988412012-06-08 21:56:10 +00005338class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005339{
5340public:
5341
5342 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005343 CommandObjectParsed (interpreter,
5344 "target stop-hook list",
5345 "List all stop-hooks.",
5346 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005347 {
5348 }
5349
5350 ~CommandObjectTargetStopHookList ()
5351 {
5352 }
5353
Jim Ingham5a988412012-06-08 21:56:10 +00005354protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005355 bool
Jim Ingham5a988412012-06-08 21:56:10 +00005356 DoExecute (Args& command, CommandReturnObject &result)
Jim Ingham9575d842011-03-11 03:53:59 +00005357 {
Jim Ingham893c9322014-11-22 01:42:44 +00005358 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005359 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005360 {
Greg Clayton7260f622011-04-18 08:33:37 +00005361 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005362 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005363 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005364 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005365
Jim Ingham9575d842011-03-11 03:53:59 +00005366 size_t num_hooks = target->GetNumStopHooks ();
5367 if (num_hooks == 0)
5368 {
5369 result.GetOutputStream().PutCString ("No stop hooks.\n");
5370 }
5371 else
5372 {
5373 for (size_t i = 0; i < num_hooks; i++)
5374 {
5375 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5376 if (i > 0)
5377 result.GetOutputStream().PutCString ("\n");
5378 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5379 }
5380 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005381 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005382 return result.Succeeded();
5383 }
5384};
5385
5386#pragma mark CommandObjectMultiwordTargetStopHooks
5387//-------------------------------------------------------------------------
5388// CommandObjectMultiwordTargetStopHooks
5389//-------------------------------------------------------------------------
5390
5391class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5392{
5393public:
5394
5395 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5396 CommandObjectMultiword (interpreter,
5397 "target stop-hook",
5398 "A set of commands for operating on debugger target stop-hooks.",
5399 "target stop-hook <subcommand> [<subcommand-options>]")
5400 {
5401 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5402 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5403 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5404 false,
5405 "target stop-hook disable [<id>]",
5406 "Disable a stop-hook.",
5407 "target stop-hook disable")));
5408 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5409 true,
5410 "target stop-hook enable [<id>]",
5411 "Enable a stop-hook.",
5412 "target stop-hook enable")));
5413 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5414 }
5415
5416 ~CommandObjectMultiwordTargetStopHooks()
5417 {
5418 }
5419};
5420
5421
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005422
5423#pragma mark CommandObjectMultiwordTarget
5424
5425//-------------------------------------------------------------------------
5426// CommandObjectMultiwordTarget
5427//-------------------------------------------------------------------------
5428
Greg Clayton66111032010-06-23 01:19:29 +00005429CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005430 CommandObjectMultiword (interpreter,
5431 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005432 "A set of commands for operating on debugger targets.",
5433 "target <subcommand> [<subcommand-options>]")
5434{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005435
Greg Clayton7260f622011-04-18 08:33:37 +00005436 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005437 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005438 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5439 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005440 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005441 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005442 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005443 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005444}
5445
5446CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget ()
5447{
5448}
5449
Greg Clayton7260f622011-04-18 08:33:37 +00005450