blob: 1352f499948a83249e42910ad88aa16e44e911f8 [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);
100 vim_free(debug_oldval);
101 debug_oldval = NULL;
102 }
103 if (debug_newval != NULL)
104 {
105 smsg(_("Newval = \"%s\""), debug_newval);
106 vim_free(debug_newval);
107 debug_newval = NULL;
108 }
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200109 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100110 if (sname != NULL)
111 msg((char *)sname);
112 vim_free(sname);
113 if (SOURCING_LNUM != 0)
114 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200115 else
116 smsg(_("cmd: %s"), cmd);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200117
118 // Repeat getting a command and executing it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200119 for (;;)
120 {
121 msg_scroll = TRUE;
122 need_wait_return = FALSE;
123
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200124 // Save the current typeahead buffer and replace it with an empty one.
125 // This makes sure we get input from the user here and don't interfere
126 // with the commands being executed. Reset "ex_normal_busy" to avoid
127 // the side effects of using ":normal". Save the stuff buffer and make
128 // it empty. Set ignore_script to avoid reading from script input.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200129 save_ex_normal_busy = ex_normal_busy;
130 ex_normal_busy = 0;
131 if (!debug_greedy)
132 {
133 save_typeahead(&typeaheadbuf);
134 typeahead_saved = TRUE;
135 save_ignore_script = ignore_script;
136 ignore_script = TRUE;
137 }
138
Bram Moolenaar747f1102022-09-18 13:06:41 +0100139 // don't debug any function call, e.g. from an expression mapping
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100140 n = debug_break_level;
141 debug_break_level = -1;
142
Bram Moolenaareead75c2019-04-21 11:35:00 +0200143 vim_free(cmdline);
144 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL);
145
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100146 debug_break_level = n;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200147 if (typeahead_saved)
148 {
Bram Moolenaarc41badb2021-06-07 22:04:52 +0200149 restore_typeahead(&typeaheadbuf, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200150 ignore_script = save_ignore_script;
151 }
152 ex_normal_busy = save_ex_normal_busy;
153
154 cmdline_row = msg_row;
155 msg_starthere();
156 if (cmdline != NULL)
157 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200158 // If this is a debug command, set "last_cmd".
159 // If not, reset "last_cmd".
160 // For a blank line use previous command.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200161 p = skipwhite(cmdline);
162 if (*p != NUL)
163 {
164 switch (*p)
165 {
166 case 'c': last_cmd = CMD_CONT;
167 tail = "ont";
168 break;
169 case 'n': last_cmd = CMD_NEXT;
170 tail = "ext";
171 break;
172 case 's': last_cmd = CMD_STEP;
173 tail = "tep";
174 break;
175 case 'f':
176 last_cmd = 0;
177 if (p[1] == 'r')
178 {
179 last_cmd = CMD_FRAME;
180 tail = "rame";
181 }
182 else
183 {
184 last_cmd = CMD_FINISH;
185 tail = "inish";
186 }
187 break;
188 case 'q': last_cmd = CMD_QUIT;
189 tail = "uit";
190 break;
191 case 'i': last_cmd = CMD_INTERRUPT;
192 tail = "nterrupt";
193 break;
194 case 'b': last_cmd = CMD_BACKTRACE;
195 if (p[1] == 't')
196 tail = "t";
197 else
198 tail = "acktrace";
199 break;
200 case 'w': last_cmd = CMD_BACKTRACE;
201 tail = "here";
202 break;
203 case 'u': last_cmd = CMD_UP;
204 tail = "p";
205 break;
206 case 'd': last_cmd = CMD_DOWN;
207 tail = "own";
208 break;
209 default: last_cmd = 0;
210 }
211 if (last_cmd != 0)
212 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200213 // Check that the tail matches.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200214 ++p;
215 while (*p != NUL && *p == *tail)
216 {
217 ++p;
218 ++tail;
219 }
220 if (ASCII_ISALPHA(*p) && last_cmd != CMD_FRAME)
221 last_cmd = 0;
222 }
223 }
224
225 if (last_cmd != 0)
226 {
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200227 // Execute debug command: decide where to break next and
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200228 // return.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200229 switch (last_cmd)
230 {
231 case CMD_CONT:
232 debug_break_level = -1;
233 break;
234 case CMD_NEXT:
235 debug_break_level = ex_nesting_level;
236 break;
237 case CMD_STEP:
238 debug_break_level = 9999;
239 break;
240 case CMD_FINISH:
241 debug_break_level = ex_nesting_level - 1;
242 break;
243 case CMD_QUIT:
244 got_int = TRUE;
245 debug_break_level = -1;
246 break;
247 case CMD_INTERRUPT:
248 got_int = TRUE;
249 debug_break_level = 9999;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200250 // Do not repeat ">interrupt" cmd, continue stepping.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200251 last_cmd = CMD_STEP;
252 break;
253 case CMD_BACKTRACE:
254 do_showbacktrace(cmd);
255 continue;
256 case CMD_FRAME:
257 if (*p == NUL)
258 {
259 do_showbacktrace(cmd);
260 }
261 else
262 {
263 p = skipwhite(p);
264 do_setdebugtracelevel(p);
265 }
266 continue;
267 case CMD_UP:
268 debug_backtrace_level++;
269 do_checkbacktracelevel();
270 continue;
271 case CMD_DOWN:
272 debug_backtrace_level--;
273 do_checkbacktracelevel();
274 continue;
275 }
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200276 // Going out reset backtrace_level
Bram Moolenaareead75c2019-04-21 11:35:00 +0200277 debug_backtrace_level = 0;
278 break;
279 }
280
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200281 // don't debug this command
Bram Moolenaareead75c2019-04-21 11:35:00 +0200282 n = debug_break_level;
283 debug_break_level = -1;
284 (void)do_cmdline(cmdline, getexline, NULL,
285 DOCMD_VERBOSE|DOCMD_EXCRESET);
286 debug_break_level = n;
287 }
288 lines_left = Rows - 1;
289 }
290 vim_free(cmdline);
291
292 --RedrawingDisabled;
293 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100294 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200295 need_wait_return = FALSE;
296 msg_scroll = save_msg_scroll;
Bram Moolenaar9781d9c2022-09-20 13:51:25 +0100297 restore_timeout_for_debugging();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200298 lines_left = Rows - 1;
299 State = save_State;
300 debug_mode = FALSE;
301 did_emsg = save_did_emsg;
302 cmd_silent = save_cmd_silent;
303 msg_silent = save_msg_silent;
304 emsg_silent = save_emsg_silent;
305 redir_off = save_redir_off;
306
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200307 // Only print the message again when typing a command before coming back
308 // here.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200309 debug_did_msg = TRUE;
310}
311
312 static int
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100313get_maxbacktrace_level(char_u *sname)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200314{
315 char *p, *q;
316 int maxbacktrace = 0;
317
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100318 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200319 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100320 p = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200321 while ((q = strstr(p, "..")) != NULL)
322 {
323 p = q + 2;
324 maxbacktrace++;
325 }
326 }
327 return maxbacktrace;
328}
329
330 static void
331do_setdebugtracelevel(char_u *arg)
332{
333 int level;
334
335 level = atoi((char *)arg);
336 if (*arg == '+' || level < 0)
337 debug_backtrace_level += level;
338 else
339 debug_backtrace_level = level;
340
341 do_checkbacktracelevel();
342}
343
344 static void
345do_checkbacktracelevel(void)
346{
347 if (debug_backtrace_level < 0)
348 {
349 debug_backtrace_level = 0;
350 msg(_("frame is zero"));
351 }
352 else
353 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200354 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100355 int max = get_maxbacktrace_level(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200356
357 if (debug_backtrace_level > max)
358 {
359 debug_backtrace_level = max;
360 smsg(_("frame at highest level: %d"), max);
361 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100362 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200363 }
364}
365
366 static void
367do_showbacktrace(char_u *cmd)
368{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100369 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200370 char *cur;
371 char *next;
372 int i = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100373 int max;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200374
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200375 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100376 max = get_maxbacktrace_level(sname);
377 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200378 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100379 cur = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200380 while (!got_int)
381 {
382 next = strstr(cur, "..");
383 if (next != NULL)
384 *next = NUL;
385 if (i == max - debug_backtrace_level)
386 smsg("->%d %s", max - i, cur);
387 else
388 smsg(" %d %s", max - i, cur);
389 ++i;
390 if (next == NULL)
391 break;
392 *next = '.';
393 cur = next + 2;
394 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100395 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200396 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100397
398 if (SOURCING_LNUM != 0)
399 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200400 else
401 smsg(_("cmd: %s"), cmd);
402}
403
404/*
405 * ":debug".
406 */
407 void
408ex_debug(exarg_T *eap)
409{
410 int debug_break_level_save = debug_break_level;
411
412 debug_break_level = 9999;
413 do_cmdline_cmd(eap->arg);
414 debug_break_level = debug_break_level_save;
415}
416
417static char_u *debug_breakpoint_name = NULL;
418static linenr_T debug_breakpoint_lnum;
419
420/*
421 * When debugging or a breakpoint is set on a skipped command, no debug prompt
422 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
423 * debug_skipped_name is then set to the source name in the breakpoint case. If
424 * a skipped command decides itself that a debug prompt should be displayed, it
425 * can do so by calling dbg_check_skipped().
426 */
427static int debug_skipped;
428static char_u *debug_skipped_name;
429
430/*
431 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
432 * at or below the break level. But only when the line is actually
433 * executed. Return TRUE and set breakpoint_name for skipped commands that
434 * decide to execute something themselves.
435 * Called from do_one_cmd() before executing a command.
436 */
437 void
438dbg_check_breakpoint(exarg_T *eap)
439{
440 char_u *p;
441
442 debug_skipped = FALSE;
443 if (debug_breakpoint_name != NULL)
444 {
445 if (!eap->skip)
446 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200447 // replace K_SNR with "<SNR>"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200448 if (debug_breakpoint_name[0] == K_SPECIAL
449 && debug_breakpoint_name[1] == KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100450 && debug_breakpoint_name[2] == KE_SNR)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200451 p = (char_u *)"<SNR>";
452 else
453 p = (char_u *)"";
454 smsg(_("Breakpoint in \"%s%s\" line %ld"),
455 p,
456 debug_breakpoint_name + (*p == NUL ? 0 : 3),
457 (long)debug_breakpoint_lnum);
458 debug_breakpoint_name = NULL;
459 do_debug(eap->cmd);
460 }
461 else
462 {
463 debug_skipped = TRUE;
464 debug_skipped_name = debug_breakpoint_name;
465 debug_breakpoint_name = NULL;
466 }
467 }
468 else if (ex_nesting_level <= debug_break_level)
469 {
470 if (!eap->skip)
471 do_debug(eap->cmd);
472 else
473 {
474 debug_skipped = TRUE;
475 debug_skipped_name = NULL;
476 }
477 }
478}
479
480/*
481 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
482 * set. Return TRUE when the debug mode is entered this time.
483 */
484 int
485dbg_check_skipped(exarg_T *eap)
486{
487 int prev_got_int;
488
489 if (debug_skipped)
490 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200491 // Save the value of got_int and reset it. We don't want a previous
492 // interruption cause flushing the input buffer.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200493 prev_got_int = got_int;
494 got_int = FALSE;
495 debug_breakpoint_name = debug_skipped_name;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200496 // eap->skip is TRUE
Bram Moolenaareead75c2019-04-21 11:35:00 +0200497 eap->skip = FALSE;
498 (void)dbg_check_breakpoint(eap);
499 eap->skip = TRUE;
500 got_int |= prev_got_int;
501 return TRUE;
502 }
503 return FALSE;
504}
505
506/*
507 * The list of breakpoints: dbg_breakp.
508 * This is a grow-array of structs.
509 */
510struct debuggy
511{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200512 int dbg_nr; // breakpoint number
513 int dbg_type; // DBG_FUNC, DBG_FILE or DBG_EXPR
514 char_u *dbg_name; // function, expression or file name
515 regprog_T *dbg_prog; // regexp program
516 linenr_T dbg_lnum; // line number in function or file
517 int dbg_forceit; // ! used
Bram Moolenaareead75c2019-04-21 11:35:00 +0200518#ifdef FEAT_EVAL
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200519 typval_T *dbg_val; // last result of watchexpression
Bram Moolenaareead75c2019-04-21 11:35:00 +0200520#endif
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200521 int dbg_level; // stored nested level for expr
Bram Moolenaareead75c2019-04-21 11:35:00 +0200522};
523
524static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL};
525#define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
526#define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200527static int last_breakp = 0; // nr of last defined breakpoint
Bram Moolenaar26a44842021-09-02 18:49:06 +0200528static int has_expr_breakpoint = FALSE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200529
530#ifdef FEAT_PROFILE
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200531// Profiling uses file and func names similar to breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200532static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL};
533#endif
534#define DBG_FUNC 1
535#define DBG_FILE 2
536#define DBG_EXPR 3
537
538static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp);
539
540/*
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200541 * Evaluate the "bp->dbg_name" expression and return the result.
Bram Moolenaar0325d392021-09-09 12:34:19 +0200542 * Disables error messages.
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200543 */
544 static typval_T *
Bram Moolenaar0325d392021-09-09 12:34:19 +0200545eval_expr_no_emsg(struct debuggy *bp)
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200546{
547 typval_T *tv;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200548
Bram Moolenaar0325d392021-09-09 12:34:19 +0200549 // Disable error messages, a bad expression would make Vim unusable.
550 ++emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200551 tv = eval_expr(bp->dbg_name, NULL);
Bram Moolenaar0325d392021-09-09 12:34:19 +0200552 --emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200553
554 return tv;
555}
556
557/*
Bram Moolenaareead75c2019-04-21 11:35:00 +0200558 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
559 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
560 * is allocated.
561 * Returns FAIL for failure.
562 */
563 static int
564dbg_parsearg(
565 char_u *arg,
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200566 garray_T *gap) // either &dbg_breakp or &prof_ga
Bram Moolenaareead75c2019-04-21 11:35:00 +0200567{
568 char_u *p = arg;
569 char_u *q;
570 struct debuggy *bp;
571 int here = FALSE;
572
573 if (ga_grow(gap, 1) == FAIL)
574 return FAIL;
575 bp = &DEBUGGY(gap, gap->ga_len);
576
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200577 // Find "func" or "file".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200578 if (STRNCMP(p, "func", 4) == 0)
579 bp->dbg_type = DBG_FUNC;
580 else if (STRNCMP(p, "file", 4) == 0)
581 bp->dbg_type = DBG_FILE;
582 else if (
583#ifdef FEAT_PROFILE
584 gap != &prof_ga &&
585#endif
586 STRNCMP(p, "here", 4) == 0)
587 {
588 if (curbuf->b_ffname == NULL)
589 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200590 emsg(_(e_no_file_name));
Bram Moolenaareead75c2019-04-21 11:35:00 +0200591 return FAIL;
592 }
593 bp->dbg_type = DBG_FILE;
594 here = TRUE;
595 }
596 else if (
597#ifdef FEAT_PROFILE
598 gap != &prof_ga &&
599#endif
600 STRNCMP(p, "expr", 4) == 0)
601 bp->dbg_type = DBG_EXPR;
602 else
603 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000604 semsg(_(e_invalid_argument_str), p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200605 return FAIL;
606 }
607 p = skipwhite(p + 4);
608
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200609 // Find optional line number.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200610 if (here)
611 bp->dbg_lnum = curwin->w_cursor.lnum;
612 else if (
613#ifdef FEAT_PROFILE
614 gap != &prof_ga &&
615#endif
616 VIM_ISDIGIT(*p))
617 {
618 bp->dbg_lnum = getdigits(&p);
619 p = skipwhite(p);
620 }
621 else
622 bp->dbg_lnum = 0;
623
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200624 // Find the function or file name. Don't accept a function name with ().
Bram Moolenaareead75c2019-04-21 11:35:00 +0200625 if ((!here && *p == NUL)
626 || (here && *p != NUL)
627 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
628 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000629 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200630 return FAIL;
631 }
632
633 if (bp->dbg_type == DBG_FUNC)
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200634 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200635 else if (here)
636 bp->dbg_name = vim_strsave(curbuf->b_ffname);
637 else if (bp->dbg_type == DBG_EXPR)
638 {
639 bp->dbg_name = vim_strsave(p);
640 if (bp->dbg_name != NULL)
Bram Moolenaar0325d392021-09-09 12:34:19 +0200641 bp->dbg_val = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200642 }
643 else
644 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200645 // Expand the file name in the same way as do_source(). This means
646 // doing it twice, so that $DIR/file gets expanded when $DIR is
647 // "~/dir".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200648 q = expand_env_save(p);
649 if (q == NULL)
650 return FAIL;
651 p = expand_env_save(q);
652 vim_free(q);
653 if (p == NULL)
654 return FAIL;
655 if (*p != '*')
656 {
657 bp->dbg_name = fix_fname(p);
658 vim_free(p);
659 }
660 else
661 bp->dbg_name = p;
662 }
663
664 if (bp->dbg_name == NULL)
665 return FAIL;
666 return OK;
667}
668
669/*
670 * ":breakadd". Also used for ":profile".
671 */
672 void
673ex_breakadd(exarg_T *eap)
674{
675 struct debuggy *bp;
676 char_u *pat;
677 garray_T *gap;
678
679 gap = &dbg_breakp;
680#ifdef FEAT_PROFILE
681 if (eap->cmdidx == CMD_profile)
682 gap = &prof_ga;
683#endif
684
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000685 if (dbg_parsearg(eap->arg, gap) != OK)
686 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200687
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000688 bp = &DEBUGGY(gap, gap->ga_len);
689 bp->dbg_forceit = eap->forceit;
690
691 if (bp->dbg_type != DBG_EXPR)
692 {
693 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
694 if (pat != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200695 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000696 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
697 vim_free(pat);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200698 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000699 if (pat == NULL || bp->dbg_prog == NULL)
700 vim_free(bp->dbg_name);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200701 else
702 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000703 if (bp->dbg_lnum == 0) // default line number is 1
704 bp->dbg_lnum = 1;
705#ifdef FEAT_PROFILE
706 if (eap->cmdidx != CMD_profile)
707#endif
708 {
709 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
710 ++debug_tick;
711 }
712 ++gap->ga_len;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200713 }
714 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000715 else
716 {
717 // DBG_EXPR
718 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp;
719 ++debug_tick;
720 if (gap == &dbg_breakp)
721 has_expr_breakpoint = TRUE;
722 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200723}
724
725/*
726 * ":debuggreedy".
727 */
728 void
729ex_debuggreedy(exarg_T *eap)
730{
731 if (eap->addr_count == 0 || eap->line2 != 0)
732 debug_greedy = TRUE;
733 else
734 debug_greedy = FALSE;
735}
736
Bram Moolenaar26a44842021-09-02 18:49:06 +0200737 static void
738update_has_expr_breakpoint()
739{
740 int i;
741
742 has_expr_breakpoint = FALSE;
743 for (i = 0; i < dbg_breakp.ga_len; ++i)
744 if (BREAKP(i).dbg_type == DBG_EXPR)
745 {
746 has_expr_breakpoint = TRUE;
747 break;
748 }
749}
750
751/*
752 * Return TRUE if there is any expression breakpoint.
753 */
754 int
755debug_has_expr_breakpoint()
756{
757 return has_expr_breakpoint;
758}
759
Bram Moolenaareead75c2019-04-21 11:35:00 +0200760/*
761 * ":breakdel" and ":profdel".
762 */
763 void
764ex_breakdel(exarg_T *eap)
765{
766 struct debuggy *bp, *bpi;
767 int nr;
768 int todel = -1;
769 int del_all = FALSE;
770 int i;
771 linenr_T best_lnum = 0;
772 garray_T *gap;
773
774 gap = &dbg_breakp;
775 if (eap->cmdidx == CMD_profdel)
776 {
777#ifdef FEAT_PROFILE
778 gap = &prof_ga;
779#else
780 ex_ni(eap);
781 return;
782#endif
783 }
784
785 if (vim_isdigit(*eap->arg))
786 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200787 // ":breakdel {nr}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200788 nr = atol((char *)eap->arg);
789 for (i = 0; i < gap->ga_len; ++i)
790 if (DEBUGGY(gap, i).dbg_nr == nr)
791 {
792 todel = i;
793 break;
794 }
795 }
796 else if (*eap->arg == '*')
797 {
798 todel = 0;
799 del_all = TRUE;
800 }
801 else
802 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200803 // ":breakdel {func|file|expr} [lnum] {name}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200804 if (dbg_parsearg(eap->arg, gap) == FAIL)
805 return;
806 bp = &DEBUGGY(gap, gap->ga_len);
807 for (i = 0; i < gap->ga_len; ++i)
808 {
809 bpi = &DEBUGGY(gap, i);
810 if (bp->dbg_type == bpi->dbg_type
811 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
812 && (bp->dbg_lnum == bpi->dbg_lnum
813 || (bp->dbg_lnum == 0
814 && (best_lnum == 0
815 || bpi->dbg_lnum < best_lnum))))
816 {
817 todel = i;
818 best_lnum = bpi->dbg_lnum;
819 }
820 }
821 vim_free(bp->dbg_name);
822 }
823
824 if (todel < 0)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200825 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000826 semsg(_(e_breakpoint_not_found_str), eap->arg);
827 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200828 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000829
830 while (gap->ga_len > 0)
831 {
832 vim_free(DEBUGGY(gap, todel).dbg_name);
833#ifdef FEAT_EVAL
834 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR
835 && DEBUGGY(gap, todel).dbg_val != NULL)
836 free_tv(DEBUGGY(gap, todel).dbg_val);
837#endif
838 vim_regfree(DEBUGGY(gap, todel).dbg_prog);
839 --gap->ga_len;
840 if (todel < gap->ga_len)
841 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
842 (gap->ga_len - todel) * sizeof(struct debuggy));
843#ifdef FEAT_PROFILE
844 if (eap->cmdidx == CMD_breakdel)
845#endif
846 ++debug_tick;
847 if (!del_all)
848 break;
849 }
850
851 // If all breakpoints were removed clear the array.
852 if (gap->ga_len == 0)
853 ga_clear(gap);
854 if (gap == &dbg_breakp)
855 update_has_expr_breakpoint();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200856}
857
858/*
859 * ":breaklist".
860 */
861 void
862ex_breaklist(exarg_T *eap UNUSED)
863{
864 struct debuggy *bp;
865 int i;
866
867 if (dbg_breakp.ga_len == 0)
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000868 {
Bram Moolenaareead75c2019-04-21 11:35:00 +0200869 msg(_("No breakpoints defined"));
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000870 return;
871 }
872
873 for (i = 0; i < dbg_breakp.ga_len; ++i)
874 {
875 bp = &BREAKP(i);
876 if (bp->dbg_type == DBG_FILE)
877 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
878 if (bp->dbg_type != DBG_EXPR)
879 smsg(_("%3d %s %s line %ld"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200880 bp->dbg_nr,
881 bp->dbg_type == DBG_FUNC ? "func" : "file",
882 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
883 (long)bp->dbg_lnum);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000884 else
885 smsg(_("%3d expr %s"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200886 bp->dbg_nr, bp->dbg_name);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000887 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200888}
889
890/*
891 * Find a breakpoint for a function or sourced file.
892 * Returns line number at which to break; zero when no matching breakpoint.
893 */
894 linenr_T
895dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200896 int file, // TRUE for a file, FALSE for a function
897 char_u *fname, // file or function name
898 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200899{
900 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
901}
902
903#if defined(FEAT_PROFILE) || defined(PROTO)
904/*
905 * Return TRUE if profiling is on for a function or sourced file.
906 */
907 int
908has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200909 int file, // TRUE for a file, FALSE for a function
910 char_u *fname, // file or function name
911 int *fp) // return: forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200912{
913 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
914 != (linenr_T)0);
915}
916#endif
917
918/*
919 * Common code for dbg_find_breakpoint() and has_profiling().
920 */
921 static linenr_T
922debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +0100923 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200924 char_u *fname, // file or function name
925 linenr_T after, // after this line number
926 garray_T *gap, // either &dbg_breakp or &prof_ga
927 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200928{
929 struct debuggy *bp;
930 int i;
931 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100932 char_u *name = NULL;
933 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200934 int prev_got_int;
935
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200936 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200937 if (gap->ga_len == 0)
938 return (linenr_T)0;
939
Bram Moolenaarb2049902021-01-24 12:53:53 +0100940 // For a script-local function remove the prefix, so that
941 // "profile func Func" matches "Func" in any script. Otherwise it's very
942 // difficult to profile/debug a script-local function. It may match a
943 // function in the wrong script, but that is much better than not being
944 // able to profile/debug a function in a script with unknown ID.
945 // Also match a script-specific name.
946 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200947 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100948 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200949 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100950 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200951 {
952 STRCPY(name, "<SNR>");
953 STRCPY(name + 5, fname + 3);
954 }
955 }
956
957 for (i = 0; i < gap->ga_len; ++i)
958 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200959 // Skip entries that are not useful or are for a line that is beyond
960 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200961 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100962 if (((bp->dbg_type == DBG_FILE) == is_file
963 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +0200964#ifdef FEAT_PROFILE
965 gap == &prof_ga ||
966#endif
967 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
968 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200969 // Save the value of got_int and reset it. We don't want a
970 // previous interruption cancel matching, only hitting CTRL-C
971 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200972 prev_got_int = got_int;
973 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100974 if ((name != NULL
975 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
976 || vim_regexec_prog(&bp->dbg_prog, FALSE,
977 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +0200978 {
979 lnum = bp->dbg_lnum;
980 if (fp != NULL)
981 *fp = bp->dbg_forceit;
982 }
983 got_int |= prev_got_int;
984 }
985#ifdef FEAT_EVAL
986 else if (bp->dbg_type == DBG_EXPR)
987 {
988 typval_T *tv;
989 int line = FALSE;
990
Bram Moolenaar0325d392021-09-09 12:34:19 +0200991 tv = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200992 if (tv != NULL)
993 {
994 if (bp->dbg_val == NULL)
995 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100996 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200997 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +0100998 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200999 line = TRUE;
1000 }
1001 else
1002 {
Bram Moolenaarcf666202022-03-10 13:29:20 +00001003 // Use "==" instead of "is" for strings, that is what we
1004 // always have done.
1005 exprtype_T type = tv->v_type == VAR_STRING
1006 ? EXPR_EQUAL : EXPR_IS;
1007
1008 if (typval_compare(tv, bp->dbg_val, type, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +02001009 && tv->vval.v_number == FALSE)
1010 {
1011 typval_T *v;
1012
1013 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +01001014 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001015 // Need to evaluate again, typval_compare() overwrites
1016 // "tv".
Bram Moolenaar0325d392021-09-09 12:34:19 +02001017 v = eval_expr_no_emsg(bp);
Bram Moolenaar34453202021-01-31 13:08:38 +01001018 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001019 free_tv(bp->dbg_val);
1020 bp->dbg_val = v;
1021 }
1022 free_tv(tv);
1023 }
1024 }
1025 else if (bp->dbg_val != NULL)
1026 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001027 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1028 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001029 free_tv(bp->dbg_val);
1030 bp->dbg_val = NULL;
1031 line = TRUE;
1032 }
1033
1034 if (line)
1035 {
1036 lnum = after > 0 ? after : 1;
1037 break;
1038 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001039 }
1040#endif
1041 }
1042 if (name != fname)
1043 vim_free(name);
1044
1045 return lnum;
1046}
1047
1048/*
1049 * Called when a breakpoint was encountered.
1050 */
1051 void
1052dbg_breakpoint(char_u *name, linenr_T lnum)
1053{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001054 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001055 debug_breakpoint_name = name;
1056 debug_breakpoint_lnum = lnum;
1057}
1058#endif