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/StackFrame.h"
31 #include "lldb/Target/Target.h"
33 #define DEFAULT_DISASM_BYTE_SIZE 32
34 #define DEFAULT_DISASM_NUM_INS 4
37 using namespace lldb_private;
39 CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
44 current_function (false),
52 some_location_specified (false),
53 symbol_containing_addr ()
55 OptionParsingStarting();
58 CommandObjectDisassemble::CommandOptions::~CommandOptions ()
63 CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
67 const int short_option = m_getopt_table[option_idx].val;
78 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
80 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
84 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
86 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
95 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
96 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
97 if (start_addr != LLDB_INVALID_ADDRESS)
98 some_location_specified = true;
103 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
104 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
105 if (end_addr != LLDB_INVALID_ADDRESS)
106 some_location_specified = true;
110 func_name.assign (option_arg);
111 some_location_specified = true;
116 some_location_specified = true;
121 // Disassemble the current source line kind of implies showing mixed
122 // source code context.
124 some_location_specified = true;
128 plugin_name.assign (option_arg);
133 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
134 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
135 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
137 flavor_string.assign (option_arg);
140 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
148 current_function = true;
149 some_location_specified = true;
153 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
154 arch.SetTriple (option_arg);
159 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
160 symbol_containing_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
161 if (symbol_containing_addr != LLDB_INVALID_ADDRESS)
163 some_location_specified = true;
169 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
177 CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
181 num_lines_context = 0;
182 num_instructions = 0;
184 current_function = false;
187 start_addr = LLDB_INVALID_ADDRESS;
188 end_addr = LLDB_INVALID_ADDRESS;
189 symbol_containing_addr = LLDB_INVALID_ADDRESS;
193 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
195 // This is a hack till we get the ability to specify features based on architecture. For now GetDisassemblyFlavor
196 // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
197 // only disassembler plugin we have...
200 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
201 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
203 flavor_string.assign(target->GetDisassemblyFlavor());
206 flavor_string.assign ("default");
210 flavor_string.assign("default");
213 some_location_specified = false;
217 CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
219 if (!some_location_specified)
220 current_function = true;
225 const OptionDefinition*
226 CommandObjectDisassemble::CommandOptions::GetDefinitions ()
228 return g_option_table;
232 CommandObjectDisassemble::CommandOptions::g_option_table[] =
234 { LLDB_OPT_SET_ALL, false, "bytes" , 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
235 { LLDB_OPT_SET_ALL, false, "context" , 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
236 { LLDB_OPT_SET_ALL, false, "mixed" , 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
237 { LLDB_OPT_SET_ALL, false, "raw" , 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
238 { LLDB_OPT_SET_ALL, false, "plugin" , 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
239 { LLDB_OPT_SET_ALL, false, "flavor" , 'F', required_argument , NULL, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
240 "Currently the only valid options are default, and for Intel"
241 " architectures, att and intel."},
242 { LLDB_OPT_SET_ALL, false, "arch" , 'A', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
244 LLDB_OPT_SET_2 , true , "start-address", 's', required_argument , NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
245 { LLDB_OPT_SET_1 , false, "end-address" , 'e', required_argument , NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
249 LLDB_OPT_SET_5 , false, "count" , 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
250 { LLDB_OPT_SET_3 , false, "name" , 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
251 "Disassemble entire contents of the given function name."},
252 { LLDB_OPT_SET_4 , false, "frame" , 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
253 { LLDB_OPT_SET_5 , false, "pc" , 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
254 { LLDB_OPT_SET_6 , false, "line" , 'l', no_argument , NULL, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there debug line table information, else disasemble around the pc."},
255 { LLDB_OPT_SET_7 , false, "address" , 'a', required_argument , NULL, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address."},
256 { 0 , false, NULL , 0, 0 , NULL, 0, eArgTypeNone, NULL }
261 //-------------------------------------------------------------------------
262 // CommandObjectDisassemble
263 //-------------------------------------------------------------------------
265 CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
266 CommandObjectParsed (interpreter,
268 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
269 "disassemble [<cmd-options>]"),
270 m_options (interpreter)
274 CommandObjectDisassemble::~CommandObjectDisassemble()
279 CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
281 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
284 result.AppendError ("invalid target, create a debug target using the 'target create' command");
285 result.SetStatus (eReturnStatusFailed);
288 if (!m_options.arch.IsValid())
289 m_options.arch = target->GetArchitecture();
291 if (!m_options.arch.IsValid())
293 result.AppendError ("use the --arch option or set the target architecure to disassemble");
294 result.SetStatus (eReturnStatusFailed);
298 const char *plugin_name = m_options.GetPluginName ();
299 const char *flavor_string = m_options.GetFlavorString();
301 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
307 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
309 m_options.arch.GetArchitectureName());
312 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
313 m_options.arch.GetArchitectureName());
314 result.SetStatus (eReturnStatusFailed);
317 else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
318 result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
320 result.SetStatus (eReturnStatusSuccessFinishResult);
322 if (command.GetArgumentCount() != 0)
324 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
325 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
326 result.SetStatus (eReturnStatusFailed);
330 if (m_options.show_mixed && m_options.num_lines_context == 0)
331 m_options.num_lines_context = 1;
333 // Always show the PC in the disassembly
334 uint32_t options = Disassembler::eOptionMarkPCAddress;
336 // Mark the source line for the current PC only if we are doing mixed source and assembly
337 if (m_options.show_mixed)
338 options |= Disassembler::eOptionMarkPCSourceLine;
340 if (m_options.show_bytes)
341 options |= Disassembler::eOptionShowBytes;
344 options |= Disassembler::eOptionRawOuput;
346 if (!m_options.func_name.empty())
348 ConstString name(m_options.func_name.c_str());
350 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
357 m_options.num_instructions,
358 m_options.show_mixed ? m_options.num_lines_context : 0,
360 result.GetOutputStream()))
362 result.SetStatus (eReturnStatusSuccessFinishResult);
366 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
367 result.SetStatus (eReturnStatusFailed);
373 StackFrame *frame = m_exe_ctx.GetFramePtr();
374 if (m_options.frame_line)
378 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
379 result.SetStatus (eReturnStatusFailed);
382 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
383 if (pc_line_entry.IsValid())
385 range = pc_line_entry.range;
389 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
390 m_options.show_mixed = false;
393 else if (m_options.current_function)
397 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
398 result.SetStatus (eReturnStatusFailed);
401 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
404 range.GetBaseAddress() = symbol->GetAddress();
405 range.SetByteSize(symbol->GetByteSize());
409 // Did the "m_options.frame_line" find a valid range already? If so
411 if (range.GetByteSize() == 0)
417 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
418 result.SetStatus (eReturnStatusFailed);
421 range.GetBaseAddress() = frame->GetFrameCodeAddress();
422 if (m_options.num_instructions == 0)
424 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
425 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
430 range.GetBaseAddress().SetOffset (m_options.start_addr);
431 if (range.GetBaseAddress().IsValid())
433 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
435 if (m_options.end_addr <= m_options.start_addr)
437 result.AppendErrorWithFormat ("End address before start address.\n");
438 result.SetStatus (eReturnStatusFailed);
441 range.SetByteSize (m_options.end_addr - m_options.start_addr);
446 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS
448 && !target->GetSectionLoadList().IsEmpty())
451 Address symbol_containing_address;
452 if (target->GetSectionLoadList().ResolveLoadAddress (m_options.symbol_containing_addr, symbol_containing_address))
454 ModuleSP module_sp (symbol_containing_address.GetModule());
456 module_sp->ResolveSymbolContextForAddress (symbol_containing_address, eSymbolContextEverything, sc);
457 if (sc.function || sc.symbol)
459 sc.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, range);
472 result.AppendErrorWithFormat ("Could not find function bounds for address 0x%" PRIx64 "\n", m_options.symbol_containing_addr);
473 result.SetStatus (eReturnStatusFailed);
481 if (m_options.num_instructions != 0)
483 if (!range.GetBaseAddress().IsValid())
485 // The default action is to disassemble the current frame function.
488 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
490 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
491 else if (sc.symbol && sc.symbol->ValueIsAddress())
492 range.GetBaseAddress() = sc.symbol->GetAddress();
494 range.GetBaseAddress() = frame->GetFrameCodeAddress();
497 if (!range.GetBaseAddress().IsValid())
499 result.AppendError ("invalid frame");
500 result.SetStatus (eReturnStatusFailed);
505 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
510 range.GetBaseAddress(),
511 m_options.num_instructions,
512 m_options.show_mixed ? m_options.num_lines_context : 0,
514 result.GetOutputStream()))
516 result.SetStatus (eReturnStatusSuccessFinishResult);
520 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
521 result.SetStatus (eReturnStatusFailed);
526 if (!range.GetBaseAddress().IsValid())
528 // The default action is to disassemble the current frame function.
531 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
533 range = sc.function->GetAddressRange();
534 else if (sc.symbol && sc.symbol->ValueIsAddress())
536 range.GetBaseAddress() = sc.symbol->GetAddress();
537 range.SetByteSize (sc.symbol->GetByteSize());
540 range.GetBaseAddress() = frame->GetFrameCodeAddress();
544 result.AppendError ("invalid frame");
545 result.SetStatus (eReturnStatusFailed);
549 if (range.GetByteSize() == 0)
550 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
552 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
558 m_options.num_instructions,
559 m_options.show_mixed ? m_options.num_lines_context : 0,
561 result.GetOutputStream()))
563 result.SetStatus (eReturnStatusSuccessFinishResult);
567 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
568 result.SetStatus (eReturnStatusFailed);
573 return result.Succeeded();