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 | |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | default: |
| 99 | error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | CommandObjectDisassemble::CommandOptions::ResetOptionValues () |
| 108 | { |
| 109 | Options::ResetOptionValues(); |
| 110 | show_mixed = false; |
| 111 | show_bytes = false; |
| 112 | num_lines_context = 0; |
| 113 | m_func_name.clear(); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 114 | m_start_addr = LLDB_INVALID_ADDRESS; |
| 115 | m_end_addr = LLDB_INVALID_ADDRESS; |
Sean Callanan | 944be70 | 2010-06-17 00:32:05 +0000 | [diff] [blame] | 116 | raw = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | const lldb::OptionDefinition* |
| 120 | CommandObjectDisassemble::CommandOptions::GetDefinitions () |
| 121 | { |
| 122 | return g_option_table; |
| 123 | } |
| 124 | |
| 125 | lldb::OptionDefinition |
| 126 | CommandObjectDisassemble::CommandOptions::g_option_table[] = |
| 127 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 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."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | |
Caroline Tice | 43b014a | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 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."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 136 | { LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."}, |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 137 | |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 138 | { LLDB_OPT_SET_3, true, "current-frame", 'f', no_argument, NULL, 0, eArgTypeNone, "Disassemble entire contents of the current frame's function."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 140 | { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
| 144 | |
| 145 | //------------------------------------------------------------------------- |
| 146 | // CommandObjectDisassemble |
| 147 | //------------------------------------------------------------------------- |
| 148 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 149 | CommandObjectDisassemble::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>]") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | { |
| 155 | } |
| 156 | |
| 157 | CommandObjectDisassemble::~CommandObjectDisassemble() |
| 158 | { |
| 159 | } |
| 160 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | bool |
| 162 | CommandObjectDisassemble::Execute |
| 163 | ( |
| 164 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | CommandReturnObject &result |
| 166 | ) |
| 167 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 168 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | { |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 188 | result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.GetArchitectureName()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | result.SetStatus (eReturnStatusFailed); |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 194 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 195 | if (command.GetArgumentCount() != 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 197 | result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n"); |
| 198 | GetOptions()->GenerateOptionUsage (m_interpreter, |
| 199 | result.GetErrorStream(), |
| 200 | this); |
| 201 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 202 | result.SetStatus (eReturnStatusFailed); |
| 203 | return false; |
| 204 | } |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 205 | ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 207 | if (m_options.show_mixed && m_options.num_lines_context == 0) |
Greg Clayton | 1924e24 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 208 | m_options.num_lines_context = 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 210 | if (!m_options.m_func_name.empty()) |
| 211 | { |
| 212 | ConstString name(m_options.m_func_name.c_str()); |
| 213 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 214 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 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, |
Sean Callanan | a846cc3 | 2011-03-10 23:35:12 +0000 | [diff] [blame^] | 221 | m_options.raw, |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 222 | result.GetOutputStream())) |
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 | result.SetStatus (eReturnStatusSuccessFinishResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
| 228 | result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); |
| 229 | result.SetStatus (eReturnStatusFailed); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 231 | } |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 232 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 234 | AddressRange range; |
| 235 | if (m_options.m_start_addr != LLDB_INVALID_ADDRESS) |
| 236 | { |
| 237 | range.GetBaseAddress().SetOffset (m_options.m_start_addr); |
| 238 | if (m_options.m_end_addr != LLDB_INVALID_ADDRESS) |
| 239 | { |
| 240 | if (m_options.m_end_addr < m_options.m_start_addr) |
| 241 | { |
| 242 | result.AppendErrorWithFormat ("End address before start address.\n"); |
| 243 | result.SetStatus (eReturnStatusFailed); |
| 244 | return false; |
| 245 | } |
| 246 | range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr); |
| 247 | } |
| 248 | else |
| 249 | range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE); |
| 250 | } |
| 251 | else |
| 252 | { |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 253 | // The default action is to disassemble the current frame function. |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 254 | if (exe_ctx.frame) |
| 255 | { |
Jason Molenda | 8f0ad08 | 2010-11-04 09:43:27 +0000 | [diff] [blame] | 256 | SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 257 | if (sc.function) |
| 258 | range = sc.function->GetAddressRange(); |
| 259 | else if (sc.symbol && sc.symbol->GetAddressRangePtr()) |
| 260 | range = *sc.symbol->GetAddressRangePtr(); |
| 261 | else |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 262 | range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress(); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
| 266 | result.AppendError ("invalid frame"); |
| 267 | result.SetStatus (eReturnStatusFailed); |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | if (range.GetByteSize() == 0) |
| 272 | range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); |
| 273 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 274 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 275 | arch, |
| 276 | exe_ctx, |
| 277 | range, |
| 278 | m_options.show_mixed ? m_options.num_lines_context : 0, |
| 279 | m_options.show_bytes, |
Sean Callanan | a846cc3 | 2011-03-10 23:35:12 +0000 | [diff] [blame^] | 280 | m_options.raw, |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 281 | result.GetOutputStream())) |
| 282 | { |
| 283 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr); |
| 288 | result.SetStatus (eReturnStatusFailed); |
| 289 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 292 | return result.Succeeded(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | } |