blob: 4a325505aef1b5abd26bd589241bc8354de3223d [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 Moolenaareead75c2019-04-21 11:35:00 +020090
Bram Moolenaar24959102022-05-07 20:01:16 +010091 State = MODE_NORMAL;
Bram Moolenaareead75c2019-04-21 11:35:00 +020092 debug_mode = TRUE;
93
94 if (!debug_did_msg)
95 msg(_("Entering Debug mode. Type \"cont\" to continue."));
96 if (debug_oldval != NULL)
97 {
98 smsg(_("Oldval = \"%s\""), debug_oldval);
99 vim_free(debug_oldval);
100 debug_oldval = NULL;
101 }
102 if (debug_newval != NULL)
103 {
104 smsg(_("Newval = \"%s\""), debug_newval);
105 vim_free(debug_newval);
106 debug_newval = NULL;
107 }
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200108 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100109 if (sname != NULL)
110 msg((char *)sname);
111 vim_free(sname);
112 if (SOURCING_LNUM != 0)
113 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200114 else
115 smsg(_("cmd: %s"), cmd);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200116
117 // Repeat getting a command and executing it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200118 for (;;)
119 {
120 msg_scroll = TRUE;
121 need_wait_return = FALSE;
122
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200123 // Save the current typeahead buffer and replace it with an empty one.
124 // This makes sure we get input from the user here and don't interfere
125 // with the commands being executed. Reset "ex_normal_busy" to avoid
126 // the side effects of using ":normal". Save the stuff buffer and make
127 // it empty. Set ignore_script to avoid reading from script input.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200128 save_ex_normal_busy = ex_normal_busy;
129 ex_normal_busy = 0;
130 if (!debug_greedy)
131 {
132 save_typeahead(&typeaheadbuf);
133 typeahead_saved = TRUE;
134 save_ignore_script = ignore_script;
135 ignore_script = TRUE;
136 }
137
Bram Moolenaar747f1102022-09-18 13:06:41 +0100138 // don't debug any function call, e.g. from an expression mapping
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100139 n = debug_break_level;
140 debug_break_level = -1;
141
Bram Moolenaareead75c2019-04-21 11:35:00 +0200142 vim_free(cmdline);
143 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL);
144
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100145 debug_break_level = n;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200146 if (typeahead_saved)
147 {
Bram Moolenaarc41badb2021-06-07 22:04:52 +0200148 restore_typeahead(&typeaheadbuf, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200149 ignore_script = save_ignore_script;
150 }
151 ex_normal_busy = save_ex_normal_busy;
152
153 cmdline_row = msg_row;
154 msg_starthere();
155 if (cmdline != NULL)
156 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200157 // If this is a debug command, set "last_cmd".
158 // If not, reset "last_cmd".
159 // For a blank line use previous command.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200160 p = skipwhite(cmdline);
161 if (*p != NUL)
162 {
163 switch (*p)
164 {
165 case 'c': last_cmd = CMD_CONT;
166 tail = "ont";
167 break;
168 case 'n': last_cmd = CMD_NEXT;
169 tail = "ext";
170 break;
171 case 's': last_cmd = CMD_STEP;
172 tail = "tep";
173 break;
174 case 'f':
175 last_cmd = 0;
176 if (p[1] == 'r')
177 {
178 last_cmd = CMD_FRAME;
179 tail = "rame";
180 }
181 else
182 {
183 last_cmd = CMD_FINISH;
184 tail = "inish";
185 }
186 break;
187 case 'q': last_cmd = CMD_QUIT;
188 tail = "uit";
189 break;
190 case 'i': last_cmd = CMD_INTERRUPT;
191 tail = "nterrupt";
192 break;
193 case 'b': last_cmd = CMD_BACKTRACE;
194 if (p[1] == 't')
195 tail = "t";
196 else
197 tail = "acktrace";
198 break;
199 case 'w': last_cmd = CMD_BACKTRACE;
200 tail = "here";
201 break;
202 case 'u': last_cmd = CMD_UP;
203 tail = "p";
204 break;
205 case 'd': last_cmd = CMD_DOWN;
206 tail = "own";
207 break;
208 default: last_cmd = 0;
209 }
210 if (last_cmd != 0)
211 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200212 // Check that the tail matches.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200213 ++p;
214 while (*p != NUL && *p == *tail)
215 {
216 ++p;
217 ++tail;
218 }
219 if (ASCII_ISALPHA(*p) && last_cmd != CMD_FRAME)
220 last_cmd = 0;
221 }
222 }
223
224 if (last_cmd != 0)
225 {
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200226 // Execute debug command: decide where to break next and
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200227 // return.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200228 switch (last_cmd)
229 {
230 case CMD_CONT:
231 debug_break_level = -1;
232 break;
233 case CMD_NEXT:
234 debug_break_level = ex_nesting_level;
235 break;
236 case CMD_STEP:
237 debug_break_level = 9999;
238 break;
239 case CMD_FINISH:
240 debug_break_level = ex_nesting_level - 1;
241 break;
242 case CMD_QUIT:
243 got_int = TRUE;
244 debug_break_level = -1;
245 break;
246 case CMD_INTERRUPT:
247 got_int = TRUE;
248 debug_break_level = 9999;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200249 // Do not repeat ">interrupt" cmd, continue stepping.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200250 last_cmd = CMD_STEP;
251 break;
252 case CMD_BACKTRACE:
253 do_showbacktrace(cmd);
254 continue;
255 case CMD_FRAME:
256 if (*p == NUL)
257 {
258 do_showbacktrace(cmd);
259 }
260 else
261 {
262 p = skipwhite(p);
263 do_setdebugtracelevel(p);
264 }
265 continue;
266 case CMD_UP:
267 debug_backtrace_level++;
268 do_checkbacktracelevel();
269 continue;
270 case CMD_DOWN:
271 debug_backtrace_level--;
272 do_checkbacktracelevel();
273 continue;
274 }
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200275 // Going out reset backtrace_level
Bram Moolenaareead75c2019-04-21 11:35:00 +0200276 debug_backtrace_level = 0;
277 break;
278 }
279
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200280 // don't debug this command
Bram Moolenaareead75c2019-04-21 11:35:00 +0200281 n = debug_break_level;
282 debug_break_level = -1;
283 (void)do_cmdline(cmdline, getexline, NULL,
284 DOCMD_VERBOSE|DOCMD_EXCRESET);
285 debug_break_level = n;
286 }
287 lines_left = Rows - 1;
288 }
289 vim_free(cmdline);
290
291 --RedrawingDisabled;
292 --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;
296 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
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100316 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200317 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100318 p = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200319 while ((q = strstr(p, "..")) != NULL)
320 {
321 p = q + 2;
322 maxbacktrace++;
323 }
324 }
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
487 if (debug_skipped)
488 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200489 // Save the value of got_int and reset it. We don't want a previous
490 // interruption cause flushing the input buffer.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200491 prev_got_int = got_int;
492 got_int = FALSE;
493 debug_breakpoint_name = debug_skipped_name;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200494 // eap->skip is TRUE
Bram Moolenaareead75c2019-04-21 11:35:00 +0200495 eap->skip = FALSE;
496 (void)dbg_check_breakpoint(eap);
497 eap->skip = TRUE;
498 got_int |= prev_got_int;
499 return TRUE;
500 }
501 return FALSE;
502}
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
683 if (dbg_parsearg(eap->arg, gap) == OK)
684 {
685 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)
692 {
693 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
694 vim_free(pat);
695 }
696 if (pat == NULL || bp->dbg_prog == NULL)
697 vim_free(bp->dbg_name);
698 else
699 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200700 if (bp->dbg_lnum == 0) // default line number is 1
Bram Moolenaareead75c2019-04-21 11:35:00 +0200701 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;
710 }
711 }
712 else
713 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200714 // DBG_EXPR
Bram Moolenaareead75c2019-04-21 11:35:00 +0200715 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp;
716 ++debug_tick;
Bram Moolenaar26a44842021-09-02 18:49:06 +0200717 if (gap == &dbg_breakp)
718 has_expr_breakpoint = TRUE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200719 }
720 }
721}
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
736update_has_expr_breakpoint()
737{
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
753debug_has_expr_breakpoint()
754{
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 Moolenaareb822a22021-12-31 15:09:27 +0000823 semsg(_(e_breakpoint_not_found_str), eap->arg);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200824 else
825 {
826 while (gap->ga_len > 0)
827 {
828 vim_free(DEBUGGY(gap, todel).dbg_name);
829#ifdef FEAT_EVAL
830 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR
831 && DEBUGGY(gap, todel).dbg_val != NULL)
832 free_tv(DEBUGGY(gap, todel).dbg_val);
833#endif
834 vim_regfree(DEBUGGY(gap, todel).dbg_prog);
835 --gap->ga_len;
836 if (todel < gap->ga_len)
837 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
838 (gap->ga_len - todel) * sizeof(struct debuggy));
839#ifdef FEAT_PROFILE
840 if (eap->cmdidx == CMD_breakdel)
841#endif
842 ++debug_tick;
843 if (!del_all)
844 break;
845 }
846
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200847 // If all breakpoints were removed clear the array.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200848 if (gap->ga_len == 0)
849 ga_clear(gap);
Bram Moolenaar26a44842021-09-02 18:49:06 +0200850 if (gap == &dbg_breakp)
851 update_has_expr_breakpoint();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200852 }
853}
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)
865 msg(_("No breakpoints defined"));
866 else
867 for (i = 0; i < dbg_breakp.ga_len; ++i)
868 {
869 bp = &BREAKP(i);
870 if (bp->dbg_type == DBG_FILE)
871 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
872 if (bp->dbg_type != DBG_EXPR)
873 smsg(_("%3d %s %s line %ld"),
874 bp->dbg_nr,
875 bp->dbg_type == DBG_FUNC ? "func" : "file",
876 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
877 (long)bp->dbg_lnum);
878 else
879 smsg(_("%3d expr %s"),
880 bp->dbg_nr, bp->dbg_name);
881 }
882}
883
884/*
885 * Find a breakpoint for a function or sourced file.
886 * Returns line number at which to break; zero when no matching breakpoint.
887 */
888 linenr_T
889dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200890 int file, // TRUE for a file, FALSE for a function
891 char_u *fname, // file or function name
892 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200893{
894 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
895}
896
897#if defined(FEAT_PROFILE) || defined(PROTO)
898/*
899 * Return TRUE if profiling is on for a function or sourced file.
900 */
901 int
902has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200903 int file, // TRUE for a file, FALSE for a function
904 char_u *fname, // file or function name
905 int *fp) // return: forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200906{
907 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
908 != (linenr_T)0);
909}
910#endif
911
912/*
913 * Common code for dbg_find_breakpoint() and has_profiling().
914 */
915 static linenr_T
916debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +0100917 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200918 char_u *fname, // file or function name
919 linenr_T after, // after this line number
920 garray_T *gap, // either &dbg_breakp or &prof_ga
921 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +0200922{
923 struct debuggy *bp;
924 int i;
925 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100926 char_u *name = NULL;
927 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200928 int prev_got_int;
929
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200930 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200931 if (gap->ga_len == 0)
932 return (linenr_T)0;
933
Bram Moolenaarb2049902021-01-24 12:53:53 +0100934 // For a script-local function remove the prefix, so that
935 // "profile func Func" matches "Func" in any script. Otherwise it's very
936 // difficult to profile/debug a script-local function. It may match a
937 // function in the wrong script, but that is much better than not being
938 // able to profile/debug a function in a script with unknown ID.
939 // Also match a script-specific name.
940 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200941 {
Bram Moolenaarb2049902021-01-24 12:53:53 +0100942 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200943 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100944 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200945 {
946 STRCPY(name, "<SNR>");
947 STRCPY(name + 5, fname + 3);
948 }
949 }
950
951 for (i = 0; i < gap->ga_len; ++i)
952 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200953 // Skip entries that are not useful or are for a line that is beyond
954 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200955 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100956 if (((bp->dbg_type == DBG_FILE) == is_file
957 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +0200958#ifdef FEAT_PROFILE
959 gap == &prof_ga ||
960#endif
961 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
962 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200963 // Save the value of got_int and reset it. We don't want a
964 // previous interruption cancel matching, only hitting CTRL-C
965 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200966 prev_got_int = got_int;
967 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100968 if ((name != NULL
969 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
970 || vim_regexec_prog(&bp->dbg_prog, FALSE,
971 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +0200972 {
973 lnum = bp->dbg_lnum;
974 if (fp != NULL)
975 *fp = bp->dbg_forceit;
976 }
977 got_int |= prev_got_int;
978 }
979#ifdef FEAT_EVAL
980 else if (bp->dbg_type == DBG_EXPR)
981 {
982 typval_T *tv;
983 int line = FALSE;
984
Bram Moolenaar0325d392021-09-09 12:34:19 +0200985 tv = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200986 if (tv != NULL)
987 {
988 if (bp->dbg_val == NULL)
989 {
Bram Moolenaar34453202021-01-31 13:08:38 +0100990 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200991 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +0100992 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200993 line = TRUE;
994 }
995 else
996 {
Bram Moolenaarcf666202022-03-10 13:29:20 +0000997 // Use "==" instead of "is" for strings, that is what we
998 // always have done.
999 exprtype_T type = tv->v_type == VAR_STRING
1000 ? EXPR_EQUAL : EXPR_IS;
1001
1002 if (typval_compare(tv, bp->dbg_val, type, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +02001003 && tv->vval.v_number == FALSE)
1004 {
1005 typval_T *v;
1006
1007 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +01001008 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001009 // Need to evaluate again, typval_compare() overwrites
1010 // "tv".
Bram Moolenaar0325d392021-09-09 12:34:19 +02001011 v = eval_expr_no_emsg(bp);
Bram Moolenaar34453202021-01-31 13:08:38 +01001012 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001013 free_tv(bp->dbg_val);
1014 bp->dbg_val = v;
1015 }
1016 free_tv(tv);
1017 }
1018 }
1019 else if (bp->dbg_val != NULL)
1020 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001021 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1022 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001023 free_tv(bp->dbg_val);
1024 bp->dbg_val = NULL;
1025 line = TRUE;
1026 }
1027
1028 if (line)
1029 {
1030 lnum = after > 0 ? after : 1;
1031 break;
1032 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001033 }
1034#endif
1035 }
1036 if (name != fname)
1037 vim_free(name);
1038
1039 return lnum;
1040}
1041
1042/*
1043 * Called when a breakpoint was encountered.
1044 */
1045 void
1046dbg_breakpoint(char_u *name, linenr_T lnum)
1047{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001048 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001049 debug_breakpoint_name = name;
1050 debug_breakpoint_lnum = lnum;
1051}
1052#endif