1 /* Top level stuff for GDB, the GNU debugger.
2 Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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, Boston, MA 02111-1307, USA. */
23 #include "call-cmds.h"
28 #include "breakpoint.h"
30 #include "expression.h"
33 #include "terminal.h" /* For job_control. */
36 #include "event-loop.h"
38 /* readline include files */
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 /* readline defines this. */
45 #include <sys/types.h>
50 #include "gdb_string.h"
54 /* Prototypes for local functions */
56 static void dont_repeat_command
PARAMS ((char *, int));
58 static void source_cleanup_lines
PARAMS ((PTR
));
60 static void user_defined_command
PARAMS ((char *, int));
62 static void init_signals
PARAMS ((void));
65 static void stop_sig
PARAMS ((int));
68 static char * line_completion_function
PARAMS ((char *, int, char *, int));
70 static char * readline_line_completion_function
PARAMS ((char *, int));
72 /* NOTE 1999-04-29: this function will be static again, after we make the
73 event loop be the default command loop for gdb, and we merge
74 event-top.c into this file, top.c */
75 /* static */ void command_loop_marker
PARAMS ((int));
77 static void while_command
PARAMS ((char *, int));
79 static void if_command
PARAMS ((char *, int));
81 static struct command_line
*
82 build_command_line
PARAMS ((enum command_control_type
, char *));
84 static struct command_line
*
85 get_command_line
PARAMS ((enum command_control_type
, char *));
87 static void realloc_body_list
PARAMS ((struct command_line
*, int));
89 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
91 static enum command_control_type
92 recurse_read_control_structure
PARAMS ((struct command_line
*));
94 static struct cleanup
* setup_user_args
PARAMS ((char *));
96 static char * locate_arg
PARAMS ((char *));
98 static char * insert_args
PARAMS ((char *));
100 static void arg_cleanup
PARAMS ((void));
102 static void init_main
PARAMS ((void));
104 static void init_cmd_lists
PARAMS ((void));
106 static void float_handler
PARAMS ((int));
108 static void init_signals
PARAMS ((void));
110 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
112 static void show_history
PARAMS ((char *, int));
114 static void set_history
PARAMS ((char *, int));
116 static void set_history_size_command
PARAMS ((char *, int,
117 struct cmd_list_element
*));
119 static void show_commands
PARAMS ((char *, int));
121 static void echo_command
PARAMS ((char *, int));
123 static void pwd_command
PARAMS ((char *, int));
125 static void show_version
PARAMS ((char *, int));
127 static void document_command
PARAMS ((char *, int));
129 static void define_command
PARAMS ((char *, int));
131 static void validate_comname
PARAMS ((char *));
133 static void help_command
PARAMS ((char *, int));
135 static void show_command
PARAMS ((char *, int));
137 static void info_command
PARAMS ((char *, int));
139 static void complete_command
PARAMS ((char *, int));
141 static void do_nothing
PARAMS ((int));
144 /* NOTE 1999-04-29: This function will be static again, once we modify
145 gdb to use the event loop as the default command loop and we merge
146 event-top.c into this file, top.c */
147 /* static */ int quit_cover
PARAMS ((PTR
));
149 static void disconnect
PARAMS ((int));
152 static void source_cleanup
PARAMS ((FILE *));
154 /* If this definition isn't overridden by the header files, assume
155 that isatty and fileno exist on this system. */
157 #define ISATTY(FP) (isatty (fileno (FP)))
160 /* Initialization file name for gdb. This is overridden in some configs. */
162 #ifndef GDBINIT_FILENAME
163 #define GDBINIT_FILENAME ".gdbinit"
165 char gdbinit
[] = GDBINIT_FILENAME
;
167 int inhibit_gdbinit
= 0;
169 /* If nonzero, and GDB has been configured to be able to use windows,
170 attempt to open them upon startup. */
174 /* Version number of GDB, as a string. */
176 extern char *version
;
178 /* Canonical host name as a string. */
180 extern char *host_name
;
182 /* Canonical target name as a string. */
184 extern char *target_name
;
186 extern char lang_frame_mismatch_warn
[]; /* language.c */
188 /* Flag for whether we want all the "from_tty" gubbish printed. */
190 int caution
= 1; /* Default is yes, sigh. */
192 /* Define all cmd_list_elements. */
194 /* Chain containing all defined commands. */
196 struct cmd_list_element
*cmdlist
;
198 /* Chain containing all defined info subcommands. */
200 struct cmd_list_element
*infolist
;
202 /* Chain containing all defined enable subcommands. */
204 struct cmd_list_element
*enablelist
;
206 /* Chain containing all defined disable subcommands. */
208 struct cmd_list_element
*disablelist
;
210 /* Chain containing all defined toggle subcommands. */
212 struct cmd_list_element
*togglelist
;
214 /* Chain containing all defined stop subcommands. */
216 struct cmd_list_element
*stoplist
;
218 /* Chain containing all defined delete subcommands. */
220 struct cmd_list_element
*deletelist
;
222 /* Chain containing all defined "enable breakpoint" subcommands. */
224 struct cmd_list_element
*enablebreaklist
;
226 /* Chain containing all defined set subcommands */
228 struct cmd_list_element
*setlist
;
230 /* Chain containing all defined unset subcommands */
232 struct cmd_list_element
*unsetlist
;
234 /* Chain containing all defined show subcommands. */
236 struct cmd_list_element
*showlist
;
238 /* Chain containing all defined \"set history\". */
240 struct cmd_list_element
*sethistlist
;
242 /* Chain containing all defined \"show history\". */
244 struct cmd_list_element
*showhistlist
;
246 /* Chain containing all defined \"unset history\". */
248 struct cmd_list_element
*unsethistlist
;
250 /* Chain containing all defined maintenance subcommands. */
252 struct cmd_list_element
*maintenancelist
;
254 /* Chain containing all defined "maintenance info" subcommands. */
256 struct cmd_list_element
*maintenanceinfolist
;
258 /* Chain containing all defined "maintenance print" subcommands. */
260 struct cmd_list_element
*maintenanceprintlist
;
262 struct cmd_list_element
*setprintlist
;
264 struct cmd_list_element
*showprintlist
;
266 struct cmd_list_element
*setchecklist
;
268 struct cmd_list_element
*showchecklist
;
270 /* stdio stream that command input is being read from. Set to stdin normally.
271 Set by source_command to the file we are sourcing. Set to NULL if we are
272 executing a user-defined command or interacting via a GUI. */
276 /* Current working directory. */
278 char *current_directory
;
280 /* The directory name is actually stored here (usually). */
281 char gdb_dirbuf
[1024];
283 /* Function to call before reading a command, if nonzero.
284 The function receives two args: an input stream,
285 and a prompt string. */
287 void (*window_hook
) PARAMS ((FILE *, char *));
292 /* gdb prints this when reading a command interactively */
295 /* Buffer used for reading command lines, and the size
296 allocated for it so far. */
301 /* Nonzero if the current command is modified by "server ". This
302 affects things like recording into the command history, comamnds
303 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
304 whatever) can issue its own commands and also send along commands
305 from the user, and have the user not notice that the user interface
306 is issuing commands too. */
309 /* Baud rate specified for talking to serial target systems. Default
310 is left as -1, so targets can choose their own defaults. */
311 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
312 or (unsigned int)-1. This is a Bad User Interface. */
316 /* Timeout limit for response from target. */
318 int remote_timeout
= 20; /* Set default to 20 */
320 /* Non-zero tells remote* modules to output debugging info. */
322 int remote_debug
= 0;
324 /* Level of control structure. */
325 static int control_level
;
327 /* Structure for arguments to user defined functions. */
328 #define MAXUSERARGS 10
331 struct user_args
*next
;
340 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
344 #define STOP_SIGNAL SIGTSTP
345 static void stop_sig
PARAMS ((int));
349 /* Some System V have job control but not sigsetmask(). */
350 #if !defined (HAVE_SIGSETMASK)
352 #define HAVE_SIGSETMASK 1
354 #define HAVE_SIGSETMASK 0
358 #if 0 == (HAVE_SIGSETMASK)
359 #define sigsetmask(n)
362 /* Hooks for alternate command interfaces. */
364 /* Called after most modules have been initialized, but before taking users
367 void (*init_ui_hook
) PARAMS ((char *argv0
));
369 /* This hook is called from within gdb's many mini-event loops which could
370 steal control from a real user interface's event loop. It returns
371 non-zero if the user is requesting a detach, zero otherwise. */
373 int (*ui_loop_hook
) PARAMS ((int));
375 /* Called instead of command_loop at top level. Can be invoked via
376 return_to_top_level. */
378 void (*command_loop_hook
) PARAMS ((void));
381 /* Called instead of fputs for all output. */
383 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, GDB_FILE
*stream
));
385 /* Called when the target says something to the host, which may
386 want to appear in a different window. */
388 void (*target_output_hook
) PARAMS ((char *));
390 /* Called from print_frame_info to list the line we stopped in. */
392 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
393 int stopline
, int noerror
));
394 /* Replaces most of query. */
396 int (*query_hook
) PARAMS ((const char *, va_list));
398 /* Replaces most of warning. */
400 void (*warning_hook
) PARAMS ((const char *, va_list));
402 /* Called from gdb_flush to flush output. */
404 void (*flush_hook
) PARAMS ((GDB_FILE
*stream
));
406 /* These three functions support getting lines of text from the user. They
407 are used in sequence. First readline_begin_hook is called with a text
408 string that might be (for example) a message for the user to type in a
409 sequence of commands to be executed at a breakpoint. If this function
410 calls back to a GUI, it might take this opportunity to pop up a text
411 interaction window with this message. Next, readline_hook is called
412 with a prompt that is emitted prior to collecting the user input.
413 It can be called multiple times. Finally, readline_end_hook is called
414 to notify the GUI that we are done with the interaction window and it
417 void (*readline_begin_hook
) PARAMS ((char *, ...));
418 char * (*readline_hook
) PARAMS ((char *));
419 void (*readline_end_hook
) PARAMS ((void));
421 /* Called as appropriate to notify the interface of the specified breakpoint
424 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
425 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
426 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
428 /* Called during long calculations to allow GUI to repair window damage, and to
429 check for stop buttons, etc... */
431 void (*interactive_hook
) PARAMS ((void));
433 /* Called when the registers have changed, as a hint to a GUI
434 to minimize window update. */
436 void (*registers_changed_hook
) PARAMS ((void));
438 /* Tell the GUI someone changed the register REGNO. -1 means
439 that the caller does not know which register changed or
440 that several registers have changed (see value_assign).*/
441 void (*register_changed_hook
) PARAMS ((int regno
));
443 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
444 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
446 /* Called when going to wait for the target. Usually allows the GUI to run
447 while waiting for target events. */
449 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
451 /* Used by UI as a wrapper around command execution. May do various things
452 like enabling/disabling buttons, etc... */
454 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
457 /* Called when the current thread changes. Argument is thread id. */
459 void (*context_hook
) PARAMS ((int id
));
461 /* Takes control from error (). Typically used to prevent longjmps out of the
462 middle of the GUI. Usually used in conjunction with a catch routine. */
464 NORETURN
void (*error_hook
) PARAMS ((void)) ATTR_NORETURN
;
467 /* Where to go for return_to_top_level (RETURN_ERROR). */
468 SIGJMP_BUF error_return
;
469 /* Where to go for return_to_top_level (RETURN_QUIT). */
470 SIGJMP_BUF quit_return
;
472 /* Return for reason REASON. This generally gets back to the command
473 loop, but can be caught via catch_errors. */
476 return_to_top_level (reason
)
477 enum return_reason reason
;
482 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
483 I can think of a reason why that is vital, though). */
484 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
486 disable_current_display ();
487 do_cleanups (ALL_CLEANUPS
);
489 if (annotation_level
> 1)
500 (NORETURN
void) SIGLONGJMP
501 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
504 /* Call FUNC with arg ARGS, catching any errors. If there is no
505 error, return the value returned by FUNC. If there is an error,
506 print ERRSTRING, print the specific error message, then return
509 Must not be called with immediate_quit in effect (bad things might
510 happen, say we got a signal in the middle of a memcpy to quit_return).
511 This is an OK restriction; with very few exceptions immediate_quit can
512 be replaced by judicious use of QUIT.
514 MASK specifies what to catch; it is normally set to
515 RETURN_MASK_ALL, if for no other reason than that the code which
516 calls catch_errors might not be set up to deal with a quit which
517 isn't caught. But if the code can deal with it, it generally
518 should be RETURN_MASK_ERROR, unless for some reason it is more
519 useful to abort only the portion of the operation inside the
520 catch_errors. Note that quit should return to the command line
521 fairly quickly, even if some further processing is being done. */
524 catch_errors (func
, args
, errstring
, mask
)
525 catch_errors_ftype
*func
;
530 SIGJMP_BUF saved_error
;
531 SIGJMP_BUF saved_quit
;
534 struct cleanup
*saved_cleanup_chain
;
535 char *saved_error_pre_print
;
536 char *saved_quit_pre_print
;
538 saved_cleanup_chain
= save_cleanups ();
539 saved_error_pre_print
= error_pre_print
;
540 saved_quit_pre_print
= quit_pre_print
;
542 if (mask
& RETURN_MASK_ERROR
)
544 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (SIGJMP_BUF
));
545 error_pre_print
= errstring
;
547 if (mask
& RETURN_MASK_QUIT
)
549 memcpy (saved_quit
, quit_return
, sizeof (SIGJMP_BUF
));
550 quit_pre_print
= errstring
;
553 if (SIGSETJMP (tmp_jmp
) == 0)
555 if (mask
& RETURN_MASK_ERROR
)
556 memcpy (error_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
557 if (mask
& RETURN_MASK_QUIT
)
558 memcpy (quit_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
559 val
= (*func
) (args
);
564 restore_cleanups (saved_cleanup_chain
);
566 if (mask
& RETURN_MASK_ERROR
)
568 memcpy (error_return
, saved_error
, sizeof (SIGJMP_BUF
));
569 error_pre_print
= saved_error_pre_print
;
571 if (mask
& RETURN_MASK_QUIT
)
573 memcpy (quit_return
, saved_quit
, sizeof (SIGJMP_BUF
));
574 quit_pre_print
= saved_quit_pre_print
;
579 /* Handler for SIGHUP. */
586 catch_errors (quit_cover
, NULL
,
587 "Could not kill the program being debugged", RETURN_MASK_ALL
);
588 signal (SIGHUP
, SIG_DFL
);
589 kill (getpid (), SIGHUP
);
592 /* Just a little helper function for disconnect(). */
594 /* NOTE 1999-04-29: This function will be static again, once we modify
595 gdb to use the event loop as the default command loop and we merge
596 event-top.c into this file, top.c */
601 caution
= 0; /* Throw caution to the wind -- we're exiting.
602 This prevents asking the user dumb questions. */
603 quit_command((char *)0, 0);
606 #endif /* defined SIGHUP */
608 /* Line number we are currently in in a file which is being sourced. */
609 /* NOTE 1999-04-29: This variable will be static again, once we modify
610 gdb to use the event loop as the default command loop and we merge
611 event-top.c into this file, top.c */
612 /* static */ int source_line_number
;
614 /* Name of the file we are sourcing. */
615 /* NOTE 1999-04-29: This variable will be static again, once we modify
616 gdb to use the event loop as the default command loop and we merge
617 event-top.c into this file, top.c */
618 /* static */ char *source_file_name
;
620 /* Buffer containing the error_pre_print used by the source stuff.
622 /* NOTE 1999-04-29: This variable will be static again, once we modify
623 gdb to use the event loop as the default command loop and we merge
624 event-top.c into this file, top.c */
625 /* static */ char *source_error
;
626 static int source_error_allocated
;
628 /* Something to glom on to the start of error_pre_print if source_file_name
630 /* NOTE 1999-04-29: This variable will be static again, once we modify
631 gdb to use the event loop as the default command loop and we merge
632 event-top.c into this file, top.c */
633 /* static */ char *source_pre_error
;
635 /* Clean up on error during a "source" command (or execution of a
636 user-defined command). */
639 source_cleanup (stream
)
642 /* Restore the previous input stream. */
646 /* Read commands from STREAM. */
648 read_command_file (stream
)
651 struct cleanup
*cleanups
;
653 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
656 do_cleanups (cleanups
);
659 extern void init_proc
PARAMS ((void));
661 void (*pre_init_ui_hook
) PARAMS ((void));
667 if (pre_init_ui_hook
)
670 /* Run the init function of each source file */
672 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
673 current_directory
= gdb_dirbuf
;
675 init_cmd_lists (); /* This needs to be done first */
676 initialize_targets (); /* Setup target_terminal macros for utils.c */
677 initialize_utils (); /* Make errors and warnings possible */
678 initialize_all_files ();
679 init_main (); /* But that omits this file! Do it now */
681 /* The signal handling mechanism is different depending whether or
682 not the async version is run. NOTE: in the future we plan to make
683 the event loop be the default engine of gdb, and this difference
686 async_init_signals ();
690 /* We need a default language for parsing expressions, so simple things like
691 "set width 0" won't fail if no language is explicitly set in a config file
692 or implicitly set by reading an executable during startup. */
693 set_language (language_c
);
694 expected_language
= current_language
; /* don't warn about the change. */
697 init_ui_hook (argv0
);
700 /* Allocate, initialize a new command line structure for one of the
701 control commands (if/while). */
703 static struct command_line
*
704 build_command_line (type
, args
)
705 enum command_control_type type
;
708 struct command_line
*cmd
;
711 error ("if/while commands require arguments.\n");
713 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
715 cmd
->control_type
= type
;
719 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
721 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
722 cmd
->line
= savestring (args
, strlen (args
));
726 /* Build and return a new command structure for the control commands
727 such as "if" and "while". */
729 static struct command_line
*
730 get_command_line (type
, arg
)
731 enum command_control_type type
;
734 struct command_line
*cmd
;
735 struct cleanup
*old_chain
= NULL
;
737 /* Allocate and build a new command line structure. */
738 cmd
= build_command_line (type
, arg
);
740 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
742 /* Read in the body of this command. */
743 if (recurse_read_control_structure (cmd
) == invalid_control
)
745 warning ("error reading in control structure\n");
746 do_cleanups (old_chain
);
750 discard_cleanups (old_chain
);
754 /* Recursively print a command (including full control structures). */
756 print_command_line (cmd
, depth
, stream
)
757 struct command_line
*cmd
;
765 for (i
= 0; i
< depth
; i
++)
766 fputs_filtered (" ", stream
);
769 /* A simple command, print it and return. */
770 if (cmd
->control_type
== simple_control
)
772 fputs_filtered (cmd
->line
, stream
);
773 fputs_filtered ("\n", stream
);
777 /* loop_continue to jump to the start of a while loop, print it
779 if (cmd
->control_type
== continue_control
)
781 fputs_filtered ("loop_continue\n", stream
);
785 /* loop_break to break out of a while loop, print it and return. */
786 if (cmd
->control_type
== break_control
)
788 fputs_filtered ("loop_break\n", stream
);
792 /* A while command. Recursively print its subcommands before returning. */
793 if (cmd
->control_type
== while_control
)
795 struct command_line
*list
;
796 fputs_filtered ("while ", stream
);
797 fputs_filtered (cmd
->line
, stream
);
798 fputs_filtered ("\n", stream
);
799 list
= *cmd
->body_list
;
802 print_command_line (list
, depth
+ 1, stream
);
807 /* An if command. Recursively print both arms before returning. */
808 if (cmd
->control_type
== if_control
)
810 fputs_filtered ("if ", stream
);
811 fputs_filtered (cmd
->line
, stream
);
812 fputs_filtered ("\n", stream
);
814 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
816 /* Show the false arm if it exists. */
817 if (cmd
->body_count
== 2)
821 for (i
= 0; i
< depth
; i
++)
822 fputs_filtered (" ", stream
);
824 fputs_filtered ("else\n", stream
);
825 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
829 for (i
= 0; i
< depth
; i
++)
830 fputs_filtered (" ", stream
);
832 fputs_filtered ("end\n", stream
);
836 /* Execute the command in CMD. */
838 enum command_control_type
839 execute_control_command (cmd
)
840 struct command_line
*cmd
;
842 struct expression
*expr
;
843 struct command_line
*current
;
844 struct cleanup
*old_chain
= 0;
848 enum command_control_type ret
;
851 switch (cmd
->control_type
)
854 /* A simple command, execute it and return. */
855 new_line
= insert_args (cmd
->line
);
857 return invalid_control
;
858 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
860 execute_command (new_line
, 0);
861 ret
= cmd
->control_type
;
864 case continue_control
:
866 /* Return for "continue", and "break" so we can either
867 continue the loop at the top, or break out. */
868 ret
= cmd
->control_type
;
873 /* Parse the loop control expression for the while statement. */
874 new_line
= insert_args (cmd
->line
);
876 return invalid_control
;
877 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
879 expr
= parse_expression (new_line
);
880 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
882 ret
= simple_control
;
885 /* Keep iterating so long as the expression is true. */
892 /* Evaluate the expression. */
893 val_mark
= value_mark ();
894 val
= evaluate_expression (expr
);
895 cond_result
= value_true (val
);
896 value_free_to_mark (val_mark
);
898 /* If the value is false, then break out of the loop. */
902 /* Execute the body of the while statement. */
903 current
= *cmd
->body_list
;
906 ret
= execute_control_command (current
);
908 /* If we got an error, or a "break" command, then stop
910 if (ret
== invalid_control
|| ret
== break_control
)
916 /* If we got a "continue" command, then restart the loop
918 if (ret
== continue_control
)
921 /* Get the next statement. */
922 current
= current
->next
;
926 /* Reset RET so that we don't recurse the break all the way down. */
927 if (ret
== break_control
)
928 ret
= simple_control
;
935 new_line
= insert_args (cmd
->line
);
937 return invalid_control
;
938 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
940 /* Parse the conditional for the if statement. */
941 expr
= parse_expression (new_line
);
942 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
945 ret
= simple_control
;
947 /* Evaluate the conditional. */
948 val_mark
= value_mark ();
949 val
= evaluate_expression (expr
);
951 /* Choose which arm to take commands from based on the value of the
952 conditional expression. */
953 if (value_true (val
))
954 current
= *cmd
->body_list
;
955 else if (cmd
->body_count
== 2)
956 current
= *(cmd
->body_list
+ 1);
957 value_free_to_mark (val_mark
);
959 /* Execute commands in the given arm. */
962 ret
= execute_control_command (current
);
964 /* If we got an error, get out. */
965 if (ret
!= simple_control
)
968 /* Get the next statement in the body. */
969 current
= current
->next
;
976 warning ("Invalid control type in command structure.");
977 return invalid_control
;
981 do_cleanups (old_chain
);
986 /* "while" command support. Executes a body of statements while the
987 loop condition is nonzero. */
990 while_command (arg
, from_tty
)
994 struct command_line
*command
= NULL
;
997 command
= get_command_line (while_control
, arg
);
1002 execute_control_command (command
);
1003 free_command_lines (&command
);
1006 /* "if" command support. Execute either the true or false arm depending
1007 on the value of the if conditional. */
1010 if_command (arg
, from_tty
)
1014 struct command_line
*command
= NULL
;
1017 command
= get_command_line (if_control
, arg
);
1019 if (command
== NULL
)
1022 execute_control_command (command
);
1023 free_command_lines (&command
);
1030 struct user_args
*oargs
= user_args
;
1032 fatal ("Internal error, arg_cleanup called with no user args.\n");
1034 user_args
= user_args
->next
;
1038 /* Bind the incomming arguments for a user defined command to
1039 $arg0, $arg1 ... $argMAXUSERARGS. */
1041 static struct cleanup
*
1045 struct user_args
*args
;
1046 struct cleanup
*old_chain
;
1047 unsigned int arg_count
= 0;
1049 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
1050 memset (args
, 0, sizeof (struct user_args
));
1052 args
->next
= user_args
;
1055 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1067 if (arg_count
>= MAXUSERARGS
)
1069 error ("user defined function may only have %d arguments.\n",
1074 /* Strip whitespace. */
1075 while (*p
== ' ' || *p
== '\t')
1078 /* P now points to an argument. */
1080 user_args
->a
[arg_count
].arg
= p
;
1082 /* Get to the end of this argument. */
1085 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1091 else if (*p
== '\\')
1114 user_args
->a
[arg_count
].len
= p
- start_arg
;
1121 /* Given character string P, return a point to the first argument ($arg),
1122 or NULL if P contains no arguments. */
1128 while ((p
= strchr (p
, '$')))
1130 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1137 /* Insert the user defined arguments stored in user_arg into the $arg
1138 arguments found in line, with the updated copy being placed into nline. */
1144 char *p
, *save_line
, *new_line
;
1147 /* First we need to know how much memory to allocate for the new line. */
1150 while ((p
= locate_arg (line
)))
1155 if (i
>= user_args
->count
)
1157 error ("Missing argument %d in user function.\n", i
);
1160 len
+= user_args
->a
[i
].len
;
1164 /* Don't forget the tail. */
1165 len
+= strlen (line
);
1167 /* Allocate space for the new line and fill it in. */
1168 new_line
= (char *)xmalloc (len
+ 1);
1169 if (new_line
== NULL
)
1172 /* Restore pointer to beginning of old line. */
1175 /* Save pointer to beginning of new line. */
1176 save_line
= new_line
;
1178 while ((p
= locate_arg (line
)))
1182 memcpy (new_line
, line
, p
- line
);
1183 new_line
+= p
- line
;
1186 len
= user_args
->a
[i
].len
;
1189 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1194 /* Don't forget the tail. */
1195 strcpy (new_line
, line
);
1197 /* Return a pointer to the beginning of the new line. */
1202 execute_user_command (c
, args
)
1203 struct cmd_list_element
*c
;
1206 register struct command_line
*cmdlines
;
1207 struct cleanup
*old_chain
;
1208 enum command_control_type ret
;
1210 old_chain
= setup_user_args (args
);
1212 cmdlines
= c
->user_commands
;
1217 /* Set the instream to 0, indicating execution of a
1218 user-defined function. */
1219 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1220 instream
= (FILE *) 0;
1223 ret
= execute_control_command (cmdlines
);
1224 if (ret
!= simple_control
&& ret
!= break_control
)
1226 warning ("Error in control structure.\n");
1229 cmdlines
= cmdlines
->next
;
1231 do_cleanups (old_chain
);
1234 /* Execute the line P as a command.
1235 Pass FROM_TTY as second argument to the defining function. */
1238 execute_command (p
, from_tty
)
1242 register struct cmd_list_element
*c
;
1243 register enum language flang
;
1244 static int warned
= 0;
1245 /* FIXME: These should really be in an appropriate header file */
1246 extern void serial_log_command
PARAMS ((const char *));
1250 /* Force cleanup of any alloca areas if using C alloca instead of
1251 a builtin alloca. */
1254 /* This can happen when command_line_input hits end of file. */
1258 serial_log_command (p
);
1260 while (*p
== ' ' || *p
== '\t') p
++;
1265 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1266 /* Pass null arg rather than an empty one. */
1269 /* Clear off trailing whitespace, except for set and complete command. */
1270 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1272 p
= arg
+ strlen (arg
) - 1;
1273 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1278 /* If this command has been hooked, run the hook first. */
1280 execute_user_command (c
->hook
, (char *)0);
1282 if (c
->class == class_user
)
1283 execute_user_command (c
, arg
);
1284 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1285 do_setshow_command (arg
, from_tty
& caution
, c
);
1286 else if (c
->function
.cfunc
== NO_FUNCTION
)
1287 error ("That is not a command, just a help topic.");
1288 else if (call_command_hook
)
1289 call_command_hook (c
, arg
, from_tty
& caution
);
1291 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1294 /* Tell the user if the language has changed (except first time). */
1295 if (current_language
!= expected_language
)
1297 if (language_mode
== language_mode_auto
) {
1298 language_info (1); /* Print what changed. */
1303 /* Warn the user if the working language does not match the
1304 language of the current frame. Only warn the user if we are
1305 actually running the program, i.e. there is a stack. */
1306 /* FIXME: This should be cacheing the frame and only running when
1307 the frame changes. */
1309 if (target_has_stack
)
1311 flang
= get_frame_language ();
1313 && flang
!= language_unknown
1314 && flang
!= current_language
->la_language
)
1316 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1323 /* NOTE 1999-04-29: This function will be static again, once we modify
1324 gdb to use the event loop as the default command loop and we merge
1325 event-top.c into this file, top.c */
1327 command_loop_marker (foo
)
1332 /* Read commands from `instream' and execute them
1333 until end of file or error reading instream. */
1338 struct cleanup
*old_chain
;
1340 int stdin_is_tty
= ISATTY (stdin
);
1341 long time_at_cmd_start
;
1343 long space_at_cmd_start
= 0;
1345 extern int display_time
;
1346 extern int display_space
;
1348 while (instream
&& !feof (instream
))
1351 extern int insert_mode
;
1353 if (window_hook
&& instream
== stdin
)
1354 (*window_hook
) (instream
, prompt
);
1357 if (instream
== stdin
&& stdin_is_tty
)
1358 reinitialize_more_filter ();
1359 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1362 /* A bit of paranoia: I want to make sure the "insert_mode" global
1363 * is clear except when it is being used for command-line editing
1364 * (see tuiIO.c, utils.c); otherwise normal output will
1365 * get messed up in the TUI. So clear it before/after
1366 * the command-line-input call. - RT
1370 /* Get a command-line. This calls the readline package. */
1371 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1372 instream
== stdin
, "prompt");
1379 time_at_cmd_start
= get_run_time ();
1384 extern char **environ
;
1385 char *lim
= (char *) sbrk (0);
1387 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1391 execute_command (command
, instream
== stdin
);
1392 /* Do any commands attached to breakpoint we stopped at. */
1393 bpstat_do_actions (&stop_bpstat
);
1394 do_cleanups (old_chain
);
1398 long cmd_time
= get_run_time () - time_at_cmd_start
;
1400 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1401 cmd_time
/ 1000000, cmd_time
% 1000000);
1407 extern char **environ
;
1408 char *lim
= (char *) sbrk (0);
1409 long space_now
= lim
- (char *) &environ
;
1410 long space_diff
= space_now
- space_at_cmd_start
;
1412 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1414 (space_diff
>= 0 ? '+' : '-'),
1422 /* Commands call this if they do not want to be repeated by null lines. */
1430 /* If we aren't reading from standard input, we are saving the last
1431 thing read from stdin in line and don't want to delete it. Null lines
1432 won't repeat here in any case. */
1433 if (instream
== stdin
)
1437 /* Read a line from the stream "instream" without command line editing.
1439 It prints PRROMPT once at the start.
1440 Action is compatible with "readline", e.g. space for the result is
1441 malloc'd and should be freed by the caller.
1443 A NULL return means end of file. */
1445 gdb_readline (prrompt
)
1450 int input_index
= 0;
1451 int result_size
= 80;
1455 /* Don't use a _filtered function here. It causes the assumed
1456 character position to be off, since the newline we read from
1457 the user is not accounted for. */
1458 fputs_unfiltered (prrompt
, gdb_stdout
);
1460 /* Move to a new line so the entered line doesn't have a prompt
1461 on the front of it. */
1462 fputs_unfiltered ("\n", gdb_stdout
);
1464 gdb_flush (gdb_stdout
);
1467 result
= (char *) xmalloc (result_size
);
1471 /* Read from stdin if we are executing a user defined command.
1472 This is the right thing for prompt_for_continue, at least. */
1473 c
= fgetc (instream
? instream
: stdin
);
1477 if (input_index
> 0)
1478 /* The last line does not end with a newline. Return it, and
1479 if we are called again fgetc will still return EOF and
1480 we'll return NULL then. */
1487 #ifndef CRLF_SOURCE_FILES
1491 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1497 result
[input_index
++] = c
;
1498 while (input_index
>= result_size
)
1501 result
= (char *) xrealloc (result
, result_size
);
1505 result
[input_index
++] = '\0';
1509 /* Variables which control command line editing and history
1510 substitution. These variables are given default values at the end
1512 static int command_editing_p
;
1513 /* NOTE 1999-04-29: This variable will be static again, once we modify
1514 gdb to use the event loop as the default command loop and we merge
1515 event-top.c into this file, top.c */
1516 /* static */ int history_expansion_p
;
1517 static int write_history_p
;
1518 static int history_size
;
1519 static char *history_filename
;
1521 /* readline uses the word breaks for two things:
1522 (1) In figuring out where to point the TEXT parameter to the
1523 rl_completion_entry_function. Since we don't use TEXT for much,
1524 it doesn't matter a lot what the word breaks are for this purpose, but
1525 it does affect how much stuff M-? lists.
1526 (2) If one of the matches contains a word break character, readline
1527 will quote it. That's why we switch between
1528 gdb_completer_word_break_characters and
1529 gdb_completer_command_word_break_characters. I'm not sure when
1530 we need this behavior (perhaps for funky characters in C++ symbols?). */
1532 /* Variables which are necessary for fancy command line editing. */
1533 char *gdb_completer_word_break_characters
=
1534 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1536 /* When completing on command names, we remove '-' from the list of
1537 word break characters, since we use it in command names. If the
1538 readline library sees one in any of the current completion strings,
1539 it thinks that the string needs to be quoted and automatically supplies
1541 char *gdb_completer_command_word_break_characters
=
1542 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1544 /* Characters that can be used to quote completion strings. Note that we
1545 can't include '"' because the gdb C parser treats such quoted sequences
1547 char *gdb_completer_quote_characters
=
1550 /* Functions that are used as part of the fancy command line editing. */
1552 /* This can be used for functions which don't want to complete on symbols
1553 but don't want to complete on anything else either. */
1556 noop_completer (text
, prefix
)
1563 /* Complete on filenames. */
1565 filename_completer (text
, word
)
1569 /* From readline. */
1570 extern char *filename_completion_function
PARAMS ((char *, int));
1571 int subsequent_name
;
1573 int return_val_used
;
1574 int return_val_alloced
;
1576 return_val_used
= 0;
1577 /* Small for testing. */
1578 return_val_alloced
= 1;
1579 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1581 subsequent_name
= 0;
1585 p
= filename_completion_function (text
, subsequent_name
);
1586 if (return_val_used
>= return_val_alloced
)
1588 return_val_alloced
*= 2;
1590 (char **) xrealloc (return_val
,
1591 return_val_alloced
* sizeof (char *));
1595 return_val
[return_val_used
++] = p
;
1598 /* Like emacs, don't complete on old versions. Especially useful
1599 in the "source" command. */
1600 if (p
[strlen (p
) - 1] == '~')
1606 /* Return exactly p. */
1607 return_val
[return_val_used
++] = p
;
1608 else if (word
> text
)
1610 /* Return some portion of p. */
1611 q
= xmalloc (strlen (p
) + 5);
1612 strcpy (q
, p
+ (word
- text
));
1613 return_val
[return_val_used
++] = q
;
1618 /* Return some of TEXT plus p. */
1619 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1620 strncpy (q
, word
, text
- word
);
1621 q
[text
- word
] = '\0';
1623 return_val
[return_val_used
++] = q
;
1627 subsequent_name
= 1;
1630 /* There is no way to do this just long enough to affect quote inserting
1631 without also affecting the next completion. This should be fixed in
1633 /* Insure that readline does the right thing
1634 with respect to inserting quotes. */
1635 rl_completer_word_break_characters
= "";
1640 /* Here are some useful test cases for completion. FIXME: These should
1641 be put in the test suite. They should be tested with both M-? and TAB.
1643 "show output-" "radix"
1644 "show output" "-radix"
1645 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1646 "p " ambiguous (all symbols)
1647 "info t foo" no completions
1648 "info t " no completions
1649 "info t" ambiguous ("info target", "info terminal", etc.)
1650 "info ajksdlfk" no completions
1651 "info ajksdlfk " no completions
1653 "info " ambiguous (all info commands)
1654 "p \"a" no completions (string constant)
1655 "p 'a" ambiguous (all symbols starting with a)
1656 "p b-a" ambiguous (all symbols starting with a)
1657 "p b-" ambiguous (all symbols)
1658 "file Make" "file" (word break hard to screw up here)
1659 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1662 /* Generate completions one by one for the completer. Each time we are
1663 called return another potential completion to the caller.
1664 line_completion just completes on commands or passes the buck to the
1665 command's completer function, the stuff specific to symbol completion
1666 is in make_symbol_completion_list.
1668 TEXT is the caller's idea of the "word" we are looking at.
1670 MATCHES is the number of matches that have currently been collected from
1671 calling this completion function. When zero, then we need to initialize,
1672 otherwise the initialization has already taken place and we can just
1673 return the next potential completion string.
1675 LINE_BUFFER is available to be looked at; it contains the entire text
1676 of the line. POINT is the offset in that line of the cursor. You
1677 should pretend that the line ends at POINT.
1679 Returns NULL if there are no more completions, else a pointer to a string
1680 which is a possible completion, it is the caller's responsibility to
1684 line_completion_function (text
, matches
, line_buffer
, point
)
1690 static char **list
= (char **)NULL
; /* Cache of completions */
1691 static int index
; /* Next cached completion */
1692 char *output
= NULL
;
1693 char *tmp_command
, *p
;
1694 /* Pointer within tmp_command which corresponds to text. */
1696 struct cmd_list_element
*c
, *result_list
;
1700 /* The caller is beginning to accumulate a new set of completions, so
1701 we need to find all of them now, and cache them for returning one at
1702 a time on future calls. */
1706 /* Free the storage used by LIST, but not by the strings inside.
1707 This is because rl_complete_internal () frees the strings. */
1713 /* Choose the default set of word break characters to break completions.
1714 If we later find out that we are doing completions on command strings
1715 (as opposed to strings supplied by the individual command completer
1716 functions, which can be any string) then we will switch to the
1717 special word break set for command strings, which leaves out the
1718 '-' character used in some commands. */
1720 rl_completer_word_break_characters
=
1721 gdb_completer_word_break_characters
;
1723 /* Decide whether to complete on a list of gdb commands or on symbols. */
1724 tmp_command
= (char *) alloca (point
+ 1);
1727 strncpy (tmp_command
, line_buffer
, point
);
1728 tmp_command
[point
] = '\0';
1729 /* Since text always contains some number of characters leading up
1730 to point, we can find the equivalent position in tmp_command
1731 by subtracting that many characters from the end of tmp_command. */
1732 word
= tmp_command
+ point
- strlen (text
);
1736 /* An empty line we want to consider ambiguous; that is, it
1737 could be any command. */
1738 c
= (struct cmd_list_element
*) -1;
1743 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1746 /* Move p up to the next interesting thing. */
1747 while (*p
== ' ' || *p
== '\t')
1754 /* It is an unrecognized command. So there are no
1755 possible completions. */
1758 else if (c
== (struct cmd_list_element
*) -1)
1762 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1763 doesn't advance over that thing itself. Do so now. */
1765 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1767 if (q
!= tmp_command
+ point
)
1769 /* There is something beyond the ambiguous
1770 command, so there are no possible completions. For
1771 example, "info t " or "info t foo" does not complete
1772 to anything, because "info t" can be "info target" or
1778 /* We're trying to complete on the command which was ambiguous.
1779 This we can deal with. */
1782 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1787 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1789 /* Insure that readline does the right thing with respect to
1790 inserting quotes. */
1791 rl_completer_word_break_characters
=
1792 gdb_completer_command_word_break_characters
;
1797 /* We've recognized a full command. */
1799 if (p
== tmp_command
+ point
)
1801 /* There is no non-whitespace in the line beyond the command. */
1803 if (p
[-1] == ' ' || p
[-1] == '\t')
1805 /* The command is followed by whitespace; we need to complete
1806 on whatever comes after command. */
1809 /* It is a prefix command; what comes after it is
1810 a subcommand (e.g. "info "). */
1811 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1813 /* Insure that readline does the right thing
1814 with respect to inserting quotes. */
1815 rl_completer_word_break_characters
=
1816 gdb_completer_command_word_break_characters
;
1820 list
= complete_on_enum (c
->enums
, p
, word
);
1821 rl_completer_word_break_characters
=
1822 gdb_completer_command_word_break_characters
;
1826 /* It is a normal command; what comes after it is
1827 completed by the command's completer function. */
1828 list
= (*c
->completer
) (p
, word
);
1833 /* The command is not followed by whitespace; we need to
1834 complete on the command itself. e.g. "p" which is a
1835 command itself but also can complete to "print", "ptype"
1839 /* Find the command we are completing on. */
1841 while (q
> tmp_command
)
1843 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1849 list
= complete_on_cmdlist (result_list
, q
, word
);
1851 /* Insure that readline does the right thing
1852 with respect to inserting quotes. */
1853 rl_completer_word_break_characters
=
1854 gdb_completer_command_word_break_characters
;
1859 /* There is non-whitespace beyond the command. */
1861 if (c
->prefixlist
&& !c
->allow_unknown
)
1863 /* It is an unrecognized subcommand of a prefix command,
1864 e.g. "info adsfkdj". */
1869 list
= complete_on_enum (c
->enums
, p
, word
);
1873 /* It is a normal command. */
1874 list
= (*c
->completer
) (p
, word
);
1880 /* If we found a list of potential completions during initialization then
1881 dole them out one at a time. The vector of completions is NULL
1882 terminated, so after returning the last one, return NULL (and continue
1883 to do so) each time we are called after that, until a new list is
1888 output
= list
[index
];
1896 /* Can't do this because readline hasn't yet checked the word breaks
1897 for figuring out whether to insert a quote. */
1899 /* Make sure the word break characters are set back to normal for the
1900 next time that readline tries to complete something. */
1901 rl_completer_word_break_characters
=
1902 gdb_completer_word_break_characters
;
1908 /* Line completion interface function for readline. */
1911 readline_line_completion_function (text
, matches
)
1915 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1918 /* Skip over a possibly quoted word (as defined by the quote characters
1919 and word break characters the completer uses). Returns pointer to the
1920 location after the "word". */
1926 char quote_char
= '\0';
1929 for (scan
= str
; *scan
!= '\0'; scan
++)
1931 if (quote_char
!= '\0')
1933 /* Ignore everything until the matching close quote char */
1934 if (*scan
== quote_char
)
1936 /* Found matching close quote. */
1941 else if (strchr (gdb_completer_quote_characters
, *scan
))
1943 /* Found start of a quoted string. */
1946 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1960 #if STOP_SIGNAL == SIGTSTP
1961 signal (SIGTSTP
, SIG_DFL
);
1963 kill (getpid (), SIGTSTP
);
1964 signal (SIGTSTP
, stop_sig
);
1966 signal (STOP_SIGNAL
, stop_sig
);
1968 printf_unfiltered ("%s", prompt
);
1969 gdb_flush (gdb_stdout
);
1971 /* Forget about any previous command -- null line now will do nothing. */
1974 #endif /* STOP_SIGNAL */
1976 /* Initialize signal handlers. */
1981 /* Under System V the default disposition of a signal is reinstated after
1982 the signal is caught and delivered to an application process. On such
1983 systems one must restore the replacement signal handler if one wishes
1984 to continue handling the signal in one's program. On BSD systems this
1985 is not needed but it is harmless, and it simplifies the code to just do
1986 it unconditionally. */
1987 signal (signo
, do_nothing
);
1993 signal (SIGINT
, request_quit
);
1995 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
1996 to the inferior and breakpoints will be ignored. */
1998 signal (SIGTRAP
, SIG_DFL
);
2001 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2002 passed to the inferior, which we don't want. It would be
2003 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2004 on BSD4.3 systems using vfork, that can affect the
2005 GDB process as well as the inferior (the signal handling tables
2006 might be in memory, shared between the two). Since we establish
2007 a handler for SIGQUIT, when we call exec it will set the signal
2008 to SIG_DFL for us. */
2009 signal (SIGQUIT
, do_nothing
);
2011 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2012 signal (SIGHUP
, disconnect
);
2014 signal (SIGFPE
, float_handler
);
2016 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2017 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2021 /* Read one line from the command input stream `instream'
2022 into the local static buffer `linebuffer' (whose current length
2024 The buffer is made bigger as necessary.
2025 Returns the address of the start of the line.
2027 NULL is returned for end of file.
2029 *If* the instream == stdin & stdin is a terminal, the line read
2030 is copied into the file line saver (global var char *line,
2031 length linesize) so that it can be duplicated.
2033 This routine either uses fancy command line editing or
2034 simple input as the user has requested. */
2037 command_line_input (prrompt
, repeat
, annotation_suffix
)
2040 char *annotation_suffix
;
2042 static char *linebuffer
= 0;
2043 static unsigned linelength
= 0;
2047 char *local_prompt
= prrompt
;
2051 /* The annotation suffix must be non-NULL. */
2052 if (annotation_suffix
== NULL
)
2053 annotation_suffix
= "";
2055 if (annotation_level
> 1 && instream
== stdin
)
2057 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
2058 + strlen (annotation_suffix
) + 40);
2059 if (prrompt
== NULL
)
2060 local_prompt
[0] = '\0';
2062 strcpy (local_prompt
, prrompt
);
2063 strcat (local_prompt
, "\n\032\032");
2064 strcat (local_prompt
, annotation_suffix
);
2065 strcat (local_prompt
, "\n");
2068 if (linebuffer
== 0)
2071 linebuffer
= (char *) xmalloc (linelength
);
2076 /* Control-C quits instantly if typed while in this loop
2077 since it should not wait until the user types a newline. */
2081 signal (STOP_SIGNAL
, stop_sig
);
2086 /* Make sure that all output has been output. Some machines may let
2087 you get away with leaving out some of the gdb_flush, but not all. */
2089 gdb_flush (gdb_stdout
);
2090 gdb_flush (gdb_stderr
);
2092 if (source_file_name
!= NULL
)
2094 ++source_line_number
;
2095 sprintf (source_error
,
2096 "%s%s:%d: Error in sourced command file:\n",
2099 source_line_number
);
2100 error_pre_print
= source_error
;
2103 if (annotation_level
> 1 && instream
== stdin
)
2105 printf_unfiltered ("\n\032\032pre-");
2106 printf_unfiltered (annotation_suffix
);
2107 printf_unfiltered ("\n");
2110 /* Don't use fancy stuff if not talking to stdin. */
2111 if (readline_hook
&& instream
== NULL
)
2113 rl
= (*readline_hook
) (local_prompt
);
2115 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2117 rl
= readline (local_prompt
);
2121 rl
= gdb_readline (local_prompt
);
2124 if (annotation_level
> 1 && instream
== stdin
)
2126 printf_unfiltered ("\n\032\032post-");
2127 printf_unfiltered (annotation_suffix
);
2128 printf_unfiltered ("\n");
2131 if (!rl
|| rl
== (char *) EOF
)
2136 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2138 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2139 nline
= (char *) xrealloc (linebuffer
, linelength
);
2140 p
+= nline
- linebuffer
;
2144 /* Copy line. Don't copy null at end. (Leaves line alone
2145 if this was just a newline) */
2149 free (rl
); /* Allocated in readline. */
2151 if (p
== linebuffer
|| *(p
- 1) != '\\')
2154 p
--; /* Put on top of '\'. */
2155 local_prompt
= (char *) 0;
2160 signal (STOP_SIGNAL
, SIG_DFL
);
2167 #define SERVER_COMMAND_LENGTH 7
2169 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2170 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2173 /* Note that we don't set `line'. Between this and the check in
2174 dont_repeat, this insures that repeating will still do the
2177 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2180 /* Do history expansion if that is wished. */
2181 if (history_expansion_p
&& instream
== stdin
2182 && ISATTY (instream
))
2184 char *history_value
;
2187 *p
= '\0'; /* Insert null now. */
2188 expanded
= history_expand (linebuffer
, &history_value
);
2191 /* Print the changes. */
2192 printf_unfiltered ("%s\n", history_value
);
2194 /* If there was an error, call this function again. */
2197 free (history_value
);
2198 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2200 if (strlen (history_value
) > linelength
)
2202 linelength
= strlen (history_value
) + 1;
2203 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2205 strcpy (linebuffer
, history_value
);
2206 p
= linebuffer
+ strlen(linebuffer
);
2207 free (history_value
);
2211 /* If we just got an empty line, and that is supposed
2212 to repeat the previous command, return the value in the
2214 if (repeat
&& p
== linebuffer
)
2216 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2222 /* Add line to history if appropriate. */
2223 if (instream
== stdin
2224 && ISATTY (stdin
) && *linebuffer
)
2225 add_history (linebuffer
);
2227 /* Note: lines consisting solely of comments are added to the command
2228 history. This is useful when you type a command, and then
2229 realize you don't want to execute it quite yet. You can comment
2230 out the command and then later fetch it from the value history
2231 and remove the '#'. The kill ring is probably better, but some
2232 people are in the habit of commenting things out. */
2234 *p1
= '\0'; /* Found a comment. */
2236 /* Save into global buffer if appropriate. */
2239 if (linelength
> linesize
)
2241 line
= xrealloc (line
, linelength
);
2242 linesize
= linelength
;
2244 strcpy (line
, linebuffer
);
2252 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2253 code bodies. This is typically used when we encounter an "else"
2254 clause for an "if" command. */
2257 realloc_body_list (command
, new_length
)
2258 struct command_line
*command
;
2262 struct command_line
**body_list
;
2264 n
= command
->body_count
;
2266 /* Nothing to do? */
2267 if (new_length
<= n
)
2270 body_list
= (struct command_line
**)
2271 xmalloc (sizeof (struct command_line
*) * new_length
);
2273 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2275 free (command
->body_list
);
2276 command
->body_list
= body_list
;
2277 command
->body_count
= new_length
;
2280 /* Read one line from the input stream. If the command is an "else" or
2281 "end", return such an indication to the caller. */
2283 static enum misc_command_type
2284 read_next_line (command
)
2285 struct command_line
**command
;
2287 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2290 if (control_level
>= 254)
2291 error ("Control nesting too deep!\n");
2293 /* Set a prompt based on the nesting of the control commands. */
2294 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2296 for (i
= 0; i
< control_level
; i
++)
2297 control_prompt
[i
] = ' ';
2298 control_prompt
[i
] = '>';
2299 control_prompt
[i
+1] = '\0';
2300 prompt_ptr
= (char *)&control_prompt
[0];
2305 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2307 /* Not sure what to do here. */
2311 /* Strip leading and trailing whitespace. */
2312 while (*p
== ' ' || *p
== '\t')
2315 p1
= p
+ strlen (p
);
2316 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2319 /* Blanks and comments don't really do anything, but we need to
2320 distinguish them from else, end and other commands which can be
2322 if (p1
== p
|| p
[0] == '#')
2325 /* Is this the end of a simple, while, or if control structure? */
2326 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2329 /* Is the else clause of an if control structure? */
2330 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2331 return else_command
;
2333 /* Check for while, if, break, continue, etc and build a new command
2334 line structure for them. */
2335 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2336 *command
= build_command_line (while_control
, p
+ 6);
2337 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2338 *command
= build_command_line (if_control
, p
+ 3);
2339 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2341 *command
= (struct command_line
*)
2342 xmalloc (sizeof (struct command_line
));
2343 (*command
)->next
= NULL
;
2344 (*command
)->line
= NULL
;
2345 (*command
)->control_type
= break_control
;
2346 (*command
)->body_count
= 0;
2347 (*command
)->body_list
= NULL
;
2349 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2351 *command
= (struct command_line
*)
2352 xmalloc (sizeof (struct command_line
));
2353 (*command
)->next
= NULL
;
2354 (*command
)->line
= NULL
;
2355 (*command
)->control_type
= continue_control
;
2356 (*command
)->body_count
= 0;
2357 (*command
)->body_list
= NULL
;
2361 /* A normal command. */
2362 *command
= (struct command_line
*)
2363 xmalloc (sizeof (struct command_line
));
2364 (*command
)->next
= NULL
;
2365 (*command
)->line
= savestring (p
, p1
- p
);
2366 (*command
)->control_type
= simple_control
;
2367 (*command
)->body_count
= 0;
2368 (*command
)->body_list
= NULL
;
2371 /* Nothing special. */
2375 /* Recursively read in the control structures and create a command_line
2376 structure from them.
2378 The parent_control parameter is the control structure in which the
2379 following commands are nested. */
2381 static enum command_control_type
2382 recurse_read_control_structure (current_cmd
)
2383 struct command_line
*current_cmd
;
2385 int current_body
, i
;
2386 enum misc_command_type val
;
2387 enum command_control_type ret
;
2388 struct command_line
**body_ptr
, *child_tail
, *next
;
2393 /* Sanity checks. */
2394 if (current_cmd
->control_type
== simple_control
)
2396 error ("Recursed on a simple control type\n");
2397 return invalid_control
;
2400 if (current_body
> current_cmd
->body_count
)
2402 error ("Allocated body is smaller than this command type needs\n");
2403 return invalid_control
;
2406 /* Read lines from the input stream and build control structures. */
2412 val
= read_next_line (&next
);
2414 /* Just skip blanks and comments. */
2415 if (val
== nop_command
)
2418 if (val
== end_command
)
2420 if (current_cmd
->control_type
== while_control
2421 || current_cmd
->control_type
== if_control
)
2423 /* Success reading an entire control structure. */
2424 ret
= simple_control
;
2429 ret
= invalid_control
;
2434 /* Not the end of a control structure. */
2435 if (val
== else_command
)
2437 if (current_cmd
->control_type
== if_control
2438 && current_body
== 1)
2440 realloc_body_list (current_cmd
, 2);
2447 ret
= invalid_control
;
2454 child_tail
->next
= next
;
2458 body_ptr
= current_cmd
->body_list
;
2459 for (i
= 1; i
< current_body
; i
++)
2468 /* If the latest line is another control structure, then recurse
2470 if (next
->control_type
== while_control
2471 || next
->control_type
== if_control
)
2474 ret
= recurse_read_control_structure (next
);
2477 if (ret
!= simple_control
)
2487 /* Read lines from the input stream and accumulate them in a chain of
2488 struct command_line's, which is then returned. For input from a
2489 terminal, the special command "end" is used to mark the end of the
2490 input, and is not included in the returned chain of commands. */
2492 #define END_MESSAGE "End with a line saying just \"end\"."
2494 struct command_line
*
2495 read_command_lines (prompt
, from_tty
)
2499 struct command_line
*head
, *tail
, *next
;
2500 struct cleanup
*old_chain
;
2501 enum command_control_type ret
;
2502 enum misc_command_type val
;
2505 if (readline_begin_hook
)
2507 /* Note - intentional to merge messages with no newline */
2508 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2510 else if (from_tty
&& input_from_terminal_p ())
2512 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2513 gdb_flush (gdb_stdout
);
2521 val
= read_next_line (&next
);
2523 /* Ignore blank lines or comments. */
2524 if (val
== nop_command
)
2527 if (val
== end_command
)
2529 ret
= simple_control
;
2533 if (val
!= ok_command
)
2535 ret
= invalid_control
;
2539 if (next
->control_type
== while_control
2540 || next
->control_type
== if_control
)
2543 ret
= recurse_read_control_structure (next
);
2546 if (ret
== invalid_control
)
2557 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2567 if (ret
!= invalid_control
)
2569 discard_cleanups (old_chain
);
2572 do_cleanups (old_chain
);
2575 if (readline_end_hook
)
2577 (*readline_end_hook
) ();
2582 /* Free a chain of struct command_line's. */
2585 free_command_lines (lptr
)
2586 struct command_line
**lptr
;
2588 register struct command_line
*l
= *lptr
;
2589 register struct command_line
*next
;
2590 struct command_line
**blist
;
2595 if (l
->body_count
> 0)
2597 blist
= l
->body_list
;
2598 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2599 free_command_lines (blist
);
2608 /* Add an element to the list of info subcommands. */
2611 add_info (name
, fun
, doc
)
2613 void (*fun
) PARAMS ((char *, int));
2616 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2619 /* Add an alias to the list of info subcommands. */
2622 add_info_alias (name
, oldname
, abbrev_flag
)
2627 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2630 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2631 Therefore, its own definition is called only for "info" with no args. */
2635 info_command (arg
, from_tty
)
2639 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2640 help_list (infolist
, "info ", -1, gdb_stdout
);
2643 /* The "complete" command is used by Emacs to implement completion. */
2647 complete_command (arg
, from_tty
)
2659 argpoint
= strlen (arg
);
2661 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2663 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2665 printf_unfiltered ("%s\n", completion
);
2670 /* The "show" command with no arguments shows all the settings. */
2674 show_command (arg
, from_tty
)
2678 cmd_show_list (showlist
, from_tty
, "");
2681 /* Add an element to the list of commands. */
2684 add_com (name
, class, fun
, doc
)
2686 enum command_class
class;
2687 void (*fun
) PARAMS ((char *, int));
2690 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2693 /* Add an alias or abbreviation command to the list of commands. */
2696 add_com_alias (name
, oldname
, class, abbrev_flag
)
2699 enum command_class
class;
2702 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2709 error ("Argument required (%s).", why
);
2714 help_command (command
, from_tty
)
2716 int from_tty
; /* Ignored */
2718 help_cmd (command
, gdb_stdout
);
2722 validate_comname (comname
)
2728 error_no_arg ("name of command to define");
2733 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2734 error ("Junk in argument list: \"%s\"", p
);
2739 /* This is just a placeholder in the command data structures. */
2741 user_defined_command (ignore
, from_tty
)
2748 define_command (comname
, from_tty
)
2752 register struct command_line
*cmds
;
2753 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2754 char *tem
= comname
;
2756 #define HOOK_STRING "hook-"
2759 validate_comname (comname
);
2761 /* Look it up, and verify that we got an exact match. */
2762 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2763 if (c
&& !STREQ (comname
, c
->name
))
2768 if (c
->class == class_user
|| c
->class == class_alias
)
2769 tem
= "Redefine command \"%s\"? ";
2771 tem
= "Really redefine built-in command \"%s\"? ";
2772 if (!query (tem
, c
->name
))
2773 error ("Command \"%s\" not redefined.", c
->name
);
2776 /* If this new command is a hook, then mark the command which it
2777 is hooking. Note that we allow hooking `help' commands, so that
2778 we can hook the `stop' pseudo-command. */
2780 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2782 /* Look up cmd it hooks, and verify that we got an exact match. */
2783 tem
= comname
+HOOK_LEN
;
2784 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2785 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2789 warning ("Your new `%s' command does not hook any existing command.",
2791 if (!query ("Proceed? "))
2792 error ("Not confirmed.");
2796 comname
= savestring (comname
, strlen (comname
));
2798 /* If the rest of the commands will be case insensitive, this one
2799 should behave in the same manner. */
2800 for (tem
= comname
; *tem
; tem
++)
2801 if (isupper(*tem
)) *tem
= tolower(*tem
);
2803 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2804 cmds
= read_command_lines (tmpbuf
, from_tty
);
2806 if (c
&& c
->class == class_user
)
2807 free_command_lines (&c
->user_commands
);
2809 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2810 (c
&& c
->class == class_user
)
2811 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2812 newc
->user_commands
= cmds
;
2814 /* If this new command is a hook, then mark both commands as being
2818 hookc
->hook
= newc
; /* Target gets hooked. */
2819 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2824 document_command (comname
, from_tty
)
2828 struct command_line
*doclines
;
2829 register struct cmd_list_element
*c
;
2830 char *tem
= comname
;
2833 validate_comname (comname
);
2835 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2837 if (c
->class != class_user
)
2838 error ("Command \"%s\" is built-in.", comname
);
2840 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2841 doclines
= read_command_lines (tmpbuf
, from_tty
);
2843 if (c
->doc
) free (c
->doc
);
2846 register struct command_line
*cl1
;
2847 register int len
= 0;
2849 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2850 len
+= strlen (cl1
->line
) + 1;
2852 c
->doc
= (char *) xmalloc (len
+ 1);
2855 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2857 strcat (c
->doc
, cl1
->line
);
2859 strcat (c
->doc
, "\n");
2863 free_command_lines (&doclines
);
2866 /* Print the GDB banner. */
2868 print_gdb_version (stream
)
2871 /* From GNU coding standards, first line is meant to be easy for a
2872 program to parse, and is just canonical program name and version
2873 number, which starts after last space. */
2875 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
2877 /* Second line is a copyright notice. */
2879 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
2881 /* Following the copyright is a brief statement that the program is
2882 free software, that users are free to copy and change it on
2883 certain conditions, that it is covered by the GNU GPL, and that
2884 there is no warranty. */
2886 fprintf_filtered (stream
, "\
2887 GDB is free software, covered by the GNU General Public License, and you are\n\
2888 welcome to change it and/or distribute copies of it under certain conditions.\n\
2889 Type \"show copying\" to see the conditions.\n\
2890 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2892 /* After the required info we print the configuration information. */
2894 fprintf_filtered (stream
, "This GDB was configured as \"");
2895 if (!STREQ (host_name
, target_name
))
2897 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
2901 fprintf_filtered (stream
, "%s", host_name
);
2903 fprintf_filtered (stream
, "\".");
2908 show_version (args
, from_tty
)
2913 print_gdb_version (gdb_stdout
);
2914 printf_filtered ("\n");
2918 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2924 printf_unfiltered ("%s", prompt
);
2925 gdb_flush (gdb_stdout
);
2928 /* This replaces the above for the frontends: it returns a pointer
2940 /* ??rehrauer: I don't know why this fails, since it looks as though
2941 assignments to prompt are wrapped in calls to savestring...
2945 prompt
= savestring (s
, strlen (s
));
2949 /* If necessary, make the user confirm that we should quit. Return
2950 non-zero if we should quit, zero if we shouldn't. */
2955 if (inferior_pid
!= 0 && target_has_execution
)
2959 /* This is something of a hack. But there's no reliable way to
2960 see if a GUI is running. The `use_windows' variable doesn't
2963 s
= "A debugging session is active.\nDo you still want to close the debugger?";
2964 else if (attach_flag
)
2965 s
= "The program is running. Quit anyway (and detach it)? ";
2967 s
= "The program is running. Exit anyway? ";
2976 /* Quit without asking for confirmation. */
2979 quit_force (args
, from_tty
)
2985 /* An optional expression may be used to cause gdb to terminate with the
2986 value of that expression. */
2989 value_ptr val
= parse_and_eval (args
);
2991 exit_code
= (int) value_as_long (val
);
2994 if (inferior_pid
!= 0 && target_has_execution
)
2997 target_detach (args
, from_tty
);
3002 /* UDI wants this, to kill the TIP. */
3005 /* Save the history information if it is appropriate to do so. */
3006 if (write_history_p
&& history_filename
)
3007 write_history (history_filename
);
3009 do_final_cleanups(ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3012 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3013 /* The above does not need to be inside a tuiDo(), since
3014 * it is not manipulating the curses screen, but rather,
3015 * it is tearing it down.
3024 /* Handle the quit command. */
3027 quit_command (args
, from_tty
)
3031 if (! quit_confirm ())
3032 error ("Not confirmed.");
3033 quit_force (args
, from_tty
);
3036 /* Returns whether GDB is running on a terminal and whether the user
3037 desires that questions be asked of them on that terminal. */
3040 input_from_terminal_p ()
3042 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3047 pwd_command (args
, from_tty
)
3051 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
3052 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3054 if (!STREQ (gdb_dirbuf
, current_directory
))
3055 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3056 current_directory
, gdb_dirbuf
);
3058 printf_unfiltered ("Working directory %s.\n", current_directory
);
3062 cd_command (dir
, from_tty
)
3067 /* Found something other than leading repetitions of "/..". */
3068 int found_real_path
;
3071 /* If the new directory is absolute, repeat is a no-op; if relative,
3072 repeat might be useful but is more likely to be a mistake. */
3076 error_no_arg ("new working directory");
3078 dir
= tilde_expand (dir
);
3079 make_cleanup (free
, dir
);
3081 if (chdir (dir
) < 0)
3082 perror_with_name (dir
);
3085 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
3087 current_directory
= dir
;
3090 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
3091 current_directory
= concat (current_directory
, dir
, NULL
);
3093 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3097 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3099 found_real_path
= 0;
3100 for (p
= current_directory
; *p
;)
3102 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3104 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3105 && (p
[3] == 0 || SLASH_P (p
[3])))
3107 if (found_real_path
)
3109 /* Search backwards for the directory just before the "/.."
3110 and obliterate it and the "/..". */
3112 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
3115 if (q
== current_directory
)
3116 /* current_directory is
3117 a relative pathname ("can't happen"--leave it alone). */
3121 strcpy (q
- 1, p
+ 3);
3126 /* We are dealing with leading repetitions of "/..", for example
3127 "/../..", which is the Mach super-root. */
3132 found_real_path
= 1;
3137 forget_cached_source_info ();
3140 pwd_command ((char *) 0, 1);
3143 struct source_cleanup_lines_args
{
3146 char *old_pre_error
;
3147 char *old_error_pre_print
;
3151 source_cleanup_lines (args
)
3154 struct source_cleanup_lines_args
*p
=
3155 (struct source_cleanup_lines_args
*)args
;
3156 source_line_number
= p
->old_line
;
3157 source_file_name
= p
->old_file
;
3158 source_pre_error
= p
->old_pre_error
;
3159 error_pre_print
= p
->old_error_pre_print
;
3164 source_command (args
, from_tty
)
3169 struct cleanup
*old_cleanups
;
3171 struct source_cleanup_lines_args old_lines
;
3176 error ("source command requires pathname of file to source.");
3179 file
= tilde_expand (file
);
3180 old_cleanups
= make_cleanup (free
, file
);
3182 stream
= fopen (file
, FOPEN_RT
);
3186 perror_with_name (file
);
3191 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3193 old_lines
.old_line
= source_line_number
;
3194 old_lines
.old_file
= source_file_name
;
3195 old_lines
.old_pre_error
= source_pre_error
;
3196 old_lines
.old_error_pre_print
= error_pre_print
;
3197 make_cleanup (source_cleanup_lines
, &old_lines
);
3198 source_line_number
= 0;
3199 source_file_name
= file
;
3200 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3201 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3202 make_cleanup (free
, source_pre_error
);
3203 /* This will get set every time we read a line. So it won't stay "" for
3205 error_pre_print
= "";
3207 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3208 if (source_error_allocated
< needed_length
)
3210 source_error_allocated
*= 2;
3211 if (source_error_allocated
< needed_length
)
3212 source_error_allocated
= needed_length
;
3213 if (source_error
== NULL
)
3214 source_error
= xmalloc (source_error_allocated
);
3216 source_error
= xrealloc (source_error
, source_error_allocated
);
3219 read_command_file (stream
);
3221 do_cleanups (old_cleanups
);
3226 echo_command (text
, from_tty
)
3234 while ((c
= *p
++) != '\0')
3238 /* \ at end of argument is used after spaces
3239 so they won't be lost. */
3243 c
= parse_escape (&p
);
3245 printf_filtered ("%c", c
);
3248 printf_filtered ("%c", c
);
3251 /* Force this output to appear now. */
3253 gdb_flush (gdb_stdout
);
3258 dont_repeat_command (ignored
, from_tty
)
3262 *line
= 0; /* Can't call dont_repeat here because we're not
3263 necessarily reading from stdin. */
3266 /* Functions to manipulate command line editing control variables. */
3268 /* Number of commands to print in each call to show_commands. */
3269 #define Hist_print 10
3271 show_commands (args
, from_tty
)
3275 /* Index for history commands. Relative to history_base. */
3278 /* Number of the history entry which we are planning to display next.
3279 Relative to history_base. */
3282 /* The first command in the history which doesn't exist (i.e. one more
3283 than the number of the last command). Relative to history_base. */
3286 extern HIST_ENTRY
*history_get
PARAMS ((int));
3288 /* Print out some of the commands from the command history. */
3289 /* First determine the length of the history list. */
3290 hist_len
= history_size
;
3291 for (offset
= 0; offset
< history_size
; offset
++)
3293 if (!history_get (history_base
+ offset
))
3302 if (args
[0] == '+' && args
[1] == '\0')
3303 /* "info editing +" should print from the stored position. */
3306 /* "info editing <exp>" should print around command number <exp>. */
3307 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3309 /* "show commands" means print the last Hist_print commands. */
3312 num
= hist_len
- Hist_print
;
3318 /* If there are at least Hist_print commands, we want to display the last
3319 Hist_print rather than, say, the last 6. */
3320 if (hist_len
- num
< Hist_print
)
3322 num
= hist_len
- Hist_print
;
3327 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3329 printf_filtered ("%5d %s\n", history_base
+ offset
,
3330 (history_get (history_base
+ offset
))->line
);
3333 /* The next command we want to display is the next one that we haven't
3337 /* If the user repeats this command with return, it should do what
3338 "show commands +" does. This is unnecessary if arg is null,
3339 because "show commands +" is not useful after "show commands". */
3340 if (from_tty
&& args
)
3347 /* Called by do_setshow_command. */
3350 set_history_size_command (args
, from_tty
, c
)
3353 struct cmd_list_element
*c
;
3355 if (history_size
== INT_MAX
)
3356 unstifle_history ();
3357 else if (history_size
>= 0)
3358 stifle_history (history_size
);
3361 history_size
= INT_MAX
;
3362 error ("History size must be non-negative");
3368 set_history (args
, from_tty
)
3372 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3373 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3378 show_history (args
, from_tty
)
3382 cmd_show_list (showhistlist
, from_tty
, "");
3385 int info_verbose
= 0; /* Default verbose msgs off */
3387 /* Called by do_setshow_command. An elaborate joke. */
3390 set_verbose (args
, from_tty
, c
)
3393 struct cmd_list_element
*c
;
3395 char *cmdname
= "verbose";
3396 struct cmd_list_element
*showcmd
;
3398 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3402 c
->doc
= "Set verbose printing of informational messages.";
3403 showcmd
->doc
= "Show verbose printing of informational messages.";
3407 c
->doc
= "Set verbosity.";
3408 showcmd
->doc
= "Show verbosity.";
3413 float_handler (signo
)
3416 /* This message is based on ANSI C, section 4.7. Note that integer
3417 divide by zero causes this, so "float" is a misnomer. */
3418 signal (SIGFPE
, float_handler
);
3419 error ("Erroneous arithmetic operation.");
3433 enablebreaklist
= NULL
;
3438 showhistlist
= NULL
;
3439 unsethistlist
= NULL
;
3440 maintenancelist
= NULL
;
3441 maintenanceinfolist
= NULL
;
3442 maintenanceprintlist
= NULL
;
3443 setprintlist
= NULL
;
3444 showprintlist
= NULL
;
3445 setchecklist
= NULL
;
3446 showchecklist
= NULL
;
3449 /* Init the history buffer. Note that we are called after the init file(s)
3450 * have been read so that the user can change the history file via his
3451 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3452 * overrides all of this.
3460 tmpenv
= getenv ("HISTSIZE");
3462 history_size
= atoi (tmpenv
);
3463 else if (!history_size
)
3466 stifle_history (history_size
);
3468 tmpenv
= getenv ("GDBHISTFILE");
3470 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3471 else if (!history_filename
) {
3472 /* We include the current directory so that if the user changes
3473 directories the file written will be the same as the one
3475 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3477 read_history (history_filename
);
3483 struct cmd_list_element
*c
;
3485 /* from event-top.c */
3486 extern int async_command_editing_p
;
3487 extern struct prompts the_prompts
;
3488 extern char *async_annotation_suffix
;
3489 extern char *new_async_prompt
;
3491 /* If we are running the asynchronous version,
3492 we initialize the prompts differently. */
3495 #ifdef DEFAULT_PROMPT
3496 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3498 prompt
= savestring ("(gdb) ", 6);
3503 /* initialize the prompt stack to a simple "(gdb) " prompt or to
3504 whatever the DEFULAT_PROMPT is. */
3505 the_prompts
.top
= 0;
3507 #ifdef DEFAULT_PROMPT
3508 PROMPT(0) = savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3510 PROMPT(0) = savestring ("(gdb) ", 6);
3513 /* Set things up for annotation_level > 1, if the user ever decides
3515 async_annotation_suffix
= "prompt";
3516 /* Set the variable associated with the setshow prompt command. */
3517 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
3520 /* Set the important stuff up for command editing. */
3521 command_editing_p
= 1;
3522 async_command_editing_p
= 1;
3523 history_expansion_p
= 0;
3524 write_history_p
= 0;
3526 /* Setup important stuff for command line editing. */
3527 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3528 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3529 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3530 rl_readline_name
= "gdb";
3532 /* Define the classes of commands.
3533 They will appear in the help list in the reverse of this order. */
3535 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3536 "Maintenance commands.\n\
3537 Some gdb commands are provided just for use by gdb maintainers.\n\
3538 These commands are subject to frequent change, and may not be as\n\
3539 well documented as user commands.",
3541 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3542 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3543 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3544 The commands in this class are those defined by the user.\n\
3545 Use the \"define\" command to define a command.", &cmdlist
);
3546 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3548 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3549 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3550 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3551 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3552 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3553 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3554 counting from zero for the innermost (currently executing) frame.\n\n\
3555 At any time gdb identifies one frame as the \"selected\" frame.\n\
3556 Variable lookups are done with respect to the selected frame.\n\
3557 When the program being debugged stops, gdb selects the innermost frame.\n\
3558 The commands below can be used to select other frames by number or address.",
3560 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3562 add_com ("pwd", class_files
, pwd_command
,
3563 "Print working directory. This is used for your program as well.");
3564 c
= add_cmd ("cd", class_files
, cd_command
,
3565 "Set working directory to DIR for debugger and program being debugged.\n\
3566 The change does not take effect for the program being debugged\n\
3567 until the next time it is started.", &cmdlist
);
3568 c
->completer
= filename_completer
;
3570 /* The set prompt command is different depending whether or not the
3571 async version is run. NOTE: this difference is going to
3572 disappear as we make the event loop be the default engine of
3577 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3584 c
= add_set_cmd ("prompt", class_support
, var_string
,
3585 (char *)&new_async_prompt
,
3588 add_show_from_set (c
, &showlist
);
3589 c
->function
.sfunc
= set_async_prompt
;
3592 add_com ("echo", class_support
, echo_command
,
3593 "Print a constant string. Give string as argument.\n\
3594 C escape sequences may be used in the argument.\n\
3595 No newline is added at the end of the argument;\n\
3596 use \"\\n\" if you want a newline to be printed.\n\
3597 Since leading and trailing whitespace are ignored in command arguments,\n\
3598 if you want to print some you must use \"\\\" before leading whitespace\n\
3599 to be printed or after trailing whitespace.");
3600 add_com ("document", class_support
, document_command
,
3601 "Document a user-defined command.\n\
3602 Give command name as argument. Give documentation on following lines.\n\
3603 End with a line of just \"end\".");
3604 add_com ("define", class_support
, define_command
,
3605 "Define a new command name. Command name is argument.\n\
3606 Definition appears on following lines, one command per line.\n\
3607 End with a line of just \"end\".\n\
3608 Use the \"document\" command to give documentation for the new command.\n\
3609 Commands defined in this way may have up to ten arguments.");
3612 c
= add_cmd ("source", class_support
, source_command
,
3613 "Read commands from a file named FILE.\n\
3614 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3615 when gdb is started.", &cmdlist
);
3617 /* Punt file name, we can't help it easily. */
3618 c
= add_cmd ("source", class_support
, source_command
,
3619 "Read commands from a file named FILE.\n\
3620 Note that the file \".gdbinit\" is read automatically in this way\n\
3621 when gdb is started.", &cmdlist
);
3623 c
->completer
= filename_completer
;
3625 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3626 add_com ("help", class_support
, help_command
, "Print list of commands.");
3627 add_com_alias ("q", "quit", class_support
, 1);
3628 add_com_alias ("h", "help", class_support
, 1);
3630 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3631 Primarily used inside of user-defined commands that should not be repeated when\n\
3634 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3637 add_show_from_set (c
, &showlist
);
3638 c
->function
.sfunc
= set_verbose
;
3639 set_verbose (NULL
, 0, c
);
3641 /* The set editing command is different depending whether or not the
3642 async version is run. NOTE: this difference is going to disappear
3643 as we make the event loop be the default engine of gdb. */
3647 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3648 "Set editing of command lines as they are typed.\n\
3649 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3650 Without an argument, command line editing is enabled. To edit, use\n\
3651 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3656 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *)&async_command_editing_p
,
3657 "Set editing of command lines as they are typed.\n\
3658 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3659 Without an argument, command line editing is enabled. To edit, use\n\
3660 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
3662 add_show_from_set (c
, &showlist
);
3663 c
->function
.sfunc
= set_async_editing_command
;
3666 add_prefix_cmd ("history", class_support
, set_history
,
3667 "Generic command for setting command history parameters.",
3668 &sethistlist
, "set history ", 0, &setlist
);
3669 add_prefix_cmd ("history", class_support
, show_history
,
3670 "Generic command for showing command history parameters.",
3671 &showhistlist
, "show history ", 0, &showlist
);
3674 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3675 "Set history expansion on command input.\n\
3676 Without an argument, history expansion is enabled.", &sethistlist
),
3680 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3681 "Set saving of the history record on exit.\n\
3682 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3683 Without an argument, saving is enabled.", &sethistlist
),
3686 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3687 "Set the size of the command history, \n\
3688 ie. the number of previous commands to keep a record of.", &sethistlist
);
3689 add_show_from_set (c
, &showhistlist
);
3690 c
->function
.sfunc
= set_history_size_command
;
3693 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3694 "Set the filename in which to record the command history\n\
3695 (the list of previous commands of which a record is kept).", &sethistlist
),
3699 (add_set_cmd ("confirm", class_support
, var_boolean
,
3701 "Set whether to confirm potentially dangerous operations.",
3705 add_prefix_cmd ("info", class_info
, info_command
,
3706 "Generic command for showing things about the program being debugged.",
3707 &infolist
, "info ", 0, &cmdlist
);
3708 add_com_alias ("i", "info", class_info
, 1);
3710 add_com ("complete", class_obscure
, complete_command
,
3711 "List the completions for the rest of the line as a command.");
3713 add_prefix_cmd ("show", class_info
, show_command
,
3714 "Generic command for showing things about the debugger.",
3715 &showlist
, "show ", 0, &cmdlist
);
3716 /* Another way to get at the same thing. */
3717 add_info ("set", show_command
, "Show all GDB settings.");
3719 add_cmd ("commands", no_class
, show_commands
,
3720 "Show the history of commands you typed.\n\
3721 You can supply a command number to start with, or a `+' to start after\n\
3722 the previous command number shown.",
3725 add_cmd ("version", no_class
, show_version
,
3726 "Show what version of GDB this is.", &showlist
);
3728 add_com ("while", class_support
, while_command
,
3729 "Execute nested commands WHILE the conditional expression is non zero.\n\
3730 The conditional expression must follow the word `while' and must in turn be\n\
3731 followed by a new line. The nested commands must be entered one per line,\n\
3732 and should be terminated by the word `end'.");
3734 add_com ("if", class_support
, if_command
,
3735 "Execute nested commands once IF the conditional expression is non zero.\n\
3736 The conditional expression must follow the word `if' and must in turn be\n\
3737 followed by a new line. The nested commands must be entered one per line,\n\
3738 and should be terminated by the word 'else' or `end'. If an else clause\n\
3739 is used, the same rules apply to its nested commands as to the first ones.");
3741 /* If target is open when baud changes, it doesn't take effect until the
3742 next open (I think, not sure). */
3743 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3744 var_zinteger
, (char *)&baud_rate
,
3745 "Set baud rate for remote serial I/O.\n\
3746 This value is used to set the speed of the serial port when debugging\n\
3747 using remote targets.", &setlist
),
3751 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3752 "Set debugging of remote protocol.\n\
3753 When enabled, each packet sent or received with the remote target\n\
3754 is displayed.", &setlist
),
3758 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *)&remote_timeout
,
3759 "Set timeout limit to wait for target to respond.\n\
3760 This value is used to set the time limit for gdb to wait for a response\n\
3761 from the target.", &setlist
),
3764 /* The set annotate command is different depending whether or not
3765 the async version is run. NOTE: this difference is going to
3766 disappear as we make the event loop be the default engine of
3770 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3771 (char *)&annotation_level
, "Set annotation_level.\n\
3772 0 == normal; 1 == fullname (for use when running under emacs)\n\
3773 2 == output annotated suitably for use by programs that control GDB.",
3775 c
= add_show_from_set (c
, &showlist
);
3779 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3780 (char *)&annotation_level
, "Set annotation_level.\n\
3781 0 == normal; 1 == fullname (for use when running under emacs)\n\
3782 2 == output annotated suitably for use by programs that control GDB.",
3784 add_show_from_set (c
, &showlist
);
3785 c
->function
.sfunc
= set_async_annotation_level
;