blob: 1e28d66f6160fe75df301493b23695fa3d44bff3 [file] [log] [blame]
Bram Moolenaareead75c2019-04-21 11:35:00 +02001/* 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)
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020017static int debug_greedy = FALSE; // batch mode debugging: don't save
18 // and restore typeahead.
Bram Moolenaareead75c2019-04-21 11:35:00 +020019static void do_setdebugtracelevel(char_u *arg);
20static void do_checkbacktracelevel(void);
21static void do_showbacktrace(char_u *cmd);
22
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020023static char_u *debug_oldval = NULL; // old and newval for debug expressions
Bram Moolenaareead75c2019-04-21 11:35:00 +020024static char_u *debug_newval = NULL;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020025static int debug_expr = 0; // use debug_expr
Bram Moolenaareead75c2019-04-21 11:35:00 +020026
27 int
28has_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
38do_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;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010054 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +020055 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
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020069 // Can't do this when there is no terminal for input/output.
Bram Moolenaareead75c2019-04-21 11:35:00 +020070 if (!gui.in_use)
71 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020072 // Break as soon as possible.
Bram Moolenaareead75c2019-04-21 11:35:00 +020073 debug_break_level = 9999;
74 return;
75 }
76#endif
77
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020078 // Make sure we are in raw mode and start termcap mode. Might have side
79 // effects...
Bram Moolenaareead75c2019-04-21 11:35:00 +020080 settmode(TMODE_RAW);
81 starttermcap();
82
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020083 ++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
Bram Moolenaareead75c2019-04-21 11:35:00 +020090
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 }
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200108 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 if (sname != NULL)
110 msg((char *)sname);
111 vim_free(sname);
112 if (SOURCING_LNUM != 0)
113 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200114 else
115 smsg(_("cmd: %s"), cmd);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200116
117 // Repeat getting a command and executing it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200118 for (;;)
119 {
120 msg_scroll = TRUE;
121 need_wait_return = FALSE;
122
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200123 // 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.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200128 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 {
Bram Moolenaarc41badb2021-06-07 22:04:52 +0200143 restore_typeahead(&typeaheadbuf, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200144 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 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200152 // If this is a debug command, set "last_cmd".
153 // If not, reset "last_cmd".
154 // For a blank line use previous command.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200155 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 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200207 // Check that the tail matches.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200208 ++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 {
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200221 // Execute debug command: decide where to break next and
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200222 // return.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200223 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;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200244 // Do not repeat ">interrupt" cmd, continue stepping.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200245 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 }
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200270 // Going out reset backtrace_level
Bram Moolenaareead75c2019-04-21 11:35:00 +0200271 debug_backtrace_level = 0;
272 break;
273 }
274
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200275 // don't debug this command
Bram Moolenaareead75c2019-04-21 11:35:00 +0200276 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
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200300 // Only print the message again when typing a command before coming back
301 // here.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200302 debug_did_msg = TRUE;
303}
304
305 static int
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100306get_maxbacktrace_level(char_u *sname)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200307{
308 char *p, *q;
309 int maxbacktrace = 0;
310
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100311 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200312 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100313 p = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200314 while ((q = strstr(p, "..")) != NULL)
315 {
316 p = q + 2;
317 maxbacktrace++;
318 }
319 }
320 return maxbacktrace;
321}
322
323 static void
324do_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
338do_checkbacktracelevel(void)
339{
340 if (debug_backtrace_level < 0)
341 {
342 debug_backtrace_level = 0;
343 msg(_("frame is zero"));
344 }
345 else
346 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200347 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100348 int max = get_maxbacktrace_level(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200349
350 if (debug_backtrace_level > max)
351 {
352 debug_backtrace_level = max;
353 smsg(_("frame at highest level: %d"), max);
354 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100355 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200356 }
357}
358
359 static void
360do_showbacktrace(char_u *cmd)
361{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100362 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200363 char *cur;
364 char *next;
365 int i = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100366 int max;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200367
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200368 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100369 max = get_maxbacktrace_level(sname);
370 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200371 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100372 cur = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200373 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 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100388 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200389 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100390
391 if (SOURCING_LNUM != 0)
392 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200393 else
394 smsg(_("cmd: %s"), cmd);
395}
396
397/*
398 * ":debug".
399 */
400 void
401ex_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
410static char_u *debug_breakpoint_name = NULL;
411static 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 */
420static int debug_skipped;
421static 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
431dbg_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 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200440 // replace K_SNR with "<SNR>"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200441 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
478dbg_check_skipped(exarg_T *eap)
479{
480 int prev_got_int;
481
482 if (debug_skipped)
483 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200484 // Save the value of got_int and reset it. We don't want a previous
485 // interruption cause flushing the input buffer.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200486 prev_got_int = got_int;
487 got_int = FALSE;
488 debug_breakpoint_name = debug_skipped_name;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200489 // eap->skip is TRUE
Bram Moolenaareead75c2019-04-21 11:35:00 +0200490 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 */
503struct debuggy
504{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200505 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
Bram Moolenaareead75c2019-04-21 11:35:00 +0200511#ifdef FEAT_EVAL
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200512 typval_T *dbg_val; // last result of watchexpression
Bram Moolenaareead75c2019-04-21 11:35:00 +0200513#endif
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200514 int dbg_level; // stored nested level for expr
Bram Moolenaareead75c2019-04-21 11:35:00 +0200515};
516
517static 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])
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200520static int last_breakp = 0; // nr of last defined breakpoint
Bram Moolenaar26a44842021-09-02 18:49:06 +0200521static int has_expr_breakpoint = FALSE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200522
523#ifdef FEAT_PROFILE
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200524// Profiling uses file and func names similar to breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200525static 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
531static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp);
532
533/*
534 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
535 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
536 * is allocated.
537 * Returns FAIL for failure.
538 */
539 static int
540dbg_parsearg(
541 char_u *arg,
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200542 garray_T *gap) // either &dbg_breakp or &prof_ga
Bram Moolenaareead75c2019-04-21 11:35:00 +0200543{
544 char_u *p = arg;
545 char_u *q;
546 struct debuggy *bp;
547 int here = FALSE;
548
549 if (ga_grow(gap, 1) == FAIL)
550 return FAIL;
551 bp = &DEBUGGY(gap, gap->ga_len);
552
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200553 // Find "func" or "file".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200554 if (STRNCMP(p, "func", 4) == 0)
555 bp->dbg_type = DBG_FUNC;
556 else if (STRNCMP(p, "file", 4) == 0)
557 bp->dbg_type = DBG_FILE;
558 else if (
559#ifdef FEAT_PROFILE
560 gap != &prof_ga &&
561#endif
562 STRNCMP(p, "here", 4) == 0)
563 {
564 if (curbuf->b_ffname == NULL)
565 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200566 emsg(_(e_no_file_name));
Bram Moolenaareead75c2019-04-21 11:35:00 +0200567 return FAIL;
568 }
569 bp->dbg_type = DBG_FILE;
570 here = TRUE;
571 }
572 else if (
573#ifdef FEAT_PROFILE
574 gap != &prof_ga &&
575#endif
576 STRNCMP(p, "expr", 4) == 0)
577 bp->dbg_type = DBG_EXPR;
578 else
579 {
580 semsg(_(e_invarg2), p);
581 return FAIL;
582 }
583 p = skipwhite(p + 4);
584
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200585 // Find optional line number.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200586 if (here)
587 bp->dbg_lnum = curwin->w_cursor.lnum;
588 else if (
589#ifdef FEAT_PROFILE
590 gap != &prof_ga &&
591#endif
592 VIM_ISDIGIT(*p))
593 {
594 bp->dbg_lnum = getdigits(&p);
595 p = skipwhite(p);
596 }
597 else
598 bp->dbg_lnum = 0;
599
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200600 // Find the function or file name. Don't accept a function name with ().
Bram Moolenaareead75c2019-04-21 11:35:00 +0200601 if ((!here && *p == NUL)
602 || (here && *p != NUL)
603 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
604 {
605 semsg(_(e_invarg2), arg);
606 return FAIL;
607 }
608
609 if (bp->dbg_type == DBG_FUNC)
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200610 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200611 else if (here)
612 bp->dbg_name = vim_strsave(curbuf->b_ffname);
613 else if (bp->dbg_type == DBG_EXPR)
614 {
615 bp->dbg_name = vim_strsave(p);
616 if (bp->dbg_name != NULL)
617 bp->dbg_val = eval_expr(bp->dbg_name, NULL);
618 }
619 else
620 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200621 // Expand the file name in the same way as do_source(). This means
622 // doing it twice, so that $DIR/file gets expanded when $DIR is
623 // "~/dir".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200624 q = expand_env_save(p);
625 if (q == NULL)
626 return FAIL;
627 p = expand_env_save(q);
628 vim_free(q);
629 if (p == NULL)
630 return FAIL;
631 if (*p != '*')
632 {
633 bp->dbg_name = fix_fname(p);
634 vim_free(p);
635 }
636 else
637 bp->dbg_name = p;
638 }
639
640 if (bp->dbg_name == NULL)
641 return FAIL;
642 return OK;
643}
644
645/*
646 * ":breakadd". Also used for ":profile".
647 */
648 void
649ex_breakadd(exarg_T *eap)
650{
651 struct debuggy *bp;
652 char_u *pat;
653 garray_T *gap;
654
655 gap = &dbg_breakp;
656#ifdef FEAT_PROFILE
657 if (eap->cmdidx == CMD_profile)
658 gap = &prof_ga;
659#endif
660
661 if (dbg_parsearg(eap->arg, gap) == OK)
662 {
663 bp = &DEBUGGY(gap, gap->ga_len);
664 bp->dbg_forceit = eap->forceit;
665
666 if (bp->dbg_type != DBG_EXPR)
667 {
668 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
669 if (pat != NULL)
670 {
671 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
672 vim_free(pat);
673 }
674 if (pat == NULL || bp->dbg_prog == NULL)
675 vim_free(bp->dbg_name);
676 else
677 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200678 if (bp->dbg_lnum == 0) // default line number is 1
Bram Moolenaareead75c2019-04-21 11:35:00 +0200679 bp->dbg_lnum = 1;
680#ifdef FEAT_PROFILE
681 if (eap->cmdidx != CMD_profile)
682#endif
683 {
684 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
685 ++debug_tick;
686 }
687 ++gap->ga_len;
688 }
689 }
690 else
691 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200692 // DBG_EXPR
Bram Moolenaareead75c2019-04-21 11:35:00 +0200693 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp;
694 ++debug_tick;
Bram Moolenaar26a44842021-09-02 18:49:06 +0200695 if (gap == &dbg_breakp)
696 has_expr_breakpoint = TRUE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200697 }
698 }
699}
700
701/*
702 * ":debuggreedy".
703 */
704 void
705ex_debuggreedy(exarg_T *eap)
706{
707 if (eap->addr_count == 0 || eap->line2 != 0)
708 debug_greedy = TRUE;
709 else
710 debug_greedy = FALSE;
711}
712
Bram Moolenaar26a44842021-09-02 18:49:06 +0200713 static void
714update_has_expr_breakpoint()
715{
716 int i;
717
718 has_expr_breakpoint = FALSE;
719 for (i = 0; i < dbg_breakp.ga_len; ++i)
720 if (BREAKP(i).dbg_type == DBG_EXPR)
721 {
722 has_expr_breakpoint = TRUE;
723 break;
724 }
725}
726
727/*
728 * Return TRUE if there is any expression breakpoint.
729 */
730 int
731debug_has_expr_breakpoint()
732{
733 return has_expr_breakpoint;
734}
735
Bram Moolenaareead75c2019-04-21 11:35:00 +0200736/*
737 * ":breakdel" and ":profdel".
738 */
739 void
740ex_breakdel(exarg_T *eap)
741{
742 struct debuggy *bp, *bpi;
743 int nr;
744 int todel = -1;
745 int del_all = FALSE;
746 int i;
747 linenr_T best_lnum = 0;
748 garray_T *gap;
749
750 gap = &dbg_breakp;
751 if (eap->cmdidx == CMD_profdel)
752 {
753#ifdef FEAT_PROFILE
754 gap = &prof_ga;
755#else
756 ex_ni(eap);
757 return;
758#endif
759 }
760
761 if (vim_isdigit(*eap->arg))
762 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200763 // ":breakdel {nr}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200764 nr = atol((char *)eap->arg);
765 for (i = 0; i < gap->ga_len; ++i)
766 if (DEBUGGY(gap, i).dbg_nr == nr)
767 {
768 todel = i;
769 break;
770 }
771 }
772 else if (*eap->arg == '*')
773 {
774 todel = 0;
775 del_all = TRUE;
776 }
777 else
778 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200779 // ":breakdel {func|file|expr} [lnum] {name}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200780 if (dbg_parsearg(eap->arg, gap) == FAIL)
781 return;
782 bp = &DEBUGGY(gap, gap->ga_len);
783 for (i = 0; i < gap->ga_len; ++i)
784 {
785 bpi = &DEBUGGY(gap, i);
786 if (bp->dbg_type == bpi->dbg_type
787 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
788 && (bp->dbg_lnum == bpi->dbg_lnum
789 || (bp->dbg_lnum == 0
790 && (best_lnum == 0
791 || bpi->dbg_lnum < best_lnum))))
792 {
793 todel = i;
794 best_lnum = bpi->dbg_lnum;
795 }
796 }
797 vim_free(bp->dbg_name);
798 }
799
800 if (todel < 0)
801 semsg(_("E161: Breakpoint not found: %s"), eap->arg);
802 else
803 {
804 while (gap->ga_len > 0)
805 {
806 vim_free(DEBUGGY(gap, todel).dbg_name);
807#ifdef FEAT_EVAL
808 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR
809 && DEBUGGY(gap, todel).dbg_val != NULL)
810 free_tv(DEBUGGY(gap, todel).dbg_val);
811#endif
812 vim_regfree(DEBUGGY(gap, todel).dbg_prog);
813 --gap->ga_len;
814 if (todel < gap->ga_len)
815 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
816 (gap->ga_len - todel) * sizeof(struct debuggy));
817#ifdef FEAT_PROFILE
818 if (eap->cmdidx == CMD_breakdel)
819#endif
820 ++debug_tick;
821 if (!del_all)
822 break;
823 }
824
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200825 // If all breakpoints were removed clear the array.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200826 if (gap->ga_len == 0)
827 ga_clear(gap);
Bram Moolenaar26a44842021-09-02 18:49:06 +0200828 if (gap == &dbg_breakp)
829 update_has_expr_breakpoint();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200830 }
831}
832
833/*
834 * ":breaklist".
835 */
836 void
837ex_breaklist(exarg_T *eap UNUSED)
838{
839 struct debuggy *bp;
840 int i;
841
842 if (dbg_breakp.ga_len == 0)
843 msg(_("No breakpoints defined"));
844 else
845 for (i = 0; i < dbg_breakp.ga_len; ++i)
846 {
847 bp = &BREAKP(i);
848 if (bp->dbg_type == DBG_FILE)
849 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
850 if (bp->dbg_type != DBG_EXPR)
851 smsg(_("%3d %s %s line %ld"),
852 bp->dbg_nr,
853 bp->dbg_type == DBG_FUNC ? "func" : "file",
854 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
855 (long)bp->dbg_lnum);
856 else
857 smsg(_("%3d expr %s"),
858 bp->dbg_nr, bp->dbg_name);
859 }
860}
861
862/*
863 * Find a breakpoint for a function or sourced file.
864 * Returns line number at which to break; zero when no matching breakpoint.
865 */
866 linenr_T
867dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200868 int file, // TRUE for a file, FALSE for a function
869 char_u *fname, // file or function name
870 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200871{
872 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
873}
874
875#if defined(FEAT_PROFILE) || defined(PROTO)
876/*
877 * Return TRUE if profiling is on for a function or sourced file.
878 */
879 int
880has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200881 int file, // TRUE for a file, FALSE for a function
882 char_u *fname, // file or function name
883 int *fp) // return: forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200884{
885 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
886 != (linenr_T)0);
887}
888#endif
889
890/*
891 * Common code for dbg_find_breakpoint() and has_profiling().
892 */
893 static linenr_T
894debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +0100895 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200896 char_u *fname, // file or function name
897 linenr_T after, // after this line number
898 garray_T *gap, // either &dbg_breakp or &prof_ga
899 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200900{
901 struct debuggy *bp;
902 int i;
903 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100904 char_u *name = NULL;
905 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200906 int prev_got_int;
907
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200908 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200909 if (gap->ga_len == 0)
910 return (linenr_T)0;
911
Bram Moolenaarb2049902021-01-24 12:53:53 +0100912 // For a script-local function remove the prefix, so that
913 // "profile func Func" matches "Func" in any script. Otherwise it's very
914 // difficult to profile/debug a script-local function. It may match a
915 // function in the wrong script, but that is much better than not being
916 // able to profile/debug a function in a script with unknown ID.
917 // Also match a script-specific name.
918 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200919 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100920 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200921 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100922 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200923 {
924 STRCPY(name, "<SNR>");
925 STRCPY(name + 5, fname + 3);
926 }
927 }
928
929 for (i = 0; i < gap->ga_len; ++i)
930 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200931 // Skip entries that are not useful or are for a line that is beyond
932 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200933 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100934 if (((bp->dbg_type == DBG_FILE) == is_file
935 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +0200936#ifdef FEAT_PROFILE
937 gap == &prof_ga ||
938#endif
939 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
940 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200941 // Save the value of got_int and reset it. We don't want a
942 // previous interruption cancel matching, only hitting CTRL-C
943 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200944 prev_got_int = got_int;
945 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100946 if ((name != NULL
947 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
948 || vim_regexec_prog(&bp->dbg_prog, FALSE,
949 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +0200950 {
951 lnum = bp->dbg_lnum;
952 if (fp != NULL)
953 *fp = bp->dbg_forceit;
954 }
955 got_int |= prev_got_int;
956 }
957#ifdef FEAT_EVAL
958 else if (bp->dbg_type == DBG_EXPR)
959 {
960 typval_T *tv;
961 int line = FALSE;
962
963 prev_got_int = got_int;
964 got_int = FALSE;
965
966 tv = eval_expr(bp->dbg_name, NULL);
967 if (tv != NULL)
968 {
969 if (bp->dbg_val == NULL)
970 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100971 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200972 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +0100973 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200974 line = TRUE;
975 }
976 else
977 {
Bram Moolenaar87396072019-12-31 22:36:18 +0100978 if (typval_compare(tv, bp->dbg_val, EXPR_IS, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +0200979 && tv->vval.v_number == FALSE)
980 {
981 typval_T *v;
982
983 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +0100984 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200985 // Need to evaluate again, typval_compare() overwrites
986 // "tv".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200987 v = eval_expr(bp->dbg_name, NULL);
Bram Moolenaar34453202021-01-31 13:08:38 +0100988 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200989 free_tv(bp->dbg_val);
990 bp->dbg_val = v;
991 }
992 free_tv(tv);
993 }
994 }
995 else if (bp->dbg_val != NULL)
996 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100997 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
998 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200999 free_tv(bp->dbg_val);
1000 bp->dbg_val = NULL;
1001 line = TRUE;
1002 }
1003
1004 if (line)
1005 {
1006 lnum = after > 0 ? after : 1;
1007 break;
1008 }
1009
1010 got_int |= prev_got_int;
1011 }
1012#endif
1013 }
1014 if (name != fname)
1015 vim_free(name);
1016
1017 return lnum;
1018}
1019
1020/*
1021 * Called when a breakpoint was encountered.
1022 */
1023 void
1024dbg_breakpoint(char_u *name, linenr_T lnum)
1025{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001026 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001027 debug_breakpoint_name = name;
1028 debug_breakpoint_lnum = lnum;
1029}
1030#endif