180814287SRaphael Isemann //===-- CommandObjectBreakpointCommand.cpp --------------------------------===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 930fdc8d8SChris Lattner #include "CommandObjectBreakpointCommand.h" 1030fdc8d8SChris Lattner #include "CommandObjectBreakpoint.h" 11b9c1b51eSKate Stone #include "lldb/Breakpoint/Breakpoint.h" 12b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointIDList.h" 13b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointLocation.h" 1444d93782SGreg Clayton #include "lldb/Core/IOHandler.h" 153eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 1630fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 1847cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 19738af7a6SJim Ingham #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 2030fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2130fdc8d8SChris Lattner 2230fdc8d8SChris Lattner using namespace lldb; 2330fdc8d8SChris Lattner using namespace lldb_private; 2430fdc8d8SChris Lattner 251f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 26e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 27e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 288fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 29e063ecccSJonas Devlieghere { 30e063ecccSJonas Devlieghere eScriptLanguageNone, 31e063ecccSJonas Devlieghere "command", 32e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 33e063ecccSJonas Devlieghere }, 34e063ecccSJonas Devlieghere { 35e063ecccSJonas Devlieghere eScriptLanguagePython, 36e063ecccSJonas Devlieghere "python", 37e063ecccSJonas Devlieghere "Commands are in the Python language.", 38e063ecccSJonas Devlieghere }, 39e063ecccSJonas Devlieghere { 408983d691SJonas Devlieghere eScriptLanguageLua, 418983d691SJonas Devlieghere "lua", 428983d691SJonas Devlieghere "Commands are in the Lua language.", 438983d691SJonas Devlieghere }, 448983d691SJonas Devlieghere { 458983d691SJonas Devlieghere eScriptLanguageDefault, 46e063ecccSJonas Devlieghere "default-script", 47e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 48e063ecccSJonas Devlieghere }, 49e063ecccSJonas Devlieghere }; 501f0f5b5bSZachary Turner 518fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 528fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 538fe53c49STatyana Krasnukha } 548fe53c49STatyana Krasnukha 55f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 56f94668e3SRaphael Isemann #include "CommandOptions.inc" 571f0f5b5bSZachary Turner 58b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 59b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 605a988416SJim Ingham public: 617428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 627428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 63b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 64b9c1b51eSKate Stone "whenever the breakpoint is hit." 65b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 66b9c1b51eSKate Stone "commands to the last created breakpoint.", 67c8ecc2a9SEugene Zelenko nullptr), 68b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 69b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 70738af7a6SJim Ingham m_options(), m_func_options("breakpoint command", false, 'F') { 7130fdc8d8SChris Lattner SetHelpLong( 72ea671fbdSKate Stone R"( 73ea671fbdSKate Stone General information about entering breakpoint commands 74ea671fbdSKate Stone ------------------------------------------------------ 75ea671fbdSKate Stone 76b9c1b51eSKate Stone )" 77b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 78ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 79b9c1b51eSKate Stone prompt until 'DONE' is entered." 80b9c1b51eSKate Stone R"( 81ea671fbdSKate Stone 82b9c1b51eSKate Stone )" 83b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 84ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 85b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 86b9c1b51eSKate Stone R"( 87ea671fbdSKate Stone 88b9c1b51eSKate Stone )" 89b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 90ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 91b9c1b51eSKate Stone more than one command per line." 92b9c1b51eSKate Stone R"( 93ea671fbdSKate Stone 94ea671fbdSKate Stone Special information about PYTHON breakpoint commands 95ea671fbdSKate Stone ---------------------------------------------------- 96ea671fbdSKate Stone 97b9c1b51eSKate Stone )" 98b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 99ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 100ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 101ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 102b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 103b9c1b51eSKate Stone R"( 104ea671fbdSKate Stone 105ea671fbdSKate Stone This auto-generated function is passed in three arguments: 106ea671fbdSKate Stone 107ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 108ea671fbdSKate Stone 109ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 110ea671fbdSKate Stone 111ea671fbdSKate Stone dict: the python session dictionary hit. 112ea671fbdSKate Stone 113b9c1b51eSKate Stone )" 114b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 115b9c1b51eSKate Stone to supply the function name prepended by the module name:" 116b9c1b51eSKate Stone R"( 117ea671fbdSKate Stone 118ea671fbdSKate Stone --python-function myutils.breakpoint_callback 119ea671fbdSKate Stone 120ea671fbdSKate Stone The function itself must have the following prototype: 121ea671fbdSKate Stone 122ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 123ea671fbdSKate Stone # Your code goes here 124ea671fbdSKate Stone 125b9c1b51eSKate Stone )" 126b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 127ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 128ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 129ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 130ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 131b9c1b51eSKate Stone via process.GetTarget()." 132b9c1b51eSKate Stone R"( 133ea671fbdSKate Stone 134b9c1b51eSKate Stone )" 135b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 136ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 137b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 138b9c1b51eSKate Stone R"( 139ea671fbdSKate Stone 140ea671fbdSKate Stone Example Python one-line breakpoint command: 141ea671fbdSKate Stone 142ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 143ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 1443b3b9ba1SDave Lee def function (frame, bp_loc, internal_dict): 1453b3b9ba1SDave Lee """frame: the lldb.SBFrame for the location at which you stopped 1463b3b9ba1SDave Lee bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information 1473b3b9ba1SDave Lee internal_dict: an LLDB support object not to be used""" 1483b3b9ba1SDave Lee print("Hit this breakpoint!") 1493b3b9ba1SDave Lee DONE 150ea671fbdSKate Stone 151ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 152ea671fbdSKate Stone 1533b3b9ba1SDave Lee (lldb) breakpoint command add -s python 1 -o 'import time; print(time.asctime())' 154ea671fbdSKate Stone (lldb) run 155ea671fbdSKate Stone Launching '.../a.out' (x86_64) 156ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 157ea671fbdSKate Stone Process 21778 Stopped 158ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 159ea671fbdSKate Stone 36 160ea671fbdSKate Stone 37 int c(int val) 161ea671fbdSKate Stone 38 { 162ea671fbdSKate Stone 39 -> return val + 3; 163ea671fbdSKate Stone 40 } 164ea671fbdSKate Stone 41 165ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 166ea671fbdSKate Stone 167ea671fbdSKate Stone Example multiple line Python breakpoint command: 168ea671fbdSKate Stone 169ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 170ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 1713b3b9ba1SDave Lee def function (frame, bp_loc, internal_dict): 1723b3b9ba1SDave Lee """frame: the lldb.SBFrame for the location at which you stopped 1733b3b9ba1SDave Lee bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information 1743b3b9ba1SDave Lee internal_dict: an LLDB support object not to be used""" 1753b3b9ba1SDave Lee global bp_count 1763b3b9ba1SDave Lee bp_count = bp_count + 1 1773b3b9ba1SDave Lee print("Hit this breakpoint " + repr(bp_count) + " times!") 1783b3b9ba1SDave Lee DONE 179ea671fbdSKate Stone 180b9c1b51eSKate Stone )" 181b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 182ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 183b9c1b51eSKate Stone initialized:" 184b9c1b51eSKate Stone R"( 185ea671fbdSKate Stone 186ea671fbdSKate Stone (lldb) script 187ea671fbdSKate Stone >>> bp_count = 0 188ea671fbdSKate Stone >>> quit() 189ea671fbdSKate Stone 190b9c1b51eSKate Stone )" 191b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 192ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 193ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 194ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 195b9c1b51eSKate Stone LLDB to stop." 196b9c1b51eSKate Stone R"( 197ea671fbdSKate Stone 198b9c1b51eSKate Stone )" 199b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 200b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 201405fe67fSCaroline Tice 202738af7a6SJim Ingham m_all_options.Append(&m_options); 203738af7a6SJim Ingham m_all_options.Append(&m_func_options, LLDB_OPT_SET_2 | LLDB_OPT_SET_3, 204738af7a6SJim Ingham LLDB_OPT_SET_2); 205738af7a6SJim Ingham m_all_options.Finalize(); 206738af7a6SJim Ingham 207405fe67fSCaroline Tice CommandArgumentEntry arg; 208405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 209405fe67fSCaroline Tice 210405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 211405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2121bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 213405fe67fSCaroline Tice 214b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 215b9c1b51eSKate Stone // argument entry. 216405fe67fSCaroline Tice arg.push_back(bp_id_arg); 217405fe67fSCaroline Tice 218405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 219405fe67fSCaroline Tice m_arguments.push_back(arg); 22030fdc8d8SChris Lattner } 22130fdc8d8SChris Lattner 222c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2235a988416SJim Ingham 224738af7a6SJim Ingham Options *GetOptions() override { return &m_all_options; } 2255a988416SJim Ingham 2260affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2277ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2280affb582SDave Lee if (output_sp && interactive) { 22944d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 23044d93782SGreg Clayton output_sp->Flush(); 23144d93782SGreg Clayton } 23244d93782SGreg Clayton } 23344d93782SGreg Clayton 234b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 235b9c1b51eSKate Stone std::string &line) override { 23644d93782SGreg Clayton io_handler.SetIsDone(true); 23744d93782SGreg Clayton 238b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 239b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 240b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 241b5796cb4SJim Ingham if (!bp_options) 242b5796cb4SJim Ingham continue; 243b5796cb4SJim Ingham 244a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 245e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 24692d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 24744d93782SGreg Clayton } 24844d93782SGreg Clayton } 24944d93782SGreg Clayton 250b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 251b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 252b9c1b51eSKate Stone CommandReturnObject &result) { 253b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 254b9c1b51eSKate Stone "> ", // Prompt 25544d93782SGreg Clayton *this, // IOHandlerDelegate 256b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 257b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2585a988416SJim Ingham } 2595a988416SJim Ingham 2605a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2615a988416SJim Ingham void 262b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 263b9c1b51eSKate Stone const char *oneliner) { 264b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 265a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 2665a988416SJim Ingham 267e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 268e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2695a988416SJim Ingham 27092d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 271b5796cb4SJim Ingham } 2725a988416SJim Ingham } 2735a988416SJim Ingham 274738af7a6SJim Ingham class CommandOptions : public OptionGroup { 2755a988416SJim Ingham public: 276b9c1b51eSKate Stone CommandOptions() 277738af7a6SJim Ingham : OptionGroup(), m_use_commands(false), m_use_script_language(false), 278b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 279738af7a6SJim Ingham m_one_liner() {} 28030fdc8d8SChris Lattner 281c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2825a988416SJim Ingham 28397206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 284b9c1b51eSKate Stone ExecutionContext *execution_context) override { 28597206d57SZachary Turner Status error; 286a925974bSAdrian Prantl const int short_option = 287a925974bSAdrian Prantl g_breakpoint_command_add_options[option_idx].short_option; 2885a988416SJim Ingham 289b9c1b51eSKate Stone switch (short_option) { 2905a988416SJim Ingham case 'o': 2915a988416SJim Ingham m_use_one_liner = true; 292adcd0268SBenjamin Kramer m_one_liner = std::string(option_arg); 2935a988416SJim Ingham break; 2945a988416SJim Ingham 2955a988416SJim Ingham case 's': 29647cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 297bd68a052SRaphael Isemann option_arg, 298bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 299b9c1b51eSKate Stone eScriptLanguageNone, error); 3008983d691SJonas Devlieghere switch (m_script_language) { 3018983d691SJonas Devlieghere case eScriptLanguagePython: 3028983d691SJonas Devlieghere case eScriptLanguageLua: 3035a988416SJim Ingham m_use_script_language = true; 3048983d691SJonas Devlieghere break; 3058983d691SJonas Devlieghere case eScriptLanguageNone: 306acdda134SJonas Devlieghere case eScriptLanguageUnknown: 3075a988416SJim Ingham m_use_script_language = false; 3088983d691SJonas Devlieghere break; 3095a988416SJim Ingham } 3105a988416SJim Ingham break; 3115a988416SJim Ingham 312b9c1b51eSKate Stone case 'e': { 3135a988416SJim Ingham bool success = false; 31447cbf4a0SPavel Labath m_stop_on_error = 31547cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3165a988416SJim Ingham if (!success) 317b9c1b51eSKate Stone error.SetErrorStringWithFormat( 318fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 319fe11483bSZachary Turner option_arg.str().c_str()); 320b9c1b51eSKate Stone } break; 3215a988416SJim Ingham 32233df7cd3SJim Ingham case 'D': 32333df7cd3SJim Ingham m_use_dummy = true; 32433df7cd3SJim Ingham break; 32533df7cd3SJim Ingham 3265a988416SJim Ingham default: 32736162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 3285a988416SJim Ingham } 3295a988416SJim Ingham return error; 3305a988416SJim Ingham } 331c8ecc2a9SEugene Zelenko 332b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3335a988416SJim Ingham m_use_commands = true; 3345a988416SJim Ingham m_use_script_language = false; 3355a988416SJim Ingham m_script_language = eScriptLanguageNone; 3365a988416SJim Ingham 3375a988416SJim Ingham m_use_one_liner = false; 3385a988416SJim Ingham m_stop_on_error = true; 3395a988416SJim Ingham m_one_liner.clear(); 34033df7cd3SJim Ingham m_use_dummy = false; 3415a988416SJim Ingham } 3425a988416SJim Ingham 3431f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 344bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_add_options); 3451f0f5b5bSZachary Turner } 3465a988416SJim Ingham 3475a988416SJim Ingham // Instance variables to hold the values for command options. 3485a988416SJim Ingham 3495a988416SJim Ingham bool m_use_commands; 3505a988416SJim Ingham bool m_use_script_language; 3515a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3525a988416SJim Ingham 3535a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3545a988416SJim Ingham bool m_use_one_liner; 3555a988416SJim Ingham std::string m_one_liner; 3565a988416SJim Ingham bool m_stop_on_error; 35733df7cd3SJim Ingham bool m_use_dummy; 3585a988416SJim Ingham }; 3595a988416SJim Ingham 3605a988416SJim Ingham protected: 361b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 362cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 36330fdc8d8SChris Lattner 364cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 36530fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 36630fdc8d8SChris Lattner 367b9c1b51eSKate Stone if (num_breakpoints == 0) { 36830fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 36930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37030fdc8d8SChris Lattner return false; 37130fdc8d8SChris Lattner } 37230fdc8d8SChris Lattner 373738af7a6SJim Ingham if (!m_func_options.GetName().empty()) { 374738af7a6SJim Ingham m_options.m_use_one_liner = false; 3751ff01cfeSJonas Devlieghere if (!m_options.m_use_script_language) { 3761ff01cfeSJonas Devlieghere m_options.m_script_language = GetDebugger().GetScriptLanguage(); 377738af7a6SJim Ingham m_options.m_use_script_language = true; 3788d4a8010SEnrico Granata } 3791ff01cfeSJonas Devlieghere } 3808d4a8010SEnrico Granata 38130fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 382b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 383cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 384b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 38530fdc8d8SChris Lattner 386b5796cb4SJim Ingham m_bp_options_vec.clear(); 387b5796cb4SJim Ingham 388b9c1b51eSKate Stone if (result.Succeeded()) { 389c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 390d9916eaeSJim Ingham 391b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 39230fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 393b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 394b9c1b51eSKate Stone Breakpoint *bp = 395cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 396c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 397b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 39839d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 39939d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 400b9c1b51eSKate Stone } else { 401b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 402b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 40305097246SAdrian Prantl // This breakpoint does have an associated location. Get its 40405097246SAdrian Prantl // breakpoint options. 40530fdc8d8SChris Lattner if (bp_loc_sp) 40639d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 40739d7d4f0SJohnny Chen } 408b5796cb4SJim Ingham if (bp_options) 409b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 410b5796cb4SJim Ingham } 411b5796cb4SJim Ingham } 41239d7d4f0SJohnny Chen 41305097246SAdrian Prantl // If we are using script language, get the script interpreter in order 41405097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 41505097246SAdrian Prantl // associated with this object. 416b9c1b51eSKate Stone if (m_options.m_use_script_language) { 417*280ae107SPedro Tammela Status error; 4185e32eb1cSJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter( 4195e32eb1cSJonas Devlieghere /*can_create=*/true, m_options.m_script_language); 42039d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 421b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 422*280ae107SPedro Tammela error = script_interp->SetBreakpointCommandCallback( 423b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 424738af7a6SJim Ingham } else if (!m_func_options.GetName().empty()) { 425*280ae107SPedro Tammela error = script_interp->SetBreakpointCommandCallbackFunction( 426738af7a6SJim Ingham m_bp_options_vec, m_func_options.GetName().c_str(), 427738af7a6SJim Ingham m_func_options.GetStructuredData()); 428b9c1b51eSKate Stone } else { 429b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 430b9c1b51eSKate Stone m_bp_options_vec, result); 4318d4a8010SEnrico Granata } 432*280ae107SPedro Tammela if (!error.Success()) 433*280ae107SPedro Tammela result.SetError(error); 434b9c1b51eSKate Stone } else { 43539d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 43639d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 437b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 43839d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 43939d7d4f0SJohnny Chen else 440b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 44130fdc8d8SChris Lattner } 44230fdc8d8SChris Lattner } 44330fdc8d8SChris Lattner 44430fdc8d8SChris Lattner return result.Succeeded(); 44530fdc8d8SChris Lattner } 44630fdc8d8SChris Lattner 4475a988416SJim Ingham private: 4485a988416SJim Ingham CommandOptions m_options; 449738af7a6SJim Ingham OptionGroupPythonClassWithDict m_func_options; 450738af7a6SJim Ingham OptionGroupOptions m_all_options; 451738af7a6SJim Ingham 452b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 453b9c1b51eSKate Stone // breakpoint options that 454b9c1b51eSKate Stone // we are currently 45505097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45605097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45705097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45805097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 45905097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 46005097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 46105097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 46205097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 46305097246SAdrian Prantl // right now. 4645a988416SJim Ingham static const char *g_reader_instructions; 4655a988416SJim Ingham }; 4665a988416SJim Ingham 467b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 468b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4695a988416SJim Ingham 47093e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 47130fdc8d8SChris Lattner 472f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 473f94668e3SRaphael Isemann #include "CommandOptions.inc" 4741f0f5b5bSZachary Turner 475b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4765a988416SJim Ingham public: 477b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 478b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 47993e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 480c8ecc2a9SEugene Zelenko nullptr), 481b9c1b51eSKate Stone m_options() { 482405fe67fSCaroline Tice CommandArgumentEntry arg; 483405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 484405fe67fSCaroline Tice 485405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 486405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 487405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 488405fe67fSCaroline Tice 489b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 490b9c1b51eSKate Stone // argument entry. 491405fe67fSCaroline Tice arg.push_back(bp_id_arg); 492405fe67fSCaroline Tice 493405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 494405fe67fSCaroline Tice m_arguments.push_back(arg); 49530fdc8d8SChris Lattner } 49630fdc8d8SChris Lattner 497c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4985a988416SJim Ingham 499b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 50033df7cd3SJim Ingham 501b9c1b51eSKate Stone class CommandOptions : public Options { 50233df7cd3SJim Ingham public: 503b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 50433df7cd3SJim Ingham 505c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50633df7cd3SJim Ingham 50797206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 508b9c1b51eSKate Stone ExecutionContext *execution_context) override { 50997206d57SZachary Turner Status error; 51033df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 51133df7cd3SJim Ingham 512b9c1b51eSKate Stone switch (short_option) { 51333df7cd3SJim Ingham case 'D': 51433df7cd3SJim Ingham m_use_dummy = true; 51533df7cd3SJim Ingham break; 51633df7cd3SJim Ingham 51733df7cd3SJim Ingham default: 51836162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 51933df7cd3SJim Ingham } 52033df7cd3SJim Ingham 52133df7cd3SJim Ingham return error; 52233df7cd3SJim Ingham } 52333df7cd3SJim Ingham 524b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 52533df7cd3SJim Ingham m_use_dummy = false; 52633df7cd3SJim Ingham } 52733df7cd3SJim Ingham 5281f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 529bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_delete_options); 5301f0f5b5bSZachary Turner } 53133df7cd3SJim Ingham 53233df7cd3SJim Ingham // Instance variables to hold the values for command options. 53333df7cd3SJim Ingham bool m_use_dummy; 53433df7cd3SJim Ingham }; 53533df7cd3SJim Ingham 5365a988416SJim Ingham protected: 537b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 538cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 53930fdc8d8SChris Lattner 540cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 54130fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 54230fdc8d8SChris Lattner 543b9c1b51eSKate Stone if (num_breakpoints == 0) { 54493e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 54530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54630fdc8d8SChris Lattner return false; 54730fdc8d8SChris Lattner } 54830fdc8d8SChris Lattner 54911eb9c64SZachary Turner if (command.empty()) { 550b9c1b51eSKate Stone result.AppendError( 551b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 55230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55330fdc8d8SChris Lattner return false; 55430fdc8d8SChris Lattner } 55530fdc8d8SChris Lattner 55630fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 557b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 558cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 559b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 56030fdc8d8SChris Lattner 561b9c1b51eSKate Stone if (result.Succeeded()) { 562c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 563b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 56430fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 565b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 566b9c1b51eSKate Stone Breakpoint *bp = 567cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 568b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 569b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 570b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 57130fdc8d8SChris Lattner if (bp_loc_sp) 57230fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 573b9c1b51eSKate Stone else { 57430fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 57530fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 57630fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 57730fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 57830fdc8d8SChris Lattner return false; 57930fdc8d8SChris Lattner } 580b9c1b51eSKate Stone } else { 58130fdc8d8SChris Lattner bp->ClearCallback(); 58230fdc8d8SChris Lattner } 58330fdc8d8SChris Lattner } 58430fdc8d8SChris Lattner } 58530fdc8d8SChris Lattner } 58630fdc8d8SChris Lattner return result.Succeeded(); 58730fdc8d8SChris Lattner } 588c8ecc2a9SEugene Zelenko 58933df7cd3SJim Ingham private: 59033df7cd3SJim Ingham CommandOptions m_options; 5915a988416SJim Ingham }; 59230fdc8d8SChris Lattner 59330fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 59430fdc8d8SChris Lattner 595b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 5965a988416SJim Ingham public: 597b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 59804a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 59904a4c091SRaphael Isemann "List the script or set of commands to be " 60004a4c091SRaphael Isemann "executed when the breakpoint is hit.", 60104a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 602405fe67fSCaroline Tice CommandArgumentEntry arg; 603405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 604405fe67fSCaroline Tice 605405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 606405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 607405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 608405fe67fSCaroline Tice 609b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 610b9c1b51eSKate Stone // argument entry. 611405fe67fSCaroline Tice arg.push_back(bp_id_arg); 612405fe67fSCaroline Tice 613405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 614405fe67fSCaroline Tice m_arguments.push_back(arg); 61530fdc8d8SChris Lattner } 61630fdc8d8SChris Lattner 617c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 61830fdc8d8SChris Lattner 6195a988416SJim Ingham protected: 620b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 62104a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 62230fdc8d8SChris Lattner 62330fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 62430fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 62530fdc8d8SChris Lattner 626b9c1b51eSKate Stone if (num_breakpoints == 0) { 62730fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 62830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 62930fdc8d8SChris Lattner return false; 63030fdc8d8SChris Lattner } 63130fdc8d8SChris Lattner 63211eb9c64SZachary Turner if (command.empty()) { 633b9c1b51eSKate Stone result.AppendError( 634b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 63530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63630fdc8d8SChris Lattner return false; 63730fdc8d8SChris Lattner } 63830fdc8d8SChris Lattner 63930fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 640b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 641b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 642b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 64330fdc8d8SChris Lattner 644b9c1b51eSKate Stone if (result.Succeeded()) { 645c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 646b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 64730fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 648b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 649b9c1b51eSKate Stone Breakpoint *bp = 650b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 65130fdc8d8SChris Lattner 652b9c1b51eSKate Stone if (bp) { 653af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 654b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 655af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 656a925974bSAdrian Prantl if (!bp_loc_sp) { 65730fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 65830fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 65930fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 66030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 66130fdc8d8SChris Lattner return false; 66230fdc8d8SChris Lattner } 66330fdc8d8SChris Lattner } 66430fdc8d8SChris Lattner 66530fdc8d8SChris Lattner StreamString id_str; 666e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 667e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 668e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 669af26b22cSJim Ingham const Baton *baton = nullptr; 670af26b22cSJim Ingham if (bp_loc_sp) 671a925974bSAdrian Prantl baton = 672a925974bSAdrian Prantl bp_loc_sp 673af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 674af26b22cSJim Ingham ->GetBaton(); 675af26b22cSJim Ingham else 676af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 677af26b22cSJim Ingham 678b9c1b51eSKate Stone if (baton) { 679b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 680b9c1b51eSKate Stone id_str.GetData()); 6814f728bfcSRaphael Isemann baton->GetDescription(result.GetOutputStream().AsRawOstream(), 6824f728bfcSRaphael Isemann eDescriptionLevelFull, 6834f728bfcSRaphael Isemann result.GetOutputStream().GetIndentLevel() + 6844f728bfcSRaphael Isemann 2); 685b9c1b51eSKate Stone } else { 686b9c1b51eSKate Stone result.AppendMessageWithFormat( 687b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 688e16c50a1SJim Ingham id_str.GetData()); 68930fdc8d8SChris Lattner } 69030fdc8d8SChris Lattner } 69130fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 692b9c1b51eSKate Stone } else { 693b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 694b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 69530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 69630fdc8d8SChris Lattner } 69730fdc8d8SChris Lattner } 69830fdc8d8SChris Lattner } 69930fdc8d8SChris Lattner 70030fdc8d8SChris Lattner return result.Succeeded(); 70130fdc8d8SChris Lattner } 7025a988416SJim Ingham }; 70330fdc8d8SChris Lattner 70430fdc8d8SChris Lattner // CommandObjectBreakpointCommand 70530fdc8d8SChris Lattner 706b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 707b9c1b51eSKate Stone CommandInterpreter &interpreter) 7087428a18cSKate Stone : CommandObjectMultiword( 709a925974bSAdrian Prantl interpreter, "command", 710a925974bSAdrian Prantl "Commands for adding, removing and listing " 711b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 712b9c1b51eSKate Stone "hit.", 713b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 714b9c1b51eSKate Stone CommandObjectSP add_command_object( 715b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 716b9c1b51eSKate Stone CommandObjectSP delete_command_object( 717b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 718b9c1b51eSKate Stone CommandObjectSP list_command_object( 719b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 72030fdc8d8SChris Lattner 72130fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 72293e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 72330fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 72430fdc8d8SChris Lattner 72523f59509SGreg Clayton LoadSubCommand("add", add_command_object); 72623f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 72723f59509SGreg Clayton LoadSubCommand("list", list_command_object); 72830fdc8d8SChris Lattner } 72930fdc8d8SChris Lattner 730c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 731