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/Interpreter/Args.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 = Args::StringToUInt32(option_arg, 0, 0, &success);
81 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
85 num_instructions = Args::StringToUInt32(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, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
236 { LLDB_OPT_SET_ALL, false, "context" , 'C', OptionParser::eRequiredArgument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
237 { LLDB_OPT_SET_ALL, false, "mixed" , 'm', OptionParser::eNoArgument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
238 { LLDB_OPT_SET_ALL, false, "raw" , 'r', OptionParser::eNoArgument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
239 { LLDB_OPT_SET_ALL, false, "plugin" , 'P', OptionParser::eRequiredArgument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
240 { LLDB_OPT_SET_ALL, false, "flavor" , 'F', OptionParser::eRequiredArgument , 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, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
245 LLDB_OPT_SET_2 , true , "start-address", 's', OptionParser::eRequiredArgument , NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
246 { LLDB_OPT_SET_1 , false, "end-address" , 'e', OptionParser::eRequiredArgument , NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
250 LLDB_OPT_SET_5 , false, "count" , 'c', OptionParser::eRequiredArgument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
251 { LLDB_OPT_SET_3 , false, "name" , 'n', OptionParser::eRequiredArgument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
252 "Disassemble entire contents of the given function name."},
253 { LLDB_OPT_SET_4 , false, "frame" , 'f', OptionParser::eNoArgument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
254 { LLDB_OPT_SET_5 , false, "pc" , 'p', OptionParser::eNoArgument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
255 { LLDB_OPT_SET_6 , false, "line" , 'l', OptionParser::eNoArgument , 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, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
257 { 0 , false, NULL , 0, 0 , 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);
374 StackFrame *frame = m_exe_ctx.GetFramePtr();
375 if (m_options.frame_line)
379 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
380 result.SetStatus (eReturnStatusFailed);
383 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
384 if (pc_line_entry.IsValid())
386 range = pc_line_entry.range;
390 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
391 m_options.show_mixed = false;
394 else if (m_options.current_function)
398 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
399 result.SetStatus (eReturnStatusFailed);
402 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
405 range.GetBaseAddress() = symbol->GetAddress();
406 range.SetByteSize(symbol->GetByteSize());
410 // Did the "m_options.frame_line" find a valid range already? If so
412 if (range.GetByteSize() == 0)
418 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
419 result.SetStatus (eReturnStatusFailed);
422 range.GetBaseAddress() = frame->GetFrameCodeAddress();
423 if (m_options.num_instructions == 0)
425 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
426 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
431 range.GetBaseAddress().SetOffset (m_options.start_addr);
432 if (range.GetBaseAddress().IsValid())
434 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
436 if (m_options.end_addr <= m_options.start_addr)
438 result.AppendErrorWithFormat ("End address before start address.\n");
439 result.SetStatus (eReturnStatusFailed);
442 range.SetByteSize (m_options.end_addr - m_options.start_addr);
447 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
449 && !target->GetSectionLoadList().IsEmpty())
452 Address symbol_containing_address;
453 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
455 ModuleSP module_sp (symbol_containing_address.GetModule());
457 bool resolve_tail_call_address = true; // PC can be one past the address range of the function.
458 module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc,
459 resolve_tail_call_address);
460 if (sc.function || sc.symbol)
462 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
475 result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
476 result.SetStatus (eReturnStatusFailed);
484 if (m_options.num_instructions != 0)
486 if (!range.GetBaseAddress().IsValid())
488 // The default action is to disassemble the current frame function.
491 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
493 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
494 else if (sc.symbol && sc.symbol->ValueIsAddress())
495 range.GetBaseAddress() = sc.symbol->GetAddress();
497 range.GetBaseAddress() = frame->GetFrameCodeAddress();
500 if (!range.GetBaseAddress().IsValid())
502 result.AppendError ("invalid frame");
503 result.SetStatus (eReturnStatusFailed);
508 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
513 range.GetBaseAddress(),
514 m_options.num_instructions,
515 m_options.show_mixed ? m_options.num_lines_context : 0,
517 result.GetOutputStream()))
519 result.SetStatus (eReturnStatusSuccessFinishResult);
523 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
524 result.SetStatus (eReturnStatusFailed);
529 if (!range.GetBaseAddress().IsValid())
531 // The default action is to disassemble the current frame function.
534 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
536 range = sc.function->GetAddressRange();
537 else if (sc.symbol && sc.symbol->ValueIsAddress())
539 range.GetBaseAddress() = sc.symbol->GetAddress();
540 range.SetByteSize (sc.symbol->GetByteSize());
543 range.GetBaseAddress() = frame->GetFrameCodeAddress();
547 result.AppendError ("invalid frame");
548 result.SetStatus (eReturnStatusFailed);
552 if (range.GetByteSize() == 0)
553 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
555 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
561 m_options.num_instructions,
562 m_options.show_mixed ? m_options.num_lines_context : 0,
564 result.GetOutputStream()))
566 result.SetStatus (eReturnStatusSuccessFinishResult);
570 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
571 result.SetStatus (eReturnStatusFailed);
576 return result.Succeeded();