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 47f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 48f94668e3SRaphael Isemann #include "CommandOptions.inc" 491f0f5b5bSZachary Turner 50b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 51b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 525a988416SJim Ingham public: 537428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 547428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 55b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 56b9c1b51eSKate Stone "whenever the breakpoint is hit." 57b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 58b9c1b51eSKate Stone "commands to the last created breakpoint.", 59c8ecc2a9SEugene Zelenko nullptr), 60b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 61b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 62b9c1b51eSKate Stone m_options() { 6330fdc8d8SChris Lattner SetHelpLong( 64ea671fbdSKate Stone R"( 65ea671fbdSKate Stone General information about entering breakpoint commands 66ea671fbdSKate Stone ------------------------------------------------------ 67ea671fbdSKate Stone 68b9c1b51eSKate Stone )" 69b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 70ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 71b9c1b51eSKate Stone prompt until 'DONE' is entered." 72b9c1b51eSKate Stone R"( 73ea671fbdSKate Stone 74b9c1b51eSKate Stone )" 75b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 76ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 77b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 78b9c1b51eSKate Stone R"( 79ea671fbdSKate Stone 80b9c1b51eSKate Stone )" 81b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 82ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 83b9c1b51eSKate Stone more than one command per line." 84b9c1b51eSKate Stone R"( 85ea671fbdSKate Stone 86ea671fbdSKate Stone Special information about PYTHON breakpoint commands 87ea671fbdSKate Stone ---------------------------------------------------- 88ea671fbdSKate Stone 89b9c1b51eSKate Stone )" 90b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 91ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 92ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 93ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 94b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 95b9c1b51eSKate Stone R"( 96ea671fbdSKate Stone 97ea671fbdSKate Stone This auto-generated function is passed in three arguments: 98ea671fbdSKate Stone 99ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 100ea671fbdSKate Stone 101ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 102ea671fbdSKate Stone 103ea671fbdSKate Stone dict: the python session dictionary hit. 104ea671fbdSKate Stone 105b9c1b51eSKate Stone )" 106b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 107b9c1b51eSKate Stone to supply the function name prepended by the module name:" 108b9c1b51eSKate Stone R"( 109ea671fbdSKate Stone 110ea671fbdSKate Stone --python-function myutils.breakpoint_callback 111ea671fbdSKate Stone 112ea671fbdSKate Stone The function itself must have the following prototype: 113ea671fbdSKate Stone 114ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 115ea671fbdSKate Stone # Your code goes here 116ea671fbdSKate Stone 117b9c1b51eSKate Stone )" 118b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 119ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 120ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 121ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 122ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 123b9c1b51eSKate Stone via process.GetTarget()." 124b9c1b51eSKate Stone R"( 125ea671fbdSKate Stone 126b9c1b51eSKate Stone )" 127b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 128ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 129b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 130b9c1b51eSKate Stone R"( 131ea671fbdSKate Stone 132ea671fbdSKate Stone Example Python one-line breakpoint command: 133ea671fbdSKate Stone 134ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 135ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 136ea671fbdSKate Stone > print "Hit this breakpoint!" 137ea671fbdSKate Stone > DONE 138ea671fbdSKate Stone 139ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 140ea671fbdSKate Stone 141ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 142ea671fbdSKate Stone (lldb) run 143ea671fbdSKate Stone Launching '.../a.out' (x86_64) 144ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 145ea671fbdSKate Stone Process 21778 Stopped 146ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 147ea671fbdSKate Stone 36 148ea671fbdSKate Stone 37 int c(int val) 149ea671fbdSKate Stone 38 { 150ea671fbdSKate Stone 39 -> return val + 3; 151ea671fbdSKate Stone 40 } 152ea671fbdSKate Stone 41 153ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 154ea671fbdSKate Stone 155ea671fbdSKate Stone Example multiple line Python breakpoint command: 156ea671fbdSKate Stone 157ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 158ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 159ea671fbdSKate Stone > global bp_count 160ea671fbdSKate Stone > bp_count = bp_count + 1 161ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!" 162ea671fbdSKate Stone > DONE 163ea671fbdSKate Stone 164ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition: 165ea671fbdSKate Stone 166ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 167ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 168ea671fbdSKate Stone > def breakpoint_output (bp_no): 169ea671fbdSKate Stone > out_string = "Hit breakpoint number " + repr (bp_no) 170ea671fbdSKate Stone > print out_string 171ea671fbdSKate Stone > return True 172ea671fbdSKate Stone > breakpoint_output (1) 173ea671fbdSKate Stone > DONE 174ea671fbdSKate Stone 175b9c1b51eSKate Stone )" 176b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 177ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 178b9c1b51eSKate Stone initialized:" 179b9c1b51eSKate Stone R"( 180ea671fbdSKate Stone 181ea671fbdSKate Stone (lldb) script 182ea671fbdSKate Stone >>> bp_count = 0 183ea671fbdSKate Stone >>> quit() 184ea671fbdSKate Stone 185b9c1b51eSKate Stone )" 186b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 187ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 188ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 189ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 190b9c1b51eSKate Stone LLDB to stop." 191b9c1b51eSKate Stone R"( 192ea671fbdSKate Stone 193b9c1b51eSKate Stone )" 194b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 195b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 196405fe67fSCaroline Tice 197405fe67fSCaroline Tice CommandArgumentEntry arg; 198405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 199405fe67fSCaroline Tice 200405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 201405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2021bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 203405fe67fSCaroline Tice 204b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 205b9c1b51eSKate Stone // argument entry. 206405fe67fSCaroline Tice arg.push_back(bp_id_arg); 207405fe67fSCaroline Tice 208405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 209405fe67fSCaroline Tice m_arguments.push_back(arg); 21030fdc8d8SChris Lattner } 21130fdc8d8SChris Lattner 212c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2135a988416SJim Ingham 214b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 2155a988416SJim Ingham 2160affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 21744d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 2180affb582SDave Lee if (output_sp && interactive) { 21944d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 22044d93782SGreg Clayton output_sp->Flush(); 22144d93782SGreg Clayton } 22244d93782SGreg Clayton } 22344d93782SGreg Clayton 224b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 225b9c1b51eSKate Stone std::string &line) override { 22644d93782SGreg Clayton io_handler.SetIsDone(true); 22744d93782SGreg Clayton 228b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 229b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 230b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 231b5796cb4SJim Ingham if (!bp_options) 232b5796cb4SJim Ingham continue; 233b5796cb4SJim Ingham 2344e4fbe82SZachary Turner auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 235e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 23692d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 23744d93782SGreg Clayton } 23844d93782SGreg Clayton } 23944d93782SGreg Clayton 240b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 241b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 242b9c1b51eSKate Stone CommandReturnObject &result) { 243b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 244b9c1b51eSKate Stone "> ", // Prompt 24544d93782SGreg Clayton *this, // IOHandlerDelegate 24644d93782SGreg Clayton true, // Run IOHandler in async mode 247b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 248b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2495a988416SJim Ingham } 2505a988416SJim Ingham 2515a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2525a988416SJim Ingham void 253b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 254b9c1b51eSKate Stone const char *oneliner) { 255b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 2564e4fbe82SZachary Turner auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 2575a988416SJim Ingham 258e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 259e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2605a988416SJim Ingham 26192d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 262b5796cb4SJim Ingham } 2635a988416SJim Ingham } 2645a988416SJim Ingham 265b9c1b51eSKate Stone class CommandOptions : public Options { 2665a988416SJim Ingham public: 267b9c1b51eSKate Stone CommandOptions() 268b9c1b51eSKate Stone : Options(), m_use_commands(false), m_use_script_language(false), 269b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 270b9c1b51eSKate Stone m_one_liner(), m_function_name() {} 27130fdc8d8SChris Lattner 272c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2735a988416SJim Ingham 27497206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 275b9c1b51eSKate Stone ExecutionContext *execution_context) override { 27697206d57SZachary Turner Status error; 2773bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 2785a988416SJim Ingham 279b9c1b51eSKate Stone switch (short_option) { 2805a988416SJim Ingham case 'o': 2815a988416SJim Ingham m_use_one_liner = true; 2825a988416SJim Ingham m_one_liner = option_arg; 2835a988416SJim Ingham break; 2845a988416SJim Ingham 2855a988416SJim Ingham case 's': 28647cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 287*bd68a052SRaphael Isemann option_arg, 288*bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 289b9c1b51eSKate Stone eScriptLanguageNone, error); 2905a988416SJim Ingham 291b9c1b51eSKate Stone if (m_script_language == eScriptLanguagePython || 292b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault) { 2935a988416SJim Ingham m_use_script_language = true; 294b9c1b51eSKate Stone } else { 2955a988416SJim Ingham m_use_script_language = false; 2965a988416SJim Ingham } 2975a988416SJim Ingham break; 2985a988416SJim Ingham 299b9c1b51eSKate Stone case 'e': { 3005a988416SJim Ingham bool success = false; 30147cbf4a0SPavel Labath m_stop_on_error = 30247cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3035a988416SJim Ingham if (!success) 304b9c1b51eSKate Stone error.SetErrorStringWithFormat( 305fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 306fe11483bSZachary Turner option_arg.str().c_str()); 307b9c1b51eSKate Stone } break; 3085a988416SJim Ingham 3095a988416SJim Ingham case 'F': 3105a988416SJim Ingham m_use_one_liner = false; 3115a988416SJim Ingham m_use_script_language = true; 3125a988416SJim Ingham m_function_name.assign(option_arg); 3135a988416SJim Ingham break; 3145a988416SJim Ingham 31533df7cd3SJim Ingham case 'D': 31633df7cd3SJim Ingham m_use_dummy = true; 31733df7cd3SJim Ingham break; 31833df7cd3SJim Ingham 3195a988416SJim Ingham default: 3205a988416SJim Ingham break; 3215a988416SJim Ingham } 3225a988416SJim Ingham return error; 3235a988416SJim Ingham } 324c8ecc2a9SEugene Zelenko 325b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3265a988416SJim Ingham m_use_commands = true; 3275a988416SJim Ingham m_use_script_language = false; 3285a988416SJim Ingham m_script_language = eScriptLanguageNone; 3295a988416SJim Ingham 3305a988416SJim Ingham m_use_one_liner = false; 3315a988416SJim Ingham m_stop_on_error = true; 3325a988416SJim Ingham m_one_liner.clear(); 3335a988416SJim Ingham m_function_name.clear(); 33433df7cd3SJim Ingham m_use_dummy = false; 3355a988416SJim Ingham } 3365a988416SJim Ingham 3371f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 338*bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_add_options); 3391f0f5b5bSZachary Turner } 3405a988416SJim Ingham 3415a988416SJim Ingham // Instance variables to hold the values for command options. 3425a988416SJim Ingham 3435a988416SJim Ingham bool m_use_commands; 3445a988416SJim Ingham bool m_use_script_language; 3455a988416SJim Ingham lldb::ScriptLanguage m_script_language; 3465a988416SJim Ingham 3475a988416SJim Ingham // Instance variables to hold the values for one_liner options. 3485a988416SJim Ingham bool m_use_one_liner; 3495a988416SJim Ingham std::string m_one_liner; 3505a988416SJim Ingham bool m_stop_on_error; 3515a988416SJim Ingham std::string m_function_name; 35233df7cd3SJim Ingham bool m_use_dummy; 3535a988416SJim Ingham }; 3545a988416SJim Ingham 3555a988416SJim Ingham protected: 356b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 35733df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 35830fdc8d8SChris Lattner 359b9c1b51eSKate Stone if (target == nullptr) { 360b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 361b9c1b51eSKate Stone "breakpoints to which to add commands"); 36230fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 36330fdc8d8SChris Lattner return false; 36430fdc8d8SChris Lattner } 36530fdc8d8SChris Lattner 36630fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 36730fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 36830fdc8d8SChris Lattner 369b9c1b51eSKate Stone if (num_breakpoints == 0) { 37030fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37230fdc8d8SChris Lattner return false; 37330fdc8d8SChris Lattner } 37430fdc8d8SChris Lattner 375b9c1b51eSKate Stone if (!m_options.m_use_script_language && 376b9c1b51eSKate Stone !m_options.m_function_name.empty()) { 377b9c1b51eSKate Stone result.AppendError("need to enable scripting to have a function run as a " 378b9c1b51eSKate Stone "breakpoint command"); 3798d4a8010SEnrico Granata result.SetStatus(eReturnStatusFailed); 3808d4a8010SEnrico Granata return false; 3818d4a8010SEnrico Granata } 3828d4a8010SEnrico Granata 38330fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 384b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 385b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 386b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 38730fdc8d8SChris Lattner 388b5796cb4SJim Ingham m_bp_options_vec.clear(); 389b5796cb4SJim Ingham 390b9c1b51eSKate Stone if (result.Succeeded()) { 391c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 392d9916eaeSJim Ingham 393b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 39430fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 395b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 396b9c1b51eSKate Stone Breakpoint *bp = 397b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 398c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 399b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 40039d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 40139d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 402b9c1b51eSKate Stone } else { 403b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 404b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 40505097246SAdrian Prantl // This breakpoint does have an associated location. Get its 40605097246SAdrian Prantl // breakpoint options. 40730fdc8d8SChris Lattner if (bp_loc_sp) 40839d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 40939d7d4f0SJohnny Chen } 410b5796cb4SJim Ingham if (bp_options) 411b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 412b5796cb4SJim Ingham } 413b5796cb4SJim Ingham } 41439d7d4f0SJohnny Chen 41505097246SAdrian Prantl // If we are using script language, get the script interpreter in order 41605097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 41705097246SAdrian Prantl // associated with this object. 418b9c1b51eSKate Stone if (m_options.m_use_script_language) { 4192b29b432SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(); 42039d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 421b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 422b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallback( 423b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 424b9c1b51eSKate Stone } else if (!m_options.m_function_name.empty()) { 425b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallbackFunction( 426b9c1b51eSKate Stone m_bp_options_vec, m_options.m_function_name.c_str()); 427b9c1b51eSKate Stone } else { 428b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 429b9c1b51eSKate Stone m_bp_options_vec, result); 4308d4a8010SEnrico Granata } 431b9c1b51eSKate Stone } else { 43239d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 43339d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 434b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 43539d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 43639d7d4f0SJohnny Chen else 437b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 43830fdc8d8SChris Lattner } 43930fdc8d8SChris Lattner } 44030fdc8d8SChris Lattner 44130fdc8d8SChris Lattner return result.Succeeded(); 44230fdc8d8SChris Lattner } 44330fdc8d8SChris Lattner 4445a988416SJim Ingham private: 4455a988416SJim Ingham CommandOptions m_options; 446b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 447b9c1b51eSKate Stone // breakpoint options that 448b9c1b51eSKate Stone // we are currently 44905097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45005097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45105097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45205097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 45305097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 45405097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 45505097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 45605097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 45705097246SAdrian Prantl // right now. 4585a988416SJim Ingham static const char *g_reader_instructions; 4595a988416SJim Ingham }; 4605a988416SJim Ingham 461b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 462b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4635a988416SJim Ingham 46493e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 46530fdc8d8SChris Lattner 466f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 467f94668e3SRaphael Isemann #include "CommandOptions.inc" 4681f0f5b5bSZachary Turner 469b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4705a988416SJim Ingham public: 471b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 472b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 47393e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 474c8ecc2a9SEugene Zelenko nullptr), 475b9c1b51eSKate Stone m_options() { 476405fe67fSCaroline Tice CommandArgumentEntry arg; 477405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 478405fe67fSCaroline Tice 479405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 480405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 481405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 482405fe67fSCaroline Tice 483b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 484b9c1b51eSKate Stone // argument entry. 485405fe67fSCaroline Tice arg.push_back(bp_id_arg); 486405fe67fSCaroline Tice 487405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 488405fe67fSCaroline Tice m_arguments.push_back(arg); 48930fdc8d8SChris Lattner } 49030fdc8d8SChris Lattner 491c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4925a988416SJim Ingham 493b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 49433df7cd3SJim Ingham 495b9c1b51eSKate Stone class CommandOptions : public Options { 49633df7cd3SJim Ingham public: 497b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 49833df7cd3SJim Ingham 499c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50033df7cd3SJim Ingham 50197206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 502b9c1b51eSKate Stone ExecutionContext *execution_context) override { 50397206d57SZachary Turner Status error; 50433df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 50533df7cd3SJim Ingham 506b9c1b51eSKate Stone switch (short_option) { 50733df7cd3SJim Ingham case 'D': 50833df7cd3SJim Ingham m_use_dummy = true; 50933df7cd3SJim Ingham break; 51033df7cd3SJim Ingham 51133df7cd3SJim Ingham default: 512b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 513b9c1b51eSKate Stone short_option); 51433df7cd3SJim Ingham break; 51533df7cd3SJim Ingham } 51633df7cd3SJim Ingham 51733df7cd3SJim Ingham return error; 51833df7cd3SJim Ingham } 51933df7cd3SJim Ingham 520b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 52133df7cd3SJim Ingham m_use_dummy = false; 52233df7cd3SJim Ingham } 52333df7cd3SJim Ingham 5241f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 525*bd68a052SRaphael Isemann return llvm::makeArrayRef(g_breakpoint_command_delete_options); 5261f0f5b5bSZachary Turner } 52733df7cd3SJim Ingham 52833df7cd3SJim Ingham // Instance variables to hold the values for command options. 52933df7cd3SJim Ingham bool m_use_dummy; 53033df7cd3SJim Ingham }; 53133df7cd3SJim Ingham 5325a988416SJim Ingham protected: 533b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 53433df7cd3SJim Ingham Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 53530fdc8d8SChris Lattner 536b9c1b51eSKate Stone if (target == nullptr) { 537b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 538b9c1b51eSKate Stone "breakpoints from which to delete commands"); 53930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54030fdc8d8SChris Lattner return false; 54130fdc8d8SChris Lattner } 54230fdc8d8SChris Lattner 54330fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 54430fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 54530fdc8d8SChris Lattner 546b9c1b51eSKate Stone if (num_breakpoints == 0) { 54793e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 54830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54930fdc8d8SChris Lattner return false; 55030fdc8d8SChris Lattner } 55130fdc8d8SChris Lattner 55211eb9c64SZachary Turner if (command.empty()) { 553b9c1b51eSKate Stone result.AppendError( 554b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 55530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 55630fdc8d8SChris Lattner return false; 55730fdc8d8SChris Lattner } 55830fdc8d8SChris Lattner 55930fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 560b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 561b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 562b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 56330fdc8d8SChris Lattner 564b9c1b51eSKate Stone if (result.Succeeded()) { 565c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 566b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 56730fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 568b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 569b9c1b51eSKate Stone Breakpoint *bp = 570b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 571b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 572b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 573b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 57430fdc8d8SChris Lattner if (bp_loc_sp) 57530fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 576b9c1b51eSKate Stone else { 57730fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 57830fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 57930fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 58030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 58130fdc8d8SChris Lattner return false; 58230fdc8d8SChris Lattner } 583b9c1b51eSKate Stone } else { 58430fdc8d8SChris Lattner bp->ClearCallback(); 58530fdc8d8SChris Lattner } 58630fdc8d8SChris Lattner } 58730fdc8d8SChris Lattner } 58830fdc8d8SChris Lattner } 58930fdc8d8SChris Lattner return result.Succeeded(); 59030fdc8d8SChris Lattner } 591c8ecc2a9SEugene Zelenko 59233df7cd3SJim Ingham private: 59333df7cd3SJim Ingham CommandOptions m_options; 5945a988416SJim Ingham }; 59530fdc8d8SChris Lattner 59630fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 59730fdc8d8SChris Lattner 598b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 5995a988416SJim Ingham public: 600b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 601b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "list", "List the script or set of " 602b9c1b51eSKate Stone "commands to be executed when " 603b9c1b51eSKate Stone "the breakpoint is hit.", 604b9c1b51eSKate Stone nullptr) { 605405fe67fSCaroline Tice CommandArgumentEntry arg; 606405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 607405fe67fSCaroline Tice 608405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 609405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 610405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 611405fe67fSCaroline Tice 612b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 613b9c1b51eSKate Stone // argument entry. 614405fe67fSCaroline Tice arg.push_back(bp_id_arg); 615405fe67fSCaroline Tice 616405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 617405fe67fSCaroline Tice m_arguments.push_back(arg); 61830fdc8d8SChris Lattner } 61930fdc8d8SChris Lattner 620c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 62130fdc8d8SChris Lattner 6225a988416SJim Ingham protected: 623b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 62457179860SJonas Devlieghere Target *target = GetDebugger().GetSelectedTarget().get(); 62530fdc8d8SChris Lattner 626b9c1b51eSKate Stone if (target == nullptr) { 627b9c1b51eSKate Stone result.AppendError("There is not a current executable; there are no " 628b9c1b51eSKate Stone "breakpoints for which to list commands"); 62930fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63030fdc8d8SChris Lattner return false; 63130fdc8d8SChris Lattner } 63230fdc8d8SChris Lattner 63330fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 63430fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 63530fdc8d8SChris Lattner 636b9c1b51eSKate Stone if (num_breakpoints == 0) { 63730fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 63830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63930fdc8d8SChris Lattner return false; 64030fdc8d8SChris Lattner } 64130fdc8d8SChris Lattner 64211eb9c64SZachary Turner if (command.empty()) { 643b9c1b51eSKate Stone result.AppendError( 644b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 64530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 64630fdc8d8SChris Lattner return false; 64730fdc8d8SChris Lattner } 64830fdc8d8SChris Lattner 64930fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 650b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 651b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 652b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 65330fdc8d8SChris Lattner 654b9c1b51eSKate Stone if (result.Succeeded()) { 655c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 656b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 65730fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 658b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 659b9c1b51eSKate Stone Breakpoint *bp = 660b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 66130fdc8d8SChris Lattner 662b9c1b51eSKate Stone if (bp) { 663af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 664b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 665af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 666af26b22cSJim Ingham if (!bp_loc_sp) 667af26b22cSJim Ingham { 66830fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 66930fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 67030fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 67130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 67230fdc8d8SChris Lattner return false; 67330fdc8d8SChris Lattner } 67430fdc8d8SChris Lattner } 67530fdc8d8SChris Lattner 67630fdc8d8SChris Lattner StreamString id_str; 677e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 678e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 679e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 680af26b22cSJim Ingham const Baton *baton = nullptr; 681af26b22cSJim Ingham if (bp_loc_sp) 682af26b22cSJim Ingham baton = bp_loc_sp 683af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 684af26b22cSJim Ingham ->GetBaton(); 685af26b22cSJim Ingham else 686af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 687af26b22cSJim Ingham 688b9c1b51eSKate Stone if (baton) { 689b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 690b9c1b51eSKate Stone id_str.GetData()); 69130fdc8d8SChris Lattner result.GetOutputStream().IndentMore(); 692b9c1b51eSKate Stone baton->GetDescription(&result.GetOutputStream(), 693b9c1b51eSKate Stone eDescriptionLevelFull); 69430fdc8d8SChris Lattner result.GetOutputStream().IndentLess(); 695b9c1b51eSKate Stone } else { 696b9c1b51eSKate Stone result.AppendMessageWithFormat( 697b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 698e16c50a1SJim Ingham id_str.GetData()); 69930fdc8d8SChris Lattner } 70030fdc8d8SChris Lattner } 70130fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 702b9c1b51eSKate Stone } else { 703b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 704b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 70530fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 70630fdc8d8SChris Lattner } 70730fdc8d8SChris Lattner } 70830fdc8d8SChris Lattner } 70930fdc8d8SChris Lattner 71030fdc8d8SChris Lattner return result.Succeeded(); 71130fdc8d8SChris Lattner } 7125a988416SJim Ingham }; 71330fdc8d8SChris Lattner 71430fdc8d8SChris Lattner // CommandObjectBreakpointCommand 71530fdc8d8SChris Lattner 716b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 717b9c1b51eSKate Stone CommandInterpreter &interpreter) 7187428a18cSKate Stone : CommandObjectMultiword( 719b9c1b51eSKate Stone interpreter, "command", "Commands for adding, removing and listing " 720b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 721b9c1b51eSKate Stone "hit.", 722b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 723b9c1b51eSKate Stone CommandObjectSP add_command_object( 724b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 725b9c1b51eSKate Stone CommandObjectSP delete_command_object( 726b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 727b9c1b51eSKate Stone CommandObjectSP list_command_object( 728b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 72930fdc8d8SChris Lattner 73030fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 73193e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 73230fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 73330fdc8d8SChris Lattner 73423f59509SGreg Clayton LoadSubCommand("add", add_command_object); 73523f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 73623f59509SGreg Clayton LoadSubCommand("list", list_command_object); 73730fdc8d8SChris Lattner } 73830fdc8d8SChris Lattner 739c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 740