1 //===-- CommandObjectDisassemble.cpp ----------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CommandObjectDisassemble.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/AddressRange.h" 17 #include "lldb/Interpreter/Args.h" 18 #include "lldb/Interpreter/CommandCompletions.h" 19 #include "lldb/Interpreter/CommandInterpreter.h" 20 #include "lldb/Interpreter/CommandReturnObject.h" 21 #include "lldb/Core/Disassembler.h" 22 #include "lldb/Interpreter/Options.h" 23 #include "lldb/Core/SourceManager.h" 24 #include "lldb/Target/StackFrame.h" 25 #include "lldb/Symbol/Symbol.h" 26 #include "lldb/Target/Process.h" 27 #include "lldb/Target/Target.h" 28 29 #define DEFAULT_DISASM_BYTE_SIZE 32 30 #define DEFAULT_DISASM_NUM_INS 4 31 32 using namespace lldb; 33 using namespace lldb_private; 34 35 CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) : 36 Options(interpreter), 37 num_lines_context(0), 38 num_instructions (0), 39 func_name(), 40 cur_function (false), 41 start_addr(), 42 end_addr (), 43 at_pc (false), 44 frame_line (false), 45 plugin_name (), 46 arch(), 47 some_location_specified (false) 48 { 49 OptionParsingStarting(); 50 } 51 52 CommandObjectDisassemble::CommandOptions::~CommandOptions () 53 { 54 } 55 56 Error 57 CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) 58 { 59 Error error; 60 61 char short_option = (char) m_getopt_table[option_idx].val; 62 63 bool success; 64 65 switch (short_option) 66 { 67 case 'm': 68 show_mixed = true; 69 break; 70 71 case 'C': 72 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success); 73 if (!success) 74 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg); 75 break; 76 77 case 'c': 78 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success); 79 if (!success) 80 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg); 81 break; 82 83 case 'b': 84 show_bytes = true; 85 break; 86 87 case 's': 88 start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0); 89 if (start_addr == LLDB_INVALID_ADDRESS) 90 start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16); 91 92 if (start_addr == LLDB_INVALID_ADDRESS) 93 error.SetErrorStringWithFormat ("invalid start address string '%s'", option_arg); 94 some_location_specified = true; 95 break; 96 case 'e': 97 end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0); 98 if (end_addr == LLDB_INVALID_ADDRESS) 99 end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16); 100 101 if (end_addr == LLDB_INVALID_ADDRESS) 102 error.SetErrorStringWithFormat ("invalid end address string '%s'", option_arg); 103 break; 104 some_location_specified = true; 105 case 'n': 106 func_name.assign (option_arg); 107 some_location_specified = true; 108 break; 109 110 case 'p': 111 at_pc = true; 112 some_location_specified = true; 113 break; 114 115 case 'l': 116 frame_line = true; 117 // Disassemble the current source line kind of implies showing mixed 118 // source code context. 119 show_mixed = true; 120 some_location_specified = true; 121 break; 122 123 case 'P': 124 plugin_name.assign (option_arg); 125 break; 126 127 case 'r': 128 raw = true; 129 break; 130 131 case 'f': 132 cur_function = true; 133 some_location_specified = true; 134 break; 135 136 case 'a': 137 arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()); 138 break; 139 140 default: 141 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option); 142 break; 143 } 144 145 return error; 146 } 147 148 void 149 CommandObjectDisassemble::CommandOptions::OptionParsingStarting () 150 { 151 show_mixed = false; 152 show_bytes = false; 153 num_lines_context = 0; 154 num_instructions = 0; 155 func_name.clear(); 156 cur_function = false; 157 at_pc = false; 158 frame_line = false; 159 start_addr = LLDB_INVALID_ADDRESS; 160 end_addr = LLDB_INVALID_ADDRESS; 161 raw = false; 162 plugin_name.clear(); 163 arch.Clear(); 164 some_location_specified = false; 165 } 166 167 Error 168 CommandObjectDisassemble::CommandOptions::OptionParsingFinished () 169 { 170 if (!some_location_specified) 171 at_pc = true; 172 return Error(); 173 174 } 175 176 const OptionDefinition* 177 CommandObjectDisassemble::CommandOptions::GetDefinitions () 178 { 179 return g_option_table; 180 } 181 182 OptionDefinition 183 CommandObjectDisassemble::CommandOptions::g_option_table[] = 184 { 185 { LLDB_OPT_SET_ALL , false , "bytes", 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."}, 186 { LLDB_OPT_SET_ALL , false , "context", 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."}, 187 { LLDB_OPT_SET_ALL , false , "mixed", 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."}, 188 { LLDB_OPT_SET_ALL , false , "raw", 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."}, 189 { LLDB_OPT_SET_ALL , false , "plugin", 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."}, 190 { LLDB_OPT_SET_ALL , false , "arch", 'a', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."}, 191 { LLDB_OPT_SET_1 | 192 LLDB_OPT_SET_2 , true , "start-address" , 's', required_argument , NULL, 0, eArgTypeStartAddress,"Address at which to start disassembling."}, 193 { LLDB_OPT_SET_1 , false , "end-address" , 'e', required_argument , NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."}, 194 { LLDB_OPT_SET_2 | 195 LLDB_OPT_SET_3 | 196 LLDB_OPT_SET_4 | 197 LLDB_OPT_SET_5 , false , "count", 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."}, 198 { LLDB_OPT_SET_3 , false , "name", 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."}, 199 { LLDB_OPT_SET_4 , false , "frame", 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."}, 200 { LLDB_OPT_SET_5 , false , "pc", 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."}, 201 { 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."}, 202 { 0 , false , NULL, 0, 0 , NULL, 0, eArgTypeNone, NULL } 203 }; 204 205 206 207 //------------------------------------------------------------------------- 208 // CommandObjectDisassemble 209 //------------------------------------------------------------------------- 210 211 CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) : 212 CommandObject (interpreter, 213 "disassemble", 214 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.", 215 "disassemble [<cmd-options>]"), 216 m_options (interpreter) 217 { 218 } 219 220 CommandObjectDisassemble::~CommandObjectDisassemble() 221 { 222 } 223 224 bool 225 CommandObjectDisassemble::Execute 226 ( 227 Args& command, 228 CommandReturnObject &result 229 ) 230 { 231 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 232 if (target == NULL) 233 { 234 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 235 result.SetStatus (eReturnStatusFailed); 236 return false; 237 } 238 if (!m_options.arch.IsValid()) 239 m_options.arch = target->GetArchitecture(); 240 241 if (!m_options.arch.IsValid()) 242 { 243 result.AppendError ("use the --arch option or set the target architecure to disassemble"); 244 result.SetStatus (eReturnStatusFailed); 245 return false; 246 } 247 248 const char *plugin_name = m_options.GetPluginName (); 249 Disassembler *disassembler = Disassembler::FindPlugin(m_options.arch, plugin_name); 250 251 if (disassembler == NULL) 252 { 253 if (plugin_name) 254 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n", 255 plugin_name, 256 m_options.arch.GetArchitectureName()); 257 else 258 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n", 259 m_options.arch.GetArchitectureName()); 260 result.SetStatus (eReturnStatusFailed); 261 return false; 262 } 263 264 result.SetStatus (eReturnStatusSuccessFinishResult); 265 266 if (command.GetArgumentCount() != 0) 267 { 268 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n"); 269 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this); 270 result.SetStatus (eReturnStatusFailed); 271 return false; 272 } 273 274 if (m_options.show_mixed && m_options.num_lines_context == 0) 275 m_options.num_lines_context = 1; 276 277 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 278 // Always show the PC in the disassembly 279 uint32_t options = Disassembler::eOptionMarkPCAddress; 280 281 // Mark the source line for the current PC only if we are doing mixed source and assembly 282 if (m_options.show_mixed) 283 options |= Disassembler::eOptionMarkPCSourceLine; 284 285 if (m_options.show_bytes) 286 options |= Disassembler::eOptionShowBytes; 287 288 if (m_options.raw) 289 options |= Disassembler::eOptionRawOuput; 290 291 if (!m_options.func_name.empty()) 292 { 293 ConstString name(m_options.func_name.c_str()); 294 295 if (Disassembler::Disassemble (m_interpreter.GetDebugger(), 296 m_options.arch, 297 plugin_name, 298 exe_ctx, 299 name, 300 NULL, // Module * 301 m_options.num_instructions, 302 m_options.show_mixed ? m_options.num_lines_context : 0, 303 options, 304 result.GetOutputStream())) 305 { 306 result.SetStatus (eReturnStatusSuccessFinishResult); 307 } 308 else 309 { 310 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); 311 result.SetStatus (eReturnStatusFailed); 312 } 313 } 314 else 315 { 316 AddressRange range; 317 StackFrame *frame = exe_ctx.GetFramePtr(); 318 if (m_options.frame_line) 319 { 320 if (frame == NULL) 321 { 322 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n"); 323 result.SetStatus (eReturnStatusFailed); 324 return false; 325 } 326 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry); 327 if (pc_line_entry.IsValid()) 328 { 329 range = pc_line_entry.range; 330 } 331 else 332 { 333 m_options.at_pc = true; // No line entry, so just disassemble around the current pc 334 m_options.show_mixed = false; 335 } 336 } 337 else if (m_options.cur_function) 338 { 339 if (frame == NULL) 340 { 341 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n"); 342 result.SetStatus (eReturnStatusFailed); 343 return false; 344 } 345 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol; 346 if (symbol) 347 { 348 range.GetBaseAddress() = symbol->GetAddress(); 349 range.SetByteSize(symbol->GetByteSize()); 350 } 351 } 352 353 // Did the "m_options.frame_line" find a valid range already? If so 354 // skip the rest... 355 if (range.GetByteSize() == 0) 356 { 357 if (m_options.at_pc) 358 { 359 if (frame == NULL) 360 { 361 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n"); 362 result.SetStatus (eReturnStatusFailed); 363 return false; 364 } 365 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 366 if (m_options.num_instructions == 0) 367 { 368 // Disassembling at the PC always disassembles some number of instructions (not the whole function). 369 m_options.num_instructions = DEFAULT_DISASM_NUM_INS; 370 } 371 } 372 else 373 { 374 range.GetBaseAddress().SetOffset (m_options.start_addr); 375 if (range.GetBaseAddress().IsValid()) 376 { 377 if (m_options.end_addr != LLDB_INVALID_ADDRESS) 378 { 379 if (m_options.end_addr <= m_options.start_addr) 380 { 381 result.AppendErrorWithFormat ("End address before start address.\n"); 382 result.SetStatus (eReturnStatusFailed); 383 return false; 384 } 385 range.SetByteSize (m_options.end_addr - m_options.start_addr); 386 } 387 } 388 } 389 } 390 391 if (m_options.num_instructions != 0) 392 { 393 if (!range.GetBaseAddress().IsValid()) 394 { 395 // The default action is to disassemble the current frame function. 396 if (frame) 397 { 398 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); 399 if (sc.function) 400 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress(); 401 else if (sc.symbol && sc.symbol->ValueIsAddress()) 402 range.GetBaseAddress() = sc.symbol->GetAddress(); 403 else 404 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 405 } 406 407 if (!range.GetBaseAddress().IsValid()) 408 { 409 result.AppendError ("invalid frame"); 410 result.SetStatus (eReturnStatusFailed); 411 return false; 412 } 413 } 414 415 if (Disassembler::Disassemble (m_interpreter.GetDebugger(), 416 m_options.arch, 417 plugin_name, 418 exe_ctx, 419 range.GetBaseAddress(), 420 m_options.num_instructions, 421 m_options.show_mixed ? m_options.num_lines_context : 0, 422 options, 423 result.GetOutputStream())) 424 { 425 result.SetStatus (eReturnStatusSuccessFinishResult); 426 } 427 else 428 { 429 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr); 430 result.SetStatus (eReturnStatusFailed); 431 } 432 } 433 else 434 { 435 if (!range.GetBaseAddress().IsValid()) 436 { 437 // The default action is to disassemble the current frame function. 438 if (frame) 439 { 440 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); 441 if (sc.function) 442 range = sc.function->GetAddressRange(); 443 else if (sc.symbol && sc.symbol->ValueIsAddress()) 444 { 445 range.GetBaseAddress() = sc.symbol->GetAddress(); 446 range.SetByteSize (sc.symbol->GetByteSize()); 447 } 448 else 449 range.GetBaseAddress() = frame->GetFrameCodeAddress(); 450 } 451 else 452 { 453 result.AppendError ("invalid frame"); 454 result.SetStatus (eReturnStatusFailed); 455 return false; 456 } 457 } 458 if (range.GetByteSize() == 0) 459 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); 460 461 if (Disassembler::Disassemble (m_interpreter.GetDebugger(), 462 m_options.arch, 463 plugin_name, 464 exe_ctx, 465 range, 466 m_options.num_instructions, 467 m_options.show_mixed ? m_options.num_lines_context : 0, 468 options, 469 result.GetOutputStream())) 470 { 471 result.SetStatus (eReturnStatusSuccessFinishResult); 472 } 473 else 474 { 475 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr); 476 result.SetStatus (eReturnStatusFailed); 477 } 478 } 479 } 480 481 return result.Succeeded(); 482 } 483