blob: 393e11e414fb4106c7c127136e0fd7ecc86feb77 [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 Moolenaar6ed545e2022-05-09 20:09:23 +010025static 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 Moolenaar9781d9c2022-09-20 13:51:25 +010090 save_timeout_for_debugging(); // disable regexp timeout flag
Bram Moolenaareead75c2019-04-21 11:35:00 +020091
Bram Moolenaar24959102022-05-07 20:01:16 +010092 State = MODE_NORMAL;
Bram Moolenaareead75c2019-04-21 11:35:00 +020093 debug_mode = TRUE;
94
95 if (!debug_did_msg)
96 msg(_("Entering Debug mode. Type \"cont\" to continue."));
97 if (debug_oldval != NULL)
98 {
99 smsg(_("Oldval = \"%s\""), debug_oldval);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +0000100 VIM_CLEAR(debug_oldval);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200101 }
102 if (debug_newval != NULL)
103 {
104 smsg(_("Newval = \"%s\""), debug_newval);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +0000105 VIM_CLEAR(debug_newval);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200106 }
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200107 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100108 if (sname != NULL)
109 msg((char *)sname);
110 vim_free(sname);
111 if (SOURCING_LNUM != 0)
112 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200113 else
114 smsg(_("cmd: %s"), cmd);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200115
116 // Repeat getting a command and executing it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200117 for (;;)
118 {
119 msg_scroll = TRUE;
120 need_wait_return = FALSE;
121
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200122 // Save the current typeahead buffer and replace it with an empty one.
123 // This makes sure we get input from the user here and don't interfere
124 // with the commands being executed. Reset "ex_normal_busy" to avoid
125 // the side effects of using ":normal". Save the stuff buffer and make
126 // it empty. Set ignore_script to avoid reading from script input.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200127 save_ex_normal_busy = ex_normal_busy;
128 ex_normal_busy = 0;
129 if (!debug_greedy)
130 {
131 save_typeahead(&typeaheadbuf);
132 typeahead_saved = TRUE;
133 save_ignore_script = ignore_script;
134 ignore_script = TRUE;
135 }
136
Bram Moolenaar747f1102022-09-18 13:06:41 +0100137 // don't debug any function call, e.g. from an expression mapping
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100138 n = debug_break_level;
139 debug_break_level = -1;
140
Bram Moolenaareead75c2019-04-21 11:35:00 +0200141 vim_free(cmdline);
142 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL);
143
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100144 debug_break_level = n;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200145 if (typeahead_saved)
146 {
Bram Moolenaarc41badb2021-06-07 22:04:52 +0200147 restore_typeahead(&typeaheadbuf, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200148 ignore_script = save_ignore_script;
149 }
150 ex_normal_busy = save_ex_normal_busy;
151
152 cmdline_row = msg_row;
153 msg_starthere();
154 if (cmdline != NULL)
155 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200156 // If this is a debug command, set "last_cmd".
157 // If not, reset "last_cmd".
158 // For a blank line use previous command.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200159 p = skipwhite(cmdline);
160 if (*p != NUL)
161 {
162 switch (*p)
163 {
164 case 'c': last_cmd = CMD_CONT;
165 tail = "ont";
166 break;
167 case 'n': last_cmd = CMD_NEXT;
168 tail = "ext";
169 break;
170 case 's': last_cmd = CMD_STEP;
171 tail = "tep";
172 break;
173 case 'f':
174 last_cmd = 0;
175 if (p[1] == 'r')
176 {
177 last_cmd = CMD_FRAME;
178 tail = "rame";
179 }
180 else
181 {
182 last_cmd = CMD_FINISH;
183 tail = "inish";
184 }
185 break;
186 case 'q': last_cmd = CMD_QUIT;
187 tail = "uit";
188 break;
189 case 'i': last_cmd = CMD_INTERRUPT;
190 tail = "nterrupt";
191 break;
192 case 'b': last_cmd = CMD_BACKTRACE;
193 if (p[1] == 't')
194 tail = "t";
195 else
196 tail = "acktrace";
197 break;
198 case 'w': last_cmd = CMD_BACKTRACE;
199 tail = "here";
200 break;
201 case 'u': last_cmd = CMD_UP;
202 tail = "p";
203 break;
204 case 'd': last_cmd = CMD_DOWN;
205 tail = "own";
206 break;
207 default: last_cmd = 0;
208 }
209 if (last_cmd != 0)
210 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200211 // Check that the tail matches.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200212 ++p;
213 while (*p != NUL && *p == *tail)
214 {
215 ++p;
216 ++tail;
217 }
218 if (ASCII_ISALPHA(*p) && last_cmd != CMD_FRAME)
219 last_cmd = 0;
220 }
221 }
222
223 if (last_cmd != 0)
224 {
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200225 // Execute debug command: decide where to break next and
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200226 // return.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200227 switch (last_cmd)
228 {
229 case CMD_CONT:
230 debug_break_level = -1;
231 break;
232 case CMD_NEXT:
233 debug_break_level = ex_nesting_level;
234 break;
235 case CMD_STEP:
236 debug_break_level = 9999;
237 break;
238 case CMD_FINISH:
239 debug_break_level = ex_nesting_level - 1;
240 break;
241 case CMD_QUIT:
242 got_int = TRUE;
243 debug_break_level = -1;
244 break;
245 case CMD_INTERRUPT:
246 got_int = TRUE;
247 debug_break_level = 9999;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200248 // Do not repeat ">interrupt" cmd, continue stepping.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200249 last_cmd = CMD_STEP;
250 break;
251 case CMD_BACKTRACE:
252 do_showbacktrace(cmd);
253 continue;
254 case CMD_FRAME:
255 if (*p == NUL)
256 {
257 do_showbacktrace(cmd);
258 }
259 else
260 {
261 p = skipwhite(p);
262 do_setdebugtracelevel(p);
263 }
264 continue;
265 case CMD_UP:
266 debug_backtrace_level++;
267 do_checkbacktracelevel();
268 continue;
269 case CMD_DOWN:
270 debug_backtrace_level--;
271 do_checkbacktracelevel();
272 continue;
273 }
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200274 // Going out reset backtrace_level
Bram Moolenaareead75c2019-04-21 11:35:00 +0200275 debug_backtrace_level = 0;
276 break;
277 }
278
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200279 // don't debug this command
Bram Moolenaareead75c2019-04-21 11:35:00 +0200280 n = debug_break_level;
281 debug_break_level = -1;
282 (void)do_cmdline(cmdline, getexline, NULL,
283 DOCMD_VERBOSE|DOCMD_EXCRESET);
284 debug_break_level = n;
285 }
286 lines_left = Rows - 1;
287 }
288 vim_free(cmdline);
289
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100290 if (RedrawingDisabled > 0)
291 --RedrawingDisabled;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200292 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100293 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200294 need_wait_return = FALSE;
295 msg_scroll = save_msg_scroll;
Bram Moolenaar9781d9c2022-09-20 13:51:25 +0100296 restore_timeout_for_debugging();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200297 lines_left = Rows - 1;
298 State = save_State;
299 debug_mode = FALSE;
300 did_emsg = save_did_emsg;
301 cmd_silent = save_cmd_silent;
302 msg_silent = save_msg_silent;
303 emsg_silent = save_emsg_silent;
304 redir_off = save_redir_off;
305
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200306 // Only print the message again when typing a command before coming back
307 // here.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200308 debug_did_msg = TRUE;
309}
310
311 static int
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100312get_maxbacktrace_level(char_u *sname)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200313{
314 char *p, *q;
315 int maxbacktrace = 0;
316
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000317 if (sname == NULL)
318 return 0;
319
320 p = (char *)sname;
321 while ((q = strstr(p, "..")) != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200322 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000323 p = q + 2;
324 maxbacktrace++;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200325 }
326 return maxbacktrace;
327}
328
329 static void
330do_setdebugtracelevel(char_u *arg)
331{
332 int level;
333
334 level = atoi((char *)arg);
335 if (*arg == '+' || level < 0)
336 debug_backtrace_level += level;
337 else
338 debug_backtrace_level = level;
339
340 do_checkbacktracelevel();
341}
342
343 static void
344do_checkbacktracelevel(void)
345{
346 if (debug_backtrace_level < 0)
347 {
348 debug_backtrace_level = 0;
349 msg(_("frame is zero"));
350 }
351 else
352 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200353 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100354 int max = get_maxbacktrace_level(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200355
356 if (debug_backtrace_level > max)
357 {
358 debug_backtrace_level = max;
359 smsg(_("frame at highest level: %d"), max);
360 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100361 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200362 }
363}
364
365 static void
366do_showbacktrace(char_u *cmd)
367{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100368 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200369 char *cur;
370 char *next;
371 int i = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100372 int max;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200373
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200374 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100375 max = get_maxbacktrace_level(sname);
376 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200377 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100378 cur = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200379 while (!got_int)
380 {
381 next = strstr(cur, "..");
382 if (next != NULL)
383 *next = NUL;
384 if (i == max - debug_backtrace_level)
385 smsg("->%d %s", max - i, cur);
386 else
387 smsg(" %d %s", max - i, cur);
388 ++i;
389 if (next == NULL)
390 break;
391 *next = '.';
392 cur = next + 2;
393 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100394 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200395 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100396
397 if (SOURCING_LNUM != 0)
398 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200399 else
400 smsg(_("cmd: %s"), cmd);
401}
402
403/*
404 * ":debug".
405 */
406 void
407ex_debug(exarg_T *eap)
408{
409 int debug_break_level_save = debug_break_level;
410
411 debug_break_level = 9999;
412 do_cmdline_cmd(eap->arg);
413 debug_break_level = debug_break_level_save;
414}
415
416static char_u *debug_breakpoint_name = NULL;
417static linenr_T debug_breakpoint_lnum;
418
419/*
420 * When debugging or a breakpoint is set on a skipped command, no debug prompt
421 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
422 * debug_skipped_name is then set to the source name in the breakpoint case. If
423 * a skipped command decides itself that a debug prompt should be displayed, it
424 * can do so by calling dbg_check_skipped().
425 */
426static int debug_skipped;
427static char_u *debug_skipped_name;
428
429/*
430 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
431 * at or below the break level. But only when the line is actually
432 * executed. Return TRUE and set breakpoint_name for skipped commands that
433 * decide to execute something themselves.
434 * Called from do_one_cmd() before executing a command.
435 */
436 void
437dbg_check_breakpoint(exarg_T *eap)
438{
439 char_u *p;
440
441 debug_skipped = FALSE;
442 if (debug_breakpoint_name != NULL)
443 {
444 if (!eap->skip)
445 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200446 // replace K_SNR with "<SNR>"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200447 if (debug_breakpoint_name[0] == K_SPECIAL
448 && debug_breakpoint_name[1] == KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100449 && debug_breakpoint_name[2] == KE_SNR)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200450 p = (char_u *)"<SNR>";
451 else
452 p = (char_u *)"";
453 smsg(_("Breakpoint in \"%s%s\" line %ld"),
454 p,
455 debug_breakpoint_name + (*p == NUL ? 0 : 3),
456 (long)debug_breakpoint_lnum);
457 debug_breakpoint_name = NULL;
458 do_debug(eap->cmd);
459 }
460 else
461 {
462 debug_skipped = TRUE;
463 debug_skipped_name = debug_breakpoint_name;
464 debug_breakpoint_name = NULL;
465 }
466 }
467 else if (ex_nesting_level <= debug_break_level)
468 {
469 if (!eap->skip)
470 do_debug(eap->cmd);
471 else
472 {
473 debug_skipped = TRUE;
474 debug_skipped_name = NULL;
475 }
476 }
477}
478
479/*
480 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
481 * set. Return TRUE when the debug mode is entered this time.
482 */
483 int
484dbg_check_skipped(exarg_T *eap)
485{
486 int prev_got_int;
487
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000488 if (!debug_skipped)
489 return FALSE;
490
491 // Save the value of got_int and reset it. We don't want a previous
492 // interruption cause flushing the input buffer.
493 prev_got_int = got_int;
494 got_int = FALSE;
495 debug_breakpoint_name = debug_skipped_name;
496 // eap->skip is TRUE
497 eap->skip = FALSE;
498 (void)dbg_check_breakpoint(eap);
499 eap->skip = TRUE;
500 got_int |= prev_got_int;
501 return TRUE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200502}
503
504/*
505 * The list of breakpoints: dbg_breakp.
506 * This is a grow-array of structs.
507 */
508struct debuggy
509{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200510 int dbg_nr; // breakpoint number
511 int dbg_type; // DBG_FUNC, DBG_FILE or DBG_EXPR
512 char_u *dbg_name; // function, expression or file name
513 regprog_T *dbg_prog; // regexp program
514 linenr_T dbg_lnum; // line number in function or file
515 int dbg_forceit; // ! used
Bram Moolenaareead75c2019-04-21 11:35:00 +0200516#ifdef FEAT_EVAL
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200517 typval_T *dbg_val; // last result of watchexpression
Bram Moolenaareead75c2019-04-21 11:35:00 +0200518#endif
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200519 int dbg_level; // stored nested level for expr
Bram Moolenaareead75c2019-04-21 11:35:00 +0200520};
521
522static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL};
523#define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
524#define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200525static int last_breakp = 0; // nr of last defined breakpoint
Bram Moolenaar26a44842021-09-02 18:49:06 +0200526static int has_expr_breakpoint = FALSE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200527
528#ifdef FEAT_PROFILE
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200529// Profiling uses file and func names similar to breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200530static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL};
531#endif
532#define DBG_FUNC 1
533#define DBG_FILE 2
534#define DBG_EXPR 3
535
536static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp);
537
538/*
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200539 * Evaluate the "bp->dbg_name" expression and return the result.
Bram Moolenaar0325d392021-09-09 12:34:19 +0200540 * Disables error messages.
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200541 */
542 static typval_T *
Bram Moolenaar0325d392021-09-09 12:34:19 +0200543eval_expr_no_emsg(struct debuggy *bp)
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200544{
545 typval_T *tv;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200546
Bram Moolenaar0325d392021-09-09 12:34:19 +0200547 // Disable error messages, a bad expression would make Vim unusable.
548 ++emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200549 tv = eval_expr(bp->dbg_name, NULL);
Bram Moolenaar0325d392021-09-09 12:34:19 +0200550 --emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200551
552 return tv;
553}
554
555/*
Bram Moolenaareead75c2019-04-21 11:35:00 +0200556 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
557 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
558 * is allocated.
559 * Returns FAIL for failure.
560 */
561 static int
562dbg_parsearg(
563 char_u *arg,
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200564 garray_T *gap) // either &dbg_breakp or &prof_ga
Bram Moolenaareead75c2019-04-21 11:35:00 +0200565{
566 char_u *p = arg;
567 char_u *q;
568 struct debuggy *bp;
569 int here = FALSE;
570
571 if (ga_grow(gap, 1) == FAIL)
572 return FAIL;
573 bp = &DEBUGGY(gap, gap->ga_len);
574
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200575 // Find "func" or "file".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200576 if (STRNCMP(p, "func", 4) == 0)
577 bp->dbg_type = DBG_FUNC;
578 else if (STRNCMP(p, "file", 4) == 0)
579 bp->dbg_type = DBG_FILE;
580 else if (
581#ifdef FEAT_PROFILE
582 gap != &prof_ga &&
583#endif
584 STRNCMP(p, "here", 4) == 0)
585 {
586 if (curbuf->b_ffname == NULL)
587 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200588 emsg(_(e_no_file_name));
Bram Moolenaareead75c2019-04-21 11:35:00 +0200589 return FAIL;
590 }
591 bp->dbg_type = DBG_FILE;
592 here = TRUE;
593 }
594 else if (
595#ifdef FEAT_PROFILE
596 gap != &prof_ga &&
597#endif
598 STRNCMP(p, "expr", 4) == 0)
599 bp->dbg_type = DBG_EXPR;
600 else
601 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000602 semsg(_(e_invalid_argument_str), p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200603 return FAIL;
604 }
605 p = skipwhite(p + 4);
606
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200607 // Find optional line number.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200608 if (here)
609 bp->dbg_lnum = curwin->w_cursor.lnum;
610 else if (
611#ifdef FEAT_PROFILE
612 gap != &prof_ga &&
613#endif
614 VIM_ISDIGIT(*p))
615 {
616 bp->dbg_lnum = getdigits(&p);
617 p = skipwhite(p);
618 }
619 else
620 bp->dbg_lnum = 0;
621
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200622 // Find the function or file name. Don't accept a function name with ().
Bram Moolenaareead75c2019-04-21 11:35:00 +0200623 if ((!here && *p == NUL)
624 || (here && *p != NUL)
625 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
626 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000627 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200628 return FAIL;
629 }
630
631 if (bp->dbg_type == DBG_FUNC)
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200632 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200633 else if (here)
634 bp->dbg_name = vim_strsave(curbuf->b_ffname);
635 else if (bp->dbg_type == DBG_EXPR)
636 {
637 bp->dbg_name = vim_strsave(p);
638 if (bp->dbg_name != NULL)
Bram Moolenaar0325d392021-09-09 12:34:19 +0200639 bp->dbg_val = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200640 }
641 else
642 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200643 // Expand the file name in the same way as do_source(). This means
644 // doing it twice, so that $DIR/file gets expanded when $DIR is
645 // "~/dir".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200646 q = expand_env_save(p);
647 if (q == NULL)
648 return FAIL;
649 p = expand_env_save(q);
650 vim_free(q);
651 if (p == NULL)
652 return FAIL;
653 if (*p != '*')
654 {
655 bp->dbg_name = fix_fname(p);
656 vim_free(p);
657 }
658 else
659 bp->dbg_name = p;
660 }
661
662 if (bp->dbg_name == NULL)
663 return FAIL;
664 return OK;
665}
666
667/*
668 * ":breakadd". Also used for ":profile".
669 */
670 void
671ex_breakadd(exarg_T *eap)
672{
673 struct debuggy *bp;
674 char_u *pat;
675 garray_T *gap;
676
677 gap = &dbg_breakp;
678#ifdef FEAT_PROFILE
679 if (eap->cmdidx == CMD_profile)
680 gap = &prof_ga;
681#endif
682
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000683 if (dbg_parsearg(eap->arg, gap) != OK)
684 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200685
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000686 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)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200693 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000694 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
695 vim_free(pat);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200696 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000697 if (pat == NULL || bp->dbg_prog == NULL)
698 vim_free(bp->dbg_name);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200699 else
700 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000701 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;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200711 }
712 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000713 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 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200721}
722
723/*
724 * ":debuggreedy".
725 */
726 void
727ex_debuggreedy(exarg_T *eap)
728{
729 if (eap->addr_count == 0 || eap->line2 != 0)
730 debug_greedy = TRUE;
731 else
732 debug_greedy = FALSE;
733}
734
Bram Moolenaar26a44842021-09-02 18:49:06 +0200735 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000736update_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200737{
738 int i;
739
740 has_expr_breakpoint = FALSE;
741 for (i = 0; i < dbg_breakp.ga_len; ++i)
742 if (BREAKP(i).dbg_type == DBG_EXPR)
743 {
744 has_expr_breakpoint = TRUE;
745 break;
746 }
747}
748
749/*
750 * Return TRUE if there is any expression breakpoint.
751 */
752 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000753debug_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200754{
755 return has_expr_breakpoint;
756}
757
Bram Moolenaareead75c2019-04-21 11:35:00 +0200758/*
759 * ":breakdel" and ":profdel".
760 */
761 void
762ex_breakdel(exarg_T *eap)
763{
764 struct debuggy *bp, *bpi;
765 int nr;
766 int todel = -1;
767 int del_all = FALSE;
768 int i;
769 linenr_T best_lnum = 0;
770 garray_T *gap;
771
772 gap = &dbg_breakp;
773 if (eap->cmdidx == CMD_profdel)
774 {
775#ifdef FEAT_PROFILE
776 gap = &prof_ga;
777#else
778 ex_ni(eap);
779 return;
780#endif
781 }
782
783 if (vim_isdigit(*eap->arg))
784 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200785 // ":breakdel {nr}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200786 nr = atol((char *)eap->arg);
787 for (i = 0; i < gap->ga_len; ++i)
788 if (DEBUGGY(gap, i).dbg_nr == nr)
789 {
790 todel = i;
791 break;
792 }
793 }
794 else if (*eap->arg == '*')
795 {
796 todel = 0;
797 del_all = TRUE;
798 }
799 else
800 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200801 // ":breakdel {func|file|expr} [lnum] {name}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200802 if (dbg_parsearg(eap->arg, gap) == FAIL)
803 return;
804 bp = &DEBUGGY(gap, gap->ga_len);
805 for (i = 0; i < gap->ga_len; ++i)
806 {
807 bpi = &DEBUGGY(gap, i);
808 if (bp->dbg_type == bpi->dbg_type
809 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
810 && (bp->dbg_lnum == bpi->dbg_lnum
811 || (bp->dbg_lnum == 0
812 && (best_lnum == 0
813 || bpi->dbg_lnum < best_lnum))))
814 {
815 todel = i;
816 best_lnum = bpi->dbg_lnum;
817 }
818 }
819 vim_free(bp->dbg_name);
820 }
821
822 if (todel < 0)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200823 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000824 semsg(_(e_breakpoint_not_found_str), eap->arg);
825 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200826 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000827
828 while (gap->ga_len > 0)
829 {
830 vim_free(DEBUGGY(gap, todel).dbg_name);
831#ifdef FEAT_EVAL
832 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR
833 && DEBUGGY(gap, todel).dbg_val != NULL)
834 free_tv(DEBUGGY(gap, todel).dbg_val);
835#endif
836 vim_regfree(DEBUGGY(gap, todel).dbg_prog);
837 --gap->ga_len;
838 if (todel < gap->ga_len)
839 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
840 (gap->ga_len - todel) * sizeof(struct debuggy));
841#ifdef FEAT_PROFILE
842 if (eap->cmdidx == CMD_breakdel)
843#endif
844 ++debug_tick;
845 if (!del_all)
846 break;
847 }
848
849 // If all breakpoints were removed clear the array.
850 if (gap->ga_len == 0)
851 ga_clear(gap);
852 if (gap == &dbg_breakp)
853 update_has_expr_breakpoint();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200854}
855
856/*
857 * ":breaklist".
858 */
859 void
860ex_breaklist(exarg_T *eap UNUSED)
861{
862 struct debuggy *bp;
863 int i;
864
865 if (dbg_breakp.ga_len == 0)
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000866 {
Bram Moolenaareead75c2019-04-21 11:35:00 +0200867 msg(_("No breakpoints defined"));
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000868 return;
869 }
870
871 for (i = 0; i < dbg_breakp.ga_len; ++i)
872 {
873 bp = &BREAKP(i);
874 if (bp->dbg_type == DBG_FILE)
875 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
876 if (bp->dbg_type != DBG_EXPR)
877 smsg(_("%3d %s %s line %ld"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200878 bp->dbg_nr,
879 bp->dbg_type == DBG_FUNC ? "func" : "file",
880 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
881 (long)bp->dbg_lnum);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000882 else
883 smsg(_("%3d expr %s"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200884 bp->dbg_nr, bp->dbg_name);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000885 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200886}
887
888/*
889 * Find a breakpoint for a function or sourced file.
890 * Returns line number at which to break; zero when no matching breakpoint.
891 */
892 linenr_T
893dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200894 int file, // TRUE for a file, FALSE for a function
895 char_u *fname, // file or function name
896 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200897{
898 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
899}
900
901#if defined(FEAT_PROFILE) || defined(PROTO)
902/*
903 * Return TRUE if profiling is on for a function or sourced file.
904 */
905 int
906has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200907 int file, // TRUE for a file, FALSE for a function
908 char_u *fname, // file or function name
909 int *fp) // return: forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200910{
911 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
912 != (linenr_T)0);
913}
914#endif
915
916/*
917 * Common code for dbg_find_breakpoint() and has_profiling().
918 */
919 static linenr_T
920debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +0100921 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200922 char_u *fname, // file or function name
923 linenr_T after, // after this line number
924 garray_T *gap, // either &dbg_breakp or &prof_ga
925 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200926{
927 struct debuggy *bp;
928 int i;
929 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100930 char_u *name = NULL;
931 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200932 int prev_got_int;
933
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200934 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200935 if (gap->ga_len == 0)
936 return (linenr_T)0;
937
Bram Moolenaarb2049902021-01-24 12:53:53 +0100938 // For a script-local function remove the prefix, so that
939 // "profile func Func" matches "Func" in any script. Otherwise it's very
940 // difficult to profile/debug a script-local function. It may match a
941 // function in the wrong script, but that is much better than not being
942 // able to profile/debug a function in a script with unknown ID.
943 // Also match a script-specific name.
944 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200945 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100946 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200947 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100948 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200949 {
950 STRCPY(name, "<SNR>");
951 STRCPY(name + 5, fname + 3);
952 }
953 }
954
955 for (i = 0; i < gap->ga_len; ++i)
956 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200957 // Skip entries that are not useful or are for a line that is beyond
958 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200959 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100960 if (((bp->dbg_type == DBG_FILE) == is_file
961 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +0200962#ifdef FEAT_PROFILE
963 gap == &prof_ga ||
964#endif
965 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
966 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200967 // Save the value of got_int and reset it. We don't want a
968 // previous interruption cancel matching, only hitting CTRL-C
969 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200970 prev_got_int = got_int;
971 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100972 if ((name != NULL
973 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
974 || vim_regexec_prog(&bp->dbg_prog, FALSE,
975 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +0200976 {
977 lnum = bp->dbg_lnum;
978 if (fp != NULL)
979 *fp = bp->dbg_forceit;
980 }
981 got_int |= prev_got_int;
982 }
983#ifdef FEAT_EVAL
984 else if (bp->dbg_type == DBG_EXPR)
985 {
986 typval_T *tv;
987 int line = FALSE;
988
Bram Moolenaar0325d392021-09-09 12:34:19 +0200989 tv = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200990 if (tv != NULL)
991 {
992 if (bp->dbg_val == NULL)
993 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100994 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200995 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +0100996 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200997 line = TRUE;
998 }
999 else
1000 {
Bram Moolenaarcf666202022-03-10 13:29:20 +00001001 // Use "==" instead of "is" for strings, that is what we
1002 // always have done.
1003 exprtype_T type = tv->v_type == VAR_STRING
1004 ? EXPR_EQUAL : EXPR_IS;
1005
1006 if (typval_compare(tv, bp->dbg_val, type, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +02001007 && tv->vval.v_number == FALSE)
1008 {
1009 typval_T *v;
1010
1011 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +01001012 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001013 // Need to evaluate again, typval_compare() overwrites
1014 // "tv".
Bram Moolenaar0325d392021-09-09 12:34:19 +02001015 v = eval_expr_no_emsg(bp);
Bram Moolenaar34453202021-01-31 13:08:38 +01001016 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001017 free_tv(bp->dbg_val);
1018 bp->dbg_val = v;
1019 }
1020 free_tv(tv);
1021 }
1022 }
1023 else if (bp->dbg_val != NULL)
1024 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001025 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1026 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001027 free_tv(bp->dbg_val);
1028 bp->dbg_val = NULL;
1029 line = TRUE;
1030 }
1031
1032 if (line)
1033 {
1034 lnum = after > 0 ? after : 1;
1035 break;
1036 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001037 }
1038#endif
1039 }
1040 if (name != fname)
1041 vim_free(name);
1042
1043 return lnum;
1044}
1045
1046/*
1047 * Called when a breakpoint was encountered.
1048 */
1049 void
1050dbg_breakpoint(char_u *name, linenr_T lnum)
1051{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001052 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001053 debug_breakpoint_name = name;
1054 debug_breakpoint_lnum = lnum;
1055}
1056#endif