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