15ffd83dbSDimitry Andric //===-- CommandObjectBreakpointCommand.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
90b57cec5SDimitry Andric #include "CommandObjectBreakpointCommand.h"
100b57cec5SDimitry Andric #include "CommandObjectBreakpoint.h"
110b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
120b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointIDList.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
140b57cec5SDimitry Andric #include "lldb/Core/IOHandler.h"
150b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
160b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
17fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
180b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
190b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
20480093f4SDimitry Andric #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
210b57cec5SDimitry Andric #include "lldb/Target/Target.h"
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric using namespace lldb;
240b57cec5SDimitry Andric using namespace lldb_private;
250b57cec5SDimitry Andric
269dba64beSDimitry Andric #define LLDB_OPTIONS_breakpoint_command_add
279dba64beSDimitry Andric #include "CommandOptions.inc"
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric class CommandObjectBreakpointCommandAdd : public CommandObjectParsed,
300b57cec5SDimitry Andric public IOHandlerDelegateMultiline {
310b57cec5SDimitry Andric public:
CommandObjectBreakpointCommandAdd(CommandInterpreter & interpreter)320b57cec5SDimitry Andric CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter)
330b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "add",
340b57cec5SDimitry Andric "Add LLDB commands to a breakpoint, to be executed "
350b57cec5SDimitry Andric "whenever the breakpoint is hit. "
36fe6060f1SDimitry Andric "The commands added to the breakpoint replace any "
37fe6060f1SDimitry Andric "commands previously added to it."
380b57cec5SDimitry Andric " If no breakpoint is specified, adds the "
390b57cec5SDimitry Andric "commands to the last created breakpoint.",
400b57cec5SDimitry Andric nullptr),
410b57cec5SDimitry Andric IOHandlerDelegateMultiline("DONE",
420b57cec5SDimitry Andric IOHandlerDelegate::Completion::LLDBCommand),
4304eeddc0SDimitry Andric m_func_options("breakpoint command", false, 'F') {
440b57cec5SDimitry Andric SetHelpLong(
450b57cec5SDimitry Andric R"(
460b57cec5SDimitry Andric General information about entering breakpoint commands
470b57cec5SDimitry Andric ------------------------------------------------------
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric )"
500b57cec5SDimitry Andric "This command will prompt for commands to be executed when the specified \
510b57cec5SDimitry Andric breakpoint is hit. Each command is typed on its own line following the '> ' \
520b57cec5SDimitry Andric prompt until 'DONE' is entered."
530b57cec5SDimitry Andric R"(
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric )"
560b57cec5SDimitry Andric "Syntactic errors may not be detected when initially entered, and many \
570b57cec5SDimitry Andric malformed commands can silently fail when executed. If your breakpoint commands \
580b57cec5SDimitry Andric do not appear to be executing, double-check the command syntax."
590b57cec5SDimitry Andric R"(
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric )"
620b57cec5SDimitry Andric "Note: You may enter any debugger command exactly as you would at the debugger \
630b57cec5SDimitry Andric prompt. There is no limit to the number of commands supplied, but do NOT enter \
640b57cec5SDimitry Andric more than one command per line."
650b57cec5SDimitry Andric R"(
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric Special information about PYTHON breakpoint commands
680b57cec5SDimitry Andric ----------------------------------------------------
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric )"
710b57cec5SDimitry Andric "You may enter either one or more lines of Python, including function \
720b57cec5SDimitry Andric definitions or calls to functions that will have been imported by the time \
730b57cec5SDimitry Andric the code executes. Single line breakpoint commands will be interpreted 'as is' \
740b57cec5SDimitry Andric when the breakpoint is hit. Multiple lines of Python will be wrapped in a \
750b57cec5SDimitry Andric generated function, and a call to the function will be attached to the breakpoint."
760b57cec5SDimitry Andric R"(
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric This auto-generated function is passed in three arguments:
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric frame: an lldb.SBFrame object for the frame which hit breakpoint.
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit.
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric dict: the python session dictionary hit.
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric )"
870b57cec5SDimitry Andric "When specifying a python function with the --python-function option, you need \
880b57cec5SDimitry Andric to supply the function name prepended by the module name:"
890b57cec5SDimitry Andric R"(
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric --python-function myutils.breakpoint_callback
920b57cec5SDimitry Andric
93fe6060f1SDimitry Andric The function itself must have either of the following prototypes:
940b57cec5SDimitry Andric
95fe6060f1SDimitry Andric def breakpoint_callback(frame, bp_loc, internal_dict):
96fe6060f1SDimitry Andric # Your code goes here
97fe6060f1SDimitry Andric
98fe6060f1SDimitry Andric or:
99fe6060f1SDimitry Andric
100fe6060f1SDimitry Andric def breakpoint_callback(frame, bp_loc, extra_args, internal_dict):
1010b57cec5SDimitry Andric # Your code goes here
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric )"
1040b57cec5SDimitry Andric "The arguments are the same as the arguments passed to generated functions as \
105fe6060f1SDimitry Andric described above. In the second form, any -k and -v pairs provided to the command will \
106fe6060f1SDimitry Andric be packaged into a SBDictionary in an SBStructuredData and passed as the extra_args parameter. \
107fe6060f1SDimitry Andric \n\n\
108fe6060f1SDimitry Andric Note that the global variable 'lldb.frame' will NOT be updated when \
1090b57cec5SDimitry Andric this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
1100b57cec5SDimitry Andric can get you to the thread via frame.GetThread(), the thread can get you to the \
1110b57cec5SDimitry Andric process via thread.GetProcess(), and the process can get you back to the target \
1120b57cec5SDimitry Andric via process.GetTarget()."
1130b57cec5SDimitry Andric R"(
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric )"
1160b57cec5SDimitry Andric "Important Note: As Python code gets collected into functions, access to global \
1170b57cec5SDimitry Andric variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
1180b57cec5SDimitry Andric Python syntax, including indentation, when entering Python breakpoint commands."
1190b57cec5SDimitry Andric R"(
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric Example Python one-line breakpoint command:
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric (lldb) breakpoint command add -s python 1
1240b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end.
125e8d8bef9SDimitry Andric def function (frame, bp_loc, internal_dict):
126e8d8bef9SDimitry Andric """frame: the lldb.SBFrame for the location at which you stopped
127e8d8bef9SDimitry Andric bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
128e8d8bef9SDimitry Andric internal_dict: an LLDB support object not to be used"""
129e8d8bef9SDimitry Andric print("Hit this breakpoint!")
130e8d8bef9SDimitry Andric DONE
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric As a convenience, this also works for a short Python one-liner:
1330b57cec5SDimitry Andric
134e8d8bef9SDimitry Andric (lldb) breakpoint command add -s python 1 -o 'import time; print(time.asctime())'
1350b57cec5SDimitry Andric (lldb) run
1360b57cec5SDimitry Andric Launching '.../a.out' (x86_64)
1370b57cec5SDimitry Andric (lldb) Fri Sep 10 12:17:45 2010
1380b57cec5SDimitry Andric Process 21778 Stopped
1390b57cec5SDimitry Andric * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread
1400b57cec5SDimitry Andric 36
1410b57cec5SDimitry Andric 37 int c(int val)
1420b57cec5SDimitry Andric 38 {
1430b57cec5SDimitry Andric 39 -> return val + 3;
1440b57cec5SDimitry Andric 40 }
1450b57cec5SDimitry Andric 41
1460b57cec5SDimitry Andric 42 int main (int argc, char const *argv[])
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric Example multiple line Python breakpoint command:
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric (lldb) breakpoint command add -s p 1
1510b57cec5SDimitry Andric Enter your Python command(s). Type 'DONE' to end.
152e8d8bef9SDimitry Andric def function (frame, bp_loc, internal_dict):
153e8d8bef9SDimitry Andric """frame: the lldb.SBFrame for the location at which you stopped
154e8d8bef9SDimitry Andric bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
155e8d8bef9SDimitry Andric internal_dict: an LLDB support object not to be used"""
156e8d8bef9SDimitry Andric global bp_count
157e8d8bef9SDimitry Andric bp_count = bp_count + 1
158e8d8bef9SDimitry Andric print("Hit this breakpoint " + repr(bp_count) + " times!")
159e8d8bef9SDimitry Andric DONE
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric )"
1620b57cec5SDimitry Andric "In this case, since there is a reference to a global variable, \
1630b57cec5SDimitry Andric 'bp_count', you will also need to make sure 'bp_count' exists and is \
1640b57cec5SDimitry Andric initialized:"
1650b57cec5SDimitry Andric R"(
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric (lldb) script
1680b57cec5SDimitry Andric >>> bp_count = 0
1690b57cec5SDimitry Andric >>> quit()
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric )"
1720b57cec5SDimitry Andric "Your Python code, however organized, can optionally return a value. \
1730b57cec5SDimitry Andric If the returned value is False, that tells LLDB not to stop at the breakpoint \
1740b57cec5SDimitry Andric to which the code is associated. Returning anything other than False, or even \
1750b57cec5SDimitry Andric returning None, or even omitting a return statement entirely, will cause \
1760b57cec5SDimitry Andric LLDB to stop."
1770b57cec5SDimitry Andric R"(
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric )"
1800b57cec5SDimitry Andric "Final Note: A warning that no breakpoint command was generated when there \
1810b57cec5SDimitry Andric are no syntax errors may indicate that a function was declared but never called.");
1820b57cec5SDimitry Andric
183480093f4SDimitry Andric m_all_options.Append(&m_options);
184480093f4SDimitry Andric m_all_options.Append(&m_func_options, LLDB_OPT_SET_2 | LLDB_OPT_SET_3,
185480093f4SDimitry Andric LLDB_OPT_SET_2);
186480093f4SDimitry Andric m_all_options.Finalize();
187480093f4SDimitry Andric
1880b57cec5SDimitry Andric CommandArgumentEntry arg;
1890b57cec5SDimitry Andric CommandArgumentData bp_id_arg;
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric // Define the first (and only) variant of this arg.
1920b57cec5SDimitry Andric bp_id_arg.arg_type = eArgTypeBreakpointID;
1930b57cec5SDimitry Andric bp_id_arg.arg_repetition = eArgRepeatOptional;
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the
1960b57cec5SDimitry Andric // argument entry.
1970b57cec5SDimitry Andric arg.push_back(bp_id_arg);
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector.
2000b57cec5SDimitry Andric m_arguments.push_back(arg);
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric ~CommandObjectBreakpointCommandAdd() override = default;
2040b57cec5SDimitry Andric
GetOptions()205480093f4SDimitry Andric Options *GetOptions() override { return &m_all_options; }
2060b57cec5SDimitry Andric
IOHandlerActivated(IOHandler & io_handler,bool interactive)2070b57cec5SDimitry Andric void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
2089dba64beSDimitry Andric StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
2090b57cec5SDimitry Andric if (output_sp && interactive) {
2100b57cec5SDimitry Andric output_sp->PutCString(g_reader_instructions);
2110b57cec5SDimitry Andric output_sp->Flush();
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
IOHandlerInputComplete(IOHandler & io_handler,std::string & line)2150b57cec5SDimitry Andric void IOHandlerInputComplete(IOHandler &io_handler,
2160b57cec5SDimitry Andric std::string &line) override {
2170b57cec5SDimitry Andric io_handler.SetIsDone(true);
2180b57cec5SDimitry Andric
219fe6060f1SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>> *bp_options_vec =
220fe6060f1SDimitry Andric (std::vector<std::reference_wrapper<BreakpointOptions>> *)
221fe6060f1SDimitry Andric io_handler.GetUserData();
222fe6060f1SDimitry Andric for (BreakpointOptions &bp_options : *bp_options_vec) {
2239dba64beSDimitry Andric auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
2240b57cec5SDimitry Andric cmd_data->user_source.SplitIntoLines(line.c_str(), line.size());
225fe6060f1SDimitry Andric bp_options.SetCommandDataCallback(cmd_data);
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
CollectDataForBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,CommandReturnObject & result)2290b57cec5SDimitry Andric void CollectDataForBreakpointCommandCallback(
230fe6060f1SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
2310b57cec5SDimitry Andric CommandReturnObject &result) {
2320b57cec5SDimitry Andric m_interpreter.GetLLDBCommandsFromIOHandler(
2330b57cec5SDimitry Andric "> ", // Prompt
2340b57cec5SDimitry Andric *this, // IOHandlerDelegate
2350b57cec5SDimitry Andric &bp_options_vec); // Baton for the "io_handler" that will be passed back
2360b57cec5SDimitry Andric // into our IOHandlerDelegate functions
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric /// Set a one-liner as the callback for the breakpoint.
SetBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,const char * oneliner)240fe6060f1SDimitry Andric void SetBreakpointCommandCallback(
241fe6060f1SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
2420b57cec5SDimitry Andric const char *oneliner) {
243fe6060f1SDimitry Andric for (BreakpointOptions &bp_options : bp_options_vec) {
2449dba64beSDimitry Andric auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric cmd_data->user_source.AppendString(oneliner);
2470b57cec5SDimitry Andric cmd_data->stop_on_error = m_options.m_stop_on_error;
2480b57cec5SDimitry Andric
249fe6060f1SDimitry Andric bp_options.SetCommandDataCallback(cmd_data);
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
253480093f4SDimitry Andric class CommandOptions : public OptionGroup {
2540b57cec5SDimitry Andric public:
25581ad6265SDimitry Andric CommandOptions() = default;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric ~CommandOptions() override = default;
2580b57cec5SDimitry Andric
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)2590b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2600b57cec5SDimitry Andric ExecutionContext *execution_context) override {
2610b57cec5SDimitry Andric Status error;
262480093f4SDimitry Andric const int short_option =
263480093f4SDimitry Andric g_breakpoint_command_add_options[option_idx].short_option;
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric switch (short_option) {
2660b57cec5SDimitry Andric case 'o':
2670b57cec5SDimitry Andric m_use_one_liner = true;
2685ffd83dbSDimitry Andric m_one_liner = std::string(option_arg);
2690b57cec5SDimitry Andric break;
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andric case 's':
2720b57cec5SDimitry Andric m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
2739dba64beSDimitry Andric option_arg,
2749dba64beSDimitry Andric g_breakpoint_command_add_options[option_idx].enum_values,
2750b57cec5SDimitry Andric eScriptLanguageNone, error);
276480093f4SDimitry Andric switch (m_script_language) {
277480093f4SDimitry Andric case eScriptLanguagePython:
278480093f4SDimitry Andric case eScriptLanguageLua:
2790b57cec5SDimitry Andric m_use_script_language = true;
280480093f4SDimitry Andric break;
281480093f4SDimitry Andric case eScriptLanguageNone:
282480093f4SDimitry Andric case eScriptLanguageUnknown:
2830b57cec5SDimitry Andric m_use_script_language = false;
284480093f4SDimitry Andric break;
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric break;
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric case 'e': {
2890b57cec5SDimitry Andric bool success = false;
2900b57cec5SDimitry Andric m_stop_on_error =
2910b57cec5SDimitry Andric OptionArgParser::ToBoolean(option_arg, false, &success);
2920b57cec5SDimitry Andric if (!success)
2930b57cec5SDimitry Andric error.SetErrorStringWithFormat(
2940b57cec5SDimitry Andric "invalid value for stop-on-error: \"%s\"",
2950b57cec5SDimitry Andric option_arg.str().c_str());
2960b57cec5SDimitry Andric } break;
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric case 'D':
2990b57cec5SDimitry Andric m_use_dummy = true;
3000b57cec5SDimitry Andric break;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric default:
3039dba64beSDimitry Andric llvm_unreachable("Unimplemented option");
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric return error;
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric
OptionParsingStarting(ExecutionContext * execution_context)3080b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override {
3090b57cec5SDimitry Andric m_use_commands = true;
3100b57cec5SDimitry Andric m_use_script_language = false;
3110b57cec5SDimitry Andric m_script_language = eScriptLanguageNone;
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric m_use_one_liner = false;
3140b57cec5SDimitry Andric m_stop_on_error = true;
3150b57cec5SDimitry Andric m_one_liner.clear();
3160b57cec5SDimitry Andric m_use_dummy = false;
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric
GetDefinitions()3190b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
320bdd1243dSDimitry Andric return llvm::ArrayRef(g_breakpoint_command_add_options);
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric // Instance variables to hold the values for command options.
3240b57cec5SDimitry Andric
325fe6060f1SDimitry Andric bool m_use_commands = false;
326fe6060f1SDimitry Andric bool m_use_script_language = false;
327fe6060f1SDimitry Andric lldb::ScriptLanguage m_script_language = eScriptLanguageNone;
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric // Instance variables to hold the values for one_liner options.
330fe6060f1SDimitry Andric bool m_use_one_liner = false;
3310b57cec5SDimitry Andric std::string m_one_liner;
3320b57cec5SDimitry Andric bool m_stop_on_error;
3330b57cec5SDimitry Andric bool m_use_dummy;
3340b57cec5SDimitry Andric };
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)337*c9157d92SDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override {
3389dba64beSDimitry Andric Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
3390b57cec5SDimitry Andric
3409dba64beSDimitry Andric const BreakpointList &breakpoints = target.GetBreakpointList();
3410b57cec5SDimitry Andric size_t num_breakpoints = breakpoints.GetSize();
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric if (num_breakpoints == 0) {
3440b57cec5SDimitry Andric result.AppendError("No breakpoints exist to have commands added");
345*c9157d92SDimitry Andric return;
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
348480093f4SDimitry Andric if (!m_func_options.GetName().empty()) {
349480093f4SDimitry Andric m_options.m_use_one_liner = false;
350480093f4SDimitry Andric if (!m_options.m_use_script_language) {
351480093f4SDimitry Andric m_options.m_script_language = GetDebugger().GetScriptLanguage();
352480093f4SDimitry Andric m_options.m_use_script_language = true;
353480093f4SDimitry Andric }
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric BreakpointIDList valid_bp_ids;
3570b57cec5SDimitry Andric CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
3589dba64beSDimitry Andric command, &target, result, &valid_bp_ids,
3590b57cec5SDimitry Andric BreakpointName::Permissions::PermissionKinds::listPerm);
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric m_bp_options_vec.clear();
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric if (result.Succeeded()) {
3640b57cec5SDimitry Andric const size_t count = valid_bp_ids.GetSize();
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) {
3670b57cec5SDimitry Andric BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
3680b57cec5SDimitry Andric if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
3690b57cec5SDimitry Andric Breakpoint *bp =
3709dba64beSDimitry Andric target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
3710b57cec5SDimitry Andric if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) {
3720b57cec5SDimitry Andric // This breakpoint does not have an associated location.
373fe6060f1SDimitry Andric m_bp_options_vec.push_back(bp->GetOptions());
3740b57cec5SDimitry Andric } else {
3750b57cec5SDimitry Andric BreakpointLocationSP bp_loc_sp(
3760b57cec5SDimitry Andric bp->FindLocationByID(cur_bp_id.GetLocationID()));
3770b57cec5SDimitry Andric // This breakpoint does have an associated location. Get its
3780b57cec5SDimitry Andric // breakpoint options.
3790b57cec5SDimitry Andric if (bp_loc_sp)
380fe6060f1SDimitry Andric m_bp_options_vec.push_back(bp_loc_sp->GetLocationOptions());
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric // If we are using script language, get the script interpreter in order
3860b57cec5SDimitry Andric // to set or collect command callback. Otherwise, call the methods
3870b57cec5SDimitry Andric // associated with this object.
3880b57cec5SDimitry Andric if (m_options.m_use_script_language) {
389e8d8bef9SDimitry Andric Status error;
390480093f4SDimitry Andric ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(
391480093f4SDimitry Andric /*can_create=*/true, m_options.m_script_language);
3920b57cec5SDimitry Andric // Special handling for one-liner specified inline.
3930b57cec5SDimitry Andric if (m_options.m_use_one_liner) {
394e8d8bef9SDimitry Andric error = script_interp->SetBreakpointCommandCallback(
3950b57cec5SDimitry Andric m_bp_options_vec, m_options.m_one_liner.c_str());
396480093f4SDimitry Andric } else if (!m_func_options.GetName().empty()) {
397e8d8bef9SDimitry Andric error = script_interp->SetBreakpointCommandCallbackFunction(
398480093f4SDimitry Andric m_bp_options_vec, m_func_options.GetName().c_str(),
399480093f4SDimitry Andric m_func_options.GetStructuredData());
4000b57cec5SDimitry Andric } else {
4010b57cec5SDimitry Andric script_interp->CollectDataForBreakpointCommandCallback(
4020b57cec5SDimitry Andric m_bp_options_vec, result);
4030b57cec5SDimitry Andric }
404e8d8bef9SDimitry Andric if (!error.Success())
405e8d8bef9SDimitry Andric result.SetError(error);
4060b57cec5SDimitry Andric } else {
4070b57cec5SDimitry Andric // Special handling for one-liner specified inline.
4080b57cec5SDimitry Andric if (m_options.m_use_one_liner)
4090b57cec5SDimitry Andric SetBreakpointCommandCallback(m_bp_options_vec,
4100b57cec5SDimitry Andric m_options.m_one_liner.c_str());
4110b57cec5SDimitry Andric else
4120b57cec5SDimitry Andric CollectDataForBreakpointCommandCallback(m_bp_options_vec, result);
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andric private:
4180b57cec5SDimitry Andric CommandOptions m_options;
419480093f4SDimitry Andric OptionGroupPythonClassWithDict m_func_options;
420480093f4SDimitry Andric OptionGroupOptions m_all_options;
421480093f4SDimitry Andric
422fe6060f1SDimitry Andric std::vector<std::reference_wrapper<BreakpointOptions>>
423fe6060f1SDimitry Andric m_bp_options_vec; // This stores the
4240b57cec5SDimitry Andric // breakpoint options that
4250b57cec5SDimitry Andric // we are currently
4260b57cec5SDimitry Andric // collecting commands for. In the CollectData... calls we need to hand this
4270b57cec5SDimitry Andric // off to the IOHandler, which may run asynchronously. So we have to have
4280b57cec5SDimitry Andric // some way to keep it alive, and not leak it. Making it an ivar of the
4290b57cec5SDimitry Andric // command object, which never goes away achieves this. Note that if we were
4300b57cec5SDimitry Andric // able to run the same command concurrently in one interpreter we'd have to
4310b57cec5SDimitry Andric // make this "per invocation". But there are many more reasons why it is not
4320b57cec5SDimitry Andric // in general safe to do that in lldb at present, so it isn't worthwhile to
4330b57cec5SDimitry Andric // come up with a more complex mechanism to address this particular weakness
4340b57cec5SDimitry Andric // right now.
4350b57cec5SDimitry Andric static const char *g_reader_instructions;
4360b57cec5SDimitry Andric };
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric const char *CommandObjectBreakpointCommandAdd::g_reader_instructions =
4390b57cec5SDimitry Andric "Enter your debugger command(s). Type 'DONE' to end.\n";
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric // CommandObjectBreakpointCommandDelete
4420b57cec5SDimitry Andric
4439dba64beSDimitry Andric #define LLDB_OPTIONS_breakpoint_command_delete
4449dba64beSDimitry Andric #include "CommandOptions.inc"
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric class CommandObjectBreakpointCommandDelete : public CommandObjectParsed {
4470b57cec5SDimitry Andric public:
CommandObjectBreakpointCommandDelete(CommandInterpreter & interpreter)4480b57cec5SDimitry Andric CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter)
4490b57cec5SDimitry Andric : CommandObjectParsed(interpreter, "delete",
4500b57cec5SDimitry Andric "Delete the set of commands from a breakpoint.",
45104eeddc0SDimitry Andric nullptr) {
4520b57cec5SDimitry Andric CommandArgumentEntry arg;
4530b57cec5SDimitry Andric CommandArgumentData bp_id_arg;
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric // Define the first (and only) variant of this arg.
4560b57cec5SDimitry Andric bp_id_arg.arg_type = eArgTypeBreakpointID;
4570b57cec5SDimitry Andric bp_id_arg.arg_repetition = eArgRepeatPlain;
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the
4600b57cec5SDimitry Andric // argument entry.
4610b57cec5SDimitry Andric arg.push_back(bp_id_arg);
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector.
4640b57cec5SDimitry Andric m_arguments.push_back(arg);
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric
4670b57cec5SDimitry Andric ~CommandObjectBreakpointCommandDelete() override = default;
4680b57cec5SDimitry Andric
GetOptions()4690b57cec5SDimitry Andric Options *GetOptions() override { return &m_options; }
4700b57cec5SDimitry Andric
4710b57cec5SDimitry Andric class CommandOptions : public Options {
4720b57cec5SDimitry Andric public:
47381ad6265SDimitry Andric CommandOptions() = default;
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric ~CommandOptions() override = default;
4760b57cec5SDimitry Andric
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)4770b57cec5SDimitry Andric Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
4780b57cec5SDimitry Andric ExecutionContext *execution_context) override {
4790b57cec5SDimitry Andric Status error;
4800b57cec5SDimitry Andric const int short_option = m_getopt_table[option_idx].val;
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric switch (short_option) {
4830b57cec5SDimitry Andric case 'D':
4840b57cec5SDimitry Andric m_use_dummy = true;
4850b57cec5SDimitry Andric break;
4860b57cec5SDimitry Andric
4870b57cec5SDimitry Andric default:
4889dba64beSDimitry Andric llvm_unreachable("Unimplemented option");
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric
4910b57cec5SDimitry Andric return error;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric
OptionParsingStarting(ExecutionContext * execution_context)4940b57cec5SDimitry Andric void OptionParsingStarting(ExecutionContext *execution_context) override {
4950b57cec5SDimitry Andric m_use_dummy = false;
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric
GetDefinitions()4980b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
499bdd1243dSDimitry Andric return llvm::ArrayRef(g_breakpoint_command_delete_options);
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric
5020b57cec5SDimitry Andric // Instance variables to hold the values for command options.
503fe6060f1SDimitry Andric bool m_use_dummy = false;
5040b57cec5SDimitry Andric };
5050b57cec5SDimitry Andric
5060b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)507*c9157d92SDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override {
5089dba64beSDimitry Andric Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
5090b57cec5SDimitry Andric
5109dba64beSDimitry Andric const BreakpointList &breakpoints = target.GetBreakpointList();
5110b57cec5SDimitry Andric size_t num_breakpoints = breakpoints.GetSize();
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric if (num_breakpoints == 0) {
5140b57cec5SDimitry Andric result.AppendError("No breakpoints exist to have commands deleted");
515*c9157d92SDimitry Andric return;
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric if (command.empty()) {
5190b57cec5SDimitry Andric result.AppendError(
5200b57cec5SDimitry Andric "No breakpoint specified from which to delete the commands");
521*c9157d92SDimitry Andric return;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andric BreakpointIDList valid_bp_ids;
5250b57cec5SDimitry Andric CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
5269dba64beSDimitry Andric command, &target, result, &valid_bp_ids,
5270b57cec5SDimitry Andric BreakpointName::Permissions::PermissionKinds::listPerm);
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric if (result.Succeeded()) {
5300b57cec5SDimitry Andric const size_t count = valid_bp_ids.GetSize();
5310b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) {
5320b57cec5SDimitry Andric BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
5330b57cec5SDimitry Andric if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
5340b57cec5SDimitry Andric Breakpoint *bp =
5359dba64beSDimitry Andric target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
5360b57cec5SDimitry Andric if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
5370b57cec5SDimitry Andric BreakpointLocationSP bp_loc_sp(
5380b57cec5SDimitry Andric bp->FindLocationByID(cur_bp_id.GetLocationID()));
5390b57cec5SDimitry Andric if (bp_loc_sp)
5400b57cec5SDimitry Andric bp_loc_sp->ClearCallback();
5410b57cec5SDimitry Andric else {
5420b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
5430b57cec5SDimitry Andric cur_bp_id.GetBreakpointID(),
5440b57cec5SDimitry Andric cur_bp_id.GetLocationID());
545*c9157d92SDimitry Andric return;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric } else {
5480b57cec5SDimitry Andric bp->ClearCallback();
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric private:
5560b57cec5SDimitry Andric CommandOptions m_options;
5570b57cec5SDimitry Andric };
5580b57cec5SDimitry Andric
5590b57cec5SDimitry Andric // CommandObjectBreakpointCommandList
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric class CommandObjectBreakpointCommandList : public CommandObjectParsed {
5620b57cec5SDimitry Andric public:
CommandObjectBreakpointCommandList(CommandInterpreter & interpreter)5630b57cec5SDimitry Andric CommandObjectBreakpointCommandList(CommandInterpreter &interpreter)
5649dba64beSDimitry Andric : CommandObjectParsed(interpreter, "list",
5659dba64beSDimitry Andric "List the script or set of commands to be "
5669dba64beSDimitry Andric "executed when the breakpoint is hit.",
5679dba64beSDimitry Andric nullptr, eCommandRequiresTarget) {
5680b57cec5SDimitry Andric CommandArgumentEntry arg;
5690b57cec5SDimitry Andric CommandArgumentData bp_id_arg;
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric // Define the first (and only) variant of this arg.
5720b57cec5SDimitry Andric bp_id_arg.arg_type = eArgTypeBreakpointID;
5730b57cec5SDimitry Andric bp_id_arg.arg_repetition = eArgRepeatPlain;
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andric // There is only one variant this argument could be; put it into the
5760b57cec5SDimitry Andric // argument entry.
5770b57cec5SDimitry Andric arg.push_back(bp_id_arg);
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric // Push the data for the first argument into the m_arguments vector.
5800b57cec5SDimitry Andric m_arguments.push_back(arg);
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric ~CommandObjectBreakpointCommandList() override = default;
5840b57cec5SDimitry Andric
5850b57cec5SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)586*c9157d92SDimitry Andric void DoExecute(Args &command, CommandReturnObject &result) override {
5879dba64beSDimitry Andric Target *target = &GetSelectedTarget();
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric const BreakpointList &breakpoints = target->GetBreakpointList();
5900b57cec5SDimitry Andric size_t num_breakpoints = breakpoints.GetSize();
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric if (num_breakpoints == 0) {
5930b57cec5SDimitry Andric result.AppendError("No breakpoints exist for which to list commands");
594*c9157d92SDimitry Andric return;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric if (command.empty()) {
5980b57cec5SDimitry Andric result.AppendError(
5990b57cec5SDimitry Andric "No breakpoint specified for which to list the commands");
600*c9157d92SDimitry Andric return;
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric BreakpointIDList valid_bp_ids;
6040b57cec5SDimitry Andric CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
6050b57cec5SDimitry Andric command, target, result, &valid_bp_ids,
6060b57cec5SDimitry Andric BreakpointName::Permissions::PermissionKinds::listPerm);
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric if (result.Succeeded()) {
6090b57cec5SDimitry Andric const size_t count = valid_bp_ids.GetSize();
6100b57cec5SDimitry Andric for (size_t i = 0; i < count; ++i) {
6110b57cec5SDimitry Andric BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
6120b57cec5SDimitry Andric if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
6130b57cec5SDimitry Andric Breakpoint *bp =
6140b57cec5SDimitry Andric target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
6150b57cec5SDimitry Andric
6160b57cec5SDimitry Andric if (bp) {
6170b57cec5SDimitry Andric BreakpointLocationSP bp_loc_sp;
6180b57cec5SDimitry Andric if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
6190b57cec5SDimitry Andric bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID());
620480093f4SDimitry Andric if (!bp_loc_sp) {
6210b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n",
6220b57cec5SDimitry Andric cur_bp_id.GetBreakpointID(),
6230b57cec5SDimitry Andric cur_bp_id.GetLocationID());
624*c9157d92SDimitry Andric return;
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric StreamString id_str;
6290b57cec5SDimitry Andric BreakpointID::GetCanonicalReference(&id_str,
6300b57cec5SDimitry Andric cur_bp_id.GetBreakpointID(),
6310b57cec5SDimitry Andric cur_bp_id.GetLocationID());
6320b57cec5SDimitry Andric const Baton *baton = nullptr;
6330b57cec5SDimitry Andric if (bp_loc_sp)
634480093f4SDimitry Andric baton =
635480093f4SDimitry Andric bp_loc_sp
6360b57cec5SDimitry Andric ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback)
637fe6060f1SDimitry Andric .GetBaton();
6380b57cec5SDimitry Andric else
639fe6060f1SDimitry Andric baton = bp->GetOptions().GetBaton();
6400b57cec5SDimitry Andric
6410b57cec5SDimitry Andric if (baton) {
6420b57cec5SDimitry Andric result.GetOutputStream().Printf("Breakpoint %s:\n",
6430b57cec5SDimitry Andric id_str.GetData());
644480093f4SDimitry Andric baton->GetDescription(result.GetOutputStream().AsRawOstream(),
645480093f4SDimitry Andric eDescriptionLevelFull,
646480093f4SDimitry Andric result.GetOutputStream().GetIndentLevel() +
647480093f4SDimitry Andric 2);
6480b57cec5SDimitry Andric } else {
6490b57cec5SDimitry Andric result.AppendMessageWithFormat(
6500b57cec5SDimitry Andric "Breakpoint %s does not have an associated command.\n",
6510b57cec5SDimitry Andric id_str.GetData());
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult);
6550b57cec5SDimitry Andric } else {
6560b57cec5SDimitry Andric result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n",
6570b57cec5SDimitry Andric cur_bp_id.GetBreakpointID());
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric }
6620b57cec5SDimitry Andric };
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric // CommandObjectBreakpointCommand
6650b57cec5SDimitry Andric
CommandObjectBreakpointCommand(CommandInterpreter & interpreter)6660b57cec5SDimitry Andric CommandObjectBreakpointCommand::CommandObjectBreakpointCommand(
6670b57cec5SDimitry Andric CommandInterpreter &interpreter)
6680b57cec5SDimitry Andric : CommandObjectMultiword(
669480093f4SDimitry Andric interpreter, "command",
670480093f4SDimitry Andric "Commands for adding, removing and listing "
6710b57cec5SDimitry Andric "LLDB commands executed when a breakpoint is "
6720b57cec5SDimitry Andric "hit.",
6730b57cec5SDimitry Andric "command <sub-command> [<sub-command-options>] <breakpoint-id>") {
6740b57cec5SDimitry Andric CommandObjectSP add_command_object(
6750b57cec5SDimitry Andric new CommandObjectBreakpointCommandAdd(interpreter));
6760b57cec5SDimitry Andric CommandObjectSP delete_command_object(
6770b57cec5SDimitry Andric new CommandObjectBreakpointCommandDelete(interpreter));
6780b57cec5SDimitry Andric CommandObjectSP list_command_object(
6790b57cec5SDimitry Andric new CommandObjectBreakpointCommandList(interpreter));
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric add_command_object->SetCommandName("breakpoint command add");
6820b57cec5SDimitry Andric delete_command_object->SetCommandName("breakpoint command delete");
6830b57cec5SDimitry Andric list_command_object->SetCommandName("breakpoint command list");
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric LoadSubCommand("add", add_command_object);
6860b57cec5SDimitry Andric LoadSubCommand("delete", delete_command_object);
6870b57cec5SDimitry Andric LoadSubCommand("list", list_command_object);
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric
6900b57cec5SDimitry Andric CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default;
691