blob: 516ea0368061ab291275bce433be8c1796c6d2c4 [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"
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/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(
131 Args &command,
132 CommandContext *context,
133 CommandInterpreter *interpreter,
134 CommandReturnObject &result
135)
136{
137 ConstString target_triple;
138 int num_args = command.GetArgumentCount();
139
140 Target *target = context->GetTarget ();
141 if (target)
142 target->GetTargetTriple(target_triple);
143
144 if (!target_triple)
145 target_triple = Host::GetTargetTriple ();
146
147 ExecutionContext exe_ctx(context->GetExecutionContext());
148 if (exe_ctx.thread == NULL || exe_ctx.frame == NULL)
149 {
150 result.AppendError ("No currently selected thread and frame.");
151 result.SetStatus (eReturnStatusFailed);
152 return false;
153 }
154
155 if (num_args < 2)
156 {
157 result.AppendErrorWithFormat ("Invalid usage, should be: %s.\n", GetSyntax());
158 result.SetStatus (eReturnStatusFailed);
159 return false;
160 }
161
162 if ((num_args - 2) %2 != 0)
163 {
164 result.AppendErrorWithFormat ("Invalid usage - unmatched args & types, should be: %s.\n", GetSyntax());
165 result.SetStatus (eReturnStatusFailed);
166 return false;
167 }
168
169 if (target_triple)
170 {
171 //const char *return_type = command.GetArgumentAtIndex(0);
172 const char *function_name = command.GetArgumentAtIndex(1);
173 // Look up the called function:
174
175 Function *target_fn = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything).FindFunctionByName (function_name);
176
177 // FIXME: If target_fn is NULL, we should look up the name as a symbol and use it and the provided
178 // return type.
179
180 if (target_fn == NULL)
181 {
182 result.AppendErrorWithFormat ("Could not find function '%s'.\n", function_name);
183 result.SetStatus (eReturnStatusFailed);
184 return false;
185 }
186
187 ValueList value_list;
188 // Okay, now parse arguments. For now we only accept basic types.
189 for (int i = 2; i < num_args; i+= 2)
190 {
191 const char *type_str = command.GetArgumentAtIndex(i);
192 const char *value_str = command.GetArgumentAtIndex(i + 1);
193 bool success;
194 if (strcmp(type_str, "int") == 0
195 || strcmp(type_str, "int32_t") == 0)
196 {
197 value_list.PushValue(Value(Args::StringToSInt32(value_str, 0, 0, &success)));
198 }
199 else if (strcmp (type_str, "int64_t") == 0)
200 {
201 value_list.PushValue(Value(Args::StringToSInt64(value_str, 0, 0, &success)));
202 }
203 else if (strcmp(type_str, "uint") == 0
204 || strcmp(type_str, "uint32_t") == 0)
205 {
206 value_list.PushValue(Value(Args::StringToUInt32(value_str, 0, 0, &success)));
207 }
208 else if (strcmp (type_str, "uint64_t") == 0)
209 {
210 value_list.PushValue(Value(Args::StringToUInt64(value_str, 0, 0, &success)));
211 }
212 else if (strcmp (type_str, "cstr") == 0)
213 {
214 Value val ((intptr_t)value_str);
215 val.SetValueType (Value::eValueTypeHostAddress);
216
217
218 void *cstr_type = target->GetScratchClangASTContext()->GetCStringType(true);
219 val.SetContext (Value::eContextTypeOpaqueClangQualType, cstr_type);
220 value_list.PushValue(val);
221
222 success = true;
223 }
224
225 if (!success)
226 {
227 result.AppendErrorWithFormat ("Could not convert value: '%s' to type '%s'.\n", value_str, type_str);
228 result.SetStatus (eReturnStatusFailed);
229 return false;
230 }
231 }
232 // Okay, we have the function and the argument list and the return type. Now make a ClangFunction object and
233 // run it:
234
235 StreamString errors;
236 ClangFunction clang_fun (target_triple.GetCString(), *target_fn, target->GetScratchClangASTContext(), value_list);
237 if (m_options.noexecute)
238 {
239 // Now write down the argument values for this call.
240 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
241 if (!clang_fun.InsertFunction (exe_ctx, args_addr, errors))
242 {
243 result.AppendErrorWithFormat("Error inserting function: '%s'.\n", errors.GetData());
244 result.SetStatus (eReturnStatusFailed);
245 return false;
246 }
247 else
248 {
249 result.Succeeded();
250 return true;
251 }
252 }
253
254 ClangFunction::ExecutionResults return_status;
255 Value return_value;
256
257 if (m_options.use_abi)
258 {
259 return_status = clang_fun.ExecuteFunctionWithABI(exe_ctx, errors, return_value);
260 }
261 else
262 {
263 bool stop_others = true;
264 return_status = clang_fun.ExecuteFunction(exe_ctx, errors, stop_others, NULL, return_value);
265 }
266
267 // Now figure out what to do with the return value.
268 if (return_status == ClangFunction::eExecutionSetupError)
269 {
270 result.AppendErrorWithFormat("Error setting up function execution: '%s'.\n", errors.GetData());
271 result.SetStatus (eReturnStatusFailed);
272 return false;
273 }
274 else if (return_status != ClangFunction::eExecutionCompleted)
275 {
276 result.AppendWarningWithFormat("Interrupted while calling function: '%s'.\n", errors.GetData());
277 result.SetStatus(eReturnStatusSuccessFinishNoResult);
278 return true;
279 }
280 else
281 {
282 // Now print out the result.
283 result.GetOutputStream().Printf("Return value: ");
284 return_value.Dump(&(result.GetOutputStream()));
285 result.Succeeded();
286 }
287
288 }
289 else
290 {
291 result.AppendError ("invalid target triple");
292 result.SetStatus (eReturnStatusFailed);
293 }
294 return result.Succeeded();
295}
296
297lldb::OptionDefinition
298CommandObjectCall::CommandOptions::g_option_table[] =
299{
Jim Ingham34e9a982010-06-15 18:47:14 +0000300{ LLDB_OPT_SET_1, true, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
301{ 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."},
302{ LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
303{ LLDB_OPT_SET_1, false, "noexecute", 'n', no_argument, NULL, 0, "no execute", "Only JIT and copy the wrapper & arguments, but don't execute."},
304{ 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 +0000305{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
306};
307