]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Top level stuff for GDB, the GNU debugger. |
b6ba6518 KB |
2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
3 | 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. | |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program 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 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program 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. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "gdbcmd.h" | |
24 | #include "call-cmds.h" | |
25 | #include "symtab.h" | |
26 | #include "inferior.h" | |
042be3a9 | 27 | #include <signal.h> |
c906108c SS |
28 | #include "target.h" |
29 | #include "breakpoint.h" | |
30 | #include "gdbtypes.h" | |
31 | #include "expression.h" | |
32 | #include "value.h" | |
33 | #include "language.h" | |
c5aa993b | 34 | #include "terminal.h" /* For job_control. */ |
c906108c | 35 | #include "annotate.h" |
c5f0f3d0 | 36 | #include "completer.h" |
c906108c | 37 | #include "top.h" |
d4f3574e | 38 | #include "version.h" |
c906108c SS |
39 | |
40 | /* readline include files */ | |
41 | #include <readline/readline.h> | |
42 | #include <readline/history.h> | |
43 | ||
44 | /* readline defines this. */ | |
45 | #undef savestring | |
46 | ||
47 | #include <sys/types.h> | |
c906108c | 48 | |
c4093a6a | 49 | #include <setjmp.h> |
2acceee2 | 50 | |
c2c6d25f | 51 | #include "event-top.h" |
c906108c SS |
52 | #include "gdb_string.h" |
53 | #include "gdb_stat.h" | |
54 | #include <ctype.h> | |
8b93c638 JM |
55 | #ifdef UI_OUT |
56 | #include "ui-out.h" | |
57 | #include "cli-out.h" | |
58 | #endif | |
c906108c | 59 | |
d318976c | 60 | /* From completer.c */ |
c906108c | 61 | |
d318976c | 62 | extern int is_complete_command (void (*func) (char *args, int from_tty)); |
c906108c | 63 | |
d318976c | 64 | /* From cli/cli-cmds.c */ |
c906108c | 65 | |
d318976c | 66 | extern void init_cmd_lists (void); |
c906108c | 67 | |
d318976c | 68 | extern void init_cli_cmds (void); |
c906108c | 69 | |
d318976c | 70 | extern void execute_user_command (struct cmd_list_element *c, char *args); |
c906108c | 71 | |
d318976c FN |
72 | /* From cli/cli-setshow.c */ |
73 | ||
74 | extern void do_setshow_command (char *, int, struct cmd_list_element *); | |
c906108c | 75 | |
d318976c | 76 | /* Exported to CLI cli/cli-cmds.c. */ |
c906108c | 77 | |
d318976c | 78 | void set_verbose (char *, int, struct cmd_list_element *); |
c906108c | 79 | |
d318976c | 80 | void show_history (char *, int); |
c906108c | 81 | |
d318976c | 82 | void set_history (char *, int); |
c906108c | 83 | |
d318976c | 84 | void show_commands (char *, int); |
c906108c | 85 | |
d318976c | 86 | void do_restore_instream_cleanup (void *stream); |
c906108c | 87 | |
d318976c | 88 | /* Prototypes for local functions */ |
c906108c | 89 | |
d318976c | 90 | static void dont_repeat_command (char *, int); |
c906108c | 91 | |
d318976c | 92 | static void init_signals (void); |
c906108c | 93 | |
d318976c FN |
94 | #ifdef STOP_SIGNAL |
95 | static void stop_sig (int); | |
96 | #endif | |
c906108c | 97 | |
a14ed312 | 98 | static void init_main (void); |
c906108c | 99 | |
a14ed312 | 100 | static void float_handler (int); |
c906108c | 101 | |
a14ed312 | 102 | static void init_signals (void); |
c906108c | 103 | |
a14ed312 | 104 | static void set_history_size_command (char *, int, struct cmd_list_element *); |
c906108c | 105 | |
a14ed312 | 106 | static void do_nothing (int); |
c906108c SS |
107 | |
108 | #ifdef SIGHUP | |
392a587b | 109 | /* NOTE 1999-04-29: This function will be static again, once we modify |
cd0fc7c3 SS |
110 | gdb to use the event loop as the default command loop and we merge |
111 | event-top.c into this file, top.c */ | |
a14ed312 | 112 | /* static */ int quit_cover (PTR); |
c906108c | 113 | |
a14ed312 | 114 | static void disconnect (int); |
c906108c SS |
115 | #endif |
116 | ||
104c1213 JM |
117 | /* Default command line prompt. This is overriden in some configs. */ |
118 | ||
119 | #ifndef DEFAULT_PROMPT | |
120 | #define DEFAULT_PROMPT "(gdb) " | |
c906108c SS |
121 | #endif |
122 | ||
123 | /* Initialization file name for gdb. This is overridden in some configs. */ | |
124 | ||
125 | #ifndef GDBINIT_FILENAME | |
126 | #define GDBINIT_FILENAME ".gdbinit" | |
127 | #endif | |
128 | char gdbinit[] = GDBINIT_FILENAME; | |
129 | ||
130 | int inhibit_gdbinit = 0; | |
131 | ||
132 | /* If nonzero, and GDB has been configured to be able to use windows, | |
133 | attempt to open them upon startup. */ | |
134 | ||
135 | int use_windows = 1; | |
136 | ||
c906108c SS |
137 | extern char lang_frame_mismatch_warn[]; /* language.c */ |
138 | ||
139 | /* Flag for whether we want all the "from_tty" gubbish printed. */ | |
140 | ||
c5aa993b | 141 | int caution = 1; /* Default is yes, sigh. */ |
c906108c | 142 | |
c906108c SS |
143 | /* stdio stream that command input is being read from. Set to stdin normally. |
144 | Set by source_command to the file we are sourcing. Set to NULL if we are | |
145 | executing a user-defined command or interacting via a GUI. */ | |
146 | ||
147 | FILE *instream; | |
148 | ||
149 | /* Current working directory. */ | |
150 | ||
151 | char *current_directory; | |
152 | ||
153 | /* The directory name is actually stored here (usually). */ | |
154 | char gdb_dirbuf[1024]; | |
155 | ||
156 | /* Function to call before reading a command, if nonzero. | |
157 | The function receives two args: an input stream, | |
158 | and a prompt string. */ | |
159 | ||
507f3c78 | 160 | void (*window_hook) (FILE *, char *); |
c906108c SS |
161 | |
162 | int epoch_interface; | |
163 | int xgdb_verbose; | |
164 | ||
165 | /* gdb prints this when reading a command interactively */ | |
c5aa993b | 166 | static char *gdb_prompt_string; /* the global prompt string */ |
a14ed312 | 167 | extern char *get_prompt (void); /* access function for prompt string */ |
c906108c SS |
168 | |
169 | /* Buffer used for reading command lines, and the size | |
170 | allocated for it so far. */ | |
171 | ||
172 | char *line; | |
173 | int linesize = 100; | |
174 | ||
175 | /* Nonzero if the current command is modified by "server ". This | |
c2d11a7d | 176 | affects things like recording into the command history, commands |
c906108c SS |
177 | repeating on RETURN, etc. This is so a user interface (emacs, GUI, |
178 | whatever) can issue its own commands and also send along commands | |
179 | from the user, and have the user not notice that the user interface | |
180 | is issuing commands too. */ | |
181 | int server_command; | |
182 | ||
183 | /* Baud rate specified for talking to serial target systems. Default | |
184 | is left as -1, so targets can choose their own defaults. */ | |
185 | /* FIXME: This means that "show remotebaud" and gr_files_info can print -1 | |
186 | or (unsigned int)-1. This is a Bad User Interface. */ | |
187 | ||
188 | int baud_rate = -1; | |
189 | ||
190 | /* Timeout limit for response from target. */ | |
191 | ||
ce808e91 AC |
192 | /* The default value has been changed many times over the years. It |
193 | was originally 5 seconds. But that was thought to be a long time | |
194 | to sit and wait, so it was changed to 2 seconds. That was thought | |
195 | to be plenty unless the connection was going through some terminal | |
196 | server or multiplexer or other form of hairy serial connection. | |
197 | ||
198 | In mid-1996, remote_timeout was moved from remote.c to top.c and | |
199 | it began being used in other remote-* targets. It appears that the | |
200 | default was changed to 20 seconds at that time, perhaps because the | |
201 | Hitachi E7000 ICE didn't always respond in a timely manner. | |
202 | ||
203 | But if 5 seconds is a long time to sit and wait for retransmissions, | |
204 | 20 seconds is far worse. This demonstrates the difficulty of using | |
205 | a single variable for all protocol timeouts. | |
206 | ||
207 | As remote.c is used much more than remote-e7000.c, it was changed | |
208 | back to 2 seconds in 1999. */ | |
209 | ||
210 | int remote_timeout = 2; | |
c906108c SS |
211 | |
212 | /* Non-zero tells remote* modules to output debugging info. */ | |
213 | ||
214 | int remote_debug = 0; | |
215 | ||
43ff13b4 JM |
216 | /* Non-zero means the target is running. Note: this is different from |
217 | saying that there is an active target and we are stopped at a | |
218 | breakpoint, for instance. This is a real indicator whether the | |
219 | target is off and running, which gdb is doing something else. */ | |
220 | int target_executing = 0; | |
221 | ||
c906108c SS |
222 | /* Level of control structure. */ |
223 | static int control_level; | |
224 | ||
c906108c SS |
225 | /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */ |
226 | ||
227 | #ifndef STOP_SIGNAL | |
228 | #ifdef SIGTSTP | |
229 | #define STOP_SIGNAL SIGTSTP | |
a14ed312 | 230 | static void stop_sig (int); |
c906108c SS |
231 | #endif |
232 | #endif | |
233 | ||
234 | /* Some System V have job control but not sigsetmask(). */ | |
235 | #if !defined (HAVE_SIGSETMASK) | |
236 | #if !defined (USG) | |
237 | #define HAVE_SIGSETMASK 1 | |
238 | #else | |
239 | #define HAVE_SIGSETMASK 0 | |
240 | #endif | |
241 | #endif | |
242 | ||
243 | #if 0 == (HAVE_SIGSETMASK) | |
244 | #define sigsetmask(n) | |
245 | #endif | |
246 | ||
247 | /* Hooks for alternate command interfaces. */ | |
248 | ||
249 | /* Called after most modules have been initialized, but before taking users | |
250 | command file. */ | |
251 | ||
507f3c78 | 252 | void (*init_ui_hook) (char *argv0); |
7a292a7a SS |
253 | |
254 | /* This hook is called from within gdb's many mini-event loops which could | |
255 | steal control from a real user interface's event loop. It returns | |
256 | non-zero if the user is requesting a detach, zero otherwise. */ | |
257 | ||
507f3c78 | 258 | int (*ui_loop_hook) (int); |
c906108c SS |
259 | |
260 | /* Called instead of command_loop at top level. Can be invoked via | |
261 | return_to_top_level. */ | |
262 | ||
507f3c78 | 263 | void (*command_loop_hook) (void); |
c906108c SS |
264 | |
265 | ||
c906108c SS |
266 | /* Called from print_frame_info to list the line we stopped in. */ |
267 | ||
507f3c78 KB |
268 | void (*print_frame_info_listing_hook) (struct symtab * s, int line, |
269 | int stopline, int noerror); | |
c906108c SS |
270 | /* Replaces most of query. */ |
271 | ||
507f3c78 | 272 | int (*query_hook) (const char *, va_list); |
c906108c SS |
273 | |
274 | /* Replaces most of warning. */ | |
275 | ||
507f3c78 | 276 | void (*warning_hook) (const char *, va_list); |
c906108c | 277 | |
c906108c SS |
278 | /* These three functions support getting lines of text from the user. They |
279 | are used in sequence. First readline_begin_hook is called with a text | |
280 | string that might be (for example) a message for the user to type in a | |
281 | sequence of commands to be executed at a breakpoint. If this function | |
282 | calls back to a GUI, it might take this opportunity to pop up a text | |
283 | interaction window with this message. Next, readline_hook is called | |
284 | with a prompt that is emitted prior to collecting the user input. | |
285 | It can be called multiple times. Finally, readline_end_hook is called | |
286 | to notify the GUI that we are done with the interaction window and it | |
287 | can close it. */ | |
288 | ||
507f3c78 KB |
289 | void (*readline_begin_hook) (char *, ...); |
290 | char *(*readline_hook) (char *); | |
291 | void (*readline_end_hook) (void); | |
c906108c SS |
292 | |
293 | /* Called as appropriate to notify the interface of the specified breakpoint | |
294 | conditions. */ | |
295 | ||
507f3c78 KB |
296 | void (*create_breakpoint_hook) (struct breakpoint * bpt); |
297 | void (*delete_breakpoint_hook) (struct breakpoint * bpt); | |
298 | void (*modify_breakpoint_hook) (struct breakpoint * bpt); | |
c906108c | 299 | |
6426a772 JM |
300 | /* Called as appropriate to notify the interface that we have attached |
301 | to or detached from an already running process. */ | |
302 | ||
507f3c78 KB |
303 | void (*attach_hook) (void); |
304 | void (*detach_hook) (void); | |
6426a772 | 305 | |
c906108c SS |
306 | /* Called during long calculations to allow GUI to repair window damage, and to |
307 | check for stop buttons, etc... */ | |
308 | ||
507f3c78 | 309 | void (*interactive_hook) (void); |
c906108c SS |
310 | |
311 | /* Called when the registers have changed, as a hint to a GUI | |
312 | to minimize window update. */ | |
313 | ||
507f3c78 | 314 | void (*registers_changed_hook) (void); |
c906108c SS |
315 | |
316 | /* Tell the GUI someone changed the register REGNO. -1 means | |
317 | that the caller does not know which register changed or | |
c5aa993b | 318 | that several registers have changed (see value_assign). */ |
507f3c78 | 319 | void (*register_changed_hook) (int regno); |
c906108c SS |
320 | |
321 | /* Tell the GUI someone changed LEN bytes of memory at ADDR */ | |
507f3c78 | 322 | void (*memory_changed_hook) (CORE_ADDR addr, int len); |
c906108c SS |
323 | |
324 | /* Called when going to wait for the target. Usually allows the GUI to run | |
325 | while waiting for target events. */ | |
326 | ||
39f77062 KB |
327 | ptid_t (*target_wait_hook) (ptid_t ptid, |
328 | struct target_waitstatus * status); | |
c906108c SS |
329 | |
330 | /* Used by UI as a wrapper around command execution. May do various things | |
331 | like enabling/disabling buttons, etc... */ | |
332 | ||
507f3c78 KB |
333 | void (*call_command_hook) (struct cmd_list_element * c, char *cmd, |
334 | int from_tty); | |
c906108c | 335 | |
96baa820 JM |
336 | /* Called after a `set' command has finished. Is only run if the |
337 | `set' command succeeded. */ | |
338 | ||
eb2f494a | 339 | void (*set_hook) (struct cmd_list_element * c); |
96baa820 | 340 | |
c906108c SS |
341 | /* Called when the current thread changes. Argument is thread id. */ |
342 | ||
507f3c78 | 343 | void (*context_hook) (int id); |
c906108c SS |
344 | |
345 | /* Takes control from error (). Typically used to prevent longjmps out of the | |
346 | middle of the GUI. Usually used in conjunction with a catch routine. */ | |
347 | ||
eb2f494a | 348 | NORETURN void (*error_hook) (void) ATTR_NORETURN; |
c906108c | 349 | \f |
c5aa993b | 350 | |
99eeeb0f ND |
351 | /* One should use catch_errors rather than manipulating these |
352 | directly. */ | |
c4093a6a JM |
353 | #if defined(HAVE_SIGSETJMP) |
354 | #define SIGJMP_BUF sigjmp_buf | |
355 | #define SIGSETJMP(buf) sigsetjmp(buf, 1) | |
356 | #define SIGLONGJMP(buf,val) siglongjmp(buf,val) | |
357 | #else | |
358 | #define SIGJMP_BUF jmp_buf | |
359 | #define SIGSETJMP(buf) setjmp(buf) | |
360 | #define SIGLONGJMP(buf,val) longjmp(buf,val) | |
361 | #endif | |
362 | ||
99eeeb0f ND |
363 | /* Where to go for return_to_top_level. */ |
364 | static SIGJMP_BUF *catch_return; | |
c906108c | 365 | |
99eeeb0f | 366 | /* Return for reason REASON to the nearest containing catch_errors(). */ |
c906108c | 367 | |
c2d11a7d | 368 | NORETURN void |
fba45db2 | 369 | return_to_top_level (enum return_reason reason) |
c906108c SS |
370 | { |
371 | quit_flag = 0; | |
372 | immediate_quit = 0; | |
373 | ||
374 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure | |
375 | I can think of a reason why that is vital, though). */ | |
c5aa993b | 376 | bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */ |
c906108c SS |
377 | |
378 | disable_current_display (); | |
379 | do_cleanups (ALL_CLEANUPS); | |
c4093a6a | 380 | if (event_loop_p && target_can_async_p () && !target_executing) |
43ff13b4 | 381 | do_exec_cleanups (ALL_CLEANUPS); |
6426a772 JM |
382 | if (event_loop_p && sync_execution) |
383 | do_exec_error_cleanups (ALL_CLEANUPS); | |
c906108c SS |
384 | |
385 | if (annotation_level > 1) | |
386 | switch (reason) | |
387 | { | |
388 | case RETURN_QUIT: | |
389 | annotate_quit (); | |
390 | break; | |
391 | case RETURN_ERROR: | |
392 | annotate_error (); | |
393 | break; | |
394 | } | |
395 | ||
99eeeb0f ND |
396 | /* Jump to the containing catch_errors() call, communicating REASON |
397 | to that call via setjmp's return value. Note that REASON can't | |
398 | be zero, by definition in defs.h. */ | |
399 | ||
eb2f494a | 400 | (NORETURN void) SIGLONGJMP (*catch_return, (int) reason); |
c906108c SS |
401 | } |
402 | ||
403 | /* Call FUNC with arg ARGS, catching any errors. If there is no | |
404 | error, return the value returned by FUNC. If there is an error, | |
405 | print ERRSTRING, print the specific error message, then return | |
406 | zero. | |
407 | ||
408 | Must not be called with immediate_quit in effect (bad things might | |
409 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
410 | This is an OK restriction; with very few exceptions immediate_quit can | |
411 | be replaced by judicious use of QUIT. | |
412 | ||
413 | MASK specifies what to catch; it is normally set to | |
414 | RETURN_MASK_ALL, if for no other reason than that the code which | |
415 | calls catch_errors might not be set up to deal with a quit which | |
416 | isn't caught. But if the code can deal with it, it generally | |
417 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
418 | useful to abort only the portion of the operation inside the | |
419 | catch_errors. Note that quit should return to the command line | |
420 | fairly quickly, even if some further processing is being done. */ | |
421 | ||
11cf8741 JM |
422 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with |
423 | error() et.al. could maintain a set of flags that indicate the the | |
424 | current state of each of the longjmp buffers. This would give the | |
425 | longjmp code the chance to detect a longjmp botch (before it gets | |
426 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
427 | code also randomly used a SET_TOP_LEVEL macro that directly | |
428 | initialize the longjmp buffers. */ | |
429 | ||
e26cc349 | 430 | /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code |
11cf8741 JM |
431 | be consolidated into a single file instead of being distributed |
432 | between utils.c and top.c? */ | |
433 | ||
c906108c | 434 | int |
fba45db2 KB |
435 | catch_errors (catch_errors_ftype *func, PTR args, char *errstring, |
436 | return_mask mask) | |
c906108c | 437 | { |
99eeeb0f ND |
438 | SIGJMP_BUF *saved_catch; |
439 | SIGJMP_BUF catch; | |
c906108c SS |
440 | int val; |
441 | struct cleanup *saved_cleanup_chain; | |
442 | char *saved_error_pre_print; | |
443 | char *saved_quit_pre_print; | |
444 | ||
99eeeb0f ND |
445 | /* Return value from SIGSETJMP(): enum return_reason if error or |
446 | quit caught, 0 otherwise. */ | |
447 | int caught; | |
448 | ||
449 | /* Override error/quit messages during FUNC. */ | |
450 | ||
c906108c SS |
451 | saved_error_pre_print = error_pre_print; |
452 | saved_quit_pre_print = quit_pre_print; | |
453 | ||
454 | if (mask & RETURN_MASK_ERROR) | |
99eeeb0f | 455 | error_pre_print = errstring; |
c906108c | 456 | if (mask & RETURN_MASK_QUIT) |
99eeeb0f ND |
457 | quit_pre_print = errstring; |
458 | ||
459 | /* Prevent error/quit during FUNC from calling cleanups established | |
460 | prior to here. */ | |
461 | ||
462 | saved_cleanup_chain = save_cleanups (); | |
463 | ||
464 | /* Call FUNC, catching error/quit events. */ | |
465 | ||
466 | saved_catch = catch_return; | |
467 | catch_return = &catch; | |
468 | caught = SIGSETJMP (catch); | |
469 | if (!caught) | |
470 | val = (*func) (args); | |
7f7e9482 AC |
471 | else |
472 | val = 0; | |
99eeeb0f ND |
473 | catch_return = saved_catch; |
474 | ||
e26cc349 | 475 | /* FIXME: cagney/1999-11-05: A correct FUNC implementation will |
99eeeb0f | 476 | clean things up (restoring the cleanup chain) to the state they |
e26cc349 | 477 | were just prior to the call. Unfortunately, many FUNC's are not |
99eeeb0f ND |
478 | that well behaved. This could be fixed by adding either a |
479 | do_cleanups call (to cover the problem) or an assertion check to | |
480 | detect bad FUNCs code. */ | |
481 | ||
482 | /* Restore the cleanup chain and error/quit messages to their | |
483 | original states. */ | |
c906108c SS |
484 | |
485 | restore_cleanups (saved_cleanup_chain); | |
486 | ||
c906108c | 487 | if (mask & RETURN_MASK_QUIT) |
99eeeb0f ND |
488 | quit_pre_print = saved_quit_pre_print; |
489 | if (mask & RETURN_MASK_ERROR) | |
490 | error_pre_print = saved_error_pre_print; | |
491 | ||
492 | /* Return normally if no error/quit event occurred. */ | |
493 | ||
494 | if (!caught) | |
495 | return val; | |
496 | ||
497 | /* If the caller didn't request that the event be caught, relay the | |
498 | event to the next containing catch_errors(). */ | |
499 | ||
500 | if (!(mask & RETURN_MASK (caught))) | |
501 | return_to_top_level (caught); | |
502 | ||
503 | /* Tell the caller that an event was caught. | |
504 | ||
505 | FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller | |
506 | can't tell what type of event occurred. | |
507 | ||
508 | A possible fix is to add a new interface, catch_event(), that | |
509 | returns enum return_reason after catching an error or a quit. | |
510 | ||
511 | When returning normally, i.e. without catching an error or a | |
512 | quit, catch_event() could return RETURN_NORMAL, which would be | |
513 | added to enum return_reason. FUNC would return information | |
514 | exclusively via ARGS. | |
515 | ||
516 | Alternatively, normal catch_event() could return FUNC's return | |
517 | value. The caller would need to be aware of potential overlap | |
518 | with enum return_reason, which could be publicly restricted to | |
519 | negative values to simplify return value processing in FUNC and | |
520 | in the caller. */ | |
521 | ||
522 | return 0; | |
c906108c SS |
523 | } |
524 | ||
11cf8741 JM |
525 | struct captured_command_args |
526 | { | |
527 | catch_command_errors_ftype *command; | |
528 | char *arg; | |
529 | int from_tty; | |
530 | }; | |
531 | ||
532 | static int | |
533 | do_captured_command (void *data) | |
534 | { | |
535 | struct captured_command_args *context = data; | |
536 | context->command (context->arg, context->from_tty); | |
537 | /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call | |
538 | isn't needed. Instead an assertion check could be made that | |
539 | simply confirmed that the called function correctly cleaned up | |
e26cc349 | 540 | after itself. Unfortunately, old code (prior to 1999-11-04) in |
11cf8741 JM |
541 | main.c was calling SET_TOP_LEVEL(), calling the command function, |
542 | and then *always* calling do_cleanups(). For the moment we | |
543 | remain ``bug compatible'' with that old code.. */ | |
544 | do_cleanups (ALL_CLEANUPS); | |
545 | return 1; | |
546 | } | |
547 | ||
548 | int | |
eb2f494a | 549 | catch_command_errors (catch_command_errors_ftype * command, |
11cf8741 JM |
550 | char *arg, int from_tty, return_mask mask) |
551 | { | |
552 | struct captured_command_args args; | |
553 | args.command = command; | |
554 | args.arg = arg; | |
555 | args.from_tty = from_tty; | |
556 | return catch_errors (do_captured_command, &args, "", mask); | |
557 | } | |
558 | ||
559 | ||
c906108c SS |
560 | /* Handler for SIGHUP. */ |
561 | ||
562 | #ifdef SIGHUP | |
563 | static void | |
fba45db2 | 564 | disconnect (int signo) |
c906108c SS |
565 | { |
566 | catch_errors (quit_cover, NULL, | |
c5aa993b | 567 | "Could not kill the program being debugged", RETURN_MASK_ALL); |
c906108c SS |
568 | signal (SIGHUP, SIG_DFL); |
569 | kill (getpid (), SIGHUP); | |
570 | } | |
571 | ||
572 | /* Just a little helper function for disconnect(). */ | |
573 | ||
392a587b | 574 | /* NOTE 1999-04-29: This function will be static again, once we modify |
cd0fc7c3 SS |
575 | gdb to use the event loop as the default command loop and we merge |
576 | event-top.c into this file, top.c */ | |
577 | /* static */ int | |
fba45db2 | 578 | quit_cover (PTR s) |
c906108c | 579 | { |
c5aa993b JM |
580 | caution = 0; /* Throw caution to the wind -- we're exiting. |
581 | This prevents asking the user dumb questions. */ | |
582 | quit_command ((char *) 0, 0); | |
c906108c SS |
583 | return 0; |
584 | } | |
585 | #endif /* defined SIGHUP */ | |
586 | \f | |
587 | /* Line number we are currently in in a file which is being sourced. */ | |
392a587b | 588 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
cd0fc7c3 SS |
589 | gdb to use the event loop as the default command loop and we merge |
590 | event-top.c into this file, top.c */ | |
591 | /* static */ int source_line_number; | |
c906108c SS |
592 | |
593 | /* Name of the file we are sourcing. */ | |
392a587b | 594 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
cd0fc7c3 SS |
595 | gdb to use the event loop as the default command loop and we merge |
596 | event-top.c into this file, top.c */ | |
597 | /* static */ char *source_file_name; | |
c906108c SS |
598 | |
599 | /* Buffer containing the error_pre_print used by the source stuff. | |
600 | Malloc'd. */ | |
392a587b | 601 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
cd0fc7c3 SS |
602 | gdb to use the event loop as the default command loop and we merge |
603 | event-top.c into this file, top.c */ | |
604 | /* static */ char *source_error; | |
c906108c SS |
605 | static int source_error_allocated; |
606 | ||
607 | /* Something to glom on to the start of error_pre_print if source_file_name | |
608 | is set. */ | |
392a587b | 609 | /* NOTE 1999-04-29: This variable will be static again, once we modify |
cd0fc7c3 SS |
610 | gdb to use the event loop as the default command loop and we merge |
611 | event-top.c into this file, top.c */ | |
612 | /* static */ char *source_pre_error; | |
c906108c SS |
613 | |
614 | /* Clean up on error during a "source" command (or execution of a | |
615 | user-defined command). */ | |
616 | ||
d318976c | 617 | void |
e41a3b1a | 618 | do_restore_instream_cleanup (void *stream) |
c906108c SS |
619 | { |
620 | /* Restore the previous input stream. */ | |
621 | instream = stream; | |
622 | } | |
623 | ||
624 | /* Read commands from STREAM. */ | |
625 | void | |
fba45db2 | 626 | read_command_file (FILE *stream) |
c906108c SS |
627 | { |
628 | struct cleanup *cleanups; | |
629 | ||
e41a3b1a | 630 | cleanups = make_cleanup (do_restore_instream_cleanup, instream); |
c906108c | 631 | instream = stream; |
c5aa993b | 632 | command_loop (); |
c906108c SS |
633 | do_cleanups (cleanups); |
634 | } | |
635 | \f | |
a14ed312 | 636 | extern void init_proc (void); |
c906108c | 637 | |
507f3c78 | 638 | void (*pre_init_ui_hook) (void); |
c906108c | 639 | |
e41a3b1a AC |
640 | #ifdef __MSDOS__ |
641 | void | |
642 | do_chdir_cleanup (void *old_dir) | |
643 | { | |
644 | chdir (old_dir); | |
b8c9b27d | 645 | xfree (old_dir); |
e41a3b1a AC |
646 | } |
647 | #endif | |
648 | ||
c906108c | 649 | void |
fba45db2 | 650 | gdb_init (char *argv0) |
c906108c SS |
651 | { |
652 | if (pre_init_ui_hook) | |
653 | pre_init_ui_hook (); | |
654 | ||
655 | /* Run the init function of each source file */ | |
656 | ||
657 | getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)); | |
658 | current_directory = gdb_dirbuf; | |
659 | ||
a0b3c4fd JM |
660 | #ifdef __MSDOS__ |
661 | /* Make sure we return to the original directory upon exit, come | |
662 | what may, since the OS doesn't do that for us. */ | |
e41a3b1a | 663 | make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory)); |
a0b3c4fd JM |
664 | #endif |
665 | ||
c5aa993b JM |
666 | init_cmd_lists (); /* This needs to be done first */ |
667 | initialize_targets (); /* Setup target_terminal macros for utils.c */ | |
668 | initialize_utils (); /* Make errors and warnings possible */ | |
c906108c | 669 | initialize_all_files (); |
e514a9d6 | 670 | initialize_current_architecture (); |
d318976c | 671 | init_cli_cmds(); |
c5aa993b | 672 | init_main (); /* But that omits this file! Do it now */ |
cd0fc7c3 SS |
673 | |
674 | /* The signal handling mechanism is different depending whether or | |
675 | not the async version is run. NOTE: in the future we plan to make | |
676 | the event loop be the default engine of gdb, and this difference | |
677 | will disappear. */ | |
6426a772 | 678 | if (event_loop_p) |
cd0fc7c3 SS |
679 | async_init_signals (); |
680 | else | |
c5aa993b | 681 | init_signals (); |
c906108c | 682 | |
c906108c SS |
683 | /* We need a default language for parsing expressions, so simple things like |
684 | "set width 0" won't fail if no language is explicitly set in a config file | |
685 | or implicitly set by reading an executable during startup. */ | |
686 | set_language (language_c); | |
c5aa993b | 687 | expected_language = current_language; /* don't warn about the change. */ |
c906108c | 688 | |
8b93c638 JM |
689 | #ifdef UI_OUT |
690 | /* Install the default UI */ | |
67e1e03a | 691 | if (!init_ui_hook) |
fb40c209 | 692 | { |
67e1e03a MH |
693 | uiout = cli_out_new (gdb_stdout); |
694 | ||
695 | /* All the interpreters should have had a look at things by now. | |
696 | Initialize the selected interpreter. */ | |
697 | if (interpreter_p) | |
698 | { | |
699 | fprintf_unfiltered (gdb_stderr, "Interpreter `%s' unrecognized.\n", | |
700 | interpreter_p); | |
701 | exit (1); | |
702 | } | |
fb40c209 AC |
703 | } |
704 | #endif | |
8b93c638 | 705 | |
c906108c SS |
706 | if (init_ui_hook) |
707 | init_ui_hook (argv0); | |
708 | } | |
709 | ||
d318976c FN |
710 | /* Execute the line P as a command. |
711 | Pass FROM_TTY as second argument to the defining function. */ | |
c906108c | 712 | |
d318976c FN |
713 | void |
714 | execute_command (char *p, int from_tty) | |
c906108c | 715 | { |
d318976c FN |
716 | register struct cmd_list_element *c; |
717 | register enum language flang; | |
718 | static int warned = 0; | |
719 | char *line; | |
720 | /* FIXME: These should really be in an appropriate header file */ | |
67e1e03a MH |
721 | extern void serial_log_command (const char *); |
722 | ||
d318976c | 723 | free_all_values (); |
c906108c | 724 | |
d318976c FN |
725 | /* Force cleanup of any alloca areas if using C alloca instead of |
726 | a builtin alloca. */ | |
727 | alloca (0); | |
c906108c | 728 | |
d318976c FN |
729 | /* This can happen when command_line_input hits end of file. */ |
730 | if (p == NULL) | |
731 | return; | |
c906108c | 732 | |
d318976c | 733 | serial_log_command (p); |
8b93c638 | 734 | |
d318976c FN |
735 | while (*p == ' ' || *p == '\t') |
736 | p++; | |
737 | if (*p) | |
8b93c638 | 738 | { |
d318976c FN |
739 | char *arg; |
740 | line = p; | |
8b93c638 | 741 | |
d318976c | 742 | c = lookup_cmd (&p, cmdlist, "", 0, 1); |
8b93c638 | 743 | |
d318976c FN |
744 | /* If the target is running, we allow only a limited set of |
745 | commands. */ | |
746 | if (event_loop_p && target_can_async_p () && target_executing) | |
747 | if (!strcmp (c->name, "help") | |
748 | && !strcmp (c->name, "pwd") | |
749 | && !strcmp (c->name, "show") | |
750 | && !strcmp (c->name, "stop")) | |
751 | error ("Cannot execute this command while the target is running."); | |
8b93c638 | 752 | |
d318976c FN |
753 | /* Pass null arg rather than an empty one. */ |
754 | arg = *p ? p : 0; | |
8b93c638 | 755 | |
d318976c FN |
756 | /* Clear off trailing whitespace, except for set and complete command. */ |
757 | if (arg | |
758 | && c->type != set_cmd | |
759 | && !is_complete_command (c->function.cfunc)) | |
8b93c638 | 760 | { |
d318976c FN |
761 | p = arg + strlen (arg) - 1; |
762 | while (p >= arg && (*p == ' ' || *p == '\t')) | |
763 | p--; | |
764 | *(p + 1) = '\0'; | |
8b93c638 JM |
765 | } |
766 | ||
d318976c FN |
767 | /* If this command has been pre-hooked, run the hook first. */ |
768 | if ((c->hook_pre) && (!c->hook_in)) | |
769 | { | |
770 | c->hook_in = 1; /* Prevent recursive hooking */ | |
771 | execute_user_command (c->hook_pre, (char *) 0); | |
772 | c->hook_in = 0; /* Allow hook to work again once it is complete */ | |
773 | } | |
c906108c | 774 | |
d318976c FN |
775 | if (c->flags & DEPRECATED_WARN_USER) |
776 | deprecated_cmd_warning (&line); | |
c906108c | 777 | |
d318976c FN |
778 | if (c->class == class_user) |
779 | execute_user_command (c, arg); | |
780 | else if (c->type == set_cmd || c->type == show_cmd) | |
781 | do_setshow_command (arg, from_tty & caution, c); | |
782 | else if (c->function.cfunc == NO_FUNCTION) | |
783 | error ("That is not a command, just a help topic."); | |
784 | else if (call_command_hook) | |
785 | call_command_hook (c, arg, from_tty & caution); | |
786 | else | |
787 | (*c->function.cfunc) (arg, from_tty & caution); | |
788 | ||
789 | /* If this command has been post-hooked, run the hook last. */ | |
790 | if ((c->hook_post) && (!c->hook_in)) | |
791 | { | |
792 | c->hook_in = 1; /* Prevent recursive hooking */ | |
793 | execute_user_command (c->hook_post, (char *) 0); | |
794 | c->hook_in = 0; /* allow hook to work again once it is complete */ | |
795 | } | |
c906108c | 796 | |
c906108c SS |
797 | } |
798 | ||
d318976c FN |
799 | /* Tell the user if the language has changed (except first time). */ |
800 | if (current_language != expected_language) | |
c906108c | 801 | { |
d318976c | 802 | if (language_mode == language_mode_auto) |
c906108c | 803 | { |
d318976c | 804 | language_info (1); /* Print what changed. */ |
c906108c | 805 | } |
d318976c | 806 | warned = 0; |
c906108c SS |
807 | } |
808 | ||
d318976c FN |
809 | /* Warn the user if the working language does not match the |
810 | language of the current frame. Only warn the user if we are | |
811 | actually running the program, i.e. there is a stack. */ | |
812 | /* FIXME: This should be cacheing the frame and only running when | |
813 | the frame changes. */ | |
814 | ||
815 | if (target_has_stack) | |
c906108c | 816 | { |
d318976c FN |
817 | flang = get_frame_language (); |
818 | if (!warned | |
819 | && flang != language_unknown | |
820 | && flang != current_language->la_language) | |
c906108c | 821 | { |
d318976c FN |
822 | printf_filtered ("%s\n", lang_frame_mismatch_warn); |
823 | warned = 1; | |
c906108c | 824 | } |
c906108c SS |
825 | } |
826 | } | |
827 | ||
d318976c FN |
828 | /* Read commands from `instream' and execute them |
829 | until end of file or error reading instream. */ | |
c906108c | 830 | |
d318976c FN |
831 | void |
832 | command_loop (void) | |
c906108c | 833 | { |
d318976c FN |
834 | struct cleanup *old_chain; |
835 | char *command; | |
836 | int stdin_is_tty = ISATTY (stdin); | |
837 | long time_at_cmd_start; | |
838 | #ifdef HAVE_SBRK | |
839 | long space_at_cmd_start = 0; | |
840 | #endif | |
841 | extern int display_time; | |
842 | extern int display_space; | |
c5aa993b | 843 | |
d318976c FN |
844 | while (instream && !feof (instream)) |
845 | { | |
d318976c FN |
846 | if (window_hook && instream == stdin) |
847 | (*window_hook) (instream, get_prompt ()); | |
c906108c | 848 | |
d318976c FN |
849 | quit_flag = 0; |
850 | if (instream == stdin && stdin_is_tty) | |
851 | reinitialize_more_filter (); | |
852 | old_chain = make_cleanup (null_cleanup, 0); | |
c906108c | 853 | |
d318976c FN |
854 | /* Get a command-line. This calls the readline package. */ |
855 | command = command_line_input (instream == stdin ? | |
856 | get_prompt () : (char *) NULL, | |
857 | instream == stdin, "prompt"); | |
d318976c FN |
858 | if (command == 0) |
859 | return; | |
c906108c | 860 | |
d318976c | 861 | time_at_cmd_start = get_run_time (); |
c906108c | 862 | |
d318976c | 863 | if (display_space) |
9e0b60a8 | 864 | { |
d318976c FN |
865 | #ifdef HAVE_SBRK |
866 | extern char **environ; | |
867 | char *lim = (char *) sbrk (0); | |
9e0b60a8 | 868 | |
d318976c | 869 | space_at_cmd_start = (long) (lim - (char *) &environ); |
9e0b60a8 | 870 | #endif |
d318976c | 871 | } |
9e0b60a8 | 872 | |
d318976c FN |
873 | execute_command (command, instream == stdin); |
874 | /* Do any commands attached to breakpoint we stopped at. */ | |
875 | bpstat_do_actions (&stop_bpstat); | |
876 | do_cleanups (old_chain); | |
9e0b60a8 | 877 | |
d318976c | 878 | if (display_time) |
9e0b60a8 | 879 | { |
d318976c | 880 | long cmd_time = get_run_time () - time_at_cmd_start; |
9e0b60a8 | 881 | |
d318976c FN |
882 | printf_unfiltered ("Command execution time: %ld.%06ld\n", |
883 | cmd_time / 1000000, cmd_time % 1000000); | |
9e0b60a8 | 884 | } |
9e0b60a8 | 885 | |
d318976c | 886 | if (display_space) |
9e0b60a8 | 887 | { |
d318976c FN |
888 | #ifdef HAVE_SBRK |
889 | extern char **environ; | |
890 | char *lim = (char *) sbrk (0); | |
891 | long space_now = lim - (char *) &environ; | |
892 | long space_diff = space_now - space_at_cmd_start; | |
893 | ||
894 | printf_unfiltered ("Space used: %ld (%c%ld for this command)\n", | |
895 | space_now, | |
896 | (space_diff >= 0 ? '+' : '-'), | |
897 | space_diff); | |
898 | #endif | |
9e0b60a8 | 899 | } |
9e0b60a8 | 900 | } |
9e0b60a8 | 901 | } |
9e0b60a8 | 902 | |
d318976c FN |
903 | /* Read commands from `instream' and execute them until end of file or |
904 | error reading instream. This command loop doesnt care about any | |
905 | such things as displaying time and space usage. If the user asks | |
906 | for those, they won't work. */ | |
907 | void | |
908 | simplified_command_loop (char *(*read_input_func) (char *), | |
909 | void (*execute_command_func) (char *, int)) | |
9e0b60a8 | 910 | { |
d318976c FN |
911 | struct cleanup *old_chain; |
912 | char *command; | |
913 | int stdin_is_tty = ISATTY (stdin); | |
9e0b60a8 | 914 | |
d318976c | 915 | while (instream && !feof (instream)) |
9e0b60a8 | 916 | { |
d318976c FN |
917 | quit_flag = 0; |
918 | if (instream == stdin && stdin_is_tty) | |
919 | reinitialize_more_filter (); | |
920 | old_chain = make_cleanup (null_cleanup, 0); | |
921 | ||
922 | /* Get a command-line. */ | |
923 | command = (*read_input_func) (instream == stdin ? | |
924 | get_prompt () : (char *) NULL); | |
9e0b60a8 | 925 | |
d318976c FN |
926 | if (command == 0) |
927 | return; | |
9e0b60a8 | 928 | |
d318976c | 929 | (*execute_command_func) (command, instream == stdin); |
9e0b60a8 | 930 | |
d318976c FN |
931 | /* Do any commands attached to breakpoint we stopped at. */ |
932 | bpstat_do_actions (&stop_bpstat); | |
9e0b60a8 | 933 | |
d318976c | 934 | do_cleanups (old_chain); |
c5aa993b | 935 | } |
9e0b60a8 | 936 | } |
d318976c FN |
937 | \f |
938 | /* Commands call this if they do not want to be repeated by null lines. */ | |
9e0b60a8 | 939 | |
d318976c FN |
940 | void |
941 | dont_repeat (void) | |
9e0b60a8 | 942 | { |
d318976c FN |
943 | if (server_command) |
944 | return; | |
9e0b60a8 | 945 | |
d318976c FN |
946 | /* If we aren't reading from standard input, we are saving the last |
947 | thing read from stdin in line and don't want to delete it. Null lines | |
948 | won't repeat here in any case. */ | |
949 | if (instream == stdin) | |
950 | *line = 0; | |
9e0b60a8 | 951 | } |
d318976c FN |
952 | \f |
953 | /* Read a line from the stream "instream" without command line editing. | |
9e0b60a8 | 954 | |
d318976c FN |
955 | It prints PROMPT_ARG once at the start. |
956 | Action is compatible with "readline", e.g. space for the result is | |
957 | malloc'd and should be freed by the caller. | |
9e0b60a8 | 958 | |
d318976c FN |
959 | A NULL return means end of file. */ |
960 | char * | |
961 | gdb_readline (char *prompt_arg) | |
9e0b60a8 | 962 | { |
d318976c FN |
963 | int c; |
964 | char *result; | |
965 | int input_index = 0; | |
966 | int result_size = 80; | |
9e0b60a8 | 967 | |
d318976c | 968 | if (prompt_arg) |
9e0b60a8 | 969 | { |
d318976c FN |
970 | /* Don't use a _filtered function here. It causes the assumed |
971 | character position to be off, since the newline we read from | |
972 | the user is not accounted for. */ | |
973 | fputs_unfiltered (prompt_arg, gdb_stdout); | |
d036b4d9 AC |
974 | /* OBSOLETE #ifdef MPW */ |
975 | /* OBSOLETE Move to a new line so the entered line doesn't have a prompt */ | |
976 | /* OBSOLETE on the front of it. */ | |
977 | /* OBSOLETE fputs_unfiltered ("\n", gdb_stdout); */ | |
978 | /* OBSOLETE #endif *//* MPW */ | |
9e0b60a8 JM |
979 | gdb_flush (gdb_stdout); |
980 | } | |
981 | ||
d318976c | 982 | result = (char *) xmalloc (result_size); |
9e0b60a8 JM |
983 | |
984 | while (1) | |
985 | { | |
d318976c FN |
986 | /* Read from stdin if we are executing a user defined command. |
987 | This is the right thing for prompt_for_continue, at least. */ | |
988 | c = fgetc (instream ? instream : stdin); | |
9e0b60a8 | 989 | |
d318976c | 990 | if (c == EOF) |
9e0b60a8 | 991 | { |
d318976c FN |
992 | if (input_index > 0) |
993 | /* The last line does not end with a newline. Return it, and | |
994 | if we are called again fgetc will still return EOF and | |
995 | we'll return NULL then. */ | |
9e0b60a8 | 996 | break; |
b8c9b27d | 997 | xfree (result); |
d318976c | 998 | return NULL; |
9e0b60a8 | 999 | } |
c5aa993b | 1000 | |
d318976c FN |
1001 | if (c == '\n') |
1002 | #ifndef CRLF_SOURCE_FILES | |
1003 | break; | |
1004 | #else | |
9e0b60a8 | 1005 | { |
d318976c FN |
1006 | if (input_index > 0 && result[input_index - 1] == '\r') |
1007 | input_index--; | |
1008 | break; | |
9e0b60a8 | 1009 | } |
d318976c | 1010 | #endif |
9e0b60a8 | 1011 | |
d318976c FN |
1012 | result[input_index++] = c; |
1013 | while (input_index >= result_size) | |
9e0b60a8 | 1014 | { |
d318976c FN |
1015 | result_size *= 2; |
1016 | result = (char *) xrealloc (result, result_size); | |
9e0b60a8 | 1017 | } |
9e0b60a8 JM |
1018 | } |
1019 | ||
d318976c FN |
1020 | result[input_index++] = '\0'; |
1021 | return result; | |
9e0b60a8 JM |
1022 | } |
1023 | ||
d318976c FN |
1024 | /* Variables which control command line editing and history |
1025 | substitution. These variables are given default values at the end | |
1026 | of this file. */ | |
1027 | static int command_editing_p; | |
1028 | /* NOTE 1999-04-29: This variable will be static again, once we modify | |
1029 | gdb to use the event loop as the default command loop and we merge | |
1030 | event-top.c into this file, top.c */ | |
1031 | /* static */ int history_expansion_p; | |
1032 | static int write_history_p; | |
1033 | static int history_size; | |
1034 | static char *history_filename; | |
9e0b60a8 | 1035 | |
9e0b60a8 | 1036 | \f |
d318976c FN |
1037 | #ifdef STOP_SIGNAL |
1038 | static void | |
1039 | stop_sig (int signo) | |
9e0b60a8 | 1040 | { |
d318976c FN |
1041 | #if STOP_SIGNAL == SIGTSTP |
1042 | signal (SIGTSTP, SIG_DFL); | |
1043 | sigsetmask (0); | |
1044 | kill (getpid (), SIGTSTP); | |
1045 | signal (SIGTSTP, stop_sig); | |
1046 | #else | |
1047 | signal (STOP_SIGNAL, stop_sig); | |
1048 | #endif | |
1049 | printf_unfiltered ("%s", get_prompt ()); | |
1050 | gdb_flush (gdb_stdout); | |
9e0b60a8 | 1051 | |
d318976c FN |
1052 | /* Forget about any previous command -- null line now will do nothing. */ |
1053 | dont_repeat (); | |
9e0b60a8 | 1054 | } |
d318976c | 1055 | #endif /* STOP_SIGNAL */ |
9e0b60a8 | 1056 | |
d318976c | 1057 | /* Initialize signal handlers. */ |
9e0b60a8 | 1058 | static void |
d318976c | 1059 | do_nothing (int signo) |
9e0b60a8 | 1060 | { |
d318976c FN |
1061 | /* Under System V the default disposition of a signal is reinstated after |
1062 | the signal is caught and delivered to an application process. On such | |
1063 | systems one must restore the replacement signal handler if one wishes | |
1064 | to continue handling the signal in one's program. On BSD systems this | |
1065 | is not needed but it is harmless, and it simplifies the code to just do | |
1066 | it unconditionally. */ | |
1067 | signal (signo, do_nothing); | |
9e0b60a8 JM |
1068 | } |
1069 | ||
9e0b60a8 | 1070 | static void |
d318976c | 1071 | init_signals (void) |
9e0b60a8 | 1072 | { |
d318976c | 1073 | signal (SIGINT, request_quit); |
9e0b60a8 | 1074 | |
d318976c FN |
1075 | /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed |
1076 | to the inferior and breakpoints will be ignored. */ | |
1077 | #ifdef SIGTRAP | |
1078 | signal (SIGTRAP, SIG_DFL); | |
1079 | #endif | |
9e0b60a8 | 1080 | |
d318976c FN |
1081 | /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get |
1082 | passed to the inferior, which we don't want. It would be | |
1083 | possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but | |
1084 | on BSD4.3 systems using vfork, that can affect the | |
1085 | GDB process as well as the inferior (the signal handling tables | |
1086 | might be in memory, shared between the two). Since we establish | |
1087 | a handler for SIGQUIT, when we call exec it will set the signal | |
1088 | to SIG_DFL for us. */ | |
1089 | signal (SIGQUIT, do_nothing); | |
1090 | #ifdef SIGHUP | |
1091 | if (signal (SIGHUP, do_nothing) != SIG_IGN) | |
1092 | signal (SIGHUP, disconnect); | |
1093 | #endif | |
1094 | signal (SIGFPE, float_handler); | |
9e0b60a8 | 1095 | |
d318976c FN |
1096 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
1097 | signal (SIGWINCH, SIGWINCH_HANDLER); | |
1098 | #endif | |
9e0b60a8 JM |
1099 | } |
1100 | \f | |
d318976c FN |
1101 | /* Read one line from the command input stream `instream' |
1102 | into the local static buffer `linebuffer' (whose current length | |
1103 | is `linelength'). | |
1104 | The buffer is made bigger as necessary. | |
1105 | Returns the address of the start of the line. | |
9e0b60a8 | 1106 | |
d318976c | 1107 | NULL is returned for end of file. |
9e0b60a8 | 1108 | |
d318976c FN |
1109 | *If* the instream == stdin & stdin is a terminal, the line read |
1110 | is copied into the file line saver (global var char *line, | |
1111 | length linesize) so that it can be duplicated. | |
9e0b60a8 | 1112 | |
d318976c FN |
1113 | This routine either uses fancy command line editing or |
1114 | simple input as the user has requested. */ | |
10689f25 | 1115 | |
d318976c FN |
1116 | char * |
1117 | command_line_input (char *prompt_arg, int repeat, char *annotation_suffix) | |
9e0b60a8 | 1118 | { |
d318976c FN |
1119 | static char *linebuffer = 0; |
1120 | static unsigned linelength = 0; | |
9e0b60a8 | 1121 | register char *p; |
d318976c FN |
1122 | char *p1; |
1123 | char *rl; | |
1124 | char *local_prompt = prompt_arg; | |
1125 | char *nline; | |
1126 | char got_eof = 0; | |
1127 | ||
1128 | /* The annotation suffix must be non-NULL. */ | |
1129 | if (annotation_suffix == NULL) | |
1130 | annotation_suffix = ""; | |
9e0b60a8 | 1131 | |
d318976c FN |
1132 | if (annotation_level > 1 && instream == stdin) |
1133 | { | |
1134 | local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg)) | |
1135 | + strlen (annotation_suffix) + 40); | |
1136 | if (prompt_arg == NULL) | |
1137 | local_prompt[0] = '\0'; | |
1138 | else | |
1139 | strcpy (local_prompt, prompt_arg); | |
1140 | strcat (local_prompt, "\n\032\032"); | |
1141 | strcat (local_prompt, annotation_suffix); | |
1142 | strcat (local_prompt, "\n"); | |
1143 | } | |
9e0b60a8 | 1144 | |
d318976c | 1145 | if (linebuffer == 0) |
9e0b60a8 | 1146 | { |
d318976c FN |
1147 | linelength = 80; |
1148 | linebuffer = (char *) xmalloc (linelength); | |
9e0b60a8 | 1149 | } |
9e0b60a8 | 1150 | |
d318976c | 1151 | p = linebuffer; |
9e0b60a8 | 1152 | |
d318976c FN |
1153 | /* Control-C quits instantly if typed while in this loop |
1154 | since it should not wait until the user types a newline. */ | |
1155 | immediate_quit++; | |
1156 | #ifdef STOP_SIGNAL | |
1157 | if (job_control) | |
73bc900d | 1158 | { |
d318976c FN |
1159 | if (event_loop_p) |
1160 | signal (STOP_SIGNAL, handle_stop_sig); | |
1161 | else | |
1162 | signal (STOP_SIGNAL, stop_sig); | |
1163 | } | |
1164 | #endif | |
1165 | ||
1166 | while (1) | |
9e0b60a8 | 1167 | { |
d318976c FN |
1168 | /* Make sure that all output has been output. Some machines may let |
1169 | you get away with leaving out some of the gdb_flush, but not all. */ | |
1170 | wrap_here (""); | |
1171 | gdb_flush (gdb_stdout); | |
1172 | gdb_flush (gdb_stderr); | |
1173 | ||
1174 | if (source_file_name != NULL) | |
1175 | { | |
1176 | ++source_line_number; | |
1177 | sprintf (source_error, | |
1178 | "%s%s:%d: Error in sourced command file:\n", | |
1179 | source_pre_error, | |
1180 | source_file_name, | |
1181 | source_line_number); | |
1182 | error_pre_print = source_error; | |
1183 | } | |
1184 | ||
1185 | if (annotation_level > 1 && instream == stdin) | |
1186 | { | |
1187 | printf_unfiltered ("\n\032\032pre-"); | |
1188 | printf_unfiltered (annotation_suffix); | |
1189 | printf_unfiltered ("\n"); | |
1190 | } | |
1191 | ||
1192 | /* Don't use fancy stuff if not talking to stdin. */ | |
1193 | if (readline_hook && instream == NULL) | |
1194 | { | |
1195 | rl = (*readline_hook) (local_prompt); | |
1196 | } | |
1197 | else if (command_editing_p && instream == stdin && ISATTY (instream)) | |
1198 | { | |
1199 | rl = readline (local_prompt); | |
1200 | } | |
9e0b60a8 | 1201 | else |
d318976c FN |
1202 | { |
1203 | rl = gdb_readline (local_prompt); | |
1204 | } | |
9e0b60a8 | 1205 | |
d318976c FN |
1206 | if (annotation_level > 1 && instream == stdin) |
1207 | { | |
1208 | printf_unfiltered ("\n\032\032post-"); | |
1209 | printf_unfiltered (annotation_suffix); | |
1210 | printf_unfiltered ("\n"); | |
1211 | } | |
9e0b60a8 | 1212 | |
d318976c | 1213 | if (!rl || rl == (char *) EOF) |
9e0b60a8 | 1214 | { |
d318976c FN |
1215 | got_eof = 1; |
1216 | break; | |
9e0b60a8 | 1217 | } |
d318976c FN |
1218 | if (strlen (rl) + 1 + (p - linebuffer) > linelength) |
1219 | { | |
1220 | linelength = strlen (rl) + 1 + (p - linebuffer); | |
1221 | nline = (char *) xrealloc (linebuffer, linelength); | |
1222 | p += nline - linebuffer; | |
1223 | linebuffer = nline; | |
1224 | } | |
1225 | p1 = rl; | |
1226 | /* Copy line. Don't copy null at end. (Leaves line alone | |
1227 | if this was just a newline) */ | |
1228 | while (*p1) | |
1229 | *p++ = *p1++; | |
9e0b60a8 | 1230 | |
b8c9b27d | 1231 | xfree (rl); /* Allocated in readline. */ |
9e0b60a8 | 1232 | |
d318976c FN |
1233 | if (p == linebuffer || *(p - 1) != '\\') |
1234 | break; | |
9e0b60a8 | 1235 | |
d318976c FN |
1236 | p--; /* Put on top of '\'. */ |
1237 | local_prompt = (char *) 0; | |
1238 | } | |
9e0b60a8 | 1239 | |
d318976c FN |
1240 | #ifdef STOP_SIGNAL |
1241 | if (job_control) | |
1242 | signal (STOP_SIGNAL, SIG_DFL); | |
1243 | #endif | |
1244 | immediate_quit--; | |
9e0b60a8 | 1245 | |
d318976c FN |
1246 | if (got_eof) |
1247 | return NULL; | |
9e0b60a8 | 1248 | |
d318976c FN |
1249 | #define SERVER_COMMAND_LENGTH 7 |
1250 | server_command = | |
1251 | (p - linebuffer > SERVER_COMMAND_LENGTH) | |
1252 | && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH); | |
1253 | if (server_command) | |
9e0b60a8 | 1254 | { |
d318976c FN |
1255 | /* Note that we don't set `line'. Between this and the check in |
1256 | dont_repeat, this insures that repeating will still do the | |
1257 | right thing. */ | |
1258 | *p = '\0'; | |
1259 | return linebuffer + SERVER_COMMAND_LENGTH; | |
9e0b60a8 | 1260 | } |
9e0b60a8 | 1261 | |
d318976c FN |
1262 | /* Do history expansion if that is wished. */ |
1263 | if (history_expansion_p && instream == stdin | |
1264 | && ISATTY (instream)) | |
1265 | { | |
1266 | char *history_value; | |
1267 | int expanded; | |
9e0b60a8 | 1268 | |
d318976c FN |
1269 | *p = '\0'; /* Insert null now. */ |
1270 | expanded = history_expand (linebuffer, &history_value); | |
1271 | if (expanded) | |
1272 | { | |
1273 | /* Print the changes. */ | |
1274 | printf_unfiltered ("%s\n", history_value); | |
9e0b60a8 | 1275 | |
d318976c FN |
1276 | /* If there was an error, call this function again. */ |
1277 | if (expanded < 0) | |
1278 | { | |
b8c9b27d | 1279 | xfree (history_value); |
d318976c FN |
1280 | return command_line_input (prompt_arg, repeat, annotation_suffix); |
1281 | } | |
1282 | if (strlen (history_value) > linelength) | |
1283 | { | |
1284 | linelength = strlen (history_value) + 1; | |
1285 | linebuffer = (char *) xrealloc (linebuffer, linelength); | |
1286 | } | |
1287 | strcpy (linebuffer, history_value); | |
1288 | p = linebuffer + strlen (linebuffer); | |
b8c9b27d | 1289 | xfree (history_value); |
d318976c FN |
1290 | } |
1291 | } | |
9e0b60a8 | 1292 | |
d318976c FN |
1293 | /* If we just got an empty line, and that is supposed |
1294 | to repeat the previous command, return the value in the | |
1295 | global buffer. */ | |
1296 | if (repeat && p == linebuffer) | |
1297 | return line; | |
1298 | for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++); | |
1299 | if (repeat && !*p1) | |
1300 | return line; | |
9e0b60a8 | 1301 | |
d318976c | 1302 | *p = 0; |
9e0b60a8 | 1303 | |
d318976c FN |
1304 | /* Add line to history if appropriate. */ |
1305 | if (instream == stdin | |
1306 | && ISATTY (stdin) && *linebuffer) | |
1307 | add_history (linebuffer); | |
9e0b60a8 | 1308 | |
d318976c FN |
1309 | /* Note: lines consisting solely of comments are added to the command |
1310 | history. This is useful when you type a command, and then | |
1311 | realize you don't want to execute it quite yet. You can comment | |
1312 | out the command and then later fetch it from the value history | |
1313 | and remove the '#'. The kill ring is probably better, but some | |
1314 | people are in the habit of commenting things out. */ | |
1315 | if (*p1 == '#') | |
1316 | *p1 = '\0'; /* Found a comment. */ | |
9e0b60a8 | 1317 | |
d318976c FN |
1318 | /* Save into global buffer if appropriate. */ |
1319 | if (repeat) | |
1320 | { | |
1321 | if (linelength > linesize) | |
1322 | { | |
1323 | line = xrealloc (line, linelength); | |
1324 | linesize = linelength; | |
1325 | } | |
1326 | strcpy (line, linebuffer); | |
1327 | return line; | |
1328 | } | |
9e0b60a8 | 1329 | |
d318976c | 1330 | return linebuffer; |
9e0b60a8 JM |
1331 | } |
1332 | \f | |
1333 | /* Print the GDB banner. */ | |
1334 | void | |
fba45db2 | 1335 | print_gdb_version (struct ui_file *stream) |
9e0b60a8 JM |
1336 | { |
1337 | /* From GNU coding standards, first line is meant to be easy for a | |
1338 | program to parse, and is just canonical program name and version | |
1339 | number, which starts after last space. */ | |
1340 | ||
b4df4f68 | 1341 | #ifdef MI_OUT |
8b93c638 | 1342 | /* Print it console style until a format is defined */ |
b4df4f68 | 1343 | fprintf_filtered (stream, "GNU gdb %s (MI_OUT)\n", version); |
8b93c638 | 1344 | #else |
9e0b60a8 | 1345 | fprintf_filtered (stream, "GNU gdb %s\n", version); |
8b93c638 | 1346 | #endif |
9e0b60a8 JM |
1347 | |
1348 | /* Second line is a copyright notice. */ | |
1349 | ||
388e1ff2 | 1350 | fprintf_filtered (stream, "Copyright 2001 Free Software Foundation, Inc.\n"); |
9e0b60a8 JM |
1351 | |
1352 | /* Following the copyright is a brief statement that the program is | |
1353 | free software, that users are free to copy and change it on | |
1354 | certain conditions, that it is covered by the GNU GPL, and that | |
1355 | there is no warranty. */ | |
1356 | ||
1357 | fprintf_filtered (stream, "\ | |
1358 | GDB is free software, covered by the GNU General Public License, and you are\n\ | |
1359 | welcome to change it and/or distribute copies of it under certain conditions.\n\ | |
1360 | Type \"show copying\" to see the conditions.\n\ | |
1361 | There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n"); | |
1362 | ||
1363 | /* After the required info we print the configuration information. */ | |
1364 | ||
1365 | fprintf_filtered (stream, "This GDB was configured as \""); | |
1366 | if (!STREQ (host_name, target_name)) | |
1367 | { | |
1368 | fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name); | |
1369 | } | |
1370 | else | |
1371 | { | |
1372 | fprintf_filtered (stream, "%s", host_name); | |
1373 | } | |
1374 | fprintf_filtered (stream, "\"."); | |
1375 | } | |
9e0b60a8 JM |
1376 | \f |
1377 | /* get_prompt: access method for the GDB prompt string. */ | |
1378 | ||
1379 | #define MAX_PROMPT_SIZE 256 | |
1380 | ||
1381 | /* | |
1382 | * int get_prompt_1 (char * buf); | |
1383 | * | |
1384 | * Work-horse for get_prompt (called via catch_errors). | |
1385 | * Argument is buffer to hold the formatted prompt. | |
1386 | * | |
1387 | * Returns: 1 for success (use formatted prompt) | |
1388 | * 0 for failure (use gdb_prompt_string). | |
c5aa993b | 1389 | */ |
9e0b60a8 JM |
1390 | |
1391 | static int gdb_prompt_escape; | |
1392 | ||
1393 | static int | |
710b33bd | 1394 | get_prompt_1 (void *data) |
9e0b60a8 | 1395 | { |
710b33bd | 1396 | char *formatted_prompt = data; |
9e0b60a8 JM |
1397 | char *local_prompt; |
1398 | ||
6426a772 | 1399 | if (event_loop_p) |
9e0b60a8 JM |
1400 | local_prompt = PROMPT (0); |
1401 | else | |
1402 | local_prompt = gdb_prompt_string; | |
1403 | ||
1404 | ||
1405 | if (gdb_prompt_escape == 0) | |
1406 | { | |
c5aa993b | 1407 | return 0; /* do no formatting */ |
9e0b60a8 | 1408 | } |
c5aa993b JM |
1409 | else |
1410 | /* formatted prompt */ | |
9e0b60a8 | 1411 | { |
c5aa993b | 1412 | char fmt[40], *promptp, *outp, *tmp; |
9e0b60a8 | 1413 | value_ptr arg_val; |
c5aa993b JM |
1414 | DOUBLEST doubleval; |
1415 | LONGEST longval; | |
9e0b60a8 JM |
1416 | CORE_ADDR addrval; |
1417 | ||
1418 | int i, len; | |
1419 | struct type *arg_type, *elt_type; | |
1420 | ||
1421 | promptp = local_prompt; | |
c5aa993b | 1422 | outp = formatted_prompt; |
9e0b60a8 JM |
1423 | |
1424 | while (*promptp != '\0') | |
1425 | { | |
1426 | int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1; | |
1427 | ||
1428 | if (*promptp != gdb_prompt_escape) | |
1429 | { | |
c5aa993b | 1430 | if (available >= 1) /* overflow protect */ |
9e0b60a8 JM |
1431 | *outp++ = *promptp++; |
1432 | } | |
1433 | else | |
1434 | { | |
1435 | /* GDB prompt string contains escape char. Parse for arg. | |
c5aa993b JM |
1436 | Two consecutive escape chars followed by arg followed by |
1437 | a comma means to insert the arg using a default format. | |
1438 | Otherwise a printf format string may be included between | |
1439 | the two escape chars. eg: | |
1440 | %%foo, insert foo using default format | |
1441 | %2.2f%foo, insert foo using "%2.2f" format | |
1442 | A mismatch between the format string and the data type | |
1443 | of "foo" is an error (which we don't know how to protect | |
1444 | against). */ | |
9e0b60a8 JM |
1445 | |
1446 | fmt[0] = '\0'; /* assume null format string */ | |
1447 | if (promptp[1] == gdb_prompt_escape) /* double esc char */ | |
1448 | { | |
1449 | promptp += 2; /* skip past two escape chars. */ | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | /* extract format string from between two esc chars */ | |
1454 | i = 0; | |
c5aa993b JM |
1455 | do |
1456 | { | |
1457 | fmt[i++] = *promptp++; /* copy format string */ | |
1458 | } | |
1459 | while (i < sizeof (fmt) - 1 && | |
1460 | *promptp != gdb_prompt_escape && | |
1461 | *promptp != '\0'); | |
9e0b60a8 JM |
1462 | |
1463 | if (*promptp != gdb_prompt_escape) | |
1464 | error ("Syntax error at prompt position %d", | |
1465 | promptp - local_prompt); | |
1466 | else | |
1467 | { | |
1468 | promptp++; /* skip second escape char */ | |
1469 | fmt[i++] = '\0'; /* terminate the format string */ | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | arg_val = parse_to_comma_and_eval (&promptp); | |
1474 | if (*promptp == ',') | |
c5aa993b | 1475 | promptp++; /* skip past the comma */ |
9e0b60a8 | 1476 | arg_type = check_typedef (VALUE_TYPE (arg_val)); |
c5aa993b | 1477 | switch (TYPE_CODE (arg_type)) |
9e0b60a8 JM |
1478 | { |
1479 | case TYPE_CODE_ARRAY: | |
1480 | elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type)); | |
c5aa993b | 1481 | if (TYPE_LENGTH (arg_type) > 0 && |
9e0b60a8 JM |
1482 | TYPE_LENGTH (elt_type) == 1 && |
1483 | TYPE_CODE (elt_type) == TYPE_CODE_INT) | |
1484 | { | |
1485 | int len = TYPE_LENGTH (arg_type); | |
1486 | ||
1487 | if (VALUE_LAZY (arg_val)) | |
1488 | value_fetch_lazy (arg_val); | |
1489 | tmp = VALUE_CONTENTS (arg_val); | |
1490 | ||
1491 | if (len > available) | |
c5aa993b | 1492 | len = available; /* overflow protect */ |
9e0b60a8 JM |
1493 | |
1494 | /* FIXME: how to protect GDB from crashing | |
c5aa993b | 1495 | from bad user-supplied format string? */ |
9e0b60a8 JM |
1496 | if (fmt[0] != 0) |
1497 | sprintf (outp, fmt, tmp); | |
1498 | else | |
1499 | strncpy (outp, tmp, len); | |
1500 | outp[len] = '\0'; | |
1501 | } | |
1502 | break; | |
1503 | case TYPE_CODE_PTR: | |
1504 | elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type)); | |
1505 | addrval = value_as_pointer (arg_val); | |
1506 | ||
1507 | if (TYPE_LENGTH (elt_type) == 1 && | |
c5aa993b | 1508 | TYPE_CODE (elt_type) == TYPE_CODE_INT && |
9e0b60a8 JM |
1509 | addrval != 0) |
1510 | { | |
1511 | /* display it as a string */ | |
1512 | char *default_fmt = "%s"; | |
1513 | char *tmp; | |
1514 | int err = 0; | |
1515 | ||
1516 | /* Limiting the number of bytes that the following call | |
c5aa993b JM |
1517 | will read protects us from sprintf overflow later. */ |
1518 | i = target_read_string (addrval, /* src */ | |
1519 | &tmp, /* dest */ | |
1520 | available, /* len */ | |
9e0b60a8 JM |
1521 | &err); |
1522 | if (err) /* read failed */ | |
1523 | error ("%s on target_read", safe_strerror (err)); | |
1524 | ||
1525 | tmp[i] = '\0'; /* force-terminate string */ | |
1526 | /* FIXME: how to protect GDB from crashing | |
c5aa993b JM |
1527 | from bad user-supplied format string? */ |
1528 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, | |
9e0b60a8 | 1529 | tmp); |
b8c9b27d | 1530 | xfree (tmp); |
9e0b60a8 JM |
1531 | } |
1532 | else | |
1533 | { | |
1534 | /* display it as a pointer */ | |
1535 | char *default_fmt = "0x%x"; | |
1536 | ||
1537 | /* FIXME: how to protect GDB from crashing | |
c5aa993b JM |
1538 | from bad user-supplied format string? */ |
1539 | if (available >= 16 /*? */ ) /* overflow protect */ | |
9e0b60a8 JM |
1540 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
1541 | (long) addrval); | |
1542 | } | |
1543 | break; | |
1544 | case TYPE_CODE_FLT: | |
1545 | { | |
1546 | char *default_fmt = "%g"; | |
1547 | ||
1548 | doubleval = value_as_double (arg_val); | |
1549 | /* FIXME: how to protect GDB from crashing | |
1550 | from bad user-supplied format string? */ | |
c5aa993b | 1551 | if (available >= 16 /*? */ ) /* overflow protect */ |
9e0b60a8 JM |
1552 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
1553 | (double) doubleval); | |
1554 | break; | |
1555 | } | |
1556 | case TYPE_CODE_INT: | |
1557 | { | |
1558 | char *default_fmt = "%d"; | |
1559 | ||
1560 | longval = value_as_long (arg_val); | |
1561 | /* FIXME: how to protect GDB from crashing | |
1562 | from bad user-supplied format string? */ | |
c5aa993b | 1563 | if (available >= 16 /*? */ ) /* overflow protect */ |
9e0b60a8 JM |
1564 | sprintf (outp, fmt[0] == 0 ? default_fmt : fmt, |
1565 | (long) longval); | |
1566 | break; | |
1567 | } | |
1568 | case TYPE_CODE_BOOL: | |
1569 | { | |
1570 | /* no default format for bool */ | |
1571 | longval = value_as_long (arg_val); | |
c5aa993b | 1572 | if (available >= 8 /*? */ ) /* overflow protect */ |
9e0b60a8 JM |
1573 | { |
1574 | if (longval) | |
1575 | strcpy (outp, "<true>"); | |
1576 | else | |
1577 | strcpy (outp, "<false>"); | |
1578 | } | |
1579 | break; | |
1580 | } | |
1581 | case TYPE_CODE_ENUM: | |
1582 | { | |
1583 | /* no default format for enum */ | |
1584 | longval = value_as_long (arg_val); | |
1585 | len = TYPE_NFIELDS (arg_type); | |
1586 | /* find enum name if possible */ | |
1587 | for (i = 0; i < len; i++) | |
1588 | if (TYPE_FIELD_BITPOS (arg_type, i) == longval) | |
c5aa993b | 1589 | break; /* match -- end loop */ |
9e0b60a8 JM |
1590 | |
1591 | if (i < len) /* enum name found */ | |
1592 | { | |
1593 | char *name = TYPE_FIELD_NAME (arg_type, i); | |
1594 | ||
1595 | strncpy (outp, name, available); | |
1596 | /* in casel available < strlen (name), */ | |
1597 | outp[available] = '\0'; | |
1598 | } | |
1599 | else | |
1600 | { | |
c5aa993b | 1601 | if (available >= 16 /*? */ ) /* overflow protect */ |
d4f3574e | 1602 | sprintf (outp, "%ld", (long) longval); |
9e0b60a8 JM |
1603 | } |
1604 | break; | |
1605 | } | |
1606 | case TYPE_CODE_VOID: | |
1607 | *outp = '\0'; | |
1608 | break; /* void type -- no output */ | |
1609 | default: | |
1610 | error ("bad data type at prompt position %d", | |
1611 | promptp - local_prompt); | |
1612 | break; | |
1613 | } | |
1614 | outp += strlen (outp); | |
1615 | } | |
1616 | } | |
1617 | *outp++ = '\0'; /* terminate prompt string */ | |
1618 | return 1; | |
1619 | } | |
1620 | } | |
1621 | ||
1622 | char * | |
fba45db2 | 1623 | get_prompt (void) |
9e0b60a8 JM |
1624 | { |
1625 | static char buf[MAX_PROMPT_SIZE]; | |
1626 | ||
c5aa993b | 1627 | if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ", |
9e0b60a8 JM |
1628 | RETURN_MASK_ALL)) |
1629 | { | |
c5aa993b | 1630 | return &buf[0]; /* successful formatted prompt */ |
9e0b60a8 JM |
1631 | } |
1632 | else | |
1633 | { | |
1634 | /* Prompt could not be formatted. */ | |
6426a772 | 1635 | if (event_loop_p) |
9e0b60a8 JM |
1636 | return PROMPT (0); |
1637 | else | |
1638 | return gdb_prompt_string; | |
1639 | } | |
1640 | } | |
1641 | ||
1642 | void | |
fba45db2 | 1643 | set_prompt (char *s) |
9e0b60a8 JM |
1644 | { |
1645 | /* ??rehrauer: I don't know why this fails, since it looks as though | |
1646 | assignments to prompt are wrapped in calls to savestring... | |
c5aa993b | 1647 | if (prompt != NULL) |
b8c9b27d | 1648 | xfree (prompt); |
c5aa993b | 1649 | */ |
6426a772 | 1650 | if (event_loop_p) |
9e0b60a8 JM |
1651 | PROMPT (0) = savestring (s, strlen (s)); |
1652 | else | |
1653 | gdb_prompt_string = savestring (s, strlen (s)); | |
1654 | } | |
9e0b60a8 | 1655 | \f |
c5aa993b | 1656 | |
9e0b60a8 JM |
1657 | /* If necessary, make the user confirm that we should quit. Return |
1658 | non-zero if we should quit, zero if we shouldn't. */ | |
1659 | ||
1660 | int | |
fba45db2 | 1661 | quit_confirm (void) |
9e0b60a8 | 1662 | { |
39f77062 | 1663 | if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution) |
9e0b60a8 JM |
1664 | { |
1665 | char *s; | |
1666 | ||
1667 | /* This is something of a hack. But there's no reliable way to | |
c5aa993b JM |
1668 | see if a GUI is running. The `use_windows' variable doesn't |
1669 | cut it. */ | |
9e0b60a8 JM |
1670 | if (init_ui_hook) |
1671 | s = "A debugging session is active.\nDo you still want to close the debugger?"; | |
1672 | else if (attach_flag) | |
1673 | s = "The program is running. Quit anyway (and detach it)? "; | |
1674 | else | |
1675 | s = "The program is running. Exit anyway? "; | |
1676 | ||
c5aa993b | 1677 | if (!query (s)) |
9e0b60a8 JM |
1678 | return 0; |
1679 | } | |
1680 | ||
1681 | return 1; | |
1682 | } | |
1683 | ||
1684 | /* Quit without asking for confirmation. */ | |
1685 | ||
1686 | void | |
fba45db2 | 1687 | quit_force (char *args, int from_tty) |
9e0b60a8 JM |
1688 | { |
1689 | int exit_code = 0; | |
1690 | ||
1691 | /* An optional expression may be used to cause gdb to terminate with the | |
1692 | value of that expression. */ | |
1693 | if (args) | |
1694 | { | |
1695 | value_ptr val = parse_and_eval (args); | |
1696 | ||
1697 | exit_code = (int) value_as_long (val); | |
1698 | } | |
1699 | ||
39f77062 | 1700 | if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution) |
9e0b60a8 JM |
1701 | { |
1702 | if (attach_flag) | |
1703 | target_detach (args, from_tty); | |
1704 | else | |
1705 | target_kill (); | |
1706 | } | |
1707 | ||
1708 | /* UDI wants this, to kill the TIP. */ | |
1709 | target_close (1); | |
1710 | ||
1711 | /* Save the history information if it is appropriate to do so. */ | |
1712 | if (write_history_p && history_filename) | |
1713 | write_history (history_filename); | |
1714 | ||
c5aa993b | 1715 | do_final_cleanups (ALL_CLEANUPS); /* Do any final cleanups before exiting */ |
9e0b60a8 | 1716 | |
9e0b60a8 JM |
1717 | exit (exit_code); |
1718 | } | |
1719 | ||
9e0b60a8 JM |
1720 | /* Returns whether GDB is running on a terminal and whether the user |
1721 | desires that questions be asked of them on that terminal. */ | |
1722 | ||
1723 | int | |
fba45db2 | 1724 | input_from_terminal_p (void) |
9e0b60a8 JM |
1725 | { |
1726 | return gdb_has_a_terminal () && (instream == stdin) & caution; | |
1727 | } | |
1728 | \f | |
9e0b60a8 JM |
1729 | /* ARGSUSED */ |
1730 | static void | |
fba45db2 | 1731 | dont_repeat_command (char *ignored, int from_tty) |
9e0b60a8 | 1732 | { |
c5aa993b JM |
1733 | *line = 0; /* Can't call dont_repeat here because we're not |
1734 | necessarily reading from stdin. */ | |
9e0b60a8 JM |
1735 | } |
1736 | \f | |
1737 | /* Functions to manipulate command line editing control variables. */ | |
1738 | ||
1739 | /* Number of commands to print in each call to show_commands. */ | |
1740 | #define Hist_print 10 | |
d318976c | 1741 | void |
fba45db2 | 1742 | show_commands (char *args, int from_tty) |
9e0b60a8 JM |
1743 | { |
1744 | /* Index for history commands. Relative to history_base. */ | |
1745 | int offset; | |
1746 | ||
1747 | /* Number of the history entry which we are planning to display next. | |
1748 | Relative to history_base. */ | |
1749 | static int num = 0; | |
1750 | ||
1751 | /* The first command in the history which doesn't exist (i.e. one more | |
1752 | than the number of the last command). Relative to history_base. */ | |
1753 | int hist_len; | |
1754 | ||
a14ed312 | 1755 | extern HIST_ENTRY *history_get (int); |
9e0b60a8 JM |
1756 | |
1757 | /* Print out some of the commands from the command history. */ | |
1758 | /* First determine the length of the history list. */ | |
1759 | hist_len = history_size; | |
1760 | for (offset = 0; offset < history_size; offset++) | |
1761 | { | |
1762 | if (!history_get (history_base + offset)) | |
1763 | { | |
1764 | hist_len = offset; | |
1765 | break; | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | if (args) | |
1770 | { | |
1771 | if (args[0] == '+' && args[1] == '\0') | |
1772 | /* "info editing +" should print from the stored position. */ | |
1773 | ; | |
1774 | else | |
1775 | /* "info editing <exp>" should print around command number <exp>. */ | |
0e828ed1 | 1776 | num = (parse_and_eval_long (args) - history_base) - Hist_print / 2; |
9e0b60a8 JM |
1777 | } |
1778 | /* "show commands" means print the last Hist_print commands. */ | |
1779 | else | |
1780 | { | |
1781 | num = hist_len - Hist_print; | |
1782 | } | |
1783 | ||
1784 | if (num < 0) | |
1785 | num = 0; | |
1786 | ||
1787 | /* If there are at least Hist_print commands, we want to display the last | |
1788 | Hist_print rather than, say, the last 6. */ | |
1789 | if (hist_len - num < Hist_print) | |
1790 | { | |
1791 | num = hist_len - Hist_print; | |
1792 | if (num < 0) | |
1793 | num = 0; | |
1794 | } | |
1795 | ||
1796 | for (offset = num; offset < num + Hist_print && offset < hist_len; offset++) | |
1797 | { | |
1798 | printf_filtered ("%5d %s\n", history_base + offset, | |
c5aa993b | 1799 | (history_get (history_base + offset))->line); |
9e0b60a8 JM |
1800 | } |
1801 | ||
1802 | /* The next command we want to display is the next one that we haven't | |
1803 | displayed yet. */ | |
1804 | num += Hist_print; | |
1805 | ||
1806 | /* If the user repeats this command with return, it should do what | |
1807 | "show commands +" does. This is unnecessary if arg is null, | |
1808 | because "show commands +" is not useful after "show commands". */ | |
1809 | if (from_tty && args) | |
1810 | { | |
1811 | args[0] = '+'; | |
1812 | args[1] = '\0'; | |
1813 | } | |
1814 | } | |
1815 | ||
1816 | /* Called by do_setshow_command. */ | |
1817 | /* ARGSUSED */ | |
1818 | static void | |
fba45db2 | 1819 | set_history_size_command (char *args, int from_tty, struct cmd_list_element *c) |
9e0b60a8 JM |
1820 | { |
1821 | if (history_size == INT_MAX) | |
1822 | unstifle_history (); | |
1823 | else if (history_size >= 0) | |
1824 | stifle_history (history_size); | |
1825 | else | |
1826 | { | |
1827 | history_size = INT_MAX; | |
1828 | error ("History size must be non-negative"); | |
1829 | } | |
1830 | } | |
1831 | ||
1832 | /* ARGSUSED */ | |
d318976c | 1833 | void |
fba45db2 | 1834 | set_history (char *args, int from_tty) |
9e0b60a8 JM |
1835 | { |
1836 | printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n"); | |
1837 | help_list (sethistlist, "set history ", -1, gdb_stdout); | |
1838 | } | |
1839 | ||
1840 | /* ARGSUSED */ | |
d318976c | 1841 | void |
fba45db2 | 1842 | show_history (char *args, int from_tty) |
9e0b60a8 JM |
1843 | { |
1844 | cmd_show_list (showhistlist, from_tty, ""); | |
1845 | } | |
1846 | ||
1847 | int info_verbose = 0; /* Default verbose msgs off */ | |
1848 | ||
1849 | /* Called by do_setshow_command. An elaborate joke. */ | |
1850 | /* ARGSUSED */ | |
d318976c | 1851 | void |
fba45db2 | 1852 | set_verbose (char *args, int from_tty, struct cmd_list_element *c) |
9e0b60a8 JM |
1853 | { |
1854 | char *cmdname = "verbose"; | |
1855 | struct cmd_list_element *showcmd; | |
1856 | ||
1857 | showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1); | |
1858 | ||
1859 | if (info_verbose) | |
1860 | { | |
1861 | c->doc = "Set verbose printing of informational messages."; | |
1862 | showcmd->doc = "Show verbose printing of informational messages."; | |
1863 | } | |
1864 | else | |
1865 | { | |
1866 | c->doc = "Set verbosity."; | |
1867 | showcmd->doc = "Show verbosity."; | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | static void | |
fba45db2 | 1872 | float_handler (int signo) |
9e0b60a8 JM |
1873 | { |
1874 | /* This message is based on ANSI C, section 4.7. Note that integer | |
1875 | divide by zero causes this, so "float" is a misnomer. */ | |
1876 | signal (SIGFPE, float_handler); | |
1877 | error ("Erroneous arithmetic operation."); | |
1878 | } | |
5d161b24 | 1879 | |
9e0b60a8 JM |
1880 | /* Init the history buffer. Note that we are called after the init file(s) |
1881 | * have been read so that the user can change the history file via his | |
1882 | * .gdbinit file (for instance). The GDBHISTFILE environment variable | |
1883 | * overrides all of this. | |
1884 | */ | |
1885 | ||
1886 | void | |
fba45db2 | 1887 | init_history (void) |
9e0b60a8 JM |
1888 | { |
1889 | char *tmpenv; | |
1890 | ||
1891 | tmpenv = getenv ("HISTSIZE"); | |
1892 | if (tmpenv) | |
1893 | history_size = atoi (tmpenv); | |
1894 | else if (!history_size) | |
1895 | history_size = 256; | |
1896 | ||
1897 | stifle_history (history_size); | |
1898 | ||
1899 | tmpenv = getenv ("GDBHISTFILE"); | |
1900 | if (tmpenv) | |
c5aa993b JM |
1901 | history_filename = savestring (tmpenv, strlen (tmpenv)); |
1902 | else if (!history_filename) | |
1903 | { | |
1904 | /* We include the current directory so that if the user changes | |
1905 | directories the file written will be the same as the one | |
1906 | that was read. */ | |
a0b3c4fd | 1907 | #ifdef __MSDOS__ |
eb2f494a AC |
1908 | /* No leading dots in file names are allowed on MSDOS. */ |
1909 | history_filename = concat (current_directory, "/_gdb_history", NULL); | |
a0b3c4fd | 1910 | #else |
c5aa993b | 1911 | history_filename = concat (current_directory, "/.gdb_history", NULL); |
a0b3c4fd | 1912 | #endif |
c5aa993b | 1913 | } |
9e0b60a8 JM |
1914 | read_history (history_filename); |
1915 | } | |
1916 | ||
1917 | static void | |
fba45db2 | 1918 | init_main (void) |
9e0b60a8 JM |
1919 | { |
1920 | struct cmd_list_element *c; | |
1921 | ||
1922 | /* If we are running the asynchronous version, | |
1923 | we initialize the prompts differently. */ | |
6426a772 | 1924 | if (!event_loop_p) |
9e0b60a8 | 1925 | { |
c5aa993b | 1926 | gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT)); |
9e0b60a8 JM |
1927 | } |
1928 | else | |
1929 | { | |
1930 | /* initialize the prompt stack to a simple "(gdb) " prompt or to | |
96baa820 | 1931 | whatever the DEFAULT_PROMPT is. */ |
9e0b60a8 | 1932 | the_prompts.top = 0; |
c5aa993b | 1933 | PREFIX (0) = ""; |
c5aa993b | 1934 | PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT)); |
c5aa993b | 1935 | SUFFIX (0) = ""; |
9e0b60a8 | 1936 | /* Set things up for annotation_level > 1, if the user ever decides |
c5aa993b | 1937 | to use it. */ |
9e0b60a8 JM |
1938 | async_annotation_suffix = "prompt"; |
1939 | /* Set the variable associated with the setshow prompt command. */ | |
1940 | new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0))); | |
0191bed7 EZ |
1941 | |
1942 | /* If gdb was started with --annotate=2, this is equivalent to | |
1943 | the user entering the command 'set annotate 2' at the gdb | |
1944 | prompt, so we need to do extra processing. */ | |
1945 | if (annotation_level > 1) | |
1946 | set_async_annotation_level (NULL, 0, NULL); | |
9e0b60a8 JM |
1947 | } |
1948 | gdb_prompt_escape = 0; /* default to none. */ | |
1949 | ||
1950 | /* Set the important stuff up for command editing. */ | |
1951 | command_editing_p = 1; | |
9e0b60a8 JM |
1952 | history_expansion_p = 0; |
1953 | write_history_p = 0; | |
1954 | ||
1955 | /* Setup important stuff for command line editing. */ | |
1956 | rl_completion_entry_function = (int (*)()) readline_line_completion_function; | |
d318976c FN |
1957 | rl_completer_word_break_characters = |
1958 | get_gdb_completer_word_break_characters (); | |
1959 | rl_completer_quote_characters = get_gdb_completer_quote_characters (); | |
9e0b60a8 JM |
1960 | rl_readline_name = "gdb"; |
1961 | ||
9e0b60a8 JM |
1962 | /* The set prompt command is different depending whether or not the |
1963 | async version is run. NOTE: this difference is going to | |
1964 | disappear as we make the event loop be the default engine of | |
1965 | gdb. */ | |
6426a772 | 1966 | if (!event_loop_p) |
9e0b60a8 JM |
1967 | { |
1968 | add_show_from_set | |
c5aa993b | 1969 | (add_set_cmd ("prompt", class_support, var_string, |
9e0b60a8 JM |
1970 | (char *) &gdb_prompt_string, "Set gdb's prompt", |
1971 | &setlist), | |
1972 | &showlist); | |
1973 | } | |
1974 | else | |
1975 | { | |
c5aa993b JM |
1976 | c = add_set_cmd ("prompt", class_support, var_string, |
1977 | (char *) &new_async_prompt, "Set gdb's prompt", | |
9e0b60a8 JM |
1978 | &setlist); |
1979 | add_show_from_set (c, &showlist); | |
1980 | c->function.sfunc = set_async_prompt; | |
1981 | } | |
1982 | ||
1983 | add_show_from_set | |
c5aa993b | 1984 | (add_set_cmd ("prompt-escape-char", class_support, var_zinteger, |
9e0b60a8 JM |
1985 | (char *) &gdb_prompt_escape, |
1986 | "Set escape character for formatting of gdb's prompt", | |
1987 | &setlist), | |
1988 | &showlist); | |
1989 | ||
9e0b60a8 JM |
1990 | add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\ |
1991 | Primarily used inside of user-defined commands that should not be repeated when\n\ | |
1992 | hitting return."); | |
1993 | ||
9e0b60a8 JM |
1994 | /* The set editing command is different depending whether or not the |
1995 | async version is run. NOTE: this difference is going to disappear | |
1996 | as we make the event loop be the default engine of gdb. */ | |
6426a772 | 1997 | if (!event_loop_p) |
9e0b60a8 JM |
1998 | { |
1999 | add_show_from_set | |
c5aa993b | 2000 | (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p, |
9e0b60a8 JM |
2001 | "Set editing of command lines as they are typed.\n\ |
2002 | Use \"on\" to enable the editing, and \"off\" to disable it.\n\ | |
2003 | Without an argument, command line editing is enabled. To edit, use\n\ | |
2004 | EMACS-like or VI-like commands like control-P or ESC.", &setlist), | |
2005 | &showlist); | |
2006 | } | |
2007 | else | |
2008 | { | |
c5aa993b | 2009 | c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p, |
9e0b60a8 JM |
2010 | "Set editing of command lines as they are typed.\n\ |
2011 | Use \"on\" to enable the editing, and \"off\" to disable it.\n\ | |
2012 | Without an argument, command line editing is enabled. To edit, use\n\ | |
2013 | EMACS-like or VI-like commands like control-P or ESC.", &setlist); | |
2014 | ||
2015 | add_show_from_set (c, &showlist); | |
2016 | c->function.sfunc = set_async_editing_command; | |
2017 | } | |
2018 | ||
9e0b60a8 | 2019 | add_show_from_set |
c5aa993b JM |
2020 | (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p, |
2021 | "Set saving of the history record on exit.\n\ | |
9e0b60a8 JM |
2022 | Use \"on\" to enable the saving, and \"off\" to disable it.\n\ |
2023 | Without an argument, saving is enabled.", &sethistlist), | |
2024 | &showhistlist); | |
2025 | ||
c5aa993b | 2026 | c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size, |
9e0b60a8 JM |
2027 | "Set the size of the command history, \n\ |
2028 | ie. the number of previous commands to keep a record of.", &sethistlist); | |
2029 | add_show_from_set (c, &showhistlist); | |
2030 | c->function.sfunc = set_history_size_command; | |
2031 | ||
7a1bd56a EZ |
2032 | c = add_set_cmd ("filename", no_class, var_filename, |
2033 | (char *) &history_filename, | |
2034 | "Set the filename in which to record the command history\n\ | |
2035 | (the list of previous commands of which a record is kept).", &sethistlist); | |
2036 | c->completer = filename_completer; | |
2037 | add_show_from_set (c, &showhistlist); | |
9e0b60a8 JM |
2038 | |
2039 | add_show_from_set | |
2040 | (add_set_cmd ("confirm", class_support, var_boolean, | |
c5aa993b | 2041 | (char *) &caution, |
9e0b60a8 JM |
2042 | "Set whether to confirm potentially dangerous operations.", |
2043 | &setlist), | |
2044 | &showlist); | |
2045 | ||
9e0b60a8 JM |
2046 | /* The set annotate command is different depending whether or not |
2047 | the async version is run. NOTE: this difference is going to | |
2048 | disappear as we make the event loop be the default engine of | |
2049 | gdb. */ | |
6426a772 | 2050 | if (!event_loop_p) |
9e0b60a8 | 2051 | { |
c5aa993b JM |
2052 | c = add_set_cmd ("annotate", class_obscure, var_zinteger, |
2053 | (char *) &annotation_level, "Set annotation_level.\n\ | |
9e0b60a8 JM |
2054 | 0 == normal; 1 == fullname (for use when running under emacs)\n\ |
2055 | 2 == output annotated suitably for use by programs that control GDB.", | |
2056 | &setlist); | |
2057 | c = add_show_from_set (c, &showlist); | |
2058 | } | |
2059 | else | |
2060 | { | |
c5aa993b JM |
2061 | c = add_set_cmd ("annotate", class_obscure, var_zinteger, |
2062 | (char *) &annotation_level, "Set annotation_level.\n\ | |
9e0b60a8 JM |
2063 | 0 == normal; 1 == fullname (for use when running under emacs)\n\ |
2064 | 2 == output annotated suitably for use by programs that control GDB.", | |
c5aa993b | 2065 | &setlist); |
9e0b60a8 JM |
2066 | add_show_from_set (c, &showlist); |
2067 | c->function.sfunc = set_async_annotation_level; | |
2068 | } | |
6426a772 | 2069 | if (event_loop_p) |
104c1213 JM |
2070 | { |
2071 | add_show_from_set | |
2072 | (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p, | |
2073 | "Set notification of completion for asynchronous execution commands.\n\ | |
2074 | Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist), | |
2075 | &showlist); | |
2076 | } | |
9e0b60a8 | 2077 | } |