blob: 3e938fccaa624c633500dbe22af11400c805dd69 [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(
131 Args &command,
Chris Lattner24943d22010-06-08 16:52:24 +0000132 CommandReturnObject &result
133)
134{
135 ConstString target_triple;
136 int num_args = command.GetArgumentCount();
137
Greg Clayton63094e02010-06-23 01:19:29 +0000138 ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext());
139 if (exe_ctx.target)
140 exe_ctx.target->GetTargetTriple(target_triple);
Chris Lattner24943d22010-06-08 16:52:24 +0000141
142 if (!target_triple)
143 target_triple = Host::GetTargetTriple ();
144
Chris Lattner24943d22010-06-08 16:52:24 +0000145 if (exe_ctx.thread == NULL || exe_ctx.frame == NULL)
146 {
147 result.AppendError ("No currently selected thread and frame.");
148 result.SetStatus (eReturnStatusFailed);
149 return false;
150 }
151
152 if (num_args < 2)
153 {
154 result.AppendErrorWithFormat ("Invalid usage, should be: %s.\n", GetSyntax());
155 result.SetStatus (eReturnStatusFailed);
156 return false;
157 }
158
159 if ((num_args - 2) %2 != 0)
160 {
161 result.AppendErrorWithFormat ("Invalid usage - unmatched args & types, should be: %s.\n", GetSyntax());
162 result.SetStatus (eReturnStatusFailed);
163 return false;
164 }
165
166 if (target_triple)
167 {
168 //const char *return_type = command.GetArgumentAtIndex(0);
169 const char *function_name = command.GetArgumentAtIndex(1);
170 // Look up the called function:
Sean Callanan0fc73582010-07-27 00:55:47 +0000171
172 Function *target_fn = NULL;
173
174 SymbolContextList sc_list;
175
176 exe_ctx.frame->GetSymbolContext(eSymbolContextEverything).FindFunctionsByName(ConstString(function_name), false, sc_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000177
Sean Callanan0fc73582010-07-27 00:55:47 +0000178 if (sc_list.GetSize() > 0)
179 {
180 SymbolContext sc;
181 sc_list.GetContextAtIndex(0, sc);
182 target_fn = sc.function;
183 }
184
Chris Lattner24943d22010-06-08 16:52:24 +0000185 // FIXME: If target_fn is NULL, we should look up the name as a symbol and use it and the provided
186 // return type.
187
188 if (target_fn == NULL)
189 {
190 result.AppendErrorWithFormat ("Could not find function '%s'.\n", function_name);
191 result.SetStatus (eReturnStatusFailed);
192 return false;
193 }
194
195 ValueList value_list;
196 // Okay, now parse arguments. For now we only accept basic types.
197 for (int i = 2; i < num_args; i+= 2)
198 {
199 const char *type_str = command.GetArgumentAtIndex(i);
200 const char *value_str = command.GetArgumentAtIndex(i + 1);
201 bool success;
202 if (strcmp(type_str, "int") == 0
203 || strcmp(type_str, "int32_t") == 0)
204 {
205 value_list.PushValue(Value(Args::StringToSInt32(value_str, 0, 0, &success)));
206 }
207 else if (strcmp (type_str, "int64_t") == 0)
208 {
209 value_list.PushValue(Value(Args::StringToSInt64(value_str, 0, 0, &success)));
210 }
211 else if (strcmp(type_str, "uint") == 0
212 || strcmp(type_str, "uint32_t") == 0)
213 {
214 value_list.PushValue(Value(Args::StringToUInt32(value_str, 0, 0, &success)));
215 }
216 else if (strcmp (type_str, "uint64_t") == 0)
217 {
218 value_list.PushValue(Value(Args::StringToUInt64(value_str, 0, 0, &success)));
219 }
220 else if (strcmp (type_str, "cstr") == 0)
221 {
222 Value val ((intptr_t)value_str);
223 val.SetValueType (Value::eValueTypeHostAddress);
224
225
Greg Clayton63094e02010-06-23 01:19:29 +0000226 void *cstr_type = exe_ctx.target->GetScratchClangASTContext()->GetCStringType(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000227 val.SetContext (Value::eContextTypeOpaqueClangQualType, cstr_type);
228 value_list.PushValue(val);
229
230 success = true;
231 }
232
233 if (!success)
234 {
235 result.AppendErrorWithFormat ("Could not convert value: '%s' to type '%s'.\n", value_str, type_str);
236 result.SetStatus (eReturnStatusFailed);
237 return false;
238 }
239 }
240 // Okay, we have the function and the argument list and the return type. Now make a ClangFunction object and
241 // run it:
242
243 StreamString errors;
Greg Clayton63094e02010-06-23 01:19:29 +0000244 ClangFunction clang_fun (target_triple.GetCString(), *target_fn, exe_ctx.target->GetScratchClangASTContext(), value_list);
Chris Lattner24943d22010-06-08 16:52:24 +0000245 if (m_options.noexecute)
246 {
247 // Now write down the argument values for this call.
248 lldb::addr_t args_addr = LLDB_INVALID_ADDRESS;
249 if (!clang_fun.InsertFunction (exe_ctx, args_addr, errors))
250 {
251 result.AppendErrorWithFormat("Error inserting function: '%s'.\n", errors.GetData());
252 result.SetStatus (eReturnStatusFailed);
253 return false;
254 }
255 else
256 {
257 result.Succeeded();
258 return true;
259 }
260 }
261
262 ClangFunction::ExecutionResults return_status;
263 Value return_value;
264
Sean Callanan65dafa82010-08-27 01:01:44 +0000265 bool stop_others = true;
266 return_status = clang_fun.ExecuteFunction(exe_ctx, errors, stop_others, NULL, return_value);
Chris Lattner24943d22010-06-08 16:52:24 +0000267
268 // Now figure out what to do with the return value.
269 if (return_status == ClangFunction::eExecutionSetupError)
270 {
271 result.AppendErrorWithFormat("Error setting up function execution: '%s'.\n", errors.GetData());
272 result.SetStatus (eReturnStatusFailed);
273 return false;
274 }
275 else if (return_status != ClangFunction::eExecutionCompleted)
276 {
277 result.AppendWarningWithFormat("Interrupted while calling function: '%s'.\n", errors.GetData());
278 result.SetStatus(eReturnStatusSuccessFinishNoResult);
279 return true;
280 }
281 else
282 {
283 // Now print out the result.
284 result.GetOutputStream().Printf("Return value: ");
285 return_value.Dump(&(result.GetOutputStream()));
286 result.Succeeded();
287 }
288
289 }
290 else
291 {
292 result.AppendError ("invalid target triple");
293 result.SetStatus (eReturnStatusFailed);
294 }
295 return result.Succeeded();
296}
297
298lldb::OptionDefinition
299CommandObjectCall::CommandOptions::g_option_table[] =
300{
Greg Clayton12bec712010-06-28 21:30:43 +0000301{ 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 +0000302{ 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."},
303{ LLDB_OPT_SET_1, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
304{ LLDB_OPT_SET_1, false, "noexecute", 'n', no_argument, NULL, 0, "no execute", "Only JIT and copy the wrapper & arguments, but don't execute."},
Greg Claytonfe424a92010-09-18 03:37:20 +0000305{ LLDB_OPT_SET_1, false, "use-abi", 'a', no_argument, NULL, 0, NULL, "Use the ABI instead of the JIT to marshall arguments."},
Chris Lattner24943d22010-06-08 16:52:24 +0000306{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
307};
308