Debian Codegraph @

libdebugger/cmd.c

raw symbols SCIP
1 /*
2 Copyright (C) 2004-2005, 2007-2009, 2011,
3 2014-2015, 2020 R. Bernstein
4 <[email protected]>
5 This file is part of GNU Make (remake variant).
6
7 GNU Make is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Make is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Make; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /** \file libdebugger/cmd.c
23 *
24 * \brief Top-level command REPL.
25 *
26 */
27
28 #include "makeint.h"
29 #include "main.h"
30 #include "msg.h"
31 #include "debug.h"
32 #include "file.h"
33 #include "print.h"
34 #include "cmd.h"
35 #include "fns.h"
36 #include "info.h"
37 #include "stack.h"
38 #include "commands.h"
39 #include "debug.h"
40 #include "file2line.h"
41 #include "cmd.h"
42
43 #ifdef HAVE_LIBREADLINE
44 #include <stdio.h>
45 #include <stdlib.h>
46 /* The following line makes Solaris' gcc/cpp not puke. */
47 #undef HAVE_READLINE_READLINE_H
48 #include <readline/readline.h>
49
50 /* From readline. ?? Should this be in configure? */
51 #ifndef whitespace
52 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
53 #endif
54 #endif /* HAVE_LIBREADLINE */
55
56 #include "cmd_initialize.h"
57
58
59 #ifdef HAVE_READLINE_READLINE_H
60 #include <readline/readline.h>
61 #endif
62
63 #ifdef HAVE_READLINE_HISTORY_H
64 #include <readline/history.h>
65 #endif
66
67 #ifdef _GNU_SOURCE
68 # define ATTRIBUTE_UNUSED __attribute__((unused))
69 #else
70 # define ATTRIBUTE_UNUSED
71 #endif
72
73 /**
74 Command-line args after the command-name part. For example in:
75 break foo
76 the below will be "foo".
77 **/
78 char *psz_debugger_args;
79
80 debug_enter_reason_t last_stop_reason;
81
82 #ifdef HAVE_LIBREADLINE
83
84 short_cmd_t short_command[256] = { { NULL,
85 (const char *) '\0',
86 (const char *) '\0',
87 (uint8_t) 255,
88 false,
89 }, };
90
91 /*!
92 Look up `psz_name` as the name of a command, and return a pointer to that
93 command. Return a NULL pointer if `psz_name` isn't a command name.
94 */
95 short_cmd_t *
96 find_command (const char *psz_name)
97 {
98 unsigned int i;
99
100 for (i = 0; aliases[i].alias; i++) {
101 const int cmp = strcmp (psz_name, aliases[i].alias);
102 if ( 0 == cmp ) {
103 psz_name = aliases[i].command;
104 break;
105 } else
106 /* Words should be in alphabetic order by alias name.
107 Have we gone too far? */
108 if (cmp < 0) break;
109 }
110
111 for (i = 0; dbg_commands[i].long_name; i++) {
112 const int cmp = strcmp (psz_name, dbg_commands[i].long_name);
113 if ( 0 == cmp ) {
114 return &(short_command[(uint8_t) dbg_commands[i].short_name]);
115 } else
116 /* Words should be in alphabetic order by command name.
117 Have we gone too far? */
118 if (cmp < 0) break;
119 }
120
121 return ((short_cmd_t *)NULL);
122 }
123
124 /*!
125 Execute a command line: the "EP" part of "REPL".
126 */
127 extern debug_return_t
128 execute_line (char *psz_line)
129 {
130 unsigned int i = 0;
131 short_cmd_t *command;
132 char *psz_word = get_word(&psz_line);
133
134 if (1 == strlen(psz_word)) {
135 if ( NULL != short_command[(uint8_t) psz_word[0]].func )
136 command = &short_command[(uint8_t) psz_word[0]];
137 else
138 command = NULL;
139 } else {
140 command = find_command (psz_word);
141 }
142 if (!command)
143 {
144 dbg_errmsg(_("No such debugger command: %s."), psz_word);
145 return debug_readloop;
146 }
147
148 if (command->needs_running && last_stop_reason == DEBUG_ERROR_HIT) {
149 dbg_errmsg(_("command: %s is only valid when `remake` not in post-mortem debugging."), psz_word);
150 return debug_readloop;
151 }
152
153 /* Get argument to command, if any. */
154 while (whitespace (psz_line[i]))
155 i++;
156
157 psz_debugger_args = psz_line + i;
158
159 /* Call the function. */
160 return ((*(command->func)) (psz_debugger_args));
161 }
162
163 /*!
164 Show command history.
165 */
166 debug_return_t
167 dbg_cmd_show_command (const char
168 *psz_args ATTRIBUTE_UNUSED)
169 {
170 /*
171 if (!psz_arg || *psz_arg) {
172 ;
173 } */
174
175 #ifdef HAVE_READLINE_HISTORY_H
176 HIST_ENTRY **hist_list = history_list();
177 unsigned int i;
178 UNUSED_ARGUMENT(psz_args);
179 if (!hist_list) return debug_readloop;
180 for (i=1; hist_list[i]; i++) {
181 dbg_msg("%5u %s", i, hist_list[i]->line);
182 }
183 #endif /* HAVE_READLINE_HISTORY_H */
184 return debug_readloop;
185 }
186
187 /*!
188 Set a variable. Set "expand' to 1 if you want variable
189 definitions inside the value getting passed in to be expanded
190 before assigment.
191 */
192 extern debug_return_t dbg_cmd_set_var (char *psz_args, int expand)
193 {
194 if (!psz_args || 0==strlen(psz_args)) {
195 dbg_msg(_("You need to supply a variable name."));
196 } else {
197 variable_t *p_v;
198 char *psz_varname = get_word(&psz_args);
199 unsigned int u_len = strlen(psz_varname);
200
201 while (*psz_args && whitespace (*psz_args))
202 *psz_args +=1;
203
204 p_v = lookup_variable (psz_varname, u_len);
205
206 if (p_v) {
207 char *psz_value = expand ? variable_expand(psz_args) : psz_args;
208
209 define_variable_in_set(p_v->name, u_len, psz_value,
210 o_debugger, 0, NULL,
211 &(p_v->fileinfo));
212 dbg_msg(_("Variable %s now has value '%s'"), psz_varname,
213 psz_value);
214 } else {
215 p_v = try_without_dollar(psz_varname);
216 if (p_v) {
217 char *psz_value = expand ? variable_expand(psz_args) : psz_args;
218 define_variable_in_set(p_v->name, u_len, psz_value,
219 o_debugger, 0, NULL,
220 &(p_v->fileinfo));
221 dbg_msg(_("Variable %s now has value '%s'"), psz_varname,
222 psz_value);
223 }
224 }
225 }
226 return debug_readloop;
227 }
228
229 #endif /* HAVE_LIBREADLINE */
230
231 #define PROMPT_LENGTH 300
232
233 #include <setjmp.h>
234 jmp_buf debugger_loop;
235
236 /* Should be less that PROMPT_LENGTH / 2 - strlen("remake ") + log(history)
237 We will make it much less that since people can't count more than
238 10 or so nested <<<<>>>>'s easily.
239 */
240 #define MAX_NEST_DEPTH 10
241
242 debug_return_t enter_debugger (target_stack_node_t *p,
243 file_t *p_target, int errcode,
244 debug_enter_reason_t reason)
245 {
246 volatile debug_return_t debug_return = debug_readloop;
247 static bool b_init = false;
248 static bool b_readline_init = false;
249 char open_depth[MAX_NEST_DEPTH];
250 char close_depth[MAX_NEST_DEPTH];
251 unsigned int i = 0;
252
253 last_stop_reason = reason;
254
255 if ( in_debugger == DEBUGGER_QUIT_RC ) {
256 return continue_execution;
257 }
258
259 if ( i_debugger_stepping > 1 || i_debugger_nexting > 1 ) {
260 /* Don't stop unless we are here from a breakpoint. But
261 do decrement the step count. */
262 if (i_debugger_stepping) i_debugger_stepping--;
263 if (i_debugger_nexting) i_debugger_nexting--;
264 if (!p_target->tracing) return continue_execution;
265 } else if ( !debugger_on_error
266 && !(i_debugger_stepping || i_debugger_nexting)
267 && p_target && !p_target->tracing && -2 != errcode )
268 return continue_execution;
269
270 /* Clear temporary breakpoints. */
271 if (p_target && p_target->tracing & BRK_TEMP)
272 switch(last_stop_reason)
273 {
274 case DEBUG_BRKPT_AFTER_CMD:
275 case DEBUG_BRKPT_BEFORE_PREREQ:
276 case DEBUG_BRKPT_AFTER_PREREQ:
277 p_target->tracing = BRK_NONE;
278 default:
279 ;
280 }
281
282 #ifdef HAVE_LIBREADLINE
283 if (use_readline_flag && !b_readline_init) {
284 rl_initialize ();
285 using_history ();
286 add_history ("");
287 b_readline_init = true;
288 }
289 #endif /* HAVE_LIBREADLINE */
290 if (!b_init) {
291 cmd_initialize();
292 file2lines.ht_size = 0;
293 b_init = true;
294 }
295
296
297 /* Set initial frame position reporting area: 0 is bottom. */
298 p_target_loc = NULL;
299 psz_target_name = (char *) "";
300 i_stack_pos = 0;
301
302 p_stack = p_stack_top = p;
303 p_floc_stack = p_stack_floc_top;
304
305 /* Get the target name either from the stack top (preferred) or
306 the passed in target.
307 */
308 if (p && p->p_target) {
309 p_target_loc = &(p->p_target->floc);
310 psz_target_name = (char *) p->p_target->name;
311 } else if (p_target) {
312 p_target_loc = &(p_target->floc);
313 psz_target_name = (char *) p_target->name;
314 }
315
316 for (i=0; i<=makelevel && i < MAX_NEST_DEPTH-5; i++) {
317 open_depth[i] = '<';
318 close_depth[i] = '>';
319 }
320
321 if ( MAX_NEST_DEPTH - 5 == i ) {
322 close_depth[i] = open_depth[i] = '.'; i++;
323 close_depth[i] = open_depth[i] = '.'; i++;
324 close_depth[i] = open_depth[i] = '.'; i++;
325 }
326
327 open_depth[i] = close_depth[i] = '\0';
328
329 in_debugger = true;
330
331 if (errcode) {
332 if (-1 == errcode) {
333 printf("\n***Entering debugger because we encountered an error.\n");
334 } else if (-2 == errcode) {
335 if (0 == makelevel) {
336 printf("\nMakefile terminated.\n");
337 dbg_msg("Use q to quit or R to restart");
338 } else {
339 printf("\nMakefile finished at level %u. Use R to restart\n",
340 makelevel);
341 dbg_msg("the makefile at this level or 's', 'n', or 'F' to continue "
342 "in parent");
343 in_debugger = DEBUGGER_QUIT_RC;
344 }
345 } else {
346 printf("\n***Entering debugger because we encountered a fatal error.\n");
347 dbg_errmsg("Exiting the debugger will exit make with exit code %d.",
348 errcode);
349 }
350 }
351
352 print_debugger_location(p_target, reason, NULL);
353
354 /* Loop reading and executing lines until the user quits. */
355 for ( debug_return = debug_readloop;
356 debug_return == debug_readloop || debug_return == debug_cmd_error; ) {
357 char prompt[PROMPT_LENGTH];
358 char *line=NULL;
359 char *s;
360
361 if (setjmp(debugger_loop))
362 dbg_errmsg("Internal error jumped back to debugger loop");
363 else {
364
365 #ifdef HAVE_LIBREADLINE
366 if (use_readline_flag) {
367 snprintf(prompt, PROMPT_LENGTH, "remake%s%d%s ",
368 open_depth, where_history(), close_depth);
369
370 line = readline (prompt);
371 } else
372 #endif /* HAVE_LIBREADLINE */
373 {
374 snprintf(prompt, PROMPT_LENGTH, "remake%s0%s ", open_depth,
375 close_depth);
376 printf("%s", prompt);
377 if (line == NULL) line = calloc(1, 2048);
378 line = fgets(line, 2048, stdin);
379 if (NULL != line) chomp(line);
380 }
381
382 if ( line ) {
383 if ( *(s=stripwhite(line)) ) {
384 add_history (s);
385 debug_return=execute_line(s);
386 } else {
387 add_history ("step");
388 debug_return=dbg_cmd_step((char *) "");
389 }
390 free (line);
391 } else {
392 dbg_cmd_quit(NULL);
393 }
394 }
395 }
396
397 if (in_debugger != DEBUGGER_QUIT_RC)
398 in_debugger=false;
399
400 return debug_return;
401 }
402
403 /*
404 * Local variables:
405 * eval: (c-set-style "gnu")
406 * indent-tabs-mode: nil
407 * End:
408 */