blob: cb0b86f76d2d1186d0204998c15f33ffa15df343 [file] [log] [blame]
Bram Moolenaareead75c2019-04-21 11:35:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * debugger.c: Vim script debugger functions
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020017static int debug_greedy = FALSE; // batch mode debugging: don't save
18 // and restore typeahead.
Bram Moolenaareead75c2019-04-21 11:35:00 +020019static void do_setdebugtracelevel(char_u *arg);
20static void do_checkbacktracelevel(void);
21static void do_showbacktrace(char_u *cmd);
22
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020023static char_u *debug_oldval = NULL; // old and newval for debug expressions
Bram Moolenaareead75c2019-04-21 11:35:00 +020024static char_u *debug_newval = NULL;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010025static int debug_expr = 0; // use debug_expr
Bram Moolenaareead75c2019-04-21 11:35:00 +020026
27 int
28has_watchexpr(void)
29{
30 return debug_expr;
31}
32
33/*
34 * do_debug(): Debug mode.
35 * Repeatedly get Ex commands, until told to continue normal execution.
36 */
37 void
38do_debug(char_u *cmd)
39{
40 int save_msg_scroll = msg_scroll;
41 int save_State = State;
42 int save_did_emsg = did_emsg;
43 int save_cmd_silent = cmd_silent;
44 int save_msg_silent = msg_silent;
45 int save_emsg_silent = emsg_silent;
46 int save_redir_off = redir_off;
47 tasave_T typeaheadbuf;
48 int typeahead_saved = FALSE;
49 int save_ignore_script = 0;
50 int save_ex_normal_busy;
51 int n;
52 char_u *cmdline = NULL;
53 char_u *p;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +010054 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +020055 char *tail = NULL;
56 static int last_cmd = 0;
57#define CMD_CONT 1
58#define CMD_NEXT 2
59#define CMD_STEP 3
60#define CMD_FINISH 4
61#define CMD_QUIT 5
62#define CMD_INTERRUPT 6
63#define CMD_BACKTRACE 7
64#define CMD_FRAME 8
65#define CMD_UP 9
66#define CMD_DOWN 10
67
68#ifdef ALWAYS_USE_GUI
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020069 // Can't do this when there is no terminal for input/output.
Bram Moolenaareead75c2019-04-21 11:35:00 +020070 if (!gui.in_use)
71 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020072 // Break as soon as possible.
Bram Moolenaareead75c2019-04-21 11:35:00 +020073 debug_break_level = 9999;
74 return;
75 }
76#endif
77
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020078 // Make sure we are in raw mode and start termcap mode. Might have side
79 // effects...
Bram Moolenaareead75c2019-04-21 11:35:00 +020080 settmode(TMODE_RAW);
81 starttermcap();
82
Bram Moolenaar31fc39e2019-04-23 18:39:49 +020083 ++RedrawingDisabled; // don't redisplay the window
84 ++no_wait_return; // don't wait for return
85 did_emsg = FALSE; // don't use error from debugged stuff
86 cmd_silent = FALSE; // display commands
87 msg_silent = FALSE; // display messages
88 emsg_silent = FALSE; // display error messages
89 redir_off = TRUE; // don't redirect debug commands
Bram Moolenaar9781d9c2022-09-20 13:51:25 +010090 save_timeout_for_debugging(); // disable regexp timeout flag
Bram Moolenaareead75c2019-04-21 11:35:00 +020091
Bram Moolenaar24959102022-05-07 20:01:16 +010092 State = MODE_NORMAL;
Bram Moolenaareead75c2019-04-21 11:35:00 +020093 debug_mode = TRUE;
94
95 if (!debug_did_msg)
96 msg(_("Entering Debug mode. Type \"cont\" to continue."));
97 if (debug_oldval != NULL)
98 {
99 smsg(_("Oldval = \"%s\""), debug_oldval);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +0000100 VIM_CLEAR(debug_oldval);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200101 }
102 if (debug_newval != NULL)
103 {
104 smsg(_("Newval = \"%s\""), debug_newval);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +0000105 VIM_CLEAR(debug_newval);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200106 }
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200107 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100108 if (sname != NULL)
109 msg((char *)sname);
110 vim_free(sname);
111 if (SOURCING_LNUM != 0)
112 smsg(_("line %ld: %s"), SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200113 else
114 smsg(_("cmd: %s"), cmd);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200115
116 // Repeat getting a command and executing it.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200117 for (;;)
118 {
119 msg_scroll = TRUE;
120 need_wait_return = FALSE;
121
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200122 // Save the current typeahead buffer and replace it with an empty one.
123 // This makes sure we get input from the user here and don't interfere
124 // with the commands being executed. Reset "ex_normal_busy" to avoid
125 // the side effects of using ":normal". Save the stuff buffer and make
126 // it empty. Set ignore_script to avoid reading from script input.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200127 save_ex_normal_busy = ex_normal_busy;
128 ex_normal_busy = 0;
129 if (!debug_greedy)
130 {
131 save_typeahead(&typeaheadbuf);
132 typeahead_saved = TRUE;
133 save_ignore_script = ignore_script;
134 ignore_script = TRUE;
135 }
136
Bram Moolenaar747f1102022-09-18 13:06:41 +0100137 // don't debug any function call, e.g. from an expression mapping
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100138 n = debug_break_level;
139 debug_break_level = -1;
140
Bram Moolenaareead75c2019-04-21 11:35:00 +0200141 vim_free(cmdline);
142 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL);
143
Bram Moolenaarb1842de2022-09-13 12:36:57 +0100144 debug_break_level = n;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200145 if (typeahead_saved)
146 {
Bram Moolenaarc41badb2021-06-07 22:04:52 +0200147 restore_typeahead(&typeaheadbuf, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200148 ignore_script = save_ignore_script;
149 }
150 ex_normal_busy = save_ex_normal_busy;
151
152 cmdline_row = msg_row;
153 msg_starthere();
154 if (cmdline != NULL)
155 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200156 // If this is a debug command, set "last_cmd".
157 // If not, reset "last_cmd".
158 // For a blank line use previous command.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200159 p = skipwhite(cmdline);
160 if (*p != NUL)
161 {
162 switch (*p)
163 {
164 case 'c': last_cmd = CMD_CONT;
165 tail = "ont";
166 break;
167 case 'n': last_cmd = CMD_NEXT;
168 tail = "ext";
169 break;
170 case 's': last_cmd = CMD_STEP;
171 tail = "tep";
172 break;
173 case 'f':
174 last_cmd = 0;
175 if (p[1] == 'r')
176 {
177 last_cmd = CMD_FRAME;
178 tail = "rame";
179 }
180 else
181 {
182 last_cmd = CMD_FINISH;
183 tail = "inish";
184 }
185 break;
186 case 'q': last_cmd = CMD_QUIT;
187 tail = "uit";
188 break;
189 case 'i': last_cmd = CMD_INTERRUPT;
190 tail = "nterrupt";
191 break;
192 case 'b': last_cmd = CMD_BACKTRACE;
193 if (p[1] == 't')
194 tail = "t";
195 else
196 tail = "acktrace";
197 break;
198 case 'w': last_cmd = CMD_BACKTRACE;
199 tail = "here";
200 break;
201 case 'u': last_cmd = CMD_UP;
202 tail = "p";
203 break;
204 case 'd': last_cmd = CMD_DOWN;
205 tail = "own";
206 break;
207 default: last_cmd = 0;
208 }
209 if (last_cmd != 0)
210 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200211 // Check that the tail matches.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200212 ++p;
213 while (*p != NUL && *p == *tail)
214 {
215 ++p;
216 ++tail;
217 }
218 if (ASCII_ISALPHA(*p) && last_cmd != CMD_FRAME)
219 last_cmd = 0;
220 }
221 }
222
223 if (last_cmd != 0)
224 {
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200225 // Execute debug command: decide where to break next and
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200226 // return.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200227 switch (last_cmd)
228 {
229 case CMD_CONT:
230 debug_break_level = -1;
231 break;
232 case CMD_NEXT:
233 debug_break_level = ex_nesting_level;
234 break;
235 case CMD_STEP:
236 debug_break_level = 9999;
237 break;
238 case CMD_FINISH:
239 debug_break_level = ex_nesting_level - 1;
240 break;
241 case CMD_QUIT:
242 got_int = TRUE;
243 debug_break_level = -1;
244 break;
245 case CMD_INTERRUPT:
246 got_int = TRUE;
247 debug_break_level = 9999;
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200248 // Do not repeat ">interrupt" cmd, continue stepping.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200249 last_cmd = CMD_STEP;
250 break;
251 case CMD_BACKTRACE:
252 do_showbacktrace(cmd);
253 continue;
254 case CMD_FRAME:
255 if (*p == NUL)
256 {
257 do_showbacktrace(cmd);
258 }
259 else
260 {
261 p = skipwhite(p);
262 do_setdebugtracelevel(p);
263 }
264 continue;
265 case CMD_UP:
266 debug_backtrace_level++;
267 do_checkbacktracelevel();
268 continue;
269 case CMD_DOWN:
270 debug_backtrace_level--;
271 do_checkbacktracelevel();
272 continue;
273 }
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200274 // Going out reset backtrace_level
Bram Moolenaareead75c2019-04-21 11:35:00 +0200275 debug_backtrace_level = 0;
276 break;
277 }
278
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200279 // don't debug this command
Bram Moolenaareead75c2019-04-21 11:35:00 +0200280 n = debug_break_level;
281 debug_break_level = -1;
282 (void)do_cmdline(cmdline, getexline, NULL,
283 DOCMD_VERBOSE|DOCMD_EXCRESET);
284 debug_break_level = n;
285 }
286 lines_left = Rows - 1;
287 }
288 vim_free(cmdline);
289
Bram Moolenaar79cdf022023-05-20 14:07:00 +0100290 if (RedrawingDisabled > 0)
291 --RedrawingDisabled;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200292 --no_wait_return;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100293 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200294 need_wait_return = FALSE;
295 msg_scroll = save_msg_scroll;
Bram Moolenaar9781d9c2022-09-20 13:51:25 +0100296 restore_timeout_for_debugging();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200297 lines_left = Rows - 1;
298 State = save_State;
299 debug_mode = FALSE;
300 did_emsg = save_did_emsg;
301 cmd_silent = save_cmd_silent;
302 msg_silent = save_msg_silent;
303 emsg_silent = save_emsg_silent;
304 redir_off = save_redir_off;
305
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200306 // Only print the message again when typing a command before coming back
307 // here.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200308 debug_did_msg = TRUE;
309}
310
311 static int
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100312get_maxbacktrace_level(char_u *sname)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200313{
314 char *p, *q;
315 int maxbacktrace = 0;
316
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000317 if (sname == NULL)
318 return 0;
319
320 p = (char *)sname;
321 while ((q = strstr(p, "..")) != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200322 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000323 p = q + 2;
324 maxbacktrace++;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200325 }
326 return maxbacktrace;
327}
328
329 static void
330do_setdebugtracelevel(char_u *arg)
331{
332 int level;
333
334 level = atoi((char *)arg);
335 if (*arg == '+' || level < 0)
336 debug_backtrace_level += level;
337 else
338 debug_backtrace_level = level;
339
340 do_checkbacktracelevel();
341}
342
343 static void
344do_checkbacktracelevel(void)
345{
346 if (debug_backtrace_level < 0)
347 {
348 debug_backtrace_level = 0;
349 msg(_("frame is zero"));
350 }
351 else
352 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200353 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100354 int max = get_maxbacktrace_level(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200355
356 if (debug_backtrace_level > max)
357 {
358 debug_backtrace_level = max;
359 smsg(_("frame at highest level: %d"), max);
360 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100361 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200362 }
363}
364
365 static void
366do_showbacktrace(char_u *cmd)
367{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100368 char_u *sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200369 char *cur;
370 char *next;
371 int i = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100372 int max;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200373
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200374 sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100375 max = get_maxbacktrace_level(sname);
376 if (sname != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200377 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100378 cur = (char *)sname;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200379 while (!got_int)
380 {
381 next = strstr(cur, "..");
382 if (next != NULL)
383 *next = NUL;
384 if (i == max - debug_backtrace_level)
385 smsg("->%d %s", max - i, cur);
386 else
387 smsg(" %d %s", max - i, cur);
388 ++i;
389 if (next == NULL)
390 break;
391 *next = '.';
392 cur = next + 2;
393 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100394 vim_free(sname);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200395 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100396
397 if (SOURCING_LNUM != 0)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200398 smsg(_("line %ld: %s"), (long)SOURCING_LNUM, cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200399 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200400 smsg(_("cmd: %s"), cmd);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200401}
402
403/*
404 * ":debug".
405 */
406 void
407ex_debug(exarg_T *eap)
408{
409 int debug_break_level_save = debug_break_level;
410
411 debug_break_level = 9999;
412 do_cmdline_cmd(eap->arg);
413 debug_break_level = debug_break_level_save;
414}
415
416static char_u *debug_breakpoint_name = NULL;
417static linenr_T debug_breakpoint_lnum;
418
419/*
420 * When debugging or a breakpoint is set on a skipped command, no debug prompt
421 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
422 * debug_skipped_name is then set to the source name in the breakpoint case. If
423 * a skipped command decides itself that a debug prompt should be displayed, it
424 * can do so by calling dbg_check_skipped().
425 */
426static int debug_skipped;
427static char_u *debug_skipped_name;
428
429/*
430 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
431 * at or below the break level. But only when the line is actually
432 * executed. Return TRUE and set breakpoint_name for skipped commands that
433 * decide to execute something themselves.
434 * Called from do_one_cmd() before executing a command.
435 */
436 void
437dbg_check_breakpoint(exarg_T *eap)
438{
439 char_u *p;
440
441 debug_skipped = FALSE;
442 if (debug_breakpoint_name != NULL)
443 {
444 if (!eap->skip)
445 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200446 // replace K_SNR with "<SNR>"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200447 if (debug_breakpoint_name[0] == K_SPECIAL
448 && debug_breakpoint_name[1] == KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100449 && debug_breakpoint_name[2] == KE_SNR)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200450 p = (char_u *)"<SNR>";
451 else
452 p = (char_u *)"";
453 smsg(_("Breakpoint in \"%s%s\" line %ld"),
454 p,
455 debug_breakpoint_name + (*p == NUL ? 0 : 3),
456 (long)debug_breakpoint_lnum);
457 debug_breakpoint_name = NULL;
458 do_debug(eap->cmd);
459 }
460 else
461 {
462 debug_skipped = TRUE;
463 debug_skipped_name = debug_breakpoint_name;
464 debug_breakpoint_name = NULL;
465 }
466 }
467 else if (ex_nesting_level <= debug_break_level)
468 {
469 if (!eap->skip)
470 do_debug(eap->cmd);
471 else
472 {
473 debug_skipped = TRUE;
474 debug_skipped_name = NULL;
475 }
476 }
477}
478
479/*
480 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
481 * set. Return TRUE when the debug mode is entered this time.
482 */
483 int
484dbg_check_skipped(exarg_T *eap)
485{
486 int prev_got_int;
487
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000488 if (!debug_skipped)
489 return FALSE;
490
491 // Save the value of got_int and reset it. We don't want a previous
492 // interruption cause flushing the input buffer.
493 prev_got_int = got_int;
494 got_int = FALSE;
495 debug_breakpoint_name = debug_skipped_name;
496 // eap->skip is TRUE
497 eap->skip = FALSE;
498 (void)dbg_check_breakpoint(eap);
499 eap->skip = TRUE;
500 got_int |= prev_got_int;
501 return TRUE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200502}
503
504/*
505 * The list of breakpoints: dbg_breakp.
506 * This is a grow-array of structs.
507 */
508struct debuggy
509{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200510 int dbg_nr; // breakpoint number
511 int dbg_type; // DBG_FUNC, DBG_FILE or DBG_EXPR
512 char_u *dbg_name; // function, expression or file name
513 regprog_T *dbg_prog; // regexp program
514 linenr_T dbg_lnum; // line number in function or file
515 int dbg_forceit; // ! used
Bram Moolenaareead75c2019-04-21 11:35:00 +0200516#ifdef FEAT_EVAL
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200517 typval_T *dbg_val; // last result of watchexpression
Bram Moolenaareead75c2019-04-21 11:35:00 +0200518#endif
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200519 int dbg_level; // stored nested level for expr
Bram Moolenaareead75c2019-04-21 11:35:00 +0200520};
521
522static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL};
523#define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
524#define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200525static int last_breakp = 0; // nr of last defined breakpoint
Bram Moolenaar26a44842021-09-02 18:49:06 +0200526static int has_expr_breakpoint = FALSE;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200527
528#ifdef FEAT_PROFILE
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200529// Profiling uses file and func names similar to breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200530static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL};
Ernie Rael21d32122023-09-02 15:09:18 +0200531
532// Profiling caches results of regexp lookups for function/script name.
533#define N_PROF_HTAB 2
534static hashtab_T prof_cache[N_PROF_HTAB];
535#define PROF_HTAB_FUNCS 0
536#define PROF_HTAB_FILES 1
537static int prof_cache_initialized;
538typedef struct profentry_S
539{
540 char pen_flags; // cache data booleans: profiling, forceit
541 char_u pen_name[1]; // actually longer
542} profentry_T;
543#define PEN_FLAG_PROFILING 1
544#define PEN_FLAG_FORCEIT 2
545#define PEN_SET_PROFILING(pe) ((pe)->pen_flags |= PEN_FLAG_PROFILING)
546#define PEN_SET_FORCEIT(pe) ((pe)->pen_flags |= PEN_FLAG_FORCEIT)
547#define PEN_IS_PROFILING(pe) (((pe)->pen_flags & PEN_FLAG_PROFILING) != 0)
548#define PEN_IS_FORCEIT(pe) (((pe)->pen_flags & PEN_FLAG_FORCEIT) != 0)
549
550#define PE2HIKEY(pe) ((pe)->pen_name)
551#define HIKEY2PE(p) ((profentry_T *)((p) - (offsetof(profentry_T, pen_name))))
552#define HI2PE(hi) HIKEY2PE((hi)->hi_key)
553
554static void prof_clear_cache(void);
555#define PROF_CLEAR_CACHE(gap) do {if ((gap) == &prof_ga) prof_clear_cache();} while (0)
556// Can enable to get some info about profile caching
557// #define PROF_CACHE_LOG
558#else
559#define PROF_CLEAR_CACHE(gap) do {} while (0)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200560#endif
561#define DBG_FUNC 1
562#define DBG_FILE 2
563#define DBG_EXPR 3
564
565static linenr_T debuggy_find(int file,char_u *fname, linenr_T after, garray_T *gap, int *fp);
566
567/*
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200568 * Evaluate the "bp->dbg_name" expression and return the result.
Bram Moolenaar0325d392021-09-09 12:34:19 +0200569 * Disables error messages.
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200570 */
571 static typval_T *
Bram Moolenaar0325d392021-09-09 12:34:19 +0200572eval_expr_no_emsg(struct debuggy *bp)
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200573{
574 typval_T *tv;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200575
Bram Moolenaar0325d392021-09-09 12:34:19 +0200576 // Disable error messages, a bad expression would make Vim unusable.
577 ++emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200578 tv = eval_expr(bp->dbg_name, NULL);
Bram Moolenaar0325d392021-09-09 12:34:19 +0200579 --emsg_off;
Bram Moolenaar072f1c62021-09-08 20:40:34 +0200580
581 return tv;
582}
583
584/*
Bram Moolenaareead75c2019-04-21 11:35:00 +0200585 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
586 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
587 * is allocated.
588 * Returns FAIL for failure.
589 */
590 static int
591dbg_parsearg(
592 char_u *arg,
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200593 garray_T *gap) // either &dbg_breakp or &prof_ga
Bram Moolenaareead75c2019-04-21 11:35:00 +0200594{
595 char_u *p = arg;
596 char_u *q;
597 struct debuggy *bp;
598 int here = FALSE;
599
600 if (ga_grow(gap, 1) == FAIL)
601 return FAIL;
602 bp = &DEBUGGY(gap, gap->ga_len);
603
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200604 // Find "func" or "file".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200605 if (STRNCMP(p, "func", 4) == 0)
606 bp->dbg_type = DBG_FUNC;
607 else if (STRNCMP(p, "file", 4) == 0)
608 bp->dbg_type = DBG_FILE;
609 else if (
610#ifdef FEAT_PROFILE
611 gap != &prof_ga &&
612#endif
613 STRNCMP(p, "here", 4) == 0)
614 {
615 if (curbuf->b_ffname == NULL)
616 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200617 emsg(_(e_no_file_name));
Bram Moolenaareead75c2019-04-21 11:35:00 +0200618 return FAIL;
619 }
620 bp->dbg_type = DBG_FILE;
621 here = TRUE;
622 }
623 else if (
624#ifdef FEAT_PROFILE
625 gap != &prof_ga &&
626#endif
627 STRNCMP(p, "expr", 4) == 0)
628 bp->dbg_type = DBG_EXPR;
629 else
630 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000631 semsg(_(e_invalid_argument_str), p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200632 return FAIL;
633 }
634 p = skipwhite(p + 4);
635
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200636 // Find optional line number.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200637 if (here)
638 bp->dbg_lnum = curwin->w_cursor.lnum;
639 else if (
640#ifdef FEAT_PROFILE
641 gap != &prof_ga &&
642#endif
643 VIM_ISDIGIT(*p))
644 {
645 bp->dbg_lnum = getdigits(&p);
646 p = skipwhite(p);
647 }
648 else
649 bp->dbg_lnum = 0;
650
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200651 // Find the function or file name. Don't accept a function name with ().
Bram Moolenaareead75c2019-04-21 11:35:00 +0200652 if ((!here && *p == NUL)
653 || (here && *p != NUL)
654 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
655 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000656 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200657 return FAIL;
658 }
659
660 if (bp->dbg_type == DBG_FUNC)
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200661 bp->dbg_name = vim_strsave(STRNCMP(p, "g:", 2) == 0 ? p + 2 : p);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200662 else if (here)
663 bp->dbg_name = vim_strsave(curbuf->b_ffname);
664 else if (bp->dbg_type == DBG_EXPR)
665 {
666 bp->dbg_name = vim_strsave(p);
667 if (bp->dbg_name != NULL)
Bram Moolenaar0325d392021-09-09 12:34:19 +0200668 bp->dbg_val = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200669 }
670 else
671 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200672 // Expand the file name in the same way as do_source(). This means
673 // doing it twice, so that $DIR/file gets expanded when $DIR is
674 // "~/dir".
Bram Moolenaareead75c2019-04-21 11:35:00 +0200675 q = expand_env_save(p);
676 if (q == NULL)
677 return FAIL;
678 p = expand_env_save(q);
679 vim_free(q);
680 if (p == NULL)
681 return FAIL;
682 if (*p != '*')
683 {
684 bp->dbg_name = fix_fname(p);
685 vim_free(p);
686 }
687 else
688 bp->dbg_name = p;
689 }
690
691 if (bp->dbg_name == NULL)
692 return FAIL;
693 return OK;
694}
695
696/*
697 * ":breakadd". Also used for ":profile".
698 */
699 void
700ex_breakadd(exarg_T *eap)
701{
702 struct debuggy *bp;
703 char_u *pat;
704 garray_T *gap;
705
706 gap = &dbg_breakp;
707#ifdef FEAT_PROFILE
708 if (eap->cmdidx == CMD_profile)
709 gap = &prof_ga;
710#endif
711
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000712 if (dbg_parsearg(eap->arg, gap) != OK)
713 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200714
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000715 bp = &DEBUGGY(gap, gap->ga_len);
716 bp->dbg_forceit = eap->forceit;
717
718 if (bp->dbg_type != DBG_EXPR)
719 {
720 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
721 if (pat != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200722 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000723 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
724 vim_free(pat);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200725 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000726 if (pat == NULL || bp->dbg_prog == NULL)
727 vim_free(bp->dbg_name);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200728 else
729 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000730 if (bp->dbg_lnum == 0) // default line number is 1
731 bp->dbg_lnum = 1;
732#ifdef FEAT_PROFILE
733 if (eap->cmdidx != CMD_profile)
734#endif
735 {
736 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
737 ++debug_tick;
738 }
739 ++gap->ga_len;
Ernie Rael21d32122023-09-02 15:09:18 +0200740 PROF_CLEAR_CACHE(gap);
Bram Moolenaareead75c2019-04-21 11:35:00 +0200741 }
742 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000743 else
744 {
745 // DBG_EXPR
746 DEBUGGY(gap, gap->ga_len++).dbg_nr = ++last_breakp;
747 ++debug_tick;
748 if (gap == &dbg_breakp)
749 has_expr_breakpoint = TRUE;
750 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200751}
752
753/*
754 * ":debuggreedy".
755 */
756 void
757ex_debuggreedy(exarg_T *eap)
758{
759 if (eap->addr_count == 0 || eap->line2 != 0)
760 debug_greedy = TRUE;
761 else
762 debug_greedy = FALSE;
763}
764
Bram Moolenaar26a44842021-09-02 18:49:06 +0200765 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000766update_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200767{
768 int i;
769
770 has_expr_breakpoint = FALSE;
771 for (i = 0; i < dbg_breakp.ga_len; ++i)
772 if (BREAKP(i).dbg_type == DBG_EXPR)
773 {
774 has_expr_breakpoint = TRUE;
775 break;
776 }
777}
778
779/*
780 * Return TRUE if there is any expression breakpoint.
781 */
782 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000783debug_has_expr_breakpoint(void)
Bram Moolenaar26a44842021-09-02 18:49:06 +0200784{
785 return has_expr_breakpoint;
786}
787
Bram Moolenaareead75c2019-04-21 11:35:00 +0200788/*
789 * ":breakdel" and ":profdel".
790 */
791 void
792ex_breakdel(exarg_T *eap)
793{
794 struct debuggy *bp, *bpi;
795 int nr;
796 int todel = -1;
797 int del_all = FALSE;
798 int i;
799 linenr_T best_lnum = 0;
800 garray_T *gap;
801
802 gap = &dbg_breakp;
803 if (eap->cmdidx == CMD_profdel)
804 {
805#ifdef FEAT_PROFILE
806 gap = &prof_ga;
807#else
808 ex_ni(eap);
809 return;
810#endif
811 }
812
813 if (vim_isdigit(*eap->arg))
814 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200815 // ":breakdel {nr}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200816 nr = atol((char *)eap->arg);
817 for (i = 0; i < gap->ga_len; ++i)
818 if (DEBUGGY(gap, i).dbg_nr == nr)
819 {
820 todel = i;
821 break;
822 }
823 }
824 else if (*eap->arg == '*')
825 {
826 todel = 0;
827 del_all = TRUE;
828 }
829 else
830 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200831 // ":breakdel {func|file|expr} [lnum] {name}"
Bram Moolenaareead75c2019-04-21 11:35:00 +0200832 if (dbg_parsearg(eap->arg, gap) == FAIL)
833 return;
834 bp = &DEBUGGY(gap, gap->ga_len);
835 for (i = 0; i < gap->ga_len; ++i)
836 {
837 bpi = &DEBUGGY(gap, i);
838 if (bp->dbg_type == bpi->dbg_type
839 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
840 && (bp->dbg_lnum == bpi->dbg_lnum
841 || (bp->dbg_lnum == 0
842 && (best_lnum == 0
843 || bpi->dbg_lnum < best_lnum))))
844 {
845 todel = i;
846 best_lnum = bpi->dbg_lnum;
847 }
848 }
849 vim_free(bp->dbg_name);
850 }
851
852 if (todel < 0)
Bram Moolenaareead75c2019-04-21 11:35:00 +0200853 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000854 semsg(_(e_breakpoint_not_found_str), eap->arg);
855 return;
Bram Moolenaareead75c2019-04-21 11:35:00 +0200856 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000857
858 while (gap->ga_len > 0)
859 {
860 vim_free(DEBUGGY(gap, todel).dbg_name);
861#ifdef FEAT_EVAL
862 if (DEBUGGY(gap, todel).dbg_type == DBG_EXPR
863 && DEBUGGY(gap, todel).dbg_val != NULL)
864 free_tv(DEBUGGY(gap, todel).dbg_val);
865#endif
866 vim_regfree(DEBUGGY(gap, todel).dbg_prog);
867 --gap->ga_len;
868 if (todel < gap->ga_len)
869 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
870 (gap->ga_len - todel) * sizeof(struct debuggy));
871#ifdef FEAT_PROFILE
872 if (eap->cmdidx == CMD_breakdel)
873#endif
874 ++debug_tick;
875 if (!del_all)
876 break;
877 }
Ernie Rael21d32122023-09-02 15:09:18 +0200878 PROF_CLEAR_CACHE(gap);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000879
880 // If all breakpoints were removed clear the array.
881 if (gap->ga_len == 0)
882 ga_clear(gap);
883 if (gap == &dbg_breakp)
884 update_has_expr_breakpoint();
Bram Moolenaareead75c2019-04-21 11:35:00 +0200885}
886
887/*
888 * ":breaklist".
889 */
890 void
891ex_breaklist(exarg_T *eap UNUSED)
892{
893 struct debuggy *bp;
894 int i;
895
896 if (dbg_breakp.ga_len == 0)
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000897 {
Bram Moolenaareead75c2019-04-21 11:35:00 +0200898 msg(_("No breakpoints defined"));
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000899 return;
900 }
901
902 for (i = 0; i < dbg_breakp.ga_len; ++i)
903 {
904 bp = &BREAKP(i);
905 if (bp->dbg_type == DBG_FILE)
906 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE);
907 if (bp->dbg_type != DBG_EXPR)
908 smsg(_("%3d %s %s line %ld"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200909 bp->dbg_nr,
910 bp->dbg_type == DBG_FUNC ? "func" : "file",
911 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff,
912 (long)bp->dbg_lnum);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000913 else
914 smsg(_("%3d expr %s"),
Bram Moolenaareead75c2019-04-21 11:35:00 +0200915 bp->dbg_nr, bp->dbg_name);
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000916 }
Bram Moolenaareead75c2019-04-21 11:35:00 +0200917}
918
919/*
920 * Find a breakpoint for a function or sourced file.
921 * Returns line number at which to break; zero when no matching breakpoint.
922 */
923 linenr_T
924dbg_find_breakpoint(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200925 int file, // TRUE for a file, FALSE for a function
926 char_u *fname, // file or function name
927 linenr_T after) // after this line number
Bram Moolenaareead75c2019-04-21 11:35:00 +0200928{
929 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
930}
931
932#if defined(FEAT_PROFILE) || defined(PROTO)
Ernie Rael21d32122023-09-02 15:09:18 +0200933#if defined(PROF_CACHE_LOG)
934static int count_lookups[2];
935#endif
Bram Moolenaareead75c2019-04-21 11:35:00 +0200936/*
937 * Return TRUE if profiling is on for a function or sourced file.
Ernie Rael21d32122023-09-02 15:09:18 +0200938 * Cache the results of debuggy_find().
939 * Cache is cleared whenever prof_ga.ga_len is changed.
Bram Moolenaareead75c2019-04-21 11:35:00 +0200940 */
941 int
942has_profiling(
Bram Moolenaar31fc39e2019-04-23 18:39:49 +0200943 int file, // TRUE for a file, FALSE for a function
944 char_u *fname, // file or function name
Ernie Rael21d32122023-09-02 15:09:18 +0200945 int *fp, // return: forceit
946 hash_T *hashp) // use/return fname hash, may be NULL
Bram Moolenaareead75c2019-04-21 11:35:00 +0200947{
Ernie Rael21d32122023-09-02 15:09:18 +0200948 if (prof_ga.ga_len == 0 || !prof_cache_initialized)
949 return debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
950 != (linenr_T)0;
951
952 hash_T hash;
953 if (hashp != NULL)
954 {
955 hash = *hashp;
956 if (hash == 0)
957 {
958 hash = hash_hash(fname);
959 *hashp = hash;
960 }
961 }
962 else
963 hash = hash_hash(fname);
964
965 hashtab_T *ht = &prof_cache[file ? PROF_HTAB_FILES : PROF_HTAB_FUNCS];
966 hashitem_T *hi = hash_lookup(ht, fname, hash);
967 profentry_T *pe;
968 if (HASHITEM_EMPTY(hi))
969 {
970 pe = alloc(offsetof(profentry_T, pen_name) + STRLEN(fname) + 1);
971 if (pe == NULL)
972 return FALSE;
973 STRCPY(pe->pen_name, fname);
974 pe->pen_flags = 0;
975 // run debuggy_find and capture return and forceit
976 int f;
977 int lnum = debuggy_find(file, fname, (linenr_T)0, &prof_ga, &f);
978 if (lnum)
979 {
980 PEN_SET_PROFILING(pe);
981 if (f)
982 PEN_SET_FORCEIT(pe);
983 }
984 hash_add_item(ht, hi, pe->pen_name, hash);
985#if defined(PROF_CACHE_LOG)
986 ch_log(NULL, "has_profiling: %s %s forceit %s, profile %s",
987 file ? "file" : "func", fname,
988 PEN_IS_FORCEIT(pe) ? "true" : "false",
989 PEN_IS_PROFILING(pe) ? "true" : "false");
990#endif
991 }
992 else
993 pe = HI2PE(hi);
994 if (fp)
995 *fp = PEN_IS_FORCEIT(pe);
996#if defined(PROF_CACHE_LOG)
997 count_lookups[file ? PROF_HTAB_FILES : PROF_HTAB_FUNCS]++;
998#endif
999 return PEN_IS_PROFILING(pe);
1000}
1001
1002 static void
Dominique Pellé4927bc72023-09-24 16:12:07 +02001003prof_clear_cache(void)
Ernie Rael21d32122023-09-02 15:09:18 +02001004{
1005 if (!prof_cache_initialized)
1006 {
1007 hash_init(&prof_cache[PROF_HTAB_FUNCS]);
1008 hash_init(&prof_cache[PROF_HTAB_FILES]);
1009 prof_cache_initialized = TRUE;
1010 }
1011
1012 hashtab_T *ht;
1013 for (ht = &prof_cache[0]; ht < &prof_cache[N_PROF_HTAB]; ht++)
1014 {
1015 if (ht->ht_used > 0)
1016 {
1017#if defined(PROF_CACHE_LOG)
1018 int idx = ht == &prof_cache[PROF_HTAB_FUNCS]
1019 ? PROF_HTAB_FUNCS : PROF_HTAB_FILES;
1020 ch_log(NULL, "prof_clear_cache: %s, used: %ld, lookups: %d",
1021 idx == PROF_HTAB_FUNCS ? "function" : "script",
1022 ht->ht_used, count_lookups[idx]);
1023 count_lookups[idx] = 0;
1024#endif
1025 hash_clear_all(ht, offsetof(profentry_T, pen_name));
1026 hash_init(ht);
1027 }
1028 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001029}
1030#endif
1031
1032/*
1033 * Common code for dbg_find_breakpoint() and has_profiling().
1034 */
1035 static linenr_T
1036debuggy_find(
Bram Moolenaarb2049902021-01-24 12:53:53 +01001037 int is_file, // TRUE for a file, FALSE for a function
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001038 char_u *fname, // file or function name
1039 linenr_T after, // after this line number
1040 garray_T *gap, // either &dbg_breakp or &prof_ga
1041 int *fp) // if not NULL: return forceit
Bram Moolenaareead75c2019-04-21 11:35:00 +02001042{
1043 struct debuggy *bp;
1044 int i;
1045 linenr_T lnum = 0;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001046 char_u *name = NULL;
1047 char_u *short_name = fname;
Bram Moolenaareead75c2019-04-21 11:35:00 +02001048 int prev_got_int;
1049
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001050 // Return quickly when there are no breakpoints.
Bram Moolenaareead75c2019-04-21 11:35:00 +02001051 if (gap->ga_len == 0)
1052 return (linenr_T)0;
1053
Bram Moolenaarb2049902021-01-24 12:53:53 +01001054 // For a script-local function remove the prefix, so that
1055 // "profile func Func" matches "Func" in any script. Otherwise it's very
1056 // difficult to profile/debug a script-local function. It may match a
1057 // function in the wrong script, but that is much better than not being
1058 // able to profile/debug a function in a script with unknown ID.
1059 // Also match a script-specific name.
1060 if (!is_file && fname[0] == K_SPECIAL)
Bram Moolenaareead75c2019-04-21 11:35:00 +02001061 {
Bram Moolenaarb2049902021-01-24 12:53:53 +01001062 short_name = vim_strchr(fname, '_') + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001063 name = alloc(STRLEN(fname) + 3);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001064 if (name != NULL)
Bram Moolenaareead75c2019-04-21 11:35:00 +02001065 {
1066 STRCPY(name, "<SNR>");
1067 STRCPY(name + 5, fname + 3);
1068 }
1069 }
1070
1071 for (i = 0; i < gap->ga_len; ++i)
1072 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001073 // Skip entries that are not useful or are for a line that is beyond
1074 // an already found breakpoint.
Bram Moolenaareead75c2019-04-21 11:35:00 +02001075 bp = &DEBUGGY(gap, i);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001076 if (((bp->dbg_type == DBG_FILE) == is_file
1077 && bp->dbg_type != DBG_EXPR && (
Bram Moolenaareead75c2019-04-21 11:35:00 +02001078#ifdef FEAT_PROFILE
1079 gap == &prof_ga ||
1080#endif
1081 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
1082 {
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001083 // Save the value of got_int and reset it. We don't want a
1084 // previous interruption cancel matching, only hitting CTRL-C
1085 // while matching should abort it.
Bram Moolenaareead75c2019-04-21 11:35:00 +02001086 prev_got_int = got_int;
1087 got_int = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001088 if ((name != NULL
1089 && vim_regexec_prog(&bp->dbg_prog, FALSE, name, (colnr_T)0))
1090 || vim_regexec_prog(&bp->dbg_prog, FALSE,
1091 short_name, (colnr_T)0))
Bram Moolenaareead75c2019-04-21 11:35:00 +02001092 {
1093 lnum = bp->dbg_lnum;
1094 if (fp != NULL)
1095 *fp = bp->dbg_forceit;
1096 }
1097 got_int |= prev_got_int;
Ernie Rael21d32122023-09-02 15:09:18 +02001098#ifdef FEAT_PROFILE
1099 if (lnum && gap == &prof_ga)
1100 break;
1101#endif
Bram Moolenaareead75c2019-04-21 11:35:00 +02001102 }
1103#ifdef FEAT_EVAL
1104 else if (bp->dbg_type == DBG_EXPR)
1105 {
1106 typval_T *tv;
1107 int line = FALSE;
1108
Bram Moolenaar0325d392021-09-09 12:34:19 +02001109 tv = eval_expr_no_emsg(bp);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001110 if (tv != NULL)
1111 {
1112 if (bp->dbg_val == NULL)
1113 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001114 debug_oldval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001115 bp->dbg_val = tv;
Bram Moolenaar34453202021-01-31 13:08:38 +01001116 debug_newval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001117 line = TRUE;
1118 }
1119 else
1120 {
Bram Moolenaarcf666202022-03-10 13:29:20 +00001121 // Use "==" instead of "is" for strings, that is what we
1122 // always have done.
1123 exprtype_T type = tv->v_type == VAR_STRING
1124 ? EXPR_EQUAL : EXPR_IS;
1125
1126 if (typval_compare(tv, bp->dbg_val, type, FALSE) == OK
Bram Moolenaareead75c2019-04-21 11:35:00 +02001127 && tv->vval.v_number == FALSE)
1128 {
1129 typval_T *v;
1130
1131 line = TRUE;
Bram Moolenaar34453202021-01-31 13:08:38 +01001132 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001133 // Need to evaluate again, typval_compare() overwrites
1134 // "tv".
Bram Moolenaar0325d392021-09-09 12:34:19 +02001135 v = eval_expr_no_emsg(bp);
Bram Moolenaar34453202021-01-31 13:08:38 +01001136 debug_newval = typval_tostring(v, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001137 free_tv(bp->dbg_val);
1138 bp->dbg_val = v;
1139 }
1140 free_tv(tv);
1141 }
1142 }
1143 else if (bp->dbg_val != NULL)
1144 {
Bram Moolenaar34453202021-01-31 13:08:38 +01001145 debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1146 debug_newval = typval_tostring(NULL, TRUE);
Bram Moolenaareead75c2019-04-21 11:35:00 +02001147 free_tv(bp->dbg_val);
1148 bp->dbg_val = NULL;
1149 line = TRUE;
1150 }
1151
1152 if (line)
1153 {
1154 lnum = after > 0 ? after : 1;
1155 break;
1156 }
Bram Moolenaareead75c2019-04-21 11:35:00 +02001157 }
1158#endif
1159 }
1160 if (name != fname)
1161 vim_free(name);
1162
1163 return lnum;
1164}
1165
1166/*
1167 * Called when a breakpoint was encountered.
1168 */
1169 void
1170dbg_breakpoint(char_u *name, linenr_T lnum)
1171{
Bram Moolenaar31fc39e2019-04-23 18:39:49 +02001172 // We need to check if this line is actually executed in do_one_cmd()
Bram Moolenaareead75c2019-04-21 11:35:00 +02001173 debug_breakpoint_name = name;
1174 debug_breakpoint_lnum = lnum;
1175}
1176#endif