1 //===-- CommandObjectDisassemble.cpp ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectDisassemble.h"
16 // Other libraries and framework includes
18 #include "lldb/Core/AddressRange.h"
19 #include "lldb/Core/Disassembler.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/SourceManager.h"
22 #include "lldb/Host/StringConvert.h"
23 #include "lldb/Interpreter/CommandCompletions.h"
24 #include "lldb/Interpreter/CommandInterpreter.h"
25 #include "lldb/Interpreter/CommandReturnObject.h"
26 #include "lldb/Interpreter/Options.h"
27 #include "lldb/Symbol/Function.h"
28 #include "lldb/Symbol/Symbol.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/SectionLoadList.h"
31 #include "lldb/Target/StackFrame.h"
32 #include "lldb/Target/Target.h"
34 #define DEFAULT_DISASM_BYTE_SIZE 32
35 #define DEFAULT_DISASM_NUM_INS 4
38 using namespace lldb_private;
40 CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
45 current_function (false),
53 some_location_specified (false),
54 symbol_containing_addr ()
56 OptionParsingStarting();
59 CommandObjectDisassemble::CommandOptions::~CommandOptions ()
64 CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
68 const int short_option = m_getopt_table[option_idx].val;
79 num_lines_context = StringConvert::ToUInt32(option_arg, 0, 0, &success);
81 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
85 num_instructions = StringConvert::ToUInt32(option_arg, 0, 0, &success);
87 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
96 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
97 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
98 if (start_addr != LLDB_INVALID_ADDRESS)
99 some_location_specified = true;
104 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
105 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
106 if (end_addr != LLDB_INVALID_ADDRESS)
107 some_location_specified = true;
111 func_name.assign (option_arg);
112 some_location_specified = true;
117 some_location_specified = true;
122 // Disassemble the current source line kind of implies showing mixed
123 // source code context.
125 some_location_specified = true;
129 plugin_name.assign (option_arg);
134 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
135 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
136 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
138 flavor_string.assign (option_arg);
141 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
149 current_function = true;
150 some_location_specified = true;
154 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
155 arch.SetTriple (option_arg);
160 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
161 symbol_containing_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
162 if (symbol_containing_addr != LLDB_INVALID_ADDRESS)
164 some_location_specified = true;
170 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
178 CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
182 num_lines_context = 0;
183 num_instructions = 0;
185 current_function = false;
188 start_addr = LLDB_INVALID_ADDRESS;
189 end_addr = LLDB_INVALID_ADDRESS;
190 symbol_containing_addr = LLDB_INVALID_ADDRESS;
194 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
196 // This is a hack till we get the ability to specify features based on architecture. For now GetDisassemblyFlavor
197 // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
198 // only disassembler plugin we have...
201 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
202 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
204 flavor_string.assign(target->GetDisassemblyFlavor());
207 flavor_string.assign ("default");
211 flavor_string.assign("default");
214 some_location_specified = false;
218 CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
220 if (!some_location_specified)
221 current_function = true;
226 const OptionDefinition*
227 CommandObjectDisassemble::CommandOptions::GetDefinitions ()
229 return g_option_table;
233 CommandObjectDisassemble::CommandOptions::g_option_table[] =
235 { LLDB_OPT_SET_ALL, false, "bytes" , 'b', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
236 { LLDB_OPT_SET_ALL, false, "context" , 'C', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
237 { LLDB_OPT_SET_ALL, false, "mixed" , 'm', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
238 { LLDB_OPT_SET_ALL, false, "raw" , 'r', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
239 { LLDB_OPT_SET_ALL, false, "plugin" , 'P', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
240 { LLDB_OPT_SET_ALL, false, "flavor" , 'F', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
241 "Currently the only valid options are default, and for Intel"
242 " architectures, att and intel."},
243 { LLDB_OPT_SET_ALL, false, "arch" , 'A', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
245 LLDB_OPT_SET_2 , true , "start-address", 's', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
246 { LLDB_OPT_SET_1 , false, "end-address" , 'e', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
250 LLDB_OPT_SET_5 , false, "count" , 'c', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
251 { LLDB_OPT_SET_3 , false, "name" , 'n', OptionParser::eRequiredArgument , NULL, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
252 "Disassemble entire contents of the given function name."},
253 { LLDB_OPT_SET_4 , false, "frame" , 'f', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
254 { LLDB_OPT_SET_5 , false, "pc" , 'p', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
255 { LLDB_OPT_SET_6 , false, "line" , 'l', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there is debug line table information, else disassemble around the pc."},
256 { LLDB_OPT_SET_7 , false, "address" , 'a', OptionParser::eRequiredArgument , NULL, NULL, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
257 { 0 , false, NULL , 0, 0 , NULL, NULL, 0, eArgTypeNone, NULL }
262 //-------------------------------------------------------------------------
263 // CommandObjectDisassemble
264 //-------------------------------------------------------------------------
266 CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
267 CommandObjectParsed (interpreter,
269 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
270 "disassemble [<cmd-options>]"),
271 m_options (interpreter)
275 CommandObjectDisassemble::~CommandObjectDisassemble()
280 CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
282 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
285 result.AppendError ("invalid target, create a debug target using the 'target create' command");
286 result.SetStatus (eReturnStatusFailed);
289 if (!m_options.arch.IsValid())
290 m_options.arch = target->GetArchitecture();
292 if (!m_options.arch.IsValid())
294 result.AppendError ("use the --arch option or set the target architecure to disassemble");
295 result.SetStatus (eReturnStatusFailed);
299 const char *plugin_name = m_options.GetPluginName ();
300 const char *flavor_string = m_options.GetFlavorString();
302 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
308 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
310 m_options.arch.GetArchitectureName());
313 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
314 m_options.arch.GetArchitectureName());
315 result.SetStatus (eReturnStatusFailed);
318 else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
319 result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
321 result.SetStatus (eReturnStatusSuccessFinishResult);
323 if (command.GetArgumentCount() != 0)
325 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
326 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
327 result.SetStatus (eReturnStatusFailed);
331 if (m_options.show_mixed && m_options.num_lines_context == 0)
332 m_options.num_lines_context = 1;
334 // Always show the PC in the disassembly
335 uint32_t options = Disassembler::eOptionMarkPCAddress;
337 // Mark the source line for the current PC only if we are doing mixed source and assembly
338 if (m_options.show_mixed)
339 options |= Disassembler::eOptionMarkPCSourceLine;
341 if (m_options.show_bytes)
342 options |= Disassembler::eOptionShowBytes;
345 options |= Disassembler::eOptionRawOuput;
347 if (!m_options.func_name.empty())
349 ConstString name(m_options.func_name.c_str());
351 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
358 m_options.num_instructions,
359 m_options.show_mixed ? m_options.num_lines_context : 0,
361 result.GetOutputStream()))
363 result.SetStatus (eReturnStatusSuccessFinishResult);
367 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
368 result.SetStatus (eReturnStatusFailed);
373 std::vector<AddressRange> ranges;
375 StackFrame *frame = m_exe_ctx.GetFramePtr();
376 if (m_options.frame_line)
380 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
381 result.SetStatus (eReturnStatusFailed);
384 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
385 if (pc_line_entry.IsValid())
387 range = pc_line_entry.range;
391 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
392 m_options.show_mixed = false;
395 else if (m_options.current_function)
399 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
400 result.SetStatus (eReturnStatusFailed);
403 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
406 range.GetBaseAddress() = symbol->GetAddress();
407 range.SetByteSize(symbol->GetByteSize());
411 // Did the "m_options.frame_line" find a valid range already? If so
413 if (range.GetByteSize() == 0)
419 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
420 result.SetStatus (eReturnStatusFailed);
423 range.GetBaseAddress() = frame->GetFrameCodeAddress();
424 if (m_options.num_instructions == 0)
426 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
427 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
429 ranges.push_back(range);
433 range.GetBaseAddress().SetOffset (m_options.start_addr);
434 if (range.GetBaseAddress().IsValid())
436 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
438 if (m_options.end_addr <= m_options.start_addr)
440 result.AppendErrorWithFormat ("End address before start address.\n");
441 result.SetStatus (eReturnStatusFailed);
444 range.SetByteSize (m_options.end_addr - m_options.start_addr);
446 ranges.push_back(range);
450 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
453 if (!target->GetSectionLoadList().IsEmpty())
456 Address symbol_containing_address;
457 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
459 ModuleSP module_sp (symbol_containing_address.GetModule());
461 bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
462 module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc,
463 resolve_tail_call_address);
464 if (sc.function || sc.symbol)
466 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
479 result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
480 result.SetStatus (eReturnStatusFailed);
483 ranges.push_back(range);
487 for (lldb::ModuleSP module_sp : target->GetImages().Modules())
489 lldb::addr_t file_addr = m_options.symbol_containing_addr;
490 Address file_address;
491 if (module_sp->ResolveFileAddress(file_addr, file_address))
494 bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
495 module_sp->ResolveSymbolContextForAddress (file_address, eSymbolContextEverything, sc, resolve_tail_call_address);
496 if (sc.function || sc.symbol)
498 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
499 ranges.push_back(range);
510 ranges.push_back(range);
512 if (m_options.num_instructions != 0)
514 if (ranges.size() == 0)
516 // The default action is to disassemble the current frame function.
519 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
521 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
522 else if (sc.symbol && sc.symbol->ValueIsAddress())
523 range.GetBaseAddress() = sc.symbol->GetAddress();
525 range.GetBaseAddress() = frame->GetFrameCodeAddress();
528 if (!range.GetBaseAddress().IsValid())
530 result.AppendError ("invalid frame");
531 result.SetStatus (eReturnStatusFailed);
536 bool print_sc_header = ranges.size() > 1;
537 for (AddressRange cur_range : ranges)
539 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
544 cur_range.GetBaseAddress(),
545 m_options.num_instructions,
546 m_options.show_mixed ? m_options.num_lines_context : 0,
548 result.GetOutputStream()))
550 result.SetStatus (eReturnStatusSuccessFinishResult);
554 if (m_options.start_addr != LLDB_INVALID_ADDRESS)
555 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
556 else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
557 result.AppendErrorWithFormat ("Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n", m_options.symbol_containing_addr);
558 result.SetStatus (eReturnStatusFailed);
562 result.AppendMessage("\n");
566 if (ranges.size() == 0)
568 // The default action is to disassemble the current frame function.
571 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
573 range = sc.function->GetAddressRange();
574 else if (sc.symbol && sc.symbol->ValueIsAddress())
576 range.GetBaseAddress() = sc.symbol->GetAddress();
577 range.SetByteSize (sc.symbol->GetByteSize());
580 range.GetBaseAddress() = frame->GetFrameCodeAddress();
584 result.AppendError ("invalid frame");
585 result.SetStatus (eReturnStatusFailed);
588 ranges.push_back(range);
591 bool print_sc_header = ranges.size() > 1;
592 for (AddressRange cur_range : ranges)
594 if (cur_range.GetByteSize() == 0)
595 cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
597 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
603 m_options.num_instructions,
604 m_options.show_mixed ? m_options.num_lines_context : 0,
606 result.GetOutputStream()))
608 result.SetStatus (eReturnStatusSuccessFinishResult);
612 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
613 result.SetStatus (eReturnStatusFailed);
616 result.AppendMessage("\n");
621 return result.Succeeded();