1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
//===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CommandObjectExpression.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/InputReader.h"
#include "lldb/Expression/ClangExpression.h"
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"
#include "lldb/Expression/DWARFExpression.h"
#include "lldb/Host/Host.h"
#include "lldb/Interpreter/CommandContext.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "llvm/ADT/StringRef.h"
using namespace lldb;
using namespace lldb_private;
CommandObjectExpression::CommandOptions::CommandOptions () :
Options()
{
// Keep only one place to reset the values to their defaults
ResetOptionValues();
}
CommandObjectExpression::CommandOptions::~CommandOptions ()
{
}
Error
CommandObjectExpression::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
{
Error error;
char short_option = (char) m_getopt_table[option_idx].val;
switch (short_option)
{
case 'l':
if (language.SetLanguageFromCString (option_arg) == false)
{
error.SetErrorStringWithFormat("Invalid language option argument '%s'.\n", option_arg);
}
break;
case 'g':
debug = true;
break;
case 'f':
error = Args::StringToFormat(option_arg, format);
break;
default:
error.SetErrorStringWithFormat("Invalid short option character '%c'.\n", short_option);
break;
}
return error;
}
void
CommandObjectExpression::CommandOptions::ResetOptionValues ()
{
Options::ResetOptionValues();
language.Clear();
debug = false;
format = eFormatDefault;
show_types = true;
show_summary = true;
}
const lldb::OptionDefinition*
CommandObjectExpression::CommandOptions::GetDefinitions ()
{
return g_option_table;
}
CommandObjectExpression::CommandObjectExpression () :
CommandObject (
"expression",
"Evaluate a C expression in the current program context, using variables currently in scope.",
"expression [<cmd-options>] <expr>"),
m_expr_line_count (0),
m_expr_lines ()
{
SetHelpLong(
"Examples: \n\
\n\
expr my_struct->a = my_array[3] \n\
expr -f bin -- (index * 8) + 5 \n\
expr char c[] = \"foo\"; c[0]\n");
}
CommandObjectExpression::~CommandObjectExpression ()
{
}
Options *
CommandObjectExpression::GetOptions ()
{
return &m_options;
}
bool
CommandObjectExpression::Execute
(
Args& command,
CommandContext *context,
CommandInterpreter *interpreter,
CommandReturnObject &result
)
{
return false;
}
size_t
CommandObjectExpression::MultiLineExpressionCallback
(
void *baton,
InputReader *reader,
lldb::InputReaderAction notification,
const char *bytes,
size_t bytes_len
)
{
FILE *out_fh = Debugger::GetSharedInstance().GetOutputFileHandle();
CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
switch (notification)
{
case eInputReaderActivate:
if (out_fh)
::fprintf (out_fh, "%s\n", "Enter expressions, then terminate with an empty line to evaluate:");
// Fall through
case eInputReaderReactivate:
//if (out_fh)
// ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
break;
case eInputReaderDeactivate:
break;
case eInputReaderGotToken:
++cmd_object_expr->m_expr_line_count;
if (bytes && bytes_len)
{
cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
}
if (bytes_len == 0)
reader->SetIsDone(true);
//else if (out_fh && !reader->IsDone())
// ::fprintf (out_fh, "%3u: ", cmd_object_expr->m_expr_line_count);
break;
case eInputReaderDone:
{
StreamFile out_stream(Debugger::GetSharedInstance().GetOutputFileHandle());
StreamFile err_stream(Debugger::GetSharedInstance().GetErrorFileHandle());
bool bare = false;
cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
bare,
out_stream,
err_stream);
}
break;
}
return bytes_len;
}
bool
CommandObjectExpression::EvaluateExpression (const char *expr, bool bare, Stream &output_stream, Stream &error_stream)
{
bool success = false;
ConstString target_triple;
Target *target = m_exe_ctx.target;
if (target)
target->GetTargetTriple(target_triple);
if (!target_triple)
target_triple = Host::GetTargetTriple ();
if (target_triple)
{
const bool show_types = m_options.show_types;
const bool show_summary = m_options.show_summary;
const bool debug = m_options.debug;
ClangExpressionDeclMap expr_decl_map(&m_exe_ctx);
ClangExpression clang_expr(target_triple.AsCString(), &expr_decl_map);
unsigned num_errors = 0;
if (bare)
num_errors = clang_expr.ParseBareExpression (llvm::StringRef(expr), error_stream);
else
num_errors = clang_expr.ParseExpression (expr, error_stream);
if (num_errors == 0)
{
StreamString dwarf_opcodes;
dwarf_opcodes.SetByteOrder(eByteOrderHost);
dwarf_opcodes.GetFlags().Set(Stream::eBinary);
ClangExpressionVariableList expr_local_vars;
clang_expr.ConvertExpressionToDWARF (expr_local_vars, dwarf_opcodes);
success = true;
DataExtractor dwarf_opcodes_data(dwarf_opcodes.GetData(), dwarf_opcodes.GetSize(), eByteOrderHost, 8);
DWARFExpression expr(dwarf_opcodes_data, 0, dwarf_opcodes_data.GetByteSize(), NULL);
expr.SetExpressionLocalVariableList(&expr_local_vars);
if (debug)
{
output_stream << "Expression parsed ok, dwarf opcodes:";
output_stream.IndentMore();
expr.GetDescription(&output_stream, lldb::eDescriptionLevelVerbose);
output_stream.IndentLess();
output_stream.EOL();
}
clang::ASTContext *ast_context = clang_expr.GetASTContext();
Value expr_result;
Error expr_error;
bool expr_success = expr.Evaluate (&m_exe_ctx, ast_context, NULL, expr_result, &expr_error);
if (expr_success)
{
lldb::Format format = m_options.format;
// Resolve any values that are possible
expr_result.ResolveValue(&m_exe_ctx, ast_context);
if (expr_result.GetContextType() == Value::eContextTypeInvalid &&
expr_result.GetValueType() == Value::eValueTypeScalar &&
format == eFormatDefault)
{
// The expression result is just a scalar with no special formatting
expr_result.GetScalar().GetValue (&output_stream, show_types);
output_stream.EOL();
}
else
{
DataExtractor data;
expr_error = expr_result.GetValueAsData (&m_exe_ctx, ast_context, data, 0);
if (expr_error.Success())
{
if (format == eFormatDefault)
format = expr_result.GetValueDefaultFormat ();
void *clang_type = expr_result.GetValueOpaqueClangQualType();
if (clang_type)
{
if (show_types)
Type::DumpClangTypeName(&output_stream, clang_type);
Type::DumpValue (
&m_exe_ctx, // The execution context for memory and variable access
ast_context, // The ASTContext that the clang type belongs to
clang_type, // The opaque clang type we want to dump that value of
&output_stream, // Stream to dump to
format, // Format to use when dumping
data, // A buffer containing the bytes for the clang type
0, // Byte offset within "data" where value is
data.GetByteSize(), // Size in bytes of the value we are dumping
0, // Bitfield bit size
0, // Bitfield bit offset
show_types, // Show types?
show_summary, // Show summary?
debug, // Debug logging output?
UINT32_MAX); // Depth to dump in case this is an aggregate type
}
else
{
data.Dump(&output_stream, // Stream to dump to
0, // Byte offset within "data"
format, // Format to use when dumping
data.GetByteSize(), // Size in bytes of each item we are dumping
1, // Number of items to dump
UINT32_MAX, // Number of items per line
LLDB_INVALID_ADDRESS, // Invalid address, don't show any offset/address context
0, // Bitfield bit size
0); // Bitfield bit offset
}
output_stream.EOL();
}
else
{
error_stream.Printf ("error: %s\n", expr_error.AsCString());
success = false;
}
}
}
else
{
error_stream.Printf ("error: %s\n", expr_error.AsCString());
}
}
}
else
{
error_stream.PutCString ("error: invalid target triple\n");
}
return success;
}
bool
CommandObjectExpression::ExecuteRawCommandString
(
const char *command,
CommandContext *context,
CommandInterpreter *interpreter,
CommandReturnObject &result
)
{
ConstString target_triple;
Target *target = context->GetTarget ();
if (target)
target->GetTargetTriple(target_triple);
if (!target_triple)
target_triple = Host::GetTargetTriple ();
ExecutionContext exe_ctx(context->GetExecutionContext());
Stream &output_stream = result.GetOutputStream();
m_options.ResetOptionValues();
const char * expr = NULL;
if (command[0] == '\0')
{
m_expr_lines.clear();
m_expr_line_count = 0;
InputReaderSP reader_sp (new InputReader());
if (reader_sp)
{
Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
this, // baton
eInputReaderGranularityLine, // token size, to pass to callback function
NULL, // end token
NULL, // prompt
true)); // echo input
if (err.Success())
{
Debugger::GetSharedInstance().PushInputReader (reader_sp);
result.SetStatus (eReturnStatusSuccessFinishNoResult);
}
else
{
result.AppendError (err.AsCString());
result.SetStatus (eReturnStatusFailed);
}
}
else
{
result.AppendError("out of memory");
result.SetStatus (eReturnStatusFailed);
}
return result.Succeeded();
}
if (command[0] == '-')
{
// We have some options and these options MUST end with --.
const char *end_options = NULL;
const char *s = command;
while (s && s[0])
{
end_options = ::strstr (s, "--");
if (end_options)
{
end_options += 2; // Get past the "--"
if (::isspace (end_options[0]))
{
expr = end_options;
while (::isspace (*expr))
++expr;
break;
}
}
s = end_options;
}
if (end_options)
{
Args args(command, end_options - command);
if (!ParseOptions(args, interpreter, result))
return false;
}
}
const bool show_types = m_options.show_types;
const bool show_summary = m_options.show_summary;
const bool debug = m_options.debug;
if (expr == NULL)
expr = command;
if (target_triple)
{
ClangExpressionDeclMap expr_decl_map(&exe_ctx);
ClangExpression clang_expr(target_triple.AsCString(), &expr_decl_map);
unsigned num_errors = clang_expr.ParseExpression (expr, result.GetErrorStream());
if (num_errors == 0)
{
StreamString dwarf_opcodes;
dwarf_opcodes.SetByteOrder(eByteOrderHost);
dwarf_opcodes.GetFlags().Set(Stream::eBinary);
ClangExpressionVariableList expr_local_vars;
clang_expr.ConvertExpressionToDWARF (expr_local_vars, dwarf_opcodes);
result.SetStatus (eReturnStatusSuccessFinishResult);
DataExtractor dwarf_opcodes_data(dwarf_opcodes.GetData(), dwarf_opcodes.GetSize(), eByteOrderHost, 8);
DWARFExpression expr(dwarf_opcodes_data, 0, dwarf_opcodes_data.GetByteSize(), NULL);
expr.SetExpressionLocalVariableList(&expr_local_vars);
expr.SetExpressionDeclMap(&expr_decl_map);
if (debug)
{
output_stream << "Expression parsed ok, dwarf opcodes:";
output_stream.IndentMore();
expr.GetDescription(&output_stream, lldb::eDescriptionLevelVerbose);
output_stream.IndentLess();
output_stream.EOL();
}
clang::ASTContext *ast_context = clang_expr.GetASTContext();
Value expr_result;
Error expr_error;
bool expr_success = expr.Evaluate (&exe_ctx, ast_context, NULL, expr_result, &expr_error);
if (expr_success)
{
lldb::Format format = m_options.format;
// Resolve any values that are possible
expr_result.ResolveValue(&exe_ctx, ast_context);
if (expr_result.GetContextType() == Value::eContextTypeInvalid &&
expr_result.GetValueType() == Value::eValueTypeScalar &&
format == eFormatDefault)
{
// The expression result is just a scalar with no special formatting
expr_result.GetScalar().GetValue (&output_stream, show_types);
output_stream.EOL();
}
else
{
DataExtractor data;
expr_error = expr_result.GetValueAsData (&exe_ctx, ast_context, data, 0);
if (expr_error.Success())
{
if (format == eFormatDefault)
format = expr_result.GetValueDefaultFormat ();
void *clang_type = expr_result.GetValueOpaqueClangQualType();
if (clang_type)
{
if (show_types)
Type::DumpClangTypeName(&output_stream, clang_type);
Type::DumpValue (
&exe_ctx, // The execution context for memory and variable access
ast_context, // The ASTContext that the clang type belongs to
clang_type, // The opaque clang type we want to dump that value of
&output_stream, // Stream to dump to
format, // Format to use when dumping
data, // A buffer containing the bytes for the clang type
0, // Byte offset within "data" where value is
data.GetByteSize(), // Size in bytes of the value we are dumping
0, // Bitfield bit size
0, // Bitfield bit offset
show_types, // Show types?
show_summary, // Show summary?
debug, // Debug logging output?
UINT32_MAX); // Depth to dump in case this is an aggregate type
}
else
{
data.Dump(&output_stream, // Stream to dump to
0, // Byte offset within "data"
format, // Format to use when dumping
data.GetByteSize(), // Size in bytes of each item we are dumping
1, // Number of items to dump
UINT32_MAX, // Number of items per line
LLDB_INVALID_ADDRESS, // Invalid address, don't show any offset/address context
0, // Bitfield bit size
0); // Bitfield bit offset
}
output_stream.EOL();
}
else
{
result.AppendError(expr_error.AsCString());
result.SetStatus (eReturnStatusFailed);
}
}
}
else
{
result.AppendError (expr_error.AsCString());
result.SetStatus (eReturnStatusFailed);
}
}
else
{
result.SetStatus (eReturnStatusFailed);
}
}
else
{
result.AppendError ("invalid target triple");
result.SetStatus (eReturnStatusFailed);
}
return result.Succeeded();
}
lldb::OptionDefinition
CommandObjectExpression::CommandOptions::g_option_table[] =
{
{ LLDB_OPT_SET_1, true, "language", 'l', required_argument, NULL, 0, "[c|c++|objc|objc++]", "Sets the language to use when parsing the expression."},
{ LLDB_OPT_SET_2, 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."},
{ LLDB_OPT_SET_3, false, "debug", 'g', no_argument, NULL, 0, NULL, "Enable verbose debug logging of the expression parsing and evaluation."},
{ 0, false, NULL, 0, 0, NULL, NULL, NULL, NULL }
};
|