1 //===-- CommandObjectExpression.cpp ---------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringRef.h"
11 #include "CommandObjectExpression.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Expression/ExpressionVariable.h"
14 #include "lldb/Expression/REPL.h"
15 #include "lldb/Expression/UserExpression.h"
16 #include "lldb/Host/OptionParser.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandOptionArgumentTable.h"
19 #include "lldb/Interpreter/CommandReturnObject.h"
20 #include "lldb/Interpreter/OptionArgParser.h"
21 #include "lldb/Target/Language.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/StackFrame.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/lldb-enumerations.h"
26 #include "lldb/lldb-private-enumerations.h"
29 using namespace lldb_private
;
31 CommandObjectExpression::CommandOptions::CommandOptions() = default;
33 CommandObjectExpression::CommandOptions::~CommandOptions() = default;
35 #define LLDB_OPTIONS_expression
36 #include "CommandOptions.inc"
38 Status
CommandObjectExpression::CommandOptions::SetOptionValue(
39 uint32_t option_idx
, llvm::StringRef option_arg
,
40 ExecutionContext
*execution_context
) {
43 const int short_option
= GetDefinitions()[option_idx
].short_option
;
45 switch (short_option
) {
47 language
= Language::GetLanguageTypeFromString(option_arg
);
48 if (language
== eLanguageTypeUnknown
) {
50 sstr
.Printf("unknown language type: '%s' for expression. "
51 "List of supported languages:\n",
52 option_arg
.str().c_str());
54 Language::PrintSupportedLanguagesForExpressions(sstr
, " ", "\n");
55 error
.SetErrorString(sstr
.GetString());
62 result
= OptionArgParser::ToBoolean(option_arg
, true, &success
);
64 error
.SetErrorStringWithFormat(
65 "invalid all-threads value setting: \"%s\"",
66 option_arg
.str().c_str());
68 try_all_threads
= result
;
73 bool tmp_value
= OptionArgParser::ToBoolean(option_arg
, true, &success
);
75 ignore_breakpoints
= tmp_value
;
77 error
.SetErrorStringWithFormat(
78 "could not convert \"%s\" to a boolean value.",
79 option_arg
.str().c_str());
85 bool tmp_value
= OptionArgParser::ToBoolean(option_arg
, true, &success
);
87 allow_jit
= tmp_value
;
89 error
.SetErrorStringWithFormat(
90 "could not convert \"%s\" to a boolean value.",
91 option_arg
.str().c_str());
96 if (option_arg
.getAsInteger(0, timeout
)) {
98 error
.SetErrorStringWithFormat("invalid timeout setting \"%s\"",
99 option_arg
.str().c_str());
105 bool tmp_value
= OptionArgParser::ToBoolean(option_arg
, true, &success
);
107 unwind_on_error
= tmp_value
;
109 error
.SetErrorStringWithFormat(
110 "could not convert \"%s\" to a boolean value.",
111 option_arg
.str().c_str());
116 if (option_arg
.empty()) {
117 m_verbosity
= eLanguageRuntimeDescriptionDisplayVerbosityFull
;
120 m_verbosity
= (LanguageRuntimeDescriptionDisplayVerbosity
)
121 OptionArgParser::ToOptionEnum(
122 option_arg
, GetDefinitions()[option_idx
].enum_values
, 0, error
);
123 if (!error
.Success())
124 error
.SetErrorStringWithFormat(
125 "unrecognized value for description-verbosity '%s'",
126 option_arg
.str().c_str());
131 unwind_on_error
= false;
132 ignore_breakpoints
= false;
141 bool tmp_value
= OptionArgParser::ToBoolean(option_arg
, true, &success
);
143 auto_apply_fixits
= tmp_value
? eLazyBoolYes
: eLazyBoolNo
;
145 error
.SetErrorStringWithFormat(
146 "could not convert \"%s\" to a boolean value.",
147 option_arg
.str().c_str());
153 bool persist_result
=
154 OptionArgParser::ToBoolean(option_arg
, true, &success
);
156 suppress_persistent_result
= !persist_result
? eLazyBoolYes
: eLazyBoolNo
;
158 error
.SetErrorStringWithFormat(
159 "could not convert \"%s\" to a boolean value.",
160 option_arg
.str().c_str());
165 llvm_unreachable("Unimplemented option");
171 void CommandObjectExpression::CommandOptions::OptionParsingStarting(
172 ExecutionContext
*execution_context
) {
174 execution_context
? execution_context
->GetProcessSP() : ProcessSP();
176 ignore_breakpoints
= process_sp
->GetIgnoreBreakpointsInExpressions();
177 unwind_on_error
= process_sp
->GetUnwindOnErrorInExpressions();
179 ignore_breakpoints
= true;
180 unwind_on_error
= true;
184 try_all_threads
= true;
187 language
= eLanguageTypeUnknown
;
188 m_verbosity
= eLanguageRuntimeDescriptionDisplayVerbosityCompact
;
189 auto_apply_fixits
= eLazyBoolCalculate
;
192 suppress_persistent_result
= eLazyBoolCalculate
;
195 llvm::ArrayRef
<OptionDefinition
>
196 CommandObjectExpression::CommandOptions::GetDefinitions() {
197 return llvm::ArrayRef(g_expression_options
);
200 EvaluateExpressionOptions
201 CommandObjectExpression::CommandOptions::GetEvaluateExpressionOptions(
202 const Target
&target
, const OptionGroupValueObjectDisplay
&display_opts
) {
203 EvaluateExpressionOptions options
;
204 options
.SetCoerceToId(display_opts
.use_objc
);
205 options
.SetUnwindOnError(unwind_on_error
);
206 options
.SetIgnoreBreakpoints(ignore_breakpoints
);
207 options
.SetKeepInMemory(true);
208 options
.SetUseDynamic(display_opts
.use_dynamic
);
209 options
.SetTryAllThreads(try_all_threads
);
210 options
.SetDebug(debug
);
211 options
.SetLanguage(language
);
212 options
.SetExecutionPolicy(
213 allow_jit
? EvaluateExpressionOptions::default_execution_policy
214 : lldb_private::eExecutionPolicyNever
);
216 bool auto_apply_fixits
;
217 if (this->auto_apply_fixits
== eLazyBoolCalculate
)
218 auto_apply_fixits
= target
.GetEnableAutoApplyFixIts();
220 auto_apply_fixits
= this->auto_apply_fixits
== eLazyBoolYes
;
222 options
.SetAutoApplyFixIts(auto_apply_fixits
);
223 options
.SetRetriesWithFixIts(target
.GetNumberOfRetriesWithFixits());
226 options
.SetExecutionPolicy(eExecutionPolicyTopLevel
);
228 // If there is any chance we are going to stop and want to see what went
229 // wrong with our expression, we should generate debug info
230 if (!ignore_breakpoints
|| !unwind_on_error
)
231 options
.SetGenerateDebugInfo(true);
234 options
.SetTimeout(std::chrono::microseconds(timeout
));
236 options
.SetTimeout(std::nullopt
);
240 bool CommandObjectExpression::CommandOptions::ShouldSuppressResult(
241 const OptionGroupValueObjectDisplay
&display_opts
) const {
242 // Explicitly disabling persistent results takes precedence over the
243 // m_verbosity/use_objc logic.
244 if (suppress_persistent_result
!= eLazyBoolCalculate
)
245 return suppress_persistent_result
== eLazyBoolYes
;
247 return display_opts
.use_objc
&&
248 m_verbosity
== eLanguageRuntimeDescriptionDisplayVerbosityCompact
;
251 CommandObjectExpression::CommandObjectExpression(
252 CommandInterpreter
&interpreter
)
253 : CommandObjectRaw(interpreter
, "expression",
254 "Evaluate an expression on the current "
255 "thread. Displays any returned value "
256 "with LLDB's default formatting.",
258 eCommandProcessMustBePaused
| eCommandTryTargetAPILock
),
259 IOHandlerDelegate(IOHandlerDelegate::Completion::Expression
),
260 m_format_options(eFormatDefault
),
261 m_repl_option(LLDB_OPT_SET_1
, false, "repl", 'r', "Drop into REPL", false,
263 m_expr_line_count(0) {
266 Single and multi-line expressions:
269 " The expression provided on the command line must be a complete expression \
270 with no newlines. To evaluate a multi-line expression, \
271 hit a return after an empty expression, and lldb will enter the multi-line expression editor. \
272 Hit return on an empty line to end the multi-line expression."
279 " If the expression can be evaluated statically (without running code) then it will be. \
280 Otherwise, by default the expression will run on the current thread with a short timeout: \
281 currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \
282 and resumed with all threads running. You can use the -a option to disable retrying on all \
283 threads. You can use the -t option to set a shorter timeout."
286 User defined variables:
289 " You can define your own variables for convenience or to be used in subsequent expressions. \
290 You define them the same way you would define variables in C. If the first character of \
291 your user defined variable is a $, then the variable's value will be available in future \
292 expressions, otherwise it will just be available in the current expression."
295 Continuing evaluation after a breakpoint:
298 " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \
299 you are done with your investigation, you can either remove the expression execution frames \
300 from the stack with \"thread return -x\" or if you are still interested in the expression result \
301 you can issue the \"continue\" command and the expression evaluation will complete and the \
302 expression result will be available using the \"thread.completed-expression\" key in the thread \
309 expr my_struct->a = my_array[3]
310 expr -f bin -- (index * 8) + 5
311 expr unsigned int $foo = 5
312 expr char c[] = \"foo
\"; c
[0])");
314 CommandArgumentEntry arg;
315 CommandArgumentData expression_arg;
317 // Define the first (and only) variant of this arg.
318 expression_arg.arg_type = eArgTypeExpression;
319 expression_arg.arg_repetition = eArgRepeatPlain;
321 // There is only one variant this argument could be; put it into the argument
323 arg.push_back(expression_arg);
325 // Push the data for the first argument into the m_arguments vector.
326 m_arguments.push_back(arg);
328 // Add the "--format
" and "--gdb
-format
"
329 m_option_group.Append(&m_format_options,
330 OptionGroupFormat::OPTION_GROUP_FORMAT |
331 OptionGroupFormat::OPTION_GROUP_GDB_FMT,
333 m_option_group.Append(&m_command_options);
334 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL,
335 LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
336 m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3);
337 m_option_group.Finalize();
340 CommandObjectExpression::~CommandObjectExpression() = default;
342 Options *CommandObjectExpression::GetOptions() { return &m_option_group; }
344 void CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
345 EvaluateExpressionOptions options;
346 options.SetCoerceToId(m_varobj_options.use_objc);
347 options.SetLanguage(m_command_options.language);
348 options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever);
349 options.SetAutoApplyFixIts(false);
350 options.SetGenerateDebugInfo(false);
352 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
354 // Get out before we start doing things that expect a valid frame pointer.
355 if (exe_ctx.GetFramePtr() == nullptr)
358 Target *exe_target = exe_ctx.GetTargetPtr();
359 Target &target = exe_target ? *exe_target : GetDummyTarget();
361 unsigned cursor_pos = request.GetRawCursorPos();
362 // Get the full user input including the suffix. The suffix is necessary
363 // as OptionsWithRaw will use it to detect if the cursor is cursor is in the
364 // argument part of in the raw input part of the arguments. If we cut of
365 // of the suffix then "expr
-arg
[cursor
] --" would interpret the "-arg
" as
366 // the raw input (as the "--" is hidden in the suffix).
367 llvm::StringRef code = request.GetRawLineWithUnusedSuffix();
369 const std::size_t original_code_size = code.size();
371 // Remove the first token which is 'expr' or some alias/abbreviation of that.
372 code = llvm::getToken(code).second.ltrim();
373 OptionsWithRaw args(code);
374 code = args.GetRawPart();
376 // The position where the expression starts in the command line.
377 assert(original_code_size >= code.size());
378 std::size_t raw_start = original_code_size - code.size();
380 // Check if the cursor is actually in the expression string, and if not, we
382 // FIXME: We should complete the options here.
383 if (cursor_pos < raw_start)
386 // Make the cursor_pos again relative to the start of the code string.
387 assert(cursor_pos >= raw_start);
388 cursor_pos -= raw_start;
390 auto language = exe_ctx.GetFrameRef().GetLanguage();
393 lldb::UserExpressionSP expr(target.GetUserExpressionForLanguage(
394 code, llvm::StringRef(), language, UserExpression::eResultTypeAny,
395 options, nullptr, error));
399 expr->Complete(exe_ctx, request, cursor_pos);
402 static lldb_private::Status
403 CanBeUsedForElementCountPrinting(ValueObject &valobj) {
404 CompilerType type(valobj.GetCompilerType());
405 CompilerType pointee;
406 if (!type.IsPointerType(&pointee))
407 return Status("as it does
not refer to a pointer
");
408 if (pointee.IsVoidType())
409 return Status("as it refers to a pointer to
void");
413 bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
414 Stream &output_stream,
415 Stream &error_stream,
416 CommandReturnObject &result) {
417 // Don't use m_exe_ctx as this might be called asynchronously after the
418 // command object DoExecute has finished when doing multi-line expression
419 // that use an input reader...
420 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
421 Target *exe_target = exe_ctx.GetTargetPtr();
422 Target &target = exe_target ? *exe_target : GetDummyTarget();
424 lldb::ValueObjectSP result_valobj_sp;
425 StackFrame *frame = exe_ctx.GetFramePtr();
427 if (m_command_options.top_level && !m_command_options.allow_jit) {
428 result.AppendErrorWithFormat(
429 "Can
't disable JIT compilation for top-level expressions.\n");
433 EvaluateExpressionOptions eval_options =
434 m_command_options.GetEvaluateExpressionOptions(target, m_varobj_options);
435 // This command manually removes the result variable, make sure expression
436 // evaluation doesn't
do it first
.
437 eval_options
.SetSuppressPersistentResult(false);
439 ExpressionResults success
= target
.EvaluateExpression(
440 expr
, frame
, result_valobj_sp
, eval_options
, &m_fixed_expression
);
442 // Only mention Fix-Its if the expression evaluator applied them.
443 // Compiler errors refer to the final expression after applying Fix-It(s).
444 if (!m_fixed_expression
.empty() && target
.GetEnableNotifyAboutFixIts()) {
445 error_stream
<< " Evaluated this expression after applying Fix-It(s):\n";
446 error_stream
<< " " << m_fixed_expression
<< "\n";
449 if (result_valobj_sp
) {
450 Format format
= m_format_options
.GetFormat();
452 if (result_valobj_sp
->GetError().Success()) {
453 if (format
!= eFormatVoid
) {
454 if (format
!= eFormatDefault
)
455 result_valobj_sp
->SetFormat(format
);
457 if (m_varobj_options
.elem_count
> 0) {
458 Status
error(CanBeUsedForElementCountPrinting(*result_valobj_sp
));
460 result
.AppendErrorWithFormat(
461 "expression cannot be used with --element-count %s\n",
462 error
.AsCString(""));
467 bool suppress_result
=
468 m_command_options
.ShouldSuppressResult(m_varobj_options
);
470 DumpValueObjectOptions
options(m_varobj_options
.GetAsDumpOptions(
471 m_command_options
.m_verbosity
, format
));
472 options
.SetHideRootName(suppress_result
);
473 options
.SetVariableFormatDisplayLanguage(
474 result_valobj_sp
->GetPreferredDisplayLanguage());
476 result_valobj_sp
->Dump(output_stream
, options
);
479 if (auto result_var_sp
=
480 target
.GetPersistentVariable(result_valobj_sp
->GetName())) {
481 auto language
= result_valobj_sp
->GetPreferredDisplayLanguage();
482 if (auto *persistent_state
=
483 target
.GetPersistentExpressionStateForLanguage(language
))
484 persistent_state
->RemovePersistentVariable(result_var_sp
);
486 result
.SetStatus(eReturnStatusSuccessFinishResult
);
489 if (result_valobj_sp
->GetError().GetError() ==
490 UserExpression::kNoResult
) {
491 if (format
!= eFormatVoid
&& GetDebugger().GetNotifyVoid()) {
492 error_stream
.PutCString("(void)\n");
495 result
.SetStatus(eReturnStatusSuccessFinishResult
);
497 const char *error_cstr
= result_valobj_sp
->GetError().AsCString();
498 if (error_cstr
&& error_cstr
[0]) {
499 const size_t error_cstr_len
= strlen(error_cstr
);
500 const bool ends_with_newline
= error_cstr
[error_cstr_len
- 1] == '\n';
501 if (strstr(error_cstr
, "error:") != error_cstr
)
502 error_stream
.PutCString("error: ");
503 error_stream
.Write(error_cstr
, error_cstr_len
);
504 if (!ends_with_newline
)
507 error_stream
.PutCString("error: unknown error\n");
510 result
.SetStatus(eReturnStatusFailed
);
514 error_stream
.Printf("error: unknown error\n");
517 return (success
!= eExpressionSetupError
&&
518 success
!= eExpressionParseError
);
521 void CommandObjectExpression::IOHandlerInputComplete(IOHandler
&io_handler
,
523 io_handler
.SetIsDone(true);
524 StreamFileSP output_sp
= io_handler
.GetOutputStreamFileSP();
525 StreamFileSP error_sp
= io_handler
.GetErrorStreamFileSP();
527 CommandReturnObject
return_obj(
528 GetCommandInterpreter().GetDebugger().GetUseColor());
529 EvaluateExpression(line
.c_str(), *output_sp
, *error_sp
, return_obj
);
536 bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler
&io_handler
,
538 // An empty lines is used to indicate the end of input
539 const size_t num_lines
= lines
.GetSize();
540 if (num_lines
> 0 && lines
[num_lines
- 1].empty()) {
541 // Remove the last empty line from "lines" so it doesn't appear in our
542 // resulting input and return true to indicate we are done getting lines
549 void CommandObjectExpression::GetMultilineExpression() {
550 m_expr_lines
.clear();
551 m_expr_line_count
= 0;
553 Debugger
&debugger
= GetCommandInterpreter().GetDebugger();
554 bool color_prompt
= debugger
.GetUseColor();
555 const bool multiple_lines
= true; // Get multiple lines
556 IOHandlerSP
io_handler_sp(
557 new IOHandlerEditline(debugger
, IOHandler::Type::Expression
,
558 "lldb-expr", // Name of input reader for history
559 llvm::StringRef(), // No prompt
560 llvm::StringRef(), // Continuation prompt
561 multiple_lines
, color_prompt
,
562 1, // Show line numbers starting at 1
565 StreamFileSP output_sp
= io_handler_sp
->GetOutputStreamFileSP();
567 output_sp
->PutCString(
568 "Enter expressions, then terminate with an empty line to evaluate:\n");
571 debugger
.RunIOHandlerAsync(io_handler_sp
);
574 static EvaluateExpressionOptions
575 GetExprOptions(ExecutionContext
&ctx
,
576 CommandObjectExpression::CommandOptions command_options
) {
577 command_options
.OptionParsingStarting(&ctx
);
579 // Default certain settings for REPL regardless of the global settings.
580 command_options
.unwind_on_error
= false;
581 command_options
.ignore_breakpoints
= false;
582 command_options
.debug
= false;
584 EvaluateExpressionOptions expr_options
;
585 expr_options
.SetUnwindOnError(command_options
.unwind_on_error
);
586 expr_options
.SetIgnoreBreakpoints(command_options
.ignore_breakpoints
);
587 expr_options
.SetTryAllThreads(command_options
.try_all_threads
);
589 if (command_options
.timeout
> 0)
590 expr_options
.SetTimeout(std::chrono::microseconds(command_options
.timeout
));
592 expr_options
.SetTimeout(std::nullopt
);
597 void CommandObjectExpression::DoExecute(llvm::StringRef command
,
598 CommandReturnObject
&result
) {
599 m_fixed_expression
.clear();
600 auto exe_ctx
= GetCommandInterpreter().GetExecutionContext();
601 m_option_group
.NotifyOptionParsingStarting(&exe_ctx
);
603 if (command
.empty()) {
604 GetMultilineExpression();
608 OptionsWithRaw
args(command
);
609 llvm::StringRef expr
= args
.GetRawPart();
611 if (args
.HasArgs()) {
612 if (!ParseOptionsAndNotify(args
.GetArgs(), result
, m_option_group
, exe_ctx
))
615 if (m_repl_option
.GetOptionValue().GetCurrentValue()) {
616 Target
&target
= GetSelectedOrDummyTarget();
618 m_expr_lines
.clear();
619 m_expr_line_count
= 0;
621 Debugger
&debugger
= target
.GetDebugger();
623 // Check if the LLDB command interpreter is sitting on top of a REPL
624 // that launched it...
625 if (debugger
.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter
,
626 IOHandler::Type::REPL
)) {
627 // the LLDB command interpreter is sitting on top of a REPL that
628 // launched it, so just say the command interpreter is done and
629 // fall back to the existing REPL
630 m_interpreter
.GetIOHandler(false)->SetIsDone(true);
632 // We are launching the REPL on top of the current LLDB command
633 // interpreter, so just push one
634 bool initialize
= false;
636 REPLSP
repl_sp(target
.GetREPL(repl_error
, m_command_options
.language
,
641 repl_sp
= target
.GetREPL(repl_error
, m_command_options
.language
,
643 if (!repl_error
.Success()) {
644 result
.SetError(repl_error
);
651 repl_sp
->SetEvaluateOptions(
652 GetExprOptions(exe_ctx
, m_command_options
));
653 repl_sp
->SetFormatOptions(m_format_options
);
654 repl_sp
->SetValueObjectDisplayOptions(m_varobj_options
);
657 IOHandlerSP
io_handler_sp(repl_sp
->GetIOHandler());
658 io_handler_sp
->SetIsDone(false);
659 debugger
.RunIOHandlerAsync(io_handler_sp
);
661 repl_error
.SetErrorStringWithFormat(
662 "Couldn't create a REPL for %s",
663 Language::GetNameForLanguageType(m_command_options
.language
));
664 result
.SetError(repl_error
);
669 // No expression following options
670 else if (expr
.empty()) {
671 GetMultilineExpression();
676 Target
&target
= GetSelectedOrDummyTarget();
677 if (EvaluateExpression(expr
, result
.GetOutputStream(),
678 result
.GetErrorStream(), result
)) {
680 if (!m_fixed_expression
.empty() && target
.GetEnableNotifyAboutFixIts()) {
681 CommandHistory
&history
= m_interpreter
.GetCommandHistory();
682 // FIXME: Can we figure out what the user actually typed (e.g. some alias
684 // If we can it would be nice to show that.
685 std::string
fixed_command("expression ");
686 if (args
.HasArgs()) {
687 // Add in any options that might have been in the original command:
688 fixed_command
.append(std::string(args
.GetArgStringWithDelimiter()));
689 fixed_command
.append(m_fixed_expression
);
691 fixed_command
.append(m_fixed_expression
);
692 history
.AppendString(fixed_command
);
696 result
.SetStatus(eReturnStatusFailed
);