Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectCall.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 "CommandObjectCall.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/StackFrame.h" |
| 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | // This command is a toy. I'm just using it to have a way to construct the arguments to |
| 35 | // calling functions. |
| 36 | // |
| 37 | |
| 38 | CommandObjectCall::CommandOptions::CommandOptions () : |
| 39 | Options() |
| 40 | { |
| 41 | // Keep only one place to reset the values to their defaults |
| 42 | ResetOptionValues(); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | CommandObjectCall::CommandOptions::~CommandOptions () |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | Error |
| 51 | CommandObjectCall::CommandOptions::SetOptionValue (int option_idx, const char *option_arg) |
| 52 | { |
| 53 | Error error; |
| 54 | |
| 55 | char short_option = (char) m_getopt_table[option_idx].val; |
| 56 | |
| 57 | switch (short_option) |
| 58 | { |
| 59 | case 'l': |
| 60 | if (language.SetLanguageFromCString (option_arg) == false) |
| 61 | { |
| 62 | error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg); |
| 63 | } |
| 64 | break; |
| 65 | |
| 66 | case 'g': |
| 67 | debug = true; |
| 68 | break; |
| 69 | |
| 70 | case 'f': |
| 71 | error = Args::StringToFormat(option_arg,format); |
| 72 | break; |
| 73 | |
| 74 | case 'n': |
| 75 | noexecute = true; |
| 76 | break; |
| 77 | |
| 78 | case 'a': |
| 79 | use_abi = true; |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | return error; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | CommandObjectCall::CommandOptions::ResetOptionValues () |
| 92 | { |
| 93 | Options::ResetOptionValues(); |
| 94 | language.Clear(); |
| 95 | debug = false; |
| 96 | format = eFormatDefault; |
| 97 | show_types = true; |
| 98 | show_summary = true; |
| 99 | noexecute = false; |
| 100 | use_abi = false; |
| 101 | } |
| 102 | |
| 103 | const lldb::OptionDefinition* |
| 104 | CommandObjectCall::CommandOptions::GetDefinitions () |
| 105 | { |
| 106 | return g_option_table; |
| 107 | } |
| 108 | |
| 109 | CommandObjectCall::CommandObjectCall () : |
| 110 | CommandObject ( |
| 111 | "call", |
| 112 | "Call a function.", |
| 113 | "call <return_type> <function-name> [[<arg1-type> <arg1-value>] ... <argn-type> <argn-value>] [<cmd-options>]", |
| 114 | eFlagProcessMustBeLaunched | eFlagProcessMustBePaused) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | CommandObjectCall::~CommandObjectCall () |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | Options * |
| 123 | CommandObjectCall::GetOptions () |
| 124 | { |
| 125 | return &m_options; |
| 126 | } |
| 127 | |
| 128 | bool |
| 129 | CommandObjectCall::Execute |
| 130 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 131 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | Args &command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | CommandReturnObject &result |
| 134 | ) |
| 135 | { |
| 136 | ConstString target_triple; |
| 137 | int num_args = command.GetArgumentCount(); |
| 138 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 139 | ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext()); |
| 140 | if (exe_ctx.target) |
| 141 | exe_ctx.target->GetTargetTriple(target_triple); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
| 143 | if (!target_triple) |
| 144 | target_triple = Host::GetTargetTriple (); |
| 145 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | if (exe_ctx.thread == NULL || exe_ctx.frame == NULL) |
| 147 | { |
| 148 | result.AppendError ("No currently selected thread and frame."); |
| 149 | result.SetStatus (eReturnStatusFailed); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (num_args < 2) |
| 154 | { |
| 155 | result.AppendErrorWithFormat ("Invalid usage, should be: %s.\n", GetSyntax()); |
| 156 | result.SetStatus (eReturnStatusFailed); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if ((num_args - 2) %2 != 0) |
| 161 | { |
| 162 | result.AppendErrorWithFormat ("Invalid usage - unmatched args & types, should be: %s.\n", GetSyntax()); |
| 163 | result.SetStatus (eReturnStatusFailed); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (target_triple) |
| 168 | { |
| 169 | //const char *return_type = command.GetArgumentAtIndex(0); |
| 170 | const char *function_name = command.GetArgumentAtIndex(1); |
| 171 | // Look up the called function: |
| 172 | |
| 173 | Function *target_fn = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything).FindFunctionByName (function_name); |
| 174 | |
| 175 | // FIXME: If target_fn is NULL, we should look up the name as a symbol and use it and the provided |
| 176 | // return type. |
| 177 | |
| 178 | if (target_fn == NULL) |
| 179 | { |
| 180 | result.AppendErrorWithFormat ("Could not find function '%s'.\n", function_name); |
| 181 | result.SetStatus (eReturnStatusFailed); |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | ValueList value_list; |
| 186 | // Okay, now parse arguments. For now we only accept basic types. |
| 187 | for (int i = 2; i < num_args; i+= 2) |
| 188 | { |
| 189 | const char *type_str = command.GetArgumentAtIndex(i); |
| 190 | const char *value_str = command.GetArgumentAtIndex(i + 1); |
| 191 | bool success; |
| 192 | if (strcmp(type_str, "int") == 0 |
| 193 | || strcmp(type_str, "int32_t") == 0) |
| 194 | { |
| 195 | value_list.PushValue(Value(Args::StringToSInt32(value_str, 0, 0, &success))); |
| 196 | } |
| 197 | else if (strcmp (type_str, "int64_t") == 0) |
| 198 | { |
| 199 | value_list.PushValue(Value(Args::StringToSInt64(value_str, 0, 0, &success))); |
| 200 | } |
| 201 | else if (strcmp(type_str, "uint") == 0 |
| 202 | || strcmp(type_str, "uint32_t") == 0) |
| 203 | { |
| 204 | value_list.PushValue(Value(Args::StringToUInt32(value_str, 0, 0, &success))); |
| 205 | } |
| 206 | else if (strcmp (type_str, "uint64_t") == 0) |
| 207 | { |
| 208 | value_list.PushValue(Value(Args::StringToUInt64(value_str, 0, 0, &success))); |
| 209 | } |
| 210 | else if (strcmp (type_str, "cstr") == 0) |
| 211 | { |
| 212 | Value val ((intptr_t)value_str); |
| 213 | val.SetValueType (Value::eValueTypeHostAddress); |
| 214 | |
| 215 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 216 | void *cstr_type = exe_ctx.target->GetScratchClangASTContext()->GetCStringType(true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | val.SetContext (Value::eContextTypeOpaqueClangQualType, cstr_type); |
| 218 | value_list.PushValue(val); |
| 219 | |
| 220 | success = true; |
| 221 | } |
| 222 | |
| 223 | if (!success) |
| 224 | { |
| 225 | result.AppendErrorWithFormat ("Could not convert value: '%s' to type '%s'.\n", value_str, type_str); |
| 226 | result.SetStatus (eReturnStatusFailed); |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | // Okay, we have the function and the argument list and the return type. Now make a ClangFunction object and |
| 231 | // run it: |
| 232 | |
| 233 | StreamString errors; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 234 | ClangFunction clang_fun (target_triple.GetCString(), *target_fn, exe_ctx.target->GetScratchClangASTContext(), value_list); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | if (m_options.noexecute) |
| 236 | { |
| 237 | // Now write down the argument values for this call. |
| 238 | lldb::addr_t args_addr = LLDB_INVALID_ADDRESS; |
| 239 | if (!clang_fun.InsertFunction (exe_ctx, args_addr, errors)) |
| 240 | { |
| 241 | result.AppendErrorWithFormat("Error inserting function: '%s'.\n", errors.GetData()); |
| 242 | result.SetStatus (eReturnStatusFailed); |
| 243 | return false; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | result.Succeeded(); |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | ClangFunction::ExecutionResults return_status; |
| 253 | Value return_value; |
| 254 | |
| 255 | if (m_options.use_abi) |
| 256 | { |
| 257 | return_status = clang_fun.ExecuteFunctionWithABI(exe_ctx, errors, return_value); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | bool stop_others = true; |
| 262 | return_status = clang_fun.ExecuteFunction(exe_ctx, errors, stop_others, NULL, return_value); |
| 263 | } |
| 264 | |
| 265 | // Now figure out what to do with the return value. |
| 266 | if (return_status == ClangFunction::eExecutionSetupError) |
| 267 | { |
| 268 | result.AppendErrorWithFormat("Error setting up function execution: '%s'.\n", errors.GetData()); |
| 269 | result.SetStatus (eReturnStatusFailed); |
| 270 | return false; |
| 271 | } |
| 272 | else if (return_status != ClangFunction::eExecutionCompleted) |
| 273 | { |
| 274 | result.AppendWarningWithFormat("Interrupted while calling function: '%s'.\n", errors.GetData()); |
| 275 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 276 | return true; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | // Now print out the result. |
| 281 | result.GetOutputStream().Printf("Return value: "); |
| 282 | return_value.Dump(&(result.GetOutputStream())); |
| 283 | result.Succeeded(); |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | result.AppendError ("invalid target triple"); |
| 290 | result.SetStatus (eReturnStatusFailed); |
| 291 | } |
| 292 | return result.Succeeded(); |
| 293 | } |
| 294 | |
| 295 | lldb::OptionDefinition |
| 296 | CommandObjectCall::CommandOptions::g_option_table[] = |
| 297 | { |
Greg Clayton | 12bec71 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 298 | { LLDB_OPT_SET_1, false, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."}, |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 299 | { LLDB_OPT_SET_1, false, "format", 'f', required_argument, NULL, 0, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]", "Specify the format that the expression output should use."}, |
| 300 | { LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."}, |
| 301 | { LLDB_OPT_SET_1, false, "noexecute", 'n', no_argument, NULL, 0, "no execute", "Only JIT and copy the wrapper & arguments, but don't execute."}, |
| 302 | { LLDB_OPT_SET_1, false, "useabi", 'a', no_argument, NULL, 0, NULL, "Use the ABI instead of the JIT to marshall arguments."}, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | { 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL } |
| 304 | }; |
| 305 | |