xref: /src/contrib/llvm-project/lldb/source/Commands/CommandObjectFrame.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- CommandObjectFrame.cpp --------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f3fbd1c0SDimitry Andric #include "CommandObjectFrame.h"
9f034231aSEd Maste #include "lldb/Core/Debugger.h"
10f034231aSEd Maste #include "lldb/Core/ValueObject.h"
11f034231aSEd Maste #include "lldb/DataFormatters/DataVisualization.h"
12f21a844fSEd Maste #include "lldb/DataFormatters/ValueObjectPrinter.h"
13706b4fc4SDimitry Andric #include "lldb/Host/Config.h"
1474a628f7SDimitry Andric #include "lldb/Host/OptionParser.h"
15f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
164b4fe385SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
17f034231aSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
18c0981da4SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
19f034231aSEd Maste #include "lldb/Interpreter/OptionGroupFormat.h"
20f034231aSEd Maste #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
21f034231aSEd Maste #include "lldb/Interpreter/OptionGroupVariable.h"
2214f1b3e8SDimitry Andric #include "lldb/Interpreter/Options.h"
23e81d9d49SDimitry Andric #include "lldb/Symbol/Function.h"
24f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
25f034231aSEd Maste #include "lldb/Symbol/Variable.h"
26f034231aSEd Maste #include "lldb/Symbol/VariableList.h"
27f034231aSEd Maste #include "lldb/Target/StackFrame.h"
2894994d37SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
2914f1b3e8SDimitry Andric #include "lldb/Target/StopInfo.h"
30f034231aSEd Maste #include "lldb/Target/Target.h"
3114f1b3e8SDimitry Andric #include "lldb/Target/Thread.h"
32f73363f1SDimitry Andric #include "lldb/Utility/Args.h"
33f034231aSEd Maste 
345f29bb8aSDimitry Andric #include <memory>
35e3b55780SDimitry Andric #include <optional>
365f29bb8aSDimitry Andric #include <string>
375f29bb8aSDimitry Andric 
38f034231aSEd Maste using namespace lldb;
39f034231aSEd Maste using namespace lldb_private;
40f034231aSEd Maste 
4114f1b3e8SDimitry Andric #pragma mark CommandObjectFrameDiagnose
4214f1b3e8SDimitry Andric 
4314f1b3e8SDimitry Andric // CommandObjectFrameInfo
4414f1b3e8SDimitry Andric 
4514f1b3e8SDimitry Andric // CommandObjectFrameDiagnose
4614f1b3e8SDimitry Andric 
47ead24645SDimitry Andric #define LLDB_OPTIONS_frame_diag
48ead24645SDimitry Andric #include "CommandOptions.inc"
4914f1b3e8SDimitry Andric 
5014f1b3e8SDimitry Andric class CommandObjectFrameDiagnose : public CommandObjectParsed {
5114f1b3e8SDimitry Andric public:
5214f1b3e8SDimitry Andric   class CommandOptions : public Options {
5314f1b3e8SDimitry Andric   public:
CommandOptions()546f8fc217SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
5514f1b3e8SDimitry Andric 
5614f1b3e8SDimitry Andric     ~CommandOptions() override = default;
5714f1b3e8SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)58b76161e4SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
5914f1b3e8SDimitry Andric                           ExecutionContext *execution_context) override {
60b76161e4SDimitry Andric       Status error;
6114f1b3e8SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
6214f1b3e8SDimitry Andric       switch (short_option) {
6314f1b3e8SDimitry Andric       case 'r':
6414f1b3e8SDimitry Andric         reg = ConstString(option_arg);
6514f1b3e8SDimitry Andric         break;
6614f1b3e8SDimitry Andric 
6714f1b3e8SDimitry Andric       case 'a': {
6814f1b3e8SDimitry Andric         address.emplace();
6914f1b3e8SDimitry Andric         if (option_arg.getAsInteger(0, *address)) {
7014f1b3e8SDimitry Andric           address.reset();
7114f1b3e8SDimitry Andric           error.SetErrorStringWithFormat("invalid address argument '%s'",
7214f1b3e8SDimitry Andric                                          option_arg.str().c_str());
7314f1b3e8SDimitry Andric         }
7414f1b3e8SDimitry Andric       } break;
7514f1b3e8SDimitry Andric 
7614f1b3e8SDimitry Andric       case 'o': {
7714f1b3e8SDimitry Andric         offset.emplace();
7814f1b3e8SDimitry Andric         if (option_arg.getAsInteger(0, *offset)) {
7914f1b3e8SDimitry Andric           offset.reset();
8014f1b3e8SDimitry Andric           error.SetErrorStringWithFormat("invalid offset argument '%s'",
8114f1b3e8SDimitry Andric                                          option_arg.str().c_str());
8214f1b3e8SDimitry Andric         }
8314f1b3e8SDimitry Andric       } break;
8414f1b3e8SDimitry Andric 
8514f1b3e8SDimitry Andric       default:
86ead24645SDimitry Andric         llvm_unreachable("Unimplemented option");
8714f1b3e8SDimitry Andric       }
8814f1b3e8SDimitry Andric 
8914f1b3e8SDimitry Andric       return error;
9014f1b3e8SDimitry Andric     }
9114f1b3e8SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)9214f1b3e8SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
9314f1b3e8SDimitry Andric       address.reset();
9414f1b3e8SDimitry Andric       reg.reset();
9514f1b3e8SDimitry Andric       offset.reset();
9614f1b3e8SDimitry Andric     }
9714f1b3e8SDimitry Andric 
GetDefinitions()9814f1b3e8SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
99e3b55780SDimitry Andric       return llvm::ArrayRef(g_frame_diag_options);
10014f1b3e8SDimitry Andric     }
10114f1b3e8SDimitry Andric 
10214f1b3e8SDimitry Andric     // Options.
103e3b55780SDimitry Andric     std::optional<lldb::addr_t> address;
104e3b55780SDimitry Andric     std::optional<ConstString> reg;
105e3b55780SDimitry Andric     std::optional<int64_t> offset;
10614f1b3e8SDimitry Andric   };
10714f1b3e8SDimitry Andric 
CommandObjectFrameDiagnose(CommandInterpreter & interpreter)10814f1b3e8SDimitry Andric   CommandObjectFrameDiagnose(CommandInterpreter &interpreter)
10914f1b3e8SDimitry Andric       : CommandObjectParsed(interpreter, "frame diagnose",
110c0981da4SDimitry Andric                             "Try to determine what path the current stop "
11114f1b3e8SDimitry Andric                             "location used to get to a register or address",
11214f1b3e8SDimitry Andric                             nullptr,
11314f1b3e8SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
11414f1b3e8SDimitry Andric                                 eCommandProcessMustBeLaunched |
1156f8fc217SDimitry Andric                                 eCommandProcessMustBePaused) {
116ac9a064cSDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex, eArgRepeatOptional);
11714f1b3e8SDimitry Andric   }
11814f1b3e8SDimitry Andric 
11914f1b3e8SDimitry Andric   ~CommandObjectFrameDiagnose() override = default;
12014f1b3e8SDimitry Andric 
GetOptions()12114f1b3e8SDimitry Andric   Options *GetOptions() override { return &m_options; }
12214f1b3e8SDimitry Andric 
12314f1b3e8SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)124b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
12514f1b3e8SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
1267fa27ce4SDimitry Andric     StackFrameSP frame_sp = thread->GetSelectedFrame(SelectMostRelevantFrame);
12714f1b3e8SDimitry Andric 
12814f1b3e8SDimitry Andric     ValueObjectSP valobj_sp;
12914f1b3e8SDimitry Andric 
130145449b1SDimitry Andric     if (m_options.address) {
131145449b1SDimitry Andric       if (m_options.reg || m_options.offset) {
13214f1b3e8SDimitry Andric         result.AppendError(
13314f1b3e8SDimitry Andric             "`frame diagnose --address` is incompatible with other arguments.");
134b1c73532SDimitry Andric         return;
13514f1b3e8SDimitry Andric       }
136e3b55780SDimitry Andric       valobj_sp = frame_sp->GuessValueForAddress(*m_options.address);
137145449b1SDimitry Andric     } else if (m_options.reg) {
13814f1b3e8SDimitry Andric       valobj_sp = frame_sp->GuessValueForRegisterAndOffset(
139e3b55780SDimitry Andric           *m_options.reg, m_options.offset.value_or(0));
14014f1b3e8SDimitry Andric     } else {
14114f1b3e8SDimitry Andric       StopInfoSP stop_info_sp = thread->GetStopInfo();
14214f1b3e8SDimitry Andric       if (!stop_info_sp) {
14314f1b3e8SDimitry Andric         result.AppendError("No arguments provided, and no stop info.");
144b1c73532SDimitry Andric         return;
14514f1b3e8SDimitry Andric       }
14614f1b3e8SDimitry Andric 
14714f1b3e8SDimitry Andric       valobj_sp = StopInfo::GetCrashingDereference(stop_info_sp);
14814f1b3e8SDimitry Andric     }
14914f1b3e8SDimitry Andric 
15014f1b3e8SDimitry Andric     if (!valobj_sp) {
15114f1b3e8SDimitry Andric       result.AppendError("No diagnosis available.");
152b1c73532SDimitry Andric       return;
15314f1b3e8SDimitry Andric     }
15414f1b3e8SDimitry Andric 
155706b4fc4SDimitry Andric     DumpValueObjectOptions::DeclPrintingHelper helper =
156706b4fc4SDimitry Andric         [&valobj_sp](ConstString type, ConstString var,
157706b4fc4SDimitry Andric                      const DumpValueObjectOptions &opts,
15874a628f7SDimitry Andric                      Stream &stream) -> bool {
15914f1b3e8SDimitry Andric       const ValueObject::GetExpressionPathFormat format = ValueObject::
16014f1b3e8SDimitry Andric           GetExpressionPathFormat::eGetExpressionPathFormatHonorPointers;
161cfca06d7SDimitry Andric       valobj_sp->GetExpressionPath(stream, format);
16214f1b3e8SDimitry Andric       stream.PutCString(" =");
16314f1b3e8SDimitry Andric       return true;
16414f1b3e8SDimitry Andric     };
16514f1b3e8SDimitry Andric 
16614f1b3e8SDimitry Andric     DumpValueObjectOptions options;
16714f1b3e8SDimitry Andric     options.SetDeclPrintingHelper(helper);
168ac9a064cSDimitry Andric     // We've already handled the case where the value object sp is null, so
169ac9a064cSDimitry Andric     // this is just to make sure future changes don't skip that:
170ac9a064cSDimitry Andric     assert(valobj_sp.get() && "Must have a valid ValueObject to print");
171ac9a064cSDimitry Andric     ValueObjectPrinter printer(*valobj_sp, &result.GetOutputStream(),
17214f1b3e8SDimitry Andric                                options);
173ac9a064cSDimitry Andric     if (llvm::Error error = printer.PrintValueObject())
174ac9a064cSDimitry Andric       result.AppendError(toString(std::move(error)));
17514f1b3e8SDimitry Andric   }
17614f1b3e8SDimitry Andric 
17714f1b3e8SDimitry Andric   CommandOptions m_options;
17814f1b3e8SDimitry Andric };
17914f1b3e8SDimitry Andric 
180f034231aSEd Maste #pragma mark CommandObjectFrameInfo
181f034231aSEd Maste 
182f034231aSEd Maste // CommandObjectFrameInfo
183f034231aSEd Maste 
18414f1b3e8SDimitry Andric class CommandObjectFrameInfo : public CommandObjectParsed {
185f034231aSEd Maste public:
CommandObjectFrameInfo(CommandInterpreter & interpreter)186f3fbd1c0SDimitry Andric   CommandObjectFrameInfo(CommandInterpreter &interpreter)
187706b4fc4SDimitry Andric       : CommandObjectParsed(interpreter, "frame info",
188706b4fc4SDimitry Andric                             "List information about the current "
18914f1b3e8SDimitry Andric                             "stack frame in the current thread.",
19014f1b3e8SDimitry Andric                             "frame info",
19114f1b3e8SDimitry Andric                             eCommandRequiresFrame | eCommandTryTargetAPILock |
192706b4fc4SDimitry Andric                                 eCommandProcessMustBeLaunched |
193706b4fc4SDimitry Andric                                 eCommandProcessMustBePaused) {}
194f034231aSEd Maste 
195f3fbd1c0SDimitry Andric   ~CommandObjectFrameInfo() override = default;
196f034231aSEd Maste 
197f034231aSEd Maste protected:
DoExecute(Args & command,CommandReturnObject & result)198b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
199f034231aSEd Maste     m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat(&result.GetOutputStream());
200f034231aSEd Maste     result.SetStatus(eReturnStatusSuccessFinishResult);
201f034231aSEd Maste   }
202f034231aSEd Maste };
203f034231aSEd Maste 
204f034231aSEd Maste #pragma mark CommandObjectFrameSelect
205f034231aSEd Maste 
206f034231aSEd Maste // CommandObjectFrameSelect
207f034231aSEd Maste 
208ead24645SDimitry Andric #define LLDB_OPTIONS_frame_select
209ead24645SDimitry Andric #include "CommandOptions.inc"
21014f1b3e8SDimitry Andric 
21114f1b3e8SDimitry Andric class CommandObjectFrameSelect : public CommandObjectParsed {
212f034231aSEd Maste public:
21314f1b3e8SDimitry Andric   class CommandOptions : public Options {
214f034231aSEd Maste   public:
CommandOptions()2156f8fc217SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
216f034231aSEd Maste 
217f3fbd1c0SDimitry Andric     ~CommandOptions() override = default;
218f034231aSEd Maste 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)219b76161e4SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
22014f1b3e8SDimitry Andric                           ExecutionContext *execution_context) override {
221b76161e4SDimitry Andric       Status error;
222f034231aSEd Maste       const int short_option = m_getopt_table[option_idx].val;
22314f1b3e8SDimitry Andric       switch (short_option) {
224ead24645SDimitry Andric       case 'r': {
225ead24645SDimitry Andric         int32_t offset = 0;
226ead24645SDimitry Andric         if (option_arg.getAsInteger(0, offset) || offset == INT32_MIN) {
22714f1b3e8SDimitry Andric           error.SetErrorStringWithFormat("invalid frame offset argument '%s'",
22814f1b3e8SDimitry Andric                                          option_arg.str().c_str());
229ead24645SDimitry Andric         } else
230ead24645SDimitry Andric           relative_frame_offset = offset;
231f034231aSEd Maste         break;
232ead24645SDimitry Andric       }
233f034231aSEd Maste 
234f034231aSEd Maste       default:
235ead24645SDimitry Andric         llvm_unreachable("Unimplemented option");
236f034231aSEd Maste       }
237f034231aSEd Maste 
238f034231aSEd Maste       return error;
239f034231aSEd Maste     }
240f034231aSEd Maste 
OptionParsingStarting(ExecutionContext * execution_context)24114f1b3e8SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
242ead24645SDimitry Andric       relative_frame_offset.reset();
243f034231aSEd Maste     }
244f034231aSEd Maste 
GetDefinitions()24514f1b3e8SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
246e3b55780SDimitry Andric       return llvm::ArrayRef(g_frame_select_options);
247f034231aSEd Maste     }
248f034231aSEd Maste 
249e3b55780SDimitry Andric     std::optional<int32_t> relative_frame_offset;
250f034231aSEd Maste   };
251f034231aSEd Maste 
CommandObjectFrameSelect(CommandInterpreter & interpreter)252f3fbd1c0SDimitry Andric   CommandObjectFrameSelect(CommandInterpreter &interpreter)
253706b4fc4SDimitry Andric       : CommandObjectParsed(interpreter, "frame select",
254706b4fc4SDimitry Andric                             "Select the current stack frame by "
25514f1b3e8SDimitry Andric                             "index from within the current thread "
25614f1b3e8SDimitry Andric                             "(see 'thread backtrace'.)",
25714f1b3e8SDimitry Andric                             nullptr,
25814f1b3e8SDimitry Andric                             eCommandRequiresThread | eCommandTryTargetAPILock |
259706b4fc4SDimitry Andric                                 eCommandProcessMustBeLaunched |
2606f8fc217SDimitry Andric                                 eCommandProcessMustBePaused) {
261ac9a064cSDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex, eArgRepeatOptional);
262f034231aSEd Maste   }
263f034231aSEd Maste 
264f3fbd1c0SDimitry Andric   ~CommandObjectFrameSelect() override = default;
265f034231aSEd Maste 
GetOptions()26614f1b3e8SDimitry Andric   Options *GetOptions() override { return &m_options; }
267f034231aSEd Maste 
268f034231aSEd Maste protected:
DoExecute(Args & command,CommandReturnObject & result)269b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
27014f1b3e8SDimitry Andric     // No need to check "thread" for validity as eCommandRequiresThread ensures
27114f1b3e8SDimitry Andric     // it is valid
272f034231aSEd Maste     Thread *thread = m_exe_ctx.GetThreadPtr();
273f034231aSEd Maste 
274f034231aSEd Maste     uint32_t frame_idx = UINT32_MAX;
275145449b1SDimitry Andric     if (m_options.relative_frame_offset) {
276f034231aSEd Maste       // The one and only argument is a signed relative frame index
2777fa27ce4SDimitry Andric       frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
278f034231aSEd Maste       if (frame_idx == UINT32_MAX)
279f034231aSEd Maste         frame_idx = 0;
280f034231aSEd Maste 
281ead24645SDimitry Andric       if (*m_options.relative_frame_offset < 0) {
282ead24645SDimitry Andric         if (static_cast<int32_t>(frame_idx) >=
283ead24645SDimitry Andric             -*m_options.relative_frame_offset)
284ead24645SDimitry Andric           frame_idx += *m_options.relative_frame_offset;
28514f1b3e8SDimitry Andric         else {
28614f1b3e8SDimitry Andric           if (frame_idx == 0) {
287f73363f1SDimitry Andric             // If you are already at the bottom of the stack, then just warn
288f73363f1SDimitry Andric             // and don't reset the frame.
289f3fbd1c0SDimitry Andric             result.AppendError("Already at the bottom of the stack.");
290b1c73532SDimitry Andric             return;
29114f1b3e8SDimitry Andric           } else
292f034231aSEd Maste             frame_idx = 0;
293f034231aSEd Maste         }
294ead24645SDimitry Andric       } else if (*m_options.relative_frame_offset > 0) {
29514f1b3e8SDimitry Andric         // I don't want "up 20" where "20" takes you past the top of the stack
2967fa27ce4SDimitry Andric         // to produce an error, but rather to just go to the top.  OTOH, start
2977fa27ce4SDimitry Andric         // by seeing if the requested frame exists, in which case we can avoid
2987fa27ce4SDimitry Andric         // counting the stack here...
2997fa27ce4SDimitry Andric         const uint32_t frame_requested = frame_idx
3007fa27ce4SDimitry Andric             + *m_options.relative_frame_offset;
3017fa27ce4SDimitry Andric         StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_requested);
3027fa27ce4SDimitry Andric         if (frame_sp)
3037fa27ce4SDimitry Andric           frame_idx = frame_requested;
3047fa27ce4SDimitry Andric         else {
3057fa27ce4SDimitry Andric           // The request went past the stack, so handle that case:
306f034231aSEd Maste           const uint32_t num_frames = thread->GetStackFrameCount();
30714f1b3e8SDimitry Andric           if (static_cast<int32_t>(num_frames - frame_idx) >
308ead24645SDimitry Andric               *m_options.relative_frame_offset)
309ead24645SDimitry Andric           frame_idx += *m_options.relative_frame_offset;
31014f1b3e8SDimitry Andric           else {
31114f1b3e8SDimitry Andric             if (frame_idx == num_frames - 1) {
31214f1b3e8SDimitry Andric               // If we are already at the top of the stack, just warn and don't
31314f1b3e8SDimitry Andric               // reset the frame.
314f3fbd1c0SDimitry Andric               result.AppendError("Already at the top of the stack.");
315b1c73532SDimitry Andric               return;
31614f1b3e8SDimitry Andric             } else
317f034231aSEd Maste               frame_idx = num_frames - 1;
318f034231aSEd Maste           }
319f034231aSEd Maste         }
3207fa27ce4SDimitry Andric       }
32114f1b3e8SDimitry Andric     } else {
32214f1b3e8SDimitry Andric       if (command.GetArgumentCount() > 1) {
32314f1b3e8SDimitry Andric         result.AppendErrorWithFormat(
32414f1b3e8SDimitry Andric             "too many arguments; expected frame-index, saw '%s'.\n",
32514f1b3e8SDimitry Andric             command[0].c_str());
32614f1b3e8SDimitry Andric         m_options.GenerateOptionUsage(
327145449b1SDimitry Andric             result.GetErrorStream(), *this,
32814f1b3e8SDimitry Andric             GetCommandInterpreter().GetDebugger().GetTerminalWidth());
329b1c73532SDimitry Andric         return;
330f034231aSEd Maste       }
33114f1b3e8SDimitry Andric 
33214f1b3e8SDimitry Andric       if (command.GetArgumentCount() == 1) {
333ead24645SDimitry Andric         if (command[0].ref().getAsInteger(0, frame_idx)) {
33414f1b3e8SDimitry Andric           result.AppendErrorWithFormat("invalid frame index argument '%s'.",
33514f1b3e8SDimitry Andric                                        command[0].c_str());
336b1c73532SDimitry Andric           return;
337f21a844fSEd Maste         }
33814f1b3e8SDimitry Andric       } else if (command.GetArgumentCount() == 0) {
3397fa27ce4SDimitry Andric         frame_idx = thread->GetSelectedFrameIndex(SelectMostRelevantFrame);
34014f1b3e8SDimitry Andric         if (frame_idx == UINT32_MAX) {
341f034231aSEd Maste           frame_idx = 0;
342f034231aSEd Maste         }
343f034231aSEd Maste       }
344f034231aSEd Maste     }
345f034231aSEd Maste 
34614f1b3e8SDimitry Andric     bool success = thread->SetSelectedFrameByIndexNoisily(
34714f1b3e8SDimitry Andric         frame_idx, result.GetOutputStream());
34814f1b3e8SDimitry Andric     if (success) {
3497fa27ce4SDimitry Andric       m_exe_ctx.SetFrameSP(thread->GetSelectedFrame(SelectMostRelevantFrame));
350f034231aSEd Maste       result.SetStatus(eReturnStatusSuccessFinishResult);
35114f1b3e8SDimitry Andric     } else {
35214f1b3e8SDimitry Andric       result.AppendErrorWithFormat("Frame index (%u) out of range.\n",
35314f1b3e8SDimitry Andric                                    frame_idx);
354f034231aSEd Maste     }
355f034231aSEd Maste   }
356f034231aSEd Maste 
357f034231aSEd Maste   CommandOptions m_options;
358f034231aSEd Maste };
359f034231aSEd Maste 
360f034231aSEd Maste #pragma mark CommandObjectFrameVariable
361f034231aSEd Maste // List images with associated information
36214f1b3e8SDimitry Andric class CommandObjectFrameVariable : public CommandObjectParsed {
363f034231aSEd Maste public:
CommandObjectFrameVariable(CommandInterpreter & interpreter)364f3fbd1c0SDimitry Andric   CommandObjectFrameVariable(CommandInterpreter &interpreter)
365f3fbd1c0SDimitry Andric       : CommandObjectParsed(
36614f1b3e8SDimitry Andric             interpreter, "frame variable",
36714f1b3e8SDimitry Andric             "Show variables for the current stack frame. Defaults to all "
368f3fbd1c0SDimitry Andric             "arguments and local variables in scope. Names of argument, "
3696f8fc217SDimitry Andric             "local, file static and file global variables can be specified.",
370706b4fc4SDimitry Andric             nullptr,
371706b4fc4SDimitry Andric             eCommandRequiresFrame | eCommandTryTargetAPILock |
372706b4fc4SDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
373706b4fc4SDimitry Andric                 eCommandRequiresProcess),
37414f1b3e8SDimitry Andric         m_option_variable(
37514f1b3e8SDimitry Andric             true), // Include the frame specific options by passing "true"
3766f8fc217SDimitry Andric         m_option_format(eFormatDefault) {
3776f8fc217SDimitry Andric     SetHelpLong(R"(
3786f8fc217SDimitry Andric Children of aggregate variables can be specified such as 'var->child.x'.  In
3796f8fc217SDimitry Andric 'frame variable', the operators -> and [] do not invoke operator overloads if
3806f8fc217SDimitry Andric they exist, but directly access the specified element.  If you want to trigger
3816f8fc217SDimitry Andric operator overloads use the expression command to print the variable instead.
3826f8fc217SDimitry Andric 
3836f8fc217SDimitry Andric It is worth noting that except for overloaded operators, when printing local
3846f8fc217SDimitry Andric variables 'expr local_var' and 'frame var local_var' produce the same results.
3856f8fc217SDimitry Andric However, 'frame variable' is more efficient, since it uses debug information and
3866f8fc217SDimitry Andric memory reads directly, rather than parsing and evaluating an expression, which
3876f8fc217SDimitry Andric may even involve JITing and running code in the target program.)");
3886f8fc217SDimitry Andric 
389ac9a064cSDimitry Andric     AddSimpleArgumentList(eArgTypeVarName, eArgRepeatStar);
390f034231aSEd Maste 
391f034231aSEd Maste     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
39214f1b3e8SDimitry Andric     m_option_group.Append(&m_option_format,
39314f1b3e8SDimitry Andric                           OptionGroupFormat::OPTION_GROUP_FORMAT |
39414f1b3e8SDimitry Andric                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
39514f1b3e8SDimitry Andric                           LLDB_OPT_SET_1);
396f034231aSEd Maste     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
397f034231aSEd Maste     m_option_group.Finalize();
398f034231aSEd Maste   }
399f034231aSEd Maste 
400f3fbd1c0SDimitry Andric   ~CommandObjectFrameVariable() override = default;
401f034231aSEd Maste 
GetOptions()40214f1b3e8SDimitry Andric   Options *GetOptions() override { return &m_option_group; }
403f034231aSEd Maste 
404f034231aSEd Maste protected:
GetScopeString(VariableSP var_sp)40514f1b3e8SDimitry Andric   llvm::StringRef GetScopeString(VariableSP var_sp) {
40614f1b3e8SDimitry Andric     if (!var_sp)
407344a3780SDimitry Andric       return llvm::StringRef();
40814f1b3e8SDimitry Andric 
40914f1b3e8SDimitry Andric     switch (var_sp->GetScope()) {
41014f1b3e8SDimitry Andric     case eValueTypeVariableGlobal:
41114f1b3e8SDimitry Andric       return "GLOBAL: ";
41214f1b3e8SDimitry Andric     case eValueTypeVariableStatic:
41314f1b3e8SDimitry Andric       return "STATIC: ";
41414f1b3e8SDimitry Andric     case eValueTypeVariableArgument:
41514f1b3e8SDimitry Andric       return "ARG: ";
41614f1b3e8SDimitry Andric     case eValueTypeVariableLocal:
41714f1b3e8SDimitry Andric       return "LOCAL: ";
41814f1b3e8SDimitry Andric     case eValueTypeVariableThreadLocal:
41914f1b3e8SDimitry Andric       return "THREAD: ";
42014f1b3e8SDimitry Andric     default:
42114f1b3e8SDimitry Andric       break;
42214f1b3e8SDimitry Andric     }
42314f1b3e8SDimitry Andric 
424344a3780SDimitry Andric     return llvm::StringRef();
42514f1b3e8SDimitry Andric   }
42614f1b3e8SDimitry Andric 
4277fa27ce4SDimitry Andric   /// Returns true if `scope` matches any of the options in `m_option_variable`.
ScopeRequested(lldb::ValueType scope)4287fa27ce4SDimitry Andric   bool ScopeRequested(lldb::ValueType scope) {
4297fa27ce4SDimitry Andric     switch (scope) {
4307fa27ce4SDimitry Andric     case eValueTypeVariableGlobal:
4317fa27ce4SDimitry Andric     case eValueTypeVariableStatic:
4327fa27ce4SDimitry Andric       return m_option_variable.show_globals;
4337fa27ce4SDimitry Andric     case eValueTypeVariableArgument:
4347fa27ce4SDimitry Andric       return m_option_variable.show_args;
4357fa27ce4SDimitry Andric     case eValueTypeVariableLocal:
4367fa27ce4SDimitry Andric       return m_option_variable.show_locals;
4377fa27ce4SDimitry Andric     case eValueTypeInvalid:
4387fa27ce4SDimitry Andric     case eValueTypeRegister:
4397fa27ce4SDimitry Andric     case eValueTypeRegisterSet:
4407fa27ce4SDimitry Andric     case eValueTypeConstResult:
4417fa27ce4SDimitry Andric     case eValueTypeVariableThreadLocal:
442b1c73532SDimitry Andric     case eValueTypeVTable:
443b1c73532SDimitry Andric     case eValueTypeVTableEntry:
4447fa27ce4SDimitry Andric       return false;
4457fa27ce4SDimitry Andric     }
4464df029ccSDimitry Andric     llvm_unreachable("Unexpected scope value");
4477fa27ce4SDimitry Andric   }
4487fa27ce4SDimitry Andric 
4497fa27ce4SDimitry Andric   /// Finds all the variables in `all_variables` whose name matches `regex`,
4507fa27ce4SDimitry Andric   /// inserting them into `matches`. Variables already contained in `matches`
4517fa27ce4SDimitry Andric   /// are not inserted again.
4527fa27ce4SDimitry Andric   /// Nullopt is returned in case of no matches.
4537fa27ce4SDimitry Andric   /// A sub-range of `matches` with all newly inserted variables is returned.
4547fa27ce4SDimitry Andric   /// This may be empty if all matches were already contained in `matches`.
4557fa27ce4SDimitry Andric   std::optional<llvm::ArrayRef<VariableSP>>
findUniqueRegexMatches(RegularExpression & regex,VariableList & matches,const VariableList & all_variables)4567fa27ce4SDimitry Andric   findUniqueRegexMatches(RegularExpression &regex,
4577fa27ce4SDimitry Andric                          VariableList &matches,
4587fa27ce4SDimitry Andric                          const VariableList &all_variables) {
4597fa27ce4SDimitry Andric     bool any_matches = false;
4607fa27ce4SDimitry Andric     const size_t previous_num_vars = matches.GetSize();
4617fa27ce4SDimitry Andric 
4627fa27ce4SDimitry Andric     for (const VariableSP &var : all_variables) {
4637fa27ce4SDimitry Andric       if (!var->NameMatches(regex) || !ScopeRequested(var->GetScope()))
4647fa27ce4SDimitry Andric         continue;
4657fa27ce4SDimitry Andric       any_matches = true;
4667fa27ce4SDimitry Andric       matches.AddVariableIfUnique(var);
4677fa27ce4SDimitry Andric     }
4687fa27ce4SDimitry Andric 
4697fa27ce4SDimitry Andric     if (any_matches)
4707fa27ce4SDimitry Andric       return matches.toArrayRef().drop_front(previous_num_vars);
4717fa27ce4SDimitry Andric     return std::nullopt;
4727fa27ce4SDimitry Andric   }
4737fa27ce4SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)474b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
475f73363f1SDimitry Andric     // No need to check "frame" for validity as eCommandRequiresFrame ensures
476f73363f1SDimitry Andric     // it is valid
477f034231aSEd Maste     StackFrame *frame = m_exe_ctx.GetFramePtr();
478f034231aSEd Maste 
479f034231aSEd Maste     Stream &s = result.GetOutputStream();
480f034231aSEd Maste 
4817fa27ce4SDimitry Andric     // Using a regex should behave like looking for an exact name match: it
4827fa27ce4SDimitry Andric     // also finds globals.
4837fa27ce4SDimitry Andric     m_option_variable.show_globals |= m_option_variable.use_regex;
4847fa27ce4SDimitry Andric 
48514f1b3e8SDimitry Andric     // Be careful about the stack frame, if any summary formatter runs code, it
486f73363f1SDimitry Andric     // might clear the StackFrameList for the thread.  So hold onto a shared
487f73363f1SDimitry Andric     // pointer to the frame so it stays alive.
488f034231aSEd Maste 
489e3b55780SDimitry Andric     Status error;
49014f1b3e8SDimitry Andric     VariableList *variable_list =
491e3b55780SDimitry Andric         frame->GetVariableList(m_option_variable.show_globals, &error);
492f034231aSEd Maste 
493e3b55780SDimitry Andric     if (error.Fail() && (!variable_list || variable_list->GetSize() == 0)) {
494e3b55780SDimitry Andric       result.AppendError(error.AsCString());
495e3b55780SDimitry Andric 
496e3b55780SDimitry Andric     }
497f034231aSEd Maste     ValueObjectSP valobj_sp;
498f034231aSEd Maste 
499f034231aSEd Maste     TypeSummaryImplSP summary_format_sp;
500f034231aSEd Maste     if (!m_option_variable.summary.IsCurrentValueEmpty())
50114f1b3e8SDimitry Andric       DataVisualization::NamedSummaryFormats::GetSummaryFormat(
50214f1b3e8SDimitry Andric           ConstString(m_option_variable.summary.GetCurrentValue()),
50314f1b3e8SDimitry Andric           summary_format_sp);
504f034231aSEd Maste     else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
5055f29bb8aSDimitry Andric       summary_format_sp = std::make_shared<StringSummaryFormat>(
50614f1b3e8SDimitry Andric           TypeSummaryImpl::Flags(),
5075f29bb8aSDimitry Andric           m_option_variable.summary_string.GetCurrentValue());
508f034231aSEd Maste 
50914f1b3e8SDimitry Andric     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
51014f1b3e8SDimitry Andric         eLanguageRuntimeDescriptionDisplayVerbosityFull, eFormatDefault,
51114f1b3e8SDimitry Andric         summary_format_sp));
512f034231aSEd Maste 
51314f1b3e8SDimitry Andric     const SymbolContext &sym_ctx =
51414f1b3e8SDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction);
515e81d9d49SDimitry Andric     if (sym_ctx.function && sym_ctx.function->IsTopLevelFunction())
516e81d9d49SDimitry Andric       m_option_variable.show_globals = true;
517e81d9d49SDimitry Andric 
51814f1b3e8SDimitry Andric     if (variable_list) {
519f034231aSEd Maste       const Format format = m_option_format.GetFormat();
520f034231aSEd Maste       options.SetFormat(format);
521f034231aSEd Maste 
52214f1b3e8SDimitry Andric       if (!command.empty()) {
523f034231aSEd Maste         VariableList regex_var_list;
524f034231aSEd Maste 
525f73363f1SDimitry Andric         // If we have any args to the variable command, we will make variable
526f73363f1SDimitry Andric         // objects from them...
52714f1b3e8SDimitry Andric         for (auto &entry : command) {
52814f1b3e8SDimitry Andric           if (m_option_variable.use_regex) {
529ead24645SDimitry Andric             llvm::StringRef name_str = entry.ref();
53014f1b3e8SDimitry Andric             RegularExpression regex(name_str);
531ead24645SDimitry Andric             if (regex.IsValid()) {
5327fa27ce4SDimitry Andric               std::optional<llvm::ArrayRef<VariableSP>> results =
5337fa27ce4SDimitry Andric                   findUniqueRegexMatches(regex, regex_var_list, *variable_list);
5347fa27ce4SDimitry Andric               if (!results) {
5357fa27ce4SDimitry Andric                 result.AppendErrorWithFormat(
5367fa27ce4SDimitry Andric                     "no variables matched the regular expression '%s'.",
5377fa27ce4SDimitry Andric                     entry.c_str());
5387fa27ce4SDimitry Andric                 continue;
5397fa27ce4SDimitry Andric               }
5407fa27ce4SDimitry Andric               for (const VariableSP &var_sp : *results) {
54114f1b3e8SDimitry Andric                 valobj_sp = frame->GetValueObjectForFrameVariable(
54214f1b3e8SDimitry Andric                     var_sp, m_varobj_options.use_dynamic);
54314f1b3e8SDimitry Andric                 if (valobj_sp) {
54414f1b3e8SDimitry Andric                   std::string scope_string;
54514f1b3e8SDimitry Andric                   if (m_option_variable.show_scope)
54614f1b3e8SDimitry Andric                     scope_string = GetScopeString(var_sp).str();
547f034231aSEd Maste 
54814f1b3e8SDimitry Andric                   if (!scope_string.empty())
54914f1b3e8SDimitry Andric                     s.PutCString(scope_string);
55014f1b3e8SDimitry Andric 
55114f1b3e8SDimitry Andric                   if (m_option_variable.show_decl &&
55214f1b3e8SDimitry Andric                       var_sp->GetDeclaration().GetFile()) {
553f034231aSEd Maste                     bool show_fullpaths = false;
554f034231aSEd Maste                     bool show_module = true;
55514f1b3e8SDimitry Andric                     if (var_sp->DumpDeclaration(&s, show_fullpaths,
55614f1b3e8SDimitry Andric                                                 show_module))
557f034231aSEd Maste                       s.PutCString(": ");
558f034231aSEd Maste                   }
559ac9a064cSDimitry Andric                   auto &strm = result.GetOutputStream();
560ac9a064cSDimitry Andric                   if (llvm::Error error = valobj_sp->Dump(strm, options))
561ac9a064cSDimitry Andric                     result.AppendError(toString(std::move(error)));
562f034231aSEd Maste                 }
563f034231aSEd Maste               }
56414f1b3e8SDimitry Andric             } else {
565ead24645SDimitry Andric               if (llvm::Error err = regex.GetError())
5666f8fc217SDimitry Andric                 result.AppendError(llvm::toString(std::move(err)));
567f034231aSEd Maste               else
5686f8fc217SDimitry Andric                 result.AppendErrorWithFormat(
5696f8fc217SDimitry Andric                     "unknown regex error when compiling '%s'", entry.c_str());
570f034231aSEd Maste             }
57114f1b3e8SDimitry Andric           } else // No regex, either exact variable names or variable
57214f1b3e8SDimitry Andric                  // expressions.
573f034231aSEd Maste           {
574b76161e4SDimitry Andric             Status error;
57514f1b3e8SDimitry Andric             uint32_t expr_path_options =
57614f1b3e8SDimitry Andric                 StackFrame::eExpressionPathOptionCheckPtrVsMember |
577e81d9d49SDimitry Andric                 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess |
578e81d9d49SDimitry Andric                 StackFrame::eExpressionPathOptionsInspectAnonymousUnions;
579f034231aSEd Maste             lldb::VariableSP var_sp;
58014f1b3e8SDimitry Andric             valobj_sp = frame->GetValueForVariableExpressionPath(
581ead24645SDimitry Andric                 entry.ref(), m_varobj_options.use_dynamic, expr_path_options,
58214f1b3e8SDimitry Andric                 var_sp, error);
58314f1b3e8SDimitry Andric             if (valobj_sp) {
58414f1b3e8SDimitry Andric               std::string scope_string;
58514f1b3e8SDimitry Andric               if (m_option_variable.show_scope)
58614f1b3e8SDimitry Andric                 scope_string = GetScopeString(var_sp).str();
58714f1b3e8SDimitry Andric 
58814f1b3e8SDimitry Andric               if (!scope_string.empty())
58914f1b3e8SDimitry Andric                 s.PutCString(scope_string);
59014f1b3e8SDimitry Andric               if (m_option_variable.show_decl && var_sp &&
59114f1b3e8SDimitry Andric                   var_sp->GetDeclaration().GetFile()) {
592f034231aSEd Maste                 var_sp->GetDeclaration().DumpStopContext(&s, false);
593f034231aSEd Maste                 s.PutCString(": ");
594f034231aSEd Maste               }
595f034231aSEd Maste 
596f034231aSEd Maste               options.SetFormat(format);
59714f1b3e8SDimitry Andric               options.SetVariableFormatDisplayLanguage(
59814f1b3e8SDimitry Andric                   valobj_sp->GetPreferredDisplayLanguage());
599f034231aSEd Maste 
600f034231aSEd Maste               Stream &output_stream = result.GetOutputStream();
60114f1b3e8SDimitry Andric               options.SetRootValueObjectName(
60214f1b3e8SDimitry Andric                   valobj_sp->GetParent() ? entry.c_str() : nullptr);
603ac9a064cSDimitry Andric               if (llvm::Error error = valobj_sp->Dump(output_stream, options))
604ac9a064cSDimitry Andric                 result.AppendError(toString(std::move(error)));
60514f1b3e8SDimitry Andric             } else {
6066f8fc217SDimitry Andric               if (auto error_cstr = error.AsCString(nullptr))
6076f8fc217SDimitry Andric                 result.AppendError(error_cstr);
608f034231aSEd Maste               else
6096f8fc217SDimitry Andric                 result.AppendErrorWithFormat(
6106f8fc217SDimitry Andric                     "unable to find any variable expression path that matches "
6116f8fc217SDimitry Andric                     "'%s'.",
61214f1b3e8SDimitry Andric                     entry.c_str());
613f034231aSEd Maste             }
614f034231aSEd Maste           }
615f034231aSEd Maste         }
61614f1b3e8SDimitry Andric       } else // No command arg specified.  Use variable_list, instead.
617f034231aSEd Maste       {
618f034231aSEd Maste         const size_t num_variables = variable_list->GetSize();
61914f1b3e8SDimitry Andric         if (num_variables > 0) {
62014f1b3e8SDimitry Andric           for (size_t i = 0; i < num_variables; i++) {
6217fa27ce4SDimitry Andric             VariableSP var_sp = variable_list->GetVariableAtIndex(i);
6227fa27ce4SDimitry Andric             if (!ScopeRequested(var_sp->GetScope()))
623d44a35e8SDimitry Andric                 continue;
6245e95aa85SEd Maste             std::string scope_string;
625d44a35e8SDimitry Andric             if (m_option_variable.show_scope)
62614f1b3e8SDimitry Andric               scope_string = GetScopeString(var_sp).str();
627f034231aSEd Maste 
628f73363f1SDimitry Andric             // Use the variable object code to make sure we are using the same
629f73363f1SDimitry Andric             // APIs as the public API will be using...
63014f1b3e8SDimitry Andric             valobj_sp = frame->GetValueObjectForFrameVariable(
63114f1b3e8SDimitry Andric                 var_sp, m_varobj_options.use_dynamic);
63214f1b3e8SDimitry Andric             if (valobj_sp) {
633f73363f1SDimitry Andric               // When dumping all variables, don't print any variables that are
634f73363f1SDimitry Andric               // not in scope to avoid extra unneeded output
63514f1b3e8SDimitry Andric               if (valobj_sp->IsInScope()) {
63614f1b3e8SDimitry Andric                 if (!valobj_sp->GetTargetSP()
63714f1b3e8SDimitry Andric                          ->GetDisplayRuntimeSupportValues() &&
638f3fbd1c0SDimitry Andric                     valobj_sp->IsRuntimeSupportValue())
6395e95aa85SEd Maste                   continue;
6405e95aa85SEd Maste 
6415e95aa85SEd Maste                 if (!scope_string.empty())
64214f1b3e8SDimitry Andric                   s.PutCString(scope_string);
6435e95aa85SEd Maste 
64414f1b3e8SDimitry Andric                 if (m_option_variable.show_decl &&
64514f1b3e8SDimitry Andric                     var_sp->GetDeclaration().GetFile()) {
646f034231aSEd Maste                   var_sp->GetDeclaration().DumpStopContext(&s, false);
647f034231aSEd Maste                   s.PutCString(": ");
648f034231aSEd Maste                 }
649f034231aSEd Maste 
650f034231aSEd Maste                 options.SetFormat(format);
65114f1b3e8SDimitry Andric                 options.SetVariableFormatDisplayLanguage(
65214f1b3e8SDimitry Andric                     valobj_sp->GetPreferredDisplayLanguage());
65314f1b3e8SDimitry Andric                 options.SetRootValueObjectName(
65414f1b3e8SDimitry Andric                     var_sp ? var_sp->GetName().AsCString() : nullptr);
655ac9a064cSDimitry Andric                 if (llvm::Error error =
656ac9a064cSDimitry Andric                         valobj_sp->Dump(result.GetOutputStream(), options))
657ac9a064cSDimitry Andric                   result.AppendError(toString(std::move(error)));
658f034231aSEd Maste               }
659f034231aSEd Maste             }
660f034231aSEd Maste           }
661f034231aSEd Maste         }
662f034231aSEd Maste       }
6636f8fc217SDimitry Andric       if (result.GetStatus() != eReturnStatusFailed)
664f034231aSEd Maste         result.SetStatus(eReturnStatusSuccessFinishResult);
665f034231aSEd Maste     }
666f034231aSEd Maste 
66794994d37SDimitry Andric     if (m_option_variable.show_recognized_args) {
66894994d37SDimitry Andric       auto recognized_frame = frame->GetRecognizedFrame();
66994994d37SDimitry Andric       if (recognized_frame) {
67094994d37SDimitry Andric         ValueObjectListSP recognized_arg_list =
67194994d37SDimitry Andric             recognized_frame->GetRecognizedArguments();
67294994d37SDimitry Andric         if (recognized_arg_list) {
67394994d37SDimitry Andric           for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
67494994d37SDimitry Andric             options.SetFormat(m_option_format.GetFormat());
67594994d37SDimitry Andric             options.SetVariableFormatDisplayLanguage(
67694994d37SDimitry Andric                 rec_value_sp->GetPreferredDisplayLanguage());
67794994d37SDimitry Andric             options.SetRootValueObjectName(rec_value_sp->GetName().AsCString());
678ac9a064cSDimitry Andric             if (llvm::Error error =
679ac9a064cSDimitry Andric                     rec_value_sp->Dump(result.GetOutputStream(), options))
680ac9a064cSDimitry Andric               result.AppendError(toString(std::move(error)));
68194994d37SDimitry Andric           }
68294994d37SDimitry Andric         }
68394994d37SDimitry Andric       }
68494994d37SDimitry Andric     }
68594994d37SDimitry Andric 
686145449b1SDimitry Andric     m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
687145449b1SDimitry Andric                                            m_cmd_name);
688f034231aSEd Maste 
689f73363f1SDimitry Andric     // Increment statistics.
690c0981da4SDimitry Andric     TargetStats &target_stats = GetSelectedOrDummyTarget().GetStatistics();
691b1c73532SDimitry Andric     if (result.Succeeded())
692c0981da4SDimitry Andric       target_stats.GetFrameVariableStats().NotifySuccess();
693f73363f1SDimitry Andric     else
694c0981da4SDimitry Andric       target_stats.GetFrameVariableStats().NotifyFailure();
695f034231aSEd Maste   }
696f034231aSEd Maste 
697f034231aSEd Maste   OptionGroupOptions m_option_group;
698f034231aSEd Maste   OptionGroupVariable m_option_variable;
699f034231aSEd Maste   OptionGroupFormat m_option_format;
700f034231aSEd Maste   OptionGroupValueObjectDisplay m_varobj_options;
701f034231aSEd Maste };
702f034231aSEd Maste 
70394994d37SDimitry Andric #pragma mark CommandObjectFrameRecognizer
70494994d37SDimitry Andric 
705ead24645SDimitry Andric #define LLDB_OPTIONS_frame_recognizer_add
706ead24645SDimitry Andric #include "CommandOptions.inc"
70794994d37SDimitry Andric 
70894994d37SDimitry Andric class CommandObjectFrameRecognizerAdd : public CommandObjectParsed {
70994994d37SDimitry Andric private:
71094994d37SDimitry Andric   class CommandOptions : public Options {
71194994d37SDimitry Andric   public:
712145449b1SDimitry Andric     CommandOptions() = default;
71394994d37SDimitry Andric     ~CommandOptions() override = default;
71494994d37SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)71594994d37SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
71694994d37SDimitry Andric                           ExecutionContext *execution_context) override {
71794994d37SDimitry Andric       Status error;
71894994d37SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
71994994d37SDimitry Andric 
72094994d37SDimitry Andric       switch (short_option) {
721c0981da4SDimitry Andric       case 'f': {
722c0981da4SDimitry Andric         bool value, success;
723c0981da4SDimitry Andric         value = OptionArgParser::ToBoolean(option_arg, true, &success);
724c0981da4SDimitry Andric         if (success) {
725c0981da4SDimitry Andric           m_first_instruction_only = value;
726c0981da4SDimitry Andric         } else {
727c0981da4SDimitry Andric           error.SetErrorStringWithFormat(
728c0981da4SDimitry Andric               "invalid boolean value '%s' passed for -f option",
729c0981da4SDimitry Andric               option_arg.str().c_str());
730c0981da4SDimitry Andric         }
731c0981da4SDimitry Andric       } break;
73294994d37SDimitry Andric       case 'l':
73394994d37SDimitry Andric         m_class_name = std::string(option_arg);
73494994d37SDimitry Andric         break;
73594994d37SDimitry Andric       case 's':
73694994d37SDimitry Andric         m_module = std::string(option_arg);
73794994d37SDimitry Andric         break;
73894994d37SDimitry Andric       case 'n':
739cfca06d7SDimitry Andric         m_symbols.push_back(std::string(option_arg));
74094994d37SDimitry Andric         break;
74194994d37SDimitry Andric       case 'x':
74294994d37SDimitry Andric         m_regex = true;
74394994d37SDimitry Andric         break;
74494994d37SDimitry Andric       default:
745ead24645SDimitry Andric         llvm_unreachable("Unimplemented option");
74694994d37SDimitry Andric       }
74794994d37SDimitry Andric 
74894994d37SDimitry Andric       return error;
74994994d37SDimitry Andric     }
75094994d37SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)75194994d37SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
75294994d37SDimitry Andric       m_module = "";
753cfca06d7SDimitry Andric       m_symbols.clear();
75494994d37SDimitry Andric       m_class_name = "";
75594994d37SDimitry Andric       m_regex = false;
756c0981da4SDimitry Andric       m_first_instruction_only = true;
75794994d37SDimitry Andric     }
75894994d37SDimitry Andric 
GetDefinitions()75994994d37SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
760e3b55780SDimitry Andric       return llvm::ArrayRef(g_frame_recognizer_add_options);
76194994d37SDimitry Andric     }
76294994d37SDimitry Andric 
76394994d37SDimitry Andric     // Instance variables to hold the values for command options.
76494994d37SDimitry Andric     std::string m_class_name;
76594994d37SDimitry Andric     std::string m_module;
766cfca06d7SDimitry Andric     std::vector<std::string> m_symbols;
76794994d37SDimitry Andric     bool m_regex;
768c0981da4SDimitry Andric     bool m_first_instruction_only;
76994994d37SDimitry Andric   };
77094994d37SDimitry Andric 
77194994d37SDimitry Andric   CommandOptions m_options;
77294994d37SDimitry Andric 
GetOptions()77394994d37SDimitry Andric   Options *GetOptions() override { return &m_options; }
77494994d37SDimitry Andric 
77594994d37SDimitry Andric protected:
776b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override;
77794994d37SDimitry Andric 
77894994d37SDimitry Andric public:
CommandObjectFrameRecognizerAdd(CommandInterpreter & interpreter)77994994d37SDimitry Andric   CommandObjectFrameRecognizerAdd(CommandInterpreter &interpreter)
78094994d37SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer add",
7816f8fc217SDimitry Andric                             "Add a new frame recognizer.", nullptr) {
78294994d37SDimitry Andric     SetHelpLong(R"(
78394994d37SDimitry Andric Frame recognizers allow for retrieving information about special frames based on
78494994d37SDimitry Andric ABI, arguments or other special properties of that frame, even without source
78594994d37SDimitry Andric code or debug info. Currently, one use case is to extract function arguments
78694994d37SDimitry Andric that would otherwise be unaccesible, or augment existing arguments.
78794994d37SDimitry Andric 
78894994d37SDimitry Andric Adding a custom frame recognizer is possible by implementing a Python class
78994994d37SDimitry Andric and using the 'frame recognizer add' command. The Python class should have a
79094994d37SDimitry Andric 'get_recognized_arguments' method and it will receive an argument of type
79194994d37SDimitry Andric lldb.SBFrame representing the current frame that we are trying to recognize.
79294994d37SDimitry Andric The method should return a (possibly empty) list of lldb.SBValue objects that
79394994d37SDimitry Andric represent the recognized arguments.
79494994d37SDimitry Andric 
79594994d37SDimitry Andric An example of a recognizer that retrieves the file descriptor values from libc
79694994d37SDimitry Andric functions 'read', 'write' and 'close' follows:
79794994d37SDimitry Andric 
79894994d37SDimitry Andric   class LibcFdRecognizer(object):
79994994d37SDimitry Andric     def get_recognized_arguments(self, frame):
80094994d37SDimitry Andric       if frame.name in ["read", "write", "close"]:
80194994d37SDimitry Andric         fd = frame.EvaluateExpression("$arg1").unsigned
802e3b55780SDimitry Andric         target = frame.thread.process.target
803e3b55780SDimitry Andric         value = target.CreateValueFromExpression("fd", "(int)%d" % fd)
80494994d37SDimitry Andric         return [value]
80594994d37SDimitry Andric       return []
80694994d37SDimitry Andric 
80794994d37SDimitry Andric The file containing this implementation can be imported via 'command script
80894994d37SDimitry Andric import' and then we can register this recognizer with 'frame recognizer add'.
80994994d37SDimitry Andric It's important to restrict the recognizer to the libc library (which is
81094994d37SDimitry Andric libsystem_kernel.dylib on macOS) to avoid matching functions with the same name
81194994d37SDimitry Andric in other modules:
81294994d37SDimitry Andric 
81394994d37SDimitry Andric (lldb) command script import .../fd_recognizer.py
81494994d37SDimitry Andric (lldb) frame recognizer add -l fd_recognizer.LibcFdRecognizer -n read -s libsystem_kernel.dylib
81594994d37SDimitry Andric 
81694994d37SDimitry Andric When the program is stopped at the beginning of the 'read' function in libc, we
81794994d37SDimitry Andric can view the recognizer arguments in 'frame variable':
81894994d37SDimitry Andric 
81994994d37SDimitry Andric (lldb) b read
82094994d37SDimitry Andric (lldb) r
82194994d37SDimitry Andric Process 1234 stopped
82294994d37SDimitry Andric * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.3
82394994d37SDimitry Andric     frame #0: 0x00007fff06013ca0 libsystem_kernel.dylib`read
82494994d37SDimitry Andric (lldb) frame variable
82594994d37SDimitry Andric (int) fd = 3
82694994d37SDimitry Andric 
82794994d37SDimitry Andric     )");
82894994d37SDimitry Andric   }
82994994d37SDimitry Andric   ~CommandObjectFrameRecognizerAdd() override = default;
83094994d37SDimitry Andric };
83194994d37SDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)832b1c73532SDimitry Andric void CommandObjectFrameRecognizerAdd::DoExecute(Args &command,
83394994d37SDimitry Andric                                                 CommandReturnObject &result) {
834706b4fc4SDimitry Andric #if LLDB_ENABLE_PYTHON
83594994d37SDimitry Andric   if (m_options.m_class_name.empty()) {
83694994d37SDimitry Andric     result.AppendErrorWithFormat(
83794994d37SDimitry Andric         "%s needs a Python class name (-l argument).\n", m_cmd_name.c_str());
838b1c73532SDimitry Andric     return;
83994994d37SDimitry Andric   }
84094994d37SDimitry Andric 
84194994d37SDimitry Andric   if (m_options.m_module.empty()) {
84294994d37SDimitry Andric     result.AppendErrorWithFormat("%s needs a module name (-s argument).\n",
84394994d37SDimitry Andric                                  m_cmd_name.c_str());
844b1c73532SDimitry Andric     return;
84594994d37SDimitry Andric   }
84694994d37SDimitry Andric 
847cfca06d7SDimitry Andric   if (m_options.m_symbols.empty()) {
848cfca06d7SDimitry Andric     result.AppendErrorWithFormat(
849cfca06d7SDimitry Andric         "%s needs at least one symbol name (-n argument).\n",
850cfca06d7SDimitry Andric         m_cmd_name.c_str());
851b1c73532SDimitry Andric     return;
852cfca06d7SDimitry Andric   }
853cfca06d7SDimitry Andric 
854cfca06d7SDimitry Andric   if (m_options.m_regex && m_options.m_symbols.size() > 1) {
855cfca06d7SDimitry Andric     result.AppendErrorWithFormat(
856cfca06d7SDimitry Andric         "%s needs only one symbol regular expression (-n argument).\n",
85794994d37SDimitry Andric         m_cmd_name.c_str());
858b1c73532SDimitry Andric     return;
85994994d37SDimitry Andric   }
86094994d37SDimitry Andric 
8615f29bb8aSDimitry Andric   ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
86294994d37SDimitry Andric 
86394994d37SDimitry Andric   if (interpreter &&
86494994d37SDimitry Andric       !interpreter->CheckObjectExists(m_options.m_class_name.c_str())) {
865706b4fc4SDimitry Andric     result.AppendWarning("The provided class does not exist - please define it "
86694994d37SDimitry Andric                          "before attempting to use this frame recognizer");
86794994d37SDimitry Andric   }
86894994d37SDimitry Andric 
86994994d37SDimitry Andric   StackFrameRecognizerSP recognizer_sp =
87094994d37SDimitry Andric       StackFrameRecognizerSP(new ScriptedStackFrameRecognizer(
87194994d37SDimitry Andric           interpreter, m_options.m_class_name.c_str()));
87294994d37SDimitry Andric   if (m_options.m_regex) {
87394994d37SDimitry Andric     auto module =
87494994d37SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_module));
87594994d37SDimitry Andric     auto func =
876cfca06d7SDimitry Andric         RegularExpressionSP(new RegularExpression(m_options.m_symbols.front()));
877b60736ecSDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
878c0981da4SDimitry Andric         recognizer_sp, module, func, m_options.m_first_instruction_only);
87994994d37SDimitry Andric   } else {
88094994d37SDimitry Andric     auto module = ConstString(m_options.m_module);
881cfca06d7SDimitry Andric     std::vector<ConstString> symbols(m_options.m_symbols.begin(),
882cfca06d7SDimitry Andric                                      m_options.m_symbols.end());
883b60736ecSDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().AddRecognizer(
884c0981da4SDimitry Andric         recognizer_sp, module, symbols, m_options.m_first_instruction_only);
88594994d37SDimitry Andric   }
88694994d37SDimitry Andric #endif
88794994d37SDimitry Andric 
88894994d37SDimitry Andric   result.SetStatus(eReturnStatusSuccessFinishNoResult);
88994994d37SDimitry Andric }
89094994d37SDimitry Andric 
89194994d37SDimitry Andric class CommandObjectFrameRecognizerClear : public CommandObjectParsed {
89294994d37SDimitry Andric public:
CommandObjectFrameRecognizerClear(CommandInterpreter & interpreter)89394994d37SDimitry Andric   CommandObjectFrameRecognizerClear(CommandInterpreter &interpreter)
89494994d37SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer clear",
89594994d37SDimitry Andric                             "Delete all frame recognizers.", nullptr) {}
89694994d37SDimitry Andric 
89794994d37SDimitry Andric   ~CommandObjectFrameRecognizerClear() override = default;
89894994d37SDimitry Andric 
89994994d37SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)900b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
901b60736ecSDimitry Andric     GetSelectedOrDummyTarget()
902b60736ecSDimitry Andric         .GetFrameRecognizerManager()
903b60736ecSDimitry Andric         .RemoveAllRecognizers();
90494994d37SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
90594994d37SDimitry Andric   }
90694994d37SDimitry Andric };
90794994d37SDimitry Andric 
90894994d37SDimitry Andric class CommandObjectFrameRecognizerDelete : public CommandObjectParsed {
90994994d37SDimitry Andric public:
CommandObjectFrameRecognizerDelete(CommandInterpreter & interpreter)91094994d37SDimitry Andric   CommandObjectFrameRecognizerDelete(CommandInterpreter &interpreter)
91194994d37SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer delete",
912145449b1SDimitry Andric                             "Delete an existing frame recognizer by id.",
913145449b1SDimitry Andric                             nullptr) {
914ac9a064cSDimitry Andric     AddSimpleArgumentList(eArgTypeRecognizerID);
915145449b1SDimitry Andric   }
91694994d37SDimitry Andric 
91794994d37SDimitry Andric   ~CommandObjectFrameRecognizerDelete() override = default;
91894994d37SDimitry Andric 
919cfca06d7SDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)920cfca06d7SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
921cfca06d7SDimitry Andric                            OptionElementVector &opt_element_vector) override {
922cfca06d7SDimitry Andric     if (request.GetCursorIndex() != 0)
923cfca06d7SDimitry Andric       return;
924cfca06d7SDimitry Andric 
925b60736ecSDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
926cfca06d7SDimitry Andric         [&request](uint32_t rid, std::string rname, std::string module,
927cfca06d7SDimitry Andric                    llvm::ArrayRef<lldb_private::ConstString> symbols,
928cfca06d7SDimitry Andric                    bool regexp) {
929cfca06d7SDimitry Andric           StreamString strm;
930cfca06d7SDimitry Andric           if (rname.empty())
931cfca06d7SDimitry Andric             rname = "(internal)";
932cfca06d7SDimitry Andric 
933cfca06d7SDimitry Andric           strm << rname;
934cfca06d7SDimitry Andric           if (!module.empty())
935cfca06d7SDimitry Andric             strm << ", module " << module;
936cfca06d7SDimitry Andric           if (!symbols.empty())
937cfca06d7SDimitry Andric             for (auto &symbol : symbols)
938cfca06d7SDimitry Andric               strm << ", symbol " << symbol;
939cfca06d7SDimitry Andric           if (regexp)
940cfca06d7SDimitry Andric             strm << " (regexp)";
941cfca06d7SDimitry Andric 
942cfca06d7SDimitry Andric           request.TryCompleteCurrentArg(std::to_string(rid), strm.GetString());
943cfca06d7SDimitry Andric         });
944cfca06d7SDimitry Andric   }
945cfca06d7SDimitry Andric 
94694994d37SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)947b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
94894994d37SDimitry Andric     if (command.GetArgumentCount() == 0) {
94994994d37SDimitry Andric       if (!m_interpreter.Confirm(
95094994d37SDimitry Andric               "About to delete all frame recognizers, do you want to do that?",
95194994d37SDimitry Andric               true)) {
95294994d37SDimitry Andric         result.AppendMessage("Operation cancelled...");
953b1c73532SDimitry Andric         return;
95494994d37SDimitry Andric       }
95594994d37SDimitry Andric 
956b60736ecSDimitry Andric       GetSelectedOrDummyTarget()
957b60736ecSDimitry Andric           .GetFrameRecognizerManager()
958b60736ecSDimitry Andric           .RemoveAllRecognizers();
95994994d37SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
960b1c73532SDimitry Andric       return;
96194994d37SDimitry Andric     }
96294994d37SDimitry Andric 
96394994d37SDimitry Andric     if (command.GetArgumentCount() != 1) {
96494994d37SDimitry Andric       result.AppendErrorWithFormat("'%s' takes zero or one arguments.\n",
96594994d37SDimitry Andric                                    m_cmd_name.c_str());
966b1c73532SDimitry Andric       return;
96794994d37SDimitry Andric     }
96894994d37SDimitry Andric 
969cfca06d7SDimitry Andric     uint32_t recognizer_id;
970cfca06d7SDimitry Andric     if (!llvm::to_integer(command.GetArgumentAtIndex(0), recognizer_id)) {
971cfca06d7SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
972cfca06d7SDimitry Andric                                    command.GetArgumentAtIndex(0));
973b1c73532SDimitry Andric       return;
974cfca06d7SDimitry Andric     }
97594994d37SDimitry Andric 
976b60736ecSDimitry Andric     if (!GetSelectedOrDummyTarget()
977b60736ecSDimitry Andric              .GetFrameRecognizerManager()
978b60736ecSDimitry Andric              .RemoveRecognizerWithID(recognizer_id)) {
979b60736ecSDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid recognizer id.\n",
980b60736ecSDimitry Andric                                    command.GetArgumentAtIndex(0));
981b1c73532SDimitry Andric       return;
982b60736ecSDimitry Andric     }
98394994d37SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
98494994d37SDimitry Andric   }
98594994d37SDimitry Andric };
98694994d37SDimitry Andric 
98794994d37SDimitry Andric class CommandObjectFrameRecognizerList : public CommandObjectParsed {
98894994d37SDimitry Andric public:
CommandObjectFrameRecognizerList(CommandInterpreter & interpreter)98994994d37SDimitry Andric   CommandObjectFrameRecognizerList(CommandInterpreter &interpreter)
99094994d37SDimitry Andric       : CommandObjectParsed(interpreter, "frame recognizer list",
99194994d37SDimitry Andric                             "Show a list of active frame recognizers.",
99294994d37SDimitry Andric                             nullptr) {}
99394994d37SDimitry Andric 
99494994d37SDimitry Andric   ~CommandObjectFrameRecognizerList() override = default;
99594994d37SDimitry Andric 
99694994d37SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)997b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
99894994d37SDimitry Andric     bool any_printed = false;
999b60736ecSDimitry Andric     GetSelectedOrDummyTarget().GetFrameRecognizerManager().ForEach(
1000cfca06d7SDimitry Andric         [&result, &any_printed](
1001cfca06d7SDimitry Andric             uint32_t recognizer_id, std::string name, std::string module,
1002cfca06d7SDimitry Andric             llvm::ArrayRef<ConstString> symbols, bool regexp) {
1003cfca06d7SDimitry Andric           Stream &stream = result.GetOutputStream();
1004cfca06d7SDimitry Andric 
1005cfca06d7SDimitry Andric           if (name.empty())
1006706b4fc4SDimitry Andric             name = "(internal)";
1007cfca06d7SDimitry Andric 
1008cfca06d7SDimitry Andric           stream << std::to_string(recognizer_id) << ": " << name;
1009cfca06d7SDimitry Andric           if (!module.empty())
1010cfca06d7SDimitry Andric             stream << ", module " << module;
1011cfca06d7SDimitry Andric           if (!symbols.empty())
1012cfca06d7SDimitry Andric             for (auto &symbol : symbols)
1013cfca06d7SDimitry Andric               stream << ", symbol " << symbol;
1014cfca06d7SDimitry Andric           if (regexp)
1015cfca06d7SDimitry Andric             stream << " (regexp)";
1016cfca06d7SDimitry Andric 
1017cfca06d7SDimitry Andric           stream.EOL();
1018cfca06d7SDimitry Andric           stream.Flush();
1019cfca06d7SDimitry Andric 
102094994d37SDimitry Andric           any_printed = true;
102194994d37SDimitry Andric         });
102294994d37SDimitry Andric 
102394994d37SDimitry Andric     if (any_printed)
102494994d37SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
102594994d37SDimitry Andric     else {
102694994d37SDimitry Andric       result.GetOutputStream().PutCString("no matching results found.\n");
102794994d37SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishNoResult);
102894994d37SDimitry Andric     }
102994994d37SDimitry Andric   }
103094994d37SDimitry Andric };
103194994d37SDimitry Andric 
103294994d37SDimitry Andric class CommandObjectFrameRecognizerInfo : public CommandObjectParsed {
103394994d37SDimitry Andric public:
CommandObjectFrameRecognizerInfo(CommandInterpreter & interpreter)103494994d37SDimitry Andric   CommandObjectFrameRecognizerInfo(CommandInterpreter &interpreter)
103594994d37SDimitry Andric       : CommandObjectParsed(
103694994d37SDimitry Andric             interpreter, "frame recognizer info",
103794994d37SDimitry Andric             "Show which frame recognizer is applied a stack frame (if any).",
103894994d37SDimitry Andric             nullptr) {
1039ac9a064cSDimitry Andric     AddSimpleArgumentList(eArgTypeFrameIndex);
104094994d37SDimitry Andric   }
104194994d37SDimitry Andric 
104294994d37SDimitry Andric   ~CommandObjectFrameRecognizerInfo() override = default;
104394994d37SDimitry Andric 
104494994d37SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)1045b1c73532SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
1046cfca06d7SDimitry Andric     const char *frame_index_str = command.GetArgumentAtIndex(0);
1047cfca06d7SDimitry Andric     uint32_t frame_index;
1048cfca06d7SDimitry Andric     if (!llvm::to_integer(frame_index_str, frame_index)) {
1049cfca06d7SDimitry Andric       result.AppendErrorWithFormat("'%s' is not a valid frame index.",
1050cfca06d7SDimitry Andric                                    frame_index_str);
1051b1c73532SDimitry Andric       return;
1052cfca06d7SDimitry Andric     }
1053cfca06d7SDimitry Andric 
105494994d37SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
105594994d37SDimitry Andric     if (process == nullptr) {
105694994d37SDimitry Andric       result.AppendError("no process");
1057b1c73532SDimitry Andric       return;
105894994d37SDimitry Andric     }
105994994d37SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
106094994d37SDimitry Andric     if (thread == nullptr) {
106194994d37SDimitry Andric       result.AppendError("no thread");
1062b1c73532SDimitry Andric       return;
106394994d37SDimitry Andric     }
106494994d37SDimitry Andric     if (command.GetArgumentCount() != 1) {
106594994d37SDimitry Andric       result.AppendErrorWithFormat(
106694994d37SDimitry Andric           "'%s' takes exactly one frame index argument.\n", m_cmd_name.c_str());
1067b1c73532SDimitry Andric       return;
106894994d37SDimitry Andric     }
106994994d37SDimitry Andric 
107094994d37SDimitry Andric     StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_index);
107194994d37SDimitry Andric     if (!frame_sp) {
107294994d37SDimitry Andric       result.AppendErrorWithFormat("no frame with index %u", frame_index);
1073b1c73532SDimitry Andric       return;
107494994d37SDimitry Andric     }
107594994d37SDimitry Andric 
1076b60736ecSDimitry Andric     auto recognizer = GetSelectedOrDummyTarget()
1077b60736ecSDimitry Andric                           .GetFrameRecognizerManager()
1078b60736ecSDimitry Andric                           .GetRecognizerForFrame(frame_sp);
107994994d37SDimitry Andric 
108094994d37SDimitry Andric     Stream &output_stream = result.GetOutputStream();
108194994d37SDimitry Andric     output_stream.Printf("frame %d ", frame_index);
108294994d37SDimitry Andric     if (recognizer) {
108394994d37SDimitry Andric       output_stream << "is recognized by ";
108494994d37SDimitry Andric       output_stream << recognizer->GetName();
108594994d37SDimitry Andric     } else {
108694994d37SDimitry Andric       output_stream << "not recognized by any recognizer";
108794994d37SDimitry Andric     }
108894994d37SDimitry Andric     output_stream.EOL();
108994994d37SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
109094994d37SDimitry Andric   }
109194994d37SDimitry Andric };
109294994d37SDimitry Andric 
109394994d37SDimitry Andric class CommandObjectFrameRecognizer : public CommandObjectMultiword {
109494994d37SDimitry Andric public:
CommandObjectFrameRecognizer(CommandInterpreter & interpreter)109594994d37SDimitry Andric   CommandObjectFrameRecognizer(CommandInterpreter &interpreter)
109694994d37SDimitry Andric       : CommandObjectMultiword(
109794994d37SDimitry Andric             interpreter, "frame recognizer",
109894994d37SDimitry Andric             "Commands for editing and viewing frame recognizers.",
109994994d37SDimitry Andric             "frame recognizer [<sub-command-options>] ") {
1100706b4fc4SDimitry Andric     LoadSubCommand("add", CommandObjectSP(new CommandObjectFrameRecognizerAdd(
1101706b4fc4SDimitry Andric                               interpreter)));
110294994d37SDimitry Andric     LoadSubCommand(
110394994d37SDimitry Andric         "clear",
110494994d37SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerClear(interpreter)));
110594994d37SDimitry Andric     LoadSubCommand(
110694994d37SDimitry Andric         "delete",
110794994d37SDimitry Andric         CommandObjectSP(new CommandObjectFrameRecognizerDelete(interpreter)));
1108706b4fc4SDimitry Andric     LoadSubCommand("list", CommandObjectSP(new CommandObjectFrameRecognizerList(
1109706b4fc4SDimitry Andric                                interpreter)));
1110706b4fc4SDimitry Andric     LoadSubCommand("info", CommandObjectSP(new CommandObjectFrameRecognizerInfo(
1111706b4fc4SDimitry Andric                                interpreter)));
111294994d37SDimitry Andric   }
111394994d37SDimitry Andric 
111494994d37SDimitry Andric   ~CommandObjectFrameRecognizer() override = default;
111594994d37SDimitry Andric };
111694994d37SDimitry Andric 
1117f034231aSEd Maste #pragma mark CommandObjectMultiwordFrame
1118f034231aSEd Maste 
1119f034231aSEd Maste // CommandObjectMultiwordFrame
1120f034231aSEd Maste 
CommandObjectMultiwordFrame(CommandInterpreter & interpreter)112114f1b3e8SDimitry Andric CommandObjectMultiwordFrame::CommandObjectMultiwordFrame(
112214f1b3e8SDimitry Andric     CommandInterpreter &interpreter)
1123706b4fc4SDimitry Andric     : CommandObjectMultiword(interpreter, "frame",
1124706b4fc4SDimitry Andric                              "Commands for selecting and "
112514f1b3e8SDimitry Andric                              "examing the current "
112614f1b3e8SDimitry Andric                              "thread's stack frames.",
112714f1b3e8SDimitry Andric                              "frame <subcommand> [<subcommand-options>]") {
112814f1b3e8SDimitry Andric   LoadSubCommand("diagnose",
112914f1b3e8SDimitry Andric                  CommandObjectSP(new CommandObjectFrameDiagnose(interpreter)));
113014f1b3e8SDimitry Andric   LoadSubCommand("info",
113114f1b3e8SDimitry Andric                  CommandObjectSP(new CommandObjectFrameInfo(interpreter)));
113214f1b3e8SDimitry Andric   LoadSubCommand("select",
113314f1b3e8SDimitry Andric                  CommandObjectSP(new CommandObjectFrameSelect(interpreter)));
113414f1b3e8SDimitry Andric   LoadSubCommand("variable",
113514f1b3e8SDimitry Andric                  CommandObjectSP(new CommandObjectFrameVariable(interpreter)));
1136706b4fc4SDimitry Andric #if LLDB_ENABLE_PYTHON
1137706b4fc4SDimitry Andric   LoadSubCommand("recognizer", CommandObjectSP(new CommandObjectFrameRecognizer(
1138706b4fc4SDimitry Andric                                    interpreter)));
113994994d37SDimitry Andric #endif
1140f034231aSEd Maste }
1141f034231aSEd Maste 
1142f3fbd1c0SDimitry Andric CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame() = default;
1143