Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Interpreter/CommandCompletions.h" |
| 19 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 20 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 21 | #include "lldb/Core/Disassembler.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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(), |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 37 | m_start_addr(), |
| 38 | m_end_addr () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 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); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 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); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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(); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 109 | m_start_addr = LLDB_INVALID_ADDRESS; |
| 110 | m_end_addr = LLDB_INVALID_ADDRESS; |
Sean Callanan | 944be70 | 2010-06-17 00:32:05 +0000 | [diff] [blame] | 111 | raw = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | { |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 123 | { LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, NULL, "Show opcode bytes when disassembling."}, |
| 124 | { LLDB_OPT_SET_ALL, false, "context", 'c', required_argument, NULL, 0, "<num-lines>", "Number of context lines of source to show."}, |
| 125 | { LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, NULL, "Enable mixed source and assembly display."}, |
| 126 | { LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, NULL, "Print raw disassembly with no symbol information."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 128 | { LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, "<start-address>", "Address to start disassembling."}, |
| 129 | { LLDB_OPT_SET_1, false, "end-address", 'e', required_argument, NULL, 0, "<end-address>", "Address to start disassembling."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 131 | { LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, "<function-name>", "Disassemble entire contents of the given function name."}, |
| 132 | |
| 133 | { LLDB_OPT_SET_3, false, "current-frame", 'f', no_argument, NULL, 0, "<current-frame>", "Disassemble entire contents of the current frame's function."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | |
| 135 | { 0, false, NULL, 0, 0, NULL, 0, NULL, NULL } |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | |
| 140 | //------------------------------------------------------------------------- |
| 141 | // CommandObjectDisassemble |
| 142 | //------------------------------------------------------------------------- |
| 143 | |
| 144 | CommandObjectDisassemble::CommandObjectDisassemble () : |
| 145 | CommandObject ("disassemble", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame^] | 146 | "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.", |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 147 | "disassemble [<cmd-options>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | { |
| 149 | } |
| 150 | |
| 151 | CommandObjectDisassemble::~CommandObjectDisassemble() |
| 152 | { |
| 153 | } |
| 154 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | bool |
| 156 | CommandObjectDisassemble::Execute |
| 157 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 158 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | CommandReturnObject &result |
| 161 | ) |
| 162 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 163 | Target *target = interpreter.GetDebugger().GetSelectedTarget().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 190 | if (command.GetArgumentCount() != 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | { |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 192 | result.AppendErrorWithFormat ("\"disassemble\" doesn't take any arguments.\n"); |
| 193 | result.SetStatus (eReturnStatusFailed); |
| 194 | return false; |
| 195 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 196 | ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 198 | if (m_options.show_mixed && m_options.num_lines_context == 0) |
| 199 | m_options.num_lines_context = 3; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 201 | if (!m_options.m_func_name.empty()) |
| 202 | { |
| 203 | ConstString name(m_options.m_func_name.c_str()); |
| 204 | |
| 205 | if (Disassembler::Disassemble (interpreter.GetDebugger(), |
| 206 | arch, |
| 207 | exe_ctx, |
| 208 | name, |
| 209 | NULL, // Module * |
| 210 | m_options.show_mixed ? m_options.num_lines_context : 0, |
| 211 | m_options.show_bytes, |
| 212 | result.GetOutputStream())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 214 | result.SetStatus (eReturnStatusSuccessFinishResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
| 218 | result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); |
| 219 | result.SetStatus (eReturnStatusFailed); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 221 | } |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 222 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 224 | AddressRange range; |
| 225 | if (m_options.m_start_addr != LLDB_INVALID_ADDRESS) |
| 226 | { |
| 227 | range.GetBaseAddress().SetOffset (m_options.m_start_addr); |
| 228 | if (m_options.m_end_addr != LLDB_INVALID_ADDRESS) |
| 229 | { |
| 230 | if (m_options.m_end_addr < m_options.m_start_addr) |
| 231 | { |
| 232 | result.AppendErrorWithFormat ("End address before start address.\n"); |
| 233 | result.SetStatus (eReturnStatusFailed); |
| 234 | return false; |
| 235 | } |
| 236 | range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr); |
| 237 | } |
| 238 | else |
| 239 | range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | if (exe_ctx.frame) |
| 244 | { |
| 245 | SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
| 246 | if (sc.function) |
| 247 | range = sc.function->GetAddressRange(); |
| 248 | else if (sc.symbol && sc.symbol->GetAddressRangePtr()) |
| 249 | range = *sc.symbol->GetAddressRangePtr(); |
| 250 | else |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 251 | range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress(); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
| 255 | result.AppendError ("invalid frame"); |
| 256 | result.SetStatus (eReturnStatusFailed); |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | if (range.GetByteSize() == 0) |
| 261 | range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); |
| 262 | |
| 263 | if (Disassembler::Disassemble (interpreter.GetDebugger(), |
| 264 | arch, |
| 265 | exe_ctx, |
| 266 | range, |
| 267 | m_options.show_mixed ? m_options.num_lines_context : 0, |
| 268 | m_options.show_bytes, |
| 269 | result.GetOutputStream())) |
| 270 | { |
| 271 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr); |
| 276 | result.SetStatus (eReturnStatusFailed); |
| 277 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 280 | return result.Succeeded(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | } |