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