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 291f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 30e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 31e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 328fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 33e063ecccSJonas Devlieghere { 34e063ecccSJonas Devlieghere eScriptLanguageNone, 35e063ecccSJonas Devlieghere "command", 36e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 37e063ecccSJonas Devlieghere }, 38e063ecccSJonas Devlieghere { 39e063ecccSJonas Devlieghere eScriptLanguagePython, 40e063ecccSJonas Devlieghere "python", 41e063ecccSJonas Devlieghere "Commands are in the Python language.", 42e063ecccSJonas Devlieghere }, 43e063ecccSJonas Devlieghere { 44e063ecccSJonas Devlieghere eSortOrderByName, 45e063ecccSJonas Devlieghere "default-script", 46e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 47e063ecccSJonas Devlieghere }, 48e063ecccSJonas Devlieghere }; 491f0f5b5bSZachary Turner 508fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 518fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 528fe53c49STatyana Krasnukha } 538fe53c49STatyana Krasnukha 54f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 55f94668e3SRaphael Isemann #include "CommandOptions.inc" 561f0f5b5bSZachary Turner 57b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 58b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 595a988416SJim Ingham public: 607428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 617428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 62b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 63b9c1b51eSKate Stone "whenever the breakpoint is hit." 64b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 65b9c1b51eSKate Stone "commands to the last created breakpoint.", 66c8ecc2a9SEugene Zelenko nullptr), 67b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 68b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 69b9c1b51eSKate Stone m_options() { 7030fdc8d8SChris Lattner SetHelpLong( 71ea671fbdSKate Stone R"( 72ea671fbdSKate Stone General information about entering breakpoint commands 73ea671fbdSKate Stone ------------------------------------------------------ 74ea671fbdSKate Stone 75b9c1b51eSKate Stone )" 76b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 77ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 78b9c1b51eSKate Stone prompt until 'DONE' is entered." 79b9c1b51eSKate Stone R"( 80ea671fbdSKate Stone 81b9c1b51eSKate Stone )" 82b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 83ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 84b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 85b9c1b51eSKate Stone R"( 86ea671fbdSKate Stone 87b9c1b51eSKate Stone )" 88b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 89ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 90b9c1b51eSKate Stone more than one command per line." 91b9c1b51eSKate Stone R"( 92ea671fbdSKate Stone 93ea671fbdSKate Stone Special information about PYTHON breakpoint commands 94ea671fbdSKate Stone ---------------------------------------------------- 95ea671fbdSKate Stone 96b9c1b51eSKate Stone )" 97b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 98ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 99ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 100ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 101b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 102b9c1b51eSKate Stone R"( 103ea671fbdSKate Stone 104ea671fbdSKate Stone This auto-generated function is passed in three arguments: 105ea671fbdSKate Stone 106ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 107ea671fbdSKate Stone 108ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 109ea671fbdSKate Stone 110ea671fbdSKate Stone dict: the python session dictionary hit. 111ea671fbdSKate Stone 112b9c1b51eSKate Stone )" 113b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 114b9c1b51eSKate Stone to supply the function name prepended by the module name:" 115b9c1b51eSKate Stone R"( 116ea671fbdSKate Stone 117ea671fbdSKate Stone --python-function myutils.breakpoint_callback 118ea671fbdSKate Stone 119ea671fbdSKate Stone The function itself must have the following prototype: 120ea671fbdSKate Stone 121ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 122ea671fbdSKate Stone # Your code goes here 123ea671fbdSKate Stone 124b9c1b51eSKate Stone )" 125b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 126ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 127ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 128ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 129ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 130b9c1b51eSKate Stone via process.GetTarget()." 131b9c1b51eSKate Stone R"( 132ea671fbdSKate Stone 133b9c1b51eSKate Stone )" 134b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 135ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 136b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 137b9c1b51eSKate Stone R"( 138ea671fbdSKate Stone 139ea671fbdSKate Stone Example Python one-line breakpoint command: 140ea671fbdSKate Stone 141ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 142ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 143ea671fbdSKate Stone > print "Hit this breakpoint!" 144ea671fbdSKate Stone > DONE 145ea671fbdSKate Stone 146ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 147ea671fbdSKate Stone 148ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 149ea671fbdSKate Stone (lldb) run 150ea671fbdSKate Stone Launching '.../a.out' (x86_64) 151ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 152ea671fbdSKate Stone Process 21778 Stopped 153ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 154ea671fbdSKate Stone 36 155ea671fbdSKate Stone 37 int c(int val) 156ea671fbdSKate Stone 38 { 157ea671fbdSKate Stone 39 -> return val + 3; 158ea671fbdSKate Stone 40 } 159ea671fbdSKate Stone 41 160ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 161ea671fbdSKate Stone 162ea671fbdSKate Stone Example multiple line Python breakpoint command: 163ea671fbdSKate Stone 164ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 165ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 166ea671fbdSKate Stone > global bp_count 167ea671fbdSKate Stone > bp_count = bp_count + 1 168ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!" 169ea671fbdSKate Stone > DONE 170ea671fbdSKate Stone 171ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition: 172ea671fbdSKate Stone 173ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 174ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 175ea671fbdSKate Stone > def breakpoint_output (bp_no): 176ea671fbdSKate Stone > out_string = "Hit breakpoint number " + repr (bp_no) 177ea671fbdSKate Stone > print out_string 178ea671fbdSKate Stone > return True 179ea671fbdSKate Stone > breakpoint_output (1) 180ea671fbdSKate Stone > DONE 181ea671fbdSKate Stone 182b9c1b51eSKate Stone )" 183b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 184ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 185b9c1b51eSKate Stone initialized:" 186b9c1b51eSKate Stone R"( 187ea671fbdSKate Stone 188ea671fbdSKate Stone (lldb) script 189ea671fbdSKate Stone >>> bp_count = 0 190ea671fbdSKate Stone >>> quit() 191ea671fbdSKate Stone 192b9c1b51eSKate Stone )" 193b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 194ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 195ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 196ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 197b9c1b51eSKate Stone LLDB to stop." 198b9c1b51eSKate Stone R"( 199ea671fbdSKate Stone 200b9c1b51eSKate Stone )" 201b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 202b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 203405fe67fSCaroline Tice 204405fe67fSCaroline Tice CommandArgumentEntry arg; 205405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 206405fe67fSCaroline Tice 207405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 208405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2091bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 210405fe67fSCaroline Tice 211b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 212b9c1b51eSKate Stone // argument entry. 213405fe67fSCaroline Tice arg.push_back(bp_id_arg); 214405fe67fSCaroline Tice 215405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 216405fe67fSCaroline Tice m_arguments.push_back(arg); 21730fdc8d8SChris Lattner } 21830fdc8d8SChris Lattner 219c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2205a988416SJim Ingham 221b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 2225a988416SJim Ingham 2230affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 22444d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 2250affb582SDave Lee if (output_sp && interactive) { 22644d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 22744d93782SGreg Clayton output_sp->Flush(); 22844d93782SGreg Clayton } 22944d93782SGreg Clayton } 23044d93782SGreg Clayton 231b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 232b9c1b51eSKate Stone std::string &line) override { 23344d93782SGreg Clayton io_handler.SetIsDone(true); 23444d93782SGreg Clayton 235b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 236b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 237b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 238b5796cb4SJim Ingham if (!bp_options) 239b5796cb4SJim Ingham continue; 240b5796cb4SJim Ingham 241a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 242e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 24392d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 24444d93782SGreg Clayton } 24544d93782SGreg Clayton } 24644d93782SGreg Clayton 247b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 248b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 249b9c1b51eSKate Stone CommandReturnObject &result) { 250b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 251b9c1b51eSKate Stone "> ", // Prompt 25244d93782SGreg Clayton *this, // IOHandlerDelegate 25344d93782SGreg Clayton true, // Run IOHandler in async mode 254b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 255b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2565a988416SJim Ingham } 2575a988416SJim Ingham 2585a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2595a988416SJim Ingham void 260b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 261b9c1b51eSKate Stone const char *oneliner) { 262b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 263a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 2645a988416SJim Ingham 265e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 266e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2675a988416SJim Ingham 26892d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 269b5796cb4SJim Ingham } 2705a988416SJim Ingham } 2715a988416SJim Ingham 272b9c1b51eSKate Stone class CommandOptions : public Options { 2735a988416SJim Ingham public: 274b9c1b51eSKate Stone CommandOptions() 275b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 276b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 277b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 27830fdc8d8SChris Lattner 279c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2805a988416SJim Ingham 28197206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 282b9c1b51eSKate Stone ExecutionContext *execution_context) override { 28397206d57SZachary Turner Status error; 2843bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 2855a988416SJim Ingham 286b9c1b51eSKate Stone switch (short_option) { 2875a988416SJim Ingham case 'o': 2885a988416SJim Ingham m_use_one_liner = true; 2895a988416SJim Ingham m_one_liner = option_arg; 2905a988416SJim Ingham break; 2915a988416SJim Ingham 2925a988416SJim Ingham case 's': 29347cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 294bd68a052SRaphael Isemann option_arg, 295bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 296b9c1b51eSKate Stone eScriptLanguageNone, error); 2975a988416SJim Ingham 298b9c1b51eSKate Stone if (m_script_language == eScriptLanguagePython || 299b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault) { 3005a988416SJim Ingham m_use_script_language = true; 301b9c1b51eSKate Stone } else { 3025a988416SJim Ingham m_use_script_language = false; 3035a988416SJim Ingham } 3045a988416SJim Ingham break; 3055a988416SJim Ingham 306b9c1b51eSKate Stone case 'e': { 3075a988416SJim Ingham bool success = false; 30847cbf4a0SPavel Labath m_stop_on_error = 30947cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3105a988416SJim Ingham if (!success) 311b9c1b51eSKate Stone error.SetErrorStringWithFormat( 312fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 313fe11483bSZachary Turner option_arg.str().c_str()); 314b9c1b51eSKate Stone } break; 3155a988416SJim Ingham 3165a988416SJim Ingham case 'F': 3175a988416SJim Ingham m_use_one_liner = false; 3185a988416SJim Ingham m_use_script_language = true; 3195a988416SJim Ingham m_function_name.assign(option_arg); 3205a988416SJim Ingham break; 3215a988416SJim Ingham 32233df7cd3SJim Ingham case 'D': 32333df7cd3SJim Ingham m_use_dummy = true; 32433df7cd3SJim Ingham break; 32533df7cd3SJim Ingham 3265a988416SJim Ingham default: 327*36162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 3285a988416SJim Ingham } 3295a988416SJim Ingham return error; 3305a988416SJim Ingham } 331c8ecc2a9SEugene Zelenko 332b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3335a988416SJim Ingham m_use_commands = true; 3345a988416SJim Ingham m_use_script_language = false; 3355a988416SJim Ingham m_script_language = eScriptLanguageNone; 3365a988416SJim Ingham 3375a988416SJim Ingham m_use_one_liner = false; 3385a988416SJim Ingham m_stop_on_error = true; 3395a988416SJim Ingham m_one_liner.clear(); 3405a988416SJim Ingham m_function_name.clear(); 34133df7cd3SJim Ingham m_use_dummy = false; 3425a988416SJim Ingham } 3435a988416SJim Ingham 3441f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 345bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_add_options); 3461f0f5b5bSZachary Turner } 3475a988416SJim Ingham 3485a988416SJim Ingham // Instance variables to hold the values for command options. 3495a988416SJim Ingham 3505a988416SJim Ingham bool m_use_commands; 3515a988416SJim Ingham bool m_use_script_language; 3525a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3535a988416SJim Ingham 3545a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3555a988416SJim Ingham bool m_use_one_liner; 3565a988416SJim Ingham std::string m_one_liner; 3575a988416SJim Ingham bool m_stop_on_error; 3585a988416SJim Ingham std::string m_function_name; 35933df7cd3SJim Ingham bool m_use_dummy; 3605a988416SJim Ingham }; 3615a988416SJim Ingham 3625a988416SJim Ingham protected: 363b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 36433df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 36530fdc8d8SChris Lattner 366b9c1b51eSKate Stone if (target == nullptr) { 367b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 368b9c1b51eSKate Stone "breakpoints to which to add commands"); 36930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37030fdc8d8SChris Lattner return false; 37130fdc8d8SChris Lattner } 37230fdc8d8SChris Lattner 37330fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 37430fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 37530fdc8d8SChris Lattner 376b9c1b51eSKate Stone if (num_breakpoints == 0) { 37730fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37930fdc8d8SChris Lattner return false; 38030fdc8d8SChris Lattner } 38130fdc8d8SChris Lattner 382b9c1b51eSKate Stone if (!m_options.m_use_script_language && 383b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 384b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 385b9c1b51eSKate Stone "breakpoint command"); 3868d4a8010SEnrico Granata result.SetStatus(eReturnStatusFailed); 3878d4a8010SEnrico Granata return false; 3888d4a8010SEnrico Granata } 3898d4a8010SEnrico Granata 39030fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 391b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 392b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 393b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 39430fdc8d8SChris Lattner 395b5796cb4SJim Ingham m_bp_options_vec.clear(); 396b5796cb4SJim Ingham 397b9c1b51eSKate Stone if (result.Succeeded()) { 398c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 399d9916eaeSJim Ingham 400b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 40130fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 402b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 403b9c1b51eSKate Stone Breakpoint *bp = 404b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 405c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 406b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 40739d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 40839d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 409b9c1b51eSKate Stone } else { 410b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 411b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 41205097246SAdrian Prantl // This breakpoint does have an associated location. Get its 41305097246SAdrian Prantl // breakpoint options. 41430fdc8d8SChris Lattner if (bp_loc_sp) 41539d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 41639d7d4f0SJohnny Chen } 417b5796cb4SJim Ingham if (bp_options) 418b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 419b5796cb4SJim Ingham } 420b5796cb4SJim Ingham } 42139d7d4f0SJohnny Chen 42205097246SAdrian Prantl // If we are using script language, get the script interpreter in order 42305097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 42405097246SAdrian Prantl // associated with this object. 425b9c1b51eSKate Stone if (m_options.m_use_script_language) { 4262b29b432SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(); 42739d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 428b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 429b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallback( 430b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 431b9c1b51eSKate Stone } else if (!m_options.m_function_name.empty()) { 432b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallbackFunction( 433b9c1b51eSKate Stone m_bp_options_vec, m_options.m_function_name.c_str()); 434b9c1b51eSKate Stone } else { 435b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 436b9c1b51eSKate Stone m_bp_options_vec, result); 4378d4a8010SEnrico Granata } 438b9c1b51eSKate Stone } else { 43939d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 44039d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 441b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 44239d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 44339d7d4f0SJohnny Chen else 444b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 44530fdc8d8SChris Lattner } 44630fdc8d8SChris Lattner } 44730fdc8d8SChris Lattner 44830fdc8d8SChris Lattner return result.Succeeded(); 44930fdc8d8SChris Lattner } 45030fdc8d8SChris Lattner 4515a988416SJim Ingham private: 4525a988416SJim Ingham CommandOptions m_options; 453b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 454b9c1b51eSKate Stone // breakpoint options that 455b9c1b51eSKate Stone // we are currently 45605097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45705097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45805097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45905097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 46005097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 46105097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 46205097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 46305097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 46405097246SAdrian Prantl // right now. 4655a988416SJim Ingham static const char *g_reader_instructions; 4665a988416SJim Ingham }; 4675a988416SJim Ingham 468b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 469b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4705a988416SJim Ingham 47193e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 47230fdc8d8SChris Lattner 473f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 474f94668e3SRaphael Isemann #include "CommandOptions.inc" 4751f0f5b5bSZachary Turner 476b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4775a988416SJim Ingham public: 478b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 479b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 48093e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 481c8ecc2a9SEugene Zelenko nullptr), 482b9c1b51eSKate Stone m_options() { 483405fe67fSCaroline Tice CommandArgumentEntry arg; 484405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 485405fe67fSCaroline Tice 486405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 487405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 488405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 489405fe67fSCaroline Tice 490b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 491b9c1b51eSKate Stone // argument entry. 492405fe67fSCaroline Tice arg.push_back(bp_id_arg); 493405fe67fSCaroline Tice 494405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 495405fe67fSCaroline Tice m_arguments.push_back(arg); 49630fdc8d8SChris Lattner } 49730fdc8d8SChris Lattner 498c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4995a988416SJim Ingham 500b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 50133df7cd3SJim Ingham 502b9c1b51eSKate Stone class CommandOptions : public Options { 50333df7cd3SJim Ingham public: 504b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 50533df7cd3SJim Ingham 506c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50733df7cd3SJim Ingham 50897206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 509b9c1b51eSKate Stone ExecutionContext *execution_context) override { 51097206d57SZachary Turner Status error; 51133df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 51233df7cd3SJim Ingham 513b9c1b51eSKate Stone switch (short_option) { 51433df7cd3SJim Ingham case 'D': 51533df7cd3SJim Ingham m_use_dummy = true; 51633df7cd3SJim Ingham break; 51733df7cd3SJim Ingham 51833df7cd3SJim Ingham default: 519*36162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 52033df7cd3SJim Ingham } 52133df7cd3SJim Ingham 52233df7cd3SJim Ingham return error; 52333df7cd3SJim Ingham } 52433df7cd3SJim Ingham 525b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 52633df7cd3SJim Ingham m_use_dummy = false; 52733df7cd3SJim Ingham } 52833df7cd3SJim Ingham 5291f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 530bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_delete_options); 5311f0f5b5bSZachary Turner } 53233df7cd3SJim Ingham 53333df7cd3SJim Ingham // Instance variables to hold the values for command options. 53433df7cd3SJim Ingham bool m_use_dummy; 53533df7cd3SJim Ingham }; 53633df7cd3SJim Ingham 5375a988416SJim Ingham protected: 538b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 53933df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 54030fdc8d8SChris Lattner 541b9c1b51eSKate Stone if (target == nullptr) { 542b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 543b9c1b51eSKate Stone "breakpoints from which to delete commands"); 54430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54530fdc8d8SChris Lattner return false; 54630fdc8d8SChris Lattner } 54730fdc8d8SChris Lattner 54830fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 54930fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 55030fdc8d8SChris Lattner 551b9c1b51eSKate Stone if (num_breakpoints == 0) { 55293e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 55330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55430fdc8d8SChris Lattner return false; 55530fdc8d8SChris Lattner } 55630fdc8d8SChris Lattner 55711eb9c64SZachary Turner if (command.empty()) { 558b9c1b51eSKate Stone result.AppendError( 559b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 56030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 56130fdc8d8SChris Lattner return false; 56230fdc8d8SChris Lattner } 56330fdc8d8SChris Lattner 56430fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 565b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 566b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 567b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 56830fdc8d8SChris Lattner 569b9c1b51eSKate Stone if (result.Succeeded()) { 570c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 571b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 57230fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 573b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 574b9c1b51eSKate Stone Breakpoint *bp = 575b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 576b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 577b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 578b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 57930fdc8d8SChris Lattner if (bp_loc_sp) 58030fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 581b9c1b51eSKate Stone else { 58230fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 58330fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 58430fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 58530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 58630fdc8d8SChris Lattner return false; 58730fdc8d8SChris Lattner } 588b9c1b51eSKate Stone } else { 58930fdc8d8SChris Lattner bp->ClearCallback(); 59030fdc8d8SChris Lattner } 59130fdc8d8SChris Lattner } 59230fdc8d8SChris Lattner } 59330fdc8d8SChris Lattner } 59430fdc8d8SChris Lattner return result.Succeeded(); 59530fdc8d8SChris Lattner } 596c8ecc2a9SEugene Zelenko 59733df7cd3SJim Ingham private: 59833df7cd3SJim Ingham CommandOptions m_options; 5995a988416SJim Ingham }; 60030fdc8d8SChris Lattner 60130fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 60230fdc8d8SChris Lattner 603b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 6045a988416SJim Ingham public: 605b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 606b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "list", "List the script or set of " 607b9c1b51eSKate Stone "commands to be executed when " 608b9c1b51eSKate Stone "the breakpoint is hit.", 609b9c1b51eSKate Stone nullptr) { 610405fe67fSCaroline Tice CommandArgumentEntry arg; 611405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 612405fe67fSCaroline Tice 613405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 614405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 615405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 616405fe67fSCaroline Tice 617b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 618b9c1b51eSKate Stone // argument entry. 619405fe67fSCaroline Tice arg.push_back(bp_id_arg); 620405fe67fSCaroline Tice 621405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 622405fe67fSCaroline Tice m_arguments.push_back(arg); 62330fdc8d8SChris Lattner } 62430fdc8d8SChris Lattner 625c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 62630fdc8d8SChris Lattner 6275a988416SJim Ingham protected: 628b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 62957179860SJonas Devlieghere Target *target = GetDebugger().GetSelectedTarget().get(); 63030fdc8d8SChris Lattner 631b9c1b51eSKate Stone if (target == nullptr) { 632b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 633b9c1b51eSKate Stone "breakpoints for which to list commands"); 63430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63530fdc8d8SChris Lattner return false; 63630fdc8d8SChris Lattner } 63730fdc8d8SChris Lattner 63830fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 63930fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 64030fdc8d8SChris Lattner 641b9c1b51eSKate Stone if (num_breakpoints == 0) { 64230fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 64330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 64430fdc8d8SChris Lattner return false; 64530fdc8d8SChris Lattner } 64630fdc8d8SChris Lattner 64711eb9c64SZachary Turner if (command.empty()) { 648b9c1b51eSKate Stone result.AppendError( 649b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 65030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 65130fdc8d8SChris Lattner return false; 65230fdc8d8SChris Lattner } 65330fdc8d8SChris Lattner 65430fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 655b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 656b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 657b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 65830fdc8d8SChris Lattner 659b9c1b51eSKate Stone if (result.Succeeded()) { 660c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 661b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 66230fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 663b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 664b9c1b51eSKate Stone Breakpoint *bp = 665b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 66630fdc8d8SChris Lattner 667b9c1b51eSKate Stone if (bp) { 668af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 669b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 670af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 671af26b22cSJim Ingham if (!bp_loc_sp) 672af26b22cSJim Ingham { 67330fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 67430fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 67530fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 67630fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 67730fdc8d8SChris Lattner return false; 67830fdc8d8SChris Lattner } 67930fdc8d8SChris Lattner } 68030fdc8d8SChris Lattner 68130fdc8d8SChris Lattner StreamString id_str; 682e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 683e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 684e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 685af26b22cSJim Ingham const Baton *baton = nullptr; 686af26b22cSJim Ingham if (bp_loc_sp) 687af26b22cSJim Ingham baton = bp_loc_sp 688af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 689af26b22cSJim Ingham ->GetBaton(); 690af26b22cSJim Ingham else 691af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 692af26b22cSJim Ingham 693b9c1b51eSKate Stone if (baton) { 694b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 695b9c1b51eSKate Stone id_str.GetData()); 69630fdc8d8SChris Lattner result.GetOutputStream().IndentMore(); 697b9c1b51eSKate Stone baton->GetDescription(&result.GetOutputStream(), 698b9c1b51eSKate Stone eDescriptionLevelFull); 69930fdc8d8SChris Lattner result.GetOutputStream().IndentLess(); 700b9c1b51eSKate Stone } else { 701b9c1b51eSKate Stone result.AppendMessageWithFormat( 702b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 703e16c50a1SJim Ingham id_str.GetData()); 70430fdc8d8SChris Lattner } 70530fdc8d8SChris Lattner } 70630fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 707b9c1b51eSKate Stone } else { 708b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 709b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 71030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 71130fdc8d8SChris Lattner } 71230fdc8d8SChris Lattner } 71330fdc8d8SChris Lattner } 71430fdc8d8SChris Lattner 71530fdc8d8SChris Lattner return result.Succeeded(); 71630fdc8d8SChris Lattner } 7175a988416SJim Ingham }; 71830fdc8d8SChris Lattner 71930fdc8d8SChris Lattner // CommandObjectBreakpointCommand 72030fdc8d8SChris Lattner 721b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 722b9c1b51eSKate Stone CommandInterpreter &interpreter) 7237428a18cSKate Stone : CommandObjectMultiword( 724b9c1b51eSKate Stone interpreter, "command", "Commands for adding, removing and listing " 725b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 726b9c1b51eSKate Stone "hit.", 727b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 728b9c1b51eSKate Stone CommandObjectSP add_command_object( 729b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 730b9c1b51eSKate Stone CommandObjectSP delete_command_object( 731b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 732b9c1b51eSKate Stone CommandObjectSP list_command_object( 733b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 73430fdc8d8SChris Lattner 73530fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 73693e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 73730fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 73830fdc8d8SChris Lattner 73923f59509SGreg Clayton LoadSubCommand("add", add_command_object); 74023f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 74123f59509SGreg Clayton LoadSubCommand("list", list_command_object); 74230fdc8d8SChris Lattner } 74330fdc8d8SChris Lattner 744c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 745