blob: a04a07836121a08c87f4e01959288bd530297e2e [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
290 --RedrawingDisabled;
291 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100292 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200293 need_wait_return = FALSE;
294 msg_scroll = save_msg_scroll;
Bram Moolenaar9781d9c2022-09-20 13:51:25 +0100295 restore_timeout_for_debugging();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200296 lines_left = Rows - 1;
297 State = save_State;
298 debug_mode = FALSE;
299 did_emsg = save_did_emsg;
300 cmd_silent = save_cmd_silent;
301 msg_silent = save_msg_silent;
302 emsg_silent = save_emsg_silent;
303 redir_off = save_redir_off;
304
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200305 // Only print the message again when typing a command before coming back
306 // here.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200307 debug_did_msg = TRUE;
308}
309
310 static int
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100311get_maxbacktrace_level(char_u *sname)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200312{
313 char *p, *q;
314 int maxbacktrace = 0;
315
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000316 if (sname == NULL)
317 return 0;
318
319 p = (char *)sname;
320 while ((q = strstr(p, "..")) != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200321 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000322 p = q + 2;
323 maxbacktrace++;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200324 }
325 return maxbacktrace;
326}
327
328 static void
329do_setdebugtracelevel(char_u *arg)
330{
331 int level;
332
333 level = atoi((char *)arg);
334 if (*arg == '+' || level < 0)
335 debug_backtrace_level += level;
336 else
337 debug_backtrace_level = level;
338
339 do_checkbacktracelevel();
340}
341
342 static void
343do_checkbacktracelevel(void)
344{
345 if (debug_backtrace_level < 0)
346 {
347 debug_backtrace_level = 0;
348 msg(_("frame is zero"));
349 }
350 else
351 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200352 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100353 int max = get_maxbacktrace_level(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200354
355 if (debug_backtrace_level > max)
356 {
357 debug_backtrace_level = max;
358 smsg(_("frame at highest level: %d"), max);
359 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100360 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200361 }
362}
363
364 static void
365do_showbacktrace(char_u *cmd)
366{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100367 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200368 char *cur;
369 char *next;
370 int i = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100371 int max;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200372
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200373 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100374 max = get_maxbacktrace_level(sname);
375 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200376 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100377 cur = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200378 while (!got_int)
379 {
380 next = strstr(cur, "..");
381 if (next != NULL)
382 *next = NUL;
383 if (i == max - debug_backtrace_level)
384 smsg("->%d %s", max - i, cur);
385 else
386 smsg(" %d %s", max - i, cur);
387 ++i;
388 if (next == NULL)
389 break;
390 *next = '.';
391 cur = next + 2;
392 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100393 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200394 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100395
396 if (SOURCING_LNUM != 0)
397 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200398 else
399 smsg(_("cmd: %s"), cmd);
400}
401
402/*
403 * ":debug".
404 */
405 void
406ex_debug(exarg_T *eap)
407{
408 int debug_break_level_save = debug_break_level;
409
410 debug_break_level = 9999;
411 do_cmdline_cmd(eap->arg);
412 debug_break_level = debug_break_level_save;
413}
414
415static char_u *debug_breakpoint_name = NULL;
416static linenr_T debug_breakpoint_lnum;
417
418/*
419 * When debugging or a breakpoint is set on a skipped command, no debug prompt
420 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
421 * debug_skipped_name is then set to the source name in the breakpoint case. If
422 * a skipped command decides itself that a debug prompt should be displayed, it
423 * can do so by calling dbg_check_skipped().
424 */
425static int debug_skipped;
426static char_u *debug_skipped_name;
427
428/*
429 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
430 * at or below the break level. But only when the line is actually
431 * executed. Return TRUE and set breakpoint_name for skipped commands that
432 * decide to execute something themselves.
433 * Called from do_one_cmd() before executing a command.
434 */
435 void
436dbg_check_breakpoint(exarg_T *eap)
437{
438 char_u *p;
439
440 debug_skipped = FALSE;
441 if (debug_breakpoint_name != NULL)
442 {
443 if (!eap->skip)
444 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200445 // replace K_SNR with "<SNR>"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200446 if (debug_breakpoint_name[0] == K_SPECIAL
447 && debug_breakpoint_name[1] == KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100448 && debug_breakpoint_name[2] == KE_SNR)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200449 p = (char_u *)"<SNR>";
450 else
451 p = (char_u *)"";
452 smsg(_("Breakpoint in \"%s%s\" line %ld"),
453 p,
454 debug_breakpoint_name + (*p == NUL ? 0 : 3),
455 (long)debug_breakpoint_lnum);
456 debug_breakpoint_name = NULL;
457 do_debug(eap->cmd);
458 }
459 else
460 {
461 debug_skipped = TRUE;
462 debug_skipped_name = debug_breakpoint_name;
463 debug_breakpoint_name = NULL;
464 }
465 }
466 else if (ex_nesting_level <= debug_break_level)
467 {
468 if (!eap->skip)
469 do_debug(eap->cmd);
470 else
471 {
472 debug_skipped = TRUE;
473 debug_skipped_name = NULL;
474 }
475 }
476}
477
478/*
479 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
480 * set. Return TRUE when the debug mode is entered this time.
481 */
482 int
483dbg_check_skipped(exarg_T *eap)
484{
485 int prev_got_int;
486
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000487 if (!debug_skipped)
488 return FALSE;
489
490 // Save the value of got_int and reset it. We don't want a previous
491 // interruption cause flushing the input buffer.
492 prev_got_int = got_int;
493 got_int = FALSE;
494 debug_breakpoint_name = debug_skipped_name;
495 // eap->skip is TRUE
496 eap->skip = FALSE;
497 (void)dbg_check_breakpoint(eap);
498 eap->skip = TRUE;
499 got_int |= prev_got_int;
500 return TRUE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200501}
502
503/*
504 * The list of breakpoints: dbg_breakp.
505 * This is a grow-array of structs.
506 */
507struct debuggy
508{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200509 int dbg_nr; // breakpoint number
510 int dbg_type; // DBG_FUNC, DBG_FILE or DBG_EXPR
511 char_u *dbg_name; // function, expression or file name
512 regprog_T *dbg_prog; // regexp program
513 linenr_T dbg_lnum; // line number in function or file
514 int dbg_forceit; // ! used
Bram Moolenaareead75c2019-04-21 11:35:00 +0200515#ifdef FEAT_EVAL
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200516 typval_T *dbg_val; // last result of watchexpression
Bram Moolenaareead75c2019-04-21 11:35:00 +0200517#endif
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200518 int dbg_level; // stored nested level for expr
Bram Moolenaareead75c2019-04-21 11:35:00 +0200519};
520
521static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL};
522#define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
523#define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200524static int last_breakp = 0; // nr of last defined breakpoint
Bram Moolenaar26a44842021-09-02 18:49:06 +0200525static int has_expr_breakpoint = FALSE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200526
527#ifdef FEAT_PROFILE
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200528// Profiling uses file and func names similar to breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200529static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL};
530#endif
531#define DBG_FUNC 1
532#define DBG_FILE 2
533#define DBG_EXPR 3
534
535static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp);
536
537/*
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200538 * Evaluate the "bp->dbg_name" expression and return the result.
Bram Moolenaar0325d392021-09-09 12:34:19 +0200539 * Disables error messages.
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200540 */
541 static typval_T *
Bram Moolenaar0325d392021-09-09 12:34:19 +0200542eval_expr_no_emsg(struct debuggy *bp)
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200543{
544 typval_T *tv;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200545
Bram Moolenaar0325d392021-09-09 12:34:19 +0200546 // Disable error messages, a bad expression would make Vim unusable.
547 ++emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200548 tv = eval_expr(bp->dbg_name, NULL);
Bram Moolenaar0325d392021-09-09 12:34:19 +0200549 --emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200550
551 return tv;
552}
553
554/*
Bram Moolenaareead75c2019-04-21 11:35:00 +0200555 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
556 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
557 * is allocated.
558 * Returns FAIL for failure.
559 */
560 static int
561dbg_parsearg(
562 char_u *arg,
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200563 garray_T *gap) // either &dbg_breakp or &prof_ga
Bram Moolenaareead75c2019-04-21 11:35:00 +0200564{
565 char_u *p = arg;
566 char_u *q;
567 struct debuggy *bp;
568 int here = FALSE;
569
570 if (ga_grow(gap, 1) == FAIL)
571 return FAIL;
572 bp = &DEBUGGY(gap, gap->ga_len);
573
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200574 // Find "func" or "file".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200575 if (STRNCMP(p, "func", 4) == 0)
576 bp->dbg_type = DBG_FUNC;
577 else if (STRNCMP(p, "file", 4) == 0)
578 bp->dbg_type = DBG_FILE;
579 else if (
580#ifdef FEAT_PROFILE
581 gap != &prof_ga &&
582#endif
583 STRNCMP(p, "here", 4) == 0)
584 {
585 if (curbuf->b_ffname == NULL)
586 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200587 emsg(_(e_no_file_name));
Bram Moolenaareead75c2019-04-21 11:35:00 +0200588 return FAIL;
589 }
590 bp->dbg_type = DBG_FILE;
591 here = TRUE;
592 }
593 else if (
594#ifdef FEAT_PROFILE
595 gap != &prof_ga &&
596#endif
597 STRNCMP(p, "expr", 4) == 0)
598 bp->dbg_type = DBG_EXPR;
599 else
600 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000601 semsg(_(e_invalid_argument_str), p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200602 return FAIL;
603 }
604 p = skipwhite(p + 4);
605
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200606 // Find optional line number.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200607 if (here)
608 bp->dbg_lnum = curwin->w_cursor.lnum;
609 else if (
610#ifdef FEAT_PROFILE
611 gap != &prof_ga &&
612#endif
613 VIM_ISDIGIT(*p))
614 {
615 bp->dbg_lnum = getdigits(&p);
616 p = skipwhite(p);
617 }
618 else
619 bp->dbg_lnum = 0;
620
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200621 // Find the function or file name. Don't accept a function name with ().
Bram Moolenaareead75c2019-04-21 11:35:00 +0200622 if ((!here && *p == NUL)
623 || (here && *p != NUL)
624 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
625 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000626 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200627 return FAIL;
628 }
629
630 if (bp->dbg_type == DBG_FUNC)
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200631 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200632 else if (here)
633 bp->dbg_name = vim_strsave(curbuf->b_ffname);
634 else if (bp->dbg_type == DBG_EXPR)
635 {
636 bp->dbg_name = vim_strsave(p);
637 if (bp->dbg_name != NULL)
Bram Moolenaar0325d392021-09-09 12:34:19 +0200638 bp->dbg_val = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200639 }
640 else
641 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200642 // Expand the file name in the same way as do_source(). This means
643 // doing it twice, so that $DIR/file gets expanded when $DIR is
644 // "~/dir".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200645 q = expand_env_save(p);
646 if (q == NULL)
647 return FAIL;
648 p = expand_env_save(q);
649 vim_free(q);
650 if (p == NULL)
651 return FAIL;
652 if (*p != '*')
653 {
654 bp->dbg_name = fix_fname(p);
655 vim_free(p);
656 }
657 else
658 bp->dbg_name = p;
659 }
660
661 if (bp->dbg_name == NULL)
662 return FAIL;
663 return OK;
664}
665
666/*
667 * ":breakadd". Also used for ":profile".
668 */
669 void
670ex_breakadd(exarg_T *eap)
671{
672 struct debuggy *bp;
673 char_u *pat;
674 garray_T *gap;
675
676 gap = &dbg_breakp;
677#ifdef FEAT_PROFILE
678 if (eap->cmdidx == CMD_profile)
679 gap = &prof_ga;
680#endif
681
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000682 if (dbg_parsearg(eap->arg, gap) != OK)
683 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200684
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000685 bp = &DEBUGGY(gap, gap->ga_len);
686 bp->dbg_forceit = eap->forceit;
687
688 if (bp->dbg_type != DBG_EXPR)
689 {
690 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
691 if (pat != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200692 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000693 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
694 vim_free(pat);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200695 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000696 if (pat == NULL || bp->dbg_prog == NULL)
697 vim_free(bp->dbg_name);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200698 else
699 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000700 if (bp->dbg_lnum == 0) // default line number is 1
701 bp->dbg_lnum = 1;
702#ifdef FEAT_PROFILE
703 if (eap->cmdidx != CMD_profile)
704#endif
705 {
706 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
707 ++debug_tick;
708 }
709 ++gap->ga_len;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200710 }
711 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000712 else
713 {
714 // DBG_EXPR
715 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp;
716 ++debug_tick;
717 if (gap == &dbg_breakp)
718 has_expr_breakpoint = TRUE;
719 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200720}
721
722/*
723 * ":debuggreedy".
724 */
725 void
726ex_debuggreedy(exarg_T *eap)
727{
728 if (eap->addr_count == 0 || eap->line2 != 0)
729 debug_greedy = TRUE;
730 else
731 debug_greedy = FALSE;
732}
733
Bram Moolenaar26a44842021-09-02 18:49:06 +0200734 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000735update_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200736{
737 int i;
738
739 has_expr_breakpoint = FALSE;
740 for (i = 0; i < dbg_breakp.ga_len; ++i)
741 if (BREAKP(i).dbg_type == DBG_EXPR)
742 {
743 has_expr_breakpoint = TRUE;
744 break;
745 }
746}
747
748/*
749 * Return TRUE if there is any expression breakpoint.
750 */
751 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000752debug_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200753{
754 return has_expr_breakpoint;
755}
756
Bram Moolenaareead75c2019-04-21 11:35:00 +0200757/*
758 * ":breakdel" and ":profdel".
759 */
760 void
761ex_breakdel(exarg_T *eap)
762{
763 struct debuggy *bp, *bpi;
764 int nr;
765 int todel = -1;
766 int del_all = FALSE;
767 int i;
768 linenr_T best_lnum = 0;
769 garray_T *gap;
770
771 gap = &dbg_breakp;
772 if (eap->cmdidx == CMD_profdel)
773 {
774#ifdef FEAT_PROFILE
775 gap = &prof_ga;
776#else
777 ex_ni(eap);
778 return;
779#endif
780 }
781
782 if (vim_isdigit(*eap->arg))
783 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200784 // ":breakdel {nr}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200785 nr = atol((char *)eap->arg);
786 for (i = 0; i < gap->ga_len; ++i)
787 if (DEBUGGY(gap, i).dbg_nr == nr)
788 {
789 todel = i;
790 break;
791 }
792 }
793 else if (*eap->arg == '*')
794 {
795 todel = 0;
796 del_all = TRUE;
797 }
798 else
799 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200800 // ":breakdel {func|file|expr} [lnum] {name}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200801 if (dbg_parsearg(eap->arg, gap) == FAIL)
802 return;
803 bp = &DEBUGGY(gap, gap->ga_len);
804 for (i = 0; i < gap->ga_len; ++i)
805 {
806 bpi = &DEBUGGY(gap, i);
807 if (bp->dbg_type == bpi->dbg_type
808 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
809 && (bp->dbg_lnum == bpi->dbg_lnum
810 || (bp->dbg_lnum == 0
811 && (best_lnum == 0
812 || bpi->dbg_lnum < best_lnum))))
813 {
814 todel = i;
815 best_lnum = bpi->dbg_lnum;
816 }
817 }
818 vim_free(bp->dbg_name);
819 }
820
821 if (todel < 0)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200822 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000823 semsg(_(e_breakpoint_not_found_str), eap->arg);
824 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200825 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000826
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();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200853}
854
855/*
856 * ":breaklist".
857 */
858 void
859ex_breaklist(exarg_T *eap UNUSED)
860{
861 struct debuggy *bp;
862 int i;
863
864 if (dbg_breakp.ga_len == 0)
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000865 {
Bram Moolenaareead75c2019-04-21 11:35:00 +0200866 msg(_("No breakpoints defined"));
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000867 return;
868 }
869
870 for (i = 0; i < dbg_breakp.ga_len; ++i)
871 {
872 bp = &BREAKP(i);
873 if (bp->dbg_type == DBG_FILE)
874 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
875 if (bp->dbg_type != DBG_EXPR)
876 smsg(_("%3d %s %s line %ld"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200877 bp->dbg_nr,
878 bp->dbg_type == DBG_FUNC ? "func" : "file",
879 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
880 (long)bp->dbg_lnum);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000881 else
882 smsg(_("%3d expr %s"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200883 bp->dbg_nr, bp->dbg_name);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000884 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200885}
886
887/*
888 * Find a breakpoint for a function or sourced file.
889 * Returns line number at which to break; zero when no matching breakpoint.
890 */
891 linenr_T
892dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200893 int file, // TRUE for a file, FALSE for a function
894 char_u *fname, // file or function name
895 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200896{
897 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
898}
899
900#if defined(FEAT_PROFILE) || defined(PROTO)
901/*
902 * Return TRUE if profiling is on for a function or sourced file.
903 */
904 int
905has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200906 int file, // TRUE for a file, FALSE for a function
907 char_u *fname, // file or function name
908 int *fp) // return: forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200909{
910 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
911 != (linenr_T)0);
912}
913#endif
914
915/*
916 * Common code for dbg_find_breakpoint() and has_profiling().
917 */
918 static linenr_T
919debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +0100920 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200921 char_u *fname, // file or function name
922 linenr_T after, // after this line number
923 garray_T *gap, // either &dbg_breakp or &prof_ga
924 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200925{
926 struct debuggy *bp;
927 int i;
928 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100929 char_u *name = NULL;
930 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200931 int prev_got_int;
932
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200933 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200934 if (gap->ga_len == 0)
935 return (linenr_T)0;
936
Bram Moolenaarb2049902021-01-24 12:53:53 +0100937 // For a script-local function remove the prefix, so that
938 // "profile func Func" matches "Func" in any script. Otherwise it's very
939 // difficult to profile/debug a script-local function. It may match a
940 // function in the wrong script, but that is much better than not being
941 // able to profile/debug a function in a script with unknown ID.
942 // Also match a script-specific name.
943 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200944 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100945 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200946 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100947 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200948 {
949 STRCPY(name, "<SNR>");
950 STRCPY(name + 5, fname + 3);
951 }
952 }
953
954 for (i = 0; i < gap->ga_len; ++i)
955 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200956 // Skip entries that are not useful or are for a line that is beyond
957 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200958 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100959 if (((bp->dbg_type == DBG_FILE) == is_file
960 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +0200961#ifdef FEAT_PROFILE
962 gap == &prof_ga ||
963#endif
964 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
965 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200966 // Save the value of got_int and reset it. We don't want a
967 // previous interruption cancel matching, only hitting CTRL-C
968 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200969 prev_got_int = got_int;
970 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100971 if ((name != NULL
972 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
973 || vim_regexec_prog(&bp->dbg_prog, FALSE,
974 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +0200975 {
976 lnum = bp->dbg_lnum;
977 if (fp != NULL)
978 *fp = bp->dbg_forceit;
979 }
980 got_int |= prev_got_int;
981 }
982#ifdef FEAT_EVAL
983 else if (bp->dbg_type == DBG_EXPR)
984 {
985 typval_T *tv;
986 int line = FALSE;
987
Bram Moolenaar0325d392021-09-09 12:34:19 +0200988 tv = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200989 if (tv != NULL)
990 {
991 if (bp->dbg_val == NULL)
992 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100993 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200994 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +0100995 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200996 line = TRUE;
997 }
998 else
999 {
Bram Moolenaarcf666202022-03-10 13:29:20 +00001000 // Use "==" instead of "is" for strings, that is what we
1001 // always have done.
1002 exprtype_T type = tv->v_type == VAR_STRING
1003 ? EXPR_EQUAL : EXPR_IS;
1004
1005 if (typval_compare(tv, bp->dbg_val, type, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +02001006 && tv->vval.v_number == FALSE)
1007 {
1008 typval_T *v;
1009
1010 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +01001011 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001012 // Need to evaluate again, typval_compare() overwrites
1013 // "tv".
Bram Moolenaar0325d392021-09-09 12:34:19 +02001014 v = eval_expr_no_emsg(bp);
Bram Moolenaar34453202021-01-31 13:08:38 +01001015 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001016 free_tv(bp->dbg_val);
1017 bp->dbg_val = v;
1018 }
1019 free_tv(tv);
1020 }
1021 }
1022 else if (bp->dbg_val != NULL)
1023 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001024 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1025 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001026 free_tv(bp->dbg_val);
1027 bp->dbg_val = NULL;
1028 line = TRUE;
1029 }
1030
1031 if (line)
1032 {
1033 lnum = after > 0 ? after : 1;
1034 break;
1035 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001036 }
1037#endif
1038 }
1039 if (name != fname)
1040 vim_free(name);
1041
1042 return lnum;
1043}
1044
1045/*
1046 * Called when a breakpoint was encountered.
1047 */
1048 void
1049dbg_breakpoint(char_u *name, linenr_T lnum)
1050{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001051 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001052 debug_breakpoint_name = name;
1053 debug_breakpoint_lnum = lnum;
1054}
1055#endif