blob: d6cf0e346a91c6b492a79c0d14768c5fb48f5d2c [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);
Zachary Turnerf343968f2016-08-09 23:06:08 +0000272 Timer scoped_timer(LLVM_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:
Kate Stone7428a182016-07-14 22:03:10 +0000727 CommandObjectTargetVariable(CommandInterpreter &interpreter)
728 : CommandObjectParsed(interpreter, "target variable",
729 "Read global variables for the current target, before or while running a process.",
730 nullptr, eCommandRequiresTarget),
731 m_option_group(interpreter),
732 m_option_variable(false), // Don't include frame options
733 m_option_format(eFormatDefault),
734 m_option_compile_units(LLDB_OPT_SET_1, false, "file", SHORT_OPTION_FILE, 0, eArgTypeFilename,
735 "A basename or fullpath to a file that contains global variables. This option can be "
736 "specified multiple times."),
737 m_option_shared_libraries(LLDB_OPT_SET_1, false, "shlib", SHORT_OPTION_SHLB, 0, eArgTypeFilename,
738 "A basename or fullpath to a shared library to use in the search for global "
739 "variables. This option can be specified multiple times."),
740 m_varobj_options()
Greg Clayton644247c2011-07-07 01:59:51 +0000741 {
Johnny Chen81ab3f52011-08-22 22:22:00 +0000742 CommandArgumentEntry arg;
743 CommandArgumentData var_name_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000744
Johnny Chen81ab3f52011-08-22 22:22:00 +0000745 // Define the first (and only) variant of this arg.
746 var_name_arg.arg_type = eArgTypeVarName;
747 var_name_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000748
Johnny Chen81ab3f52011-08-22 22:22:00 +0000749 // There is only one variant this argument could be; put it into the argument entry.
750 arg.push_back (var_name_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000751
Johnny Chen81ab3f52011-08-22 22:22:00 +0000752 // Push the data for the first argument into the m_arguments vector.
753 m_arguments.push_back (arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000754
Greg Clayton644247c2011-07-07 01:59:51 +0000755 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton715c2362011-07-07 04:38:25 +0000756 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton5009f9d2011-10-27 17:55:14 +0000757 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 +0000758 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
759 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
760 m_option_group.Finalize();
761 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000762
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000763 ~CommandObjectTargetVariable() override = default;
Greg Clayton884fb692011-07-08 21:46:14 +0000764
765 void
766 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name)
767 {
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000768 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000769
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000770 if (!valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
771 valobj_sp->IsRuntimeSupportValue())
Enrico Granata560558e2015-02-11 02:35:39 +0000772 return;
773
Greg Clayton884fb692011-07-08 21:46:14 +0000774 switch (var_sp->GetScope())
775 {
776 case eValueTypeVariableGlobal:
777 if (m_option_variable.show_scope)
778 s.PutCString("GLOBAL: ");
779 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000780
Greg Clayton884fb692011-07-08 21:46:14 +0000781 case eValueTypeVariableStatic:
782 if (m_option_variable.show_scope)
783 s.PutCString("STATIC: ");
784 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000785
Greg Clayton884fb692011-07-08 21:46:14 +0000786 case eValueTypeVariableArgument:
787 if (m_option_variable.show_scope)
788 s.PutCString(" ARG: ");
789 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000790
Greg Clayton884fb692011-07-08 21:46:14 +0000791 case eValueTypeVariableLocal:
792 if (m_option_variable.show_scope)
793 s.PutCString(" LOCAL: ");
794 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000795
Greg Clayton63a27af2016-07-01 17:17:23 +0000796 case eValueTypeVariableThreadLocal:
797 if (m_option_variable.show_scope)
798 s.PutCString("THREAD: ");
799 break;
800
Greg Clayton884fb692011-07-08 21:46:14 +0000801 default:
802 break;
803 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000804
Greg Clayton45ba8542011-07-10 19:21:23 +0000805 if (m_option_variable.show_decl)
Greg Clayton884fb692011-07-08 21:46:14 +0000806 {
Greg Clayton45ba8542011-07-10 19:21:23 +0000807 bool show_fullpaths = false;
808 bool show_module = true;
809 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
810 s.PutCString (": ");
Greg Clayton884fb692011-07-08 21:46:14 +0000811 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000812
Greg Clayton1deb7962011-10-25 06:44:01 +0000813 const Format format = m_option_format.GetFormat();
Greg Clayton884fb692011-07-08 21:46:14 +0000814 if (format != eFormatDefault)
Enrico Granata0c489f52012-03-01 04:24:26 +0000815 options.SetFormat(format);
816
817 options.SetRootValueObjectName(root_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000819 valobj_sp->Dump(s,options);
Greg Clayton884fb692011-07-08 21:46:14 +0000820 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000821
Greg Claytonc7bece562013-01-25 18:06:21 +0000822 static size_t GetVariableCallback (void *baton,
823 const char *name,
824 VariableList &variable_list)
Greg Clayton884fb692011-07-08 21:46:14 +0000825 {
826 Target *target = static_cast<Target *>(baton);
827 if (target)
828 {
829 return target->GetImages().FindGlobalVariables (ConstString(name),
830 true,
831 UINT32_MAX,
832 variable_list);
833 }
834 return 0;
835 }
Greg Clayton884fb692011-07-08 21:46:14 +0000836
Jim Ingham5a988412012-06-08 21:56:10 +0000837 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000838 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +0000839 {
840 return &m_option_group;
841 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000842
Jim Ingham5a988412012-06-08 21:56:10 +0000843protected:
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000844 void
845 DumpGlobalVariableList(const ExecutionContext &exe_ctx, const SymbolContext &sc, const VariableList &variable_list, Stream &s)
846 {
847 size_t count = variable_list.GetSize();
848 if (count > 0)
849 {
850 if (sc.module_sp)
851 {
852 if (sc.comp_unit)
853 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000854 s.Printf ("Global variables for %s in %s:\n",
855 sc.comp_unit->GetPath().c_str(),
856 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000857 }
858 else
859 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000860 s.Printf ("Global variables for %s\n",
861 sc.module_sp->GetFileSpec().GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000862 }
863 }
864 else if (sc.comp_unit)
865 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000866 s.Printf ("Global variables for %s\n",
867 sc.comp_unit->GetPath().c_str());
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000868 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000869
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000870 for (uint32_t i = 0; i < count; ++i)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000871 {
872 VariableSP var_sp (variable_list.GetVariableAtIndex(i));
873 if (var_sp)
874 {
875 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000876
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000877 if (valobj_sp)
878 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString());
879 }
880 }
881 }
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000882 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000883
Bruce Mitchener13d21e92015-10-07 16:56:17 +0000884 bool
885 DoExecute (Args& args, CommandReturnObject &result) override
Greg Clayton644247c2011-07-07 01:59:51 +0000886 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000887 Target *target = m_exe_ctx.GetTargetPtr();
888 const size_t argc = args.GetArgumentCount();
889 Stream &s = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000890
Greg Claytonf9fc6092013-01-09 19:44:40 +0000891 if (argc > 0)
Greg Clayton644247c2011-07-07 01:59:51 +0000892 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000893
894 for (size_t idx = 0; idx < argc; ++idx)
Greg Clayton644247c2011-07-07 01:59:51 +0000895 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000896 VariableList variable_list;
897 ValueObjectList valobj_list;
Greg Clayton884fb692011-07-08 21:46:14 +0000898
Greg Claytonf9fc6092013-01-09 19:44:40 +0000899 const char *arg = args.GetArgumentAtIndex(idx);
Greg Claytonc7bece562013-01-25 18:06:21 +0000900 size_t matches = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000901 bool use_var_name = false;
902 if (m_option_variable.use_regex)
Greg Clayton644247c2011-07-07 01:59:51 +0000903 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000904 RegularExpression regex(arg);
905 if (!regex.IsValid ())
Greg Clayton644247c2011-07-07 01:59:51 +0000906 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000907 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg);
Greg Clayton715c2362011-07-07 04:38:25 +0000908 result.SetStatus (eReturnStatusFailed);
909 return false;
910 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000911 use_var_name = true;
912 matches = target->GetImages().FindGlobalVariables (regex,
913 true,
914 UINT32_MAX,
915 variable_list);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000916 }
917 else
918 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000919 Error error (Variable::GetValuesForVariableExpressionPath (arg,
920 m_exe_ctx.GetBestExecutionContextScope(),
921 GetVariableCallback,
922 target,
923 variable_list,
924 valobj_list));
925 matches = variable_list.GetSize();
926 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000927
Greg Claytonf9fc6092013-01-09 19:44:40 +0000928 if (matches == 0)
929 {
930 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg);
931 result.SetStatus (eReturnStatusFailed);
932 return false;
933 }
934 else
935 {
936 for (uint32_t global_idx=0; global_idx<matches; ++global_idx)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000937 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000938 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx));
939 if (var_sp)
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000940 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000941 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx));
942 if (!valobj_sp)
943 valobj_sp = ValueObjectVariable::Create (m_exe_ctx.GetBestExecutionContextScope(), var_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000944
Greg Claytonf9fc6092013-01-09 19:44:40 +0000945 if (valobj_sp)
946 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg);
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000947 }
948 }
Greg Clayton9a5a9342011-10-05 22:17:32 +0000949 }
Greg Clayton644247c2011-07-07 01:59:51 +0000950 }
951 }
952 else
953 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000954 const FileSpecList &compile_units = m_option_compile_units.GetOptionValue().GetCurrentValue();
955 const FileSpecList &shlibs = m_option_shared_libraries.GetOptionValue().GetCurrentValue();
956 SymbolContextList sc_list;
957 const size_t num_compile_units = compile_units.GetSize();
958 const size_t num_shlibs = shlibs.GetSize();
959 if (num_compile_units == 0 && num_shlibs == 0)
960 {
961 bool success = false;
Jason Molendab57e4a12013-11-04 09:33:30 +0000962 StackFrame *frame = m_exe_ctx.GetFramePtr();
Eugene Zelenkof13e6522016-02-25 19:02:39 +0000963 CompileUnit *comp_unit = nullptr;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000964 if (frame)
965 {
966 SymbolContext sc = frame->GetSymbolContext (eSymbolContextCompUnit);
967 if (sc.comp_unit)
968 {
969 const bool can_create = true;
970 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
971 if (comp_unit_varlist_sp)
972 {
973 size_t count = comp_unit_varlist_sp->GetSize();
974 if (count > 0)
975 {
976 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
977 success = true;
978 }
979 }
980 }
981 }
982 if (!success)
983 {
984 if (frame)
985 {
986 if (comp_unit)
Greg Claytonb5ad4ec2013-04-29 17:25:54 +0000987 result.AppendErrorWithFormat ("no global variables in current compile unit: %s\n",
988 comp_unit->GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000989 else
990 result.AppendErrorWithFormat ("no debug information for frame %u\n", frame->GetFrameIndex());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000991 }
Greg Claytonf9fc6092013-01-09 19:44:40 +0000992 else
993 result.AppendError ("'target variable' takes one or more global variable names as arguments\n");
994 result.SetStatus (eReturnStatusFailed);
995 }
996 }
997 else
998 {
999 SymbolContextList sc_list;
1000 const bool append = true;
1001 // We have one or more compile unit or shlib
1002 if (num_shlibs > 0)
1003 {
1004 for (size_t shlib_idx=0; shlib_idx<num_shlibs; ++shlib_idx)
1005 {
1006 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
1007 ModuleSpec module_spec (module_file);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001008
Greg Claytonf9fc6092013-01-09 19:44:40 +00001009 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
1010 if (module_sp)
1011 {
1012 if (num_compile_units > 0)
1013 {
1014 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1015 module_sp->FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1016 }
1017 else
1018 {
1019 SymbolContext sc;
1020 sc.module_sp = module_sp;
1021 sc_list.Append(sc);
1022 }
1023 }
1024 else
1025 {
1026 // Didn't find matching shlib/module in target...
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001027 result.AppendErrorWithFormat ("target doesn't contain the specified shared library: %s\n",
1028 module_file.GetPath().c_str());
Greg Claytonf9fc6092013-01-09 19:44:40 +00001029 }
1030 }
1031 }
1032 else
1033 {
1034 // No shared libraries, we just want to find globals for the compile units files that were specified
1035 for (size_t cu_idx=0; cu_idx<num_compile_units; ++cu_idx)
1036 target->GetImages().FindCompileUnits(compile_units.GetFileSpecAtIndex(cu_idx), append, sc_list);
1037 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001038
Greg Claytonf9fc6092013-01-09 19:44:40 +00001039 const uint32_t num_scs = sc_list.GetSize();
1040 if (num_scs > 0)
1041 {
1042 SymbolContext sc;
1043 for (uint32_t sc_idx=0; sc_idx<num_scs; ++sc_idx)
1044 {
1045 if (sc_list.GetContextAtIndex(sc_idx, sc))
1046 {
1047 if (sc.comp_unit)
1048 {
1049 const bool can_create = true;
1050 VariableListSP comp_unit_varlist_sp (sc.comp_unit->GetVariableList(can_create));
1051 if (comp_unit_varlist_sp)
1052 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
1053 }
1054 else if (sc.module_sp)
1055 {
1056 // Get all global variables for this module
1057 lldb_private::RegularExpression all_globals_regex("."); // Any global with at least one character
1058 VariableList variable_list;
1059 sc.module_sp->FindGlobalVariables(all_globals_regex, append, UINT32_MAX, variable_list);
1060 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1061 }
1062 }
1063 }
1064 }
1065 }
Greg Clayton644247c2011-07-07 01:59:51 +00001066 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00001067
Enrico Granata61a80ba2011-08-12 16:42:31 +00001068 if (m_interpreter.TruncationWarningNecessary())
1069 {
1070 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1071 m_cmd_name.c_str());
1072 m_interpreter.TruncationWarningGiven();
1073 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001074
Greg Clayton644247c2011-07-07 01:59:51 +00001075 return result.Succeeded();
1076 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001077
Greg Clayton644247c2011-07-07 01:59:51 +00001078 OptionGroupOptions m_option_group;
Greg Clayton715c2362011-07-07 04:38:25 +00001079 OptionGroupVariable m_option_variable;
Greg Clayton1deb7962011-10-25 06:44:01 +00001080 OptionGroupFormat m_option_format;
Greg Clayton644247c2011-07-07 01:59:51 +00001081 OptionGroupFileList m_option_compile_units;
1082 OptionGroupFileList m_option_shared_libraries;
1083 OptionGroupValueObjectDisplay m_varobj_options;
Greg Clayton644247c2011-07-07 01:59:51 +00001084};
1085
Greg Claytoneffe5c92011-05-03 22:09:39 +00001086#pragma mark CommandObjectTargetModulesSearchPathsAdd
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087
Jim Ingham5a988412012-06-08 21:56:10 +00001088class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089{
1090public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001091 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001092 CommandObjectParsed(interpreter,
1093 "target modules search-paths add",
1094 "Add new image search paths substitution pairs to the current target.",
1095 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 {
Caroline Tice405fe672010-10-04 22:28:36 +00001097 CommandArgumentEntry arg;
1098 CommandArgumentData old_prefix_arg;
1099 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001100
Caroline Tice405fe672010-10-04 22:28:36 +00001101 // Define the first variant of this arg pair.
1102 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1103 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001104
Caroline Tice405fe672010-10-04 22:28:36 +00001105 // Define the first variant of this arg pair.
1106 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1107 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001108
Caroline Tice405fe672010-10-04 22:28:36 +00001109 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1110 // must always occur together, they are treated as two variants of one argument rather than two independent
1111 // arguments. Push them both into the first argument position for m_arguments...
1112
1113 arg.push_back (old_prefix_arg);
1114 arg.push_back (new_prefix_arg);
1115
1116 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117 }
1118
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001119 ~CommandObjectTargetModulesSearchPathsAdd() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120
Jim Ingham5a988412012-06-08 21:56:10 +00001121protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001123 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124 {
Greg Claytona7015092010-09-18 01:14:36 +00001125 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126 if (target)
1127 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001128 const size_t argc = command.GetArgumentCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 if (argc & 1)
1130 {
Greg Clayton7260f622011-04-18 08:33:37 +00001131 result.AppendError ("add requires an even number of arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 result.SetStatus (eReturnStatusFailed);
1133 }
1134 else
1135 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001136 for (size_t i = 0; i < argc; i+=2)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 {
1138 const char *from = command.GetArgumentAtIndex(i);
1139 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 if (from[0] && to[0])
1142 {
Jason Molenda71387652015-07-31 04:21:25 +00001143 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
1144 if (log)
1145 {
1146 log->Printf ("target modules search path adding ImageSearchPath pair: '%s' -> '%s'",
1147 from, to);
1148 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 bool last_pair = ((argc - i) == 2);
Greg Clayton66111032010-06-23 01:19:29 +00001150 target->GetImageSearchPathList().Append (ConstString(from),
1151 ConstString(to),
1152 last_pair); // Notify if this is the last pair
Johnny Chen7791b332011-02-03 00:30:19 +00001153 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 }
1155 else
1156 {
1157 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001158 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 else
Greg Clayton7260f622011-04-18 08:33:37 +00001160 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161 result.SetStatus (eReturnStatusFailed);
1162 }
1163 }
1164 }
1165 }
1166 else
1167 {
Greg Clayton7260f622011-04-18 08:33:37 +00001168 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 result.SetStatus (eReturnStatusFailed);
1170 }
1171 return result.Succeeded();
1172 }
1173};
1174
Greg Claytoneffe5c92011-05-03 22:09:39 +00001175#pragma mark CommandObjectTargetModulesSearchPathsClear
1176
Jim Ingham5a988412012-06-08 21:56:10 +00001177class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178{
1179public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001180 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001181 CommandObjectParsed (interpreter,
1182 "target modules search-paths clear",
1183 "Clear all current image search path substitution pairs from the current target.",
1184 "target modules search-paths clear")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 {
1186 }
1187
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001188 ~CommandObjectTargetModulesSearchPathsClear() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189
Jim Ingham5a988412012-06-08 21:56:10 +00001190protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001192 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 {
Greg Claytona7015092010-09-18 01:14:36 +00001194 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 if (target)
1196 {
1197 bool notify = true;
1198 target->GetImageSearchPathList().Clear(notify);
Johnny Chen7791b332011-02-03 00:30:19 +00001199 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 }
1201 else
1202 {
Greg Clayton7260f622011-04-18 08:33:37 +00001203 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 result.SetStatus (eReturnStatusFailed);
1205 }
1206 return result.Succeeded();
1207 }
1208};
1209
Greg Claytoneffe5c92011-05-03 22:09:39 +00001210#pragma mark CommandObjectTargetModulesSearchPathsInsert
1211
Jim Ingham5a988412012-06-08 21:56:10 +00001212class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213{
1214public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001215 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001216 CommandObjectParsed(interpreter,
1217 "target modules search-paths insert",
1218 "Insert a new image search path substitution pair into the current target at the specified index.",
1219 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 {
Caroline Tice405fe672010-10-04 22:28:36 +00001221 CommandArgumentEntry arg1;
1222 CommandArgumentEntry arg2;
1223 CommandArgumentData index_arg;
1224 CommandArgumentData old_prefix_arg;
1225 CommandArgumentData new_prefix_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001226
Caroline Tice405fe672010-10-04 22:28:36 +00001227 // Define the first and only variant of this arg.
1228 index_arg.arg_type = eArgTypeIndex;
1229 index_arg.arg_repetition = eArgRepeatPlain;
1230
1231 // Put the one and only variant into the first arg for m_arguments:
1232 arg1.push_back (index_arg);
1233
1234 // Define the first variant of this arg pair.
1235 old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1236 old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001237
Caroline Tice405fe672010-10-04 22:28:36 +00001238 // Define the first variant of this arg pair.
1239 new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1240 new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001241
Caroline Tice405fe672010-10-04 22:28:36 +00001242 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they
1243 // must always occur together, they are treated as two variants of one argument rather than two independent
1244 // arguments. Push them both into the same argument position for m_arguments...
1245
1246 arg2.push_back (old_prefix_arg);
1247 arg2.push_back (new_prefix_arg);
1248
1249 // Add arguments to m_arguments.
1250 m_arguments.push_back (arg1);
1251 m_arguments.push_back (arg2);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 }
1253
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001254 ~CommandObjectTargetModulesSearchPathsInsert() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255
Jim Ingham5a988412012-06-08 21:56:10 +00001256protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001258 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 {
Greg Claytona7015092010-09-18 01:14:36 +00001260 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261 if (target)
1262 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001263 size_t argc = command.GetArgumentCount();
Bruce Mitchener4d31a482015-10-07 10:11:10 +00001264 // check for at least 3 arguments and an odd number of parameters
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265 if (argc >= 3 && argc & 1)
1266 {
1267 bool success = false;
1268
Vince Harron5275aaa2015-01-15 20:08:35 +00001269 uint32_t insert_idx = StringConvert::ToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270
1271 if (!success)
1272 {
1273 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0));
1274 result.SetStatus (eReturnStatusFailed);
1275 return result.Succeeded();
1276 }
1277
1278 // shift off the index
1279 command.Shift();
1280 argc = command.GetArgumentCount();
1281
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001282 for (uint32_t i = 0; i < argc; i += 2, ++insert_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 {
1284 const char *from = command.GetArgumentAtIndex(i);
1285 const char *to = command.GetArgumentAtIndex(i+1);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001286
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 if (from[0] && to[0])
1288 {
1289 bool last_pair = ((argc - i) == 2);
1290 target->GetImageSearchPathList().Insert (ConstString(from),
1291 ConstString(to),
1292 insert_idx,
1293 last_pair);
Johnny Chen7791b332011-02-03 00:30:19 +00001294 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295 }
1296 else
1297 {
1298 if (from[0])
Greg Clayton7260f622011-04-18 08:33:37 +00001299 result.AppendError ("<path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001300 else
Greg Clayton7260f622011-04-18 08:33:37 +00001301 result.AppendError ("<new-path-prefix> can't be empty\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302 result.SetStatus (eReturnStatusFailed);
1303 return false;
1304 }
1305 }
1306 }
1307 else
1308 {
Greg Clayton7260f622011-04-18 08:33:37 +00001309 result.AppendError ("insert requires at least three arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 result.SetStatus (eReturnStatusFailed);
1311 return result.Succeeded();
1312 }
1313
1314 }
1315 else
1316 {
Greg Clayton7260f622011-04-18 08:33:37 +00001317 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 result.SetStatus (eReturnStatusFailed);
1319 }
1320 return result.Succeeded();
1321 }
1322};
1323
Greg Claytoneffe5c92011-05-03 22:09:39 +00001324#pragma mark CommandObjectTargetModulesSearchPathsList
1325
Jim Ingham5a988412012-06-08 21:56:10 +00001326class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327{
1328public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001329 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00001330 CommandObjectParsed (interpreter,
1331 "target modules search-paths list",
1332 "List all current image search path substitution pairs in the current target.",
1333 "target modules search-paths list")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 {
1335 }
1336
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001337 ~CommandObjectTargetModulesSearchPathsList() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338
Jim Ingham5a988412012-06-08 21:56:10 +00001339protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001341 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 {
Greg Claytona7015092010-09-18 01:14:36 +00001343 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344 if (target)
1345 {
1346 if (command.GetArgumentCount() != 0)
1347 {
Greg Clayton7260f622011-04-18 08:33:37 +00001348 result.AppendError ("list takes no arguments\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 result.SetStatus (eReturnStatusFailed);
1350 return result.Succeeded();
1351 }
1352
1353 target->GetImageSearchPathList().Dump(&result.GetOutputStream());
Johnny Chen7791b332011-02-03 00:30:19 +00001354 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 }
1356 else
1357 {
Greg Clayton7260f622011-04-18 08:33:37 +00001358 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 result.SetStatus (eReturnStatusFailed);
1360 }
1361 return result.Succeeded();
1362 }
1363};
1364
Greg Claytoneffe5c92011-05-03 22:09:39 +00001365#pragma mark CommandObjectTargetModulesSearchPathsQuery
1366
Jim Ingham5a988412012-06-08 21:56:10 +00001367class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368{
1369public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00001370 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001371 CommandObjectParsed(interpreter,
1372 "target modules search-paths query",
1373 "Transform a path using the first applicable image search path.",
1374 nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 {
Caroline Tice405fe672010-10-04 22:28:36 +00001376 CommandArgumentEntry arg;
1377 CommandArgumentData path_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001378
Caroline Tice405fe672010-10-04 22:28:36 +00001379 // Define the first (and only) variant of this arg.
Sean Callanan31542552012-10-24 01:12:14 +00001380 path_arg.arg_type = eArgTypeDirectoryName;
Caroline Tice405fe672010-10-04 22:28:36 +00001381 path_arg.arg_repetition = eArgRepeatPlain;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001382
Caroline Tice405fe672010-10-04 22:28:36 +00001383 // There is only one variant this argument could be; put it into the argument entry.
1384 arg.push_back (path_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001385
Caroline Tice405fe672010-10-04 22:28:36 +00001386 // Push the data for the first argument into the m_arguments vector.
1387 m_arguments.push_back (arg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 }
1389
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001390 ~CommandObjectTargetModulesSearchPathsQuery() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391
Jim Ingham5a988412012-06-08 21:56:10 +00001392protected:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00001394 DoExecute (Args& command, CommandReturnObject &result) override
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395 {
Greg Claytona7015092010-09-18 01:14:36 +00001396 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397 if (target)
1398 {
1399 if (command.GetArgumentCount() != 1)
1400 {
Greg Clayton7260f622011-04-18 08:33:37 +00001401 result.AppendError ("query requires one argument\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 result.SetStatus (eReturnStatusFailed);
1403 return result.Succeeded();
1404 }
1405
1406 ConstString orig(command.GetArgumentAtIndex(0));
1407 ConstString transformed;
1408 if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1409 result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1410 else
1411 result.GetOutputStream().Printf("%s\n", orig.GetCString());
Johnny Chen7791b332011-02-03 00:30:19 +00001412
1413 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 }
1415 else
1416 {
Greg Clayton7260f622011-04-18 08:33:37 +00001417 result.AppendError ("invalid target\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418 result.SetStatus (eReturnStatusFailed);
1419 }
1420 return result.Succeeded();
1421 }
1422};
1423
Greg Claytoneffe5c92011-05-03 22:09:39 +00001424//----------------------------------------------------------------------
1425// Static Helper functions
1426//----------------------------------------------------------------------
1427static void
1428DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width)
1429{
1430 if (module)
1431 {
Todd Fiala7df337f2015-10-13 23:41:19 +00001432 StreamString arch_strm;
1433
Greg Claytoneffe5c92011-05-03 22:09:39 +00001434 if (full_triple)
Todd Fiala7df337f2015-10-13 23:41:19 +00001435 module->GetArchitecture().DumpTriple(arch_strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001436 else
Todd Fiala7df337f2015-10-13 23:41:19 +00001437 arch_strm.PutCString(module->GetArchitecture().GetArchitectureName());
1438 std::string arch_str = arch_strm.GetString();
1439
Greg Claytoneffe5c92011-05-03 22:09:39 +00001440 if (width)
Todd Fiala7df337f2015-10-13 23:41:19 +00001441 strm.Printf("%-*s", width, arch_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00001442 else
Todd Fiala7df337f2015-10-13 23:41:19 +00001443 strm.PutCString(arch_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00001444 }
1445}
1446
1447static void
1448DumpModuleUUID (Stream &strm, Module *module)
1449{
Jim Ingham28eb5712012-10-12 17:34:26 +00001450 if (module && module->GetUUID().IsValid())
Greg Clayton3418c852011-08-10 02:10:13 +00001451 module->GetUUID().Dump (&strm);
1452 else
1453 strm.PutCString(" ");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001454}
1455
1456static uint32_t
Greg Claytona0ca6602012-10-18 16:33:33 +00001457DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1458 Stream &strm,
1459 Module *module,
1460 const FileSpec &file_spec,
1461 bool load_addresses)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001462{
1463 uint32_t num_matches = 0;
1464 if (module)
1465 {
1466 SymbolContextList sc_list;
1467 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
1468 0,
1469 false,
1470 eSymbolContextCompUnit,
1471 sc_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001472
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001473 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001474 {
1475 SymbolContext sc;
1476 if (sc_list.GetContextAtIndex(i, sc))
1477 {
1478 if (i > 0)
1479 strm << "\n\n";
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001480
Greg Claytoneffe5c92011-05-03 22:09:39 +00001481 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `"
1482 << module->GetFileSpec().GetFilename() << "\n";
1483 LineTable *line_table = sc.comp_unit->GetLineTable();
1484 if (line_table)
1485 line_table->GetDescription (&strm,
Greg Claytonc14ee322011-09-22 04:58:26 +00001486 interpreter.GetExecutionContext().GetTargetPtr(),
Greg Claytoneffe5c92011-05-03 22:09:39 +00001487 lldb::eDescriptionLevelBrief);
1488 else
1489 strm << "No line table";
1490 }
1491 }
1492 }
1493 return num_matches;
1494}
1495
1496static void
1497DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1498{
1499 if (file_spec_ptr)
1500 {
1501 if (width > 0)
1502 {
Jason Molendadb7d11c2013-05-06 10:21:11 +00001503 std::string fullpath = file_spec_ptr->GetPath();
1504 strm.Printf("%-*s", width, fullpath.c_str());
1505 return;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001506 }
1507 else
1508 {
1509 file_spec_ptr->Dump(&strm);
1510 return;
1511 }
1512 }
1513 // Keep the width spacing correct if things go wrong...
1514 if (width > 0)
1515 strm.Printf("%-*s", width, "");
1516}
1517
1518static void
1519DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1520{
1521 if (file_spec_ptr)
1522 {
1523 if (width > 0)
1524 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1525 else
1526 file_spec_ptr->GetDirectory().Dump(&strm);
1527 return;
1528 }
1529 // Keep the width spacing correct if things go wrong...
1530 if (width > 0)
1531 strm.Printf("%-*s", width, "");
1532}
1533
1534static void
1535DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
1536{
1537 if (file_spec_ptr)
1538 {
1539 if (width > 0)
1540 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1541 else
1542 file_spec_ptr->GetFilename().Dump(&strm);
1543 return;
1544 }
1545 // Keep the width spacing correct if things go wrong...
1546 if (width > 0)
1547 strm.Printf("%-*s", width, "");
1548}
1549
Adrian McCarthy543725c2016-04-04 21:21:49 +00001550static size_t
1551DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list)
1552{
1553 size_t num_dumped = 0;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001554 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
Adrian McCarthy543725c2016-04-04 21:21:49 +00001555 const size_t num_modules = module_list.GetSize();
1556 if (num_modules > 0)
1557 {
1558 strm.Printf("Dumping headers for %" PRIu64 " module(s).\n", static_cast<uint64_t>(num_modules));
1559 strm.IndentMore();
1560 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx)
1561 {
1562 Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
1563 if (module)
1564 {
1565 if (num_dumped++ > 0)
1566 {
1567 strm.EOL();
1568 strm.EOL();
1569 }
1570 ObjectFile *objfile = module->GetObjectFile();
1571 objfile->Dump(&strm);
1572 }
1573 }
1574 strm.IndentLess();
1575 }
1576 return num_dumped;
1577}
1578
Greg Claytoneffe5c92011-05-03 22:09:39 +00001579static void
1580DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
1581{
1582 if (module)
1583 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001584 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1585 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001586 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001587 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001588 if (symtab)
Greg Claytonc14ee322011-09-22 04:58:26 +00001589 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001590 }
1591 }
1592}
1593
1594static void
1595DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
1596{
1597 if (module)
1598 {
Greg Clayton3046e662013-07-10 01:23:25 +00001599 SectionList *section_list = module->GetSectionList();
Michael Sartaina7499c92013-07-01 19:45:50 +00001600 if (section_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001601 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001602 strm.Printf ("Sections for '%s' (%s):\n",
1603 module->GetSpecificationDescription().c_str(),
1604 module->GetArchitecture().GetArchitectureName());
1605 strm.IndentMore();
1606 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX);
1607 strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001608 }
1609 }
1610}
1611
1612static bool
1613DumpModuleSymbolVendor (Stream &strm, Module *module)
1614{
1615 if (module)
1616 {
1617 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
1618 if (symbol_vendor)
1619 {
1620 symbol_vendor->Dump(&strm);
1621 return true;
1622 }
1623 }
1624 return false;
1625}
1626
Greg Claytonc4a8a762012-05-15 18:43:44 +00001627static void
1628DumpAddress (ExecutionContextScope *exe_scope, const Address &so_addr, bool verbose, Stream &strm)
1629{
1630 strm.IndentMore();
1631 strm.Indent (" Address: ");
1632 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1633 strm.PutCString (" (");
1634 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1635 strm.PutCString (")\n");
1636 strm.Indent (" Summary: ");
1637 const uint32_t save_indent = strm.GetIndentLevel ();
1638 strm.SetIndentLevel (save_indent + 13);
1639 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1640 strm.SetIndentLevel (save_indent);
1641 // Print out detailed address information when verbose is enabled
1642 if (verbose)
1643 {
1644 strm.EOL();
1645 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1646 }
1647 strm.IndentLess();
1648}
1649
Greg Claytoneffe5c92011-05-03 22:09:39 +00001650static bool
Greg Claytone72dfb32012-02-24 01:59:29 +00001651LookupAddressInModule (CommandInterpreter &interpreter,
1652 Stream &strm,
1653 Module *module,
1654 uint32_t resolve_mask,
1655 lldb::addr_t raw_addr,
1656 lldb::addr_t offset,
1657 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001658{
1659 if (module)
1660 {
1661 lldb::addr_t addr = raw_addr - offset;
1662 Address so_addr;
1663 SymbolContext sc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001664 Target *target = interpreter.GetExecutionContext().GetTargetPtr();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001665 if (target && !target->GetSectionLoadList().IsEmpty())
1666 {
1667 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
1668 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +00001669 else if (so_addr.GetModule().get() != module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001670 return false;
1671 }
1672 else
1673 {
1674 if (!module->ResolveFileAddress (addr, so_addr))
1675 return false;
1676 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001677
Greg Claytoneffe5c92011-05-03 22:09:39 +00001678 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope();
Greg Claytonc4a8a762012-05-15 18:43:44 +00001679 DumpAddress (exe_scope, so_addr, verbose, strm);
1680// strm.IndentMore();
1681// strm.Indent (" Address: ");
1682// so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1683// strm.PutCString (" (");
1684// so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1685// strm.PutCString (")\n");
1686// strm.Indent (" Summary: ");
1687// const uint32_t save_indent = strm.GetIndentLevel ();
1688// strm.SetIndentLevel (save_indent + 13);
1689// so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
1690// strm.SetIndentLevel (save_indent);
1691// // Print out detailed address information when verbose is enabled
1692// if (verbose)
1693// {
1694// strm.EOL();
1695// so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1696// }
1697// strm.IndentLess();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001698 return true;
1699 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001700
Greg Claytoneffe5c92011-05-03 22:09:39 +00001701 return false;
1702}
1703
1704static uint32_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001705LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001706{
1707 if (module)
1708 {
1709 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001710
Michael Sartaina7499c92013-07-01 19:45:50 +00001711 SymbolVendor *sym_vendor = module->GetSymbolVendor ();
1712 if (sym_vendor)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001713 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001714 Symtab *symtab = sym_vendor->GetSymtab();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001715 if (symtab)
1716 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00001717 std::vector<uint32_t> match_indexes;
1718 ConstString symbol_name (name);
1719 uint32_t num_matches = 0;
1720 if (name_is_regex)
1721 {
1722 RegularExpression name_regexp(name);
1723 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
1724 eSymbolTypeAny,
1725 match_indexes);
1726 }
1727 else
1728 {
1729 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
1730 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001731
Greg Claytoneffe5c92011-05-03 22:09:39 +00001732 if (num_matches > 0)
1733 {
1734 strm.Indent ();
1735 strm.Printf("%u symbols match %s'%s' in ", num_matches,
1736 name_is_regex ? "the regular expression " : "", name);
1737 DumpFullpath (strm, &module->GetFileSpec(), 0);
1738 strm.PutCString(":\n");
1739 strm.IndentMore ();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001740 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001741 {
1742 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
Greg Clayton358cf1e2015-06-25 21:46:34 +00001743 if (symbol && symbol->ValueIsAddress())
1744 {
1745 DumpAddress (interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1746 symbol->GetAddressRef(),
1747 verbose,
1748 strm);
1749 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001750 }
1751 strm.IndentLess ();
1752 return num_matches;
1753 }
1754 }
1755 }
1756 }
1757 return 0;
1758}
1759
Greg Claytoneffe5c92011-05-03 22:09:39 +00001760static void
Greg Claytonc4a8a762012-05-15 18:43:44 +00001761DumpSymbolContextList (ExecutionContextScope *exe_scope, Stream &strm, SymbolContextList &sc_list, bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001762{
1763 strm.IndentMore ();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001764
Greg Claytoneffe5c92011-05-03 22:09:39 +00001765 const uint32_t num_matches = sc_list.GetSize();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001766
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001767 for (uint32_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001768 {
1769 SymbolContext sc;
1770 if (sc_list.GetContextAtIndex(i, sc))
1771 {
Sean Callananf6172c22012-02-11 00:24:04 +00001772 AddressRange range;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001773
Sean Callananf6172c22012-02-11 00:24:04 +00001774 sc.GetAddressRange(eSymbolContextEverything,
1775 0,
1776 true,
1777 range);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001778
Greg Claytonc4a8a762012-05-15 18:43:44 +00001779 DumpAddress (exe_scope, range.GetBaseAddress(), verbose, strm);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001780 }
1781 }
1782 strm.IndentLess ();
1783}
1784
Greg Claytonc7bece562013-01-25 18:06:21 +00001785static size_t
Greg Claytonc4a8a762012-05-15 18:43:44 +00001786LookupFunctionInModule (CommandInterpreter &interpreter,
1787 Stream &strm,
1788 Module *module,
1789 const char *name,
1790 bool name_is_regex,
1791 bool include_inlines,
1792 bool include_symbols,
1793 bool verbose)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001794{
1795 if (module && name && name[0])
1796 {
1797 SymbolContextList sc_list;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001798 const bool append = true;
Greg Claytonc7bece562013-01-25 18:06:21 +00001799 size_t num_matches = 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001800 if (name_is_regex)
1801 {
1802 RegularExpression function_name_regex (name);
1803 num_matches = module->FindFunctions (function_name_regex,
1804 include_symbols,
Sean Callanan9df05fb2012-02-10 22:52:19 +00001805 include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001806 append,
1807 sc_list);
1808 }
1809 else
1810 {
1811 ConstString function_name (name);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00001812 num_matches = module->FindFunctions(function_name,
1813 nullptr,
1814 eFunctionNameTypeAuto,
1815 include_symbols,
1816 include_inlines,
1817 append,
1818 sc_list);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001819 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001820
Greg Claytoneffe5c92011-05-03 22:09:39 +00001821 if (num_matches)
1822 {
1823 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001824 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytoneffe5c92011-05-03 22:09:39 +00001825 DumpFullpath (strm, &module->GetFileSpec(), 0);
1826 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001827 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001828 }
1829 return num_matches;
1830 }
1831 return 0;
1832}
1833
Greg Claytonc7bece562013-01-25 18:06:21 +00001834static size_t
Greg Claytonaafa5c92012-05-15 19:26:12 +00001835LookupTypeInModule (CommandInterpreter &interpreter,
Greg Clayton644247c2011-07-07 01:59:51 +00001836 Stream &strm,
1837 Module *module,
1838 const char *name_cstr,
1839 bool name_is_regex)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001840{
1841 if (module && name_cstr && name_cstr[0])
1842 {
Greg Claytond1767f02011-12-08 02:13:16 +00001843 TypeList type_list;
Greg Clayton84db9102012-03-26 23:03:23 +00001844 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001845 size_t num_matches = 0;
Greg Clayton84db9102012-03-26 23:03:23 +00001846 bool name_is_fully_qualified = false;
Greg Claytond1767f02011-12-08 02:13:16 +00001847 SymbolContext sc;
1848
1849 ConstString name(name_cstr);
Greg Claytonae088e52016-02-10 21:28:13 +00001850 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1851 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 +00001852
Greg Claytond1767f02011-12-08 02:13:16 +00001853 if (num_matches)
1854 {
1855 strm.Indent ();
Deepak Panickal99fbc072014-03-03 15:39:47 +00001856 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, num_matches > 1 ? "es" : "");
Greg Claytond1767f02011-12-08 02:13:16 +00001857 DumpFullpath (strm, &module->GetFileSpec(), 0);
1858 strm.PutCString(":\n");
Sean Callanan5c19eac2013-11-06 19:28:40 +00001859 for (TypeSP type_sp : type_list.Types())
Greg Claytoneffe5c92011-05-03 22:09:39 +00001860 {
Greg Claytond1767f02011-12-08 02:13:16 +00001861 if (type_sp)
Greg Claytoneffe5c92011-05-03 22:09:39 +00001862 {
Greg Claytond1767f02011-12-08 02:13:16 +00001863 // Resolve the clang type so that any forward references
1864 // to types that haven't yet been parsed will get parsed.
Greg Clayton99558cc42015-08-24 23:46:31 +00001865 type_sp->GetFullCompilerType ();
Greg Claytond1767f02011-12-08 02:13:16 +00001866 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
Greg Claytonaafa5c92012-05-15 19:26:12 +00001867 // Print all typedef chains
1868 TypeSP typedef_type_sp (type_sp);
1869 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1870 while (typedefed_type_sp)
1871 {
1872 strm.EOL();
1873 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
Greg Clayton99558cc42015-08-24 23:46:31 +00001874 typedefed_type_sp->GetFullCompilerType ();
Greg Claytonaafa5c92012-05-15 19:26:12 +00001875 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1876 typedef_type_sp = typedefed_type_sp;
1877 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1878 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00001879 }
Greg Claytond1767f02011-12-08 02:13:16 +00001880 strm.EOL();
Greg Claytoneffe5c92011-05-03 22:09:39 +00001881 }
Greg Claytond1767f02011-12-08 02:13:16 +00001882 }
1883 return num_matches;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001884 }
1885 return 0;
1886}
1887
Greg Claytonc7bece562013-01-25 18:06:21 +00001888static size_t
Sean Callanand38b4a92012-06-06 20:49:55 +00001889LookupTypeHere (CommandInterpreter &interpreter,
1890 Stream &strm,
1891 const SymbolContext &sym_ctx,
1892 const char *name_cstr,
1893 bool name_is_regex)
1894{
1895 if (!sym_ctx.module_sp)
1896 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001897
Sean Callanand38b4a92012-06-06 20:49:55 +00001898 TypeList type_list;
1899 const uint32_t max_num_matches = UINT32_MAX;
Greg Claytonc7bece562013-01-25 18:06:21 +00001900 size_t num_matches = 1;
Sean Callanand38b4a92012-06-06 20:49:55 +00001901 bool name_is_fully_qualified = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001902
Sean Callanand38b4a92012-06-06 20:49:55 +00001903 ConstString name(name_cstr);
Greg Claytonae088e52016-02-10 21:28:13 +00001904 llvm::DenseSet<SymbolFile *> searched_symbol_files;
1905 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 +00001906
Sean Callanand38b4a92012-06-06 20:49:55 +00001907 if (num_matches)
1908 {
1909 strm.Indent ();
1910 strm.PutCString("Best match found in ");
1911 DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0);
1912 strm.PutCString(":\n");
Enrico Granatae5ffa082015-09-23 19:32:56 +00001913
Sean Callanand38b4a92012-06-06 20:49:55 +00001914 TypeSP type_sp (type_list.GetTypeAtIndex(0));
1915 if (type_sp)
1916 {
1917 // Resolve the clang type so that any forward references
1918 // to types that haven't yet been parsed will get parsed.
Greg Clayton99558cc42015-08-24 23:46:31 +00001919 type_sp->GetFullCompilerType ();
Sean Callanand38b4a92012-06-06 20:49:55 +00001920 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1921 // Print all typedef chains
1922 TypeSP typedef_type_sp (type_sp);
1923 TypeSP typedefed_type_sp (typedef_type_sp->GetTypedefType());
1924 while (typedefed_type_sp)
1925 {
1926 strm.EOL();
1927 strm.Printf(" typedef '%s': ", typedef_type_sp->GetName().GetCString());
Greg Clayton99558cc42015-08-24 23:46:31 +00001928 typedefed_type_sp->GetFullCompilerType ();
Sean Callanand38b4a92012-06-06 20:49:55 +00001929 typedefed_type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
1930 typedef_type_sp = typedefed_type_sp;
1931 typedefed_type_sp = typedef_type_sp->GetTypedefType();
1932 }
1933 }
1934 strm.EOL();
1935 }
1936 return num_matches;
1937}
1938
1939static uint32_t
Greg Claytoneffe5c92011-05-03 22:09:39 +00001940LookupFileAndLineInModule (CommandInterpreter &interpreter,
Sean Callanand38b4a92012-06-06 20:49:55 +00001941 Stream &strm,
Greg Claytoneffe5c92011-05-03 22:09:39 +00001942 Module *module,
1943 const FileSpec &file_spec,
1944 uint32_t line,
1945 bool check_inlines,
1946 bool verbose)
1947{
1948 if (module && file_spec)
1949 {
1950 SymbolContextList sc_list;
1951 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
1952 eSymbolContextEverything, sc_list);
1953 if (num_matches > 0)
1954 {
1955 strm.Indent ();
1956 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
1957 strm << file_spec;
1958 if (line > 0)
1959 strm.Printf (":%u", line);
1960 strm << " in ";
1961 DumpFullpath (strm, &module->GetFileSpec(), 0);
1962 strm.PutCString(":\n");
Greg Claytonc4a8a762012-05-15 18:43:44 +00001963 DumpSymbolContextList (interpreter.GetExecutionContext().GetBestExecutionContextScope(), strm, sc_list, verbose);
Greg Claytoneffe5c92011-05-03 22:09:39 +00001964 return num_matches;
1965 }
1966 }
1967 return 0;
Greg Claytoneffe5c92011-05-03 22:09:39 +00001968}
1969
Greg Clayton8ee64382011-11-10 01:18:58 +00001970static size_t
1971FindModulesByName (Target *target,
1972 const char *module_name,
1973 ModuleList &module_list,
1974 bool check_global_list)
1975{
Greg Clayton8ee64382011-11-10 01:18:58 +00001976 FileSpec module_file_spec(module_name, false);
Greg Claytonb9a01b32012-02-26 05:51:37 +00001977 ModuleSpec module_spec (module_file_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001978
Greg Clayton8ee64382011-11-10 01:18:58 +00001979 const size_t initial_size = module_list.GetSize ();
1980
Greg Claytonf3156262012-07-11 20:46:47 +00001981 if (check_global_list)
Greg Clayton8ee64382011-11-10 01:18:58 +00001982 {
1983 // Check the global list
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001984 std::lock_guard<std::recursive_mutex> guard(Module::GetAllocationModuleCollectionMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00001985 const size_t num_modules = Module::GetNumberAllocatedModules();
Greg Clayton8ee64382011-11-10 01:18:58 +00001986 ModuleSP module_sp;
Greg Claytonc7bece562013-01-25 18:06:21 +00001987 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Clayton8ee64382011-11-10 01:18:58 +00001988 {
1989 Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001990
Greg Clayton8ee64382011-11-10 01:18:58 +00001991 if (module)
1992 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00001993 if (module->MatchesModuleSpec (module_spec))
Greg Clayton8ee64382011-11-10 01:18:58 +00001994 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00001995 module_sp = module->shared_from_this();
Greg Clayton8ee64382011-11-10 01:18:58 +00001996 module_list.AppendIfNeeded(module_sp);
1997 }
1998 }
1999 }
2000 }
Greg Claytonf3156262012-07-11 20:46:47 +00002001 else
2002 {
2003 if (target)
2004 {
2005 const size_t num_matches = target->GetImages().FindModules (module_spec, module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002006
Greg Claytonf3156262012-07-11 20:46:47 +00002007 // Not found in our module list for our target, check the main
2008 // shared module list in case it is a extra file used somewhere
2009 // else
2010 if (num_matches == 0)
2011 {
2012 module_spec.GetArchitecture() = target->GetArchitecture();
2013 ModuleList::FindSharedModules (module_spec, module_list);
2014 }
2015 }
2016 else
2017 {
2018 ModuleList::FindSharedModules (module_spec,module_list);
2019 }
2020 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002021
Greg Clayton8ee64382011-11-10 01:18:58 +00002022 return module_list.GetSize () - initial_size;
2023}
2024
Greg Claytoneffe5c92011-05-03 22:09:39 +00002025#pragma mark CommandObjectTargetModulesModuleAutoComplete
2026
2027//----------------------------------------------------------------------
2028// A base command object class that can auto complete with module file
2029// paths
2030//----------------------------------------------------------------------
2031
Jim Ingham5a988412012-06-08 21:56:10 +00002032class CommandObjectTargetModulesModuleAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002033{
2034public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002035 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter,
2036 const char *name,
2037 const char *help,
2038 const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00002039 CommandObjectParsed (interpreter, name, help, syntax)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002040 {
2041 CommandArgumentEntry arg;
2042 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002043
Greg Claytoneffe5c92011-05-03 22:09:39 +00002044 // Define the first (and only) variant of this arg.
2045 file_arg.arg_type = eArgTypeFilename;
2046 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002047
Greg Claytoneffe5c92011-05-03 22:09:39 +00002048 // There is only one variant this argument could be; put it into the argument entry.
2049 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002050
Greg Claytoneffe5c92011-05-03 22:09:39 +00002051 // Push the data for the first argument into the m_arguments vector.
2052 m_arguments.push_back (arg);
2053 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002054
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002055 ~CommandObjectTargetModulesModuleAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002056
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002057 int
Greg Claytoneffe5c92011-05-03 22:09:39 +00002058 HandleArgumentCompletion (Args &input,
2059 int &cursor_index,
2060 int &cursor_char_position,
2061 OptionElementVector &opt_element_vector,
2062 int match_start_point,
2063 int max_return_elements,
2064 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002065 StringList &matches) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002066 {
2067 // Arguments are the standard module completer.
2068 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2069 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002070
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002071 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2072 CommandCompletions::eModuleCompletion,
2073 completion_str.c_str(),
2074 match_start_point,
2075 max_return_elements,
2076 nullptr,
2077 word_complete,
2078 matches);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002079 return matches.GetSize();
2080 }
2081};
2082
2083#pragma mark CommandObjectTargetModulesSourceFileAutoComplete
2084
2085//----------------------------------------------------------------------
2086// A base command object class that can auto complete with module source
2087// file paths
2088//----------------------------------------------------------------------
2089
Jim Ingham5a988412012-06-08 21:56:10 +00002090class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002091{
2092public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002093 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter,
Greg Claytonf9fc6092013-01-09 19:44:40 +00002094 const char *name,
2095 const char *help,
2096 const char *syntax,
2097 uint32_t flags) :
2098 CommandObjectParsed (interpreter, name, help, syntax, flags)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002099 {
2100 CommandArgumentEntry arg;
2101 CommandArgumentData source_file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002102
Greg Claytoneffe5c92011-05-03 22:09:39 +00002103 // Define the first (and only) variant of this arg.
2104 source_file_arg.arg_type = eArgTypeSourceFile;
2105 source_file_arg.arg_repetition = eArgRepeatPlus;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002106
Greg Claytoneffe5c92011-05-03 22:09:39 +00002107 // There is only one variant this argument could be; put it into the argument entry.
2108 arg.push_back (source_file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002109
Greg Claytoneffe5c92011-05-03 22:09:39 +00002110 // Push the data for the first argument into the m_arguments vector.
2111 m_arguments.push_back (arg);
2112 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002113
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002114 ~CommandObjectTargetModulesSourceFileAutoComplete() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002115
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002116 int
Greg Claytoneffe5c92011-05-03 22:09:39 +00002117 HandleArgumentCompletion (Args &input,
2118 int &cursor_index,
2119 int &cursor_char_position,
2120 OptionElementVector &opt_element_vector,
2121 int match_start_point,
2122 int max_return_elements,
2123 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002124 StringList &matches) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002125 {
2126 // Arguments are the standard source file completer.
2127 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2128 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002129
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002130 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2131 CommandCompletions::eSourceFileCompletion,
2132 completion_str.c_str(),
2133 match_start_point,
2134 max_return_elements,
2135 nullptr,
2136 word_complete,
2137 matches);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002138 return matches.GetSize();
2139 }
2140};
2141
Adrian McCarthy543725c2016-04-04 21:21:49 +00002142#pragma mark CommandObjectTargetModulesDumpObjfile
2143
2144class CommandObjectTargetModulesDumpObjfile : public CommandObjectTargetModulesModuleAutoComplete
2145{
2146public:
2147 CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter)
2148 : CommandObjectTargetModulesModuleAutoComplete(interpreter, "target modules dump objfile",
2149 "Dump the object file headers from one or more target modules.",
2150 nullptr)
2151 {
2152 }
2153
2154 ~CommandObjectTargetModulesDumpObjfile() override = default;
2155
2156protected:
2157 bool
2158 DoExecute(Args &command, CommandReturnObject &result) override
2159 {
2160 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
2161 if (target == nullptr)
2162 {
2163 result.AppendError("invalid target, create a debug target using the 'target create' command");
2164 result.SetStatus(eReturnStatusFailed);
2165 return false;
2166 }
2167
2168 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2169 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2170 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2171
2172 size_t num_dumped = 0;
2173 if (command.GetArgumentCount() == 0)
2174 {
2175 // Dump all headers for all modules images
2176 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(), target->GetImages());
2177 if (num_dumped == 0)
2178 {
2179 result.AppendError("the target has no associated executable images");
2180 result.SetStatus(eReturnStatusFailed);
2181 }
2182 }
2183 else
2184 {
2185 // Find the modules that match the basename or full path.
2186 ModuleList module_list;
2187 const char *arg_cstr;
2188 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
2189 {
2190 size_t num_matched = FindModulesByName(target, arg_cstr, module_list, true);
2191 if (num_matched == 0)
2192 {
2193 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2194 }
2195 }
2196 // Dump all the modules we found.
2197 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(), module_list);
2198 }
2199
2200 if (num_dumped > 0)
2201 {
2202 result.SetStatus(eReturnStatusSuccessFinishResult);
2203 }
2204 else
2205 {
2206 result.AppendError("no matching executable images found");
2207 result.SetStatus(eReturnStatusFailed);
2208 }
2209 return result.Succeeded();
2210 }
2211};
2212
Greg Claytoneffe5c92011-05-03 22:09:39 +00002213#pragma mark CommandObjectTargetModulesDumpSymtab
2214
Greg Claytoneffe5c92011-05-03 22:09:39 +00002215class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
2216{
2217public:
2218 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002219 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2220 "target modules dump symtab",
2221 "Dump the symbol table from one or more target modules.",
2222 nullptr),
2223 m_options(interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002224 {
2225 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002226
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002227 ~CommandObjectTargetModulesDumpSymtab() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002228
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002229 Options *
2230 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002231 {
2232 return &m_options;
2233 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002234
Jim Ingham5a988412012-06-08 21:56:10 +00002235 class CommandOptions : public Options
2236 {
2237 public:
Jim Ingham5a988412012-06-08 21:56:10 +00002238 CommandOptions (CommandInterpreter &interpreter) :
2239 Options(interpreter),
2240 m_sort_order (eSortOrderNone)
2241 {
2242 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002243
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002244 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002245
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002246 Error
2247 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham5a988412012-06-08 21:56:10 +00002248 {
2249 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00002250 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002251
Jim Ingham5a988412012-06-08 21:56:10 +00002252 switch (short_option)
2253 {
2254 case 's':
2255 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg,
2256 g_option_table[option_idx].enum_values,
2257 eSortOrderNone,
2258 error);
2259 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002260
Jim Ingham5a988412012-06-08 21:56:10 +00002261 default:
2262 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
2263 break;
Jim Ingham5a988412012-06-08 21:56:10 +00002264 }
2265 return error;
2266 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002267
Jim Ingham5a988412012-06-08 21:56:10 +00002268 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002269 OptionParsingStarting () override
Jim Ingham5a988412012-06-08 21:56:10 +00002270 {
2271 m_sort_order = eSortOrderNone;
2272 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002273
Jim Ingham5a988412012-06-08 21:56:10 +00002274 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002275 GetDefinitions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002276 {
2277 return g_option_table;
2278 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002279
Jim Ingham5a988412012-06-08 21:56:10 +00002280 // Options table: Required for subclasses of Options.
2281 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002282
Jim Ingham5a988412012-06-08 21:56:10 +00002283 SortOrder m_sort_order;
2284 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002285
Jim Ingham5a988412012-06-08 21:56:10 +00002286protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002287 bool
2288 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002289 {
2290 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002291 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002292 {
2293 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2294 result.SetStatus (eReturnStatusFailed);
2295 return false;
2296 }
2297 else
2298 {
2299 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002300
Greg Claytoneffe5c92011-05-03 22:09:39 +00002301 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2302 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2303 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002304
Greg Claytoneffe5c92011-05-03 22:09:39 +00002305 if (command.GetArgumentCount() == 0)
2306 {
2307 // Dump all sections for all modules images
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00002308 std::lock_guard<std::recursive_mutex> guard(target->GetImages().GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002309 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002310 if (num_modules > 0)
2311 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002312 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002313 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002314 {
2315 if (num_dumped > 0)
2316 {
2317 result.GetOutputStream().EOL();
2318 result.GetOutputStream().EOL();
2319 }
2320 num_dumped++;
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002321 DumpModuleSymtab (m_interpreter,
2322 result.GetOutputStream(),
2323 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2324 m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002325 }
2326 }
2327 else
2328 {
2329 result.AppendError ("the target has no associated executable images");
2330 result.SetStatus (eReturnStatusFailed);
2331 return false;
2332 }
2333 }
2334 else
2335 {
2336 // Dump specified images (by basename or fullpath)
2337 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002338 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002339 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002340 ModuleList module_list;
2341 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2342 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002343 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002344 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002345 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002346 Module *module = module_list.GetModulePointerAtIndex(i);
2347 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002348 {
2349 if (num_dumped > 0)
2350 {
2351 result.GetOutputStream().EOL();
2352 result.GetOutputStream().EOL();
2353 }
2354 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002355 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002356 }
2357 }
2358 }
2359 else
2360 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2361 }
2362 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002363
Greg Claytoneffe5c92011-05-03 22:09:39 +00002364 if (num_dumped > 0)
2365 result.SetStatus (eReturnStatusSuccessFinishResult);
2366 else
2367 {
2368 result.AppendError ("no matching executable images found");
2369 result.SetStatus (eReturnStatusFailed);
2370 }
2371 }
2372 return result.Succeeded();
2373 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002374
Greg Claytoneffe5c92011-05-03 22:09:39 +00002375 CommandOptions m_options;
2376};
2377
2378static OptionEnumValueElement
2379g_sort_option_enumeration[4] =
2380{
2381 { eSortOrderNone, "none", "No sorting, use the original symbol table order."},
2382 { eSortOrderByAddress, "address", "Sort output by symbol address."},
2383 { eSortOrderByName, "name", "Sort output by symbol name."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002384 { 0, nullptr, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002385};
2386
Greg Claytoneffe5c92011-05-03 22:09:39 +00002387OptionDefinition
2388CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] =
2389{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002390 { 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."},
2391 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002392};
2393
2394#pragma mark CommandObjectTargetModulesDumpSections
2395
2396//----------------------------------------------------------------------
2397// Image section dumping command
2398//----------------------------------------------------------------------
2399
2400class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete
2401{
2402public:
2403 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002404 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2405 "target modules dump sections",
2406 "Dump the sections from one or more target modules.",
2407 //"target modules dump sections [<file1> ...]")
2408 nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002409 {
2410 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002411
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002412 ~CommandObjectTargetModulesDumpSections() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002413
Jim Ingham5a988412012-06-08 21:56:10 +00002414protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002415 bool
2416 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002417 {
2418 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002419 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002420 {
2421 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2422 result.SetStatus (eReturnStatusFailed);
2423 return false;
2424 }
2425 else
2426 {
2427 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002428
Greg Claytoneffe5c92011-05-03 22:09:39 +00002429 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2430 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2431 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002432
Greg Claytoneffe5c92011-05-03 22:09:39 +00002433 if (command.GetArgumentCount() == 0)
2434 {
2435 // Dump all sections for all modules images
Greg Claytonc7bece562013-01-25 18:06:21 +00002436 const size_t num_modules = target->GetImages().GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002437 if (num_modules > 0)
2438 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002439 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytonc7bece562013-01-25 18:06:21 +00002440 for (size_t image_idx = 0; image_idx<num_modules; ++image_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002441 {
2442 num_dumped++;
2443 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
2444 }
2445 }
2446 else
2447 {
2448 result.AppendError ("the target has no associated executable images");
2449 result.SetStatus (eReturnStatusFailed);
2450 return false;
2451 }
2452 }
2453 else
2454 {
2455 // Dump specified images (by basename or fullpath)
2456 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002457 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002458 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002459 ModuleList module_list;
2460 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2461 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002462 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002463 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002464 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002465 Module *module = module_list.GetModulePointerAtIndex(i);
2466 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002467 {
2468 num_dumped++;
Greg Clayton8ee64382011-11-10 01:18:58 +00002469 DumpModuleSections (m_interpreter, result.GetOutputStream(), module);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002470 }
2471 }
2472 }
2473 else
Greg Clayton8ee64382011-11-10 01:18:58 +00002474 {
2475 // Check the global list
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00002476 std::lock_guard<std::recursive_mutex> guard(Module::GetAllocationModuleCollectionMutex());
Greg Clayton8ee64382011-11-10 01:18:58 +00002477
Greg Claytoneffe5c92011-05-03 22:09:39 +00002478 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
Greg Clayton8ee64382011-11-10 01:18:58 +00002479 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002480 }
2481 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002482
Greg Claytoneffe5c92011-05-03 22:09:39 +00002483 if (num_dumped > 0)
2484 result.SetStatus (eReturnStatusSuccessFinishResult);
2485 else
2486 {
2487 result.AppendError ("no matching executable images found");
2488 result.SetStatus (eReturnStatusFailed);
2489 }
2490 }
2491 return result.Succeeded();
2492 }
2493};
2494
Greg Claytoneffe5c92011-05-03 22:09:39 +00002495#pragma mark CommandObjectTargetModulesDumpSymfile
2496
2497//----------------------------------------------------------------------
2498// Image debug symbol dumping command
2499//----------------------------------------------------------------------
2500
2501class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete
2502{
2503public:
2504 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002505 CommandObjectTargetModulesModuleAutoComplete(interpreter,
2506 "target modules dump symfile",
2507 "Dump the debug symbol file for one or more target modules.",
2508 //"target modules dump symfile [<file1> ...]")
2509 nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002510 {
2511 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002512
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002513 ~CommandObjectTargetModulesDumpSymfile() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002514
Jim Ingham5a988412012-06-08 21:56:10 +00002515protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002516 bool
2517 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002518 {
2519 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002520 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002521 {
2522 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2523 result.SetStatus (eReturnStatusFailed);
2524 return false;
2525 }
2526 else
2527 {
2528 uint32_t num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002529
Greg Claytoneffe5c92011-05-03 22:09:39 +00002530 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2531 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2532 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002533
Greg Claytoneffe5c92011-05-03 22:09:39 +00002534 if (command.GetArgumentCount() == 0)
2535 {
2536 // Dump all sections for all modules images
Enrico Granata17598482012-11-08 02:22:02 +00002537 const ModuleList &target_modules = target->GetImages();
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00002538 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002539 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002540 if (num_modules > 0)
2541 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002542 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002543 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
2544 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00002545 if (DumpModuleSymbolVendor (result.GetOutputStream(), target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002546 num_dumped++;
2547 }
2548 }
2549 else
2550 {
2551 result.AppendError ("the target has no associated executable images");
2552 result.SetStatus (eReturnStatusFailed);
2553 return false;
2554 }
2555 }
2556 else
2557 {
2558 // Dump specified images (by basename or fullpath)
2559 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002560 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002561 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002562 ModuleList module_list;
2563 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true);
2564 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002565 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002566 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002567 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002568 Module *module = module_list.GetModulePointerAtIndex(i);
2569 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002570 {
Greg Clayton8ee64382011-11-10 01:18:58 +00002571 if (DumpModuleSymbolVendor (result.GetOutputStream(), module))
Greg Claytoneffe5c92011-05-03 22:09:39 +00002572 num_dumped++;
2573 }
2574 }
2575 }
2576 else
2577 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
2578 }
2579 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002580
Greg Claytoneffe5c92011-05-03 22:09:39 +00002581 if (num_dumped > 0)
2582 result.SetStatus (eReturnStatusSuccessFinishResult);
2583 else
2584 {
2585 result.AppendError ("no matching executable images found");
2586 result.SetStatus (eReturnStatusFailed);
2587 }
2588 }
2589 return result.Succeeded();
2590 }
2591};
2592
Greg Claytoneffe5c92011-05-03 22:09:39 +00002593#pragma mark CommandObjectTargetModulesDumpLineTable
2594
2595//----------------------------------------------------------------------
2596// Image debug line table dumping command
2597//----------------------------------------------------------------------
2598
2599class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete
2600{
2601public:
2602 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002603 CommandObjectTargetModulesSourceFileAutoComplete(interpreter,
2604 "target modules dump line-table",
2605 "Dump the line table for one or more compilation units.",
2606 nullptr,
2607 eCommandRequiresTarget)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002608 {
2609 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002610
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002611 ~CommandObjectTargetModulesDumpLineTable() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002612
Jim Ingham5a988412012-06-08 21:56:10 +00002613protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002614 bool
2615 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002616 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002617 Target *target = m_exe_ctx.GetTargetPtr();
2618 uint32_t total_num_dumped = 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002619
Greg Claytonf9fc6092013-01-09 19:44:40 +00002620 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2621 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2622 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002623
Greg Claytonf9fc6092013-01-09 19:44:40 +00002624 if (command.GetArgumentCount() == 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002625 {
Dawn Perchik954b40b2016-01-05 19:51:51 +00002626 result.AppendError ("file option must be specified.");
Greg Claytoneffe5c92011-05-03 22:09:39 +00002627 result.SetStatus (eReturnStatusFailed);
Dawn Perchik954b40b2016-01-05 19:51:51 +00002628 return result.Succeeded();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002629 }
2630 else
2631 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002632 // Dump specified images (by basename or fullpath)
2633 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002634 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002635 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002636 FileSpec file_spec(arg_cstr, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002637
Greg Claytonf9fc6092013-01-09 19:44:40 +00002638 const ModuleList &target_modules = target->GetImages();
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00002639 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00002640 const size_t num_modules = target_modules.GetSize();
Greg Claytonf9fc6092013-01-09 19:44:40 +00002641 if (num_modules > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002642 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002643 uint32_t num_dumped = 0;
2644 for (uint32_t i = 0; i<num_modules; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002645 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00002646 if (DumpCompileUnitLineTable (m_interpreter,
2647 result.GetOutputStream(),
2648 target_modules.GetModulePointerAtIndexUnlocked(i),
2649 file_spec,
2650 m_exe_ctx.GetProcessPtr() && m_exe_ctx.GetProcessRef().IsAlive()))
2651 num_dumped++;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002652 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002653 if (num_dumped == 0)
2654 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
2655 else
2656 total_num_dumped += num_dumped;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002657 }
2658 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00002659 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002660
Greg Claytonf9fc6092013-01-09 19:44:40 +00002661 if (total_num_dumped > 0)
2662 result.SetStatus (eReturnStatusSuccessFinishResult);
2663 else
2664 {
2665 result.AppendError ("no source filenames matched any command arguments");
2666 result.SetStatus (eReturnStatusFailed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002667 }
2668 return result.Succeeded();
2669 }
2670};
2671
Greg Claytoneffe5c92011-05-03 22:09:39 +00002672#pragma mark CommandObjectTargetModulesDump
2673
2674//----------------------------------------------------------------------
2675// Dump multi-word command for target modules
2676//----------------------------------------------------------------------
2677
2678class CommandObjectTargetModulesDump : public CommandObjectMultiword
2679{
2680public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00002681 //------------------------------------------------------------------
2682 // Constructors and Destructors
2683 //------------------------------------------------------------------
Adrian McCarthy543725c2016-04-04 21:21:49 +00002684 CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
2685 : CommandObjectMultiword(
Kate Stone7428a182016-07-14 22:03:10 +00002686 interpreter, "target modules dump", "Commands for dumping information about one or more target modules.",
Adrian McCarthy543725c2016-04-04 21:21:49 +00002687 "target modules dump [headers|symtab|sections|symfile|line-table] [<file1> <file2> ...]")
Greg Claytoneffe5c92011-05-03 22:09:39 +00002688 {
Adrian McCarthy543725c2016-04-04 21:21:49 +00002689 LoadSubCommand("objfile", CommandObjectSP(new CommandObjectTargetModulesDumpObjfile(interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002690 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
2691 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
2692 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
2693 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter)));
2694 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002695
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002696 ~CommandObjectTargetModulesDump() override = default;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002697};
2698
Jim Ingham5a988412012-06-08 21:56:10 +00002699class CommandObjectTargetModulesAdd : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00002700{
2701public:
2702 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00002703 CommandObjectParsed (interpreter,
2704 "target modules add",
2705 "Add a new module to the current target's modules.",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002706 "target modules add [<module>]"),
Greg Clayton1c5f1862012-11-30 19:05:35 +00002707 m_option_group (interpreter),
2708 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 +00002709 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002710 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton1c5f1862012-11-30 19:05:35 +00002711 m_option_group.Append (&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002712 m_option_group.Finalize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002713 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002714
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002715 ~CommandObjectTargetModulesAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002716
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002717 Options *
2718 GetOptions () override
Greg Clayton50a24bd2012-11-29 22:16:27 +00002719 {
2720 return &m_option_group;
2721 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002722
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002723 int
Jim Ingham5a988412012-06-08 21:56:10 +00002724 HandleArgumentCompletion (Args &input,
2725 int &cursor_index,
2726 int &cursor_char_position,
2727 OptionElementVector &opt_element_vector,
2728 int match_start_point,
2729 int max_return_elements,
2730 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002731 StringList &matches) override
Jim Ingham5a988412012-06-08 21:56:10 +00002732 {
2733 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
2734 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002735
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002736 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
2737 CommandCompletions::eDiskFileCompletion,
2738 completion_str.c_str(),
2739 match_start_point,
2740 max_return_elements,
2741 nullptr,
2742 word_complete,
2743 matches);
Jim Ingham5a988412012-06-08 21:56:10 +00002744 return matches.GetSize();
2745 }
2746
2747protected:
Greg Clayton50a24bd2012-11-29 22:16:27 +00002748 OptionGroupOptions m_option_group;
2749 OptionGroupUUID m_uuid_option_group;
Greg Clayton1c5f1862012-11-30 19:05:35 +00002750 OptionGroupFile m_symbol_file;
Greg Clayton50a24bd2012-11-29 22:16:27 +00002751
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002752 bool
2753 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002754 {
2755 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002756 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002757 {
2758 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2759 result.SetStatus (eReturnStatusFailed);
2760 return false;
2761 }
2762 else
2763 {
Sean Callananb36c6c02012-12-13 01:39:39 +00002764 bool flush = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002765
Greg Claytoneffe5c92011-05-03 22:09:39 +00002766 const size_t argc = args.GetArgumentCount();
2767 if (argc == 0)
2768 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002769 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2770 {
2771 // We are given a UUID only, go locate the file
2772 ModuleSpec module_spec;
2773 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002774 if (m_symbol_file.GetOptionValue().OptionWasSet())
2775 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002776 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
2777 {
2778 ModuleSP module_sp (target->GetSharedModule (module_spec));
2779 if (module_sp)
2780 {
2781 result.SetStatus (eReturnStatusSuccessFinishResult);
2782 return true;
2783 }
2784 else
2785 {
2786 StreamString strm;
2787 module_spec.GetUUID().Dump (&strm);
2788 if (module_spec.GetFileSpec())
2789 {
2790 if (module_spec.GetSymbolFileSpec())
2791 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002792 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 +00002793 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002794 module_spec.GetFileSpec().GetPath().c_str(),
2795 module_spec.GetSymbolFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002796 }
2797 else
2798 {
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002799 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s with path %s",
Greg Clayton50a24bd2012-11-29 22:16:27 +00002800 strm.GetString().c_str(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002801 module_spec.GetFileSpec().GetPath().c_str());
Greg Clayton50a24bd2012-11-29 22:16:27 +00002802 }
2803 }
2804 else
2805 {
2806 result.AppendErrorWithFormat ("Unable to create the executable or symbol file with UUID %s",
2807 strm.GetString().c_str());
2808 }
2809 result.SetStatus (eReturnStatusFailed);
2810 return false;
2811 }
2812 }
2813 else
2814 {
2815 StreamString strm;
2816 module_spec.GetUUID().Dump (&strm);
2817 result.AppendErrorWithFormat ("Unable to locate the executable or symbol file with UUID %s", strm.GetString().c_str());
2818 result.SetStatus (eReturnStatusFailed);
2819 return false;
2820 }
2821 }
2822 else
2823 {
2824 result.AppendError ("one or more executable image paths must be specified");
2825 result.SetStatus (eReturnStatusFailed);
2826 return false;
2827 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002828 }
2829 else
2830 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002831 for (size_t i = 0; i < argc; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002832 {
2833 const char *path = args.GetArgumentAtIndex(i);
2834 if (path)
2835 {
2836 FileSpec file_spec(path, true);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002837 if (file_spec.Exists())
2838 {
Greg Claytonb9a01b32012-02-26 05:51:37 +00002839 ModuleSpec module_spec (file_spec);
Greg Clayton50a24bd2012-11-29 22:16:27 +00002840 if (m_uuid_option_group.GetOptionValue ().OptionWasSet())
2841 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue ().GetCurrentValue();
Greg Clayton1c5f1862012-11-30 19:05:35 +00002842 if (m_symbol_file.GetOptionValue().OptionWasSet())
2843 module_spec.GetSymbolFileSpec() = m_symbol_file.GetOptionValue().GetCurrentValue();
Jason Molendab019cd92013-09-11 21:25:46 +00002844 if (!module_spec.GetArchitecture().IsValid())
2845 module_spec.GetArchitecture() = target->GetArchitecture();
Greg Clayton50a24bd2012-11-29 22:16:27 +00002846 Error error;
2847 ModuleSP module_sp (target->GetSharedModule (module_spec, &error));
Greg Claytoneffe5c92011-05-03 22:09:39 +00002848 if (!module_sp)
2849 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00002850 const char *error_cstr = error.AsCString();
2851 if (error_cstr)
2852 result.AppendError (error_cstr);
2853 else
2854 result.AppendErrorWithFormat ("unsupported module: %s", path);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002855 result.SetStatus (eReturnStatusFailed);
2856 return false;
2857 }
Sean Callananb36c6c02012-12-13 01:39:39 +00002858 else
2859 {
2860 flush = true;
2861 }
Jason Molenda2f7af6a2011-08-02 23:28:55 +00002862 result.SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002863 }
2864 else
2865 {
2866 char resolved_path[PATH_MAX];
2867 result.SetStatus (eReturnStatusFailed);
2868 if (file_spec.GetPath (resolved_path, sizeof(resolved_path)))
2869 {
2870 if (strcmp (resolved_path, path) != 0)
2871 {
2872 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path);
2873 break;
2874 }
2875 }
2876 result.AppendErrorWithFormat ("invalid module path '%s'\n", path);
2877 break;
2878 }
2879 }
2880 }
2881 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002882
Sean Callananb36c6c02012-12-13 01:39:39 +00002883 if (flush)
2884 {
2885 ProcessSP process = target->GetProcessSP();
2886 if (process)
2887 process->Flush();
2888 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002889 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002890
Greg Claytoneffe5c92011-05-03 22:09:39 +00002891 return result.Succeeded();
2892 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002893};
2894
2895class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete
2896{
2897public:
2898 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) :
2899 CommandObjectTargetModulesModuleAutoComplete (interpreter,
2900 "target modules load",
2901 "Set the load addresses for one or more sections in a target module.",
2902 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"),
2903 m_option_group (interpreter),
Jason Molendac6127dd2014-11-21 02:25:15 +00002904 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 +00002905 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)
2906 {
2907 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2908 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2909 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2910 m_option_group.Finalize();
2911 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002912
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002913 ~CommandObjectTargetModulesLoad() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002914
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002915 Options *
2916 GetOptions () override
Jim Ingham5a988412012-06-08 21:56:10 +00002917 {
2918 return &m_option_group;
2919 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002920
Jim Ingham5a988412012-06-08 21:56:10 +00002921protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00002922 bool
2923 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00002924 {
2925 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00002926 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002927 {
2928 result.AppendError ("invalid target, create a debug target using the 'target create' command");
2929 result.SetStatus (eReturnStatusFailed);
2930 return false;
2931 }
2932 else
2933 {
2934 const size_t argc = args.GetArgumentCount();
Greg Claytonb9a01b32012-02-26 05:51:37 +00002935 ModuleSpec module_spec;
2936 bool search_using_module_spec = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002937 if (m_file_option.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002938 {
2939 search_using_module_spec = true;
Jason Molendac6127dd2014-11-21 02:25:15 +00002940 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2941 const bool use_global_module_list = true;
2942 ModuleList module_list;
2943 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
2944 if (num_matches == 1)
2945 {
2946 module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec();
2947 }
2948 else if (num_matches > 1 )
2949 {
2950 search_using_module_spec = false;
2951 result.AppendErrorWithFormat ("more than 1 module matched by name '%s'\n", arg_cstr);
2952 result.SetStatus (eReturnStatusFailed);
2953 }
2954 else
2955 {
2956 search_using_module_spec = false;
2957 result.AppendErrorWithFormat ("no object file for module '%s'\n", arg_cstr);
2958 result.SetStatus (eReturnStatusFailed);
2959 }
Greg Claytonb9a01b32012-02-26 05:51:37 +00002960 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002961
Greg Claytoneffe5c92011-05-03 22:09:39 +00002962 if (m_uuid_option_group.GetOptionValue().OptionWasSet())
Greg Claytonb9a01b32012-02-26 05:51:37 +00002963 {
2964 search_using_module_spec = true;
2965 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
2966 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00002967
Greg Claytonb9a01b32012-02-26 05:51:37 +00002968 if (search_using_module_spec)
Greg Claytoneffe5c92011-05-03 22:09:39 +00002969 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00002970 ModuleList matching_modules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00002971 const size_t num_matches = target->GetImages().FindModules (module_spec, matching_modules);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002972
2973 char path[PATH_MAX];
2974 if (num_matches == 1)
2975 {
2976 Module *module = matching_modules.GetModulePointerAtIndex(0);
2977 if (module)
2978 {
2979 ObjectFile *objfile = module->GetObjectFile();
2980 if (objfile)
2981 {
Greg Clayton3046e662013-07-10 01:23:25 +00002982 SectionList *section_list = module->GetSectionList();
Greg Claytoneffe5c92011-05-03 22:09:39 +00002983 if (section_list)
2984 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002985 bool changed = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00002986 if (argc == 0)
2987 {
2988 if (m_slide_option.GetOptionValue().OptionWasSet())
2989 {
Greg Clayton741f3f92012-03-27 21:10:07 +00002990 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue();
Greg Clayton751caf62014-02-07 22:54:47 +00002991 const bool slide_is_offset = true;
2992 module->SetLoadAddress (*target, slide, slide_is_offset, changed);
Greg Claytoneffe5c92011-05-03 22:09:39 +00002993 }
2994 else
2995 {
2996 result.AppendError ("one or more section name + load address pair must be specified");
2997 result.SetStatus (eReturnStatusFailed);
2998 return false;
2999 }
3000 }
3001 else
3002 {
3003 if (m_slide_option.GetOptionValue().OptionWasSet())
3004 {
3005 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n");
3006 result.SetStatus (eReturnStatusFailed);
3007 return false;
3008 }
3009
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003010 for (size_t i = 0; i < argc; i += 2)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003011 {
3012 const char *sect_name = args.GetArgumentAtIndex(i);
3013 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1);
3014 if (sect_name && load_addr_cstr)
3015 {
3016 ConstString const_sect_name(sect_name);
3017 bool success = false;
Vince Harron5275aaa2015-01-15 20:08:35 +00003018 addr_t load_addr = StringConvert::ToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003019 if (success)
3020 {
3021 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
3022 if (section_sp)
3023 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003024 if (section_sp->IsThreadSpecific())
3025 {
3026 result.AppendErrorWithFormat ("thread specific sections are not yet supported (section '%s')\n", sect_name);
3027 result.SetStatus (eReturnStatusFailed);
3028 break;
3029 }
3030 else
3031 {
Greg Clayton7820bd12012-07-07 01:24:12 +00003032 if (target->GetSectionLoadList().SetSectionLoadAddress (section_sp, load_addr))
Greg Clayton741f3f92012-03-27 21:10:07 +00003033 changed = true;
Daniel Malead01b2952012-11-29 21:49:15 +00003034 result.AppendMessageWithFormat("section '%s' loaded at 0x%" PRIx64 "\n", sect_name, load_addr);
Greg Clayton741f3f92012-03-27 21:10:07 +00003035 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003036 }
3037 else
3038 {
3039 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name);
3040 result.SetStatus (eReturnStatusFailed);
3041 break;
3042 }
3043 }
3044 else
3045 {
3046 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr);
3047 result.SetStatus (eReturnStatusFailed);
3048 break;
3049 }
3050 }
3051 else
3052 {
3053 if (sect_name)
3054 result.AppendError ("section names must be followed by a load address.\n");
3055 else
3056 result.AppendError ("one or more section name + load address pair must be specified.\n");
3057 result.SetStatus (eReturnStatusFailed);
3058 break;
3059 }
3060 }
3061 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003062
Greg Clayton741f3f92012-03-27 21:10:07 +00003063 if (changed)
Greg Clayton3c947372013-01-29 01:17:09 +00003064 {
Greg Clayton741f3f92012-03-27 21:10:07 +00003065 target->ModulesDidLoad (matching_modules);
Greg Clayton3c947372013-01-29 01:17:09 +00003066 Process *process = m_exe_ctx.GetProcessPtr();
3067 if (process)
3068 process->Flush();
3069 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003070 }
3071 else
3072 {
3073 module->GetFileSpec().GetPath (path, sizeof(path));
3074 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path);
3075 result.SetStatus (eReturnStatusFailed);
3076 }
3077 }
3078 else
3079 {
3080 module->GetFileSpec().GetPath (path, sizeof(path));
3081 result.AppendErrorWithFormat ("no object file for module '%s'\n", path);
3082 result.SetStatus (eReturnStatusFailed);
3083 }
3084 }
3085 else
3086 {
Jim Ingham28eb5712012-10-12 17:34:26 +00003087 FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
3088 if (module_spec_file)
3089 {
3090 module_spec_file->GetPath (path, sizeof(path));
3091 result.AppendErrorWithFormat ("invalid module '%s'.\n", path);
3092 }
3093 else
3094 result.AppendError ("no module spec");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003095 result.SetStatus (eReturnStatusFailed);
3096 }
3097 }
3098 else
3099 {
Jason Molendac16b4af2013-05-03 23:56:12 +00003100 std::string uuid_str;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003101
Greg Claytonb9a01b32012-02-26 05:51:37 +00003102 if (module_spec.GetFileSpec())
3103 module_spec.GetFileSpec().GetPath (path, sizeof(path));
Greg Claytoneffe5c92011-05-03 22:09:39 +00003104 else
3105 path[0] = '\0';
3106
Greg Claytonb9a01b32012-02-26 05:51:37 +00003107 if (module_spec.GetUUIDPtr())
Jason Molendac16b4af2013-05-03 23:56:12 +00003108 uuid_str = module_spec.GetUUID().GetAsString();
Greg Claytoneffe5c92011-05-03 22:09:39 +00003109 if (num_matches > 1)
3110 {
3111 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n",
3112 path[0] ? " file=" : "",
3113 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003114 !uuid_str.empty() ? " uuid=" : "",
3115 uuid_str.c_str());
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003116 for (size_t i = 0; i < num_matches; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003117 {
3118 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path)))
3119 result.AppendMessageWithFormat("%s\n", path);
3120 }
3121 }
3122 else
3123 {
3124 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n",
3125 path[0] ? " file=" : "",
3126 path,
Jason Molendac16b4af2013-05-03 23:56:12 +00003127 !uuid_str.empty() ? " uuid=" : "",
3128 uuid_str.c_str());
Greg Claytoneffe5c92011-05-03 22:09:39 +00003129 }
3130 result.SetStatus (eReturnStatusFailed);
3131 }
3132 }
3133 else
3134 {
3135 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n");
3136 result.SetStatus (eReturnStatusFailed);
3137 return false;
3138 }
3139 }
3140 return result.Succeeded();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003141 }
3142
Greg Claytoneffe5c92011-05-03 22:09:39 +00003143 OptionGroupOptions m_option_group;
3144 OptionGroupUUID m_uuid_option_group;
Jason Molendac6127dd2014-11-21 02:25:15 +00003145 OptionGroupString m_file_option;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003146 OptionGroupUInt64 m_slide_option;
3147};
3148
3149//----------------------------------------------------------------------
3150// List images with associated information
3151//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003152class CommandObjectTargetModulesList : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003153{
3154public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003155 class CommandOptions : public Options
3156 {
3157 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003158 CommandOptions (CommandInterpreter &interpreter) :
Greg Clayton65a03992011-08-09 00:01:09 +00003159 Options(interpreter),
Jim Inghamc10312c2011-10-24 18:36:33 +00003160 m_format_array(),
Daniel Dunbara08823f2011-10-31 22:50:49 +00003161 m_use_global_module_list (false),
Jim Inghamc10312c2011-10-24 18:36:33 +00003162 m_module_addr (LLDB_INVALID_ADDRESS)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003163 {
3164 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003165
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003166 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003167
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003168 Error
3169 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003170 {
Greg Claytonb9d5df52012-12-06 22:49:16 +00003171 Error error;
3172
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003173 const int short_option = m_getopt_table[option_idx].val;
Greg Clayton65a03992011-08-09 00:01:09 +00003174 if (short_option == 'g')
3175 {
3176 m_use_global_module_list = true;
3177 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003178 else if (short_option == 'a')
3179 {
Jim Inghame7b849e2013-03-15 23:09:19 +00003180 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3181 m_module_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jim Inghamc10312c2011-10-24 18:36:33 +00003182 }
Greg Clayton65a03992011-08-09 00:01:09 +00003183 else
3184 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003185 unsigned long width = 0;
Greg Clayton65a03992011-08-09 00:01:09 +00003186 if (option_arg)
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003187 width = strtoul(option_arg, nullptr, 0);
Greg Clayton65a03992011-08-09 00:01:09 +00003188 m_format_array.push_back(std::make_pair(short_option, width));
3189 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003190 return error;
3191 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003192
Greg Claytoneffe5c92011-05-03 22:09:39 +00003193 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003194 OptionParsingStarting () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003195 {
3196 m_format_array.clear();
Greg Clayton65a03992011-08-09 00:01:09 +00003197 m_use_global_module_list = false;
Jim Inghamc10312c2011-10-24 18:36:33 +00003198 m_module_addr = LLDB_INVALID_ADDRESS;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003199 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003200
Greg Claytoneffe5c92011-05-03 22:09:39 +00003201 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003202 GetDefinitions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003203 {
3204 return g_option_table;
3205 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003206
Greg Claytoneffe5c92011-05-03 22:09:39 +00003207 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003208
Greg Claytoneffe5c92011-05-03 22:09:39 +00003209 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003210
Greg Claytoneffe5c92011-05-03 22:09:39 +00003211 // Instance variables to hold the values for command options.
3212 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
3213 FormatWidthCollection m_format_array;
Greg Clayton65a03992011-08-09 00:01:09 +00003214 bool m_use_global_module_list;
Jim Inghamc10312c2011-10-24 18:36:33 +00003215 lldb::addr_t m_module_addr;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003216 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003217
Greg Claytoneffe5c92011-05-03 22:09:39 +00003218 CommandObjectTargetModulesList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00003219 CommandObjectParsed (interpreter,
3220 "target modules list",
3221 "List current executable and dependent shared library images.",
3222 "target modules list [<cmd-options>]"),
Greg Claytoneffe5c92011-05-03 22:09:39 +00003223 m_options (interpreter)
3224 {
3225 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003226
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003227 ~CommandObjectTargetModulesList() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003228
Greg Claytoneffe5c92011-05-03 22:09:39 +00003229 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003230 GetOptions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003231 {
3232 return &m_options;
3233 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003234
Jim Ingham5a988412012-06-08 21:56:10 +00003235protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003236 bool
3237 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003238 {
3239 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Greg Clayton3418c852011-08-10 02:10:13 +00003240 const bool use_global_module_list = m_options.m_use_global_module_list;
Greg Clayton234076c2012-06-27 20:26:19 +00003241 // Define a local module list here to ensure it lives longer than any "locker"
3242 // object which might lock its contents below (through the "module_list_ptr"
3243 // variable).
3244 ModuleList module_list;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003245 if (target == nullptr && !use_global_module_list)
Greg Claytoneffe5c92011-05-03 22:09:39 +00003246 {
3247 result.AppendError ("invalid target, create a debug target using the 'target create' command");
3248 result.SetStatus (eReturnStatusFailed);
3249 return false;
3250 }
3251 else
3252 {
Greg Clayton3418c852011-08-10 02:10:13 +00003253 if (target)
3254 {
3255 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3256 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3257 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3258 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003259 // Dump all sections for all modules images
Jim Inghamc10312c2011-10-24 18:36:33 +00003260 Stream &strm = result.GetOutputStream();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003261
Jim Inghamc10312c2011-10-24 18:36:33 +00003262 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS)
3263 {
3264 if (target)
3265 {
3266 Address module_address;
3267 if (module_address.SetLoadAddress(m_options.m_module_addr, target))
3268 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003269 ModuleSP module_sp (module_address.GetModule());
3270 if (module_sp)
Jim Inghamc10312c2011-10-24 18:36:33 +00003271 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003272 PrintModule (target, module_sp.get(), 0, strm);
Jim Inghamc10312c2011-10-24 18:36:33 +00003273 result.SetStatus (eReturnStatusSuccessFinishResult);
3274 }
3275 else
3276 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003277 result.AppendErrorWithFormat ("Couldn't find module matching address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003278 result.SetStatus (eReturnStatusFailed);
3279 }
3280 }
3281 else
3282 {
Jim Ingham17fafa12012-12-15 02:40:54 +00003283 result.AppendErrorWithFormat ("Couldn't find module containing address: 0x%" PRIx64 ".", m_options.m_module_addr);
Jim Inghamc10312c2011-10-24 18:36:33 +00003284 result.SetStatus (eReturnStatusFailed);
3285 }
3286 }
3287 else
3288 {
3289 result.AppendError ("Can only look up modules by address with a valid target.");
3290 result.SetStatus (eReturnStatusFailed);
3291 }
3292 return result.Succeeded();
3293 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003294
Greg Claytonc7bece562013-01-25 18:06:21 +00003295 size_t num_modules = 0;
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00003296
3297 // This locker will be locked on the mutex in module_list_ptr if it is non-nullptr.
3298 // Otherwise it will lock the AllocationModuleCollectionMutex when accessing
3299 // the global module list directly.
3300 std::unique_lock<std::recursive_mutex> guard(Module::GetAllocationModuleCollectionMutex(), std::defer_lock);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00003301
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003302 const ModuleList *module_list_ptr = nullptr;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003303 const size_t argc = command.GetArgumentCount();
3304 if (argc == 0)
Greg Clayton65a03992011-08-09 00:01:09 +00003305 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003306 if (use_global_module_list)
3307 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00003308 guard.lock();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003309 num_modules = Module::GetNumberAllocatedModules();
3310 }
3311 else
3312 {
3313 module_list_ptr = &target->GetImages();
Greg Claytonc4a8a762012-05-15 18:43:44 +00003314 }
Greg Clayton65a03992011-08-09 00:01:09 +00003315 }
3316 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003317 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003318 for (size_t i = 0; i < argc; ++i)
Greg Claytonc4a8a762012-05-15 18:43:44 +00003319 {
3320 // Dump specified images (by basename or fullpath)
3321 const char *arg_cstr = command.GetArgumentAtIndex(i);
3322 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, use_global_module_list);
3323 if (num_matches == 0)
3324 {
3325 if (argc == 1)
3326 {
3327 result.AppendErrorWithFormat ("no modules found that match '%s'", arg_cstr);
3328 result.SetStatus (eReturnStatusFailed);
3329 return false;
3330 }
3331 }
3332 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003333
Greg Claytonc4a8a762012-05-15 18:43:44 +00003334 module_list_ptr = &module_list;
3335 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003336
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00003337 std::unique_lock<std::recursive_mutex> lock;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003338 if (module_list_ptr != nullptr)
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003339 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00003340 lock = std::unique_lock<std::recursive_mutex>(module_list_ptr->GetMutex());
3341
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003342 num_modules = module_list_ptr->GetSize();
3343 }
Greg Clayton65a03992011-08-09 00:01:09 +00003344
Greg Claytoneffe5c92011-05-03 22:09:39 +00003345 if (num_modules > 0)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003346 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00003347 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
3348 {
Greg Clayton3418c852011-08-10 02:10:13 +00003349 ModuleSP module_sp;
Greg Clayton65a03992011-08-09 00:01:09 +00003350 Module *module;
Greg Claytonc4a8a762012-05-15 18:43:44 +00003351 if (module_list_ptr)
Greg Clayton65a03992011-08-09 00:01:09 +00003352 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00003353 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
Greg Claytonc4a8a762012-05-15 18:43:44 +00003354 module = module_sp.get();
Greg Clayton65a03992011-08-09 00:01:09 +00003355 }
3356 else
3357 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003358 module = Module::GetAllocatedModuleAtIndex(image_idx);
3359 module_sp = module->shared_from_this();
Greg Clayton65a03992011-08-09 00:01:09 +00003360 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003361
Greg Claytonc7bece562013-01-25 18:06:21 +00003362 const size_t indent = strm.Printf("[%3u] ", image_idx);
3363 PrintModule (target, module, indent, strm);
Greg Clayton3418c852011-08-10 02:10:13 +00003364
Greg Claytoneffe5c92011-05-03 22:09:39 +00003365 }
3366 result.SetStatus (eReturnStatusSuccessFinishResult);
3367 }
3368 else
3369 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00003370 if (argc)
3371 {
3372 if (use_global_module_list)
3373 result.AppendError ("the global module list has no matching modules");
3374 else
3375 result.AppendError ("the target has no matching modules");
3376 }
Greg Clayton3418c852011-08-10 02:10:13 +00003377 else
Greg Claytonc4a8a762012-05-15 18:43:44 +00003378 {
3379 if (use_global_module_list)
3380 result.AppendError ("the global module list is empty");
3381 else
3382 result.AppendError ("the target has no associated executable images");
3383 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003384 result.SetStatus (eReturnStatusFailed);
3385 return false;
3386 }
3387 }
3388 return result.Succeeded();
3389 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003390
3391 void
Greg Claytonc7bece562013-01-25 18:06:21 +00003392 PrintModule (Target *target, Module *module, int indent, Stream &strm)
Jim Inghamc10312c2011-10-24 18:36:33 +00003393 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003394 if (module == nullptr)
Jim Ingham28eb5712012-10-12 17:34:26 +00003395 {
3396 strm.PutCString("Null module");
3397 return;
3398 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003399
Jim Inghamc10312c2011-10-24 18:36:33 +00003400 bool dump_object_name = false;
3401 if (m_options.m_format_array.empty())
3402 {
Greg Claytonc9660542012-02-05 02:38:54 +00003403 m_options.m_format_array.push_back(std::make_pair('u', 0));
3404 m_options.m_format_array.push_back(std::make_pair('h', 0));
3405 m_options.m_format_array.push_back(std::make_pair('f', 0));
3406 m_options.m_format_array.push_back(std::make_pair('S', 0));
Jim Inghamc10312c2011-10-24 18:36:33 +00003407 }
Greg Claytonc9660542012-02-05 02:38:54 +00003408 const size_t num_entries = m_options.m_format_array.size();
3409 bool print_space = false;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003410 for (size_t i = 0; i < num_entries; ++i)
Jim Inghamc10312c2011-10-24 18:36:33 +00003411 {
Greg Claytonc9660542012-02-05 02:38:54 +00003412 if (print_space)
3413 strm.PutChar(' ');
3414 print_space = true;
3415 const char format_char = m_options.m_format_array[i].first;
3416 uint32_t width = m_options.m_format_array[i].second;
3417 switch (format_char)
Jim Inghamc10312c2011-10-24 18:36:33 +00003418 {
Greg Claytonc9660542012-02-05 02:38:54 +00003419 case 'A':
3420 DumpModuleArchitecture (strm, module, false, width);
3421 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003422
Greg Claytonc9660542012-02-05 02:38:54 +00003423 case 't':
3424 DumpModuleArchitecture (strm, module, true, width);
3425 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003426
Greg Claytonc9660542012-02-05 02:38:54 +00003427 case 'f':
3428 DumpFullpath (strm, &module->GetFileSpec(), width);
3429 dump_object_name = true;
3430 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003431
Greg Claytonc9660542012-02-05 02:38:54 +00003432 case 'd':
3433 DumpDirectory (strm, &module->GetFileSpec(), width);
3434 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003435
Greg Claytonc9660542012-02-05 02:38:54 +00003436 case 'b':
3437 DumpBasename (strm, &module->GetFileSpec(), width);
3438 dump_object_name = true;
3439 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003440
Greg Claytonc9660542012-02-05 02:38:54 +00003441 case 'h':
3442 case 'o':
3443 // Image header address
3444 {
3445 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16;
Jim Inghamc10312c2011-10-24 18:36:33 +00003446
Greg Claytonc9660542012-02-05 02:38:54 +00003447 ObjectFile *objfile = module->GetObjectFile ();
3448 if (objfile)
Jim Inghamc10312c2011-10-24 18:36:33 +00003449 {
Greg Claytonc9660542012-02-05 02:38:54 +00003450 Address header_addr(objfile->GetHeaderAddress());
3451 if (header_addr.IsValid())
Jim Inghamc10312c2011-10-24 18:36:33 +00003452 {
Greg Claytonc9660542012-02-05 02:38:54 +00003453 if (target && !target->GetSectionLoadList().IsEmpty())
Jim Inghamc10312c2011-10-24 18:36:33 +00003454 {
Greg Claytonc9660542012-02-05 02:38:54 +00003455 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target);
3456 if (header_load_addr == LLDB_INVALID_ADDRESS)
3457 {
3458 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress);
3459 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003460 else
Greg Claytonc9660542012-02-05 02:38:54 +00003461 {
3462 if (format_char == 'o')
3463 {
3464 // Show the offset of slide for the image
Daniel Malead01b2952012-11-29 21:49:15 +00003465 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress());
Greg Claytonc9660542012-02-05 02:38:54 +00003466 }
3467 else
3468 {
3469 // Show the load address of the image
Daniel Malead01b2952012-11-29 21:49:15 +00003470 strm.Printf ("0x%*.*" PRIx64, addr_nibble_width, addr_nibble_width, header_load_addr);
Greg Claytonc9660542012-02-05 02:38:54 +00003471 }
3472 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003473 break;
3474 }
Greg Claytonc9660542012-02-05 02:38:54 +00003475 // The address was valid, but the image isn't loaded, output the address in an appropriate format
3476 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress);
3477 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003478 }
Jim Inghamc10312c2011-10-24 18:36:33 +00003479 }
Greg Claytonc9660542012-02-05 02:38:54 +00003480 strm.Printf ("%*s", addr_nibble_width + 2, "");
3481 }
3482 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003483
Greg Claytonc9660542012-02-05 02:38:54 +00003484 case 'r':
3485 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003486 size_t ref_count = 0;
Greg Claytonc9660542012-02-05 02:38:54 +00003487 ModuleSP module_sp (module->shared_from_this());
3488 if (module_sp)
3489 {
3490 // Take one away to make sure we don't count our local "module_sp"
3491 ref_count = module_sp.use_count() - 1;
3492 }
3493 if (width)
Greg Clayton6fea17e2014-03-03 19:15:20 +00003494 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003495 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00003496 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
Greg Claytonc9660542012-02-05 02:38:54 +00003497 }
3498 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003499
Greg Claytonc9660542012-02-05 02:38:54 +00003500 case 's':
3501 case 'S':
3502 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003503 const SymbolVendor *symbol_vendor = module->GetSymbolVendor();
Greg Claytonc9660542012-02-05 02:38:54 +00003504 if (symbol_vendor)
3505 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003506 const FileSpec symfile_spec = symbol_vendor->GetMainFileSpec();
3507 if (format_char == 'S')
Greg Claytonc9660542012-02-05 02:38:54 +00003508 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003509 // Dump symbol file only if different from module file
3510 if (!symfile_spec || symfile_spec == module->GetFileSpec())
Greg Claytonc9660542012-02-05 02:38:54 +00003511 {
Ilia Ke912e3e2015-03-10 21:18:59 +00003512 print_space = false;
3513 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003514 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003515 // Add a newline and indent past the index
3516 strm.Printf ("\n%*s", indent, "");
Greg Claytonc9660542012-02-05 02:38:54 +00003517 }
Ilia Ke912e3e2015-03-10 21:18:59 +00003518 DumpFullpath (strm, &symfile_spec, width);
3519 dump_object_name = true;
3520 break;
Greg Claytonc9660542012-02-05 02:38:54 +00003521 }
3522 strm.Printf("%.*s", width, "<NONE>");
3523 }
3524 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003525
Greg Claytonc9660542012-02-05 02:38:54 +00003526 case 'm':
3527 module->GetModificationTime().Dump(&strm, width);
3528 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003529
Greg Claytonc9660542012-02-05 02:38:54 +00003530 case 'p':
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003531 strm.Printf("%p", static_cast<void*>(module));
Greg Claytonc9660542012-02-05 02:38:54 +00003532 break;
3533
3534 case 'u':
3535 DumpModuleUUID(strm, module);
3536 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003537
Greg Claytonc9660542012-02-05 02:38:54 +00003538 default:
3539 break;
Jim Inghamc10312c2011-10-24 18:36:33 +00003540 }
Greg Claytonc9660542012-02-05 02:38:54 +00003541 }
3542 if (dump_object_name)
3543 {
3544 const char *object_name = module->GetObjectName().GetCString();
3545 if (object_name)
3546 strm.Printf ("(%s)", object_name);
Jim Inghamc10312c2011-10-24 18:36:33 +00003547 }
3548 strm.EOL();
3549 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003550
Greg Claytoneffe5c92011-05-03 22:09:39 +00003551 CommandOptions m_options;
3552};
3553
3554OptionDefinition
3555CommandObjectTargetModulesList::CommandOptions::g_option_table[] =
3556{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003557 { LLDB_OPT_SET_1, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Display the image at this address."},
3558 { LLDB_OPT_SET_1, false, "arch", 'A', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the architecture when listing images."},
3559 { LLDB_OPT_SET_1, false, "triple", 't', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the triple when listing images."},
3560 { 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."},
3561 { 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)."},
3562 { LLDB_OPT_SET_1, false, "uuid", 'u', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the UUID when listing images."},
3563 { LLDB_OPT_SET_1, false, "fullpath", 'f', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
3564 { LLDB_OPT_SET_1, false, "directory", 'd', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
3565 { LLDB_OPT_SET_1, false, "basename", 'b', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
3566 { LLDB_OPT_SET_1, false, "symfile", 's', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
3567 { 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."},
3568 { LLDB_OPT_SET_1, false, "mod-time", 'm', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeWidth, "Display the modification time with optional width of the module."},
3569 { 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."},
3570 { LLDB_OPT_SET_1, false, "pointer", 'p', OptionParser::eOptionalArgument, nullptr, nullptr, 0, eArgTypeNone, "Display the module pointer."},
3571 { 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."},
3572 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003573};
3574
Jason Molenda380241a2012-07-12 00:20:07 +00003575#pragma mark CommandObjectTargetModulesShowUnwind
Greg Claytoneffe5c92011-05-03 22:09:39 +00003576
Jason Molenda380241a2012-07-12 00:20:07 +00003577//----------------------------------------------------------------------
3578// Lookup unwind information in images
3579//----------------------------------------------------------------------
3580
3581class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed
3582{
3583public:
Jason Molenda380241a2012-07-12 00:20:07 +00003584 enum
3585 {
3586 eLookupTypeInvalid = -1,
3587 eLookupTypeAddress = 0,
3588 eLookupTypeSymbol,
3589 eLookupTypeFunction,
3590 eLookupTypeFunctionOrSymbol,
3591 kNumLookupTypes
3592 };
3593
3594 class CommandOptions : public Options
3595 {
3596 public:
Jason Molenda380241a2012-07-12 00:20:07 +00003597 CommandOptions (CommandInterpreter &interpreter) :
Greg Claytonf9fc6092013-01-09 19:44:40 +00003598 Options(interpreter),
3599 m_type(eLookupTypeInvalid),
3600 m_str(),
3601 m_addr(LLDB_INVALID_ADDRESS)
Jason Molenda380241a2012-07-12 00:20:07 +00003602 {
3603 }
3604
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003605 ~CommandOptions() override = default;
Jason Molenda380241a2012-07-12 00:20:07 +00003606
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003607 Error
3608 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jason Molenda380241a2012-07-12 00:20:07 +00003609 {
3610 Error error;
3611
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003612 const int short_option = m_getopt_table[option_idx].val;
Jason Molenda380241a2012-07-12 00:20:07 +00003613
3614 switch (short_option)
3615 {
3616 case 'a':
Jason Molenda535ab862013-04-23 04:30:57 +00003617 {
3618 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
Michael Sartainb1e15922013-08-22 20:42:30 +00003619 m_str = option_arg;
Jason Molenda380241a2012-07-12 00:20:07 +00003620 m_type = eLookupTypeAddress;
Jason Molenda535ab862013-04-23 04:30:57 +00003621 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
Jason Molenda380241a2012-07-12 00:20:07 +00003622 if (m_addr == LLDB_INVALID_ADDRESS)
3623 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg);
3624 break;
Jason Molenda535ab862013-04-23 04:30:57 +00003625 }
Jason Molenda380241a2012-07-12 00:20:07 +00003626
3627 case 'n':
3628 m_str = option_arg;
3629 m_type = eLookupTypeFunctionOrSymbol;
3630 break;
Michael Sartainb1e15922013-08-22 20:42:30 +00003631
3632 default:
3633 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
3634 break;
Jason Molenda380241a2012-07-12 00:20:07 +00003635 }
3636
3637 return error;
3638 }
3639
3640 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003641 OptionParsingStarting () override
Jason Molenda380241a2012-07-12 00:20:07 +00003642 {
3643 m_type = eLookupTypeInvalid;
3644 m_str.clear();
3645 m_addr = LLDB_INVALID_ADDRESS;
3646 }
3647
3648 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003649 GetDefinitions () override
Jason Molenda380241a2012-07-12 00:20:07 +00003650 {
3651 return g_option_table;
3652 }
3653
3654 // Options table: Required for subclasses of Options.
3655
3656 static OptionDefinition g_option_table[];
3657
3658 // Instance variables to hold the values for command options.
3659
3660 int m_type; // Should be a eLookupTypeXXX enum after parsing options
3661 std::string m_str; // Holds name lookup
3662 lldb::addr_t m_addr; // Holds the address to lookup
3663 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003664
Jason Molenda380241a2012-07-12 00:20:07 +00003665 CommandObjectTargetModulesShowUnwind (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003666 CommandObjectParsed(interpreter,
3667 "target modules show-unwind",
3668 "Show synthesized unwind instructions for a function.",
3669 nullptr,
3670 eCommandRequiresTarget |
3671 eCommandRequiresProcess |
3672 eCommandProcessMustBeLaunched |
3673 eCommandProcessMustBePaused ),
Jason Molenda380241a2012-07-12 00:20:07 +00003674 m_options (interpreter)
3675 {
3676 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003677
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003678 ~CommandObjectTargetModulesShowUnwind() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003679
Jason Molenda380241a2012-07-12 00:20:07 +00003680 Options *
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003681 GetOptions () override
Jason Molenda380241a2012-07-12 00:20:07 +00003682 {
3683 return &m_options;
3684 }
3685
3686protected:
3687 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003688 DoExecute (Args& command, CommandReturnObject &result) override
Jason Molenda380241a2012-07-12 00:20:07 +00003689 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00003690 Target *target = m_exe_ctx.GetTargetPtr();
3691 Process *process = m_exe_ctx.GetProcessPtr();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003692 ABI *abi = nullptr;
Jason Molenda380241a2012-07-12 00:20:07 +00003693 if (process)
3694 abi = process->GetABI().get();
3695
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003696 if (process == nullptr)
Jason Molenda380241a2012-07-12 00:20:07 +00003697 {
3698 result.AppendError ("You must have a process running to use this command.");
3699 result.SetStatus (eReturnStatusFailed);
3700 return false;
3701 }
3702
3703 ThreadList threads(process->GetThreadList());
3704 if (threads.GetSize() == 0)
3705 {
3706 result.AppendError ("The process must be paused to use this command.");
3707 result.SetStatus (eReturnStatusFailed);
3708 return false;
3709 }
3710
3711 ThreadSP thread(threads.GetThreadAtIndex(0));
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003712 if (!thread)
Jason Molenda380241a2012-07-12 00:20:07 +00003713 {
3714 result.AppendError ("The process must be paused to use this command.");
3715 result.SetStatus (eReturnStatusFailed);
3716 return false;
3717 }
3718
Jason Molenda535ab862013-04-23 04:30:57 +00003719 SymbolContextList sc_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003720
Jason Molenda380241a2012-07-12 00:20:07 +00003721 if (m_options.m_type == eLookupTypeFunctionOrSymbol)
3722 {
Jason Molenda380241a2012-07-12 00:20:07 +00003723 ConstString function_name (m_options.m_str.c_str());
Jason Molenda535ab862013-04-23 04:30:57 +00003724 target->GetImages().FindFunctions (function_name, eFunctionNameTypeAuto, true, false, true, sc_list);
3725 }
3726 else if (m_options.m_type == eLookupTypeAddress && target)
3727 {
3728 Address addr;
3729 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.m_addr, addr))
Jason Molenda380241a2012-07-12 00:20:07 +00003730 {
3731 SymbolContext sc;
Jason Molenda535ab862013-04-23 04:30:57 +00003732 ModuleSP module_sp (addr.GetModule());
3733 module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextEverything, sc);
3734 if (sc.function || sc.symbol)
Jason Molenda380241a2012-07-12 00:20:07 +00003735 {
Jason Molenda535ab862013-04-23 04:30:57 +00003736 sc_list.Append(sc);
Jason Molenda380241a2012-07-12 00:20:07 +00003737 }
Jason Molenda535ab862013-04-23 04:30:57 +00003738 }
3739 }
Michael Sartainb1e15922013-08-22 20:42:30 +00003740 else
3741 {
3742 result.AppendError ("address-expression or function name option must be specified.");
3743 result.SetStatus (eReturnStatusFailed);
3744 return false;
3745 }
Jason Molenda380241a2012-07-12 00:20:07 +00003746
Jason Molenda535ab862013-04-23 04:30:57 +00003747 size_t num_matches = sc_list.GetSize();
Michael Sartainb1e15922013-08-22 20:42:30 +00003748 if (num_matches == 0)
3749 {
3750 result.AppendErrorWithFormat ("no unwind data found that matches '%s'.", m_options.m_str.c_str());
3751 result.SetStatus (eReturnStatusFailed);
3752 return false;
3753 }
3754
Jason Molenda535ab862013-04-23 04:30:57 +00003755 for (uint32_t idx = 0; idx < num_matches; idx++)
3756 {
3757 SymbolContext sc;
3758 sc_list.GetContextAtIndex(idx, sc);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003759 if (sc.symbol == nullptr && sc.function == nullptr)
Jason Molenda535ab862013-04-23 04:30:57 +00003760 continue;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003761 if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
Jason Molenda535ab862013-04-23 04:30:57 +00003762 continue;
3763 AddressRange range;
3764 if (!sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range))
3765 continue;
3766 if (!range.GetBaseAddress().IsValid())
3767 continue;
3768 ConstString funcname(sc.GetFunctionName());
3769 if (funcname.IsEmpty())
3770 continue;
3771 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3772 if (abi)
3773 start_addr = abi->FixCodeAddress(start_addr);
Jason Molenda380241a2012-07-12 00:20:07 +00003774
Jason Molenda535ab862013-04-23 04:30:57 +00003775 FuncUnwindersSP func_unwinders_sp (sc.module_sp->GetObjectFile()->GetUnwindTable().GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003776 if (!func_unwinders_sp)
Jason Molenda535ab862013-04-23 04:30:57 +00003777 continue;
Jason Molenda380241a2012-07-12 00:20:07 +00003778
Jason Molenda34549b82015-01-13 06:04:04 +00003779 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 +00003780
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003781 UnwindPlanSP non_callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread, -1);
3782 if (non_callsite_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003783 {
Jason Molenda34549b82015-01-13 06:04:04 +00003784 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 +00003785 }
Jason Molendae589e7e2014-12-08 03:09:00 +00003786 UnwindPlanSP callsite_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(*target, -1);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003787 if (callsite_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003788 {
Jason Molenda34549b82015-01-13 06:04:04 +00003789 result.GetOutputStream().Printf("Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", callsite_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003790 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003791 UnwindPlanSP fast_unwind_plan = func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread);
3792 if (fast_unwind_plan)
Jason Molenda535ab862013-04-23 04:30:57 +00003793 {
Jason Molenda34549b82015-01-13 06:04:04 +00003794 result.GetOutputStream().Printf("Fast UnwindPlan is '%s'\n", fast_unwind_plan->GetSourceName().AsCString());
Jason Molenda535ab862013-04-23 04:30:57 +00003795 }
3796
Jason Molenda34549b82015-01-13 06:04:04 +00003797 result.GetOutputStream().Printf("\n");
3798
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003799 UnwindPlanSP assembly_sp = func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread, 0);
Jason Molenda34549b82015-01-13 06:04:04 +00003800 if (assembly_sp)
3801 {
3802 result.GetOutputStream().Printf("Assembly language inspection UnwindPlan:\n");
3803 assembly_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3804 result.GetOutputStream().Printf("\n");
3805 }
Jason Molenda34549b82015-01-13 06:04:04 +00003806
3807 UnwindPlanSP ehframe_sp = func_unwinders_sp->GetEHFrameUnwindPlan(*target, 0);
3808 if (ehframe_sp)
3809 {
3810 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3811 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3812 result.GetOutputStream().Printf("\n");
3813 }
3814
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003815 UnwindPlanSP ehframe_augmented_sp = func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread, 0);
Jason Molenda34549b82015-01-13 06:04:04 +00003816 if (ehframe_augmented_sp)
3817 {
3818 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3819 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3820 result.GetOutputStream().Printf("\n");
3821 }
3822
Tamas Berghammer648f3c72015-09-30 13:50:14 +00003823 UnwindPlanSP arm_unwind_sp = func_unwinders_sp->GetArmUnwindUnwindPlan(*target, 0);
3824 if (arm_unwind_sp)
3825 {
3826 result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n");
3827 arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3828 result.GetOutputStream().Printf("\n");
3829 }
3830
Jason Molenda34549b82015-01-13 06:04:04 +00003831 UnwindPlanSP compact_unwind_sp = func_unwinders_sp->GetCompactUnwindUnwindPlan(*target, 0);
3832 if (compact_unwind_sp)
3833 {
3834 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3835 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3836 result.GetOutputStream().Printf("\n");
3837 }
3838
3839 if (fast_unwind_plan)
3840 {
3841 result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3842 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3843 result.GetOutputStream().Printf("\n");
3844 }
3845
3846 ABISP abi_sp = process->GetABI();
3847 if (abi_sp)
3848 {
3849 UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3850 if (abi_sp->CreateDefaultUnwindPlan (arch_default))
3851 {
3852 result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3853 arch_default.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3854 result.GetOutputStream().Printf("\n");
3855 }
3856
3857 UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3858 if (abi_sp->CreateFunctionEntryUnwindPlan (arch_entry))
3859 {
3860 result.GetOutputStream().Printf("Arch default at entry point UnwindPlan:\n");
3861 arch_entry.Dump(result.GetOutputStream(), thread.get(), LLDB_INVALID_ADDRESS);
3862 result.GetOutputStream().Printf("\n");
3863 }
3864 }
Jason Molenda535ab862013-04-23 04:30:57 +00003865
3866 result.GetOutputStream().Printf ("\n");
Jason Molenda380241a2012-07-12 00:20:07 +00003867 }
3868 return result.Succeeded();
3869 }
3870
3871 CommandOptions m_options;
3872};
3873
3874OptionDefinition
3875CommandObjectTargetModulesShowUnwind::CommandOptions::g_option_table[] =
3876{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003877 { LLDB_OPT_SET_1, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFunctionName, "Show unwind instructions for a function or symbol name."},
3878 { LLDB_OPT_SET_2, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Show unwind instructions for a function or symbol containing an address"},
3879 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jason Molenda380241a2012-07-12 00:20:07 +00003880};
Greg Claytoneffe5c92011-05-03 22:09:39 +00003881
3882//----------------------------------------------------------------------
3883// Lookup information in images
3884//----------------------------------------------------------------------
Jim Ingham5a988412012-06-08 21:56:10 +00003885class CommandObjectTargetModulesLookup : public CommandObjectParsed
Greg Claytoneffe5c92011-05-03 22:09:39 +00003886{
3887public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003888 enum
3889 {
3890 eLookupTypeInvalid = -1,
3891 eLookupTypeAddress = 0,
3892 eLookupTypeSymbol,
3893 eLookupTypeFileLine, // Line is optional
3894 eLookupTypeFunction,
Greg Claytonc4a8a762012-05-15 18:43:44 +00003895 eLookupTypeFunctionOrSymbol,
Greg Claytoneffe5c92011-05-03 22:09:39 +00003896 eLookupTypeType,
3897 kNumLookupTypes
3898 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003899
Greg Claytoneffe5c92011-05-03 22:09:39 +00003900 class CommandOptions : public Options
3901 {
3902 public:
Greg Claytoneffe5c92011-05-03 22:09:39 +00003903 CommandOptions (CommandInterpreter &interpreter) :
3904 Options(interpreter)
3905 {
3906 OptionParsingStarting();
3907 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003908
Eugene Zelenkof13e6522016-02-25 19:02:39 +00003909 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003910
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003911 Error
3912 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003913 {
3914 Error error;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003915
Greg Clayton3bcdfc02012-12-04 00:32:51 +00003916 const int short_option = m_getopt_table[option_idx].val;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003917
Greg Claytoneffe5c92011-05-03 22:09:39 +00003918 switch (short_option)
3919 {
3920 case 'a':
Jim Inghame7b849e2013-03-15 23:09:19 +00003921 {
3922 m_type = eLookupTypeAddress;
3923 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
3924 m_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
3925 }
Greg Claytoneffe5c92011-05-03 22:09:39 +00003926 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003927
Greg Claytoneffe5c92011-05-03 22:09:39 +00003928 case 'o':
Vince Harron5275aaa2015-01-15 20:08:35 +00003929 m_offset = StringConvert::ToUInt64(option_arg, LLDB_INVALID_ADDRESS);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003930 if (m_offset == LLDB_INVALID_ADDRESS)
Greg Clayton86edbf42011-10-26 00:56:27 +00003931 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003932 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003933
Greg Claytoneffe5c92011-05-03 22:09:39 +00003934 case 's':
3935 m_str = option_arg;
3936 m_type = eLookupTypeSymbol;
3937 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003938
Greg Claytoneffe5c92011-05-03 22:09:39 +00003939 case 'f':
3940 m_file.SetFile (option_arg, false);
3941 m_type = eLookupTypeFileLine;
3942 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003943
Greg Claytoneffe5c92011-05-03 22:09:39 +00003944 case 'i':
Sean Callanand4a7c122012-02-11 01:22:21 +00003945 m_include_inlines = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003946 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003947
Greg Claytoneffe5c92011-05-03 22:09:39 +00003948 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00003949 m_line_number = StringConvert::ToUInt32(option_arg, UINT32_MAX);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003950 if (m_line_number == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00003951 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg);
Greg Claytoneffe5c92011-05-03 22:09:39 +00003952 else if (m_line_number == 0)
Greg Clayton86edbf42011-10-26 00:56:27 +00003953 error.SetErrorString ("zero is an invalid line number");
Greg Claytoneffe5c92011-05-03 22:09:39 +00003954 m_type = eLookupTypeFileLine;
3955 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003956
Greg Claytonc4a8a762012-05-15 18:43:44 +00003957 case 'F':
Greg Claytoneffe5c92011-05-03 22:09:39 +00003958 m_str = option_arg;
3959 m_type = eLookupTypeFunction;
3960 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003961
Greg Claytonc4a8a762012-05-15 18:43:44 +00003962 case 'n':
3963 m_str = option_arg;
3964 m_type = eLookupTypeFunctionOrSymbol;
3965 break;
3966
Greg Claytoneffe5c92011-05-03 22:09:39 +00003967 case 't':
3968 m_str = option_arg;
3969 m_type = eLookupTypeType;
3970 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003971
Greg Claytoneffe5c92011-05-03 22:09:39 +00003972 case 'v':
3973 m_verbose = 1;
3974 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003975
Sean Callanand38b4a92012-06-06 20:49:55 +00003976 case 'A':
3977 m_print_all = true;
3978 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003979
Greg Claytoneffe5c92011-05-03 22:09:39 +00003980 case 'r':
3981 m_use_regex = true;
3982 break;
3983 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003984
Greg Claytoneffe5c92011-05-03 22:09:39 +00003985 return error;
3986 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003987
Greg Claytoneffe5c92011-05-03 22:09:39 +00003988 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00003989 OptionParsingStarting () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00003990 {
3991 m_type = eLookupTypeInvalid;
3992 m_str.clear();
3993 m_file.Clear();
3994 m_addr = LLDB_INVALID_ADDRESS;
3995 m_offset = 0;
3996 m_line_number = 0;
3997 m_use_regex = false;
Sean Callanand4a7c122012-02-11 01:22:21 +00003998 m_include_inlines = true;
Greg Claytoneffe5c92011-05-03 22:09:39 +00003999 m_verbose = false;
Sean Callanand38b4a92012-06-06 20:49:55 +00004000 m_print_all = false;
Greg Claytoneffe5c92011-05-03 22:09:39 +00004001 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004002
Greg Claytoneffe5c92011-05-03 22:09:39 +00004003 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004004 GetDefinitions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00004005 {
4006 return g_option_table;
4007 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004008
Greg Claytoneffe5c92011-05-03 22:09:39 +00004009 // Options table: Required for subclasses of Options.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004010
Greg Claytoneffe5c92011-05-03 22:09:39 +00004011 static OptionDefinition g_option_table[];
4012 int m_type; // Should be a eLookupTypeXXX enum after parsing options
4013 std::string m_str; // Holds name lookup
4014 FileSpec m_file; // Files for file lookups
4015 lldb::addr_t m_addr; // Holds the address to lookup
4016 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
4017 uint32_t m_line_number; // Line number for file+line lookups
4018 bool m_use_regex; // Name lookups in m_str are regular expressions.
Sean Callanand4a7c122012-02-11 01:22:21 +00004019 bool m_include_inlines;// Check for inline entries when looking up by file/line.
Greg Claytoneffe5c92011-05-03 22:09:39 +00004020 bool m_verbose; // Enable verbose lookup info
Sean Callanand38b4a92012-06-06 20:49:55 +00004021 bool m_print_all; // Print all matches, even in cases where there's a best match.
Greg Claytoneffe5c92011-05-03 22:09:39 +00004022 };
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004023
Greg Claytoneffe5c92011-05-03 22:09:39 +00004024 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) :
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004025 CommandObjectParsed(interpreter,
4026 "target modules lookup",
4027 "Look up information within executable and dependent shared library images.",
4028 nullptr,
4029 eCommandRequiresTarget),
Jim Ingham5a988412012-06-08 21:56:10 +00004030 m_options (interpreter)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004031 {
4032 CommandArgumentEntry arg;
4033 CommandArgumentData file_arg;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004034
Greg Claytoneffe5c92011-05-03 22:09:39 +00004035 // Define the first (and only) variant of this arg.
4036 file_arg.arg_type = eArgTypeFilename;
4037 file_arg.arg_repetition = eArgRepeatStar;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004038
Greg Claytoneffe5c92011-05-03 22:09:39 +00004039 // There is only one variant this argument could be; put it into the argument entry.
4040 arg.push_back (file_arg);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004041
Greg Claytoneffe5c92011-05-03 22:09:39 +00004042 // Push the data for the first argument into the m_arguments vector.
4043 m_arguments.push_back (arg);
4044 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004045
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004046 ~CommandObjectTargetModulesLookup() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004047
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004048 Options *
4049 GetOptions () override
Greg Claytoneffe5c92011-05-03 22:09:39 +00004050 {
4051 return &m_options;
4052 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004053
Sean Callanand38b4a92012-06-06 20:49:55 +00004054 bool
4055 LookupHere (CommandInterpreter &interpreter, CommandReturnObject &result, bool &syntax_error)
4056 {
4057 switch (m_options.m_type)
4058 {
4059 case eLookupTypeAddress:
4060 case eLookupTypeFileLine:
4061 case eLookupTypeFunction:
4062 case eLookupTypeFunctionOrSymbol:
4063 case eLookupTypeSymbol:
4064 default:
4065 return false;
4066 case eLookupTypeType:
4067 break;
4068 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004069
Jason Molendab57e4a12013-11-04 09:33:30 +00004070 StackFrameSP frame = m_exe_ctx.GetFrameSP();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004071
Sean Callanand38b4a92012-06-06 20:49:55 +00004072 if (!frame)
4073 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004074
Sean Callanand38b4a92012-06-06 20:49:55 +00004075 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004076
Sean Callanand38b4a92012-06-06 20:49:55 +00004077 if (!sym_ctx.module_sp)
4078 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004079
Sean Callanand38b4a92012-06-06 20:49:55 +00004080 switch (m_options.m_type)
4081 {
4082 default:
4083 return false;
4084 case eLookupTypeType:
4085 if (!m_options.m_str.empty())
4086 {
4087 if (LookupTypeHere (m_interpreter,
4088 result.GetOutputStream(),
4089 sym_ctx,
4090 m_options.m_str.c_str(),
4091 m_options.m_use_regex))
4092 {
4093 result.SetStatus(eReturnStatusSuccessFinishResult);
4094 return true;
4095 }
4096 }
4097 break;
4098 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004099
Sean Callanand38b4a92012-06-06 20:49:55 +00004100 return true;
4101 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004102
Greg Claytoneffe5c92011-05-03 22:09:39 +00004103 bool
4104 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
4105 {
4106 switch (m_options.m_type)
4107 {
4108 case eLookupTypeAddress:
4109 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
4110 {
Saleem Abdulrasoolc98d9692016-01-22 20:26:34 +00004111 if (LookupAddressInModule (m_interpreter,
4112 result.GetOutputStream(),
4113 module,
4114 eSymbolContextEverything | (m_options.m_verbose ? static_cast<int>(eSymbolContextVariable) : 0),
4115 m_options.m_addr,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004116 m_options.m_offset,
4117 m_options.m_verbose))
4118 {
4119 result.SetStatus(eReturnStatusSuccessFinishResult);
4120 return true;
4121 }
4122 }
4123 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004124
Greg Claytoneffe5c92011-05-03 22:09:39 +00004125 case eLookupTypeSymbol:
4126 if (!m_options.m_str.empty())
4127 {
Greg Claytonc4a8a762012-05-15 18:43:44 +00004128 if (LookupSymbolInModule (m_interpreter,
4129 result.GetOutputStream(),
4130 module,
4131 m_options.m_str.c_str(),
4132 m_options.m_use_regex,
4133 m_options.m_verbose))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004134 {
4135 result.SetStatus(eReturnStatusSuccessFinishResult);
4136 return true;
4137 }
4138 }
4139 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004140
Greg Claytoneffe5c92011-05-03 22:09:39 +00004141 case eLookupTypeFileLine:
4142 if (m_options.m_file)
4143 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004144 if (LookupFileAndLineInModule (m_interpreter,
4145 result.GetOutputStream(),
4146 module,
4147 m_options.m_file,
4148 m_options.m_line_number,
Sean Callanand4a7c122012-02-11 01:22:21 +00004149 m_options.m_include_inlines,
Greg Claytoneffe5c92011-05-03 22:09:39 +00004150 m_options.m_verbose))
4151 {
4152 result.SetStatus(eReturnStatusSuccessFinishResult);
4153 return true;
4154 }
4155 }
4156 break;
Greg Claytonc4a8a762012-05-15 18:43:44 +00004157
4158 case eLookupTypeFunctionOrSymbol:
Greg Claytoneffe5c92011-05-03 22:09:39 +00004159 case eLookupTypeFunction:
4160 if (!m_options.m_str.empty())
4161 {
4162 if (LookupFunctionInModule (m_interpreter,
4163 result.GetOutputStream(),
4164 module,
4165 m_options.m_str.c_str(),
4166 m_options.m_use_regex,
Sean Callanand4a7c122012-02-11 01:22:21 +00004167 m_options.m_include_inlines,
Greg Claytonc4a8a762012-05-15 18:43:44 +00004168 m_options.m_type == eLookupTypeFunctionOrSymbol, // include symbols
Greg Claytoneffe5c92011-05-03 22:09:39 +00004169 m_options.m_verbose))
4170 {
4171 result.SetStatus(eReturnStatusSuccessFinishResult);
4172 return true;
4173 }
4174 }
4175 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004176
Greg Claytoneffe5c92011-05-03 22:09:39 +00004177 case eLookupTypeType:
4178 if (!m_options.m_str.empty())
4179 {
4180 if (LookupTypeInModule (m_interpreter,
4181 result.GetOutputStream(),
4182 module,
4183 m_options.m_str.c_str(),
4184 m_options.m_use_regex))
4185 {
4186 result.SetStatus(eReturnStatusSuccessFinishResult);
4187 return true;
4188 }
4189 }
4190 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004191
Greg Claytoneffe5c92011-05-03 22:09:39 +00004192 default:
4193 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
4194 syntax_error = true;
4195 break;
4196 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004197
Greg Claytoneffe5c92011-05-03 22:09:39 +00004198 result.SetStatus (eReturnStatusFailed);
4199 return false;
4200 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004201
Jim Ingham5a988412012-06-08 21:56:10 +00004202protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004203 bool
4204 DoExecute (Args& command, CommandReturnObject &result) override
Greg Claytoneffe5c92011-05-03 22:09:39 +00004205 {
4206 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004207 if (target == nullptr)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004208 {
4209 result.AppendError ("invalid target, create a debug target using the 'target create' command");
4210 result.SetStatus (eReturnStatusFailed);
4211 return false;
4212 }
4213 else
4214 {
4215 bool syntax_error = false;
4216 uint32_t i;
4217 uint32_t num_successful_lookups = 0;
4218 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
4219 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
4220 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
4221 // Dump all sections for all modules images
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004222
Greg Claytoneffe5c92011-05-03 22:09:39 +00004223 if (command.GetArgumentCount() == 0)
4224 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004225 ModuleSP current_module;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004226
Sean Callanand38b4a92012-06-06 20:49:55 +00004227 // Where it is possible to look in the current symbol context
4228 // first, try that. If this search was successful and --all
4229 // was not passed, don't print anything else.
4230 if (LookupHere (m_interpreter, result, syntax_error))
4231 {
4232 result.GetOutputStream().EOL();
4233 num_successful_lookups++;
4234 if (!m_options.m_print_all)
4235 {
4236 result.SetStatus (eReturnStatusSuccessFinishResult);
4237 return result.Succeeded();
4238 }
4239 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004240
Sean Callanand38b4a92012-06-06 20:49:55 +00004241 // Dump all sections for all other modules
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004242
Enrico Granata17598482012-11-08 02:22:02 +00004243 const ModuleList &target_modules = target->GetImages();
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00004244 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
Greg Claytonc7bece562013-01-25 18:06:21 +00004245 const size_t num_modules = target_modules.GetSize();
Greg Claytoneffe5c92011-05-03 22:09:39 +00004246 if (num_modules > 0)
4247 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004248 for (i = 0; i < num_modules && !syntax_error; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004249 {
Sean Callanand38b4a92012-06-06 20:49:55 +00004250 Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004251
Sean Callanand38b4a92012-06-06 20:49:55 +00004252 if (module_pointer != current_module.get() &&
4253 LookupInModule (m_interpreter, target_modules.GetModulePointerAtIndexUnlocked(i), result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004254 {
4255 result.GetOutputStream().EOL();
4256 num_successful_lookups++;
4257 }
4258 }
4259 }
4260 else
4261 {
4262 result.AppendError ("the target has no associated executable images");
4263 result.SetStatus (eReturnStatusFailed);
4264 return false;
4265 }
4266 }
4267 else
4268 {
4269 // Dump specified images (by basename or fullpath)
4270 const char *arg_cstr;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004271 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr && !syntax_error; ++i)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004272 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004273 ModuleList module_list;
4274 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false);
4275 if (num_matches > 0)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004276 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004277 for (size_t j=0; j<num_matches; ++j)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004278 {
Jason Molendaccd41e52012-10-04 22:47:07 +00004279 Module *module = module_list.GetModulePointerAtIndex(j);
Greg Clayton8ee64382011-11-10 01:18:58 +00004280 if (module)
Greg Claytoneffe5c92011-05-03 22:09:39 +00004281 {
Greg Clayton8ee64382011-11-10 01:18:58 +00004282 if (LookupInModule (m_interpreter, module, result, syntax_error))
Greg Claytoneffe5c92011-05-03 22:09:39 +00004283 {
4284 result.GetOutputStream().EOL();
4285 num_successful_lookups++;
4286 }
4287 }
4288 }
4289 }
4290 else
4291 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
4292 }
4293 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004294
Greg Claytoneffe5c92011-05-03 22:09:39 +00004295 if (num_successful_lookups > 0)
4296 result.SetStatus (eReturnStatusSuccessFinishResult);
4297 else
4298 result.SetStatus (eReturnStatusFailed);
4299 }
4300 return result.Succeeded();
4301 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004302
Greg Claytoneffe5c92011-05-03 22:09:39 +00004303 CommandOptions m_options;
4304};
4305
4306OptionDefinition
4307CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] =
4308{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004309 { LLDB_OPT_SET_1, true, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Lookup an address in one or more target modules."},
4310 { 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 +00004311 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 | LLDB_OPT_SET_5
4312 /* 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 +00004313 false, "regex", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
4314 { 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."},
4315 { 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."},
4316 { 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 +00004317 { LLDB_OPT_SET_FROM_TO(3,5),
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004318 false, "no-inlines", 'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Ignore inline entries (must be used in conjunction with --file or --function)."},
4319 { 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."},
4320 { 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."},
4321 { 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."},
4322 { LLDB_OPT_SET_ALL, false, "verbose", 'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable verbose lookup information."},
4323 { 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."},
4324 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Greg Claytoneffe5c92011-05-03 22:09:39 +00004325};
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004326
Jim Ingham9575d842011-03-11 03:53:59 +00004327#pragma mark CommandObjectMultiwordImageSearchPaths
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004328
4329//-------------------------------------------------------------------------
4330// CommandObjectMultiwordImageSearchPaths
4331//-------------------------------------------------------------------------
4332
Greg Claytoneffe5c92011-05-03 22:09:39 +00004333class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004334{
4335public:
Kate Stone7428a182016-07-14 22:03:10 +00004336 CommandObjectTargetModulesImageSearchPaths(CommandInterpreter &interpreter)
4337 : CommandObjectMultiword(interpreter, "target modules search-paths",
4338 "Commands for managing module search paths for a target.",
4339 "target modules search-paths <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004340 {
Greg Claytoneffe5c92011-05-03 22:09:39 +00004341 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter)));
4342 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter)));
4343 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter)));
4344 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter)));
4345 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004346 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004347
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004348 ~CommandObjectTargetModulesImageSearchPaths() override = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004349};
4350
Greg Claytoneffe5c92011-05-03 22:09:39 +00004351#pragma mark CommandObjectTargetModules
4352
4353//-------------------------------------------------------------------------
4354// CommandObjectTargetModules
4355//-------------------------------------------------------------------------
4356
4357class CommandObjectTargetModules : public CommandObjectMultiword
4358{
4359public:
4360 //------------------------------------------------------------------
4361 // Constructors and Destructors
4362 //------------------------------------------------------------------
Kate Stone7428a182016-07-14 22:03:10 +00004363 CommandObjectTargetModules(CommandInterpreter &interpreter)
4364 : CommandObjectMultiword(interpreter, "target modules",
4365 "Commands for accessing information for one or more target modules.",
4366 "target modules <sub-command> ...")
Greg Claytoneffe5c92011-05-03 22:09:39 +00004367 {
4368 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter)));
4369 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004370 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter)));
4371 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter)));
4372 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter)));
4373 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter)));
Jason Molenda380241a2012-07-12 00:20:07 +00004374 LoadSubCommand ("show-unwind", CommandObjectSP (new CommandObjectTargetModulesShowUnwind (interpreter)));
Greg Claytoneffe5c92011-05-03 22:09:39 +00004375
4376 }
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004377
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004378 ~CommandObjectTargetModules() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004379
Greg Claytoneffe5c92011-05-03 22:09:39 +00004380private:
4381 //------------------------------------------------------------------
4382 // For CommandObjectTargetModules only
4383 //------------------------------------------------------------------
4384 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules);
4385};
4386
Jim Ingham5a988412012-06-08 21:56:10 +00004387class CommandObjectTargetSymbolsAdd : public CommandObjectParsed
Greg Claytone72dfb32012-02-24 01:59:29 +00004388{
4389public:
4390 CommandObjectTargetSymbolsAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00004391 CommandObjectParsed (interpreter,
4392 "target symbols add",
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004393 "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 +00004394 "target symbols add [<symfile>]", eCommandRequiresTarget),
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004395 m_option_group (interpreter),
4396 m_file_option (LLDB_OPT_SET_1, false, "shlib", 's', CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Fullpath or basename for module to find debug symbols for."),
4397 m_current_frame_option (LLDB_OPT_SET_2, false, "frame", 'F', "Locate the debug symbols the currently selected frame.", false, true)
4398
Greg Claytone72dfb32012-02-24 01:59:29 +00004399 {
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004400 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4401 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4402 m_option_group.Append (&m_current_frame_option, LLDB_OPT_SET_2, LLDB_OPT_SET_2);
4403 m_option_group.Finalize();
Greg Claytone72dfb32012-02-24 01:59:29 +00004404 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004405
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004406 ~CommandObjectTargetSymbolsAdd() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004407
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004408 int
Jim Ingham5a988412012-06-08 21:56:10 +00004409 HandleArgumentCompletion (Args &input,
4410 int &cursor_index,
4411 int &cursor_char_position,
4412 OptionElementVector &opt_element_vector,
4413 int match_start_point,
4414 int max_return_elements,
4415 bool &word_complete,
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004416 StringList &matches) override
Jim Ingham5a988412012-06-08 21:56:10 +00004417 {
4418 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
4419 completion_str.erase (cursor_char_position);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004420
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004421 CommandCompletions::InvokeCommonCompletionCallbacks(m_interpreter,
4422 CommandCompletions::eDiskFileCompletion,
4423 completion_str.c_str(),
4424 match_start_point,
4425 max_return_elements,
4426 nullptr,
4427 word_complete,
4428 matches);
Jim Ingham5a988412012-06-08 21:56:10 +00004429 return matches.GetSize();
4430 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004431
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004432 Options *
4433 GetOptions () override
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004434 {
4435 return &m_option_group;
4436 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004437
Jim Ingham5a988412012-06-08 21:56:10 +00004438protected:
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004439 bool
4440 AddModuleSymbols (Target *target,
Greg Clayton89deb062012-12-12 01:15:30 +00004441 ModuleSpec &module_spec,
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004442 bool &flush,
4443 CommandReturnObject &result)
4444 {
Greg Clayton89deb062012-12-12 01:15:30 +00004445 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4446 if (symbol_fspec)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004447 {
4448 char symfile_path[PATH_MAX];
Greg Clayton89deb062012-12-12 01:15:30 +00004449 symbol_fspec.GetPath (symfile_path, sizeof(symfile_path));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004450
Greg Clayton89deb062012-12-12 01:15:30 +00004451 if (!module_spec.GetUUID().IsValid())
4452 {
4453 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4454 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4455 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004456 // We now have a module that represents a symbol file
4457 // that can be used for a module that might exist in the
4458 // current target, so we need to find that module in the
4459 // target
Greg Clayton89deb062012-12-12 01:15:30 +00004460 ModuleList matching_module_list;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004461
Greg Clayton9aae0a12013-05-15 19:52:08 +00004462 size_t num_matches = 0;
4463 // First extract all module specs from the symbol file
4464 lldb_private::ModuleSpecList symfile_module_specs;
Greg Clayton2540a8a2013-07-12 22:07:46 +00004465 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 0, 0, symfile_module_specs))
Greg Clayton9aae0a12013-05-15 19:52:08 +00004466 {
4467 // Now extract the module spec that matches the target architecture
4468 ModuleSpec target_arch_module_spec;
4469 ModuleSpec symfile_module_spec;
4470 target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4471 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, symfile_module_spec))
4472 {
4473 // See if it has a UUID?
4474 if (symfile_module_spec.GetUUID().IsValid())
4475 {
4476 // It has a UUID, look for this UUID in the target modules
4477 ModuleSpec symfile_uuid_module_spec;
4478 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4479 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
4480 }
4481 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004482
Greg Clayton9aae0a12013-05-15 19:52:08 +00004483 if (num_matches == 0)
4484 {
4485 // No matches yet, iterate through the module specs to find a UUID value that
4486 // we can match up to an image in our target
4487 const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004488 for (size_t i = 0; i < num_symfile_module_specs && num_matches == 0; ++i)
Greg Clayton9aae0a12013-05-15 19:52:08 +00004489 {
4490 if (symfile_module_specs.GetModuleSpecAtIndex(i, symfile_module_spec))
4491 {
4492 if (symfile_module_spec.GetUUID().IsValid())
4493 {
4494 // It has a UUID, look for this UUID in the target modules
4495 ModuleSpec symfile_uuid_module_spec;
4496 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4497 num_matches = target->GetImages().FindModules (symfile_uuid_module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004498 }
Greg Clayton9aae0a12013-05-15 19:52:08 +00004499 }
4500 }
4501 }
4502 }
4503
4504 // Just try to match up the file by basename if we have no matches at this point
4505 if (num_matches == 0)
4506 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004507
Greg Clayton91c0e742013-01-11 23:44:27 +00004508 while (num_matches == 0)
4509 {
4510 ConstString filename_no_extension(module_spec.GetFileSpec().GetFileNameStrippingExtension());
4511 // Empty string returned, lets bail
4512 if (!filename_no_extension)
4513 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004514
Greg Clayton91c0e742013-01-11 23:44:27 +00004515 // Check if there was no extension to strip and the basename is the same
4516 if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4517 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004518
Greg Clayton91c0e742013-01-11 23:44:27 +00004519 // Replace basename with one less extension
4520 module_spec.GetFileSpec().GetFilename() = filename_no_extension;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004521
Greg Clayton91c0e742013-01-11 23:44:27 +00004522 num_matches = target->GetImages().FindModules (module_spec, matching_module_list);
4523 }
4524
Greg Clayton89deb062012-12-12 01:15:30 +00004525 if (num_matches > 1)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004526 {
Greg Clayton89deb062012-12-12 01:15:30 +00004527 result.AppendErrorWithFormat ("multiple modules match symbol file '%s', use the --uuid option to resolve the ambiguity.\n", symfile_path);
4528 }
4529 else if (num_matches == 1)
4530 {
4531 ModuleSP module_sp (matching_module_list.GetModuleAtIndex(0));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004532
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004533 // The module has not yet created its symbol vendor, we can just
4534 // give the existing target module the symfile path to use for
4535 // when it decides to create it!
Greg Clayton89deb062012-12-12 01:15:30 +00004536 module_sp->SetSymbolFileFileSpec (symbol_fspec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004537
Greg Clayton136dff82012-12-14 02:15:00 +00004538 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(true, &result.GetErrorStream());
Greg Clayton89deb062012-12-12 01:15:30 +00004539 if (symbol_vendor)
4540 {
4541 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004542
Greg Clayton89deb062012-12-12 01:15:30 +00004543 if (symbol_file)
4544 {
4545 ObjectFile *object_file = symbol_file->GetObjectFile();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004546
Greg Clayton89deb062012-12-12 01:15:30 +00004547 if (object_file && object_file->GetFileSpec() == symbol_fspec)
4548 {
4549 // Provide feedback that the symfile has been successfully added.
4550 const FileSpec &module_fs = module_sp->GetFileSpec();
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004551 result.AppendMessageWithFormat("symbol file '%s' has been added to '%s'\n",
Greg Clayton89deb062012-12-12 01:15:30 +00004552 symfile_path,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00004553 module_fs.GetPath().c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004554
Greg Clayton89deb062012-12-12 01:15:30 +00004555 // Let clients know something changed in the module
4556 // if it is currently loaded
4557 ModuleList module_list;
4558 module_list.Append (module_sp);
Enrico Granataf15ee4e2013-04-05 18:49:06 +00004559 target->SymbolsDidLoad (module_list);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004560
Greg Clayton91c0e742013-01-11 23:44:27 +00004561 // Make sure we load any scripting resources that may be embedded
4562 // in the debug info files in case the platform supports that.
4563 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00004564 StreamString feedback_stream;
4565 module_sp->LoadScriptingResourceInTarget (target, error,&feedback_stream);
Enrico Granatacaa84cb2013-05-20 22:40:54 +00004566 if (error.Fail() && error.AsCString())
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004567 result.AppendWarningWithFormat("unable to load scripting data for module %s - error reported was %s",
4568 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
4569 error.AsCString());
Enrico Granata97303392013-05-21 00:00:30 +00004570 else if (feedback_stream.GetSize())
4571 result.AppendWarningWithFormat("%s",feedback_stream.GetData());
Greg Clayton91c0e742013-01-11 23:44:27 +00004572
Greg Clayton89deb062012-12-12 01:15:30 +00004573 flush = true;
4574 result.SetStatus (eReturnStatusSuccessFinishResult);
4575 return true;
4576 }
4577 }
4578 }
4579 // Clear the symbol file spec if anything went wrong
4580 module_sp->SetSymbolFileFileSpec (FileSpec());
Greg Clayton89deb062012-12-12 01:15:30 +00004581 }
4582
4583 if (module_spec.GetUUID().IsValid())
4584 {
4585 StreamString ss_symfile_uuid;
4586 module_spec.GetUUID().Dump(&ss_symfile_uuid);
4587 result.AppendErrorWithFormat ("symbol file '%s' (%s) does not match any existing module%s\n",
4588 symfile_path,
4589 ss_symfile_uuid.GetData(),
4590 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4591 ? "\n please specify the full path to the symbol file"
4592 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004593 }
4594 else
4595 {
Greg Clayton89deb062012-12-12 01:15:30 +00004596 result.AppendErrorWithFormat ("symbol file '%s' does not match any existing module%s\n",
4597 symfile_path,
4598 (symbol_fspec.GetFileType() != FileSpec::eFileTypeRegular)
4599 ? "\n please specify the full path to the symbol file"
4600 : "");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004601 }
4602 }
4603 else
4604 {
4605 result.AppendError ("one or more executable image paths must be specified");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004606 }
Greg Clayton89deb062012-12-12 01:15:30 +00004607 result.SetStatus (eReturnStatusFailed);
4608 return false;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004609 }
4610
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004611 bool
4612 DoExecute (Args& args, CommandReturnObject &result) override
Greg Claytone72dfb32012-02-24 01:59:29 +00004613 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004614 Target *target = m_exe_ctx.GetTargetPtr();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004615 result.SetStatus (eReturnStatusFailed);
Greg Claytonf9fc6092013-01-09 19:44:40 +00004616 bool flush = false;
4617 ModuleSpec module_spec;
4618 const bool uuid_option_set = m_uuid_option_group.GetOptionValue().OptionWasSet();
4619 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4620 const bool frame_option_set = m_current_frame_option.GetOptionValue().OptionWasSet();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004621 const size_t argc = args.GetArgumentCount();
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004622
Greg Claytonf9fc6092013-01-09 19:44:40 +00004623 if (argc == 0)
4624 {
4625 if (uuid_option_set || file_option_set || frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004626 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004627 bool success = false;
4628 bool error_set = false;
4629 if (frame_option_set)
Greg Claytone72dfb32012-02-24 01:59:29 +00004630 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004631 Process *process = m_exe_ctx.GetProcessPtr();
4632 if (process)
Greg Claytone72dfb32012-02-24 01:59:29 +00004633 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004634 const StateType process_state = process->GetState();
4635 if (StateIsStoppedState (process_state, true))
Greg Claytone72dfb32012-02-24 01:59:29 +00004636 {
Jason Molendab57e4a12013-11-04 09:33:30 +00004637 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004638 if (frame)
Greg Claytone72dfb32012-02-24 01:59:29 +00004639 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004640 ModuleSP frame_module_sp (frame->GetSymbolContext(eSymbolContextModule).module_sp);
4641 if (frame_module_sp)
Greg Claytone72dfb32012-02-24 01:59:29 +00004642 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004643 if (frame_module_sp->GetPlatformFileSpec().Exists())
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004644 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004645 module_spec.GetArchitecture() = frame_module_sp->GetArchitecture();
4646 module_spec.GetFileSpec() = frame_module_sp->GetPlatformFileSpec();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004647 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004648 module_spec.GetUUID() = frame_module_sp->GetUUID();
4649 success = module_spec.GetUUID().IsValid() || module_spec.GetFileSpec();
Greg Claytone72dfb32012-02-24 01:59:29 +00004650 }
Johnny Chen82e5a262012-08-22 00:18:43 +00004651 else
4652 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004653 result.AppendError ("frame has no module");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004654 error_set = true;
Johnny Chen82e5a262012-08-22 00:18:43 +00004655 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004656 }
4657 else
4658 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004659 result.AppendError ("invalid current frame");
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004660 error_set = true;
Greg Claytone72dfb32012-02-24 01:59:29 +00004661 }
Greg Claytone72dfb32012-02-24 01:59:29 +00004662 }
4663 else
4664 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004665 result.AppendErrorWithFormat ("process is not stopped: %s", StateAsCString(process_state));
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004666 error_set = true;
4667 }
4668 }
4669 else
4670 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004671 result.AppendError ("a process must exist in order to use the --frame option");
4672 error_set = true;
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004673 }
4674 }
4675 else
4676 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004677 if (uuid_option_set)
4678 {
4679 module_spec.GetUUID() = m_uuid_option_group.GetOptionValue().GetCurrentValue();
4680 success |= module_spec.GetUUID().IsValid();
4681 }
4682 else if (file_option_set)
4683 {
4684 module_spec.GetFileSpec() = m_file_option.GetOptionValue().GetCurrentValue();
4685 ModuleSP module_sp (target->GetImages().FindFirstModule(module_spec));
4686 if (module_sp)
4687 {
4688 module_spec.GetFileSpec() = module_sp->GetFileSpec();
4689 module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec();
4690 module_spec.GetUUID() = module_sp->GetUUID();
4691 module_spec.GetArchitecture() = module_sp->GetArchitecture();
4692 }
4693 else
4694 {
4695 module_spec.GetArchitecture() = target->GetArchitecture();
4696 }
Greg Clayton479d5452016-02-25 00:56:47 +00004697 success |= module_spec.GetUUID().IsValid() || module_spec.GetFileSpec().Exists();
Greg Claytonf9fc6092013-01-09 19:44:40 +00004698 }
4699 }
4700
4701 if (success)
4702 {
4703 if (Symbols::DownloadObjectAndSymbolFile (module_spec))
4704 {
4705 if (module_spec.GetSymbolFileSpec())
4706 success = AddModuleSymbols (target, module_spec, flush, result);
4707 }
4708 }
4709
4710 if (!success && !error_set)
4711 {
4712 StreamString error_strm;
4713 if (uuid_option_set)
4714 {
4715 error_strm.PutCString("unable to find debug symbols for UUID ");
4716 module_spec.GetUUID().Dump (&error_strm);
4717 }
4718 else if (file_option_set)
4719 {
4720 error_strm.PutCString("unable to find debug symbols for the executable file ");
4721 error_strm << module_spec.GetFileSpec();
4722 }
4723 else if (frame_option_set)
4724 {
4725 error_strm.PutCString("unable to find debug symbols for the current frame");
4726 }
4727 result.AppendError (error_strm.GetData());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004728 }
4729 }
4730 else
4731 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004732 result.AppendError ("one or more symbol file paths must be specified, or options must be specified");
4733 }
4734 }
4735 else
4736 {
4737 if (uuid_option_set)
4738 {
4739 result.AppendError ("specify either one or more paths to symbol files or use the --uuid option without arguments");
4740 }
4741 else if (file_option_set)
4742 {
4743 result.AppendError ("specify either one or more paths to symbol files or use the --file option without arguments");
4744 }
4745 else if (frame_option_set)
4746 {
4747 result.AppendError ("specify either one or more paths to symbol files or use the --frame option without arguments");
4748 }
4749 else
4750 {
4751 PlatformSP platform_sp (target->GetPlatform());
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004752
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004753 for (size_t i = 0; i < argc; ++i)
Greg Claytonf9fc6092013-01-09 19:44:40 +00004754 {
4755 const char *symfile_path = args.GetArgumentAtIndex(i);
4756 if (symfile_path)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004757 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004758 module_spec.GetSymbolFileSpec().SetFile(symfile_path, true);
4759 if (platform_sp)
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004760 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00004761 FileSpec symfile_spec;
4762 if (platform_sp->ResolveSymbolFile(*target, module_spec, symfile_spec).Success())
4763 module_spec.GetSymbolFileSpec() = symfile_spec;
4764 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004765
Greg Claytonf9fc6092013-01-09 19:44:40 +00004766 ArchSpec arch;
4767 bool symfile_exists = module_spec.GetSymbolFileSpec().Exists();
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004768
Greg Claytonf9fc6092013-01-09 19:44:40 +00004769 if (symfile_exists)
4770 {
4771 if (!AddModuleSymbols (target, module_spec, flush, result))
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004772 break;
Greg Claytonf9fc6092013-01-09 19:44:40 +00004773 }
4774 else
4775 {
4776 char resolved_symfile_path[PATH_MAX];
4777 if (module_spec.GetSymbolFileSpec().GetPath (resolved_symfile_path, sizeof(resolved_symfile_path)))
4778 {
4779 if (strcmp (resolved_symfile_path, symfile_path) != 0)
4780 {
4781 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", symfile_path, resolved_symfile_path);
4782 break;
4783 }
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004784 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004785 result.AppendErrorWithFormat ("invalid module path '%s'\n", symfile_path);
4786 break;
Greg Claytone72dfb32012-02-24 01:59:29 +00004787 }
4788 }
4789 }
4790 }
Greg Claytonf9fc6092013-01-09 19:44:40 +00004791 }
Greg Claytonfa559e52012-05-18 02:38:05 +00004792
Greg Claytonf9fc6092013-01-09 19:44:40 +00004793 if (flush)
4794 {
4795 Process *process = m_exe_ctx.GetProcessPtr();
4796 if (process)
4797 process->Flush();
Greg Claytone72dfb32012-02-24 01:59:29 +00004798 }
4799 return result.Succeeded();
4800 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004801
Greg Claytonb5f0fea2012-09-27 22:26:11 +00004802 OptionGroupOptions m_option_group;
4803 OptionGroupUUID m_uuid_option_group;
4804 OptionGroupFile m_file_option;
4805 OptionGroupBoolean m_current_frame_option;
Greg Claytone72dfb32012-02-24 01:59:29 +00004806};
4807
Greg Claytone72dfb32012-02-24 01:59:29 +00004808#pragma mark CommandObjectTargetSymbols
4809
4810//-------------------------------------------------------------------------
4811// CommandObjectTargetSymbols
4812//-------------------------------------------------------------------------
4813
4814class CommandObjectTargetSymbols : public CommandObjectMultiword
4815{
4816public:
4817 //------------------------------------------------------------------
4818 // Constructors and Destructors
4819 //------------------------------------------------------------------
Kate Stone7428a182016-07-14 22:03:10 +00004820 CommandObjectTargetSymbols(CommandInterpreter &interpreter)
4821 : CommandObjectMultiword(interpreter, "target symbols", "Commands for adding and managing debug symbol files.",
4822 "target symbols <sub-command> ...")
Greg Claytone72dfb32012-02-24 01:59:29 +00004823 {
4824 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetSymbolsAdd (interpreter)));
Greg Claytone72dfb32012-02-24 01:59:29 +00004825 }
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004826
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004827 ~CommandObjectTargetSymbols() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004828
Greg Claytone72dfb32012-02-24 01:59:29 +00004829private:
4830 //------------------------------------------------------------------
4831 // For CommandObjectTargetModules only
4832 //------------------------------------------------------------------
4833 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetSymbols);
4834};
4835
Jim Ingham9575d842011-03-11 03:53:59 +00004836#pragma mark CommandObjectTargetStopHookAdd
4837
4838//-------------------------------------------------------------------------
4839// CommandObjectTargetStopHookAdd
4840//-------------------------------------------------------------------------
4841
Greg Clayton44d93782014-01-27 23:43:24 +00004842class CommandObjectTargetStopHookAdd :
4843 public CommandObjectParsed,
4844 public IOHandlerDelegateMultiline
Jim Ingham9575d842011-03-11 03:53:59 +00004845{
4846public:
Jim Ingham9575d842011-03-11 03:53:59 +00004847 class CommandOptions : public Options
4848 {
4849 public:
Greg Claytoneb0103f2011-04-07 22:46:35 +00004850 CommandOptions (CommandInterpreter &interpreter) :
4851 Options(interpreter),
Jim Ingham9575d842011-03-11 03:53:59 +00004852 m_line_start(0),
4853 m_line_end (UINT_MAX),
4854 m_func_name_type_mask (eFunctionNameTypeAuto),
4855 m_sym_ctx_specified (false),
Johnny Chenb1372c02011-05-02 23:47:55 +00004856 m_thread_specified (false),
4857 m_use_one_liner (false),
4858 m_one_liner()
Jim Ingham9575d842011-03-11 03:53:59 +00004859 {
4860 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004861
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004862 ~CommandOptions() override = default;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004863
Greg Claytone0d378b2011-03-24 21:19:54 +00004864 const OptionDefinition*
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004865 GetDefinitions () override
Jim Ingham9575d842011-03-11 03:53:59 +00004866 {
4867 return g_option_table;
4868 }
4869
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004870 Error
4871 SetOptionValue (uint32_t option_idx, const char *option_arg) override
Jim Ingham9575d842011-03-11 03:53:59 +00004872 {
4873 Error error;
Greg Clayton3bcdfc02012-12-04 00:32:51 +00004874 const int short_option = m_getopt_table[option_idx].val;
Jim Ingham9575d842011-03-11 03:53:59 +00004875 bool success;
4876
4877 switch (short_option)
4878 {
4879 case 'c':
4880 m_class_name = option_arg;
4881 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004882 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004883
Jim Ingham9575d842011-03-11 03:53:59 +00004884 case 'e':
Vince Harron5275aaa2015-01-15 20:08:35 +00004885 m_line_end = StringConvert::ToUInt32 (option_arg, UINT_MAX, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004886 if (!success)
4887 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004888 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004889 break;
4890 }
4891 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004892 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004893
Jim Ingham9575d842011-03-11 03:53:59 +00004894 case 'l':
Vince Harron5275aaa2015-01-15 20:08:35 +00004895 m_line_start = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00004896 if (!success)
4897 {
Greg Clayton86edbf42011-10-26 00:56:27 +00004898 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004899 break;
4900 }
4901 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004902 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004903
Sean Callanand4a7c122012-02-11 01:22:21 +00004904 case 'i':
4905 m_no_inlines = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004906 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004907
Jim Ingham9575d842011-03-11 03:53:59 +00004908 case 'n':
4909 m_function_name = option_arg;
4910 m_func_name_type_mask |= eFunctionNameTypeAuto;
4911 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004912 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004913
Jim Ingham9575d842011-03-11 03:53:59 +00004914 case 'f':
4915 m_file_name = option_arg;
4916 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004917 break;
4918
Jim Ingham9575d842011-03-11 03:53:59 +00004919 case 's':
4920 m_module_name = option_arg;
4921 m_sym_ctx_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004922 break;
4923
Jim Ingham9575d842011-03-11 03:53:59 +00004924 case 't' :
Vince Harron5275aaa2015-01-15 20:08:35 +00004925 m_thread_id = StringConvert::ToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004926 if (m_thread_id == LLDB_INVALID_THREAD_ID)
Greg Clayton86edbf42011-10-26 00:56:27 +00004927 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004928 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004929 break;
4930
Jim Ingham9575d842011-03-11 03:53:59 +00004931 case 'T':
4932 m_thread_name = option_arg;
4933 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004934 break;
4935
Jim Ingham9575d842011-03-11 03:53:59 +00004936 case 'q':
4937 m_queue_name = option_arg;
4938 m_thread_specified = true;
4939 break;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004940
Jim Ingham9575d842011-03-11 03:53:59 +00004941 case 'x':
Vince Harron5275aaa2015-01-15 20:08:35 +00004942 m_thread_index = StringConvert::ToUInt32(option_arg, UINT32_MAX, 0);
Jim Ingham9575d842011-03-11 03:53:59 +00004943 if (m_thread_id == UINT32_MAX)
Greg Clayton86edbf42011-10-26 00:56:27 +00004944 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
Jim Ingham9575d842011-03-11 03:53:59 +00004945 m_thread_specified = true;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004946 break;
4947
Johnny Chenb1372c02011-05-02 23:47:55 +00004948 case 'o':
4949 m_use_one_liner = true;
4950 m_one_liner = option_arg;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004951 break;
4952
Jim Ingham9575d842011-03-11 03:53:59 +00004953 default:
Greg Clayton86edbf42011-10-26 00:56:27 +00004954 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option);
Eugene Zelenkof13e6522016-02-25 19:02:39 +00004955 break;
Jim Ingham9575d842011-03-11 03:53:59 +00004956 }
4957 return error;
4958 }
4959
4960 void
Bruce Mitchener13d21e92015-10-07 16:56:17 +00004961 OptionParsingStarting () override
Jim Ingham9575d842011-03-11 03:53:59 +00004962 {
4963 m_class_name.clear();
4964 m_function_name.clear();
4965 m_line_start = 0;
4966 m_line_end = UINT_MAX;
4967 m_file_name.clear();
4968 m_module_name.clear();
4969 m_func_name_type_mask = eFunctionNameTypeAuto;
4970 m_thread_id = LLDB_INVALID_THREAD_ID;
4971 m_thread_index = UINT32_MAX;
4972 m_thread_name.clear();
4973 m_queue_name.clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004974
Sean Callanand4a7c122012-02-11 01:22:21 +00004975 m_no_inlines = false;
Jim Ingham9575d842011-03-11 03:53:59 +00004976 m_sym_ctx_specified = false;
4977 m_thread_specified = false;
Johnny Chenb1372c02011-05-02 23:47:55 +00004978
4979 m_use_one_liner = false;
4980 m_one_liner.clear();
Jim Ingham9575d842011-03-11 03:53:59 +00004981 }
4982
Greg Claytone0d378b2011-03-24 21:19:54 +00004983 static OptionDefinition g_option_table[];
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004984
Jim Ingham9575d842011-03-11 03:53:59 +00004985 std::string m_class_name;
4986 std::string m_function_name;
4987 uint32_t m_line_start;
4988 uint32_t m_line_end;
4989 std::string m_file_name;
4990 std::string m_module_name;
4991 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4992 lldb::tid_t m_thread_id;
4993 uint32_t m_thread_index;
4994 std::string m_thread_name;
4995 std::string m_queue_name;
4996 bool m_sym_ctx_specified;
Sean Callanand4a7c122012-02-11 01:22:21 +00004997 bool m_no_inlines;
Jim Ingham9575d842011-03-11 03:53:59 +00004998 bool m_thread_specified;
Johnny Chenb1372c02011-05-02 23:47:55 +00004999 // Instance variables to hold the values for one_liner options.
5000 bool m_use_one_liner;
5001 std::string m_one_liner;
Jim Ingham9575d842011-03-11 03:53:59 +00005002 };
5003
Jim Ingham9575d842011-03-11 03:53:59 +00005004 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005005 CommandObjectParsed (interpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00005006 "target stop-hook add",
Jim Ingham5a988412012-06-08 21:56:10 +00005007 "Add a hook to be executed when the target stops.",
5008 "target stop-hook add"),
Greg Claytonc3d874a2014-05-08 16:59:00 +00005009 IOHandlerDelegateMultiline ("DONE", IOHandlerDelegate::Completion::LLDBCommand),
Greg Claytoneb0103f2011-04-07 22:46:35 +00005010 m_options (interpreter)
Jim Ingham9575d842011-03-11 03:53:59 +00005011 {
5012 }
5013
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005014 ~CommandObjectTargetStopHookAdd() override = default;
5015
5016 Options *
5017 GetOptions () override
Jim Ingham9575d842011-03-11 03:53:59 +00005018 {
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005019 return &m_options;
Jim Ingham9575d842011-03-11 03:53:59 +00005020 }
5021
Jim Ingham5a988412012-06-08 21:56:10 +00005022protected:
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005023 void
5024 IOHandlerActivated (IOHandler &io_handler) override
Greg Clayton44d93782014-01-27 23:43:24 +00005025 {
5026 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5027 if (output_sp)
5028 {
5029 output_sp->PutCString("Enter your stop hook command(s). Type 'DONE' to end.\n");
5030 output_sp->Flush();
5031 }
5032 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005033
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005034 void
5035 IOHandlerInputComplete (IOHandler &io_handler, std::string &line) override
Greg Clayton44d93782014-01-27 23:43:24 +00005036 {
5037 if (m_stop_hook_sp)
5038 {
5039 if (line.empty())
5040 {
5041 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5042 if (error_sp)
5043 {
5044 error_sp->Printf("error: stop hook #%" PRIu64 " aborted, no commands.\n", m_stop_hook_sp->GetID());
5045 error_sp->Flush();
5046 }
5047 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
5048 if (target)
5049 target->RemoveStopHookByID(m_stop_hook_sp->GetID());
5050 }
5051 else
5052 {
5053 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
5054 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5055 if (output_sp)
5056 {
5057 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", m_stop_hook_sp->GetID());
5058 output_sp->Flush();
5059 }
5060 }
5061 m_stop_hook_sp.reset();
5062 }
5063 io_handler.SetIsDone(true);
5064 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005065
Jim Ingham9575d842011-03-11 03:53:59 +00005066 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005067 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005068 {
Greg Clayton44d93782014-01-27 23:43:24 +00005069 m_stop_hook_sp.reset();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005070
Jim Ingham893c9322014-11-22 01:42:44 +00005071 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005072 if (target)
5073 {
Greg Clayton44d93782014-01-27 23:43:24 +00005074 Target::StopHookSP new_hook_sp = target->CreateStopHook();
Jim Ingham9575d842011-03-11 03:53:59 +00005075
5076 // First step, make the specifier.
Greg Clayton7b0992d2013-04-18 22:45:39 +00005077 std::unique_ptr<SymbolContextSpecifier> specifier_ap;
Jim Ingham9575d842011-03-11 03:53:59 +00005078 if (m_options.m_sym_ctx_specified)
5079 {
5080 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005081
Jim Ingham9575d842011-03-11 03:53:59 +00005082 if (!m_options.m_module_name.empty())
5083 {
5084 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified);
5085 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005086
Jim Ingham9575d842011-03-11 03:53:59 +00005087 if (!m_options.m_class_name.empty())
5088 {
5089 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified);
5090 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005091
Jim Ingham9575d842011-03-11 03:53:59 +00005092 if (!m_options.m_file_name.empty())
5093 {
5094 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified);
5095 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005096
Jim Ingham9575d842011-03-11 03:53:59 +00005097 if (m_options.m_line_start != 0)
5098 {
5099 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified);
5100 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005101
Jim Ingham9575d842011-03-11 03:53:59 +00005102 if (m_options.m_line_end != UINT_MAX)
5103 {
5104 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
5105 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005106
Jim Ingham9575d842011-03-11 03:53:59 +00005107 if (!m_options.m_function_name.empty())
5108 {
5109 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified);
5110 }
5111 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005112
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005113 if (specifier_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00005114 new_hook_sp->SetSpecifier (specifier_ap.release());
5115
5116 // Next see if any of the thread options have been entered:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005117
Jim Ingham9575d842011-03-11 03:53:59 +00005118 if (m_options.m_thread_specified)
5119 {
5120 ThreadSpec *thread_spec = new ThreadSpec();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005121
Jim Ingham9575d842011-03-11 03:53:59 +00005122 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
5123 {
5124 thread_spec->SetTID (m_options.m_thread_id);
5125 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005126
Jim Ingham9575d842011-03-11 03:53:59 +00005127 if (m_options.m_thread_index != UINT32_MAX)
5128 thread_spec->SetIndex (m_options.m_thread_index);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005129
Jim Ingham9575d842011-03-11 03:53:59 +00005130 if (!m_options.m_thread_name.empty())
5131 thread_spec->SetName (m_options.m_thread_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005132
Jim Ingham9575d842011-03-11 03:53:59 +00005133 if (!m_options.m_queue_name.empty())
5134 thread_spec->SetQueueName (m_options.m_queue_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005135
Jim Ingham9575d842011-03-11 03:53:59 +00005136 new_hook_sp->SetThreadSpecifier (thread_spec);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005137
Jim Ingham9575d842011-03-11 03:53:59 +00005138 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005139 if (m_options.m_use_one_liner)
Jim Ingham9575d842011-03-11 03:53:59 +00005140 {
Johnny Chenb1372c02011-05-02 23:47:55 +00005141 // Use one-liner.
5142 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str());
Daniel Malead01b2952012-11-29 21:49:15 +00005143 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", new_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00005144 }
Johnny Chenb1372c02011-05-02 23:47:55 +00005145 else
Jim Ingham9575d842011-03-11 03:53:59 +00005146 {
Greg Clayton44d93782014-01-27 23:43:24 +00005147 m_stop_hook_sp = new_hook_sp;
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005148 m_interpreter.GetLLDBCommandsFromIOHandler("> ", // Prompt
5149 *this, // IOHandlerDelegate
5150 true, // Run IOHandler in async mode
5151 nullptr); // Baton for the "io_handler" that will be passed back into our IOHandlerDelegate functions
Greg Clayton44d93782014-01-27 23:43:24 +00005152
Jim Ingham9575d842011-03-11 03:53:59 +00005153 }
Jim Ingham9575d842011-03-11 03:53:59 +00005154 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5155 }
5156 else
5157 {
Greg Clayton7260f622011-04-18 08:33:37 +00005158 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005159 result.SetStatus (eReturnStatusFailed);
5160 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005161
Jim Ingham9575d842011-03-11 03:53:59 +00005162 return result.Succeeded();
5163 }
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005164
Jim Ingham9575d842011-03-11 03:53:59 +00005165private:
5166 CommandOptions m_options;
Greg Clayton44d93782014-01-27 23:43:24 +00005167 Target::StopHookSP m_stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00005168};
5169
Greg Claytone0d378b2011-03-24 21:19:54 +00005170OptionDefinition
Jim Ingham9575d842011-03-11 03:53:59 +00005171CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] =
5172{
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005173 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeOneLiner,
Johnny Chenb1372c02011-05-02 23:47:55 +00005174 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005175 { LLDB_OPT_SET_ALL, false, "shlib", 's', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
Jim Ingham9575d842011-03-11 03:53:59 +00005176 "Set the module within which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005177 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex,
Jim Ingham9575d842011-03-11 03:53:59 +00005178 "The stop hook is run only for the thread whose index matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005179 { LLDB_OPT_SET_ALL, false, "thread-id", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadID,
Jim Ingham9575d842011-03-11 03:53:59 +00005180 "The stop hook is run only for the thread whose TID matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005181 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadName,
Jim Ingham9575d842011-03-11 03:53:59 +00005182 "The stop hook is run only for the thread whose thread name matches this argument."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005183 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeQueueName,
Jim Ingham9575d842011-03-11 03:53:59 +00005184 "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 +00005185 { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
Jim Ingham9575d842011-03-11 03:53:59 +00005186 "Specify the source file within which the stop-hook is to be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005187 { LLDB_OPT_SET_1, false, "start-line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005188 "Set the start of the line range for which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005189 { LLDB_OPT_SET_1, false, "end-line", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLineNum,
Jim Ingham9575d842011-03-11 03:53:59 +00005190 "Set the end of the line range for which the stop-hook is to be run."},
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005191 { LLDB_OPT_SET_2, false, "classname", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeClassName,
Jim Ingham9575d842011-03-11 03:53:59 +00005192 "Specify the class within which the stop-hook is to be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005193 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
Jim Ingham9575d842011-03-11 03:53:59 +00005194 "Set the function name within which the stop hook will be run." },
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005195 { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
Jim Ingham9575d842011-03-11 03:53:59 +00005196};
5197
5198#pragma mark CommandObjectTargetStopHookDelete
5199
5200//-------------------------------------------------------------------------
5201// CommandObjectTargetStopHookDelete
5202//-------------------------------------------------------------------------
5203
Jim Ingham5a988412012-06-08 21:56:10 +00005204class CommandObjectTargetStopHookDelete : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005205{
5206public:
Jim Ingham9575d842011-03-11 03:53:59 +00005207 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005208 CommandObjectParsed (interpreter,
5209 "target stop-hook delete",
5210 "Delete a stop-hook.",
5211 "target stop-hook delete [<idx>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005212 {
5213 }
5214
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005215 ~CommandObjectTargetStopHookDelete() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005216
Jim Ingham5a988412012-06-08 21:56:10 +00005217protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005218 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005219 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005220 {
Jim Ingham893c9322014-11-22 01:42:44 +00005221 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005222 if (target)
5223 {
5224 // FIXME: see if we can use the breakpoint id style parser?
5225 size_t num_args = command.GetArgumentCount();
5226 if (num_args == 0)
5227 {
5228 if (!m_interpreter.Confirm ("Delete all stop hooks?", true))
5229 {
5230 result.SetStatus (eReturnStatusFailed);
5231 return false;
5232 }
5233 else
5234 {
5235 target->RemoveAllStopHooks();
5236 }
5237 }
5238 else
5239 {
5240 bool success;
5241 for (size_t i = 0; i < num_args; i++)
5242 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005243 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005244 if (!success)
5245 {
Greg Clayton7260f622011-04-18 08:33:37 +00005246 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005247 result.SetStatus(eReturnStatusFailed);
5248 return false;
5249 }
5250 success = target->RemoveStopHookByID (user_id);
5251 if (!success)
5252 {
Greg Clayton7260f622011-04-18 08:33:37 +00005253 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005254 result.SetStatus(eReturnStatusFailed);
5255 return false;
5256 }
5257 }
5258 }
5259 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5260 }
5261 else
5262 {
Greg Clayton7260f622011-04-18 08:33:37 +00005263 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005264 result.SetStatus (eReturnStatusFailed);
5265 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005266
Jim Ingham9575d842011-03-11 03:53:59 +00005267 return result.Succeeded();
5268 }
5269};
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005270
Jim Ingham9575d842011-03-11 03:53:59 +00005271#pragma mark CommandObjectTargetStopHookEnableDisable
5272
5273//-------------------------------------------------------------------------
5274// CommandObjectTargetStopHookEnableDisable
5275//-------------------------------------------------------------------------
5276
Jim Ingham5a988412012-06-08 21:56:10 +00005277class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005278{
5279public:
Jim Ingham9575d842011-03-11 03:53:59 +00005280 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) :
Jim Ingham5a988412012-06-08 21:56:10 +00005281 CommandObjectParsed (interpreter,
5282 name,
5283 help,
5284 syntax),
Jim Ingham9575d842011-03-11 03:53:59 +00005285 m_enable (enable)
5286 {
5287 }
5288
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005289 ~CommandObjectTargetStopHookEnableDisable() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005290
Jim Ingham5a988412012-06-08 21:56:10 +00005291protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005292 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005293 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005294 {
Jim Ingham893c9322014-11-22 01:42:44 +00005295 Target *target = GetSelectedOrDummyTarget();
Jim Ingham9575d842011-03-11 03:53:59 +00005296 if (target)
5297 {
5298 // FIXME: see if we can use the breakpoint id style parser?
5299 size_t num_args = command.GetArgumentCount();
5300 bool success;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005301
Jim Ingham9575d842011-03-11 03:53:59 +00005302 if (num_args == 0)
5303 {
5304 target->SetAllStopHooksActiveState (m_enable);
5305 }
5306 else
5307 {
5308 for (size_t i = 0; i < num_args; i++)
5309 {
Vince Harron5275aaa2015-01-15 20:08:35 +00005310 lldb::user_id_t user_id = StringConvert::ToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success);
Jim Ingham9575d842011-03-11 03:53:59 +00005311 if (!success)
5312 {
Greg Clayton7260f622011-04-18 08:33:37 +00005313 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005314 result.SetStatus(eReturnStatusFailed);
5315 return false;
5316 }
5317 success = target->SetStopHookActiveStateByID (user_id, m_enable);
5318 if (!success)
5319 {
Greg Clayton7260f622011-04-18 08:33:37 +00005320 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i));
Jim Ingham9575d842011-03-11 03:53:59 +00005321 result.SetStatus(eReturnStatusFailed);
5322 return false;
5323 }
5324 }
5325 }
5326 result.SetStatus (eReturnStatusSuccessFinishNoResult);
5327 }
5328 else
5329 {
Greg Clayton7260f622011-04-18 08:33:37 +00005330 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005331 result.SetStatus (eReturnStatusFailed);
5332 }
5333 return result.Succeeded();
5334 }
5335private:
5336 bool m_enable;
5337};
5338
5339#pragma mark CommandObjectTargetStopHookList
5340
5341//-------------------------------------------------------------------------
5342// CommandObjectTargetStopHookList
5343//-------------------------------------------------------------------------
5344
Jim Ingham5a988412012-06-08 21:56:10 +00005345class CommandObjectTargetStopHookList : public CommandObjectParsed
Jim Ingham9575d842011-03-11 03:53:59 +00005346{
5347public:
Jim Ingham9575d842011-03-11 03:53:59 +00005348 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +00005349 CommandObjectParsed (interpreter,
5350 "target stop-hook list",
5351 "List all stop-hooks.",
5352 "target stop-hook list [<type>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005353 {
5354 }
5355
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005356 ~CommandObjectTargetStopHookList() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005357
Jim Ingham5a988412012-06-08 21:56:10 +00005358protected:
Jim Ingham9575d842011-03-11 03:53:59 +00005359 bool
Bruce Mitchener13d21e92015-10-07 16:56:17 +00005360 DoExecute (Args& command, CommandReturnObject &result) override
Jim Ingham9575d842011-03-11 03:53:59 +00005361 {
Jim Ingham893c9322014-11-22 01:42:44 +00005362 Target *target = GetSelectedOrDummyTarget();
Johnny Chenfaa5c132011-11-29 23:56:14 +00005363 if (!target)
Jim Ingham9575d842011-03-11 03:53:59 +00005364 {
Greg Clayton7260f622011-04-18 08:33:37 +00005365 result.AppendError ("invalid target\n");
Jim Ingham9575d842011-03-11 03:53:59 +00005366 result.SetStatus (eReturnStatusFailed);
Jason Molendaeffcd2a2011-09-23 21:15:42 +00005367 return result.Succeeded();
Jim Ingham9575d842011-03-11 03:53:59 +00005368 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005369
Jim Ingham9575d842011-03-11 03:53:59 +00005370 size_t num_hooks = target->GetNumStopHooks ();
5371 if (num_hooks == 0)
5372 {
5373 result.GetOutputStream().PutCString ("No stop hooks.\n");
5374 }
5375 else
5376 {
5377 for (size_t i = 0; i < num_hooks; i++)
5378 {
5379 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i);
5380 if (i > 0)
5381 result.GetOutputStream().PutCString ("\n");
5382 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull);
5383 }
5384 }
Johnny Chend0cff1e2011-11-30 19:09:20 +00005385 result.SetStatus (eReturnStatusSuccessFinishResult);
Jim Ingham9575d842011-03-11 03:53:59 +00005386 return result.Succeeded();
5387 }
5388};
5389
5390#pragma mark CommandObjectMultiwordTargetStopHooks
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005391
Jim Ingham9575d842011-03-11 03:53:59 +00005392//-------------------------------------------------------------------------
5393// CommandObjectMultiwordTargetStopHooks
5394//-------------------------------------------------------------------------
5395
5396class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword
5397{
5398public:
Kate Stone7428a182016-07-14 22:03:10 +00005399 CommandObjectMultiwordTargetStopHooks(CommandInterpreter &interpreter)
5400 : CommandObjectMultiword(interpreter, "target stop-hook",
5401 "Commands for operating on debugger target stop-hooks.",
5402 "target stop-hook <subcommand> [<subcommand-options>]")
Jim Ingham9575d842011-03-11 03:53:59 +00005403 {
5404 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter)));
5405 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter)));
5406 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5407 false,
5408 "target stop-hook disable [<id>]",
5409 "Disable a stop-hook.",
5410 "target stop-hook disable")));
5411 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter,
5412 true,
5413 "target stop-hook enable [<id>]",
5414 "Enable a stop-hook.",
5415 "target stop-hook enable")));
5416 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter)));
5417 }
5418
Eugene Zelenkof13e6522016-02-25 19:02:39 +00005419 ~CommandObjectMultiwordTargetStopHooks() override = default;
Jim Ingham9575d842011-03-11 03:53:59 +00005420};
5421
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005422#pragma mark CommandObjectMultiwordTarget
5423
5424//-------------------------------------------------------------------------
5425// CommandObjectMultiwordTarget
5426//-------------------------------------------------------------------------
5427
Kate Stone7428a182016-07-14 22:03:10 +00005428CommandObjectMultiwordTarget::CommandObjectMultiwordTarget(CommandInterpreter &interpreter)
5429 : CommandObjectMultiword(interpreter, "target", "Commands for operating on debugger targets.",
5430 "target <subcommand> [<subcommand-options>]")
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005431{
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;