1 //===-- CommandObjectBreakpointCommand.cpp ----------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CommandObjectBreakpointCommand.h" 10 #include "CommandObjectBreakpoint.h" 11 #include "lldb/Breakpoint/Breakpoint.h" 12 #include "lldb/Breakpoint/BreakpointIDList.h" 13 #include "lldb/Breakpoint/BreakpointLocation.h" 14 #include "lldb/Breakpoint/StoppointCallbackContext.h" 15 #include "lldb/Core/IOHandler.h" 16 #include "lldb/Host/OptionParser.h" 17 #include "lldb/Interpreter/CommandInterpreter.h" 18 #include "lldb/Interpreter/CommandReturnObject.h" 19 #include "lldb/Interpreter/OptionArgParser.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Target/Thread.h" 22 #include "lldb/Utility/State.h" 23 24 #include "llvm/ADT/STLExtras.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 //------------------------------------------------------------------------- 30 // CommandObjectBreakpointCommandAdd 31 //------------------------------------------------------------------------- 32 33 // FIXME: "script-type" needs to have its contents determined dynamically, so 34 // somebody can add a new scripting 35 // language to lldb and have it pickable here without having to change this 36 // enumeration by hand and rebuild lldb proper. 37 38 static constexpr OptionEnumValueElement g_script_option_enumeration[] = { 39 {eScriptLanguageNone, "command", 40 "Commands are in the lldb command interpreter language"}, 41 {eScriptLanguagePython, "python", "Commands are in the Python language."}, 42 {eSortOrderByName, "default-script", 43 "Commands are in the default scripting language."} }; 44 45 static constexpr OptionEnumValues ScriptOptionEnum() { 46 return OptionEnumValues(g_script_option_enumeration); 47 } 48 49 static constexpr OptionDefinition g_breakpoint_add_options[] = { 50 // clang-format off 51 { LLDB_OPT_SET_1, false, "one-liner", 'o', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeOneLiner, "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." }, 52 { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean, "Specify whether breakpoint command execution should terminate on error." }, 53 { LLDB_OPT_SET_ALL, false, "script-type", 's', OptionParser::eRequiredArgument, nullptr, ScriptOptionEnum(), 0, eArgTypeNone, "Specify the language for the commands - if none is specified, the lldb command interpreter will be used." }, 54 { LLDB_OPT_SET_2, false, "python-function", 'F', OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypePythonFunction, "Give the name of a Python function to run as command for this breakpoint. Be sure to give a module name if appropriate." }, 55 { LLDB_OPT_SET_ALL, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Sets Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets." }, 56 // clang-format on 57 }; 58 59 class CommandObjectBreakpointCommandAdd : public CommandObjectParsed, 60 public IOHandlerDelegateMultiline { 61 public: 62 CommandObjectBreakpointCommandAdd(CommandInterpreter &interpreter) 63 : CommandObjectParsed(interpreter, "add", 64 "Add LLDB commands to a breakpoint, to be executed " 65 "whenever the breakpoint is hit." 66 " If no breakpoint is specified, adds the " 67 "commands to the last created breakpoint.", 68 nullptr), 69 IOHandlerDelegateMultiline("DONE", 70 IOHandlerDelegate::Completion::LLDBCommand), 71 m_options() { 72 SetHelpLong( 73 R"( 74 General information about entering breakpoint commands 75 ------------------------------------------------------ 76 77 )" 78 "This command will prompt for commands to be executed when the specified \ 79 breakpoint is hit. Each command is typed on its own line following the '> ' \ 80 prompt until 'DONE' is entered." 81 R"( 82 83 )" 84 "Syntactic errors may not be detected when initially entered, and many \ 85 malformed commands can silently fail when executed. If your breakpoint commands \ 86 do not appear to be executing, double-check the command syntax." 87 R"( 88 89 )" 90 "Note: You may enter any debugger command exactly as you would at the debugger \ 91 prompt. There is no limit to the number of commands supplied, but do NOT enter \ 92 more than one command per line." 93 R"( 94 95 Special information about PYTHON breakpoint commands 96 ---------------------------------------------------- 97 98 )" 99 "You may enter either one or more lines of Python, including function \ 100 definitions or calls to functions that will have been imported by the time \ 101 the code executes. Single line breakpoint commands will be interpreted 'as is' \ 102 when the breakpoint is hit. Multiple lines of Python will be wrapped in a \ 103 generated function, and a call to the function will be attached to the breakpoint." 104 R"( 105 106 This auto-generated function is passed in three arguments: 107 108 frame: an lldb.SBFrame object for the frame which hit breakpoint. 109 110 bp_loc: an lldb.SBBreakpointLocation object that represents the breakpoint location that was hit. 111 112 dict: the python session dictionary hit. 113 114 )" 115 "When specifying a python function with the --python-function option, you need \ 116 to supply the function name prepended by the module name:" 117 R"( 118 119 --python-function myutils.breakpoint_callback 120 121 The function itself must have the following prototype: 122 123 def breakpoint_callback(frame, bp_loc, dict): 124 # Your code goes here 125 126 )" 127 "The arguments are the same as the arguments passed to generated functions as \ 128 described above. Note that the global variable 'lldb.frame' will NOT be updated when \ 129 this function is called, so be sure to use the 'frame' argument. The 'frame' argument \ 130 can get you to the thread via frame.GetThread(), the thread can get you to the \ 131 process via thread.GetProcess(), and the process can get you back to the target \ 132 via process.GetTarget()." 133 R"( 134 135 )" 136 "Important Note: As Python code gets collected into functions, access to global \ 137 variables requires explicit scoping using the 'global' keyword. Be sure to use correct \ 138 Python syntax, including indentation, when entering Python breakpoint commands." 139 R"( 140 141 Example Python one-line breakpoint command: 142 143 (lldb) breakpoint command add -s python 1 144 Enter your Python command(s). Type 'DONE' to end. 145 > print "Hit this breakpoint!" 146 > DONE 147 148 As a convenience, this also works for a short Python one-liner: 149 150 (lldb) breakpoint command add -s python 1 -o 'import time; print time.asctime()' 151 (lldb) run 152 Launching '.../a.out' (x86_64) 153 (lldb) Fri Sep 10 12:17:45 2010 154 Process 21778 Stopped 155 * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread 156 36 157 37 int c(int val) 158 38 { 159 39 -> return val + 3; 160 40 } 161 41 162 42 int main (int argc, char const *argv[]) 163 164 Example multiple line Python breakpoint command: 165 166 (lldb) breakpoint command add -s p 1 167 Enter your Python command(s). Type 'DONE' to end. 168 > global bp_count 169 > bp_count = bp_count + 1 170 > print "Hit this breakpoint " + repr(bp_count) + " times!" 171 > DONE 172 173 Example multiple line Python breakpoint command, using function definition: 174 175 (lldb) breakpoint command add -s python 1 176 Enter your Python command(s). Type 'DONE' to end. 177 > def breakpoint_output (bp_no): 178 > out_string = "Hit breakpoint number " + repr (bp_no) 179 > print out_string 180 > return True 181 > breakpoint_output (1) 182 > DONE 183 184 )" 185 "In this case, since there is a reference to a global variable, \ 186 'bp_count', you will also need to make sure 'bp_count' exists and is \ 187 initialized:" 188 R"( 189 190 (lldb) script 191 >>> bp_count = 0 192 >>> quit() 193 194 )" 195 "Your Python code, however organized, can optionally return a value. \ 196 If the returned value is False, that tells LLDB not to stop at the breakpoint \ 197 to which the code is associated. Returning anything other than False, or even \ 198 returning None, or even omitting a return statement entirely, will cause \ 199 LLDB to stop." 200 R"( 201 202 )" 203 "Final Note: A warning that no breakpoint command was generated when there \ 204 are no syntax errors may indicate that a function was declared but never called."); 205 206 CommandArgumentEntry arg; 207 CommandArgumentData bp_id_arg; 208 209 // Define the first (and only) variant of this arg. 210 bp_id_arg.arg_type = eArgTypeBreakpointID; 211 bp_id_arg.arg_repetition = eArgRepeatOptional; 212 213 // There is only one variant this argument could be; put it into the 214 // argument entry. 215 arg.push_back(bp_id_arg); 216 217 // Push the data for the first argument into the m_arguments vector. 218 m_arguments.push_back(arg); 219 } 220 221 ~CommandObjectBreakpointCommandAdd() override = default; 222 223 Options *GetOptions() override { return &m_options; } 224 225 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 226 StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 227 if (output_sp && interactive) { 228 output_sp->PutCString(g_reader_instructions); 229 output_sp->Flush(); 230 } 231 } 232 233 void IOHandlerInputComplete(IOHandler &io_handler, 234 std::string &line) override { 235 io_handler.SetIsDone(true); 236 237 std::vector<BreakpointOptions *> *bp_options_vec = 238 (std::vector<BreakpointOptions *> *)io_handler.GetUserData(); 239 for (BreakpointOptions *bp_options : *bp_options_vec) { 240 if (!bp_options) 241 continue; 242 243 auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 244 cmd_data->user_source.SplitIntoLines(line.c_str(), line.size()); 245 bp_options->SetCommandDataCallback(cmd_data); 246 } 247 } 248 249 void CollectDataForBreakpointCommandCallback( 250 std::vector<BreakpointOptions *> &bp_options_vec, 251 CommandReturnObject &result) { 252 m_interpreter.GetLLDBCommandsFromIOHandler( 253 "> ", // Prompt 254 *this, // IOHandlerDelegate 255 true, // Run IOHandler in async mode 256 &bp_options_vec); // Baton for the "io_handler" that will be passed back 257 // into our IOHandlerDelegate functions 258 } 259 260 /// Set a one-liner as the callback for the breakpoint. 261 void 262 SetBreakpointCommandCallback(std::vector<BreakpointOptions *> &bp_options_vec, 263 const char *oneliner) { 264 for (auto bp_options : bp_options_vec) { 265 auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>(); 266 267 cmd_data->user_source.AppendString(oneliner); 268 cmd_data->stop_on_error = m_options.m_stop_on_error; 269 270 bp_options->SetCommandDataCallback(cmd_data); 271 } 272 } 273 274 class CommandOptions : public Options { 275 public: 276 CommandOptions() 277 : Options(), m_use_commands(false), m_use_script_language(false), 278 m_script_language(eScriptLanguageNone), m_use_one_liner(false), 279 m_one_liner(), m_function_name() {} 280 281 ~CommandOptions() override = default; 282 283 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 284 ExecutionContext *execution_context) override { 285 Status error; 286 const int short_option = m_getopt_table[option_idx].val; 287 288 switch (short_option) { 289 case 'o': 290 m_use_one_liner = true; 291 m_one_liner = option_arg; 292 break; 293 294 case 's': 295 m_script_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum( 296 option_arg, g_breakpoint_add_options[option_idx].enum_values, 297 eScriptLanguageNone, error); 298 299 if (m_script_language == eScriptLanguagePython || 300 m_script_language == eScriptLanguageDefault) { 301 m_use_script_language = true; 302 } else { 303 m_use_script_language = false; 304 } 305 break; 306 307 case 'e': { 308 bool success = false; 309 m_stop_on_error = 310 OptionArgParser::ToBoolean(option_arg, false, &success); 311 if (!success) 312 error.SetErrorStringWithFormat( 313 "invalid value for stop-on-error: \"%s\"", 314 option_arg.str().c_str()); 315 } break; 316 317 case 'F': 318 m_use_one_liner = false; 319 m_use_script_language = true; 320 m_function_name.assign(option_arg); 321 break; 322 323 case 'D': 324 m_use_dummy = true; 325 break; 326 327 default: 328 break; 329 } 330 return error; 331 } 332 333 void OptionParsingStarting(ExecutionContext *execution_context) override { 334 m_use_commands = true; 335 m_use_script_language = false; 336 m_script_language = eScriptLanguageNone; 337 338 m_use_one_liner = false; 339 m_stop_on_error = true; 340 m_one_liner.clear(); 341 m_function_name.clear(); 342 m_use_dummy = false; 343 } 344 345 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 346 return llvm::makeArrayRef(g_breakpoint_add_options); 347 } 348 349 // Instance variables to hold the values for command options. 350 351 bool m_use_commands; 352 bool m_use_script_language; 353 lldb::ScriptLanguage m_script_language; 354 355 // Instance variables to hold the values for one_liner options. 356 bool m_use_one_liner; 357 std::string m_one_liner; 358 bool m_stop_on_error; 359 std::string m_function_name; 360 bool m_use_dummy; 361 }; 362 363 protected: 364 bool DoExecute(Args &command, CommandReturnObject &result) override { 365 Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 366 367 if (target == nullptr) { 368 result.AppendError("There is not a current executable; there are no " 369 "breakpoints to which to add commands"); 370 result.SetStatus(eReturnStatusFailed); 371 return false; 372 } 373 374 const BreakpointList &breakpoints = target->GetBreakpointList(); 375 size_t num_breakpoints = breakpoints.GetSize(); 376 377 if (num_breakpoints == 0) { 378 result.AppendError("No breakpoints exist to have commands added"); 379 result.SetStatus(eReturnStatusFailed); 380 return false; 381 } 382 383 if (!m_options.m_use_script_language && 384 !m_options.m_function_name.empty()) { 385 result.AppendError("need to enable scripting to have a function run as a " 386 "breakpoint command"); 387 result.SetStatus(eReturnStatusFailed); 388 return false; 389 } 390 391 BreakpointIDList valid_bp_ids; 392 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 393 command, target, result, &valid_bp_ids, 394 BreakpointName::Permissions::PermissionKinds::listPerm); 395 396 m_bp_options_vec.clear(); 397 398 if (result.Succeeded()) { 399 const size_t count = valid_bp_ids.GetSize(); 400 401 for (size_t i = 0; i < count; ++i) { 402 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 403 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 404 Breakpoint *bp = 405 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 406 BreakpointOptions *bp_options = nullptr; 407 if (cur_bp_id.GetLocationID() == LLDB_INVALID_BREAK_ID) { 408 // This breakpoint does not have an associated location. 409 bp_options = bp->GetOptions(); 410 } else { 411 BreakpointLocationSP bp_loc_sp( 412 bp->FindLocationByID(cur_bp_id.GetLocationID())); 413 // This breakpoint does have an associated location. Get its 414 // breakpoint options. 415 if (bp_loc_sp) 416 bp_options = bp_loc_sp->GetLocationOptions(); 417 } 418 if (bp_options) 419 m_bp_options_vec.push_back(bp_options); 420 } 421 } 422 423 // If we are using script language, get the script interpreter in order 424 // to set or collect command callback. Otherwise, call the methods 425 // associated with this object. 426 if (m_options.m_use_script_language) { 427 ScriptInterpreter *script_interp = m_interpreter.GetScriptInterpreter(); 428 // Special handling for one-liner specified inline. 429 if (m_options.m_use_one_liner) { 430 script_interp->SetBreakpointCommandCallback( 431 m_bp_options_vec, m_options.m_one_liner.c_str()); 432 } else if (!m_options.m_function_name.empty()) { 433 script_interp->SetBreakpointCommandCallbackFunction( 434 m_bp_options_vec, m_options.m_function_name.c_str()); 435 } else { 436 script_interp->CollectDataForBreakpointCommandCallback( 437 m_bp_options_vec, result); 438 } 439 } else { 440 // Special handling for one-liner specified inline. 441 if (m_options.m_use_one_liner) 442 SetBreakpointCommandCallback(m_bp_options_vec, 443 m_options.m_one_liner.c_str()); 444 else 445 CollectDataForBreakpointCommandCallback(m_bp_options_vec, result); 446 } 447 } 448 449 return result.Succeeded(); 450 } 451 452 private: 453 CommandOptions m_options; 454 std::vector<BreakpointOptions *> m_bp_options_vec; // This stores the 455 // breakpoint options that 456 // we are currently 457 // collecting commands for. In the CollectData... calls we need to hand this 458 // off to the IOHandler, which may run asynchronously. So we have to have 459 // some way to keep it alive, and not leak it. Making it an ivar of the 460 // command object, which never goes away achieves this. Note that if we were 461 // able to run the same command concurrently in one interpreter we'd have to 462 // make this "per invocation". But there are many more reasons why it is not 463 // in general safe to do that in lldb at present, so it isn't worthwhile to 464 // come up with a more complex mechanism to address this particular weakness 465 // right now. 466 static const char *g_reader_instructions; 467 }; 468 469 const char *CommandObjectBreakpointCommandAdd::g_reader_instructions = 470 "Enter your debugger command(s). Type 'DONE' to end.\n"; 471 472 //------------------------------------------------------------------------- 473 // CommandObjectBreakpointCommandDelete 474 //------------------------------------------------------------------------- 475 476 static constexpr OptionDefinition g_breakpoint_delete_options[] = { 477 // clang-format off 478 { LLDB_OPT_SET_1, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Delete commands from Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets." }, 479 // clang-format on 480 }; 481 482 class CommandObjectBreakpointCommandDelete : public CommandObjectParsed { 483 public: 484 CommandObjectBreakpointCommandDelete(CommandInterpreter &interpreter) 485 : CommandObjectParsed(interpreter, "delete", 486 "Delete the set of commands from a breakpoint.", 487 nullptr), 488 m_options() { 489 CommandArgumentEntry arg; 490 CommandArgumentData bp_id_arg; 491 492 // Define the first (and only) variant of this arg. 493 bp_id_arg.arg_type = eArgTypeBreakpointID; 494 bp_id_arg.arg_repetition = eArgRepeatPlain; 495 496 // There is only one variant this argument could be; put it into the 497 // argument entry. 498 arg.push_back(bp_id_arg); 499 500 // Push the data for the first argument into the m_arguments vector. 501 m_arguments.push_back(arg); 502 } 503 504 ~CommandObjectBreakpointCommandDelete() override = default; 505 506 Options *GetOptions() override { return &m_options; } 507 508 class CommandOptions : public Options { 509 public: 510 CommandOptions() : Options(), m_use_dummy(false) {} 511 512 ~CommandOptions() override = default; 513 514 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 515 ExecutionContext *execution_context) override { 516 Status error; 517 const int short_option = m_getopt_table[option_idx].val; 518 519 switch (short_option) { 520 case 'D': 521 m_use_dummy = true; 522 break; 523 524 default: 525 error.SetErrorStringWithFormat("unrecognized option '%c'", 526 short_option); 527 break; 528 } 529 530 return error; 531 } 532 533 void OptionParsingStarting(ExecutionContext *execution_context) override { 534 m_use_dummy = false; 535 } 536 537 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 538 return llvm::makeArrayRef(g_breakpoint_delete_options); 539 } 540 541 // Instance variables to hold the values for command options. 542 bool m_use_dummy; 543 }; 544 545 protected: 546 bool DoExecute(Args &command, CommandReturnObject &result) override { 547 Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy); 548 549 if (target == nullptr) { 550 result.AppendError("There is not a current executable; there are no " 551 "breakpoints from which to delete commands"); 552 result.SetStatus(eReturnStatusFailed); 553 return false; 554 } 555 556 const BreakpointList &breakpoints = target->GetBreakpointList(); 557 size_t num_breakpoints = breakpoints.GetSize(); 558 559 if (num_breakpoints == 0) { 560 result.AppendError("No breakpoints exist to have commands deleted"); 561 result.SetStatus(eReturnStatusFailed); 562 return false; 563 } 564 565 if (command.empty()) { 566 result.AppendError( 567 "No breakpoint specified from which to delete the commands"); 568 result.SetStatus(eReturnStatusFailed); 569 return false; 570 } 571 572 BreakpointIDList valid_bp_ids; 573 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 574 command, target, result, &valid_bp_ids, 575 BreakpointName::Permissions::PermissionKinds::listPerm); 576 577 if (result.Succeeded()) { 578 const size_t count = valid_bp_ids.GetSize(); 579 for (size_t i = 0; i < count; ++i) { 580 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 581 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 582 Breakpoint *bp = 583 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 584 if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 585 BreakpointLocationSP bp_loc_sp( 586 bp->FindLocationByID(cur_bp_id.GetLocationID())); 587 if (bp_loc_sp) 588 bp_loc_sp->ClearCallback(); 589 else { 590 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 591 cur_bp_id.GetBreakpointID(), 592 cur_bp_id.GetLocationID()); 593 result.SetStatus(eReturnStatusFailed); 594 return false; 595 } 596 } else { 597 bp->ClearCallback(); 598 } 599 } 600 } 601 } 602 return result.Succeeded(); 603 } 604 605 private: 606 CommandOptions m_options; 607 }; 608 609 //------------------------------------------------------------------------- 610 // CommandObjectBreakpointCommandList 611 //------------------------------------------------------------------------- 612 613 class CommandObjectBreakpointCommandList : public CommandObjectParsed { 614 public: 615 CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) 616 : CommandObjectParsed(interpreter, "list", "List the script or set of " 617 "commands to be executed when " 618 "the breakpoint is hit.", 619 nullptr) { 620 CommandArgumentEntry arg; 621 CommandArgumentData bp_id_arg; 622 623 // Define the first (and only) variant of this arg. 624 bp_id_arg.arg_type = eArgTypeBreakpointID; 625 bp_id_arg.arg_repetition = eArgRepeatPlain; 626 627 // There is only one variant this argument could be; put it into the 628 // argument entry. 629 arg.push_back(bp_id_arg); 630 631 // Push the data for the first argument into the m_arguments vector. 632 m_arguments.push_back(arg); 633 } 634 635 ~CommandObjectBreakpointCommandList() override = default; 636 637 protected: 638 bool DoExecute(Args &command, CommandReturnObject &result) override { 639 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 640 641 if (target == nullptr) { 642 result.AppendError("There is not a current executable; there are no " 643 "breakpoints for which to list commands"); 644 result.SetStatus(eReturnStatusFailed); 645 return false; 646 } 647 648 const BreakpointList &breakpoints = target->GetBreakpointList(); 649 size_t num_breakpoints = breakpoints.GetSize(); 650 651 if (num_breakpoints == 0) { 652 result.AppendError("No breakpoints exist for which to list commands"); 653 result.SetStatus(eReturnStatusFailed); 654 return false; 655 } 656 657 if (command.empty()) { 658 result.AppendError( 659 "No breakpoint specified for which to list the commands"); 660 result.SetStatus(eReturnStatusFailed); 661 return false; 662 } 663 664 BreakpointIDList valid_bp_ids; 665 CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs( 666 command, target, result, &valid_bp_ids, 667 BreakpointName::Permissions::PermissionKinds::listPerm); 668 669 if (result.Succeeded()) { 670 const size_t count = valid_bp_ids.GetSize(); 671 for (size_t i = 0; i < count; ++i) { 672 BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i); 673 if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) { 674 Breakpoint *bp = 675 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get(); 676 677 if (bp) { 678 BreakpointLocationSP bp_loc_sp; 679 if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) { 680 bp_loc_sp = bp->FindLocationByID(cur_bp_id.GetLocationID()); 681 if (!bp_loc_sp) 682 { 683 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.%u.\n", 684 cur_bp_id.GetBreakpointID(), 685 cur_bp_id.GetLocationID()); 686 result.SetStatus(eReturnStatusFailed); 687 return false; 688 } 689 } 690 691 StreamString id_str; 692 BreakpointID::GetCanonicalReference(&id_str, 693 cur_bp_id.GetBreakpointID(), 694 cur_bp_id.GetLocationID()); 695 const Baton *baton = nullptr; 696 if (bp_loc_sp) 697 baton = bp_loc_sp 698 ->GetOptionsSpecifyingKind(BreakpointOptions::eCallback) 699 ->GetBaton(); 700 else 701 baton = bp->GetOptions()->GetBaton(); 702 703 if (baton) { 704 result.GetOutputStream().Printf("Breakpoint %s:\n", 705 id_str.GetData()); 706 result.GetOutputStream().IndentMore(); 707 baton->GetDescription(&result.GetOutputStream(), 708 eDescriptionLevelFull); 709 result.GetOutputStream().IndentLess(); 710 } else { 711 result.AppendMessageWithFormat( 712 "Breakpoint %s does not have an associated command.\n", 713 id_str.GetData()); 714 } 715 } 716 result.SetStatus(eReturnStatusSuccessFinishResult); 717 } else { 718 result.AppendErrorWithFormat("Invalid breakpoint ID: %u.\n", 719 cur_bp_id.GetBreakpointID()); 720 result.SetStatus(eReturnStatusFailed); 721 } 722 } 723 } 724 725 return result.Succeeded(); 726 } 727 }; 728 729 //------------------------------------------------------------------------- 730 // CommandObjectBreakpointCommand 731 //------------------------------------------------------------------------- 732 733 CommandObjectBreakpointCommand::CommandObjectBreakpointCommand( 734 CommandInterpreter &interpreter) 735 : CommandObjectMultiword( 736 interpreter, "command", "Commands for adding, removing and listing " 737 "LLDB commands executed when a breakpoint is " 738 "hit.", 739 "command <sub-command> [<sub-command-options>] <breakpoint-id>") { 740 CommandObjectSP add_command_object( 741 new CommandObjectBreakpointCommandAdd(interpreter)); 742 CommandObjectSP delete_command_object( 743 new CommandObjectBreakpointCommandDelete(interpreter)); 744 CommandObjectSP list_command_object( 745 new CommandObjectBreakpointCommandList(interpreter)); 746 747 add_command_object->SetCommandName("breakpoint command add"); 748 delete_command_object->SetCommandName("breakpoint command delete"); 749 list_command_object->SetCommandName("breakpoint command list"); 750 751 LoadSubCommand("add", add_command_object); 752 LoadSubCommand("delete", delete_command_object); 753 LoadSubCommand("list", list_command_object); 754 } 755 756 CommandObjectBreakpointCommand::~CommandObjectBreakpointCommand() = default; 757