blob: c58f3f529c84dd519f2c5de2e542a3670becc75e [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
Jim Ingham360f53f2010-11-30 02:22:11 +000016#include "CommandObjectThread.h" // For DisplayThreadInfo.
Sean Callanan841026f2010-07-23 00:16:21 +000017#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Value.h"
19#include "lldb/Core/InputReader.h"
Jim Ingham324067b2010-09-30 00:54:27 +000020#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000022#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilson63c468c2010-07-23 21:47:22 +000023#include "lldb/Expression/ClangFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Expression/DWARFExpression.h"
25#include "lldb/Host/Host.h"
Sean Callanan841026f2010-07-23 00:16:21 +000026#include "lldb/Core/Debugger.h"
Greg Clayton63094e02010-06-23 01:19:29 +000027#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham324067b2010-09-30 00:54:27 +000029#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Symbol/Variable.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/StackFrame.h"
34#include "lldb/Target/Target.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
40CommandObjectExpression::CommandOptions::CommandOptions () :
41 Options()
42{
43 // Keep only one place to reset the values to their defaults
44 ResetOptionValues();
45}
46
47
48CommandObjectExpression::CommandOptions::~CommandOptions ()
49{
50}
51
52Error
53CommandObjectExpression::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
54{
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':
73 error = Args::StringToFormat(option_arg, format);
74 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
80 case 'u':
81 bool success;
82 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
83 if (!success)
84 error.SetErrorStringWithFormat("Could not convert \"%s\" to a boolean value.", option_arg);
85 break;
Chris Lattner24943d22010-06-08 16:52:24 +000086
87 default:
88 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
89 break;
90 }
91
92 return error;
93}
94
95void
96CommandObjectExpression::CommandOptions::ResetOptionValues ()
97{
98 Options::ResetOptionValues();
Caroline Ticec1ad82e2010-09-07 22:38:08 +000099 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000100 debug = false;
101 format = eFormatDefault;
Jim Ingham324067b2010-09-30 00:54:27 +0000102 print_object = false;
Jim Inghamea9d4262010-11-05 19:25:48 +0000103 unwind_on_error = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000104 show_types = true;
105 show_summary = true;
106}
107
108const lldb::OptionDefinition*
109CommandObjectExpression::CommandOptions::GetDefinitions ()
110{
111 return g_option_table;
112}
113
Greg Clayton238c0a12010-09-18 01:14:36 +0000114CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
115 CommandObject (interpreter,
116 "expression",
Johnny Chen106a7372010-09-30 18:16:58 +0000117 "Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000118 NULL),
Johnny Chencc112ac2010-09-30 18:30:25 +0000119 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000120 m_expr_lines ()
121{
122 SetHelpLong(
123"Examples: \n\
124\n\
125 expr my_struct->a = my_array[3] \n\
126 expr -f bin -- (index * 8) + 5 \n\
127 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-10-04 22:28:36 +0000128
129 CommandArgumentEntry arg;
130 CommandArgumentData expression_arg;
131
132 // Define the first (and only) variant of this arg.
133 expression_arg.arg_type = eArgTypeExpression;
134 expression_arg.arg_repetition = eArgRepeatPlain;
135
136 // There is only one variant this argument could be; put it into the argument entry.
137 arg.push_back (expression_arg);
138
139 // Push the data for the first argument into the m_arguments vector.
140 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000141}
142
143CommandObjectExpression::~CommandObjectExpression ()
144{
145}
146
147Options *
148CommandObjectExpression::GetOptions ()
149{
150 return &m_options;
151}
152
153
154bool
155CommandObjectExpression::Execute
156(
157 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000158 CommandReturnObject &result
159)
160{
161 return false;
162}
163
164
165size_t
166CommandObjectExpression::MultiLineExpressionCallback
167(
168 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000169 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000170 lldb::InputReaderAction notification,
171 const char *bytes,
172 size_t bytes_len
173)
174{
Chris Lattner24943d22010-06-08 16:52:24 +0000175 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
176
177 switch (notification)
178 {
179 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000180 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000181 // Fall through
182 case eInputReaderReactivate:
183 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000184 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000185 break;
186
187 case eInputReaderDeactivate:
188 break;
189
190 case eInputReaderGotToken:
191 ++cmd_object_expr->m_expr_line_count;
192 if (bytes && bytes_len)
193 {
194 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
195 }
196
197 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000198 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000199 //else if (out_fh && !reader->IsDone())
200 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
201 break;
202
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000203 case eInputReaderInterrupt:
204 cmd_object_expr->m_expr_lines.clear();
205 reader.SetIsDone (true);
206 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Expression evaluation cancelled.");
207 break;
208
209 case eInputReaderEndOfFile:
210 reader.SetIsDone (true);
211 break;
212
Chris Lattner24943d22010-06-08 16:52:24 +0000213 case eInputReaderDone:
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000214 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000215 {
Chris Lattner24943d22010-06-08 16:52:24 +0000216 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Greg Clayton63094e02010-06-23 01:19:29 +0000217 reader.GetDebugger().GetOutputStream(),
218 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000219 }
220 break;
221 }
222
223 return bytes_len;
224}
225
226bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000227CommandObjectExpression::EvaluateExpression
228(
229 const char *expr,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000230 Stream &output_stream,
231 Stream &error_stream,
232 CommandReturnObject *result
233)
Chris Lattner24943d22010-06-08 16:52:24 +0000234{
Sean Callananf18d91c2010-09-01 00:58:00 +0000235 if (!m_exe_ctx.process)
236 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000237 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000238 return false;
239 }
240
Sean Callanan77e93942010-10-29 00:29:03 +0000241 const char *prefix = NULL;
242
243 if (m_exe_ctx.target)
244 prefix = m_exe_ctx.target->GetExpressionPrefixContentsAsCString();
245
Jim Ingham360f53f2010-11-30 02:22:11 +0000246 lldb::ValueObjectSP result_valobj_sp;
247 Process::ExecutionResults execution_results = ClangUserExpression::Evaluate (m_exe_ctx, m_options.unwind_on_error, expr, prefix, result_valobj_sp);
Greg Claytond1719722010-10-05 03:13:51 +0000248 assert (result_valobj_sp.get());
249 if (result_valobj_sp->GetError().Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000250 {
Greg Clayton11730f32010-10-06 03:09:11 +0000251 if (m_options.format != eFormatDefault)
252 result_valobj_sp->SetFormat (m_options.format);
253
Greg Clayton377e0b42010-10-05 00:31:29 +0000254 ValueObject::DumpValueObject (output_stream,
255 m_exe_ctx.GetBestExecutionContextScope(),
256 result_valobj_sp.get(), // Variable object to dump
257 result_valobj_sp->GetName().AsCString(),// Root object name
258 0, // Pointer depth to traverse (zero means stop at pointers)
259 0, // Current depth, this is the top most, so zero...
260 UINT32_MAX, // Max depth to go when dumping concrete types, dump everything...
261 m_options.show_types, // Show types when dumping?
262 false, // Show locations of variables, no since this is a host address which we don't care to see
263 m_options.print_object, // Print the objective C object?
Greg Claytonbf8e42b2010-10-14 22:52:14 +0000264 true, // Scope is already checked. Const results are always in scope.
265 false); // Don't flatten output
Johnny Chen0deefc72010-08-13 00:42:30 +0000266 if (result)
267 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000268 }
269 else
270 {
Greg Claytond1719722010-10-05 03:13:51 +0000271 error_stream.PutCString(result_valobj_sp->GetError().AsCString());
Jim Ingham360f53f2010-11-30 02:22:11 +0000272 // If we've been interrupted, display state information.
273 if (execution_results == Process::eExecutionInterrupted && !m_options.unwind_on_error)
274 {
275 if (m_exe_ctx.thread)
276 lldb_private::DisplayThreadInfo (m_interpreter, result->GetOutputStream(), m_exe_ctx.thread, false, true);
277 else
278 {
279 lldb_private::DisplayThreadsInfo (m_interpreter, &m_exe_ctx, *result, true, true);
280 }
281 }
Johnny Chen0deefc72010-08-13 00:42:30 +0000282 if (result)
Greg Claytond1719722010-10-05 03:13:51 +0000283 result->SetStatus (eReturnStatusFailed);
Sean Callanan841026f2010-07-23 00:16:21 +0000284 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000285
Sean Callanan841026f2010-07-23 00:16:21 +0000286 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000287}
288
289bool
290CommandObjectExpression::ExecuteRawCommandString
291(
292 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000293 CommandReturnObject &result
294)
295{
Greg Clayton238c0a12010-09-18 01:14:36 +0000296 m_exe_ctx = m_interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000297
298 m_options.ResetOptionValues();
299
300 const char * expr = NULL;
301
302 if (command[0] == '\0')
303 {
304 m_expr_lines.clear();
305 m_expr_line_count = 0;
306
Greg Clayton238c0a12010-09-18 01:14:36 +0000307 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (reader_sp)
309 {
310 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
311 this, // baton
312 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000313 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000314 NULL, // prompt
315 true)); // echo input
316 if (err.Success())
317 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000318 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000319 result.SetStatus (eReturnStatusSuccessFinishNoResult);
320 }
321 else
322 {
323 result.AppendError (err.AsCString());
324 result.SetStatus (eReturnStatusFailed);
325 }
326 }
327 else
328 {
329 result.AppendError("out of memory");
330 result.SetStatus (eReturnStatusFailed);
331 }
332 return result.Succeeded();
333 }
334
335 if (command[0] == '-')
336 {
337 // We have some options and these options MUST end with --.
338 const char *end_options = NULL;
339 const char *s = command;
340 while (s && s[0])
341 {
342 end_options = ::strstr (s, "--");
343 if (end_options)
344 {
345 end_options += 2; // Get past the "--"
346 if (::isspace (end_options[0]))
347 {
348 expr = end_options;
349 while (::isspace (*expr))
350 ++expr;
351 break;
352 }
353 }
354 s = end_options;
355 }
356
357 if (end_options)
358 {
Greg Clayton63094e02010-06-23 01:19:29 +0000359 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000360 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000361 return false;
362 }
363 }
364
Chris Lattner24943d22010-06-08 16:52:24 +0000365 if (expr == NULL)
366 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000367
Greg Clayton377e0b42010-10-05 00:31:29 +0000368 if (EvaluateExpression (expr, result.GetOutputStream(), result.GetErrorStream(), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000369 return true;
370
371 result.SetStatus (eReturnStatusFailed);
372 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000373}
374
375lldb::OptionDefinition
376CommandObjectExpression::CommandOptions::g_option_table[] =
377{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000378 //{ 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 +0000379//{ 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."},
380{ 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 +0000381{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
382{ 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 +0000383{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
384{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, eArgTypeNone, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
385{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000386};
387