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. " 65*60ad0fd3SJim Ingham "The commands added to the breakpoint replace any " 66*60ad0fd3SJim Ingham "commands previously added to it." 67b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 68b9c1b51eSKate Stone "commands to the last created breakpoint.", 69c8ecc2a9SEugene Zelenko nullptr), 70b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 71b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 72738af7a6SJim Ingham m_options(), m_func_options("breakpoint command", false, 'F') { 7330fdc8d8SChris Lattner SetHelpLong( 74ea671fbdSKate Stone R"( 75ea671fbdSKate Stone General information about entering breakpoint commands 76ea671fbdSKate Stone ------------------------------------------------------ 77ea671fbdSKate Stone 78b9c1b51eSKate Stone )" 79b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 80ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 81b9c1b51eSKate Stone prompt until 'DONE' is entered." 82b9c1b51eSKate Stone R"( 83ea671fbdSKate Stone 84b9c1b51eSKate Stone )" 85b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 86ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 87b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 88b9c1b51eSKate Stone R"( 89ea671fbdSKate Stone 90b9c1b51eSKate Stone )" 91b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 92ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 93b9c1b51eSKate Stone more than one command per line." 94b9c1b51eSKate Stone R"( 95ea671fbdSKate Stone 96ea671fbdSKate Stone Special information about PYTHON breakpoint commands 97ea671fbdSKate Stone ---------------------------------------------------- 98ea671fbdSKate Stone 99b9c1b51eSKate Stone )" 100b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 101ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 102ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 103ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 104b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 105b9c1b51eSKate Stone R"( 106ea671fbdSKate Stone 107ea671fbdSKate Stone This auto-generated function is passed in three arguments: 108ea671fbdSKate Stone 109ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 110ea671fbdSKate Stone 111ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 112ea671fbdSKate Stone 113ea671fbdSKate Stone dict: the python session dictionary hit. 114ea671fbdSKate Stone 115b9c1b51eSKate Stone )" 116b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 117b9c1b51eSKate Stone to supply the function name prepended by the module name:" 118b9c1b51eSKate Stone R"( 119ea671fbdSKate Stone 120ea671fbdSKate Stone --python-function myutils.breakpoint_callback 121ea671fbdSKate Stone 122365b186cSJim Ingham The function itself must have either of the following prototypes: 123ea671fbdSKate Stone 124483ec136SJim Ingham def breakpoint_callback(frame, bp_loc, internal_dict): 125ea671fbdSKate Stone # Your code goes here 126ea671fbdSKate Stone 127365b186cSJim Ingham or: 128365b186cSJim Ingham 129483ec136SJim Ingham def breakpoint_callback(frame, bp_loc, extra_args, internal_dict): 130365b186cSJim Ingham # Your code goes here 131365b186cSJim Ingham 132b9c1b51eSKate Stone )" 133b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 134365b186cSJim Ingham described above. In the second form, any -k and -v pairs provided to the command will \ 135365b186cSJim Ingham be packaged into a SBDictionary in an SBStructuredData and passed as the extra_args parameter. \ 136365b186cSJim Ingham \n\n\ 137365b186cSJim Ingham Note that the global variable 'lldb.frame' will NOT be updated when \ 138ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 139ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 140ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 141b9c1b51eSKate Stone via process.GetTarget()." 142b9c1b51eSKate Stone R"( 143ea671fbdSKate Stone 144b9c1b51eSKate Stone )" 145b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 146ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 147b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 148b9c1b51eSKate Stone R"( 149ea671fbdSKate Stone 150ea671fbdSKate Stone Example Python one-line breakpoint command: 151ea671fbdSKate Stone 152ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 153ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 1543b3b9ba1SDave Lee def function (frame, bp_loc, internal_dict): 1553b3b9ba1SDave Lee """frame: the lldb.SBFrame for the location at which you stopped 1563b3b9ba1SDave Lee bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information 1573b3b9ba1SDave Lee internal_dict: an LLDB support object not to be used""" 1583b3b9ba1SDave Lee print("Hit this breakpoint!") 1593b3b9ba1SDave Lee DONE 160ea671fbdSKate Stone 161ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 162ea671fbdSKate Stone 1633b3b9ba1SDave Lee (lldb) breakpoint command add -s python 1 -o 'import time; print(time.asctime())' 164ea671fbdSKate Stone (lldb) run 165ea671fbdSKate Stone Launching '.../a.out' (x86_64) 166ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 167ea671fbdSKate Stone Process 21778 Stopped 168ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 169ea671fbdSKate Stone 36 170ea671fbdSKate Stone 37 int c(int val) 171ea671fbdSKate Stone 38 { 172ea671fbdSKate Stone 39 -> return val + 3; 173ea671fbdSKate Stone 40 } 174ea671fbdSKate Stone 41 175ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 176ea671fbdSKate Stone 177ea671fbdSKate Stone Example multiple line Python breakpoint command: 178ea671fbdSKate Stone 179ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 180ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 1813b3b9ba1SDave Lee def function (frame, bp_loc, internal_dict): 1823b3b9ba1SDave Lee """frame: the lldb.SBFrame for the location at which you stopped 1833b3b9ba1SDave Lee bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information 1843b3b9ba1SDave Lee internal_dict: an LLDB support object not to be used""" 1853b3b9ba1SDave Lee global bp_count 1863b3b9ba1SDave Lee bp_count = bp_count + 1 1873b3b9ba1SDave Lee print("Hit this breakpoint " + repr(bp_count) + " times!") 1883b3b9ba1SDave Lee DONE 189ea671fbdSKate Stone 190b9c1b51eSKate Stone )" 191b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 192ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 193b9c1b51eSKate Stone initialized:" 194b9c1b51eSKate Stone R"( 195ea671fbdSKate Stone 196ea671fbdSKate Stone (lldb) script 197ea671fbdSKate Stone >>> bp_count = 0 198ea671fbdSKate Stone >>> quit() 199ea671fbdSKate Stone 200b9c1b51eSKate Stone )" 201b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 202ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 203ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 204ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 205b9c1b51eSKate Stone LLDB to stop." 206b9c1b51eSKate Stone R"( 207ea671fbdSKate Stone 208b9c1b51eSKate Stone )" 209b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 210b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 211405fe67fSCaroline Tice 212738af7a6SJim Ingham m_all_options.Append(&m_options); 213738af7a6SJim Ingham m_all_options.Append(&m_func_options, LLDB_OPT_SET_2 | LLDB_OPT_SET_3, 214738af7a6SJim Ingham LLDB_OPT_SET_2); 215738af7a6SJim Ingham m_all_options.Finalize(); 216738af7a6SJim Ingham 217405fe67fSCaroline Tice CommandArgumentEntry arg; 218405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 219405fe67fSCaroline Tice 220405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 221405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2221bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 223405fe67fSCaroline Tice 224b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 225b9c1b51eSKate Stone // argument entry. 226405fe67fSCaroline Tice arg.push_back(bp_id_arg); 227405fe67fSCaroline Tice 228405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 229405fe67fSCaroline Tice m_arguments.push_back(arg); 23030fdc8d8SChris Lattner } 23130fdc8d8SChris Lattner 232c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2335a988416SJim Ingham 234738af7a6SJim Ingham Options *GetOptions() override { return &m_all_options; } 2355a988416SJim Ingham 2360affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2377ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2380affb582SDave Lee if (output_sp && interactive) { 23944d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 24044d93782SGreg Clayton output_sp->Flush(); 24144d93782SGreg Clayton } 24244d93782SGreg Clayton } 24344d93782SGreg Clayton 244b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 245b9c1b51eSKate Stone std::string &line) override { 24644d93782SGreg Clayton io_handler.SetIsDone(true); 24744d93782SGreg Clayton 248b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 249b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 250b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 251b5796cb4SJim Ingham if (!bp_options) 252b5796cb4SJim Ingham continue; 253b5796cb4SJim Ingham 254a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 255e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 25692d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 25744d93782SGreg Clayton } 25844d93782SGreg Clayton } 25944d93782SGreg Clayton 260b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 261b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 262b9c1b51eSKate Stone CommandReturnObject &result) { 263b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 264b9c1b51eSKate Stone "> ", // Prompt 26544d93782SGreg Clayton *this, // IOHandlerDelegate 266b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 267b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2685a988416SJim Ingham } 2695a988416SJim Ingham 2705a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2715a988416SJim Ingham void 272b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 273b9c1b51eSKate Stone const char *oneliner) { 274b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 275a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 2765a988416SJim Ingham 277e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 278e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2795a988416SJim Ingham 28092d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 281b5796cb4SJim Ingham } 2825a988416SJim Ingham } 2835a988416SJim Ingham 284738af7a6SJim Ingham class CommandOptions : public OptionGroup { 2855a988416SJim Ingham public: 286b9c1b51eSKate Stone CommandOptions() 287738af7a6SJim Ingham : OptionGroup(), m_use_commands(false), m_use_script_language(false), 288b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 289738af7a6SJim Ingham m_one_liner() {} 29030fdc8d8SChris Lattner 291c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2925a988416SJim Ingham 29397206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 294b9c1b51eSKate Stone ExecutionContext *execution_context) override { 29597206d57SZachary Turner Status error; 296a925974bSAdrian Prantl const int short_option = 297a925974bSAdrian Prantl g_breakpoint_command_add_options[option_idx].short_option; 2985a988416SJim Ingham 299b9c1b51eSKate Stone switch (short_option) { 3005a988416SJim Ingham case 'o': 3015a988416SJim Ingham m_use_one_liner = true; 302adcd0268SBenjamin Kramer m_one_liner = std::string(option_arg); 3035a988416SJim Ingham break; 3045a988416SJim Ingham 3055a988416SJim Ingham case 's': 30647cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 307bd68a052SRaphael Isemann option_arg, 308bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 309b9c1b51eSKate Stone eScriptLanguageNone, error); 3108983d691SJonas Devlieghere switch (m_script_language) { 3118983d691SJonas Devlieghere case eScriptLanguagePython: 3128983d691SJonas Devlieghere case eScriptLanguageLua: 3135a988416SJim Ingham m_use_script_language = true; 3148983d691SJonas Devlieghere break; 3158983d691SJonas Devlieghere case eScriptLanguageNone: 316acdda134SJonas Devlieghere case eScriptLanguageUnknown: 3175a988416SJim Ingham m_use_script_language = false; 3188983d691SJonas Devlieghere break; 3195a988416SJim Ingham } 3205a988416SJim Ingham break; 3215a988416SJim Ingham 322b9c1b51eSKate Stone case 'e': { 3235a988416SJim Ingham bool success = false; 32447cbf4a0SPavel Labath m_stop_on_error = 32547cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3265a988416SJim Ingham if (!success) 327b9c1b51eSKate Stone error.SetErrorStringWithFormat( 328fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 329fe11483bSZachary Turner option_arg.str().c_str()); 330b9c1b51eSKate Stone } break; 3315a988416SJim Ingham 33233df7cd3SJim Ingham case 'D': 33333df7cd3SJim Ingham m_use_dummy = true; 33433df7cd3SJim Ingham break; 33533df7cd3SJim Ingham 3365a988416SJim Ingham default: 33736162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 3385a988416SJim Ingham } 3395a988416SJim Ingham return error; 3405a988416SJim Ingham } 341c8ecc2a9SEugene Zelenko 342b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3435a988416SJim Ingham m_use_commands = true; 3445a988416SJim Ingham m_use_script_language = false; 3455a988416SJim Ingham m_script_language = eScriptLanguageNone; 3465a988416SJim Ingham 3475a988416SJim Ingham m_use_one_liner = false; 3485a988416SJim Ingham m_stop_on_error = true; 3495a988416SJim Ingham m_one_liner.clear(); 35033df7cd3SJim Ingham m_use_dummy = false; 3515a988416SJim Ingham } 3525a988416SJim Ingham 3531f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 354bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_add_options); 3551f0f5b5bSZachary Turner } 3565a988416SJim Ingham 3575a988416SJim Ingham // Instance variables to hold the values for command options. 3585a988416SJim Ingham 3595a988416SJim Ingham bool m_use_commands; 3605a988416SJim Ingham bool m_use_script_language; 3615a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3625a988416SJim Ingham 3635a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3645a988416SJim Ingham bool m_use_one_liner; 3655a988416SJim Ingham std::string m_one_liner; 3665a988416SJim Ingham bool m_stop_on_error; 36733df7cd3SJim Ingham bool m_use_dummy; 3685a988416SJim Ingham }; 3695a988416SJim Ingham 3705a988416SJim Ingham protected: 371b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 372cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 37330fdc8d8SChris Lattner 374cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 37530fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 37630fdc8d8SChris Lattner 377b9c1b51eSKate Stone if (num_breakpoints == 0) { 37830fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 38030fdc8d8SChris Lattner return false; 38130fdc8d8SChris Lattner } 38230fdc8d8SChris Lattner 383738af7a6SJim Ingham if (!m_func_options.GetName().empty()) { 384738af7a6SJim Ingham m_options.m_use_one_liner = false; 3851ff01cfeSJonas Devlieghere if (!m_options.m_use_script_language) { 3861ff01cfeSJonas Devlieghere m_options.m_script_language = GetDebugger().GetScriptLanguage(); 387738af7a6SJim Ingham m_options.m_use_script_language = true; 3888d4a8010SEnrico Granata } 3891ff01cfeSJonas Devlieghere } 3908d4a8010SEnrico Granata 39130fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 392b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 393cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 394b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 39530fdc8d8SChris Lattner 396b5796cb4SJim Ingham m_bp_options_vec.clear(); 397b5796cb4SJim Ingham 398b9c1b51eSKate Stone if (result.Succeeded()) { 399c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 400d9916eaeSJim Ingham 401b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 40230fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 403b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 404b9c1b51eSKate Stone Breakpoint *bp = 405cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 406c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 407b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 40839d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 40939d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 410b9c1b51eSKate Stone } else { 411b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 412b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 41305097246SAdrian Prantl // This breakpoint does have an associated location. Get its 41405097246SAdrian Prantl // breakpoint options. 41530fdc8d8SChris Lattner if (bp_loc_sp) 41639d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 41739d7d4f0SJohnny Chen } 418b5796cb4SJim Ingham if (bp_options) 419b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 420b5796cb4SJim Ingham } 421b5796cb4SJim Ingham } 42239d7d4f0SJohnny Chen 42305097246SAdrian Prantl // If we are using script language, get the script interpreter in order 42405097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 42505097246SAdrian Prantl // associated with this object. 426b9c1b51eSKate Stone if (m_options.m_use_script_language) { 427280ae107SPedro Tammela Status error; 4285e32eb1cSJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter( 4295e32eb1cSJonas Devlieghere /*can_create=*/true, m_options.m_script_language); 43039d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 431b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 432280ae107SPedro Tammela error = script_interp->SetBreakpointCommandCallback( 433b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 434738af7a6SJim Ingham } else if (!m_func_options.GetName().empty()) { 435280ae107SPedro Tammela error = script_interp->SetBreakpointCommandCallbackFunction( 436738af7a6SJim Ingham m_bp_options_vec, m_func_options.GetName().c_str(), 437738af7a6SJim Ingham m_func_options.GetStructuredData()); 438b9c1b51eSKate Stone } else { 439b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 440b9c1b51eSKate Stone m_bp_options_vec, result); 4418d4a8010SEnrico Granata } 442280ae107SPedro Tammela if (!error.Success()) 443280ae107SPedro Tammela result.SetError(error); 444b9c1b51eSKate Stone } else { 44539d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 44639d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 447b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 44839d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 44939d7d4f0SJohnny Chen else 450b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 45130fdc8d8SChris Lattner } 45230fdc8d8SChris Lattner } 45330fdc8d8SChris Lattner 45430fdc8d8SChris Lattner return result.Succeeded(); 45530fdc8d8SChris Lattner } 45630fdc8d8SChris Lattner 4575a988416SJim Ingham private: 4585a988416SJim Ingham CommandOptions m_options; 459738af7a6SJim Ingham OptionGroupPythonClassWithDict m_func_options; 460738af7a6SJim Ingham OptionGroupOptions m_all_options; 461738af7a6SJim Ingham 462b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 463b9c1b51eSKate Stone // breakpoint options that 464b9c1b51eSKate Stone // we are currently 46505097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 46605097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 46705097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 46805097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 46905097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 47005097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 47105097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 47205097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 47305097246SAdrian Prantl // right now. 4745a988416SJim Ingham static const char *g_reader_instructions; 4755a988416SJim Ingham }; 4765a988416SJim Ingham 477b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 478b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4795a988416SJim Ingham 48093e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 48130fdc8d8SChris Lattner 482f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 483f94668e3SRaphael Isemann #include "CommandOptions.inc" 4841f0f5b5bSZachary Turner 485b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4865a988416SJim Ingham public: 487b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 488b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 48993e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 490c8ecc2a9SEugene Zelenko nullptr), 491b9c1b51eSKate Stone m_options() { 492405fe67fSCaroline Tice CommandArgumentEntry arg; 493405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 494405fe67fSCaroline Tice 495405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 496405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 497405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 498405fe67fSCaroline Tice 499b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 500b9c1b51eSKate Stone // argument entry. 501405fe67fSCaroline Tice arg.push_back(bp_id_arg); 502405fe67fSCaroline Tice 503405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 504405fe67fSCaroline Tice m_arguments.push_back(arg); 50530fdc8d8SChris Lattner } 50630fdc8d8SChris Lattner 507c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 5085a988416SJim Ingham 509b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 51033df7cd3SJim Ingham 511b9c1b51eSKate Stone class CommandOptions : public Options { 51233df7cd3SJim Ingham public: 513b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 51433df7cd3SJim Ingham 515c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 51633df7cd3SJim Ingham 51797206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 518b9c1b51eSKate Stone ExecutionContext *execution_context) override { 51997206d57SZachary Turner Status error; 52033df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 52133df7cd3SJim Ingham 522b9c1b51eSKate Stone switch (short_option) { 52333df7cd3SJim Ingham case 'D': 52433df7cd3SJim Ingham m_use_dummy = true; 52533df7cd3SJim Ingham break; 52633df7cd3SJim Ingham 52733df7cd3SJim Ingham default: 52836162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 52933df7cd3SJim Ingham } 53033df7cd3SJim Ingham 53133df7cd3SJim Ingham return error; 53233df7cd3SJim Ingham } 53333df7cd3SJim Ingham 534b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 53533df7cd3SJim Ingham m_use_dummy = false; 53633df7cd3SJim Ingham } 53733df7cd3SJim Ingham 5381f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 539bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_delete_options); 5401f0f5b5bSZachary Turner } 54133df7cd3SJim Ingham 54233df7cd3SJim Ingham // Instance variables to hold the values for command options. 54333df7cd3SJim Ingham bool m_use_dummy; 54433df7cd3SJim Ingham }; 54533df7cd3SJim Ingham 5465a988416SJim Ingham protected: 547b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 548cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 54930fdc8d8SChris Lattner 550cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 55130fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 55230fdc8d8SChris Lattner 553b9c1b51eSKate Stone if (num_breakpoints == 0) { 55493e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 55530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55630fdc8d8SChris Lattner return false; 55730fdc8d8SChris Lattner } 55830fdc8d8SChris Lattner 55911eb9c64SZachary Turner if (command.empty()) { 560b9c1b51eSKate Stone result.AppendError( 561b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 56230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 56330fdc8d8SChris Lattner return false; 56430fdc8d8SChris Lattner } 56530fdc8d8SChris Lattner 56630fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 567b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 568cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 569b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 57030fdc8d8SChris Lattner 571b9c1b51eSKate Stone if (result.Succeeded()) { 572c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 573b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 57430fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 575b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 576b9c1b51eSKate Stone Breakpoint *bp = 577cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 578b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 579b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 580b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 58130fdc8d8SChris Lattner if (bp_loc_sp) 58230fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 583b9c1b51eSKate Stone else { 58430fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 58530fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 58630fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 58730fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 58830fdc8d8SChris Lattner return false; 58930fdc8d8SChris Lattner } 590b9c1b51eSKate Stone } else { 59130fdc8d8SChris Lattner bp->ClearCallback(); 59230fdc8d8SChris Lattner } 59330fdc8d8SChris Lattner } 59430fdc8d8SChris Lattner } 59530fdc8d8SChris Lattner } 59630fdc8d8SChris Lattner return result.Succeeded(); 59730fdc8d8SChris Lattner } 598c8ecc2a9SEugene Zelenko 59933df7cd3SJim Ingham private: 60033df7cd3SJim Ingham CommandOptions m_options; 6015a988416SJim Ingham }; 60230fdc8d8SChris Lattner 60330fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 60430fdc8d8SChris Lattner 605b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 6065a988416SJim Ingham public: 607b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 60804a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 60904a4c091SRaphael Isemann "List the script or set of commands to be " 61004a4c091SRaphael Isemann "executed when the breakpoint is hit.", 61104a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 612405fe67fSCaroline Tice CommandArgumentEntry arg; 613405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 614405fe67fSCaroline Tice 615405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 616405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 617405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 618405fe67fSCaroline Tice 619b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 620b9c1b51eSKate Stone // argument entry. 621405fe67fSCaroline Tice arg.push_back(bp_id_arg); 622405fe67fSCaroline Tice 623405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 624405fe67fSCaroline Tice m_arguments.push_back(arg); 62530fdc8d8SChris Lattner } 62630fdc8d8SChris Lattner 627c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 62830fdc8d8SChris Lattner 6295a988416SJim Ingham protected: 630b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 63104a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 63230fdc8d8SChris Lattner 63330fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 63430fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 63530fdc8d8SChris Lattner 636b9c1b51eSKate Stone if (num_breakpoints == 0) { 63730fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 63830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63930fdc8d8SChris Lattner return false; 64030fdc8d8SChris Lattner } 64130fdc8d8SChris Lattner 64211eb9c64SZachary Turner if (command.empty()) { 643b9c1b51eSKate Stone result.AppendError( 644b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 64530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 64630fdc8d8SChris Lattner return false; 64730fdc8d8SChris Lattner } 64830fdc8d8SChris Lattner 64930fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 650b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 651b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 652b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 65330fdc8d8SChris Lattner 654b9c1b51eSKate Stone if (result.Succeeded()) { 655c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 656b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 65730fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 658b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 659b9c1b51eSKate Stone Breakpoint *bp = 660b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 66130fdc8d8SChris Lattner 662b9c1b51eSKate Stone if (bp) { 663af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 664b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 665af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 666a925974bSAdrian Prantl if (!bp_loc_sp) { 66730fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 66830fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 66930fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 67030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 67130fdc8d8SChris Lattner return false; 67230fdc8d8SChris Lattner } 67330fdc8d8SChris Lattner } 67430fdc8d8SChris Lattner 67530fdc8d8SChris Lattner StreamString id_str; 676e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 677e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 678e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 679af26b22cSJim Ingham const Baton *baton = nullptr; 680af26b22cSJim Ingham if (bp_loc_sp) 681a925974bSAdrian Prantl baton = 682a925974bSAdrian Prantl bp_loc_sp 683af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 684af26b22cSJim Ingham ->GetBaton(); 685af26b22cSJim Ingham else 686af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 687af26b22cSJim Ingham 688b9c1b51eSKate Stone if (baton) { 689b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 690b9c1b51eSKate Stone id_str.GetData()); 6914f728bfcSRaphael Isemann baton->GetDescription(result.GetOutputStream().AsRawOstream(), 6924f728bfcSRaphael Isemann eDescriptionLevelFull, 6934f728bfcSRaphael Isemann result.GetOutputStream().GetIndentLevel() + 6944f728bfcSRaphael Isemann 2); 695b9c1b51eSKate Stone } else { 696b9c1b51eSKate Stone result.AppendMessageWithFormat( 697b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 698e16c50a1SJim Ingham id_str.GetData()); 69930fdc8d8SChris Lattner } 70030fdc8d8SChris Lattner } 70130fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 702b9c1b51eSKate Stone } else { 703b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 704b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 70530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 70630fdc8d8SChris Lattner } 70730fdc8d8SChris Lattner } 70830fdc8d8SChris Lattner } 70930fdc8d8SChris Lattner 71030fdc8d8SChris Lattner return result.Succeeded(); 71130fdc8d8SChris Lattner } 7125a988416SJim Ingham }; 71330fdc8d8SChris Lattner 71430fdc8d8SChris Lattner // CommandObjectBreakpointCommand 71530fdc8d8SChris Lattner 716b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 717b9c1b51eSKate Stone CommandInterpreter &interpreter) 7187428a18cSKate Stone : CommandObjectMultiword( 719a925974bSAdrian Prantl interpreter, "command", 720a925974bSAdrian Prantl "Commands for adding, removing and listing " 721b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 722b9c1b51eSKate Stone "hit.", 723b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 724b9c1b51eSKate Stone CommandObjectSP add_command_object( 725b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 726b9c1b51eSKate Stone CommandObjectSP delete_command_object( 727b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 728b9c1b51eSKate Stone CommandObjectSP list_command_object( 729b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 73030fdc8d8SChris Lattner 73130fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 73293e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 73330fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 73430fdc8d8SChris Lattner 73523f59509SGreg Clayton LoadSubCommand("add", add_command_object); 73623f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 73723f59509SGreg Clayton LoadSubCommand("list", list_command_object); 73830fdc8d8SChris Lattner } 73930fdc8d8SChris Lattner 740c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 741