blob: 32236f8c91777f96b5d9dead6c63e9952458823a [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
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000202 case eInputReaderInterrupt:
203 cmd_object_expr->m_expr_lines.clear();
204 reader.SetIsDone (true);
205 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Expression evaluation cancelled.");
206 break;
207
208 case eInputReaderEndOfFile:
209 reader.SetIsDone (true);
210 break;
211
Chris Lattner24943d22010-06-08 16:52:24 +0000212 case eInputReaderDone:
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000213 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000214 {
Chris Lattner24943d22010-06-08 16:52:24 +0000215 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Greg Clayton63094e02010-06-23 01:19:29 +0000216 reader.GetDebugger().GetOutputStream(),
217 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000218 }
219 break;
220 }
221
222 return bytes_len;
223}
224
225bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000226CommandObjectExpression::EvaluateExpression
227(
228 const char *expr,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000229 Stream &output_stream,
230 Stream &error_stream,
231 CommandReturnObject *result
232)
Chris Lattner24943d22010-06-08 16:52:24 +0000233{
Sean Callananf18d91c2010-09-01 00:58:00 +0000234 if (!m_exe_ctx.process)
235 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000236 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000237 return false;
238 }
239
Sean Callanan77e93942010-10-29 00:29:03 +0000240 const char *prefix = NULL;
241
242 if (m_exe_ctx.target)
243 prefix = m_exe_ctx.target->GetExpressionPrefixContentsAsCString();
244
Jim Inghamea9d4262010-11-05 19:25:48 +0000245 lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (m_exe_ctx, m_options.unwind_on_error, expr, prefix));
Greg Claytond1719722010-10-05 03:13:51 +0000246 assert (result_valobj_sp.get());
247 if (result_valobj_sp->GetError().Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000248 {
Greg Clayton11730f32010-10-06 03:09:11 +0000249 if (m_options.format != eFormatDefault)
250 result_valobj_sp->SetFormat (m_options.format);
251
Greg Clayton377e0b42010-10-05 00:31:29 +0000252 ValueObject::DumpValueObject (output_stream,
253 m_exe_ctx.GetBestExecutionContextScope(),
254 result_valobj_sp.get(), // Variable object to dump
255 result_valobj_sp->GetName().AsCString(),// Root object name
256 0, // Pointer depth to traverse (zero means stop at pointers)
257 0, // Current depth, this is the top most, so zero...
258 UINT32_MAX, // Max depth to go when dumping concrete types, dump everything...
259 m_options.show_types, // Show types when dumping?
260 false, // Show locations of variables, no since this is a host address which we don't care to see
261 m_options.print_object, // Print the objective C object?
Greg Claytonbf8e42b2010-10-14 22:52:14 +0000262 true, // Scope is already checked. Const results are always in scope.
263 false); // Don't flatten output
Johnny Chen0deefc72010-08-13 00:42:30 +0000264 if (result)
265 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000266 }
267 else
268 {
Greg Claytond1719722010-10-05 03:13:51 +0000269 error_stream.PutCString(result_valobj_sp->GetError().AsCString());
Johnny Chen0deefc72010-08-13 00:42:30 +0000270 if (result)
Greg Claytond1719722010-10-05 03:13:51 +0000271 result->SetStatus (eReturnStatusFailed);
Sean Callanan841026f2010-07-23 00:16:21 +0000272 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000273
Sean Callanan841026f2010-07-23 00:16:21 +0000274 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000275}
276
277bool
278CommandObjectExpression::ExecuteRawCommandString
279(
280 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000281 CommandReturnObject &result
282)
283{
Greg Clayton238c0a12010-09-18 01:14:36 +0000284 m_exe_ctx = m_interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000285
286 m_options.ResetOptionValues();
287
288 const char * expr = NULL;
289
290 if (command[0] == '\0')
291 {
292 m_expr_lines.clear();
293 m_expr_line_count = 0;
294
Greg Clayton238c0a12010-09-18 01:14:36 +0000295 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000296 if (reader_sp)
297 {
298 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
299 this, // baton
300 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000301 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000302 NULL, // prompt
303 true)); // echo input
304 if (err.Success())
305 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000306 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000307 result.SetStatus (eReturnStatusSuccessFinishNoResult);
308 }
309 else
310 {
311 result.AppendError (err.AsCString());
312 result.SetStatus (eReturnStatusFailed);
313 }
314 }
315 else
316 {
317 result.AppendError("out of memory");
318 result.SetStatus (eReturnStatusFailed);
319 }
320 return result.Succeeded();
321 }
322
323 if (command[0] == '-')
324 {
325 // We have some options and these options MUST end with --.
326 const char *end_options = NULL;
327 const char *s = command;
328 while (s && s[0])
329 {
330 end_options = ::strstr (s, "--");
331 if (end_options)
332 {
333 end_options += 2; // Get past the "--"
334 if (::isspace (end_options[0]))
335 {
336 expr = end_options;
337 while (::isspace (*expr))
338 ++expr;
339 break;
340 }
341 }
342 s = end_options;
343 }
344
345 if (end_options)
346 {
Greg Clayton63094e02010-06-23 01:19:29 +0000347 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000348 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000349 return false;
350 }
351 }
352
Chris Lattner24943d22010-06-08 16:52:24 +0000353 if (expr == NULL)
354 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000355
Greg Clayton377e0b42010-10-05 00:31:29 +0000356 if (EvaluateExpression (expr, result.GetOutputStream(), result.GetErrorStream(), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000357 return true;
358
359 result.SetStatus (eReturnStatusFailed);
360 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000361}
362
363lldb::OptionDefinition
364CommandObjectExpression::CommandOptions::g_option_table[] =
365{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000366 //{ 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 +0000367//{ 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."},
368{ 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 +0000369{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
370{ 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 +0000371{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
372{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, eArgTypeNone, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
373{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000374};
375