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