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" 14b9c1b51eSKate Stone #include "lldb/Breakpoint/StoppointCallbackContext.h" 1544d93782SGreg Clayton #include "lldb/Core/IOHandler.h" 163eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h" 1830fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 1947cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 2030fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2130fdc8d8SChris Lattner #include "lldb/Target/Thread.h" 22d821c997SPavel Labath #include "lldb/Utility/State.h" 2330fdc8d8SChris Lattner 244e4fbe82SZachary Turner #include "llvm/ADT/STLExtras.h" 254e4fbe82SZachary Turner 2630fdc8d8SChris Lattner using namespace lldb; 2730fdc8d8SChris Lattner using namespace lldb_private; 2830fdc8d8SChris Lattner 2930fdc8d8SChris Lattner // CommandObjectBreakpointCommandAdd 3030fdc8d8SChris Lattner 311f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 321f0f5b5bSZachary Turner // somebody can add a new scripting 331f0f5b5bSZachary Turner // language to lldb and have it pickable here without having to change this 341f0f5b5bSZachary Turner // enumeration by hand and rebuild lldb proper. 351f0f5b5bSZachary Turner 368fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 371f0f5b5bSZachary Turner {eScriptLanguageNone, "command", 381f0f5b5bSZachary Turner "Commands are in the lldb command interpreter language"}, 391f0f5b5bSZachary Turner {eScriptLanguagePython, "python", "Commands are in the Python language."}, 401f0f5b5bSZachary Turner {eSortOrderByName, "default-script", 418fe53c49STatyana Krasnukha "Commands are in the default scripting language."} }; 421f0f5b5bSZachary Turner 438fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 448fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 458fe53c49STatyana Krasnukha } 468fe53c49STatyana Krasnukha 478fe53c49STatyana Krasnukha static constexpr OptionDefinition g_breakpoint_add_options[] = { 48*f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 49*f94668e3SRaphael Isemann #include "CommandOptions.inc" 501f0f5b5bSZachary Turner }; 511f0f5b5bSZachary Turner 52b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 53b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 545a988416SJim Ingham public: 557428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 567428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 57b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 58b9c1b51eSKate Stone "whenever the breakpoint is hit." 59b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 60b9c1b51eSKate Stone "commands to the last created breakpoint.", 61c8ecc2a9SEugene Zelenko nullptr), 62b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 63b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 64b9c1b51eSKate Stone m_options() { 6530fdc8d8SChris Lattner SetHelpLong( 66ea671fbdSKate Stone R"( 67ea671fbdSKate Stone General information about entering breakpoint commands 68ea671fbdSKate Stone ------------------------------------------------------ 69ea671fbdSKate Stone 70b9c1b51eSKate Stone )" 71b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 72ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 73b9c1b51eSKate Stone prompt until 'DONE' is entered." 74b9c1b51eSKate Stone R"( 75ea671fbdSKate Stone 76b9c1b51eSKate Stone )" 77b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 78ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 79b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 80b9c1b51eSKate Stone R"( 81ea671fbdSKate Stone 82b9c1b51eSKate Stone )" 83b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 84ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 85b9c1b51eSKate Stone more than one command per line." 86b9c1b51eSKate Stone R"( 87ea671fbdSKate Stone 88ea671fbdSKate Stone Special information about PYTHON breakpoint commands 89ea671fbdSKate Stone ---------------------------------------------------- 90ea671fbdSKate Stone 91b9c1b51eSKate Stone )" 92b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 93ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 94ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 95ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 96b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 97b9c1b51eSKate Stone R"( 98ea671fbdSKate Stone 99ea671fbdSKate Stone This auto-generated function is passed in three arguments: 100ea671fbdSKate Stone 101ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 102ea671fbdSKate Stone 103ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 104ea671fbdSKate Stone 105ea671fbdSKate Stone dict: the python session dictionary hit. 106ea671fbdSKate Stone 107b9c1b51eSKate Stone )" 108b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 109b9c1b51eSKate Stone to supply the function name prepended by the module name:" 110b9c1b51eSKate Stone R"( 111ea671fbdSKate Stone 112ea671fbdSKate Stone --python-function myutils.breakpoint_callback 113ea671fbdSKate Stone 114ea671fbdSKate Stone The function itself must have the following prototype: 115ea671fbdSKate Stone 116ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 117ea671fbdSKate Stone # Your code goes here 118ea671fbdSKate Stone 119b9c1b51eSKate Stone )" 120b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 121ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 122ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 123ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 124ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 125b9c1b51eSKate Stone via process.GetTarget()." 126b9c1b51eSKate Stone R"( 127ea671fbdSKate Stone 128b9c1b51eSKate Stone )" 129b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 130ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 131b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 132b9c1b51eSKate Stone R"( 133ea671fbdSKate Stone 134ea671fbdSKate Stone Example Python one-line breakpoint command: 135ea671fbdSKate Stone 136ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 137ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 138ea671fbdSKate Stone > print "Hit this breakpoint!" 139ea671fbdSKate Stone > DONE 140ea671fbdSKate Stone 141ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 142ea671fbdSKate Stone 143ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 144ea671fbdSKate Stone (lldb) run 145ea671fbdSKate Stone Launching '.../a.out' (x86_64) 146ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 147ea671fbdSKate Stone Process 21778 Stopped 148ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 149ea671fbdSKate Stone 36 150ea671fbdSKate Stone 37 int c(int val) 151ea671fbdSKate Stone 38 { 152ea671fbdSKate Stone 39 -> return val + 3; 153ea671fbdSKate Stone 40 } 154ea671fbdSKate Stone 41 155ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 156ea671fbdSKate Stone 157ea671fbdSKate Stone Example multiple line Python breakpoint command: 158ea671fbdSKate Stone 159ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 160ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 161ea671fbdSKate Stone > global bp_count 162ea671fbdSKate Stone > bp_count = bp_count + 1 163ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!" 164ea671fbdSKate Stone > DONE 165ea671fbdSKate Stone 166ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition: 167ea671fbdSKate Stone 168ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 169ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 170ea671fbdSKate Stone > def breakpoint_output (bp_no): 171ea671fbdSKate Stone > out_string = "Hit breakpoint number " + repr (bp_no) 172ea671fbdSKate Stone > print out_string 173ea671fbdSKate Stone > return True 174ea671fbdSKate Stone > breakpoint_output (1) 175ea671fbdSKate Stone > DONE 176ea671fbdSKate Stone 177b9c1b51eSKate Stone )" 178b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 179ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 180b9c1b51eSKate Stone initialized:" 181b9c1b51eSKate Stone R"( 182ea671fbdSKate Stone 183ea671fbdSKate Stone (lldb) script 184ea671fbdSKate Stone >>> bp_count = 0 185ea671fbdSKate Stone >>> quit() 186ea671fbdSKate Stone 187b9c1b51eSKate Stone )" 188b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 189ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 190ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 191ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 192b9c1b51eSKate Stone LLDB to stop." 193b9c1b51eSKate Stone R"( 194ea671fbdSKate Stone 195b9c1b51eSKate Stone )" 196b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 197b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 198405fe67fSCaroline Tice 199405fe67fSCaroline Tice CommandArgumentEntry arg; 200405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 201405fe67fSCaroline Tice 202405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 203405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2041bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 205405fe67fSCaroline Tice 206b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 207b9c1b51eSKate Stone // argument entry. 208405fe67fSCaroline Tice arg.push_back(bp_id_arg); 209405fe67fSCaroline Tice 210405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 211405fe67fSCaroline Tice m_arguments.push_back(arg); 21230fdc8d8SChris Lattner } 21330fdc8d8SChris Lattner 214c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2155a988416SJim Ingham 216b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 2175a988416SJim Ingham 2180affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 21944d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 2200affb582SDave Lee if (output_sp && interactive) { 22144d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 22244d93782SGreg Clayton output_sp->Flush(); 22344d93782SGreg Clayton } 22444d93782SGreg Clayton } 22544d93782SGreg Clayton 226b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 227b9c1b51eSKate Stone std::string &line) override { 22844d93782SGreg Clayton io_handler.SetIsDone(true); 22944d93782SGreg Clayton 230b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 231b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 232b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 233b5796cb4SJim Ingham if (!bp_options) 234b5796cb4SJim Ingham continue; 235b5796cb4SJim Ingham 2364e4fbe82SZachary Turner auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 237e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 23892d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 23944d93782SGreg Clayton } 24044d93782SGreg Clayton } 24144d93782SGreg Clayton 242b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 243b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 244b9c1b51eSKate Stone CommandReturnObject &result) { 245b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 246b9c1b51eSKate Stone "> ", // Prompt 24744d93782SGreg Clayton *this, // IOHandlerDelegate 24844d93782SGreg Clayton true, // Run IOHandler in async mode 249b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 250b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2515a988416SJim Ingham } 2525a988416SJim Ingham 2535a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2545a988416SJim Ingham void 255b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 256b9c1b51eSKate Stone const char *oneliner) { 257b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 2584e4fbe82SZachary Turner auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 2595a988416SJim Ingham 260e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 261e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2625a988416SJim Ingham 26392d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 264b5796cb4SJim Ingham } 2655a988416SJim Ingham } 2665a988416SJim Ingham 267b9c1b51eSKate Stone class CommandOptions : public Options { 2685a988416SJim Ingham public: 269b9c1b51eSKate Stone CommandOptions() 270b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 271b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 272b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 27330fdc8d8SChris Lattner 274c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2755a988416SJim Ingham 27697206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 277b9c1b51eSKate Stone ExecutionContext *execution_context) override { 27897206d57SZachary Turner Status error; 2793bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 2805a988416SJim Ingham 281b9c1b51eSKate Stone switch (short_option) { 2825a988416SJim Ingham case 'o': 2835a988416SJim Ingham m_use_one_liner = true; 2845a988416SJim Ingham m_one_liner = option_arg; 2855a988416SJim Ingham break; 2865a988416SJim Ingham 2875a988416SJim Ingham case 's': 28847cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 289fe11483bSZachary Turner option_arg, g_breakpoint_add_options[option_idx].enum_values, 290b9c1b51eSKate Stone eScriptLanguageNone, error); 2915a988416SJim Ingham 292b9c1b51eSKate Stone if (m_script_language == eScriptLanguagePython || 293b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault) { 2945a988416SJim Ingham m_use_script_language = true; 295b9c1b51eSKate Stone } else { 2965a988416SJim Ingham m_use_script_language = false; 2975a988416SJim Ingham } 2985a988416SJim Ingham break; 2995a988416SJim Ingham 300b9c1b51eSKate Stone case 'e': { 3015a988416SJim Ingham bool success = false; 30247cbf4a0SPavel Labath m_stop_on_error = 30347cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3045a988416SJim Ingham if (!success) 305b9c1b51eSKate Stone error.SetErrorStringWithFormat( 306fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 307fe11483bSZachary Turner option_arg.str().c_str()); 308b9c1b51eSKate Stone } break; 3095a988416SJim Ingham 3105a988416SJim Ingham case 'F': 3115a988416SJim Ingham m_use_one_liner = false; 3125a988416SJim Ingham m_use_script_language = true; 3135a988416SJim Ingham m_function_name.assign(option_arg); 3145a988416SJim Ingham break; 3155a988416SJim Ingham 31633df7cd3SJim Ingham case 'D': 31733df7cd3SJim Ingham m_use_dummy = true; 31833df7cd3SJim Ingham break; 31933df7cd3SJim Ingham 3205a988416SJim Ingham default: 3215a988416SJim Ingham break; 3225a988416SJim Ingham } 3235a988416SJim Ingham return error; 3245a988416SJim Ingham } 325c8ecc2a9SEugene Zelenko 326b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3275a988416SJim Ingham m_use_commands = true; 3285a988416SJim Ingham m_use_script_language = false; 3295a988416SJim Ingham m_script_language = eScriptLanguageNone; 3305a988416SJim Ingham 3315a988416SJim Ingham m_use_one_liner = false; 3325a988416SJim Ingham m_stop_on_error = true; 3335a988416SJim Ingham m_one_liner.clear(); 3345a988416SJim Ingham m_function_name.clear(); 33533df7cd3SJim Ingham m_use_dummy = false; 3365a988416SJim Ingham } 3375a988416SJim Ingham 3381f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 33970602439SZachary Turner return llvm::makeArrayRef(g_breakpoint_add_options); 3401f0f5b5bSZachary Turner } 3415a988416SJim Ingham 3425a988416SJim Ingham // Instance variables to hold the values for command options. 3435a988416SJim Ingham 3445a988416SJim Ingham bool m_use_commands; 3455a988416SJim Ingham bool m_use_script_language; 3465a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3475a988416SJim Ingham 3485a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3495a988416SJim Ingham bool m_use_one_liner; 3505a988416SJim Ingham std::string m_one_liner; 3515a988416SJim Ingham bool m_stop_on_error; 3525a988416SJim Ingham std::string m_function_name; 35333df7cd3SJim Ingham bool m_use_dummy; 3545a988416SJim Ingham }; 3555a988416SJim Ingham 3565a988416SJim Ingham protected: 357b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 35833df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 35930fdc8d8SChris Lattner 360b9c1b51eSKate Stone if (target == nullptr) { 361b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 362b9c1b51eSKate Stone "breakpoints to which to add commands"); 36330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 36430fdc8d8SChris Lattner return false; 36530fdc8d8SChris Lattner } 36630fdc8d8SChris Lattner 36730fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 36830fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 36930fdc8d8SChris Lattner 370b9c1b51eSKate Stone if (num_breakpoints == 0) { 37130fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37330fdc8d8SChris Lattner return false; 37430fdc8d8SChris Lattner } 37530fdc8d8SChris Lattner 376b9c1b51eSKate Stone if (!m_options.m_use_script_language && 377b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 378b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 379b9c1b51eSKate Stone "breakpoint command"); 3808d4a8010SEnrico Granata result.SetStatus(eReturnStatusFailed); 3818d4a8010SEnrico Granata return false; 3828d4a8010SEnrico Granata } 3838d4a8010SEnrico Granata 38430fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 385b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 386b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 387b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 38830fdc8d8SChris Lattner 389b5796cb4SJim Ingham m_bp_options_vec.clear(); 390b5796cb4SJim Ingham 391b9c1b51eSKate Stone if (result.Succeeded()) { 392c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 393d9916eaeSJim Ingham 394b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 39530fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 396b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 397b9c1b51eSKate Stone Breakpoint *bp = 398b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 399c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 400b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 40139d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 40239d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 403b9c1b51eSKate Stone } else { 404b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 405b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 40605097246SAdrian Prantl // This breakpoint does have an associated location. Get its 40705097246SAdrian Prantl // breakpoint options. 40830fdc8d8SChris Lattner if (bp_loc_sp) 40939d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 41039d7d4f0SJohnny Chen } 411b5796cb4SJim Ingham if (bp_options) 412b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 413b5796cb4SJim Ingham } 414b5796cb4SJim Ingham } 41539d7d4f0SJohnny Chen 41605097246SAdrian Prantl // If we are using script language, get the script interpreter in order 41705097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 41805097246SAdrian Prantl // associated with this object. 419b9c1b51eSKate Stone if (m_options.m_use_script_language) { 4202b29b432SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(); 42139d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 422b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 423b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallback( 424b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 425b9c1b51eSKate Stone } else if (!m_options.m_function_name.empty()) { 426b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallbackFunction( 427b9c1b51eSKate Stone m_bp_options_vec, m_options.m_function_name.c_str()); 428b9c1b51eSKate Stone } else { 429b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 430b9c1b51eSKate Stone m_bp_options_vec, result); 4318d4a8010SEnrico Granata } 432b9c1b51eSKate Stone } else { 43339d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 43439d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 435b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 43639d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 43739d7d4f0SJohnny Chen else 438b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 43930fdc8d8SChris Lattner } 44030fdc8d8SChris Lattner } 44130fdc8d8SChris Lattner 44230fdc8d8SChris Lattner return result.Succeeded(); 44330fdc8d8SChris Lattner } 44430fdc8d8SChris Lattner 4455a988416SJim Ingham private: 4465a988416SJim Ingham CommandOptions m_options; 447b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 448b9c1b51eSKate Stone // breakpoint options that 449b9c1b51eSKate Stone // we are currently 45005097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45105097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45205097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45305097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 45405097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 45505097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 45605097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 45705097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 45805097246SAdrian Prantl // right now. 4595a988416SJim Ingham static const char *g_reader_instructions; 4605a988416SJim Ingham }; 4615a988416SJim Ingham 462b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 463b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4645a988416SJim Ingham 46593e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 46630fdc8d8SChris Lattner 4678fe53c49STatyana Krasnukha static constexpr OptionDefinition g_breakpoint_delete_options[] = { 468*f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 469*f94668e3SRaphael Isemann #include "CommandOptions.inc" 4701f0f5b5bSZachary Turner }; 4711f0f5b5bSZachary Turner 472b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4735a988416SJim Ingham public: 474b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 475b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 47693e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 477c8ecc2a9SEugene Zelenko nullptr), 478b9c1b51eSKate Stone m_options() { 479405fe67fSCaroline Tice CommandArgumentEntry arg; 480405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 481405fe67fSCaroline Tice 482405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 483405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 484405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 485405fe67fSCaroline Tice 486b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 487b9c1b51eSKate Stone // argument entry. 488405fe67fSCaroline Tice arg.push_back(bp_id_arg); 489405fe67fSCaroline Tice 490405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 491405fe67fSCaroline Tice m_arguments.push_back(arg); 49230fdc8d8SChris Lattner } 49330fdc8d8SChris Lattner 494c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4955a988416SJim Ingham 496b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 49733df7cd3SJim Ingham 498b9c1b51eSKate Stone class CommandOptions : public Options { 49933df7cd3SJim Ingham public: 500b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 50133df7cd3SJim Ingham 502c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50333df7cd3SJim Ingham 50497206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 505b9c1b51eSKate Stone ExecutionContext *execution_context) override { 50697206d57SZachary Turner Status error; 50733df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 50833df7cd3SJim Ingham 509b9c1b51eSKate Stone switch (short_option) { 51033df7cd3SJim Ingham case 'D': 51133df7cd3SJim Ingham m_use_dummy = true; 51233df7cd3SJim Ingham break; 51333df7cd3SJim Ingham 51433df7cd3SJim Ingham default: 515b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 516b9c1b51eSKate Stone short_option); 51733df7cd3SJim Ingham break; 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 { 52870602439SZachary Turner return llvm::makeArrayRef(g_breakpoint_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 { 53733df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 53830fdc8d8SChris Lattner 539b9c1b51eSKate Stone if (target == nullptr) { 540b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 541b9c1b51eSKate Stone "breakpoints from which to delete commands"); 54230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54330fdc8d8SChris Lattner return false; 54430fdc8d8SChris Lattner } 54530fdc8d8SChris Lattner 54630fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 54730fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 54830fdc8d8SChris Lattner 549b9c1b51eSKate Stone if (num_breakpoints == 0) { 55093e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 55130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55230fdc8d8SChris Lattner return false; 55330fdc8d8SChris Lattner } 55430fdc8d8SChris Lattner 55511eb9c64SZachary Turner if (command.empty()) { 556b9c1b51eSKate Stone result.AppendError( 557b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 55830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55930fdc8d8SChris Lattner return false; 56030fdc8d8SChris Lattner } 56130fdc8d8SChris Lattner 56230fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 563b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 564b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 565b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 56630fdc8d8SChris Lattner 567b9c1b51eSKate Stone if (result.Succeeded()) { 568c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 569b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 57030fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 571b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 572b9c1b51eSKate Stone Breakpoint *bp = 573b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 574b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 575b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 576b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 57730fdc8d8SChris Lattner if (bp_loc_sp) 57830fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 579b9c1b51eSKate Stone else { 58030fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 58130fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 58230fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 58330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 58430fdc8d8SChris Lattner return false; 58530fdc8d8SChris Lattner } 586b9c1b51eSKate Stone } else { 58730fdc8d8SChris Lattner bp->ClearCallback(); 58830fdc8d8SChris Lattner } 58930fdc8d8SChris Lattner } 59030fdc8d8SChris Lattner } 59130fdc8d8SChris Lattner } 59230fdc8d8SChris Lattner return result.Succeeded(); 59330fdc8d8SChris Lattner } 594c8ecc2a9SEugene Zelenko 59533df7cd3SJim Ingham private: 59633df7cd3SJim Ingham CommandOptions m_options; 5975a988416SJim Ingham }; 59830fdc8d8SChris Lattner 59930fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 60030fdc8d8SChris Lattner 601b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 6025a988416SJim Ingham public: 603b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 604b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "list", "List the script or set of " 605b9c1b51eSKate Stone "commands to be executed when " 606b9c1b51eSKate Stone "the breakpoint is hit.", 607b9c1b51eSKate Stone nullptr) { 608405fe67fSCaroline Tice CommandArgumentEntry arg; 609405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 610405fe67fSCaroline Tice 611405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 612405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 613405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 614405fe67fSCaroline Tice 615b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 616b9c1b51eSKate Stone // argument entry. 617405fe67fSCaroline Tice arg.push_back(bp_id_arg); 618405fe67fSCaroline Tice 619405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 620405fe67fSCaroline Tice m_arguments.push_back(arg); 62130fdc8d8SChris Lattner } 62230fdc8d8SChris Lattner 623c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 62430fdc8d8SChris Lattner 6255a988416SJim Ingham protected: 626b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 62757179860SJonas Devlieghere Target *target = GetDebugger().GetSelectedTarget().get(); 62830fdc8d8SChris Lattner 629b9c1b51eSKate Stone if (target == nullptr) { 630b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 631b9c1b51eSKate Stone "breakpoints for which to list commands"); 63230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63330fdc8d8SChris Lattner return false; 63430fdc8d8SChris Lattner } 63530fdc8d8SChris Lattner 63630fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 63730fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 63830fdc8d8SChris Lattner 639b9c1b51eSKate Stone if (num_breakpoints == 0) { 64030fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 64130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 64230fdc8d8SChris Lattner return false; 64330fdc8d8SChris Lattner } 64430fdc8d8SChris Lattner 64511eb9c64SZachary Turner if (command.empty()) { 646b9c1b51eSKate Stone result.AppendError( 647b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 64830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 64930fdc8d8SChris Lattner return false; 65030fdc8d8SChris Lattner } 65130fdc8d8SChris Lattner 65230fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 653b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 654b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 655b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 65630fdc8d8SChris Lattner 657b9c1b51eSKate Stone if (result.Succeeded()) { 658c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 659b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 66030fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 661b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 662b9c1b51eSKate Stone Breakpoint *bp = 663b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 66430fdc8d8SChris Lattner 665b9c1b51eSKate Stone if (bp) { 666af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 667b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 668af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 669af26b22cSJim Ingham if (!bp_loc_sp) 670af26b22cSJim Ingham { 67130fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 67230fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 67330fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 67430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 67530fdc8d8SChris Lattner return false; 67630fdc8d8SChris Lattner } 67730fdc8d8SChris Lattner } 67830fdc8d8SChris Lattner 67930fdc8d8SChris Lattner StreamString id_str; 680e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 681e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 682e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 683af26b22cSJim Ingham const Baton *baton = nullptr; 684af26b22cSJim Ingham if (bp_loc_sp) 685af26b22cSJim Ingham baton = bp_loc_sp 686af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 687af26b22cSJim Ingham ->GetBaton(); 688af26b22cSJim Ingham else 689af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 690af26b22cSJim Ingham 691b9c1b51eSKate Stone if (baton) { 692b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 693b9c1b51eSKate Stone id_str.GetData()); 69430fdc8d8SChris Lattner result.GetOutputStream().IndentMore(); 695b9c1b51eSKate Stone baton->GetDescription(&result.GetOutputStream(), 696b9c1b51eSKate Stone eDescriptionLevelFull); 69730fdc8d8SChris Lattner result.GetOutputStream().IndentLess(); 698b9c1b51eSKate Stone } else { 699b9c1b51eSKate Stone result.AppendMessageWithFormat( 700b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 701e16c50a1SJim Ingham id_str.GetData()); 70230fdc8d8SChris Lattner } 70330fdc8d8SChris Lattner } 70430fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 705b9c1b51eSKate Stone } else { 706b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 707b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 70830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 70930fdc8d8SChris Lattner } 71030fdc8d8SChris Lattner } 71130fdc8d8SChris Lattner } 71230fdc8d8SChris Lattner 71330fdc8d8SChris Lattner return result.Succeeded(); 71430fdc8d8SChris Lattner } 7155a988416SJim Ingham }; 71630fdc8d8SChris Lattner 71730fdc8d8SChris Lattner // CommandObjectBreakpointCommand 71830fdc8d8SChris Lattner 719b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 720b9c1b51eSKate Stone CommandInterpreter &interpreter) 7217428a18cSKate Stone : CommandObjectMultiword( 722b9c1b51eSKate Stone interpreter, "command", "Commands for adding, removing and listing " 723b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 724b9c1b51eSKate Stone "hit.", 725b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 726b9c1b51eSKate Stone CommandObjectSP add_command_object( 727b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 728b9c1b51eSKate Stone CommandObjectSP delete_command_object( 729b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 730b9c1b51eSKate Stone CommandObjectSP list_command_object( 731b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 73230fdc8d8SChris Lattner 73330fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 73493e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 73530fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 73630fdc8d8SChris Lattner 73723f59509SGreg Clayton LoadSubCommand("add", add_command_object); 73823f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 73923f59509SGreg Clayton LoadSubCommand("list", list_command_object); 74030fdc8d8SChris Lattner } 74130fdc8d8SChris Lattner 742c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 743