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