blob: 1d92f317f9567027094e574fe1498e856fc3400d [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
10#include "CommandObjectTarget.h"
11
12// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013// C++ Includes
Eugene Zelenkof13e6522016-02-25 19:02:39 +000014#include <cerrno>
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016// Other libraries and framework includes
17// Project includes
Jim Ingham40af72e2010-06-15 19:49:27 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Debugger.h"
Greg Clayton44d93782014-01-27 23:43:24 +000020#include "lldb/Core/IOHandler.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Core/Module.h"
22#include "lldb/Core/ModuleSpec.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000023#include "lldb/Core/Section.h"
Greg Clayton7260f622011-04-18 08:33:37 +000024#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
Greg Clayton644247c2011-07-07 01:59:51 +000026#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000027#include "lldb/DataFormatters/ValueObjectPrinter.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000028#include "lldb/Host/StringConvert.h"
Greg Claytonc8f814d2012-09-27 03:13:55 +000029#include "lldb/Host/Symbols.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Interpreter/CommandInterpreter.h"
31#include "lldb/Interpreter/CommandReturnObject.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000032#include "lldb/Interpreter/Options.h"
Greg Clayton7260f622011-04-18 08:33:37 +000033#include "lldb/Interpreter/OptionGroupArchitecture.h"
Greg Claytonaa149cb2011-08-11 02:48:45 +000034#include "lldb/Interpreter/OptionGroupBoolean.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000035#include "lldb/Interpreter/OptionGroupFile.h"
Greg Clayton1deb7962011-10-25 06:44:01 +000036#include "lldb/Interpreter/OptionGroupFormat.h"
Greg Clayton715c2362011-07-07 04:38:25 +000037#include "lldb/Interpreter/OptionGroupVariable.h"
Greg Clayton7260f622011-04-18 08:33:37 +000038#include "lldb/Interpreter/OptionGroupPlatform.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000039#include "lldb/Interpreter/OptionGroupUInt64.h"
40#include "lldb/Interpreter/OptionGroupUUID.h"
Jason Molendac6127dd2014-11-21 02:25:15 +000041#include "lldb/Interpreter/OptionGroupString.h"
Greg Clayton644247c2011-07-07 01:59:51 +000042#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
Greg Clayton1f746072012-08-29 21:13:06 +000043#include "lldb/Symbol/CompileUnit.h"
Jason Molenda380241a2012-07-12 00:20:07 +000044#include "lldb/Symbol/FuncUnwinders.h"
Greg Claytoneffe5c92011-05-03 22:09:39 +000045#include "lldb/Symbol/LineTable.h"
46#include "lldb/Symbol/ObjectFile.h"
47#include "lldb/Symbol/SymbolFile.h"
48#include "lldb/Symbol/SymbolVendor.h"
Jason Molenda380241a2012-07-12 00:20:07 +000049#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton644247c2011-07-07 01:59:51 +000050#include "lldb/Symbol/VariableList.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000051#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000053#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000054#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055#include "lldb/Target/Thread.h"
Jim Ingham9575d842011-03-11 03:53:59 +000056#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58using namespace lldb;
59using namespace lldb_private;
60
Greg Clayton7260f622011-04-18 08:33:37 +000061static void
62DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm)
63{
Greg Clayton176761e2011-04-19 04:19:37 +000064 const ArchSpec &target_arch = target->GetArchitecture();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000065
Greg Claytonaa149cb2011-08-11 02:48:45 +000066 Module *exe_module = target->GetExecutableModulePointer();
Greg Clayton7260f622011-04-18 08:33:37 +000067 char exe_path[PATH_MAX];
68 bool exe_valid = false;
Greg Claytonaa149cb2011-08-11 02:48:45 +000069 if (exe_module)
70 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000071
Greg Clayton7260f622011-04-18 08:33:37 +000072 if (!exe_valid)
73 ::strcpy (exe_path, "<none>");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000074
Greg Clayton7260f622011-04-18 08:33:37 +000075 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000076
Greg Clayton7260f622011-04-18 08:33:37 +000077 uint32_t properties = 0;
78 if (target_arch.IsValid())
79 {
Todd Fiala7df337f2015-10-13 23:41:19 +000080 strm.Printf ("%sarch=", properties++ > 0 ? ", " : " ( ");
81 target_arch.DumpTriple (strm);
Greg Clayton7260f622011-04-18 08:33:37 +000082 properties++;
83 }
84 PlatformSP platform_sp (target->GetPlatform());
85 if (platform_sp)
Greg Clayton57abc5d2013-05-10 21:47:16 +000086 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName().GetCString());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000087
Greg Clayton7260f622011-04-18 08:33:37 +000088 ProcessSP process_sp (target->GetProcessSP());
89 bool show_process_status = false;
90 if (process_sp)
91 {
92 lldb::pid_t pid = process_sp->GetID();
93 StateType state = process_sp->GetState();
94 if (show_stopped_process_status)
Greg Clayton2637f822011-11-17 01:23:07 +000095 show_process_status = StateIsStoppedState(state, true);
Greg Clayton7260f622011-04-18 08:33:37 +000096 const char *state_cstr = StateAsCString (state);
97 if (pid != LLDB_INVALID_PROCESS_ID)
Daniel Malead01b2952012-11-29 21:49:15 +000098 strm.Printf ("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
Greg Clayton7260f622011-04-18 08:33:37 +000099 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
100 }
101 if (properties > 0)
102 strm.PutCString (" )\n");
103 else
104 strm.EOL();
105 if (show_process_status)
106 {
107 const bool only_threads_with_stop_reason = true;
108 const uint32_t start_frame = 0;
109 const uint32_t num_frames = 1;
110 const uint32_t num_frames_with_source = 1;
111 process_sp->GetStatus (strm);
112 process_sp->GetThreadStatus (strm,
113 only_threads_with_stop_reason,
114 start_frame,
115 num_frames,
116 num_frames_with_source);
117
118 }
119}
120
121static uint32_t
122DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm)
123{
124 const uint32_t num_targets = target_list.GetNumTargets();
125 if (num_targets)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000126 {
Greg Clayton7260f622011-04-18 08:33:37 +0000127 TargetSP selected_target_sp (target_list.GetSelectedTarget());
128 strm.PutCString ("Current targets:\n");
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000129 for (uint32_t i = 0; i < num_targets; ++i)
Greg Clayton7260f622011-04-18 08:33:37 +0000130 {
131 TargetSP target_sp (target_list.GetTargetAtIndex (i));
132 if (target_sp)
133 {
134 bool is_selected = target_sp.get() == selected_target_sp.get();
135 DumpTargetInfo (i,
136 target_sp.get(),
137 is_selected ? "* " : " ",
138 show_stopped_process_status,
139 strm);
140 }
141 }
142 }
143 return num_targets;
144}
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000145
Greg Clayton7260f622011-04-18 08:33:37 +0000146#pragma mark CommandObjectTargetCreate
147
148//-------------------------------------------------------------------------
149// "target create"
150//-------------------------------------------------------------------------
151
Jim Ingham5a988412012-06-08 21:56:10 +0000152class CommandObjectTargetCreate : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000153{
154public:
155 CommandObjectTargetCreate(CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000156 CommandObjectParsed(interpreter,
157 "target create",
158 "Create a target using the argument as the main executable.",
159 nullptr),
Greg Clayton7260f622011-04-18 08:33:37 +0000160 m_option_group (interpreter),
Greg Clayton644247c2011-07-07 01:59:51 +0000161 m_arch_option (),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000162 m_core_file (LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000163 m_platform_path (LLDB_OPT_SET_1, false, "platform-path", 'P', 0, eArgTypePath, "Path to the remote file to use for this target."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000164 m_symbol_file (LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug symbols file for when debug symbols are not in the executable."),
Jim Inghambf22b962013-07-11 01:47:46 +0000165 m_remote_file (LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename, "Fullpath to the file on the remote host if debugging remotely."),
Greg Clayton1c5f1862012-11-30 19:05:35 +0000166 m_add_dependents (LLDB_OPT_SET_1, false, "no-dependents", 'd', "Don't load dependent files when creating the target, just add the specified executable.", true, true)
Greg Clayton7260f622011-04-18 08:33:37 +0000167 {
168 CommandArgumentEntry arg;
169 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170
Greg Clayton7260f622011-04-18 08:33:37 +0000171 // Define the first (and only) variant of this arg.
172 file_arg.arg_type = eArgTypeFilename;
173 file_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000174
Greg Clayton7260f622011-04-18 08:33:37 +0000175 // There is only one variant this argument could be; put it into the argument entry.
176 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000177
Greg Clayton7260f622011-04-18 08:33:37 +0000178 // Push the data for the first argument into the m_arguments vector.
179 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000180
Greg Clayton644247c2011-07-07 01:59:51 +0000181 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000182 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000183 m_option_group.Append (&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000184 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Jim Inghambf22b962013-07-11 01:47:46 +0000185 m_option_group.Append (&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +0000186 m_option_group.Append (&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton7260f622011-04-18 08:33:37 +0000187 m_option_group.Finalize();
188 }
189
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000190 ~CommandObjectTargetCreate() override = default;
Greg Clayton7260f622011-04-18 08:33:37 +0000191
192 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000193 GetOptions () override
Greg Clayton7260f622011-04-18 08:33:37 +0000194 {
195 return &m_option_group;
196 }
197
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000198 int
Jim Ingham5a988412012-06-08 21:56:10 +0000199 HandleArgumentCompletion (Args &input,
200 int &cursor_index,
201 int &cursor_char_position,
202 OptionElementVector &opt_element_vector,
203 int match_start_point,
204 int max_return_elements,
205 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000206 StringList &matches) override
Jim Ingham5a988412012-06-08 21:56:10 +0000207 {
208 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
209 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000210
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000211 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
212 CommandCompletions::eDiskFileCompletion,
213 completion_str.c_str(),
214 match_start_point,
215 max_return_elements,
216 nullptr,
217 word_complete,
218 matches);
Jim Ingham5a988412012-06-08 21:56:10 +0000219 return matches.GetSize();
220 }
221
222protected:
Greg Clayton7260f622011-04-18 08:33:37 +0000223 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000224 DoExecute (Args& command, CommandReturnObject &result) override
Greg Clayton7260f622011-04-18 08:33:37 +0000225 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000226 const size_t argc = command.GetArgumentCount();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000227 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue());
Jim Inghambf22b962013-07-11 01:47:46 +0000228 FileSpec remote_file (m_remote_file.GetOptionValue().GetCurrentValue());
Greg Claytonc3776bf2012-02-09 06:16:32 +0000229
Greg Clayton5acc1252014-08-15 18:00:45 +0000230 if (core_file)
231 {
232 if (!core_file.Exists())
233 {
234 result.AppendErrorWithFormat("core file '%s' doesn't exist", core_file.GetPath().c_str());
235 result.SetStatus (eReturnStatusFailed);
236 return false;
237
238 }
239 if (!core_file.Readable())
240 {
241 result.AppendErrorWithFormat("core file '%s' is not readable", core_file.GetPath().c_str());
242 result.SetStatus (eReturnStatusFailed);
243 return false;
244 }
245 }
246
Daniel Maleae0f8f572013-08-26 23:57:52 +0000247 if (argc == 1 || core_file || remote_file)
Greg Clayton7260f622011-04-18 08:33:37 +0000248 {
Greg Clayton1c5f1862012-11-30 19:05:35 +0000249 FileSpec symfile (m_symbol_file.GetOptionValue().GetCurrentValue());
250 if (symfile)
251 {
Greg Clayton5acc1252014-08-15 18:00:45 +0000252 if (symfile.Exists())
253 {
254 if (!symfile.Readable())
255 {
Adrian McCarthyf7d18932015-11-20 23:09:11 +0000256 result.AppendErrorWithFormat("symbol file '%s' is not readable", symfile.GetPath().c_str());
Greg Clayton5acc1252014-08-15 18:00:45 +0000257 result.SetStatus (eReturnStatusFailed);
258 return false;
259 }
260 }
261 else
Greg Clayton1c5f1862012-11-30 19:05:35 +0000262 {
263 char symfile_path[PATH_MAX];
264 symfile.GetPath(symfile_path, sizeof(symfile_path));
265 result.AppendErrorWithFormat("invalid symbol file path '%s'", symfile_path);
266 result.SetStatus (eReturnStatusFailed);
267 return false;
268 }
269 }
270
Greg Clayton7260f622011-04-18 08:33:37 +0000271 const char *file_path = command.GetArgumentAtIndex(0);
272 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000273 FileSpec file_spec;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000274
Daniel Maleae0f8f572013-08-26 23:57:52 +0000275 if (file_path)
276 file_spec.SetFile (file_path, true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000277
Daniel Maleae0f8f572013-08-26 23:57:52 +0000278 bool must_set_platform_path = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279
Greg Clayton7260f622011-04-18 08:33:37 +0000280 Debugger &debugger = m_interpreter.GetDebugger();
Daniel Maleae0f8f572013-08-26 23:57:52 +0000281
282 TargetSP target_sp;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000283 const char *arch_cstr = m_arch_option.GetArchitectureName();
Greg Clayton1c5f1862012-11-30 19:05:35 +0000284 const bool get_dependent_files = m_add_dependents.GetOptionValue().GetCurrentValue();
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000285 Error error(debugger.GetTargetList().CreateTarget(debugger,
286 file_path,
287 arch_cstr,
288 get_dependent_files,
289 nullptr,
290 target_sp));
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000291
Greg Clayton7260f622011-04-18 08:33:37 +0000292 if (target_sp)
293 {
Greg Claytond26a1e52015-01-28 22:08:17 +0000294 // Only get the platform after we create the target because we might have
295 // switched platforms depending on what the arguments were to CreateTarget()
296 // we can't rely on the selected platform.
297
298 PlatformSP platform_sp = target_sp->GetPlatform();
299
300 if (remote_file)
301 {
302 if (platform_sp)
303 {
304 // I have a remote file.. two possible cases
305 if (file_spec && file_spec.Exists())
306 {
307 // if the remote file does not exist, push it there
308 if (!platform_sp->GetFileExists (remote_file))
309 {
310 Error err = platform_sp->PutFile(file_spec, remote_file);
311 if (err.Fail())
312 {
313 result.AppendError(err.AsCString());
314 result.SetStatus (eReturnStatusFailed);
315 return false;
316 }
317 }
318 }
319 else
320 {
321 // there is no local file and we need one
322 // in order to make the remote ---> local transfer we need a platform
323 // TODO: if the user has passed in a --platform argument, use it to fetch the right platform
324 if (!platform_sp)
325 {
326 result.AppendError("unable to perform remote debugging without a platform");
327 result.SetStatus (eReturnStatusFailed);
328 return false;
329 }
330 if (file_path)
331 {
332 // copy the remote file to the local file
333 Error err = platform_sp->GetFile(remote_file, file_spec);
334 if (err.Fail())
335 {
336 result.AppendError(err.AsCString());
337 result.SetStatus (eReturnStatusFailed);
338 return false;
339 }
340 }
341 else
342 {
343 // make up a local file
344 result.AppendError("remote --> local transfer without local path is not implemented yet");
345 result.SetStatus (eReturnStatusFailed);
346 return false;
347 }
348 }
349 }
350 else
351 {
352 result.AppendError("no platform found for target");
353 result.SetStatus (eReturnStatusFailed);
354 return false;
355 }
356 }
357
Jim Inghambf22b962013-07-11 01:47:46 +0000358 if (symfile || remote_file)
Greg Clayton1c5f1862012-11-30 19:05:35 +0000359 {
360 ModuleSP module_sp (target_sp->GetExecutableModule());
361 if (module_sp)
Jim Inghambf22b962013-07-11 01:47:46 +0000362 {
363 if (symfile)
364 module_sp->SetSymbolFileFileSpec(symfile);
365 if (remote_file)
366 {
367 std::string remote_path = remote_file.GetPath();
368 target_sp->SetArg0(remote_path.c_str());
369 module_sp->SetPlatformFileSpec(remote_file);
370 }
371 }
Greg Clayton1c5f1862012-11-30 19:05:35 +0000372 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000373
Greg Clayton7260f622011-04-18 08:33:37 +0000374 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000375 if (must_set_platform_path)
376 {
377 ModuleSpec main_module_spec(file_spec);
378 ModuleSP module_sp = target_sp->GetSharedModule(main_module_spec);
379 if (module_sp)
380 module_sp->SetPlatformFileSpec(remote_file);
381 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000382 if (core_file)
383 {
Greg Claytonc3776bf2012-02-09 06:16:32 +0000384 char core_path[PATH_MAX];
385 core_file.GetPath(core_path, sizeof(core_path));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000386 if (core_file.Exists())
Greg Claytonc3776bf2012-02-09 06:16:32 +0000387 {
Jason Molenda25c91012015-06-26 02:16:48 +0000388 if (!core_file.Readable())
389 {
390 result.AppendMessageWithFormat ("Core file '%s' is not readable.\n", core_path);
391 result.SetStatus (eReturnStatusFailed);
392 return false;
393 }
Greg Claytonc859e2d2012-02-13 23:10:39 +0000394 FileSpec core_file_dir;
395 core_file_dir.GetDirectory() = core_file.GetDirectory();
396 target_sp->GetExecutableSearchPaths ().Append (core_file_dir);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000397
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000398 ProcessSP process_sp(target_sp->CreateProcess(m_interpreter.GetDebugger().GetListener(), nullptr, &core_file));
Greg Claytonc859e2d2012-02-13 23:10:39 +0000399
400 if (process_sp)
Greg Claytonc3776bf2012-02-09 06:16:32 +0000401 {
Bruce Mitchener4d31a482015-10-07 10:11:10 +0000402 // Seems weird that we Launch a core file, but that is
Greg Claytonc859e2d2012-02-13 23:10:39 +0000403 // what we do!
404 error = process_sp->LoadCore();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000405
Greg Claytonc859e2d2012-02-13 23:10:39 +0000406 if (error.Fail())
407 {
408 result.AppendError(error.AsCString("can't find plug-in for core file"));
409 result.SetStatus (eReturnStatusFailed);
410 return false;
411 }
412 else
413 {
414 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName());
415 result.SetStatus (eReturnStatusSuccessFinishNoResult);
416 }
Greg Claytonc3776bf2012-02-09 06:16:32 +0000417 }
418 else
419 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000420 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path);
421 result.SetStatus (eReturnStatusFailed);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000422 }
423 }
424 else
425 {
Greg Claytonc859e2d2012-02-13 23:10:39 +0000426 result.AppendErrorWithFormat ("Core file '%s' does not exist\n", core_path);
Greg Claytonc3776bf2012-02-09 06:16:32 +0000427 result.SetStatus (eReturnStatusFailed);
428 }
429 }
430 else
431 {
432 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName());
433 result.SetStatus (eReturnStatusSuccessFinishNoResult);
434 }
Greg Clayton7260f622011-04-18 08:33:37 +0000435 }
436 else
437 {
438 result.AppendError(error.AsCString());
439 result.SetStatus (eReturnStatusFailed);
440 }
441 }
442 else
443 {
Jason Molenda65e06422015-01-20 03:06:17 +0000444 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 +0000445 result.SetStatus (eReturnStatusFailed);
446 }
447 return result.Succeeded();
Greg Clayton7260f622011-04-18 08:33:37 +0000448 }
449
Greg Clayton7260f622011-04-18 08:33:37 +0000450private:
451 OptionGroupOptions m_option_group;
Greg Clayton644247c2011-07-07 01:59:51 +0000452 OptionGroupArchitecture m_arch_option;
Greg Claytonc3776bf2012-02-09 06:16:32 +0000453 OptionGroupFile m_core_file;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000454 OptionGroupFile m_platform_path;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000455 OptionGroupFile m_symbol_file;
Jim Inghambf22b962013-07-11 01:47:46 +0000456 OptionGroupFile m_remote_file;
Greg Clayton1c5f1862012-11-30 19:05:35 +0000457 OptionGroupBoolean m_add_dependents;
Greg Clayton7260f622011-04-18 08:33:37 +0000458};
459
460#pragma mark CommandObjectTargetList
461
462//----------------------------------------------------------------------
463// "target list"
464//----------------------------------------------------------------------
465
Jim Ingham5a988412012-06-08 21:56:10 +0000466class CommandObjectTargetList : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000467{
468public:
469 CommandObjectTargetList (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000470 CommandObjectParsed(interpreter,
471 "target list",
472 "List all current targets in the current debug session.",
473 nullptr)
Greg Clayton7260f622011-04-18 08:33:37 +0000474 {
475 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000476
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000477 ~CommandObjectTargetList() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000478
Jim Ingham5a988412012-06-08 21:56:10 +0000479protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000480 bool
481 DoExecute (Args& args, CommandReturnObject &result) override
Greg Clayton7260f622011-04-18 08:33:37 +0000482 {
483 if (args.GetArgumentCount() == 0)
484 {
485 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000486
Greg Clayton7260f622011-04-18 08:33:37 +0000487 bool show_stopped_process_status = false;
488 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0)
489 {
490 strm.PutCString ("No targets.\n");
491 }
Johnny Chen1ee61a72011-04-18 21:08:05 +0000492 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000493 }
494 else
495 {
496 result.AppendError ("the 'target list' command takes no arguments\n");
497 result.SetStatus (eReturnStatusFailed);
498 }
499 return result.Succeeded();
500 }
501};
502
Greg Clayton7260f622011-04-18 08:33:37 +0000503#pragma mark CommandObjectTargetSelect
504
505//----------------------------------------------------------------------
506// "target select"
507//----------------------------------------------------------------------
508
Jim Ingham5a988412012-06-08 21:56:10 +0000509class CommandObjectTargetSelect : public CommandObjectParsed
Greg Clayton7260f622011-04-18 08:33:37 +0000510{
511public:
512 CommandObjectTargetSelect (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000513 CommandObjectParsed(interpreter,
514 "target select",
515 "Select a target as the current target by target index.",
516 nullptr)
Greg Clayton7260f622011-04-18 08:33:37 +0000517 {
518 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000519
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000520 ~CommandObjectTargetSelect() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000521
Jim Ingham5a988412012-06-08 21:56:10 +0000522protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000523 bool
524 DoExecute (Args& args, CommandReturnObject &result) override
Greg Clayton7260f622011-04-18 08:33:37 +0000525 {
526 if (args.GetArgumentCount() == 1)
527 {
528 bool success = false;
529 const char *target_idx_arg = args.GetArgumentAtIndex(0);
Vince Harron5275aaa2015-01-15 20:08:35 +0000530 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Greg Clayton7260f622011-04-18 08:33:37 +0000531 if (success)
532 {
533 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
534 const uint32_t num_targets = target_list.GetNumTargets();
535 if (target_idx < num_targets)
536 {
537 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx));
538 if (target_sp)
539 {
540 Stream &strm = result.GetOutputStream();
541 target_list.SetSelectedTarget (target_sp.get());
542 bool show_stopped_process_status = false;
543 DumpTargetList (target_list, show_stopped_process_status, strm);
Johnny Chen1ee61a72011-04-18 21:08:05 +0000544 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton7260f622011-04-18 08:33:37 +0000545 }
546 else
547 {
548 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx);
549 result.SetStatus (eReturnStatusFailed);
550 }
551 }
552 else
553 {
Todd Fiala352237d2014-08-29 20:14:21 +0000554 if (num_targets > 0)
555 {
556 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n",
557 target_idx,
558 num_targets - 1);
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000559 }
560 else
Todd Fiala352237d2014-08-29 20:14:21 +0000561 {
562 result.AppendErrorWithFormat ("index %u is out of range since there are no active targets\n",
563 target_idx);
564 }
Greg Clayton7260f622011-04-18 08:33:37 +0000565 result.SetStatus (eReturnStatusFailed);
566 }
567 }
568 else
569 {
570 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg);
571 result.SetStatus (eReturnStatusFailed);
572 }
573 }
574 else
575 {
576 result.AppendError ("'target select' takes a single argument: a target index\n");
577 result.SetStatus (eReturnStatusFailed);
578 }
579 return result.Succeeded();
580 }
581};
582
Greg Clayton3418c852011-08-10 02:10:13 +0000583#pragma mark CommandObjectTargetSelect
584
585//----------------------------------------------------------------------
586// "target delete"
587//----------------------------------------------------------------------
588
Jim Ingham5a988412012-06-08 21:56:10 +0000589class CommandObjectTargetDelete : public CommandObjectParsed
Greg Clayton3418c852011-08-10 02:10:13 +0000590{
591public:
592 CommandObjectTargetDelete (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000593 CommandObjectParsed(interpreter,
594 "target delete",
595 "Delete one or more targets by target index.",
596 nullptr),
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000597 m_option_group(interpreter),
598 m_all_option(LLDB_OPT_SET_1, false, "all", 'a', "Delete all targets.", false, true),
599 m_cleanup_option(
600 LLDB_OPT_SET_1,
601 false,
602 "clean", 'c',
603 "Perform extra cleanup to minimize memory consumption after deleting the target. "
604 "By default, LLDB will keep in memory any modules previously loaded by the target as well "
605 "as all of its debug info. Specifying --clean will unload all of these shared modules and "
606 "cause them to be reparsed again the next time the target is run",
607 false, true)
Greg Clayton3418c852011-08-10 02:10:13 +0000608 {
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000609 m_option_group.Append(&m_all_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
610 m_option_group.Append(&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Claytonaa149cb2011-08-11 02:48:45 +0000611 m_option_group.Finalize();
Greg Clayton3418c852011-08-10 02:10:13 +0000612 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000613
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000614 ~CommandObjectTargetDelete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000615
Jim Ingham5a988412012-06-08 21:56:10 +0000616 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000617 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +0000618 {
619 return &m_option_group;
620 }
621
622protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000623 bool
624 DoExecute (Args& args, CommandReturnObject &result) override
Greg Clayton3418c852011-08-10 02:10:13 +0000625 {
626 const size_t argc = args.GetArgumentCount();
627 std::vector<TargetSP> delete_target_list;
628 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList();
Greg Clayton3418c852011-08-10 02:10:13 +0000629 TargetSP target_sp;
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000630
631 if (m_all_option.GetOptionValue())
632 {
David Blaikiedab893c2015-03-27 20:19:14 +0000633 for (int i = 0; i < target_list.GetNumTargets(); ++i)
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000634 delete_target_list.push_back(target_list.GetTargetAtIndex(i));
635 }
636 else if (argc > 0)
Greg Clayton3418c852011-08-10 02:10:13 +0000637 {
638 const uint32_t num_targets = target_list.GetNumTargets();
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000639 // Bail out if don't have any targets.
640 if (num_targets == 0) {
641 result.AppendError("no targets to delete");
642 result.SetStatus(eReturnStatusFailed);
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000643 return false;
Filipe Cabecinhasf065fdc2012-07-09 13:02:17 +0000644 }
645
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000646 for (uint32_t arg_idx = 0; arg_idx < argc; ++arg_idx)
Greg Clayton3418c852011-08-10 02:10:13 +0000647 {
648 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx);
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000649 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +0000650 uint32_t target_idx = StringConvert::ToUInt32 (target_idx_arg, UINT32_MAX, 0, &success);
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000651 if (!success)
Greg Clayton3418c852011-08-10 02:10:13 +0000652 {
653 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg);
654 result.SetStatus (eReturnStatusFailed);
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000655 return false;
Greg Clayton3418c852011-08-10 02:10:13 +0000656 }
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000657 if (target_idx < num_targets)
658 {
659 target_sp = target_list.GetTargetAtIndex (target_idx);
660 if (target_sp)
661 {
662 delete_target_list.push_back (target_sp);
663 continue;
664 }
665 }
666 if (num_targets > 1)
667 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n",
668 target_idx,
669 num_targets - 1);
670 else
671 result.AppendErrorWithFormat("target index %u is out of range, the only valid index is 0\n",
672 target_idx);
673
674 result.SetStatus (eReturnStatusFailed);
675 return false;
Greg Clayton3418c852011-08-10 02:10:13 +0000676 }
Greg Clayton3418c852011-08-10 02:10:13 +0000677 }
678 else
679 {
680 target_sp = target_list.GetSelectedTarget();
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000681 if (!target_sp)
Greg Clayton3418c852011-08-10 02:10:13 +0000682 {
683 result.AppendErrorWithFormat("no target is currently selected\n");
684 result.SetStatus (eReturnStatusFailed);
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000685 return false;
Greg Clayton3418c852011-08-10 02:10:13 +0000686 }
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000687 delete_target_list.push_back (target_sp);
Greg Clayton3418c852011-08-10 02:10:13 +0000688 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000689
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000690 const size_t num_targets_to_delete = delete_target_list.size();
691 for (size_t idx = 0; idx < num_targets_to_delete; ++idx)
692 {
693 target_sp = delete_target_list[idx];
694 target_list.DeleteTarget(target_sp);
695 target_sp->Destroy();
696 }
697 // If "--clean" was specified, prune any orphaned shared modules from
698 // the global shared module list
699 if (m_cleanup_option.GetOptionValue ())
700 {
701 const bool mandatory = true;
702 ModuleList::RemoveOrphanSharedModules(mandatory);
703 }
704 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete);
705 result.SetStatus(eReturnStatusSuccessFinishResult);
706
707 return true;
Greg Clayton3418c852011-08-10 02:10:13 +0000708 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000709
Greg Claytonaa149cb2011-08-11 02:48:45 +0000710 OptionGroupOptions m_option_group;
Zachary Turneraa4dabf2015-03-26 16:43:13 +0000711 OptionGroupBoolean m_all_option;
Greg Claytonaa149cb2011-08-11 02:48:45 +0000712 OptionGroupBoolean m_cleanup_option;
Greg Clayton3418c852011-08-10 02:10:13 +0000713};
714
Greg Clayton644247c2011-07-07 01:59:51 +0000715#pragma mark CommandObjectTargetVariable
716
717//----------------------------------------------------------------------
718// "target variable"
719//----------------------------------------------------------------------
720
Jim Ingham5a988412012-06-08 21:56:10 +0000721class CommandObjectTargetVariable : public CommandObjectParsed
Greg Clayton644247c2011-07-07 01:59:51 +0000722{
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000723 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
724 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
725
Greg Clayton644247c2011-07-07 01:59:51 +0000726public:
727 CommandObjectTargetVariable (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000728 CommandObjectParsed(interpreter,
729 "target variable",
730 "Read global variable(s) prior to, or while running your binary.",
731 nullptr,
732 eCommandRequiresTarget),
Greg Clayton644247c2011-07-07 01:59:51 +0000733 m_option_group (interpreter),
Greg Clayton715c2362011-07-07 04:38:25 +0000734 m_option_variable (false), // Don't include frame options
Greg Clayton1deb7962011-10-25 06:44:01 +0000735 m_option_format (eFormatDefault),
Saleem Abdulrasool44edda02014-03-18 04:43:47 +0000736 m_option_compile_units (LLDB_OPT_SET_1, false, "file",
737 SHORT_OPTION_FILE, 0, eArgTypeFilename,
738 "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."),
739 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",
740 SHORT_OPTION_SHLB, 0, eArgTypeFilename,
741 "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 +0000742 m_varobj_options()
743 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000744 CommandArgumentEntry arg;
745 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000746
Johnny Chen81ab3f52011-08-22 22:22:00 +0000747 // Define the first (and only) variant of this arg.
748 var_name_arg.arg_type = eArgTypeVarName;
749 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000750
Johnny Chen81ab3f52011-08-22 22:22:00 +0000751 // There is only one variant this argument could be; put it into the argument entry.
752 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000753
Johnny Chen81ab3f52011-08-22 22:22:00 +0000754 // Push the data for the first argument into the m_arguments vector.
755 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000756
Greg Clayton644247c2011-07-07 01:59:51 +0000757 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000758 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000759 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 +0000760 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
761 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
762 m_option_group.Finalize();
763 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000764
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000765 ~CommandObjectTargetVariable() override = default;
Greg Clayton884fb692011-07-08 21:46:14 +0000766
767 void
768 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
769 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000770 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000771
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000772 if (!valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
773 valobj_sp->IsRuntimeSupportValue())
Enrico Granata560558e2015-02-11 02:35:39 +0000774 return;
775
Greg Clayton884fb692011-07-08 21:46:14 +0000776 switch (var_sp->GetScope())
777 {
778 case eValueTypeVariableGlobal:
779 if (m_option_variable.show_scope)
780 s.PutCString("GLOBAL: ");
781 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000782
Greg Clayton884fb692011-07-08 21:46:14 +0000783 case eValueTypeVariableStatic:
784 if (m_option_variable.show_scope)
785 s.PutCString("STATIC: ");
786 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000787
Greg Clayton884fb692011-07-08 21:46:14 +0000788 case eValueTypeVariableArgument:
789 if (m_option_variable.show_scope)
790 s.PutCString(" ARG: ");
791 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000792
Greg Clayton884fb692011-07-08 21:46:14 +0000793 case eValueTypeVariableLocal:
794 if (m_option_variable.show_scope)
795 s.PutCString(" LOCAL: ");
796 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000797
Greg Clayton884fb692011-07-08 21:46:14 +0000798 default:
799 break;
800 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000801
Greg Clayton45ba8542011-07-10 19:21:23 +0000802 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000803 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000804 bool show_fullpaths = false;
805 bool show_module = true;
806 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
807 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000808 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000809
Greg Clayton1deb7962011-10-25 06:44:01 +0000810 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000811 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000812 options.SetFormat(format);
813
814 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000815
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000816 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000817 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818
Greg Claytonc7bece562013-01-25 18:06:21 +0000819 static size_t GetVariableCallback (void *baton,
820 const char *name,
821 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000822 {
823 Target *target = static_cast<Target *>(baton);
824 if (target)
825 {
826 return target->GetImages().FindGlobalVariables (ConstString(name),
827 true,
828 UINT32_MAX,
829 variable_list);
830 }
831 return 0;
832 }
Greg Clayton884fb692011-07-08 21:46:14 +0000833
Jim Ingham5a988412012-06-08 21:56:10 +0000834 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000835 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +0000836 {
837 return &m_option_group;
838 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000839
Jim Ingham5a988412012-06-08 21:56:10 +0000840protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000841 void
842 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
843 {
844 size_t count = variable_list.GetSize();
845 if (count > 0)
846 {
847 if (sc.module_sp)
848 {
849 if (sc.comp_unit)
850 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000851 s.Printf ("Global variables for %s in %s:\n",
852 sc.comp_unit->GetPath().c_str(),
853 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000854 }
855 else
856 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000857 s.Printf ("Global variables for %s\n",
858 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000859 }
860 }
861 else if (sc.comp_unit)
862 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000863 s.Printf ("Global variables for %s\n",
864 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000865 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000866
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000867 for (uint32_t i = 0; i < count; ++i)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000868 {
869 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
870 if (var_sp)
871 {
872 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000873
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000874 if (valobj_sp)
875 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
876 }
877 }
878 }
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000879 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000880
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000881 bool
882 DoExecute (Args& args, CommandReturnObject &result) override
Greg Clayton644247c2011-07-07 01:59:51 +0000883 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000884 Target *target = m_exe_ctx.GetTargetPtr();
885 const size_t argc = args.GetArgumentCount();
886 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000887
Greg Claytonf9fc6092013-01-09 19:44:40 +0000888 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000889 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000890
891 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000892 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000893 VariableList variable_list;
894 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000895
Greg Claytonf9fc6092013-01-09 19:44:40 +0000896 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000897 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000898 bool use_var_name = false;
899 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000900 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000901 RegularExpression regex(arg);
902 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000903 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000904 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000905 result.SetStatus (eReturnStatusFailed);
906 return false;
907 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000908 use_var_name = true;
909 matches = target->GetImages().FindGlobalVariables (regex,
910 true,
911 UINT32_MAX,
912 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000913 }
914 else
915 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000916 Error error (Variable::GetValuesForVariableExpressionPath (arg,
917 m_exe_ctx.GetBestExecutionContextScope(),
918 GetVariableCallback,
919 target,
920 variable_list,
921 valobj_list));
922 matches = variable_list.GetSize();
923 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000924
Greg Claytonf9fc6092013-01-09 19:44:40 +0000925 if (matches == 0)
926 {
927 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
928 result.SetStatus (eReturnStatusFailed);
929 return false;
930 }
931 else
932 {
933 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000934 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000935 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
936 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000937 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000938 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
939 if (!valobj_sp)
940 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000941
Greg Claytonf9fc6092013-01-09 19:44:40 +0000942 if (valobj_sp)
943 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000944 }
945 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000946 }
Greg Clayton644247c2011-07-07 01:59:51 +0000947 }
948 }
949 else
950 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000951 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
952 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
953 SymbolContextList sc_list;
954 const size_t num_compile_units = compile_units.GetSize();
955 const size_t num_shlibs = shlibs.GetSize();
956 if (num_compile_units == 0 && num_shlibs == 0)
957 {
958 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000959 StackFrame *frame = m_exe_ctx.GetFramePtr();
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000960 CompileUnit *comp_unit = nullptr;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000961 if (frame)
962 {
963 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
964 if (sc.comp_unit)
965 {
966 const bool can_create = true;
967 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
968 if (comp_unit_varlist_sp)
969 {
970 size_t count = comp_unit_varlist_sp->GetSize();
971 if (count > 0)
972 {
973 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
974 success = true;
975 }
976 }
977 }
978 }
979 if (!success)
980 {
981 if (frame)
982 {
983 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000984 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
985 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000986 else
987 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000988 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000989 else
990 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
991 result.SetStatus (eReturnStatusFailed);
992 }
993 }
994 else
995 {
996 SymbolContextList sc_list;
997 const bool append = true;
998 // We have one or more compile unit or shlib
999 if (num_shlibs > 0)
1000 {
1001 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
1002 {
1003 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
1004 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001005
Greg Claytonf9fc6092013-01-09 19:44:40 +00001006 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
1007 if (module_sp)
1008 {
1009 if (num_compile_units > 0)
1010 {
1011 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1012 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1013 }
1014 else
1015 {
1016 SymbolContext sc;
1017 sc.module_sp = module_sp;
1018 sc_list.Append(sc);
1019 }
1020 }
1021 else
1022 {
1023 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001024 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1025 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001026 }
1027 }
1028 }
1029 else
1030 {
1031 // No shared libraries, we just want to find globals for the compile units files that were specified
1032 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1033 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1034 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001035
Greg Claytonf9fc6092013-01-09 19:44:40 +00001036 const uint32_t num_scs = sc_list.GetSize();
1037 if (num_scs > 0)
1038 {
1039 SymbolContext sc;
1040 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1041 {
1042 if (sc_list.GetContextAtIndex(sc_idx, sc))
1043 {
1044 if (sc.comp_unit)
1045 {
1046 const bool can_create = true;
1047 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1048 if (comp_unit_varlist_sp)
1049 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1050 }
1051 else if (sc.module_sp)
1052 {
1053 // Get all global variables for this module
1054 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1055 VariableList variable_list;
1056 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1057 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1058 }
1059 }
1060 }
1061 }
1062 }
Greg Clayton644247c2011-07-07 01:59:51 +00001063 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001064
Enrico Granata61a80ba2011-08-12 16:42:31 +00001065 if (m_interpreter.TruncationWarningNecessary())
1066 {
1067 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1068 m_cmd_name.c_str());
1069 m_interpreter.TruncationWarningGiven();
1070 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001071
Greg Clayton644247c2011-07-07 01:59:51 +00001072 return result.Succeeded();
1073 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001074
Greg Clayton644247c2011-07-07 01:59:51 +00001075 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001076 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001077 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001078 OptionGroupFileList m_option_compile_units;
1079 OptionGroupFileList m_option_shared_libraries;
1080 OptionGroupValueObjectDisplay m_varobj_options;
Greg Clayton644247c2011-07-07 01:59:51 +00001081};
1082
Greg Claytoneffe5c92011-05-03 22:09:39 +00001083#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084
Jim Ingham5a988412012-06-08 21:56:10 +00001085class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086{
1087public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001088 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001089 CommandObjectParsed(interpreter,
1090 "target modules search-paths add",
1091 "Add new image search paths substitution pairs to the current target.",
1092 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093 {
Caroline Tice405fe672010-10-04 22:28:36 +00001094 CommandArgumentEntry arg;
1095 CommandArgumentData old_prefix_arg;
1096 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001097
Caroline Tice405fe672010-10-04 22:28:36 +00001098 // Define the first variant of this arg pair.
1099 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1100 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001101
Caroline Tice405fe672010-10-04 22:28:36 +00001102 // Define the first variant of this arg pair.
1103 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1104 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001105
Caroline Tice405fe672010-10-04 22:28:36 +00001106 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1107 // must always occur together, they are treated as two variants of one argument rather than two independent
1108 // arguments. Push them both into the first argument position for m_arguments...
1109
1110 arg.push_back (old_prefix_arg);
1111 arg.push_back (new_prefix_arg);
1112
1113 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 }
1115
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001116 ~CommandObjectTargetModulesSearchPathsAdd() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117
Jim Ingham5a988412012-06-08 21:56:10 +00001118protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001120 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 {
Greg Claytona7015092010-09-18 01:14:36 +00001122 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 if (target)
1124 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001125 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126 if (argc & 1)
1127 {
Greg Clayton7260f622011-04-18 08:33:37 +00001128 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 result.SetStatus (eReturnStatusFailed);
1130 }
1131 else
1132 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001133 for (size_t i = 0; i < argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 {
1135 const char *from = command.GetArgumentAtIndex(i);
1136 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 if (from[0] && to[0])
1139 {
Jason Molenda71387652015-07-31 04:21:25 +00001140 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
1141 if (log)
1142 {
1143 log->Printf ("target modules search path adding ImageSearchPath pair: '%s' -> '%s'",
1144 from, to);
1145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001147 target->GetImageSearchPathList().Append (ConstString(from),
1148 ConstString(to),
1149 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001150 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 }
1152 else
1153 {
1154 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001155 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 else
Greg Clayton7260f622011-04-18 08:33:37 +00001157 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 result.SetStatus (eReturnStatusFailed);
1159 }
1160 }
1161 }
1162 }
1163 else
1164 {
Greg Clayton7260f622011-04-18 08:33:37 +00001165 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 result.SetStatus (eReturnStatusFailed);
1167 }
1168 return result.Succeeded();
1169 }
1170};
1171
Greg Claytoneffe5c92011-05-03 22:09:39 +00001172#pragma mark CommandObjectTargetModulesSearchPathsClear
1173
Jim Ingham5a988412012-06-08 21:56:10 +00001174class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175{
1176public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001177 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001178 CommandObjectParsed (interpreter,
1179 "target modules search-paths clear",
1180 "Clear all current image search path substitution pairs from the current target.",
1181 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 {
1183 }
1184
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001185 ~CommandObjectTargetModulesSearchPathsClear() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186
Jim Ingham5a988412012-06-08 21:56:10 +00001187protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001189 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190 {
Greg Claytona7015092010-09-18 01:14:36 +00001191 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 if (target)
1193 {
1194 bool notify = true;
1195 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001196 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 }
1198 else
1199 {
Greg Clayton7260f622011-04-18 08:33:37 +00001200 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 result.SetStatus (eReturnStatusFailed);
1202 }
1203 return result.Succeeded();
1204 }
1205};
1206
Greg Claytoneffe5c92011-05-03 22:09:39 +00001207#pragma mark CommandObjectTargetModulesSearchPathsInsert
1208
Jim Ingham5a988412012-06-08 21:56:10 +00001209class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210{
1211public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001212 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001213 CommandObjectParsed(interpreter,
1214 "target modules search-paths insert",
1215 "Insert a new image search path substitution pair into the current target at the specified index.",
1216 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 {
Caroline Tice405fe672010-10-04 22:28:36 +00001218 CommandArgumentEntry arg1;
1219 CommandArgumentEntry arg2;
1220 CommandArgumentData index_arg;
1221 CommandArgumentData old_prefix_arg;
1222 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001223
Caroline Tice405fe672010-10-04 22:28:36 +00001224 // Define the first and only variant of this arg.
1225 index_arg.arg_type = eArgTypeIndex;
1226 index_arg.arg_repetition = eArgRepeatPlain;
1227
1228 // Put the one and only variant into the first arg for m_arguments:
1229 arg1.push_back (index_arg);
1230
1231 // Define the first variant of this arg pair.
1232 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1233 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001234
Caroline Tice405fe672010-10-04 22:28:36 +00001235 // Define the first variant of this arg pair.
1236 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1237 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001238
Caroline Tice405fe672010-10-04 22:28:36 +00001239 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1240 // must always occur together, they are treated as two variants of one argument rather than two independent
1241 // arguments. Push them both into the same argument position for m_arguments...
1242
1243 arg2.push_back (old_prefix_arg);
1244 arg2.push_back (new_prefix_arg);
1245
1246 // Add arguments to m_arguments.
1247 m_arguments.push_back (arg1);
1248 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 }
1250
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001251 ~CommandObjectTargetModulesSearchPathsInsert() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252
Jim Ingham5a988412012-06-08 21:56:10 +00001253protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001255 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 {
Greg Claytona7015092010-09-18 01:14:36 +00001257 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 if (target)
1259 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001260 size_t argc = command.GetArgumentCount();
Bruce Mitchener4d31a482015-10-07 10:11:10 +00001261 // check for at least 3 arguments and an odd number of parameters
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 if (argc >= 3 && argc & 1)
1263 {
1264 bool success = false;
1265
Vince Harron5275aaa2015-01-15 20:08:35 +00001266 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267
1268 if (!success)
1269 {
1270 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1271 result.SetStatus (eReturnStatusFailed);
1272 return result.Succeeded();
1273 }
1274
1275 // shift off the index
1276 command.Shift();
1277 argc = command.GetArgumentCount();
1278
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001279 for (uint32_t i = 0; i < argc; i += 2, ++insert_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280 {
1281 const char *from = command.GetArgumentAtIndex(i);
1282 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001283
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284 if (from[0] && to[0])
1285 {
1286 bool last_pair = ((argc - i) == 2);
1287 target->GetImageSearchPathList().Insert (ConstString(from),
1288 ConstString(to),
1289 insert_idx,
1290 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001291 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292 }
1293 else
1294 {
1295 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001296 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 else
Greg Clayton7260f622011-04-18 08:33:37 +00001298 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299 result.SetStatus (eReturnStatusFailed);
1300 return false;
1301 }
1302 }
1303 }
1304 else
1305 {
Greg Clayton7260f622011-04-18 08:33:37 +00001306 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307 result.SetStatus (eReturnStatusFailed);
1308 return result.Succeeded();
1309 }
1310
1311 }
1312 else
1313 {
Greg Clayton7260f622011-04-18 08:33:37 +00001314 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 result.SetStatus (eReturnStatusFailed);
1316 }
1317 return result.Succeeded();
1318 }
1319};
1320
Greg Claytoneffe5c92011-05-03 22:09:39 +00001321#pragma mark CommandObjectTargetModulesSearchPathsList
1322
Jim Ingham5a988412012-06-08 21:56:10 +00001323class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324{
1325public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001326 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001327 CommandObjectParsed (interpreter,
1328 "target modules search-paths list",
1329 "List all current image search path substitution pairs in the current target.",
1330 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 {
1332 }
1333
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001334 ~CommandObjectTargetModulesSearchPathsList() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335
Jim Ingham5a988412012-06-08 21:56:10 +00001336protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001338 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339 {
Greg Claytona7015092010-09-18 01:14:36 +00001340 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341 if (target)
1342 {
1343 if (command.GetArgumentCount() != 0)
1344 {
Greg Clayton7260f622011-04-18 08:33:37 +00001345 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 result.SetStatus (eReturnStatusFailed);
1347 return result.Succeeded();
1348 }
1349
1350 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001351 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352 }
1353 else
1354 {
Greg Clayton7260f622011-04-18 08:33:37 +00001355 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 result.SetStatus (eReturnStatusFailed);
1357 }
1358 return result.Succeeded();
1359 }
1360};
1361
Greg Claytoneffe5c92011-05-03 22:09:39 +00001362#pragma mark CommandObjectTargetModulesSearchPathsQuery
1363
Jim Ingham5a988412012-06-08 21:56:10 +00001364class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365{
1366public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001367 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001368 CommandObjectParsed(interpreter,
1369 "target modules search-paths query",
1370 "Transform a path using the first applicable image search path.",
1371 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372 {
Caroline Tice405fe672010-10-04 22:28:36 +00001373 CommandArgumentEntry arg;
1374 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001375
Caroline Tice405fe672010-10-04 22:28:36 +00001376 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001377 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001378 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001379
Caroline Tice405fe672010-10-04 22:28:36 +00001380 // There is only one variant this argument could be; put it into the argument entry.
1381 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001382
Caroline Tice405fe672010-10-04 22:28:36 +00001383 // Push the data for the first argument into the m_arguments vector.
1384 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385 }
1386
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001387 ~CommandObjectTargetModulesSearchPathsQuery() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388
Jim Ingham5a988412012-06-08 21:56:10 +00001389protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001391 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 {
Greg Claytona7015092010-09-18 01:14:36 +00001393 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 if (target)
1395 {
1396 if (command.GetArgumentCount() != 1)
1397 {
Greg Clayton7260f622011-04-18 08:33:37 +00001398 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001399 result.SetStatus (eReturnStatusFailed);
1400 return result.Succeeded();
1401 }
1402
1403 ConstString orig(command.GetArgumentAtIndex(0));
1404 ConstString transformed;
1405 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1406 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1407 else
1408 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001409
1410 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 }
1412 else
1413 {
Greg Clayton7260f622011-04-18 08:33:37 +00001414 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001415 result.SetStatus (eReturnStatusFailed);
1416 }
1417 return result.Succeeded();
1418 }
1419};
1420
Greg Claytoneffe5c92011-05-03 22:09:39 +00001421//----------------------------------------------------------------------
1422// Static Helper functions
1423//----------------------------------------------------------------------
1424static void
1425DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1426{
1427 if (module)
1428 {
Todd Fiala7df337f2015-10-13 23:41:19 +00001429 StreamString arch_strm;
1430
Greg Claytoneffe5c92011-05-03 22:09:39 +00001431 if (full_triple)
Todd Fiala7df337f2015-10-13 23:41:19 +00001432 module->GetArchitecture().DumpTriple(arch_strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001433 else
Todd Fiala7df337f2015-10-13 23:41:19 +00001434 arch_strm.PutCString(module->GetArchitecture().GetArchitectureName());
1435 std::string arch_str = arch_strm.GetString();
1436
Greg Claytoneffe5c92011-05-03 22:09:39 +00001437 if (width)
Todd Fiala7df337f2015-10-13 23:41:19 +00001438 strm.Printf("%-*s", width, arch_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00001439 else
Todd Fiala7df337f2015-10-13 23:41:19 +00001440 strm.PutCString(arch_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00001441 }
1442}
1443
1444static void
1445DumpModuleUUID (Stream &strm, Module *module)
1446{
Jim Ingham28eb5712012-10-12 17:34:26 +00001447 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001448 module->GetUUID().Dump (&strm);
1449 else
1450 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001451}
1452
1453static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001454DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1455 Stream &strm,
1456 Module *module,
1457 const FileSpec &file_spec,
1458 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001459{
1460 uint32_t num_matches = 0;
1461 if (module)
1462 {
1463 SymbolContextList sc_list;
1464 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1465 0,
1466 false,
1467 eSymbolContextCompUnit,
1468 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001469
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001470 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001471 {
1472 SymbolContext sc;
1473 if (sc_list.GetContextAtIndex(i, sc))
1474 {
1475 if (i > 0)
1476 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001477
Greg Claytoneffe5c92011-05-03 22:09:39 +00001478 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1479 << module->GetFileSpec().GetFilename() << "\n";
1480 LineTable *line_table = sc.comp_unit->GetLineTable();
1481 if (line_table)
1482 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001483 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001484 lldb::eDescriptionLevelBrief);
1485 else
1486 strm << "No line table";
1487 }
1488 }
1489 }
1490 return num_matches;
1491}
1492
1493static void
1494DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1495{
1496 if (file_spec_ptr)
1497 {
1498 if (width > 0)
1499 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001500 std::string fullpath = file_spec_ptr->GetPath();
1501 strm.Printf("%-*s", width, fullpath.c_str());
1502 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001503 }
1504 else
1505 {
1506 file_spec_ptr->Dump(&strm);
1507 return;
1508 }
1509 }
1510 // Keep the width spacing correct if things go wrong...
1511 if (width > 0)
1512 strm.Printf("%-*s", width, "");
1513}
1514
1515static void
1516DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1517{
1518 if (file_spec_ptr)
1519 {
1520 if (width > 0)
1521 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1522 else
1523 file_spec_ptr->GetDirectory().Dump(&strm);
1524 return;
1525 }
1526 // Keep the width spacing correct if things go wrong...
1527 if (width > 0)
1528 strm.Printf("%-*s", width, "");
1529}
1530
1531static void
1532DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1533{
1534 if (file_spec_ptr)
1535 {
1536 if (width > 0)
1537 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1538 else
1539 file_spec_ptr->GetFilename().Dump(&strm);
1540 return;
1541 }
1542 // Keep the width spacing correct if things go wrong...
1543 if (width > 0)
1544 strm.Printf("%-*s", width, "");
1545}
1546
Adrian McCarthy543725c2016-04-04 21:21:49 +00001547static size_t
1548DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list)
1549{
1550 size_t num_dumped = 0;
1551 Mutex::Locker modules_locker(module_list.GetMutex());
1552 const size_t num_modules = module_list.GetSize();
1553 if (num_modules > 0)
1554 {
1555 strm.Printf("Dumping headers for %" PRIu64 " module(s).\n", static_cast<uint64_t>(num_modules));
1556 strm.IndentMore();
1557 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx)
1558 {
1559 Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
1560 if (module)
1561 {
1562 if (num_dumped++ > 0)
1563 {
1564 strm.EOL();
1565 strm.EOL();
1566 }
1567 ObjectFile *objfile = module->GetObjectFile();
1568 objfile->Dump(&strm);
1569 }
1570 }
1571 strm.IndentLess();
1572 }
1573 return num_dumped;
1574}
1575
Greg Claytoneffe5c92011-05-03 22:09:39 +00001576static void
1577DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1578{
1579 if (module)
1580 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001581 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1582 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001583 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001584 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001585 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001586 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001587 }
1588 }
1589}
1590
1591static void
1592DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1593{
1594 if (module)
1595 {
Greg Clayton3046e662013-07-10 01:23:25 +00001596 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001597 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001598 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001599 strm.Printf ("Sections for '%s' (%s):\n",
1600 module->GetSpecificationDescription().c_str(),
1601 module->GetArchitecture().GetArchitectureName());
1602 strm.IndentMore();
1603 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1604 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001605 }
1606 }
1607}
1608
1609static bool
1610DumpModuleSymbolVendor (Stream &strm, Module *module)
1611{
1612 if (module)
1613 {
1614 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1615 if (symbol_vendor)
1616 {
1617 symbol_vendor->Dump(&strm);
1618 return true;
1619 }
1620 }
1621 return false;
1622}
1623
Greg Claytonc4a8a762012-05-15 18:43:44 +00001624static void
1625DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1626{
1627 strm.IndentMore();
1628 strm.Indent (" Address: ");
1629 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1630 strm.PutCString (" (");
1631 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1632 strm.PutCString (")\n");
1633 strm.Indent (" Summary: ");
1634 const uint32_t save_indent = strm.GetIndentLevel ();
1635 strm.SetIndentLevel (save_indent + 13);
1636 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1637 strm.SetIndentLevel (save_indent);
1638 // Print out detailed address information when verbose is enabled
1639 if (verbose)
1640 {
1641 strm.EOL();
1642 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1643 }
1644 strm.IndentLess();
1645}
1646
Greg Claytoneffe5c92011-05-03 22:09:39 +00001647static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001648LookupAddressInModule (CommandInterpreter &interpreter,
1649 Stream &strm,
1650 Module *module,
1651 uint32_t resolve_mask,
1652 lldb::addr_t raw_addr,
1653 lldb::addr_t offset,
1654 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001655{
1656 if (module)
1657 {
1658 lldb::addr_t addr = raw_addr - offset;
1659 Address so_addr;
1660 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001661 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001662 if (target && !target->GetSectionLoadList().IsEmpty())
1663 {
1664 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1665 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001666 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001667 return false;
1668 }
1669 else
1670 {
1671 if (!module->ResolveFileAddress (addr, so_addr))
1672 return false;
1673 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001674
Greg Claytoneffe5c92011-05-03 22:09:39 +00001675 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001676 DumpAddress (exe_scope, so_addr, verbose, strm);
1677// strm.IndentMore();
1678// strm.Indent (" Address: ");
1679// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1680// strm.PutCString (" (");
1681// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1682// strm.PutCString (")\n");
1683// strm.Indent (" Summary: ");
1684// const uint32_t save_indent = strm.GetIndentLevel ();
1685// strm.SetIndentLevel (save_indent + 13);
1686// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1687// strm.SetIndentLevel (save_indent);
1688// // Print out detailed address information when verbose is enabled
1689// if (verbose)
1690// {
1691// strm.EOL();
1692// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1693// }
1694// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001695 return true;
1696 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001697
Greg Claytoneffe5c92011-05-03 22:09:39 +00001698 return false;
1699}
1700
1701static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001702LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001703{
1704 if (module)
1705 {
1706 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001707
Michael Sartaina7499c92013-07-01 19:45:50 +00001708 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1709 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001710 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001711 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001712 if (symtab)
1713 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001714 std::vector<uint32_t> match_indexes;
1715 ConstString symbol_name (name);
1716 uint32_t num_matches = 0;
1717 if (name_is_regex)
1718 {
1719 RegularExpression name_regexp(name);
1720 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1721 eSymbolTypeAny,
1722 match_indexes);
1723 }
1724 else
1725 {
1726 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1727 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001728
Greg Claytoneffe5c92011-05-03 22:09:39 +00001729 if (num_matches > 0)
1730 {
1731 strm.Indent ();
1732 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1733 name_is_regex ? "the regular expression " : "", name);
1734 DumpFullpath (strm, &module->GetFileSpec(), 0);
1735 strm.PutCString(":\n");
1736 strm.IndentMore ();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001737 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001738 {
1739 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton358cf1e2015-06-25 21:46:34 +00001740 if (symbol && symbol->ValueIsAddress())
1741 {
1742 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1743 symbol->GetAddressRef(),
1744 verbose,
1745 strm);
1746 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001747 }
1748 strm.IndentLess ();
1749 return num_matches;
1750 }
1751 }
1752 }
1753 }
1754 return 0;
1755}
1756
Greg Claytoneffe5c92011-05-03 22:09:39 +00001757static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001758DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001759{
1760 strm.IndentMore ();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001761
Greg Claytoneffe5c92011-05-03 22:09:39 +00001762 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001763
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001764 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001765 {
1766 SymbolContext sc;
1767 if (sc_list.GetContextAtIndex(i, sc))
1768 {
Sean Callananf6172c22012-02-11 00:24:04 +00001769 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001770
Sean Callananf6172c22012-02-11 00:24:04 +00001771 sc.GetAddressRange(eSymbolContextEverything,
1772 0,
1773 true,
1774 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001775
Greg Claytonc4a8a762012-05-15 18:43:44 +00001776 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001777 }
1778 }
1779 strm.IndentLess ();
1780}
1781
Greg Claytonc7bece562013-01-25 18:06:21 +00001782static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001783LookupFunctionInModule (CommandInterpreter &interpreter,
1784 Stream &strm,
1785 Module *module,
1786 const char *name,
1787 bool name_is_regex,
1788 bool include_inlines,
1789 bool include_symbols,
1790 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001791{
1792 if (module && name && name[0])
1793 {
1794 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001795 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001796 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001797 if (name_is_regex)
1798 {
1799 RegularExpression function_name_regex (name);
1800 num_matches = module->FindFunctions (function_name_regex,
1801 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001802 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001803 append,
1804 sc_list);
1805 }
1806 else
1807 {
1808 ConstString function_name (name);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001809 num_matches = module->FindFunctions(function_name,
1810 nullptr,
1811 eFunctionNameTypeAuto,
1812 include_symbols,
1813 include_inlines,
1814 append,
1815 sc_list);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001816 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001817
Greg Claytoneffe5c92011-05-03 22:09:39 +00001818 if (num_matches)
1819 {
1820 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001821 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001822 DumpFullpath (strm, &module->GetFileSpec(), 0);
1823 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001824 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001825 }
1826 return num_matches;
1827 }
1828 return 0;
1829}
1830
Greg Claytonc7bece562013-01-25 18:06:21 +00001831static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001832LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001833 Stream &strm,
1834 Module *module,
1835 const char *name_cstr,
1836 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001837{
1838 if (module && name_cstr && name_cstr[0])
1839 {
Greg Claytond1767f02011-12-08 02:13:16 +00001840 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001841 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001842 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001843 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001844 SymbolContext sc;
1845
1846 ConstString name(name_cstr);
Greg Claytonae088e52016-02-10 21:28:13 +00001847 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1848 num_matches = module->FindTypes(sc, name, name_is_fully_qualified, max_num_matches, searched_symbol_files, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001849
Greg Claytond1767f02011-12-08 02:13:16 +00001850 if (num_matches)
1851 {
1852 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001853 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001854 DumpFullpath (strm, &module->GetFileSpec(), 0);
1855 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001856 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001857 {
Greg Claytond1767f02011-12-08 02:13:16 +00001858 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001859 {
Greg Claytond1767f02011-12-08 02:13:16 +00001860 // Resolve the clang type so that any forward references
1861 // to types that haven't yet been parsed will get parsed.
Greg Clayton99558cc42015-08-24 23:46:31 +00001862 type_sp->GetFullCompilerType ();
Greg Claytond1767f02011-12-08 02:13:16 +00001863 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001864 // Print all typedef chains
1865 TypeSP typedef_type_sp (type_sp);
1866 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1867 while (typedefed_type_sp)
1868 {
1869 strm.EOL();
1870 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
Greg Clayton99558cc42015-08-24 23:46:31 +00001871 typedefed_type_sp->GetFullCompilerType ();
Greg Claytonaafa5c92012-05-15 19:26:12 +00001872 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1873 typedef_type_sp = typedefed_type_sp;
1874 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1875 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001876 }
Greg Claytond1767f02011-12-08 02:13:16 +00001877 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001878 }
Greg Claytond1767f02011-12-08 02:13:16 +00001879 }
1880 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001881 }
1882 return 0;
1883}
1884
Greg Claytonc7bece562013-01-25 18:06:21 +00001885static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001886LookupTypeHere (CommandInterpreter &interpreter,
1887 Stream &strm,
1888 const SymbolContext &sym_ctx,
1889 const char *name_cstr,
1890 bool name_is_regex)
1891{
1892 if (!sym_ctx.module_sp)
1893 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001894
Sean Callanand38b4a92012-06-06 20:49:55 +00001895 TypeList type_list;
1896 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001897 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001898 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001899
Sean Callanand38b4a92012-06-06 20:49:55 +00001900 ConstString name(name_cstr);
Greg Claytonae088e52016-02-10 21:28:13 +00001901 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1902 num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, searched_symbol_files, type_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001903
Sean Callanand38b4a92012-06-06 20:49:55 +00001904 if (num_matches)
1905 {
1906 strm.Indent ();
1907 strm.PutCString("Best match found in ");
1908 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1909 strm.PutCString(":\n");
Enrico Granatae5ffa082015-09-23 19:32:56 +00001910
Sean Callanand38b4a92012-06-06 20:49:55 +00001911 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1912 if (type_sp)
1913 {
1914 // Resolve the clang type so that any forward references
1915 // to types that haven't yet been parsed will get parsed.
Greg Clayton99558cc42015-08-24 23:46:31 +00001916 type_sp->GetFullCompilerType ();
Sean Callanand38b4a92012-06-06 20:49:55 +00001917 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1918 // Print all typedef chains
1919 TypeSP typedef_type_sp (type_sp);
1920 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1921 while (typedefed_type_sp)
1922 {
1923 strm.EOL();
1924 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
Greg Clayton99558cc42015-08-24 23:46:31 +00001925 typedefed_type_sp->GetFullCompilerType ();
Sean Callanand38b4a92012-06-06 20:49:55 +00001926 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1927 typedef_type_sp = typedefed_type_sp;
1928 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1929 }
1930 }
1931 strm.EOL();
1932 }
1933 return num_matches;
1934}
1935
1936static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001937LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001938 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001939 Module *module,
1940 const FileSpec &file_spec,
1941 uint32_t line,
1942 bool check_inlines,
1943 bool verbose)
1944{
1945 if (module && file_spec)
1946 {
1947 SymbolContextList sc_list;
1948 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1949 eSymbolContextEverything, sc_list);
1950 if (num_matches > 0)
1951 {
1952 strm.Indent ();
1953 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1954 strm << file_spec;
1955 if (line > 0)
1956 strm.Printf (":%u", line);
1957 strm << " in ";
1958 DumpFullpath (strm, &module->GetFileSpec(), 0);
1959 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001960 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001961 return num_matches;
1962 }
1963 }
1964 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001965}
1966
Greg Clayton8ee64382011-11-10 01:18:58 +00001967static size_t
1968FindModulesByName (Target *target,
1969 const char *module_name,
1970 ModuleList &module_list,
1971 bool check_global_list)
1972{
Greg Clayton8ee64382011-11-10 01:18:58 +00001973 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001974 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001975
Greg Clayton8ee64382011-11-10 01:18:58 +00001976 const size_t initial_size = module_list.GetSize ();
1977
Greg Claytonf3156262012-07-11 20:46:47 +00001978 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001979 {
1980 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00001981 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001982 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001983 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001984 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001985 {
1986 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001987
Greg Clayton8ee64382011-11-10 01:18:58 +00001988 if (module)
1989 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001990 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001991 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001992 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001993 module_list.AppendIfNeeded(module_sp);
1994 }
1995 }
1996 }
1997 }
Greg Claytonf3156262012-07-11 20:46:47 +00001998 else
1999 {
2000 if (target)
2001 {
2002 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002003
Greg Claytonf3156262012-07-11 20:46:47 +00002004 // Not found in our module list for our target, check the main
2005 // shared module list in case it is a extra file used somewhere
2006 // else
2007 if (num_matches == 0)
2008 {
2009 module_spec.GetArchitecture() = target->GetArchitecture();
2010 ModuleList::FindSharedModules (module_spec, module_list);
2011 }
2012 }
2013 else
2014 {
2015 ModuleList::FindSharedModules (module_spec,module_list);
2016 }
2017 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002018
Greg Clayton8ee64382011-11-10 01:18:58 +00002019 return module_list.GetSize () - initial_size;
2020}
2021
Greg Claytoneffe5c92011-05-03 22:09:39 +00002022#pragma mark CommandObjectTargetModulesModuleAutoComplete
2023
2024//----------------------------------------------------------------------
2025// A base command object class that can auto complete with module file
2026// paths
2027//----------------------------------------------------------------------
2028
Jim Ingham5a988412012-06-08 21:56:10 +00002029class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002030{
2031public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002032 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2033 const char *name,
2034 const char *help,
2035 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002036 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002037 {
2038 CommandArgumentEntry arg;
2039 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002040
Greg Claytoneffe5c92011-05-03 22:09:39 +00002041 // Define the first (and only) variant of this arg.
2042 file_arg.arg_type = eArgTypeFilename;
2043 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002044
Greg Claytoneffe5c92011-05-03 22:09:39 +00002045 // There is only one variant this argument could be; put it into the argument entry.
2046 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002047
Greg Claytoneffe5c92011-05-03 22:09:39 +00002048 // Push the data for the first argument into the m_arguments vector.
2049 m_arguments.push_back (arg);
2050 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002051
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002052 ~CommandObjectTargetModulesModuleAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002053
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002054 int
Greg Claytoneffe5c92011-05-03 22:09:39 +00002055 HandleArgumentCompletion (Args &input,
2056 int &cursor_index,
2057 int &cursor_char_position,
2058 OptionElementVector &opt_element_vector,
2059 int match_start_point,
2060 int max_return_elements,
2061 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002062 StringList &matches) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002063 {
2064 // Arguments are the standard module completer.
2065 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2066 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002067
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002068 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2069 CommandCompletions::eModuleCompletion,
2070 completion_str.c_str(),
2071 match_start_point,
2072 max_return_elements,
2073 nullptr,
2074 word_complete,
2075 matches);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002076 return matches.GetSize();
2077 }
2078};
2079
2080#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2081
2082//----------------------------------------------------------------------
2083// A base command object class that can auto complete with module source
2084// file paths
2085//----------------------------------------------------------------------
2086
Jim Ingham5a988412012-06-08 21:56:10 +00002087class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002088{
2089public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002090 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002091 const char *name,
2092 const char *help,
2093 const char *syntax,
2094 uint32_t flags) :
2095 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002096 {
2097 CommandArgumentEntry arg;
2098 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002099
Greg Claytoneffe5c92011-05-03 22:09:39 +00002100 // Define the first (and only) variant of this arg.
2101 source_file_arg.arg_type = eArgTypeSourceFile;
2102 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002103
Greg Claytoneffe5c92011-05-03 22:09:39 +00002104 // There is only one variant this argument could be; put it into the argument entry.
2105 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002106
Greg Claytoneffe5c92011-05-03 22:09:39 +00002107 // Push the data for the first argument into the m_arguments vector.
2108 m_arguments.push_back (arg);
2109 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002110
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002111 ~CommandObjectTargetModulesSourceFileAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002112
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002113 int
Greg Claytoneffe5c92011-05-03 22:09:39 +00002114 HandleArgumentCompletion (Args &input,
2115 int &cursor_index,
2116 int &cursor_char_position,
2117 OptionElementVector &opt_element_vector,
2118 int match_start_point,
2119 int max_return_elements,
2120 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002121 StringList &matches) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002122 {
2123 // Arguments are the standard source file completer.
2124 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2125 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002126
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002127 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2128 CommandCompletions::eSourceFileCompletion,
2129 completion_str.c_str(),
2130 match_start_point,
2131 max_return_elements,
2132 nullptr,
2133 word_complete,
2134 matches);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002135 return matches.GetSize();
2136 }
2137};
2138
Adrian McCarthy543725c2016-04-04 21:21:49 +00002139#pragma mark CommandObjectTargetModulesDumpObjfile
2140
2141class CommandObjectTargetModulesDumpObjfile : public CommandObjectTargetModulesModuleAutoComplete
2142{
2143public:
2144 CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter)
2145 : CommandObjectTargetModulesModuleAutoComplete(interpreter, "target modules dump objfile",
2146 "Dump the object file headers from one or more target modules.",
2147 nullptr)
2148 {
2149 }
2150
2151 ~CommandObjectTargetModulesDumpObjfile() override = default;
2152
2153protected:
2154 bool
2155 DoExecute(Args &command, CommandReturnObject &result) override
2156 {
2157 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2158 if (target == nullptr)
2159 {
2160 result.AppendError("invalid target, create a debug target using the 'target create' command");
2161 result.SetStatus(eReturnStatusFailed);
2162 return false;
2163 }
2164
2165 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2166 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2167 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2168
2169 size_t num_dumped = 0;
2170 if (command.GetArgumentCount() == 0)
2171 {
2172 // Dump all headers for all modules images
2173 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(), target->GetImages());
2174 if (num_dumped == 0)
2175 {
2176 result.AppendError("the target has no associated executable images");
2177 result.SetStatus(eReturnStatusFailed);
2178 }
2179 }
2180 else
2181 {
2182 // Find the modules that match the basename or full path.
2183 ModuleList module_list;
2184 const char *arg_cstr;
2185 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
2186 {
2187 size_t num_matched = FindModulesByName(target, arg_cstr, module_list, true);
2188 if (num_matched == 0)
2189 {
2190 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2191 }
2192 }
2193 // Dump all the modules we found.
2194 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(), module_list);
2195 }
2196
2197 if (num_dumped > 0)
2198 {
2199 result.SetStatus(eReturnStatusSuccessFinishResult);
2200 }
2201 else
2202 {
2203 result.AppendError("no matching executable images found");
2204 result.SetStatus(eReturnStatusFailed);
2205 }
2206 return result.Succeeded();
2207 }
2208};
2209
Greg Claytoneffe5c92011-05-03 22:09:39 +00002210#pragma mark CommandObjectTargetModulesDumpSymtab
2211
Greg Claytoneffe5c92011-05-03 22:09:39 +00002212class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2213{
2214public:
2215 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002216 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2217 "target modules dump symtab",
2218 "Dump the symbol table from one or more target modules.",
2219 nullptr),
2220 m_options(interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002221 {
2222 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002223
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002224 ~CommandObjectTargetModulesDumpSymtab() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002225
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002226 Options *
2227 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002228 {
2229 return &m_options;
2230 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002231
Jim Ingham5a988412012-06-08 21:56:10 +00002232 class CommandOptions : public Options
2233 {
2234 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002235 CommandOptions (CommandInterpreter &interpreter) :
2236 Options(interpreter),
2237 m_sort_order (eSortOrderNone)
2238 {
2239 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002240
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002241 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002242
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002243 Error
2244 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham5a988412012-06-08 21:56:10 +00002245 {
2246 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002247 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002248
Jim Ingham5a988412012-06-08 21:56:10 +00002249 switch (short_option)
2250 {
2251 case 's':
2252 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2253 g_option_table[option_idx].enum_values,
2254 eSortOrderNone,
2255 error);
2256 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002257
Jim Ingham5a988412012-06-08 21:56:10 +00002258 default:
2259 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2260 break;
Jim Ingham5a988412012-06-08 21:56:10 +00002261 }
2262 return error;
2263 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002264
Jim Ingham5a988412012-06-08 21:56:10 +00002265 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002266 OptionParsingStarting () override
Jim Ingham5a988412012-06-08 21:56:10 +00002267 {
2268 m_sort_order = eSortOrderNone;
2269 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002270
Jim Ingham5a988412012-06-08 21:56:10 +00002271 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002272 GetDefinitions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002273 {
2274 return g_option_table;
2275 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002276
Jim Ingham5a988412012-06-08 21:56:10 +00002277 // Options table: Required for subclasses of Options.
2278 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002279
Jim Ingham5a988412012-06-08 21:56:10 +00002280 SortOrder m_sort_order;
2281 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002282
Jim Ingham5a988412012-06-08 21:56:10 +00002283protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002284 bool
2285 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002286 {
2287 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002288 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002289 {
2290 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2291 result.SetStatus (eReturnStatusFailed);
2292 return false;
2293 }
2294 else
2295 {
2296 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002297
Greg Claytoneffe5c92011-05-03 22:09:39 +00002298 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2299 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2300 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002301
Greg Claytoneffe5c92011-05-03 22:09:39 +00002302 if (command.GetArgumentCount() == 0)
2303 {
2304 // Dump all sections for all modules images
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002305 Mutex::Locker modules_locker(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002306 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002307 if (num_modules > 0)
2308 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002309 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002310 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002311 {
2312 if (num_dumped > 0)
2313 {
2314 result.GetOutputStream().EOL();
2315 result.GetOutputStream().EOL();
2316 }
2317 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002318 DumpModuleSymtab (m_interpreter,
2319 result.GetOutputStream(),
2320 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2321 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002322 }
2323 }
2324 else
2325 {
2326 result.AppendError ("the target has no associated executable images");
2327 result.SetStatus (eReturnStatusFailed);
2328 return false;
2329 }
2330 }
2331 else
2332 {
2333 // Dump specified images (by basename or fullpath)
2334 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002335 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002336 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002337 ModuleList module_list;
2338 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2339 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002340 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002341 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002342 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002343 Module *module = module_list.GetModulePointerAtIndex(i);
2344 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002345 {
2346 if (num_dumped > 0)
2347 {
2348 result.GetOutputStream().EOL();
2349 result.GetOutputStream().EOL();
2350 }
2351 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002352 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002353 }
2354 }
2355 }
2356 else
2357 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2358 }
2359 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002360
Greg Claytoneffe5c92011-05-03 22:09:39 +00002361 if (num_dumped > 0)
2362 result.SetStatus (eReturnStatusSuccessFinishResult);
2363 else
2364 {
2365 result.AppendError ("no matching executable images found");
2366 result.SetStatus (eReturnStatusFailed);
2367 }
2368 }
2369 return result.Succeeded();
2370 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002371
Greg Claytoneffe5c92011-05-03 22:09:39 +00002372 CommandOptions m_options;
2373};
2374
2375static OptionEnumValueElement
2376g_sort_option_enumeration[4] =
2377{
2378 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2379 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2380 { eSortOrderByName, "name", "Sort output by symbol name."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002381 { 0, nullptr, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002382};
2383
Greg Claytoneffe5c92011-05-03 22:09:39 +00002384OptionDefinition
2385CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2386{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002387 { LLDB_OPT_SET_1, false, "sort", 's', OptionParser::eRequiredArgument, nullptr, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."},
2388 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002389};
2390
2391#pragma mark CommandObjectTargetModulesDumpSections
2392
2393//----------------------------------------------------------------------
2394// Image section dumping command
2395//----------------------------------------------------------------------
2396
2397class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2398{
2399public:
2400 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002401 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2402 "target modules dump sections",
2403 "Dump the sections from one or more target modules.",
2404 //"target modules dump sections [<file1> ...]")
2405 nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002406 {
2407 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002408
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002409 ~CommandObjectTargetModulesDumpSections() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002410
Jim Ingham5a988412012-06-08 21:56:10 +00002411protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002412 bool
2413 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002414 {
2415 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002416 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002417 {
2418 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2419 result.SetStatus (eReturnStatusFailed);
2420 return false;
2421 }
2422 else
2423 {
2424 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002425
Greg Claytoneffe5c92011-05-03 22:09:39 +00002426 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2427 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2428 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002429
Greg Claytoneffe5c92011-05-03 22:09:39 +00002430 if (command.GetArgumentCount() == 0)
2431 {
2432 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002433 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002434 if (num_modules > 0)
2435 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002436 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002437 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002438 {
2439 num_dumped++;
2440 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2441 }
2442 }
2443 else
2444 {
2445 result.AppendError ("the target has no associated executable images");
2446 result.SetStatus (eReturnStatusFailed);
2447 return false;
2448 }
2449 }
2450 else
2451 {
2452 // Dump specified images (by basename or fullpath)
2453 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002454 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002455 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002456 ModuleList module_list;
2457 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2458 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002459 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002460 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002461 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002462 Module *module = module_list.GetModulePointerAtIndex(i);
2463 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002464 {
2465 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002466 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002467 }
2468 }
2469 }
2470 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002471 {
2472 // Check the global list
Greg Claytonb26e6be2012-01-27 18:08:35 +00002473 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002474
Greg Claytoneffe5c92011-05-03 22:09:39 +00002475 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002476 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002477 }
2478 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002479
Greg Claytoneffe5c92011-05-03 22:09:39 +00002480 if (num_dumped > 0)
2481 result.SetStatus (eReturnStatusSuccessFinishResult);
2482 else
2483 {
2484 result.AppendError ("no matching executable images found");
2485 result.SetStatus (eReturnStatusFailed);
2486 }
2487 }
2488 return result.Succeeded();
2489 }
2490};
2491
Greg Claytoneffe5c92011-05-03 22:09:39 +00002492#pragma mark CommandObjectTargetModulesDumpSymfile
2493
2494//----------------------------------------------------------------------
2495// Image debug symbol dumping command
2496//----------------------------------------------------------------------
2497
2498class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2499{
2500public:
2501 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002502 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2503 "target modules dump symfile",
2504 "Dump the debug symbol file for one or more target modules.",
2505 //"target modules dump symfile [<file1> ...]")
2506 nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002507 {
2508 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002509
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002510 ~CommandObjectTargetModulesDumpSymfile() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002511
Jim Ingham5a988412012-06-08 21:56:10 +00002512protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002513 bool
2514 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002515 {
2516 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002517 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002518 {
2519 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2520 result.SetStatus (eReturnStatusFailed);
2521 return false;
2522 }
2523 else
2524 {
2525 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002526
Greg Claytoneffe5c92011-05-03 22:09:39 +00002527 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2528 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2529 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002530
Greg Claytoneffe5c92011-05-03 22:09:39 +00002531 if (command.GetArgumentCount() == 0)
2532 {
2533 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002534 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002535 Mutex::Locker modules_locker (target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002536 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002537 if (num_modules > 0)
2538 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002539 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002540 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2541 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002542 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002543 num_dumped++;
2544 }
2545 }
2546 else
2547 {
2548 result.AppendError ("the target has no associated executable images");
2549 result.SetStatus (eReturnStatusFailed);
2550 return false;
2551 }
2552 }
2553 else
2554 {
2555 // Dump specified images (by basename or fullpath)
2556 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002557 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002558 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002559 ModuleList module_list;
2560 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2561 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002562 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002563 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002564 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002565 Module *module = module_list.GetModulePointerAtIndex(i);
2566 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002567 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002568 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002569 num_dumped++;
2570 }
2571 }
2572 }
2573 else
2574 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2575 }
2576 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002577
Greg Claytoneffe5c92011-05-03 22:09:39 +00002578 if (num_dumped > 0)
2579 result.SetStatus (eReturnStatusSuccessFinishResult);
2580 else
2581 {
2582 result.AppendError ("no matching executable images found");
2583 result.SetStatus (eReturnStatusFailed);
2584 }
2585 }
2586 return result.Succeeded();
2587 }
2588};
2589
Greg Claytoneffe5c92011-05-03 22:09:39 +00002590#pragma mark CommandObjectTargetModulesDumpLineTable
2591
2592//----------------------------------------------------------------------
2593// Image debug line table dumping command
2594//----------------------------------------------------------------------
2595
2596class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2597{
2598public:
2599 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002600 CommandObjectTargetModulesSourceFileAutoComplete(interpreter,
2601 "target modules dump line-table",
2602 "Dump the line table for one or more compilation units.",
2603 nullptr,
2604 eCommandRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002605 {
2606 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002607
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002608 ~CommandObjectTargetModulesDumpLineTable() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002609
Jim Ingham5a988412012-06-08 21:56:10 +00002610protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002611 bool
2612 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002613 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002614 Target *target = m_exe_ctx.GetTargetPtr();
2615 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002616
Greg Claytonf9fc6092013-01-09 19:44:40 +00002617 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2618 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2619 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002620
Greg Claytonf9fc6092013-01-09 19:44:40 +00002621 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002622 {
Dawn Perchik954b40b2016-01-05 19:51:51 +00002623 result.AppendError ("file option must be specified.");
Greg Claytoneffe5c92011-05-03 22:09:39 +00002624 result.SetStatus (eReturnStatusFailed);
Dawn Perchik954b40b2016-01-05 19:51:51 +00002625 return result.Succeeded();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002626 }
2627 else
2628 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002629 // Dump specified images (by basename or fullpath)
2630 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002631 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002632 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002633 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002634
Greg Claytonf9fc6092013-01-09 19:44:40 +00002635 const ModuleList &target_modules = target->GetImages();
2636 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002637 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002638 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002639 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002640 uint32_t num_dumped = 0;
2641 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002642 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002643 if (DumpCompileUnitLineTable (m_interpreter,
2644 result.GetOutputStream(),
2645 target_modules.GetModulePointerAtIndexUnlocked(i),
2646 file_spec,
2647 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2648 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002649 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002650 if (num_dumped == 0)
2651 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2652 else
2653 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002654 }
2655 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002656 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002657
Greg Claytonf9fc6092013-01-09 19:44:40 +00002658 if (total_num_dumped > 0)
2659 result.SetStatus (eReturnStatusSuccessFinishResult);
2660 else
2661 {
2662 result.AppendError ("no source filenames matched any command arguments");
2663 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002664 }
2665 return result.Succeeded();
2666 }
2667};
2668
Greg Claytoneffe5c92011-05-03 22:09:39 +00002669#pragma mark CommandObjectTargetModulesDump
2670
2671//----------------------------------------------------------------------
2672// Dump multi-word command for target modules
2673//----------------------------------------------------------------------
2674
2675class CommandObjectTargetModulesDump : public CommandObjectMultiword
2676{
2677public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002678 //------------------------------------------------------------------
2679 // Constructors and Destructors
2680 //------------------------------------------------------------------
Adrian McCarthy543725c2016-04-04 21:21:49 +00002681 CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
2682 : CommandObjectMultiword(
2683 interpreter, "target modules dump",
2684 "A set of commands for dumping information about one or more target modules.",
2685 "target modules dump [headers|symtab|sections|symfile|line-table] [<file1> <file2> ...]")
Greg Claytoneffe5c92011-05-03 22:09:39 +00002686 {
Adrian McCarthy543725c2016-04-04 21:21:49 +00002687 LoadSubCommand("objfile", CommandObjectSP(new CommandObjectTargetModulesDumpObjfile(interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002688 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2689 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2690 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2691 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2692 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002693
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002694 ~CommandObjectTargetModulesDump() override = default;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002695};
2696
Jim Ingham5a988412012-06-08 21:56:10 +00002697class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002698{
2699public:
2700 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002701 CommandObjectParsed (interpreter,
2702 "target modules add",
2703 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002704 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002705 m_option_group (interpreter),
2706 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 +00002707 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002708 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002709 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002710 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002711 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002712
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002713 ~CommandObjectTargetModulesAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002714
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002715 Options *
2716 GetOptions () override
Greg Clayton50a24bd2012-11-29 22:16:27 +00002717 {
2718 return &m_option_group;
2719 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002720
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002721 int
Jim Ingham5a988412012-06-08 21:56:10 +00002722 HandleArgumentCompletion (Args &input,
2723 int &cursor_index,
2724 int &cursor_char_position,
2725 OptionElementVector &opt_element_vector,
2726 int match_start_point,
2727 int max_return_elements,
2728 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002729 StringList &matches) override
Jim Ingham5a988412012-06-08 21:56:10 +00002730 {
2731 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2732 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002733
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002734 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2735 CommandCompletions::eDiskFileCompletion,
2736 completion_str.c_str(),
2737 match_start_point,
2738 max_return_elements,
2739 nullptr,
2740 word_complete,
2741 matches);
Jim Ingham5a988412012-06-08 21:56:10 +00002742 return matches.GetSize();
2743 }
2744
2745protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002746 OptionGroupOptions m_option_group;
2747 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002748 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002749
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002750 bool
2751 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002752 {
2753 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002754 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002755 {
2756 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2757 result.SetStatus (eReturnStatusFailed);
2758 return false;
2759 }
2760 else
2761 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002762 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002763
Greg Claytoneffe5c92011-05-03 22:09:39 +00002764 const size_t argc = args.GetArgumentCount();
2765 if (argc == 0)
2766 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002767 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2768 {
2769 // We are given a UUID only, go locate the file
2770 ModuleSpec module_spec;
2771 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002772 if (m_symbol_file.GetOptionValue().OptionWasSet())
2773 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002774 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2775 {
2776 ModuleSP module_sp (target->GetSharedModule (module_spec));
2777 if (module_sp)
2778 {
2779 result.SetStatus (eReturnStatusSuccessFinishResult);
2780 return true;
2781 }
2782 else
2783 {
2784 StreamString strm;
2785 module_spec.GetUUID().Dump (&strm);
2786 if (module_spec.GetFileSpec())
2787 {
2788 if (module_spec.GetSymbolFileSpec())
2789 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002790 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 +00002791 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002792 module_spec.GetFileSpec().GetPath().c_str(),
2793 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002794 }
2795 else
2796 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002797 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002798 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002799 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002800 }
2801 }
2802 else
2803 {
2804 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2805 strm.GetString().c_str());
2806 }
2807 result.SetStatus (eReturnStatusFailed);
2808 return false;
2809 }
2810 }
2811 else
2812 {
2813 StreamString strm;
2814 module_spec.GetUUID().Dump (&strm);
2815 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2816 result.SetStatus (eReturnStatusFailed);
2817 return false;
2818 }
2819 }
2820 else
2821 {
2822 result.AppendError ("one or more executable image paths must be specified");
2823 result.SetStatus (eReturnStatusFailed);
2824 return false;
2825 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002826 }
2827 else
2828 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002829 for (size_t i = 0; i < argc; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002830 {
2831 const char *path = args.GetArgumentAtIndex(i);
2832 if (path)
2833 {
2834 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002835 if (file_spec.Exists())
2836 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002837 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002838 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2839 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002840 if (m_symbol_file.GetOptionValue().OptionWasSet())
2841 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002842 if (!module_spec.GetArchitecture().IsValid())
2843 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002844 Error error;
2845 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002846 if (!module_sp)
2847 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002848 const char *error_cstr = error.AsCString();
2849 if (error_cstr)
2850 result.AppendError (error_cstr);
2851 else
2852 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002853 result.SetStatus (eReturnStatusFailed);
2854 return false;
2855 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002856 else
2857 {
2858 flush = true;
2859 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002860 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002861 }
2862 else
2863 {
2864 char resolved_path[PATH_MAX];
2865 result.SetStatus (eReturnStatusFailed);
2866 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2867 {
2868 if (strcmp (resolved_path, path) != 0)
2869 {
2870 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2871 break;
2872 }
2873 }
2874 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2875 break;
2876 }
2877 }
2878 }
2879 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002880
Sean Callananb36c6c02012-12-13 01:39:39 +00002881 if (flush)
2882 {
2883 ProcessSP process = target->GetProcessSP();
2884 if (process)
2885 process->Flush();
2886 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002887 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002888
Greg Claytoneffe5c92011-05-03 22:09:39 +00002889 return result.Succeeded();
2890 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002891};
2892
2893class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2894{
2895public:
2896 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2897 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2898 "target modules load",
2899 "Set the load addresses for one or more sections in a target module.",
2900 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2901 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002902 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 +00002903 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)
2904 {
2905 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2906 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2907 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2908 m_option_group.Finalize();
2909 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002910
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002911 ~CommandObjectTargetModulesLoad() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002912
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002913 Options *
2914 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002915 {
2916 return &m_option_group;
2917 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002918
Jim Ingham5a988412012-06-08 21:56:10 +00002919protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002920 bool
2921 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002922 {
2923 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002924 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002925 {
2926 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2927 result.SetStatus (eReturnStatusFailed);
2928 return false;
2929 }
2930 else
2931 {
2932 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002933 ModuleSpec module_spec;
2934 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002935 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002936 {
2937 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002938 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2939 const bool use_global_module_list = true;
2940 ModuleList module_list;
2941 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2942 if (num_matches == 1)
2943 {
2944 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2945 }
2946 else if (num_matches > 1 )
2947 {
2948 search_using_module_spec = false;
2949 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2950 result.SetStatus (eReturnStatusFailed);
2951 }
2952 else
2953 {
2954 search_using_module_spec = false;
2955 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2956 result.SetStatus (eReturnStatusFailed);
2957 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002958 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002959
Greg Claytoneffe5c92011-05-03 22:09:39 +00002960 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002961 {
2962 search_using_module_spec = true;
2963 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2964 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002965
Greg Claytonb9a01b32012-02-26 05:51:37 +00002966 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002967 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002968 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002969 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002970
2971 char path[PATH_MAX];
2972 if (num_matches == 1)
2973 {
2974 Module *module = matching_modules.GetModulePointerAtIndex(0);
2975 if (module)
2976 {
2977 ObjectFile *objfile = module->GetObjectFile();
2978 if (objfile)
2979 {
Greg Clayton3046e662013-07-10 01:23:25 +00002980 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002981 if (section_list)
2982 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002983 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002984 if (argc == 0)
2985 {
2986 if (m_slide_option.GetOptionValue().OptionWasSet())
2987 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002988 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002989 const bool slide_is_offset = true;
2990 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002991 }
2992 else
2993 {
2994 result.AppendError ("one or more section name + load address pair must be specified");
2995 result.SetStatus (eReturnStatusFailed);
2996 return false;
2997 }
2998 }
2999 else
3000 {
3001 if (m_slide_option.GetOptionValue().OptionWasSet())
3002 {
3003 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
3004 result.SetStatus (eReturnStatusFailed);
3005 return false;
3006 }
3007
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003008 for (size_t i = 0; i < argc; i += 2)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003009 {
3010 const char *sect_name = args.GetArgumentAtIndex(i);
3011 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
3012 if (sect_name && load_addr_cstr)
3013 {
3014 ConstString const_sect_name(sect_name);
3015 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00003016 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003017 if (success)
3018 {
3019 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
3020 if (section_sp)
3021 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003022 if (section_sp->IsThreadSpecific())
3023 {
3024 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
3025 result.SetStatus (eReturnStatusFailed);
3026 break;
3027 }
3028 else
3029 {
Greg Clayton7820bd12012-07-07 01:24:12 +00003030 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00003031 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00003032 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00003033 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003034 }
3035 else
3036 {
3037 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
3038 result.SetStatus (eReturnStatusFailed);
3039 break;
3040 }
3041 }
3042 else
3043 {
3044 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
3045 result.SetStatus (eReturnStatusFailed);
3046 break;
3047 }
3048 }
3049 else
3050 {
3051 if (sect_name)
3052 result.AppendError ("section names must be followed by a load address.\n");
3053 else
3054 result.AppendError ("one or more section name + load address pair must be specified.\n");
3055 result.SetStatus (eReturnStatusFailed);
3056 break;
3057 }
3058 }
3059 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003060
Greg Clayton741f3f92012-03-27 21:10:07 +00003061 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003062 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003063 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003064 Process *process = m_exe_ctx.GetProcessPtr();
3065 if (process)
3066 process->Flush();
3067 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003068 }
3069 else
3070 {
3071 module->GetFileSpec().GetPath (path, sizeof(path));
3072 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3073 result.SetStatus (eReturnStatusFailed);
3074 }
3075 }
3076 else
3077 {
3078 module->GetFileSpec().GetPath (path, sizeof(path));
3079 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3080 result.SetStatus (eReturnStatusFailed);
3081 }
3082 }
3083 else
3084 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003085 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3086 if (module_spec_file)
3087 {
3088 module_spec_file->GetPath (path, sizeof(path));
3089 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3090 }
3091 else
3092 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003093 result.SetStatus (eReturnStatusFailed);
3094 }
3095 }
3096 else
3097 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003098 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003099
Greg Claytonb9a01b32012-02-26 05:51:37 +00003100 if (module_spec.GetFileSpec())
3101 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003102 else
3103 path[0] = '\0';
3104
Greg Claytonb9a01b32012-02-26 05:51:37 +00003105 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003106 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003107 if (num_matches > 1)
3108 {
3109 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3110 path[0] ? " file=" : "",
3111 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003112 !uuid_str.empty() ? " uuid=" : "",
3113 uuid_str.c_str());
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003114 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003115 {
3116 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3117 result.AppendMessageWithFormat("%s\n", path);
3118 }
3119 }
3120 else
3121 {
3122 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3123 path[0] ? " file=" : "",
3124 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003125 !uuid_str.empty() ? " uuid=" : "",
3126 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003127 }
3128 result.SetStatus (eReturnStatusFailed);
3129 }
3130 }
3131 else
3132 {
3133 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3134 result.SetStatus (eReturnStatusFailed);
3135 return false;
3136 }
3137 }
3138 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003139 }
3140
Greg Claytoneffe5c92011-05-03 22:09:39 +00003141 OptionGroupOptions m_option_group;
3142 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003143 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003144 OptionGroupUInt64 m_slide_option;
3145};
3146
3147//----------------------------------------------------------------------
3148// List images with associated information
3149//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003150class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003151{
3152public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003153 class CommandOptions : public Options
3154 {
3155 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003156 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003157 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003158 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003159 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003160 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003161 {
3162 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003163
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003164 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003165
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003166 Error
3167 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003168 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003169 Error error;
3170
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003171 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003172 if (short_option == 'g')
3173 {
3174 m_use_global_module_list = true;
3175 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003176 else if (short_option == 'a')
3177 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003178 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3179 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003180 }
Greg Clayton65a03992011-08-09 00:01:09 +00003181 else
3182 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003183 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003184 if (option_arg)
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003185 width = strtoul(option_arg, nullptr, 0);
Greg Clayton65a03992011-08-09 00:01:09 +00003186 m_format_array.push_back(std::make_pair(short_option, width));
3187 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003188 return error;
3189 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003190
Greg Claytoneffe5c92011-05-03 22:09:39 +00003191 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003192 OptionParsingStarting () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003193 {
3194 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003195 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003196 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003197 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003198
Greg Claytoneffe5c92011-05-03 22:09:39 +00003199 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003200 GetDefinitions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003201 {
3202 return g_option_table;
3203 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003204
Greg Claytoneffe5c92011-05-03 22:09:39 +00003205 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003206
Greg Claytoneffe5c92011-05-03 22:09:39 +00003207 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003208
Greg Claytoneffe5c92011-05-03 22:09:39 +00003209 // Instance variables to hold the values for command options.
3210 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3211 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003212 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003213 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003214 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003215
Greg Claytoneffe5c92011-05-03 22:09:39 +00003216 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003217 CommandObjectParsed (interpreter,
3218 "target modules list",
3219 "List current executable and dependent shared library images.",
3220 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003221 m_options (interpreter)
3222 {
3223 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003224
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003225 ~CommandObjectTargetModulesList() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003226
Greg Claytoneffe5c92011-05-03 22:09:39 +00003227 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003228 GetOptions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003229 {
3230 return &m_options;
3231 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003232
Jim Ingham5a988412012-06-08 21:56:10 +00003233protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003234 bool
3235 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003236 {
3237 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003238 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003239 // Define a local module list here to ensure it lives longer than any "locker"
3240 // object which might lock its contents below (through the "module_list_ptr"
3241 // variable).
3242 ModuleList module_list;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003243 if (target == nullptr && !use_global_module_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003244 {
3245 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3246 result.SetStatus (eReturnStatusFailed);
3247 return false;
3248 }
3249 else
3250 {
Greg Clayton3418c852011-08-10 02:10:13 +00003251 if (target)
3252 {
3253 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3254 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3255 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3256 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003257 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003258 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003259
Jim Inghamc10312c2011-10-24 18:36:33 +00003260 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3261 {
3262 if (target)
3263 {
3264 Address module_address;
3265 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3266 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003267 ModuleSP module_sp (module_address.GetModule());
3268 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003269 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003270 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003271 result.SetStatus (eReturnStatusSuccessFinishResult);
3272 }
3273 else
3274 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003275 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003276 result.SetStatus (eReturnStatusFailed);
3277 }
3278 }
3279 else
3280 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003281 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003282 result.SetStatus (eReturnStatusFailed);
3283 }
3284 }
3285 else
3286 {
3287 result.AppendError ("Can only look up modules by address with a valid target.");
3288 result.SetStatus (eReturnStatusFailed);
3289 }
3290 return result.Succeeded();
3291 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003292
Greg Claytonc7bece562013-01-25 18:06:21 +00003293 size_t num_modules = 0;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003294 Mutex::Locker locker; // This locker will be locked on the mutex in module_list_ptr if it is non-nullptr.
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003295 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3296 // the global module list directly.
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003297 const ModuleList *module_list_ptr = nullptr;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003298 const size_t argc = command.GetArgumentCount();
3299 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003300 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003301 if (use_global_module_list)
3302 {
3303 locker.Lock (Module::GetAllocationModuleCollectionMutex());
3304 num_modules = Module::GetNumberAllocatedModules();
3305 }
3306 else
3307 {
3308 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003309 }
Greg Clayton65a03992011-08-09 00:01:09 +00003310 }
3311 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003312 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003313 for (size_t i = 0; i < argc; ++i)
Greg Claytonc4a8a762012-05-15 18:43:44 +00003314 {
3315 // Dump specified images (by basename or fullpath)
3316 const char *arg_cstr = command.GetArgumentAtIndex(i);
3317 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3318 if (num_matches == 0)
3319 {
3320 if (argc == 1)
3321 {
3322 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3323 result.SetStatus (eReturnStatusFailed);
3324 return false;
3325 }
3326 }
3327 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003328
Greg Claytonc4a8a762012-05-15 18:43:44 +00003329 module_list_ptr = &module_list;
3330 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003331
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003332 if (module_list_ptr != nullptr)
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003333 {
3334 locker.Lock(module_list_ptr->GetMutex());
3335 num_modules = module_list_ptr->GetSize();
3336 }
Greg Clayton65a03992011-08-09 00:01:09 +00003337
Greg Claytoneffe5c92011-05-03 22:09:39 +00003338 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003339 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003340 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3341 {
Greg Clayton3418c852011-08-10 02:10:13 +00003342 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003343 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003344 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003345 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003346 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003347 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003348 }
3349 else
3350 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003351 module = Module::GetAllocatedModuleAtIndex(image_idx);
3352 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003353 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003354
Greg Claytonc7bece562013-01-25 18:06:21 +00003355 const size_t indent = strm.Printf("[%3u] ", image_idx);
3356 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003357
Greg Claytoneffe5c92011-05-03 22:09:39 +00003358 }
3359 result.SetStatus (eReturnStatusSuccessFinishResult);
3360 }
3361 else
3362 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003363 if (argc)
3364 {
3365 if (use_global_module_list)
3366 result.AppendError ("the global module list has no matching modules");
3367 else
3368 result.AppendError ("the target has no matching modules");
3369 }
Greg Clayton3418c852011-08-10 02:10:13 +00003370 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003371 {
3372 if (use_global_module_list)
3373 result.AppendError ("the global module list is empty");
3374 else
3375 result.AppendError ("the target has no associated executable images");
3376 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003377 result.SetStatus (eReturnStatusFailed);
3378 return false;
3379 }
3380 }
3381 return result.Succeeded();
3382 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003383
3384 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003385 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003386 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003387 if (module == nullptr)
Jim Ingham28eb5712012-10-12 17:34:26 +00003388 {
3389 strm.PutCString("Null module");
3390 return;
3391 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003392
Jim Inghamc10312c2011-10-24 18:36:33 +00003393 bool dump_object_name = false;
3394 if (m_options.m_format_array.empty())
3395 {
Greg Claytonc9660542012-02-05 02:38:54 +00003396 m_options.m_format_array.push_back(std::make_pair('u', 0));
3397 m_options.m_format_array.push_back(std::make_pair('h', 0));
3398 m_options.m_format_array.push_back(std::make_pair('f', 0));
3399 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003400 }
Greg Claytonc9660542012-02-05 02:38:54 +00003401 const size_t num_entries = m_options.m_format_array.size();
3402 bool print_space = false;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003403 for (size_t i = 0; i < num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003404 {
Greg Claytonc9660542012-02-05 02:38:54 +00003405 if (print_space)
3406 strm.PutChar(' ');
3407 print_space = true;
3408 const char format_char = m_options.m_format_array[i].first;
3409 uint32_t width = m_options.m_format_array[i].second;
3410 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003411 {
Greg Claytonc9660542012-02-05 02:38:54 +00003412 case 'A':
3413 DumpModuleArchitecture (strm, module, false, width);
3414 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003415
Greg Claytonc9660542012-02-05 02:38:54 +00003416 case 't':
3417 DumpModuleArchitecture (strm, module, true, width);
3418 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003419
Greg Claytonc9660542012-02-05 02:38:54 +00003420 case 'f':
3421 DumpFullpath (strm, &module->GetFileSpec(), width);
3422 dump_object_name = true;
3423 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003424
Greg Claytonc9660542012-02-05 02:38:54 +00003425 case 'd':
3426 DumpDirectory (strm, &module->GetFileSpec(), width);
3427 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003428
Greg Claytonc9660542012-02-05 02:38:54 +00003429 case 'b':
3430 DumpBasename (strm, &module->GetFileSpec(), width);
3431 dump_object_name = true;
3432 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003433
Greg Claytonc9660542012-02-05 02:38:54 +00003434 case 'h':
3435 case 'o':
3436 // Image header address
3437 {
3438 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003439
Greg Claytonc9660542012-02-05 02:38:54 +00003440 ObjectFile *objfile = module->GetObjectFile ();
3441 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003442 {
Greg Claytonc9660542012-02-05 02:38:54 +00003443 Address header_addr(objfile->GetHeaderAddress());
3444 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003445 {
Greg Claytonc9660542012-02-05 02:38:54 +00003446 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003447 {
Greg Claytonc9660542012-02-05 02:38:54 +00003448 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3449 if (header_load_addr == LLDB_INVALID_ADDRESS)
3450 {
3451 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3452 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003453 else
Greg Claytonc9660542012-02-05 02:38:54 +00003454 {
3455 if (format_char == 'o')
3456 {
3457 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003458 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003459 }
3460 else
3461 {
3462 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003463 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003464 }
3465 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003466 break;
3467 }
Greg Claytonc9660542012-02-05 02:38:54 +00003468 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3469 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3470 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003471 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003472 }
Greg Claytonc9660542012-02-05 02:38:54 +00003473 strm.Printf ("%*s", addr_nibble_width + 2, "");
3474 }
3475 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003476
Greg Claytonc9660542012-02-05 02:38:54 +00003477 case 'r':
3478 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003479 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003480 ModuleSP module_sp (module->shared_from_this());
3481 if (module_sp)
3482 {
3483 // Take one away to make sure we don't count our local "module_sp"
3484 ref_count = module_sp.use_count() - 1;
3485 }
3486 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003487 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003488 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003489 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003490 }
3491 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003492
Greg Claytonc9660542012-02-05 02:38:54 +00003493 case 's':
3494 case 'S':
3495 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003496 const SymbolVendor *symbol_vendor = module->GetSymbolVendor();
Greg Claytonc9660542012-02-05 02:38:54 +00003497 if (symbol_vendor)
3498 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003499 const FileSpec symfile_spec = symbol_vendor->GetMainFileSpec();
3500 if (format_char == 'S')
Greg Claytonc9660542012-02-05 02:38:54 +00003501 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003502 // Dump symbol file only if different from module file
3503 if (!symfile_spec || symfile_spec == module->GetFileSpec())
Greg Claytonc9660542012-02-05 02:38:54 +00003504 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003505 print_space = false;
3506 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003507 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003508 // Add a newline and indent past the index
3509 strm.Printf ("\n%*s", indent, "");
Greg Claytonc9660542012-02-05 02:38:54 +00003510 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003511 DumpFullpath (strm, &symfile_spec, width);
3512 dump_object_name = true;
3513 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003514 }
3515 strm.Printf("%.*s", width, "<NONE>");
3516 }
3517 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003518
Greg Claytonc9660542012-02-05 02:38:54 +00003519 case 'm':
3520 module->GetModificationTime().Dump(&strm, width);
3521 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003522
Greg Claytonc9660542012-02-05 02:38:54 +00003523 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003524 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003525 break;
3526
3527 case 'u':
3528 DumpModuleUUID(strm, module);
3529 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003530
Greg Claytonc9660542012-02-05 02:38:54 +00003531 default:
3532 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003533 }
Greg Claytonc9660542012-02-05 02:38:54 +00003534 }
3535 if (dump_object_name)
3536 {
3537 const char *object_name = module->GetObjectName().GetCString();
3538 if (object_name)
3539 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003540 }
3541 strm.EOL();
3542 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003543
Greg Claytoneffe5c92011-05-03 22:09:39 +00003544 CommandOptions m_options;
3545};
3546
3547OptionDefinition
3548CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3549{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003550 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3551 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the architecture when listing images."},
3552 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the triple when listing images."},
3553 { LLDB_OPT_SET_1, false, "header", 'h', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the image header address as a load address if debugging, a file address otherwise."},
3554 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the image header address offset from the header file address (the slide amount)."},
3555 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the UUID when listing images."},
3556 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3557 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3558 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3559 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3560 { LLDB_OPT_SET_1, false, "symfile-unique", 'S', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the symbol file with optional width only if it is different from the executable object file."},
3561 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3562 { LLDB_OPT_SET_1, false, "ref-count", 'r', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the reference count if the module is still in the shared module cache."},
3563 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the module pointer."},
3564 { LLDB_OPT_SET_1, false, "global", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the modules from the global module list, not just the current target."},
3565 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003566};
3567
Jason Molenda380241a2012-07-12 00:20:07 +00003568#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003569
Jason Molenda380241a2012-07-12 00:20:07 +00003570//----------------------------------------------------------------------
3571// Lookup unwind information in images
3572//----------------------------------------------------------------------
3573
3574class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3575{
3576public:
Jason Molenda380241a2012-07-12 00:20:07 +00003577 enum
3578 {
3579 eLookupTypeInvalid = -1,
3580 eLookupTypeAddress = 0,
3581 eLookupTypeSymbol,
3582 eLookupTypeFunction,
3583 eLookupTypeFunctionOrSymbol,
3584 kNumLookupTypes
3585 };
3586
3587 class CommandOptions : public Options
3588 {
3589 public:
Jason Molenda380241a2012-07-12 00:20:07 +00003590 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003591 Options(interpreter),
3592 m_type(eLookupTypeInvalid),
3593 m_str(),
3594 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003595 {
3596 }
3597
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003598 ~CommandOptions() override = default;
Jason Molenda380241a2012-07-12 00:20:07 +00003599
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003600 Error
3601 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jason Molenda380241a2012-07-12 00:20:07 +00003602 {
3603 Error error;
3604
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003605 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003606
3607 switch (short_option)
3608 {
3609 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003610 {
3611 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003612 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003613 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003614 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003615 if (m_addr == LLDB_INVALID_ADDRESS)
3616 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3617 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003618 }
Jason Molenda380241a2012-07-12 00:20:07 +00003619
3620 case 'n':
3621 m_str = option_arg;
3622 m_type = eLookupTypeFunctionOrSymbol;
3623 break;
Michael Sartainb1e15922013-08-22 20:42:30 +00003624
3625 default:
3626 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3627 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003628 }
3629
3630 return error;
3631 }
3632
3633 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003634 OptionParsingStarting () override
Jason Molenda380241a2012-07-12 00:20:07 +00003635 {
3636 m_type = eLookupTypeInvalid;
3637 m_str.clear();
3638 m_addr = LLDB_INVALID_ADDRESS;
3639 }
3640
3641 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003642 GetDefinitions () override
Jason Molenda380241a2012-07-12 00:20:07 +00003643 {
3644 return g_option_table;
3645 }
3646
3647 // Options table: Required for subclasses of Options.
3648
3649 static OptionDefinition g_option_table[];
3650
3651 // Instance variables to hold the values for command options.
3652
3653 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3654 std::string m_str; // Holds name lookup
3655 lldb::addr_t m_addr; // Holds the address to lookup
3656 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003657
Jason Molenda380241a2012-07-12 00:20:07 +00003658 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003659 CommandObjectParsed(interpreter,
3660 "target modules show-unwind",
3661 "Show synthesized unwind instructions for a function.",
3662 nullptr,
3663 eCommandRequiresTarget |
3664 eCommandRequiresProcess |
3665 eCommandProcessMustBeLaunched |
3666 eCommandProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003667 m_options (interpreter)
3668 {
3669 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003670
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003671 ~CommandObjectTargetModulesShowUnwind() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003672
Jason Molenda380241a2012-07-12 00:20:07 +00003673 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003674 GetOptions () override
Jason Molenda380241a2012-07-12 00:20:07 +00003675 {
3676 return &m_options;
3677 }
3678
3679protected:
3680 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003681 DoExecute (Args& command, CommandReturnObject &result) override
Jason Molenda380241a2012-07-12 00:20:07 +00003682 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003683 Target *target = m_exe_ctx.GetTargetPtr();
3684 Process *process = m_exe_ctx.GetProcessPtr();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003685 ABI *abi = nullptr;
Jason Molenda380241a2012-07-12 00:20:07 +00003686 if (process)
3687 abi = process->GetABI().get();
3688
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003689 if (process == nullptr)
Jason Molenda380241a2012-07-12 00:20:07 +00003690 {
3691 result.AppendError ("You must have a process running to use this command.");
3692 result.SetStatus (eReturnStatusFailed);
3693 return false;
3694 }
3695
3696 ThreadList threads(process->GetThreadList());
3697 if (threads.GetSize() == 0)
3698 {
3699 result.AppendError ("The process must be paused to use this command.");
3700 result.SetStatus (eReturnStatusFailed);
3701 return false;
3702 }
3703
3704 ThreadSP thread(threads.GetThreadAtIndex(0));
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003705 if (!thread)
Jason Molenda380241a2012-07-12 00:20:07 +00003706 {
3707 result.AppendError ("The process must be paused to use this command.");
3708 result.SetStatus (eReturnStatusFailed);
3709 return false;
3710 }
3711
Jason Molenda535ab862013-04-23 04:30:57 +00003712 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003713
Jason Molenda380241a2012-07-12 00:20:07 +00003714 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3715 {
Jason Molenda380241a2012-07-12 00:20:07 +00003716 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003717 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3718 }
3719 else if (m_options.m_type == eLookupTypeAddress && target)
3720 {
3721 Address addr;
3722 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003723 {
3724 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003725 ModuleSP module_sp (addr.GetModule());
3726 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3727 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003728 {
Jason Molenda535ab862013-04-23 04:30:57 +00003729 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003730 }
Jason Molenda535ab862013-04-23 04:30:57 +00003731 }
3732 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003733 else
3734 {
3735 result.AppendError ("address-expression or function name option must be specified.");
3736 result.SetStatus (eReturnStatusFailed);
3737 return false;
3738 }
Jason Molenda380241a2012-07-12 00:20:07 +00003739
Jason Molenda535ab862013-04-23 04:30:57 +00003740 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003741 if (num_matches == 0)
3742 {
3743 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3744 result.SetStatus (eReturnStatusFailed);
3745 return false;
3746 }
3747
Jason Molenda535ab862013-04-23 04:30:57 +00003748 for (uint32_t idx = 0; idx < num_matches; idx++)
3749 {
3750 SymbolContext sc;
3751 sc_list.GetContextAtIndex(idx, sc);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003752 if (sc.symbol == nullptr && sc.function == nullptr)
Jason Molenda535ab862013-04-23 04:30:57 +00003753 continue;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003754 if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
Jason Molenda535ab862013-04-23 04:30:57 +00003755 continue;
3756 AddressRange range;
3757 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3758 continue;
3759 if (!range.GetBaseAddress().IsValid())
3760 continue;
3761 ConstString funcname(sc.GetFunctionName());
3762 if (funcname.IsEmpty())
3763 continue;
3764 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3765 if (abi)
3766 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003767
Jason Molenda535ab862013-04-23 04:30:57 +00003768 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003769 if (!func_unwinders_sp)
Jason Molenda535ab862013-04-23 04:30:57 +00003770 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003771
Jason Molenda34549b82015-01-13 06:04:04 +00003772 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 +00003773
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003774 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread, -1);
3775 if (non_callsite_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003776 {
Jason Molenda34549b82015-01-13 06:04:04 +00003777 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 +00003778 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003779 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003780 if (callsite_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003781 {
Jason Molenda34549b82015-01-13 06:04:04 +00003782 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003783 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003784 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread);
3785 if (fast_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003786 {
Jason Molenda34549b82015-01-13 06:04:04 +00003787 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003788 }
3789
Jason Molenda34549b82015-01-13 06:04:04 +00003790 result.GetOutputStream().Printf("\n");
3791
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003792 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread, 0);
Jason Molenda34549b82015-01-13 06:04:04 +00003793 if (assembly_sp)
3794 {
3795 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3796 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3797 result.GetOutputStream().Printf("\n");
3798 }
Jason Molenda34549b82015-01-13 06:04:04 +00003799
3800 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3801 if (ehframe_sp)
3802 {
3803 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3804 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3805 result.GetOutputStream().Printf("\n");
3806 }
3807
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003808 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread, 0);
Jason Molenda34549b82015-01-13 06:04:04 +00003809 if (ehframe_augmented_sp)
3810 {
3811 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3812 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3813 result.GetOutputStream().Printf("\n");
3814 }
3815
Tamas Berghammer648f3c72015-09-30 13:50:14 +00003816 UnwindPlanSP arm_unwind_sp = func_unwinders_sp->GetArmUnwindUnwindPlan(*target, 0);
3817 if (arm_unwind_sp)
3818 {
3819 result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n");
3820 arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3821 result.GetOutputStream().Printf("\n");
3822 }
3823
Jason Molenda34549b82015-01-13 06:04:04 +00003824 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3825 if (compact_unwind_sp)
3826 {
3827 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3828 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3829 result.GetOutputStream().Printf("\n");
3830 }
3831
3832 if (fast_unwind_plan)
3833 {
3834 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3835 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3836 result.GetOutputStream().Printf("\n");
3837 }
3838
3839 ABISP abi_sp = process->GetABI();
3840 if (abi_sp)
3841 {
3842 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3843 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3844 {
3845 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3846 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3847 result.GetOutputStream().Printf("\n");
3848 }
3849
3850 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3851 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3852 {
3853 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3854 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3855 result.GetOutputStream().Printf("\n");
3856 }
3857 }
Jason Molenda535ab862013-04-23 04:30:57 +00003858
3859 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003860 }
3861 return result.Succeeded();
3862 }
3863
3864 CommandOptions m_options;
3865};
3866
3867OptionDefinition
3868CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3869{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003870 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3871 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3872 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jason Molenda380241a2012-07-12 00:20:07 +00003873};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003874
3875//----------------------------------------------------------------------
3876// Lookup information in images
3877//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003878class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003879{
3880public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003881 enum
3882 {
3883 eLookupTypeInvalid = -1,
3884 eLookupTypeAddress = 0,
3885 eLookupTypeSymbol,
3886 eLookupTypeFileLine, // Line is optional
3887 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003888 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003889 eLookupTypeType,
3890 kNumLookupTypes
3891 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003892
Greg Claytoneffe5c92011-05-03 22:09:39 +00003893 class CommandOptions : public Options
3894 {
3895 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003896 CommandOptions (CommandInterpreter &interpreter) :
3897 Options(interpreter)
3898 {
3899 OptionParsingStarting();
3900 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003901
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003902 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003903
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003904 Error
3905 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003906 {
3907 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003908
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003909 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003910
Greg Claytoneffe5c92011-05-03 22:09:39 +00003911 switch (short_option)
3912 {
3913 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003914 {
3915 m_type = eLookupTypeAddress;
3916 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3917 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3918 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003919 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003920
Greg Claytoneffe5c92011-05-03 22:09:39 +00003921 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003922 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003923 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003924 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003925 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003926
Greg Claytoneffe5c92011-05-03 22:09:39 +00003927 case 's':
3928 m_str = option_arg;
3929 m_type = eLookupTypeSymbol;
3930 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003931
Greg Claytoneffe5c92011-05-03 22:09:39 +00003932 case 'f':
3933 m_file.SetFile (option_arg, false);
3934 m_type = eLookupTypeFileLine;
3935 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003936
Greg Claytoneffe5c92011-05-03 22:09:39 +00003937 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003938 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003939 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003940
Greg Claytoneffe5c92011-05-03 22:09:39 +00003941 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003942 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003943 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003944 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003945 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003946 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003947 m_type = eLookupTypeFileLine;
3948 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003949
Greg Claytonc4a8a762012-05-15 18:43:44 +00003950 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003951 m_str = option_arg;
3952 m_type = eLookupTypeFunction;
3953 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003954
Greg Claytonc4a8a762012-05-15 18:43:44 +00003955 case 'n':
3956 m_str = option_arg;
3957 m_type = eLookupTypeFunctionOrSymbol;
3958 break;
3959
Greg Claytoneffe5c92011-05-03 22:09:39 +00003960 case 't':
3961 m_str = option_arg;
3962 m_type = eLookupTypeType;
3963 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003964
Greg Claytoneffe5c92011-05-03 22:09:39 +00003965 case 'v':
3966 m_verbose = 1;
3967 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003968
Sean Callanand38b4a92012-06-06 20:49:55 +00003969 case 'A':
3970 m_print_all = true;
3971 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003972
Greg Claytoneffe5c92011-05-03 22:09:39 +00003973 case 'r':
3974 m_use_regex = true;
3975 break;
3976 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003977
Greg Claytoneffe5c92011-05-03 22:09:39 +00003978 return error;
3979 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003980
Greg Claytoneffe5c92011-05-03 22:09:39 +00003981 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003982 OptionParsingStarting () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003983 {
3984 m_type = eLookupTypeInvalid;
3985 m_str.clear();
3986 m_file.Clear();
3987 m_addr = LLDB_INVALID_ADDRESS;
3988 m_offset = 0;
3989 m_line_number = 0;
3990 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003991 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003992 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00003993 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003994 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003995
Greg Claytoneffe5c92011-05-03 22:09:39 +00003996 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003997 GetDefinitions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003998 {
3999 return g_option_table;
4000 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004001
Greg Claytoneffe5c92011-05-03 22:09:39 +00004002 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004003
Greg Claytoneffe5c92011-05-03 22:09:39 +00004004 static OptionDefinition g_option_table[];
4005 int m_type; // Should be a eLookupTypeXXX enum after parsing options
4006 std::string m_str; // Holds name lookup
4007 FileSpec m_file; // Files for file lookups
4008 lldb::addr_t m_addr; // Holds the address to lookup
4009 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
4010 uint32_t m_line_number; // Line number for file+line lookups
4011 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00004012 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00004013 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00004014 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00004015 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004016
Greg Claytoneffe5c92011-05-03 22:09:39 +00004017 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004018 CommandObjectParsed(interpreter,
4019 "target modules lookup",
4020 "Look up information within executable and dependent shared library images.",
4021 nullptr,
4022 eCommandRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00004023 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004024 {
4025 CommandArgumentEntry arg;
4026 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004027
Greg Claytoneffe5c92011-05-03 22:09:39 +00004028 // Define the first (and only) variant of this arg.
4029 file_arg.arg_type = eArgTypeFilename;
4030 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004031
Greg Claytoneffe5c92011-05-03 22:09:39 +00004032 // There is only one variant this argument could be; put it into the argument entry.
4033 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004034
Greg Claytoneffe5c92011-05-03 22:09:39 +00004035 // Push the data for the first argument into the m_arguments vector.
4036 m_arguments.push_back (arg);
4037 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004038
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004039 ~CommandObjectTargetModulesLookup() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004040
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004041 Options *
4042 GetOptions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00004043 {
4044 return &m_options;
4045 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004046
Sean Callanand38b4a92012-06-06 20:49:55 +00004047 bool
4048 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4049 {
4050 switch (m_options.m_type)
4051 {
4052 case eLookupTypeAddress:
4053 case eLookupTypeFileLine:
4054 case eLookupTypeFunction:
4055 case eLookupTypeFunctionOrSymbol:
4056 case eLookupTypeSymbol:
4057 default:
4058 return false;
4059 case eLookupTypeType:
4060 break;
4061 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004062
Jason Molendab57e4a12013-11-04 09:33:30 +00004063 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004064
Sean Callanand38b4a92012-06-06 20:49:55 +00004065 if (!frame)
4066 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004067
Sean Callanand38b4a92012-06-06 20:49:55 +00004068 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004069
Sean Callanand38b4a92012-06-06 20:49:55 +00004070 if (!sym_ctx.module_sp)
4071 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004072
Sean Callanand38b4a92012-06-06 20:49:55 +00004073 switch (m_options.m_type)
4074 {
4075 default:
4076 return false;
4077 case eLookupTypeType:
4078 if (!m_options.m_str.empty())
4079 {
4080 if (LookupTypeHere (m_interpreter,
4081 result.GetOutputStream(),
4082 sym_ctx,
4083 m_options.m_str.c_str(),
4084 m_options.m_use_regex))
4085 {
4086 result.SetStatus(eReturnStatusSuccessFinishResult);
4087 return true;
4088 }
4089 }
4090 break;
4091 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004092
Sean Callanand38b4a92012-06-06 20:49:55 +00004093 return true;
4094 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004095
Greg Claytoneffe5c92011-05-03 22:09:39 +00004096 bool
4097 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4098 {
4099 switch (m_options.m_type)
4100 {
4101 case eLookupTypeAddress:
4102 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4103 {
Saleem Abdulrasoolc98d9692016-01-22 20:26:34 +00004104 if (LookupAddressInModule (m_interpreter,
4105 result.GetOutputStream(),
4106 module,
4107 eSymbolContextEverything | (m_options.m_verbose ? static_cast<int>(eSymbolContextVariable) : 0),
4108 m_options.m_addr,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004109 m_options.m_offset,
4110 m_options.m_verbose))
4111 {
4112 result.SetStatus(eReturnStatusSuccessFinishResult);
4113 return true;
4114 }
4115 }
4116 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004117
Greg Claytoneffe5c92011-05-03 22:09:39 +00004118 case eLookupTypeSymbol:
4119 if (!m_options.m_str.empty())
4120 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004121 if (LookupSymbolInModule (m_interpreter,
4122 result.GetOutputStream(),
4123 module,
4124 m_options.m_str.c_str(),
4125 m_options.m_use_regex,
4126 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004127 {
4128 result.SetStatus(eReturnStatusSuccessFinishResult);
4129 return true;
4130 }
4131 }
4132 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004133
Greg Claytoneffe5c92011-05-03 22:09:39 +00004134 case eLookupTypeFileLine:
4135 if (m_options.m_file)
4136 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004137 if (LookupFileAndLineInModule (m_interpreter,
4138 result.GetOutputStream(),
4139 module,
4140 m_options.m_file,
4141 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004142 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004143 m_options.m_verbose))
4144 {
4145 result.SetStatus(eReturnStatusSuccessFinishResult);
4146 return true;
4147 }
4148 }
4149 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004150
4151 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004152 case eLookupTypeFunction:
4153 if (!m_options.m_str.empty())
4154 {
4155 if (LookupFunctionInModule (m_interpreter,
4156 result.GetOutputStream(),
4157 module,
4158 m_options.m_str.c_str(),
4159 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004160 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004161 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004162 m_options.m_verbose))
4163 {
4164 result.SetStatus(eReturnStatusSuccessFinishResult);
4165 return true;
4166 }
4167 }
4168 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004169
Greg Claytoneffe5c92011-05-03 22:09:39 +00004170 case eLookupTypeType:
4171 if (!m_options.m_str.empty())
4172 {
4173 if (LookupTypeInModule (m_interpreter,
4174 result.GetOutputStream(),
4175 module,
4176 m_options.m_str.c_str(),
4177 m_options.m_use_regex))
4178 {
4179 result.SetStatus(eReturnStatusSuccessFinishResult);
4180 return true;
4181 }
4182 }
4183 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004184
Greg Claytoneffe5c92011-05-03 22:09:39 +00004185 default:
4186 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4187 syntax_error = true;
4188 break;
4189 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004190
Greg Claytoneffe5c92011-05-03 22:09:39 +00004191 result.SetStatus (eReturnStatusFailed);
4192 return false;
4193 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004194
Jim Ingham5a988412012-06-08 21:56:10 +00004195protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004196 bool
4197 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00004198 {
4199 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004200 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004201 {
4202 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4203 result.SetStatus (eReturnStatusFailed);
4204 return false;
4205 }
4206 else
4207 {
4208 bool syntax_error = false;
4209 uint32_t i;
4210 uint32_t num_successful_lookups = 0;
4211 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4212 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4213 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4214 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004215
Greg Claytoneffe5c92011-05-03 22:09:39 +00004216 if (command.GetArgumentCount() == 0)
4217 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004218 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004219
Sean Callanand38b4a92012-06-06 20:49:55 +00004220 // Where it is possible to look in the current symbol context
4221 // first, try that. If this search was successful and --all
4222 // was not passed, don't print anything else.
4223 if (LookupHere (m_interpreter, result, syntax_error))
4224 {
4225 result.GetOutputStream().EOL();
4226 num_successful_lookups++;
4227 if (!m_options.m_print_all)
4228 {
4229 result.SetStatus (eReturnStatusSuccessFinishResult);
4230 return result.Succeeded();
4231 }
4232 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004233
Sean Callanand38b4a92012-06-06 20:49:55 +00004234 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004235
Enrico Granata17598482012-11-08 02:22:02 +00004236 const ModuleList &target_modules = target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00004237 Mutex::Locker modules_locker(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004238 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004239 if (num_modules > 0)
4240 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004241 for (i = 0; i < num_modules && !syntax_error; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004242 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004243 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004244
Sean Callanand38b4a92012-06-06 20:49:55 +00004245 if (module_pointer != current_module.get() &&
4246 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004247 {
4248 result.GetOutputStream().EOL();
4249 num_successful_lookups++;
4250 }
4251 }
4252 }
4253 else
4254 {
4255 result.AppendError ("the target has no associated executable images");
4256 result.SetStatus (eReturnStatusFailed);
4257 return false;
4258 }
4259 }
4260 else
4261 {
4262 // Dump specified images (by basename or fullpath)
4263 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004264 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr && !syntax_error; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004265 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004266 ModuleList module_list;
4267 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4268 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004269 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004270 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004271 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004272 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004273 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004274 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004275 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004276 {
4277 result.GetOutputStream().EOL();
4278 num_successful_lookups++;
4279 }
4280 }
4281 }
4282 }
4283 else
4284 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4285 }
4286 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004287
Greg Claytoneffe5c92011-05-03 22:09:39 +00004288 if (num_successful_lookups > 0)
4289 result.SetStatus (eReturnStatusSuccessFinishResult);
4290 else
4291 result.SetStatus (eReturnStatusFailed);
4292 }
4293 return result.Succeeded();
4294 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004295
Greg Claytoneffe5c92011-05-03 22:09:39 +00004296 CommandOptions m_options;
4297};
4298
4299OptionDefinition
4300CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4301{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004302 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4303 { LLDB_OPT_SET_1, false, "offset", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
Greg Claytonc4a8a762012-05-15 18:43:44 +00004304 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4305 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_6 */ ,
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004306 false, "regex", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4307 { LLDB_OPT_SET_2, true, "symbol", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more target modules."},
4308 { LLDB_OPT_SET_3, true, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more target modules."},
4309 { LLDB_OPT_SET_3, false, "line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
Jim Inghama8f55662012-06-04 22:47:34 +00004310 { LLDB_OPT_SET_FROM_TO(3,5),
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004311 false, "no-inlines", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4312 { LLDB_OPT_SET_4, true, "function", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."},
4313 { LLDB_OPT_SET_5, true, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionOrSymbol, "Lookup a function or symbol by name in one or more target modules."},
4314 { LLDB_OPT_SET_6, true, "type", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."},
4315 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose lookup information."},
4316 { LLDB_OPT_SET_ALL, false, "all", 'A', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Print all matches, not just the best match, if a best match is available."},
4317 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004318};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004319
Jim Ingham9575d842011-03-11 03:53:59 +00004320#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004321
4322//-------------------------------------------------------------------------
4323// CommandObjectMultiwordImageSearchPaths
4324//-------------------------------------------------------------------------
4325
Greg Claytoneffe5c92011-05-03 22:09:39 +00004326class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004327{
4328public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004329 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) :
4330 CommandObjectMultiword (interpreter,
4331 "target modules search-paths",
4332 "A set of commands for operating on debugger target image search paths.",
4333 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004334 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004335 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4336 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4337 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4338 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4339 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004340 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004341
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004342 ~CommandObjectTargetModulesImageSearchPaths() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004343};
4344
Greg Claytoneffe5c92011-05-03 22:09:39 +00004345#pragma mark CommandObjectTargetModules
4346
4347//-------------------------------------------------------------------------
4348// CommandObjectTargetModules
4349//-------------------------------------------------------------------------
4350
4351class CommandObjectTargetModules : public CommandObjectMultiword
4352{
4353public:
4354 //------------------------------------------------------------------
4355 // Constructors and Destructors
4356 //------------------------------------------------------------------
4357 CommandObjectTargetModules(CommandInterpreter &interpreter) :
4358 CommandObjectMultiword (interpreter,
4359 "target modules",
4360 "A set of commands for accessing information for one or more target modules.",
4361 "target modules <sub-command> ...")
4362 {
4363 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4364 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004365 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4366 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4367 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4368 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004369 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004370
4371 }
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004372
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004373 ~CommandObjectTargetModules() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004374
Greg Claytoneffe5c92011-05-03 22:09:39 +00004375private:
4376 //------------------------------------------------------------------
4377 // For CommandObjectTargetModules only
4378 //------------------------------------------------------------------
4379 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4380};
4381
Jim Ingham5a988412012-06-08 21:56:10 +00004382class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004383{
4384public:
4385 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004386 CommandObjectParsed (interpreter,
4387 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004388 "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.",
Enrico Granatae87764f2015-05-27 05:04:35 +00004389 "target symbols add [<symfile>]", eCommandRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004390 m_option_group (interpreter),
4391 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4392 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4393
Greg Claytone72dfb32012-02-24 01:59:29 +00004394 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004395 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4396 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4397 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4398 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004399 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004400
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004401 ~CommandObjectTargetSymbolsAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004402
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004403 int
Jim Ingham5a988412012-06-08 21:56:10 +00004404 HandleArgumentCompletion (Args &input,
4405 int &cursor_index,
4406 int &cursor_char_position,
4407 OptionElementVector &opt_element_vector,
4408 int match_start_point,
4409 int max_return_elements,
4410 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004411 StringList &matches) override
Jim Ingham5a988412012-06-08 21:56:10 +00004412 {
4413 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4414 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004415
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004416 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
4417 CommandCompletions::eDiskFileCompletion,
4418 completion_str.c_str(),
4419 match_start_point,
4420 max_return_elements,
4421 nullptr,
4422 word_complete,
4423 matches);
Jim Ingham5a988412012-06-08 21:56:10 +00004424 return matches.GetSize();
4425 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004426
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004427 Options *
4428 GetOptions () override
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004429 {
4430 return &m_option_group;
4431 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004432
Jim Ingham5a988412012-06-08 21:56:10 +00004433protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004434 bool
4435 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004436 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004437 bool &flush,
4438 CommandReturnObject &result)
4439 {
Greg Clayton89deb062012-12-12 01:15:30 +00004440 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4441 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004442 {
4443 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004444 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004445
Greg Clayton89deb062012-12-12 01:15:30 +00004446 if (!module_spec.GetUUID().IsValid())
4447 {
4448 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4449 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4450 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004451 // We now have a module that represents a symbol file
4452 // that can be used for a module that might exist in the
4453 // current target, so we need to find that module in the
4454 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004455 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004456
Greg Clayton9aae0a12013-05-15 19:52:08 +00004457 size_t num_matches = 0;
4458 // First extract all module specs from the symbol file
4459 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004460 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004461 {
4462 // Now extract the module spec that matches the target architecture
4463 ModuleSpec target_arch_module_spec;
4464 ModuleSpec symfile_module_spec;
4465 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4466 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4467 {
4468 // See if it has a UUID?
4469 if (symfile_module_spec.GetUUID().IsValid())
4470 {
4471 // It has a UUID, look for this UUID in the target modules
4472 ModuleSpec symfile_uuid_module_spec;
4473 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4474 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4475 }
4476 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004477
Greg Clayton9aae0a12013-05-15 19:52:08 +00004478 if (num_matches == 0)
4479 {
4480 // No matches yet, iterate through the module specs to find a UUID value that
4481 // we can match up to an image in our target
4482 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004483 for (size_t i = 0; i < num_symfile_module_specs && num_matches == 0; ++i)
Greg Clayton9aae0a12013-05-15 19:52:08 +00004484 {
4485 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4486 {
4487 if (symfile_module_spec.GetUUID().IsValid())
4488 {
4489 // It has a UUID, look for this UUID in the target modules
4490 ModuleSpec symfile_uuid_module_spec;
4491 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4492 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004493 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004494 }
4495 }
4496 }
4497 }
4498
4499 // Just try to match up the file by basename if we have no matches at this point
4500 if (num_matches == 0)
4501 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004502
Greg Clayton91c0e742013-01-11 23:44:27 +00004503 while (num_matches == 0)
4504 {
4505 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4506 // Empty string returned, lets bail
4507 if (!filename_no_extension)
4508 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004509
Greg Clayton91c0e742013-01-11 23:44:27 +00004510 // Check if there was no extension to strip and the basename is the same
4511 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4512 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004513
Greg Clayton91c0e742013-01-11 23:44:27 +00004514 // Replace basename with one less extension
4515 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004516
Greg Clayton91c0e742013-01-11 23:44:27 +00004517 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4518 }
4519
Greg Clayton89deb062012-12-12 01:15:30 +00004520 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004521 {
Greg Clayton89deb062012-12-12 01:15:30 +00004522 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4523 }
4524 else if (num_matches == 1)
4525 {
4526 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004527
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004528 // The module has not yet created its symbol vendor, we can just
4529 // give the existing target module the symfile path to use for
4530 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004531 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004532
Greg Clayton136dff82012-12-14 02:15:00 +00004533 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004534 if (symbol_vendor)
4535 {
4536 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004537
Greg Clayton89deb062012-12-12 01:15:30 +00004538 if (symbol_file)
4539 {
4540 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004541
Greg Clayton89deb062012-12-12 01:15:30 +00004542 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4543 {
4544 // Provide feedback that the symfile has been successfully added.
4545 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004546 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004547 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004548 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004549
Greg Clayton89deb062012-12-12 01:15:30 +00004550 // Let clients know something changed in the module
4551 // if it is currently loaded
4552 ModuleList module_list;
4553 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004554 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004555
Greg Clayton91c0e742013-01-11 23:44:27 +00004556 // Make sure we load any scripting resources that may be embedded
4557 // in the debug info files in case the platform supports that.
4558 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004559 StreamString feedback_stream;
4560 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004561 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004562 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4563 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4564 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004565 else if (feedback_stream.GetSize())
4566 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004567
Greg Clayton89deb062012-12-12 01:15:30 +00004568 flush = true;
4569 result.SetStatus (eReturnStatusSuccessFinishResult);
4570 return true;
4571 }
4572 }
4573 }
4574 // Clear the symbol file spec if anything went wrong
4575 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004576 }
4577
4578 if (module_spec.GetUUID().IsValid())
4579 {
4580 StreamString ss_symfile_uuid;
4581 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4582 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4583 symfile_path,
4584 ss_symfile_uuid.GetData(),
4585 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4586 ? "\n please specify the full path to the symbol file"
4587 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004588 }
4589 else
4590 {
Greg Clayton89deb062012-12-12 01:15:30 +00004591 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4592 symfile_path,
4593 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4594 ? "\n please specify the full path to the symbol file"
4595 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004596 }
4597 }
4598 else
4599 {
4600 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004601 }
Greg Clayton89deb062012-12-12 01:15:30 +00004602 result.SetStatus (eReturnStatusFailed);
4603 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004604 }
4605
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004606 bool
4607 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytone72dfb32012-02-24 01:59:29 +00004608 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004609 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004610 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004611 bool flush = false;
4612 ModuleSpec module_spec;
4613 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4614 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4615 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004616 const size_t argc = args.GetArgumentCount();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004617
Greg Claytonf9fc6092013-01-09 19:44:40 +00004618 if (argc == 0)
4619 {
4620 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004621 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004622 bool success = false;
4623 bool error_set = false;
4624 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004625 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004626 Process *process = m_exe_ctx.GetProcessPtr();
4627 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004628 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004629 const StateType process_state = process->GetState();
4630 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004631 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004632 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004633 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004634 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004635 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4636 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004637 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004638 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004639 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004640 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4641 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004642 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004643 module_spec.GetUUID() = frame_module_sp->GetUUID();
4644 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004645 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004646 else
4647 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004648 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004649 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004650 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004651 }
4652 else
4653 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004654 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004655 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004656 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004657 }
4658 else
4659 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004660 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004661 error_set = true;
4662 }
4663 }
4664 else
4665 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004666 result.AppendError ("a process must exist in order to use the --frame option");
4667 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004668 }
4669 }
4670 else
4671 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004672 if (uuid_option_set)
4673 {
4674 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4675 success |= module_spec.GetUUID().IsValid();
4676 }
4677 else if (file_option_set)
4678 {
4679 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4680 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4681 if (module_sp)
4682 {
4683 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4684 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4685 module_spec.GetUUID() = module_sp->GetUUID();
4686 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4687 }
4688 else
4689 {
4690 module_spec.GetArchitecture() = target->GetArchitecture();
4691 }
Greg Clayton479d5452016-02-25 00:56:47 +00004692 success |= module_spec.GetUUID().IsValid() || module_spec.GetFileSpec().Exists();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004693 }
4694 }
4695
4696 if (success)
4697 {
4698 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4699 {
4700 if (module_spec.GetSymbolFileSpec())
4701 success = AddModuleSymbols (target, module_spec, flush, result);
4702 }
4703 }
4704
4705 if (!success && !error_set)
4706 {
4707 StreamString error_strm;
4708 if (uuid_option_set)
4709 {
4710 error_strm.PutCString("unable to find debug symbols for UUID ");
4711 module_spec.GetUUID().Dump (&error_strm);
4712 }
4713 else if (file_option_set)
4714 {
4715 error_strm.PutCString("unable to find debug symbols for the executable file ");
4716 error_strm << module_spec.GetFileSpec();
4717 }
4718 else if (frame_option_set)
4719 {
4720 error_strm.PutCString("unable to find debug symbols for the current frame");
4721 }
4722 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004723 }
4724 }
4725 else
4726 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004727 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4728 }
4729 }
4730 else
4731 {
4732 if (uuid_option_set)
4733 {
4734 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4735 }
4736 else if (file_option_set)
4737 {
4738 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4739 }
4740 else if (frame_option_set)
4741 {
4742 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4743 }
4744 else
4745 {
4746 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004747
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004748 for (size_t i = 0; i < argc; ++i)
Greg Claytonf9fc6092013-01-09 19:44:40 +00004749 {
4750 const char *symfile_path = args.GetArgumentAtIndex(i);
4751 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004752 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004753 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4754 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004755 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004756 FileSpec symfile_spec;
4757 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4758 module_spec.GetSymbolFileSpec() = symfile_spec;
4759 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004760
Greg Claytonf9fc6092013-01-09 19:44:40 +00004761 ArchSpec arch;
4762 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004763
Greg Claytonf9fc6092013-01-09 19:44:40 +00004764 if (symfile_exists)
4765 {
4766 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004767 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004768 }
4769 else
4770 {
4771 char resolved_symfile_path[PATH_MAX];
4772 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4773 {
4774 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4775 {
4776 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4777 break;
4778 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004779 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004780 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4781 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004782 }
4783 }
4784 }
4785 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004786 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004787
Greg Claytonf9fc6092013-01-09 19:44:40 +00004788 if (flush)
4789 {
4790 Process *process = m_exe_ctx.GetProcessPtr();
4791 if (process)
4792 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004793 }
4794 return result.Succeeded();
4795 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004796
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004797 OptionGroupOptions m_option_group;
4798 OptionGroupUUID m_uuid_option_group;
4799 OptionGroupFile m_file_option;
4800 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004801};
4802
Greg Claytone72dfb32012-02-24 01:59:29 +00004803#pragma mark CommandObjectTargetSymbols
4804
4805//-------------------------------------------------------------------------
4806// CommandObjectTargetSymbols
4807//-------------------------------------------------------------------------
4808
4809class CommandObjectTargetSymbols : public CommandObjectMultiword
4810{
4811public:
4812 //------------------------------------------------------------------
4813 // Constructors and Destructors
4814 //------------------------------------------------------------------
4815 CommandObjectTargetSymbols(CommandInterpreter &interpreter) :
4816 CommandObjectMultiword (interpreter,
4817 "target symbols",
4818 "A set of commands for adding and managing debug symbol files.",
4819 "target symbols <sub-command> ...")
4820 {
4821 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00004822 }
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004823
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004824 ~CommandObjectTargetSymbols() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004825
Greg Claytone72dfb32012-02-24 01:59:29 +00004826private:
4827 //------------------------------------------------------------------
4828 // For CommandObjectTargetModules only
4829 //------------------------------------------------------------------
4830 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4831};
4832
Jim Ingham9575d842011-03-11 03:53:59 +00004833#pragma mark CommandObjectTargetStopHookAdd
4834
4835//-------------------------------------------------------------------------
4836// CommandObjectTargetStopHookAdd
4837//-------------------------------------------------------------------------
4838
Greg Clayton44d93782014-01-27 23:43:24 +00004839class CommandObjectTargetStopHookAdd :
4840 public CommandObjectParsed,
4841 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004842{
4843public:
Jim Ingham9575d842011-03-11 03:53:59 +00004844 class CommandOptions : public Options
4845 {
4846 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004847 CommandOptions (CommandInterpreter &interpreter) :
4848 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004849 m_line_start(0),
4850 m_line_end (UINT_MAX),
4851 m_func_name_type_mask (eFunctionNameTypeAuto),
4852 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004853 m_thread_specified (false),
4854 m_use_one_liner (false),
4855 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004856 {
4857 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004858
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004859 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004860
Greg Claytone0d378b2011-03-24 21:19:54 +00004861 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004862 GetDefinitions () override
Jim Ingham9575d842011-03-11 03:53:59 +00004863 {
4864 return g_option_table;
4865 }
4866
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004867 Error
4868 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham9575d842011-03-11 03:53:59 +00004869 {
4870 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004871 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004872 bool success;
4873
4874 switch (short_option)
4875 {
4876 case 'c':
4877 m_class_name = option_arg;
4878 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004879 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004880
Jim Ingham9575d842011-03-11 03:53:59 +00004881 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004882 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004883 if (!success)
4884 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004885 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004886 break;
4887 }
4888 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004889 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004890
Jim Ingham9575d842011-03-11 03:53:59 +00004891 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004892 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004893 if (!success)
4894 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004895 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004896 break;
4897 }
4898 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004899 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004900
Sean Callanand4a7c122012-02-11 01:22:21 +00004901 case 'i':
4902 m_no_inlines = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004903 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004904
Jim Ingham9575d842011-03-11 03:53:59 +00004905 case 'n':
4906 m_function_name = option_arg;
4907 m_func_name_type_mask |= eFunctionNameTypeAuto;
4908 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004909 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004910
Jim Ingham9575d842011-03-11 03:53:59 +00004911 case 'f':
4912 m_file_name = option_arg;
4913 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004914 break;
4915
Jim Ingham9575d842011-03-11 03:53:59 +00004916 case 's':
4917 m_module_name = option_arg;
4918 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004919 break;
4920
Jim Ingham9575d842011-03-11 03:53:59 +00004921 case 't' :
Vince Harron5275aaa2015-01-15 20:08:35 +00004922 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004923 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004924 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004925 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004926 break;
4927
Jim Ingham9575d842011-03-11 03:53:59 +00004928 case 'T':
4929 m_thread_name = option_arg;
4930 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004931 break;
4932
Jim Ingham9575d842011-03-11 03:53:59 +00004933 case 'q':
4934 m_queue_name = option_arg;
4935 m_thread_specified = true;
4936 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004937
Jim Ingham9575d842011-03-11 03:53:59 +00004938 case 'x':
Vince Harron5275aaa2015-01-15 20:08:35 +00004939 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004940 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004941 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004942 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004943 break;
4944
Johnny Chenb1372c02011-05-02 23:47:55 +00004945 case 'o':
4946 m_use_one_liner = true;
4947 m_one_liner = option_arg;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004948 break;
4949
Jim Ingham9575d842011-03-11 03:53:59 +00004950 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004951 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004952 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004953 }
4954 return error;
4955 }
4956
4957 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004958 OptionParsingStarting () override
Jim Ingham9575d842011-03-11 03:53:59 +00004959 {
4960 m_class_name.clear();
4961 m_function_name.clear();
4962 m_line_start = 0;
4963 m_line_end = UINT_MAX;
4964 m_file_name.clear();
4965 m_module_name.clear();
4966 m_func_name_type_mask = eFunctionNameTypeAuto;
4967 m_thread_id = LLDB_INVALID_THREAD_ID;
4968 m_thread_index = UINT32_MAX;
4969 m_thread_name.clear();
4970 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004971
Sean Callanand4a7c122012-02-11 01:22:21 +00004972 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004973 m_sym_ctx_specified = false;
4974 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004975
4976 m_use_one_liner = false;
4977 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004978 }
4979
Greg Claytone0d378b2011-03-24 21:19:54 +00004980 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004981
Jim Ingham9575d842011-03-11 03:53:59 +00004982 std::string m_class_name;
4983 std::string m_function_name;
4984 uint32_t m_line_start;
4985 uint32_t m_line_end;
4986 std::string m_file_name;
4987 std::string m_module_name;
4988 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4989 lldb::tid_t m_thread_id;
4990 uint32_t m_thread_index;
4991 std::string m_thread_name;
4992 std::string m_queue_name;
4993 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004994 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004995 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00004996 // Instance variables to hold the values for one_liner options.
4997 bool m_use_one_liner;
4998 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00004999 };
5000
Jim Ingham9575d842011-03-11 03:53:59 +00005001 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005002 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00005003 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005004 "Add a hook to be executed when the target stops.",
5005 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005006 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005007 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005008 {
5009 }
5010
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005011 ~CommandObjectTargetStopHookAdd() override = default;
5012
5013 Options *
5014 GetOptions () override
Jim Ingham9575d842011-03-11 03:53:59 +00005015 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005016 return &m_options;
Jim Ingham9575d842011-03-11 03:53:59 +00005017 }
5018
Jim Ingham5a988412012-06-08 21:56:10 +00005019protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005020 void
5021 IOHandlerActivated (IOHandler &io_handler) override
Greg Clayton44d93782014-01-27 23:43:24 +00005022 {
5023 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5024 if (output_sp)
5025 {
5026 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5027 output_sp->Flush();
5028 }
5029 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005030
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005031 void
5032 IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override
Greg Clayton44d93782014-01-27 23:43:24 +00005033 {
5034 if (m_stop_hook_sp)
5035 {
5036 if (line.empty())
5037 {
5038 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5039 if (error_sp)
5040 {
5041 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5042 error_sp->Flush();
5043 }
5044 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5045 if (target)
5046 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5047 }
5048 else
5049 {
5050 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5051 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5052 if (output_sp)
5053 {
5054 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5055 output_sp->Flush();
5056 }
5057 }
5058 m_stop_hook_sp.reset();
5059 }
5060 io_handler.SetIsDone(true);
5061 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005062
Jim Ingham9575d842011-03-11 03:53:59 +00005063 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005064 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005065 {
Greg Clayton44d93782014-01-27 23:43:24 +00005066 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005067
Jim Ingham893c9322014-11-22 01:42:44 +00005068 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005069 if (target)
5070 {
Greg Clayton44d93782014-01-27 23:43:24 +00005071 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005072
5073 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005074 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005075 if (m_options.m_sym_ctx_specified)
5076 {
5077 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005078
Jim Ingham9575d842011-03-11 03:53:59 +00005079 if (!m_options.m_module_name.empty())
5080 {
5081 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5082 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005083
Jim Ingham9575d842011-03-11 03:53:59 +00005084 if (!m_options.m_class_name.empty())
5085 {
5086 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5087 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005088
Jim Ingham9575d842011-03-11 03:53:59 +00005089 if (!m_options.m_file_name.empty())
5090 {
5091 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5092 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005093
Jim Ingham9575d842011-03-11 03:53:59 +00005094 if (m_options.m_line_start != 0)
5095 {
5096 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5097 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005098
Jim Ingham9575d842011-03-11 03:53:59 +00005099 if (m_options.m_line_end != UINT_MAX)
5100 {
5101 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5102 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005103
Jim Ingham9575d842011-03-11 03:53:59 +00005104 if (!m_options.m_function_name.empty())
5105 {
5106 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5107 }
5108 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005109
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005110 if (specifier_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00005111 new_hook_sp->SetSpecifier (specifier_ap.release());
5112
5113 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005114
Jim Ingham9575d842011-03-11 03:53:59 +00005115 if (m_options.m_thread_specified)
5116 {
5117 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005118
Jim Ingham9575d842011-03-11 03:53:59 +00005119 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5120 {
5121 thread_spec->SetTID (m_options.m_thread_id);
5122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005123
Jim Ingham9575d842011-03-11 03:53:59 +00005124 if (m_options.m_thread_index != UINT32_MAX)
5125 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005126
Jim Ingham9575d842011-03-11 03:53:59 +00005127 if (!m_options.m_thread_name.empty())
5128 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005129
Jim Ingham9575d842011-03-11 03:53:59 +00005130 if (!m_options.m_queue_name.empty())
5131 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005132
Jim Ingham9575d842011-03-11 03:53:59 +00005133 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005134
Jim Ingham9575d842011-03-11 03:53:59 +00005135 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005136 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005137 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005138 // Use one-liner.
5139 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005140 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005141 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005142 else
Jim Ingham9575d842011-03-11 03:53:59 +00005143 {
Greg Clayton44d93782014-01-27 23:43:24 +00005144 m_stop_hook_sp = new_hook_sp;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005145 m_interpreter.GetLLDBCommandsFromIOHandler("> ", // Prompt
5146 *this, // IOHandlerDelegate
5147 true, // Run IOHandler in async mode
5148 nullptr); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
Greg Clayton44d93782014-01-27 23:43:24 +00005149
Jim Ingham9575d842011-03-11 03:53:59 +00005150 }
Jim Ingham9575d842011-03-11 03:53:59 +00005151 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5152 }
5153 else
5154 {
Greg Clayton7260f622011-04-18 08:33:37 +00005155 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005156 result.SetStatus (eReturnStatusFailed);
5157 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005158
Jim Ingham9575d842011-03-11 03:53:59 +00005159 return result.Succeeded();
5160 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005161
Jim Ingham9575d842011-03-11 03:53:59 +00005162private:
5163 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005164 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005165};
5166
Greg Claytone0d378b2011-03-24 21:19:54 +00005167OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005168CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5169{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005170 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005171 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005172 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005173 "Set the module within which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005174 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005175 "The stop hook is run only for the thread whose index matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005176 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005177 "The stop hook is run only for the thread whose TID matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005178 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005179 "The stop hook is run only for the thread whose thread name matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005180 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005181 "The stop hook is run only for threads in the queue whose name is given by this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005182 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005183 "Specify the source file within which the stop-hook is to be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005184 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005185 "Set the start of the line range for which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005186 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005187 "Set the end of the line range for which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005188 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005189 "Specify the class within which the stop-hook is to be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005190 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005191 "Set the function name within which the stop hook will be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005192 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jim Ingham9575d842011-03-11 03:53:59 +00005193};
5194
5195#pragma mark CommandObjectTargetStopHookDelete
5196
5197//-------------------------------------------------------------------------
5198// CommandObjectTargetStopHookDelete
5199//-------------------------------------------------------------------------
5200
Jim Ingham5a988412012-06-08 21:56:10 +00005201class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005202{
5203public:
Jim Ingham9575d842011-03-11 03:53:59 +00005204 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005205 CommandObjectParsed (interpreter,
5206 "target stop-hook delete",
5207 "Delete a stop-hook.",
5208 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005209 {
5210 }
5211
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005212 ~CommandObjectTargetStopHookDelete() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005213
Jim Ingham5a988412012-06-08 21:56:10 +00005214protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005215 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005216 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005217 {
Jim Ingham893c9322014-11-22 01:42:44 +00005218 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005219 if (target)
5220 {
5221 // FIXME: see if we can use the breakpoint id style parser?
5222 size_t num_args = command.GetArgumentCount();
5223 if (num_args == 0)
5224 {
5225 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5226 {
5227 result.SetStatus (eReturnStatusFailed);
5228 return false;
5229 }
5230 else
5231 {
5232 target->RemoveAllStopHooks();
5233 }
5234 }
5235 else
5236 {
5237 bool success;
5238 for (size_t i = 0; i < num_args; i++)
5239 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005240 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005241 if (!success)
5242 {
Greg Clayton7260f622011-04-18 08:33:37 +00005243 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005244 result.SetStatus(eReturnStatusFailed);
5245 return false;
5246 }
5247 success = target->RemoveStopHookByID (user_id);
5248 if (!success)
5249 {
Greg Clayton7260f622011-04-18 08:33:37 +00005250 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005251 result.SetStatus(eReturnStatusFailed);
5252 return false;
5253 }
5254 }
5255 }
5256 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5257 }
5258 else
5259 {
Greg Clayton7260f622011-04-18 08:33:37 +00005260 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005261 result.SetStatus (eReturnStatusFailed);
5262 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005263
Jim Ingham9575d842011-03-11 03:53:59 +00005264 return result.Succeeded();
5265 }
5266};
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005267
Jim Ingham9575d842011-03-11 03:53:59 +00005268#pragma mark CommandObjectTargetStopHookEnableDisable
5269
5270//-------------------------------------------------------------------------
5271// CommandObjectTargetStopHookEnableDisable
5272//-------------------------------------------------------------------------
5273
Jim Ingham5a988412012-06-08 21:56:10 +00005274class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005275{
5276public:
Jim Ingham9575d842011-03-11 03:53:59 +00005277 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005278 CommandObjectParsed (interpreter,
5279 name,
5280 help,
5281 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005282 m_enable (enable)
5283 {
5284 }
5285
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005286 ~CommandObjectTargetStopHookEnableDisable() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005287
Jim Ingham5a988412012-06-08 21:56:10 +00005288protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005289 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005290 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005291 {
Jim Ingham893c9322014-11-22 01:42:44 +00005292 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005293 if (target)
5294 {
5295 // FIXME: see if we can use the breakpoint id style parser?
5296 size_t num_args = command.GetArgumentCount();
5297 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005298
Jim Ingham9575d842011-03-11 03:53:59 +00005299 if (num_args == 0)
5300 {
5301 target->SetAllStopHooksActiveState (m_enable);
5302 }
5303 else
5304 {
5305 for (size_t i = 0; i < num_args; i++)
5306 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005307 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005308 if (!success)
5309 {
Greg Clayton7260f622011-04-18 08:33:37 +00005310 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005311 result.SetStatus(eReturnStatusFailed);
5312 return false;
5313 }
5314 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5315 if (!success)
5316 {
Greg Clayton7260f622011-04-18 08:33:37 +00005317 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005318 result.SetStatus(eReturnStatusFailed);
5319 return false;
5320 }
5321 }
5322 }
5323 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5324 }
5325 else
5326 {
Greg Clayton7260f622011-04-18 08:33:37 +00005327 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005328 result.SetStatus (eReturnStatusFailed);
5329 }
5330 return result.Succeeded();
5331 }
5332private:
5333 bool m_enable;
5334};
5335
5336#pragma mark CommandObjectTargetStopHookList
5337
5338//-------------------------------------------------------------------------
5339// CommandObjectTargetStopHookList
5340//-------------------------------------------------------------------------
5341
Jim Ingham5a988412012-06-08 21:56:10 +00005342class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005343{
5344public:
Jim Ingham9575d842011-03-11 03:53:59 +00005345 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005346 CommandObjectParsed (interpreter,
5347 "target stop-hook list",
5348 "List all stop-hooks.",
5349 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005350 {
5351 }
5352
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005353 ~CommandObjectTargetStopHookList() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005354
Jim Ingham5a988412012-06-08 21:56:10 +00005355protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005356 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005357 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005358 {
Jim Ingham893c9322014-11-22 01:42:44 +00005359 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005360 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005361 {
Greg Clayton7260f622011-04-18 08:33:37 +00005362 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005363 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005364 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005365 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005366
Jim Ingham9575d842011-03-11 03:53:59 +00005367 size_t num_hooks = target->GetNumStopHooks ();
5368 if (num_hooks == 0)
5369 {
5370 result.GetOutputStream().PutCString ("No stop hooks.\n");
5371 }
5372 else
5373 {
5374 for (size_t i = 0; i < num_hooks; i++)
5375 {
5376 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5377 if (i > 0)
5378 result.GetOutputStream().PutCString ("\n");
5379 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5380 }
5381 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005382 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005383 return result.Succeeded();
5384 }
5385};
5386
5387#pragma mark CommandObjectMultiwordTargetStopHooks
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005388
Jim Ingham9575d842011-03-11 03:53:59 +00005389//-------------------------------------------------------------------------
5390// CommandObjectMultiwordTargetStopHooks
5391//-------------------------------------------------------------------------
5392
5393class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5394{
5395public:
Jim Ingham9575d842011-03-11 03:53:59 +00005396 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) :
5397 CommandObjectMultiword (interpreter,
5398 "target stop-hook",
5399 "A set of commands for operating on debugger target stop-hooks.",
5400 "target stop-hook <subcommand> [<subcommand-options>]")
5401 {
5402 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5403 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5404 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5405 false,
5406 "target stop-hook disable [<id>]",
5407 "Disable a stop-hook.",
5408 "target stop-hook disable")));
5409 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5410 true,
5411 "target stop-hook enable [<id>]",
5412 "Enable a stop-hook.",
5413 "target stop-hook enable")));
5414 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5415 }
5416
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005417 ~CommandObjectMultiwordTargetStopHooks() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005418};
5419
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005420#pragma mark CommandObjectMultiwordTarget
5421
5422//-------------------------------------------------------------------------
5423// CommandObjectMultiwordTarget
5424//-------------------------------------------------------------------------
5425
Greg Clayton66111032010-06-23 01:19:29 +00005426CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) :
Greg Claytona7015092010-09-18 01:14:36 +00005427 CommandObjectMultiword (interpreter,
5428 "target",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005429 "A set of commands for operating on debugger targets.",
5430 "target <subcommand> [<subcommand-options>]")
5431{
Greg Clayton7260f622011-04-18 08:33:37 +00005432 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter)));
Greg Clayton3418c852011-08-10 02:10:13 +00005433 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter)));
Greg Clayton7260f622011-04-18 08:33:37 +00005434 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter)));
5435 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter)));
Jim Ingham9575d842011-03-11 03:53:59 +00005436 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00005437 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00005438 LoadSubCommand ("symbols", CommandObjectSP (new CommandObjectTargetSymbols (interpreter)));
Greg Clayton644247c2011-07-07 01:59:51 +00005439 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005440}
5441
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005442CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget() = default;