blob: b3dfc533b166c6e27862fb48053a918fd6842f40 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Ingham84cdc152010-06-15 19:49:27 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#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 Clayton63094e02010-06-23 01:19:29 +000023#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#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
31using namespace lldb;
32using 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
38CommandObjectCall::CommandOptions::CommandOptions () :
39 Options()
40{
41 // Keep only one place to reset the values to their defaults
42 ResetOptionValues();
43}
44
45
46CommandObjectCall::CommandOptions::~CommandOptions ()
47{
48}
49
50Error
51CommandObjectCall::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
90void
91CommandObjectCall::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
103const lldb::OptionDefinition*
104CommandObjectCall::CommandOptions::GetDefinitions ()
105{
106 return g_option_table;
107}
108
109CommandObjectCall::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
118CommandObjectCall::~CommandObjectCall ()
119{
120}
121
122Options *
123CommandObjectCall::GetOptions ()
124{
125 return &m_options;
126}
127
128bool
129CommandObjectCall::Execute
130(
Greg Clayton63094e02010-06-23 01:19:29 +0000131 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000132 Args &command,
Chris Lattner24943d22010-06-08 16:52:24 +0000133 CommandReturnObject &result
134)
135{
136 ConstString target_triple;
137 int num_args = command.GetArgumentCount();
138
Greg Clayton63094e02010-06-23 01:19:29 +0000139 ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext());
140 if (exe_ctx.target)
141 exe_ctx.target->GetTargetTriple(target_triple);
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 if (!target_triple)
144 target_triple = Host::GetTargetTriple ();
145
Chris Lattner24943d22010-06-08 16:52:24 +0000146 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:
Sean Callanan0fc73582010-07-27 00:55:47 +0000172
173 Function *target_fn = NULL;
174
175 SymbolContextList sc_list;
176
177 exe_ctx.frame->GetSymbolContext(eSymbolContextEverything).FindFunctionsByName(ConstString(function_name), false, sc_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000178
Sean Callanan0fc73582010-07-27 00:55:47 +0000179 if (sc_list.GetSize() > 0)
180 {
181 SymbolContext sc;
182 sc_list.GetContextAtIndex(0, sc);
183 target_fn = sc.function;
184 }
185
Chris Lattner24943d22010-06-08 16:52:24 +0000186 // FIXME: If target_fn is NULL, we should look up the name as a symbol and use it and the provided
187 // return type.
188
189 if (target_fn == NULL)
190 {
191 result.AppendErrorWithFormat ("Could not find function '%s'.\n", function_name);
192 result.SetStatus (eReturnStatusFailed);
193 return false;
194 }
195
196 ValueList value_list;
197 // Okay, now parse arguments. For now we only accept basic types.
198 for (int i = 2; i < num_args; i+= 2)
199 {
200 const char *type_str = command.GetArgumentAtIndex(i);
201 const char *value_str = command.GetArgumentAtIndex(i + 1);
202 bool success;
203 if (strcmp(type_str, "int") == 0
204 || strcmp(type_str, "int32_t") == 0)
205 {
206 value_list.PushValue(Value(Args::StringToSInt32(value_str, 0, 0, &success)));
207 }
208 else if (strcmp (type_str, "int64_t") == 0)
209 {
210 value_list.PushValue(Value(Args::StringToSInt64(value_str, 0, 0, &success)));
211 }
212 else if (strcmp(type_str, "uint") == 0
213 || strcmp(type_str, "uint32_t") == 0)
214 {
215 value_list.PushValue(Value(Args::StringToUInt32(value_str, 0, 0, &success)));
216 }
217 else if (strcmp (type_str, "uint64_t") == 0)
218 {
219 value_list.PushValue(Value(Args::StringToUInt64(value_str, 0, 0, &success)));
220 }
221 else if (strcmp (type_str, "cstr") == 0)
222 {
223 Value val ((intptr_t)value_str);
224 val.SetValueType (Value::eValueTypeHostAddress);
225
226
Greg Clayton63094e02010-06-23 01:19:29 +0000227 void *cstr_type = exe_ctx.target->GetScratchClangASTContext()->GetCStringType(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000228 val.SetContext (Value::eContextTypeOpaqueClangQualType, cstr_type);
229 value_list.PushValue(val);
230
231 success = true;
232 }
233
234 if (!success)
235 {
236 result.AppendErrorWithFormat ("Could not convert value: '%s' to type '%s'.\n", value_str, type_str);
237 result.SetStatus (eReturnStatusFailed);
238 return false;
239 }
240 }
241 // Okay, we have the function and the argument list and the return type. Now make a ClangFunction object and
242 // run it:
243
244 StreamString errors;
Greg Clayton63094e02010-06-23 01:19:29 +0000245 ClangFunction clang_fun (target_triple.GetCString(), *target_fn, exe_ctx.target->GetScratchClangASTContext(), value_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000246 if (m_options.noexecute)
247 {
248 // Now write down the argument values for this call.
249 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
250 if (!clang_fun.InsertFunction (exe_ctx, args_addr, errors))
251 {
252 result.AppendErrorWithFormat("Error inserting function: '%s'.\n", errors.GetData());
253 result.SetStatus (eReturnStatusFailed);
254 return false;
255 }
256 else
257 {
258 result.Succeeded();
259 return true;
260 }
261 }
262
263 ClangFunction::ExecutionResults return_status;
264 Value return_value;
265
266 if (m_options.use_abi)
267 {
268 return_status = clang_fun.ExecuteFunctionWithABI(exe_ctx, errors, return_value);
269 }
270 else
271 {
272 bool stop_others = true;
273 return_status = clang_fun.ExecuteFunction(exe_ctx, errors, stop_others, NULL, return_value);
274 }
275
276 // Now figure out what to do with the return value.
277 if (return_status == ClangFunction::eExecutionSetupError)
278 {
279 result.AppendErrorWithFormat("Error setting up function execution: '%s'.\n", errors.GetData());
280 result.SetStatus (eReturnStatusFailed);
281 return false;
282 }
283 else if (return_status != ClangFunction::eExecutionCompleted)
284 {
285 result.AppendWarningWithFormat("Interrupted while calling function: '%s'.\n", errors.GetData());
286 result.SetStatus(eReturnStatusSuccessFinishNoResult);
287 return true;
288 }
289 else
290 {
291 // Now print out the result.
292 result.GetOutputStream().Printf("Return value: ");
293 return_value.Dump(&(result.GetOutputStream()));
294 result.Succeeded();
295 }
296
297 }
298 else
299 {
300 result.AppendError ("invalid target triple");
301 result.SetStatus (eReturnStatusFailed);
302 }
303 return result.Succeeded();
304}
305
306lldb::OptionDefinition
307CommandObjectCall::CommandOptions::g_option_table[] =
308{
Greg Clayton12bec712010-06-28 21:30:43 +0000309{ 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 Ingham34e9a982010-06-15 18:47:14 +0000310{ 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."},
311{ LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
312{ LLDB_OPT_SET_1, false, "noexecute", 'n', no_argument, NULL, 0, "no execute", "Only JIT and copy the wrapper & arguments, but don't execute."},
313{ LLDB_OPT_SET_1, false, "useabi", 'a', no_argument, NULL, 0, NULL, "Use the ABI instead of the JIT to marshall arguments."},
Chris Lattner24943d22010-06-08 16:52:24 +0000314{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
315};
316