blob: 2045c59d058c4da693bd97dbeeefbe7ed1e8aec6 [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;
Chris Lattner24943d22010-06-08 16:52:24 +000078
79 default:
80 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
81 break;
82 }
83
84 return error;
85}
86
87void
88CommandObjectExpression::CommandOptions::ResetOptionValues ()
89{
90 Options::ResetOptionValues();
Caroline Ticec1ad82e2010-09-07 22:38:08 +000091 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000092 debug = false;
93 format = eFormatDefault;
Jim Ingham324067b2010-09-30 00:54:27 +000094 print_object = false;
Chris Lattner24943d22010-06-08 16:52:24 +000095 show_types = true;
96 show_summary = true;
97}
98
99const lldb::OptionDefinition*
100CommandObjectExpression::CommandOptions::GetDefinitions ()
101{
102 return g_option_table;
103}
104
Greg Clayton238c0a12010-09-18 01:14:36 +0000105CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
106 CommandObject (interpreter,
107 "expression",
Johnny Chen106a7372010-09-30 18:16:58 +0000108 "Evaluate a C/ObjC/C++ expression in the current program context, using variables currently in scope.",
Caroline Tice43b014a2010-10-04 22:28:36 +0000109 NULL),
Johnny Chencc112ac2010-09-30 18:30:25 +0000110 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000111 m_expr_lines ()
112{
113 SetHelpLong(
114"Examples: \n\
115\n\
116 expr my_struct->a = my_array[3] \n\
117 expr -f bin -- (index * 8) + 5 \n\
118 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-10-04 22:28:36 +0000119
120 CommandArgumentEntry arg;
121 CommandArgumentData expression_arg;
122
123 // Define the first (and only) variant of this arg.
124 expression_arg.arg_type = eArgTypeExpression;
125 expression_arg.arg_repetition = eArgRepeatPlain;
126
127 // There is only one variant this argument could be; put it into the argument entry.
128 arg.push_back (expression_arg);
129
130 // Push the data for the first argument into the m_arguments vector.
131 m_arguments.push_back (arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000132}
133
134CommandObjectExpression::~CommandObjectExpression ()
135{
136}
137
138Options *
139CommandObjectExpression::GetOptions ()
140{
141 return &m_options;
142}
143
144
145bool
146CommandObjectExpression::Execute
147(
148 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000149 CommandReturnObject &result
150)
151{
152 return false;
153}
154
155
156size_t
157CommandObjectExpression::MultiLineExpressionCallback
158(
159 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000160 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000161 lldb::InputReaderAction notification,
162 const char *bytes,
163 size_t bytes_len
164)
165{
Chris Lattner24943d22010-06-08 16:52:24 +0000166 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
167
168 switch (notification)
169 {
170 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000171 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000172 // Fall through
173 case eInputReaderReactivate:
174 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000175 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000176 break;
177
178 case eInputReaderDeactivate:
179 break;
180
181 case eInputReaderGotToken:
182 ++cmd_object_expr->m_expr_line_count;
183 if (bytes && bytes_len)
184 {
185 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
186 }
187
188 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000189 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000190 //else if (out_fh && !reader->IsDone())
191 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
192 break;
193
194 case eInputReaderDone:
195 {
Chris Lattner24943d22010-06-08 16:52:24 +0000196 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Greg Clayton63094e02010-06-23 01:19:29 +0000197 reader.GetDebugger().GetOutputStream(),
198 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000199 }
200 break;
201 }
202
203 return bytes_len;
204}
205
206bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000207CommandObjectExpression::EvaluateExpression
208(
209 const char *expr,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000210 Stream &output_stream,
211 Stream &error_stream,
212 CommandReturnObject *result
213)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Sean Callananf18d91c2010-09-01 00:58:00 +0000215 if (!m_exe_ctx.process)
216 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000217 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000218 return false;
219 }
220
Sean Callanan77e93942010-10-29 00:29:03 +0000221 const char *prefix = NULL;
222
223 if (m_exe_ctx.target)
224 prefix = m_exe_ctx.target->GetExpressionPrefixContentsAsCString();
225
226 lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (m_exe_ctx, expr, prefix));
Greg Claytond1719722010-10-05 03:13:51 +0000227 assert (result_valobj_sp.get());
228 if (result_valobj_sp->GetError().Success())
Chris Lattner24943d22010-06-08 16:52:24 +0000229 {
Greg Clayton11730f32010-10-06 03:09:11 +0000230 if (m_options.format != eFormatDefault)
231 result_valobj_sp->SetFormat (m_options.format);
232
Greg Clayton377e0b42010-10-05 00:31:29 +0000233 ValueObject::DumpValueObject (output_stream,
234 m_exe_ctx.GetBestExecutionContextScope(),
235 result_valobj_sp.get(), // Variable object to dump
236 result_valobj_sp->GetName().AsCString(),// Root object name
237 0, // Pointer depth to traverse (zero means stop at pointers)
238 0, // Current depth, this is the top most, so zero...
239 UINT32_MAX, // Max depth to go when dumping concrete types, dump everything...
240 m_options.show_types, // Show types when dumping?
241 false, // Show locations of variables, no since this is a host address which we don't care to see
242 m_options.print_object, // Print the objective C object?
Greg Claytonbf8e42b2010-10-14 22:52:14 +0000243 true, // Scope is already checked. Const results are always in scope.
244 false); // Don't flatten output
Johnny Chen0deefc72010-08-13 00:42:30 +0000245 if (result)
246 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000247 }
248 else
249 {
Greg Claytond1719722010-10-05 03:13:51 +0000250 error_stream.PutCString(result_valobj_sp->GetError().AsCString());
Johnny Chen0deefc72010-08-13 00:42:30 +0000251 if (result)
Greg Claytond1719722010-10-05 03:13:51 +0000252 result->SetStatus (eReturnStatusFailed);
Sean Callanan841026f2010-07-23 00:16:21 +0000253 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000254
Sean Callanan841026f2010-07-23 00:16:21 +0000255 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000256}
257
258bool
259CommandObjectExpression::ExecuteRawCommandString
260(
261 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000262 CommandReturnObject &result
263)
264{
Greg Clayton238c0a12010-09-18 01:14:36 +0000265 m_exe_ctx = m_interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000266
267 m_options.ResetOptionValues();
268
269 const char * expr = NULL;
270
271 if (command[0] == '\0')
272 {
273 m_expr_lines.clear();
274 m_expr_line_count = 0;
275
Greg Clayton238c0a12010-09-18 01:14:36 +0000276 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000277 if (reader_sp)
278 {
279 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
280 this, // baton
281 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000282 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000283 NULL, // prompt
284 true)); // echo input
285 if (err.Success())
286 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000287 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000288 result.SetStatus (eReturnStatusSuccessFinishNoResult);
289 }
290 else
291 {
292 result.AppendError (err.AsCString());
293 result.SetStatus (eReturnStatusFailed);
294 }
295 }
296 else
297 {
298 result.AppendError("out of memory");
299 result.SetStatus (eReturnStatusFailed);
300 }
301 return result.Succeeded();
302 }
303
304 if (command[0] == '-')
305 {
306 // We have some options and these options MUST end with --.
307 const char *end_options = NULL;
308 const char *s = command;
309 while (s && s[0])
310 {
311 end_options = ::strstr (s, "--");
312 if (end_options)
313 {
314 end_options += 2; // Get past the "--"
315 if (::isspace (end_options[0]))
316 {
317 expr = end_options;
318 while (::isspace (*expr))
319 ++expr;
320 break;
321 }
322 }
323 s = end_options;
324 }
325
326 if (end_options)
327 {
Greg Clayton63094e02010-06-23 01:19:29 +0000328 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000329 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000330 return false;
331 }
332 }
333
Chris Lattner24943d22010-06-08 16:52:24 +0000334 if (expr == NULL)
335 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000336
Greg Clayton377e0b42010-10-05 00:31:29 +0000337 if (EvaluateExpression (expr, result.GetOutputStream(), result.GetErrorStream(), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000338 return true;
339
340 result.SetStatus (eReturnStatusFailed);
341 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000342}
343
344lldb::OptionDefinition
345CommandObjectExpression::CommandOptions::g_option_table[] =
346{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000347 //{ 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 +0000348//{ 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."},
349{ LLDB_OPT_SET_1, false, "format", 'f', required_argument, NULL, 0, eArgTypeExprFormat, "Specify the format that the expression output should use."},
350{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression"},
351{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, eArgTypeNone, "Enable verbose debug logging of the expression parsing and evaluation."},
352{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, eArgTypeNone, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
353{ 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000354};
355