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 arch = Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg);
171 symbol_containing_addr = Args::StringToAddress(
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
208 // is really only valid for x86 (and for the llvm assembler plugin, but I'm
209 // papering over that since that is the
210 // only disassembler plugin we have...
212 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 ||
213 target->GetArchitecture().GetTriple().getArch() ==
214 llvm::Triple::x86_64) {
215 flavor_string.assign(target->GetDisassemblyFlavor());
217 flavor_string.assign("default");
220 flavor_string.assign("default");
223 some_location_specified = false;
226 Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished(
227 ExecutionContext *execution_context) {
228 if (!some_location_specified)
229 current_function = true;
233 llvm::ArrayRef<OptionDefinition>
234 CommandObjectDisassemble::CommandOptions::GetDefinitions() {
235 return llvm::makeArrayRef(g_disassemble_options);
238 //-------------------------------------------------------------------------
239 // CommandObjectDisassemble
240 //-------------------------------------------------------------------------
242 CommandObjectDisassemble::CommandObjectDisassemble(
243 CommandInterpreter &interpreter)
244 : CommandObjectParsed(
245 interpreter, "disassemble",
246 "Disassemble specified instructions in the current target. "
247 "Defaults to the current function for the current thread and "
249 "disassemble [<cmd-options>]"),
252 CommandObjectDisassemble::~CommandObjectDisassemble() = default;
254 bool CommandObjectDisassemble::DoExecute(Args &command,
255 CommandReturnObject &result) {
256 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
257 if (target == nullptr) {
258 result.AppendError("invalid target, create a debug target using the "
259 "'target create' command");
260 result.SetStatus(eReturnStatusFailed);
263 if (!m_options.arch.IsValid())
264 m_options.arch = target->GetArchitecture();
266 if (!m_options.arch.IsValid()) {
268 "use the --arch option or set the target architecture to disassemble");
269 result.SetStatus(eReturnStatusFailed);
273 const char *plugin_name = m_options.GetPluginName();
274 const char *flavor_string = m_options.GetFlavorString();
276 DisassemblerSP disassembler =
277 Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
281 result.AppendErrorWithFormat(
282 "Unable to find Disassembler plug-in named '%s' that supports the "
283 "'%s' architecture.\n",
284 plugin_name, m_options.arch.GetArchitectureName());
286 result.AppendErrorWithFormat(
287 "Unable to find Disassembler plug-in for the '%s' architecture.\n",
288 m_options.arch.GetArchitectureName());
289 result.SetStatus(eReturnStatusFailed);
291 } else if (flavor_string != nullptr &&
292 !disassembler->FlavorValidForArchSpec(m_options.arch,
294 result.AppendWarningWithFormat(
295 "invalid disassembler flavor \"%s\", using default.\n", flavor_string);
297 result.SetStatus(eReturnStatusSuccessFinishResult);
299 if (!command.empty()) {
300 result.AppendErrorWithFormat(
301 "\"disassemble\" arguments are specified as options.\n");
302 const int terminal_width =
303 GetCommandInterpreter().GetDebugger().GetTerminalWidth();
304 GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this,
306 result.SetStatus(eReturnStatusFailed);
310 if (m_options.show_mixed && m_options.num_lines_context == 0)
311 m_options.num_lines_context = 2;
313 // Always show the PC in the disassembly
314 uint32_t options = Disassembler::eOptionMarkPCAddress;
316 // Mark the source line for the current PC only if we are doing mixed source
318 if (m_options.show_mixed)
319 options |= Disassembler::eOptionMarkPCSourceLine;
321 if (m_options.show_bytes)
322 options |= Disassembler::eOptionShowBytes;
325 options |= Disassembler::eOptionRawOuput;
327 if (!m_options.func_name.empty()) {
328 ConstString name(m_options.func_name.c_str());
330 if (Disassembler::Disassemble(
331 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
332 flavor_string, m_exe_ctx, name,
334 m_options.num_instructions, m_options.show_mixed,
335 m_options.show_mixed ? m_options.num_lines_context : 0, options,
336 result.GetOutputStream())) {
337 result.SetStatus(eReturnStatusSuccessFinishResult);
339 result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n",
341 result.SetStatus(eReturnStatusFailed);
344 std::vector<AddressRange> ranges;
346 StackFrame *frame = m_exe_ctx.GetFramePtr();
347 if (m_options.frame_line) {
348 if (frame == nullptr) {
349 result.AppendError("Cannot disassemble around the current line without "
350 "a selected frame.\n");
351 result.SetStatus(eReturnStatusFailed);
354 LineEntry pc_line_entry(
355 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
356 if (pc_line_entry.IsValid()) {
357 range = pc_line_entry.range;
360 true; // No line entry, so just disassemble around the current pc
361 m_options.show_mixed = false;
363 } else if (m_options.current_function) {
364 if (frame == nullptr) {
365 result.AppendError("Cannot disassemble around the current function "
366 "without a selected frame.\n");
367 result.SetStatus(eReturnStatusFailed);
370 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
372 range.GetBaseAddress() = symbol->GetAddress();
373 range.SetByteSize(symbol->GetByteSize());
377 // Did the "m_options.frame_line" find a valid range already? If so
379 if (range.GetByteSize() == 0) {
380 if (m_options.at_pc) {
381 if (frame == nullptr) {
382 result.AppendError("Cannot disassemble around the current PC without "
383 "a selected frame.\n");
384 result.SetStatus(eReturnStatusFailed);
387 range.GetBaseAddress() = frame->GetFrameCodeAddress();
388 if (m_options.num_instructions == 0) {
389 // Disassembling at the PC always disassembles some number of
390 // instructions (not the whole function).
391 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
393 ranges.push_back(range);
395 range.GetBaseAddress().SetOffset(m_options.start_addr);
396 if (range.GetBaseAddress().IsValid()) {
397 if (m_options.end_addr != LLDB_INVALID_ADDRESS) {
398 if (m_options.end_addr <= m_options.start_addr) {
399 result.AppendErrorWithFormat(
400 "End address before start address.\n");
401 result.SetStatus(eReturnStatusFailed);
404 range.SetByteSize(m_options.end_addr - m_options.start_addr);
406 ranges.push_back(range);
408 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS &&
410 if (!target->GetSectionLoadList().IsEmpty()) {
412 Address symbol_containing_address;
413 if (target->GetSectionLoadList().ResolveLoadAddress(
414 m_options.symbol_containing_addr,
415 symbol_containing_address)) {
416 ModuleSP module_sp(symbol_containing_address.GetModule());
418 bool resolve_tail_call_address = true; // PC can be one past the
419 // address range of the
421 module_sp->ResolveSymbolContextForAddress(
422 symbol_containing_address, eSymbolContextEverything, sc,
423 resolve_tail_call_address);
424 if (sc.function || sc.symbol) {
425 sc.GetAddressRange(eSymbolContextFunction |
426 eSymbolContextSymbol,
435 result.AppendErrorWithFormat(
436 "Could not find function bounds for address 0x%" PRIx64
438 m_options.symbol_containing_addr);
439 result.SetStatus(eReturnStatusFailed);
442 ranges.push_back(range);
444 for (lldb::ModuleSP module_sp : target->GetImages().Modules()) {
445 lldb::addr_t file_addr = m_options.symbol_containing_addr;
446 Address file_address;
447 if (module_sp->ResolveFileAddress(file_addr, file_address)) {
449 bool resolve_tail_call_address = true; // PC can be one past
450 // the address range of
452 module_sp->ResolveSymbolContextForAddress(
453 file_address, eSymbolContextEverything, sc,
454 resolve_tail_call_address);
455 if (sc.function || sc.symbol) {
456 sc.GetAddressRange(eSymbolContextFunction |
457 eSymbolContextSymbol,
459 ranges.push_back(range);
468 ranges.push_back(range);
470 if (m_options.num_instructions != 0) {
471 if (ranges.empty()) {
472 // The default action is to disassemble the current frame function.
474 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
475 eSymbolContextSymbol));
477 range.GetBaseAddress() =
478 sc.function->GetAddressRange().GetBaseAddress();
479 else if (sc.symbol && sc.symbol->ValueIsAddress())
480 range.GetBaseAddress() = sc.symbol->GetAddress();
482 range.GetBaseAddress() = frame->GetFrameCodeAddress();
485 if (!range.GetBaseAddress().IsValid()) {
486 result.AppendError("invalid frame");
487 result.SetStatus(eReturnStatusFailed);
492 bool print_sc_header = ranges.size() > 1;
493 for (AddressRange cur_range : ranges) {
494 if (Disassembler::Disassemble(
495 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
496 flavor_string, m_exe_ctx, cur_range.GetBaseAddress(),
497 m_options.num_instructions, m_options.show_mixed,
498 m_options.show_mixed ? m_options.num_lines_context : 0, options,
499 result.GetOutputStream())) {
500 result.SetStatus(eReturnStatusSuccessFinishResult);
502 if (m_options.start_addr != LLDB_INVALID_ADDRESS)
503 result.AppendErrorWithFormat(
504 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
505 m_options.start_addr);
506 else if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS)
507 result.AppendErrorWithFormat(
508 "Failed to disassemble memory in function at 0x%8.8" PRIx64
510 m_options.symbol_containing_addr);
511 result.SetStatus(eReturnStatusFailed);
515 result.AppendMessage("\n");
517 if (ranges.empty()) {
518 // The default action is to disassemble the current frame function.
520 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction |
521 eSymbolContextSymbol));
523 range = sc.function->GetAddressRange();
524 else if (sc.symbol && sc.symbol->ValueIsAddress()) {
525 range.GetBaseAddress() = sc.symbol->GetAddress();
526 range.SetByteSize(sc.symbol->GetByteSize());
528 range.GetBaseAddress() = frame->GetFrameCodeAddress();
530 result.AppendError("invalid frame");
531 result.SetStatus(eReturnStatusFailed);
534 ranges.push_back(range);
537 bool print_sc_header = ranges.size() > 1;
538 for (AddressRange cur_range : ranges) {
539 if (cur_range.GetByteSize() == 0)
540 cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
542 if (Disassembler::Disassemble(
543 m_interpreter.GetDebugger(), m_options.arch, plugin_name,
544 flavor_string, m_exe_ctx, cur_range, m_options.num_instructions,
545 m_options.show_mixed,
546 m_options.show_mixed ? m_options.num_lines_context : 0, options,
547 result.GetOutputStream())) {
548 result.SetStatus(eReturnStatusSuccessFinishResult);
550 result.AppendErrorWithFormat(
551 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
552 m_options.start_addr);
553 result.SetStatus(eReturnStatusFailed);
556 result.AppendMessage("\n");
561 return result.Succeeded();