xref: /freebsd-src/contrib/llvm-project/lldb/source/Commands/CommandObjectFrame.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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) {
116*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex, eArgRepeatOptional);
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   ~CommandObjectFrameDiagnose() override = default;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric protected:
1245f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
1250b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
12606c3fb27SDimitry Andric     StackFrameSP frame_sp = thread->GetSelectedFrame(SelectMostRelevantFrame);
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
1290b57cec5SDimitry Andric 
13081ad6265SDimitry Andric     if (m_options.address) {
13181ad6265SDimitry Andric       if (m_options.reg || m_options.offset) {
1320b57cec5SDimitry Andric         result.AppendError(
1330b57cec5SDimitry Andric             "`frame diagnose --address` is incompatible with other arguments.");
1345f757f3fSDimitry Andric         return;
1350b57cec5SDimitry Andric       }
136bdd1243dSDimitry Andric       valobj_sp = frame_sp->GuessValueForAddress(*m_options.address);
13781ad6265SDimitry Andric     } else if (m_options.reg) {
1380b57cec5SDimitry Andric       valobj_sp = frame_sp->GuessValueForRegisterAndOffset(
139bdd1243dSDimitry Andric           *m_options.reg, m_options.offset.value_or(0));
1400b57cec5SDimitry Andric     } else {
1410b57cec5SDimitry Andric       StopInfoSP stop_info_sp = thread->GetStopInfo();
1420b57cec5SDimitry Andric       if (!stop_info_sp) {
1430b57cec5SDimitry Andric         result.AppendError("No arguments provided, and no stop info.");
1445f757f3fSDimitry Andric         return;
1450b57cec5SDimitry Andric       }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric       valobj_sp = StopInfo::GetCrashingDereference(stop_info_sp);
1480b57cec5SDimitry Andric     }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric     if (!valobj_sp) {
1510b57cec5SDimitry Andric       result.AppendError("No diagnosis available.");
1525f757f3fSDimitry Andric       return;
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric 
155480093f4SDimitry Andric     DumpValueObjectOptions::DeclPrintingHelper helper =
156480093f4SDimitry Andric         [&valobj_sp](ConstString type, ConstString var,
157480093f4SDimitry Andric                      const DumpValueObjectOptions &opts,
1580b57cec5SDimitry Andric                      Stream &stream) -> bool {
1590b57cec5SDimitry Andric       const ValueObject::GetExpressionPathFormat format = ValueObject::
1600b57cec5SDimitry Andric           GetExpressionPathFormat::eGetExpressionPathFormatHonorPointers;
1615ffd83dbSDimitry Andric       valobj_sp->GetExpressionPath(stream, format);
1620b57cec5SDimitry Andric       stream.PutCString(" =");
1630b57cec5SDimitry Andric       return true;
1640b57cec5SDimitry Andric     };
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     DumpValueObjectOptions options;
1670b57cec5SDimitry Andric     options.SetDeclPrintingHelper(helper);
168*0fca6ea1SDimitry Andric     // We've already handled the case where the value object sp is null, so
169*0fca6ea1SDimitry Andric     // this is just to make sure future changes don't skip that:
170*0fca6ea1SDimitry Andric     assert(valobj_sp.get() && "Must have a valid ValueObject to print");
171*0fca6ea1SDimitry Andric     ValueObjectPrinter printer(*valobj_sp, &result.GetOutputStream(),
1720b57cec5SDimitry Andric                                options);
173*0fca6ea1SDimitry Andric     if (llvm::Error error = printer.PrintValueObject())
174*0fca6ea1SDimitry Andric       result.AppendError(toString(std::move(error)));
1750b57cec5SDimitry Andric   }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   CommandOptions m_options;
1780b57cec5SDimitry Andric };
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric #pragma mark CommandObjectFrameInfo
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric // CommandObjectFrameInfo
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric class CommandObjectFrameInfo : public CommandObjectParsed {
1850b57cec5SDimitry Andric public:
1860b57cec5SDimitry Andric   CommandObjectFrameInfo(CommandInterpreter &interpreter)
187480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame info",
188480093f4SDimitry Andric                             "List information about the current "
1890b57cec5SDimitry Andric                             "stack frame in the current thread.",
1900b57cec5SDimitry Andric                             "frame info",
1910b57cec5SDimitry Andric                             eCommandRequiresFrame | eCommandTryTargetAPILock |
192480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
193480093f4SDimitry Andric                                 eCommandProcessMustBePaused) {}
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   ~CommandObjectFrameInfo() override = default;
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric protected:
1985f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
1990b57cec5SDimitry Andric     m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat(&result.GetOutputStream());
2000b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
2010b57cec5SDimitry Andric   }
2020b57cec5SDimitry Andric };
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric #pragma mark CommandObjectFrameSelect
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric // CommandObjectFrameSelect
2070b57cec5SDimitry Andric 
2089dba64beSDimitry Andric #define LLDB_OPTIONS_frame_select
2099dba64beSDimitry Andric #include "CommandOptions.inc"
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric class CommandObjectFrameSelect : public CommandObjectParsed {
2120b57cec5SDimitry Andric public:
2130b57cec5SDimitry Andric   class CommandOptions : public Options {
2140b57cec5SDimitry Andric   public:
21504eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     ~CommandOptions() override = default;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2200b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
2210b57cec5SDimitry Andric       Status error;
2220b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
2230b57cec5SDimitry Andric       switch (short_option) {
2249dba64beSDimitry Andric       case 'r': {
2259dba64beSDimitry Andric         int32_t offset = 0;
2269dba64beSDimitry Andric         if (option_arg.getAsInteger(0, offset) || offset == INT32_MIN) {
2270b57cec5SDimitry Andric           error.SetErrorStringWithFormat("invalid frame offset argument '%s'",
2280b57cec5SDimitry Andric                                          option_arg.str().c_str());
2299dba64beSDimitry Andric         } else
2309dba64beSDimitry Andric           relative_frame_offset = offset;
2310b57cec5SDimitry Andric         break;
2329dba64beSDimitry Andric       }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric       default:
2359dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
2360b57cec5SDimitry Andric       }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric       return error;
2390b57cec5SDimitry Andric     }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
2429dba64beSDimitry Andric       relative_frame_offset.reset();
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
246bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_select_options);
2470b57cec5SDimitry Andric     }
2480b57cec5SDimitry Andric 
249bdd1243dSDimitry Andric     std::optional<int32_t> relative_frame_offset;
2500b57cec5SDimitry Andric   };
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   CommandObjectFrameSelect(CommandInterpreter &interpreter)
253480093f4SDimitry Andric       : CommandObjectParsed(interpreter, "frame select",
254480093f4SDimitry Andric                             "Select the current stack frame by "
2550b57cec5SDimitry Andric                             "index from within the current thread "
2560b57cec5SDimitry Andric                             "(see 'thread backtrace'.)",
2570b57cec5SDimitry Andric                             nullptr,
2580b57cec5SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
259480093f4SDimitry Andric                                 eCommandProcessMustBeLaunched |
26004eeddc0SDimitry Andric                                 eCommandProcessMustBePaused) {
261*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex, eArgRepeatOptional);
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   ~CommandObjectFrameSelect() override = default;
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric protected:
2695f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
2700b57cec5SDimitry Andric     // No need to check "thread" for validity as eCommandRequiresThread ensures
2710b57cec5SDimitry Andric     // it is valid
2720b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     uint32_t frame_idx = UINT32_MAX;
27581ad6265SDimitry Andric     if (m_options.relative_frame_offset) {
2760b57cec5SDimitry Andric       // The one and only argument is a signed relative frame index
27706c3fb27SDimitry Andric       frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
2780b57cec5SDimitry Andric       if (frame_idx == UINT32_MAX)
2790b57cec5SDimitry Andric         frame_idx = 0;
2800b57cec5SDimitry Andric 
2819dba64beSDimitry Andric       if (*m_options.relative_frame_offset < 0) {
2829dba64beSDimitry Andric         if (static_cast<int32_t>(frame_idx) >=
2839dba64beSDimitry Andric             -*m_options.relative_frame_offset)
2849dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
2850b57cec5SDimitry Andric         else {
2860b57cec5SDimitry Andric           if (frame_idx == 0) {
2870b57cec5SDimitry Andric             // If you are already at the bottom of the stack, then just warn
2880b57cec5SDimitry Andric             // and don't reset the frame.
2890b57cec5SDimitry Andric             result.AppendError("Already at the bottom of the stack.");
2905f757f3fSDimitry Andric             return;
2910b57cec5SDimitry Andric           } else
2920b57cec5SDimitry Andric             frame_idx = 0;
2930b57cec5SDimitry Andric         }
2949dba64beSDimitry Andric       } else if (*m_options.relative_frame_offset > 0) {
2950b57cec5SDimitry Andric         // I don't want "up 20" where "20" takes you past the top of the stack
29606c3fb27SDimitry Andric         // to produce an error, but rather to just go to the top.  OTOH, start
29706c3fb27SDimitry Andric         // by seeing if the requested frame exists, in which case we can avoid
29806c3fb27SDimitry Andric         // counting the stack here...
29906c3fb27SDimitry Andric         const uint32_t frame_requested = frame_idx
30006c3fb27SDimitry Andric             + *m_options.relative_frame_offset;
30106c3fb27SDimitry Andric         StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_requested);
30206c3fb27SDimitry Andric         if (frame_sp)
30306c3fb27SDimitry Andric           frame_idx = frame_requested;
30406c3fb27SDimitry Andric         else {
30506c3fb27SDimitry Andric           // The request went past the stack, so handle that case:
3060b57cec5SDimitry Andric           const uint32_t num_frames = thread->GetStackFrameCount();
3070b57cec5SDimitry Andric           if (static_cast<int32_t>(num_frames - frame_idx) >
3089dba64beSDimitry Andric               *m_options.relative_frame_offset)
3099dba64beSDimitry Andric           frame_idx += *m_options.relative_frame_offset;
3100b57cec5SDimitry Andric           else {
3110b57cec5SDimitry Andric             if (frame_idx == num_frames - 1) {
3120b57cec5SDimitry Andric               // If we are already at the top of the stack, just warn and don't
3130b57cec5SDimitry Andric               // reset the frame.
3140b57cec5SDimitry Andric               result.AppendError("Already at the top of the stack.");
3155f757f3fSDimitry Andric               return;
3160b57cec5SDimitry Andric             } else
3170b57cec5SDimitry Andric               frame_idx = num_frames - 1;
3180b57cec5SDimitry Andric           }
3190b57cec5SDimitry Andric         }
32006c3fb27SDimitry Andric       }
3210b57cec5SDimitry Andric     } else {
3220b57cec5SDimitry Andric       if (command.GetArgumentCount() > 1) {
3230b57cec5SDimitry Andric         result.AppendErrorWithFormat(
3240b57cec5SDimitry Andric             "too many arguments; expected frame-index, saw '%s'.\n",
3250b57cec5SDimitry Andric             command[0].c_str());
3260b57cec5SDimitry Andric         m_options.GenerateOptionUsage(
32781ad6265SDimitry Andric             result.GetErrorStream(), *this,
3280b57cec5SDimitry Andric             GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3295f757f3fSDimitry Andric         return;
3300b57cec5SDimitry Andric       }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric       if (command.GetArgumentCount() == 1) {
3339dba64beSDimitry Andric         if (command[0].ref().getAsInteger(0, frame_idx)) {
3340b57cec5SDimitry Andric           result.AppendErrorWithFormat("invalid frame index argument '%s'.",
3350b57cec5SDimitry Andric                                        command[0].c_str());
3365f757f3fSDimitry Andric           return;
3370b57cec5SDimitry Andric         }
3380b57cec5SDimitry Andric       } else if (command.GetArgumentCount() == 0) {
33906c3fb27SDimitry Andric         frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
3400b57cec5SDimitry Andric         if (frame_idx == UINT32_MAX) {
3410b57cec5SDimitry Andric           frame_idx = 0;
3420b57cec5SDimitry Andric         }
3430b57cec5SDimitry Andric       }
3440b57cec5SDimitry Andric     }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric     bool success = thread->SetSelectedFrameByIndexNoisily(
3470b57cec5SDimitry Andric         frame_idx, result.GetOutputStream());
3480b57cec5SDimitry Andric     if (success) {
34906c3fb27SDimitry Andric       m_exe_ctx.SetFrameSP(thread->GetSelectedFrame(SelectMostRelevantFrame));
3500b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
3510b57cec5SDimitry Andric     } else {
3520b57cec5SDimitry Andric       result.AppendErrorWithFormat("Frame index (%u) out of range.\n",
3530b57cec5SDimitry Andric                                    frame_idx);
3540b57cec5SDimitry Andric     }
3550b57cec5SDimitry Andric   }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   CommandOptions m_options;
3580b57cec5SDimitry Andric };
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric #pragma mark CommandObjectFrameVariable
3610b57cec5SDimitry Andric // List images with associated information
3620b57cec5SDimitry Andric class CommandObjectFrameVariable : public CommandObjectParsed {
3630b57cec5SDimitry Andric public:
3640b57cec5SDimitry Andric   CommandObjectFrameVariable(CommandInterpreter &interpreter)
3650b57cec5SDimitry Andric       : CommandObjectParsed(
3660b57cec5SDimitry Andric             interpreter, "frame variable",
3670b57cec5SDimitry Andric             "Show variables for the current stack frame. Defaults to all "
3680b57cec5SDimitry Andric             "arguments and local variables in scope. Names of argument, "
36904eeddc0SDimitry Andric             "local, file static and file global variables can be specified.",
370480093f4SDimitry Andric             nullptr,
371480093f4SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
372480093f4SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
373480093f4SDimitry Andric                 eCommandRequiresProcess),
3740b57cec5SDimitry Andric         m_option_variable(
3750b57cec5SDimitry Andric             true), // Include the frame specific options by passing "true"
37604eeddc0SDimitry Andric         m_option_format(eFormatDefault) {
37704eeddc0SDimitry Andric     SetHelpLong(R"(
37804eeddc0SDimitry Andric Children of aggregate variables can be specified such as 'var->child.x'.  In
37904eeddc0SDimitry Andric 'frame variable', the operators -> and [] do not invoke operator overloads if
38004eeddc0SDimitry Andric they exist, but directly access the specified element.  If you want to trigger
38104eeddc0SDimitry Andric operator overloads use the expression command to print the variable instead.
38204eeddc0SDimitry Andric 
38304eeddc0SDimitry Andric It is worth noting that except for overloaded operators, when printing local
38404eeddc0SDimitry Andric variables 'expr local_var' and 'frame var local_var' produce the same results.
38504eeddc0SDimitry Andric However, 'frame variable' is more efficient, since it uses debug information and
38604eeddc0SDimitry Andric memory reads directly, rather than parsing and evaluating an expression, which
38704eeddc0SDimitry Andric may even involve JITing and running code in the target program.)");
38804eeddc0SDimitry Andric 
389*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeVarName, eArgRepeatStar);
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3920b57cec5SDimitry Andric     m_option_group.Append(&m_option_format,
3930b57cec5SDimitry Andric                           OptionGroupFormat::OPTION_GROUP_FORMAT |
3940b57cec5SDimitry Andric                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
3950b57cec5SDimitry Andric                           LLDB_OPT_SET_1);
3960b57cec5SDimitry Andric     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3970b57cec5SDimitry Andric     m_option_group.Finalize();
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   ~CommandObjectFrameVariable() override = default;
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric protected:
4050b57cec5SDimitry Andric   llvm::StringRef GetScopeString(VariableSP var_sp) {
4060b57cec5SDimitry Andric     if (!var_sp)
407fe6060f1SDimitry Andric       return llvm::StringRef();
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric     switch (var_sp->GetScope()) {
4100b57cec5SDimitry Andric     case eValueTypeVariableGlobal:
4110b57cec5SDimitry Andric       return "GLOBAL: ";
4120b57cec5SDimitry Andric     case eValueTypeVariableStatic:
4130b57cec5SDimitry Andric       return "STATIC: ";
4140b57cec5SDimitry Andric     case eValueTypeVariableArgument:
4150b57cec5SDimitry Andric       return "ARG: ";
4160b57cec5SDimitry Andric     case eValueTypeVariableLocal:
4170b57cec5SDimitry Andric       return "LOCAL: ";
4180b57cec5SDimitry Andric     case eValueTypeVariableThreadLocal:
4190b57cec5SDimitry Andric       return "THREAD: ";
4200b57cec5SDimitry Andric     default:
4210b57cec5SDimitry Andric       break;
4220b57cec5SDimitry Andric     }
4230b57cec5SDimitry Andric 
424fe6060f1SDimitry Andric     return llvm::StringRef();
4250b57cec5SDimitry Andric   }
4260b57cec5SDimitry Andric 
42706c3fb27SDimitry Andric   /// Returns true if `scope` matches any of the options in `m_option_variable`.
42806c3fb27SDimitry Andric   bool ScopeRequested(lldb::ValueType scope) {
42906c3fb27SDimitry Andric     switch (scope) {
43006c3fb27SDimitry Andric     case eValueTypeVariableGlobal:
43106c3fb27SDimitry Andric     case eValueTypeVariableStatic:
43206c3fb27SDimitry Andric       return m_option_variable.show_globals;
43306c3fb27SDimitry Andric     case eValueTypeVariableArgument:
43406c3fb27SDimitry Andric       return m_option_variable.show_args;
43506c3fb27SDimitry Andric     case eValueTypeVariableLocal:
43606c3fb27SDimitry Andric       return m_option_variable.show_locals;
43706c3fb27SDimitry Andric     case eValueTypeInvalid:
43806c3fb27SDimitry Andric     case eValueTypeRegister:
43906c3fb27SDimitry Andric     case eValueTypeRegisterSet:
44006c3fb27SDimitry Andric     case eValueTypeConstResult:
44106c3fb27SDimitry Andric     case eValueTypeVariableThreadLocal:
4425f757f3fSDimitry Andric     case eValueTypeVTable:
4435f757f3fSDimitry Andric     case eValueTypeVTableEntry:
44406c3fb27SDimitry Andric       return false;
44506c3fb27SDimitry Andric     }
4467a6dacacSDimitry Andric     llvm_unreachable("Unexpected scope value");
44706c3fb27SDimitry Andric   }
44806c3fb27SDimitry Andric 
44906c3fb27SDimitry Andric   /// Finds all the variables in `all_variables` whose name matches `regex`,
45006c3fb27SDimitry Andric   /// inserting them into `matches`. Variables already contained in `matches`
45106c3fb27SDimitry Andric   /// are not inserted again.
45206c3fb27SDimitry Andric   /// Nullopt is returned in case of no matches.
45306c3fb27SDimitry Andric   /// A sub-range of `matches` with all newly inserted variables is returned.
45406c3fb27SDimitry Andric   /// This may be empty if all matches were already contained in `matches`.
45506c3fb27SDimitry Andric   std::optional<llvm::ArrayRef<VariableSP>>
45606c3fb27SDimitry Andric   findUniqueRegexMatches(RegularExpression &regex,
45706c3fb27SDimitry Andric                          VariableList &matches,
45806c3fb27SDimitry Andric                          const VariableList &all_variables) {
45906c3fb27SDimitry Andric     bool any_matches = false;
46006c3fb27SDimitry Andric     const size_t previous_num_vars = matches.GetSize();
46106c3fb27SDimitry Andric 
46206c3fb27SDimitry Andric     for (const VariableSP &var : all_variables) {
46306c3fb27SDimitry Andric       if (!var->NameMatches(regex) || !ScopeRequested(var->GetScope()))
46406c3fb27SDimitry Andric         continue;
46506c3fb27SDimitry Andric       any_matches = true;
46606c3fb27SDimitry Andric       matches.AddVariableIfUnique(var);
46706c3fb27SDimitry Andric     }
46806c3fb27SDimitry Andric 
46906c3fb27SDimitry Andric     if (any_matches)
47006c3fb27SDimitry Andric       return matches.toArrayRef().drop_front(previous_num_vars);
47106c3fb27SDimitry Andric     return std::nullopt;
47206c3fb27SDimitry Andric   }
47306c3fb27SDimitry Andric 
4745f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
4750b57cec5SDimitry Andric     // No need to check "frame" for validity as eCommandRequiresFrame ensures
4760b57cec5SDimitry Andric     // it is valid
4770b57cec5SDimitry Andric     StackFrame *frame = m_exe_ctx.GetFramePtr();
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric     Stream &s = result.GetOutputStream();
4800b57cec5SDimitry Andric 
48106c3fb27SDimitry Andric     // Using a regex should behave like looking for an exact name match: it
48206c3fb27SDimitry Andric     // also finds globals.
48306c3fb27SDimitry Andric     m_option_variable.show_globals |= m_option_variable.use_regex;
48406c3fb27SDimitry Andric 
4850b57cec5SDimitry Andric     // Be careful about the stack frame, if any summary formatter runs code, it
4860b57cec5SDimitry Andric     // might clear the StackFrameList for the thread.  So hold onto a shared
4870b57cec5SDimitry Andric     // pointer to the frame so it stays alive.
4880b57cec5SDimitry Andric 
489bdd1243dSDimitry Andric     Status error;
4900b57cec5SDimitry Andric     VariableList *variable_list =
491bdd1243dSDimitry Andric         frame->GetVariableList(m_option_variable.show_globals, &error);
4920b57cec5SDimitry Andric 
493bdd1243dSDimitry Andric     if (error.Fail() && (!variable_list || variable_list->GetSize() == 0)) {
494bdd1243dSDimitry Andric       result.AppendError(error.AsCString());
495bdd1243dSDimitry Andric 
496bdd1243dSDimitry Andric     }
4970b57cec5SDimitry Andric     ValueObjectSP valobj_sp;
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric     TypeSummaryImplSP summary_format_sp;
5000b57cec5SDimitry Andric     if (!m_option_variable.summary.IsCurrentValueEmpty())
5010b57cec5SDimitry Andric       DataVisualization::NamedSummaryFormats::GetSummaryFormat(
5020b57cec5SDimitry Andric           ConstString(m_option_variable.summary.GetCurrentValue()),
5030b57cec5SDimitry Andric           summary_format_sp);
5040b57cec5SDimitry Andric     else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
5050b57cec5SDimitry Andric       summary_format_sp = std::make_shared<StringSummaryFormat>(
5060b57cec5SDimitry Andric           TypeSummaryImpl::Flags(),
5070b57cec5SDimitry Andric           m_option_variable.summary_string.GetCurrentValue());
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
5100b57cec5SDimitry Andric         eLanguageRuntimeDescriptionDisplayVerbosityFull, eFormatDefault,
5110b57cec5SDimitry Andric         summary_format_sp));
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric     const SymbolContext &sym_ctx =
5140b57cec5SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction);
5150b57cec5SDimitry Andric     if (sym_ctx.function && sym_ctx.function->IsTopLevelFunction())
5160b57cec5SDimitry Andric       m_option_variable.show_globals = true;
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric     if (variable_list) {
5190b57cec5SDimitry Andric       const Format format = m_option_format.GetFormat();
5200b57cec5SDimitry Andric       options.SetFormat(format);
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric       if (!command.empty()) {
5230b57cec5SDimitry Andric         VariableList regex_var_list;
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric         // If we have any args to the variable command, we will make variable
5260b57cec5SDimitry Andric         // objects from them...
5270b57cec5SDimitry Andric         for (auto &entry : command) {
5280b57cec5SDimitry Andric           if (m_option_variable.use_regex) {
5299dba64beSDimitry Andric             llvm::StringRef name_str = entry.ref();
5300b57cec5SDimitry Andric             RegularExpression regex(name_str);
5319dba64beSDimitry Andric             if (regex.IsValid()) {
53206c3fb27SDimitry Andric               std::optional<llvm::ArrayRef<VariableSP>> results =
53306c3fb27SDimitry Andric                   findUniqueRegexMatches(regex, regex_var_list, *variable_list);
53406c3fb27SDimitry Andric               if (!results) {
53506c3fb27SDimitry Andric                 result.AppendErrorWithFormat(
53606c3fb27SDimitry Andric                     "no variables matched the regular expression '%s'.",
53706c3fb27SDimitry Andric                     entry.c_str());
53806c3fb27SDimitry Andric                 continue;
53906c3fb27SDimitry Andric               }
54006c3fb27SDimitry Andric               for (const VariableSP &var_sp : *results) {
5410b57cec5SDimitry Andric                 valobj_sp = frame->GetValueObjectForFrameVariable(
5420b57cec5SDimitry Andric                     var_sp, m_varobj_options.use_dynamic);
5430b57cec5SDimitry Andric                 if (valobj_sp) {
5440b57cec5SDimitry Andric                   std::string scope_string;
5450b57cec5SDimitry Andric                   if (m_option_variable.show_scope)
5460b57cec5SDimitry Andric                     scope_string = GetScopeString(var_sp).str();
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric                   if (!scope_string.empty())
5490b57cec5SDimitry Andric                     s.PutCString(scope_string);
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric                   if (m_option_variable.show_decl &&
5520b57cec5SDimitry Andric                       var_sp->GetDeclaration().GetFile()) {
5530b57cec5SDimitry Andric                     bool show_fullpaths = false;
5540b57cec5SDimitry Andric                     bool show_module = true;
5550b57cec5SDimitry Andric                     if (var_sp->DumpDeclaration(&s, show_fullpaths,
5560b57cec5SDimitry Andric                                                 show_module))
5570b57cec5SDimitry Andric                       s.PutCString(": ");
5580b57cec5SDimitry Andric                   }
559*0fca6ea1SDimitry Andric                   auto &strm = result.GetOutputStream();
560*0fca6ea1SDimitry Andric                   if (llvm::Error error = valobj_sp->Dump(strm, options))
561*0fca6ea1SDimitry Andric                     result.AppendError(toString(std::move(error)));
5620b57cec5SDimitry Andric                 }
5630b57cec5SDimitry Andric               }
5640b57cec5SDimitry Andric             } else {
5659dba64beSDimitry Andric               if (llvm::Error err = regex.GetError())
56604eeddc0SDimitry Andric                 result.AppendError(llvm::toString(std::move(err)));
5670b57cec5SDimitry Andric               else
56804eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
56904eeddc0SDimitry Andric                     "unknown regex error when compiling '%s'", entry.c_str());
5700b57cec5SDimitry Andric             }
5710b57cec5SDimitry Andric           } else // No regex, either exact variable names or variable
5720b57cec5SDimitry Andric                  // expressions.
5730b57cec5SDimitry Andric           {
5740b57cec5SDimitry Andric             Status error;
5750b57cec5SDimitry Andric             uint32_t expr_path_options =
5760b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionCheckPtrVsMember |
5770b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess |
5780b57cec5SDimitry Andric                 StackFrame::eExpressionPathOptionsInspectAnonymousUnions;
5790b57cec5SDimitry Andric             lldb::VariableSP var_sp;
5800b57cec5SDimitry Andric             valobj_sp = frame->GetValueForVariableExpressionPath(
5819dba64beSDimitry Andric                 entry.ref(), m_varobj_options.use_dynamic, expr_path_options,
5820b57cec5SDimitry Andric                 var_sp, error);
5830b57cec5SDimitry Andric             if (valobj_sp) {
5840b57cec5SDimitry Andric               std::string scope_string;
5850b57cec5SDimitry Andric               if (m_option_variable.show_scope)
5860b57cec5SDimitry Andric                 scope_string = GetScopeString(var_sp).str();
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric               if (!scope_string.empty())
5890b57cec5SDimitry Andric                 s.PutCString(scope_string);
5900b57cec5SDimitry Andric               if (m_option_variable.show_decl && var_sp &&
5910b57cec5SDimitry Andric                   var_sp->GetDeclaration().GetFile()) {
5920b57cec5SDimitry Andric                 var_sp->GetDeclaration().DumpStopContext(&s, false);
5930b57cec5SDimitry Andric                 s.PutCString(": ");
5940b57cec5SDimitry Andric               }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric               options.SetFormat(format);
5970b57cec5SDimitry Andric               options.SetVariableFormatDisplayLanguage(
5980b57cec5SDimitry Andric                   valobj_sp->GetPreferredDisplayLanguage());
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric               Stream &output_stream = result.GetOutputStream();
6010b57cec5SDimitry Andric               options.SetRootValueObjectName(
6020b57cec5SDimitry Andric                   valobj_sp->GetParent() ? entry.c_str() : nullptr);
603*0fca6ea1SDimitry Andric               if (llvm::Error error = valobj_sp->Dump(output_stream, options))
604*0fca6ea1SDimitry Andric                 result.AppendError(toString(std::move(error)));
6050b57cec5SDimitry Andric             } else {
60604eeddc0SDimitry Andric               if (auto error_cstr = error.AsCString(nullptr))
60704eeddc0SDimitry Andric                 result.AppendError(error_cstr);
6080b57cec5SDimitry Andric               else
60904eeddc0SDimitry Andric                 result.AppendErrorWithFormat(
61004eeddc0SDimitry Andric                     "unable to find any variable expression path that matches "
61104eeddc0SDimitry Andric                     "'%s'.",
6120b57cec5SDimitry Andric                     entry.c_str());
6130b57cec5SDimitry Andric             }
6140b57cec5SDimitry Andric           }
6150b57cec5SDimitry Andric         }
6160b57cec5SDimitry Andric       } else // No command arg specified.  Use variable_list, instead.
6170b57cec5SDimitry Andric       {
6180b57cec5SDimitry Andric         const size_t num_variables = variable_list->GetSize();
6190b57cec5SDimitry Andric         if (num_variables > 0) {
6200b57cec5SDimitry Andric           for (size_t i = 0; i < num_variables; i++) {
62106c3fb27SDimitry Andric             VariableSP var_sp = variable_list->GetVariableAtIndex(i);
62206c3fb27SDimitry Andric             if (!ScopeRequested(var_sp->GetScope()))
6230b57cec5SDimitry Andric                 continue;
6240b57cec5SDimitry Andric             std::string scope_string;
6250b57cec5SDimitry Andric             if (m_option_variable.show_scope)
6260b57cec5SDimitry Andric               scope_string = GetScopeString(var_sp).str();
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric             // Use the variable object code to make sure we are using the same
6290b57cec5SDimitry Andric             // APIs as the public API will be using...
6300b57cec5SDimitry Andric             valobj_sp = frame->GetValueObjectForFrameVariable(
6310b57cec5SDimitry Andric                 var_sp, m_varobj_options.use_dynamic);
6320b57cec5SDimitry Andric             if (valobj_sp) {
6330b57cec5SDimitry Andric               // When dumping all variables, don't print any variables that are
6340b57cec5SDimitry Andric               // not in scope to avoid extra unneeded output
6350b57cec5SDimitry Andric               if (valobj_sp->IsInScope()) {
6360b57cec5SDimitry Andric                 if (!valobj_sp->GetTargetSP()
6370b57cec5SDimitry Andric                          ->GetDisplayRuntimeSupportValues() &&
6380b57cec5SDimitry Andric                     valobj_sp->IsRuntimeSupportValue())
6390b57cec5SDimitry Andric                   continue;
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric                 if (!scope_string.empty())
6420b57cec5SDimitry Andric                   s.PutCString(scope_string);
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric                 if (m_option_variable.show_decl &&
6450b57cec5SDimitry Andric                     var_sp->GetDeclaration().GetFile()) {
6460b57cec5SDimitry Andric                   var_sp->GetDeclaration().DumpStopContext(&s, false);
6470b57cec5SDimitry Andric                   s.PutCString(": ");
6480b57cec5SDimitry Andric                 }
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric                 options.SetFormat(format);
6510b57cec5SDimitry Andric                 options.SetVariableFormatDisplayLanguage(
6520b57cec5SDimitry Andric                     valobj_sp->GetPreferredDisplayLanguage());
6530b57cec5SDimitry Andric                 options.SetRootValueObjectName(
6540b57cec5SDimitry Andric                     var_sp ? var_sp->GetName().AsCString() : nullptr);
655*0fca6ea1SDimitry Andric                 if (llvm::Error error =
656*0fca6ea1SDimitry Andric                         valobj_sp->Dump(result.GetOutputStream(), options))
657*0fca6ea1SDimitry Andric                   result.AppendError(toString(std::move(error)));
6580b57cec5SDimitry Andric               }
6590b57cec5SDimitry Andric             }
6600b57cec5SDimitry Andric           }
6610b57cec5SDimitry Andric         }
6620b57cec5SDimitry Andric       }
66304eeddc0SDimitry Andric       if (result.GetStatus() != eReturnStatusFailed)
6640b57cec5SDimitry Andric         result.SetStatus(eReturnStatusSuccessFinishResult);
6650b57cec5SDimitry Andric     }
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric     if (m_option_variable.show_recognized_args) {
6680b57cec5SDimitry Andric       auto recognized_frame = frame->GetRecognizedFrame();
6690b57cec5SDimitry Andric       if (recognized_frame) {
6700b57cec5SDimitry Andric         ValueObjectListSP recognized_arg_list =
6710b57cec5SDimitry Andric             recognized_frame->GetRecognizedArguments();
6720b57cec5SDimitry Andric         if (recognized_arg_list) {
6730b57cec5SDimitry Andric           for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
6740b57cec5SDimitry Andric             options.SetFormat(m_option_format.GetFormat());
6750b57cec5SDimitry Andric             options.SetVariableFormatDisplayLanguage(
6760b57cec5SDimitry Andric                 rec_value_sp->GetPreferredDisplayLanguage());
6770b57cec5SDimitry Andric             options.SetRootValueObjectName(rec_value_sp->GetName().AsCString());
678*0fca6ea1SDimitry Andric             if (llvm::Error error =
679*0fca6ea1SDimitry Andric                     rec_value_sp->Dump(result.GetOutputStream(), options))
680*0fca6ea1SDimitry Andric               result.AppendError(toString(std::move(error)));
6810b57cec5SDimitry Andric           }
6820b57cec5SDimitry Andric         }
6830b57cec5SDimitry Andric       }
6840b57cec5SDimitry Andric     }
6850b57cec5SDimitry Andric 
68681ad6265SDimitry Andric     m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
68781ad6265SDimitry Andric                                            m_cmd_name);
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric     // Increment statistics.
690349cc55cSDimitry Andric     TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
6915f757f3fSDimitry Andric     if (result.Succeeded())
692349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifySuccess();
6930b57cec5SDimitry Andric     else
694349cc55cSDimitry Andric       target_stats.GetFrameVariableStats().NotifyFailure();
6950b57cec5SDimitry Andric   }
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric   OptionGroupOptions m_option_group;
6980b57cec5SDimitry Andric   OptionGroupVariable m_option_variable;
6990b57cec5SDimitry Andric   OptionGroupFormat m_option_format;
7000b57cec5SDimitry Andric   OptionGroupValueObjectDisplay m_varobj_options;
7010b57cec5SDimitry Andric };
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric #pragma mark CommandObjectFrameRecognizer
7040b57cec5SDimitry Andric 
7059dba64beSDimitry Andric #define LLDB_OPTIONS_frame_recognizer_add
7069dba64beSDimitry Andric #include "CommandOptions.inc"
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric class CommandObjectFrameRecognizerAdd : public CommandObjectParsed {
7090b57cec5SDimitry Andric private:
7100b57cec5SDimitry Andric   class CommandOptions : public Options {
7110b57cec5SDimitry Andric   public:
71281ad6265SDimitry Andric     CommandOptions() = default;
7130b57cec5SDimitry Andric     ~CommandOptions() override = default;
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
7160b57cec5SDimitry Andric                           ExecutionContext *execution_context) override {
7170b57cec5SDimitry Andric       Status error;
7180b57cec5SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric       switch (short_option) {
721349cc55cSDimitry Andric       case 'f': {
722349cc55cSDimitry Andric         bool value, success;
723349cc55cSDimitry Andric         value = OptionArgParser::ToBoolean(option_arg, true, &success);
724349cc55cSDimitry Andric         if (success) {
725349cc55cSDimitry Andric           m_first_instruction_only = value;
726349cc55cSDimitry Andric         } else {
727349cc55cSDimitry Andric           error.SetErrorStringWithFormat(
728349cc55cSDimitry Andric               "invalid boolean value '%s' passed for -f option",
729349cc55cSDimitry Andric               option_arg.str().c_str());
730349cc55cSDimitry Andric         }
731349cc55cSDimitry Andric       } break;
7320b57cec5SDimitry Andric       case 'l':
7330b57cec5SDimitry Andric         m_class_name = std::string(option_arg);
7340b57cec5SDimitry Andric         break;
7350b57cec5SDimitry Andric       case 's':
7360b57cec5SDimitry Andric         m_module = std::string(option_arg);
7370b57cec5SDimitry Andric         break;
7380b57cec5SDimitry Andric       case 'n':
7395ffd83dbSDimitry Andric         m_symbols.push_back(std::string(option_arg));
7400b57cec5SDimitry Andric         break;
7410b57cec5SDimitry Andric       case 'x':
7420b57cec5SDimitry Andric         m_regex = true;
7430b57cec5SDimitry Andric         break;
7440b57cec5SDimitry Andric       default:
7459dba64beSDimitry Andric         llvm_unreachable("Unimplemented option");
7460b57cec5SDimitry Andric       }
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric       return error;
7490b57cec5SDimitry Andric     }
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
7520b57cec5SDimitry Andric       m_module = "";
7535ffd83dbSDimitry Andric       m_symbols.clear();
7540b57cec5SDimitry Andric       m_class_name = "";
7550b57cec5SDimitry Andric       m_regex = false;
756349cc55cSDimitry Andric       m_first_instruction_only = true;
7570b57cec5SDimitry Andric     }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
760bdd1243dSDimitry Andric       return llvm::ArrayRef(g_frame_recognizer_add_options);
7610b57cec5SDimitry Andric     }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric     // Instance variables to hold the values for command options.
7640b57cec5SDimitry Andric     std::string m_class_name;
7650b57cec5SDimitry Andric     std::string m_module;
7665ffd83dbSDimitry Andric     std::vector<std::string> m_symbols;
7670b57cec5SDimitry Andric     bool m_regex;
768349cc55cSDimitry Andric     bool m_first_instruction_only;
7690b57cec5SDimitry Andric   };
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   CommandOptions m_options;
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric   Options *GetOptions() override { return &m_options; }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric protected:
7765f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override;
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric public:
7790b57cec5SDimitry Andric   CommandObjectFrameRecognizerAdd(CommandInterpreter &interpreter)
7800b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer add",
78104eeddc0SDimitry Andric                             "Add a new frame recognizer.", nullptr) {
7820b57cec5SDimitry Andric     SetHelpLong(R"(
7830b57cec5SDimitry Andric Frame recognizers allow for retrieving information about special frames based on
7840b57cec5SDimitry Andric ABI, arguments or other special properties of that frame, even without source
7850b57cec5SDimitry Andric code or debug info. Currently, one use case is to extract function arguments
7860b57cec5SDimitry Andric that would otherwise be unaccesible, or augment existing arguments.
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric Adding a custom frame recognizer is possible by implementing a Python class
7890b57cec5SDimitry Andric and using the 'frame recognizer add' command. The Python class should have a
7900b57cec5SDimitry Andric 'get_recognized_arguments' method and it will receive an argument of type
7910b57cec5SDimitry Andric lldb.SBFrame representing the current frame that we are trying to recognize.
7920b57cec5SDimitry Andric The method should return a (possibly empty) list of lldb.SBValue objects that
7930b57cec5SDimitry Andric represent the recognized arguments.
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric An example of a recognizer that retrieves the file descriptor values from libc
7960b57cec5SDimitry Andric functions 'read', 'write' and 'close' follows:
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   class LibcFdRecognizer(object):
7990b57cec5SDimitry Andric     def get_recognized_arguments(self, frame):
8000b57cec5SDimitry Andric       if frame.name in ["read", "write", "close"]:
8010b57cec5SDimitry Andric         fd = frame.EvaluateExpression("$arg1").unsigned
802bdd1243dSDimitry Andric         target = frame.thread.process.target
803bdd1243dSDimitry Andric         value = target.CreateValueFromExpression("fd", "(int)%d" % fd)
8040b57cec5SDimitry Andric         return [value]
8050b57cec5SDimitry Andric       return []
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric The file containing this implementation can be imported via 'command script
8080b57cec5SDimitry Andric import' and then we can register this recognizer with 'frame recognizer add'.
8090b57cec5SDimitry Andric It's important to restrict the recognizer to the libc library (which is
8100b57cec5SDimitry Andric libsystem_kernel.dylib on macOS) to avoid matching functions with the same name
8110b57cec5SDimitry Andric in other modules:
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric (lldb) command script import .../fd_recognizer.py
8140b57cec5SDimitry Andric (lldb) frame recognizer add -l fd_recognizer.LibcFdRecognizer -n read -s libsystem_kernel.dylib
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric When the program is stopped at the beginning of the 'read' function in libc, we
8170b57cec5SDimitry Andric can view the recognizer arguments in 'frame variable':
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric (lldb) b read
8200b57cec5SDimitry Andric (lldb) r
8210b57cec5SDimitry Andric Process 1234 stopped
8220b57cec5SDimitry Andric * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.3
8230b57cec5SDimitry Andric     frame #0: 0x00007fff06013ca0 libsystem_kernel.dylib`read
8240b57cec5SDimitry Andric (lldb) frame variable
8250b57cec5SDimitry Andric (int) fd = 3
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     )");
8280b57cec5SDimitry Andric   }
8290b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerAdd() override = default;
8300b57cec5SDimitry Andric };
8310b57cec5SDimitry Andric 
8325f757f3fSDimitry Andric void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
8330b57cec5SDimitry Andric                                                 CommandReturnObject &result) {
834480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
8350b57cec5SDimitry Andric   if (m_options.m_class_name.empty()) {
8360b57cec5SDimitry Andric     result.AppendErrorWithFormat(
8370b57cec5SDimitry Andric         "%s needs a Python class name (-l argument).\n", m_cmd_name.c_str());
8385f757f3fSDimitry Andric     return;
8390b57cec5SDimitry Andric   }
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric   if (m_options.m_module.empty()) {
8420b57cec5SDimitry Andric     result.AppendErrorWithFormat("%s needs a module name (-s argument).\n",
8430b57cec5SDimitry Andric                                  m_cmd_name.c_str());
8445f757f3fSDimitry Andric     return;
8450b57cec5SDimitry Andric   }
8460b57cec5SDimitry Andric 
8475ffd83dbSDimitry Andric   if (m_options.m_symbols.empty()) {
8485ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8495ffd83dbSDimitry Andric         "%s needs at least one symbol name (-n argument).\n",
8505ffd83dbSDimitry Andric         m_cmd_name.c_str());
8515f757f3fSDimitry Andric     return;
8525ffd83dbSDimitry Andric   }
8535ffd83dbSDimitry Andric 
8545ffd83dbSDimitry Andric   if (m_options.m_regex && m_options.m_symbols.size() > 1) {
8555ffd83dbSDimitry Andric     result.AppendErrorWithFormat(
8565ffd83dbSDimitry Andric         "%s needs only one symbol regular expression (-n argument).\n",
8570b57cec5SDimitry Andric         m_cmd_name.c_str());
8585f757f3fSDimitry Andric     return;
8590b57cec5SDimitry Andric   }
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric   ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   if (interpreter &&
8640b57cec5SDimitry Andric       !interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
865480093f4SDimitry Andric     result.AppendWarning("The provided class does not exist - please define it "
8660b57cec5SDimitry Andric                          "before attempting to use this frame recognizer");
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   StackFrameRecognizerSP recognizer_sp =
8700b57cec5SDimitry Andric       StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
8710b57cec5SDimitry Andric           interpreter, m_options.m_class_name.c_str()));
8720b57cec5SDimitry Andric   if (m_options.m_regex) {
8730b57cec5SDimitry Andric     auto module =
8740b57cec5SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_module));
8750b57cec5SDimitry Andric     auto func =
8765ffd83dbSDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
877e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
878349cc55cSDimitry Andric         recognizer_sp, module, func, m_options.m_first_instruction_only);
8790b57cec5SDimitry Andric   } else {
8800b57cec5SDimitry Andric     auto module = ConstString(m_options.m_module);
8815ffd83dbSDimitry Andric     std::vector<ConstString> symbols(m_options.m_symbols.begin(),
8825ffd83dbSDimitry Andric                                      m_options.m_symbols.end());
883e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
884349cc55cSDimitry Andric         recognizer_sp, module, symbols, m_options.m_first_instruction_only);
8850b57cec5SDimitry Andric   }
8860b57cec5SDimitry Andric #endif
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishNoResult);
8890b57cec5SDimitry Andric }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
8920b57cec5SDimitry Andric public:
8930b57cec5SDimitry Andric   CommandObjectFrameRecognizerClear(CommandInterpreter &interpreter)
8940b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer clear",
8950b57cec5SDimitry Andric                             "Delete all frame recognizers.", nullptr) {}
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerClear() override = default;
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric protected:
9005f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
901e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget()
902e8d8bef9SDimitry Andric         .GetFrameRecognizerManager()
903e8d8bef9SDimitry Andric         .RemoveAllRecognizers();
9040b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric };
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
9090b57cec5SDimitry Andric public:
9100b57cec5SDimitry Andric   CommandObjectFrameRecognizerDelete(CommandInterpreter &interpreter)
9110b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer delete",
91281ad6265SDimitry Andric                             "Delete an existing frame recognizer by id.",
91381ad6265SDimitry Andric                             nullptr) {
914*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeRecognizerID);
91581ad6265SDimitry Andric   }
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerDelete() override = default;
9180b57cec5SDimitry Andric 
9195ffd83dbSDimitry Andric   void
9205ffd83dbSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
9215ffd83dbSDimitry Andric                            OptionElementVector &opt_element_vector) override {
9225ffd83dbSDimitry Andric     if (request.GetCursorIndex() != 0)
9235ffd83dbSDimitry Andric       return;
9245ffd83dbSDimitry Andric 
925e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
9265ffd83dbSDimitry Andric         [&request](uint32_t rid, std::string rname, std::string module,
9275ffd83dbSDimitry Andric                    llvm::ArrayRef<lldb_private::ConstString> symbols,
9285ffd83dbSDimitry Andric                    bool regexp) {
9295ffd83dbSDimitry Andric           StreamString strm;
9305ffd83dbSDimitry Andric           if (rname.empty())
9315ffd83dbSDimitry Andric             rname = "(internal)";
9325ffd83dbSDimitry Andric 
9335ffd83dbSDimitry Andric           strm << rname;
9345ffd83dbSDimitry Andric           if (!module.empty())
9355ffd83dbSDimitry Andric             strm << ", module " << module;
9365ffd83dbSDimitry Andric           if (!symbols.empty())
9375ffd83dbSDimitry Andric             for (auto &symbol : symbols)
9385ffd83dbSDimitry Andric               strm << ", symbol " << symbol;
9395ffd83dbSDimitry Andric           if (regexp)
9405ffd83dbSDimitry Andric             strm << " (regexp)";
9415ffd83dbSDimitry Andric 
9425ffd83dbSDimitry Andric           request.TryCompleteCurrentArg(std::to_string(rid), strm.GetString());
9435ffd83dbSDimitry Andric         });
9445ffd83dbSDimitry Andric   }
9455ffd83dbSDimitry Andric 
9460b57cec5SDimitry Andric protected:
9475f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
9480b57cec5SDimitry Andric     if (command.GetArgumentCount() == 0) {
9490b57cec5SDimitry Andric       if (!m_interpreter.Confirm(
9500b57cec5SDimitry Andric               "About to delete all frame recognizers, do you want to do that?",
9510b57cec5SDimitry Andric               true)) {
9520b57cec5SDimitry Andric         result.AppendMessage("Operation cancelled...");
9535f757f3fSDimitry Andric         return;
9540b57cec5SDimitry Andric       }
9550b57cec5SDimitry Andric 
956e8d8bef9SDimitry Andric       GetSelectedOrDummyTarget()
957e8d8bef9SDimitry Andric           .GetFrameRecognizerManager()
958e8d8bef9SDimitry Andric           .RemoveAllRecognizers();
9590b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
9605f757f3fSDimitry Andric       return;
9610b57cec5SDimitry Andric     }
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
9640b57cec5SDimitry Andric       result.AppendErrorWithFormat("'%s' takes zero or one arguments.\n",
9650b57cec5SDimitry Andric                                    m_cmd_name.c_str());
9665f757f3fSDimitry Andric       return;
9670b57cec5SDimitry Andric     }
9680b57cec5SDimitry Andric 
9695ffd83dbSDimitry Andric     uint32_t recognizer_id;
9705ffd83dbSDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), recognizer_id)) {
9715ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
9725ffd83dbSDimitry Andric                                    command.GetArgumentAtIndex(0));
9735f757f3fSDimitry Andric       return;
9745ffd83dbSDimitry Andric     }
9750b57cec5SDimitry Andric 
976e8d8bef9SDimitry Andric     if (!GetSelectedOrDummyTarget()
977e8d8bef9SDimitry Andric              .GetFrameRecognizerManager()
978e8d8bef9SDimitry Andric              .RemoveRecognizerWithID(recognizer_id)) {
979e8d8bef9SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
980e8d8bef9SDimitry Andric                                    command.GetArgumentAtIndex(0));
9815f757f3fSDimitry Andric       return;
982e8d8bef9SDimitry Andric     }
9830b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
9840b57cec5SDimitry Andric   }
9850b57cec5SDimitry Andric };
9860b57cec5SDimitry Andric 
9870b57cec5SDimitry Andric class CommandObjectFrameRecognizerList : public CommandObjectParsed {
9880b57cec5SDimitry Andric public:
9890b57cec5SDimitry Andric   CommandObjectFrameRecognizerList(CommandInterpreter &interpreter)
9900b57cec5SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer list",
9910b57cec5SDimitry Andric                             "Show a list of active frame recognizers.",
9920b57cec5SDimitry Andric                             nullptr) {}
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerList() override = default;
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric protected:
9975f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
9980b57cec5SDimitry Andric     bool any_printed = false;
999e8d8bef9SDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
10005ffd83dbSDimitry Andric         [&result, &any_printed](
10015ffd83dbSDimitry Andric             uint32_t recognizer_id, std::string name, std::string module,
10025ffd83dbSDimitry Andric             llvm::ArrayRef<ConstString> symbols, bool regexp) {
10035ffd83dbSDimitry Andric           Stream &stream = result.GetOutputStream();
10045ffd83dbSDimitry Andric 
10055ffd83dbSDimitry Andric           if (name.empty())
1006480093f4SDimitry Andric             name = "(internal)";
10075ffd83dbSDimitry Andric 
10085ffd83dbSDimitry Andric           stream << std::to_string(recognizer_id) << ": " << name;
10095ffd83dbSDimitry Andric           if (!module.empty())
10105ffd83dbSDimitry Andric             stream << ", module " << module;
10115ffd83dbSDimitry Andric           if (!symbols.empty())
10125ffd83dbSDimitry Andric             for (auto &symbol : symbols)
10135ffd83dbSDimitry Andric               stream << ", symbol " << symbol;
10145ffd83dbSDimitry Andric           if (regexp)
10155ffd83dbSDimitry Andric             stream << " (regexp)";
10165ffd83dbSDimitry Andric 
10175ffd83dbSDimitry Andric           stream.EOL();
10185ffd83dbSDimitry Andric           stream.Flush();
10195ffd83dbSDimitry Andric 
10200b57cec5SDimitry Andric           any_printed = true;
10210b57cec5SDimitry Andric         });
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric     if (any_printed)
10240b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
10250b57cec5SDimitry Andric     else {
10260b57cec5SDimitry Andric       result.GetOutputStream().PutCString("no matching results found.\n");
10270b57cec5SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
10280b57cec5SDimitry Andric     }
10290b57cec5SDimitry Andric   }
10300b57cec5SDimitry Andric };
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
10330b57cec5SDimitry Andric public:
10340b57cec5SDimitry Andric   CommandObjectFrameRecognizerInfo(CommandInterpreter &interpreter)
10350b57cec5SDimitry Andric       : CommandObjectParsed(
10360b57cec5SDimitry Andric             interpreter, "frame recognizer info",
10370b57cec5SDimitry Andric             "Show which frame recognizer is applied a stack frame (if any).",
10380b57cec5SDimitry Andric             nullptr) {
1039*0fca6ea1SDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex);
10400b57cec5SDimitry Andric   }
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   ~CommandObjectFrameRecognizerInfo() override = default;
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric protected:
10455f757f3fSDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
10465ffd83dbSDimitry Andric     const char *frame_index_str = command.GetArgumentAtIndex(0);
10475ffd83dbSDimitry Andric     uint32_t frame_index;
10485ffd83dbSDimitry Andric     if (!llvm::to_integer(frame_index_str, frame_index)) {
10495ffd83dbSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid frame index.",
10505ffd83dbSDimitry Andric                                    frame_index_str);
10515f757f3fSDimitry Andric       return;
10525ffd83dbSDimitry Andric     }
10535ffd83dbSDimitry Andric 
10540b57cec5SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
10550b57cec5SDimitry Andric     if (process == nullptr) {
10560b57cec5SDimitry Andric       result.AppendError("no process");
10575f757f3fSDimitry Andric       return;
10580b57cec5SDimitry Andric     }
10590b57cec5SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
10600b57cec5SDimitry Andric     if (thread == nullptr) {
10610b57cec5SDimitry Andric       result.AppendError("no thread");
10625f757f3fSDimitry Andric       return;
10630b57cec5SDimitry Andric     }
10640b57cec5SDimitry Andric     if (command.GetArgumentCount() != 1) {
10650b57cec5SDimitry Andric       result.AppendErrorWithFormat(
10660b57cec5SDimitry Andric           "'%s' takes exactly one frame index argument.\n", m_cmd_name.c_str());
10675f757f3fSDimitry Andric       return;
10680b57cec5SDimitry Andric     }
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric     StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_index);
10710b57cec5SDimitry Andric     if (!frame_sp) {
10720b57cec5SDimitry Andric       result.AppendErrorWithFormat("no frame with index %u", frame_index);
10735f757f3fSDimitry Andric       return;
10740b57cec5SDimitry Andric     }
10750b57cec5SDimitry Andric 
1076e8d8bef9SDimitry Andric     auto recognizer = GetSelectedOrDummyTarget()
1077e8d8bef9SDimitry Andric                           .GetFrameRecognizerManager()
1078e8d8bef9SDimitry Andric                           .GetRecognizerForFrame(frame_sp);
10790b57cec5SDimitry Andric 
10800b57cec5SDimitry Andric     Stream &output_stream = result.GetOutputStream();
10810b57cec5SDimitry Andric     output_stream.Printf("frame %d ", frame_index);
10820b57cec5SDimitry Andric     if (recognizer) {
10830b57cec5SDimitry Andric       output_stream << "is recognized by ";
10840b57cec5SDimitry Andric       output_stream << recognizer->GetName();
10850b57cec5SDimitry Andric     } else {
10860b57cec5SDimitry Andric       output_stream << "not recognized by any recognizer";
10870b57cec5SDimitry Andric     }
10880b57cec5SDimitry Andric     output_stream.EOL();
10890b57cec5SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
10900b57cec5SDimitry Andric   }
10910b57cec5SDimitry Andric };
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric class CommandObjectFrameRecognizer : public CommandObjectMultiword {
10940b57cec5SDimitry Andric public:
10950b57cec5SDimitry Andric   CommandObjectFrameRecognizer(CommandInterpreter &interpreter)
10960b57cec5SDimitry Andric       : CommandObjectMultiword(
10970b57cec5SDimitry Andric             interpreter, "frame recognizer",
10980b57cec5SDimitry Andric             "Commands for editing and viewing frame recognizers.",
10990b57cec5SDimitry Andric             "frame recognizer [<sub-command-options>] ") {
1100480093f4SDimitry Andric     LoadSubCommand("add", CommandObjectSP(new CommandObjectFrameRecognizerAdd(
1101480093f4SDimitry Andric                               interpreter)));
11020b57cec5SDimitry Andric     LoadSubCommand(
11030b57cec5SDimitry Andric         "clear",
11040b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerClear(interpreter)));
11050b57cec5SDimitry Andric     LoadSubCommand(
11060b57cec5SDimitry Andric         "delete",
11070b57cec5SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerDelete(interpreter)));
1108480093f4SDimitry Andric     LoadSubCommand("list", CommandObjectSP(new CommandObjectFrameRecognizerList(
1109480093f4SDimitry Andric                                interpreter)));
1110480093f4SDimitry Andric     LoadSubCommand("info", CommandObjectSP(new CommandObjectFrameRecognizerInfo(
1111480093f4SDimitry Andric                                interpreter)));
11120b57cec5SDimitry Andric   }
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric   ~CommandObjectFrameRecognizer() override = default;
11150b57cec5SDimitry Andric };
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric #pragma mark CommandObjectMultiwordFrame
11180b57cec5SDimitry Andric 
11190b57cec5SDimitry Andric // CommandObjectMultiwordFrame
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric CommandObjectMultiwordFrame::CommandObjectMultiwordFrame(
11220b57cec5SDimitry Andric     CommandInterpreter &interpreter)
1123480093f4SDimitry Andric     : CommandObjectMultiword(interpreter, "frame",
1124480093f4SDimitry Andric                              "Commands for selecting and "
11250b57cec5SDimitry Andric                              "examing the current "
11260b57cec5SDimitry Andric                              "thread's stack frames.",
11270b57cec5SDimitry Andric                              "frame <subcommand> [<subcommand-options>]") {
11280b57cec5SDimitry Andric   LoadSubCommand("diagnose",
11290b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameDiagnose(interpreter)));
11300b57cec5SDimitry Andric   LoadSubCommand("info",
11310b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameInfo(interpreter)));
11320b57cec5SDimitry Andric   LoadSubCommand("select",
11330b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameSelect(interpreter)));
11340b57cec5SDimitry Andric   LoadSubCommand("variable",
11350b57cec5SDimitry Andric                  CommandObjectSP(new CommandObjectFrameVariable(interpreter)));
1136480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
1137480093f4SDimitry Andric   LoadSubCommand("recognizer", CommandObjectSP(new CommandObjectFrameRecognizer(
1138480093f4SDimitry Andric                                    interpreter)));
11390b57cec5SDimitry Andric #endif
11400b57cec5SDimitry Andric }
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame() = default;
1143