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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "CommandObjectDisassemble.h"
15 #include "lldb/Core/AddressRange.h"
16 #include "lldb/Core/Disassembler.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/SourceManager.h"
19 #include "lldb/Host/OptionParser.h"
20 #include "lldb/Interpreter/CommandCompletions.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Interpreter/CommandReturnObject.h"
23 #include "lldb/Interpreter/Options.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/Symbol.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/SectionLoadList.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
31 #define DEFAULT_DISASM_BYTE_SIZE 32
32 #define DEFAULT_DISASM_NUM_INS 4
35 using namespace lldb_private;
37 static OptionDefinition g_disassemble_options[] = {
39 { LLDB_OPT_SET_ALL, false, "bytes", 'b', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Show opcode bytes when disassembling." },
40 { LLDB_OPT_SET_ALL, false, "context", 'C', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of context lines of source to show." },
41 { LLDB_OPT_SET_ALL, false, "mixed", 'm', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable mixed source and assembly display." },
42 { LLDB_OPT_SET_ALL, false, "raw", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Print raw disassembly with no symbol information." },
43 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use." },
44 { LLDB_OPT_SET_ALL, false, "flavor", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
45 "Currently the only valid options are default, and for Intel "
46 "architectures, att and intel." },
47 { LLDB_OPT_SET_ALL, false, "arch", 'A', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeArchitecture, "Specify the architecture to use from cross disassembly." },
49 LLDB_OPT_SET_2, true, "start-address", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to start disassembling." },
50 { LLDB_OPT_SET_1, false, "end-address", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling." },
54 LLDB_OPT_SET_5, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of instructions to display." },
55 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name." },
56 { LLDB_OPT_SET_4, false, "frame", 'f', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble from the start of the current frame's function." },
57 { LLDB_OPT_SET_5, false, "pc", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble around the current pc." },
58 { LLDB_OPT_SET_6, false, "line", 'l', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there is debug line "
59 "table information, else disassemble around the pc." },
60 { LLDB_OPT_SET_7, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address." },
64 CommandObjectDisassemble::CommandOptions::CommandOptions()
65 : Options(), num_lines_context(0), num_instructions(0), func_name(),
66 current_function(false), start_addr(), end_addr(), at_pc(false),
67 frame_line(false), plugin_name(), flavor_string(), arch(),
68 some_location_specified(false), symbol_containing_addr() {
69 OptionParsingStarting(nullptr);
72 CommandObjectDisassemble::CommandOptions::~CommandOptions() = default;
74 Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
75 uint32_t option_idx, llvm::StringRef option_arg,
76 ExecutionContext *execution_context) {
79 const int short_option = m_getopt_table[option_idx].val;
81 switch (short_option) {
87 if (option_arg.getAsInteger(0, num_lines_context))
88 error.SetErrorStringWithFormat("invalid num context lines string: \"%s\"",
89 option_arg.str().c_str());
93 if (option_arg.getAsInteger(0, num_instructions))
94 error.SetErrorStringWithFormat(
95 "invalid num of instructions string: \"%s\"",
96 option_arg.str().c_str());
104 start_addr = Args::StringToAddress(execution_context, option_arg,
105 LLDB_INVALID_ADDRESS, &error);
106 if (start_addr != LLDB_INVALID_ADDRESS)
107 some_location_specified = true;
110 end_addr = Args::StringToAddress(execution_context, option_arg,
111 LLDB_INVALID_ADDRESS, &error);
112 if (end_addr != LLDB_INVALID_ADDRESS)
113 some_location_specified = true;
117 func_name.assign(option_arg);
118 some_location_specified = true;
123 some_location_specified = true;
128 // Disassemble the current source line kind of implies showing mixed
129 // source code context.
131 some_location_specified = true;
135 plugin_name.assign(option_arg);
140 execution_context ? execution_context->GetTargetSP() : TargetSP();
141 if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() ==
143 target_sp->GetArchitecture().GetTriple().getArch() ==
144 llvm::Triple::x86_64)) {
145 flavor_string.assign(option_arg);
147 error.SetErrorStringWithFormat("Disassembler flavors are currently only "
148 "supported for x86 and x86_64 targets.");
157 current_function = true;
158 some_location_specified = true;
162 if (execution_context) {
164 execution_context ? execution_context->GetTargetSP() : TargetSP();
165 auto platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP();
166 if (!arch.SetTriple(option_arg, platform_sp.get()))
167 arch.SetTriple(option_arg);
172 symbol_containing_addr = Args::StringToAddress(
173 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
174 if (symbol_containing_addr != LLDB_INVALID_ADDRESS) {
175 some_location_specified = true;
180 error.SetErrorStringWithFormat("unrecognized short option '%c'",
188 void CommandObjectDisassemble::CommandOptions::OptionParsingStarting(
189 ExecutionContext *execution_context) {
192 num_lines_context = 0;
193 num_instructions = 0;
195 current_function = false;
198 start_addr = LLDB_INVALID_ADDRESS;
199 end_addr = LLDB_INVALID_ADDRESS;
200 symbol_containing_addr = LLDB_INVALID_ADDRESS;
205 execution_context ? execution_context->GetTargetPtr() : nullptr;
207 // This is a hack till we get the ability to specify features based on
208 // architecture. For now GetDisassemblyFlavor
209 // is really only valid for x86 (and for the llvm assembler plugin, but I'm
210 // papering over that since that is the
211 // only disassembler plugin we have...
213 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 ||
214 target->GetArchitecture().GetTriple().getArch() ==
215 llvm::Triple::x86_64) {
216 flavor_string.assign(target->GetDisassemblyFlavor());
218 flavor_string.assign("default");
221 flavor_string.assign("default");
224 some_location_specified = false;
227 Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished(
228 ExecutionContext *execution_context) {
229 if (!some_location_specified)
230 current_function = true;
234 llvm::ArrayRef<OptionDefinition>
235 CommandObjectDisassemble::CommandOptions::GetDefinitions() {
236 return llvm::makeArrayRef(g_disassemble_options);
239 //-------------------------------------------------------------------------
240 // CommandObjectDisassemble
241 //-------------------------------------------------------------------------
243 CommandObjectDisassemble::CommandObjectDisassemble(
244 CommandInterpreter &interpreter)
245 : CommandObjectParsed(
246 interpreter, "disassemble",
247 "Disassemble specified instructions in the current target. "
248 "Defaults to the current function for the current thread and "
250 "disassemble [<cmd-options>]"),
253 CommandObjectDisassemble::~CommandObjectDisassemble() = default;
255 bool CommandObjectDisassemble::DoExecute(Args &command,
256 CommandReturnObject &result) {
257 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
258 if (target == nullptr) {
259 result.AppendError("invalid target, create a debug target using the "
260 "'target create' command");
261 result.SetStatus(eReturnStatusFailed);
264 if (!m_options.arch.IsValid())
265 m_options.arch = target->GetArchitecture();
267 if (!m_options.arch.IsValid()) {
269 "use the --arch option or set the target architecture to disassemble");
270 result.SetStatus(eReturnStatusFailed);
274 const char *plugin_name = m_options.GetPluginName();
275 const char *flavor_string = m_options.GetFlavorString();
277 DisassemblerSP disassembler =
278 Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
282 result.AppendErrorWithFormat(
283 "Unable to find Disassembler plug-in named '%s' that supports the "
284 "'%s' architecture.\n",
285 plugin_name, m_options.arch.GetArchitectureName());
287 result.AppendErrorWithFormat(
288 "Unable to find Disassembler plug-in for the '%s' architecture.\n",
289 m_options.arch.GetArchitectureName());
290 result.SetStatus(eReturnStatusFailed);
292 } else if (flavor_string != nullptr &&
293 !disassembler->FlavorValidForArchSpec(m_options.arch,
295 result.AppendWarningWithFormat(
296 "invalid disassembler flavor \"%s\", using default.\n", flavor_string);
298 result.SetStatus(eReturnStatusSuccessFinishResult);
300 if (!command.empty()) {
301 result.AppendErrorWithFormat(
302 "\"disassemble\" arguments are specified as options.\n");
303 const int terminal_width =
304 GetCommandInterpreter().GetDebugger().GetTerminalWidth();
305 GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this,
307 result.SetStatus(eReturnStatusFailed);
311 if (m_options.show_mixed && m_options.num_lines_context == 0)
312 m_options.num_lines_context = 2;
314 // Always show the PC in the disassembly
315 uint32_t options = Disassembler::eOptionMarkPCAddress;
317 // Mark the source line for the current PC only if we are doing mixed source
319 if (m_options.show_mixed)
320 options |= Disassembler::eOptionMarkPCSourceLine;
322 if (m_options.show_bytes)
323 options |= Disassembler::eOptionShowBytes;
326 options |= Disassembler::eOptionRawOuput;
328 if (!m_options.func_name.empty()) {
329 ConstString name(m_options.func_name.c_str());
331 if (Disassembler::Disassemble(
332 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
333 flavor_string, m_exe_ctx, name,
335 m_options.num_instructions, m_options.show_mixed,
336 m_options.show_mixed ? m_options.num_lines_context : 0, options,
337 result.GetOutputStream())) {
338 result.SetStatus(eReturnStatusSuccessFinishResult);
340 result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n",
342 result.SetStatus(eReturnStatusFailed);
345 std::vector<AddressRange> ranges;
347 StackFrame *frame = m_exe_ctx.GetFramePtr();
348 if (m_options.frame_line) {
349 if (frame == nullptr) {
350 result.AppendError("Cannot disassemble around the current line without "
351 "a selected frame.\n");
352 result.SetStatus(eReturnStatusFailed);
355 LineEntry pc_line_entry(
356 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
357 if (pc_line_entry.IsValid()) {
358 range = pc_line_entry.range;
361 true; // No line entry, so just disassemble around the current pc
362 m_options.show_mixed = false;
364 } else if (m_options.current_function) {
365 if (frame == nullptr) {
366 result.AppendError("Cannot disassemble around the current function "
367 "without a selected frame.\n");
368 result.SetStatus(eReturnStatusFailed);
371 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
373 range.GetBaseAddress() = symbol->GetAddress();
374 range.SetByteSize(symbol->GetByteSize());
378 // Did the "m_options.frame_line" find a valid range already? If so
380 if (range.GetByteSize() == 0) {
381 if (m_options.at_pc) {
382 if (frame == nullptr) {
383 result.AppendError("Cannot disassemble around the current PC without "
384 "a selected frame.\n");
385 result.SetStatus(eReturnStatusFailed);
388 range.GetBaseAddress() = frame->GetFrameCodeAddress();
389 if (m_options.num_instructions == 0) {
390 // Disassembling at the PC always disassembles some number of
391 // instructions (not the whole function).
392 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
394 ranges.push_back(range);
396 range.GetBaseAddress().SetOffset(m_options.start_addr);
397 if (range.GetBaseAddress().IsValid()) {
398 if (m_options.end_addr != LLDB_INVALID_ADDRESS) {
399 if (m_options.end_addr <= m_options.start_addr) {
400 result.AppendErrorWithFormat(
401 "End address before start address.\n");
402 result.SetStatus(eReturnStatusFailed);
405 range.SetByteSize(m_options.end_addr - m_options.start_addr);
407 ranges.push_back(range);
409 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS &&
411 if (!target->GetSectionLoadList().IsEmpty()) {
413 Address symbol_containing_address;
414 if (target->GetSectionLoadList().ResolveLoadAddress(
415 m_options.symbol_containing_addr,
416 symbol_containing_address)) {
417 ModuleSP module_sp(symbol_containing_address.GetModule());
419 bool resolve_tail_call_address = true; // PC can be one past the
420 // address range of the
422 module_sp->ResolveSymbolContextForAddress(
423 symbol_containing_address, eSymbolContextEverything, sc,
424 resolve_tail_call_address);
425 if (sc.function || sc.symbol) {
426 sc.GetAddressRange(eSymbolContextFunction |
427 eSymbolContextSymbol,
436 result.AppendErrorWithFormat(
437 "Could not find function bounds for address 0x%" PRIx64
439 m_options.symbol_containing_addr);
440 result.SetStatus(eReturnStatusFailed);
443 ranges.push_back(range);
445 for (lldb::ModuleSP module_sp : target->GetImages().Modules()) {
446 lldb::addr_t file_addr = m_options.symbol_containing_addr;
447 Address file_address;
448 if (module_sp->ResolveFileAddress(file_addr, file_address)) {
450 bool resolve_tail_call_address = true; // PC can be one past
451 // the address range of
453 module_sp->ResolveSymbolContextForAddress(
454 file_address, eSymbolContextEverything, sc,
455 resolve_tail_call_address);
456 if (sc.function || sc.symbol) {
457 sc.GetAddressRange(eSymbolContextFunction |
458 eSymbolContextSymbol,
460 ranges.push_back(range);
469 ranges.push_back(range);
471 if (m_options.num_instructions != 0) {
472 if (ranges.empty()) {
473 // The default action is to disassemble the current frame function.
475 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
476 eSymbolContextSymbol));
478 range.GetBaseAddress() =
479 sc.function->GetAddressRange().GetBaseAddress();
480 else if (sc.symbol && sc.symbol->ValueIsAddress())
481 range.GetBaseAddress() = sc.symbol->GetAddress();
483 range.GetBaseAddress() = frame->GetFrameCodeAddress();
486 if (!range.GetBaseAddress().IsValid()) {
487 result.AppendError("invalid frame");
488 result.SetStatus(eReturnStatusFailed);
493 bool print_sc_header = ranges.size() > 1;
494 for (AddressRange cur_range : ranges) {
495 if (Disassembler::Disassemble(
496 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
497 flavor_string, m_exe_ctx, cur_range.GetBaseAddress(),
498 m_options.num_instructions, m_options.show_mixed,
499 m_options.show_mixed ? m_options.num_lines_context : 0, options,
500 result.GetOutputStream())) {
501 result.SetStatus(eReturnStatusSuccessFinishResult);
503 if (m_options.start_addr != LLDB_INVALID_ADDRESS)
504 result.AppendErrorWithFormat(
505 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
506 m_options.start_addr);
507 else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
508 result.AppendErrorWithFormat(
509 "Failed to disassemble memory in function at 0x%8.8" PRIx64
511 m_options.symbol_containing_addr);
512 result.SetStatus(eReturnStatusFailed);
516 result.AppendMessage("\n");
518 if (ranges.empty()) {
519 // The default action is to disassemble the current frame function.
521 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
522 eSymbolContextSymbol));
524 range = sc.function->GetAddressRange();
525 else if (sc.symbol && sc.symbol->ValueIsAddress()) {
526 range.GetBaseAddress() = sc.symbol->GetAddress();
527 range.SetByteSize(sc.symbol->GetByteSize());
529 range.GetBaseAddress() = frame->GetFrameCodeAddress();
531 result.AppendError("invalid frame");
532 result.SetStatus(eReturnStatusFailed);
535 ranges.push_back(range);
538 bool print_sc_header = ranges.size() > 1;
539 for (AddressRange cur_range : ranges) {
540 if (cur_range.GetByteSize() == 0)
541 cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
543 if (Disassembler::Disassemble(
544 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
545 flavor_string, m_exe_ctx, cur_range, m_options.num_instructions,
546 m_options.show_mixed,
547 m_options.show_mixed ? m_options.num_lines_context : 0, options,
548 result.GetOutputStream())) {
549 result.SetStatus(eReturnStatusSuccessFinishResult);
551 result.AppendErrorWithFormat(
552 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
553 m_options.start_addr);
554 result.SetStatus(eReturnStatusFailed);
557 result.AppendMessage("\n");
562 return result.Succeeded();