26 #define LLDB_OPTIONS_breakpoint_command_add
27 #include "CommandOptions.inc"
34 "Add LLDB commands to a breakpoint, to be executed "
35 "whenever the breakpoint is hit. "
36 "The commands added to the breakpoint replace any "
37 "commands previously added to it."
38 " If no breakpoint is specified, adds the "
39 "commands to the last created breakpoint.",
43 m_func_options(
"breakpoint command", false,
'F') {
46 General information about entering breakpoint commands
47 ------------------------------------------------------
50 "This command will prompt for commands to be executed when the specified \
51 breakpoint is hit. Each command is typed on its own line following the '> ' \
52 prompt until 'DONE' is entered."
56 "Syntactic errors may not be detected when initially entered, and many \
57 malformed commands can silently fail when executed. If your breakpoint commands \
58 do not appear to be executing, double-check the command syntax."
62 "Note: You may enter any debugger command exactly as you would at the debugger \
63 prompt. There is no limit to the number of commands supplied, but do NOT enter \
64 more than one command per line."
67 Special information about PYTHON breakpoint commands
68 ----------------------------------------------------
71 "You may enter either one or more lines of Python, including function \
72 definitions or calls to functions that will have been imported by the time \
73 the code executes. Single line breakpoint commands will be interpreted 'as is' \
74 when the breakpoint is hit. Multiple lines of Python will be wrapped in a \
75 generated function, and a call to the function will be attached to the breakpoint."
78 This auto-generated function is passed in three arguments:
80 frame: an lldb.SBFrame object for the frame which hit breakpoint.
82 bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit.
84 dict: the python session dictionary hit.
87 "When specifying a python function with the --python-function option, you need \
88 to supply the function name prepended by the module name:"
91 --python-function myutils.breakpoint_callback
93 The function itself must have either of the following prototypes:
95 def breakpoint_callback(frame, bp_loc, internal_dict):
100 def breakpoint_callback(frame, bp_loc, extra_args, internal_dict):
101 # Your code goes here
104 "The arguments are the same as the arguments passed to generated functions as \
105 described above. In the second form, any -k and -v pairs provided to the command will \
106 be packaged into a SBDictionary in an SBStructuredData and passed as the extra_args parameter. \
108 Note that the global variable 'lldb.frame' will NOT be updated when \
109 this function is called, so be sure to use the 'frame' argument. The 'frame' argument \
110 can get you to the thread via frame.GetThread(), the thread can get you to the \
111 process via thread.GetProcess(), and the process can get you back to the target \
112 via process.GetTarget()."
116 "Important Note: As Python code gets collected into functions, access to global \
117 variables requires explicit scoping using the 'global' keyword. Be sure to use correct \
118 Python syntax, including indentation, when entering Python breakpoint commands."
121 Example Python one-line breakpoint command:
123 (lldb) breakpoint command add -s python 1
124 Enter your Python command(s). Type 'DONE' to end.
125 def function (frame, bp_loc, internal_dict):
126 """frame: the lldb.SBFrame for the location at which you stopped
127 bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
128 internal_dict: an LLDB support object not to be used"""
129 print("Hit this breakpoint!")
132 As a convenience, this also works for a short Python one-liner:
134 (lldb) breakpoint command add -s python 1 -o 'import time; print(time.asctime())'
136 Launching '.../a.out' (x86_64)
137 (lldb) Fri Sep 10 12:17:45 2010
138 Process 21778 Stopped
139 * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread
143 39 -> return val + 3;
146 42 int main (int argc, char const *argv[])
148 Example multiple line Python breakpoint command:
150 (lldb) breakpoint command add -s p 1
151 Enter your Python command(s). Type 'DONE' to end.
152 def function (frame, bp_loc, internal_dict):
153 """frame: the lldb.SBFrame for the location at which you stopped
154 bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
155 internal_dict: an LLDB support object not to be used"""
157 bp_count = bp_count + 1
158 print("Hit this breakpoint " + repr(bp_count) + " times!")
162 "In this case, since there is a reference to a global variable, \
163 'bp_count', you will also need to make sure 'bp_count' exists and is \
172 "Your Python code, however organized, can optionally return a value. \
173 If the returned value is False, that tells LLDB not to stop at the breakpoint \
174 to which the code is associated. Returning anything other than False, or even \
175 returning None, or even omitting a return statement entirely, will cause \
180 "Final Note: A warning that no breakpoint command was generated when there \
181 are no syntax errors may indicate that a function was declared but never called.");
183 m_all_options.Append(&m_options);
186 m_all_options.Finalize();
197 arg.push_back(bp_id_arg);
200 m_arguments.push_back(arg);
205 Options *GetOptions()
override {
return &m_all_options; }
207 void IOHandlerActivated(
IOHandler &io_handler,
bool interactive)
override {
209 if (output_sp && interactive) {
210 output_sp->PutCString(g_reader_instructions);
215 void IOHandlerInputComplete(
IOHandler &io_handler,
219 std::vector<std::reference_wrapper<BreakpointOptions>> *bp_options_vec =
220 (std::vector<std::reference_wrapper<BreakpointOptions>> *)
223 auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
224 cmd_data->user_source.SplitIntoLines(line.c_str(), line.size());
225 bp_options.SetCommandDataCallback(cmd_data);
229 void CollectDataForBreakpointCommandCallback(
230 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
232 m_interpreter.GetLLDBCommandsFromIOHandler(
240 void SetBreakpointCommandCallback(
241 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
242 const char *oneliner) {
244 auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
246 cmd_data->user_source.AppendString(oneliner);
247 cmd_data->stop_on_error = m_options.m_stop_on_error;
249 bp_options.SetCommandDataCallback(cmd_data);
255 CommandOptions() =
default;
257 ~CommandOptions()
override =
default;
259 Status SetOptionValue(
uint32_t option_idx, llvm::StringRef option_arg,
262 const int short_option =
263 g_breakpoint_command_add_options[option_idx].short_option;
265 switch (short_option) {
267 m_use_one_liner =
true;
274 g_breakpoint_command_add_options[option_idx].enum_values,
276 switch (m_script_language) {
279 m_use_script_language =
true;
283 m_use_script_language =
false;
289 bool success =
false;
291 OptionArgParser::ToBoolean(option_arg,
false, &success);
293 error.SetErrorStringWithFormat(
294 "invalid value for stop-on-error: \"%s\"",
295 option_arg.str().c_str());
303 llvm_unreachable(
"Unimplemented option");
309 m_use_commands =
true;
310 m_use_script_language =
false;
313 m_use_one_liner =
false;
314 m_stop_on_error =
true;
319 llvm::ArrayRef<OptionDefinition> GetDefinitions()
override {
320 return llvm::makeArrayRef(g_breakpoint_command_add_options);
325 bool m_use_commands =
false;
326 bool m_use_script_language =
false;
330 bool m_use_one_liner =
false;
332 bool m_stop_on_error;
338 Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
341 size_t num_breakpoints = breakpoints.
GetSize();
343 if (num_breakpoints == 0) {
344 result.
AppendError(
"No breakpoints exist to have commands added");
348 if (!m_func_options.GetName().empty()) {
349 m_options.m_use_one_liner =
false;
350 if (!m_options.m_use_script_language) {
351 m_options.m_script_language = GetDebugger().GetScriptLanguage();
352 m_options.m_use_script_language =
true;
357 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
358 command, &target, result, &valid_bp_ids,
359 BreakpointName::Permissions::PermissionKinds::listPerm);
361 m_bp_options_vec.clear();
364 const size_t count = valid_bp_ids.
GetSize();
366 for (
size_t i = 0; i < count; ++i) {
375 BreakpointLocationSP bp_loc_sp(
380 m_bp_options_vec.push_back(bp_loc_sp->GetLocationOptions());
388 if (m_options.m_use_script_language) {
391 true, m_options.m_script_language);
393 if (m_options.m_use_one_liner) {
395 m_bp_options_vec, m_options.m_one_liner.c_str());
396 }
else if (!m_func_options.GetName().empty()) {
398 m_bp_options_vec, m_func_options.GetName().c_str(),
399 m_func_options.GetStructuredData());
402 m_bp_options_vec, result);
404 if (!
error.Success())
408 if (m_options.m_use_one_liner)
409 SetBreakpointCommandCallback(m_bp_options_vec,
410 m_options.m_one_liner.c_str());
412 CollectDataForBreakpointCommandCallback(m_bp_options_vec, result);
420 CommandOptions m_options;
424 std::vector<std::reference_wrapper<BreakpointOptions>>
437 static const char *g_reader_instructions;
441 "Enter your debugger command(s). Type 'DONE' to end.\n";
445 #define LLDB_OPTIONS_breakpoint_command_delete
446 #include "CommandOptions.inc"
452 "Delete the set of commands from a breakpoint.",
463 arg.push_back(bp_id_arg);
466 m_arguments.push_back(arg);
482 const int short_option = m_getopt_table[option_idx].val;
484 switch (short_option) {
490 llvm_unreachable(
"Unimplemented option");
501 return llvm::makeArrayRef(g_breakpoint_command_delete_options);
505 bool m_use_dummy =
false;
510 Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
513 size_t num_breakpoints = breakpoints.
GetSize();
515 if (num_breakpoints == 0) {
516 result.
AppendError(
"No breakpoints exist to have commands deleted");
520 if (command.
empty()) {
522 "No breakpoint specified from which to delete the commands");
527 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
528 command, &target, result, &valid_bp_ids,
529 BreakpointName::Permissions::PermissionKinds::listPerm);
532 const size_t count = valid_bp_ids.
GetSize();
533 for (
size_t i = 0; i < count; ++i) {
539 BreakpointLocationSP bp_loc_sp(
542 bp_loc_sp->ClearCallback();
568 "List the script or set of commands to be "
569 "executed when the breakpoint is hit.",
570 nullptr, eCommandRequiresTarget) {
580 arg.push_back(bp_id_arg);
583 m_arguments.push_back(arg);
590 Target *target = &GetSelectedTarget();
593 size_t num_breakpoints = breakpoints.
GetSize();
595 if (num_breakpoints == 0) {
596 result.
AppendError(
"No breakpoints exist for which to list commands");
600 if (command.
empty()) {
602 "No breakpoint specified for which to list the commands");
607 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
608 command, target, result, &valid_bp_ids,
609 BreakpointName::Permissions::PermissionKinds::listPerm);
612 const size_t count = valid_bp_ids.
GetSize();
613 for (
size_t i = 0; i < count; ++i) {
620 BreakpointLocationSP bp_loc_sp;
632 BreakpointID::GetCanonicalReference(&id_str,
635 const Baton *baton =
nullptr;
639 ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback)
653 "Breakpoint %s does not have an associated command.\n",
671 CommandObjectBreakpointCommand::CommandObjectBreakpointCommand(
674 interpreter,
"command",
675 "Commands for adding, removing and listing "
676 "LLDB commands executed when a breakpoint is "
678 "command <sub-command> [<sub-command-options>] <breakpoint-id>") {
679 CommandObjectSP add_command_object(
681 CommandObjectSP delete_command_object(
683 CommandObjectSP list_command_object(
686 add_command_object->SetCommandName(
"breakpoint command add");
687 delete_command_object->SetCommandName(
"breakpoint command delete");
688 list_command_object->SetCommandName(
"breakpoint command list");