blob: 1e2d0235598b1a3f2bbe5fcd1016709c67cb9b6f [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"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000020#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilson63c468c2010-07-23 21:47:22 +000021#include "lldb/Expression/ClangFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Expression/DWARFExpression.h"
23#include "lldb/Host/Host.h"
Sean Callanan841026f2010-07-23 00:16:21 +000024#include "lldb/Core/Debugger.h"
Greg Clayton63094e02010-06-23 01:19:29 +000025#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Interpreter/CommandReturnObject.h"
27#include "lldb/Symbol/ObjectFile.h"
28#include "lldb/Symbol/Variable.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/StackFrame.h"
31#include "lldb/Target/Target.h"
Sean Callanan841026f2010-07-23 00:16:21 +000032#include "llvm/ADT/StringRef.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
34using namespace lldb;
35using namespace lldb_private;
36
37CommandObjectExpression::CommandOptions::CommandOptions () :
38 Options()
39{
40 // Keep only one place to reset the values to their defaults
41 ResetOptionValues();
42}
43
44
45CommandObjectExpression::CommandOptions::~CommandOptions ()
46{
47}
48
49Error
50CommandObjectExpression::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
51{
52 Error error;
53
54 char short_option = (char) m_getopt_table[option_idx].val;
55
56 switch (short_option)
57 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000058 //case 'l':
59 //if (language.SetLanguageFromCString (option_arg) == false)
60 //{
61 // error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
62 //}
63 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000064
65 case 'g':
66 debug = true;
67 break;
68
69 case 'f':
70 error = Args::StringToFormat(option_arg, format);
71 break;
72
73 default:
74 error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
75 break;
76 }
77
78 return error;
79}
80
81void
82CommandObjectExpression::CommandOptions::ResetOptionValues ()
83{
84 Options::ResetOptionValues();
Caroline Ticec1ad82e2010-09-07 22:38:08 +000085 //language.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000086 debug = false;
87 format = eFormatDefault;
88 show_types = true;
89 show_summary = true;
90}
91
92const lldb::OptionDefinition*
93CommandObjectExpression::CommandOptions::GetDefinitions ()
94{
95 return g_option_table;
96}
97
Greg Clayton238c0a12010-09-18 01:14:36 +000098CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
99 CommandObject (interpreter,
100 "expression",
101 "Evaluate an Objective-C++ expression in the current program context, using variables currently in scope.",
102 "expression [<cmd-options>] <expr>"),
103m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000104 m_expr_lines ()
105{
106 SetHelpLong(
107"Examples: \n\
108\n\
109 expr my_struct->a = my_array[3] \n\
110 expr -f bin -- (index * 8) + 5 \n\
111 expr char c[] = \"foo\"; c[0]\n");
112}
113
114CommandObjectExpression::~CommandObjectExpression ()
115{
116}
117
118Options *
119CommandObjectExpression::GetOptions ()
120{
121 return &m_options;
122}
123
124
125bool
126CommandObjectExpression::Execute
127(
128 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000129 CommandReturnObject &result
130)
131{
132 return false;
133}
134
135
136size_t
137CommandObjectExpression::MultiLineExpressionCallback
138(
139 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000140 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000141 lldb::InputReaderAction notification,
142 const char *bytes,
143 size_t bytes_len
144)
145{
Chris Lattner24943d22010-06-08 16:52:24 +0000146 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
147
148 switch (notification)
149 {
150 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000151 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000152 // Fall through
153 case eInputReaderReactivate:
154 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000155 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000156 break;
157
158 case eInputReaderDeactivate:
159 break;
160
161 case eInputReaderGotToken:
162 ++cmd_object_expr->m_expr_line_count;
163 if (bytes && bytes_len)
164 {
165 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
166 }
167
168 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000169 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000170 //else if (out_fh && !reader->IsDone())
171 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
172 break;
173
174 case eInputReaderDone:
175 {
Chris Lattner24943d22010-06-08 16:52:24 +0000176 bool bare = false;
177 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
178 bare,
Greg Clayton63094e02010-06-23 01:19:29 +0000179 reader.GetDebugger().GetOutputStream(),
180 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000181 }
182 break;
183 }
184
185 return bytes_len;
186}
187
188bool
Johnny Chen0deefc72010-08-13 00:42:30 +0000189CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream,
190 CommandReturnObject *result)
Chris Lattner24943d22010-06-08 16:52:24 +0000191{
Sean Callananf18d91c2010-09-01 00:58:00 +0000192 if (!m_exe_ctx.process)
193 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000194 error_stream.Printf ("Execution context doesn't contain a process\n");
Sean Callananf18d91c2010-09-01 00:58:00 +0000195 return false;
196 }
197
198 if (!m_exe_ctx.process->GetDynamicCheckers())
199 {
200 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
201
202 StreamString install_errors;
203
204 if (!dynamic_checkers->Install(install_errors, m_exe_ctx))
205 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000206 error_stream.Printf("Couldn't install dynamic checkers into the execution context: %s\n", install_errors.GetData());
Sean Callananf18d91c2010-09-01 00:58:00 +0000207 return false;
208 }
209
210 m_exe_ctx.process->SetDynamicCheckers(dynamic_checkers);
211 }
212
Sean Callanan65dafa82010-08-27 01:01:44 +0000213 ClangUserExpression user_expression (expr);
Sean Callanan46b62492010-06-24 00:16:27 +0000214
Sean Callanan65dafa82010-08-27 01:01:44 +0000215 if (!user_expression.Parse (error_stream, m_exe_ctx))
Sean Callanan46b62492010-06-24 00:16:27 +0000216 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000217 error_stream.Printf ("Couldn't parse the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000218 return false;
219 }
220
Sean Callanane8a59a82010-09-13 21:34:21 +0000221 ClangExpressionVariable *expr_result = NULL;
Sean Callanan46b62492010-06-24 00:16:27 +0000222
Sean Callanan65dafa82010-08-27 01:01:44 +0000223 if (!user_expression.Execute (error_stream, m_exe_ctx, expr_result))
Chris Lattner24943d22010-06-08 16:52:24 +0000224 {
Jim Inghamdeb0fd22010-09-01 19:53:33 +0000225 error_stream.Printf ("Couldn't execute the expresssion\n");
Sean Callanan46b62492010-06-24 00:16:27 +0000226 return false;
227 }
Chris Lattner24943d22010-06-08 16:52:24 +0000228
Sean Callanan82b74c82010-08-12 01:56:52 +0000229 if (expr_result)
Sean Callanan841026f2010-07-23 00:16:21 +0000230 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000231 StreamString ss;
Sean Callanan841026f2010-07-23 00:16:21 +0000232
Johnny Chen0deefc72010-08-13 00:42:30 +0000233 Error rc = expr_result->Print (ss,
234 m_exe_ctx,
235 m_options.format,
236 m_options.show_types,
237 m_options.show_summary,
238 m_options.debug);
Sean Callanan82b74c82010-08-12 01:56:52 +0000239
Johnny Chen0deefc72010-08-13 00:42:30 +0000240 if (rc.Fail()) {
241 error_stream.Printf ("Couldn't print result : %s\n", rc.AsCString());
242 return false;
243 }
244
245 output_stream.PutCString(ss.GetString().c_str());
246 if (result)
247 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000248 }
249 else
250 {
Johnny Chen0deefc72010-08-13 00:42:30 +0000251 if (result)
252 result->SetStatus (eReturnStatusSuccessFinishNoResult);
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
Johnny Chen0deefc72010-08-13 00:42:30 +0000337 if (EvaluateExpression (expr, false, result.GetOutputStream(), result.GetErrorStream(), &result))
338 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."},
Sean Callanan848960c2010-06-23 23:18:04 +0000348{ LLDB_OPT_SET_ALL, 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_ALL, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
350{ 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 +0000351{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
352};
353