blob: b29c0c7777d6a313aa581d8393849c09eabcd821 [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 {
58 case 'l':
59 if (language.SetLanguageFromCString (option_arg) == false)
60 {
61 error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
62 }
63 break;
64
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();
85 language.Clear();
86 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
98CommandObjectExpression::CommandObjectExpression () :
99 CommandObject (
100 "expression",
101 "Evaluate a C expression in the current program context, using variables currently in scope.",
102 "expression [<cmd-options>] <expr>"),
103 m_expr_line_count (0),
104 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(
Greg Clayton63094e02010-06-23 01:19:29 +0000128 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000129 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000130 CommandReturnObject &result
131)
132{
133 return false;
134}
135
136
137size_t
138CommandObjectExpression::MultiLineExpressionCallback
139(
140 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000141 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000142 lldb::InputReaderAction notification,
143 const char *bytes,
144 size_t bytes_len
145)
146{
Chris Lattner24943d22010-06-08 16:52:24 +0000147 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
148
149 switch (notification)
150 {
151 case eInputReaderActivate:
Greg Clayton63094e02010-06-23 01:19:29 +0000152 reader.GetDebugger().GetOutputStream().Printf("%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
Chris Lattner24943d22010-06-08 16:52:24 +0000153 // Fall through
154 case eInputReaderReactivate:
155 //if (out_fh)
Greg Clayton63094e02010-06-23 01:19:29 +0000156 // reader.GetDebugger().GetOutputStream().Printf ("%3u: ", cmd_object_expr->m_expr_line_count);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 break;
158
159 case eInputReaderDeactivate:
160 break;
161
162 case eInputReaderGotToken:
163 ++cmd_object_expr->m_expr_line_count;
164 if (bytes && bytes_len)
165 {
166 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
167 }
168
169 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000170 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000171 //else if (out_fh && !reader->IsDone())
172 // ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
173 break;
174
175 case eInputReaderDone:
176 {
Chris Lattner24943d22010-06-08 16:52:24 +0000177 bool bare = false;
178 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
179 bare,
Greg Clayton63094e02010-06-23 01:19:29 +0000180 reader.GetDebugger().GetOutputStream(),
181 reader.GetDebugger().GetErrorStream());
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183 break;
184 }
185
186 return bytes_len;
187}
188
189bool
Johnny Chen0deefc72010-08-13 00:42:30 +0000190CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream,
191 CommandReturnObject *result)
Chris Lattner24943d22010-06-08 16:52:24 +0000192{
Sean Callanan65dafa82010-08-27 01:01:44 +0000193 ClangUserExpression user_expression (expr);
Sean Callanan46b62492010-06-24 00:16:27 +0000194
Sean Callanan65dafa82010-08-27 01:01:44 +0000195 if (!user_expression.Parse (error_stream, m_exe_ctx))
Sean Callanan46b62492010-06-24 00:16:27 +0000196 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000197 error_stream.Printf ("Couldn't parse the expresssion");
Sean Callanan46b62492010-06-24 00:16:27 +0000198 return false;
199 }
200
Sean Callanan65dafa82010-08-27 01:01:44 +0000201 ClangExpressionVariable *expr_result;
Sean Callanan46b62492010-06-24 00:16:27 +0000202
Sean Callanan65dafa82010-08-27 01:01:44 +0000203 if (!user_expression.Execute (error_stream, m_exe_ctx, expr_result))
Chris Lattner24943d22010-06-08 16:52:24 +0000204 {
Sean Callanan65dafa82010-08-27 01:01:44 +0000205 error_stream.Printf ("Couldn't execute the expresssion");
Sean Callanan46b62492010-06-24 00:16:27 +0000206 return false;
207 }
Chris Lattner24943d22010-06-08 16:52:24 +0000208
Sean Callanan82b74c82010-08-12 01:56:52 +0000209 if (expr_result)
Sean Callanan841026f2010-07-23 00:16:21 +0000210 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000211 StreamString ss;
Sean Callanan841026f2010-07-23 00:16:21 +0000212
Johnny Chen0deefc72010-08-13 00:42:30 +0000213 Error rc = expr_result->Print (ss,
214 m_exe_ctx,
215 m_options.format,
216 m_options.show_types,
217 m_options.show_summary,
218 m_options.debug);
Sean Callanan82b74c82010-08-12 01:56:52 +0000219
Johnny Chen0deefc72010-08-13 00:42:30 +0000220 if (rc.Fail()) {
221 error_stream.Printf ("Couldn't print result : %s\n", rc.AsCString());
222 return false;
223 }
224
225 output_stream.PutCString(ss.GetString().c_str());
226 if (result)
227 result->SetStatus (eReturnStatusSuccessFinishResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000228 }
229 else
230 {
Sean Callanan82b74c82010-08-12 01:56:52 +0000231 error_stream.Printf ("Expression produced no result\n");
Johnny Chen0deefc72010-08-13 00:42:30 +0000232 if (result)
233 result->SetStatus (eReturnStatusSuccessFinishNoResult);
Sean Callanan841026f2010-07-23 00:16:21 +0000234 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000235
Sean Callanan841026f2010-07-23 00:16:21 +0000236 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000237}
238
239bool
240CommandObjectExpression::ExecuteRawCommandString
241(
Greg Clayton63094e02010-06-23 01:19:29 +0000242 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000243 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000244 CommandReturnObject &result
245)
246{
Sean Callanan46b62492010-06-24 00:16:27 +0000247 m_exe_ctx = interpreter.GetDebugger().GetExecutionContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000248
249 m_options.ResetOptionValues();
250
251 const char * expr = NULL;
252
253 if (command[0] == '\0')
254 {
255 m_expr_lines.clear();
256 m_expr_line_count = 0;
257
Greg Clayton63094e02010-06-23 01:19:29 +0000258 InputReaderSP reader_sp (new InputReader(interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000259 if (reader_sp)
260 {
261 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
262 this, // baton
263 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000264 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000265 NULL, // prompt
266 true)); // echo input
267 if (err.Success())
268 {
Greg Clayton63094e02010-06-23 01:19:29 +0000269 interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000270 result.SetStatus (eReturnStatusSuccessFinishNoResult);
271 }
272 else
273 {
274 result.AppendError (err.AsCString());
275 result.SetStatus (eReturnStatusFailed);
276 }
277 }
278 else
279 {
280 result.AppendError("out of memory");
281 result.SetStatus (eReturnStatusFailed);
282 }
283 return result.Succeeded();
284 }
285
286 if (command[0] == '-')
287 {
288 // We have some options and these options MUST end with --.
289 const char *end_options = NULL;
290 const char *s = command;
291 while (s && s[0])
292 {
293 end_options = ::strstr (s, "--");
294 if (end_options)
295 {
296 end_options += 2; // Get past the "--"
297 if (::isspace (end_options[0]))
298 {
299 expr = end_options;
300 while (::isspace (*expr))
301 ++expr;
302 break;
303 }
304 }
305 s = end_options;
306 }
307
308 if (end_options)
309 {
Greg Clayton63094e02010-06-23 01:19:29 +0000310 Args args (command, end_options - command);
311 if (!ParseOptions (interpreter, args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000312 return false;
313 }
314 }
315
Chris Lattner24943d22010-06-08 16:52:24 +0000316 if (expr == NULL)
317 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000318
Johnny Chen0deefc72010-08-13 00:42:30 +0000319 if (EvaluateExpression (expr, false, result.GetOutputStream(), result.GetErrorStream(), &result))
320 return true;
321
322 result.SetStatus (eReturnStatusFailed);
323 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000324}
325
326lldb::OptionDefinition
327CommandObjectExpression::CommandOptions::g_option_table[] =
328{
Greg Clayton12bec712010-06-28 21:30:43 +0000329{ 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 +0000330{ 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."},
331{ LLDB_OPT_SET_ALL, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
332{ 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 +0000333{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
334};
335