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" 20738af7a6SJim Ingham #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h" 2130fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2230fdc8d8SChris Lattner #include "lldb/Target/Thread.h" 23d821c997SPavel Labath #include "lldb/Utility/State.h" 2430fdc8d8SChris Lattner 254e4fbe82SZachary Turner #include "llvm/ADT/STLExtras.h" 264e4fbe82SZachary Turner 2730fdc8d8SChris Lattner using namespace lldb; 2830fdc8d8SChris Lattner using namespace lldb_private; 2930fdc8d8SChris Lattner 301f0f5b5bSZachary Turner // FIXME: "script-type" needs to have its contents determined dynamically, so 31e063ecccSJonas Devlieghere // somebody can add a new scripting language to lldb and have it pickable here 32e063ecccSJonas Devlieghere // without having to change this enumeration by hand and rebuild lldb proper. 338fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 34e063ecccSJonas Devlieghere { 35e063ecccSJonas Devlieghere eScriptLanguageNone, 36e063ecccSJonas Devlieghere "command", 37e063ecccSJonas Devlieghere "Commands are in the lldb command interpreter language", 38e063ecccSJonas Devlieghere }, 39e063ecccSJonas Devlieghere { 40e063ecccSJonas Devlieghere eScriptLanguagePython, 41e063ecccSJonas Devlieghere "python", 42e063ecccSJonas Devlieghere "Commands are in the Python language.", 43e063ecccSJonas Devlieghere }, 44e063ecccSJonas Devlieghere { 45e063ecccSJonas Devlieghere eSortOrderByName, 46e063ecccSJonas Devlieghere "default-script", 47e063ecccSJonas Devlieghere "Commands are in the default scripting language.", 48e063ecccSJonas Devlieghere }, 49e063ecccSJonas Devlieghere }; 501f0f5b5bSZachary Turner 518fe53c49STatyana Krasnukha static constexpr OptionEnumValues ScriptOptionEnum() { 528fe53c49STatyana Krasnukha return OptionEnumValues(g_script_option_enumeration); 538fe53c49STatyana Krasnukha } 548fe53c49STatyana Krasnukha 55f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_add 56f94668e3SRaphael Isemann #include "CommandOptions.inc" 571f0f5b5bSZachary Turner 58b9c1b51eSKate Stone class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 59b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 605a988416SJim Ingham public: 617428a18cSKate Stone CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 627428a18cSKate Stone : CommandObjectParsed(interpreter, "add", 63b9c1b51eSKate Stone "Add LLDB commands to a breakpoint, to be executed " 64b9c1b51eSKate Stone "whenever the breakpoint is hit." 65b9c1b51eSKate Stone " If no breakpoint is specified, adds the " 66b9c1b51eSKate Stone "commands to the last created breakpoint.", 67c8ecc2a9SEugene Zelenko nullptr), 68b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE", 69b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 70738af7a6SJim Ingham m_options(), m_func_options("breakpoint command", false, 'F') { 7130fdc8d8SChris Lattner SetHelpLong( 72ea671fbdSKate Stone R"( 73ea671fbdSKate Stone General information about entering breakpoint commands 74ea671fbdSKate Stone ------------------------------------------------------ 75ea671fbdSKate Stone 76b9c1b51eSKate Stone )" 77b9c1b51eSKate Stone "This command will prompt for commands to be executed when the specified \ 78ea671fbdSKate Stone breakpoint is hit. Each command is typed on its own line following the '> ' \ 79b9c1b51eSKate Stone prompt until 'DONE' is entered." 80b9c1b51eSKate Stone R"( 81ea671fbdSKate Stone 82b9c1b51eSKate Stone )" 83b9c1b51eSKate Stone "Syntactic errors may not be detected when initially entered, and many \ 84ea671fbdSKate Stone malformed commands can silently fail when executed. If your breakpoint commands \ 85b9c1b51eSKate Stone do not appear to be executing, double-check the command syntax." 86b9c1b51eSKate Stone R"( 87ea671fbdSKate Stone 88b9c1b51eSKate Stone )" 89b9c1b51eSKate Stone "Note: You may enter any debugger command exactly as you would at the debugger \ 90ea671fbdSKate Stone prompt. There is no limit to the number of commands supplied, but do NOT enter \ 91b9c1b51eSKate Stone more than one command per line." 92b9c1b51eSKate Stone R"( 93ea671fbdSKate Stone 94ea671fbdSKate Stone Special information about PYTHON breakpoint commands 95ea671fbdSKate Stone ---------------------------------------------------- 96ea671fbdSKate Stone 97b9c1b51eSKate Stone )" 98b9c1b51eSKate Stone "You may enter either one or more lines of Python, including function \ 99ea671fbdSKate Stone definitions or calls to functions that will have been imported by the time \ 100ea671fbdSKate Stone the code executes. Single line breakpoint commands will be interpreted 'as is' \ 101ea671fbdSKate Stone when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 102b9c1b51eSKate Stone generated function, and a call to the function will be attached to the breakpoint." 103b9c1b51eSKate Stone R"( 104ea671fbdSKate Stone 105ea671fbdSKate Stone This auto-generated function is passed in three arguments: 106ea671fbdSKate Stone 107ea671fbdSKate Stone frame: an lldb.SBFrame object for the frame which hit breakpoint. 108ea671fbdSKate Stone 109ea671fbdSKate Stone bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 110ea671fbdSKate Stone 111ea671fbdSKate Stone dict: the python session dictionary hit. 112ea671fbdSKate Stone 113b9c1b51eSKate Stone )" 114b9c1b51eSKate Stone "When specifying a python function with the --python-function option, you need \ 115b9c1b51eSKate Stone to supply the function name prepended by the module name:" 116b9c1b51eSKate Stone R"( 117ea671fbdSKate Stone 118ea671fbdSKate Stone --python-function myutils.breakpoint_callback 119ea671fbdSKate Stone 120ea671fbdSKate Stone The function itself must have the following prototype: 121ea671fbdSKate Stone 122ea671fbdSKate Stone def breakpoint_callback(frame, bp_loc, dict): 123ea671fbdSKate Stone # Your code goes here 124ea671fbdSKate Stone 125b9c1b51eSKate Stone )" 126b9c1b51eSKate Stone "The arguments are the same as the arguments passed to generated functions as \ 127ea671fbdSKate Stone described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 128ea671fbdSKate Stone this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 129ea671fbdSKate Stone can get you to the thread via frame.GetThread(), the thread can get you to the \ 130ea671fbdSKate Stone process via thread.GetProcess(), and the process can get you back to the target \ 131b9c1b51eSKate Stone via process.GetTarget()." 132b9c1b51eSKate Stone R"( 133ea671fbdSKate Stone 134b9c1b51eSKate Stone )" 135b9c1b51eSKate Stone "Important Note: As Python code gets collected into functions, access to global \ 136ea671fbdSKate Stone variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 137b9c1b51eSKate Stone Python syntax, including indentation, when entering Python breakpoint commands." 138b9c1b51eSKate Stone R"( 139ea671fbdSKate Stone 140ea671fbdSKate Stone Example Python one-line breakpoint command: 141ea671fbdSKate Stone 142ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 143ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 144ea671fbdSKate Stone > print "Hit this breakpoint!" 145ea671fbdSKate Stone > DONE 146ea671fbdSKate Stone 147ea671fbdSKate Stone As a convenience, this also works for a short Python one-liner: 148ea671fbdSKate Stone 149ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 150ea671fbdSKate Stone (lldb) run 151ea671fbdSKate Stone Launching '.../a.out' (x86_64) 152ea671fbdSKate Stone (lldb) Fri Sep 10 12:17:45 2010 153ea671fbdSKate Stone Process 21778 Stopped 154ea671fbdSKate Stone * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 155ea671fbdSKate Stone 36 156ea671fbdSKate Stone 37 int c(int val) 157ea671fbdSKate Stone 38 { 158ea671fbdSKate Stone 39 -> return val + 3; 159ea671fbdSKate Stone 40 } 160ea671fbdSKate Stone 41 161ea671fbdSKate Stone 42 int main (int argc, char const *argv[]) 162ea671fbdSKate Stone 163ea671fbdSKate Stone Example multiple line Python breakpoint command: 164ea671fbdSKate Stone 165ea671fbdSKate Stone (lldb) breakpoint command add -s p 1 166ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 167ea671fbdSKate Stone > global bp_count 168ea671fbdSKate Stone > bp_count = bp_count + 1 169ea671fbdSKate Stone > print "Hit this breakpoint " + repr(bp_count) + " times!" 170ea671fbdSKate Stone > DONE 171ea671fbdSKate Stone 172ea671fbdSKate Stone Example multiple line Python breakpoint command, using function definition: 173ea671fbdSKate Stone 174ea671fbdSKate Stone (lldb) breakpoint command add -s python 1 175ea671fbdSKate Stone Enter your Python command(s). Type 'DONE' to end. 176ea671fbdSKate Stone > def breakpoint_output (bp_no): 177ea671fbdSKate Stone > out_string = "Hit breakpoint number " + repr (bp_no) 178ea671fbdSKate Stone > print out_string 179ea671fbdSKate Stone > return True 180ea671fbdSKate Stone > breakpoint_output (1) 181ea671fbdSKate Stone > DONE 182ea671fbdSKate Stone 183b9c1b51eSKate Stone )" 184b9c1b51eSKate Stone "In this case, since there is a reference to a global variable, \ 185ea671fbdSKate Stone 'bp_count', you will also need to make sure 'bp_count' exists and is \ 186b9c1b51eSKate Stone initialized:" 187b9c1b51eSKate Stone R"( 188ea671fbdSKate Stone 189ea671fbdSKate Stone (lldb) script 190ea671fbdSKate Stone >>> bp_count = 0 191ea671fbdSKate Stone >>> quit() 192ea671fbdSKate Stone 193b9c1b51eSKate Stone )" 194b9c1b51eSKate Stone "Your Python code, however organized, can optionally return a value. \ 195ea671fbdSKate Stone If the returned value is False, that tells LLDB not to stop at the breakpoint \ 196ea671fbdSKate Stone to which the code is associated. Returning anything other than False, or even \ 197ea671fbdSKate Stone returning None, or even omitting a return statement entirely, will cause \ 198b9c1b51eSKate Stone LLDB to stop." 199b9c1b51eSKate Stone R"( 200ea671fbdSKate Stone 201b9c1b51eSKate Stone )" 202b9c1b51eSKate Stone "Final Note: A warning that no breakpoint command was generated when there \ 203b9c1b51eSKate Stone are no syntax errors may indicate that a function was declared but never called."); 204405fe67fSCaroline Tice 205738af7a6SJim Ingham m_all_options.Append(&m_options); 206738af7a6SJim Ingham m_all_options.Append(&m_func_options, LLDB_OPT_SET_2 | LLDB_OPT_SET_3, 207738af7a6SJim Ingham LLDB_OPT_SET_2); 208738af7a6SJim Ingham m_all_options.Finalize(); 209738af7a6SJim Ingham 210405fe67fSCaroline Tice CommandArgumentEntry arg; 211405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 212405fe67fSCaroline Tice 213405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 214405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 2151bb0750bSJim Ingham bp_id_arg.arg_repetition = eArgRepeatOptional; 216405fe67fSCaroline Tice 217b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 218b9c1b51eSKate Stone // argument entry. 219405fe67fSCaroline Tice arg.push_back(bp_id_arg); 220405fe67fSCaroline Tice 221405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 222405fe67fSCaroline Tice m_arguments.push_back(arg); 22330fdc8d8SChris Lattner } 22430fdc8d8SChris Lattner 225c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandAdd() override = default; 2265a988416SJim Ingham 227738af7a6SJim Ingham Options *GetOptions() override { return &m_all_options; } 2285a988416SJim Ingham 2290affb582SDave Lee void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 2307ca15ba7SLawrence D'Anna StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 2310affb582SDave Lee if (output_sp && interactive) { 23244d93782SGreg Clayton output_sp->PutCString(g_reader_instructions); 23344d93782SGreg Clayton output_sp->Flush(); 23444d93782SGreg Clayton } 23544d93782SGreg Clayton } 23644d93782SGreg Clayton 237b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 238b9c1b51eSKate Stone std::string &line) override { 23944d93782SGreg Clayton io_handler.SetIsDone(true); 24044d93782SGreg Clayton 241b9c1b51eSKate Stone std::vector<BreakpointOptions *> *bp_options_vec = 242b9c1b51eSKate Stone (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 243b9c1b51eSKate Stone for (BreakpointOptions *bp_options : *bp_options_vec) { 244b5796cb4SJim Ingham if (!bp_options) 245b5796cb4SJim Ingham continue; 246b5796cb4SJim Ingham 247a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 248e14dc268SJim Ingham cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 24992d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 25044d93782SGreg Clayton } 25144d93782SGreg Clayton } 25244d93782SGreg Clayton 253b9c1b51eSKate Stone void CollectDataForBreakpointCommandCallback( 254b9c1b51eSKate Stone std::vector<BreakpointOptions *> &bp_options_vec, 255b9c1b51eSKate Stone CommandReturnObject &result) { 256b9c1b51eSKate Stone m_interpreter.GetLLDBCommandsFromIOHandler( 257b9c1b51eSKate Stone "> ", // Prompt 25844d93782SGreg Clayton *this, // IOHandlerDelegate 25944d93782SGreg Clayton true, // Run IOHandler in async mode 260b9c1b51eSKate Stone &bp_options_vec); // Baton for the "io_handler" that will be passed back 261b9c1b51eSKate Stone // into our IOHandlerDelegate functions 2625a988416SJim Ingham } 2635a988416SJim Ingham 2645a988416SJim Ingham /// Set a one-liner as the callback for the breakpoint. 2655a988416SJim Ingham void 266b5796cb4SJim Ingham SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 267b9c1b51eSKate Stone const char *oneliner) { 268b9c1b51eSKate Stone for (auto bp_options : bp_options_vec) { 269a8f3ae7cSJonas Devlieghere auto cmd_data = std::make_unique<BreakpointOptions::CommandData>(); 2705a988416SJim Ingham 271e14dc268SJim Ingham cmd_data->user_source.AppendString(oneliner); 272e14dc268SJim Ingham cmd_data->stop_on_error = m_options.m_stop_on_error; 2735a988416SJim Ingham 27492d1960eSJim Ingham bp_options->SetCommandDataCallback(cmd_data); 275b5796cb4SJim Ingham } 2765a988416SJim Ingham } 2775a988416SJim Ingham 278738af7a6SJim Ingham class CommandOptions : public OptionGroup { 2795a988416SJim Ingham public: 280b9c1b51eSKate Stone CommandOptions() 281738af7a6SJim Ingham : OptionGroup(), m_use_commands(false), m_use_script_language(false), 282b9c1b51eSKate Stone m_script_language(eScriptLanguageNone), m_use_one_liner(false), 283738af7a6SJim Ingham m_one_liner() {} 28430fdc8d8SChris Lattner 285c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 2865a988416SJim Ingham 28797206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 288b9c1b51eSKate Stone ExecutionContext *execution_context) override { 28997206d57SZachary Turner Status error; 290*a925974bSAdrian Prantl const int short_option = 291*a925974bSAdrian Prantl g_breakpoint_command_add_options[option_idx].short_option; 2925a988416SJim Ingham 293b9c1b51eSKate Stone switch (short_option) { 2945a988416SJim Ingham case 'o': 2955a988416SJim Ingham m_use_one_liner = true; 2965a988416SJim Ingham m_one_liner = option_arg; 2975a988416SJim Ingham break; 2985a988416SJim Ingham 2995a988416SJim Ingham case 's': 30047cbf4a0SPavel Labath m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 301bd68a052SRaphael Isemann option_arg, 302bd68a052SRaphael Isemann g_breakpoint_command_add_options[option_idx].enum_values, 303b9c1b51eSKate Stone eScriptLanguageNone, error); 3045a988416SJim Ingham 305b9c1b51eSKate Stone if (m_script_language == eScriptLanguagePython || 306b9c1b51eSKate Stone m_script_language == eScriptLanguageDefault) { 3075a988416SJim Ingham m_use_script_language = true; 308b9c1b51eSKate Stone } else { 3095a988416SJim Ingham m_use_script_language = false; 3105a988416SJim Ingham } 3115a988416SJim Ingham break; 3125a988416SJim Ingham 313b9c1b51eSKate Stone case 'e': { 3145a988416SJim Ingham bool success = false; 31547cbf4a0SPavel Labath m_stop_on_error = 31647cbf4a0SPavel Labath OptionArgParser::ToBoolean(option_arg, false, &success); 3175a988416SJim Ingham if (!success) 318b9c1b51eSKate Stone error.SetErrorStringWithFormat( 319fe11483bSZachary Turner "invalid value for stop-on-error: \"%s\"", 320fe11483bSZachary Turner option_arg.str().c_str()); 321b9c1b51eSKate Stone } break; 3225a988416SJim Ingham 32333df7cd3SJim Ingham case 'D': 32433df7cd3SJim Ingham m_use_dummy = true; 32533df7cd3SJim Ingham break; 32633df7cd3SJim Ingham 3275a988416SJim Ingham default: 32836162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 3295a988416SJim Ingham } 3305a988416SJim Ingham return error; 3315a988416SJim Ingham } 332c8ecc2a9SEugene Zelenko 333b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 3345a988416SJim Ingham m_use_commands = true; 3355a988416SJim Ingham m_use_script_language = false; 3365a988416SJim Ingham m_script_language = eScriptLanguageNone; 3375a988416SJim Ingham 3385a988416SJim Ingham m_use_one_liner = false; 3395a988416SJim Ingham m_stop_on_error = true; 3405a988416SJim Ingham m_one_liner.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; 35833df7cd3SJim Ingham bool m_use_dummy; 3595a988416SJim Ingham }; 3605a988416SJim Ingham 3615a988416SJim Ingham protected: 362b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 363cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 36430fdc8d8SChris Lattner 365cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 36630fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 36730fdc8d8SChris Lattner 368b9c1b51eSKate Stone if (num_breakpoints == 0) { 36930fdc8d8SChris Lattner result.AppendError("No breakpoints exist to have commands added"); 37030fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 37130fdc8d8SChris Lattner return false; 37230fdc8d8SChris Lattner } 37330fdc8d8SChris Lattner 374738af7a6SJim Ingham if (!m_func_options.GetName().empty()) { 375738af7a6SJim Ingham m_options.m_use_one_liner = false; 376738af7a6SJim Ingham m_options.m_use_script_language = true; 3778d4a8010SEnrico Granata } 3788d4a8010SEnrico Granata 37930fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 380b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 381cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 382b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 38330fdc8d8SChris Lattner 384b5796cb4SJim Ingham m_bp_options_vec.clear(); 385b5796cb4SJim Ingham 386b9c1b51eSKate Stone if (result.Succeeded()) { 387c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 388d9916eaeSJim Ingham 389b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 39030fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 391b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 392b9c1b51eSKate Stone Breakpoint *bp = 393cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 394c8ecc2a9SEugene Zelenko BreakpointOptions *bp_options = nullptr; 395b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 39639d7d4f0SJohnny Chen // This breakpoint does not have an associated location. 39739d7d4f0SJohnny Chen bp_options = bp->GetOptions(); 398b9c1b51eSKate Stone } else { 399b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 400b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 40105097246SAdrian Prantl // This breakpoint does have an associated location. Get its 40205097246SAdrian Prantl // breakpoint options. 40330fdc8d8SChris Lattner if (bp_loc_sp) 40439d7d4f0SJohnny Chen bp_options = bp_loc_sp->GetLocationOptions(); 40539d7d4f0SJohnny Chen } 406b5796cb4SJim Ingham if (bp_options) 407b5796cb4SJim Ingham m_bp_options_vec.push_back(bp_options); 408b5796cb4SJim Ingham } 409b5796cb4SJim Ingham } 41039d7d4f0SJohnny Chen 41105097246SAdrian Prantl // If we are using script language, get the script interpreter in order 41205097246SAdrian Prantl // to set or collect command callback. Otherwise, call the methods 41305097246SAdrian Prantl // associated with this object. 414b9c1b51eSKate Stone if (m_options.m_use_script_language) { 4152b29b432SJonas Devlieghere ScriptInterpreter *script_interp = GetDebugger().GetScriptInterpreter(); 41639d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 417b9c1b51eSKate Stone if (m_options.m_use_one_liner) { 418b9c1b51eSKate Stone script_interp->SetBreakpointCommandCallback( 419b9c1b51eSKate Stone m_bp_options_vec, m_options.m_one_liner.c_str()); 420738af7a6SJim Ingham } else if (!m_func_options.GetName().empty()) { 421738af7a6SJim Ingham Status error = script_interp->SetBreakpointCommandCallbackFunction( 422738af7a6SJim Ingham m_bp_options_vec, m_func_options.GetName().c_str(), 423738af7a6SJim Ingham m_func_options.GetStructuredData()); 424738af7a6SJim Ingham if (!error.Success()) 425738af7a6SJim Ingham result.SetError(error); 426b9c1b51eSKate Stone } else { 427b9c1b51eSKate Stone script_interp->CollectDataForBreakpointCommandCallback( 428b9c1b51eSKate Stone m_bp_options_vec, result); 4298d4a8010SEnrico Granata } 430b9c1b51eSKate Stone } else { 43139d7d4f0SJohnny Chen // Special handling for one-liner specified inline. 43239d7d4f0SJohnny Chen if (m_options.m_use_one_liner) 433b5796cb4SJim Ingham SetBreakpointCommandCallback(m_bp_options_vec, 43439d7d4f0SJohnny Chen m_options.m_one_liner.c_str()); 43539d7d4f0SJohnny Chen else 436b9c1b51eSKate Stone CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 43730fdc8d8SChris Lattner } 43830fdc8d8SChris Lattner } 43930fdc8d8SChris Lattner 44030fdc8d8SChris Lattner return result.Succeeded(); 44130fdc8d8SChris Lattner } 44230fdc8d8SChris Lattner 4435a988416SJim Ingham private: 4445a988416SJim Ingham CommandOptions m_options; 445738af7a6SJim Ingham OptionGroupPythonClassWithDict m_func_options; 446738af7a6SJim Ingham OptionGroupOptions m_all_options; 447738af7a6SJim Ingham 448b9c1b51eSKate Stone std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 449b9c1b51eSKate Stone // breakpoint options that 450b9c1b51eSKate Stone // we are currently 45105097246SAdrian Prantl // collecting commands for. In the CollectData... calls we need to hand this 45205097246SAdrian Prantl // off to the IOHandler, which may run asynchronously. So we have to have 45305097246SAdrian Prantl // some way to keep it alive, and not leak it. Making it an ivar of the 45405097246SAdrian Prantl // command object, which never goes away achieves this. Note that if we were 45505097246SAdrian Prantl // able to run the same command concurrently in one interpreter we'd have to 45605097246SAdrian Prantl // make this "per invocation". But there are many more reasons why it is not 45705097246SAdrian Prantl // in general safe to do that in lldb at present, so it isn't worthwhile to 45805097246SAdrian Prantl // come up with a more complex mechanism to address this particular weakness 45905097246SAdrian Prantl // right now. 4605a988416SJim Ingham static const char *g_reader_instructions; 4615a988416SJim Ingham }; 4625a988416SJim Ingham 463b9c1b51eSKate Stone const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 464b9c1b51eSKate Stone "Enter your debugger command(s). Type 'DONE' to end.\n"; 4655a988416SJim Ingham 46693e0f19fSCaroline Tice // CommandObjectBreakpointCommandDelete 46730fdc8d8SChris Lattner 468f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_command_delete 469f94668e3SRaphael Isemann #include "CommandOptions.inc" 4701f0f5b5bSZachary Turner 471b9c1b51eSKate Stone class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 4725a988416SJim Ingham public: 473b9c1b51eSKate Stone CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 474b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "delete", 47593e0f19fSCaroline Tice "Delete the set of commands from a breakpoint.", 476c8ecc2a9SEugene Zelenko nullptr), 477b9c1b51eSKate Stone m_options() { 478405fe67fSCaroline Tice CommandArgumentEntry arg; 479405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 480405fe67fSCaroline Tice 481405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 482405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 483405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 484405fe67fSCaroline Tice 485b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 486b9c1b51eSKate Stone // argument entry. 487405fe67fSCaroline Tice arg.push_back(bp_id_arg); 488405fe67fSCaroline Tice 489405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 490405fe67fSCaroline Tice m_arguments.push_back(arg); 49130fdc8d8SChris Lattner } 49230fdc8d8SChris Lattner 493c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandDelete() override = default; 4945a988416SJim Ingham 495b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 49633df7cd3SJim Ingham 497b9c1b51eSKate Stone class CommandOptions : public Options { 49833df7cd3SJim Ingham public: 499b9c1b51eSKate Stone CommandOptions() : Options(), m_use_dummy(false) {} 50033df7cd3SJim Ingham 501c8ecc2a9SEugene Zelenko ~CommandOptions() override = default; 50233df7cd3SJim Ingham 50397206d57SZachary Turner Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 504b9c1b51eSKate Stone ExecutionContext *execution_context) override { 50597206d57SZachary Turner Status error; 50633df7cd3SJim Ingham const int short_option = m_getopt_table[option_idx].val; 50733df7cd3SJim Ingham 508b9c1b51eSKate Stone switch (short_option) { 50933df7cd3SJim Ingham case 'D': 51033df7cd3SJim Ingham m_use_dummy = true; 51133df7cd3SJim Ingham break; 51233df7cd3SJim Ingham 51333df7cd3SJim Ingham default: 51436162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 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 { 525bd68a052SRaphael 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 { 534cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 53530fdc8d8SChris Lattner 536cb2380c9SRaphael Isemann const BreakpointList &breakpoints = target.GetBreakpointList(); 53730fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 53830fdc8d8SChris Lattner 539b9c1b51eSKate Stone if (num_breakpoints == 0) { 54093e0f19fSCaroline Tice result.AppendError("No breakpoints exist to have commands deleted"); 54130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54230fdc8d8SChris Lattner return false; 54330fdc8d8SChris Lattner } 54430fdc8d8SChris Lattner 54511eb9c64SZachary Turner if (command.empty()) { 546b9c1b51eSKate Stone result.AppendError( 547b9c1b51eSKate Stone "No breakpoint specified from which to delete the commands"); 54830fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 54930fdc8d8SChris Lattner return false; 55030fdc8d8SChris Lattner } 55130fdc8d8SChris Lattner 55230fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 553b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 554cb2380c9SRaphael Isemann command, &target, result, &valid_bp_ids, 555b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 55630fdc8d8SChris Lattner 557b9c1b51eSKate Stone if (result.Succeeded()) { 558c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 559b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 56030fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 561b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 562b9c1b51eSKate Stone Breakpoint *bp = 563cb2380c9SRaphael Isemann target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 564b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 565b9c1b51eSKate Stone BreakpointLocationSP bp_loc_sp( 566b9c1b51eSKate Stone bp->FindLocationByID(cur_bp_id.GetLocationID())); 56730fdc8d8SChris Lattner if (bp_loc_sp) 56830fdc8d8SChris Lattner bp_loc_sp->ClearCallback(); 569b9c1b51eSKate Stone else { 57030fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 57130fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 57230fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 57330fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 57430fdc8d8SChris Lattner return false; 57530fdc8d8SChris Lattner } 576b9c1b51eSKate Stone } else { 57730fdc8d8SChris Lattner bp->ClearCallback(); 57830fdc8d8SChris Lattner } 57930fdc8d8SChris Lattner } 58030fdc8d8SChris Lattner } 58130fdc8d8SChris Lattner } 58230fdc8d8SChris Lattner return result.Succeeded(); 58330fdc8d8SChris Lattner } 584c8ecc2a9SEugene Zelenko 58533df7cd3SJim Ingham private: 58633df7cd3SJim Ingham CommandOptions m_options; 5875a988416SJim Ingham }; 58830fdc8d8SChris Lattner 58930fdc8d8SChris Lattner // CommandObjectBreakpointCommandList 59030fdc8d8SChris Lattner 591b9c1b51eSKate Stone class CommandObjectBreakpointCommandList : public CommandObjectParsed { 5925a988416SJim Ingham public: 593b9c1b51eSKate Stone CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 59404a4c091SRaphael Isemann : CommandObjectParsed(interpreter, "list", 59504a4c091SRaphael Isemann "List the script or set of commands to be " 59604a4c091SRaphael Isemann "executed when the breakpoint is hit.", 59704a4c091SRaphael Isemann nullptr, eCommandRequiresTarget) { 598405fe67fSCaroline Tice CommandArgumentEntry arg; 599405fe67fSCaroline Tice CommandArgumentData bp_id_arg; 600405fe67fSCaroline Tice 601405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 602405fe67fSCaroline Tice bp_id_arg.arg_type = eArgTypeBreakpointID; 603405fe67fSCaroline Tice bp_id_arg.arg_repetition = eArgRepeatPlain; 604405fe67fSCaroline Tice 605b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 606b9c1b51eSKate Stone // argument entry. 607405fe67fSCaroline Tice arg.push_back(bp_id_arg); 608405fe67fSCaroline Tice 609405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 610405fe67fSCaroline Tice m_arguments.push_back(arg); 61130fdc8d8SChris Lattner } 61230fdc8d8SChris Lattner 613c8ecc2a9SEugene Zelenko ~CommandObjectBreakpointCommandList() override = default; 61430fdc8d8SChris Lattner 6155a988416SJim Ingham protected: 616b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 61704a4c091SRaphael Isemann Target *target = &GetSelectedTarget(); 61830fdc8d8SChris Lattner 61930fdc8d8SChris Lattner const BreakpointList &breakpoints = target->GetBreakpointList(); 62030fdc8d8SChris Lattner size_t num_breakpoints = breakpoints.GetSize(); 62130fdc8d8SChris Lattner 622b9c1b51eSKate Stone if (num_breakpoints == 0) { 62330fdc8d8SChris Lattner result.AppendError("No breakpoints exist for which to list commands"); 62430fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 62530fdc8d8SChris Lattner return false; 62630fdc8d8SChris Lattner } 62730fdc8d8SChris Lattner 62811eb9c64SZachary Turner if (command.empty()) { 629b9c1b51eSKate Stone result.AppendError( 630b9c1b51eSKate Stone "No breakpoint specified for which to list the commands"); 63130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 63230fdc8d8SChris Lattner return false; 63330fdc8d8SChris Lattner } 63430fdc8d8SChris Lattner 63530fdc8d8SChris Lattner BreakpointIDList valid_bp_ids; 636b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 637b842f2ecSJim Ingham command, target, result, &valid_bp_ids, 638b842f2ecSJim Ingham BreakpointName::Permissions::PermissionKinds::listPerm); 63930fdc8d8SChris Lattner 640b9c1b51eSKate Stone if (result.Succeeded()) { 641c982c768SGreg Clayton const size_t count = valid_bp_ids.GetSize(); 642b9c1b51eSKate Stone for (size_t i = 0; i < count; ++i) { 64330fdc8d8SChris Lattner BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 644b9c1b51eSKate Stone if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 645b9c1b51eSKate Stone Breakpoint *bp = 646b9c1b51eSKate Stone target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 64730fdc8d8SChris Lattner 648b9c1b51eSKate Stone if (bp) { 649af26b22cSJim Ingham BreakpointLocationSP bp_loc_sp; 650b9c1b51eSKate Stone if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 651af26b22cSJim Ingham bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 652*a925974bSAdrian Prantl if (!bp_loc_sp) { 65330fdc8d8SChris Lattner result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 65430fdc8d8SChris Lattner cur_bp_id.GetBreakpointID(), 65530fdc8d8SChris Lattner cur_bp_id.GetLocationID()); 65630fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 65730fdc8d8SChris Lattner return false; 65830fdc8d8SChris Lattner } 65930fdc8d8SChris Lattner } 66030fdc8d8SChris Lattner 66130fdc8d8SChris Lattner StreamString id_str; 662e16c50a1SJim Ingham BreakpointID::GetCanonicalReference(&id_str, 663e16c50a1SJim Ingham cur_bp_id.GetBreakpointID(), 664e16c50a1SJim Ingham cur_bp_id.GetLocationID()); 665af26b22cSJim Ingham const Baton *baton = nullptr; 666af26b22cSJim Ingham if (bp_loc_sp) 667*a925974bSAdrian Prantl baton = 668*a925974bSAdrian Prantl bp_loc_sp 669af26b22cSJim Ingham ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 670af26b22cSJim Ingham ->GetBaton(); 671af26b22cSJim Ingham else 672af26b22cSJim Ingham baton = bp->GetOptions()->GetBaton(); 673af26b22cSJim Ingham 674b9c1b51eSKate Stone if (baton) { 675b9c1b51eSKate Stone result.GetOutputStream().Printf("Breakpoint %s:\n", 676b9c1b51eSKate Stone id_str.GetData()); 67730fdc8d8SChris Lattner result.GetOutputStream().IndentMore(); 678b9c1b51eSKate Stone baton->GetDescription(&result.GetOutputStream(), 679b9c1b51eSKate Stone eDescriptionLevelFull); 68030fdc8d8SChris Lattner result.GetOutputStream().IndentLess(); 681b9c1b51eSKate Stone } else { 682b9c1b51eSKate Stone result.AppendMessageWithFormat( 683b9c1b51eSKate Stone "Breakpoint %s does not have an associated command.\n", 684e16c50a1SJim Ingham id_str.GetData()); 68530fdc8d8SChris Lattner } 68630fdc8d8SChris Lattner } 68730fdc8d8SChris Lattner result.SetStatus(eReturnStatusSuccessFinishResult); 688b9c1b51eSKate Stone } else { 689b9c1b51eSKate Stone result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 690b9c1b51eSKate Stone cur_bp_id.GetBreakpointID()); 69130fdc8d8SChris Lattner result.SetStatus(eReturnStatusFailed); 69230fdc8d8SChris Lattner } 69330fdc8d8SChris Lattner } 69430fdc8d8SChris Lattner } 69530fdc8d8SChris Lattner 69630fdc8d8SChris Lattner return result.Succeeded(); 69730fdc8d8SChris Lattner } 6985a988416SJim Ingham }; 69930fdc8d8SChris Lattner 70030fdc8d8SChris Lattner // CommandObjectBreakpointCommand 70130fdc8d8SChris Lattner 702b9c1b51eSKate Stone CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 703b9c1b51eSKate Stone CommandInterpreter &interpreter) 7047428a18cSKate Stone : CommandObjectMultiword( 705*a925974bSAdrian Prantl interpreter, "command", 706*a925974bSAdrian Prantl "Commands for adding, removing and listing " 707b9c1b51eSKate Stone "LLDB commands executed when a breakpoint is " 708b9c1b51eSKate Stone "hit.", 709b9c1b51eSKate Stone "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 710b9c1b51eSKate Stone CommandObjectSP add_command_object( 711b9c1b51eSKate Stone new CommandObjectBreakpointCommandAdd(interpreter)); 712b9c1b51eSKate Stone CommandObjectSP delete_command_object( 713b9c1b51eSKate Stone new CommandObjectBreakpointCommandDelete(interpreter)); 714b9c1b51eSKate Stone CommandObjectSP list_command_object( 715b9c1b51eSKate Stone new CommandObjectBreakpointCommandList(interpreter)); 71630fdc8d8SChris Lattner 71730fdc8d8SChris Lattner add_command_object->SetCommandName("breakpoint command add"); 71893e0f19fSCaroline Tice delete_command_object->SetCommandName("breakpoint command delete"); 71930fdc8d8SChris Lattner list_command_object->SetCommandName("breakpoint command list"); 72030fdc8d8SChris Lattner 72123f59509SGreg Clayton LoadSubCommand("add", add_command_object); 72223f59509SGreg Clayton LoadSubCommand("delete", delete_command_object); 72323f59509SGreg Clayton LoadSubCommand("list", list_command_object); 72430fdc8d8SChris Lattner } 72530fdc8d8SChris Lattner 726c8ecc2a9SEugene Zelenko CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 727