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