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:
5404eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric     ~CommandOptions() override = default;
570b57cec5SDimitry Andric 
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 
920b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
930b57cec5SDimitry Andric       address.reset();
940b57cec5SDimitry Andric       reg.reset();
950b57cec5SDimitry Andric       offset.reset();
960b57cec5SDimitry Andric     }
970b57cec5SDimitry Andric 
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 
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 
1330b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric protected:
1360b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
1370b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
138*fe013be4SDimitry 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.");
1460b57cec5SDimitry Andric         return false;
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.");
1560b57cec5SDimitry Andric         return false;
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.");
1640b57cec5SDimitry Andric       return false;
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     return true;
1850b57cec5SDimitry Andric   }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   CommandOptions m_options;
1880b57cec5SDimitry Andric };
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric #pragma mark CommandObjectFrameInfo
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric // CommandObjectFrameInfo
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric class CommandObjectFrameInfo : public CommandObjectParsed {
1950b57cec5SDimitry Andric public:
1960b57cec5SDimitry Andric   CommandObjectFrameInfo(CommandInterpreter &interpreter)
197480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame info",
198480093f4SDimitry Andric                             "List information about the current "
1990b57cec5SDimitry Andric                             "stack frame in the current thread.",
2000b57cec5SDimitry Andric                             "frame info",
2010b57cec5SDimitry Andric                             eCommandRequiresFrame | eCommandTryTargetAPILock |
202480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
203480093f4SDimitry Andric                                 eCommandProcessMustBePaused) {}
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   ~CommandObjectFrameInfo() override = default;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric protected:
2080b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
2090b57cec5SDimitry Andric     m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat(&result.GetOutputStream());
2100b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
2110b57cec5SDimitry Andric     return result.Succeeded();
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric };
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric #pragma mark CommandObjectFrameSelect
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric // CommandObjectFrameSelect
2180b57cec5SDimitry Andric 
2199dba64beSDimitry Andric #define LLDB_OPTIONS_frame_select
2209dba64beSDimitry Andric #include "CommandOptions.inc"
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric class CommandObjectFrameSelect : public CommandObjectParsed {
2230b57cec5SDimitry Andric public:
2240b57cec5SDimitry Andric   class CommandOptions : public Options {
2250b57cec5SDimitry Andric   public:
22604eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     ~CommandOptions() override = default;
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2310b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
2320b57cec5SDimitry Andric       Status error;
2330b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2340b57cec5SDimitry Andric       switch (short_option) {
2359dba64beSDimitry Andric       case 'r': {
2369dba64beSDimitry Andric         int32_t offset = 0;
2379dba64beSDimitry Andric         if (option_arg.getAsInteger(0, offset) || offset == INT32_MIN) {
2380b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid frame offset argument '%s'",
2390b57cec5SDimitry Andric                                          option_arg.str().c_str());
2409dba64beSDimitry Andric         } else
2419dba64beSDimitry Andric           relative_frame_offset = offset;
2420b57cec5SDimitry Andric         break;
2439dba64beSDimitry Andric       }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric       default:
2469dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
2470b57cec5SDimitry Andric       }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric       return error;
2500b57cec5SDimitry Andric     }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2539dba64beSDimitry Andric       relative_frame_offset.reset();
2540b57cec5SDimitry Andric     }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
257bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_select_options);
2580b57cec5SDimitry Andric     }
2590b57cec5SDimitry Andric 
260bdd1243dSDimitry Andric     std::optional<int32_t> relative_frame_offset;
2610b57cec5SDimitry Andric   };
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   CommandObjectFrameSelect(CommandInterpreter &interpreter)
264480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame select",
265480093f4SDimitry Andric                             "Select the current stack frame by "
2660b57cec5SDimitry Andric                             "index from within the current thread "
2670b57cec5SDimitry Andric                             "(see 'thread backtrace'.)",
2680b57cec5SDimitry Andric                             nullptr,
2690b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
270480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
27104eeddc0SDimitry Andric                                 eCommandProcessMustBePaused) {
2720b57cec5SDimitry Andric     CommandArgumentEntry arg;
2730b57cec5SDimitry Andric     CommandArgumentData index_arg;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
2760b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
2770b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatOptional;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
2800b57cec5SDimitry Andric     // argument entry.
2810b57cec5SDimitry Andric     arg.push_back(index_arg);
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
2840b57cec5SDimitry Andric     m_arguments.push_back(arg);
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   ~CommandObjectFrameSelect() override = default;
2880b57cec5SDimitry Andric 
2895ffd83dbSDimitry Andric   void
2905ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
2915ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
292e8d8bef9SDimitry Andric     if (request.GetCursorIndex() != 0)
2935ffd83dbSDimitry Andric       return;
2945ffd83dbSDimitry Andric 
295*fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
296*fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eFrameIndexCompletion, request, nullptr);
2975ffd83dbSDimitry Andric   }
2985ffd83dbSDimitry Andric 
2990b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric protected:
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;
30881ad6265SDimitry Andric     if (m_options.relative_frame_offset) {
3090b57cec5SDimitry Andric       // The one and only argument is a signed relative frame index
310*fe013be4SDimitry Andric       frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
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
329*fe013be4SDimitry Andric         // to produce an error, but rather to just go to the top.  OTOH, start
330*fe013be4SDimitry Andric         // by seeing if the requested frame exists, in which case we can avoid
331*fe013be4SDimitry Andric         // counting the stack here...
332*fe013be4SDimitry Andric         const uint32_t frame_requested = frame_idx
333*fe013be4SDimitry Andric             + *m_options.relative_frame_offset;
334*fe013be4SDimitry Andric         StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_requested);
335*fe013be4SDimitry Andric         if (frame_sp)
336*fe013be4SDimitry Andric           frame_idx = frame_requested;
337*fe013be4SDimitry Andric         else {
338*fe013be4SDimitry Andric           // The request went past the stack, so handle that case:
3390b57cec5SDimitry Andric           const uint32_t num_frames = thread->GetStackFrameCount();
3400b57cec5SDimitry Andric           if (static_cast<int32_t>(num_frames - frame_idx) >
3419dba64beSDimitry Andric               *m_options.relative_frame_offset)
3429dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3430b57cec5SDimitry Andric           else {
3440b57cec5SDimitry Andric             if (frame_idx == num_frames - 1) {
3450b57cec5SDimitry Andric               // If we are already at the top of the stack, just warn and don't
3460b57cec5SDimitry Andric               // reset the frame.
3470b57cec5SDimitry Andric               result.AppendError("Already at the top of the stack.");
3480b57cec5SDimitry Andric               return false;
3490b57cec5SDimitry Andric             } else
3500b57cec5SDimitry Andric               frame_idx = num_frames - 1;
3510b57cec5SDimitry Andric           }
3520b57cec5SDimitry Andric         }
353*fe013be4SDimitry Andric       }
3540b57cec5SDimitry Andric     } else {
3550b57cec5SDimitry Andric       if (command.GetArgumentCount() > 1) {
3560b57cec5SDimitry Andric         result.AppendErrorWithFormat(
3570b57cec5SDimitry Andric             "too many arguments; expected frame-index, saw '%s'.\n",
3580b57cec5SDimitry Andric             command[0].c_str());
3590b57cec5SDimitry Andric         m_options.GenerateOptionUsage(
36081ad6265SDimitry Andric             result.GetErrorStream(), *this,
3610b57cec5SDimitry Andric             GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3620b57cec5SDimitry Andric         return false;
3630b57cec5SDimitry Andric       }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
3669dba64beSDimitry Andric         if (command[0].ref().getAsInteger(0, frame_idx)) {
3670b57cec5SDimitry Andric           result.AppendErrorWithFormat("invalid frame index argument '%s'.",
3680b57cec5SDimitry Andric                                        command[0].c_str());
3690b57cec5SDimitry Andric           return false;
3700b57cec5SDimitry Andric         }
3710b57cec5SDimitry Andric       } else if (command.GetArgumentCount() == 0) {
372*fe013be4SDimitry Andric         frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
3730b57cec5SDimitry Andric         if (frame_idx == UINT32_MAX) {
3740b57cec5SDimitry Andric           frame_idx = 0;
3750b57cec5SDimitry Andric         }
3760b57cec5SDimitry Andric       }
3770b57cec5SDimitry Andric     }
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric     bool success = thread->SetSelectedFrameByIndexNoisily(
3800b57cec5SDimitry Andric         frame_idx, result.GetOutputStream());
3810b57cec5SDimitry Andric     if (success) {
382*fe013be4SDimitry Andric       m_exe_ctx.SetFrameSP(thread->GetSelectedFrame(SelectMostRelevantFrame));
3830b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
3840b57cec5SDimitry Andric     } else {
3850b57cec5SDimitry Andric       result.AppendErrorWithFormat("Frame index (%u) out of range.\n",
3860b57cec5SDimitry Andric                                    frame_idx);
3870b57cec5SDimitry Andric     }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric     return result.Succeeded();
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   CommandOptions m_options;
3930b57cec5SDimitry Andric };
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric #pragma mark CommandObjectFrameVariable
3960b57cec5SDimitry Andric // List images with associated information
3970b57cec5SDimitry Andric class CommandObjectFrameVariable : public CommandObjectParsed {
3980b57cec5SDimitry Andric public:
3990b57cec5SDimitry Andric   CommandObjectFrameVariable(CommandInterpreter &interpreter)
4000b57cec5SDimitry Andric       : CommandObjectParsed(
4010b57cec5SDimitry Andric             interpreter, "frame variable",
4020b57cec5SDimitry Andric             "Show variables for the current stack frame. Defaults to all "
4030b57cec5SDimitry Andric             "arguments and local variables in scope. Names of argument, "
40404eeddc0SDimitry Andric             "local, file static and file global variables can be specified.",
405480093f4SDimitry Andric             nullptr,
406480093f4SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
407480093f4SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
408480093f4SDimitry Andric                 eCommandRequiresProcess),
4090b57cec5SDimitry Andric         m_option_variable(
4100b57cec5SDimitry Andric             true), // Include the frame specific options by passing "true"
41104eeddc0SDimitry Andric         m_option_format(eFormatDefault) {
41204eeddc0SDimitry Andric     SetHelpLong(R"(
41304eeddc0SDimitry Andric Children of aggregate variables can be specified such as 'var->child.x'.  In
41404eeddc0SDimitry Andric 'frame variable', the operators -> and [] do not invoke operator overloads if
41504eeddc0SDimitry Andric they exist, but directly access the specified element.  If you want to trigger
41604eeddc0SDimitry Andric operator overloads use the expression command to print the variable instead.
41704eeddc0SDimitry Andric 
41804eeddc0SDimitry Andric It is worth noting that except for overloaded operators, when printing local
41904eeddc0SDimitry Andric variables 'expr local_var' and 'frame var local_var' produce the same results.
42004eeddc0SDimitry Andric However, 'frame variable' is more efficient, since it uses debug information and
42104eeddc0SDimitry Andric memory reads directly, rather than parsing and evaluating an expression, which
42204eeddc0SDimitry Andric may even involve JITing and running code in the target program.)");
42304eeddc0SDimitry Andric 
4240b57cec5SDimitry Andric     CommandArgumentEntry arg;
4250b57cec5SDimitry Andric     CommandArgumentData var_name_arg;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
4280b57cec5SDimitry Andric     var_name_arg.arg_type = eArgTypeVarName;
4290b57cec5SDimitry Andric     var_name_arg.arg_repetition = eArgRepeatStar;
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
4320b57cec5SDimitry Andric     // argument entry.
4330b57cec5SDimitry Andric     arg.push_back(var_name_arg);
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
4360b57cec5SDimitry Andric     m_arguments.push_back(arg);
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4390b57cec5SDimitry Andric     m_option_group.Append(&m_option_format,
4400b57cec5SDimitry Andric                           OptionGroupFormat::OPTION_GROUP_FORMAT |
4410b57cec5SDimitry Andric                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
4420b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
4430b57cec5SDimitry Andric     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4440b57cec5SDimitry Andric     m_option_group.Finalize();
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   ~CommandObjectFrameVariable() override = default;
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
4500b57cec5SDimitry Andric 
4519dba64beSDimitry Andric   void
4529dba64beSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
4530b57cec5SDimitry Andric                            OptionElementVector &opt_element_vector) override {
4540b57cec5SDimitry Andric     // Arguments are the standard source file completer.
455*fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
456*fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eVariablePathCompletion, request,
457*fe013be4SDimitry Andric         nullptr);
4580b57cec5SDimitry Andric   }
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric protected:
4610b57cec5SDimitry Andric   llvm::StringRef GetScopeString(VariableSP var_sp) {
4620b57cec5SDimitry Andric     if (!var_sp)
463fe6060f1SDimitry Andric       return llvm::StringRef();
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric     switch (var_sp->GetScope()) {
4660b57cec5SDimitry Andric     case eValueTypeVariableGlobal:
4670b57cec5SDimitry Andric       return "GLOBAL: ";
4680b57cec5SDimitry Andric     case eValueTypeVariableStatic:
4690b57cec5SDimitry Andric       return "STATIC: ";
4700b57cec5SDimitry Andric     case eValueTypeVariableArgument:
4710b57cec5SDimitry Andric       return "ARG: ";
4720b57cec5SDimitry Andric     case eValueTypeVariableLocal:
4730b57cec5SDimitry Andric       return "LOCAL: ";
4740b57cec5SDimitry Andric     case eValueTypeVariableThreadLocal:
4750b57cec5SDimitry Andric       return "THREAD: ";
4760b57cec5SDimitry Andric     default:
4770b57cec5SDimitry Andric       break;
4780b57cec5SDimitry Andric     }
4790b57cec5SDimitry Andric 
480fe6060f1SDimitry Andric     return llvm::StringRef();
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric 
483*fe013be4SDimitry Andric   /// Returns true if `scope` matches any of the options in `m_option_variable`.
484*fe013be4SDimitry Andric   bool ScopeRequested(lldb::ValueType scope) {
485*fe013be4SDimitry Andric     switch (scope) {
486*fe013be4SDimitry Andric     case eValueTypeVariableGlobal:
487*fe013be4SDimitry Andric     case eValueTypeVariableStatic:
488*fe013be4SDimitry Andric       return m_option_variable.show_globals;
489*fe013be4SDimitry Andric     case eValueTypeVariableArgument:
490*fe013be4SDimitry Andric       return m_option_variable.show_args;
491*fe013be4SDimitry Andric     case eValueTypeVariableLocal:
492*fe013be4SDimitry Andric       return m_option_variable.show_locals;
493*fe013be4SDimitry Andric     case eValueTypeInvalid:
494*fe013be4SDimitry Andric     case eValueTypeRegister:
495*fe013be4SDimitry Andric     case eValueTypeRegisterSet:
496*fe013be4SDimitry Andric     case eValueTypeConstResult:
497*fe013be4SDimitry Andric     case eValueTypeVariableThreadLocal:
498*fe013be4SDimitry Andric       return false;
499*fe013be4SDimitry Andric     }
500*fe013be4SDimitry Andric   }
501*fe013be4SDimitry Andric 
502*fe013be4SDimitry Andric   /// Finds all the variables in `all_variables` whose name matches `regex`,
503*fe013be4SDimitry Andric   /// inserting them into `matches`. Variables already contained in `matches`
504*fe013be4SDimitry Andric   /// are not inserted again.
505*fe013be4SDimitry Andric   /// Nullopt is returned in case of no matches.
506*fe013be4SDimitry Andric   /// A sub-range of `matches` with all newly inserted variables is returned.
507*fe013be4SDimitry Andric   /// This may be empty if all matches were already contained in `matches`.
508*fe013be4SDimitry Andric   std::optional<llvm::ArrayRef<VariableSP>>
509*fe013be4SDimitry Andric   findUniqueRegexMatches(RegularExpression &regex,
510*fe013be4SDimitry Andric                          VariableList &matches,
511*fe013be4SDimitry Andric                          const VariableList &all_variables) {
512*fe013be4SDimitry Andric     bool any_matches = false;
513*fe013be4SDimitry Andric     const size_t previous_num_vars = matches.GetSize();
514*fe013be4SDimitry Andric 
515*fe013be4SDimitry Andric     for (const VariableSP &var : all_variables) {
516*fe013be4SDimitry Andric       if (!var->NameMatches(regex) || !ScopeRequested(var->GetScope()))
517*fe013be4SDimitry Andric         continue;
518*fe013be4SDimitry Andric       any_matches = true;
519*fe013be4SDimitry Andric       matches.AddVariableIfUnique(var);
520*fe013be4SDimitry Andric     }
521*fe013be4SDimitry Andric 
522*fe013be4SDimitry Andric     if (any_matches)
523*fe013be4SDimitry Andric       return matches.toArrayRef().drop_front(previous_num_vars);
524*fe013be4SDimitry Andric     return std::nullopt;
525*fe013be4SDimitry Andric   }
526*fe013be4SDimitry Andric 
5270b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
5280b57cec5SDimitry Andric     // No need to check "frame" for validity as eCommandRequiresFrame ensures
5290b57cec5SDimitry Andric     // it is valid
5300b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric     Stream &s = result.GetOutputStream();
5330b57cec5SDimitry Andric 
534*fe013be4SDimitry Andric     // Using a regex should behave like looking for an exact name match: it
535*fe013be4SDimitry Andric     // also finds globals.
536*fe013be4SDimitry Andric     m_option_variable.show_globals |= m_option_variable.use_regex;
537*fe013be4SDimitry Andric 
5380b57cec5SDimitry Andric     // Be careful about the stack frame, if any summary formatter runs code, it
5390b57cec5SDimitry Andric     // might clear the StackFrameList for the thread.  So hold onto a shared
5400b57cec5SDimitry Andric     // pointer to the frame so it stays alive.
5410b57cec5SDimitry Andric 
542bdd1243dSDimitry Andric     Status error;
5430b57cec5SDimitry Andric     VariableList *variable_list =
544bdd1243dSDimitry Andric         frame->GetVariableList(m_option_variable.show_globals, &error);
5450b57cec5SDimitry Andric 
546bdd1243dSDimitry Andric     if (error.Fail() && (!variable_list || variable_list->GetSize() == 0)) {
547bdd1243dSDimitry Andric       result.AppendError(error.AsCString());
548bdd1243dSDimitry Andric 
549bdd1243dSDimitry Andric     }
5500b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric     TypeSummaryImplSP summary_format_sp;
5530b57cec5SDimitry Andric     if (!m_option_variable.summary.IsCurrentValueEmpty())
5540b57cec5SDimitry Andric       DataVisualization::NamedSummaryFormats::GetSummaryFormat(
5550b57cec5SDimitry Andric           ConstString(m_option_variable.summary.GetCurrentValue()),
5560b57cec5SDimitry Andric           summary_format_sp);
5570b57cec5SDimitry Andric     else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
5580b57cec5SDimitry Andric       summary_format_sp = std::make_shared<StringSummaryFormat>(
5590b57cec5SDimitry Andric           TypeSummaryImpl::Flags(),
5600b57cec5SDimitry Andric           m_option_variable.summary_string.GetCurrentValue());
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
5630b57cec5SDimitry Andric         eLanguageRuntimeDescriptionDisplayVerbosityFull, eFormatDefault,
5640b57cec5SDimitry Andric         summary_format_sp));
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric     const SymbolContext &sym_ctx =
5670b57cec5SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction);
5680b57cec5SDimitry Andric     if (sym_ctx.function && sym_ctx.function->IsTopLevelFunction())
5690b57cec5SDimitry Andric       m_option_variable.show_globals = true;
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric     if (variable_list) {
5720b57cec5SDimitry Andric       const Format format = m_option_format.GetFormat();
5730b57cec5SDimitry Andric       options.SetFormat(format);
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric       if (!command.empty()) {
5760b57cec5SDimitry Andric         VariableList regex_var_list;
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric         // If we have any args to the variable command, we will make variable
5790b57cec5SDimitry Andric         // objects from them...
5800b57cec5SDimitry Andric         for (auto &entry : command) {
5810b57cec5SDimitry Andric           if (m_option_variable.use_regex) {
5829dba64beSDimitry Andric             llvm::StringRef name_str = entry.ref();
5830b57cec5SDimitry Andric             RegularExpression regex(name_str);
5849dba64beSDimitry Andric             if (regex.IsValid()) {
585*fe013be4SDimitry Andric               std::optional<llvm::ArrayRef<VariableSP>> results =
586*fe013be4SDimitry Andric                   findUniqueRegexMatches(regex, regex_var_list, *variable_list);
587*fe013be4SDimitry Andric               if (!results) {
588*fe013be4SDimitry Andric                 result.AppendErrorWithFormat(
589*fe013be4SDimitry Andric                     "no variables matched the regular expression '%s'.",
590*fe013be4SDimitry Andric                     entry.c_str());
591*fe013be4SDimitry Andric                 continue;
592*fe013be4SDimitry Andric               }
593*fe013be4SDimitry Andric               for (const VariableSP &var_sp : *results) {
5940b57cec5SDimitry Andric                 valobj_sp = frame->GetValueObjectForFrameVariable(
5950b57cec5SDimitry Andric                     var_sp, m_varobj_options.use_dynamic);
5960b57cec5SDimitry Andric                 if (valobj_sp) {
5970b57cec5SDimitry Andric                   std::string scope_string;
5980b57cec5SDimitry Andric                   if (m_option_variable.show_scope)
5990b57cec5SDimitry Andric                     scope_string = GetScopeString(var_sp).str();
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric                   if (!scope_string.empty())
6020b57cec5SDimitry Andric                     s.PutCString(scope_string);
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric                   if (m_option_variable.show_decl &&
6050b57cec5SDimitry Andric                       var_sp->GetDeclaration().GetFile()) {
6060b57cec5SDimitry Andric                     bool show_fullpaths = false;
6070b57cec5SDimitry Andric                     bool show_module = true;
6080b57cec5SDimitry Andric                     if (var_sp->DumpDeclaration(&s, show_fullpaths,
6090b57cec5SDimitry Andric                                                 show_module))
6100b57cec5SDimitry Andric                       s.PutCString(": ");
6110b57cec5SDimitry Andric                   }
6120b57cec5SDimitry Andric                   valobj_sp->Dump(result.GetOutputStream(), options);
6130b57cec5SDimitry Andric                 }
6140b57cec5SDimitry Andric               }
6150b57cec5SDimitry Andric             } else {
6169dba64beSDimitry Andric               if (llvm::Error err = regex.GetError())
61704eeddc0SDimitry Andric                 result.AppendError(llvm::toString(std::move(err)));
6180b57cec5SDimitry Andric               else
61904eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
62004eeddc0SDimitry Andric                     "unknown regex error when compiling '%s'", entry.c_str());
6210b57cec5SDimitry Andric             }
6220b57cec5SDimitry Andric           } else // No regex, either exact variable names or variable
6230b57cec5SDimitry Andric                  // expressions.
6240b57cec5SDimitry Andric           {
6250b57cec5SDimitry Andric             Status error;
6260b57cec5SDimitry Andric             uint32_t expr_path_options =
6270b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionCheckPtrVsMember |
6280b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess |
6290b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsInspectAnonymousUnions;
6300b57cec5SDimitry Andric             lldb::VariableSP var_sp;
6310b57cec5SDimitry Andric             valobj_sp = frame->GetValueForVariableExpressionPath(
6329dba64beSDimitry Andric                 entry.ref(), m_varobj_options.use_dynamic, expr_path_options,
6330b57cec5SDimitry Andric                 var_sp, error);
6340b57cec5SDimitry Andric             if (valobj_sp) {
6350b57cec5SDimitry Andric               std::string scope_string;
6360b57cec5SDimitry Andric               if (m_option_variable.show_scope)
6370b57cec5SDimitry Andric                 scope_string = GetScopeString(var_sp).str();
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric               if (!scope_string.empty())
6400b57cec5SDimitry Andric                 s.PutCString(scope_string);
6410b57cec5SDimitry Andric               if (m_option_variable.show_decl && var_sp &&
6420b57cec5SDimitry Andric                   var_sp->GetDeclaration().GetFile()) {
6430b57cec5SDimitry Andric                 var_sp->GetDeclaration().DumpStopContext(&s, false);
6440b57cec5SDimitry Andric                 s.PutCString(": ");
6450b57cec5SDimitry Andric               }
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric               options.SetFormat(format);
6480b57cec5SDimitry Andric               options.SetVariableFormatDisplayLanguage(
6490b57cec5SDimitry Andric                   valobj_sp->GetPreferredDisplayLanguage());
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric               Stream &output_stream = result.GetOutputStream();
6520b57cec5SDimitry Andric               options.SetRootValueObjectName(
6530b57cec5SDimitry Andric                   valobj_sp->GetParent() ? entry.c_str() : nullptr);
6540b57cec5SDimitry Andric               valobj_sp->Dump(output_stream, options);
6550b57cec5SDimitry Andric             } else {
65604eeddc0SDimitry Andric               if (auto error_cstr = error.AsCString(nullptr))
65704eeddc0SDimitry Andric                 result.AppendError(error_cstr);
6580b57cec5SDimitry Andric               else
65904eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
66004eeddc0SDimitry Andric                     "unable to find any variable expression path that matches "
66104eeddc0SDimitry Andric                     "'%s'.",
6620b57cec5SDimitry Andric                     entry.c_str());
6630b57cec5SDimitry Andric             }
6640b57cec5SDimitry Andric           }
6650b57cec5SDimitry Andric         }
6660b57cec5SDimitry Andric       } else // No command arg specified.  Use variable_list, instead.
6670b57cec5SDimitry Andric       {
6680b57cec5SDimitry Andric         const size_t num_variables = variable_list->GetSize();
6690b57cec5SDimitry Andric         if (num_variables > 0) {
6700b57cec5SDimitry Andric           for (size_t i = 0; i < num_variables; i++) {
671*fe013be4SDimitry Andric             VariableSP var_sp = variable_list->GetVariableAtIndex(i);
672*fe013be4SDimitry Andric             if (!ScopeRequested(var_sp->GetScope()))
6730b57cec5SDimitry Andric                 continue;
6740b57cec5SDimitry Andric             std::string scope_string;
6750b57cec5SDimitry Andric             if (m_option_variable.show_scope)
6760b57cec5SDimitry Andric               scope_string = GetScopeString(var_sp).str();
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric             // Use the variable object code to make sure we are using the same
6790b57cec5SDimitry Andric             // APIs as the public API will be using...
6800b57cec5SDimitry Andric             valobj_sp = frame->GetValueObjectForFrameVariable(
6810b57cec5SDimitry Andric                 var_sp, m_varobj_options.use_dynamic);
6820b57cec5SDimitry Andric             if (valobj_sp) {
6830b57cec5SDimitry Andric               // When dumping all variables, don't print any variables that are
6840b57cec5SDimitry Andric               // not in scope to avoid extra unneeded output
6850b57cec5SDimitry Andric               if (valobj_sp->IsInScope()) {
6860b57cec5SDimitry Andric                 if (!valobj_sp->GetTargetSP()
6870b57cec5SDimitry Andric                          ->GetDisplayRuntimeSupportValues() &&
6880b57cec5SDimitry Andric                     valobj_sp->IsRuntimeSupportValue())
6890b57cec5SDimitry Andric                   continue;
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric                 if (!scope_string.empty())
6920b57cec5SDimitry Andric                   s.PutCString(scope_string);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric                 if (m_option_variable.show_decl &&
6950b57cec5SDimitry Andric                     var_sp->GetDeclaration().GetFile()) {
6960b57cec5SDimitry Andric                   var_sp->GetDeclaration().DumpStopContext(&s, false);
6970b57cec5SDimitry Andric                   s.PutCString(": ");
6980b57cec5SDimitry Andric                 }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric                 options.SetFormat(format);
7010b57cec5SDimitry Andric                 options.SetVariableFormatDisplayLanguage(
7020b57cec5SDimitry Andric                     valobj_sp->GetPreferredDisplayLanguage());
7030b57cec5SDimitry Andric                 options.SetRootValueObjectName(
7040b57cec5SDimitry Andric                     var_sp ? var_sp->GetName().AsCString() : nullptr);
7050b57cec5SDimitry Andric                 valobj_sp->Dump(result.GetOutputStream(), options);
7060b57cec5SDimitry Andric               }
7070b57cec5SDimitry Andric             }
7080b57cec5SDimitry Andric           }
7090b57cec5SDimitry Andric         }
7100b57cec5SDimitry Andric       }
71104eeddc0SDimitry Andric       if (result.GetStatus() != eReturnStatusFailed)
7120b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
7130b57cec5SDimitry Andric     }
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric     if (m_option_variable.show_recognized_args) {
7160b57cec5SDimitry Andric       auto recognized_frame = frame->GetRecognizedFrame();
7170b57cec5SDimitry Andric       if (recognized_frame) {
7180b57cec5SDimitry Andric         ValueObjectListSP recognized_arg_list =
7190b57cec5SDimitry Andric             recognized_frame->GetRecognizedArguments();
7200b57cec5SDimitry Andric         if (recognized_arg_list) {
7210b57cec5SDimitry Andric           for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
7220b57cec5SDimitry Andric             options.SetFormat(m_option_format.GetFormat());
7230b57cec5SDimitry Andric             options.SetVariableFormatDisplayLanguage(
7240b57cec5SDimitry Andric                 rec_value_sp->GetPreferredDisplayLanguage());
7250b57cec5SDimitry Andric             options.SetRootValueObjectName(rec_value_sp->GetName().AsCString());
7260b57cec5SDimitry Andric             rec_value_sp->Dump(result.GetOutputStream(), options);
7270b57cec5SDimitry Andric           }
7280b57cec5SDimitry Andric         }
7290b57cec5SDimitry Andric       }
7300b57cec5SDimitry Andric     }
7310b57cec5SDimitry Andric 
73281ad6265SDimitry Andric     m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
73381ad6265SDimitry Andric                                            m_cmd_name);
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric     // Increment statistics.
7360b57cec5SDimitry Andric     bool res = result.Succeeded();
737349cc55cSDimitry Andric     TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
7380b57cec5SDimitry Andric     if (res)
739349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifySuccess();
7400b57cec5SDimitry Andric     else
741349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifyFailure();
7420b57cec5SDimitry Andric     return res;
7430b57cec5SDimitry Andric   }
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
7460b57cec5SDimitry Andric   OptionGroupVariable m_option_variable;
7470b57cec5SDimitry Andric   OptionGroupFormat m_option_format;
7480b57cec5SDimitry Andric   OptionGroupValueObjectDisplay m_varobj_options;
7490b57cec5SDimitry Andric };
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric #pragma mark CommandObjectFrameRecognizer
7520b57cec5SDimitry Andric 
7539dba64beSDimitry Andric #define LLDB_OPTIONS_frame_recognizer_add
7549dba64beSDimitry Andric #include "CommandOptions.inc"
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric class CommandObjectFrameRecognizerAdd : public CommandObjectParsed {
7570b57cec5SDimitry Andric private:
7580b57cec5SDimitry Andric   class CommandOptions : public Options {
7590b57cec5SDimitry Andric   public:
76081ad6265SDimitry Andric     CommandOptions() = default;
7610b57cec5SDimitry Andric     ~CommandOptions() override = default;
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7640b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7650b57cec5SDimitry Andric       Status error;
7660b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric       switch (short_option) {
769349cc55cSDimitry Andric       case 'f': {
770349cc55cSDimitry Andric         bool value, success;
771349cc55cSDimitry Andric         value = OptionArgParser::ToBoolean(option_arg, true, &success);
772349cc55cSDimitry Andric         if (success) {
773349cc55cSDimitry Andric           m_first_instruction_only = value;
774349cc55cSDimitry Andric         } else {
775349cc55cSDimitry Andric           error.SetErrorStringWithFormat(
776349cc55cSDimitry Andric               "invalid boolean value '%s' passed for -f option",
777349cc55cSDimitry Andric               option_arg.str().c_str());
778349cc55cSDimitry Andric         }
779349cc55cSDimitry Andric       } break;
7800b57cec5SDimitry Andric       case 'l':
7810b57cec5SDimitry Andric         m_class_name = std::string(option_arg);
7820b57cec5SDimitry Andric         break;
7830b57cec5SDimitry Andric       case 's':
7840b57cec5SDimitry Andric         m_module = std::string(option_arg);
7850b57cec5SDimitry Andric         break;
7860b57cec5SDimitry Andric       case 'n':
7875ffd83dbSDimitry Andric         m_symbols.push_back(std::string(option_arg));
7880b57cec5SDimitry Andric         break;
7890b57cec5SDimitry Andric       case 'x':
7900b57cec5SDimitry Andric         m_regex = true;
7910b57cec5SDimitry Andric         break;
7920b57cec5SDimitry Andric       default:
7939dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7940b57cec5SDimitry Andric       }
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric       return error;
7970b57cec5SDimitry Andric     }
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
8000b57cec5SDimitry Andric       m_module = "";
8015ffd83dbSDimitry Andric       m_symbols.clear();
8020b57cec5SDimitry Andric       m_class_name = "";
8030b57cec5SDimitry Andric       m_regex = false;
804349cc55cSDimitry Andric       m_first_instruction_only = true;
8050b57cec5SDimitry Andric     }
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
808bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_recognizer_add_options);
8090b57cec5SDimitry Andric     }
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
8120b57cec5SDimitry Andric     std::string m_class_name;
8130b57cec5SDimitry Andric     std::string m_module;
8145ffd83dbSDimitry Andric     std::vector<std::string> m_symbols;
8150b57cec5SDimitry Andric     bool m_regex;
816349cc55cSDimitry Andric     bool m_first_instruction_only;
8170b57cec5SDimitry Andric   };
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric   CommandOptions m_options;
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
8220b57cec5SDimitry Andric 
8230b57cec5SDimitry Andric protected:
8240b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override;
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric public:
8270b57cec5SDimitry Andric   CommandObjectFrameRecognizerAdd(CommandInterpreter &interpreter)
8280b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer add",
82904eeddc0SDimitry Andric                             "Add a new frame recognizer.", nullptr) {
8300b57cec5SDimitry Andric     SetHelpLong(R"(
8310b57cec5SDimitry Andric Frame recognizers allow for retrieving information about special frames based on
8320b57cec5SDimitry Andric ABI, arguments or other special properties of that frame, even without source
8330b57cec5SDimitry Andric code or debug info. Currently, one use case is to extract function arguments
8340b57cec5SDimitry Andric that would otherwise be unaccesible, or augment existing arguments.
8350b57cec5SDimitry Andric 
8360b57cec5SDimitry Andric Adding a custom frame recognizer is possible by implementing a Python class
8370b57cec5SDimitry Andric and using the 'frame recognizer add' command. The Python class should have a
8380b57cec5SDimitry Andric 'get_recognized_arguments' method and it will receive an argument of type
8390b57cec5SDimitry Andric lldb.SBFrame representing the current frame that we are trying to recognize.
8400b57cec5SDimitry Andric The method should return a (possibly empty) list of lldb.SBValue objects that
8410b57cec5SDimitry Andric represent the recognized arguments.
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric An example of a recognizer that retrieves the file descriptor values from libc
8440b57cec5SDimitry Andric functions 'read', 'write' and 'close' follows:
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric   class LibcFdRecognizer(object):
8470b57cec5SDimitry Andric     def get_recognized_arguments(self, frame):
8480b57cec5SDimitry Andric       if frame.name in ["read", "write", "close"]:
8490b57cec5SDimitry Andric         fd = frame.EvaluateExpression("$arg1").unsigned
850bdd1243dSDimitry Andric         target = frame.thread.process.target
851bdd1243dSDimitry Andric         value = target.CreateValueFromExpression("fd", "(int)%d" % fd)
8520b57cec5SDimitry Andric         return [value]
8530b57cec5SDimitry Andric       return []
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric The file containing this implementation can be imported via 'command script
8560b57cec5SDimitry Andric import' and then we can register this recognizer with 'frame recognizer add'.
8570b57cec5SDimitry Andric It's important to restrict the recognizer to the libc library (which is
8580b57cec5SDimitry Andric libsystem_kernel.dylib on macOS) to avoid matching functions with the same name
8590b57cec5SDimitry Andric in other modules:
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric (lldb) command script import .../fd_recognizer.py
8620b57cec5SDimitry Andric (lldb) frame recognizer add -l fd_recognizer.LibcFdRecognizer -n read -s libsystem_kernel.dylib
8630b57cec5SDimitry Andric 
8640b57cec5SDimitry Andric When the program is stopped at the beginning of the 'read' function in libc, we
8650b57cec5SDimitry Andric can view the recognizer arguments in 'frame variable':
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric (lldb) b read
8680b57cec5SDimitry Andric (lldb) r
8690b57cec5SDimitry Andric Process 1234 stopped
8700b57cec5SDimitry Andric * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.3
8710b57cec5SDimitry Andric     frame #0: 0x00007fff06013ca0 libsystem_kernel.dylib`read
8720b57cec5SDimitry Andric (lldb) frame variable
8730b57cec5SDimitry Andric (int) fd = 3
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric     )");
8760b57cec5SDimitry Andric   }
8770b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerAdd() override = default;
8780b57cec5SDimitry Andric };
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric bool CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
8810b57cec5SDimitry Andric                                                 CommandReturnObject &result) {
882480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
8830b57cec5SDimitry Andric   if (m_options.m_class_name.empty()) {
8840b57cec5SDimitry Andric     result.AppendErrorWithFormat(
8850b57cec5SDimitry Andric         "%s needs a Python class name (-l argument).\n", m_cmd_name.c_str());
8860b57cec5SDimitry Andric     return false;
8870b57cec5SDimitry Andric   }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric   if (m_options.m_module.empty()) {
8900b57cec5SDimitry Andric     result.AppendErrorWithFormat("%s needs a module name (-s argument).\n",
8910b57cec5SDimitry Andric                                  m_cmd_name.c_str());
8920b57cec5SDimitry Andric     return false;
8930b57cec5SDimitry Andric   }
8940b57cec5SDimitry Andric 
8955ffd83dbSDimitry Andric   if (m_options.m_symbols.empty()) {
8965ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8975ffd83dbSDimitry Andric         "%s needs at least one symbol name (-n argument).\n",
8985ffd83dbSDimitry Andric         m_cmd_name.c_str());
8995ffd83dbSDimitry Andric     return false;
9005ffd83dbSDimitry Andric   }
9015ffd83dbSDimitry Andric 
9025ffd83dbSDimitry Andric   if (m_options.m_regex && m_options.m_symbols.size() > 1) {
9035ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
9045ffd83dbSDimitry Andric         "%s needs only one symbol regular expression (-n argument).\n",
9050b57cec5SDimitry Andric         m_cmd_name.c_str());
9060b57cec5SDimitry Andric     return false;
9070b57cec5SDimitry Andric   }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric   ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   if (interpreter &&
9120b57cec5SDimitry Andric       !interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
913480093f4SDimitry Andric     result.AppendWarning("The provided class does not exist - please define it "
9140b57cec5SDimitry Andric                          "before attempting to use this frame recognizer");
9150b57cec5SDimitry Andric   }
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   StackFrameRecognizerSP recognizer_sp =
9180b57cec5SDimitry Andric       StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
9190b57cec5SDimitry Andric           interpreter, m_options.m_class_name.c_str()));
9200b57cec5SDimitry Andric   if (m_options.m_regex) {
9210b57cec5SDimitry Andric     auto module =
9220b57cec5SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_module));
9230b57cec5SDimitry Andric     auto func =
9245ffd83dbSDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
925e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
926349cc55cSDimitry Andric         recognizer_sp, module, func, m_options.m_first_instruction_only);
9270b57cec5SDimitry Andric   } else {
9280b57cec5SDimitry Andric     auto module = ConstString(m_options.m_module);
9295ffd83dbSDimitry Andric     std::vector<ConstString> symbols(m_options.m_symbols.begin(),
9305ffd83dbSDimitry Andric                                      m_options.m_symbols.end());
931e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
932349cc55cSDimitry Andric         recognizer_sp, module, symbols, m_options.m_first_instruction_only);
9330b57cec5SDimitry Andric   }
9340b57cec5SDimitry Andric #endif
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishNoResult);
9370b57cec5SDimitry Andric   return result.Succeeded();
9380b57cec5SDimitry Andric }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
9410b57cec5SDimitry Andric public:
9420b57cec5SDimitry Andric   CommandObjectFrameRecognizerClear(CommandInterpreter &interpreter)
9430b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer clear",
9440b57cec5SDimitry Andric                             "Delete all frame recognizers.", nullptr) {}
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerClear() override = default;
9470b57cec5SDimitry Andric 
9480b57cec5SDimitry Andric protected:
9490b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
950e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget()
951e8d8bef9SDimitry Andric         .GetFrameRecognizerManager()
952e8d8bef9SDimitry Andric         .RemoveAllRecognizers();
9530b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9540b57cec5SDimitry Andric     return result.Succeeded();
9550b57cec5SDimitry Andric   }
9560b57cec5SDimitry Andric };
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
9590b57cec5SDimitry Andric public:
9600b57cec5SDimitry Andric   CommandObjectFrameRecognizerDelete(CommandInterpreter &interpreter)
9610b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer delete",
96281ad6265SDimitry Andric                             "Delete an existing frame recognizer by id.",
96381ad6265SDimitry Andric                             nullptr) {
96481ad6265SDimitry Andric     CommandArgumentData thread_arg{eArgTypeRecognizerID, eArgRepeatPlain};
96581ad6265SDimitry Andric     m_arguments.push_back({thread_arg});
96681ad6265SDimitry Andric   }
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerDelete() override = default;
9690b57cec5SDimitry Andric 
9705ffd83dbSDimitry Andric   void
9715ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
9725ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
9735ffd83dbSDimitry Andric     if (request.GetCursorIndex() != 0)
9745ffd83dbSDimitry Andric       return;
9755ffd83dbSDimitry Andric 
976e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
9775ffd83dbSDimitry Andric         [&request](uint32_t rid, std::string rname, std::string module,
9785ffd83dbSDimitry Andric                    llvm::ArrayRef<lldb_private::ConstString> symbols,
9795ffd83dbSDimitry Andric                    bool regexp) {
9805ffd83dbSDimitry Andric           StreamString strm;
9815ffd83dbSDimitry Andric           if (rname.empty())
9825ffd83dbSDimitry Andric             rname = "(internal)";
9835ffd83dbSDimitry Andric 
9845ffd83dbSDimitry Andric           strm << rname;
9855ffd83dbSDimitry Andric           if (!module.empty())
9865ffd83dbSDimitry Andric             strm << ", module " << module;
9875ffd83dbSDimitry Andric           if (!symbols.empty())
9885ffd83dbSDimitry Andric             for (auto &symbol : symbols)
9895ffd83dbSDimitry Andric               strm << ", symbol " << symbol;
9905ffd83dbSDimitry Andric           if (regexp)
9915ffd83dbSDimitry Andric             strm << " (regexp)";
9925ffd83dbSDimitry Andric 
9935ffd83dbSDimitry Andric           request.TryCompleteCurrentArg(std::to_string(rid), strm.GetString());
9945ffd83dbSDimitry Andric         });
9955ffd83dbSDimitry Andric   }
9965ffd83dbSDimitry Andric 
9970b57cec5SDimitry Andric protected:
9980b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
9990b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
10000b57cec5SDimitry Andric       if (!m_interpreter.Confirm(
10010b57cec5SDimitry Andric               "About to delete all frame recognizers, do you want to do that?",
10020b57cec5SDimitry Andric               true)) {
10030b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
10040b57cec5SDimitry Andric         return false;
10050b57cec5SDimitry Andric       }
10060b57cec5SDimitry Andric 
1007e8d8bef9SDimitry Andric       GetSelectedOrDummyTarget()
1008e8d8bef9SDimitry Andric           .GetFrameRecognizerManager()
1009e8d8bef9SDimitry Andric           .RemoveAllRecognizers();
10100b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
10110b57cec5SDimitry Andric       return result.Succeeded();
10120b57cec5SDimitry Andric     }
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
10150b57cec5SDimitry Andric       result.AppendErrorWithFormat("'%s' takes zero or one arguments.\n",
10160b57cec5SDimitry Andric                                    m_cmd_name.c_str());
10170b57cec5SDimitry Andric       return false;
10180b57cec5SDimitry Andric     }
10190b57cec5SDimitry Andric 
10205ffd83dbSDimitry Andric     uint32_t recognizer_id;
10215ffd83dbSDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), recognizer_id)) {
10225ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
10235ffd83dbSDimitry Andric                                    command.GetArgumentAtIndex(0));
10245ffd83dbSDimitry Andric       return false;
10255ffd83dbSDimitry Andric     }
10260b57cec5SDimitry Andric 
1027e8d8bef9SDimitry Andric     if (!GetSelectedOrDummyTarget()
1028e8d8bef9SDimitry Andric              .GetFrameRecognizerManager()
1029e8d8bef9SDimitry Andric              .RemoveRecognizerWithID(recognizer_id)) {
1030e8d8bef9SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
1031e8d8bef9SDimitry Andric                                    command.GetArgumentAtIndex(0));
1032e8d8bef9SDimitry Andric       return false;
1033e8d8bef9SDimitry Andric     }
10340b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
10350b57cec5SDimitry Andric     return result.Succeeded();
10360b57cec5SDimitry Andric   }
10370b57cec5SDimitry Andric };
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric class CommandObjectFrameRecognizerList : public CommandObjectParsed {
10400b57cec5SDimitry Andric public:
10410b57cec5SDimitry Andric   CommandObjectFrameRecognizerList(CommandInterpreter &interpreter)
10420b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer list",
10430b57cec5SDimitry Andric                             "Show a list of active frame recognizers.",
10440b57cec5SDimitry Andric                             nullptr) {}
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerList() override = default;
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric protected:
10490b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
10500b57cec5SDimitry Andric     bool any_printed = false;
1051e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
10525ffd83dbSDimitry Andric         [&result, &any_printed](
10535ffd83dbSDimitry Andric             uint32_t recognizer_id, std::string name, std::string module,
10545ffd83dbSDimitry Andric             llvm::ArrayRef<ConstString> symbols, bool regexp) {
10555ffd83dbSDimitry Andric           Stream &stream = result.GetOutputStream();
10565ffd83dbSDimitry Andric 
10575ffd83dbSDimitry Andric           if (name.empty())
1058480093f4SDimitry Andric             name = "(internal)";
10595ffd83dbSDimitry Andric 
10605ffd83dbSDimitry Andric           stream << std::to_string(recognizer_id) << ": " << name;
10615ffd83dbSDimitry Andric           if (!module.empty())
10625ffd83dbSDimitry Andric             stream << ", module " << module;
10635ffd83dbSDimitry Andric           if (!symbols.empty())
10645ffd83dbSDimitry Andric             for (auto &symbol : symbols)
10655ffd83dbSDimitry Andric               stream << ", symbol " << symbol;
10665ffd83dbSDimitry Andric           if (regexp)
10675ffd83dbSDimitry Andric             stream << " (regexp)";
10685ffd83dbSDimitry Andric 
10695ffd83dbSDimitry Andric           stream.EOL();
10705ffd83dbSDimitry Andric           stream.Flush();
10715ffd83dbSDimitry Andric 
10720b57cec5SDimitry Andric           any_printed = true;
10730b57cec5SDimitry Andric         });
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric     if (any_printed)
10760b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
10770b57cec5SDimitry Andric     else {
10780b57cec5SDimitry Andric       result.GetOutputStream().PutCString("no matching results found.\n");
10790b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
10800b57cec5SDimitry Andric     }
10810b57cec5SDimitry Andric     return result.Succeeded();
10820b57cec5SDimitry Andric   }
10830b57cec5SDimitry Andric };
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
10860b57cec5SDimitry Andric public:
10870b57cec5SDimitry Andric   CommandObjectFrameRecognizerInfo(CommandInterpreter &interpreter)
10880b57cec5SDimitry Andric       : CommandObjectParsed(
10890b57cec5SDimitry Andric             interpreter, "frame recognizer info",
10900b57cec5SDimitry Andric             "Show which frame recognizer is applied a stack frame (if any).",
10910b57cec5SDimitry Andric             nullptr) {
10920b57cec5SDimitry Andric     CommandArgumentEntry arg;
10930b57cec5SDimitry Andric     CommandArgumentData index_arg;
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric     // Define the first (and only) variant of this arg.
10960b57cec5SDimitry Andric     index_arg.arg_type = eArgTypeFrameIndex;
10970b57cec5SDimitry Andric     index_arg.arg_repetition = eArgRepeatPlain;
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric     // There is only one variant this argument could be; put it into the
11000b57cec5SDimitry Andric     // argument entry.
11010b57cec5SDimitry Andric     arg.push_back(index_arg);
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric     // Push the data for the first argument into the m_arguments vector.
11040b57cec5SDimitry Andric     m_arguments.push_back(arg);
11050b57cec5SDimitry Andric   }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerInfo() override = default;
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric protected:
11100b57cec5SDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
11115ffd83dbSDimitry Andric     const char *frame_index_str = command.GetArgumentAtIndex(0);
11125ffd83dbSDimitry Andric     uint32_t frame_index;
11135ffd83dbSDimitry Andric     if (!llvm::to_integer(frame_index_str, frame_index)) {
11145ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid frame index.",
11155ffd83dbSDimitry Andric                                    frame_index_str);
11165ffd83dbSDimitry Andric       return false;
11175ffd83dbSDimitry Andric     }
11185ffd83dbSDimitry Andric 
11190b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
11200b57cec5SDimitry Andric     if (process == nullptr) {
11210b57cec5SDimitry Andric       result.AppendError("no process");
11220b57cec5SDimitry Andric       return false;
11230b57cec5SDimitry Andric     }
11240b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
11250b57cec5SDimitry Andric     if (thread == nullptr) {
11260b57cec5SDimitry Andric       result.AppendError("no thread");
11270b57cec5SDimitry Andric       return false;
11280b57cec5SDimitry Andric     }
11290b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
11300b57cec5SDimitry Andric       result.AppendErrorWithFormat(
11310b57cec5SDimitry Andric           "'%s' takes exactly one frame index argument.\n", m_cmd_name.c_str());
11320b57cec5SDimitry Andric       return false;
11330b57cec5SDimitry Andric     }
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric     StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_index);
11360b57cec5SDimitry Andric     if (!frame_sp) {
11370b57cec5SDimitry Andric       result.AppendErrorWithFormat("no frame with index %u", frame_index);
11380b57cec5SDimitry Andric       return false;
11390b57cec5SDimitry Andric     }
11400b57cec5SDimitry Andric 
1141e8d8bef9SDimitry Andric     auto recognizer = GetSelectedOrDummyTarget()
1142e8d8bef9SDimitry Andric                           .GetFrameRecognizerManager()
1143e8d8bef9SDimitry Andric                           .GetRecognizerForFrame(frame_sp);
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
11460b57cec5SDimitry Andric     output_stream.Printf("frame %d ", frame_index);
11470b57cec5SDimitry Andric     if (recognizer) {
11480b57cec5SDimitry Andric       output_stream << "is recognized by ";
11490b57cec5SDimitry Andric       output_stream << recognizer->GetName();
11500b57cec5SDimitry Andric     } else {
11510b57cec5SDimitry Andric       output_stream << "not recognized by any recognizer";
11520b57cec5SDimitry Andric     }
11530b57cec5SDimitry Andric     output_stream.EOL();
11540b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
11550b57cec5SDimitry Andric     return result.Succeeded();
11560b57cec5SDimitry Andric   }
11570b57cec5SDimitry Andric };
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric class CommandObjectFrameRecognizer : public CommandObjectMultiword {
11600b57cec5SDimitry Andric public:
11610b57cec5SDimitry Andric   CommandObjectFrameRecognizer(CommandInterpreter &interpreter)
11620b57cec5SDimitry Andric       : CommandObjectMultiword(
11630b57cec5SDimitry Andric             interpreter, "frame recognizer",
11640b57cec5SDimitry Andric             "Commands for editing and viewing frame recognizers.",
11650b57cec5SDimitry Andric             "frame recognizer [<sub-command-options>] ") {
1166480093f4SDimitry Andric     LoadSubCommand("add", CommandObjectSP(new CommandObjectFrameRecognizerAdd(
1167480093f4SDimitry Andric                               interpreter)));
11680b57cec5SDimitry Andric     LoadSubCommand(
11690b57cec5SDimitry Andric         "clear",
11700b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerClear(interpreter)));
11710b57cec5SDimitry Andric     LoadSubCommand(
11720b57cec5SDimitry Andric         "delete",
11730b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerDelete(interpreter)));
1174480093f4SDimitry Andric     LoadSubCommand("list", CommandObjectSP(new CommandObjectFrameRecognizerList(
1175480093f4SDimitry Andric                                interpreter)));
1176480093f4SDimitry Andric     LoadSubCommand("info", CommandObjectSP(new CommandObjectFrameRecognizerInfo(
1177480093f4SDimitry Andric                                interpreter)));
11780b57cec5SDimitry Andric   }
11790b57cec5SDimitry Andric 
11800b57cec5SDimitry Andric   ~CommandObjectFrameRecognizer() override = default;
11810b57cec5SDimitry Andric };
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric #pragma mark CommandObjectMultiwordFrame
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric // CommandObjectMultiwordFrame
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric CommandObjectMultiwordFrame::CommandObjectMultiwordFrame(
11880b57cec5SDimitry Andric     CommandInterpreter &interpreter)
1189480093f4SDimitry Andric     : CommandObjectMultiword(interpreter, "frame",
1190480093f4SDimitry Andric                              "Commands for selecting and "
11910b57cec5SDimitry Andric                              "examing the current "
11920b57cec5SDimitry Andric                              "thread's stack frames.",
11930b57cec5SDimitry Andric                              "frame <subcommand> [<subcommand-options>]") {
11940b57cec5SDimitry Andric   LoadSubCommand("diagnose",
11950b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameDiagnose(interpreter)));
11960b57cec5SDimitry Andric   LoadSubCommand("info",
11970b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameInfo(interpreter)));
11980b57cec5SDimitry Andric   LoadSubCommand("select",
11990b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameSelect(interpreter)));
12000b57cec5SDimitry Andric   LoadSubCommand("variable",
12010b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameVariable(interpreter)));
1202480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
1203480093f4SDimitry Andric   LoadSubCommand("recognizer", CommandObjectSP(new CommandObjectFrameRecognizer(
1204480093f4SDimitry Andric                                    interpreter)));
12050b57cec5SDimitry Andric #endif
12060b57cec5SDimitry Andric }
12070b57cec5SDimitry Andric 
12080b57cec5SDimitry Andric CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame() = default;
1209