Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectArgs.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 "CommandObjectArgs.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Value.h" |
| 18 | #include "lldb/Expression/ClangExpression.h" |
| 19 | #include "lldb/Expression/ClangExpressionVariable.h" |
| 20 | #include "lldb/Expression/ClangFunction.h" |
| 21 | #include "lldb/Host/Host.h" |
| 22 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 25 | #include "lldb/Symbol/ObjectFile.h" |
| 26 | #include "lldb/Symbol/Variable.h" |
| 27 | #include "lldb/Target/Process.h" |
| 28 | #include "lldb/Target/Target.h" |
| 29 | #include "lldb/Target/Thread.h" |
| 30 | #include "lldb/Target/StackFrame.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | // This command is a toy. I'm just using it to have a way to construct the arguments to |
| 36 | // calling functions. |
| 37 | // |
| 38 | |
| 39 | CommandObjectArgs::CommandOptions::CommandOptions () : |
| 40 | Options() |
| 41 | { |
| 42 | // Keep only one place to reset the values to their defaults |
| 43 | ResetOptionValues(); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | CommandObjectArgs::CommandOptions::~CommandOptions () |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | Error |
| 52 | CommandObjectArgs::CommandOptions::SetOptionValue (int option_idx, const char *option_arg) |
| 53 | { |
| 54 | Error error; |
| 55 | |
| 56 | char short_option = (char) m_getopt_table[option_idx].val; |
| 57 | |
| 58 | switch (short_option) |
| 59 | { |
| 60 | default: |
| 61 | error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | return error; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | CommandObjectArgs::CommandOptions::ResetOptionValues () |
| 70 | { |
| 71 | Options::ResetOptionValues(); |
| 72 | } |
| 73 | |
| 74 | const lldb::OptionDefinition* |
| 75 | CommandObjectArgs::CommandOptions::GetDefinitions () |
| 76 | { |
| 77 | return g_option_table; |
| 78 | } |
| 79 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 80 | CommandObjectArgs::CommandObjectArgs (CommandInterpreter &interpreter) : |
| 81 | CommandObject (interpreter, |
| 82 | "args", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*", |
| 84 | "args") |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | CommandObjectArgs::~CommandObjectArgs () |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | Options * |
| 93 | CommandObjectArgs::GetOptions () |
| 94 | { |
| 95 | return &m_options; |
| 96 | } |
| 97 | |
| 98 | bool |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 99 | CommandObjectArgs::Execute |
| 100 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 101 | Args& args, |
| 102 | CommandReturnObject &result |
| 103 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | { |
| 105 | ConstString target_triple; |
| 106 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 107 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 108 | Process *process = m_interpreter.GetDebugger().GetExecutionContext().process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | if (!process) |
| 110 | { |
| 111 | result.AppendError ("Args found no process."); |
| 112 | result.SetStatus (eReturnStatusFailed); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | const ABI *abi = process->GetABI (); |
| 117 | if (!abi) |
| 118 | { |
| 119 | result.AppendError ("The current process has no ABI."); |
| 120 | result.SetStatus (eReturnStatusFailed); |
| 121 | return false; |
| 122 | } |
| 123 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 124 | int num_args = args.GetArgumentCount (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | int arg_index; |
| 126 | |
| 127 | if (!num_args) |
| 128 | { |
| 129 | result.AppendError ("args requires at least one argument"); |
| 130 | result.SetStatus (eReturnStatusFailed); |
| 131 | return false; |
| 132 | } |
| 133 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 134 | Thread *thread = m_interpreter.GetDebugger().GetExecutionContext ().thread; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
| 136 | if (!thread) |
| 137 | { |
| 138 | result.AppendError ("args found no thread."); |
| 139 | result.SetStatus (eReturnStatusFailed); |
| 140 | return false; |
| 141 | } |
| 142 | |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 143 | lldb::StackFrameSP thread_cur_frame = thread->GetSelectedFrame (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | if (!thread_cur_frame) |
| 145 | { |
| 146 | result.AppendError ("The current thread has no current frame."); |
| 147 | result.SetStatus (eReturnStatusFailed); |
| 148 | return false; |
| 149 | } |
| 150 | |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 151 | Module *thread_module = thread_cur_frame->GetFrameCodeAddress ().GetModule (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | if (!thread_module) |
| 153 | { |
| 154 | result.AppendError ("The PC has no associated module."); |
| 155 | result.SetStatus (eReturnStatusFailed); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | TypeList *thread_type_list = thread_module->GetTypeList (); |
| 160 | if (!thread_type_list) |
| 161 | { |
| 162 | result.AppendError ("The module has no type list."); |
| 163 | result.SetStatus (eReturnStatusFailed); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | ClangASTContext &ast_context = thread_type_list->GetClangASTContext(); |
| 168 | |
| 169 | ValueList value_list; |
| 170 | |
| 171 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 172 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 173 | const char *arg_type_cstr = args.GetArgumentAtIndex(arg_index); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | Value value; |
| 175 | value.SetValueType(Value::eValueTypeScalar); |
| 176 | void *type; |
| 177 | |
| 178 | char *int_pos; |
Eli Friedman | b4a4728 | 2010-06-09 07:57:51 +0000 | [diff] [blame] | 179 | if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int"))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | { |
| 181 | Encoding encoding = eEncodingSint; |
| 182 | |
| 183 | int width = 0; |
| 184 | |
| 185 | if (int_pos > arg_type_cstr + 1) |
| 186 | { |
| 187 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 188 | result.SetStatus (eReturnStatusFailed); |
| 189 | return false; |
| 190 | } |
| 191 | if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u') |
| 192 | { |
| 193 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 194 | result.SetStatus (eReturnStatusFailed); |
| 195 | return false; |
| 196 | } |
| 197 | if (arg_type_cstr[0] == 'u') |
| 198 | { |
| 199 | encoding = eEncodingUint; |
| 200 | } |
| 201 | |
| 202 | char *width_pos = int_pos + 3; |
| 203 | |
| 204 | if (!strcmp (width_pos, "8_t")) |
| 205 | width = 8; |
| 206 | else if (!strcmp (width_pos, "16_t")) |
| 207 | width = 16; |
| 208 | else if (!strcmp (width_pos, "32_t")) |
| 209 | width = 32; |
| 210 | else if (!strcmp (width_pos, "64_t")) |
| 211 | width = 64; |
| 212 | else |
| 213 | { |
| 214 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 215 | result.SetStatus (eReturnStatusFailed); |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width); |
| 220 | |
| 221 | if (!type) |
| 222 | { |
| 223 | result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n", |
| 224 | arg_type_cstr, |
| 225 | (encoding == eEncodingSint ? "signed" : "unsigned"), |
| 226 | width); |
| 227 | |
| 228 | result.SetStatus (eReturnStatusFailed); |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | else if (strchr (arg_type_cstr, '*')) |
| 233 | { |
| 234 | if (!strcmp (arg_type_cstr, "void*")) |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 235 | type = ast_context.CreatePointerType (ast_context.GetBuiltInType_void ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | else if (!strcmp (arg_type_cstr, "char*")) |
| 237 | type = ast_context.GetCStringType (false); |
| 238 | else |
| 239 | { |
| 240 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 241 | result.SetStatus (eReturnStatusFailed); |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 248 | result.SetStatus (eReturnStatusFailed); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | value.SetContext (Value::eContextTypeOpaqueClangQualType, type); |
| 253 | |
| 254 | value_list.PushValue(value); |
| 255 | } |
| 256 | |
| 257 | if (!abi->GetArgumentValues (*thread, value_list)) |
| 258 | { |
| 259 | result.AppendError ("Couldn't get argument values"); |
| 260 | result.SetStatus (eReturnStatusFailed); |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | result.GetOutputStream ().Printf("Arguments : \n"); |
| 265 | |
| 266 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 267 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 268 | result.GetOutputStream ().Printf ("%d (%s): ", arg_index, args.GetArgumentAtIndex (arg_index)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ()); |
| 270 | result.GetOutputStream ().Printf("\n"); |
| 271 | } |
| 272 | |
| 273 | return result.Succeeded(); |
| 274 | } |
| 275 | |
| 276 | lldb::OptionDefinition |
| 277 | CommandObjectArgs::CommandOptions::g_option_table[] = |
| 278 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 279 | { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."}, |
| 280 | { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | }; |
| 282 | |