15ffd83dbSDimitry Andric //===-- CommandObjectFrame.cpp --------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric #include "CommandObjectFrame.h"
90b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
100b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
110b57cec5SDimitry Andric #include "lldb/DataFormatters/DataVisualization.h"
120b57cec5SDimitry Andric #include "lldb/DataFormatters/ValueObjectPrinter.h"
13480093f4SDimitry Andric #include "lldb/Host/Config.h"
140b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
150b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
160b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
170b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupFormat.h"
180b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
190b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupVariable.h"
200b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
250b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
260b57cec5SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
270b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
280b57cec5SDimitry Andric #include "lldb/Target/Target.h"
290b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
300b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #include <memory>
330b57cec5SDimitry Andric #include <string>
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace lldb;
360b57cec5SDimitry Andric using namespace lldb_private;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric #pragma mark CommandObjectFrameDiagnose
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric // CommandObjectFrameInfo
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric // CommandObjectFrameDiagnose
430b57cec5SDimitry Andric 
449dba64beSDimitry Andric #define LLDB_OPTIONS_frame_diag
459dba64beSDimitry Andric #include "CommandOptions.inc"
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric class CommandObjectFrameDiagnose : public CommandObjectParsed {
480b57cec5SDimitry Andric public:
490b57cec5SDimitry Andric   class CommandOptions : public Options {
500b57cec5SDimitry Andric   public:
CommandOptions()510b57cec5SDimitry Andric     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric     ~CommandOptions() override = default;
540b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)550b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
560b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
570b57cec5SDimitry Andric       Status error;
580b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
590b57cec5SDimitry Andric       switch (short_option) {
600b57cec5SDimitry Andric       case 'r':
610b57cec5SDimitry Andric         reg = ConstString(option_arg);
620b57cec5SDimitry Andric         break;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric       case 'a': {
650b57cec5SDimitry Andric         address.emplace();
660b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, *address)) {
670b57cec5SDimitry Andric           address.reset();
680b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid address argument '%s'",
690b57cec5SDimitry Andric                                          option_arg.str().c_str());
700b57cec5SDimitry Andric         }
710b57cec5SDimitry Andric       } break;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric       case 'o': {
740b57cec5SDimitry Andric         offset.emplace();
750b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, *offset)) {
760b57cec5SDimitry Andric           offset.reset();
770b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid offset argument '%s'",
780b57cec5SDimitry Andric                                          option_arg.str().c_str());
790b57cec5SDimitry Andric         }
800b57cec5SDimitry Andric       } break;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric       default:
839dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
840b57cec5SDimitry Andric       }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric       return error;
870b57cec5SDimitry Andric     }
880b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)890b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
900b57cec5SDimitry Andric       address.reset();
910b57cec5SDimitry Andric       reg.reset();
920b57cec5SDimitry Andric       offset.reset();
930b57cec5SDimitry Andric     }
940b57cec5SDimitry Andric 
GetDefinitions()950b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
960b57cec5SDimitry Andric       return llvm::makeArrayRef(g_frame_diag_options);
970b57cec5SDimitry Andric     }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric     // Options.
1000b57cec5SDimitry Andric     llvm::Optional<lldb::addr_t> address;
1010b57cec5SDimitry Andric     llvm::Optional<ConstString> reg;
1020b57cec5SDimitry Andric     llvm::Optional<int64_t> offset;
1030b57cec5SDimitry Andric   };
1040b57cec5SDimitry Andric 
CommandObjectFrameDiagnose(CommandInterpreter & interpreter)1050b57cec5SDimitry Andric   CommandObjectFrameDiagnose(CommandInterpreter &interpreter)
1060b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame diagnose",
1070b57cec5SDimitry Andric                             "Try to determine what path path the current stop "
1080b57cec5SDimitry Andric                             "location used to get to a register or address",
1090b57cec5SDimitry Andric                             nullptr,
1100b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
1110b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
1120b57cec5SDimitry Andric                                 eCommandProcessMustBePaused),
1130b57cec5SDimitry Andric         m_options() {
1140b57cec5SDimitry Andric     CommandArgumentEntry arg;
1150b57cec5SDimitry Andric     CommandArgumentData index_arg;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1180b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
1190b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatOptional;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1220b57cec5SDimitry Andric     // argument entry.
1230b57cec5SDimitry Andric     arg.push_back(index_arg);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1260b57cec5SDimitry Andric     m_arguments.push_back(arg);
1270b57cec5SDimitry Andric   }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   ~CommandObjectFrameDiagnose() override = default;
1300b57cec5SDimitry Andric 
GetOptions()1310b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)1340b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1350b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
1360b57cec5SDimitry Andric     StackFrameSP frame_sp = thread->GetSelectedFrame();
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric     if (m_options.address.hasValue()) {
1410b57cec5SDimitry Andric       if (m_options.reg.hasValue() || m_options.offset.hasValue()) {
1420b57cec5SDimitry Andric         result.AppendError(
1430b57cec5SDimitry Andric             "`frame diagnose --address` is incompatible with other arguments.");
1440b57cec5SDimitry Andric         return false;
1450b57cec5SDimitry Andric       }
1460b57cec5SDimitry Andric       valobj_sp = frame_sp->GuessValueForAddress(m_options.address.getValue());
1470b57cec5SDimitry Andric     } else if (m_options.reg.hasValue()) {
1480b57cec5SDimitry Andric       valobj_sp = frame_sp->GuessValueForRegisterAndOffset(
1490b57cec5SDimitry Andric           m_options.reg.getValue(), m_options.offset.getValueOr(0));
1500b57cec5SDimitry Andric     } else {
1510b57cec5SDimitry Andric       StopInfoSP stop_info_sp = thread->GetStopInfo();
1520b57cec5SDimitry Andric       if (!stop_info_sp) {
1530b57cec5SDimitry Andric         result.AppendError("No arguments provided, and no stop info.");
1540b57cec5SDimitry Andric         return false;
1550b57cec5SDimitry Andric       }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric       valobj_sp = StopInfo::GetCrashingDereference(stop_info_sp);
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric     if (!valobj_sp) {
1610b57cec5SDimitry Andric       result.AppendError("No diagnosis available.");
1620b57cec5SDimitry Andric       return false;
1630b57cec5SDimitry Andric     }
1640b57cec5SDimitry Andric 
165480093f4SDimitry Andric     DumpValueObjectOptions::DeclPrintingHelper helper =
166480093f4SDimitry Andric         [&valobj_sp](ConstString type, ConstString var,
167480093f4SDimitry Andric                      const DumpValueObjectOptions &opts,
1680b57cec5SDimitry Andric                      Stream &stream) -> bool {
1690b57cec5SDimitry Andric       const ValueObject::GetExpressionPathFormat format = ValueObject::
1700b57cec5SDimitry Andric           GetExpressionPathFormat::eGetExpressionPathFormatHonorPointers;
1715ffd83dbSDimitry Andric       valobj_sp->GetExpressionPath(stream, format);
1720b57cec5SDimitry Andric       stream.PutCString(" =");
1730b57cec5SDimitry Andric       return true;
1740b57cec5SDimitry Andric     };
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     DumpValueObjectOptions options;
1770b57cec5SDimitry Andric     options.SetDeclPrintingHelper(helper);
1780b57cec5SDimitry Andric     ValueObjectPrinter printer(valobj_sp.get(), &result.GetOutputStream(),
1790b57cec5SDimitry Andric                                options);
1800b57cec5SDimitry Andric     printer.PrintValueObject();
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric     return true;
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   CommandOptions m_options;
1860b57cec5SDimitry Andric };
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric #pragma mark CommandObjectFrameInfo
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric // CommandObjectFrameInfo
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric class CommandObjectFrameInfo : public CommandObjectParsed {
1930b57cec5SDimitry Andric public:
CommandObjectFrameInfo(CommandInterpreter & interpreter)1940b57cec5SDimitry Andric   CommandObjectFrameInfo(CommandInterpreter &interpreter)
195480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame info",
196480093f4SDimitry Andric                             "List information about the current "
1970b57cec5SDimitry Andric                             "stack frame in the current thread.",
1980b57cec5SDimitry Andric                             "frame info",
1990b57cec5SDimitry Andric                             eCommandRequiresFrame | eCommandTryTargetAPILock |
200480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
201480093f4SDimitry Andric                                 eCommandProcessMustBePaused) {}
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   ~CommandObjectFrameInfo() override = default;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)2060b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
2070b57cec5SDimitry Andric     m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat(&result.GetOutputStream());
2080b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
2090b57cec5SDimitry Andric     return result.Succeeded();
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric };
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric #pragma mark CommandObjectFrameSelect
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric // CommandObjectFrameSelect
2160b57cec5SDimitry Andric 
2179dba64beSDimitry Andric #define LLDB_OPTIONS_frame_select
2189dba64beSDimitry Andric #include "CommandOptions.inc"
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric class CommandObjectFrameSelect : public CommandObjectParsed {
2210b57cec5SDimitry Andric public:
2220b57cec5SDimitry Andric   class CommandOptions : public Options {
2230b57cec5SDimitry Andric   public:
CommandOptions()2240b57cec5SDimitry Andric     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     ~CommandOptions() override = default;
2270b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)2280b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2290b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
2300b57cec5SDimitry Andric       Status error;
2310b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2320b57cec5SDimitry Andric       switch (short_option) {
2339dba64beSDimitry Andric       case 'r': {
2349dba64beSDimitry Andric         int32_t offset = 0;
2359dba64beSDimitry Andric         if (option_arg.getAsInteger(0, offset) || offset == INT32_MIN) {
2360b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid frame offset argument '%s'",
2370b57cec5SDimitry Andric                                          option_arg.str().c_str());
2389dba64beSDimitry Andric         } else
2399dba64beSDimitry Andric           relative_frame_offset = offset;
2400b57cec5SDimitry Andric         break;
2419dba64beSDimitry Andric       }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric       default:
2449dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
2450b57cec5SDimitry Andric       }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric       return error;
2480b57cec5SDimitry Andric     }
2490b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)2500b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2519dba64beSDimitry Andric       relative_frame_offset.reset();
2520b57cec5SDimitry Andric     }
2530b57cec5SDimitry Andric 
GetDefinitions()2540b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2550b57cec5SDimitry Andric       return llvm::makeArrayRef(g_frame_select_options);
2560b57cec5SDimitry Andric     }
2570b57cec5SDimitry Andric 
2589dba64beSDimitry Andric     llvm::Optional<int32_t> relative_frame_offset;
2590b57cec5SDimitry Andric   };
2600b57cec5SDimitry Andric 
CommandObjectFrameSelect(CommandInterpreter & interpreter)2610b57cec5SDimitry Andric   CommandObjectFrameSelect(CommandInterpreter &interpreter)
262480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame select",
263480093f4SDimitry Andric                             "Select the current stack frame by "
2640b57cec5SDimitry Andric                             "index from within the current thread "
2650b57cec5SDimitry Andric                             "(see 'thread backtrace'.)",
2660b57cec5SDimitry Andric                             nullptr,
2670b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
268480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
269480093f4SDimitry Andric                                 eCommandProcessMustBePaused),
2700b57cec5SDimitry Andric         m_options() {
2710b57cec5SDimitry Andric     CommandArgumentEntry arg;
2720b57cec5SDimitry Andric     CommandArgumentData index_arg;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
2750b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
2760b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatOptional;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
2790b57cec5SDimitry Andric     // argument entry.
2800b57cec5SDimitry Andric     arg.push_back(index_arg);
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
2830b57cec5SDimitry Andric     m_arguments.push_back(arg);
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   ~CommandObjectFrameSelect() override = default;
2870b57cec5SDimitry Andric 
2885ffd83dbSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)2895ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
2905ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
291af732203SDimitry Andric     if (request.GetCursorIndex() != 0)
2925ffd83dbSDimitry Andric       return;
2935ffd83dbSDimitry Andric 
294af732203SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
295af732203SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eFrameIndexCompletion,
296af732203SDimitry Andric         request, nullptr);
2975ffd83dbSDimitry Andric   }
2985ffd83dbSDimitry Andric 
GetOptions()2990b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)3020b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
3030b57cec5SDimitry Andric     // No need to check "thread" for validity as eCommandRequiresThread ensures
3040b57cec5SDimitry Andric     // it is valid
3050b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric     uint32_t frame_idx = UINT32_MAX;
3089dba64beSDimitry Andric     if (m_options.relative_frame_offset.hasValue()) {
3090b57cec5SDimitry Andric       // The one and only argument is a signed relative frame index
3100b57cec5SDimitry Andric       frame_idx = thread->GetSelectedFrameIndex();
3110b57cec5SDimitry Andric       if (frame_idx == UINT32_MAX)
3120b57cec5SDimitry Andric         frame_idx = 0;
3130b57cec5SDimitry Andric 
3149dba64beSDimitry Andric       if (*m_options.relative_frame_offset < 0) {
3159dba64beSDimitry Andric         if (static_cast<int32_t>(frame_idx) >=
3169dba64beSDimitry Andric             -*m_options.relative_frame_offset)
3179dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3180b57cec5SDimitry Andric         else {
3190b57cec5SDimitry Andric           if (frame_idx == 0) {
3200b57cec5SDimitry Andric             // If you are already at the bottom of the stack, then just warn
3210b57cec5SDimitry Andric             // and don't reset the frame.
3220b57cec5SDimitry Andric             result.AppendError("Already at the bottom of the stack.");
3230b57cec5SDimitry Andric             return false;
3240b57cec5SDimitry Andric           } else
3250b57cec5SDimitry Andric             frame_idx = 0;
3260b57cec5SDimitry Andric         }
3279dba64beSDimitry Andric       } else if (*m_options.relative_frame_offset > 0) {
3280b57cec5SDimitry Andric         // I don't want "up 20" where "20" takes you past the top of the stack
3290b57cec5SDimitry Andric         // to produce
3300b57cec5SDimitry Andric         // an error, but rather to just go to the top.  So I have to count the
3310b57cec5SDimitry Andric         // stack here...
3320b57cec5SDimitry Andric         const uint32_t num_frames = thread->GetStackFrameCount();
3330b57cec5SDimitry Andric         if (static_cast<int32_t>(num_frames - frame_idx) >
3349dba64beSDimitry Andric             *m_options.relative_frame_offset)
3359dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3360b57cec5SDimitry Andric         else {
3370b57cec5SDimitry Andric           if (frame_idx == num_frames - 1) {
3380b57cec5SDimitry Andric             // If we are already at the top of the stack, just warn and don't
3390b57cec5SDimitry Andric             // reset the frame.
3400b57cec5SDimitry Andric             result.AppendError("Already at the top of the stack.");
3410b57cec5SDimitry Andric             return false;
3420b57cec5SDimitry Andric           } else
3430b57cec5SDimitry Andric             frame_idx = num_frames - 1;
3440b57cec5SDimitry Andric         }
3450b57cec5SDimitry Andric       }
3460b57cec5SDimitry Andric     } else {
3470b57cec5SDimitry Andric       if (command.GetArgumentCount() > 1) {
3480b57cec5SDimitry Andric         result.AppendErrorWithFormat(
3490b57cec5SDimitry Andric             "too many arguments; expected frame-index, saw '%s'.\n",
3500b57cec5SDimitry Andric             command[0].c_str());
3510b57cec5SDimitry Andric         m_options.GenerateOptionUsage(
3520b57cec5SDimitry Andric             result.GetErrorStream(), this,
3530b57cec5SDimitry Andric             GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3540b57cec5SDimitry Andric         return false;
3550b57cec5SDimitry Andric       }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
3589dba64beSDimitry Andric         if (command[0].ref().getAsInteger(0, frame_idx)) {
3590b57cec5SDimitry Andric           result.AppendErrorWithFormat("invalid frame index argument '%s'.",
3600b57cec5SDimitry Andric                                        command[0].c_str());
3610b57cec5SDimitry Andric           return false;
3620b57cec5SDimitry Andric         }
3630b57cec5SDimitry Andric       } else if (command.GetArgumentCount() == 0) {
3640b57cec5SDimitry Andric         frame_idx = thread->GetSelectedFrameIndex();
3650b57cec5SDimitry Andric         if (frame_idx == UINT32_MAX) {
3660b57cec5SDimitry Andric           frame_idx = 0;
3670b57cec5SDimitry Andric         }
3680b57cec5SDimitry Andric       }
3690b57cec5SDimitry Andric     }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric     bool success = thread->SetSelectedFrameByIndexNoisily(
3720b57cec5SDimitry Andric         frame_idx, result.GetOutputStream());
3730b57cec5SDimitry Andric     if (success) {
3740b57cec5SDimitry Andric       m_exe_ctx.SetFrameSP(thread->GetSelectedFrame());
3750b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
3760b57cec5SDimitry Andric     } else {
3770b57cec5SDimitry Andric       result.AppendErrorWithFormat("Frame index (%u) out of range.\n",
3780b57cec5SDimitry Andric                                    frame_idx);
3790b57cec5SDimitry Andric     }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric     return result.Succeeded();
3820b57cec5SDimitry Andric   }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   CommandOptions m_options;
3850b57cec5SDimitry Andric };
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric #pragma mark CommandObjectFrameVariable
3880b57cec5SDimitry Andric // List images with associated information
3890b57cec5SDimitry Andric class CommandObjectFrameVariable : public CommandObjectParsed {
3900b57cec5SDimitry Andric public:
CommandObjectFrameVariable(CommandInterpreter & interpreter)3910b57cec5SDimitry Andric   CommandObjectFrameVariable(CommandInterpreter &interpreter)
3920b57cec5SDimitry Andric       : CommandObjectParsed(
3930b57cec5SDimitry Andric             interpreter, "frame variable",
3940b57cec5SDimitry Andric             "Show variables for the current stack frame. Defaults to all "
3950b57cec5SDimitry Andric             "arguments and local variables in scope. Names of argument, "
3960b57cec5SDimitry Andric             "local, file static and file global variables can be specified. "
3970b57cec5SDimitry Andric             "Children of aggregate variables can be specified such as "
3980b57cec5SDimitry Andric             "'var->child.x'.  The -> and [] operators in 'frame variable' do "
3990b57cec5SDimitry Andric             "not invoke operator overloads if they exist, but directly access "
4000b57cec5SDimitry Andric             "the specified element.  If you want to trigger operator overloads "
4010b57cec5SDimitry Andric             "use the expression command to print the variable instead."
4020b57cec5SDimitry Andric             "\nIt is worth noting that except for overloaded "
4030b57cec5SDimitry Andric             "operators, when printing local variables 'expr local_var' and "
4040b57cec5SDimitry Andric             "'frame var local_var' produce the same "
4050b57cec5SDimitry Andric             "results.  However, 'frame variable' is more efficient, since it "
4060b57cec5SDimitry Andric             "uses debug information and memory reads directly, rather than "
4070b57cec5SDimitry Andric             "parsing and evaluating an expression, which may even involve "
4080b57cec5SDimitry Andric             "JITing and running code in the target program.",
409480093f4SDimitry Andric             nullptr,
410480093f4SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
411480093f4SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
412480093f4SDimitry Andric                 eCommandRequiresProcess),
4130b57cec5SDimitry Andric         m_option_group(),
4140b57cec5SDimitry Andric         m_option_variable(
4150b57cec5SDimitry Andric             true), // Include the frame specific options by passing "true"
416480093f4SDimitry Andric         m_option_format(eFormatDefault), m_varobj_options() {
4170b57cec5SDimitry Andric     CommandArgumentEntry arg;
4180b57cec5SDimitry Andric     CommandArgumentData var_name_arg;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
4210b57cec5SDimitry Andric     var_name_arg.arg_type = eArgTypeVarName;
4220b57cec5SDimitry Andric     var_name_arg.arg_repetition = eArgRepeatStar;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
4250b57cec5SDimitry Andric     // argument entry.
4260b57cec5SDimitry Andric     arg.push_back(var_name_arg);
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
4290b57cec5SDimitry Andric     m_arguments.push_back(arg);
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4320b57cec5SDimitry Andric     m_option_group.Append(&m_option_format,
4330b57cec5SDimitry Andric                           OptionGroupFormat::OPTION_GROUP_FORMAT |
4340b57cec5SDimitry Andric                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
4350b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
4360b57cec5SDimitry Andric     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4370b57cec5SDimitry Andric     m_option_group.Finalize();
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   ~CommandObjectFrameVariable() override = default;
4410b57cec5SDimitry Andric 
GetOptions()4420b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
4430b57cec5SDimitry Andric 
4449dba64beSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)4459dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
4460b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
4470b57cec5SDimitry Andric     // Arguments are the standard source file completer.
4480b57cec5SDimitry Andric     CommandCompletions::InvokeCommonCompletionCallbacks(
4490b57cec5SDimitry Andric         GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion,
4500b57cec5SDimitry Andric         request, nullptr);
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric protected:
GetScopeString(VariableSP var_sp)4540b57cec5SDimitry Andric   llvm::StringRef GetScopeString(VariableSP var_sp) {
4550b57cec5SDimitry Andric     if (!var_sp)
456*5f7ddb14SDimitry Andric       return llvm::StringRef();
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric     switch (var_sp->GetScope()) {
4590b57cec5SDimitry Andric     case eValueTypeVariableGlobal:
4600b57cec5SDimitry Andric       return "GLOBAL: ";
4610b57cec5SDimitry Andric     case eValueTypeVariableStatic:
4620b57cec5SDimitry Andric       return "STATIC: ";
4630b57cec5SDimitry Andric     case eValueTypeVariableArgument:
4640b57cec5SDimitry Andric       return "ARG: ";
4650b57cec5SDimitry Andric     case eValueTypeVariableLocal:
4660b57cec5SDimitry Andric       return "LOCAL: ";
4670b57cec5SDimitry Andric     case eValueTypeVariableThreadLocal:
4680b57cec5SDimitry Andric       return "THREAD: ";
4690b57cec5SDimitry Andric     default:
4700b57cec5SDimitry Andric       break;
4710b57cec5SDimitry Andric     }
4720b57cec5SDimitry Andric 
473*5f7ddb14SDimitry Andric     return llvm::StringRef();
4740b57cec5SDimitry Andric   }
4750b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)4760b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
4770b57cec5SDimitry Andric     // No need to check "frame" for validity as eCommandRequiresFrame ensures
4780b57cec5SDimitry Andric     // it is valid
4790b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric     Stream &s = result.GetOutputStream();
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     // Be careful about the stack frame, if any summary formatter runs code, it
4840b57cec5SDimitry Andric     // might clear the StackFrameList for the thread.  So hold onto a shared
4850b57cec5SDimitry Andric     // pointer to the frame so it stays alive.
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric     VariableList *variable_list =
4880b57cec5SDimitry Andric         frame->GetVariableList(m_option_variable.show_globals);
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric     VariableSP var_sp;
4910b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric     TypeSummaryImplSP summary_format_sp;
4940b57cec5SDimitry Andric     if (!m_option_variable.summary.IsCurrentValueEmpty())
4950b57cec5SDimitry Andric       DataVisualization::NamedSummaryFormats::GetSummaryFormat(
4960b57cec5SDimitry Andric           ConstString(m_option_variable.summary.GetCurrentValue()),
4970b57cec5SDimitry Andric           summary_format_sp);
4980b57cec5SDimitry Andric     else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
4990b57cec5SDimitry Andric       summary_format_sp = std::make_shared<StringSummaryFormat>(
5000b57cec5SDimitry Andric           TypeSummaryImpl::Flags(),
5010b57cec5SDimitry Andric           m_option_variable.summary_string.GetCurrentValue());
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
5040b57cec5SDimitry Andric         eLanguageRuntimeDescriptionDisplayVerbosityFull, eFormatDefault,
5050b57cec5SDimitry Andric         summary_format_sp));
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric     const SymbolContext &sym_ctx =
5080b57cec5SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction);
5090b57cec5SDimitry Andric     if (sym_ctx.function && sym_ctx.function->IsTopLevelFunction())
5100b57cec5SDimitry Andric       m_option_variable.show_globals = true;
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric     if (variable_list) {
5130b57cec5SDimitry Andric       const Format format = m_option_format.GetFormat();
5140b57cec5SDimitry Andric       options.SetFormat(format);
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric       if (!command.empty()) {
5170b57cec5SDimitry Andric         VariableList regex_var_list;
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric         // If we have any args to the variable command, we will make variable
5200b57cec5SDimitry Andric         // objects from them...
5210b57cec5SDimitry Andric         for (auto &entry : command) {
5220b57cec5SDimitry Andric           if (m_option_variable.use_regex) {
5230b57cec5SDimitry Andric             const size_t regex_start_index = regex_var_list.GetSize();
5249dba64beSDimitry Andric             llvm::StringRef name_str = entry.ref();
5250b57cec5SDimitry Andric             RegularExpression regex(name_str);
5269dba64beSDimitry Andric             if (regex.IsValid()) {
5270b57cec5SDimitry Andric               size_t num_matches = 0;
5280b57cec5SDimitry Andric               const size_t num_new_regex_vars =
5290b57cec5SDimitry Andric                   variable_list->AppendVariablesIfUnique(regex, regex_var_list,
5300b57cec5SDimitry Andric                                                          num_matches);
5310b57cec5SDimitry Andric               if (num_new_regex_vars > 0) {
5320b57cec5SDimitry Andric                 for (size_t regex_idx = regex_start_index,
5330b57cec5SDimitry Andric                             end_index = regex_var_list.GetSize();
5340b57cec5SDimitry Andric                      regex_idx < end_index; ++regex_idx) {
5350b57cec5SDimitry Andric                   var_sp = regex_var_list.GetVariableAtIndex(regex_idx);
5360b57cec5SDimitry Andric                   if (var_sp) {
5370b57cec5SDimitry Andric                     valobj_sp = frame->GetValueObjectForFrameVariable(
5380b57cec5SDimitry Andric                         var_sp, m_varobj_options.use_dynamic);
5390b57cec5SDimitry Andric                     if (valobj_sp) {
5400b57cec5SDimitry Andric                       std::string scope_string;
5410b57cec5SDimitry Andric                       if (m_option_variable.show_scope)
5420b57cec5SDimitry Andric                         scope_string = GetScopeString(var_sp).str();
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric                       if (!scope_string.empty())
5450b57cec5SDimitry Andric                         s.PutCString(scope_string);
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric                       if (m_option_variable.show_decl &&
5480b57cec5SDimitry Andric                           var_sp->GetDeclaration().GetFile()) {
5490b57cec5SDimitry Andric                         bool show_fullpaths = false;
5500b57cec5SDimitry Andric                         bool show_module = true;
5510b57cec5SDimitry Andric                         if (var_sp->DumpDeclaration(&s, show_fullpaths,
5520b57cec5SDimitry Andric                                                     show_module))
5530b57cec5SDimitry Andric                           s.PutCString(": ");
5540b57cec5SDimitry Andric                       }
5550b57cec5SDimitry Andric                       valobj_sp->Dump(result.GetOutputStream(), options);
5560b57cec5SDimitry Andric                     }
5570b57cec5SDimitry Andric                   }
5580b57cec5SDimitry Andric                 }
5590b57cec5SDimitry Andric               } else if (num_matches == 0) {
5600b57cec5SDimitry Andric                 result.GetErrorStream().Printf("error: no variables matched "
5610b57cec5SDimitry Andric                                                "the regular expression '%s'.\n",
5620b57cec5SDimitry Andric                                                entry.c_str());
5630b57cec5SDimitry Andric               }
5640b57cec5SDimitry Andric             } else {
5659dba64beSDimitry Andric               if (llvm::Error err = regex.GetError())
5669dba64beSDimitry Andric                 result.GetErrorStream().Printf(
5679dba64beSDimitry Andric                     "error: %s\n", llvm::toString(std::move(err)).c_str());
5680b57cec5SDimitry Andric               else
5690b57cec5SDimitry Andric                 result.GetErrorStream().Printf(
5700b57cec5SDimitry Andric                     "error: unknown regex error when compiling '%s'\n",
5710b57cec5SDimitry Andric                     entry.c_str());
5720b57cec5SDimitry Andric             }
5730b57cec5SDimitry Andric           } else // No regex, either exact variable names or variable
5740b57cec5SDimitry Andric                  // expressions.
5750b57cec5SDimitry Andric           {
5760b57cec5SDimitry Andric             Status error;
5770b57cec5SDimitry Andric             uint32_t expr_path_options =
5780b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionCheckPtrVsMember |
5790b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess |
5800b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsInspectAnonymousUnions;
5810b57cec5SDimitry Andric             lldb::VariableSP var_sp;
5820b57cec5SDimitry Andric             valobj_sp = frame->GetValueForVariableExpressionPath(
5839dba64beSDimitry Andric                 entry.ref(), m_varobj_options.use_dynamic, expr_path_options,
5840b57cec5SDimitry Andric                 var_sp, error);
5850b57cec5SDimitry Andric             if (valobj_sp) {
5860b57cec5SDimitry Andric               std::string scope_string;
5870b57cec5SDimitry Andric               if (m_option_variable.show_scope)
5880b57cec5SDimitry Andric                 scope_string = GetScopeString(var_sp).str();
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric               if (!scope_string.empty())
5910b57cec5SDimitry Andric                 s.PutCString(scope_string);
5920b57cec5SDimitry Andric               if (m_option_variable.show_decl && var_sp &&
5930b57cec5SDimitry Andric                   var_sp->GetDeclaration().GetFile()) {
5940b57cec5SDimitry Andric                 var_sp->GetDeclaration().DumpStopContext(&s, false);
5950b57cec5SDimitry Andric                 s.PutCString(": ");
5960b57cec5SDimitry Andric               }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric               options.SetFormat(format);
5990b57cec5SDimitry Andric               options.SetVariableFormatDisplayLanguage(
6000b57cec5SDimitry Andric                   valobj_sp->GetPreferredDisplayLanguage());
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric               Stream &output_stream = result.GetOutputStream();
6030b57cec5SDimitry Andric               options.SetRootValueObjectName(
6040b57cec5SDimitry Andric                   valobj_sp->GetParent() ? entry.c_str() : nullptr);
6050b57cec5SDimitry Andric               valobj_sp->Dump(output_stream, options);
6060b57cec5SDimitry Andric             } else {
6070b57cec5SDimitry Andric               const char *error_cstr = error.AsCString(nullptr);
6080b57cec5SDimitry Andric               if (error_cstr)
6090b57cec5SDimitry Andric                 result.GetErrorStream().Printf("error: %s\n", error_cstr);
6100b57cec5SDimitry Andric               else
6110b57cec5SDimitry Andric                 result.GetErrorStream().Printf("error: unable to find any "
6120b57cec5SDimitry Andric                                                "variable expression path that "
6130b57cec5SDimitry Andric                                                "matches '%s'.\n",
6140b57cec5SDimitry Andric                                                entry.c_str());
6150b57cec5SDimitry Andric             }
6160b57cec5SDimitry Andric           }
6170b57cec5SDimitry Andric         }
6180b57cec5SDimitry Andric       } else // No command arg specified.  Use variable_list, instead.
6190b57cec5SDimitry Andric       {
6200b57cec5SDimitry Andric         const size_t num_variables = variable_list->GetSize();
6210b57cec5SDimitry Andric         if (num_variables > 0) {
6220b57cec5SDimitry Andric           for (size_t i = 0; i < num_variables; i++) {
6230b57cec5SDimitry Andric             var_sp = variable_list->GetVariableAtIndex(i);
6240b57cec5SDimitry Andric             switch (var_sp->GetScope()) {
6250b57cec5SDimitry Andric             case eValueTypeVariableGlobal:
6260b57cec5SDimitry Andric               if (!m_option_variable.show_globals)
6270b57cec5SDimitry Andric                 continue;
6280b57cec5SDimitry Andric               break;
6290b57cec5SDimitry Andric             case eValueTypeVariableStatic:
6300b57cec5SDimitry Andric               if (!m_option_variable.show_globals)
6310b57cec5SDimitry Andric                 continue;
6320b57cec5SDimitry Andric               break;
6330b57cec5SDimitry Andric             case eValueTypeVariableArgument:
6340b57cec5SDimitry Andric               if (!m_option_variable.show_args)
6350b57cec5SDimitry Andric                 continue;
6360b57cec5SDimitry Andric               break;
6370b57cec5SDimitry Andric             case eValueTypeVariableLocal:
6380b57cec5SDimitry Andric               if (!m_option_variable.show_locals)
6390b57cec5SDimitry Andric                 continue;
6400b57cec5SDimitry Andric               break;
6410b57cec5SDimitry Andric             default:
6420b57cec5SDimitry Andric               continue;
6430b57cec5SDimitry Andric               break;
6440b57cec5SDimitry Andric             }
6450b57cec5SDimitry Andric             std::string scope_string;
6460b57cec5SDimitry Andric             if (m_option_variable.show_scope)
6470b57cec5SDimitry Andric               scope_string = GetScopeString(var_sp).str();
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric             // Use the variable object code to make sure we are using the same
6500b57cec5SDimitry Andric             // APIs as the public API will be using...
6510b57cec5SDimitry Andric             valobj_sp = frame->GetValueObjectForFrameVariable(
6520b57cec5SDimitry Andric                 var_sp, m_varobj_options.use_dynamic);
6530b57cec5SDimitry Andric             if (valobj_sp) {
6540b57cec5SDimitry Andric               // When dumping all variables, don't print any variables that are
6550b57cec5SDimitry Andric               // not in scope to avoid extra unneeded output
6560b57cec5SDimitry Andric               if (valobj_sp->IsInScope()) {
6570b57cec5SDimitry Andric                 if (!valobj_sp->GetTargetSP()
6580b57cec5SDimitry Andric                          ->GetDisplayRuntimeSupportValues() &&
6590b57cec5SDimitry Andric                     valobj_sp->IsRuntimeSupportValue())
6600b57cec5SDimitry Andric                   continue;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric                 if (!scope_string.empty())
6630b57cec5SDimitry Andric                   s.PutCString(scope_string);
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric                 if (m_option_variable.show_decl &&
6660b57cec5SDimitry Andric                     var_sp->GetDeclaration().GetFile()) {
6670b57cec5SDimitry Andric                   var_sp->GetDeclaration().DumpStopContext(&s, false);
6680b57cec5SDimitry Andric                   s.PutCString(": ");
6690b57cec5SDimitry Andric                 }
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric                 options.SetFormat(format);
6720b57cec5SDimitry Andric                 options.SetVariableFormatDisplayLanguage(
6730b57cec5SDimitry Andric                     valobj_sp->GetPreferredDisplayLanguage());
6740b57cec5SDimitry Andric                 options.SetRootValueObjectName(
6750b57cec5SDimitry Andric                     var_sp ? var_sp->GetName().AsCString() : nullptr);
6760b57cec5SDimitry Andric                 valobj_sp->Dump(result.GetOutputStream(), options);
6770b57cec5SDimitry Andric               }
6780b57cec5SDimitry Andric             }
6790b57cec5SDimitry Andric           }
6800b57cec5SDimitry Andric         }
6810b57cec5SDimitry Andric       }
6820b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
6830b57cec5SDimitry Andric     }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric     if (m_option_variable.show_recognized_args) {
6860b57cec5SDimitry Andric       auto recognized_frame = frame->GetRecognizedFrame();
6870b57cec5SDimitry Andric       if (recognized_frame) {
6880b57cec5SDimitry Andric         ValueObjectListSP recognized_arg_list =
6890b57cec5SDimitry Andric             recognized_frame->GetRecognizedArguments();
6900b57cec5SDimitry Andric         if (recognized_arg_list) {
6910b57cec5SDimitry Andric           for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
6920b57cec5SDimitry Andric             options.SetFormat(m_option_format.GetFormat());
6930b57cec5SDimitry Andric             options.SetVariableFormatDisplayLanguage(
6940b57cec5SDimitry Andric                 rec_value_sp->GetPreferredDisplayLanguage());
6950b57cec5SDimitry Andric             options.SetRootValueObjectName(rec_value_sp->GetName().AsCString());
6960b57cec5SDimitry Andric             rec_value_sp->Dump(result.GetOutputStream(), options);
6970b57cec5SDimitry Andric           }
6980b57cec5SDimitry Andric         }
6990b57cec5SDimitry Andric       }
7000b57cec5SDimitry Andric     }
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric     if (m_interpreter.TruncationWarningNecessary()) {
7030b57cec5SDimitry Andric       result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
7040b57cec5SDimitry Andric                                       m_cmd_name.c_str());
7050b57cec5SDimitry Andric       m_interpreter.TruncationWarningGiven();
7060b57cec5SDimitry Andric     }
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric     // Increment statistics.
7090b57cec5SDimitry Andric     bool res = result.Succeeded();
7109dba64beSDimitry Andric     Target &target = GetSelectedOrDummyTarget();
7110b57cec5SDimitry Andric     if (res)
7129dba64beSDimitry Andric       target.IncrementStats(StatisticKind::FrameVarSuccess);
7130b57cec5SDimitry Andric     else
7149dba64beSDimitry Andric       target.IncrementStats(StatisticKind::FrameVarFailure);
7150b57cec5SDimitry Andric     return res;
7160b57cec5SDimitry Andric   }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
7190b57cec5SDimitry Andric   OptionGroupVariable m_option_variable;
7200b57cec5SDimitry Andric   OptionGroupFormat m_option_format;
7210b57cec5SDimitry Andric   OptionGroupValueObjectDisplay m_varobj_options;
7220b57cec5SDimitry Andric };
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric #pragma mark CommandObjectFrameRecognizer
7250b57cec5SDimitry Andric 
7269dba64beSDimitry Andric #define LLDB_OPTIONS_frame_recognizer_add
7279dba64beSDimitry Andric #include "CommandOptions.inc"
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric class CommandObjectFrameRecognizerAdd : public CommandObjectParsed {
7300b57cec5SDimitry Andric private:
7310b57cec5SDimitry Andric   class CommandOptions : public Options {
7320b57cec5SDimitry Andric   public:
CommandOptions()7330b57cec5SDimitry Andric     CommandOptions() : Options() {}
7340b57cec5SDimitry Andric     ~CommandOptions() override = default;
7350b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)7360b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7370b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7380b57cec5SDimitry Andric       Status error;
7390b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric       switch (short_option) {
7420b57cec5SDimitry Andric       case 'l':
7430b57cec5SDimitry Andric         m_class_name = std::string(option_arg);
7440b57cec5SDimitry Andric         break;
7450b57cec5SDimitry Andric       case 's':
7460b57cec5SDimitry Andric         m_module = std::string(option_arg);
7470b57cec5SDimitry Andric         break;
7480b57cec5SDimitry Andric       case 'n':
7495ffd83dbSDimitry Andric         m_symbols.push_back(std::string(option_arg));
7500b57cec5SDimitry Andric         break;
7510b57cec5SDimitry Andric       case 'x':
7520b57cec5SDimitry Andric         m_regex = true;
7530b57cec5SDimitry Andric         break;
7540b57cec5SDimitry Andric       default:
7559dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7560b57cec5SDimitry Andric       }
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric       return error;
7590b57cec5SDimitry Andric     }
7600b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)7610b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7620b57cec5SDimitry Andric       m_module = "";
7635ffd83dbSDimitry Andric       m_symbols.clear();
7640b57cec5SDimitry Andric       m_class_name = "";
7650b57cec5SDimitry Andric       m_regex = false;
7660b57cec5SDimitry Andric     }
7670b57cec5SDimitry Andric 
GetDefinitions()7680b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
7690b57cec5SDimitry Andric       return llvm::makeArrayRef(g_frame_recognizer_add_options);
7700b57cec5SDimitry Andric     }
7710b57cec5SDimitry Andric 
7720b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
7730b57cec5SDimitry Andric     std::string m_class_name;
7740b57cec5SDimitry Andric     std::string m_module;
7755ffd83dbSDimitry Andric     std::vector<std::string> m_symbols;
7760b57cec5SDimitry Andric     bool m_regex;
7770b57cec5SDimitry Andric   };
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric   CommandOptions m_options;
7800b57cec5SDimitry Andric 
GetOptions()7810b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric protected:
7840b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override;
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerAdd(CommandInterpreter & interpreter)7870b57cec5SDimitry Andric   CommandObjectFrameRecognizerAdd(CommandInterpreter &interpreter)
7880b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer add",
7890b57cec5SDimitry Andric                             "Add a new frame recognizer.", nullptr),
7900b57cec5SDimitry Andric         m_options() {
7910b57cec5SDimitry Andric     SetHelpLong(R"(
7920b57cec5SDimitry Andric Frame recognizers allow for retrieving information about special frames based on
7930b57cec5SDimitry Andric ABI, arguments or other special properties of that frame, even without source
7940b57cec5SDimitry Andric code or debug info. Currently, one use case is to extract function arguments
7950b57cec5SDimitry Andric that would otherwise be unaccesible, or augment existing arguments.
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric Adding a custom frame recognizer is possible by implementing a Python class
7980b57cec5SDimitry Andric and using the 'frame recognizer add' command. The Python class should have a
7990b57cec5SDimitry Andric 'get_recognized_arguments' method and it will receive an argument of type
8000b57cec5SDimitry Andric lldb.SBFrame representing the current frame that we are trying to recognize.
8010b57cec5SDimitry Andric The method should return a (possibly empty) list of lldb.SBValue objects that
8020b57cec5SDimitry Andric represent the recognized arguments.
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric An example of a recognizer that retrieves the file descriptor values from libc
8050b57cec5SDimitry Andric functions 'read', 'write' and 'close' follows:
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   class LibcFdRecognizer(object):
8080b57cec5SDimitry Andric     def get_recognized_arguments(self, frame):
8090b57cec5SDimitry Andric       if frame.name in ["read", "write", "close"]:
8100b57cec5SDimitry Andric         fd = frame.EvaluateExpression("$arg1").unsigned
8110b57cec5SDimitry Andric         value = lldb.target.CreateValueFromExpression("fd", "(int)%d" % fd)
8120b57cec5SDimitry Andric         return [value]
8130b57cec5SDimitry Andric       return []
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric The file containing this implementation can be imported via 'command script
8160b57cec5SDimitry Andric import' and then we can register this recognizer with 'frame recognizer add'.
8170b57cec5SDimitry Andric It's important to restrict the recognizer to the libc library (which is
8180b57cec5SDimitry Andric libsystem_kernel.dylib on macOS) to avoid matching functions with the same name
8190b57cec5SDimitry Andric in other modules:
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric (lldb) command script import .../fd_recognizer.py
8220b57cec5SDimitry Andric (lldb) frame recognizer add -l fd_recognizer.LibcFdRecognizer -n read -s libsystem_kernel.dylib
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric When the program is stopped at the beginning of the 'read' function in libc, we
8250b57cec5SDimitry Andric can view the recognizer arguments in 'frame variable':
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric (lldb) b read
8280b57cec5SDimitry Andric (lldb) r
8290b57cec5SDimitry Andric Process 1234 stopped
8300b57cec5SDimitry Andric * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.3
8310b57cec5SDimitry Andric     frame #0: 0x00007fff06013ca0 libsystem_kernel.dylib`read
8320b57cec5SDimitry Andric (lldb) frame variable
8330b57cec5SDimitry Andric (int) fd = 3
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric     )");
8360b57cec5SDimitry Andric   }
8370b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerAdd() override = default;
8380b57cec5SDimitry Andric };
8390b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)8400b57cec5SDimitry Andric bool CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
8410b57cec5SDimitry Andric                                                 CommandReturnObject &result) {
842480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
8430b57cec5SDimitry Andric   if (m_options.m_class_name.empty()) {
8440b57cec5SDimitry Andric     result.AppendErrorWithFormat(
8450b57cec5SDimitry Andric         "%s needs a Python class name (-l argument).\n", m_cmd_name.c_str());
8460b57cec5SDimitry Andric     return false;
8470b57cec5SDimitry Andric   }
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric   if (m_options.m_module.empty()) {
8500b57cec5SDimitry Andric     result.AppendErrorWithFormat("%s needs a module name (-s argument).\n",
8510b57cec5SDimitry Andric                                  m_cmd_name.c_str());
8520b57cec5SDimitry Andric     return false;
8530b57cec5SDimitry Andric   }
8540b57cec5SDimitry Andric 
8555ffd83dbSDimitry Andric   if (m_options.m_symbols.empty()) {
8565ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8575ffd83dbSDimitry Andric         "%s needs at least one symbol name (-n argument).\n",
8585ffd83dbSDimitry Andric         m_cmd_name.c_str());
8595ffd83dbSDimitry Andric     return false;
8605ffd83dbSDimitry Andric   }
8615ffd83dbSDimitry Andric 
8625ffd83dbSDimitry Andric   if (m_options.m_regex && m_options.m_symbols.size() > 1) {
8635ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8645ffd83dbSDimitry Andric         "%s needs only one symbol regular expression (-n argument).\n",
8650b57cec5SDimitry Andric         m_cmd_name.c_str());
8660b57cec5SDimitry Andric     return false;
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric   if (interpreter &&
8720b57cec5SDimitry Andric       !interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
873480093f4SDimitry Andric     result.AppendWarning("The provided class does not exist - please define it "
8740b57cec5SDimitry Andric                          "before attempting to use this frame recognizer");
8750b57cec5SDimitry Andric   }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   StackFrameRecognizerSP recognizer_sp =
8780b57cec5SDimitry Andric       StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
8790b57cec5SDimitry Andric           interpreter, m_options.m_class_name.c_str()));
8800b57cec5SDimitry Andric   if (m_options.m_regex) {
8810b57cec5SDimitry Andric     auto module =
8820b57cec5SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_module));
8830b57cec5SDimitry Andric     auto func =
8845ffd83dbSDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
885af732203SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
886af732203SDimitry Andric         recognizer_sp, module, func);
8870b57cec5SDimitry Andric   } else {
8880b57cec5SDimitry Andric     auto module = ConstString(m_options.m_module);
8895ffd83dbSDimitry Andric     std::vector<ConstString> symbols(m_options.m_symbols.begin(),
8905ffd83dbSDimitry Andric                                      m_options.m_symbols.end());
891af732203SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
892af732203SDimitry Andric         recognizer_sp, module, symbols);
8930b57cec5SDimitry Andric   }
8940b57cec5SDimitry Andric #endif
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishNoResult);
8970b57cec5SDimitry Andric   return result.Succeeded();
8980b57cec5SDimitry Andric }
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
9010b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerClear(CommandInterpreter & interpreter)9020b57cec5SDimitry Andric   CommandObjectFrameRecognizerClear(CommandInterpreter &interpreter)
9030b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer clear",
9040b57cec5SDimitry Andric                             "Delete all frame recognizers.", nullptr) {}
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerClear() override = default;
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)9090b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
910af732203SDimitry Andric     GetSelectedOrDummyTarget()
911af732203SDimitry Andric         .GetFrameRecognizerManager()
912af732203SDimitry Andric         .RemoveAllRecognizers();
9130b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9140b57cec5SDimitry Andric     return result.Succeeded();
9150b57cec5SDimitry Andric   }
9160b57cec5SDimitry Andric };
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
9190b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerDelete(CommandInterpreter & interpreter)9200b57cec5SDimitry Andric   CommandObjectFrameRecognizerDelete(CommandInterpreter &interpreter)
9210b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer delete",
9220b57cec5SDimitry Andric                             "Delete an existing frame recognizer.", nullptr) {}
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerDelete() override = default;
9250b57cec5SDimitry Andric 
9265ffd83dbSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)9275ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
9285ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
9295ffd83dbSDimitry Andric     if (request.GetCursorIndex() != 0)
9305ffd83dbSDimitry Andric       return;
9315ffd83dbSDimitry Andric 
932af732203SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
9335ffd83dbSDimitry Andric         [&request](uint32_t rid, std::string rname, std::string module,
9345ffd83dbSDimitry Andric                    llvm::ArrayRef<lldb_private::ConstString> symbols,
9355ffd83dbSDimitry Andric                    bool regexp) {
9365ffd83dbSDimitry Andric           StreamString strm;
9375ffd83dbSDimitry Andric           if (rname.empty())
9385ffd83dbSDimitry Andric             rname = "(internal)";
9395ffd83dbSDimitry Andric 
9405ffd83dbSDimitry Andric           strm << rname;
9415ffd83dbSDimitry Andric           if (!module.empty())
9425ffd83dbSDimitry Andric             strm << ", module " << module;
9435ffd83dbSDimitry Andric           if (!symbols.empty())
9445ffd83dbSDimitry Andric             for (auto &symbol : symbols)
9455ffd83dbSDimitry Andric               strm << ", symbol " << symbol;
9465ffd83dbSDimitry Andric           if (regexp)
9475ffd83dbSDimitry Andric             strm << " (regexp)";
9485ffd83dbSDimitry Andric 
9495ffd83dbSDimitry Andric           request.TryCompleteCurrentArg(std::to_string(rid), strm.GetString());
9505ffd83dbSDimitry Andric         });
9515ffd83dbSDimitry Andric   }
9525ffd83dbSDimitry Andric 
9530b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)9540b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
9550b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
9560b57cec5SDimitry Andric       if (!m_interpreter.Confirm(
9570b57cec5SDimitry Andric               "About to delete all frame recognizers, do you want to do that?",
9580b57cec5SDimitry Andric               true)) {
9590b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
9600b57cec5SDimitry Andric         return false;
9610b57cec5SDimitry Andric       }
9620b57cec5SDimitry Andric 
963af732203SDimitry Andric       GetSelectedOrDummyTarget()
964af732203SDimitry Andric           .GetFrameRecognizerManager()
965af732203SDimitry Andric           .RemoveAllRecognizers();
9660b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
9670b57cec5SDimitry Andric       return result.Succeeded();
9680b57cec5SDimitry Andric     }
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
9710b57cec5SDimitry Andric       result.AppendErrorWithFormat("'%s' takes zero or one arguments.\n",
9720b57cec5SDimitry Andric                                    m_cmd_name.c_str());
9730b57cec5SDimitry Andric       return false;
9740b57cec5SDimitry Andric     }
9750b57cec5SDimitry Andric 
9765ffd83dbSDimitry Andric     uint32_t recognizer_id;
9775ffd83dbSDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), recognizer_id)) {
9785ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
9795ffd83dbSDimitry Andric                                    command.GetArgumentAtIndex(0));
9805ffd83dbSDimitry Andric       return false;
9815ffd83dbSDimitry Andric     }
9820b57cec5SDimitry Andric 
983af732203SDimitry Andric     if (!GetSelectedOrDummyTarget()
984af732203SDimitry Andric              .GetFrameRecognizerManager()
985af732203SDimitry Andric              .RemoveRecognizerWithID(recognizer_id)) {
986af732203SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
987af732203SDimitry Andric                                    command.GetArgumentAtIndex(0));
988af732203SDimitry Andric       return false;
989af732203SDimitry Andric     }
9900b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9910b57cec5SDimitry Andric     return result.Succeeded();
9920b57cec5SDimitry Andric   }
9930b57cec5SDimitry Andric };
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric class CommandObjectFrameRecognizerList : public CommandObjectParsed {
9960b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerList(CommandInterpreter & interpreter)9970b57cec5SDimitry Andric   CommandObjectFrameRecognizerList(CommandInterpreter &interpreter)
9980b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer list",
9990b57cec5SDimitry Andric                             "Show a list of active frame recognizers.",
10000b57cec5SDimitry Andric                             nullptr) {}
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerList() override = default;
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)10050b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
10060b57cec5SDimitry Andric     bool any_printed = false;
1007af732203SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
10085ffd83dbSDimitry Andric         [&result, &any_printed](
10095ffd83dbSDimitry Andric             uint32_t recognizer_id, std::string name, std::string module,
10105ffd83dbSDimitry Andric             llvm::ArrayRef<ConstString> symbols, bool regexp) {
10115ffd83dbSDimitry Andric           Stream &stream = result.GetOutputStream();
10125ffd83dbSDimitry Andric 
10135ffd83dbSDimitry Andric           if (name.empty())
1014480093f4SDimitry Andric             name = "(internal)";
10155ffd83dbSDimitry Andric 
10165ffd83dbSDimitry Andric           stream << std::to_string(recognizer_id) << ": " << name;
10175ffd83dbSDimitry Andric           if (!module.empty())
10185ffd83dbSDimitry Andric             stream << ", module " << module;
10195ffd83dbSDimitry Andric           if (!symbols.empty())
10205ffd83dbSDimitry Andric             for (auto &symbol : symbols)
10215ffd83dbSDimitry Andric               stream << ", symbol " << symbol;
10225ffd83dbSDimitry Andric           if (regexp)
10235ffd83dbSDimitry Andric             stream << " (regexp)";
10245ffd83dbSDimitry Andric 
10255ffd83dbSDimitry Andric           stream.EOL();
10265ffd83dbSDimitry Andric           stream.Flush();
10275ffd83dbSDimitry Andric 
10280b57cec5SDimitry Andric           any_printed = true;
10290b57cec5SDimitry Andric         });
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric     if (any_printed)
10320b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
10330b57cec5SDimitry Andric     else {
10340b57cec5SDimitry Andric       result.GetOutputStream().PutCString("no matching results found.\n");
10350b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
10360b57cec5SDimitry Andric     }
10370b57cec5SDimitry Andric     return result.Succeeded();
10380b57cec5SDimitry Andric   }
10390b57cec5SDimitry Andric };
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
10420b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerInfo(CommandInterpreter & interpreter)10430b57cec5SDimitry Andric   CommandObjectFrameRecognizerInfo(CommandInterpreter &interpreter)
10440b57cec5SDimitry Andric       : CommandObjectParsed(
10450b57cec5SDimitry Andric             interpreter, "frame recognizer info",
10460b57cec5SDimitry Andric             "Show which frame recognizer is applied a stack frame (if any).",
10470b57cec5SDimitry Andric             nullptr) {
10480b57cec5SDimitry Andric     CommandArgumentEntry arg;
10490b57cec5SDimitry Andric     CommandArgumentData index_arg;
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
10520b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
10530b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatPlain;
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
10560b57cec5SDimitry Andric     // argument entry.
10570b57cec5SDimitry Andric     arg.push_back(index_arg);
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
10600b57cec5SDimitry Andric     m_arguments.push_back(arg);
10610b57cec5SDimitry Andric   }
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerInfo() override = default;
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)10660b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
10675ffd83dbSDimitry Andric     const char *frame_index_str = command.GetArgumentAtIndex(0);
10685ffd83dbSDimitry Andric     uint32_t frame_index;
10695ffd83dbSDimitry Andric     if (!llvm::to_integer(frame_index_str, frame_index)) {
10705ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid frame index.",
10715ffd83dbSDimitry Andric                                    frame_index_str);
10725ffd83dbSDimitry Andric       return false;
10735ffd83dbSDimitry Andric     }
10745ffd83dbSDimitry Andric 
10750b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
10760b57cec5SDimitry Andric     if (process == nullptr) {
10770b57cec5SDimitry Andric       result.AppendError("no process");
10780b57cec5SDimitry Andric       return false;
10790b57cec5SDimitry Andric     }
10800b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
10810b57cec5SDimitry Andric     if (thread == nullptr) {
10820b57cec5SDimitry Andric       result.AppendError("no thread");
10830b57cec5SDimitry Andric       return false;
10840b57cec5SDimitry Andric     }
10850b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
10860b57cec5SDimitry Andric       result.AppendErrorWithFormat(
10870b57cec5SDimitry Andric           "'%s' takes exactly one frame index argument.\n", m_cmd_name.c_str());
10880b57cec5SDimitry Andric       return false;
10890b57cec5SDimitry Andric     }
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric     StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_index);
10920b57cec5SDimitry Andric     if (!frame_sp) {
10930b57cec5SDimitry Andric       result.AppendErrorWithFormat("no frame with index %u", frame_index);
10940b57cec5SDimitry Andric       return false;
10950b57cec5SDimitry Andric     }
10960b57cec5SDimitry Andric 
1097af732203SDimitry Andric     auto recognizer = GetSelectedOrDummyTarget()
1098af732203SDimitry Andric                           .GetFrameRecognizerManager()
1099af732203SDimitry Andric                           .GetRecognizerForFrame(frame_sp);
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
11020b57cec5SDimitry Andric     output_stream.Printf("frame %d ", frame_index);
11030b57cec5SDimitry Andric     if (recognizer) {
11040b57cec5SDimitry Andric       output_stream << "is recognized by ";
11050b57cec5SDimitry Andric       output_stream << recognizer->GetName();
11060b57cec5SDimitry Andric     } else {
11070b57cec5SDimitry Andric       output_stream << "not recognized by any recognizer";
11080b57cec5SDimitry Andric     }
11090b57cec5SDimitry Andric     output_stream.EOL();
11100b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
11110b57cec5SDimitry Andric     return result.Succeeded();
11120b57cec5SDimitry Andric   }
11130b57cec5SDimitry Andric };
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric class CommandObjectFrameRecognizer : public CommandObjectMultiword {
11160b57cec5SDimitry Andric public:
CommandObjectFrameRecognizer(CommandInterpreter & interpreter)11170b57cec5SDimitry Andric   CommandObjectFrameRecognizer(CommandInterpreter &interpreter)
11180b57cec5SDimitry Andric       : CommandObjectMultiword(
11190b57cec5SDimitry Andric             interpreter, "frame recognizer",
11200b57cec5SDimitry Andric             "Commands for editing and viewing frame recognizers.",
11210b57cec5SDimitry Andric             "frame recognizer [<sub-command-options>] ") {
1122480093f4SDimitry Andric     LoadSubCommand("add", CommandObjectSP(new CommandObjectFrameRecognizerAdd(
1123480093f4SDimitry Andric                               interpreter)));
11240b57cec5SDimitry Andric     LoadSubCommand(
11250b57cec5SDimitry Andric         "clear",
11260b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerClear(interpreter)));
11270b57cec5SDimitry Andric     LoadSubCommand(
11280b57cec5SDimitry Andric         "delete",
11290b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerDelete(interpreter)));
1130480093f4SDimitry Andric     LoadSubCommand("list", CommandObjectSP(new CommandObjectFrameRecognizerList(
1131480093f4SDimitry Andric                                interpreter)));
1132480093f4SDimitry Andric     LoadSubCommand("info", CommandObjectSP(new CommandObjectFrameRecognizerInfo(
1133480093f4SDimitry Andric                                interpreter)));
11340b57cec5SDimitry Andric   }
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric   ~CommandObjectFrameRecognizer() override = default;
11370b57cec5SDimitry Andric };
11380b57cec5SDimitry Andric 
11390b57cec5SDimitry Andric #pragma mark CommandObjectMultiwordFrame
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric // CommandObjectMultiwordFrame
11420b57cec5SDimitry Andric 
CommandObjectMultiwordFrame(CommandInterpreter & interpreter)11430b57cec5SDimitry Andric CommandObjectMultiwordFrame::CommandObjectMultiwordFrame(
11440b57cec5SDimitry Andric     CommandInterpreter &interpreter)
1145480093f4SDimitry Andric     : CommandObjectMultiword(interpreter, "frame",
1146480093f4SDimitry Andric                              "Commands for selecting and "
11470b57cec5SDimitry Andric                              "examing the current "
11480b57cec5SDimitry Andric                              "thread's stack frames.",
11490b57cec5SDimitry Andric                              "frame <subcommand> [<subcommand-options>]") {
11500b57cec5SDimitry Andric   LoadSubCommand("diagnose",
11510b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameDiagnose(interpreter)));
11520b57cec5SDimitry Andric   LoadSubCommand("info",
11530b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameInfo(interpreter)));
11540b57cec5SDimitry Andric   LoadSubCommand("select",
11550b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameSelect(interpreter)));
11560b57cec5SDimitry Andric   LoadSubCommand("variable",
11570b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameVariable(interpreter)));
1158480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
1159480093f4SDimitry Andric   LoadSubCommand("recognizer", CommandObjectSP(new CommandObjectFrameRecognizer(
1160480093f4SDimitry Andric                                    interpreter)));
11610b57cec5SDimitry Andric #endif
11620b57cec5SDimitry Andric }
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame() = default;
1165