blob: 436b2e7540c6cba43c3a13ddf1a58dc374d49c83 [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"
Sean Callanan841026f2010-07-23 00:16:21 +000034#include "llvm/ADT/StringRef.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
36using namespace lldb;
37using namespace lldb_private;
38
39CommandObjectExpression::CommandOptions::CommandOptions () :
40 Options()
41{
42 // Keep only one place to reset the values to their defaults
43 ResetOptionValues();
44}
45
46
47CommandObjectExpression::CommandOptions::~CommandOptions ()
48{
49}
50
51Error
52CommandObjectExpression::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
53{
54 Error error;
55
56 char short_option = (char) m_getopt_table[option_idx].val;
57
58 switch (short_option)
59 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000060 //case 'l':
61 //if (language.SetLanguageFromCString (option_arg) == false)
62 //{
63 // error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
64 //}
65 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000066
67 case 'g':
68 debug = true;
69 break;
70
71 case 'f':
72 error = Args::StringToFormat(option_arg, format);
73 break;
Jim Ingham324067b2010-09-30 00:54:27 +000074
75 case 'o':
76 print_object = true;
77 break;
Jim Inghamea9d4262010-11-05 19:25:48 +000078
79 case 'u':
80 bool success;
81 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
82 if (!success)
83 error.SetErrorStringWithFormat("Could not convert \"%s\" to a boolean value.", option_arg);
84 break;
Chris Lattner24943d22010-06-08 16:52:24 +000085
86 default:
87 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
88 break;
89 }
90
91 return error;
92}
93
94void
95CommandObjectExpression::CommandOptions::ResetOptionValues ()
96{
97 Options::ResetOptionValues();
Caroline Ticec1ad82e2010-09-07 22:38:08 +000098 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000099 debug = false;
100 format = eFormatDefault;
Jim Ingham324067b2010-09-30 00:54:27 +0000101 print_object = false;
Jim Inghamea9d4262010-11-05 19:25:48 +0000102 unwind_on_error = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000103 show_types = true;
104 show_summary = true;
105}
106
107const lldb::OptionDefinition*
108CommandObjectExpression::CommandOptions::GetDefinitions ()
109{
110 return g_option_table;
111}
112
Greg Clayton238c0a12010-09-18 01:14:36 +0000113CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
114 CommandObject (interpreter,
115 "expression",
Johnny Chen106a7372010-09-30 18:16:58 +0000116 "Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000117 NULL),
Johnny Chencc112ac2010-09-30 18:30:25 +0000118 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000119 m_expr_lines ()
120{
121 SetHelpLong(
122"Examples: \n\
123\n\
124 expr my_struct->a = my_array[3] \n\
125 expr -f bin -- (index * 8) + 5 \n\
126 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-10-04 22:28:36 +0000127
128 CommandArgumentEntry arg;
129 CommandArgumentData expression_arg;
130
131 // Define the first (and only) variant of this arg.
132 expression_arg.arg_type = eArgTypeExpression;
133 expression_arg.arg_repetition = eArgRepeatPlain;
134
135 // There is only one variant this argument could be; put it into the argument entry.
136 arg.push_back (expression_arg);
137
138 // Push the data for the first argument into the m_arguments vector.
139 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000140}
141
142CommandObjectExpression::~CommandObjectExpression ()
143{
144}
145
146Options *
147CommandObjectExpression::GetOptions ()
148{
149 return &m_options;
150}
151
152
153bool
154CommandObjectExpression::Execute
155(
156 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000157 CommandReturnObject &result
158)
159{
160 return false;
161}
162
163
164size_t
165CommandObjectExpression::MultiLineExpressionCallback
166(
167 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000168 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000169 lldb::InputReaderAction notification,
170 const char *bytes,
171 size_t bytes_len
172)
173{
Chris Lattner24943d22010-06-08 16:52:24 +0000174 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
175
176 switch (notification)
177 {
178 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000179 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000180 // Fall through
181 case eInputReaderReactivate:
182 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000183 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000184 break;
185
186 case eInputReaderDeactivate:
187 break;
188
189 case eInputReaderGotToken:
190 ++cmd_object_expr->m_expr_line_count;
191 if (bytes && bytes_len)
192 {
193 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
194 }
195
196 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000197 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000198 //else if (out_fh && !reader->IsDone())
199 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
200 break;
201
202 case eInputReaderDone:
203 {
Chris Lattner24943d22010-06-08 16:52:24 +0000204 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Greg Clayton63094e02010-06-23 01:19:29 +0000205 reader.GetDebugger().GetOutputStream(),
206 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000207 }
208 break;
209 }
210
211 return bytes_len;
212}
213
214bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000215CommandObjectExpression::EvaluateExpression
216(
217 const char *expr,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000218 Stream &output_stream,
219 Stream &error_stream,
220 CommandReturnObject *result
221)
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
Sean Callananf18d91c2010-09-01 00:58:00 +0000223 if (!m_exe_ctx.process)
224 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000225 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000226 return false;
227 }
228
Sean Callanan77e93942010-10-29 00:29:03 +0000229 const char *prefix = NULL;
230
231 if (m_exe_ctx.target)
232 prefix = m_exe_ctx.target->GetExpressionPrefixContentsAsCString();
233
Jim Inghamea9d4262010-11-05 19:25:48 +0000234 lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (m_exe_ctx, m_options.unwind_on_error, expr, prefix));
Greg Claytond1719722010-10-05 03:13:51 +0000235 assert (result_valobj_sp.get());
236 if (result_valobj_sp->GetError().Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000237 {
Greg Clayton11730f32010-10-06 03:09:11 +0000238 if (m_options.format != eFormatDefault)
239 result_valobj_sp->SetFormat (m_options.format);
240
Greg Clayton377e0b42010-10-05 00:31:29 +0000241 ValueObject::DumpValueObject (output_stream,
242 m_exe_ctx.GetBestExecutionContextScope(),
243 result_valobj_sp.get(), // Variable object to dump
244 result_valobj_sp->GetName().AsCString(),// Root object name
245 0, // Pointer depth to traverse (zero means stop at pointers)
246 0, // Current depth, this is the top most, so zero...
247 UINT32_MAX, // Max depth to go when dumping concrete types, dump everything...
248 m_options.show_types, // Show types when dumping?
249 false, // Show locations of variables, no since this is a host address which we don't care to see
250 m_options.print_object, // Print the objective C object?
Greg Claytonbf8e42b2010-10-14 22:52:14 +0000251 true, // Scope is already checked. Const results are always in scope.
252 false); // Don't flatten output
Johnny Chen0deefc72010-08-13 00:42:30 +0000253 if (result)
254 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000255 }
256 else
257 {
Greg Claytond1719722010-10-05 03:13:51 +0000258 error_stream.PutCString(result_valobj_sp->GetError().AsCString());
Johnny Chen0deefc72010-08-13 00:42:30 +0000259 if (result)
Greg Claytond1719722010-10-05 03:13:51 +0000260 result->SetStatus (eReturnStatusFailed);
Sean Callanan841026f2010-07-23 00:16:21 +0000261 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000262
Sean Callanan841026f2010-07-23 00:16:21 +0000263 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000264}
265
266bool
267CommandObjectExpression::ExecuteRawCommandString
268(
269 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000270 CommandReturnObject &result
271)
272{
Greg Clayton238c0a12010-09-18 01:14:36 +0000273 m_exe_ctx = m_interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000274
275 m_options.ResetOptionValues();
276
277 const char * expr = NULL;
278
279 if (command[0] == '\0')
280 {
281 m_expr_lines.clear();
282 m_expr_line_count = 0;
283
Greg Clayton238c0a12010-09-18 01:14:36 +0000284 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000285 if (reader_sp)
286 {
287 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
288 this, // baton
289 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000290 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000291 NULL, // prompt
292 true)); // echo input
293 if (err.Success())
294 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000295 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000296 result.SetStatus (eReturnStatusSuccessFinishNoResult);
297 }
298 else
299 {
300 result.AppendError (err.AsCString());
301 result.SetStatus (eReturnStatusFailed);
302 }
303 }
304 else
305 {
306 result.AppendError("out of memory");
307 result.SetStatus (eReturnStatusFailed);
308 }
309 return result.Succeeded();
310 }
311
312 if (command[0] == '-')
313 {
314 // We have some options and these options MUST end with --.
315 const char *end_options = NULL;
316 const char *s = command;
317 while (s && s[0])
318 {
319 end_options = ::strstr (s, "--");
320 if (end_options)
321 {
322 end_options += 2; // Get past the "--"
323 if (::isspace (end_options[0]))
324 {
325 expr = end_options;
326 while (::isspace (*expr))
327 ++expr;
328 break;
329 }
330 }
331 s = end_options;
332 }
333
334 if (end_options)
335 {
Greg Clayton63094e02010-06-23 01:19:29 +0000336 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000337 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000338 return false;
339 }
340 }
341
Chris Lattner24943d22010-06-08 16:52:24 +0000342 if (expr == NULL)
343 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000344
Greg Clayton377e0b42010-10-05 00:31:29 +0000345 if (EvaluateExpression (expr, result.GetOutputStream(), result.GetErrorStream(), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000346 return true;
347
348 result.SetStatus (eReturnStatusFailed);
349 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000350}
351
352lldb::OptionDefinition
353CommandObjectExpression::CommandOptions::g_option_table[] =
354{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000355 //{ 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 +0000356//{ 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."},
357{ 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 +0000358{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
359{ 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 +0000360{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
361{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, eArgTypeNone, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
362{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000363};
364