1 //===-- CommandObjectDisassemble.cpp --------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectDisassemble.h" 10 #include "lldb/Core/AddressRange.h" 11 #include "lldb/Core/Disassembler.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Host/OptionParser.h" 14 #include "lldb/Interpreter/CommandInterpreter.h" 15 #include "lldb/Interpreter/CommandReturnObject.h" 16 #include "lldb/Interpreter/OptionArgParser.h" 17 #include "lldb/Interpreter/Options.h" 18 #include "lldb/Symbol/Function.h" 19 #include "lldb/Symbol/Symbol.h" 20 #include "lldb/Target/SectionLoadList.h" 21 #include "lldb/Target/StackFrame.h" 22 #include "lldb/Target/Target.h" 23 24 #define DEFAULT_DISASM_BYTE_SIZE 32 25 #define DEFAULT_DISASM_NUM_INS 4 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 #define LLDB_OPTIONS_disassemble 31 #include "CommandOptions.inc" 32 33 CommandObjectDisassemble::CommandOptions::CommandOptions() 34 : Options(), num_lines_context(0), num_instructions(0), func_name(), 35 current_function(false), start_addr(), end_addr(), at_pc(false), 36 frame_line(false), plugin_name(), flavor_string(), arch(), 37 some_location_specified(false), symbol_containing_addr() { 38 OptionParsingStarting(nullptr); 39 } 40 41 CommandObjectDisassemble::CommandOptions::~CommandOptions() = default; 42 43 Status CommandObjectDisassemble::CommandOptions::SetOptionValue( 44 uint32_t option_idx, llvm::StringRef option_arg, 45 ExecutionContext *execution_context) { 46 Status error; 47 48 const int short_option = m_getopt_table[option_idx].val; 49 50 switch (short_option) { 51 case 'm': 52 show_mixed = true; 53 break; 54 55 case 'C': 56 if (option_arg.getAsInteger(0, num_lines_context)) 57 error.SetErrorStringWithFormat("invalid num context lines string: \"%s\"", 58 option_arg.str().c_str()); 59 break; 60 61 case 'c': 62 if (option_arg.getAsInteger(0, num_instructions)) 63 error.SetErrorStringWithFormat( 64 "invalid num of instructions string: \"%s\"", 65 option_arg.str().c_str()); 66 break; 67 68 case 'b': 69 show_bytes = true; 70 break; 71 72 case 's': { 73 start_addr = OptionArgParser::ToAddress(execution_context, option_arg, 74 LLDB_INVALID_ADDRESS, &error); 75 if (start_addr != LLDB_INVALID_ADDRESS) 76 some_location_specified = true; 77 } break; 78 case 'e': { 79 end_addr = OptionArgParser::ToAddress(execution_context, option_arg, 80 LLDB_INVALID_ADDRESS, &error); 81 if (end_addr != LLDB_INVALID_ADDRESS) 82 some_location_specified = true; 83 } break; 84 85 case 'n': 86 func_name.assign(std::string(option_arg)); 87 some_location_specified = true; 88 break; 89 90 case 'p': 91 at_pc = true; 92 some_location_specified = true; 93 break; 94 95 case 'l': 96 frame_line = true; 97 // Disassemble the current source line kind of implies showing mixed source 98 // code context. 99 show_mixed = true; 100 some_location_specified = true; 101 break; 102 103 case 'P': 104 plugin_name.assign(std::string(option_arg)); 105 break; 106 107 case 'F': { 108 TargetSP target_sp = 109 execution_context ? execution_context->GetTargetSP() : TargetSP(); 110 if (target_sp && (target_sp->GetArchitecture().GetTriple().getArch() == 111 llvm::Triple::x86 || 112 target_sp->GetArchitecture().GetTriple().getArch() == 113 llvm::Triple::x86_64)) { 114 flavor_string.assign(std::string(option_arg)); 115 } else 116 error.SetErrorStringWithFormat("Disassembler flavors are currently only " 117 "supported for x86 and x86_64 targets."); 118 break; 119 } 120 121 case 'r': 122 raw = true; 123 break; 124 125 case 'f': 126 current_function = true; 127 some_location_specified = true; 128 break; 129 130 case 'A': 131 if (execution_context) { 132 const auto &target_sp = execution_context->GetTargetSP(); 133 auto platform_ptr = target_sp ? target_sp->GetPlatform().get() : nullptr; 134 arch = Platform::GetAugmentedArchSpec(platform_ptr, option_arg); 135 } 136 break; 137 138 case 'a': { 139 symbol_containing_addr = OptionArgParser::ToAddress( 140 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 141 if (symbol_containing_addr != LLDB_INVALID_ADDRESS) { 142 some_location_specified = true; 143 } 144 } break; 145 146 default: 147 llvm_unreachable("Unimplemented option"); 148 } 149 150 return error; 151 } 152 153 void CommandObjectDisassemble::CommandOptions::OptionParsingStarting( 154 ExecutionContext *execution_context) { 155 show_mixed = false; 156 show_bytes = false; 157 num_lines_context = 0; 158 num_instructions = 0; 159 func_name.clear(); 160 current_function = false; 161 at_pc = false; 162 frame_line = false; 163 start_addr = LLDB_INVALID_ADDRESS; 164 end_addr = LLDB_INVALID_ADDRESS; 165 symbol_containing_addr = LLDB_INVALID_ADDRESS; 166 raw = false; 167 plugin_name.clear(); 168 169 Target *target = 170 execution_context ? execution_context->GetTargetPtr() : nullptr; 171 172 // This is a hack till we get the ability to specify features based on 173 // architecture. For now GetDisassemblyFlavor is really only valid for x86 174 // (and for the llvm assembler plugin, but I'm papering over that since that 175 // is the only disassembler plugin we have... 176 if (target) { 177 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86 || 178 target->GetArchitecture().GetTriple().getArch() == 179 llvm::Triple::x86_64) { 180 flavor_string.assign(target->GetDisassemblyFlavor()); 181 } else 182 flavor_string.assign("default"); 183 184 } else 185 flavor_string.assign("default"); 186 187 arch.Clear(); 188 some_location_specified = false; 189 } 190 191 Status CommandObjectDisassemble::CommandOptions::OptionParsingFinished( 192 ExecutionContext *execution_context) { 193 if (!some_location_specified) 194 current_function = true; 195 return Status(); 196 } 197 198 llvm::ArrayRef<OptionDefinition> 199 CommandObjectDisassemble::CommandOptions::GetDefinitions() { 200 return llvm::makeArrayRef(g_disassemble_options); 201 } 202 203 // CommandObjectDisassemble 204 205 CommandObjectDisassemble::CommandObjectDisassemble( 206 CommandInterpreter &interpreter) 207 : CommandObjectParsed( 208 interpreter, "disassemble", 209 "Disassemble specified instructions in the current target. " 210 "Defaults to the current function for the current thread and " 211 "stack frame.", 212 "disassemble [<cmd-options>]", eCommandRequiresTarget), 213 m_options() {} 214 215 CommandObjectDisassemble::~CommandObjectDisassemble() = default; 216 217 bool CommandObjectDisassemble::DoExecute(Args &command, 218 CommandReturnObject &result) { 219 Target *target = &GetSelectedTarget(); 220 221 if (!m_options.arch.IsValid()) 222 m_options.arch = target->GetArchitecture(); 223 224 if (!m_options.arch.IsValid()) { 225 result.AppendError( 226 "use the --arch option or set the target architecture to disassemble"); 227 result.SetStatus(eReturnStatusFailed); 228 return false; 229 } 230 231 const char *plugin_name = m_options.GetPluginName(); 232 const char *flavor_string = m_options.GetFlavorString(); 233 234 DisassemblerSP disassembler = 235 Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name); 236 237 if (!disassembler) { 238 if (plugin_name) { 239 result.AppendErrorWithFormat( 240 "Unable to find Disassembler plug-in named '%s' that supports the " 241 "'%s' architecture.\n", 242 plugin_name, m_options.arch.GetArchitectureName()); 243 } else 244 result.AppendErrorWithFormat( 245 "Unable to find Disassembler plug-in for the '%s' architecture.\n", 246 m_options.arch.GetArchitectureName()); 247 result.SetStatus(eReturnStatusFailed); 248 return false; 249 } else if (flavor_string != nullptr && !disassembler->FlavorValidForArchSpec( 250 m_options.arch, flavor_string)) 251 result.AppendWarningWithFormat( 252 "invalid disassembler flavor \"%s\", using default.\n", flavor_string); 253 254 result.SetStatus(eReturnStatusSuccessFinishResult); 255 256 if (!command.empty()) { 257 result.AppendErrorWithFormat( 258 "\"disassemble\" arguments are specified as options.\n"); 259 const int terminal_width = 260 GetCommandInterpreter().GetDebugger().GetTerminalWidth(); 261 GetOptions()->GenerateOptionUsage(result.GetErrorStream(), this, 262 terminal_width); 263 result.SetStatus(eReturnStatusFailed); 264 return false; 265 } 266 267 if (m_options.show_mixed && m_options.num_lines_context == 0) 268 m_options.num_lines_context = 2; 269 270 // Always show the PC in the disassembly 271 uint32_t options = Disassembler::eOptionMarkPCAddress; 272 273 // Mark the source line for the current PC only if we are doing mixed source 274 // and assembly 275 if (m_options.show_mixed) 276 options |= Disassembler::eOptionMarkPCSourceLine; 277 278 if (m_options.show_bytes) 279 options |= Disassembler::eOptionShowBytes; 280 281 if (m_options.raw) 282 options |= Disassembler::eOptionRawOuput; 283 284 std::vector<AddressRange> ranges; 285 if (!m_options.func_name.empty()) { 286 ConstString name(m_options.func_name.c_str()); 287 const bool include_symbols = true; 288 const bool include_inlines = true; 289 290 // Find functions matching the given name. 291 SymbolContextList sc_list; 292 target->GetImages().FindFunctions( 293 name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list); 294 295 AddressRange range; 296 const uint32_t scope = 297 eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol; 298 const bool use_inline_block_range = true; 299 for (SymbolContext sc : sc_list.SymbolContexts()) { 300 for (uint32_t range_idx = 0; 301 sc.GetAddressRange(scope, range_idx, use_inline_block_range, range); 302 ++range_idx) { 303 ranges.push_back(range); 304 } 305 } 306 if (ranges.empty()) { 307 result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n", 308 name.GetCString()); 309 result.SetStatus(eReturnStatusFailed); 310 return result.Succeeded(); 311 } 312 } else { 313 AddressRange range; 314 StackFrame *frame = m_exe_ctx.GetFramePtr(); 315 if (m_options.frame_line) { 316 if (frame == nullptr) { 317 result.AppendError("Cannot disassemble around the current line without " 318 "a selected frame.\n"); 319 result.SetStatus(eReturnStatusFailed); 320 return false; 321 } 322 LineEntry pc_line_entry( 323 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry); 324 if (pc_line_entry.IsValid()) { 325 range = pc_line_entry.range; 326 } else { 327 m_options.at_pc = 328 true; // No line entry, so just disassemble around the current pc 329 m_options.show_mixed = false; 330 } 331 } else if (m_options.current_function) { 332 if (frame == nullptr) { 333 result.AppendError("Cannot disassemble around the current function " 334 "without a selected frame.\n"); 335 result.SetStatus(eReturnStatusFailed); 336 return false; 337 } 338 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol; 339 if (symbol) { 340 range.GetBaseAddress() = symbol->GetAddress(); 341 range.SetByteSize(symbol->GetByteSize()); 342 } 343 } 344 345 // Did the "m_options.frame_line" find a valid range already? If so skip 346 // the rest... 347 if (range.GetByteSize() == 0) { 348 if (m_options.at_pc) { 349 if (frame == nullptr) { 350 result.AppendError("Cannot disassemble around the current PC without " 351 "a selected frame.\n"); 352 result.SetStatus(eReturnStatusFailed); 353 return false; 354 } 355 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 356 if (m_options.num_instructions == 0) { 357 // Disassembling at the PC always disassembles some number of 358 // instructions (not the whole function). 359 m_options.num_instructions = DEFAULT_DISASM_NUM_INS; 360 } 361 ranges.push_back(range); 362 } else { 363 range.GetBaseAddress().SetOffset(m_options.start_addr); 364 if (range.GetBaseAddress().IsValid()) { 365 if (m_options.end_addr != LLDB_INVALID_ADDRESS) { 366 if (m_options.end_addr <= m_options.start_addr) { 367 result.AppendErrorWithFormat( 368 "End address before start address.\n"); 369 result.SetStatus(eReturnStatusFailed); 370 return false; 371 } 372 range.SetByteSize(m_options.end_addr - m_options.start_addr); 373 } 374 ranges.push_back(range); 375 } else { 376 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS && 377 target) { 378 if (!target->GetSectionLoadList().IsEmpty()) { 379 bool failed = false; 380 Address symbol_containing_address; 381 if (target->GetSectionLoadList().ResolveLoadAddress( 382 m_options.symbol_containing_addr, 383 symbol_containing_address)) { 384 ModuleSP module_sp(symbol_containing_address.GetModule()); 385 SymbolContext sc; 386 bool resolve_tail_call_address = true; // PC can be one past the 387 // address range of the 388 // function. 389 module_sp->ResolveSymbolContextForAddress( 390 symbol_containing_address, eSymbolContextEverything, sc, 391 resolve_tail_call_address); 392 if (sc.function || sc.symbol) { 393 sc.GetAddressRange(eSymbolContextFunction | 394 eSymbolContextSymbol, 395 0, false, range); 396 } else { 397 failed = true; 398 } 399 } else { 400 failed = true; 401 } 402 if (failed) { 403 result.AppendErrorWithFormat( 404 "Could not find function bounds for address 0x%" PRIx64 405 "\n", 406 m_options.symbol_containing_addr); 407 result.SetStatus(eReturnStatusFailed); 408 return false; 409 } 410 ranges.push_back(range); 411 } else { 412 for (lldb::ModuleSP module_sp : target->GetImages().Modules()) { 413 lldb::addr_t file_addr = m_options.symbol_containing_addr; 414 Address file_address; 415 if (module_sp->ResolveFileAddress(file_addr, file_address)) { 416 SymbolContext sc; 417 bool resolve_tail_call_address = true; // PC can be one past 418 // the address range of 419 // the function. 420 module_sp->ResolveSymbolContextForAddress( 421 file_address, eSymbolContextEverything, sc, 422 resolve_tail_call_address); 423 if (sc.function || sc.symbol) { 424 sc.GetAddressRange(eSymbolContextFunction | 425 eSymbolContextSymbol, 426 0, false, range); 427 ranges.push_back(range); 428 } 429 } 430 } 431 } 432 } 433 } 434 } 435 } else 436 ranges.push_back(range); 437 438 if (ranges.empty()) { 439 // The default action is to disassemble the current frame function. 440 if (frame) { 441 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | 442 eSymbolContextSymbol)); 443 if (sc.function) 444 range = sc.function->GetAddressRange(); 445 else if (sc.symbol && sc.symbol->ValueIsAddress()) { 446 range.GetBaseAddress() = sc.symbol->GetAddress(); 447 range.SetByteSize(sc.symbol->GetByteSize()); 448 } else 449 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 450 } 451 if (!range.GetBaseAddress().IsValid()) { 452 result.AppendError("invalid frame"); 453 result.SetStatus(eReturnStatusFailed); 454 return false; 455 } 456 ranges.push_back(range); 457 } 458 } 459 460 bool print_sc_header = ranges.size() > 1; 461 for (AddressRange cur_range : ranges) { 462 Disassembler::Limit limit; 463 if (m_options.num_instructions == 0) { 464 limit = {Disassembler::Limit::Bytes, cur_range.GetByteSize()}; 465 if (limit.value == 0) 466 limit.value = DEFAULT_DISASM_BYTE_SIZE; 467 } else { 468 limit = {Disassembler::Limit::Instructions, m_options.num_instructions}; 469 } 470 if (Disassembler::Disassemble( 471 GetDebugger(), m_options.arch, plugin_name, flavor_string, 472 m_exe_ctx, cur_range.GetBaseAddress(), limit, m_options.show_mixed, 473 m_options.show_mixed ? m_options.num_lines_context : 0, options, 474 result.GetOutputStream())) { 475 result.SetStatus(eReturnStatusSuccessFinishResult); 476 } else { 477 if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) { 478 result.AppendErrorWithFormat( 479 "Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n", 480 m_options.symbol_containing_addr); 481 } else { 482 result.AppendErrorWithFormat( 483 "Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", 484 cur_range.GetBaseAddress().GetLoadAddress(target)); 485 } 486 result.SetStatus(eReturnStatusFailed); 487 } 488 if (print_sc_header) 489 result.AppendMessage("\n"); 490 } 491 492 return result.Succeeded(); 493 } 494