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 extern void initialize_utils
PARAMS ((void));
56 /* Prototypes for local functions */
58 static void dont_repeat_command
PARAMS ((char *, int));
60 static void source_cleanup_lines
PARAMS ((PTR
));
62 static void user_defined_command
PARAMS ((char *, int));
64 static void init_signals
PARAMS ((void));
67 static void stop_sig
PARAMS ((int));
70 static char * line_completion_function
PARAMS ((char *, int, char *, int));
72 static char * readline_line_completion_function
PARAMS ((char *, int));
74 /* NOTE 4/29/99: this function will be static again, after we make the
75 event loop be the default command loop for gdb, and we merge
76 event-top.c into this file, top.c */
77 /* static */ void command_loop_marker
PARAMS ((int));
79 extern void set_async_editing_command
PARAMS ((char *, int, struct cmd_list_element
*));
81 extern void set_async_annotation_level
PARAMS ((char *, int, struct cmd_list_element
*));
83 extern void set_async_prompt
PARAMS ((char *, int, struct cmd_list_element
*));
85 static void while_command
PARAMS ((char *, int));
87 static void if_command
PARAMS ((char *, int));
89 static struct command_line
*
90 build_command_line
PARAMS ((enum command_control_type
, char *));
92 static struct command_line
*
93 get_command_line
PARAMS ((enum command_control_type
, char *));
95 static void realloc_body_list
PARAMS ((struct command_line
*, int));
97 static enum misc_command_type read_next_line
PARAMS ((struct command_line
**));
99 static enum command_control_type
100 recurse_read_control_structure
PARAMS ((struct command_line
*));
102 static struct cleanup
* setup_user_args
PARAMS ((char *));
104 static char * locate_arg
PARAMS ((char *));
106 static char * insert_args
PARAMS ((char *));
108 static void arg_cleanup
PARAMS ((void));
110 static void init_main
PARAMS ((void));
112 static void init_cmd_lists
PARAMS ((void));
114 static void float_handler
PARAMS ((int));
116 static void init_signals
PARAMS ((void));
118 static void set_verbose
PARAMS ((char *, int, struct cmd_list_element
*));
120 static void show_history
PARAMS ((char *, int));
122 static void set_history
PARAMS ((char *, int));
124 static void set_history_size_command
PARAMS ((char *, int,
125 struct cmd_list_element
*));
127 static void show_commands
PARAMS ((char *, int));
129 static void echo_command
PARAMS ((char *, int));
131 static void pwd_command
PARAMS ((char *, int));
133 static void show_version
PARAMS ((char *, int));
135 static void document_command
PARAMS ((char *, int));
137 static void define_command
PARAMS ((char *, int));
139 static void validate_comname
PARAMS ((char *));
141 static void help_command
PARAMS ((char *, int));
143 static void show_command
PARAMS ((char *, int));
145 static void info_command
PARAMS ((char *, int));
147 static void complete_command
PARAMS ((char *, int));
149 static void do_nothing
PARAMS ((int));
152 /* NOTE 4/29/99: This function will be static again, once we modify
153 gdb to use the event loop as the default command loop and we merge
154 event-top.c into this file, top.c */
155 /* static */ int quit_cover
PARAMS ((PTR
));
157 static void disconnect
PARAMS ((int));
160 static void source_cleanup
PARAMS ((FILE *));
162 /* If this definition isn't overridden by the header files, assume
163 that isatty and fileno exist on this system. */
165 #define ISATTY(FP) (isatty (fileno (FP)))
168 /* Initialization file name for gdb. This is overridden in some configs. */
170 #ifndef GDBINIT_FILENAME
171 #define GDBINIT_FILENAME ".gdbinit"
173 char gdbinit
[] = GDBINIT_FILENAME
;
175 int inhibit_gdbinit
= 0;
177 /* If nonzero, and GDB has been configured to be able to use windows,
178 attempt to open them upon startup. */
182 /* Version number of GDB, as a string. */
184 extern char *version
;
186 /* Canonical host name as a string. */
188 extern char *host_name
;
190 /* Canonical target name as a string. */
192 extern char *target_name
;
194 extern char lang_frame_mismatch_warn
[]; /* language.c */
196 /* Flag for whether we want all the "from_tty" gubbish printed. */
198 int caution
= 1; /* Default is yes, sigh. */
200 /* Define all cmd_list_elements. */
202 /* Chain containing all defined commands. */
204 struct cmd_list_element
*cmdlist
;
206 /* Chain containing all defined info subcommands. */
208 struct cmd_list_element
*infolist
;
210 /* Chain containing all defined enable subcommands. */
212 struct cmd_list_element
*enablelist
;
214 /* Chain containing all defined disable subcommands. */
216 struct cmd_list_element
*disablelist
;
218 /* Chain containing all defined toggle subcommands. */
220 struct cmd_list_element
*togglelist
;
222 /* Chain containing all defined stop subcommands. */
224 struct cmd_list_element
*stoplist
;
226 /* Chain containing all defined delete subcommands. */
228 struct cmd_list_element
*deletelist
;
230 /* Chain containing all defined "enable breakpoint" subcommands. */
232 struct cmd_list_element
*enablebreaklist
;
234 /* Chain containing all defined set subcommands */
236 struct cmd_list_element
*setlist
;
238 /* Chain containing all defined unset subcommands */
240 struct cmd_list_element
*unsetlist
;
242 /* Chain containing all defined show subcommands. */
244 struct cmd_list_element
*showlist
;
246 /* Chain containing all defined \"set history\". */
248 struct cmd_list_element
*sethistlist
;
250 /* Chain containing all defined \"show history\". */
252 struct cmd_list_element
*showhistlist
;
254 /* Chain containing all defined \"unset history\". */
256 struct cmd_list_element
*unsethistlist
;
258 /* Chain containing all defined maintenance subcommands. */
260 struct cmd_list_element
*maintenancelist
;
262 /* Chain containing all defined "maintenance info" subcommands. */
264 struct cmd_list_element
*maintenanceinfolist
;
266 /* Chain containing all defined "maintenance print" subcommands. */
268 struct cmd_list_element
*maintenanceprintlist
;
270 struct cmd_list_element
*setprintlist
;
272 struct cmd_list_element
*showprintlist
;
274 struct cmd_list_element
*setchecklist
;
276 struct cmd_list_element
*showchecklist
;
278 /* stdio stream that command input is being read from. Set to stdin normally.
279 Set by source_command to the file we are sourcing. Set to NULL if we are
280 executing a user-defined command or interacting via a GUI. */
284 /* Current working directory. */
286 char *current_directory
;
288 /* The directory name is actually stored here (usually). */
289 char gdb_dirbuf
[1024];
291 /* Function to call before reading a command, if nonzero.
292 The function receives two args: an input stream,
293 and a prompt string. */
295 void (*window_hook
) PARAMS ((FILE *, char *));
300 /* gdb prints this when reading a command interactively */
303 /* Buffer used for reading command lines, and the size
304 allocated for it so far. */
309 /* Nonzero if the current command is modified by "server ". This
310 affects things like recording into the command history, comamnds
311 repeating on RETURN, etc. This is so a user interface (emacs, GUI,
312 whatever) can issue its own commands and also send along commands
313 from the user, and have the user not notice that the user interface
314 is issuing commands too. */
317 /* Baud rate specified for talking to serial target systems. Default
318 is left as -1, so targets can choose their own defaults. */
319 /* FIXME: This means that "show remotebaud" and gr_files_info can print -1
320 or (unsigned int)-1. This is a Bad User Interface. */
324 /* Timeout limit for response from target. */
326 int remote_timeout
= 20; /* Set default to 20 */
328 /* Non-zero tells remote* modules to output debugging info. */
330 int remote_debug
= 0;
332 /* Level of control structure. */
333 static int control_level
;
335 /* Structure for arguments to user defined functions. */
336 #define MAXUSERARGS 10
339 struct user_args
*next
;
348 /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */
352 #define STOP_SIGNAL SIGTSTP
353 static void stop_sig
PARAMS ((int));
357 /* Some System V have job control but not sigsetmask(). */
358 #if !defined (HAVE_SIGSETMASK)
360 #define HAVE_SIGSETMASK 1
362 #define HAVE_SIGSETMASK 0
366 #if 0 == (HAVE_SIGSETMASK)
367 #define sigsetmask(n)
370 /* Hooks for alternate command interfaces. */
372 /* Called after most modules have been initialized, but before taking users
375 void (*init_ui_hook
) PARAMS ((char *argv0
));
377 /* This hook is called from within gdb's many mini-event loops which could
378 steal control from a real user interface's event loop. It returns
379 non-zero if the user is requesting a detach, zero otherwise. */
381 int (*ui_loop_hook
) PARAMS ((int));
383 /* Called instead of command_loop at top level. Can be invoked via
384 return_to_top_level. */
386 void (*command_loop_hook
) PARAMS ((void));
389 /* Called instead of fputs for all output. */
391 void (*fputs_unfiltered_hook
) PARAMS ((const char *linebuffer
, GDB_FILE
*stream
));
393 /* Called when the target says something to the host, which may
394 want to appear in a different window. */
396 void (*target_output_hook
) PARAMS ((char *));
398 /* Called from print_frame_info to list the line we stopped in. */
400 void (*print_frame_info_listing_hook
) PARAMS ((struct symtab
*s
, int line
,
401 int stopline
, int noerror
));
402 /* Replaces most of query. */
404 int (*query_hook
) PARAMS ((const char *, va_list));
406 /* Replaces most of warning. */
408 void (*warning_hook
) PARAMS ((const char *, va_list));
410 /* Called from gdb_flush to flush output. */
412 void (*flush_hook
) PARAMS ((GDB_FILE
*stream
));
414 /* These three functions support getting lines of text from the user. They
415 are used in sequence. First readline_begin_hook is called with a text
416 string that might be (for example) a message for the user to type in a
417 sequence of commands to be executed at a breakpoint. If this function
418 calls back to a GUI, it might take this opportunity to pop up a text
419 interaction window with this message. Next, readline_hook is called
420 with a prompt that is emitted prior to collecting the user input.
421 It can be called multiple times. Finally, readline_end_hook is called
422 to notify the GUI that we are done with the interaction window and it
425 void (*readline_begin_hook
) PARAMS ((char *, ...));
426 char * (*readline_hook
) PARAMS ((char *));
427 void (*readline_end_hook
) PARAMS ((void));
429 /* Called as appropriate to notify the interface of the specified breakpoint
432 void (*create_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
433 void (*delete_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
434 void (*modify_breakpoint_hook
) PARAMS ((struct breakpoint
*bpt
));
436 /* Called during long calculations to allow GUI to repair window damage, and to
437 check for stop buttons, etc... */
439 void (*interactive_hook
) PARAMS ((void));
441 /* Called when the registers have changed, as a hint to a GUI
442 to minimize window update. */
444 void (*registers_changed_hook
) PARAMS ((void));
446 /* Tell the GUI someone changed the register REGNO. -1 means
447 that the caller does not know which register changed or
448 that several registers have changed (see value_assign).*/
449 void (*register_changed_hook
) PARAMS ((int regno
));
451 /* Tell the GUI someone changed LEN bytes of memory at ADDR */
452 void (*memory_changed_hook
) PARAMS ((CORE_ADDR addr
, int len
));
454 /* Called when going to wait for the target. Usually allows the GUI to run
455 while waiting for target events. */
457 int (*target_wait_hook
) PARAMS ((int pid
, struct target_waitstatus
*status
));
459 /* Used by UI as a wrapper around command execution. May do various things
460 like enabling/disabling buttons, etc... */
462 void (*call_command_hook
) PARAMS ((struct cmd_list_element
*c
, char *cmd
,
465 /* Called when the current thread changes. Argument is thread id. */
467 void (*context_hook
) PARAMS ((int id
));
469 /* Takes control from error (). Typically used to prevent longjmps out of the
470 middle of the GUI. Usually used in conjunction with a catch routine. */
472 NORETURN
void (*error_hook
) PARAMS ((void)) ATTR_NORETURN
;
475 /* Where to go for return_to_top_level (RETURN_ERROR). */
476 SIGJMP_BUF error_return
;
477 /* Where to go for return_to_top_level (RETURN_QUIT). */
478 SIGJMP_BUF quit_return
;
480 /* Return for reason REASON. This generally gets back to the command
481 loop, but can be caught via catch_errors. */
484 return_to_top_level (reason
)
485 enum return_reason reason
;
490 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
491 I can think of a reason why that is vital, though). */
492 bpstat_clear_actions(stop_bpstat
); /* Clear queued breakpoint commands */
494 disable_current_display ();
495 do_cleanups (ALL_CLEANUPS
);
497 if (annotation_level
> 1)
508 (NORETURN
void) SIGLONGJMP
509 (reason
== RETURN_ERROR
? error_return
: quit_return
, 1);
512 /* Call FUNC with arg ARGS, catching any errors. If there is no
513 error, return the value returned by FUNC. If there is an error,
514 print ERRSTRING, print the specific error message, then return
517 Must not be called with immediate_quit in effect (bad things might
518 happen, say we got a signal in the middle of a memcpy to quit_return).
519 This is an OK restriction; with very few exceptions immediate_quit can
520 be replaced by judicious use of QUIT.
522 MASK specifies what to catch; it is normally set to
523 RETURN_MASK_ALL, if for no other reason than that the code which
524 calls catch_errors might not be set up to deal with a quit which
525 isn't caught. But if the code can deal with it, it generally
526 should be RETURN_MASK_ERROR, unless for some reason it is more
527 useful to abort only the portion of the operation inside the
528 catch_errors. Note that quit should return to the command line
529 fairly quickly, even if some further processing is being done. */
532 catch_errors (func
, args
, errstring
, mask
)
533 catch_errors_ftype
*func
;
538 SIGJMP_BUF saved_error
;
539 SIGJMP_BUF saved_quit
;
542 struct cleanup
*saved_cleanup_chain
;
543 char *saved_error_pre_print
;
544 char *saved_quit_pre_print
;
546 saved_cleanup_chain
= save_cleanups ();
547 saved_error_pre_print
= error_pre_print
;
548 saved_quit_pre_print
= quit_pre_print
;
550 if (mask
& RETURN_MASK_ERROR
)
552 memcpy ((char *)saved_error
, (char *)error_return
, sizeof (SIGJMP_BUF
));
553 error_pre_print
= errstring
;
555 if (mask
& RETURN_MASK_QUIT
)
557 memcpy (saved_quit
, quit_return
, sizeof (SIGJMP_BUF
));
558 quit_pre_print
= errstring
;
561 if (SIGSETJMP (tmp_jmp
) == 0)
563 if (mask
& RETURN_MASK_ERROR
)
564 memcpy (error_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
565 if (mask
& RETURN_MASK_QUIT
)
566 memcpy (quit_return
, tmp_jmp
, sizeof (SIGJMP_BUF
));
567 val
= (*func
) (args
);
572 restore_cleanups (saved_cleanup_chain
);
574 if (mask
& RETURN_MASK_ERROR
)
576 memcpy (error_return
, saved_error
, sizeof (SIGJMP_BUF
));
577 error_pre_print
= saved_error_pre_print
;
579 if (mask
& RETURN_MASK_QUIT
)
581 memcpy (quit_return
, saved_quit
, sizeof (SIGJMP_BUF
));
582 quit_pre_print
= saved_quit_pre_print
;
587 /* Handler for SIGHUP. */
594 catch_errors (quit_cover
, NULL
,
595 "Could not kill the program being debugged", RETURN_MASK_ALL
);
596 signal (SIGHUP
, SIG_DFL
);
597 kill (getpid (), SIGHUP
);
600 /* Just a little helper function for disconnect(). */
602 /* NOTE 4/29/99: This function will be static again, once we modify
603 gdb to use the event loop as the default command loop and we merge
604 event-top.c into this file, top.c */
609 caution
= 0; /* Throw caution to the wind -- we're exiting.
610 This prevents asking the user dumb questions. */
611 quit_command((char *)0, 0);
614 #endif /* defined SIGHUP */
616 /* Line number we are currently in in a file which is being sourced. */
617 /* NOTE 4/29/99: This variable will be static again, once we modify
618 gdb to use the event loop as the default command loop and we merge
619 event-top.c into this file, top.c */
620 /* static */ int source_line_number
;
622 /* Name of the file we are sourcing. */
623 /* NOTE 4/29/99: This variable will be static again, once we modify
624 gdb to use the event loop as the default command loop and we merge
625 event-top.c into this file, top.c */
626 /* static */ char *source_file_name
;
628 /* Buffer containing the error_pre_print used by the source stuff.
630 /* NOTE 4/29/99: 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_error
;
634 static int source_error_allocated
;
636 /* Something to glom on to the start of error_pre_print if source_file_name
638 /* NOTE 4/29/99: This variable will be static again, once we modify
639 gdb to use the event loop as the default command loop and we merge
640 event-top.c into this file, top.c */
641 /* static */ char *source_pre_error
;
643 /* Clean up on error during a "source" command (or execution of a
644 user-defined command). */
647 source_cleanup (stream
)
650 /* Restore the previous input stream. */
654 /* Read commands from STREAM. */
656 read_command_file (stream
)
659 struct cleanup
*cleanups
;
661 cleanups
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
664 do_cleanups (cleanups
);
667 extern void init_proc
PARAMS ((void));
669 void (*pre_init_ui_hook
) PARAMS ((void));
675 if (pre_init_ui_hook
)
678 /* Run the init function of each source file */
680 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
681 current_directory
= gdb_dirbuf
;
683 init_cmd_lists (); /* This needs to be done first */
684 initialize_targets (); /* Setup target_terminal macros for utils.c */
685 initialize_utils (); /* Make errors and warnings possible */
686 initialize_all_files ();
687 init_main (); /* But that omits this file! Do it now */
689 /* The signal handling mechanism is different depending whether or
690 not the async version is run. NOTE: in the future we plan to make
691 the event loop be the default engine of gdb, and this difference
694 async_init_signals ();
700 /* We need a default language for parsing expressions, so simple things like
701 "set width 0" won't fail if no language is explicitly set in a config file
702 or implicitly set by reading an executable during startup. */
703 set_language (language_c
);
704 expected_language
= current_language
; /* don't warn about the change. */
707 init_ui_hook (argv0
);
710 /* Allocate, initialize a new command line structure for one of the
711 control commands (if/while). */
713 static struct command_line
*
714 build_command_line (type
, args
)
715 enum command_control_type type
;
718 struct command_line
*cmd
;
721 error ("if/while commands require arguments.\n");
723 cmd
= (struct command_line
*)xmalloc (sizeof (struct command_line
));
725 cmd
->control_type
= type
;
729 = (struct command_line
**)xmalloc (sizeof (struct command_line
*)
731 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
732 cmd
->line
= savestring (args
, strlen (args
));
736 /* Build and return a new command structure for the control commands
737 such as "if" and "while". */
739 static struct command_line
*
740 get_command_line (type
, arg
)
741 enum command_control_type type
;
744 struct command_line
*cmd
;
745 struct cleanup
*old_chain
= NULL
;
747 /* Allocate and build a new command line structure. */
748 cmd
= build_command_line (type
, arg
);
750 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
, &cmd
);
752 /* Read in the body of this command. */
753 if (recurse_read_control_structure (cmd
) == invalid_control
)
755 warning ("error reading in control structure\n");
756 do_cleanups (old_chain
);
760 discard_cleanups (old_chain
);
764 /* Recursively print a command (including full control structures). */
766 print_command_line (cmd
, depth
)
767 struct command_line
*cmd
;
774 for (i
= 0; i
< depth
; i
++)
775 fputs_filtered (" ", gdb_stdout
);
778 /* A simple command, print it and return. */
779 if (cmd
->control_type
== simple_control
)
781 fputs_filtered (cmd
->line
, gdb_stdout
);
782 fputs_filtered ("\n", gdb_stdout
);
786 /* loop_continue to jump to the start of a while loop, print it
788 if (cmd
->control_type
== continue_control
)
790 fputs_filtered ("loop_continue\n", gdb_stdout
);
794 /* loop_break to break out of a while loop, print it and return. */
795 if (cmd
->control_type
== break_control
)
797 fputs_filtered ("loop_break\n", gdb_stdout
);
801 /* A while command. Recursively print its subcommands before returning. */
802 if (cmd
->control_type
== while_control
)
804 struct command_line
*list
;
805 fputs_filtered ("while ", gdb_stdout
);
806 fputs_filtered (cmd
->line
, gdb_stdout
);
807 fputs_filtered ("\n", gdb_stdout
);
808 list
= *cmd
->body_list
;
811 print_command_line (list
, depth
+ 1);
816 /* An if command. Recursively print both arms before returning. */
817 if (cmd
->control_type
== if_control
)
819 fputs_filtered ("if ", gdb_stdout
);
820 fputs_filtered (cmd
->line
, gdb_stdout
);
821 fputs_filtered ("\n", gdb_stdout
);
823 print_command_line (cmd
->body_list
[0], depth
+ 1);
825 /* Show the false arm if it exists. */
826 if (cmd
->body_count
== 2)
830 for (i
= 0; i
< depth
; i
++)
831 fputs_filtered (" ", gdb_stdout
);
833 fputs_filtered ("else\n", gdb_stdout
);
834 print_command_line (cmd
->body_list
[1], depth
+ 1);
838 for (i
= 0; i
< depth
; i
++)
839 fputs_filtered (" ", gdb_stdout
);
841 fputs_filtered ("end\n", gdb_stdout
);
845 /* Execute the command in CMD. */
847 enum command_control_type
848 execute_control_command (cmd
)
849 struct command_line
*cmd
;
851 struct expression
*expr
;
852 struct command_line
*current
;
853 struct cleanup
*old_chain
= 0;
857 enum command_control_type ret
;
860 switch (cmd
->control_type
)
863 /* A simple command, execute it and return. */
864 new_line
= insert_args (cmd
->line
);
866 return invalid_control
;
867 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
869 execute_command (new_line
, 0);
870 ret
= cmd
->control_type
;
873 case continue_control
:
875 /* Return for "continue", and "break" so we can either
876 continue the loop at the top, or break out. */
877 ret
= cmd
->control_type
;
882 /* Parse the loop control expression for the while statement. */
883 new_line
= insert_args (cmd
->line
);
885 return invalid_control
;
886 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
888 expr
= parse_expression (new_line
);
889 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
891 ret
= simple_control
;
894 /* Keep iterating so long as the expression is true. */
901 /* Evaluate the expression. */
902 val_mark
= value_mark ();
903 val
= evaluate_expression (expr
);
904 cond_result
= value_true (val
);
905 value_free_to_mark (val_mark
);
907 /* If the value is false, then break out of the loop. */
911 /* Execute the body of the while statement. */
912 current
= *cmd
->body_list
;
915 ret
= execute_control_command (current
);
917 /* If we got an error, or a "break" command, then stop
919 if (ret
== invalid_control
|| ret
== break_control
)
925 /* If we got a "continue" command, then restart the loop
927 if (ret
== continue_control
)
930 /* Get the next statement. */
931 current
= current
->next
;
935 /* Reset RET so that we don't recurse the break all the way down. */
936 if (ret
== break_control
)
937 ret
= simple_control
;
944 new_line
= insert_args (cmd
->line
);
946 return invalid_control
;
947 old_chain
= make_cleanup ((make_cleanup_func
) free_current_contents
,
949 /* Parse the conditional for the if statement. */
950 expr
= parse_expression (new_line
);
951 make_cleanup ((make_cleanup_func
) free_current_contents
, &expr
);
954 ret
= simple_control
;
956 /* Evaluate the conditional. */
957 val_mark
= value_mark ();
958 val
= evaluate_expression (expr
);
960 /* Choose which arm to take commands from based on the value of the
961 conditional expression. */
962 if (value_true (val
))
963 current
= *cmd
->body_list
;
964 else if (cmd
->body_count
== 2)
965 current
= *(cmd
->body_list
+ 1);
966 value_free_to_mark (val_mark
);
968 /* Execute commands in the given arm. */
971 ret
= execute_control_command (current
);
973 /* If we got an error, get out. */
974 if (ret
!= simple_control
)
977 /* Get the next statement in the body. */
978 current
= current
->next
;
985 warning ("Invalid control type in command structure.");
986 return invalid_control
;
990 do_cleanups (old_chain
);
995 /* "while" command support. Executes a body of statements while the
996 loop condition is nonzero. */
999 while_command (arg
, from_tty
)
1003 struct command_line
*command
= NULL
;
1006 command
= get_command_line (while_control
, arg
);
1008 if (command
== NULL
)
1011 execute_control_command (command
);
1012 free_command_lines (&command
);
1015 /* "if" command support. Execute either the true or false arm depending
1016 on the value of the if conditional. */
1019 if_command (arg
, from_tty
)
1023 struct command_line
*command
= NULL
;
1026 command
= get_command_line (if_control
, arg
);
1028 if (command
== NULL
)
1031 execute_control_command (command
);
1032 free_command_lines (&command
);
1039 struct user_args
*oargs
= user_args
;
1041 fatal ("Internal error, arg_cleanup called with no user args.\n");
1043 user_args
= user_args
->next
;
1047 /* Bind the incomming arguments for a user defined command to
1048 $arg0, $arg1 ... $argMAXUSERARGS. */
1050 static struct cleanup
*
1054 struct user_args
*args
;
1055 struct cleanup
*old_chain
;
1056 unsigned int arg_count
= 0;
1058 args
= (struct user_args
*)xmalloc (sizeof (struct user_args
));
1059 memset (args
, 0, sizeof (struct user_args
));
1061 args
->next
= user_args
;
1064 old_chain
= make_cleanup ((make_cleanup_func
) arg_cleanup
, 0);
1076 if (arg_count
>= MAXUSERARGS
)
1078 error ("user defined function may only have %d arguments.\n",
1083 /* Strip whitespace. */
1084 while (*p
== ' ' || *p
== '\t')
1087 /* P now points to an argument. */
1089 user_args
->a
[arg_count
].arg
= p
;
1091 /* Get to the end of this argument. */
1094 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
1100 else if (*p
== '\\')
1123 user_args
->a
[arg_count
].len
= p
- start_arg
;
1130 /* Given character string P, return a point to the first argument ($arg),
1131 or NULL if P contains no arguments. */
1137 while ((p
= strchr (p
, '$')))
1139 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
1146 /* Insert the user defined arguments stored in user_arg into the $arg
1147 arguments found in line, with the updated copy being placed into nline. */
1153 char *p
, *save_line
, *new_line
;
1156 /* First we need to know how much memory to allocate for the new line. */
1159 while ((p
= locate_arg (line
)))
1164 if (i
>= user_args
->count
)
1166 error ("Missing argument %d in user function.\n", i
);
1169 len
+= user_args
->a
[i
].len
;
1173 /* Don't forget the tail. */
1174 len
+= strlen (line
);
1176 /* Allocate space for the new line and fill it in. */
1177 new_line
= (char *)xmalloc (len
+ 1);
1178 if (new_line
== NULL
)
1181 /* Restore pointer to beginning of old line. */
1184 /* Save pointer to beginning of new line. */
1185 save_line
= new_line
;
1187 while ((p
= locate_arg (line
)))
1191 memcpy (new_line
, line
, p
- line
);
1192 new_line
+= p
- line
;
1195 len
= user_args
->a
[i
].len
;
1198 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
1203 /* Don't forget the tail. */
1204 strcpy (new_line
, line
);
1206 /* Return a pointer to the beginning of the new line. */
1211 execute_user_command (c
, args
)
1212 struct cmd_list_element
*c
;
1215 register struct command_line
*cmdlines
;
1216 struct cleanup
*old_chain
;
1217 enum command_control_type ret
;
1219 old_chain
= setup_user_args (args
);
1221 cmdlines
= c
->user_commands
;
1226 /* Set the instream to 0, indicating execution of a
1227 user-defined function. */
1228 old_chain
= make_cleanup ((make_cleanup_func
) source_cleanup
, instream
);
1229 instream
= (FILE *) 0;
1232 ret
= execute_control_command (cmdlines
);
1233 if (ret
!= simple_control
&& ret
!= break_control
)
1235 warning ("Error in control structure.\n");
1238 cmdlines
= cmdlines
->next
;
1240 do_cleanups (old_chain
);
1243 /* Execute the line P as a command.
1244 Pass FROM_TTY as second argument to the defining function. */
1247 execute_command (p
, from_tty
)
1251 register struct cmd_list_element
*c
;
1252 register enum language flang
;
1253 static int warned
= 0;
1254 /* FIXME: These should really be in an appropriate header file */
1255 extern void serial_log_command
PARAMS ((const char *));
1259 /* Force cleanup of any alloca areas if using C alloca instead of
1260 a builtin alloca. */
1263 /* This can happen when command_line_input hits end of file. */
1267 serial_log_command (p
);
1269 while (*p
== ' ' || *p
== '\t') p
++;
1274 c
= lookup_cmd (&p
, cmdlist
, "", 0, 1);
1275 /* Pass null arg rather than an empty one. */
1278 /* Clear off trailing whitespace, except for set and complete command. */
1279 if (arg
&& c
->type
!= set_cmd
&& c
->function
.cfunc
!= complete_command
)
1281 p
= arg
+ strlen (arg
) - 1;
1282 while (p
>= arg
&& (*p
== ' ' || *p
== '\t'))
1287 /* If this command has been hooked, run the hook first. */
1289 execute_user_command (c
->hook
, (char *)0);
1291 if (c
->class == class_user
)
1292 execute_user_command (c
, arg
);
1293 else if (c
->type
== set_cmd
|| c
->type
== show_cmd
)
1294 do_setshow_command (arg
, from_tty
& caution
, c
);
1295 else if (c
->function
.cfunc
== NO_FUNCTION
)
1296 error ("That is not a command, just a help topic.");
1297 else if (call_command_hook
)
1298 call_command_hook (c
, arg
, from_tty
& caution
);
1300 (*c
->function
.cfunc
) (arg
, from_tty
& caution
);
1303 /* Tell the user if the language has changed (except first time). */
1304 if (current_language
!= expected_language
)
1306 if (language_mode
== language_mode_auto
) {
1307 language_info (1); /* Print what changed. */
1312 /* Warn the user if the working language does not match the
1313 language of the current frame. Only warn the user if we are
1314 actually running the program, i.e. there is a stack. */
1315 /* FIXME: This should be cacheing the frame and only running when
1316 the frame changes. */
1318 if (target_has_stack
)
1320 flang
= get_frame_language ();
1322 && flang
!= language_unknown
1323 && flang
!= current_language
->la_language
)
1325 printf_filtered ("%s\n", lang_frame_mismatch_warn
);
1332 /* NOTE 4/29/99: This function will be static again, once we modify
1333 gdb to use the event loop as the default command loop and we merge
1334 event-top.c into this file, top.c */
1336 command_loop_marker (foo
)
1341 /* Read commands from `instream' and execute them
1342 until end of file or error reading instream. */
1347 struct cleanup
*old_chain
;
1349 int stdin_is_tty
= ISATTY (stdin
);
1350 long time_at_cmd_start
;
1352 long space_at_cmd_start
= 0;
1354 extern int display_time
;
1355 extern int display_space
;
1357 while (instream
&& !feof (instream
))
1360 extern int insert_mode
;
1362 if (window_hook
&& instream
== stdin
)
1363 (*window_hook
) (instream
, prompt
);
1366 if (instream
== stdin
&& stdin_is_tty
)
1367 reinitialize_more_filter ();
1368 old_chain
= make_cleanup ((make_cleanup_func
) command_loop_marker
, 0);
1371 /* A bit of paranoia: I want to make sure the "insert_mode" global
1372 * is clear except when it is being used for command-line editing
1373 * (see tuiIO.c, utils.c); otherwise normal output will
1374 * get messed up in the TUI. So clear it before/after
1375 * the command-line-input call. - RT
1379 /* Get a command-line. This calls the readline package. */
1380 command
= command_line_input (instream
== stdin
? prompt
: (char *) NULL
,
1381 instream
== stdin
, "prompt");
1388 time_at_cmd_start
= get_run_time ();
1393 extern char **environ
;
1394 char *lim
= (char *) sbrk (0);
1396 space_at_cmd_start
= (long) (lim
- (char *) &environ
);
1400 execute_command (command
, instream
== stdin
);
1401 /* Do any commands attached to breakpoint we stopped at. */
1402 bpstat_do_actions (&stop_bpstat
);
1403 do_cleanups (old_chain
);
1407 long cmd_time
= get_run_time () - time_at_cmd_start
;
1409 printf_unfiltered ("Command execution time: %ld.%06ld\n",
1410 cmd_time
/ 1000000, cmd_time
% 1000000);
1416 extern char **environ
;
1417 char *lim
= (char *) sbrk (0);
1418 long space_now
= lim
- (char *) &environ
;
1419 long space_diff
= space_now
- space_at_cmd_start
;
1421 printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1423 (space_diff
>= 0 ? '+' : '-'),
1430 /* Commands call this if they do not want to be repeated by null lines. */
1438 /* If we aren't reading from standard input, we are saving the last
1439 thing read from stdin in line and don't want to delete it. Null lines
1440 won't repeat here in any case. */
1441 if (instream
== stdin
)
1445 /* Read a line from the stream "instream" without command line editing.
1447 It prints PRROMPT once at the start.
1448 Action is compatible with "readline", e.g. space for the result is
1449 malloc'd and should be freed by the caller.
1451 A NULL return means end of file. */
1453 gdb_readline (prrompt
)
1458 int input_index
= 0;
1459 int result_size
= 80;
1463 /* Don't use a _filtered function here. It causes the assumed
1464 character position to be off, since the newline we read from
1465 the user is not accounted for. */
1466 fputs_unfiltered (prrompt
, gdb_stdout
);
1468 /* Move to a new line so the entered line doesn't have a prompt
1469 on the front of it. */
1470 fputs_unfiltered ("\n", gdb_stdout
);
1472 gdb_flush (gdb_stdout
);
1475 result
= (char *) xmalloc (result_size
);
1479 /* Read from stdin if we are executing a user defined command.
1480 This is the right thing for prompt_for_continue, at least. */
1481 c
= fgetc (instream
? instream
: stdin
);
1485 if (input_index
> 0)
1486 /* The last line does not end with a newline. Return it, and
1487 if we are called again fgetc will still return EOF and
1488 we'll return NULL then. */
1495 #ifndef CRLF_SOURCE_FILES
1499 if (input_index
> 0 && result
[input_index
- 1] == '\r')
1505 result
[input_index
++] = c
;
1506 while (input_index
>= result_size
)
1509 result
= (char *) xrealloc (result
, result_size
);
1513 result
[input_index
++] = '\0';
1517 /* Variables which control command line editing and history
1518 substitution. These variables are given default values at the end
1520 static int command_editing_p
;
1521 /* NOTE 4/29/99: This variable will be static again, once we modify
1522 gdb to use the event loop as the default command loop and we merge
1523 event-top.c into this file, top.c */
1524 /* static */ int history_expansion_p
;
1525 static int write_history_p
;
1526 static int history_size
;
1527 static char *history_filename
;
1529 /* readline uses the word breaks for two things:
1530 (1) In figuring out where to point the TEXT parameter to the
1531 rl_completion_entry_function. Since we don't use TEXT for much,
1532 it doesn't matter a lot what the word breaks are for this purpose, but
1533 it does affect how much stuff M-? lists.
1534 (2) If one of the matches contains a word break character, readline
1535 will quote it. That's why we switch between
1536 gdb_completer_word_break_characters and
1537 gdb_completer_command_word_break_characters. I'm not sure when
1538 we need this behavior (perhaps for funky characters in C++ symbols?). */
1540 /* Variables which are necessary for fancy command line editing. */
1541 char *gdb_completer_word_break_characters
=
1542 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1544 /* When completing on command names, we remove '-' from the list of
1545 word break characters, since we use it in command names. If the
1546 readline library sees one in any of the current completion strings,
1547 it thinks that the string needs to be quoted and automatically supplies
1549 char *gdb_completer_command_word_break_characters
=
1550 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1552 /* Characters that can be used to quote completion strings. Note that we
1553 can't include '"' because the gdb C parser treats such quoted sequences
1555 char *gdb_completer_quote_characters
=
1558 /* Functions that are used as part of the fancy command line editing. */
1560 /* This can be used for functions which don't want to complete on symbols
1561 but don't want to complete on anything else either. */
1564 noop_completer (text
, prefix
)
1571 /* Complete on filenames. */
1573 filename_completer (text
, word
)
1577 /* From readline. */
1578 extern char *filename_completion_function
PARAMS ((char *, int));
1579 int subsequent_name
;
1581 int return_val_used
;
1582 int return_val_alloced
;
1584 return_val_used
= 0;
1585 /* Small for testing. */
1586 return_val_alloced
= 1;
1587 return_val
= (char **) xmalloc (return_val_alloced
* sizeof (char *));
1589 subsequent_name
= 0;
1593 p
= filename_completion_function (text
, subsequent_name
);
1594 if (return_val_used
>= return_val_alloced
)
1596 return_val_alloced
*= 2;
1598 (char **) xrealloc (return_val
,
1599 return_val_alloced
* sizeof (char *));
1603 return_val
[return_val_used
++] = p
;
1606 /* Like emacs, don't complete on old versions. Especially useful
1607 in the "source" command. */
1608 if (p
[strlen (p
) - 1] == '~')
1614 /* Return exactly p. */
1615 return_val
[return_val_used
++] = p
;
1616 else if (word
> text
)
1618 /* Return some portion of p. */
1619 q
= xmalloc (strlen (p
) + 5);
1620 strcpy (q
, p
+ (word
- text
));
1621 return_val
[return_val_used
++] = q
;
1626 /* Return some of TEXT plus p. */
1627 q
= xmalloc (strlen (p
) + (text
- word
) + 5);
1628 strncpy (q
, word
, text
- word
);
1629 q
[text
- word
] = '\0';
1631 return_val
[return_val_used
++] = q
;
1635 subsequent_name
= 1;
1638 /* There is no way to do this just long enough to affect quote inserting
1639 without also affecting the next completion. This should be fixed in
1641 /* Insure that readline does the right thing
1642 with respect to inserting quotes. */
1643 rl_completer_word_break_characters
= "";
1648 /* Here are some useful test cases for completion. FIXME: These should
1649 be put in the test suite. They should be tested with both M-? and TAB.
1651 "show output-" "radix"
1652 "show output" "-radix"
1653 "p" ambiguous (commands starting with p--path, print, printf, etc.)
1654 "p " ambiguous (all symbols)
1655 "info t foo" no completions
1656 "info t " no completions
1657 "info t" ambiguous ("info target", "info terminal", etc.)
1658 "info ajksdlfk" no completions
1659 "info ajksdlfk " no completions
1661 "info " ambiguous (all info commands)
1662 "p \"a" no completions (string constant)
1663 "p 'a" ambiguous (all symbols starting with a)
1664 "p b-a" ambiguous (all symbols starting with a)
1665 "p b-" ambiguous (all symbols)
1666 "file Make" "file" (word break hard to screw up here)
1667 "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1670 /* Generate completions one by one for the completer. Each time we are
1671 called return another potential completion to the caller.
1672 line_completion just completes on commands or passes the buck to the
1673 command's completer function, the stuff specific to symbol completion
1674 is in make_symbol_completion_list.
1676 TEXT is the caller's idea of the "word" we are looking at.
1678 MATCHES is the number of matches that have currently been collected from
1679 calling this completion function. When zero, then we need to initialize,
1680 otherwise the initialization has already taken place and we can just
1681 return the next potential completion string.
1683 LINE_BUFFER is available to be looked at; it contains the entire text
1684 of the line. POINT is the offset in that line of the cursor. You
1685 should pretend that the line ends at POINT.
1687 Returns NULL if there are no more completions, else a pointer to a string
1688 which is a possible completion, it is the caller's responsibility to
1692 line_completion_function (text
, matches
, line_buffer
, point
)
1698 static char **list
= (char **)NULL
; /* Cache of completions */
1699 static int index
; /* Next cached completion */
1700 char *output
= NULL
;
1701 char *tmp_command
, *p
;
1702 /* Pointer within tmp_command which corresponds to text. */
1704 struct cmd_list_element
*c
, *result_list
;
1708 /* The caller is beginning to accumulate a new set of completions, so
1709 we need to find all of them now, and cache them for returning one at
1710 a time on future calls. */
1714 /* Free the storage used by LIST, but not by the strings inside.
1715 This is because rl_complete_internal () frees the strings. */
1721 /* Choose the default set of word break characters to break completions.
1722 If we later find out that we are doing completions on command strings
1723 (as opposed to strings supplied by the individual command completer
1724 functions, which can be any string) then we will switch to the
1725 special word break set for command strings, which leaves out the
1726 '-' character used in some commands. */
1728 rl_completer_word_break_characters
=
1729 gdb_completer_word_break_characters
;
1731 /* Decide whether to complete on a list of gdb commands or on symbols. */
1732 tmp_command
= (char *) alloca (point
+ 1);
1735 strncpy (tmp_command
, line_buffer
, point
);
1736 tmp_command
[point
] = '\0';
1737 /* Since text always contains some number of characters leading up
1738 to point, we can find the equivalent position in tmp_command
1739 by subtracting that many characters from the end of tmp_command. */
1740 word
= tmp_command
+ point
- strlen (text
);
1744 /* An empty line we want to consider ambiguous; that is, it
1745 could be any command. */
1746 c
= (struct cmd_list_element
*) -1;
1751 c
= lookup_cmd_1 (&p
, cmdlist
, &result_list
, 1);
1754 /* Move p up to the next interesting thing. */
1755 while (*p
== ' ' || *p
== '\t')
1762 /* It is an unrecognized command. So there are no
1763 possible completions. */
1766 else if (c
== (struct cmd_list_element
*) -1)
1770 /* lookup_cmd_1 advances p up to the first ambiguous thing, but
1771 doesn't advance over that thing itself. Do so now. */
1773 while (*q
&& (isalnum (*q
) || *q
== '-' || *q
== '_'))
1775 if (q
!= tmp_command
+ point
)
1777 /* There is something beyond the ambiguous
1778 command, so there are no possible completions. For
1779 example, "info t " or "info t foo" does not complete
1780 to anything, because "info t" can be "info target" or
1786 /* We're trying to complete on the command which was ambiguous.
1787 This we can deal with. */
1790 list
= complete_on_cmdlist (*result_list
->prefixlist
, p
,
1795 list
= complete_on_cmdlist (cmdlist
, p
, word
);
1797 /* Insure that readline does the right thing with respect to
1798 inserting quotes. */
1799 rl_completer_word_break_characters
=
1800 gdb_completer_command_word_break_characters
;
1805 /* We've recognized a full command. */
1807 if (p
== tmp_command
+ point
)
1809 /* There is no non-whitespace in the line beyond the command. */
1811 if (p
[-1] == ' ' || p
[-1] == '\t')
1813 /* The command is followed by whitespace; we need to complete
1814 on whatever comes after command. */
1817 /* It is a prefix command; what comes after it is
1818 a subcommand (e.g. "info "). */
1819 list
= complete_on_cmdlist (*c
->prefixlist
, p
, word
);
1821 /* Insure that readline does the right thing
1822 with respect to inserting quotes. */
1823 rl_completer_word_break_characters
=
1824 gdb_completer_command_word_break_characters
;
1828 list
= complete_on_enum (c
->enums
, p
, word
);
1829 rl_completer_word_break_characters
=
1830 gdb_completer_command_word_break_characters
;
1834 /* It is a normal command; what comes after it is
1835 completed by the command's completer function. */
1836 list
= (*c
->completer
) (p
, word
);
1841 /* The command is not followed by whitespace; we need to
1842 complete on the command itself. e.g. "p" which is a
1843 command itself but also can complete to "print", "ptype"
1847 /* Find the command we are completing on. */
1849 while (q
> tmp_command
)
1851 if (isalnum (q
[-1]) || q
[-1] == '-' || q
[-1] == '_')
1857 list
= complete_on_cmdlist (result_list
, q
, word
);
1859 /* Insure that readline does the right thing
1860 with respect to inserting quotes. */
1861 rl_completer_word_break_characters
=
1862 gdb_completer_command_word_break_characters
;
1867 /* There is non-whitespace beyond the command. */
1869 if (c
->prefixlist
&& !c
->allow_unknown
)
1871 /* It is an unrecognized subcommand of a prefix command,
1872 e.g. "info adsfkdj". */
1877 list
= complete_on_enum (c
->enums
, p
, word
);
1881 /* It is a normal command. */
1882 list
= (*c
->completer
) (p
, word
);
1888 /* If we found a list of potential completions during initialization then
1889 dole them out one at a time. The vector of completions is NULL
1890 terminated, so after returning the last one, return NULL (and continue
1891 to do so) each time we are called after that, until a new list is
1896 output
= list
[index
];
1904 /* Can't do this because readline hasn't yet checked the word breaks
1905 for figuring out whether to insert a quote. */
1907 /* Make sure the word break characters are set back to normal for the
1908 next time that readline tries to complete something. */
1909 rl_completer_word_break_characters
=
1910 gdb_completer_word_break_characters
;
1916 /* Line completion interface function for readline. */
1919 readline_line_completion_function (text
, matches
)
1923 return line_completion_function (text
, matches
, rl_line_buffer
, rl_point
);
1926 /* Skip over a possibly quoted word (as defined by the quote characters
1927 and word break characters the completer uses). Returns pointer to the
1928 location after the "word". */
1934 char quote_char
= '\0';
1937 for (scan
= str
; *scan
!= '\0'; scan
++)
1939 if (quote_char
!= '\0')
1941 /* Ignore everything until the matching close quote char */
1942 if (*scan
== quote_char
)
1944 /* Found matching close quote. */
1949 else if (strchr (gdb_completer_quote_characters
, *scan
))
1951 /* Found start of a quoted string. */
1954 else if (strchr (gdb_completer_word_break_characters
, *scan
))
1968 #if STOP_SIGNAL == SIGTSTP
1969 signal (SIGTSTP
, SIG_DFL
);
1971 kill (getpid (), SIGTSTP
);
1972 signal (SIGTSTP
, stop_sig
);
1974 signal (STOP_SIGNAL
, stop_sig
);
1976 printf_unfiltered ("%s", prompt
);
1977 gdb_flush (gdb_stdout
);
1979 /* Forget about any previous command -- null line now will do nothing. */
1982 #endif /* STOP_SIGNAL */
1984 /* Initialize signal handlers. */
1989 /* Under System V the default disposition of a signal is reinstated after
1990 the signal is caught and delivered to an application process. On such
1991 systems one must restore the replacement signal handler if one wishes
1992 to continue handling the signal in one's program. On BSD systems this
1993 is not needed but it is harmless, and it simplifies the code to just do
1994 it unconditionally. */
1995 signal (signo
, do_nothing
);
2001 signal (SIGINT
, request_quit
);
2003 /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2004 to the inferior and breakpoints will be ignored. */
2006 signal (SIGTRAP
, SIG_DFL
);
2009 /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2010 passed to the inferior, which we don't want. It would be
2011 possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2012 on BSD4.3 systems using vfork, that can affect the
2013 GDB process as well as the inferior (the signal handling tables
2014 might be in memory, shared between the two). Since we establish
2015 a handler for SIGQUIT, when we call exec it will set the signal
2016 to SIG_DFL for us. */
2017 signal (SIGQUIT
, do_nothing
);
2019 if (signal (SIGHUP
, do_nothing
) != SIG_IGN
)
2020 signal (SIGHUP
, disconnect
);
2022 signal (SIGFPE
, float_handler
);
2024 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2025 signal (SIGWINCH
, SIGWINCH_HANDLER
);
2029 /* Read one line from the command input stream `instream'
2030 into the local static buffer `linebuffer' (whose current length
2032 The buffer is made bigger as necessary.
2033 Returns the address of the start of the line.
2035 NULL is returned for end of file.
2037 *If* the instream == stdin & stdin is a terminal, the line read
2038 is copied into the file line saver (global var char *line,
2039 length linesize) so that it can be duplicated.
2041 This routine either uses fancy command line editing or
2042 simple input as the user has requested. */
2045 command_line_input (prrompt
, repeat
, annotation_suffix
)
2048 char *annotation_suffix
;
2050 static char *linebuffer
= 0;
2051 static unsigned linelength
= 0;
2055 char *local_prompt
= prrompt
;
2059 /* The annotation suffix must be non-NULL. */
2060 if (annotation_suffix
== NULL
)
2061 annotation_suffix
= "";
2063 if (annotation_level
> 1 && instream
== stdin
)
2065 local_prompt
= alloca ((prrompt
== NULL
? 0 : strlen (prrompt
))
2066 + strlen (annotation_suffix
) + 40);
2067 if (prrompt
== NULL
)
2068 local_prompt
[0] = '\0';
2070 strcpy (local_prompt
, prrompt
);
2071 strcat (local_prompt
, "\n\032\032");
2072 strcat (local_prompt
, annotation_suffix
);
2073 strcat (local_prompt
, "\n");
2076 if (linebuffer
== 0)
2079 linebuffer
= (char *) xmalloc (linelength
);
2084 /* Control-C quits instantly if typed while in this loop
2085 since it should not wait until the user types a newline. */
2089 signal (STOP_SIGNAL
, stop_sig
);
2094 /* Make sure that all output has been output. Some machines may let
2095 you get away with leaving out some of the gdb_flush, but not all. */
2097 gdb_flush (gdb_stdout
);
2098 gdb_flush (gdb_stderr
);
2100 if (source_file_name
!= NULL
)
2102 ++source_line_number
;
2103 sprintf (source_error
,
2104 "%s%s:%d: Error in sourced command file:\n",
2107 source_line_number
);
2108 error_pre_print
= source_error
;
2111 if (annotation_level
> 1 && instream
== stdin
)
2113 printf_unfiltered ("\n\032\032pre-");
2114 printf_unfiltered (annotation_suffix
);
2115 printf_unfiltered ("\n");
2118 /* Don't use fancy stuff if not talking to stdin. */
2119 if (readline_hook
&& instream
== NULL
)
2121 rl
= (*readline_hook
) (local_prompt
);
2123 else if (command_editing_p
&& instream
== stdin
&& ISATTY (instream
))
2125 rl
= readline (local_prompt
);
2129 rl
= gdb_readline (local_prompt
);
2132 if (annotation_level
> 1 && instream
== stdin
)
2134 printf_unfiltered ("\n\032\032post-");
2135 printf_unfiltered (annotation_suffix
);
2136 printf_unfiltered ("\n");
2139 if (!rl
|| rl
== (char *) EOF
)
2144 if (strlen(rl
) + 1 + (p
- linebuffer
) > linelength
)
2146 linelength
= strlen(rl
) + 1 + (p
- linebuffer
);
2147 nline
= (char *) xrealloc (linebuffer
, linelength
);
2148 p
+= nline
- linebuffer
;
2152 /* Copy line. Don't copy null at end. (Leaves line alone
2153 if this was just a newline) */
2157 free (rl
); /* Allocated in readline. */
2159 if (p
== linebuffer
|| *(p
- 1) != '\\')
2162 p
--; /* Put on top of '\'. */
2163 local_prompt
= (char *) 0;
2168 signal (STOP_SIGNAL
, SIG_DFL
);
2175 #define SERVER_COMMAND_LENGTH 7
2177 (p
- linebuffer
> SERVER_COMMAND_LENGTH
)
2178 && STREQN (linebuffer
, "server ", SERVER_COMMAND_LENGTH
);
2181 /* Note that we don't set `line'. Between this and the check in
2182 dont_repeat, this insures that repeating will still do the
2185 return linebuffer
+ SERVER_COMMAND_LENGTH
;
2188 /* Do history expansion if that is wished. */
2189 if (history_expansion_p
&& instream
== stdin
2190 && ISATTY (instream
))
2192 char *history_value
;
2195 *p
= '\0'; /* Insert null now. */
2196 expanded
= history_expand (linebuffer
, &history_value
);
2199 /* Print the changes. */
2200 printf_unfiltered ("%s\n", history_value
);
2202 /* If there was an error, call this function again. */
2205 free (history_value
);
2206 return command_line_input (prrompt
, repeat
, annotation_suffix
);
2208 if (strlen (history_value
) > linelength
)
2210 linelength
= strlen (history_value
) + 1;
2211 linebuffer
= (char *) xrealloc (linebuffer
, linelength
);
2213 strcpy (linebuffer
, history_value
);
2214 p
= linebuffer
+ strlen(linebuffer
);
2215 free (history_value
);
2219 /* If we just got an empty line, and that is supposed
2220 to repeat the previous command, return the value in the
2222 if (repeat
&& p
== linebuffer
)
2224 for (p1
= linebuffer
; *p1
== ' ' || *p1
== '\t'; p1
++) ;
2230 /* Add line to history if appropriate. */
2231 if (instream
== stdin
2232 && ISATTY (stdin
) && *linebuffer
)
2233 add_history (linebuffer
);
2235 /* Note: lines consisting solely of comments are added to the command
2236 history. This is useful when you type a command, and then
2237 realize you don't want to execute it quite yet. You can comment
2238 out the command and then later fetch it from the value history
2239 and remove the '#'. The kill ring is probably better, but some
2240 people are in the habit of commenting things out. */
2242 *p1
= '\0'; /* Found a comment. */
2244 /* Save into global buffer if appropriate. */
2247 if (linelength
> linesize
)
2249 line
= xrealloc (line
, linelength
);
2250 linesize
= linelength
;
2252 strcpy (line
, linebuffer
);
2260 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2261 code bodies. This is typically used when we encounter an "else"
2262 clause for an "if" command. */
2265 realloc_body_list (command
, new_length
)
2266 struct command_line
*command
;
2270 struct command_line
**body_list
;
2272 n
= command
->body_count
;
2274 /* Nothing to do? */
2275 if (new_length
<= n
)
2278 body_list
= (struct command_line
**)
2279 xmalloc (sizeof (struct command_line
*) * new_length
);
2281 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
2283 free (command
->body_list
);
2284 command
->body_list
= body_list
;
2285 command
->body_count
= new_length
;
2288 /* Read one line from the input stream. If the command is an "else" or
2289 "end", return such an indication to the caller. */
2291 static enum misc_command_type
2292 read_next_line (command
)
2293 struct command_line
**command
;
2295 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
2298 if (control_level
>= 254)
2299 error ("Control nesting too deep!\n");
2301 /* Set a prompt based on the nesting of the control commands. */
2302 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
2304 for (i
= 0; i
< control_level
; i
++)
2305 control_prompt
[i
] = ' ';
2306 control_prompt
[i
] = '>';
2307 control_prompt
[i
+1] = '\0';
2308 prompt_ptr
= (char *)&control_prompt
[0];
2313 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
2315 /* Not sure what to do here. */
2319 /* Strip leading and trailing whitespace. */
2320 while (*p
== ' ' || *p
== '\t')
2323 p1
= p
+ strlen (p
);
2324 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
2327 /* Blanks and comments don't really do anything, but we need to
2328 distinguish them from else, end and other commands which can be
2330 if (p1
== p
|| p
[0] == '#')
2333 /* Is this the end of a simple, while, or if control structure? */
2334 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
2337 /* Is the else clause of an if control structure? */
2338 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
2339 return else_command
;
2341 /* Check for while, if, break, continue, etc and build a new command
2342 line structure for them. */
2343 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
2344 *command
= build_command_line (while_control
, p
+ 6);
2345 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
2346 *command
= build_command_line (if_control
, p
+ 3);
2347 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
2349 *command
= (struct command_line
*)
2350 xmalloc (sizeof (struct command_line
));
2351 (*command
)->next
= NULL
;
2352 (*command
)->line
= NULL
;
2353 (*command
)->control_type
= break_control
;
2354 (*command
)->body_count
= 0;
2355 (*command
)->body_list
= NULL
;
2357 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
2359 *command
= (struct command_line
*)
2360 xmalloc (sizeof (struct command_line
));
2361 (*command
)->next
= NULL
;
2362 (*command
)->line
= NULL
;
2363 (*command
)->control_type
= continue_control
;
2364 (*command
)->body_count
= 0;
2365 (*command
)->body_list
= NULL
;
2369 /* A normal command. */
2370 *command
= (struct command_line
*)
2371 xmalloc (sizeof (struct command_line
));
2372 (*command
)->next
= NULL
;
2373 (*command
)->line
= savestring (p
, p1
- p
);
2374 (*command
)->control_type
= simple_control
;
2375 (*command
)->body_count
= 0;
2376 (*command
)->body_list
= NULL
;
2379 /* Nothing special. */
2383 /* Recursively read in the control structures and create a command_line
2384 structure from them.
2386 The parent_control parameter is the control structure in which the
2387 following commands are nested. */
2389 static enum command_control_type
2390 recurse_read_control_structure (current_cmd
)
2391 struct command_line
*current_cmd
;
2393 int current_body
, i
;
2394 enum misc_command_type val
;
2395 enum command_control_type ret
;
2396 struct command_line
**body_ptr
, *child_tail
, *next
;
2401 /* Sanity checks. */
2402 if (current_cmd
->control_type
== simple_control
)
2404 error ("Recursed on a simple control type\n");
2405 return invalid_control
;
2408 if (current_body
> current_cmd
->body_count
)
2410 error ("Allocated body is smaller than this command type needs\n");
2411 return invalid_control
;
2414 /* Read lines from the input stream and build control structures. */
2420 val
= read_next_line (&next
);
2422 /* Just skip blanks and comments. */
2423 if (val
== nop_command
)
2426 if (val
== end_command
)
2428 if (current_cmd
->control_type
== while_control
2429 || current_cmd
->control_type
== if_control
)
2431 /* Success reading an entire control structure. */
2432 ret
= simple_control
;
2437 ret
= invalid_control
;
2442 /* Not the end of a control structure. */
2443 if (val
== else_command
)
2445 if (current_cmd
->control_type
== if_control
2446 && current_body
== 1)
2448 realloc_body_list (current_cmd
, 2);
2455 ret
= invalid_control
;
2462 child_tail
->next
= next
;
2466 body_ptr
= current_cmd
->body_list
;
2467 for (i
= 1; i
< current_body
; i
++)
2476 /* If the latest line is another control structure, then recurse
2478 if (next
->control_type
== while_control
2479 || next
->control_type
== if_control
)
2482 ret
= recurse_read_control_structure (next
);
2485 if (ret
!= simple_control
)
2495 /* Read lines from the input stream and accumulate them in a chain of
2496 struct command_line's, which is then returned. For input from a
2497 terminal, the special command "end" is used to mark the end of the
2498 input, and is not included in the returned chain of commands. */
2500 #define END_MESSAGE "End with a line saying just \"end\"."
2502 struct command_line
*
2503 read_command_lines (prompt
, from_tty
)
2507 struct command_line
*head
, *tail
, *next
;
2508 struct cleanup
*old_chain
;
2509 enum command_control_type ret
;
2510 enum misc_command_type val
;
2513 if (readline_begin_hook
)
2515 /* Note - intentional to merge messages with no newline */
2516 (*readline_begin_hook
) ("%s %s\n", prompt
, END_MESSAGE
);
2518 else if (from_tty
&& input_from_terminal_p ())
2520 printf_unfiltered ("%s\n%s\n", prompt
, END_MESSAGE
);
2521 gdb_flush (gdb_stdout
);
2529 val
= read_next_line (&next
);
2531 /* Ignore blank lines or comments. */
2532 if (val
== nop_command
)
2535 if (val
== end_command
)
2537 ret
= simple_control
;
2541 if (val
!= ok_command
)
2543 ret
= invalid_control
;
2547 if (next
->control_type
== while_control
2548 || next
->control_type
== if_control
)
2551 ret
= recurse_read_control_structure (next
);
2554 if (ret
== invalid_control
)
2565 old_chain
= make_cleanup ((make_cleanup_func
) free_command_lines
,
2575 if (ret
!= invalid_control
)
2577 discard_cleanups (old_chain
);
2580 do_cleanups (old_chain
);
2583 if (readline_end_hook
)
2585 (*readline_end_hook
) ();
2590 /* Free a chain of struct command_line's. */
2593 free_command_lines (lptr
)
2594 struct command_line
**lptr
;
2596 register struct command_line
*l
= *lptr
;
2597 register struct command_line
*next
;
2598 struct command_line
**blist
;
2603 if (l
->body_count
> 0)
2605 blist
= l
->body_list
;
2606 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
2607 free_command_lines (blist
);
2616 /* Add an element to the list of info subcommands. */
2619 add_info (name
, fun
, doc
)
2621 void (*fun
) PARAMS ((char *, int));
2624 add_cmd (name
, no_class
, fun
, doc
, &infolist
);
2627 /* Add an alias to the list of info subcommands. */
2630 add_info_alias (name
, oldname
, abbrev_flag
)
2635 add_alias_cmd (name
, oldname
, 0, abbrev_flag
, &infolist
);
2638 /* The "info" command is defined as a prefix, with allow_unknown = 0.
2639 Therefore, its own definition is called only for "info" with no args. */
2643 info_command (arg
, from_tty
)
2647 printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2648 help_list (infolist
, "info ", -1, gdb_stdout
);
2651 /* The "complete" command is used by Emacs to implement completion. */
2655 complete_command (arg
, from_tty
)
2667 argpoint
= strlen (arg
);
2669 for (completion
= line_completion_function (arg
, i
= 0, arg
, argpoint
);
2671 completion
= line_completion_function (arg
, ++i
, arg
, argpoint
))
2673 printf_unfiltered ("%s\n", completion
);
2678 /* The "show" command with no arguments shows all the settings. */
2682 show_command (arg
, from_tty
)
2686 cmd_show_list (showlist
, from_tty
, "");
2689 /* Add an element to the list of commands. */
2692 add_com (name
, class, fun
, doc
)
2694 enum command_class
class;
2695 void (*fun
) PARAMS ((char *, int));
2698 add_cmd (name
, class, fun
, doc
, &cmdlist
);
2701 /* Add an alias or abbreviation command to the list of commands. */
2704 add_com_alias (name
, oldname
, class, abbrev_flag
)
2707 enum command_class
class;
2710 add_alias_cmd (name
, oldname
, class, abbrev_flag
, &cmdlist
);
2717 error ("Argument required (%s).", why
);
2722 help_command (command
, from_tty
)
2724 int from_tty
; /* Ignored */
2726 help_cmd (command
, gdb_stdout
);
2730 validate_comname (comname
)
2736 error_no_arg ("name of command to define");
2741 if (!isalnum(*p
) && *p
!= '-' && *p
!= '_')
2742 error ("Junk in argument list: \"%s\"", p
);
2747 /* This is just a placeholder in the command data structures. */
2749 user_defined_command (ignore
, from_tty
)
2756 define_command (comname
, from_tty
)
2760 register struct command_line
*cmds
;
2761 register struct cmd_list_element
*c
, *newc
, *hookc
= 0;
2762 char *tem
= comname
;
2764 #define HOOK_STRING "hook-"
2767 validate_comname (comname
);
2769 /* Look it up, and verify that we got an exact match. */
2770 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
2771 if (c
&& !STREQ (comname
, c
->name
))
2776 if (c
->class == class_user
|| c
->class == class_alias
)
2777 tem
= "Redefine command \"%s\"? ";
2779 tem
= "Really redefine built-in command \"%s\"? ";
2780 if (!query (tem
, c
->name
))
2781 error ("Command \"%s\" not redefined.", c
->name
);
2784 /* If this new command is a hook, then mark the command which it
2785 is hooking. Note that we allow hooking `help' commands, so that
2786 we can hook the `stop' pseudo-command. */
2788 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
2790 /* Look up cmd it hooks, and verify that we got an exact match. */
2791 tem
= comname
+HOOK_LEN
;
2792 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
2793 if (hookc
&& !STREQ (comname
+HOOK_LEN
, hookc
->name
))
2797 warning ("Your new `%s' command does not hook any existing command.",
2799 if (!query ("Proceed? "))
2800 error ("Not confirmed.");
2804 comname
= savestring (comname
, strlen (comname
));
2806 /* If the rest of the commands will be case insensitive, this one
2807 should behave in the same manner. */
2808 for (tem
= comname
; *tem
; tem
++)
2809 if (isupper(*tem
)) *tem
= tolower(*tem
);
2811 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
2812 cmds
= read_command_lines (tmpbuf
, from_tty
);
2814 if (c
&& c
->class == class_user
)
2815 free_command_lines (&c
->user_commands
);
2817 newc
= add_cmd (comname
, class_user
, user_defined_command
,
2818 (c
&& c
->class == class_user
)
2819 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
2820 newc
->user_commands
= cmds
;
2822 /* If this new command is a hook, then mark both commands as being
2826 hookc
->hook
= newc
; /* Target gets hooked. */
2827 newc
->hookee
= hookc
; /* We are marked as hooking target cmd. */
2832 document_command (comname
, from_tty
)
2836 struct command_line
*doclines
;
2837 register struct cmd_list_element
*c
;
2838 char *tem
= comname
;
2841 validate_comname (comname
);
2843 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
2845 if (c
->class != class_user
)
2846 error ("Command \"%s\" is built-in.", comname
);
2848 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
2849 doclines
= read_command_lines (tmpbuf
, from_tty
);
2851 if (c
->doc
) free (c
->doc
);
2854 register struct command_line
*cl1
;
2855 register int len
= 0;
2857 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2858 len
+= strlen (cl1
->line
) + 1;
2860 c
->doc
= (char *) xmalloc (len
+ 1);
2863 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
2865 strcat (c
->doc
, cl1
->line
);
2867 strcat (c
->doc
, "\n");
2871 free_command_lines (&doclines
);
2874 /* Print the GDB banner. */
2876 print_gdb_version (stream
)
2879 /* From GNU coding standards, first line is meant to be easy for a
2880 program to parse, and is just canonical program name and version
2881 number, which starts after last space. */
2883 fprintf_filtered (stream
, "GNU gdb %s\n", version
);
2885 /* Second line is a copyright notice. */
2887 fprintf_filtered (stream
, "Copyright 1998 Free Software Foundation, Inc.\n");
2889 /* Following the copyright is a brief statement that the program is
2890 free software, that users are free to copy and change it on
2891 certain conditions, that it is covered by the GNU GPL, and that
2892 there is no warranty. */
2894 fprintf_filtered (stream
, "\
2895 GDB is free software, covered by the GNU General Public License, and you are\n\
2896 welcome to change it and/or distribute copies of it under certain conditions.\n\
2897 Type \"show copying\" to see the conditions.\n\
2898 There is absolutely no warranty for GDB. Type \"show warranty\" for details.\n");
2900 /* After the required info we print the configuration information. */
2902 fprintf_filtered (stream
, "This GDB was configured as \"");
2903 if (!STREQ (host_name
, target_name
))
2905 fprintf_filtered (stream
, "--host=%s --target=%s", host_name
, target_name
);
2909 fprintf_filtered (stream
, "%s", host_name
);
2911 fprintf_filtered (stream
, "\".");
2916 show_version (args
, from_tty
)
2921 print_gdb_version (gdb_stdout
);
2922 printf_filtered ("\n");
2926 /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb
2932 printf_unfiltered ("%s", prompt
);
2933 gdb_flush (gdb_stdout
);
2936 /* This replaces the above for the frontends: it returns a pointer
2948 /* ??rehrauer: I don't know why this fails, since it looks as though
2949 assignments to prompt are wrapped in calls to savestring...
2953 prompt
= savestring (s
, strlen (s
));
2957 /* If necessary, make the user confirm that we should quit. Return
2958 non-zero if we should quit, zero if we shouldn't. */
2963 if (inferior_pid
!= 0 && target_has_execution
)
2967 /* This is something of a hack. But there's no reliable way to
2968 see if a GUI is running. The `use_windows' variable doesn't
2971 s
= "A debugging session is active.\nDo you still want to close the debugger?";
2972 else if (attach_flag
)
2973 s
= "The program is running. Quit anyway (and detach it)? ";
2975 s
= "The program is running. Exit anyway? ";
2984 /* Quit without asking for confirmation. */
2987 quit_force (args
, from_tty
)
2993 /* An optional expression may be used to cause gdb to terminate with the
2994 value of that expression. */
2997 value_ptr val
= parse_and_eval (args
);
2999 exit_code
= (int) value_as_long (val
);
3002 if (inferior_pid
!= 0 && target_has_execution
)
3005 target_detach (args
, from_tty
);
3010 /* UDI wants this, to kill the TIP. */
3013 /* Save the history information if it is appropriate to do so. */
3014 if (write_history_p
&& history_filename
)
3015 write_history (history_filename
);
3017 do_final_cleanups(ALL_CLEANUPS
); /* Do any final cleanups before exiting */
3020 /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3021 /* The above does not need to be inside a tuiDo(), since
3022 * it is not manipulating the curses screen, but rather,
3023 * it is tearing it down.
3032 /* Handle the quit command. */
3035 quit_command (args
, from_tty
)
3039 if (! quit_confirm ())
3040 error ("Not confirmed.");
3041 quit_force (args
, from_tty
);
3044 /* Returns whether GDB is running on a terminal and whether the user
3045 desires that questions be asked of them on that terminal. */
3048 input_from_terminal_p ()
3050 return gdb_has_a_terminal () && (instream
== stdin
) & caution
;
3055 pwd_command (args
, from_tty
)
3059 if (args
) error ("The \"pwd\" command does not take an argument: %s", args
);
3060 getcwd (gdb_dirbuf
, sizeof (gdb_dirbuf
));
3062 if (!STREQ (gdb_dirbuf
, current_directory
))
3063 printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3064 current_directory
, gdb_dirbuf
);
3066 printf_unfiltered ("Working directory %s.\n", current_directory
);
3070 cd_command (dir
, from_tty
)
3075 /* Found something other than leading repetitions of "/..". */
3076 int found_real_path
;
3079 /* If the new directory is absolute, repeat is a no-op; if relative,
3080 repeat might be useful but is more likely to be a mistake. */
3084 error_no_arg ("new working directory");
3086 dir
= tilde_expand (dir
);
3087 make_cleanup (free
, dir
);
3089 if (chdir (dir
) < 0)
3090 perror_with_name (dir
);
3093 dir
= savestring (dir
, len
- (len
> 1 && SLASH_P(dir
[len
-1])));
3095 current_directory
= dir
;
3098 if (SLASH_P (current_directory
[0]) && current_directory
[1] == '\0')
3099 current_directory
= concat (current_directory
, dir
, NULL
);
3101 current_directory
= concat (current_directory
, SLASH_STRING
, dir
, NULL
);
3105 /* Now simplify any occurrences of `.' and `..' in the pathname. */
3107 found_real_path
= 0;
3108 for (p
= current_directory
; *p
;)
3110 if (SLASH_P (p
[0]) && p
[1] == '.' && (p
[2] == 0 || SLASH_P (p
[2])))
3112 else if (SLASH_P (p
[0]) && p
[1] == '.' && p
[2] == '.'
3113 && (p
[3] == 0 || SLASH_P (p
[3])))
3115 if (found_real_path
)
3117 /* Search backwards for the directory just before the "/.."
3118 and obliterate it and the "/..". */
3120 while (q
!= current_directory
&& ! SLASH_P (q
[-1]))
3123 if (q
== current_directory
)
3124 /* current_directory is
3125 a relative pathname ("can't happen"--leave it alone). */
3129 strcpy (q
- 1, p
+ 3);
3134 /* We are dealing with leading repetitions of "/..", for example
3135 "/../..", which is the Mach super-root. */
3140 found_real_path
= 1;
3145 forget_cached_source_info ();
3148 pwd_command ((char *) 0, 1);
3151 struct source_cleanup_lines_args
{
3154 char *old_pre_error
;
3155 char *old_error_pre_print
;
3159 source_cleanup_lines (args
)
3162 struct source_cleanup_lines_args
*p
=
3163 (struct source_cleanup_lines_args
*)args
;
3164 source_line_number
= p
->old_line
;
3165 source_file_name
= p
->old_file
;
3166 source_pre_error
= p
->old_pre_error
;
3167 error_pre_print
= p
->old_error_pre_print
;
3172 source_command (args
, from_tty
)
3177 struct cleanup
*old_cleanups
;
3179 struct source_cleanup_lines_args old_lines
;
3184 error ("source command requires pathname of file to source.");
3187 file
= tilde_expand (file
);
3188 old_cleanups
= make_cleanup (free
, file
);
3190 stream
= fopen (file
, FOPEN_RT
);
3194 perror_with_name (file
);
3199 make_cleanup ((make_cleanup_func
) fclose
, stream
);
3201 old_lines
.old_line
= source_line_number
;
3202 old_lines
.old_file
= source_file_name
;
3203 old_lines
.old_pre_error
= source_pre_error
;
3204 old_lines
.old_error_pre_print
= error_pre_print
;
3205 make_cleanup (source_cleanup_lines
, &old_lines
);
3206 source_line_number
= 0;
3207 source_file_name
= file
;
3208 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
3209 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
3210 make_cleanup (free
, source_pre_error
);
3211 /* This will get set every time we read a line. So it won't stay "" for
3213 error_pre_print
= "";
3215 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
3216 if (source_error_allocated
< needed_length
)
3218 source_error_allocated
*= 2;
3219 if (source_error_allocated
< needed_length
)
3220 source_error_allocated
= needed_length
;
3221 if (source_error
== NULL
)
3222 source_error
= xmalloc (source_error_allocated
);
3224 source_error
= xrealloc (source_error
, source_error_allocated
);
3227 read_command_file (stream
);
3229 do_cleanups (old_cleanups
);
3234 echo_command (text
, from_tty
)
3242 while ((c
= *p
++) != '\0')
3246 /* \ at end of argument is used after spaces
3247 so they won't be lost. */
3251 c
= parse_escape (&p
);
3253 printf_filtered ("%c", c
);
3256 printf_filtered ("%c", c
);
3259 /* Force this output to appear now. */
3261 gdb_flush (gdb_stdout
);
3266 dont_repeat_command (ignored
, from_tty
)
3270 *line
= 0; /* Can't call dont_repeat here because we're not
3271 necessarily reading from stdin. */
3274 /* Functions to manipulate command line editing control variables. */
3276 /* Number of commands to print in each call to show_commands. */
3277 #define Hist_print 10
3279 show_commands (args
, from_tty
)
3283 /* Index for history commands. Relative to history_base. */
3286 /* Number of the history entry which we are planning to display next.
3287 Relative to history_base. */
3290 /* The first command in the history which doesn't exist (i.e. one more
3291 than the number of the last command). Relative to history_base. */
3294 extern HIST_ENTRY
*history_get
PARAMS ((int));
3296 /* Print out some of the commands from the command history. */
3297 /* First determine the length of the history list. */
3298 hist_len
= history_size
;
3299 for (offset
= 0; offset
< history_size
; offset
++)
3301 if (!history_get (history_base
+ offset
))
3310 if (args
[0] == '+' && args
[1] == '\0')
3311 /* "info editing +" should print from the stored position. */
3314 /* "info editing <exp>" should print around command number <exp>. */
3315 num
= (parse_and_eval_address (args
) - history_base
) - Hist_print
/ 2;
3317 /* "show commands" means print the last Hist_print commands. */
3320 num
= hist_len
- Hist_print
;
3326 /* If there are at least Hist_print commands, we want to display the last
3327 Hist_print rather than, say, the last 6. */
3328 if (hist_len
- num
< Hist_print
)
3330 num
= hist_len
- Hist_print
;
3335 for (offset
= num
; offset
< num
+ Hist_print
&& offset
< hist_len
; offset
++)
3337 printf_filtered ("%5d %s\n", history_base
+ offset
,
3338 (history_get (history_base
+ offset
))->line
);
3341 /* The next command we want to display is the next one that we haven't
3345 /* If the user repeats this command with return, it should do what
3346 "show commands +" does. This is unnecessary if arg is null,
3347 because "show commands +" is not useful after "show commands". */
3348 if (from_tty
&& args
)
3355 /* Called by do_setshow_command. */
3358 set_history_size_command (args
, from_tty
, c
)
3361 struct cmd_list_element
*c
;
3363 if (history_size
== INT_MAX
)
3364 unstifle_history ();
3365 else if (history_size
>= 0)
3366 stifle_history (history_size
);
3369 history_size
= INT_MAX
;
3370 error ("History size must be non-negative");
3376 set_history (args
, from_tty
)
3380 printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3381 help_list (sethistlist
, "set history ", -1, gdb_stdout
);
3386 show_history (args
, from_tty
)
3390 cmd_show_list (showhistlist
, from_tty
, "");
3393 int info_verbose
= 0; /* Default verbose msgs off */
3395 /* Called by do_setshow_command. An elaborate joke. */
3398 set_verbose (args
, from_tty
, c
)
3401 struct cmd_list_element
*c
;
3403 char *cmdname
= "verbose";
3404 struct cmd_list_element
*showcmd
;
3406 showcmd
= lookup_cmd_1 (&cmdname
, showlist
, NULL
, 1);
3410 c
->doc
= "Set verbose printing of informational messages.";
3411 showcmd
->doc
= "Show verbose printing of informational messages.";
3415 c
->doc
= "Set verbosity.";
3416 showcmd
->doc
= "Show verbosity.";
3421 float_handler (signo
)
3424 /* This message is based on ANSI C, section 4.7. Note that integer
3425 divide by zero causes this, so "float" is a misnomer. */
3426 signal (SIGFPE
, float_handler
);
3427 error ("Erroneous arithmetic operation.");
3441 enablebreaklist
= NULL
;
3446 showhistlist
= NULL
;
3447 unsethistlist
= NULL
;
3448 maintenancelist
= NULL
;
3449 maintenanceinfolist
= NULL
;
3450 maintenanceprintlist
= NULL
;
3451 setprintlist
= NULL
;
3452 showprintlist
= NULL
;
3453 setchecklist
= NULL
;
3454 showchecklist
= NULL
;
3457 /* Init the history buffer. Note that we are called after the init file(s)
3458 * have been read so that the user can change the history file via his
3459 * .gdbinit file (for instance). The GDBHISTFILE environment variable
3460 * overrides all of this.
3468 tmpenv
= getenv ("HISTSIZE");
3470 history_size
= atoi (tmpenv
);
3471 else if (!history_size
)
3474 stifle_history (history_size
);
3476 tmpenv
= getenv ("GDBHISTFILE");
3478 history_filename
= savestring (tmpenv
, strlen(tmpenv
));
3479 else if (!history_filename
) {
3480 /* We include the current directory so that if the user changes
3481 directories the file written will be the same as the one
3483 history_filename
= concat (current_directory
, "/.gdb_history", NULL
);
3485 read_history (history_filename
);
3491 struct cmd_list_element
*c
;
3493 /* from event-top.c */
3494 extern int async_command_editing_p
;
3495 extern struct prompts the_prompts
;
3496 extern char *async_annotation_suffix
;
3497 extern char *new_async_prompt
;
3499 /* If we are running the asynchronous version,
3500 we initialize the prompts differently. */
3503 #ifdef DEFAULT_PROMPT
3504 prompt
= savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3506 prompt
= savestring ("(gdb) ", 6);
3511 /* initialize the prompt stack to a simple "(gdb) " prompt or to
3512 whatever the DEFULAT_PROMPT is. */
3513 the_prompts
.top
= 0;
3515 #ifdef DEFAULT_PROMPT
3516 PROMPT(0) = savestring (DEFAULT_PROMPT
, strlen(DEFAULT_PROMPT
));
3518 PROMPT(0) = savestring ("(gdb) ", 6);
3521 /* Set things up for annotation_level > 1, if the user ever decides
3523 async_annotation_suffix
= "prompt";
3524 /* Set the variable associated with the setshow prompt command. */
3525 new_async_prompt
= savestring (PROMPT (0), strlen (PROMPT (0)));
3528 /* Set the important stuff up for command editing. */
3529 command_editing_p
= 1;
3530 async_command_editing_p
= 1;
3531 history_expansion_p
= 0;
3532 write_history_p
= 0;
3534 /* Setup important stuff for command line editing. */
3535 rl_completion_entry_function
= (int (*)()) readline_line_completion_function
;
3536 rl_completer_word_break_characters
= gdb_completer_word_break_characters
;
3537 rl_completer_quote_characters
= gdb_completer_quote_characters
;
3538 rl_readline_name
= "gdb";
3540 /* Define the classes of commands.
3541 They will appear in the help list in the reverse of this order. */
3543 add_cmd ("internals", class_maintenance
, NO_FUNCTION
,
3544 "Maintenance commands.\n\
3545 Some gdb commands are provided just for use by gdb maintainers.\n\
3546 These commands are subject to frequent change, and may not be as\n\
3547 well documented as user commands.",
3549 add_cmd ("obscure", class_obscure
, NO_FUNCTION
, "Obscure features.", &cmdlist
);
3550 add_cmd ("aliases", class_alias
, NO_FUNCTION
, "Aliases of other commands.", &cmdlist
);
3551 add_cmd ("user-defined", class_user
, NO_FUNCTION
, "User-defined commands.\n\
3552 The commands in this class are those defined by the user.\n\
3553 Use the \"define\" command to define a command.", &cmdlist
);
3554 add_cmd ("support", class_support
, NO_FUNCTION
, "Support facilities.", &cmdlist
);
3556 add_cmd ("status", class_info
, NO_FUNCTION
, "Status inquiries.", &cmdlist
);
3557 add_cmd ("files", class_files
, NO_FUNCTION
, "Specifying and examining files.", &cmdlist
);
3558 add_cmd ("breakpoints", class_breakpoint
, NO_FUNCTION
, "Making program stop at certain points.", &cmdlist
);
3559 add_cmd ("data", class_vars
, NO_FUNCTION
, "Examining data.", &cmdlist
);
3560 add_cmd ("stack", class_stack
, NO_FUNCTION
, "Examining the stack.\n\
3561 The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
3562 counting from zero for the innermost (currently executing) frame.\n\n\
3563 At any time gdb identifies one frame as the \"selected\" frame.\n\
3564 Variable lookups are done with respect to the selected frame.\n\
3565 When the program being debugged stops, gdb selects the innermost frame.\n\
3566 The commands below can be used to select other frames by number or address.",
3568 add_cmd ("running", class_run
, NO_FUNCTION
, "Running the program.", &cmdlist
);
3570 add_com ("pwd", class_files
, pwd_command
,
3571 "Print working directory. This is used for your program as well.");
3572 c
= add_cmd ("cd", class_files
, cd_command
,
3573 "Set working directory to DIR for debugger and program being debugged.\n\
3574 The change does not take effect for the program being debugged\n\
3575 until the next time it is started.", &cmdlist
);
3576 c
->completer
= filename_completer
;
3578 /* The set prompt command is different depending whether or not the
3579 async version is run. NOTE: this difference is going to
3580 disappear as we make the event loop be the default engine of
3585 (add_set_cmd ("prompt", class_support
, var_string
, (char *)&prompt
,
3592 c
= add_set_cmd ("prompt", class_support
, var_string
,
3593 (char *)&new_async_prompt
,
3596 add_show_from_set (c
, &showlist
);
3597 c
->function
.sfunc
= set_async_prompt
;
3600 add_com ("echo", class_support
, echo_command
,
3601 "Print a constant string. Give string as argument.\n\
3602 C escape sequences may be used in the argument.\n\
3603 No newline is added at the end of the argument;\n\
3604 use \"\\n\" if you want a newline to be printed.\n\
3605 Since leading and trailing whitespace are ignored in command arguments,\n\
3606 if you want to print some you must use \"\\\" before leading whitespace\n\
3607 to be printed or after trailing whitespace.");
3608 add_com ("document", class_support
, document_command
,
3609 "Document a user-defined command.\n\
3610 Give command name as argument. Give documentation on following lines.\n\
3611 End with a line of just \"end\".");
3612 add_com ("define", class_support
, define_command
,
3613 "Define a new command name. Command name is argument.\n\
3614 Definition appears on following lines, one command per line.\n\
3615 End with a line of just \"end\".\n\
3616 Use the \"document\" command to give documentation for the new command.\n\
3617 Commands defined in this way may have up to ten arguments.");
3620 c
= add_cmd ("source", class_support
, source_command
,
3621 "Read commands from a file named FILE.\n\
3622 Note that the file \"" GDBINIT_FILENAME
"\" is read automatically in this way\n\
3623 when gdb is started.", &cmdlist
);
3625 /* Punt file name, we can't help it easily. */
3626 c
= add_cmd ("source", class_support
, source_command
,
3627 "Read commands from a file named FILE.\n\
3628 Note that the file \".gdbinit\" is read automatically in this way\n\
3629 when gdb is started.", &cmdlist
);
3631 c
->completer
= filename_completer
;
3633 add_com ("quit", class_support
, quit_command
, "Exit gdb.");
3634 add_com ("help", class_support
, help_command
, "Print list of commands.");
3635 add_com_alias ("q", "quit", class_support
, 1);
3636 add_com_alias ("h", "help", class_support
, 1);
3638 add_com ("dont-repeat", class_support
, dont_repeat_command
, "Don't repeat this command.\n\
3639 Primarily used inside of user-defined commands that should not be repeated when\n\
3642 c
= add_set_cmd ("verbose", class_support
, var_boolean
, (char *)&info_verbose
,
3645 add_show_from_set (c
, &showlist
);
3646 c
->function
.sfunc
= set_verbose
;
3647 set_verbose (NULL
, 0, c
);
3649 /* The set editing command is different depending whether or not the
3650 async version is run. NOTE: this difference is going to disappear
3651 as we make the event loop be the default engine of gdb. */
3655 (add_set_cmd ("editing", class_support
, var_boolean
, (char *)&command_editing_p
,
3656 "Set editing of command lines as they are typed.\n\
3657 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3658 Without an argument, command line editing is enabled. To edit, use\n\
3659 EMACS-like or VI-like commands like control-P or ESC.", &setlist
),
3664 c
= add_set_cmd ("editing", class_support
, var_boolean
, (char *)&async_command_editing_p
,
3665 "Set editing of command lines as they are typed.\n\
3666 Use \"on\" to enable the editing, and \"off\" to disable it.\n\
3667 Without an argument, command line editing is enabled. To edit, use\n\
3668 EMACS-like or VI-like commands like control-P or ESC.", &setlist
);
3670 add_show_from_set (c
, &showlist
);
3671 c
->function
.sfunc
= set_async_editing_command
;
3674 add_prefix_cmd ("history", class_support
, set_history
,
3675 "Generic command for setting command history parameters.",
3676 &sethistlist
, "set history ", 0, &setlist
);
3677 add_prefix_cmd ("history", class_support
, show_history
,
3678 "Generic command for showing command history parameters.",
3679 &showhistlist
, "show history ", 0, &showlist
);
3682 (add_set_cmd ("expansion", no_class
, var_boolean
, (char *)&history_expansion_p
,
3683 "Set history expansion on command input.\n\
3684 Without an argument, history expansion is enabled.", &sethistlist
),
3688 (add_set_cmd ("save", no_class
, var_boolean
, (char *)&write_history_p
,
3689 "Set saving of the history record on exit.\n\
3690 Use \"on\" to enable the saving, and \"off\" to disable it.\n\
3691 Without an argument, saving is enabled.", &sethistlist
),
3694 c
= add_set_cmd ("size", no_class
, var_integer
, (char *)&history_size
,
3695 "Set the size of the command history, \n\
3696 ie. the number of previous commands to keep a record of.", &sethistlist
);
3697 add_show_from_set (c
, &showhistlist
);
3698 c
->function
.sfunc
= set_history_size_command
;
3701 (add_set_cmd ("filename", no_class
, var_filename
, (char *)&history_filename
,
3702 "Set the filename in which to record the command history\n\
3703 (the list of previous commands of which a record is kept).", &sethistlist
),
3707 (add_set_cmd ("confirm", class_support
, var_boolean
,
3709 "Set whether to confirm potentially dangerous operations.",
3713 add_prefix_cmd ("info", class_info
, info_command
,
3714 "Generic command for showing things about the program being debugged.",
3715 &infolist
, "info ", 0, &cmdlist
);
3716 add_com_alias ("i", "info", class_info
, 1);
3718 add_com ("complete", class_obscure
, complete_command
,
3719 "List the completions for the rest of the line as a command.");
3721 add_prefix_cmd ("show", class_info
, show_command
,
3722 "Generic command for showing things about the debugger.",
3723 &showlist
, "show ", 0, &cmdlist
);
3724 /* Another way to get at the same thing. */
3725 add_info ("set", show_command
, "Show all GDB settings.");
3727 add_cmd ("commands", no_class
, show_commands
,
3728 "Show the history of commands you typed.\n\
3729 You can supply a command number to start with, or a `+' to start after\n\
3730 the previous command number shown.",
3733 add_cmd ("version", no_class
, show_version
,
3734 "Show what version of GDB this is.", &showlist
);
3736 add_com ("while", class_support
, while_command
,
3737 "Execute nested commands WHILE the conditional expression is non zero.\n\
3738 The conditional expression must follow the word `while' and must in turn be\n\
3739 followed by a new line. The nested commands must be entered one per line,\n\
3740 and should be terminated by the word `end'.");
3742 add_com ("if", class_support
, if_command
,
3743 "Execute nested commands once IF the conditional expression is non zero.\n\
3744 The conditional expression must follow the word `if' and must in turn be\n\
3745 followed by a new line. The nested commands must be entered one per line,\n\
3746 and should be terminated by the word 'else' or `end'. If an else clause\n\
3747 is used, the same rules apply to its nested commands as to the first ones.");
3749 /* If target is open when baud changes, it doesn't take effect until the
3750 next open (I think, not sure). */
3751 add_show_from_set (add_set_cmd ("remotebaud", no_class
,
3752 var_zinteger
, (char *)&baud_rate
,
3753 "Set baud rate for remote serial I/O.\n\
3754 This value is used to set the speed of the serial port when debugging\n\
3755 using remote targets.", &setlist
),
3759 add_set_cmd ("remotedebug", no_class
, var_zinteger
, (char *)&remote_debug
,
3760 "Set debugging of remote protocol.\n\
3761 When enabled, each packet sent or received with the remote target\n\
3762 is displayed.", &setlist
),
3766 add_set_cmd ("remotetimeout", no_class
, var_integer
, (char *)&remote_timeout
,
3767 "Set timeout limit to wait for target to respond.\n\
3768 This value is used to set the time limit for gdb to wait for a response\n\
3769 from the target.", &setlist
),
3772 /* The set annotate command is different depending whether or not
3773 the async version is run. NOTE: this difference is going to
3774 disappear as we make the event loop be the default engine of
3778 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3779 (char *)&annotation_level
, "Set annotation_level.\n\
3780 0 == normal; 1 == fullname (for use when running under emacs)\n\
3781 2 == output annotated suitably for use by programs that control GDB.",
3783 c
= add_show_from_set (c
, &showlist
);
3787 c
= add_set_cmd ("annotate", class_obscure
, var_zinteger
,
3788 (char *)&annotation_level
, "Set annotation_level.\n\
3789 0 == normal; 1 == fullname (for use when running under emacs)\n\
3790 2 == output annotated suitably for use by programs that control GDB.",
3792 add_show_from_set (c
, &showlist
);
3793 c
->function
.sfunc
= set_async_annotation_level
;