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