blob: 67745ee7097b632376a565a858c4aacfb2ae676b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectExpression.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 "CommandObjectExpression.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Sean Callanan841026f2010-07-23 00:16:21 +000016#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Value.h"
18#include "lldb/Core/InputReader.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000020#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilson63c468c2010-07-23 21:47:22 +000021#include "lldb/Expression/ClangFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Expression/DWARFExpression.h"
23#include "lldb/Host/Host.h"
Sean Callanan841026f2010-07-23 00:16:21 +000024#include "lldb/Core/Debugger.h"
Greg Clayton63094e02010-06-23 01:19:29 +000025#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Interpreter/CommandReturnObject.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Symbol/Variable.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/StackFrame.h"
31#include "lldb/Target/Target.h"
Sean Callanan841026f2010-07-23 00:16:21 +000032#include "llvm/ADT/StringRef.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
34using namespace lldb;
35using namespace lldb_private;
36
37CommandObjectExpression::CommandOptions::CommandOptions () :
38 Options()
39{
40 // Keep only one place to reset the values to their defaults
41 ResetOptionValues();
42}
43
44
45CommandObjectExpression::CommandOptions::~CommandOptions ()
46{
47}
48
49Error
50CommandObjectExpression::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
51{
52 Error error;
53
54 char short_option = (char) m_getopt_table[option_idx].val;
55
56 switch (short_option)
57 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000058 //case 'l':
59 //if (language.SetLanguageFromCString (option_arg) == false)
60 //{
61 // error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
62 //}
63 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000064
65 case 'g':
66 debug = true;
67 break;
68
69 case 'f':
70 error = Args::StringToFormat(option_arg, format);
71 break;
72
73 default:
74 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
75 break;
76 }
77
78 return error;
79}
80
81void
82CommandObjectExpression::CommandOptions::ResetOptionValues ()
83{
84 Options::ResetOptionValues();
Caroline Ticec1ad82e2010-09-07 22:38:08 +000085 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000086 debug = false;
87 format = eFormatDefault;
88 show_types = true;
89 show_summary = true;
90}
91
92const lldb::OptionDefinition*
93CommandObjectExpression::CommandOptions::GetDefinitions ()
94{
95 return g_option_table;
96}
97
98CommandObjectExpression::CommandObjectExpression () :
99 CommandObject (
100 "expression",
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000101 "Evaluate an Objective-C++ expression in the current program context, using variables currently in scope.",
Chris Lattner24943d22010-06-08 16:52:24 +0000102 "expression [<cmd-options>] <expr>"),
103 m_expr_line_count (0),
104 m_expr_lines ()
105{
106 SetHelpLong(
107"Examples: \n\
108\n\
109 expr my_struct->a = my_array[3] \n\
110 expr -f bin -- (index * 8) + 5 \n\
111 expr char c[] = \"foo\"; c[0]\n");
112}
113
114CommandObjectExpression::~CommandObjectExpression ()
115{
116}
117
118Options *
119CommandObjectExpression::GetOptions ()
120{
121 return &m_options;
122}
123
124
125bool
126CommandObjectExpression::Execute
127(
Greg Clayton63094e02010-06-23 01:19:29 +0000128 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000129 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000130 CommandReturnObject &result
131)
132{
133 return false;
134}
135
136
137size_t
138CommandObjectExpression::MultiLineExpressionCallback
139(
140 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000141 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000142 lldb::InputReaderAction notification,
143 const char *bytes,
144 size_t bytes_len
145)
146{
Chris Lattner24943d22010-06-08 16:52:24 +0000147 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
148
149 switch (notification)
150 {
151 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000152 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000153 // Fall through
154 case eInputReaderReactivate:
155 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000156 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 break;
158
159 case eInputReaderDeactivate:
160 break;
161
162 case eInputReaderGotToken:
163 ++cmd_object_expr->m_expr_line_count;
164 if (bytes && bytes_len)
165 {
166 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
167 }
168
169 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000170 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000171 //else if (out_fh && !reader->IsDone())
172 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
173 break;
174
175 case eInputReaderDone:
176 {
Chris Lattner24943d22010-06-08 16:52:24 +0000177 bool bare = false;
178 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
179 bare,
Greg Clayton63094e02010-06-23 01:19:29 +0000180 reader.GetDebugger().GetOutputStream(),
181 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183 break;
184 }
185
186 return bytes_len;
187}
188
189bool
Johnny Chen0deefc72010-08-13 00:42:30 +0000190CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream,
191 CommandReturnObject *result)
Chris Lattner24943d22010-06-08 16:52:24 +0000192{
Sean Callananf18d91c2010-09-01 00:58:00 +0000193 if (!m_exe_ctx.process)
194 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000195 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000196 return false;
197 }
198
199 if (!m_exe_ctx.process->GetDynamicCheckers())
200 {
201 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
202
203 StreamString install_errors;
204
205 if (!dynamic_checkers->Install(install_errors, m_exe_ctx))
206 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000207 error_stream.Printf("Couldn't install dynamic checkers into the execution context: %s\n", install_errors.GetData());
Sean Callananf18d91c2010-09-01 00:58:00 +0000208 return false;
209 }
210
211 m_exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
212 }
213
Sean Callanan65dafa82010-08-27 01:01:44 +0000214 ClangUserExpression user_expression (expr);
Sean Callanan46b62492010-06-24 00:16:27 +0000215
Sean Callanan65dafa82010-08-27 01:01:44 +0000216 if (!user_expression.Parse (error_stream, m_exe_ctx))
Sean Callanan46b62492010-06-24 00:16:27 +0000217 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000218 error_stream.Printf ("Couldn't parse the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000219 return false;
220 }
221
Sean Callanan65dafa82010-08-27 01:01:44 +0000222 ClangExpressionVariable *expr_result;
Sean Callanan46b62492010-06-24 00:16:27 +0000223
Sean Callanan65dafa82010-08-27 01:01:44 +0000224 if (!user_expression.Execute (error_stream, m_exe_ctx, expr_result))
Chris Lattner24943d22010-06-08 16:52:24 +0000225 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000226 error_stream.Printf ("Couldn't execute the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000227 return false;
228 }
Chris Lattner24943d22010-06-08 16:52:24 +0000229
Sean Callanan82b74c82010-08-12 01:56:52 +0000230 if (expr_result)
Sean Callanan841026f2010-07-23 00:16:21 +0000231 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000232 StreamString ss;
Sean Callanan841026f2010-07-23 00:16:21 +0000233
Johnny Chen0deefc72010-08-13 00:42:30 +0000234 Error rc = expr_result->Print (ss,
235 m_exe_ctx,
236 m_options.format,
237 m_options.show_types,
238 m_options.show_summary,
239 m_options.debug);
Sean Callanan82b74c82010-08-12 01:56:52 +0000240
Johnny Chen0deefc72010-08-13 00:42:30 +0000241 if (rc.Fail()) {
242 error_stream.Printf ("Couldn't print result : %s\n", rc.AsCString());
243 return false;
244 }
245
246 output_stream.PutCString(ss.GetString().c_str());
247 if (result)
248 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000249 }
250 else
251 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000252 error_stream.Printf ("Expression produced no result\n");
Johnny Chen0deefc72010-08-13 00:42:30 +0000253 if (result)
254 result->SetStatus (eReturnStatusSuccessFinishNoResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000255 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000256
Sean Callanan841026f2010-07-23 00:16:21 +0000257 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000258}
259
260bool
261CommandObjectExpression::ExecuteRawCommandString
262(
Greg Clayton63094e02010-06-23 01:19:29 +0000263 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000264 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000265 CommandReturnObject &result
266)
267{
Sean Callanan46b62492010-06-24 00:16:27 +0000268 m_exe_ctx = interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000269
270 m_options.ResetOptionValues();
271
272 const char * expr = NULL;
273
274 if (command[0] == '\0')
275 {
276 m_expr_lines.clear();
277 m_expr_line_count = 0;
278
Greg Clayton63094e02010-06-23 01:19:29 +0000279 InputReaderSP reader_sp (new InputReader(interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000280 if (reader_sp)
281 {
282 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
283 this, // baton
284 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000285 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000286 NULL, // prompt
287 true)); // echo input
288 if (err.Success())
289 {
Greg Clayton63094e02010-06-23 01:19:29 +0000290 interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000291 result.SetStatus (eReturnStatusSuccessFinishNoResult);
292 }
293 else
294 {
295 result.AppendError (err.AsCString());
296 result.SetStatus (eReturnStatusFailed);
297 }
298 }
299 else
300 {
301 result.AppendError("out of memory");
302 result.SetStatus (eReturnStatusFailed);
303 }
304 return result.Succeeded();
305 }
306
307 if (command[0] == '-')
308 {
309 // We have some options and these options MUST end with --.
310 const char *end_options = NULL;
311 const char *s = command;
312 while (s && s[0])
313 {
314 end_options = ::strstr (s, "--");
315 if (end_options)
316 {
317 end_options += 2; // Get past the "--"
318 if (::isspace (end_options[0]))
319 {
320 expr = end_options;
321 while (::isspace (*expr))
322 ++expr;
323 break;
324 }
325 }
326 s = end_options;
327 }
328
329 if (end_options)
330 {
Greg Clayton63094e02010-06-23 01:19:29 +0000331 Args args (command, end_options - command);
332 if (!ParseOptions (interpreter, args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000333 return false;
334 }
335 }
336
Chris Lattner24943d22010-06-08 16:52:24 +0000337 if (expr == NULL)
338 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000339
Johnny Chen0deefc72010-08-13 00:42:30 +0000340 if (EvaluateExpression (expr, false, result.GetOutputStream(), result.GetErrorStream(), &result))
341 return true;
342
343 result.SetStatus (eReturnStatusFailed);
344 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000345}
346
347lldb::OptionDefinition
348CommandObjectExpression::CommandOptions::g_option_table[] =
349{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000350 //{ LLDB_OPT_SET_ALL, false, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
Sean Callanan848960c2010-06-23 23:18:04 +0000351{ LLDB_OPT_SET_ALL, 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."},
352{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
353{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, NULL, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
Chris Lattner24943d22010-06-08 16:52:24 +0000354{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
355};
356