1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * debugger.c: Vim script debugger functions 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_EVAL) || defined(PROTO) 17 static int debug_greedy = FALSE; // batch mode debugging: don't save 18 // and restore typeahead. 19 static void do_setdebugtracelevel(char_u *arg); 20 static void do_checkbacktracelevel(void); 21 static void do_showbacktrace(char_u *cmd); 22 23 static char_u *debug_oldval = NULL; // old and newval for debug expressions 24 static char_u *debug_newval = NULL; 25 static int debug_expr = 0; // use debug_expr 26 27 int 28 has_watchexpr(void) 29 { 30 return debug_expr; 31 } 32 33 /* 34 * do_debug(): Debug mode. 35 * Repeatedly get Ex commands, until told to continue normal execution. 36 */ 37 void 38 do_debug(char_u *cmd) 39 { 40 int save_msg_scroll = msg_scroll; 41 int save_State = State; 42 int save_did_emsg = did_emsg; 43 int save_cmd_silent = cmd_silent; 44 int save_msg_silent = msg_silent; 45 int save_emsg_silent = emsg_silent; 46 int save_redir_off = redir_off; 47 tasave_T typeaheadbuf; 48 int typeahead_saved = FALSE; 49 int save_ignore_script = 0; 50 int save_ex_normal_busy; 51 int n; 52 char_u *cmdline = NULL; 53 char_u *p; 54 char_u *sname; 55 char *tail = NULL; 56 static int last_cmd = 0; 57 #define CMD_CONT 1 58 #define CMD_NEXT 2 59 #define CMD_STEP 3 60 #define CMD_FINISH 4 61 #define CMD_QUIT 5 62 #define CMD_INTERRUPT 6 63 #define CMD_BACKTRACE 7 64 #define CMD_FRAME 8 65 #define CMD_UP 9 66 #define CMD_DOWN 10 67 68 #ifdef ALWAYS_USE_GUI 69 // Can't do this when there is no terminal for input/output. 70 if (!gui.in_use) 71 { 72 // Break as soon as possible. 73 debug_break_level = 9999; 74 return; 75 } 76 #endif 77 78 // Make sure we are in raw mode and start termcap mode. Might have side 79 // effects... 80 settmode(TMODE_RAW); 81 starttermcap(); 82 83 ++RedrawingDisabled; // don't redisplay the window 84 ++no_wait_return; // don't wait for return 85 did_emsg = FALSE; // don't use error from debugged stuff 86 cmd_silent = FALSE; // display commands 87 msg_silent = FALSE; // display messages 88 emsg_silent = FALSE; // display error messages 89 redir_off = TRUE; // don't redirect debug commands 90 91 State = NORMAL; 92 debug_mode = TRUE; 93 94 if (!debug_did_msg) 95 msg(_("Entering Debug mode. Type \"cont\" to continue.")); 96 if (debug_oldval != NULL) 97 { 98 smsg(_("Oldval = \"%s\""), debug_oldval); 99 vim_free(debug_oldval); 100 debug_oldval = NULL; 101 } 102 if (debug_newval != NULL) 103 { 104 smsg(_("Newval = \"%s\""), debug_newval); 105 vim_free(debug_newval); 106 debug_newval = NULL; 107 } 108 sname = estack_sfile(ESTACK_NONE); 109 if (sname != NULL) 110 msg((char *)sname); 111 vim_free(sname); 112 if (SOURCING_LNUM != 0) 113 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd); 114 else 115 smsg(_("cmd: %s"), cmd); 116 117 // Repeat getting a command and executing it. 118 for (;;) 119 { 120 msg_scroll = TRUE; 121 need_wait_return = FALSE; 122 123 // Save the current typeahead buffer and replace it with an empty one. 124 // This makes sure we get input from the user here and don't interfere 125 // with the commands being executed. Reset "ex_normal_busy" to avoid 126 // the side effects of using ":normal". Save the stuff buffer and make 127 // it empty. Set ignore_script to avoid reading from script input. 128 save_ex_normal_busy = ex_normal_busy; 129 ex_normal_busy = 0; 130 if (!debug_greedy) 131 { 132 save_typeahead(&typeaheadbuf); 133 typeahead_saved = TRUE; 134 save_ignore_script = ignore_script; 135 ignore_script = TRUE; 136 } 137 138 vim_free(cmdline); 139 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL); 140 141 if (typeahead_saved) 142 { 143 restore_typeahead(&typeaheadbuf, TRUE); 144 ignore_script = save_ignore_script; 145 } 146 ex_normal_busy = save_ex_normal_busy; 147 148 cmdline_row = msg_row; 149 msg_starthere(); 150 if (cmdline != NULL) 151 { 152 // If this is a debug command, set "last_cmd". 153 // If not, reset "last_cmd". 154 // For a blank line use previous command. 155 p = skipwhite(cmdline); 156 if (*p != NUL) 157 { 158 switch (*p) 159 { 160 case 'c': last_cmd = CMD_CONT; 161 tail = "ont"; 162 break; 163 case 'n': last_cmd = CMD_NEXT; 164 tail = "ext"; 165 break; 166 case 's': last_cmd = CMD_STEP; 167 tail = "tep"; 168 break; 169 case 'f': 170 last_cmd = 0; 171 if (p[1] == 'r') 172 { 173 last_cmd = CMD_FRAME; 174 tail = "rame"; 175 } 176 else 177 { 178 last_cmd = CMD_FINISH; 179 tail = "inish"; 180 } 181 break; 182 case 'q': last_cmd = CMD_QUIT; 183 tail = "uit"; 184 break; 185 case 'i': last_cmd = CMD_INTERRUPT; 186 tail = "nterrupt"; 187 break; 188 case 'b': last_cmd = CMD_BACKTRACE; 189 if (p[1] == 't') 190 tail = "t"; 191 else 192 tail = "acktrace"; 193 break; 194 case 'w': last_cmd = CMD_BACKTRACE; 195 tail = "here"; 196 break; 197 case 'u': last_cmd = CMD_UP; 198 tail = "p"; 199 break; 200 case 'd': last_cmd = CMD_DOWN; 201 tail = "own"; 202 break; 203 default: last_cmd = 0; 204 } 205 if (last_cmd != 0) 206 { 207 // Check that the tail matches. 208 ++p; 209 while (*p != NUL && *p == *tail) 210 { 211 ++p; 212 ++tail; 213 } 214 if (ASCII_ISALPHA(*p) && last_cmd != CMD_FRAME) 215 last_cmd = 0; 216 } 217 } 218 219 if (last_cmd != 0) 220 { 221 // Execute debug command: decide where to break next and 222 // return. 223 switch (last_cmd) 224 { 225 case CMD_CONT: 226 debug_break_level = -1; 227 break; 228 case CMD_NEXT: 229 debug_break_level = ex_nesting_level; 230 break; 231 case CMD_STEP: 232 debug_break_level = 9999; 233 break; 234 case CMD_FINISH: 235 debug_break_level = ex_nesting_level - 1; 236 break; 237 case CMD_QUIT: 238 got_int = TRUE; 239 debug_break_level = -1; 240 break; 241 case CMD_INTERRUPT: 242 got_int = TRUE; 243 debug_break_level = 9999; 244 // Do not repeat ">interrupt" cmd, continue stepping. 245 last_cmd = CMD_STEP; 246 break; 247 case CMD_BACKTRACE: 248 do_showbacktrace(cmd); 249 continue; 250 case CMD_FRAME: 251 if (*p == NUL) 252 { 253 do_showbacktrace(cmd); 254 } 255 else 256 { 257 p = skipwhite(p); 258 do_setdebugtracelevel(p); 259 } 260 continue; 261 case CMD_UP: 262 debug_backtrace_level++; 263 do_checkbacktracelevel(); 264 continue; 265 case CMD_DOWN: 266 debug_backtrace_level--; 267 do_checkbacktracelevel(); 268 continue; 269 } 270 // Going out reset backtrace_level 271 debug_backtrace_level = 0; 272 break; 273 } 274 275 // don't debug this command 276 n = debug_break_level; 277 debug_break_level = -1; 278 (void)do_cmdline(cmdline, getexline, NULL, 279 DOCMD_VERBOSE|DOCMD_EXCRESET); 280 debug_break_level = n; 281 } 282 lines_left = Rows - 1; 283 } 284 vim_free(cmdline); 285 286 --RedrawingDisabled; 287 --no_wait_return; 288 redraw_all_later(NOT_VALID); 289 need_wait_return = FALSE; 290 msg_scroll = save_msg_scroll; 291 lines_left = Rows - 1; 292 State = save_State; 293 debug_mode = FALSE; 294 did_emsg = save_did_emsg; 295 cmd_silent = save_cmd_silent; 296 msg_silent = save_msg_silent; 297 emsg_silent = save_emsg_silent; 298 redir_off = save_redir_off; 299 300 // Only print the message again when typing a command before coming back 301 // here. 302 debug_did_msg = TRUE; 303 } 304 305 static int 306 get_maxbacktrace_level(char_u *sname) 307 { 308 char *p, *q; 309 int maxbacktrace = 0; 310 311 if (sname != NULL) 312 { 313 p = (char *)sname; 314 while ((q = strstr(p, "..")) != NULL) 315 { 316 p = q + 2; 317 maxbacktrace++; 318 } 319 } 320 return maxbacktrace; 321 } 322 323 static void 324 do_setdebugtracelevel(char_u *arg) 325 { 326 int level; 327 328 level = atoi((char *)arg); 329 if (*arg == '+' || level < 0) 330 debug_backtrace_level += level; 331 else 332 debug_backtrace_level = level; 333 334 do_checkbacktracelevel(); 335 } 336 337 static void 338 do_checkbacktracelevel(void) 339 { 340 if (debug_backtrace_level < 0) 341 { 342 debug_backtrace_level = 0; 343 msg(_("frame is zero")); 344 } 345 else 346 { 347 char_u *sname = estack_sfile(ESTACK_NONE); 348 int max = get_maxbacktrace_level(sname); 349 350 if (debug_backtrace_level > max) 351 { 352 debug_backtrace_level = max; 353 smsg(_("frame at highest level: %d"), max); 354 } 355 vim_free(sname); 356 } 357 } 358 359 static void 360 do_showbacktrace(char_u *cmd) 361 { 362 char_u *sname; 363 char *cur; 364 char *next; 365 int i = 0; 366 int max; 367 368 sname = estack_sfile(ESTACK_NONE); 369 max = get_maxbacktrace_level(sname); 370 if (sname != NULL) 371 { 372 cur = (char *)sname; 373 while (!got_int) 374 { 375 next = strstr(cur, ".."); 376 if (next != NULL) 377 *next = NUL; 378 if (i == max - debug_backtrace_level) 379 smsg("->%d %s", max - i, cur); 380 else 381 smsg(" %d %s", max - i, cur); 382 ++i; 383 if (next == NULL) 384 break; 385 *next = '.'; 386 cur = next + 2; 387 } 388 vim_free(sname); 389 } 390 391 if (SOURCING_LNUM != 0) 392 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd); 393 else 394 smsg(_("cmd: %s"), cmd); 395 } 396 397 /* 398 * ":debug". 399 */ 400 void 401 ex_debug(exarg_T *eap) 402 { 403 int debug_break_level_save = debug_break_level; 404 405 debug_break_level = 9999; 406 do_cmdline_cmd(eap->arg); 407 debug_break_level = debug_break_level_save; 408 } 409 410 static char_u *debug_breakpoint_name = NULL; 411 static linenr_T debug_breakpoint_lnum; 412 413 /* 414 * When debugging or a breakpoint is set on a skipped command, no debug prompt 415 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and 416 * debug_skipped_name is then set to the source name in the breakpoint case. If 417 * a skipped command decides itself that a debug prompt should be displayed, it 418 * can do so by calling dbg_check_skipped(). 419 */ 420 static int debug_skipped; 421 static char_u *debug_skipped_name; 422 423 /* 424 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is 425 * at or below the break level. But only when the line is actually 426 * executed. Return TRUE and set breakpoint_name for skipped commands that 427 * decide to execute something themselves. 428 * Called from do_one_cmd() before executing a command. 429 */ 430 void 431 dbg_check_breakpoint(exarg_T *eap) 432 { 433 char_u *p; 434 435 debug_skipped = FALSE; 436 if (debug_breakpoint_name != NULL) 437 { 438 if (!eap->skip) 439 { 440 // replace K_SNR with "<SNR>" 441 if (debug_breakpoint_name[0] == K_SPECIAL 442 && debug_breakpoint_name[1] == KS_EXTRA 443 && debug_breakpoint_name[2] == (int)KE_SNR) 444 p = (char_u *)"<SNR>"; 445 else 446 p = (char_u *)""; 447 smsg(_("Breakpoint in \"%s%s\" line %ld"), 448 p, 449 debug_breakpoint_name + (*p == NUL ? 0 : 3), 450 (long)debug_breakpoint_lnum); 451 debug_breakpoint_name = NULL; 452 do_debug(eap->cmd); 453 } 454 else 455 { 456 debug_skipped = TRUE; 457 debug_skipped_name = debug_breakpoint_name; 458 debug_breakpoint_name = NULL; 459 } 460 } 461 else if (ex_nesting_level <= debug_break_level) 462 { 463 if (!eap->skip) 464 do_debug(eap->cmd); 465 else 466 { 467 debug_skipped = TRUE; 468 debug_skipped_name = NULL; 469 } 470 } 471 } 472 473 /* 474 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was 475 * set. Return TRUE when the debug mode is entered this time. 476 */ 477 int 478 dbg_check_skipped(exarg_T *eap) 479 { 480 int prev_got_int; 481 482 if (debug_skipped) 483 { 484 // Save the value of got_int and reset it. We don't want a previous 485 // interruption cause flushing the input buffer. 486 prev_got_int = got_int; 487 got_int = FALSE; 488 debug_breakpoint_name = debug_skipped_name; 489 // eap->skip is TRUE 490 eap->skip = FALSE; 491 (void)dbg_check_breakpoint(eap); 492 eap->skip = TRUE; 493 got_int |= prev_got_int; 494 return TRUE; 495 } 496 return FALSE; 497 } 498 499 /* 500 * The list of breakpoints: dbg_breakp. 501 * This is a grow-array of structs. 502 */ 503 struct debuggy 504 { 505 int dbg_nr; // breakpoint number 506 int dbg_type; // DBG_FUNC, DBG_FILE or DBG_EXPR 507 char_u *dbg_name; // function, expression or file name 508 regprog_T *dbg_prog; // regexp program 509 linenr_T dbg_lnum; // line number in function or file 510 int dbg_forceit; // ! used 511 #ifdef FEAT_EVAL 512 typval_T *dbg_val; // last result of watchexpression 513 #endif 514 int dbg_level; // stored nested level for expr 515 }; 516 517 static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL}; 518 #define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx]) 519 #define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx]) 520 static int last_breakp = 0; // nr of last defined breakpoint 521 static int has_expr_breakpoint = FALSE; 522 523 #ifdef FEAT_PROFILE 524 // Profiling uses file and func names similar to breakpoints. 525 static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL}; 526 #endif 527 #define DBG_FUNC 1 528 #define DBG_FILE 2 529 #define DBG_EXPR 3 530 531 static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp); 532 533 /* 534 * Evaluate the "bp->dbg_name" expression and return the result. 535 * Restore the got_int and called_emsg flags. 536 */ 537 static typval_T * 538 eval_expr_restore(struct debuggy *bp) 539 { 540 typval_T *tv; 541 int prev_called_emsg = called_emsg; 542 int prev_did_emsg = did_emsg; 543 544 got_int = FALSE; 545 tv = eval_expr(bp->dbg_name, NULL); 546 547 // Evaluating the expression should not result in breaking the sequence of 548 // commands. 549 got_int = FALSE; 550 called_emsg = prev_called_emsg; 551 did_emsg = prev_did_emsg; 552 553 return tv; 554 } 555 556 /* 557 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them 558 * in the entry just after the last one in dbg_breakp. Note that "dbg_name" 559 * is allocated. 560 * Returns FAIL for failure. 561 */ 562 static int 563 dbg_parsearg( 564 char_u *arg, 565 garray_T *gap) // either &dbg_breakp or &prof_ga 566 { 567 char_u *p = arg; 568 char_u *q; 569 struct debuggy *bp; 570 int here = FALSE; 571 572 if (ga_grow(gap, 1) == FAIL) 573 return FAIL; 574 bp = &DEBUGGY(gap, gap->ga_len); 575 576 // Find "func" or "file". 577 if (STRNCMP(p, "func", 4) == 0) 578 bp->dbg_type = DBG_FUNC; 579 else if (STRNCMP(p, "file", 4) == 0) 580 bp->dbg_type = DBG_FILE; 581 else if ( 582 #ifdef FEAT_PROFILE 583 gap != &prof_ga && 584 #endif 585 STRNCMP(p, "here", 4) == 0) 586 { 587 if (curbuf->b_ffname == NULL) 588 { 589 emsg(_(e_no_file_name)); 590 return FAIL; 591 } 592 bp->dbg_type = DBG_FILE; 593 here = TRUE; 594 } 595 else if ( 596 #ifdef FEAT_PROFILE 597 gap != &prof_ga && 598 #endif 599 STRNCMP(p, "expr", 4) == 0) 600 bp->dbg_type = DBG_EXPR; 601 else 602 { 603 semsg(_(e_invarg2), p); 604 return FAIL; 605 } 606 p = skipwhite(p + 4); 607 608 // Find optional line number. 609 if (here) 610 bp->dbg_lnum = curwin->w_cursor.lnum; 611 else if ( 612 #ifdef FEAT_PROFILE 613 gap != &prof_ga && 614 #endif 615 VIM_ISDIGIT(*p)) 616 { 617 bp->dbg_lnum = getdigits(&p); 618 p = skipwhite(p); 619 } 620 else 621 bp->dbg_lnum = 0; 622 623 // Find the function or file name. Don't accept a function name with (). 624 if ((!here && *p == NUL) 625 || (here && *p != NUL) 626 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL)) 627 { 628 semsg(_(e_invarg2), arg); 629 return FAIL; 630 } 631 632 if (bp->dbg_type == DBG_FUNC) 633 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p); 634 else if (here) 635 bp->dbg_name = vim_strsave(curbuf->b_ffname); 636 else if (bp->dbg_type == DBG_EXPR) 637 { 638 bp->dbg_name = vim_strsave(p); 639 if (bp->dbg_name != NULL) 640 bp->dbg_val = eval_expr_restore(bp); 641 } 642 else 643 { 644 // Expand the file name in the same way as do_source(). This means 645 // doing it twice, so that $DIR/file gets expanded when $DIR is 646 // "~/dir". 647 q = expand_env_save(p); 648 if (q == NULL) 649 return FAIL; 650 p = expand_env_save(q); 651 vim_free(q); 652 if (p == NULL) 653 return FAIL; 654 if (*p != '*') 655 { 656 bp->dbg_name = fix_fname(p); 657 vim_free(p); 658 } 659 else 660 bp->dbg_name = p; 661 } 662 663 if (bp->dbg_name == NULL) 664 return FAIL; 665 return OK; 666 } 667 668 /* 669 * ":breakadd". Also used for ":profile". 670 */ 671 void 672 ex_breakadd(exarg_T *eap) 673 { 674 struct debuggy *bp; 675 char_u *pat; 676 garray_T *gap; 677 678 gap = &dbg_breakp; 679 #ifdef FEAT_PROFILE 680 if (eap->cmdidx == CMD_profile) 681 gap = &prof_ga; 682 #endif 683 684 if (dbg_parsearg(eap->arg, gap) == OK) 685 { 686 bp = &DEBUGGY(gap, gap->ga_len); 687 bp->dbg_forceit = eap->forceit; 688 689 if (bp->dbg_type != DBG_EXPR) 690 { 691 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE); 692 if (pat != NULL) 693 { 694 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 695 vim_free(pat); 696 } 697 if (pat == NULL || bp->dbg_prog == NULL) 698 vim_free(bp->dbg_name); 699 else 700 { 701 if (bp->dbg_lnum == 0) // default line number is 1 702 bp->dbg_lnum = 1; 703 #ifdef FEAT_PROFILE 704 if (eap->cmdidx != CMD_profile) 705 #endif 706 { 707 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp; 708 ++debug_tick; 709 } 710 ++gap->ga_len; 711 } 712 } 713 else 714 { 715 // DBG_EXPR 716 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp; 717 ++debug_tick; 718 if (gap == &dbg_breakp) 719 has_expr_breakpoint = TRUE; 720 } 721 } 722 } 723 724 /* 725 * ":debuggreedy". 726 */ 727 void 728 ex_debuggreedy(exarg_T *eap) 729 { 730 if (eap->addr_count == 0 || eap->line2 != 0) 731 debug_greedy = TRUE; 732 else 733 debug_greedy = FALSE; 734 } 735 736 static void 737 update_has_expr_breakpoint() 738 { 739 int i; 740 741 has_expr_breakpoint = FALSE; 742 for (i = 0; i < dbg_breakp.ga_len; ++i) 743 if (BREAKP(i).dbg_type == DBG_EXPR) 744 { 745 has_expr_breakpoint = TRUE; 746 break; 747 } 748 } 749 750 /* 751 * Return TRUE if there is any expression breakpoint. 752 */ 753 int 754 debug_has_expr_breakpoint() 755 { 756 return has_expr_breakpoint; 757 } 758 759 /* 760 * ":breakdel" and ":profdel". 761 */ 762 void 763 ex_breakdel(exarg_T *eap) 764 { 765 struct debuggy *bp, *bpi; 766 int nr; 767 int todel = -1; 768 int del_all = FALSE; 769 int i; 770 linenr_T best_lnum = 0; 771 garray_T *gap; 772 773 gap = &dbg_breakp; 774 if (eap->cmdidx == CMD_profdel) 775 { 776 #ifdef FEAT_PROFILE 777 gap = &prof_ga; 778 #else 779 ex_ni(eap); 780 return; 781 #endif 782 } 783 784 if (vim_isdigit(*eap->arg)) 785 { 786 // ":breakdel {nr}" 787 nr = atol((char *)eap->arg); 788 for (i = 0; i < gap->ga_len; ++i) 789 if (DEBUGGY(gap, i).dbg_nr == nr) 790 { 791 todel = i; 792 break; 793 } 794 } 795 else if (*eap->arg == '*') 796 { 797 todel = 0; 798 del_all = TRUE; 799 } 800 else 801 { 802 // ":breakdel {func|file|expr} [lnum] {name}" 803 if (dbg_parsearg(eap->arg, gap) == FAIL) 804 return; 805 bp = &DEBUGGY(gap, gap->ga_len); 806 for (i = 0; i < gap->ga_len; ++i) 807 { 808 bpi = &DEBUGGY(gap, i); 809 if (bp->dbg_type == bpi->dbg_type 810 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0 811 && (bp->dbg_lnum == bpi->dbg_lnum 812 || (bp->dbg_lnum == 0 813 && (best_lnum == 0 814 || bpi->dbg_lnum < best_lnum)))) 815 { 816 todel = i; 817 best_lnum = bpi->dbg_lnum; 818 } 819 } 820 vim_free(bp->dbg_name); 821 } 822 823 if (todel < 0) 824 semsg(_("E161: Breakpoint not found: %s"), eap->arg); 825 else 826 { 827 while (gap->ga_len > 0) 828 { 829 vim_free(DEBUGGY(gap, todel).dbg_name); 830 #ifdef FEAT_EVAL 831 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR 832 && DEBUGGY(gap, todel).dbg_val != NULL) 833 free_tv(DEBUGGY(gap, todel).dbg_val); 834 #endif 835 vim_regfree(DEBUGGY(gap, todel).dbg_prog); 836 --gap->ga_len; 837 if (todel < gap->ga_len) 838 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1), 839 (gap->ga_len - todel) * sizeof(struct debuggy)); 840 #ifdef FEAT_PROFILE 841 if (eap->cmdidx == CMD_breakdel) 842 #endif 843 ++debug_tick; 844 if (!del_all) 845 break; 846 } 847 848 // If all breakpoints were removed clear the array. 849 if (gap->ga_len == 0) 850 ga_clear(gap); 851 if (gap == &dbg_breakp) 852 update_has_expr_breakpoint(); 853 } 854 } 855 856 /* 857 * ":breaklist". 858 */ 859 void 860 ex_breaklist(exarg_T *eap UNUSED) 861 { 862 struct debuggy *bp; 863 int i; 864 865 if (dbg_breakp.ga_len == 0) 866 msg(_("No breakpoints defined")); 867 else 868 for (i = 0; i < dbg_breakp.ga_len; ++i) 869 { 870 bp = &BREAKP(i); 871 if (bp->dbg_type == DBG_FILE) 872 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE); 873 if (bp->dbg_type != DBG_EXPR) 874 smsg(_("%3d %s %s line %ld"), 875 bp->dbg_nr, 876 bp->dbg_type == DBG_FUNC ? "func" : "file", 877 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff, 878 (long)bp->dbg_lnum); 879 else 880 smsg(_("%3d expr %s"), 881 bp->dbg_nr, bp->dbg_name); 882 } 883 } 884 885 /* 886 * Find a breakpoint for a function or sourced file. 887 * Returns line number at which to break; zero when no matching breakpoint. 888 */ 889 linenr_T 890 dbg_find_breakpoint( 891 int file, // TRUE for a file, FALSE for a function 892 char_u *fname, // file or function name 893 linenr_T after) // after this line number 894 { 895 return debuggy_find(file, fname, after, &dbg_breakp, NULL); 896 } 897 898 #if defined(FEAT_PROFILE) || defined(PROTO) 899 /* 900 * Return TRUE if profiling is on for a function or sourced file. 901 */ 902 int 903 has_profiling( 904 int file, // TRUE for a file, FALSE for a function 905 char_u *fname, // file or function name 906 int *fp) // return: forceit 907 { 908 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp) 909 != (linenr_T)0); 910 } 911 #endif 912 913 /* 914 * Common code for dbg_find_breakpoint() and has_profiling(). 915 */ 916 static linenr_T 917 debuggy_find( 918 int is_file, // TRUE for a file, FALSE for a function 919 char_u *fname, // file or function name 920 linenr_T after, // after this line number 921 garray_T *gap, // either &dbg_breakp or &prof_ga 922 int *fp) // if not NULL: return forceit 923 { 924 struct debuggy *bp; 925 int i; 926 linenr_T lnum = 0; 927 char_u *name = NULL; 928 char_u *short_name = fname; 929 int prev_got_int; 930 931 // Return quickly when there are no breakpoints. 932 if (gap->ga_len == 0) 933 return (linenr_T)0; 934 935 // For a script-local function remove the prefix, so that 936 // "profile func Func" matches "Func" in any script. Otherwise it's very 937 // difficult to profile/debug a script-local function. It may match a 938 // function in the wrong script, but that is much better than not being 939 // able to profile/debug a function in a script with unknown ID. 940 // Also match a script-specific name. 941 if (!is_file && fname[0] == K_SPECIAL) 942 { 943 short_name = vim_strchr(fname, '_') + 1; 944 name = alloc(STRLEN(fname) + 3); 945 if (name != NULL) 946 { 947 STRCPY(name, "<SNR>"); 948 STRCPY(name + 5, fname + 3); 949 } 950 } 951 952 for (i = 0; i < gap->ga_len; ++i) 953 { 954 // Skip entries that are not useful or are for a line that is beyond 955 // an already found breakpoint. 956 bp = &DEBUGGY(gap, i); 957 if (((bp->dbg_type == DBG_FILE) == is_file 958 && bp->dbg_type != DBG_EXPR && ( 959 #ifdef FEAT_PROFILE 960 gap == &prof_ga || 961 #endif 962 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum))))) 963 { 964 // Save the value of got_int and reset it. We don't want a 965 // previous interruption cancel matching, only hitting CTRL-C 966 // while matching should abort it. 967 prev_got_int = got_int; 968 got_int = FALSE; 969 if ((name != NULL 970 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0)) 971 || vim_regexec_prog(&bp->dbg_prog, FALSE, 972 short_name, (colnr_T)0)) 973 { 974 lnum = bp->dbg_lnum; 975 if (fp != NULL) 976 *fp = bp->dbg_forceit; 977 } 978 got_int |= prev_got_int; 979 } 980 #ifdef FEAT_EVAL 981 else if (bp->dbg_type == DBG_EXPR) 982 { 983 typval_T *tv; 984 int line = FALSE; 985 986 tv = eval_expr_restore(bp); 987 if (tv != NULL) 988 { 989 if (bp->dbg_val == NULL) 990 { 991 debug_oldval = typval_tostring(NULL, TRUE); 992 bp->dbg_val = tv; 993 debug_newval = typval_tostring(bp->dbg_val, TRUE); 994 line = TRUE; 995 } 996 else 997 { 998 if (typval_compare(tv, bp->dbg_val, EXPR_IS, FALSE) == OK 999 && tv->vval.v_number == FALSE) 1000 { 1001 typval_T *v; 1002 1003 line = TRUE; 1004 debug_oldval = typval_tostring(bp->dbg_val, TRUE); 1005 // Need to evaluate again, typval_compare() overwrites 1006 // "tv". 1007 v = eval_expr_restore(bp); 1008 debug_newval = typval_tostring(v, TRUE); 1009 free_tv(bp->dbg_val); 1010 bp->dbg_val = v; 1011 } 1012 free_tv(tv); 1013 } 1014 } 1015 else if (bp->dbg_val != NULL) 1016 { 1017 debug_oldval = typval_tostring(bp->dbg_val, TRUE); 1018 debug_newval = typval_tostring(NULL, TRUE); 1019 free_tv(bp->dbg_val); 1020 bp->dbg_val = NULL; 1021 line = TRUE; 1022 } 1023 1024 if (line) 1025 { 1026 lnum = after > 0 ? after : 1; 1027 break; 1028 } 1029 } 1030 #endif 1031 } 1032 if (name != fname) 1033 vim_free(name); 1034 1035 return lnum; 1036 } 1037 1038 /* 1039 * Called when a breakpoint was encountered. 1040 */ 1041 void 1042 dbg_breakpoint(char_u *name, linenr_T lnum) 1043 { 1044 // We need to check if this line is actually executed in do_one_cmd() 1045 debug_breakpoint_name = name; 1046 debug_breakpoint_lnum = lnum; 1047 } 1048 #endif 1049