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 |
| 16 | #include "lldb/Core/Args.h" |
| 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" |
| 23 | #include "lldb/Interpreter/CommandContext.h" |
| 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 | |
| 80 | CommandObjectArgs::CommandObjectArgs () : |
| 81 | CommandObject ("args", |
| 82 | "When stopped at the start of a function, reads function arguments of type (u?)int(8|16|32|64)_t, (void|char)*", |
| 83 | "args") |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | CommandObjectArgs::~CommandObjectArgs () |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | Options * |
| 92 | CommandObjectArgs::GetOptions () |
| 93 | { |
| 94 | return &m_options; |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | CommandObjectArgs::Execute(Args &command, |
| 99 | CommandContext *context, |
| 100 | CommandInterpreter *interpreter, |
| 101 | CommandReturnObject &result) |
| 102 | { |
| 103 | ConstString target_triple; |
| 104 | |
| 105 | Process *process = context->GetExecutionContext().process; |
| 106 | if (!process) |
| 107 | { |
| 108 | result.AppendError ("Args found no process."); |
| 109 | result.SetStatus (eReturnStatusFailed); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | const ABI *abi = process->GetABI (); |
| 114 | if (!abi) |
| 115 | { |
| 116 | result.AppendError ("The current process has no ABI."); |
| 117 | result.SetStatus (eReturnStatusFailed); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | int num_args = command.GetArgumentCount (); |
| 122 | int arg_index; |
| 123 | |
| 124 | if (!num_args) |
| 125 | { |
| 126 | result.AppendError ("args requires at least one argument"); |
| 127 | result.SetStatus (eReturnStatusFailed); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | Thread *thread = context->GetExecutionContext ().thread; |
| 132 | |
| 133 | if (!thread) |
| 134 | { |
| 135 | result.AppendError ("args found no thread."); |
| 136 | result.SetStatus (eReturnStatusFailed); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | lldb::StackFrameSP thread_cur_frame = thread->GetCurrentFrame (); |
| 141 | if (!thread_cur_frame) |
| 142 | { |
| 143 | result.AppendError ("The current thread has no current frame."); |
| 144 | result.SetStatus (eReturnStatusFailed); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | Module *thread_module = thread_cur_frame->GetPC ().GetModule (); |
| 149 | if (!thread_module) |
| 150 | { |
| 151 | result.AppendError ("The PC has no associated module."); |
| 152 | result.SetStatus (eReturnStatusFailed); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | TypeList *thread_type_list = thread_module->GetTypeList (); |
| 157 | if (!thread_type_list) |
| 158 | { |
| 159 | result.AppendError ("The module has no type list."); |
| 160 | result.SetStatus (eReturnStatusFailed); |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | ClangASTContext &ast_context = thread_type_list->GetClangASTContext(); |
| 165 | |
| 166 | ValueList value_list; |
| 167 | |
| 168 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 169 | { |
| 170 | const char *arg_type_cstr = command.GetArgumentAtIndex(arg_index); |
| 171 | Value value; |
| 172 | value.SetValueType(Value::eValueTypeScalar); |
| 173 | void *type; |
| 174 | |
| 175 | char *int_pos; |
Eli Friedman | b4a4728 | 2010-06-09 07:57:51 +0000 | [diff] [blame^] | 176 | if ((int_pos = strstr (const_cast<char*>(arg_type_cstr), "int"))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | { |
| 178 | Encoding encoding = eEncodingSint; |
| 179 | |
| 180 | int width = 0; |
| 181 | |
| 182 | if (int_pos > arg_type_cstr + 1) |
| 183 | { |
| 184 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 185 | result.SetStatus (eReturnStatusFailed); |
| 186 | return false; |
| 187 | } |
| 188 | if (int_pos == arg_type_cstr + 1 && arg_type_cstr[0] != 'u') |
| 189 | { |
| 190 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 191 | result.SetStatus (eReturnStatusFailed); |
| 192 | return false; |
| 193 | } |
| 194 | if (arg_type_cstr[0] == 'u') |
| 195 | { |
| 196 | encoding = eEncodingUint; |
| 197 | } |
| 198 | |
| 199 | char *width_pos = int_pos + 3; |
| 200 | |
| 201 | if (!strcmp (width_pos, "8_t")) |
| 202 | width = 8; |
| 203 | else if (!strcmp (width_pos, "16_t")) |
| 204 | width = 16; |
| 205 | else if (!strcmp (width_pos, "32_t")) |
| 206 | width = 32; |
| 207 | else if (!strcmp (width_pos, "64_t")) |
| 208 | width = 64; |
| 209 | else |
| 210 | { |
| 211 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 212 | result.SetStatus (eReturnStatusFailed); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | type = ast_context.GetBuiltinTypeForEncodingAndBitSize(encoding, width); |
| 217 | |
| 218 | if (!type) |
| 219 | { |
| 220 | result.AppendErrorWithFormat ("Couldn't get Clang type for format %s (%s integer, width %d).\n", |
| 221 | arg_type_cstr, |
| 222 | (encoding == eEncodingSint ? "signed" : "unsigned"), |
| 223 | width); |
| 224 | |
| 225 | result.SetStatus (eReturnStatusFailed); |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | else if (strchr (arg_type_cstr, '*')) |
| 230 | { |
| 231 | if (!strcmp (arg_type_cstr, "void*")) |
| 232 | type = ast_context.CreatePointerType (ast_context.GetVoidBuiltInType ()); |
| 233 | else if (!strcmp (arg_type_cstr, "char*")) |
| 234 | type = ast_context.GetCStringType (false); |
| 235 | else |
| 236 | { |
| 237 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 238 | result.SetStatus (eReturnStatusFailed); |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | result.AppendErrorWithFormat ("Invalid format: %s.\n", arg_type_cstr); |
| 245 | result.SetStatus (eReturnStatusFailed); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | value.SetContext (Value::eContextTypeOpaqueClangQualType, type); |
| 250 | |
| 251 | value_list.PushValue(value); |
| 252 | } |
| 253 | |
| 254 | if (!abi->GetArgumentValues (*thread, value_list)) |
| 255 | { |
| 256 | result.AppendError ("Couldn't get argument values"); |
| 257 | result.SetStatus (eReturnStatusFailed); |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | result.GetOutputStream ().Printf("Arguments : \n"); |
| 262 | |
| 263 | for (arg_index = 0; arg_index < num_args; ++arg_index) |
| 264 | { |
| 265 | result.GetOutputStream ().Printf ("%d (%s): ", arg_index, command.GetArgumentAtIndex (arg_index)); |
| 266 | value_list.GetValueAtIndex (arg_index)->Dump (&result.GetOutputStream ()); |
| 267 | result.GetOutputStream ().Printf("\n"); |
| 268 | } |
| 269 | |
| 270 | return result.Succeeded(); |
| 271 | } |
| 272 | |
| 273 | lldb::OptionDefinition |
| 274 | CommandObjectArgs::CommandOptions::g_option_table[] = |
| 275 | { |
| 276 | { 0, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."}, |
| 277 | { 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL } |
| 278 | }; |
| 279 | |