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