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"
16fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
170b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
18349cc55cSDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
190b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupFormat.h"
200b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
210b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupVariable.h"
220b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
260b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
270b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
280b57cec5SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
290b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
300b57cec5SDimitry Andric #include "lldb/Target/Target.h"
310b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
320b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric #include <memory>
35bdd1243dSDimitry Andric #include <optional>
360b57cec5SDimitry Andric #include <string>
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric using namespace lldb;
390b57cec5SDimitry Andric using namespace lldb_private;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #pragma mark CommandObjectFrameDiagnose
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric // CommandObjectFrameInfo
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric // CommandObjectFrameDiagnose
460b57cec5SDimitry Andric 
479dba64beSDimitry Andric #define LLDB_OPTIONS_frame_diag
489dba64beSDimitry Andric #include "CommandOptions.inc"
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric class CommandObjectFrameDiagnose : public CommandObjectParsed {
510b57cec5SDimitry Andric public:
520b57cec5SDimitry Andric   class CommandOptions : public Options {
530b57cec5SDimitry Andric   public:
CommandOptions()5404eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric     ~CommandOptions() override = default;
570b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)580b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
590b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
600b57cec5SDimitry Andric       Status error;
610b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
620b57cec5SDimitry Andric       switch (short_option) {
630b57cec5SDimitry Andric       case 'r':
640b57cec5SDimitry Andric         reg = ConstString(option_arg);
650b57cec5SDimitry Andric         break;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric       case 'a': {
680b57cec5SDimitry Andric         address.emplace();
690b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, *address)) {
700b57cec5SDimitry Andric           address.reset();
710b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid address argument '%s'",
720b57cec5SDimitry Andric                                          option_arg.str().c_str());
730b57cec5SDimitry Andric         }
740b57cec5SDimitry Andric       } break;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric       case 'o': {
770b57cec5SDimitry Andric         offset.emplace();
780b57cec5SDimitry Andric         if (option_arg.getAsInteger(0, *offset)) {
790b57cec5SDimitry Andric           offset.reset();
800b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid offset argument '%s'",
810b57cec5SDimitry Andric                                          option_arg.str().c_str());
820b57cec5SDimitry Andric         }
830b57cec5SDimitry Andric       } break;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric       default:
869dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
870b57cec5SDimitry Andric       }
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric       return error;
900b57cec5SDimitry Andric     }
910b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)920b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
930b57cec5SDimitry Andric       address.reset();
940b57cec5SDimitry Andric       reg.reset();
950b57cec5SDimitry Andric       offset.reset();
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric 
GetDefinitions()980b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
99bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_diag_options);
1000b57cec5SDimitry Andric     }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric     // Options.
103bdd1243dSDimitry Andric     std::optional<lldb::addr_t> address;
104bdd1243dSDimitry Andric     std::optional<ConstString> reg;
105bdd1243dSDimitry Andric     std::optional<int64_t> offset;
1060b57cec5SDimitry Andric   };
1070b57cec5SDimitry Andric 
CommandObjectFrameDiagnose(CommandInterpreter & interpreter)1080b57cec5SDimitry Andric   CommandObjectFrameDiagnose(CommandInterpreter &interpreter)
1090b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame diagnose",
110349cc55cSDimitry Andric                             "Try to determine what path the current stop "
1110b57cec5SDimitry Andric                             "location used to get to a register or address",
1120b57cec5SDimitry Andric                             nullptr,
1130b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
1140b57cec5SDimitry Andric                                 eCommandProcessMustBeLaunched |
11504eeddc0SDimitry Andric                                 eCommandProcessMustBePaused) {
1160b57cec5SDimitry Andric     CommandArgumentEntry arg;
1170b57cec5SDimitry Andric     CommandArgumentData index_arg;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
1200b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
1210b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatOptional;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
1240b57cec5SDimitry Andric     // argument entry.
1250b57cec5SDimitry Andric     arg.push_back(index_arg);
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
1280b57cec5SDimitry Andric     m_arguments.push_back(arg);
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   ~CommandObjectFrameDiagnose() override = default;
1320b57cec5SDimitry Andric 
GetOptions()1330b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)136c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
1370b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
138fe013be4SDimitry Andric     StackFrameSP frame_sp = thread->GetSelectedFrame(SelectMostRelevantFrame);
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
1410b57cec5SDimitry Andric 
14281ad6265SDimitry Andric     if (m_options.address) {
14381ad6265SDimitry Andric       if (m_options.reg || m_options.offset) {
1440b57cec5SDimitry Andric         result.AppendError(
1450b57cec5SDimitry Andric             "`frame diagnose --address` is incompatible with other arguments.");
146c9157d92SDimitry Andric         return;
1470b57cec5SDimitry Andric       }
148bdd1243dSDimitry Andric       valobj_sp = frame_sp->GuessValueForAddress(*m_options.address);
14981ad6265SDimitry Andric     } else if (m_options.reg) {
1500b57cec5SDimitry Andric       valobj_sp = frame_sp->GuessValueForRegisterAndOffset(
151bdd1243dSDimitry Andric           *m_options.reg, m_options.offset.value_or(0));
1520b57cec5SDimitry Andric     } else {
1530b57cec5SDimitry Andric       StopInfoSP stop_info_sp = thread->GetStopInfo();
1540b57cec5SDimitry Andric       if (!stop_info_sp) {
1550b57cec5SDimitry Andric         result.AppendError("No arguments provided, and no stop info.");
156c9157d92SDimitry Andric         return;
1570b57cec5SDimitry Andric       }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric       valobj_sp = StopInfo::GetCrashingDereference(stop_info_sp);
1600b57cec5SDimitry Andric     }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric     if (!valobj_sp) {
1630b57cec5SDimitry Andric       result.AppendError("No diagnosis available.");
164c9157d92SDimitry Andric       return;
1650b57cec5SDimitry Andric     }
1660b57cec5SDimitry Andric 
167480093f4SDimitry Andric     DumpValueObjectOptions::DeclPrintingHelper helper =
168480093f4SDimitry Andric         [&valobj_sp](ConstString type, ConstString var,
169480093f4SDimitry Andric                      const DumpValueObjectOptions &opts,
1700b57cec5SDimitry Andric                      Stream &stream) -> bool {
1710b57cec5SDimitry Andric       const ValueObject::GetExpressionPathFormat format = ValueObject::
1720b57cec5SDimitry Andric           GetExpressionPathFormat::eGetExpressionPathFormatHonorPointers;
1735ffd83dbSDimitry Andric       valobj_sp->GetExpressionPath(stream, format);
1740b57cec5SDimitry Andric       stream.PutCString(" =");
1750b57cec5SDimitry Andric       return true;
1760b57cec5SDimitry Andric     };
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     DumpValueObjectOptions options;
1790b57cec5SDimitry Andric     options.SetDeclPrintingHelper(helper);
1800b57cec5SDimitry Andric     ValueObjectPrinter printer(valobj_sp.get(), &result.GetOutputStream(),
1810b57cec5SDimitry Andric                                options);
1820b57cec5SDimitry Andric     printer.PrintValueObject();
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)206c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
2070b57cec5SDimitry Andric     m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat(&result.GetOutputStream());
2080b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric };
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric #pragma mark CommandObjectFrameSelect
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric // CommandObjectFrameSelect
2150b57cec5SDimitry Andric 
2169dba64beSDimitry Andric #define LLDB_OPTIONS_frame_select
2179dba64beSDimitry Andric #include "CommandOptions.inc"
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric class CommandObjectFrameSelect : public CommandObjectParsed {
2200b57cec5SDimitry Andric public:
2210b57cec5SDimitry Andric   class CommandOptions : public Options {
2220b57cec5SDimitry Andric   public:
CommandOptions()22304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     ~CommandOptions() override = default;
2260b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)2270b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2280b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
2290b57cec5SDimitry Andric       Status error;
2300b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2310b57cec5SDimitry Andric       switch (short_option) {
2329dba64beSDimitry Andric       case 'r': {
2339dba64beSDimitry Andric         int32_t offset = 0;
2349dba64beSDimitry Andric         if (option_arg.getAsInteger(0, offset) || offset == INT32_MIN) {
2350b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid frame offset argument '%s'",
2360b57cec5SDimitry Andric                                          option_arg.str().c_str());
2379dba64beSDimitry Andric         } else
2389dba64beSDimitry Andric           relative_frame_offset = offset;
2390b57cec5SDimitry Andric         break;
2409dba64beSDimitry Andric       }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric       default:
2439dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
2440b57cec5SDimitry Andric       }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric       return error;
2470b57cec5SDimitry Andric     }
2480b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)2490b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2509dba64beSDimitry Andric       relative_frame_offset.reset();
2510b57cec5SDimitry Andric     }
2520b57cec5SDimitry Andric 
GetDefinitions()2530b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
254bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_select_options);
2550b57cec5SDimitry Andric     }
2560b57cec5SDimitry Andric 
257bdd1243dSDimitry Andric     std::optional<int32_t> relative_frame_offset;
2580b57cec5SDimitry Andric   };
2590b57cec5SDimitry Andric 
CommandObjectFrameSelect(CommandInterpreter & interpreter)2600b57cec5SDimitry Andric   CommandObjectFrameSelect(CommandInterpreter &interpreter)
261480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame select",
262480093f4SDimitry Andric                             "Select the current stack frame by "
2630b57cec5SDimitry Andric                             "index from within the current thread "
2640b57cec5SDimitry Andric                             "(see 'thread backtrace'.)",
2650b57cec5SDimitry Andric                             nullptr,
2660b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
267480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
26804eeddc0SDimitry Andric                                 eCommandProcessMustBePaused) {
2690b57cec5SDimitry Andric     CommandArgumentEntry arg;
2700b57cec5SDimitry Andric     CommandArgumentData index_arg;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
2730b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
2740b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatOptional;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
2770b57cec5SDimitry Andric     // argument entry.
2780b57cec5SDimitry Andric     arg.push_back(index_arg);
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
2810b57cec5SDimitry Andric     m_arguments.push_back(arg);
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   ~CommandObjectFrameSelect() override = default;
2850b57cec5SDimitry Andric 
2865ffd83dbSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)2875ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
2885ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
289e8d8bef9SDimitry Andric     if (request.GetCursorIndex() != 0)
2905ffd83dbSDimitry Andric       return;
2915ffd83dbSDimitry Andric 
292fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
293fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eFrameIndexCompletion, request, nullptr);
2945ffd83dbSDimitry Andric   }
2955ffd83dbSDimitry Andric 
GetOptions()2960b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)299c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
3000b57cec5SDimitry Andric     // No need to check "thread" for validity as eCommandRequiresThread ensures
3010b57cec5SDimitry Andric     // it is valid
3020b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric     uint32_t frame_idx = UINT32_MAX;
30581ad6265SDimitry Andric     if (m_options.relative_frame_offset) {
3060b57cec5SDimitry Andric       // The one and only argument is a signed relative frame index
307fe013be4SDimitry Andric       frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
3080b57cec5SDimitry Andric       if (frame_idx == UINT32_MAX)
3090b57cec5SDimitry Andric         frame_idx = 0;
3100b57cec5SDimitry Andric 
3119dba64beSDimitry Andric       if (*m_options.relative_frame_offset < 0) {
3129dba64beSDimitry Andric         if (static_cast<int32_t>(frame_idx) >=
3139dba64beSDimitry Andric             -*m_options.relative_frame_offset)
3149dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3150b57cec5SDimitry Andric         else {
3160b57cec5SDimitry Andric           if (frame_idx == 0) {
3170b57cec5SDimitry Andric             // If you are already at the bottom of the stack, then just warn
3180b57cec5SDimitry Andric             // and don't reset the frame.
3190b57cec5SDimitry Andric             result.AppendError("Already at the bottom of the stack.");
320c9157d92SDimitry Andric             return;
3210b57cec5SDimitry Andric           } else
3220b57cec5SDimitry Andric             frame_idx = 0;
3230b57cec5SDimitry Andric         }
3249dba64beSDimitry Andric       } else if (*m_options.relative_frame_offset > 0) {
3250b57cec5SDimitry Andric         // I don't want "up 20" where "20" takes you past the top of the stack
326fe013be4SDimitry Andric         // to produce an error, but rather to just go to the top.  OTOH, start
327fe013be4SDimitry Andric         // by seeing if the requested frame exists, in which case we can avoid
328fe013be4SDimitry Andric         // counting the stack here...
329fe013be4SDimitry Andric         const uint32_t frame_requested = frame_idx
330fe013be4SDimitry Andric             + *m_options.relative_frame_offset;
331fe013be4SDimitry Andric         StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_requested);
332fe013be4SDimitry Andric         if (frame_sp)
333fe013be4SDimitry Andric           frame_idx = frame_requested;
334fe013be4SDimitry Andric         else {
335fe013be4SDimitry Andric           // The request went past the stack, so handle that case:
3360b57cec5SDimitry Andric           const uint32_t num_frames = thread->GetStackFrameCount();
3370b57cec5SDimitry Andric           if (static_cast<int32_t>(num_frames - frame_idx) >
3389dba64beSDimitry Andric               *m_options.relative_frame_offset)
3399dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3400b57cec5SDimitry Andric           else {
3410b57cec5SDimitry Andric             if (frame_idx == num_frames - 1) {
3420b57cec5SDimitry Andric               // If we are already at the top of the stack, just warn and don't
3430b57cec5SDimitry Andric               // reset the frame.
3440b57cec5SDimitry Andric               result.AppendError("Already at the top of the stack.");
345c9157d92SDimitry Andric               return;
3460b57cec5SDimitry Andric             } else
3470b57cec5SDimitry Andric               frame_idx = num_frames - 1;
3480b57cec5SDimitry Andric           }
3490b57cec5SDimitry Andric         }
350fe013be4SDimitry Andric       }
3510b57cec5SDimitry Andric     } else {
3520b57cec5SDimitry Andric       if (command.GetArgumentCount() > 1) {
3530b57cec5SDimitry Andric         result.AppendErrorWithFormat(
3540b57cec5SDimitry Andric             "too many arguments; expected frame-index, saw '%s'.\n",
3550b57cec5SDimitry Andric             command[0].c_str());
3560b57cec5SDimitry Andric         m_options.GenerateOptionUsage(
35781ad6265SDimitry Andric             result.GetErrorStream(), *this,
3580b57cec5SDimitry Andric             GetCommandInterpreter().GetDebugger().GetTerminalWidth());
359c9157d92SDimitry Andric         return;
3600b57cec5SDimitry Andric       }
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
3639dba64beSDimitry Andric         if (command[0].ref().getAsInteger(0, frame_idx)) {
3640b57cec5SDimitry Andric           result.AppendErrorWithFormat("invalid frame index argument '%s'.",
3650b57cec5SDimitry Andric                                        command[0].c_str());
366c9157d92SDimitry Andric           return;
3670b57cec5SDimitry Andric         }
3680b57cec5SDimitry Andric       } else if (command.GetArgumentCount() == 0) {
369fe013be4SDimitry Andric         frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
3700b57cec5SDimitry Andric         if (frame_idx == UINT32_MAX) {
3710b57cec5SDimitry Andric           frame_idx = 0;
3720b57cec5SDimitry Andric         }
3730b57cec5SDimitry Andric       }
3740b57cec5SDimitry Andric     }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric     bool success = thread->SetSelectedFrameByIndexNoisily(
3770b57cec5SDimitry Andric         frame_idx, result.GetOutputStream());
3780b57cec5SDimitry Andric     if (success) {
379fe013be4SDimitry Andric       m_exe_ctx.SetFrameSP(thread->GetSelectedFrame(SelectMostRelevantFrame));
3800b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
3810b57cec5SDimitry Andric     } else {
3820b57cec5SDimitry Andric       result.AppendErrorWithFormat("Frame index (%u) out of range.\n",
3830b57cec5SDimitry Andric                                    frame_idx);
3840b57cec5SDimitry Andric     }
3850b57cec5SDimitry Andric   }
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   CommandOptions m_options;
3880b57cec5SDimitry Andric };
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric #pragma mark CommandObjectFrameVariable
3910b57cec5SDimitry Andric // List images with associated information
3920b57cec5SDimitry Andric class CommandObjectFrameVariable : public CommandObjectParsed {
3930b57cec5SDimitry Andric public:
CommandObjectFrameVariable(CommandInterpreter & interpreter)3940b57cec5SDimitry Andric   CommandObjectFrameVariable(CommandInterpreter &interpreter)
3950b57cec5SDimitry Andric       : CommandObjectParsed(
3960b57cec5SDimitry Andric             interpreter, "frame variable",
3970b57cec5SDimitry Andric             "Show variables for the current stack frame. Defaults to all "
3980b57cec5SDimitry Andric             "arguments and local variables in scope. Names of argument, "
39904eeddc0SDimitry Andric             "local, file static and file global variables can be specified.",
400480093f4SDimitry Andric             nullptr,
401480093f4SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
402480093f4SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
403480093f4SDimitry Andric                 eCommandRequiresProcess),
4040b57cec5SDimitry Andric         m_option_variable(
4050b57cec5SDimitry Andric             true), // Include the frame specific options by passing "true"
40604eeddc0SDimitry Andric         m_option_format(eFormatDefault) {
40704eeddc0SDimitry Andric     SetHelpLong(R"(
40804eeddc0SDimitry Andric Children of aggregate variables can be specified such as 'var->child.x'.  In
40904eeddc0SDimitry Andric 'frame variable', the operators -> and [] do not invoke operator overloads if
41004eeddc0SDimitry Andric they exist, but directly access the specified element.  If you want to trigger
41104eeddc0SDimitry Andric operator overloads use the expression command to print the variable instead.
41204eeddc0SDimitry Andric 
41304eeddc0SDimitry Andric It is worth noting that except for overloaded operators, when printing local
41404eeddc0SDimitry Andric variables 'expr local_var' and 'frame var local_var' produce the same results.
41504eeddc0SDimitry Andric However, 'frame variable' is more efficient, since it uses debug information and
41604eeddc0SDimitry Andric memory reads directly, rather than parsing and evaluating an expression, which
41704eeddc0SDimitry Andric may even involve JITing and running code in the target program.)");
41804eeddc0SDimitry Andric 
4190b57cec5SDimitry Andric     CommandArgumentEntry arg;
4200b57cec5SDimitry Andric     CommandArgumentData var_name_arg;
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
4230b57cec5SDimitry Andric     var_name_arg.arg_type = eArgTypeVarName;
4240b57cec5SDimitry Andric     var_name_arg.arg_repetition = eArgRepeatStar;
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
4270b57cec5SDimitry Andric     // argument entry.
4280b57cec5SDimitry Andric     arg.push_back(var_name_arg);
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
4310b57cec5SDimitry Andric     m_arguments.push_back(arg);
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4340b57cec5SDimitry Andric     m_option_group.Append(&m_option_format,
4350b57cec5SDimitry Andric                           OptionGroupFormat::OPTION_GROUP_FORMAT |
4360b57cec5SDimitry Andric                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
4370b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
4380b57cec5SDimitry Andric     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4390b57cec5SDimitry Andric     m_option_group.Finalize();
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   ~CommandObjectFrameVariable() override = default;
4430b57cec5SDimitry Andric 
GetOptions()4440b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
4450b57cec5SDimitry Andric 
4469dba64beSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)4479dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
4480b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
4490b57cec5SDimitry Andric     // Arguments are the standard source file completer.
450fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
451fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eVariablePathCompletion, request,
452fe013be4SDimitry Andric         nullptr);
4530b57cec5SDimitry Andric   }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric protected:
GetScopeString(VariableSP var_sp)4560b57cec5SDimitry Andric   llvm::StringRef GetScopeString(VariableSP var_sp) {
4570b57cec5SDimitry Andric     if (!var_sp)
458fe6060f1SDimitry Andric       return llvm::StringRef();
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric     switch (var_sp->GetScope()) {
4610b57cec5SDimitry Andric     case eValueTypeVariableGlobal:
4620b57cec5SDimitry Andric       return "GLOBAL: ";
4630b57cec5SDimitry Andric     case eValueTypeVariableStatic:
4640b57cec5SDimitry Andric       return "STATIC: ";
4650b57cec5SDimitry Andric     case eValueTypeVariableArgument:
4660b57cec5SDimitry Andric       return "ARG: ";
4670b57cec5SDimitry Andric     case eValueTypeVariableLocal:
4680b57cec5SDimitry Andric       return "LOCAL: ";
4690b57cec5SDimitry Andric     case eValueTypeVariableThreadLocal:
4700b57cec5SDimitry Andric       return "THREAD: ";
4710b57cec5SDimitry Andric     default:
4720b57cec5SDimitry Andric       break;
4730b57cec5SDimitry Andric     }
4740b57cec5SDimitry Andric 
475fe6060f1SDimitry Andric     return llvm::StringRef();
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
478fe013be4SDimitry Andric   /// Returns true if `scope` matches any of the options in `m_option_variable`.
ScopeRequested(lldb::ValueType scope)479fe013be4SDimitry Andric   bool ScopeRequested(lldb::ValueType scope) {
480fe013be4SDimitry Andric     switch (scope) {
481fe013be4SDimitry Andric     case eValueTypeVariableGlobal:
482fe013be4SDimitry Andric     case eValueTypeVariableStatic:
483fe013be4SDimitry Andric       return m_option_variable.show_globals;
484fe013be4SDimitry Andric     case eValueTypeVariableArgument:
485fe013be4SDimitry Andric       return m_option_variable.show_args;
486fe013be4SDimitry Andric     case eValueTypeVariableLocal:
487fe013be4SDimitry Andric       return m_option_variable.show_locals;
488fe013be4SDimitry Andric     case eValueTypeInvalid:
489fe013be4SDimitry Andric     case eValueTypeRegister:
490fe013be4SDimitry Andric     case eValueTypeRegisterSet:
491fe013be4SDimitry Andric     case eValueTypeConstResult:
492fe013be4SDimitry Andric     case eValueTypeVariableThreadLocal:
493c9157d92SDimitry Andric     case eValueTypeVTable:
494c9157d92SDimitry Andric     case eValueTypeVTableEntry:
495fe013be4SDimitry Andric       return false;
496fe013be4SDimitry Andric     }
497*a58f00eaSDimitry Andric     llvm_unreachable("Unexpected scope value");
498fe013be4SDimitry Andric   }
499fe013be4SDimitry Andric 
500fe013be4SDimitry Andric   /// Finds all the variables in `all_variables` whose name matches `regex`,
501fe013be4SDimitry Andric   /// inserting them into `matches`. Variables already contained in `matches`
502fe013be4SDimitry Andric   /// are not inserted again.
503fe013be4SDimitry Andric   /// Nullopt is returned in case of no matches.
504fe013be4SDimitry Andric   /// A sub-range of `matches` with all newly inserted variables is returned.
505fe013be4SDimitry Andric   /// This may be empty if all matches were already contained in `matches`.
506fe013be4SDimitry Andric   std::optional<llvm::ArrayRef<VariableSP>>
findUniqueRegexMatches(RegularExpression & regex,VariableList & matches,const VariableList & all_variables)507fe013be4SDimitry Andric   findUniqueRegexMatches(RegularExpression &regex,
508fe013be4SDimitry Andric                          VariableList &matches,
509fe013be4SDimitry Andric                          const VariableList &all_variables) {
510fe013be4SDimitry Andric     bool any_matches = false;
511fe013be4SDimitry Andric     const size_t previous_num_vars = matches.GetSize();
512fe013be4SDimitry Andric 
513fe013be4SDimitry Andric     for (const VariableSP &var : all_variables) {
514fe013be4SDimitry Andric       if (!var->NameMatches(regex) || !ScopeRequested(var->GetScope()))
515fe013be4SDimitry Andric         continue;
516fe013be4SDimitry Andric       any_matches = true;
517fe013be4SDimitry Andric       matches.AddVariableIfUnique(var);
518fe013be4SDimitry Andric     }
519fe013be4SDimitry Andric 
520fe013be4SDimitry Andric     if (any_matches)
521fe013be4SDimitry Andric       return matches.toArrayRef().drop_front(previous_num_vars);
522fe013be4SDimitry Andric     return std::nullopt;
523fe013be4SDimitry Andric   }
524fe013be4SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)525c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
5260b57cec5SDimitry Andric     // No need to check "frame" for validity as eCommandRequiresFrame ensures
5270b57cec5SDimitry Andric     // it is valid
5280b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric     Stream &s = result.GetOutputStream();
5310b57cec5SDimitry Andric 
532fe013be4SDimitry Andric     // Using a regex should behave like looking for an exact name match: it
533fe013be4SDimitry Andric     // also finds globals.
534fe013be4SDimitry Andric     m_option_variable.show_globals |= m_option_variable.use_regex;
535fe013be4SDimitry Andric 
5360b57cec5SDimitry Andric     // Be careful about the stack frame, if any summary formatter runs code, it
5370b57cec5SDimitry Andric     // might clear the StackFrameList for the thread.  So hold onto a shared
5380b57cec5SDimitry Andric     // pointer to the frame so it stays alive.
5390b57cec5SDimitry Andric 
540bdd1243dSDimitry Andric     Status error;
5410b57cec5SDimitry Andric     VariableList *variable_list =
542bdd1243dSDimitry Andric         frame->GetVariableList(m_option_variable.show_globals, &error);
5430b57cec5SDimitry Andric 
544bdd1243dSDimitry Andric     if (error.Fail() && (!variable_list || variable_list->GetSize() == 0)) {
545bdd1243dSDimitry Andric       result.AppendError(error.AsCString());
546bdd1243dSDimitry Andric 
547bdd1243dSDimitry Andric     }
5480b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric     TypeSummaryImplSP summary_format_sp;
5510b57cec5SDimitry Andric     if (!m_option_variable.summary.IsCurrentValueEmpty())
5520b57cec5SDimitry Andric       DataVisualization::NamedSummaryFormats::GetSummaryFormat(
5530b57cec5SDimitry Andric           ConstString(m_option_variable.summary.GetCurrentValue()),
5540b57cec5SDimitry Andric           summary_format_sp);
5550b57cec5SDimitry Andric     else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
5560b57cec5SDimitry Andric       summary_format_sp = std::make_shared<StringSummaryFormat>(
5570b57cec5SDimitry Andric           TypeSummaryImpl::Flags(),
5580b57cec5SDimitry Andric           m_option_variable.summary_string.GetCurrentValue());
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
5610b57cec5SDimitry Andric         eLanguageRuntimeDescriptionDisplayVerbosityFull, eFormatDefault,
5620b57cec5SDimitry Andric         summary_format_sp));
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric     const SymbolContext &sym_ctx =
5650b57cec5SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction);
5660b57cec5SDimitry Andric     if (sym_ctx.function && sym_ctx.function->IsTopLevelFunction())
5670b57cec5SDimitry Andric       m_option_variable.show_globals = true;
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric     if (variable_list) {
5700b57cec5SDimitry Andric       const Format format = m_option_format.GetFormat();
5710b57cec5SDimitry Andric       options.SetFormat(format);
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric       if (!command.empty()) {
5740b57cec5SDimitry Andric         VariableList regex_var_list;
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric         // If we have any args to the variable command, we will make variable
5770b57cec5SDimitry Andric         // objects from them...
5780b57cec5SDimitry Andric         for (auto &entry : command) {
5790b57cec5SDimitry Andric           if (m_option_variable.use_regex) {
5809dba64beSDimitry Andric             llvm::StringRef name_str = entry.ref();
5810b57cec5SDimitry Andric             RegularExpression regex(name_str);
5829dba64beSDimitry Andric             if (regex.IsValid()) {
583fe013be4SDimitry Andric               std::optional<llvm::ArrayRef<VariableSP>> results =
584fe013be4SDimitry Andric                   findUniqueRegexMatches(regex, regex_var_list, *variable_list);
585fe013be4SDimitry Andric               if (!results) {
586fe013be4SDimitry Andric                 result.AppendErrorWithFormat(
587fe013be4SDimitry Andric                     "no variables matched the regular expression '%s'.",
588fe013be4SDimitry Andric                     entry.c_str());
589fe013be4SDimitry Andric                 continue;
590fe013be4SDimitry Andric               }
591fe013be4SDimitry Andric               for (const VariableSP &var_sp : *results) {
5920b57cec5SDimitry Andric                 valobj_sp = frame->GetValueObjectForFrameVariable(
5930b57cec5SDimitry Andric                     var_sp, m_varobj_options.use_dynamic);
5940b57cec5SDimitry Andric                 if (valobj_sp) {
5950b57cec5SDimitry Andric                   std::string scope_string;
5960b57cec5SDimitry Andric                   if (m_option_variable.show_scope)
5970b57cec5SDimitry Andric                     scope_string = GetScopeString(var_sp).str();
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric                   if (!scope_string.empty())
6000b57cec5SDimitry Andric                     s.PutCString(scope_string);
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric                   if (m_option_variable.show_decl &&
6030b57cec5SDimitry Andric                       var_sp->GetDeclaration().GetFile()) {
6040b57cec5SDimitry Andric                     bool show_fullpaths = false;
6050b57cec5SDimitry Andric                     bool show_module = true;
6060b57cec5SDimitry Andric                     if (var_sp->DumpDeclaration(&s, show_fullpaths,
6070b57cec5SDimitry Andric                                                 show_module))
6080b57cec5SDimitry Andric                       s.PutCString(": ");
6090b57cec5SDimitry Andric                   }
6100b57cec5SDimitry Andric                   valobj_sp->Dump(result.GetOutputStream(), options);
6110b57cec5SDimitry Andric                 }
6120b57cec5SDimitry Andric               }
6130b57cec5SDimitry Andric             } else {
6149dba64beSDimitry Andric               if (llvm::Error err = regex.GetError())
61504eeddc0SDimitry Andric                 result.AppendError(llvm::toString(std::move(err)));
6160b57cec5SDimitry Andric               else
61704eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
61804eeddc0SDimitry Andric                     "unknown regex error when compiling '%s'", entry.c_str());
6190b57cec5SDimitry Andric             }
6200b57cec5SDimitry Andric           } else // No regex, either exact variable names or variable
6210b57cec5SDimitry Andric                  // expressions.
6220b57cec5SDimitry Andric           {
6230b57cec5SDimitry Andric             Status error;
6240b57cec5SDimitry Andric             uint32_t expr_path_options =
6250b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionCheckPtrVsMember |
6260b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess |
6270b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsInspectAnonymousUnions;
6280b57cec5SDimitry Andric             lldb::VariableSP var_sp;
6290b57cec5SDimitry Andric             valobj_sp = frame->GetValueForVariableExpressionPath(
6309dba64beSDimitry Andric                 entry.ref(), m_varobj_options.use_dynamic, expr_path_options,
6310b57cec5SDimitry Andric                 var_sp, error);
6320b57cec5SDimitry Andric             if (valobj_sp) {
6330b57cec5SDimitry Andric               std::string scope_string;
6340b57cec5SDimitry Andric               if (m_option_variable.show_scope)
6350b57cec5SDimitry Andric                 scope_string = GetScopeString(var_sp).str();
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric               if (!scope_string.empty())
6380b57cec5SDimitry Andric                 s.PutCString(scope_string);
6390b57cec5SDimitry Andric               if (m_option_variable.show_decl && var_sp &&
6400b57cec5SDimitry Andric                   var_sp->GetDeclaration().GetFile()) {
6410b57cec5SDimitry Andric                 var_sp->GetDeclaration().DumpStopContext(&s, false);
6420b57cec5SDimitry Andric                 s.PutCString(": ");
6430b57cec5SDimitry Andric               }
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric               options.SetFormat(format);
6460b57cec5SDimitry Andric               options.SetVariableFormatDisplayLanguage(
6470b57cec5SDimitry Andric                   valobj_sp->GetPreferredDisplayLanguage());
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric               Stream &output_stream = result.GetOutputStream();
6500b57cec5SDimitry Andric               options.SetRootValueObjectName(
6510b57cec5SDimitry Andric                   valobj_sp->GetParent() ? entry.c_str() : nullptr);
6520b57cec5SDimitry Andric               valobj_sp->Dump(output_stream, options);
6530b57cec5SDimitry Andric             } else {
65404eeddc0SDimitry Andric               if (auto error_cstr = error.AsCString(nullptr))
65504eeddc0SDimitry Andric                 result.AppendError(error_cstr);
6560b57cec5SDimitry Andric               else
65704eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
65804eeddc0SDimitry Andric                     "unable to find any variable expression path that matches "
65904eeddc0SDimitry Andric                     "'%s'.",
6600b57cec5SDimitry Andric                     entry.c_str());
6610b57cec5SDimitry Andric             }
6620b57cec5SDimitry Andric           }
6630b57cec5SDimitry Andric         }
6640b57cec5SDimitry Andric       } else // No command arg specified.  Use variable_list, instead.
6650b57cec5SDimitry Andric       {
6660b57cec5SDimitry Andric         const size_t num_variables = variable_list->GetSize();
6670b57cec5SDimitry Andric         if (num_variables > 0) {
6680b57cec5SDimitry Andric           for (size_t i = 0; i < num_variables; i++) {
669fe013be4SDimitry Andric             VariableSP var_sp = variable_list->GetVariableAtIndex(i);
670fe013be4SDimitry Andric             if (!ScopeRequested(var_sp->GetScope()))
6710b57cec5SDimitry Andric                 continue;
6720b57cec5SDimitry Andric             std::string scope_string;
6730b57cec5SDimitry Andric             if (m_option_variable.show_scope)
6740b57cec5SDimitry Andric               scope_string = GetScopeString(var_sp).str();
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric             // Use the variable object code to make sure we are using the same
6770b57cec5SDimitry Andric             // APIs as the public API will be using...
6780b57cec5SDimitry Andric             valobj_sp = frame->GetValueObjectForFrameVariable(
6790b57cec5SDimitry Andric                 var_sp, m_varobj_options.use_dynamic);
6800b57cec5SDimitry Andric             if (valobj_sp) {
6810b57cec5SDimitry Andric               // When dumping all variables, don't print any variables that are
6820b57cec5SDimitry Andric               // not in scope to avoid extra unneeded output
6830b57cec5SDimitry Andric               if (valobj_sp->IsInScope()) {
6840b57cec5SDimitry Andric                 if (!valobj_sp->GetTargetSP()
6850b57cec5SDimitry Andric                          ->GetDisplayRuntimeSupportValues() &&
6860b57cec5SDimitry Andric                     valobj_sp->IsRuntimeSupportValue())
6870b57cec5SDimitry Andric                   continue;
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric                 if (!scope_string.empty())
6900b57cec5SDimitry Andric                   s.PutCString(scope_string);
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric                 if (m_option_variable.show_decl &&
6930b57cec5SDimitry Andric                     var_sp->GetDeclaration().GetFile()) {
6940b57cec5SDimitry Andric                   var_sp->GetDeclaration().DumpStopContext(&s, false);
6950b57cec5SDimitry Andric                   s.PutCString(": ");
6960b57cec5SDimitry Andric                 }
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric                 options.SetFormat(format);
6990b57cec5SDimitry Andric                 options.SetVariableFormatDisplayLanguage(
7000b57cec5SDimitry Andric                     valobj_sp->GetPreferredDisplayLanguage());
7010b57cec5SDimitry Andric                 options.SetRootValueObjectName(
7020b57cec5SDimitry Andric                     var_sp ? var_sp->GetName().AsCString() : nullptr);
7030b57cec5SDimitry Andric                 valobj_sp->Dump(result.GetOutputStream(), options);
7040b57cec5SDimitry Andric               }
7050b57cec5SDimitry Andric             }
7060b57cec5SDimitry Andric           }
7070b57cec5SDimitry Andric         }
7080b57cec5SDimitry Andric       }
70904eeddc0SDimitry Andric       if (result.GetStatus() != eReturnStatusFailed)
7100b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
7110b57cec5SDimitry Andric     }
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric     if (m_option_variable.show_recognized_args) {
7140b57cec5SDimitry Andric       auto recognized_frame = frame->GetRecognizedFrame();
7150b57cec5SDimitry Andric       if (recognized_frame) {
7160b57cec5SDimitry Andric         ValueObjectListSP recognized_arg_list =
7170b57cec5SDimitry Andric             recognized_frame->GetRecognizedArguments();
7180b57cec5SDimitry Andric         if (recognized_arg_list) {
7190b57cec5SDimitry Andric           for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
7200b57cec5SDimitry Andric             options.SetFormat(m_option_format.GetFormat());
7210b57cec5SDimitry Andric             options.SetVariableFormatDisplayLanguage(
7220b57cec5SDimitry Andric                 rec_value_sp->GetPreferredDisplayLanguage());
7230b57cec5SDimitry Andric             options.SetRootValueObjectName(rec_value_sp->GetName().AsCString());
7240b57cec5SDimitry Andric             rec_value_sp->Dump(result.GetOutputStream(), options);
7250b57cec5SDimitry Andric           }
7260b57cec5SDimitry Andric         }
7270b57cec5SDimitry Andric       }
7280b57cec5SDimitry Andric     }
7290b57cec5SDimitry Andric 
73081ad6265SDimitry Andric     m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
73181ad6265SDimitry Andric                                            m_cmd_name);
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     // Increment statistics.
734349cc55cSDimitry Andric     TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
735c9157d92SDimitry Andric     if (result.Succeeded())
736349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifySuccess();
7370b57cec5SDimitry Andric     else
738349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifyFailure();
7390b57cec5SDimitry Andric   }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
7420b57cec5SDimitry Andric   OptionGroupVariable m_option_variable;
7430b57cec5SDimitry Andric   OptionGroupFormat m_option_format;
7440b57cec5SDimitry Andric   OptionGroupValueObjectDisplay m_varobj_options;
7450b57cec5SDimitry Andric };
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric #pragma mark CommandObjectFrameRecognizer
7480b57cec5SDimitry Andric 
7499dba64beSDimitry Andric #define LLDB_OPTIONS_frame_recognizer_add
7509dba64beSDimitry Andric #include "CommandOptions.inc"
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric class CommandObjectFrameRecognizerAdd : public CommandObjectParsed {
7530b57cec5SDimitry Andric private:
7540b57cec5SDimitry Andric   class CommandOptions : public Options {
7550b57cec5SDimitry Andric   public:
75681ad6265SDimitry Andric     CommandOptions() = default;
7570b57cec5SDimitry Andric     ~CommandOptions() override = default;
7580b57cec5SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)7590b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7600b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7610b57cec5SDimitry Andric       Status error;
7620b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric       switch (short_option) {
765349cc55cSDimitry Andric       case 'f': {
766349cc55cSDimitry Andric         bool value, success;
767349cc55cSDimitry Andric         value = OptionArgParser::ToBoolean(option_arg, true, &success);
768349cc55cSDimitry Andric         if (success) {
769349cc55cSDimitry Andric           m_first_instruction_only = value;
770349cc55cSDimitry Andric         } else {
771349cc55cSDimitry Andric           error.SetErrorStringWithFormat(
772349cc55cSDimitry Andric               "invalid boolean value '%s' passed for -f option",
773349cc55cSDimitry Andric               option_arg.str().c_str());
774349cc55cSDimitry Andric         }
775349cc55cSDimitry Andric       } break;
7760b57cec5SDimitry Andric       case 'l':
7770b57cec5SDimitry Andric         m_class_name = std::string(option_arg);
7780b57cec5SDimitry Andric         break;
7790b57cec5SDimitry Andric       case 's':
7800b57cec5SDimitry Andric         m_module = std::string(option_arg);
7810b57cec5SDimitry Andric         break;
7820b57cec5SDimitry Andric       case 'n':
7835ffd83dbSDimitry Andric         m_symbols.push_back(std::string(option_arg));
7840b57cec5SDimitry Andric         break;
7850b57cec5SDimitry Andric       case 'x':
7860b57cec5SDimitry Andric         m_regex = true;
7870b57cec5SDimitry Andric         break;
7880b57cec5SDimitry Andric       default:
7899dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7900b57cec5SDimitry Andric       }
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric       return error;
7930b57cec5SDimitry Andric     }
7940b57cec5SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)7950b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7960b57cec5SDimitry Andric       m_module = "";
7975ffd83dbSDimitry Andric       m_symbols.clear();
7980b57cec5SDimitry Andric       m_class_name = "";
7990b57cec5SDimitry Andric       m_regex = false;
800349cc55cSDimitry Andric       m_first_instruction_only = true;
8010b57cec5SDimitry Andric     }
8020b57cec5SDimitry Andric 
GetDefinitions()8030b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
804bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_recognizer_add_options);
8050b57cec5SDimitry Andric     }
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
8080b57cec5SDimitry Andric     std::string m_class_name;
8090b57cec5SDimitry Andric     std::string m_module;
8105ffd83dbSDimitry Andric     std::vector<std::string> m_symbols;
8110b57cec5SDimitry Andric     bool m_regex;
812349cc55cSDimitry Andric     bool m_first_instruction_only;
8130b57cec5SDimitry Andric   };
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric   CommandOptions m_options;
8160b57cec5SDimitry Andric 
GetOptions()8170b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric protected:
820c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override;
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerAdd(CommandInterpreter & interpreter)8230b57cec5SDimitry Andric   CommandObjectFrameRecognizerAdd(CommandInterpreter &interpreter)
8240b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer add",
82504eeddc0SDimitry Andric                             "Add a new frame recognizer.", nullptr) {
8260b57cec5SDimitry Andric     SetHelpLong(R"(
8270b57cec5SDimitry Andric Frame recognizers allow for retrieving information about special frames based on
8280b57cec5SDimitry Andric ABI, arguments or other special properties of that frame, even without source
8290b57cec5SDimitry Andric code or debug info. Currently, one use case is to extract function arguments
8300b57cec5SDimitry Andric that would otherwise be unaccesible, or augment existing arguments.
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric Adding a custom frame recognizer is possible by implementing a Python class
8330b57cec5SDimitry Andric and using the 'frame recognizer add' command. The Python class should have a
8340b57cec5SDimitry Andric 'get_recognized_arguments' method and it will receive an argument of type
8350b57cec5SDimitry Andric lldb.SBFrame representing the current frame that we are trying to recognize.
8360b57cec5SDimitry Andric The method should return a (possibly empty) list of lldb.SBValue objects that
8370b57cec5SDimitry Andric represent the recognized arguments.
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric An example of a recognizer that retrieves the file descriptor values from libc
8400b57cec5SDimitry Andric functions 'read', 'write' and 'close' follows:
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric   class LibcFdRecognizer(object):
8430b57cec5SDimitry Andric     def get_recognized_arguments(self, frame):
8440b57cec5SDimitry Andric       if frame.name in ["read", "write", "close"]:
8450b57cec5SDimitry Andric         fd = frame.EvaluateExpression("$arg1").unsigned
846bdd1243dSDimitry Andric         target = frame.thread.process.target
847bdd1243dSDimitry Andric         value = target.CreateValueFromExpression("fd", "(int)%d" % fd)
8480b57cec5SDimitry Andric         return [value]
8490b57cec5SDimitry Andric       return []
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric The file containing this implementation can be imported via 'command script
8520b57cec5SDimitry Andric import' and then we can register this recognizer with 'frame recognizer add'.
8530b57cec5SDimitry Andric It's important to restrict the recognizer to the libc library (which is
8540b57cec5SDimitry Andric libsystem_kernel.dylib on macOS) to avoid matching functions with the same name
8550b57cec5SDimitry Andric in other modules:
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric (lldb) command script import .../fd_recognizer.py
8580b57cec5SDimitry Andric (lldb) frame recognizer add -l fd_recognizer.LibcFdRecognizer -n read -s libsystem_kernel.dylib
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric When the program is stopped at the beginning of the 'read' function in libc, we
8610b57cec5SDimitry Andric can view the recognizer arguments in 'frame variable':
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric (lldb) b read
8640b57cec5SDimitry Andric (lldb) r
8650b57cec5SDimitry Andric Process 1234 stopped
8660b57cec5SDimitry Andric * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.3
8670b57cec5SDimitry Andric     frame #0: 0x00007fff06013ca0 libsystem_kernel.dylib`read
8680b57cec5SDimitry Andric (lldb) frame variable
8690b57cec5SDimitry Andric (int) fd = 3
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric     )");
8720b57cec5SDimitry Andric   }
8730b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerAdd() override = default;
8740b57cec5SDimitry Andric };
8750b57cec5SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)876c9157d92SDimitry Andric void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
8770b57cec5SDimitry Andric                                                 CommandReturnObject &result) {
878480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
8790b57cec5SDimitry Andric   if (m_options.m_class_name.empty()) {
8800b57cec5SDimitry Andric     result.AppendErrorWithFormat(
8810b57cec5SDimitry Andric         "%s needs a Python class name (-l argument).\n", m_cmd_name.c_str());
882c9157d92SDimitry Andric     return;
8830b57cec5SDimitry Andric   }
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric   if (m_options.m_module.empty()) {
8860b57cec5SDimitry Andric     result.AppendErrorWithFormat("%s needs a module name (-s argument).\n",
8870b57cec5SDimitry Andric                                  m_cmd_name.c_str());
888c9157d92SDimitry Andric     return;
8890b57cec5SDimitry Andric   }
8900b57cec5SDimitry Andric 
8915ffd83dbSDimitry Andric   if (m_options.m_symbols.empty()) {
8925ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8935ffd83dbSDimitry Andric         "%s needs at least one symbol name (-n argument).\n",
8945ffd83dbSDimitry Andric         m_cmd_name.c_str());
895c9157d92SDimitry Andric     return;
8965ffd83dbSDimitry Andric   }
8975ffd83dbSDimitry Andric 
8985ffd83dbSDimitry Andric   if (m_options.m_regex && m_options.m_symbols.size() > 1) {
8995ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
9005ffd83dbSDimitry Andric         "%s needs only one symbol regular expression (-n argument).\n",
9010b57cec5SDimitry Andric         m_cmd_name.c_str());
902c9157d92SDimitry Andric     return;
9030b57cec5SDimitry Andric   }
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric   ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   if (interpreter &&
9080b57cec5SDimitry Andric       !interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
909480093f4SDimitry Andric     result.AppendWarning("The provided class does not exist - please define it "
9100b57cec5SDimitry Andric                          "before attempting to use this frame recognizer");
9110b57cec5SDimitry Andric   }
9120b57cec5SDimitry Andric 
9130b57cec5SDimitry Andric   StackFrameRecognizerSP recognizer_sp =
9140b57cec5SDimitry Andric       StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
9150b57cec5SDimitry Andric           interpreter, m_options.m_class_name.c_str()));
9160b57cec5SDimitry Andric   if (m_options.m_regex) {
9170b57cec5SDimitry Andric     auto module =
9180b57cec5SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_module));
9190b57cec5SDimitry Andric     auto func =
9205ffd83dbSDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
921e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
922349cc55cSDimitry Andric         recognizer_sp, module, func, m_options.m_first_instruction_only);
9230b57cec5SDimitry Andric   } else {
9240b57cec5SDimitry Andric     auto module = ConstString(m_options.m_module);
9255ffd83dbSDimitry Andric     std::vector<ConstString> symbols(m_options.m_symbols.begin(),
9265ffd83dbSDimitry Andric                                      m_options.m_symbols.end());
927e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
928349cc55cSDimitry Andric         recognizer_sp, module, symbols, m_options.m_first_instruction_only);
9290b57cec5SDimitry Andric   }
9300b57cec5SDimitry Andric #endif
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishNoResult);
9330b57cec5SDimitry Andric }
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
9360b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerClear(CommandInterpreter & interpreter)9370b57cec5SDimitry Andric   CommandObjectFrameRecognizerClear(CommandInterpreter &interpreter)
9380b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer clear",
9390b57cec5SDimitry Andric                             "Delete all frame recognizers.", nullptr) {}
9400b57cec5SDimitry Andric 
9410b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerClear() override = default;
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)944c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
945e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget()
946e8d8bef9SDimitry Andric         .GetFrameRecognizerManager()
947e8d8bef9SDimitry Andric         .RemoveAllRecognizers();
9480b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9490b57cec5SDimitry Andric   }
9500b57cec5SDimitry Andric };
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
9530b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerDelete(CommandInterpreter & interpreter)9540b57cec5SDimitry Andric   CommandObjectFrameRecognizerDelete(CommandInterpreter &interpreter)
9550b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer delete",
95681ad6265SDimitry Andric                             "Delete an existing frame recognizer by id.",
95781ad6265SDimitry Andric                             nullptr) {
95881ad6265SDimitry Andric     CommandArgumentData thread_arg{eArgTypeRecognizerID, eArgRepeatPlain};
95981ad6265SDimitry Andric     m_arguments.push_back({thread_arg});
96081ad6265SDimitry Andric   }
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerDelete() override = default;
9630b57cec5SDimitry Andric 
9645ffd83dbSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)9655ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
9665ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
9675ffd83dbSDimitry Andric     if (request.GetCursorIndex() != 0)
9685ffd83dbSDimitry Andric       return;
9695ffd83dbSDimitry Andric 
970e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
9715ffd83dbSDimitry Andric         [&request](uint32_t rid, std::string rname, std::string module,
9725ffd83dbSDimitry Andric                    llvm::ArrayRef<lldb_private::ConstString> symbols,
9735ffd83dbSDimitry Andric                    bool regexp) {
9745ffd83dbSDimitry Andric           StreamString strm;
9755ffd83dbSDimitry Andric           if (rname.empty())
9765ffd83dbSDimitry Andric             rname = "(internal)";
9775ffd83dbSDimitry Andric 
9785ffd83dbSDimitry Andric           strm << rname;
9795ffd83dbSDimitry Andric           if (!module.empty())
9805ffd83dbSDimitry Andric             strm << ", module " << module;
9815ffd83dbSDimitry Andric           if (!symbols.empty())
9825ffd83dbSDimitry Andric             for (auto &symbol : symbols)
9835ffd83dbSDimitry Andric               strm << ", symbol " << symbol;
9845ffd83dbSDimitry Andric           if (regexp)
9855ffd83dbSDimitry Andric             strm << " (regexp)";
9865ffd83dbSDimitry Andric 
9875ffd83dbSDimitry Andric           request.TryCompleteCurrentArg(std::to_string(rid), strm.GetString());
9885ffd83dbSDimitry Andric         });
9895ffd83dbSDimitry Andric   }
9905ffd83dbSDimitry Andric 
9910b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)992c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
9930b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
9940b57cec5SDimitry Andric       if (!m_interpreter.Confirm(
9950b57cec5SDimitry Andric               "About to delete all frame recognizers, do you want to do that?",
9960b57cec5SDimitry Andric               true)) {
9970b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
998c9157d92SDimitry Andric         return;
9990b57cec5SDimitry Andric       }
10000b57cec5SDimitry Andric 
1001e8d8bef9SDimitry Andric       GetSelectedOrDummyTarget()
1002e8d8bef9SDimitry Andric           .GetFrameRecognizerManager()
1003e8d8bef9SDimitry Andric           .RemoveAllRecognizers();
10040b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
1005c9157d92SDimitry Andric       return;
10060b57cec5SDimitry Andric     }
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
10090b57cec5SDimitry Andric       result.AppendErrorWithFormat("'%s' takes zero or one arguments.\n",
10100b57cec5SDimitry Andric                                    m_cmd_name.c_str());
1011c9157d92SDimitry Andric       return;
10120b57cec5SDimitry Andric     }
10130b57cec5SDimitry Andric 
10145ffd83dbSDimitry Andric     uint32_t recognizer_id;
10155ffd83dbSDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), recognizer_id)) {
10165ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
10175ffd83dbSDimitry Andric                                    command.GetArgumentAtIndex(0));
1018c9157d92SDimitry Andric       return;
10195ffd83dbSDimitry Andric     }
10200b57cec5SDimitry Andric 
1021e8d8bef9SDimitry Andric     if (!GetSelectedOrDummyTarget()
1022e8d8bef9SDimitry Andric              .GetFrameRecognizerManager()
1023e8d8bef9SDimitry Andric              .RemoveRecognizerWithID(recognizer_id)) {
1024e8d8bef9SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
1025e8d8bef9SDimitry Andric                                    command.GetArgumentAtIndex(0));
1026c9157d92SDimitry Andric       return;
1027e8d8bef9SDimitry Andric     }
10280b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
10290b57cec5SDimitry Andric   }
10300b57cec5SDimitry Andric };
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric class CommandObjectFrameRecognizerList : public CommandObjectParsed {
10330b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerList(CommandInterpreter & interpreter)10340b57cec5SDimitry Andric   CommandObjectFrameRecognizerList(CommandInterpreter &interpreter)
10350b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer list",
10360b57cec5SDimitry Andric                             "Show a list of active frame recognizers.",
10370b57cec5SDimitry Andric                             nullptr) {}
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerList() override = default;
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)1042c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
10430b57cec5SDimitry Andric     bool any_printed = false;
1044e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
10455ffd83dbSDimitry Andric         [&result, &any_printed](
10465ffd83dbSDimitry Andric             uint32_t recognizer_id, std::string name, std::string module,
10475ffd83dbSDimitry Andric             llvm::ArrayRef<ConstString> symbols, bool regexp) {
10485ffd83dbSDimitry Andric           Stream &stream = result.GetOutputStream();
10495ffd83dbSDimitry Andric 
10505ffd83dbSDimitry Andric           if (name.empty())
1051480093f4SDimitry Andric             name = "(internal)";
10525ffd83dbSDimitry Andric 
10535ffd83dbSDimitry Andric           stream << std::to_string(recognizer_id) << ": " << name;
10545ffd83dbSDimitry Andric           if (!module.empty())
10555ffd83dbSDimitry Andric             stream << ", module " << module;
10565ffd83dbSDimitry Andric           if (!symbols.empty())
10575ffd83dbSDimitry Andric             for (auto &symbol : symbols)
10585ffd83dbSDimitry Andric               stream << ", symbol " << symbol;
10595ffd83dbSDimitry Andric           if (regexp)
10605ffd83dbSDimitry Andric             stream << " (regexp)";
10615ffd83dbSDimitry Andric 
10625ffd83dbSDimitry Andric           stream.EOL();
10635ffd83dbSDimitry Andric           stream.Flush();
10645ffd83dbSDimitry Andric 
10650b57cec5SDimitry Andric           any_printed = true;
10660b57cec5SDimitry Andric         });
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric     if (any_printed)
10690b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
10700b57cec5SDimitry Andric     else {
10710b57cec5SDimitry Andric       result.GetOutputStream().PutCString("no matching results found.\n");
10720b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
10730b57cec5SDimitry Andric     }
10740b57cec5SDimitry Andric   }
10750b57cec5SDimitry Andric };
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
10780b57cec5SDimitry Andric public:
CommandObjectFrameRecognizerInfo(CommandInterpreter & interpreter)10790b57cec5SDimitry Andric   CommandObjectFrameRecognizerInfo(CommandInterpreter &interpreter)
10800b57cec5SDimitry Andric       : CommandObjectParsed(
10810b57cec5SDimitry Andric             interpreter, "frame recognizer info",
10820b57cec5SDimitry Andric             "Show which frame recognizer is applied a stack frame (if any).",
10830b57cec5SDimitry Andric             nullptr) {
10840b57cec5SDimitry Andric     CommandArgumentEntry arg;
10850b57cec5SDimitry Andric     CommandArgumentData index_arg;
10860b57cec5SDimitry Andric 
10870b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
10880b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
10890b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatPlain;
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
10920b57cec5SDimitry Andric     // argument entry.
10930b57cec5SDimitry Andric     arg.push_back(index_arg);
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
10960b57cec5SDimitry Andric     m_arguments.push_back(arg);
10970b57cec5SDimitry Andric   }
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerInfo() override = default;
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)1102c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
11035ffd83dbSDimitry Andric     const char *frame_index_str = command.GetArgumentAtIndex(0);
11045ffd83dbSDimitry Andric     uint32_t frame_index;
11055ffd83dbSDimitry Andric     if (!llvm::to_integer(frame_index_str, frame_index)) {
11065ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid frame index.",
11075ffd83dbSDimitry Andric                                    frame_index_str);
1108c9157d92SDimitry Andric       return;
11095ffd83dbSDimitry Andric     }
11105ffd83dbSDimitry Andric 
11110b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
11120b57cec5SDimitry Andric     if (process == nullptr) {
11130b57cec5SDimitry Andric       result.AppendError("no process");
1114c9157d92SDimitry Andric       return;
11150b57cec5SDimitry Andric     }
11160b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
11170b57cec5SDimitry Andric     if (thread == nullptr) {
11180b57cec5SDimitry Andric       result.AppendError("no thread");
1119c9157d92SDimitry Andric       return;
11200b57cec5SDimitry Andric     }
11210b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
11220b57cec5SDimitry Andric       result.AppendErrorWithFormat(
11230b57cec5SDimitry Andric           "'%s' takes exactly one frame index argument.\n", m_cmd_name.c_str());
1124c9157d92SDimitry Andric       return;
11250b57cec5SDimitry Andric     }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric     StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_index);
11280b57cec5SDimitry Andric     if (!frame_sp) {
11290b57cec5SDimitry Andric       result.AppendErrorWithFormat("no frame with index %u", frame_index);
1130c9157d92SDimitry Andric       return;
11310b57cec5SDimitry Andric     }
11320b57cec5SDimitry Andric 
1133e8d8bef9SDimitry Andric     auto recognizer = GetSelectedOrDummyTarget()
1134e8d8bef9SDimitry Andric                           .GetFrameRecognizerManager()
1135e8d8bef9SDimitry Andric                           .GetRecognizerForFrame(frame_sp);
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
11380b57cec5SDimitry Andric     output_stream.Printf("frame %d ", frame_index);
11390b57cec5SDimitry Andric     if (recognizer) {
11400b57cec5SDimitry Andric       output_stream << "is recognized by ";
11410b57cec5SDimitry Andric       output_stream << recognizer->GetName();
11420b57cec5SDimitry Andric     } else {
11430b57cec5SDimitry Andric       output_stream << "not recognized by any recognizer";
11440b57cec5SDimitry Andric     }
11450b57cec5SDimitry Andric     output_stream.EOL();
11460b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
11470b57cec5SDimitry Andric   }
11480b57cec5SDimitry Andric };
11490b57cec5SDimitry Andric 
11500b57cec5SDimitry Andric class CommandObjectFrameRecognizer : public CommandObjectMultiword {
11510b57cec5SDimitry Andric public:
CommandObjectFrameRecognizer(CommandInterpreter & interpreter)11520b57cec5SDimitry Andric   CommandObjectFrameRecognizer(CommandInterpreter &interpreter)
11530b57cec5SDimitry Andric       : CommandObjectMultiword(
11540b57cec5SDimitry Andric             interpreter, "frame recognizer",
11550b57cec5SDimitry Andric             "Commands for editing and viewing frame recognizers.",
11560b57cec5SDimitry Andric             "frame recognizer [<sub-command-options>] ") {
1157480093f4SDimitry Andric     LoadSubCommand("add", CommandObjectSP(new CommandObjectFrameRecognizerAdd(
1158480093f4SDimitry Andric                               interpreter)));
11590b57cec5SDimitry Andric     LoadSubCommand(
11600b57cec5SDimitry Andric         "clear",
11610b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerClear(interpreter)));
11620b57cec5SDimitry Andric     LoadSubCommand(
11630b57cec5SDimitry Andric         "delete",
11640b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerDelete(interpreter)));
1165480093f4SDimitry Andric     LoadSubCommand("list", CommandObjectSP(new CommandObjectFrameRecognizerList(
1166480093f4SDimitry Andric                                interpreter)));
1167480093f4SDimitry Andric     LoadSubCommand("info", CommandObjectSP(new CommandObjectFrameRecognizerInfo(
1168480093f4SDimitry Andric                                interpreter)));
11690b57cec5SDimitry Andric   }
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric   ~CommandObjectFrameRecognizer() override = default;
11720b57cec5SDimitry Andric };
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric #pragma mark CommandObjectMultiwordFrame
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric // CommandObjectMultiwordFrame
11770b57cec5SDimitry Andric 
CommandObjectMultiwordFrame(CommandInterpreter & interpreter)11780b57cec5SDimitry Andric CommandObjectMultiwordFrame::CommandObjectMultiwordFrame(
11790b57cec5SDimitry Andric     CommandInterpreter &interpreter)
1180480093f4SDimitry Andric     : CommandObjectMultiword(interpreter, "frame",
1181480093f4SDimitry Andric                              "Commands for selecting and "
11820b57cec5SDimitry Andric                              "examing the current "
11830b57cec5SDimitry Andric                              "thread's stack frames.",
11840b57cec5SDimitry Andric                              "frame <subcommand> [<subcommand-options>]") {
11850b57cec5SDimitry Andric   LoadSubCommand("diagnose",
11860b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameDiagnose(interpreter)));
11870b57cec5SDimitry Andric   LoadSubCommand("info",
11880b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameInfo(interpreter)));
11890b57cec5SDimitry Andric   LoadSubCommand("select",
11900b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameSelect(interpreter)));
11910b57cec5SDimitry Andric   LoadSubCommand("variable",
11920b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameVariable(interpreter)));
1193480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
1194480093f4SDimitry Andric   LoadSubCommand("recognizer", CommandObjectSP(new CommandObjectFrameRecognizer(
1195480093f4SDimitry Andric                                    interpreter)));
11960b57cec5SDimitry Andric #endif
11970b57cec5SDimitry Andric }
11980b57cec5SDimitry Andric 
11990b57cec5SDimitry Andric CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame() = default;
1200