CommandObjectDisassemble.cpp revision 8f0ad0847e3db48ef58f75c6a25f343de5fa43b4
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 31using namespace lldb; 32using namespace lldb_private; 33 34CommandObjectDisassemble::CommandOptions::CommandOptions () : 35 Options(), 36 m_func_name(), 37 m_start_addr(), 38 m_end_addr () 39{ 40 ResetOptionValues(); 41} 42 43CommandObjectDisassemble::CommandOptions::~CommandOptions () 44{ 45} 46 47Error 48CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg) 49{ 50 Error error; 51 52 char short_option = (char) m_getopt_table[option_idx].val; 53 54 switch (short_option) 55 { 56 case 'm': 57 show_mixed = true; 58 break; 59 60 case 'c': 61 num_lines_context = Args::StringToUInt32(option_arg, 0, 0); 62 break; 63 64 case 'b': 65 show_bytes = true; 66 break; 67 68 case 's': 69 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0); 70 if (m_start_addr == LLDB_INVALID_ADDRESS) 71 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16); 72 73 if (m_start_addr == LLDB_INVALID_ADDRESS) 74 error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", optarg); 75 break; 76 case 'e': 77 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0); 78 if (m_end_addr == LLDB_INVALID_ADDRESS) 79 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16); 80 81 if (m_end_addr == LLDB_INVALID_ADDRESS) 82 error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", optarg); 83 break; 84 85 case 'n': 86 m_func_name = option_arg; 87 break; 88 89 case 'r': 90 raw = true; 91 break; 92 93 case 'f': 94 // The default action is to disassemble the function for the current frame. 95 // There's no need to set any flag. 96 break; 97 98 default: 99 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option); 100 break; 101 } 102 103 return error; 104} 105 106void 107CommandObjectDisassemble::CommandOptions::ResetOptionValues () 108{ 109 Options::ResetOptionValues(); 110 show_mixed = false; 111 show_bytes = false; 112 num_lines_context = 0; 113 m_func_name.clear(); 114 m_start_addr = LLDB_INVALID_ADDRESS; 115 m_end_addr = LLDB_INVALID_ADDRESS; 116 raw = false; 117} 118 119const lldb::OptionDefinition* 120CommandObjectDisassemble::CommandOptions::GetDefinitions () 121{ 122 return g_option_table; 123} 124 125lldb::OptionDefinition 126CommandObjectDisassemble::CommandOptions::g_option_table[] = 127{ 128{ LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."}, 129{ LLDB_OPT_SET_ALL, false, "context", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."}, 130{ LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."}, 131{ LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."}, 132 133{ LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, eArgTypeStartAddress, "Address at which to start disassembling."}, 134{ LLDB_OPT_SET_1, false, "end-address", 'e', required_argument, NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."}, 135 136{ LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."}, 137 138{ LLDB_OPT_SET_3, true, "current-frame", 'f', no_argument, NULL, 0, eArgTypeNone, "Disassemble entire contents of the current frame's function."}, 139 140{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 141}; 142 143 144 145//------------------------------------------------------------------------- 146// CommandObjectDisassemble 147//------------------------------------------------------------------------- 148 149CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) : 150 CommandObject (interpreter, 151 "disassemble", 152 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.", 153 "disassemble [<cmd-options>]") 154{ 155} 156 157CommandObjectDisassemble::~CommandObjectDisassemble() 158{ 159} 160 161bool 162CommandObjectDisassemble::Execute 163( 164 Args& command, 165 CommandReturnObject &result 166) 167{ 168 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 169 if (target == NULL) 170 { 171 result.AppendError ("invalid target, set executable file using 'file' command"); 172 result.SetStatus (eReturnStatusFailed); 173 return false; 174 } 175 176 ArchSpec arch(target->GetArchitecture()); 177 if (!arch.IsValid()) 178 { 179 result.AppendError ("target needs valid architecure in order to be able to disassemble"); 180 result.SetStatus (eReturnStatusFailed); 181 return false; 182 } 183 184 Disassembler *disassembler = Disassembler::FindPlugin(arch); 185 186 if (disassembler == NULL) 187 { 188 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString()); 189 result.SetStatus (eReturnStatusFailed); 190 return false; 191 } 192 193 result.SetStatus (eReturnStatusSuccessFinishResult); 194 195 if (command.GetArgumentCount() != 0) 196 { 197 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n"); 198 GetOptions()->GenerateOptionUsage (m_interpreter, 199 result.GetErrorStream(), 200 this); 201 202 result.SetStatus (eReturnStatusFailed); 203 return false; 204 } 205 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext()); 206 207 if (m_options.show_mixed && m_options.num_lines_context == 0) 208 m_options.num_lines_context = 1; 209 210 if (!m_options.m_func_name.empty()) 211 { 212 ConstString name(m_options.m_func_name.c_str()); 213 214 if (Disassembler::Disassemble (m_interpreter.GetDebugger(), 215 arch, 216 exe_ctx, 217 name, 218 NULL, // Module * 219 m_options.show_mixed ? m_options.num_lines_context : 0, 220 m_options.show_bytes, 221 result.GetOutputStream())) 222 { 223 result.SetStatus (eReturnStatusSuccessFinishResult); 224 } 225 else 226 { 227 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); 228 result.SetStatus (eReturnStatusFailed); 229 } 230 } 231 else 232 { 233 AddressRange range; 234 if (m_options.m_start_addr != LLDB_INVALID_ADDRESS) 235 { 236 range.GetBaseAddress().SetOffset (m_options.m_start_addr); 237 if (m_options.m_end_addr != LLDB_INVALID_ADDRESS) 238 { 239 if (m_options.m_end_addr < m_options.m_start_addr) 240 { 241 result.AppendErrorWithFormat ("End address before start address.\n"); 242 result.SetStatus (eReturnStatusFailed); 243 return false; 244 } 245 range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr); 246 } 247 else 248 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); 249 } 250 else 251 { 252 // The default action is to disassemble the current frame function. 253 if (exe_ctx.frame) 254 { 255 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); 256 if (sc.function) 257 range = sc.function->GetAddressRange(); 258 else if (sc.symbol && sc.symbol->GetAddressRangePtr()) 259 range = *sc.symbol->GetAddressRangePtr(); 260 else 261 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress(); 262 } 263 else 264 { 265 result.AppendError ("invalid frame"); 266 result.SetStatus (eReturnStatusFailed); 267 return false; 268 } 269 } 270 if (range.GetByteSize() == 0) 271 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); 272 273 if (Disassembler::Disassemble (m_interpreter.GetDebugger(), 274 arch, 275 exe_ctx, 276 range, 277 m_options.show_mixed ? m_options.num_lines_context : 0, 278 m_options.show_bytes, 279 result.GetOutputStream())) 280 { 281 result.SetStatus (eReturnStatusSuccessFinishResult); 282 } 283 else 284 { 285 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr); 286 result.SetStatus (eReturnStatusFailed); 287 } 288 } 289 290 return result.Succeeded(); 291} 292