blob: fa578f499879434cd6fb2cd24f4883ca88080b87 [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"
Jim Ingham324067b2010-09-30 00:54:27 +000019#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000021#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilson63c468c2010-07-23 21:47:22 +000022#include "lldb/Expression/ClangFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Expression/DWARFExpression.h"
24#include "lldb/Host/Host.h"
Sean Callanan841026f2010-07-23 00:16:21 +000025#include "lldb/Core/Debugger.h"
Greg Clayton63094e02010-06-23 01:19:29 +000026#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham324067b2010-09-30 00:54:27 +000028#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Symbol/ObjectFile.h"
30#include "lldb/Symbol/Variable.h"
31#include "lldb/Target/Process.h"
32#include "lldb/Target/StackFrame.h"
33#include "lldb/Target/Target.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000034#include "lldb/Target/Thread.h"
Sean Callanan841026f2010-07-23 00:16:21 +000035#include "llvm/ADT/StringRef.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Greg Claytonf15996e2011-04-07 22:46:35 +000040CommandObjectExpression::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen93356432011-04-08 22:39:17 +000041 Options(interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43 // Keep only one place to reset the values to their defaults
Greg Clayton143fcc32011-04-13 00:18:08 +000044 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47
48CommandObjectExpression::CommandOptions::~CommandOptions ()
49{
50}
51
52Error
Greg Clayton143fcc32011-04-13 00:18:08 +000053CommandObjectExpression::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000054{
55 Error error;
56
57 char short_option = (char) m_getopt_table[option_idx].val;
58
59 switch (short_option)
60 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000061 //case 'l':
62 //if (language.SetLanguageFromCString (option_arg) == false)
63 //{
64 // error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
65 //}
66 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000067
68 case 'g':
69 debug = true;
70 break;
71
72 case 'f':
Greg Clayton56bbdaf2011-04-28 20:55:26 +000073 error = Args::StringToFormat(option_arg, format, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000074 break;
Jim Ingham324067b2010-09-30 00:54:27 +000075
76 case 'o':
77 print_object = true;
78 break;
Jim Inghamea9d4262010-11-05 19:25:48 +000079
Jim Inghame41494a2011-04-16 00:01:13 +000080 case 'd':
81 {
82 bool success;
83 bool result;
84 result = Args::StringToBoolean(option_arg, true, &success);
85 if (!success)
86 error.SetErrorStringWithFormat("Invalid dynamic value setting: \"%s\".\n", option_arg);
87 else
88 {
89 if (result)
90 use_dynamic = eLazyBoolYes;
91 else
92 use_dynamic = eLazyBoolNo;
93 }
94 }
95 break;
96
Jim Inghamea9d4262010-11-05 19:25:48 +000097 case 'u':
98 bool success;
99 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
100 if (!success)
101 error.SetErrorStringWithFormat("Could not convert \"%s\" to a boolean value.", option_arg);
102 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000103
104 default:
105 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
106 break;
107 }
108
109 return error;
110}
111
112void
Greg Clayton143fcc32011-04-13 00:18:08 +0000113CommandObjectExpression::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000114{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000115 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000116 debug = false;
117 format = eFormatDefault;
Jim Ingham324067b2010-09-30 00:54:27 +0000118 print_object = false;
Jim Inghame41494a2011-04-16 00:01:13 +0000119 use_dynamic = eLazyBoolCalculate;
Jim Inghamea9d4262010-11-05 19:25:48 +0000120 unwind_on_error = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000121 show_types = true;
122 show_summary = true;
123}
124
Greg Claytonb3448432011-03-24 21:19:54 +0000125const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000126CommandObjectExpression::CommandOptions::GetDefinitions ()
127{
128 return g_option_table;
129}
130
Greg Clayton238c0a12010-09-18 01:14:36 +0000131CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
132 CommandObject (interpreter,
133 "expression",
Johnny Chen106a7372010-09-30 18:16:58 +0000134 "Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000135 NULL),
Greg Claytonf15996e2011-04-07 22:46:35 +0000136 m_options (interpreter),
Johnny Chencc112ac2010-09-30 18:30:25 +0000137 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000138 m_expr_lines ()
139{
140 SetHelpLong(
141"Examples: \n\
142\n\
143 expr my_struct->a = my_array[3] \n\
144 expr -f bin -- (index * 8) + 5 \n\
145 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-10-04 22:28:36 +0000146
147 CommandArgumentEntry arg;
148 CommandArgumentData expression_arg;
149
150 // Define the first (and only) variant of this arg.
151 expression_arg.arg_type = eArgTypeExpression;
152 expression_arg.arg_repetition = eArgRepeatPlain;
153
154 // There is only one variant this argument could be; put it into the argument entry.
155 arg.push_back (expression_arg);
156
157 // Push the data for the first argument into the m_arguments vector.
158 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000159}
160
161CommandObjectExpression::~CommandObjectExpression ()
162{
163}
164
165Options *
166CommandObjectExpression::GetOptions ()
167{
168 return &m_options;
169}
170
171
172bool
173CommandObjectExpression::Execute
174(
175 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000176 CommandReturnObject &result
177)
178{
179 return false;
180}
181
182
183size_t
184CommandObjectExpression::MultiLineExpressionCallback
185(
186 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000187 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000188 lldb::InputReaderAction notification,
189 const char *bytes,
190 size_t bytes_len
191)
192{
Chris Lattner24943d22010-06-08 16:52:24 +0000193 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
194
195 switch (notification)
196 {
197 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000198 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000199 // Fall through
200 case eInputReaderReactivate:
201 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000202 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000203 break;
204
205 case eInputReaderDeactivate:
206 break;
207
Caroline Tice4a348082011-05-02 20:41:46 +0000208 case eInputReaderAsynchronousOutputWritten:
209 break;
210
Chris Lattner24943d22010-06-08 16:52:24 +0000211 case eInputReaderGotToken:
212 ++cmd_object_expr->m_expr_line_count;
213 if (bytes && bytes_len)
214 {
215 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
216 }
217
218 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000219 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000220 //else if (out_fh && !reader->IsDone())
221 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
222 break;
223
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000224 case eInputReaderInterrupt:
225 cmd_object_expr->m_expr_lines.clear();
226 reader.SetIsDone (true);
227 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Expression evaluation cancelled.");
228 break;
229
230 case eInputReaderEndOfFile:
231 reader.SetIsDone (true);
232 break;
233
Chris Lattner24943d22010-06-08 16:52:24 +0000234 case eInputReaderDone:
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000235 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000236 {
Chris Lattner24943d22010-06-08 16:52:24 +0000237 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Greg Clayton63094e02010-06-23 01:19:29 +0000238 reader.GetDebugger().GetOutputStream(),
239 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000240 }
241 break;
242 }
243
244 return bytes_len;
245}
246
247bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000248CommandObjectExpression::EvaluateExpression
249(
250 const char *expr,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000251 Stream &output_stream,
252 Stream &error_stream,
253 CommandReturnObject *result
254)
Chris Lattner24943d22010-06-08 16:52:24 +0000255{
Sean Callanan77e93942010-10-29 00:29:03 +0000256 if (m_exe_ctx.target)
Chris Lattner24943d22010-06-08 16:52:24 +0000257 {
Greg Clayton427f2902010-12-14 02:59:59 +0000258 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton11730f32010-10-06 03:09:11 +0000259
Greg Claytonb3448432011-03-24 21:19:54 +0000260 ExecutionResults exe_results;
Sean Callanan6a925532011-01-13 08:53:35 +0000261
262 bool keep_in_memory = true;
Jim Inghame41494a2011-04-16 00:01:13 +0000263 bool use_dynamic;
264 // If use dynamic is not set, get it from the target:
265 switch (m_options.use_dynamic)
266 {
267 case eLazyBoolCalculate:
268 {
269 if (m_exe_ctx.target->GetPreferDynamicValue())
270 use_dynamic = true;
271 else
272 use_dynamic = false;
273 }
274 break;
275 case eLazyBoolYes:
276 use_dynamic = true;
277 break;
278 case eLazyBoolNo:
279 use_dynamic = false;
280 break;
281 }
Sean Callanan6a925532011-01-13 08:53:35 +0000282
Jim Inghame41494a2011-04-16 00:01:13 +0000283 exe_results = m_exe_ctx.target->EvaluateExpression(expr, m_exe_ctx.frame, m_options.unwind_on_error, use_dynamic, keep_in_memory, result_valobj_sp);
Greg Clayton427f2902010-12-14 02:59:59 +0000284
285 if (exe_results == eExecutionInterrupted && !m_options.unwind_on_error)
Jim Ingham360f53f2010-11-30 02:22:11 +0000286 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000287 uint32_t start_frame = 0;
288 uint32_t num_frames = 1;
289 uint32_t num_frames_with_source = 0;
Jim Ingham360f53f2010-11-30 02:22:11 +0000290 if (m_exe_ctx.thread)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000291 {
292 m_exe_ctx.thread->GetStatus (result->GetOutputStream(),
293 start_frame,
294 num_frames,
295 num_frames_with_source);
296 }
297 else if (m_exe_ctx.process)
298 {
299 bool only_threads_with_stop_reason = true;
300 m_exe_ctx.process->GetThreadStatus (result->GetOutputStream(),
301 only_threads_with_stop_reason,
302 start_frame,
303 num_frames,
304 num_frames_with_source);
305 }
Greg Clayton427f2902010-12-14 02:59:59 +0000306 }
307
308 if (result_valobj_sp)
309 {
310 if (result_valobj_sp->GetError().Success())
311 {
312 if (m_options.format != eFormatDefault)
313 result_valobj_sp->SetFormat (m_options.format);
314
315 ValueObject::DumpValueObject (output_stream,
Greg Clayton427f2902010-12-14 02:59:59 +0000316 result_valobj_sp.get(), // Variable object to dump
317 result_valobj_sp->GetName().GetCString(),// Root object name
318 0, // Pointer depth to traverse (zero means stop at pointers)
319 0, // Current depth, this is the top most, so zero...
320 UINT32_MAX, // Max depth to go when dumping concrete types, dump everything...
321 m_options.show_types, // Show types when dumping?
322 false, // Show locations of variables, no since this is a host address which we don't care to see
323 m_options.print_object, // Print the objective C object?
Jim Inghame41494a2011-04-16 00:01:13 +0000324 use_dynamic,
Greg Clayton427f2902010-12-14 02:59:59 +0000325 true, // Scope is already checked. Const results are always in scope.
326 false); // Don't flatten output
327 if (result)
328 result->SetStatus (eReturnStatusSuccessFinishResult);
329 }
330 else
331 {
332 error_stream.PutCString(result_valobj_sp->GetError().AsCString());
333 if (result)
334 result->SetStatus (eReturnStatusFailed);
Jim Ingham360f53f2010-11-30 02:22:11 +0000335 }
336 }
Greg Clayton427f2902010-12-14 02:59:59 +0000337 }
338 else
339 {
340 error_stream.Printf ("error: invalid execution context for expression\n");
341 return false;
Sean Callanan841026f2010-07-23 00:16:21 +0000342 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000343
Sean Callanan841026f2010-07-23 00:16:21 +0000344 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000345}
346
347bool
348CommandObjectExpression::ExecuteRawCommandString
349(
350 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000351 CommandReturnObject &result
352)
353{
Greg Claytonb72d0f02011-04-12 05:54:46 +0000354 m_exe_ctx = m_interpreter.GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000355
Greg Clayton143fcc32011-04-13 00:18:08 +0000356 m_options.NotifyOptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +0000357
358 const char * expr = NULL;
359
360 if (command[0] == '\0')
361 {
362 m_expr_lines.clear();
363 m_expr_line_count = 0;
364
Greg Clayton238c0a12010-09-18 01:14:36 +0000365 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000366 if (reader_sp)
367 {
368 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
369 this, // baton
370 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000371 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000372 NULL, // prompt
373 true)); // echo input
374 if (err.Success())
375 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000376 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000377 result.SetStatus (eReturnStatusSuccessFinishNoResult);
378 }
379 else
380 {
381 result.AppendError (err.AsCString());
382 result.SetStatus (eReturnStatusFailed);
383 }
384 }
385 else
386 {
387 result.AppendError("out of memory");
388 result.SetStatus (eReturnStatusFailed);
389 }
390 return result.Succeeded();
391 }
392
393 if (command[0] == '-')
394 {
395 // We have some options and these options MUST end with --.
396 const char *end_options = NULL;
397 const char *s = command;
398 while (s && s[0])
399 {
400 end_options = ::strstr (s, "--");
401 if (end_options)
402 {
403 end_options += 2; // Get past the "--"
404 if (::isspace (end_options[0]))
405 {
406 expr = end_options;
407 while (::isspace (*expr))
408 ++expr;
409 break;
410 }
411 }
412 s = end_options;
413 }
414
415 if (end_options)
416 {
Greg Clayton63094e02010-06-23 01:19:29 +0000417 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000418 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000419 return false;
Greg Clayton143fcc32011-04-13 00:18:08 +0000420
421 Error error (m_options.NotifyOptionParsingFinished());
422 if (error.Fail())
423 {
424 result.AppendError (error.AsCString());
425 result.SetStatus (eReturnStatusFailed);
426 return false;
427 }
Chris Lattner24943d22010-06-08 16:52:24 +0000428 }
429 }
430
Chris Lattner24943d22010-06-08 16:52:24 +0000431 if (expr == NULL)
432 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000433
Greg Clayton377e0b42010-10-05 00:31:29 +0000434 if (EvaluateExpression (expr, result.GetOutputStream(), result.GetErrorStream(), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000435 return true;
436
437 result.SetStatus (eReturnStatusFailed);
438 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000439}
440
Greg Claytonb3448432011-03-24 21:19:54 +0000441OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000442CommandObjectExpression::CommandOptions::g_option_table[] =
443{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000444 //{ LLDB_OPT_SET_ALL, false, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000445//{ 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."},
446{ LLDB_OPT_SET_1, false, "format", 'f', required_argument, NULL, 0, eArgTypeExprFormat, "Specify the format that the expression output should use."},
Jim Inghamea9d4262010-11-05 19:25:48 +0000447{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
Jim Inghame41494a2011-04-16 00:01:13 +0000448{ LLDB_OPT_SET_2, false, "dynamic-value", 'd', required_argument, NULL, 0, eArgTypeBoolean, "Upcast the value resulting from the expression to its dynamic type if available."},
Jim Inghamea9d4262010-11-05 19:25:48 +0000449{ LLDB_OPT_SET_ALL, false, "unwind-on-error", 'u', required_argument, NULL, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, breakpoint hit or signal."},
Caroline Tice4d6675c2010-10-01 19:59:14 +0000450{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
451{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, eArgTypeNone, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
452{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000453};
454