blob: 51ce177d9946052f21f81ba9dc2c0771984e5ad3 [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.",
Greg Clayton238c0a12010-09-18 01:14:36 +0000109 "expression [<cmd-options>] <expr>"),
110m_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");
119}
120
121CommandObjectExpression::~CommandObjectExpression ()
122{
123}
124
125Options *
126CommandObjectExpression::GetOptions ()
127{
128 return &m_options;
129}
130
131
132bool
133CommandObjectExpression::Execute
134(
135 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000136 CommandReturnObject &result
137)
138{
139 return false;
140}
141
142
143size_t
144CommandObjectExpression::MultiLineExpressionCallback
145(
146 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000147 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000148 lldb::InputReaderAction notification,
149 const char *bytes,
150 size_t bytes_len
151)
152{
Chris Lattner24943d22010-06-08 16:52:24 +0000153 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
154
155 switch (notification)
156 {
157 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000158 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000159 // Fall through
160 case eInputReaderReactivate:
161 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000162 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000163 break;
164
165 case eInputReaderDeactivate:
166 break;
167
168 case eInputReaderGotToken:
169 ++cmd_object_expr->m_expr_line_count;
170 if (bytes && bytes_len)
171 {
172 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
173 }
174
175 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000176 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000177 //else if (out_fh && !reader->IsDone())
178 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
179 break;
180
181 case eInputReaderDone:
182 {
Chris Lattner24943d22010-06-08 16:52:24 +0000183 bool bare = false;
184 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
185 bare,
Greg Clayton63094e02010-06-23 01:19:29 +0000186 reader.GetDebugger().GetOutputStream(),
187 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000188 }
189 break;
190 }
191
192 return bytes_len;
193}
194
195bool
Johnny Chen0deefc72010-08-13 00:42:30 +0000196CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream,
197 CommandReturnObject *result)
Chris Lattner24943d22010-06-08 16:52:24 +0000198{
Sean Callananf18d91c2010-09-01 00:58:00 +0000199 if (!m_exe_ctx.process)
200 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000201 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000202 return false;
203 }
204
205 if (!m_exe_ctx.process->GetDynamicCheckers())
206 {
207 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
208
209 StreamString install_errors;
210
211 if (!dynamic_checkers->Install(install_errors, m_exe_ctx))
212 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000213 error_stream.Printf("Couldn't install dynamic checkers into the execution context: %s\n", install_errors.GetData());
Sean Callananf18d91c2010-09-01 00:58:00 +0000214 return false;
215 }
216
217 m_exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
218 }
219
Sean Callanan65dafa82010-08-27 01:01:44 +0000220 ClangUserExpression user_expression (expr);
Sean Callanan46b62492010-06-24 00:16:27 +0000221
Sean Callanan65dafa82010-08-27 01:01:44 +0000222 if (!user_expression.Parse (error_stream, m_exe_ctx))
Sean Callanan46b62492010-06-24 00:16:27 +0000223 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000224 error_stream.Printf ("Couldn't parse the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000225 return false;
226 }
227
Sean Callanane8a59a82010-09-13 21:34:21 +0000228 ClangExpressionVariable *expr_result = NULL;
Sean Callanan46b62492010-06-24 00:16:27 +0000229
Sean Callanan65dafa82010-08-27 01:01:44 +0000230 if (!user_expression.Execute (error_stream, m_exe_ctx, expr_result))
Chris Lattner24943d22010-06-08 16:52:24 +0000231 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000232 error_stream.Printf ("Couldn't execute the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000233 return false;
234 }
Chris Lattner24943d22010-06-08 16:52:24 +0000235
Sean Callanan82b74c82010-08-12 01:56:52 +0000236 if (expr_result)
Sean Callanan841026f2010-07-23 00:16:21 +0000237 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000238 StreamString ss;
Sean Callanan841026f2010-07-23 00:16:21 +0000239
Jim Ingham324067b2010-09-30 00:54:27 +0000240 if (m_options.print_object)
241 {
242 Value result_value;
243 if (expr_result->PointValueAtData(result_value, &m_exe_ctx))
244 {
245 bool obj_result;
246 ObjCLanguageRuntime *runtime = m_exe_ctx.process->GetObjCLanguageRuntime();
247 obj_result = runtime->GetObjectDescription (ss, result_value, m_exe_ctx.GetBestExecutionContextScope());
248 if (!obj_result)
249 {
250 error_stream.Printf ("Could not get object description: %s.\n", ss.GetData());
251 return false;
252 }
253 // Sometimes the description doesn't have a newline on the end. For now, I'll just add one here, if
254 ss.Printf("\n");
255 }
256 }
257 else
258 {
259 Error rc = expr_result->Print (ss,
260 m_exe_ctx,
261 m_options.format,
262 m_options.show_types,
263 m_options.show_summary,
264 m_options.debug);
265
266 if (rc.Fail()) {
267 error_stream.Printf ("Couldn't print result : %s\n", rc.AsCString());
268 return false;
269 }
Johnny Chen0deefc72010-08-13 00:42:30 +0000270 }
271
272 output_stream.PutCString(ss.GetString().c_str());
273 if (result)
274 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000275 }
276 else
277 {
Johnny Chen0deefc72010-08-13 00:42:30 +0000278 if (result)
279 result->SetStatus (eReturnStatusSuccessFinishNoResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000280 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000281
Sean Callanan841026f2010-07-23 00:16:21 +0000282 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000283}
284
285bool
286CommandObjectExpression::ExecuteRawCommandString
287(
288 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000289 CommandReturnObject &result
290)
291{
Greg Clayton238c0a12010-09-18 01:14:36 +0000292 m_exe_ctx = m_interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000293
294 m_options.ResetOptionValues();
295
296 const char * expr = NULL;
297
298 if (command[0] == '\0')
299 {
300 m_expr_lines.clear();
301 m_expr_line_count = 0;
302
Greg Clayton238c0a12010-09-18 01:14:36 +0000303 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000304 if (reader_sp)
305 {
306 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
307 this, // baton
308 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000309 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000310 NULL, // prompt
311 true)); // echo input
312 if (err.Success())
313 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000314 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000315 result.SetStatus (eReturnStatusSuccessFinishNoResult);
316 }
317 else
318 {
319 result.AppendError (err.AsCString());
320 result.SetStatus (eReturnStatusFailed);
321 }
322 }
323 else
324 {
325 result.AppendError("out of memory");
326 result.SetStatus (eReturnStatusFailed);
327 }
328 return result.Succeeded();
329 }
330
331 if (command[0] == '-')
332 {
333 // We have some options and these options MUST end with --.
334 const char *end_options = NULL;
335 const char *s = command;
336 while (s && s[0])
337 {
338 end_options = ::strstr (s, "--");
339 if (end_options)
340 {
341 end_options += 2; // Get past the "--"
342 if (::isspace (end_options[0]))
343 {
344 expr = end_options;
345 while (::isspace (*expr))
346 ++expr;
347 break;
348 }
349 }
350 s = end_options;
351 }
352
353 if (end_options)
354 {
Greg Clayton63094e02010-06-23 01:19:29 +0000355 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000356 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000357 return false;
358 }
359 }
360
Chris Lattner24943d22010-06-08 16:52:24 +0000361 if (expr == NULL)
362 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000363
Johnny Chen0deefc72010-08-13 00:42:30 +0000364 if (EvaluateExpression (expr, false, result.GetOutputStream(), result.GetErrorStream(), &result))
365 return true;
366
367 result.SetStatus (eReturnStatusFailed);
368 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000369}
370
371lldb::OptionDefinition
372CommandObjectExpression::CommandOptions::g_option_table[] =
373{
Caroline Ticec1ad82e2010-09-07 22:38:08 +0000374 //{ LLDB_OPT_SET_ALL, false, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
Jim Ingham324067b2010-09-30 00:54:27 +0000375{ 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."},
376{ LLDB_OPT_SET_2, false, "object-description", 'o', no_argument, NULL, 0, NULL, "Print the object description of the value resulting from the expression"},
Sean Callanan848960c2010-06-23 23:18:04 +0000377{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
378{ LLDB_OPT_SET_ALL, false, "use-ir", 'i', no_argument, NULL, 0, NULL, "[Temporary] Instructs the expression evaluator to use IR instead of ASTs."},
Chris Lattner24943d22010-06-08 16:52:24 +0000379{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
380};
381