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/OptionArgParser.h"
24 #include "lldb/Interpreter/Options.h"
25 #include "lldb/Symbol/Function.h"
26 #include "lldb/Symbol/Symbol.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/SectionLoadList.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
32 #define DEFAULT_DISASM_BYTE_SIZE 32
33 #define DEFAULT_DISASM_NUM_INS 4
36 using namespace lldb_private;
38 static OptionDefinition g_disassemble_options[] = {
40 { LLDB_OPT_SET_ALL, false, "bytes", 'b', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Show opcode bytes when disassembling." },
41 { LLDB_OPT_SET_ALL, false, "context", 'C', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of context lines of source to show." },
42 { LLDB_OPT_SET_ALL, false, "mixed", 'm', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Enable mixed source and assembly display." },
43 { LLDB_OPT_SET_ALL, false, "raw", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Print raw disassembly with no symbol information." },
44 { LLDB_OPT_SET_ALL, false, "plugin", 'P', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use." },
45 { LLDB_OPT_SET_ALL, false, "flavor", 'F', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
46 "Currently the only valid options are default, and for Intel "
47 "architectures, att and intel." },
48 { LLDB_OPT_SET_ALL, false, "arch", 'A', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeArchitecture, "Specify the architecture to use from cross disassembly." },
50 LLDB_OPT_SET_2, true, "start-address", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to start disassembling." },
51 { LLDB_OPT_SET_1, false, "end-address", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling." },
55 LLDB_OPT_SET_5, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNumLines, "Number of instructions to display." },
56 { LLDB_OPT_SET_3, false, "name", 'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name." },
57 { LLDB_OPT_SET_4, false, "frame", 'f', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble from the start of the current frame's function." },
58 { LLDB_OPT_SET_5, false, "pc", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Disassemble around the current pc." },
59 { 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 "
60 "table information, else disassemble around the pc." },
61 { LLDB_OPT_SET_7, false, "address", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeAddressOrExpression, "Disassemble function containing this address." },
65 CommandObjectDisassemble::CommandOptions::CommandOptions()
66 : Options(), num_lines_context(0), num_instructions(0), func_name(),
67 current_function(false), start_addr(), end_addr(), at_pc(false),
68 frame_line(false), plugin_name(), flavor_string(), arch(),
69 some_location_specified(false), symbol_containing_addr() {
70 OptionParsingStarting(nullptr);
73 CommandObjectDisassemble::CommandOptions::~CommandOptions() = default;
75 Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
76 uint32_t option_idx, llvm::StringRef option_arg,
77 ExecutionContext *execution_context) {
80 const int short_option = m_getopt_table[option_idx].val;
82 switch (short_option) {
88 if (option_arg.getAsInteger(0, num_lines_context))
89 error.SetErrorStringWithFormat("invalid num context lines string: \"%s\"",
90 option_arg.str().c_str());
94 if (option_arg.getAsInteger(0, num_instructions))
95 error.SetErrorStringWithFormat(
96 "invalid num of instructions string: \"%s\"",
97 option_arg.str().c_str());
105 start_addr = OptionArgParser::ToAddress(execution_context, option_arg,
106 LLDB_INVALID_ADDRESS, &error);
107 if (start_addr != LLDB_INVALID_ADDRESS)
108 some_location_specified = true;
111 end_addr = OptionArgParser::ToAddress(execution_context, option_arg,
112 LLDB_INVALID_ADDRESS, &error);
113 if (end_addr != LLDB_INVALID_ADDRESS)
114 some_location_specified = true;
118 func_name.assign(option_arg);
119 some_location_specified = true;
124 some_location_specified = true;
129 // Disassemble the current source line kind of implies showing mixed source
132 some_location_specified = true;
136 plugin_name.assign(option_arg);
141 execution_context ? execution_context->GetTargetSP() : TargetSP();
142 if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() ==
144 target_sp->GetArchitecture().GetTriple().getArch() ==
145 llvm::Triple::x86_64)) {
146 flavor_string.assign(option_arg);
148 error.SetErrorStringWithFormat("Disassembler flavors are currently only "
149 "supported for x86 and x86_64 targets.");
158 current_function = true;
159 some_location_specified = true;
163 if (execution_context) {
164 const auto &target_sp = execution_context->GetTargetSP();
165 auto platform_ptr = target_sp ? target_sp->GetPlatform().get() : nullptr;
166 arch = Platform::GetAugmentedArchSpec(platform_ptr, option_arg);
171 symbol_containing_addr = OptionArgParser::ToAddress(
172 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
173 if (symbol_containing_addr != LLDB_INVALID_ADDRESS) {
174 some_location_specified = true;
179 error.SetErrorStringWithFormat("unrecognized short option '%c'",
187 void CommandObjectDisassemble::CommandOptions::OptionParsingStarting(
188 ExecutionContext *execution_context) {
191 num_lines_context = 0;
192 num_instructions = 0;
194 current_function = false;
197 start_addr = LLDB_INVALID_ADDRESS;
198 end_addr = LLDB_INVALID_ADDRESS;
199 symbol_containing_addr = LLDB_INVALID_ADDRESS;
204 execution_context ? execution_context->GetTargetPtr() : nullptr;
206 // This is a hack till we get the ability to specify features based on
207 // architecture. For now GetDisassemblyFlavor is really only valid for x86
208 // (and for the llvm assembler plugin, but I'm papering over that since that
209 // is the only disassembler plugin we have...
211 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 ||
212 target->GetArchitecture().GetTriple().getArch() ==
213 llvm::Triple::x86_64) {
214 flavor_string.assign(target->GetDisassemblyFlavor());
216 flavor_string.assign("default");
219 flavor_string.assign("default");
222 some_location_specified = false;
225 Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished(
226 ExecutionContext *execution_context) {
227 if (!some_location_specified)
228 current_function = true;
232 llvm::ArrayRef<OptionDefinition>
233 CommandObjectDisassemble::CommandOptions::GetDefinitions() {
234 return llvm::makeArrayRef(g_disassemble_options);
237 //-------------------------------------------------------------------------
238 // CommandObjectDisassemble
239 //-------------------------------------------------------------------------
241 CommandObjectDisassemble::CommandObjectDisassemble(
242 CommandInterpreter &interpreter)
243 : CommandObjectParsed(
244 interpreter, "disassemble",
245 "Disassemble specified instructions in the current target. "
246 "Defaults to the current function for the current thread and "
248 "disassemble [<cmd-options>]"),
251 CommandObjectDisassemble::~CommandObjectDisassemble() = default;
253 bool CommandObjectDisassemble::DoExecute(Args &command,
254 CommandReturnObject &result) {
255 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
256 if (target == nullptr) {
257 result.AppendError("invalid target, create a debug target using the "
258 "'target create' command");
259 result.SetStatus(eReturnStatusFailed);
262 if (!m_options.arch.IsValid())
263 m_options.arch = target->GetArchitecture();
265 if (!m_options.arch.IsValid()) {
267 "use the --arch option or set the target architecture to disassemble");
268 result.SetStatus(eReturnStatusFailed);
272 const char *plugin_name = m_options.GetPluginName();
273 const char *flavor_string = m_options.GetFlavorString();
275 DisassemblerSP disassembler =
276 Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
280 result.AppendErrorWithFormat(
281 "Unable to find Disassembler plug-in named '%s' that supports the "
282 "'%s' architecture.\n",
283 plugin_name, m_options.arch.GetArchitectureName());
285 result.AppendErrorWithFormat(
286 "Unable to find Disassembler plug-in for the '%s' architecture.\n",
287 m_options.arch.GetArchitectureName());
288 result.SetStatus(eReturnStatusFailed);
290 } else if (flavor_string != nullptr &&
291 !disassembler->FlavorValidForArchSpec(m_options.arch,
293 result.AppendWarningWithFormat(
294 "invalid disassembler flavor \"%s\", using default.\n", flavor_string);
296 result.SetStatus(eReturnStatusSuccessFinishResult);
298 if (!command.empty()) {
299 result.AppendErrorWithFormat(
300 "\"disassemble\" arguments are specified as options.\n");
301 const int terminal_width =
302 GetCommandInterpreter().GetDebugger().GetTerminalWidth();
303 GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this,
305 result.SetStatus(eReturnStatusFailed);
309 if (m_options.show_mixed && m_options.num_lines_context == 0)
310 m_options.num_lines_context = 2;
312 // Always show the PC in the disassembly
313 uint32_t options = Disassembler::eOptionMarkPCAddress;
315 // Mark the source line for the current PC only if we are doing mixed source
317 if (m_options.show_mixed)
318 options |= Disassembler::eOptionMarkPCSourceLine;
320 if (m_options.show_bytes)
321 options |= Disassembler::eOptionShowBytes;
324 options |= Disassembler::eOptionRawOuput;
326 if (!m_options.func_name.empty()) {
327 ConstString name(m_options.func_name.c_str());
329 if (Disassembler::Disassemble(
330 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
331 flavor_string, m_exe_ctx, name,
333 m_options.num_instructions, m_options.show_mixed,
334 m_options.show_mixed ? m_options.num_lines_context : 0, options,
335 result.GetOutputStream())) {
336 result.SetStatus(eReturnStatusSuccessFinishResult);
338 result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n",
340 result.SetStatus(eReturnStatusFailed);
343 std::vector<AddressRange> ranges;
345 StackFrame *frame = m_exe_ctx.GetFramePtr();
346 if (m_options.frame_line) {
347 if (frame == nullptr) {
348 result.AppendError("Cannot disassemble around the current line without "
349 "a selected frame.\n");
350 result.SetStatus(eReturnStatusFailed);
353 LineEntry pc_line_entry(
354 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
355 if (pc_line_entry.IsValid()) {
356 range = pc_line_entry.range;
359 true; // No line entry, so just disassemble around the current pc
360 m_options.show_mixed = false;
362 } else if (m_options.current_function) {
363 if (frame == nullptr) {
364 result.AppendError("Cannot disassemble around the current function "
365 "without a selected frame.\n");
366 result.SetStatus(eReturnStatusFailed);
369 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
371 range.GetBaseAddress() = symbol->GetAddress();
372 range.SetByteSize(symbol->GetByteSize());
376 // Did the "m_options.frame_line" find a valid range already? If so skip
378 if (range.GetByteSize() == 0) {
379 if (m_options.at_pc) {
380 if (frame == nullptr) {
381 result.AppendError("Cannot disassemble around the current PC without "
382 "a selected frame.\n");
383 result.SetStatus(eReturnStatusFailed);
386 range.GetBaseAddress() = frame->GetFrameCodeAddress();
387 if (m_options.num_instructions == 0) {
388 // Disassembling at the PC always disassembles some number of
389 // instructions (not the whole function).
390 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
392 ranges.push_back(range);
394 range.GetBaseAddress().SetOffset(m_options.start_addr);
395 if (range.GetBaseAddress().IsValid()) {
396 if (m_options.end_addr != LLDB_INVALID_ADDRESS) {
397 if (m_options.end_addr <= m_options.start_addr) {
398 result.AppendErrorWithFormat(
399 "End address before start address.\n");
400 result.SetStatus(eReturnStatusFailed);
403 range.SetByteSize(m_options.end_addr - m_options.start_addr);
405 ranges.push_back(range);
407 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS &&
409 if (!target->GetSectionLoadList().IsEmpty()) {
411 Address symbol_containing_address;
412 if (target->GetSectionLoadList().ResolveLoadAddress(
413 m_options.symbol_containing_addr,
414 symbol_containing_address)) {
415 ModuleSP module_sp(symbol_containing_address.GetModule());
417 bool resolve_tail_call_address = true; // PC can be one past the
418 // address range of the
420 module_sp->ResolveSymbolContextForAddress(
421 symbol_containing_address, eSymbolContextEverything, sc,
422 resolve_tail_call_address);
423 if (sc.function || sc.symbol) {
424 sc.GetAddressRange(eSymbolContextFunction |
425 eSymbolContextSymbol,
434 result.AppendErrorWithFormat(
435 "Could not find function bounds for address 0x%" PRIx64
437 m_options.symbol_containing_addr);
438 result.SetStatus(eReturnStatusFailed);
441 ranges.push_back(range);
443 for (lldb::ModuleSP module_sp : target->GetImages().Modules()) {
444 lldb::addr_t file_addr = m_options.symbol_containing_addr;
445 Address file_address;
446 if (module_sp->ResolveFileAddress(file_addr, file_address)) {
448 bool resolve_tail_call_address = true; // PC can be one past
449 // the address range of
451 module_sp->ResolveSymbolContextForAddress(
452 file_address, eSymbolContextEverything, sc,
453 resolve_tail_call_address);
454 if (sc.function || sc.symbol) {
455 sc.GetAddressRange(eSymbolContextFunction |
456 eSymbolContextSymbol,
458 ranges.push_back(range);
467 ranges.push_back(range);
469 if (m_options.num_instructions != 0) {
470 if (ranges.empty()) {
471 // The default action is to disassemble the current frame function.
473 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
474 eSymbolContextSymbol));
476 range.GetBaseAddress() =
477 sc.function->GetAddressRange().GetBaseAddress();
478 else if (sc.symbol && sc.symbol->ValueIsAddress())
479 range.GetBaseAddress() = sc.symbol->GetAddress();
481 range.GetBaseAddress() = frame->GetFrameCodeAddress();
484 if (!range.GetBaseAddress().IsValid()) {
485 result.AppendError("invalid frame");
486 result.SetStatus(eReturnStatusFailed);
491 bool print_sc_header = ranges.size() > 1;
492 for (AddressRange cur_range : ranges) {
493 if (Disassembler::Disassemble(
494 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
495 flavor_string, m_exe_ctx, cur_range.GetBaseAddress(),
496 m_options.num_instructions, m_options.show_mixed,
497 m_options.show_mixed ? m_options.num_lines_context : 0, options,
498 result.GetOutputStream())) {
499 result.SetStatus(eReturnStatusSuccessFinishResult);
501 if (m_options.start_addr != LLDB_INVALID_ADDRESS)
502 result.AppendErrorWithFormat(
503 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
504 m_options.start_addr);
505 else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
506 result.AppendErrorWithFormat(
507 "Failed to disassemble memory in function at 0x%8.8" PRIx64
509 m_options.symbol_containing_addr);
510 result.SetStatus(eReturnStatusFailed);
514 result.AppendMessage("\n");
516 if (ranges.empty()) {
517 // The default action is to disassemble the current frame function.
519 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
520 eSymbolContextSymbol));
522 range = sc.function->GetAddressRange();
523 else if (sc.symbol && sc.symbol->ValueIsAddress()) {
524 range.GetBaseAddress() = sc.symbol->GetAddress();
525 range.SetByteSize(sc.symbol->GetByteSize());
527 range.GetBaseAddress() = frame->GetFrameCodeAddress();
529 result.AppendError("invalid frame");
530 result.SetStatus(eReturnStatusFailed);
533 ranges.push_back(range);
536 bool print_sc_header = ranges.size() > 1;
537 for (AddressRange cur_range : ranges) {
538 if (cur_range.GetByteSize() == 0)
539 cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
541 if (Disassembler::Disassemble(
542 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
543 flavor_string, m_exe_ctx, cur_range, m_options.num_instructions,
544 m_options.show_mixed,
545 m_options.show_mixed ? m_options.num_lines_context : 0, options,
546 result.GetOutputStream())) {
547 result.SetStatus(eReturnStatusSuccessFinishResult);
549 result.AppendErrorWithFormat(
550 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
551 m_options.start_addr);
552 result.SetStatus(eReturnStatusFailed);
555 result.AppendMessage("\n");
560 return result.Succeeded();