blob: f8a52aece1b352faebd740f7790be7e9a94afb6b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectImage.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 "CommandObjectImage.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton63094e02010-06-23 01:19:29 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/FileSpec.h"
Greg Clayton63094e02010-06-23 01:19:29 +000018#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/RegularExpression.h"
20#include "lldb/Core/Stream.h"
Greg Clayton63094e02010-06-23 01:19:29 +000021#include "lldb/Interpreter/Args.h"
22#include "lldb/Interpreter/Options.h"
23#include "lldb/Interpreter/CommandCompletions.h"
24#include "lldb/Interpreter/CommandInterpreter.h"
25#include "lldb/Interpreter/CommandReturnObject.h"
26#include "lldb/Symbol/LineTable.h"
27#include "lldb/Symbol/ObjectFile.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Symbol/SymbolFile.h"
29#include "lldb/Symbol/SymbolVendor.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// Static Helper functions
38//----------------------------------------------------------------------
39static void
40DumpModuleArchitecture (Stream &strm, Module *module, uint32_t width)
41{
42 if (module)
43 {
44 if (width)
45 strm.Printf("%-*s", width, module->GetArchitecture().AsCString());
46 else
47 strm.PutCString(module->GetArchitecture().AsCString());
48 }
49}
50
51static void
52DumpModuleUUID (Stream &strm, Module *module)
53{
54 module->GetUUID().Dump (&strm);
55}
56
57static uint32_t
58DumpCompileUnitLineTable
59(
Greg Clayton63094e02010-06-23 01:19:29 +000060 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000061 Stream &strm,
62 Module *module,
63 const FileSpec &file_spec,
64 bool load_addresses
65)
66{
67 uint32_t num_matches = 0;
68 if (module)
69 {
70 SymbolContextList sc_list;
71 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec,
72 0,
73 false,
74 eSymbolContextCompUnit,
75 sc_list);
76
77 for (uint32_t i=0; i<num_matches; ++i)
78 {
79 SymbolContext sc;
80 if (sc_list.GetContextAtIndex(i, sc))
81 {
82 if (i > 0)
83 strm << "\n\n";
84
85 strm << "Line table for " << *dynamic_cast<FileSpec*> (sc.comp_unit) << " in `"
86 << module->GetFileSpec().GetFilename() << "\n";
87 LineTable *line_table = sc.comp_unit->GetLineTable();
88 if (line_table)
Greg Clayton63094e02010-06-23 01:19:29 +000089 line_table->GetDescription (&strm,
Greg Claytoneea26402010-09-14 23:36:40 +000090 interpreter.GetDebugger().GetExecutionContext().target,
Greg Clayton63094e02010-06-23 01:19:29 +000091 lldb::eDescriptionLevelBrief);
Chris Lattner24943d22010-06-08 16:52:24 +000092 else
93 strm << "No line table";
94 }
95 }
96 }
97 return num_matches;
98}
99
100static void
101DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
102{
103 if (file_spec_ptr)
104 {
105 if (width > 0)
106 {
107 char fullpath[PATH_MAX];
108 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath)))
109 {
110 strm.Printf("%-*s", width, fullpath);
111 return;
112 }
113 }
114 else
115 {
116 file_spec_ptr->Dump(&strm);
117 return;
118 }
119 }
120 // Keep the width spacing correct if things go wrong...
121 if (width > 0)
122 strm.Printf("%-*s", width, "");
123}
124
125static void
126DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
127{
128 if (file_spec_ptr)
129 {
130 if (width > 0)
131 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
132 else
133 file_spec_ptr->GetDirectory().Dump(&strm);
134 return;
135 }
136 // Keep the width spacing correct if things go wrong...
137 if (width > 0)
138 strm.Printf("%-*s", width, "");
139}
140
141static void
142DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width)
143{
144 if (file_spec_ptr)
145 {
146 if (width > 0)
147 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
148 else
149 file_spec_ptr->GetFilename().Dump(&strm);
150 return;
151 }
152 // Keep the width spacing correct if things go wrong...
153 if (width > 0)
154 strm.Printf("%-*s", width, "");
155}
156
157
158static void
Greg Clayton63094e02010-06-23 01:19:29 +0000159DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module)
Chris Lattner24943d22010-06-08 16:52:24 +0000160{
161 if (module)
162 {
163 ObjectFile *objfile = module->GetObjectFile ();
164 if (objfile)
165 {
166 Symtab *symtab = objfile->GetSymtab();
167 if (symtab)
Greg Claytoneea26402010-09-14 23:36:40 +0000168 symtab->Dump(&strm, interpreter.GetDebugger().GetExecutionContext().target);
Chris Lattner24943d22010-06-08 16:52:24 +0000169 }
170 }
171}
172
173static void
Greg Clayton63094e02010-06-23 01:19:29 +0000174DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module)
Chris Lattner24943d22010-06-08 16:52:24 +0000175{
176 if (module)
177 {
178 ObjectFile *objfile = module->GetObjectFile ();
179 if (objfile)
180 {
181 SectionList *section_list = objfile->GetSectionList();
182 if (section_list)
Greg Clayton3fed8b92010-10-08 00:21:05 +0000183 {
184 strm.PutCString ("Sections for '");
185 strm << module->GetFileSpec();
186 strm.Printf ("' (%s):\n", module->GetArchitecture().AsCString());
187 strm.IndentMore();
Greg Claytoneea26402010-09-14 23:36:40 +0000188 section_list->Dump(&strm, interpreter.GetDebugger().GetExecutionContext().target, true);
Greg Clayton3fed8b92010-10-08 00:21:05 +0000189 strm.IndentLess();
190 }
Chris Lattner24943d22010-06-08 16:52:24 +0000191 }
192 }
193}
194
195static bool
196DumpModuleSymbolVendor (Stream &strm, Module *module)
197{
198 if (module)
199 {
200 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true);
201 if (symbol_vendor)
202 {
203 symbol_vendor->Dump(&strm);
204 return true;
205 }
206 }
207 return false;
208}
209
210static bool
Greg Clayton960d6a42010-08-03 00:35:52 +0000211LookupAddressInModule
212(
213 CommandInterpreter &interpreter,
214 Stream &strm,
215 Module *module,
216 uint32_t resolve_mask,
217 lldb::addr_t raw_addr,
218 lldb::addr_t offset,
219 bool verbose
220)
Chris Lattner24943d22010-06-08 16:52:24 +0000221{
222 if (module)
223 {
224 lldb::addr_t addr = raw_addr - offset;
225 Address so_addr;
226 SymbolContext sc;
Greg Claytoneea26402010-09-14 23:36:40 +0000227 Target *target = interpreter.GetDebugger().GetExecutionContext().target;
228 if (target && !target->GetSectionLoadList().IsEmpty())
Chris Lattner24943d22010-06-08 16:52:24 +0000229 {
Greg Claytoneea26402010-09-14 23:36:40 +0000230 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr))
Chris Lattner24943d22010-06-08 16:52:24 +0000231 return false;
232 else if (so_addr.GetModule() != module)
233 return false;
234 }
235 else
236 {
237 if (!module->ResolveFileAddress (addr, so_addr))
238 return false;
239 }
240
241 // If an offset was given, print out the address we ended up looking up
242 if (offset)
243 strm.Printf("0x%llx: ", addr);
244
Greg Clayton63094e02010-06-23 01:19:29 +0000245 ExecutionContextScope *exe_scope = interpreter.GetDebugger().GetExecutionContext().GetBestExecutionContextScope();
Greg Clayton12bec712010-06-28 21:30:43 +0000246 strm.IndentMore();
247 strm.Indent (" Address: ");
248 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset);
249 strm.EOL();
Greg Clayton70436352010-06-30 23:03:03 +0000250 strm.Indent (" Summary: ");
Chris Lattner24943d22010-06-08 16:52:24 +0000251 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription);
Greg Clayton70436352010-06-30 23:03:03 +0000252 strm.EOL();
Greg Clayton960d6a42010-08-03 00:35:52 +0000253 // Print out detailed address information when verbose is enabled
254 if (verbose)
255 {
256 if (so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext))
257 strm.EOL();
258 }
Greg Clayton12bec712010-06-28 21:30:43 +0000259 strm.IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +0000260 return true;
261 }
262
263 return false;
264}
265
266static uint32_t
Greg Clayton63094e02010-06-23 01:19:29 +0000267LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex)
Chris Lattner24943d22010-06-08 16:52:24 +0000268{
269 if (module)
270 {
271 SymbolContext sc;
272
273 ObjectFile *objfile = module->GetObjectFile ();
274 if (objfile)
275 {
276 Symtab *symtab = objfile->GetSymtab();
277 if (symtab)
278 {
279 uint32_t i;
280 std::vector<uint32_t> match_indexes;
281 ConstString symbol_name (name);
282 uint32_t num_matches = 0;
283 if (name_is_regex)
284 {
285 RegularExpression name_regexp(name);
Greg Clayton7c36fa02010-09-11 03:13:28 +0000286 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp,
287 eSymbolTypeAny,
Chris Lattner24943d22010-06-08 16:52:24 +0000288 match_indexes);
289 }
290 else
291 {
292 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes);
293 }
294
295
296 if (num_matches > 0)
297 {
298 strm.Indent ();
299 strm.Printf("%u symbols match %s'%s' in ", num_matches,
300 name_is_regex ? "the regular expression " : "", name);
301 DumpFullpath (strm, &module->GetFileSpec(), 0);
302 strm.PutCString(":\n");
303 strm.IndentMore ();
304 Symtab::DumpSymbolHeader (&strm);
305 for (i=0; i < num_matches; ++i)
306 {
307 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
308 strm.Indent ();
Greg Claytoneea26402010-09-14 23:36:40 +0000309 symbol->Dump (&strm, interpreter.GetDebugger().GetExecutionContext().target, i);
Chris Lattner24943d22010-06-08 16:52:24 +0000310 }
311 strm.IndentLess ();
312 return num_matches;
313 }
314 }
315 }
316 }
317 return 0;
318}
319
320
321static void
Greg Clayton63094e02010-06-23 01:19:29 +0000322DumpSymbolContextList (CommandInterpreter &interpreter, Stream &strm, SymbolContextList &sc_list, bool prepend_addr)
Chris Lattner24943d22010-06-08 16:52:24 +0000323{
324 strm.IndentMore ();
325 uint32_t i;
326 const uint32_t num_matches = sc_list.GetSize();
327
328 for (i=0; i<num_matches; ++i)
329 {
330 SymbolContext sc;
331 if (sc_list.GetContextAtIndex(i, sc))
332 {
333 strm.Indent();
334 if (prepend_addr)
335 {
336 if (sc.line_entry.range.GetBaseAddress().IsValid())
337 {
Greg Claytoneea26402010-09-14 23:36:40 +0000338 lldb::addr_t vm_addr = sc.line_entry.range.GetBaseAddress().GetLoadAddress(interpreter.GetDebugger().GetExecutionContext().target);
Chris Lattner24943d22010-06-08 16:52:24 +0000339 int addr_size = sizeof (addr_t);
Greg Clayton63094e02010-06-23 01:19:29 +0000340 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000341 if (process)
342 addr_size = process->GetAddressByteSize();
343 if (vm_addr != LLDB_INVALID_ADDRESS)
344 strm.Address (vm_addr, addr_size);
345 else
346 sc.line_entry.range.GetBaseAddress().Dump (&strm, NULL, Address::DumpStyleSectionNameOffset);
347
348 strm.PutCString(" in ");
349 }
350 }
Greg Clayton72b71582010-09-02 21:44:10 +0000351 sc.DumpStopContext(&strm, interpreter.GetDebugger().GetExecutionContext().process, sc.line_entry.range.GetBaseAddress(), true, true, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000352 }
353 }
354 strm.IndentLess ();
355}
356
357static uint32_t
Greg Clayton63094e02010-06-23 01:19:29 +0000358LookupFunctionInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex)
Chris Lattner24943d22010-06-08 16:52:24 +0000359{
360 if (module && name && name[0])
361 {
362 SymbolContextList sc_list;
363
364 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
365 if (symbol_vendor)
366 {
367 uint32_t num_matches = 0;
368 if (name_is_regex)
369 {
370 RegularExpression function_name_regex (name);
371 num_matches = symbol_vendor->FindFunctions(function_name_regex, true, sc_list);
372
373 }
374 else
375 {
376 ConstString function_name(name);
Greg Clayton12bec712010-06-28 21:30:43 +0000377 num_matches = symbol_vendor->FindFunctions(function_name, eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector, true, sc_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000378 }
379
380 if (num_matches)
381 {
382 strm.Indent ();
383 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
384 DumpFullpath (strm, &module->GetFileSpec(), 0);
385 strm.PutCString(":\n");
Greg Clayton63094e02010-06-23 01:19:29 +0000386 DumpSymbolContextList (interpreter, strm, sc_list, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000387 }
388 return num_matches;
389 }
390 }
391 return 0;
392}
393
394static uint32_t
Greg Clayton960d6a42010-08-03 00:35:52 +0000395LookupTypeInModule
396(
397 CommandInterpreter &interpreter,
398 Stream &strm,
399 Module *module,
400 const char *name_cstr,
401 bool name_is_regex
402)
403{
404 if (module && name_cstr && name_cstr[0])
405 {
406 SymbolContextList sc_list;
407
408 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
409 if (symbol_vendor)
410 {
411 TypeList type_list;
412 uint32_t num_matches = 0;
413 SymbolContext sc;
414// if (name_is_regex)
415// {
416// RegularExpression name_regex (name_cstr);
417// num_matches = symbol_vendor->FindFunctions(sc, name_regex, true, UINT32_MAX, type_list);
418// }
419// else
420// {
421 ConstString name(name_cstr);
422 num_matches = symbol_vendor->FindTypes(sc, name, true, UINT32_MAX, type_list);
423// }
424
425 if (num_matches)
426 {
427 strm.Indent ();
428 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
429 DumpFullpath (strm, &module->GetFileSpec(), 0);
430 strm.PutCString(":\n");
431 const uint32_t num_types = type_list.GetSize();
432 for (uint32_t i=0; i<num_types; ++i)
433 {
434 TypeSP type_sp (type_list.GetTypeAtIndex(i));
435 if (type_sp)
436 {
437 // Resolve the clang type so that any forward references
438 // to types that haven't yet been parsed will get parsed.
Greg Clayton462d4142010-09-29 01:12:09 +0000439 type_sp->GetClangType ();
Greg Clayton960d6a42010-08-03 00:35:52 +0000440 type_sp->GetDescription (&strm, eDescriptionLevelFull, true);
441 }
442 strm.EOL();
443 }
444 }
445 return num_matches;
446 }
447 }
448 return 0;
449}
450
451static uint32_t
Greg Clayton63094e02010-06-23 01:19:29 +0000452LookupFileAndLineInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const FileSpec &file_spec, uint32_t line, bool check_inlines)
Chris Lattner24943d22010-06-08 16:52:24 +0000453{
454 if (module && file_spec)
455 {
456 SymbolContextList sc_list;
457 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
458 eSymbolContextEverything, sc_list);
459 if (num_matches > 0)
460 {
461 strm.Indent ();
462 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : "");
463 strm << file_spec;
464 if (line > 0)
465 strm.Printf (":%u", line);
466 strm << " in ";
467 DumpFullpath (strm, &module->GetFileSpec(), 0);
468 strm.PutCString(":\n");
Greg Clayton63094e02010-06-23 01:19:29 +0000469 DumpSymbolContextList (interpreter, strm, sc_list, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000470 return num_matches;
471 }
472 }
473 return 0;
474
475}
476
477
478//----------------------------------------------------------------------
479// Image symbol table dumping command
480//----------------------------------------------------------------------
481
482class CommandObjectImageDumpModuleList : public CommandObject
483{
484public:
485
Greg Clayton238c0a12010-09-18 01:14:36 +0000486 CommandObjectImageDumpModuleList (CommandInterpreter &interpreter,
487 const char *name,
Greg Clayton63094e02010-06-23 01:19:29 +0000488 const char *help,
489 const char *syntax) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000490 CommandObject (interpreter, name, help, syntax)
Chris Lattner24943d22010-06-08 16:52:24 +0000491 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000492 CommandArgumentEntry arg;
493 CommandArgumentData file_arg;
494
495 // Define the first (and only) variant of this arg.
496 file_arg.arg_type = eArgTypeFilename;
497 file_arg.arg_repetition = eArgRepeatStar;
498
499 // There is only one variant this argument could be; put it into the argument entry.
500 arg.push_back (file_arg);
501
502 // Push the data for the first argument into the m_arguments vector.
503 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000504 }
505
506 virtual
507 ~CommandObjectImageDumpModuleList ()
508 {
509 }
510
511 virtual int
Greg Clayton238c0a12010-09-18 01:14:36 +0000512 HandleArgumentCompletion (Args &input,
Greg Clayton63094e02010-06-23 01:19:29 +0000513 int &cursor_index,
514 int &cursor_char_position,
515 OptionElementVector &opt_element_vector,
516 int match_start_point,
517 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000518 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000519 StringList &matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000520 {
521 // Arguments are the standard module completer.
522 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
523 completion_str.erase (cursor_char_position);
524
Greg Clayton238c0a12010-09-18 01:14:36 +0000525 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +0000526 CommandCompletions::eModuleCompletion,
527 completion_str.c_str(),
528 match_start_point,
529 max_return_elements,
530 NULL,
Jim Ingham802f8b02010-06-30 05:02:46 +0000531 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000532 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000533 return matches.GetSize();
534 }
535};
536
537class CommandObjectImageDumpSourceFileList : public CommandObject
538{
539public:
540
Greg Clayton238c0a12010-09-18 01:14:36 +0000541 CommandObjectImageDumpSourceFileList (CommandInterpreter &interpreter,
542 const char *name,
Greg Clayton63094e02010-06-23 01:19:29 +0000543 const char *help,
544 const char *syntax) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000545 CommandObject (interpreter, name, help, syntax)
Chris Lattner24943d22010-06-08 16:52:24 +0000546 {
Caroline Tice43b014a2010-10-04 22:28:36 +0000547 CommandArgumentEntry arg;
548 CommandArgumentData source_file_arg;
549
550 // Define the first (and only) variant of this arg.
551 source_file_arg.arg_type = eArgTypeSourceFile;
552 source_file_arg.arg_repetition = eArgRepeatPlus;
553
554 // There is only one variant this argument could be; put it into the argument entry.
555 arg.push_back (source_file_arg);
556
557 // Push the data for the first argument into the m_arguments vector.
558 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000559 }
560
561 virtual
562 ~CommandObjectImageDumpSourceFileList ()
563 {
564 }
565
566 virtual int
Greg Clayton238c0a12010-09-18 01:14:36 +0000567 HandleArgumentCompletion (Args &input,
Greg Clayton63094e02010-06-23 01:19:29 +0000568 int &cursor_index,
569 int &cursor_char_position,
570 OptionElementVector &opt_element_vector,
571 int match_start_point,
572 int max_return_elements,
Greg Clayton54e7afa2010-07-09 20:39:50 +0000573 bool &word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000574 StringList &matches)
Chris Lattner24943d22010-06-08 16:52:24 +0000575 {
576 // Arguments are the standard source file completer.
577 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
578 completion_str.erase (cursor_char_position);
579
Greg Clayton238c0a12010-09-18 01:14:36 +0000580 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +0000581 CommandCompletions::eSourceFileCompletion,
582 completion_str.c_str(),
583 match_start_point,
584 max_return_elements,
585 NULL,
Jim Ingham802f8b02010-06-30 05:02:46 +0000586 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000587 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000588 return matches.GetSize();
589 }
590};
591
592
593class CommandObjectImageDumpSymtab : public CommandObjectImageDumpModuleList
594{
595public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000596 CommandObjectImageDumpSymtab (CommandInterpreter &interpreter) :
597 CommandObjectImageDumpModuleList (interpreter,
598 "image dump symtab",
599 "Dump the symbol table from one or more executable images.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000600 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000601 {
602 }
603
604 virtual
605 ~CommandObjectImageDumpSymtab ()
606 {
607 }
608
609 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000610 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000611 CommandReturnObject &result)
612 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000613 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000614 if (target == NULL)
615 {
616 result.AppendError ("invalid target, set executable file using 'file' command");
617 result.SetStatus (eReturnStatusFailed);
618 return false;
619 }
620 else
621 {
622 uint32_t num_dumped = 0;
623
624 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
625 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
626 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
627
628 if (command.GetArgumentCount() == 0)
629 {
630 // Dump all sections for all modules images
631 const uint32_t num_modules = target->GetImages().GetSize();
632 if (num_modules > 0)
633 {
634 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules);
635 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
636 {
637 num_dumped++;
Greg Clayton238c0a12010-09-18 01:14:36 +0000638 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000639 }
640 }
641 else
642 {
643 result.AppendError ("the target has no associated executable images");
644 result.SetStatus (eReturnStatusFailed);
645 return false;
646 }
647 }
648 else
649 {
650 // Dump specified images (by basename or fullpath)
651 const char *arg_cstr;
652 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
653 {
654 FileSpec image_file(arg_cstr);
655 ModuleList matching_modules;
Greg Clayton661825b2010-06-28 23:51:11 +0000656 size_t num_matching_modules = target->GetImages().FindModules(&image_file, NULL, NULL, NULL, matching_modules);
Chris Lattner24943d22010-06-08 16:52:24 +0000657
Greg Clayton661825b2010-06-28 23:51:11 +0000658 // Not found in our module list for our target, check the main
659 // shared module list in case it is a extra file used somewhere
660 // else
661 if (num_matching_modules == 0)
662 num_matching_modules = ModuleList::FindSharedModules (image_file,
663 target->GetArchitecture(),
664 NULL,
665 NULL,
666 matching_modules);
667
Chris Lattner24943d22010-06-08 16:52:24 +0000668 if (num_matching_modules > 0)
669 {
670 for (size_t i=0; i<num_matching_modules; ++i)
671 {
672 Module *image_module = matching_modules.GetModulePointerAtIndex(i);
673 if (image_module)
674 {
675 num_dumped++;
Greg Clayton238c0a12010-09-18 01:14:36 +0000676 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), image_module);
Chris Lattner24943d22010-06-08 16:52:24 +0000677 }
678 }
679 }
680 else
681 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
682 }
683 }
684
685 if (num_dumped > 0)
686 result.SetStatus (eReturnStatusSuccessFinishResult);
687 else
688 {
689 result.AppendError ("no matching executable images found");
690 result.SetStatus (eReturnStatusFailed);
691 }
692 }
693 return result.Succeeded();
694 }
695
696};
697
698//----------------------------------------------------------------------
699// Image section dumping command
700//----------------------------------------------------------------------
701class CommandObjectImageDumpSections : public CommandObjectImageDumpModuleList
702{
703public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000704 CommandObjectImageDumpSections (CommandInterpreter &interpreter) :
705 CommandObjectImageDumpModuleList (interpreter,
706 "image dump sections",
Greg Clayton63094e02010-06-23 01:19:29 +0000707 "Dump the sections from one or more executable images.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000708 //"image dump sections [<file1> ...]")
709 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000710 {
711 }
712
713 virtual
714 ~CommandObjectImageDumpSections ()
715 {
716 }
717
718 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000719 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000720 CommandReturnObject &result)
721 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000722 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000723 if (target == NULL)
724 {
725 result.AppendError ("invalid target, set executable file using 'file' command");
726 result.SetStatus (eReturnStatusFailed);
727 return false;
728 }
729 else
730 {
731 uint32_t num_dumped = 0;
732
733 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
734 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
735 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
736
737 if (command.GetArgumentCount() == 0)
738 {
739 // Dump all sections for all modules images
740 const uint32_t num_modules = target->GetImages().GetSize();
741 if (num_modules > 0)
742 {
743 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules);
744 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
745 {
746 num_dumped++;
Greg Clayton238c0a12010-09-18 01:14:36 +0000747 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx));
Chris Lattner24943d22010-06-08 16:52:24 +0000748 }
749 }
750 else
751 {
752 result.AppendError ("the target has no associated executable images");
753 result.SetStatus (eReturnStatusFailed);
754 return false;
755 }
756 }
757 else
758 {
759 // Dump specified images (by basename or fullpath)
760 const char *arg_cstr;
761 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
762 {
763 FileSpec image_file(arg_cstr);
764 ModuleList matching_modules;
Greg Clayton661825b2010-06-28 23:51:11 +0000765 size_t num_matching_modules = target->GetImages().FindModules(&image_file, NULL, NULL, NULL, matching_modules);
Chris Lattner24943d22010-06-08 16:52:24 +0000766
Greg Clayton661825b2010-06-28 23:51:11 +0000767 // Not found in our module list for our target, check the main
768 // shared module list in case it is a extra file used somewhere
769 // else
770 if (num_matching_modules == 0)
771 num_matching_modules = ModuleList::FindSharedModules (image_file,
772 target->GetArchitecture(),
773 NULL,
774 NULL,
775 matching_modules);
776
Chris Lattner24943d22010-06-08 16:52:24 +0000777 if (num_matching_modules > 0)
778 {
779 for (size_t i=0; i<num_matching_modules; ++i)
780 {
781 Module * image_module = matching_modules.GetModulePointerAtIndex(i);
782 if (image_module)
783 {
784 num_dumped++;
Greg Clayton238c0a12010-09-18 01:14:36 +0000785 DumpModuleSections (m_interpreter, result.GetOutputStream(), image_module);
Chris Lattner24943d22010-06-08 16:52:24 +0000786 }
787 }
788 }
789 else
790 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
791 }
792 }
793
794 if (num_dumped > 0)
795 result.SetStatus (eReturnStatusSuccessFinishResult);
796 else
797 {
798 result.AppendError ("no matching executable images found");
799 result.SetStatus (eReturnStatusFailed);
800 }
801 }
802 return result.Succeeded();
803 }
804};
805
806//----------------------------------------------------------------------
807// Image debug symbol dumping command
808//----------------------------------------------------------------------
809class CommandObjectImageDumpSymfile : public CommandObjectImageDumpModuleList
810{
811public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000812 CommandObjectImageDumpSymfile (CommandInterpreter &interpreter) :
813 CommandObjectImageDumpModuleList (interpreter,
814 "image dump symfile",
815 "Dump the debug symbol file for one or more executable images.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000816 //"image dump symfile [<file1> ...]")
817 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000818 {
819 }
820
821 virtual
822 ~CommandObjectImageDumpSymfile ()
823 {
824 }
825
826 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000827 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000828 CommandReturnObject &result)
829 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000830 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000831 if (target == NULL)
832 {
833 result.AppendError ("invalid target, set executable file using 'file' command");
834 result.SetStatus (eReturnStatusFailed);
835 return false;
836 }
837 else
838 {
839 uint32_t num_dumped = 0;
840
841 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
842 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
843 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
844
845 if (command.GetArgumentCount() == 0)
846 {
847 // Dump all sections for all modules images
848 const uint32_t num_modules = target->GetImages().GetSize();
849 if (num_modules > 0)
850 {
851 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules);
852 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
853 {
854 if (DumpModuleSymbolVendor (result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx)))
855 num_dumped++;
856 }
857 }
858 else
859 {
860 result.AppendError ("the target has no associated executable images");
861 result.SetStatus (eReturnStatusFailed);
862 return false;
863 }
864 }
865 else
866 {
867 // Dump specified images (by basename or fullpath)
868 const char *arg_cstr;
869 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
870 {
871 FileSpec image_file(arg_cstr);
872 ModuleList matching_modules;
Greg Clayton661825b2010-06-28 23:51:11 +0000873 size_t num_matching_modules = target->GetImages().FindModules(&image_file, NULL, NULL, NULL, matching_modules);
Chris Lattner24943d22010-06-08 16:52:24 +0000874
Greg Clayton661825b2010-06-28 23:51:11 +0000875 // Not found in our module list for our target, check the main
876 // shared module list in case it is a extra file used somewhere
877 // else
878 if (num_matching_modules == 0)
879 num_matching_modules = ModuleList::FindSharedModules (image_file,
880 target->GetArchitecture(),
881 NULL,
882 NULL,
883 matching_modules);
884
Chris Lattner24943d22010-06-08 16:52:24 +0000885 if (num_matching_modules > 0)
886 {
887 for (size_t i=0; i<num_matching_modules; ++i)
888 {
889 Module * image_module = matching_modules.GetModulePointerAtIndex(i);
890 if (image_module)
891 {
892 if (DumpModuleSymbolVendor (result.GetOutputStream(), image_module))
893 num_dumped++;
894 }
895 }
896 }
897 else
898 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
899 }
900 }
901
902 if (num_dumped > 0)
903 result.SetStatus (eReturnStatusSuccessFinishResult);
904 else
905 {
906 result.AppendError ("no matching executable images found");
907 result.SetStatus (eReturnStatusFailed);
908 }
909 }
910 return result.Succeeded();
911 }
912};
913
914//----------------------------------------------------------------------
915// Image debug symbol dumping command
916//----------------------------------------------------------------------
917class CommandObjectImageDumpLineTable : public CommandObjectImageDumpSourceFileList
918{
919public:
Greg Clayton238c0a12010-09-18 01:14:36 +0000920 CommandObjectImageDumpLineTable (CommandInterpreter &interpreter) :
921 CommandObjectImageDumpSourceFileList (interpreter,
922 "image dump line-table",
923 "Dump the debug symbol file for one or more executable images.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000924 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000925 {
926 }
927
928 virtual
929 ~CommandObjectImageDumpLineTable ()
930 {
931 }
932
933 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000934 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000935 CommandReturnObject &result)
936 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000937 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000938 if (target == NULL)
939 {
940 result.AppendError ("invalid target, set executable file using 'file' command");
941 result.SetStatus (eReturnStatusFailed);
942 return false;
943 }
944 else
945 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000946 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000947 uint32_t total_num_dumped = 0;
948
949 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
950 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
951 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
952
953 if (command.GetArgumentCount() == 0)
954 {
955 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str());
956 result.SetStatus (eReturnStatusFailed);
957 }
958 else
959 {
960 // Dump specified images (by basename or fullpath)
961 const char *arg_cstr;
962 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx)
963 {
964 FileSpec file_spec(arg_cstr);
965 const uint32_t num_modules = target->GetImages().GetSize();
966 if (num_modules > 0)
967 {
968 uint32_t num_dumped = 0;
969 for (uint32_t i = 0; i<num_modules; ++i)
970 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000971 if (DumpCompileUnitLineTable (m_interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000972 result.GetOutputStream(),
973 target->GetImages().GetModulePointerAtIndex(i),
974 file_spec,
975 exe_ctx.process != NULL && exe_ctx.process->IsAlive()))
976 num_dumped++;
977 }
978 if (num_dumped == 0)
979 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr);
980 else
981 total_num_dumped += num_dumped;
982 }
983 }
984 }
985
986 if (total_num_dumped > 0)
987 result.SetStatus (eReturnStatusSuccessFinishResult);
988 else
989 {
990 result.AppendError ("no source filenames matched any command arguments");
991 result.SetStatus (eReturnStatusFailed);
992 }
993 }
994 return result.Succeeded();
995 }
996};
997
998//----------------------------------------------------------------------
999// Dump multi-word command
1000//----------------------------------------------------------------------
1001class CommandObjectImageDump : public CommandObjectMultiword
1002{
1003public:
1004
1005 //------------------------------------------------------------------
1006 // Constructors and Destructors
1007 //------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +00001008 CommandObjectImageDump(CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001009 CommandObjectMultiword (interpreter,
1010 "image dump",
Caroline Ticeabb507a2010-09-08 21:06:11 +00001011 "A set of commands for dumping information about one or more executable images; 'line-table' expects a source file name",
Greg Clayton63094e02010-06-23 01:19:29 +00001012 "image dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
Chris Lattner24943d22010-06-08 16:52:24 +00001013 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001014 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectImageDumpSymtab (interpreter)));
1015 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectImageDumpSections (interpreter)));
1016 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectImageDumpSymfile (interpreter)));
1017 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectImageDumpLineTable (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001018 }
1019
1020 virtual
1021 ~CommandObjectImageDump()
1022 {
1023 }
1024};
1025
1026//----------------------------------------------------------------------
1027// List images with associated information
1028//----------------------------------------------------------------------
1029class CommandObjectImageList : public CommandObject
1030{
1031public:
1032
1033 class CommandOptions : public Options
1034 {
1035 public:
1036
1037 CommandOptions () :
1038 Options(),
1039 m_format_array()
1040 {
1041 }
1042
1043 virtual
1044 ~CommandOptions ()
1045 {
1046 }
1047
1048 virtual Error
1049 SetOptionValue (int option_idx, const char *option_arg)
1050 {
1051 char short_option = (char) m_getopt_table[option_idx].val;
1052 uint32_t width = 0;
1053 if (option_arg)
1054 width = strtoul (option_arg, NULL, 0);
1055 m_format_array.push_back(std::make_pair(short_option, width));
1056 Error error;
1057 return error;
1058 }
1059
1060 void
1061 ResetOptionValues ()
1062 {
1063 Options::ResetOptionValues();
1064 m_format_array.clear();
1065 }
1066
1067 const lldb::OptionDefinition*
1068 GetDefinitions ()
1069 {
1070 return g_option_table;
1071 }
1072
1073 // Options table: Required for subclasses of Options.
1074
1075 static lldb::OptionDefinition g_option_table[];
1076
1077 // Instance variables to hold the values for command options.
1078 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection;
1079 FormatWidthCollection m_format_array;
1080 };
1081
Greg Clayton238c0a12010-09-18 01:14:36 +00001082 CommandObjectImageList (CommandInterpreter &interpreter) :
1083 CommandObject (interpreter,
1084 "image list",
1085 "List current executable and dependent shared library images.",
1086 "image list [<cmd-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +00001087 {
1088 }
1089
1090 virtual
1091 ~CommandObjectImageList ()
1092 {
1093 }
1094
1095 virtual
1096 Options *
1097 GetOptions ()
1098 {
1099 return &m_options;
1100 }
1101
1102 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001103 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001104 CommandReturnObject &result)
1105 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001106 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001107 if (target == NULL)
1108 {
1109 result.AppendError ("invalid target, set executable file using 'file' command");
1110 result.SetStatus (eReturnStatusFailed);
1111 return false;
1112 }
1113 else
1114 {
1115 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1116 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1117 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1118 // Dump all sections for all modules images
1119 const uint32_t num_modules = target->GetImages().GetSize();
1120 if (num_modules > 0)
1121 {
1122 Stream &strm = result.GetOutputStream();
1123
1124 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx)
1125 {
1126 Module *module = target->GetImages().GetModulePointerAtIndex(image_idx);
1127 strm.Printf("[%3u] ", image_idx);
1128
1129 if (m_options.m_format_array.empty())
1130 {
1131 DumpFullpath(strm, &module->GetFileSpec(), 0);
1132 }
1133 else
1134 {
1135 const size_t num_entries = m_options.m_format_array.size();
1136 for (size_t i=0; i<num_entries; ++i)
1137 {
1138 if (i > 0)
1139 strm.PutChar(' ');
1140 char format_char = m_options.m_format_array[i].first;
1141 uint32_t width = m_options.m_format_array[i].second;
1142 switch (format_char)
1143 {
1144 case 'a':
1145 DumpModuleArchitecture (strm, module, width);
1146 break;
1147
1148 case 'f':
1149 DumpFullpath (strm, &module->GetFileSpec(), width);
1150 break;
1151
1152 case 'd':
1153 DumpDirectory (strm, &module->GetFileSpec(), width);
1154 break;
1155
1156 case 'b':
1157 DumpBasename (strm, &module->GetFileSpec(), width);
1158 break;
1159
1160 case 's':
1161 case 'S':
1162 {
1163 SymbolVendor *symbol_vendor = module->GetSymbolVendor();
1164 if (symbol_vendor)
1165 {
1166 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile();
1167 if (symbol_file)
1168 {
1169 if (format_char == 'S')
1170 DumpBasename(strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
1171 else
1172 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width);
1173 break;
1174 }
1175 }
1176 strm.Printf("%.*s", width, "<NONE>");
1177 }
1178 break;
1179
1180 case 'u':
1181 DumpModuleUUID(strm, module);
1182 break;
1183
1184 default:
1185 break;
1186 }
1187 }
1188 }
1189 strm.EOL();
1190 }
1191 result.SetStatus (eReturnStatusSuccessFinishResult);
1192 }
1193 else
1194 {
1195 result.AppendError ("the target has no associated executable images");
1196 result.SetStatus (eReturnStatusFailed);
1197 return false;
1198 }
1199 }
1200 return result.Succeeded();
1201 }
1202protected:
1203
1204 CommandOptions m_options;
1205};
1206
1207lldb::OptionDefinition
1208CommandObjectImageList::CommandOptions::g_option_table[] =
1209{
Caroline Tice4d6675c2010-10-01 19:59:14 +00001210{ LLDB_OPT_SET_1, false, "arch", 'a', optional_argument, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."},
1211{ LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."},
1212{ LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."},
1213{ LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."},
1214{ LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."},
1215{ LLDB_OPT_SET_1, false, "symfile", 's', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."},
1216{ LLDB_OPT_SET_1, false, "symfile-basename", 'S', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename to the image symbol file with optional width."},
1217{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +00001218};
1219
1220
1221
1222//----------------------------------------------------------------------
1223// Lookup information in images
1224//----------------------------------------------------------------------
1225class CommandObjectImageLookup : public CommandObject
1226{
1227public:
1228
1229 enum
1230 {
1231 eLookupTypeInvalid = -1,
1232 eLookupTypeAddress = 0,
1233 eLookupTypeSymbol,
1234 eLookupTypeFileLine, // Line is optional
1235 eLookupTypeFunction,
Greg Clayton960d6a42010-08-03 00:35:52 +00001236 eLookupTypeType,
Chris Lattner24943d22010-06-08 16:52:24 +00001237 kNumLookupTypes
1238 };
1239
1240 class CommandOptions : public Options
1241 {
1242 public:
1243
1244 CommandOptions () :
1245 Options()
1246 {
1247 ResetOptionValues();
1248 }
1249
1250 virtual
1251 ~CommandOptions ()
1252 {
1253 }
1254
1255 virtual Error
1256 SetOptionValue (int option_idx, const char *option_arg)
1257 {
1258 Error error;
1259
1260 char short_option = (char) m_getopt_table[option_idx].val;
1261
1262 switch (short_option)
1263 {
1264 case 'a':
1265 m_type = eLookupTypeAddress;
1266 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
1267 if (m_addr == LLDB_INVALID_ADDRESS)
1268 error.SetErrorStringWithFormat ("Invalid address string '%s'.\n", option_arg);
1269 break;
1270
1271 case 'o':
1272 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS);
1273 if (m_offset == LLDB_INVALID_ADDRESS)
1274 error.SetErrorStringWithFormat ("Invalid offset string '%s'.\n", option_arg);
1275 break;
1276
1277 case 's':
1278 m_str = option_arg;
1279 m_type = eLookupTypeSymbol;
1280 break;
1281
1282 case 'f':
1283 m_file.SetFile (option_arg);
1284 m_type = eLookupTypeFileLine;
1285 break;
1286
1287 case 'i':
1288 m_check_inlines = false;
1289 break;
1290
1291 case 'l':
1292 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX);
1293 if (m_line_number == UINT32_MAX)
1294 error.SetErrorStringWithFormat ("Invalid line number string '%s'.\n", option_arg);
1295 else if (m_line_number == 0)
1296 error.SetErrorString ("Zero is an invalid line number.");
1297 m_type = eLookupTypeFileLine;
1298 break;
1299
1300 case 'n':
1301 m_str = option_arg;
1302 m_type = eLookupTypeFunction;
1303 break;
1304
Greg Clayton960d6a42010-08-03 00:35:52 +00001305 case 't':
1306 m_str = option_arg;
1307 m_type = eLookupTypeType;
1308 break;
1309
1310 case 'v':
1311 m_verbose = 1;
1312 break;
1313
Chris Lattner24943d22010-06-08 16:52:24 +00001314 case 'r':
1315 m_use_regex = true;
1316 break;
1317 }
1318
1319 return error;
1320 }
1321
1322 void
1323 ResetOptionValues ()
1324 {
1325 Options::ResetOptionValues();
1326 m_type = eLookupTypeInvalid;
1327 m_str.clear();
1328 m_file.Clear();
1329 m_addr = LLDB_INVALID_ADDRESS;
1330 m_offset = 0;
1331 m_line_number = 0;
1332 m_use_regex = false;
1333 m_check_inlines = true;
Greg Clayton960d6a42010-08-03 00:35:52 +00001334 m_verbose = false;
Chris Lattner24943d22010-06-08 16:52:24 +00001335 }
1336
1337 const lldb::OptionDefinition*
1338 GetDefinitions ()
1339 {
1340 return g_option_table;
1341 }
1342
1343 // Options table: Required for subclasses of Options.
1344
1345 static lldb::OptionDefinition g_option_table[];
1346 int m_type; // Should be a eLookupTypeXXX enum after parsing options
1347 std::string m_str; // Holds name lookup
1348 FileSpec m_file; // Files for file lookups
1349 lldb::addr_t m_addr; // Holds the address to lookup
1350 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups.
1351 uint32_t m_line_number; // Line number for file+line lookups
1352 bool m_use_regex; // Name lookups in m_str are regular expressions.
1353 bool m_check_inlines;// Check for inline entries when looking up by file/line.
Greg Clayton960d6a42010-08-03 00:35:52 +00001354 bool m_verbose; // Enable verbose lookup info
1355
Chris Lattner24943d22010-06-08 16:52:24 +00001356 };
1357
Greg Clayton238c0a12010-09-18 01:14:36 +00001358 CommandObjectImageLookup (CommandInterpreter &interpreter) :
1359 CommandObject (interpreter,
1360 "image lookup",
1361 "Look up information within executable and dependent shared library images.",
Caroline Tice43b014a2010-10-04 22:28:36 +00001362 NULL)
Chris Lattner24943d22010-06-08 16:52:24 +00001363 {
Caroline Tice43b014a2010-10-04 22:28:36 +00001364 CommandArgumentEntry arg;
1365 CommandArgumentData file_arg;
1366
1367 // Define the first (and only) variant of this arg.
1368 file_arg.arg_type = eArgTypeFilename;
1369 file_arg.arg_repetition = eArgRepeatStar;
1370
1371 // There is only one variant this argument could be; put it into the argument entry.
1372 arg.push_back (file_arg);
1373
1374 // Push the data for the first argument into the m_arguments vector.
1375 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +00001376 }
1377
1378 virtual
1379 ~CommandObjectImageLookup ()
1380 {
1381 }
1382
1383 virtual
1384 Options *
1385 GetOptions ()
1386 {
1387 return &m_options;
1388 }
1389
1390
1391 bool
Greg Clayton63094e02010-06-23 01:19:29 +00001392 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error)
Chris Lattner24943d22010-06-08 16:52:24 +00001393 {
1394 switch (m_options.m_type)
1395 {
1396 case eLookupTypeAddress:
1397 if (m_options.m_addr != LLDB_INVALID_ADDRESS)
1398 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001399 if (LookupAddressInModule (m_interpreter,
Greg Clayton960d6a42010-08-03 00:35:52 +00001400 result.GetOutputStream(),
1401 module,
1402 eSymbolContextEverything,
1403 m_options.m_addr,
1404 m_options.m_offset,
1405 m_options.m_verbose))
Chris Lattner24943d22010-06-08 16:52:24 +00001406 {
1407 result.SetStatus(eReturnStatusSuccessFinishResult);
1408 return true;
1409 }
1410 }
1411 break;
1412
1413 case eLookupTypeSymbol:
1414 if (!m_options.m_str.empty())
1415 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001416 if (LookupSymbolInModule (m_interpreter, result.GetOutputStream(), module, m_options.m_str.c_str(), m_options.m_use_regex))
Chris Lattner24943d22010-06-08 16:52:24 +00001417 {
1418 result.SetStatus(eReturnStatusSuccessFinishResult);
1419 return true;
1420 }
1421 }
1422 break;
1423
1424 case eLookupTypeFileLine:
1425 if (m_options.m_file)
1426 {
1427
Greg Clayton238c0a12010-09-18 01:14:36 +00001428 if (LookupFileAndLineInModule (m_interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +00001429 result.GetOutputStream(),
1430 module,
1431 m_options.m_file,
1432 m_options.m_line_number,
1433 m_options.m_check_inlines))
1434 {
1435 result.SetStatus(eReturnStatusSuccessFinishResult);
1436 return true;
1437 }
1438 }
1439 break;
1440
1441 case eLookupTypeFunction:
1442 if (!m_options.m_str.empty())
1443 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001444 if (LookupFunctionInModule (m_interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +00001445 result.GetOutputStream(),
1446 module,
1447 m_options.m_str.c_str(),
1448 m_options.m_use_regex))
1449 {
1450 result.SetStatus(eReturnStatusSuccessFinishResult);
1451 return true;
1452 }
1453 }
1454 break;
1455
Greg Clayton960d6a42010-08-03 00:35:52 +00001456 case eLookupTypeType:
1457 if (!m_options.m_str.empty())
1458 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001459 if (LookupTypeInModule (m_interpreter,
Greg Clayton960d6a42010-08-03 00:35:52 +00001460 result.GetOutputStream(),
1461 module,
1462 m_options.m_str.c_str(),
1463 m_options.m_use_regex))
1464 {
1465 result.SetStatus(eReturnStatusSuccessFinishResult);
1466 return true;
1467 }
1468 }
1469 break;
1470
Chris Lattner24943d22010-06-08 16:52:24 +00001471 default:
Greg Clayton238c0a12010-09-18 01:14:36 +00001472 m_options.GenerateOptionUsage (m_interpreter, result.GetErrorStream(), this);
Chris Lattner24943d22010-06-08 16:52:24 +00001473 syntax_error = true;
1474 break;
1475 }
1476
1477 result.SetStatus (eReturnStatusFailed);
1478 return false;
1479 }
1480
1481 virtual bool
Greg Clayton238c0a12010-09-18 01:14:36 +00001482 Execute (Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +00001483 CommandReturnObject &result)
1484 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001485 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +00001486 if (target == NULL)
1487 {
1488 result.AppendError ("invalid target, set executable file using 'file' command");
1489 result.SetStatus (eReturnStatusFailed);
1490 return false;
1491 }
1492 else
1493 {
1494 bool syntax_error = false;
1495 uint32_t i;
1496 uint32_t num_successful_lookups = 0;
1497 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1498 result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1499 result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1500 // Dump all sections for all modules images
1501
1502 if (command.GetArgumentCount() == 0)
1503 {
1504 // Dump all sections for all modules images
1505 const uint32_t num_modules = target->GetImages().GetSize();
1506 if (num_modules > 0)
1507 {
1508 for (i = 0; i<num_modules && syntax_error == false; ++i)
1509 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001510 if (LookupInModule (m_interpreter, target->GetImages().GetModulePointerAtIndex(i), result, syntax_error))
Chris Lattner24943d22010-06-08 16:52:24 +00001511 {
1512 result.GetOutputStream().EOL();
1513 num_successful_lookups++;
1514 }
1515 }
1516 }
1517 else
1518 {
1519 result.AppendError ("the target has no associated executable images");
1520 result.SetStatus (eReturnStatusFailed);
1521 return false;
1522 }
1523 }
1524 else
1525 {
1526 // Dump specified images (by basename or fullpath)
1527 const char *arg_cstr;
1528 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i)
1529 {
1530 FileSpec image_file(arg_cstr);
1531 ModuleList matching_modules;
Greg Clayton661825b2010-06-28 23:51:11 +00001532 size_t num_matching_modules = target->GetImages().FindModules(&image_file, NULL, NULL, NULL, matching_modules);
Chris Lattner24943d22010-06-08 16:52:24 +00001533
Greg Clayton661825b2010-06-28 23:51:11 +00001534 // Not found in our module list for our target, check the main
1535 // shared module list in case it is a extra file used somewhere
1536 // else
1537 if (num_matching_modules == 0)
1538 num_matching_modules = ModuleList::FindSharedModules (image_file,
1539 target->GetArchitecture(),
1540 NULL,
1541 NULL,
1542 matching_modules);
1543
Chris Lattner24943d22010-06-08 16:52:24 +00001544 if (num_matching_modules > 0)
1545 {
Greg Claytonbef15832010-07-14 00:18:15 +00001546 for (size_t j=0; j<num_matching_modules; ++j)
Chris Lattner24943d22010-06-08 16:52:24 +00001547 {
Greg Claytonbef15832010-07-14 00:18:15 +00001548 Module * image_module = matching_modules.GetModulePointerAtIndex(j);
Chris Lattner24943d22010-06-08 16:52:24 +00001549 if (image_module)
1550 {
Greg Clayton238c0a12010-09-18 01:14:36 +00001551 if (LookupInModule (m_interpreter, image_module, result, syntax_error))
Chris Lattner24943d22010-06-08 16:52:24 +00001552 {
1553 result.GetOutputStream().EOL();
1554 num_successful_lookups++;
1555 }
1556 }
1557 }
1558 }
1559 else
1560 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
1561 }
1562 }
1563
1564 if (num_successful_lookups > 0)
1565 result.SetStatus (eReturnStatusSuccessFinishResult);
1566 else
1567 result.SetStatus (eReturnStatusFailed);
1568 }
1569 return result.Succeeded();
1570 }
1571protected:
1572
1573 CommandOptions m_options;
1574};
1575
1576lldb::OptionDefinition
1577CommandObjectImageLookup::CommandOptions::g_option_table[] =
1578{
Caroline Tice4d6675c2010-10-01 19:59:14 +00001579{ LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more executable images."},
1580{ LLDB_OPT_SET_1, false, "offset", 'o', required_argument, NULL, 0, eArgTypeOffset, "When looking up an address subtract <offset> from any addresses before doing the lookup."},
1581{ LLDB_OPT_SET_2, true, "symbol", 's', required_argument, NULL, 0, eArgTypeSymbol, "Lookup a symbol by name in the symbol tables in one or more executable images."},
1582{ LLDB_OPT_SET_2, false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."},
1583{ LLDB_OPT_SET_3, true, "file", 'f', required_argument, NULL, 0, eArgTypeFilename, "Lookup a file by fullpath or basename in one or more executable images."},
1584{ LLDB_OPT_SET_3, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "Lookup a line number in a file (must be used in conjunction with --file)."},
1585{ LLDB_OPT_SET_3, false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Check inline line entries (must be used in conjunction with --file)."},
1586{ LLDB_OPT_SET_4, true, "function", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more executable images."},
1587{ LLDB_OPT_SET_5, true, "type", 't', required_argument, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more executable images."},
1588{ LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."},
1589{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +00001590};
1591
1592
1593
1594
1595
1596//----------------------------------------------------------------------
1597// CommandObjectImage constructor
1598//----------------------------------------------------------------------
Greg Clayton63094e02010-06-23 01:19:29 +00001599CommandObjectImage::CommandObjectImage(CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +00001600 CommandObjectMultiword (interpreter,
1601 "image",
Caroline Ticec1ad82e2010-09-07 22:38:08 +00001602 "A set of commands for accessing information for one or more executable images.",
1603 "image <sub-command> ...")
Chris Lattner24943d22010-06-08 16:52:24 +00001604{
Greg Clayton238c0a12010-09-18 01:14:36 +00001605 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectImageDump (interpreter)));
1606 LoadSubCommand ("list", CommandObjectSP (new CommandObjectImageList (interpreter)));
1607 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectImageLookup (interpreter)));
Chris Lattner24943d22010-06-08 16:52:24 +00001608}
1609
1610//----------------------------------------------------------------------
1611// Destructor
1612//----------------------------------------------------------------------
1613CommandObjectImage::~CommandObjectImage()
1614{
1615}
1616