130fdc8d8SChris Lattner //===-- CommandObjectBreakpointCommand.cpp ----------------------*- C++ -*-===// 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 224e4fbe82SZachary Turner 2330fdc8d8SChris Lattner using namespace lldb; 2430fdc8d8SChris Lattner using namespace lldb_private; 2530fdc8d8SChris Lattner 261f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 27e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 28e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 298fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 30e063ecccSJonas Devlieghere { 31e063ecccSJonas Devlieghere eScriptLanguageNone, 32e063ecccSJonas Devlieghere "command", 33e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 34e063ecccSJonas Devlieghere }, 35e063ecccSJonas Devlieghere { 36e063ecccSJonas Devlieghere eScriptLanguagePython, 37e063ecccSJonas Devlieghere "python", 38e063ecccSJonas Devlieghere "Commands are in the Python language.", 39e063ecccSJonas Devlieghere }, 40e063ecccSJonas Devlieghere { 41*8983d691SJonas Devlieghere eScriptLanguageLua, 42*8983d691SJonas Devlieghere "lua", 43*8983d691SJonas Devlieghere "Commands are in the Lua language.", 44*8983d691SJonas Devlieghere }, 45*8983d691SJonas Devlieghere { 46*8983d691SJonas Devlieghere eScriptLanguageDefault, 47e063ecccSJonas Devlieghere "default-script", 48e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 49e063ecccSJonas Devlieghere }, 50e063ecccSJonas Devlieghere }; 511f0f5b5bSZachary Turner 528fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 538fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 548fe53c49STatyana Krasnukha } 558fe53c49STatyana Krasnukha 56f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 57f94668e3SRaphael Isemann #include "CommandOptions.inc" 581f0f5b5bSZachary Turner 59b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 60b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 615a988416SJim Ingham public: 627428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 637428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 64b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 65b9c1b51eSKate Stone "whenever the breakpoint is hit." 66b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 67b9c1b51eSKate Stone "commands to the last created breakpoint.", 68c8ecc2a9SEugene Zelenko nullptr), 69b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 70b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 71738af7a6SJim Ingham m_options(), m_func_options("breakpoint command", false, 'F') { 7230fdc8d8SChris Lattner SetHelpLong( 73ea671fbdSKate Stone R"( 74ea671fbdSKate Stone General information about entering breakpoint commands 75ea671fbdSKate Stone ------------------------------------------------------ 76ea671fbdSKate Stone 77b9c1b51eSKate Stone )" 78b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 79ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 80b9c1b51eSKate Stone prompt until 'DONE' is entered." 81b9c1b51eSKate Stone R"( 82ea671fbdSKate Stone 83b9c1b51eSKate Stone )" 84b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 85ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 86b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 87b9c1b51eSKate Stone R"( 88ea671fbdSKate Stone 89b9c1b51eSKate Stone )" 90b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 91ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 92b9c1b51eSKate Stone more than one command per line." 93b9c1b51eSKate Stone R"( 94ea671fbdSKate Stone 95ea671fbdSKate Stone Special information about PYTHON breakpoint commands 96ea671fbdSKate Stone ---------------------------------------------------- 97ea671fbdSKate Stone 98b9c1b51eSKate Stone )" 99b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 100ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 101ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 102ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 103b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 104b9c1b51eSKate Stone R"( 105ea671fbdSKate Stone 106ea671fbdSKate Stone This auto-generated function is passed in three arguments: 107ea671fbdSKate Stone 108ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 109ea671fbdSKate Stone 110ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 111ea671fbdSKate Stone 112ea671fbdSKate Stone dict: the python session dictionary hit. 113ea671fbdSKate Stone 114b9c1b51eSKate Stone )" 115b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 116b9c1b51eSKate Stone to supply the function name prepended by the module name:" 117b9c1b51eSKate Stone R"( 118ea671fbdSKate Stone 119ea671fbdSKate Stone --python-function myutils.breakpoint_callback 120ea671fbdSKate Stone 121ea671fbdSKate Stone The function itself must have the following prototype: 122ea671fbdSKate Stone 123ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 124ea671fbdSKate Stone # Your code goes here 125ea671fbdSKate Stone 126b9c1b51eSKate Stone )" 127b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 128ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 129ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 130ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 131ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 132b9c1b51eSKate Stone via process.GetTarget()." 133b9c1b51eSKate Stone R"( 134ea671fbdSKate Stone 135b9c1b51eSKate Stone )" 136b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 137ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 138b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 139b9c1b51eSKate Stone R"( 140ea671fbdSKate Stone 141ea671fbdSKate Stone Example Python one-line breakpoint command: 142ea671fbdSKate Stone 143ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 144ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 145ea671fbdSKate Stone > print "Hit this breakpoint!" 146ea671fbdSKate Stone > DONE 147ea671fbdSKate Stone 148ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 149ea671fbdSKate Stone 150ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 151ea671fbdSKate Stone (lldb) run 152ea671fbdSKate Stone Launching '.../a.out' (x86_64) 153ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 154ea671fbdSKate Stone Process 21778 Stopped 155ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 156ea671fbdSKate Stone 36 157ea671fbdSKate Stone 37 int c(int val) 158ea671fbdSKate Stone 38 { 159ea671fbdSKate Stone 39 -> return val + 3; 160ea671fbdSKate Stone 40 } 161ea671fbdSKate Stone 41 162ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 163ea671fbdSKate Stone 164ea671fbdSKate Stone Example multiple line Python breakpoint command: 165ea671fbdSKate Stone 166ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 167ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 168ea671fbdSKate Stone > global bp_count 169ea671fbdSKate Stone > bp_count = bp_count + 1 170ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!" 171ea671fbdSKate Stone > DONE 172ea671fbdSKate Stone 173ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition: 174ea671fbdSKate Stone 175ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 176ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 177ea671fbdSKate Stone > def breakpoint_output (bp_no): 178ea671fbdSKate Stone > out_string = "Hit breakpoint number " + repr (bp_no) 179ea671fbdSKate Stone > print out_string 180ea671fbdSKate Stone > return True 181ea671fbdSKate Stone > breakpoint_output (1) 182ea671fbdSKate Stone > DONE 183ea671fbdSKate Stone 184b9c1b51eSKate Stone )" 185b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 186ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 187b9c1b51eSKate Stone initialized:" 188b9c1b51eSKate Stone R"( 189ea671fbdSKate Stone 190ea671fbdSKate Stone (lldb) script 191ea671fbdSKate Stone >>> bp_count = 0 192ea671fbdSKate Stone >>> quit() 193ea671fbdSKate Stone 194b9c1b51eSKate Stone )" 195b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 196ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 197ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 198ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 199b9c1b51eSKate Stone LLDB to stop." 200b9c1b51eSKate Stone R"( 201ea671fbdSKate Stone 202b9c1b51eSKate Stone )" 203b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 204b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 205405fe67fSCaroline Tice 206738af7a6SJim Ingham m_all_options.Append(&m_options); 207738af7a6SJim Ingham m_all_options.Append(&m_func_options, LLDB_OPT_SET_2 | LLDB_OPT_SET_3, 208738af7a6SJim Ingham LLDB_OPT_SET_2); 209738af7a6SJim Ingham m_all_options.Finalize(); 210738af7a6SJim Ingham 211405fe67fSCaroline Tice CommandArgumentEntry arg; 212405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 213405fe67fSCaroline Tice 214405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 215405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2161bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 217405fe67fSCaroline Tice 218b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 219b9c1b51eSKate Stone // argument entry. 220405fe67fSCaroline Tice arg.push_back(bp_id_arg); 221405fe67fSCaroline Tice 222405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 223405fe67fSCaroline Tice m_arguments.push_back(arg); 22430fdc8d8SChris Lattner } 22530fdc8d8SChris Lattner 226c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2275a988416SJim Ingham 228738af7a6SJim Ingham Options *GetOptions() override { return &m_all_options; } 2295a988416SJim Ingham 2300affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2317ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2320affb582SDave Lee if (output_sp && interactive) { 23344d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 23444d93782SGreg Clayton output_sp->Flush(); 23544d93782SGreg Clayton } 23644d93782SGreg Clayton } 23744d93782SGreg Clayton 238b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 239b9c1b51eSKate Stone std::string &line) override { 24044d93782SGreg Clayton io_handler.SetIsDone(true); 24144d93782SGreg Clayton 242b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 243b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 244b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 245b5796cb4SJim Ingham if (!bp_options) 246b5796cb4SJim Ingham continue; 247b5796cb4SJim Ingham 248a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 249e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 25092d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 25144d93782SGreg Clayton } 25244d93782SGreg Clayton } 25344d93782SGreg Clayton 254b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 255b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 256b9c1b51eSKate Stone CommandReturnObject &result) { 257b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 258b9c1b51eSKate Stone "> ", // Prompt 25944d93782SGreg Clayton *this, // IOHandlerDelegate 26044d93782SGreg Clayton true, // Run IOHandler in async mode 261b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 262b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2635a988416SJim Ingham } 2645a988416SJim Ingham 2655a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2665a988416SJim Ingham void 267b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 268b9c1b51eSKate Stone const char *oneliner) { 269b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 270a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 2715a988416SJim Ingham 272e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 273e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2745a988416SJim Ingham 27592d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 276b5796cb4SJim Ingham } 2775a988416SJim Ingham } 2785a988416SJim Ingham 279738af7a6SJim Ingham class CommandOptions : public OptionGroup { 2805a988416SJim Ingham public: 281b9c1b51eSKate Stone CommandOptions() 282738af7a6SJim Ingham : OptionGroup(), m_use_commands(false), m_use_script_language(false), 283b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 284738af7a6SJim Ingham m_one_liner() {} 28530fdc8d8SChris Lattner 286c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2875a988416SJim Ingham 28897206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 289b9c1b51eSKate Stone ExecutionContext *execution_context) override { 29097206d57SZachary Turner Status error; 291a925974bSAdrian Prantl const int short_option = 292a925974bSAdrian Prantl g_breakpoint_command_add_options[option_idx].short_option; 2935a988416SJim Ingham 294b9c1b51eSKate Stone switch (short_option) { 2955a988416SJim Ingham case 'o': 2965a988416SJim Ingham m_use_one_liner = true; 2975a988416SJim Ingham m_one_liner = option_arg; 2985a988416SJim Ingham break; 2995a988416SJim Ingham 3005a988416SJim Ingham case 's': 30147cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 302bd68a052SRaphael Isemann option_arg, 303bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 304b9c1b51eSKate Stone eScriptLanguageNone, error); 305*8983d691SJonas Devlieghere switch (m_script_language) { 306*8983d691SJonas Devlieghere case eScriptLanguagePython: 307*8983d691SJonas Devlieghere case eScriptLanguageLua: 3085a988416SJim Ingham m_use_script_language = true; 309*8983d691SJonas Devlieghere break; 310*8983d691SJonas Devlieghere case eScriptLanguageNone: 3115a988416SJim Ingham m_use_script_language = false; 312*8983d691SJonas Devlieghere break; 3135a988416SJim Ingham } 3145a988416SJim Ingham break; 3155a988416SJim Ingham 316b9c1b51eSKate Stone case 'e': { 3175a988416SJim Ingham bool success = false; 31847cbf4a0SPavel Labath m_stop_on_error = 31947cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3205a988416SJim Ingham if (!success) 321b9c1b51eSKate Stone error.SetErrorStringWithFormat( 322fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 323fe11483bSZachary Turner option_arg.str().c_str()); 324b9c1b51eSKate Stone } break; 3255a988416SJim Ingham 32633df7cd3SJim Ingham case 'D': 32733df7cd3SJim Ingham m_use_dummy = true; 32833df7cd3SJim Ingham break; 32933df7cd3SJim Ingham 3305a988416SJim Ingham default: 33136162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 3325a988416SJim Ingham } 3335a988416SJim Ingham return error; 3345a988416SJim Ingham } 335c8ecc2a9SEugene Zelenko 336b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3375a988416SJim Ingham m_use_commands = true; 3385a988416SJim Ingham m_use_script_language = false; 3395a988416SJim Ingham m_script_language = eScriptLanguageNone; 3405a988416SJim Ingham 3415a988416SJim Ingham m_use_one_liner = false; 3425a988416SJim Ingham m_stop_on_error = true; 3435a988416SJim Ingham m_one_liner.clear(); 34433df7cd3SJim Ingham m_use_dummy = false; 3455a988416SJim Ingham } 3465a988416SJim Ingham 3471f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 348bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_add_options); 3491f0f5b5bSZachary Turner } 3505a988416SJim Ingham 3515a988416SJim Ingham // Instance variables to hold the values for command options. 3525a988416SJim Ingham 3535a988416SJim Ingham bool m_use_commands; 3545a988416SJim Ingham bool m_use_script_language; 3555a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3565a988416SJim Ingham 3575a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3585a988416SJim Ingham bool m_use_one_liner; 3595a988416SJim Ingham std::string m_one_liner; 3605a988416SJim Ingham bool m_stop_on_error; 36133df7cd3SJim Ingham bool m_use_dummy; 3625a988416SJim Ingham }; 3635a988416SJim Ingham 3645a988416SJim Ingham protected: 365b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 366cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 36730fdc8d8SChris Lattner 368cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 36930fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 37030fdc8d8SChris Lattner 371b9c1b51eSKate Stone if (num_breakpoints == 0) { 37230fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37430fdc8d8SChris Lattner return false; 37530fdc8d8SChris Lattner } 37630fdc8d8SChris Lattner 377738af7a6SJim Ingham if (!m_func_options.GetName().empty()) { 378738af7a6SJim Ingham m_options.m_use_one_liner = false; 379738af7a6SJim Ingham m_options.m_use_script_language = true; 3808d4a8010SEnrico Granata } 3818d4a8010SEnrico Granata 38230fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 383b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 384cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 385b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 38630fdc8d8SChris Lattner 387b5796cb4SJim Ingham m_bp_options_vec.clear(); 388b5796cb4SJim Ingham 389b9c1b51eSKate Stone if (result.Succeeded()) { 390c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 391d9916eaeSJim Ingham 392b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 39330fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 394b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 395b9c1b51eSKate Stone Breakpoint *bp = 396cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 397c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 398b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 39939d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 40039d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 401b9c1b51eSKate Stone } else { 402b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 403b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 40405097246SAdrian Prantl // This breakpoint does have an associated location. Get its 40505097246SAdrian Prantl // breakpoint options. 40630fdc8d8SChris Lattner if (bp_loc_sp) 40739d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 40839d7d4f0SJohnny Chen } 409b5796cb4SJim Ingham if (bp_options) 410b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 411b5796cb4SJim Ingham } 412b5796cb4SJim Ingham } 41339d7d4f0SJohnny Chen 41405097246SAdrian Prantl // If we are using script language, get the script interpreter in order 41505097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 41605097246SAdrian Prantl // associated with this object. 417b9c1b51eSKate Stone if (m_options.m_use_script_language) { 4182b29b432SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(); 41939d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 420b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 421b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallback( 422b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 423738af7a6SJim Ingham } else if (!m_func_options.GetName().empty()) { 424738af7a6SJim Ingham Status error = script_interp->SetBreakpointCommandCallbackFunction( 425738af7a6SJim Ingham m_bp_options_vec, m_func_options.GetName().c_str(), 426738af7a6SJim Ingham m_func_options.GetStructuredData()); 427738af7a6SJim Ingham if (!error.Success()) 428738af7a6SJim Ingham result.SetError(error); 429b9c1b51eSKate Stone } else { 430b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 431b9c1b51eSKate Stone m_bp_options_vec, result); 4328d4a8010SEnrico Granata } 433b9c1b51eSKate Stone } else { 43439d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 43539d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 436b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 43739d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 43839d7d4f0SJohnny Chen else 439b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 44030fdc8d8SChris Lattner } 44130fdc8d8SChris Lattner } 44230fdc8d8SChris Lattner 44330fdc8d8SChris Lattner return result.Succeeded(); 44430fdc8d8SChris Lattner } 44530fdc8d8SChris Lattner 4465a988416SJim Ingham private: 4475a988416SJim Ingham CommandOptions m_options; 448738af7a6SJim Ingham OptionGroupPythonClassWithDict m_func_options; 449738af7a6SJim Ingham OptionGroupOptions m_all_options; 450738af7a6SJim Ingham 451b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 452b9c1b51eSKate Stone // breakpoint options that 453b9c1b51eSKate Stone // we are currently 45405097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45505097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45605097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45705097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 45805097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 45905097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 46005097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 46105097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 46205097246SAdrian Prantl // right now. 4635a988416SJim Ingham static const char *g_reader_instructions; 4645a988416SJim Ingham }; 4655a988416SJim Ingham 466b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 467b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4685a988416SJim Ingham 46993e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 47030fdc8d8SChris Lattner 471f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 472f94668e3SRaphael Isemann #include "CommandOptions.inc" 4731f0f5b5bSZachary Turner 474b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4755a988416SJim Ingham public: 476b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 477b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 47893e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 479c8ecc2a9SEugene Zelenko nullptr), 480b9c1b51eSKate Stone m_options() { 481405fe67fSCaroline Tice CommandArgumentEntry arg; 482405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 483405fe67fSCaroline Tice 484405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 485405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 486405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 487405fe67fSCaroline Tice 488b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 489b9c1b51eSKate Stone // argument entry. 490405fe67fSCaroline Tice arg.push_back(bp_id_arg); 491405fe67fSCaroline Tice 492405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 493405fe67fSCaroline Tice m_arguments.push_back(arg); 49430fdc8d8SChris Lattner } 49530fdc8d8SChris Lattner 496c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4975a988416SJim Ingham 498b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 49933df7cd3SJim Ingham 500b9c1b51eSKate Stone class CommandOptions : public Options { 50133df7cd3SJim Ingham public: 502b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 50333df7cd3SJim Ingham 504c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50533df7cd3SJim Ingham 50697206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 507b9c1b51eSKate Stone ExecutionContext *execution_context) override { 50897206d57SZachary Turner Status error; 50933df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 51033df7cd3SJim Ingham 511b9c1b51eSKate Stone switch (short_option) { 51233df7cd3SJim Ingham case 'D': 51333df7cd3SJim Ingham m_use_dummy = true; 51433df7cd3SJim Ingham break; 51533df7cd3SJim Ingham 51633df7cd3SJim Ingham default: 51736162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 51833df7cd3SJim Ingham } 51933df7cd3SJim Ingham 52033df7cd3SJim Ingham return error; 52133df7cd3SJim Ingham } 52233df7cd3SJim Ingham 523b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 52433df7cd3SJim Ingham m_use_dummy = false; 52533df7cd3SJim Ingham } 52633df7cd3SJim Ingham 5271f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 528bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_delete_options); 5291f0f5b5bSZachary Turner } 53033df7cd3SJim Ingham 53133df7cd3SJim Ingham // Instance variables to hold the values for command options. 53233df7cd3SJim Ingham bool m_use_dummy; 53333df7cd3SJim Ingham }; 53433df7cd3SJim Ingham 5355a988416SJim Ingham protected: 536b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 537cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 53830fdc8d8SChris Lattner 539cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 54030fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 54130fdc8d8SChris Lattner 542b9c1b51eSKate Stone if (num_breakpoints == 0) { 54393e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 54430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54530fdc8d8SChris Lattner return false; 54630fdc8d8SChris Lattner } 54730fdc8d8SChris Lattner 54811eb9c64SZachary Turner if (command.empty()) { 549b9c1b51eSKate Stone result.AppendError( 550b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 55130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55230fdc8d8SChris Lattner return false; 55330fdc8d8SChris Lattner } 55430fdc8d8SChris Lattner 55530fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 556b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 557cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 558b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 55930fdc8d8SChris Lattner 560b9c1b51eSKate Stone if (result.Succeeded()) { 561c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 562b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 56330fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 564b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 565b9c1b51eSKate Stone Breakpoint *bp = 566cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 567b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 568b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 569b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 57030fdc8d8SChris Lattner if (bp_loc_sp) 57130fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 572b9c1b51eSKate Stone else { 57330fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 57430fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 57530fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 57630fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 57730fdc8d8SChris Lattner return false; 57830fdc8d8SChris Lattner } 579b9c1b51eSKate Stone } else { 58030fdc8d8SChris Lattner bp->ClearCallback(); 58130fdc8d8SChris Lattner } 58230fdc8d8SChris Lattner } 58330fdc8d8SChris Lattner } 58430fdc8d8SChris Lattner } 58530fdc8d8SChris Lattner return result.Succeeded(); 58630fdc8d8SChris Lattner } 587c8ecc2a9SEugene Zelenko 58833df7cd3SJim Ingham private: 58933df7cd3SJim Ingham CommandOptions m_options; 5905a988416SJim Ingham }; 59130fdc8d8SChris Lattner 59230fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 59330fdc8d8SChris Lattner 594b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 5955a988416SJim Ingham public: 596b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 59704a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 59804a4c091SRaphael Isemann "List the script or set of commands to be " 59904a4c091SRaphael Isemann "executed when the breakpoint is hit.", 60004a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 601405fe67fSCaroline Tice CommandArgumentEntry arg; 602405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 603405fe67fSCaroline Tice 604405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 605405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 606405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 607405fe67fSCaroline Tice 608b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 609b9c1b51eSKate Stone // argument entry. 610405fe67fSCaroline Tice arg.push_back(bp_id_arg); 611405fe67fSCaroline Tice 612405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 613405fe67fSCaroline Tice m_arguments.push_back(arg); 61430fdc8d8SChris Lattner } 61530fdc8d8SChris Lattner 616c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 61730fdc8d8SChris Lattner 6185a988416SJim Ingham protected: 619b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 62004a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 62130fdc8d8SChris Lattner 62230fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 62330fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 62430fdc8d8SChris Lattner 625b9c1b51eSKate Stone if (num_breakpoints == 0) { 62630fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 62730fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 62830fdc8d8SChris Lattner return false; 62930fdc8d8SChris Lattner } 63030fdc8d8SChris Lattner 63111eb9c64SZachary Turner if (command.empty()) { 632b9c1b51eSKate Stone result.AppendError( 633b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 63430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63530fdc8d8SChris Lattner return false; 63630fdc8d8SChris Lattner } 63730fdc8d8SChris Lattner 63830fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 639b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 640b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 641b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 64230fdc8d8SChris Lattner 643b9c1b51eSKate Stone if (result.Succeeded()) { 644c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 645b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 64630fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 647b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 648b9c1b51eSKate Stone Breakpoint *bp = 649b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 65030fdc8d8SChris Lattner 651b9c1b51eSKate Stone if (bp) { 652af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 653b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 654af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 655a925974bSAdrian Prantl if (!bp_loc_sp) { 65630fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 65730fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 65830fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 65930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 66030fdc8d8SChris Lattner return false; 66130fdc8d8SChris Lattner } 66230fdc8d8SChris Lattner } 66330fdc8d8SChris Lattner 66430fdc8d8SChris Lattner StreamString id_str; 665e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 666e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 667e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 668af26b22cSJim Ingham const Baton *baton = nullptr; 669af26b22cSJim Ingham if (bp_loc_sp) 670a925974bSAdrian Prantl baton = 671a925974bSAdrian Prantl bp_loc_sp 672af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 673af26b22cSJim Ingham ->GetBaton(); 674af26b22cSJim Ingham else 675af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 676af26b22cSJim Ingham 677b9c1b51eSKate Stone if (baton) { 678b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 679b9c1b51eSKate Stone id_str.GetData()); 6804f728bfcSRaphael Isemann baton->GetDescription(result.GetOutputStream().AsRawOstream(), 6814f728bfcSRaphael Isemann eDescriptionLevelFull, 6824f728bfcSRaphael Isemann result.GetOutputStream().GetIndentLevel() + 6834f728bfcSRaphael Isemann 2); 684b9c1b51eSKate Stone } else { 685b9c1b51eSKate Stone result.AppendMessageWithFormat( 686b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 687e16c50a1SJim Ingham id_str.GetData()); 68830fdc8d8SChris Lattner } 68930fdc8d8SChris Lattner } 69030fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 691b9c1b51eSKate Stone } else { 692b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 693b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 69430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 69530fdc8d8SChris Lattner } 69630fdc8d8SChris Lattner } 69730fdc8d8SChris Lattner } 69830fdc8d8SChris Lattner 69930fdc8d8SChris Lattner return result.Succeeded(); 70030fdc8d8SChris Lattner } 7015a988416SJim Ingham }; 70230fdc8d8SChris Lattner 70330fdc8d8SChris Lattner // CommandObjectBreakpointCommand 70430fdc8d8SChris Lattner 705b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 706b9c1b51eSKate Stone CommandInterpreter &interpreter) 7077428a18cSKate Stone : CommandObjectMultiword( 708a925974bSAdrian Prantl interpreter, "command", 709a925974bSAdrian Prantl "Commands for adding, removing and listing " 710b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 711b9c1b51eSKate Stone "hit.", 712b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 713b9c1b51eSKate Stone CommandObjectSP add_command_object( 714b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 715b9c1b51eSKate Stone CommandObjectSP delete_command_object( 716b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 717b9c1b51eSKate Stone CommandObjectSP list_command_object( 718b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 71930fdc8d8SChris Lattner 72030fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 72193e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 72230fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 72330fdc8d8SChris Lattner 72423f59509SGreg Clayton LoadSubCommand("add", add_command_object); 72523f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 72623f59509SGreg Clayton LoadSubCommand("list", list_command_object); 72730fdc8d8SChris Lattner } 72830fdc8d8SChris Lattner 729c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 730