1*69200Smckusick /* Top level `main' program for GDB, the GNU debugger. 2*69200Smckusick Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 3*69200Smckusick Free Software Foundation, Inc. 4*69200Smckusick 5*69200Smckusick This file is part of GDB. 6*69200Smckusick 7*69200Smckusick This program is free software; you can redistribute it and/or modify 8*69200Smckusick it under the terms of the GNU General Public License as published by 9*69200Smckusick the Free Software Foundation; either version 2 of the License, or 10*69200Smckusick (at your option) any later version. 11*69200Smckusick 12*69200Smckusick This program is distributed in the hope that it will be useful, 13*69200Smckusick but WITHOUT ANY WARRANTY; without even the implied warranty of 14*69200Smckusick MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*69200Smckusick GNU General Public License for more details. 16*69200Smckusick 17*69200Smckusick You should have received a copy of the GNU General Public License 18*69200Smckusick along with this program; if not, write to the Free Software 19*69200Smckusick Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 20*69200Smckusick 21*69200Smckusick #include <stdio.h> 22*69200Smckusick #include <sys/param.h> 23*69200Smckusick int fclose (); 24*69200Smckusick #include "defs.h" 25*69200Smckusick #include "gdbcmd.h" 26*69200Smckusick #include "call-cmds.h" 27*69200Smckusick #include "gdbcore.h" 28*69200Smckusick #include "symtab.h" 29*69200Smckusick #include "inferior.h" 30*69200Smckusick #include "signals.h" 31*69200Smckusick #include "target.h" 32*69200Smckusick #include "breakpoint.h" 33*69200Smckusick #include "gdbtypes.h" 34*69200Smckusick #include "expression.h" 35*69200Smckusick #include "language.h" 36*69200Smckusick 37*69200Smckusick #include "getopt.h" 38*69200Smckusick 39*69200Smckusick /* readline include files */ 40*69200Smckusick #include "readline.h" 41*69200Smckusick #include "history.h" 42*69200Smckusick 43*69200Smckusick /* readline defines these. */ 44*69200Smckusick #undef savestring 45*69200Smckusick #undef CTRL 46*69200Smckusick 47*69200Smckusick #ifdef USG 48*69200Smckusick #include <sys/types.h> 49*69200Smckusick #include <unistd.h> 50*69200Smckusick #endif 51*69200Smckusick 52*69200Smckusick #include <string.h> 53*69200Smckusick #ifndef NO_SYS_FILE 54*69200Smckusick #include <sys/file.h> 55*69200Smckusick #endif 56*69200Smckusick #include <setjmp.h> 57*69200Smckusick #include <sys/stat.h> 58*69200Smckusick #include <ctype.h> 59*69200Smckusick 60*69200Smckusick #ifdef SET_STACK_LIMIT_HUGE 61*69200Smckusick #include <sys/time.h> 62*69200Smckusick #include <sys/resource.h> 63*69200Smckusick 64*69200Smckusick int original_stack_limit; 65*69200Smckusick #endif 66*69200Smckusick 67*69200Smckusick /* Prototypes for local functions */ 68*69200Smckusick 69*69200Smckusick static char * 70*69200Smckusick symbol_completion_function PARAMS ((char *, int)); 71*69200Smckusick 72*69200Smckusick static void 73*69200Smckusick command_loop PARAMS ((void)); 74*69200Smckusick 75*69200Smckusick static void 76*69200Smckusick command_loop_marker PARAMS ((int)); 77*69200Smckusick 78*69200Smckusick static void 79*69200Smckusick print_gdb_version PARAMS ((void)); 80*69200Smckusick 81*69200Smckusick static void 82*69200Smckusick quit_command PARAMS ((char *, int)); 83*69200Smckusick 84*69200Smckusick static void 85*69200Smckusick initialize_main PARAMS ((void)); 86*69200Smckusick 87*69200Smckusick static void 88*69200Smckusick initialize_history PARAMS ((void)); 89*69200Smckusick 90*69200Smckusick static void 91*69200Smckusick initialize_cmd_lists PARAMS ((void)); 92*69200Smckusick 93*69200Smckusick static void 94*69200Smckusick float_handler PARAMS ((int)); 95*69200Smckusick 96*69200Smckusick static void 97*69200Smckusick source_command PARAMS ((char *, int)); 98*69200Smckusick 99*69200Smckusick static void 100*69200Smckusick cd_command PARAMS ((char *, int)); 101*69200Smckusick 102*69200Smckusick static void 103*69200Smckusick print_gnu_advertisement PARAMS ((void)); 104*69200Smckusick 105*69200Smckusick static void 106*69200Smckusick init_signals PARAMS ((void)); 107*69200Smckusick 108*69200Smckusick static void 109*69200Smckusick read_command_file PARAMS ((FILE *)); 110*69200Smckusick 111*69200Smckusick static void 112*69200Smckusick set_verbose PARAMS ((char *, int, struct cmd_list_element *)); 113*69200Smckusick 114*69200Smckusick static void 115*69200Smckusick show_history PARAMS ((char *, int)); 116*69200Smckusick 117*69200Smckusick static void 118*69200Smckusick set_history PARAMS ((char *, int)); 119*69200Smckusick 120*69200Smckusick static void 121*69200Smckusick set_history_size_command PARAMS ((char *, int, struct cmd_list_element *)); 122*69200Smckusick 123*69200Smckusick static void 124*69200Smckusick show_commands PARAMS ((char *, int)); 125*69200Smckusick 126*69200Smckusick static void 127*69200Smckusick echo_command PARAMS ((char *, int)); 128*69200Smckusick 129*69200Smckusick static void 130*69200Smckusick pwd_command PARAMS ((char *, int)); 131*69200Smckusick 132*69200Smckusick static void 133*69200Smckusick show_version PARAMS ((char *, int)); 134*69200Smckusick 135*69200Smckusick static void 136*69200Smckusick document_command PARAMS ((char *, int)); 137*69200Smckusick 138*69200Smckusick static void 139*69200Smckusick define_command PARAMS ((char *, int)); 140*69200Smckusick 141*69200Smckusick static void 142*69200Smckusick validate_comname PARAMS ((char *)); 143*69200Smckusick 144*69200Smckusick static void 145*69200Smckusick help_command PARAMS ((char *, int)); 146*69200Smckusick 147*69200Smckusick static void 148*69200Smckusick show_command PARAMS ((char *, int)); 149*69200Smckusick 150*69200Smckusick static void 151*69200Smckusick info_command PARAMS ((char *, int)); 152*69200Smckusick 153*69200Smckusick static void 154*69200Smckusick do_nothing PARAMS ((int)); 155*69200Smckusick 156*69200Smckusick static int 157*69200Smckusick quit_cover PARAMS ((char *)); 158*69200Smckusick 159*69200Smckusick static void 160*69200Smckusick disconnect PARAMS ((int)); 161*69200Smckusick 162*69200Smckusick void 163*69200Smckusick source_cleanup PARAMS ((FILE *)); 164*69200Smckusick 165*69200Smckusick /* If this definition isn't overridden by the header files, assume 166*69200Smckusick that isatty and fileno exist on this system. */ 167*69200Smckusick #ifndef ISATTY 168*69200Smckusick #define ISATTY(FP) (isatty (fileno (FP))) 169*69200Smckusick #endif 170*69200Smckusick 171*69200Smckusick /* Initialization file name for gdb. This is overridden in some configs. */ 172*69200Smckusick 173*69200Smckusick #ifndef GDBINIT_FILENAME 174*69200Smckusick #define GDBINIT_FILENAME ".gdbinit" 175*69200Smckusick #endif 176*69200Smckusick char gdbinit[] = GDBINIT_FILENAME; 177*69200Smckusick 178*69200Smckusick #define ALL_CLEANUPS ((struct cleanup *)0) 179*69200Smckusick 180*69200Smckusick /* Version number of GDB, as a string. */ 181*69200Smckusick 182*69200Smckusick extern char *version; 183*69200Smckusick 184*69200Smckusick /* Message to be printed before the error message, when an error occurs. */ 185*69200Smckusick 186*69200Smckusick extern char *error_pre_print; 187*69200Smckusick 188*69200Smckusick /* Message to be printed before the warning message, when a warning occurs. */ 189*69200Smckusick 190*69200Smckusick extern char *warning_pre_print; 191*69200Smckusick 192*69200Smckusick extern char lang_frame_mismatch_warn[]; /* language.c */ 193*69200Smckusick 194*69200Smckusick /* Whether GDB's stdin is on a terminal. */ 195*69200Smckusick 196*69200Smckusick extern int gdb_has_a_terminal; /* inflow.c */ 197*69200Smckusick 198*69200Smckusick /* Flag for whether we want all the "from_tty" gubbish printed. */ 199*69200Smckusick 200*69200Smckusick int caution = 1; /* Default is yes, sigh. */ 201*69200Smckusick 202*69200Smckusick /* 203*69200Smckusick * Define all cmd_list_element's 204*69200Smckusick */ 205*69200Smckusick 206*69200Smckusick /* Chain containing all defined commands. */ 207*69200Smckusick 208*69200Smckusick struct cmd_list_element *cmdlist; 209*69200Smckusick 210*69200Smckusick /* Chain containing all defined info subcommands. */ 211*69200Smckusick 212*69200Smckusick struct cmd_list_element *infolist; 213*69200Smckusick 214*69200Smckusick /* Chain containing all defined enable subcommands. */ 215*69200Smckusick 216*69200Smckusick struct cmd_list_element *enablelist; 217*69200Smckusick 218*69200Smckusick /* Chain containing all defined disable subcommands. */ 219*69200Smckusick 220*69200Smckusick struct cmd_list_element *disablelist; 221*69200Smckusick 222*69200Smckusick /* Chain containing all defined delete subcommands. */ 223*69200Smckusick 224*69200Smckusick struct cmd_list_element *deletelist; 225*69200Smckusick 226*69200Smckusick /* Chain containing all defined "enable breakpoint" subcommands. */ 227*69200Smckusick 228*69200Smckusick struct cmd_list_element *enablebreaklist; 229*69200Smckusick 230*69200Smckusick /* Chain containing all defined set subcommands */ 231*69200Smckusick 232*69200Smckusick struct cmd_list_element *setlist; 233*69200Smckusick 234*69200Smckusick /* Chain containing all defined unset subcommands */ 235*69200Smckusick 236*69200Smckusick struct cmd_list_element *unsetlist; 237*69200Smckusick 238*69200Smckusick /* Chain containing all defined show subcommands. */ 239*69200Smckusick 240*69200Smckusick struct cmd_list_element *showlist; 241*69200Smckusick 242*69200Smckusick /* Chain containing all defined \"set history\". */ 243*69200Smckusick 244*69200Smckusick struct cmd_list_element *sethistlist; 245*69200Smckusick 246*69200Smckusick /* Chain containing all defined \"show history\". */ 247*69200Smckusick 248*69200Smckusick struct cmd_list_element *showhistlist; 249*69200Smckusick 250*69200Smckusick /* Chain containing all defined \"unset history\". */ 251*69200Smckusick 252*69200Smckusick struct cmd_list_element *unsethistlist; 253*69200Smckusick 254*69200Smckusick /* Chain containing all defined maintenance subcommands. */ 255*69200Smckusick 256*69200Smckusick #if MAINTENANCE_CMDS 257*69200Smckusick struct cmd_list_element *maintenancelist; 258*69200Smckusick #endif 259*69200Smckusick 260*69200Smckusick /* Chain containing all defined "maintenance info" subcommands. */ 261*69200Smckusick 262*69200Smckusick #if MAINTENANCE_CMDS 263*69200Smckusick struct cmd_list_element *maintenanceinfolist; 264*69200Smckusick #endif 265*69200Smckusick 266*69200Smckusick /* Chain containing all defined "maintenance print" subcommands. */ 267*69200Smckusick 268*69200Smckusick #if MAINTENANCE_CMDS 269*69200Smckusick struct cmd_list_element *maintenanceprintlist; 270*69200Smckusick #endif 271*69200Smckusick 272*69200Smckusick struct cmd_list_element *setprintlist; 273*69200Smckusick 274*69200Smckusick struct cmd_list_element *showprintlist; 275*69200Smckusick 276*69200Smckusick struct cmd_list_element *setchecklist; 277*69200Smckusick 278*69200Smckusick struct cmd_list_element *showchecklist; 279*69200Smckusick 280*69200Smckusick /* stdio stream that command input is being read from. */ 281*69200Smckusick 282*69200Smckusick FILE *instream; 283*69200Smckusick 284*69200Smckusick /* Current working directory. */ 285*69200Smckusick 286*69200Smckusick char *current_directory; 287*69200Smckusick 288*69200Smckusick /* The directory name is actually stored here (usually). */ 289*69200Smckusick static char dirbuf[MAXPATHLEN]; 290*69200Smckusick 291*69200Smckusick #ifdef KERNELDEBUG 292*69200Smckusick /* Nonzero if we're debugging /dev/kmem or a kernel crash dump */ 293*69200Smckusick int kernel_debugging; 294*69200Smckusick #endif 295*69200Smckusick 296*69200Smckusick /* Function to call before reading a command, if nonzero. 297*69200Smckusick The function receives two args: an input stream, 298*69200Smckusick and a prompt string. */ 299*69200Smckusick 300*69200Smckusick void (*window_hook) PARAMS ((FILE *, char *)); 301*69200Smckusick 302*69200Smckusick extern int frame_file_full_name; 303*69200Smckusick extern int mapped_symbol_files; 304*69200Smckusick extern int readnow_symbol_files; 305*69200Smckusick 306*69200Smckusick /* Nonzero to inhibit confirmation of quitting or restarting 307*69200Smckusick a stopped inferior. */ 308*69200Smckusick 309*69200Smckusick int inhibit_confirm; 310*69200Smckusick 311*69200Smckusick /* The number of lines on a page */ 312*69200Smckusick int pagesize; 313*69200Smckusick 314*69200Smckusick /* Nonzero if we should refrain from using an X window. */ 315*69200Smckusick 316*69200Smckusick int inhibit_windows = 0; 317*69200Smckusick 318*69200Smckusick extern int frame_file_full_name; 319*69200Smckusick int epoch_interface; 320*69200Smckusick int xgdb_verbose; 321*69200Smckusick 322*69200Smckusick /* Buffer used for reading command lines, and the size 323*69200Smckusick allocated for it so far. */ 324*69200Smckusick 325*69200Smckusick char *line; 326*69200Smckusick int linesize = 100; 327*69200Smckusick 328*69200Smckusick char *getenv (); 329*69200Smckusick 330*69200Smckusick /* gdb prints this when reading a command interactively */ 331*69200Smckusick static char *masterprompt; 332*69200Smckusick 333*69200Smckusick /* Baud rate specified for talking to serial target systems. Default 334*69200Smckusick is left as a zero pointer, so targets can choose their own defaults. */ 335*69200Smckusick 336*69200Smckusick char *baud_rate; 337*69200Smckusick 338*69200Smckusick /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */ 339*69200Smckusick 340*69200Smckusick #ifndef STOP_SIGNAL 341*69200Smckusick #ifdef SIGTSTP 342*69200Smckusick #define STOP_SIGNAL SIGTSTP 343*69200Smckusick static void stop_sig PARAMS ((int)); 344*69200Smckusick #endif 345*69200Smckusick #endif 346*69200Smckusick 347*69200Smckusick /* Some System V have job control but not sigsetmask(). */ 348*69200Smckusick #if !defined (HAVE_SIGSETMASK) 349*69200Smckusick #define HAVE_SIGSETMASK !defined (USG) 350*69200Smckusick #endif 351*69200Smckusick 352*69200Smckusick #if 0 == (HAVE_SIGSETMASK) 353*69200Smckusick #define sigsetmask(n) 354*69200Smckusick #endif 355*69200Smckusick 356*69200Smckusick /* This is how `error' returns to command level. */ 357*69200Smckusick 358*69200Smckusick jmp_buf to_top_level; 359*69200Smckusick 360*69200Smckusick NORETURN void 361*69200Smckusick return_to_top_level () 362*69200Smckusick { 363*69200Smckusick quit_flag = 0; 364*69200Smckusick immediate_quit = 0; 365*69200Smckusick bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */ 366*69200Smckusick disable_current_display (); 367*69200Smckusick do_cleanups (ALL_CLEANUPS); 368*69200Smckusick (NORETURN void) longjmp (to_top_level, 1); 369*69200Smckusick } 370*69200Smckusick 371*69200Smckusick /* Call FUNC with arg ARGS, catching any errors. 372*69200Smckusick If there is no error, return the value returned by FUNC. 373*69200Smckusick If there is an error, print ERRSTRING, print the specific error message, 374*69200Smckusick then return zero. */ 375*69200Smckusick 376*69200Smckusick int 377*69200Smckusick catch_errors (func, args, errstring) 378*69200Smckusick int (*func) PARAMS ((char *)); 379*69200Smckusick char *args; 380*69200Smckusick char *errstring; 381*69200Smckusick { 382*69200Smckusick jmp_buf saved; 383*69200Smckusick int val; 384*69200Smckusick struct cleanup *saved_cleanup_chain; 385*69200Smckusick char *saved_error_pre_print; 386*69200Smckusick 387*69200Smckusick saved_cleanup_chain = save_cleanups (); 388*69200Smckusick saved_error_pre_print = error_pre_print; 389*69200Smckusick 390*69200Smckusick memcpy ((char *)saved, (char *)to_top_level, sizeof (jmp_buf)); 391*69200Smckusick error_pre_print = errstring; 392*69200Smckusick 393*69200Smckusick if (setjmp (to_top_level) == 0) 394*69200Smckusick val = (*func) (args); 395*69200Smckusick else 396*69200Smckusick val = 0; 397*69200Smckusick 398*69200Smckusick restore_cleanups (saved_cleanup_chain); 399*69200Smckusick 400*69200Smckusick error_pre_print = saved_error_pre_print; 401*69200Smckusick memcpy ((char *)to_top_level, (char *)saved, sizeof (jmp_buf)); 402*69200Smckusick return val; 403*69200Smckusick } 404*69200Smckusick 405*69200Smckusick /* Handler for SIGHUP. */ 406*69200Smckusick 407*69200Smckusick static void 408*69200Smckusick disconnect (signo) 409*69200Smckusick int signo; 410*69200Smckusick { 411*69200Smckusick catch_errors (quit_cover, NULL, "Could not kill inferior process"); 412*69200Smckusick signal (SIGHUP, SIG_DFL); 413*69200Smckusick kill (getpid (), SIGHUP); 414*69200Smckusick } 415*69200Smckusick 416*69200Smckusick /* Just a little helper function for disconnect(). */ 417*69200Smckusick 418*69200Smckusick static int 419*69200Smckusick quit_cover (s) 420*69200Smckusick char *s; 421*69200Smckusick { 422*69200Smckusick caution = 0; /* Throw caution to the wind -- we're exiting. 423*69200Smckusick This prevents asking the user dumb questions. */ 424*69200Smckusick quit_command((char *)0, 0); 425*69200Smckusick return 0; 426*69200Smckusick } 427*69200Smckusick 428*69200Smckusick /* 429*69200Smckusick * Source an init file if it exists. 430*69200Smckusick */ 431*69200Smckusick void 432*69200Smckusick sourcefile(file) 433*69200Smckusick char *file; 434*69200Smckusick { 435*69200Smckusick if (access (file, R_OK) == 0) 436*69200Smckusick if (!setjmp (to_top_level)) 437*69200Smckusick source_command (file, 0); 438*69200Smckusick do_cleanups(ALL_CLEANUPS); 439*69200Smckusick } 440*69200Smckusick 441*69200Smckusick /* 442*69200Smckusick * Source $HOME/.gdbinit and $cwd/.gdbinit. 443*69200Smckusick * If X is enabled, also $HOME/.xgdbinit and $cwd/.xgdbinit.source 444*69200Smckusick */ 445*69200Smckusick void 446*69200Smckusick source_init_files() 447*69200Smckusick { 448*69200Smckusick char *homedir, initfile[256]; 449*69200Smckusick int samedir = 0; 450*69200Smckusick 451*69200Smckusick /* Read init file, if it exists in home directory */ 452*69200Smckusick homedir = getenv ("HOME"); 453*69200Smckusick if (homedir) { 454*69200Smckusick struct stat homebuf, cwdbuf; 455*69200Smckusick 456*69200Smckusick sprintf(initfile, "%s/%s", homedir, gdbinit); 457*69200Smckusick sourcefile(initfile); 458*69200Smckusick if (!inhibit_windows) { 459*69200Smckusick sprintf(initfile, "%s/.xgdbinit", homedir); 460*69200Smckusick sourcefile(initfile); 461*69200Smckusick } 462*69200Smckusick #ifdef KERNELDEBUG 463*69200Smckusick if (kernel_debugging) { 464*69200Smckusick sprintf(initfile, "%s/.kgdbinit", homedir); 465*69200Smckusick sourcefile(initfile); 466*69200Smckusick } 467*69200Smckusick #endif 468*69200Smckusick /* Determine if current directory is the same as the home 469*69200Smckusick directory, so we don't source the same file twice. */ 470*69200Smckusick 471*69200Smckusick bzero (&homebuf, sizeof (struct stat)); 472*69200Smckusick bzero (&cwdbuf, sizeof (struct stat)); 473*69200Smckusick 474*69200Smckusick stat(homedir, &homebuf); 475*69200Smckusick stat(".", &cwdbuf); 476*69200Smckusick 477*69200Smckusick samedir = bcmp(&homebuf, &cwdbuf, sizeof(struct stat)) == 0; 478*69200Smckusick } 479*69200Smckusick /* Read the input file in the current directory, *if* it isn't 480*69200Smckusick the same file (it should exist, also). */ 481*69200Smckusick if (!samedir) { 482*69200Smckusick sourcefile(gdbinit); 483*69200Smckusick sourcefile(".xgdbinit"); 484*69200Smckusick #ifdef KERNELDEBUG 485*69200Smckusick if (kernel_debugging) 486*69200Smckusick sourcefile(".kgdbinit"); 487*69200Smckusick #endif 488*69200Smckusick } 489*69200Smckusick } 490*69200Smckusick 491*69200Smckusick /* Clean up on error during a "source" command (or execution of a 492*69200Smckusick user-defined command). */ 493*69200Smckusick 494*69200Smckusick void 495*69200Smckusick source_cleanup (stream) 496*69200Smckusick FILE *stream; 497*69200Smckusick { 498*69200Smckusick /* Restore the previous input stream. */ 499*69200Smckusick instream = stream; 500*69200Smckusick } 501*69200Smckusick 502*69200Smckusick /* Read commands from STREAM. */ 503*69200Smckusick static void 504*69200Smckusick read_command_file (stream) 505*69200Smckusick FILE *stream; 506*69200Smckusick { 507*69200Smckusick struct cleanup *cleanups; 508*69200Smckusick 509*69200Smckusick cleanups = make_cleanup (source_cleanup, instream); 510*69200Smckusick instream = stream; 511*69200Smckusick command_loop (); 512*69200Smckusick do_cleanups (cleanups); 513*69200Smckusick } 514*69200Smckusick 515*69200Smckusick /* 516*69200Smckusick * Process the core file argument. 517*69200Smckusick * We infer (lexically) from the name and (semantically) from the 518*69200Smckusick * corresponding file type what the target is. It's one of a live 519*69200Smckusick * user process (pid), a user process core dump (core), a kernel 520*69200Smckusick * crash dump (vmcore), a live kernel (/dev/mem), a remote kernel (/dev/ttya) 521*69200Smckusick * or a remote internet host (@127.0.0.1). 522*69200Smckusick */ 523*69200Smckusick void 524*69200Smckusick do_core_arg(corefile, batch) 525*69200Smckusick char *corefile; 526*69200Smckusick int batch; 527*69200Smckusick { 528*69200Smckusick struct stat stb; 529*69200Smckusick 530*69200Smckusick if (setjmp(to_top_level)) 531*69200Smckusick return; 532*69200Smckusick 533*69200Smckusick if (corefile[0] == '@') 534*69200Smckusick /* Internet host -- we're remote debugging. */ 535*69200Smckusick remote_open(corefile, 0); 536*69200Smckusick else if (stat(corefile, &stb) == 0) { 537*69200Smckusick #ifdef KERNELDEBUG 538*69200Smckusick if (S_ISCHR(stb.st_mode)) { 539*69200Smckusick /* 540*69200Smckusick * character device -- either we're debugging a live 541*69200Smckusick * kernel (/dev/mem, /dev/kmem) or a remote kernel 542*69200Smckusick * over a serial link. Use the heuristic that 543*69200Smckusick * /dev/mem has a "small" major device number. 544*69200Smckusick */ 545*69200Smckusick if (major(stb.st_rdev) <= 4) 546*69200Smckusick /* kernel memory */ 547*69200Smckusick kernel_core_open(corefile, !batch); 548*69200Smckusick else 549*69200Smckusick remote_open(corefile, !batch); 550*69200Smckusick } else if (kernel_debugging) 551*69200Smckusick /* a kernel crash dump */ 552*69200Smckusick kernel_core_open(corefile, !batch); 553*69200Smckusick else 554*69200Smckusick #endif 555*69200Smckusick core_file_command(corefile, !batch); 556*69200Smckusick } else if (isdigit(corefile[0])) 557*69200Smckusick attach_command(corefile, !batch); 558*69200Smckusick else 559*69200Smckusick /* 560*69200Smckusick * stat() failed and arg isn't a pid 561*69200Smckusick */ 562*69200Smckusick perror(corefile); 563*69200Smckusick } 564*69200Smckusick 565*69200Smckusick int 566*69200Smckusick main (argc, argv) 567*69200Smckusick int argc; 568*69200Smckusick char **argv; 569*69200Smckusick { 570*69200Smckusick int count; 571*69200Smckusick static int inhibit_gdbinit = 0; 572*69200Smckusick static int quiet = 1; 573*69200Smckusick static int batch = 0; 574*69200Smckusick 575*69200Smckusick /* Pointers to various arguments from command line. */ 576*69200Smckusick char *symarg = NULL; 577*69200Smckusick char *execarg = NULL; 578*69200Smckusick char *corearg = NULL; 579*69200Smckusick char *cdarg = NULL; 580*69200Smckusick char *ttyarg = NULL; 581*69200Smckusick char *cp; 582*69200Smckusick 583*69200Smckusick /* Pointers to all arguments of +command option. */ 584*69200Smckusick char **cmdarg; 585*69200Smckusick /* Allocated size of cmdarg. */ 586*69200Smckusick int cmdsize; 587*69200Smckusick /* Number of elements of cmdarg used. */ 588*69200Smckusick int ncmd; 589*69200Smckusick 590*69200Smckusick /* Indices of all arguments of +directory option. */ 591*69200Smckusick char **dirarg; 592*69200Smckusick /* Allocated size. */ 593*69200Smckusick int dirsize; 594*69200Smckusick /* Number of elements used. */ 595*69200Smckusick int ndir; 596*69200Smckusick 597*69200Smckusick register int i; 598*69200Smckusick 599*69200Smckusick /* XXX Windows only for xgdb. */ 600*69200Smckusick char *strrchr(); 601*69200Smckusick if (cp = strrchr(argv[0], '/')) 602*69200Smckusick ++cp; 603*69200Smckusick else 604*69200Smckusick cp = argv[0]; 605*69200Smckusick if (*cp != 'x') 606*69200Smckusick inhibit_windows = 1; 607*69200Smckusick 608*69200Smckusick /* This needs to happen before the first use of malloc. */ 609*69200Smckusick init_malloc ((PTR) NULL); 610*69200Smckusick 611*69200Smckusick #if defined (ALIGN_STACK_ON_STARTUP) 612*69200Smckusick i = (int) &count & 0x3; 613*69200Smckusick if (i != 0) 614*69200Smckusick alloca (4 - i); 615*69200Smckusick #endif 616*69200Smckusick 617*69200Smckusick /* If error() is called from initialization code, just exit */ 618*69200Smckusick if (setjmp (to_top_level)) { 619*69200Smckusick exit(1); 620*69200Smckusick } 621*69200Smckusick 622*69200Smckusick cmdsize = 1; 623*69200Smckusick cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg)); 624*69200Smckusick ncmd = 0; 625*69200Smckusick dirsize = 1; 626*69200Smckusick dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg)); 627*69200Smckusick ndir = 0; 628*69200Smckusick 629*69200Smckusick quit_flag = 0; 630*69200Smckusick instream = stdin; 631*69200Smckusick 632*69200Smckusick getcwd (dirbuf, sizeof (dirbuf)); 633*69200Smckusick current_directory = dirbuf; 634*69200Smckusick 635*69200Smckusick #ifdef SET_STACK_LIMIT_HUGE 636*69200Smckusick { 637*69200Smckusick struct rlimit rlim; 638*69200Smckusick 639*69200Smckusick /* Set the stack limit huge so that alloca (particularly stringtab 640*69200Smckusick * in dbxread.c) does not fail. */ 641*69200Smckusick getrlimit (RLIMIT_STACK, &rlim); 642*69200Smckusick original_stack_limit = rlim.rlim_cur; 643*69200Smckusick rlim.rlim_cur = rlim.rlim_max; 644*69200Smckusick setrlimit (RLIMIT_STACK, &rlim); 645*69200Smckusick } 646*69200Smckusick #endif /* SET_STACK_LIMIT_HUGE */ 647*69200Smckusick 648*69200Smckusick /* Parse arguments and options. */ 649*69200Smckusick { 650*69200Smckusick int c; 651*69200Smckusick static int print_help; 652*69200Smckusick /* When var field is 0, use flag field to record the equivalent 653*69200Smckusick short option (or arbitrary numbers starting at 10 for those 654*69200Smckusick with no equivalent). */ 655*69200Smckusick static struct option long_options[] = 656*69200Smckusick { 657*69200Smckusick {"readnow", no_argument, &readnow_symbol_files, 1}, 658*69200Smckusick {"r", no_argument, &readnow_symbol_files, 1}, 659*69200Smckusick {"mapped", no_argument, &mapped_symbol_files, 1}, 660*69200Smckusick {"m", no_argument, &mapped_symbol_files, 1}, 661*69200Smckusick {"quiet", no_argument, &quiet, 1}, 662*69200Smckusick {"q", no_argument, &quiet, 1}, 663*69200Smckusick {"version", no_argument, &quiet, 0}, 664*69200Smckusick {"nc", no_argument, &inhibit_confirm, 1}, 665*69200Smckusick {"nx", no_argument, &inhibit_gdbinit, 1}, 666*69200Smckusick {"nw", no_argument, &inhibit_windows, 1}, 667*69200Smckusick {"n", no_argument, &inhibit_gdbinit, 1}, 668*69200Smckusick {"batch", no_argument, &batch, 1}, 669*69200Smckusick {"epoch", no_argument, &epoch_interface, 1}, 670*69200Smckusick {"fullname", no_argument, &frame_file_full_name, 1}, 671*69200Smckusick {"f", no_argument, &frame_file_full_name, 1}, 672*69200Smckusick {"help", no_argument, &print_help, 1}, 673*69200Smckusick {"se", required_argument, 0, 10}, 674*69200Smckusick {"symbols", required_argument, 0, 's'}, 675*69200Smckusick {"s", required_argument, 0, 's'}, 676*69200Smckusick {"exec", required_argument, 0, 'e'}, 677*69200Smckusick {"e", required_argument, 0, 'e'}, 678*69200Smckusick {"core", required_argument, 0, 'c'}, 679*69200Smckusick {"c", required_argument, 0, 'c'}, 680*69200Smckusick {"command", required_argument, 0, 'x'}, 681*69200Smckusick {"x", required_argument, 0, 'x'}, 682*69200Smckusick {"directory", required_argument, 0, 'd'}, 683*69200Smckusick {"cd", required_argument, 0, 11}, 684*69200Smckusick {"tty", required_argument, 0, 't'}, 685*69200Smckusick {"baud", required_argument, 0, 'b'}, 686*69200Smckusick {"b", required_argument, 0, 'b'}, 687*69200Smckusick {"w", no_argument, &write_files, 1}, 688*69200Smckusick #ifdef KERNELDEBUG 689*69200Smckusick {"k", no_argument, &kernel_debugging, 1}, 690*69200Smckusick #endif 691*69200Smckusick /* Allow machine descriptions to add more options... */ 692*69200Smckusick #ifdef ADDITIONAL_OPTIONS 693*69200Smckusick ADDITIONAL_OPTIONS 694*69200Smckusick #endif 695*69200Smckusick {0, no_argument, 0, 0}, 696*69200Smckusick }; 697*69200Smckusick 698*69200Smckusick while (1) 699*69200Smckusick { 700*69200Smckusick int option_index; 701*69200Smckusick 702*69200Smckusick c = getopt_long_only (argc, argv, "", 703*69200Smckusick long_options, &option_index); 704*69200Smckusick if (c == EOF) 705*69200Smckusick break; 706*69200Smckusick 707*69200Smckusick /* Long option that takes an argument. */ 708*69200Smckusick if (c == 0 && long_options[option_index].flag == 0) 709*69200Smckusick c = long_options[option_index].val; 710*69200Smckusick 711*69200Smckusick switch (c) 712*69200Smckusick { 713*69200Smckusick case 0: 714*69200Smckusick /* Long option that just sets a flag. */ 715*69200Smckusick break; 716*69200Smckusick case 10: 717*69200Smckusick symarg = optarg; 718*69200Smckusick execarg = optarg; 719*69200Smckusick break; 720*69200Smckusick case 11: 721*69200Smckusick cdarg = optarg; 722*69200Smckusick break; 723*69200Smckusick case 's': 724*69200Smckusick symarg = optarg; 725*69200Smckusick break; 726*69200Smckusick case 'e': 727*69200Smckusick execarg = optarg; 728*69200Smckusick break; 729*69200Smckusick case 'c': 730*69200Smckusick corearg = optarg; 731*69200Smckusick break; 732*69200Smckusick case 'x': 733*69200Smckusick cmdarg[ncmd++] = optarg; 734*69200Smckusick if (ncmd >= cmdsize) 735*69200Smckusick { 736*69200Smckusick cmdsize *= 2; 737*69200Smckusick cmdarg = (char **) xrealloc ((char *)cmdarg, 738*69200Smckusick cmdsize * sizeof (*cmdarg)); 739*69200Smckusick } 740*69200Smckusick break; 741*69200Smckusick case 'd': 742*69200Smckusick dirarg[ndir++] = optarg; 743*69200Smckusick if (ndir >= dirsize) 744*69200Smckusick { 745*69200Smckusick dirsize *= 2; 746*69200Smckusick dirarg = (char **) xrealloc ((char *)dirarg, 747*69200Smckusick dirsize * sizeof (*dirarg)); 748*69200Smckusick } 749*69200Smckusick break; 750*69200Smckusick case 't': 751*69200Smckusick ttyarg = optarg; 752*69200Smckusick break; 753*69200Smckusick case 'q': 754*69200Smckusick quiet = 1; 755*69200Smckusick break; 756*69200Smckusick case 'b': 757*69200Smckusick baud_rate = optarg; 758*69200Smckusick break; 759*69200Smckusick #ifdef ADDITIONAL_OPTION_CASES 760*69200Smckusick ADDITIONAL_OPTION_CASES 761*69200Smckusick #endif 762*69200Smckusick case '?': 763*69200Smckusick fprintf (stderr, 764*69200Smckusick "Use `%s +help' for a complete list of options.\n", 765*69200Smckusick argv[0]); 766*69200Smckusick exit (1); 767*69200Smckusick } 768*69200Smckusick 769*69200Smckusick } 770*69200Smckusick if (print_help) 771*69200Smckusick { 772*69200Smckusick fputs ("\ 773*69200Smckusick This is GDB, the GNU debugger. Use the command\n\ 774*69200Smckusick gdb [options] [executable [core-file]]\n\ 775*69200Smckusick to enter the debugger.\n\ 776*69200Smckusick \n\ 777*69200Smckusick Options available are:\n\ 778*69200Smckusick -help Print this message.\n\ 779*69200Smckusick -quiet Do not print version number on startup.\n\ 780*69200Smckusick -fullname Output information used by emacs-GDB interface.\n\ 781*69200Smckusick -epoch Output information used by epoch emacs-GDB interface.\n\ 782*69200Smckusick -batch Exit after processing options.\n\ 783*69200Smckusick -nx Do not read .gdbinit file.\n\ 784*69200Smckusick -tty=TTY Use TTY for input/output by the program being debugged.\n\ 785*69200Smckusick -cd=DIR Change current directory to DIR.\n\ 786*69200Smckusick -directory=DIR Search for source files in DIR.\n\ 787*69200Smckusick -command=FILE Execute GDB commands from FILE.\n\ 788*69200Smckusick -symbols=SYMFILE Read symbols from SYMFILE.\n\ 789*69200Smckusick -exec=EXECFILE Use EXECFILE as the executable.\n\ 790*69200Smckusick -se=FILE Use FILE as symbol file and executable file.\n\ 791*69200Smckusick -core=COREFILE Analyze the core dump COREFILE.\n\ 792*69200Smckusick -b BAUDRATE Set serial port baud rate used for remote debugging.\n\ 793*69200Smckusick -mapped Use mapped symbol files if supported on this system.\n\ 794*69200Smckusick -readnow Fully read symbol files on first access.\n\ 795*69200Smckusick -k Kernel debugging.\n\ 796*69200Smckusick -w Writeable text.\n\ 797*69200Smckusick -version Print GNU message and version number on startup.\n\ 798*69200Smckusick -nc Don't confirm quit or run commands.\n\ 799*69200Smckusick ", stderr); 800*69200Smckusick #ifdef ADDITIONAL_OPTION_HELP 801*69200Smckusick fputs (ADDITIONAL_OPTION_HELP, stderr); 802*69200Smckusick #endif 803*69200Smckusick fputs ("\n\ 804*69200Smckusick For more information, type \"help\" from within GDB, or consult the\n\ 805*69200Smckusick GDB manual (available as on-line info or a printed manual).\n", stderr); 806*69200Smckusick /* Exiting after printing this message seems like 807*69200Smckusick the most useful thing to do. */ 808*69200Smckusick exit (0); 809*69200Smckusick } 810*69200Smckusick 811*69200Smckusick /* OK, that's all the options. The other arguments are filenames. */ 812*69200Smckusick count = 0; 813*69200Smckusick for (; optind < argc; optind++) 814*69200Smckusick switch (++count) 815*69200Smckusick { 816*69200Smckusick case 1: 817*69200Smckusick symarg = argv[optind]; 818*69200Smckusick execarg = argv[optind]; 819*69200Smckusick break; 820*69200Smckusick case 2: 821*69200Smckusick corearg = argv[optind]; 822*69200Smckusick break; 823*69200Smckusick case 3: 824*69200Smckusick fprintf (stderr, 825*69200Smckusick "Excess command line arguments ignored. (%s%s)\n", 826*69200Smckusick argv[optind], (optind == argc - 1) ? "" : " ..."); 827*69200Smckusick break; 828*69200Smckusick } 829*69200Smckusick if (batch) 830*69200Smckusick quiet = 1; 831*69200Smckusick } 832*69200Smckusick 833*69200Smckusick /* Run the init function of each source file */ 834*69200Smckusick 835*69200Smckusick initialize_cmd_lists (); /* This needs to be done first */ 836*69200Smckusick initialize_all_files (); 837*69200Smckusick initialize_main (); /* But that omits this file! Do it now */ 838*69200Smckusick init_signals (); 839*69200Smckusick 840*69200Smckusick if (!quiet) 841*69200Smckusick { 842*69200Smckusick /* Print all the junk at the top, with trailing "..." if we are about 843*69200Smckusick to read a symbol file (possibly slowly). */ 844*69200Smckusick print_gnu_advertisement (); 845*69200Smckusick print_gdb_version (); 846*69200Smckusick if (symarg) 847*69200Smckusick printf_filtered (".."); 848*69200Smckusick wrap_here(""); 849*69200Smckusick fflush (stdout); /* Force to screen during slow operations */ 850*69200Smckusick } 851*69200Smckusick 852*69200Smckusick error_pre_print = "\n\n"; 853*69200Smckusick /* We may get more than one warning, don't double space all of them... */ 854*69200Smckusick warning_pre_print = "\nwarning: "; 855*69200Smckusick 856*69200Smckusick /* We need a default language for parsing expressions, so simple things like 857*69200Smckusick "set width 0" won't fail if no language is explicitly set in a config file 858*69200Smckusick or implicitly set by reading an executable during startup. */ 859*69200Smckusick set_language (language_c); 860*69200Smckusick expected_language = current_language; /* don't warn about the change. */ 861*69200Smckusick 862*69200Smckusick /* Now perform all the actions indicated by the arguments. */ 863*69200Smckusick if (cdarg != NULL) 864*69200Smckusick { 865*69200Smckusick if (!setjmp (to_top_level)) 866*69200Smckusick { 867*69200Smckusick cd_command (cdarg, 0); 868*69200Smckusick init_source_path (); 869*69200Smckusick } 870*69200Smckusick } 871*69200Smckusick do_cleanups (ALL_CLEANUPS); 872*69200Smckusick 873*69200Smckusick for (i = 0; i < ndir; i++) 874*69200Smckusick if (!setjmp (to_top_level)) 875*69200Smckusick directory_command (dirarg[i], 0); 876*69200Smckusick free ((PTR)dirarg); 877*69200Smckusick do_cleanups (ALL_CLEANUPS); 878*69200Smckusick 879*69200Smckusick if (execarg != NULL 880*69200Smckusick && symarg != NULL 881*69200Smckusick && strcmp (execarg, symarg) == 0) 882*69200Smckusick { 883*69200Smckusick /* The exec file and the symbol-file are the same. If we can't open 884*69200Smckusick it, better only print one error message. */ 885*69200Smckusick if (!setjmp (to_top_level)) 886*69200Smckusick { 887*69200Smckusick exec_file_command (execarg, !batch); 888*69200Smckusick symbol_file_command (symarg, 0); 889*69200Smckusick } 890*69200Smckusick } 891*69200Smckusick else 892*69200Smckusick { 893*69200Smckusick if (execarg != NULL) 894*69200Smckusick if (!setjmp (to_top_level)) 895*69200Smckusick exec_file_command (execarg, !batch); 896*69200Smckusick if (symarg != NULL) 897*69200Smckusick if (!setjmp (to_top_level)) 898*69200Smckusick symbol_file_command (symarg, 0); 899*69200Smckusick } 900*69200Smckusick do_cleanups (ALL_CLEANUPS); 901*69200Smckusick 902*69200Smckusick /* After the symbol file has been read, print a newline to get us 903*69200Smckusick beyond the copyright line... But errors should still set off 904*69200Smckusick the error message with a (single) blank line. */ 905*69200Smckusick if (!quiet) 906*69200Smckusick printf_filtered ("\n"); 907*69200Smckusick error_pre_print = "\n"; 908*69200Smckusick warning_pre_print = "\nwarning: "; 909*69200Smckusick 910*69200Smckusick if (corearg != NULL) 911*69200Smckusick do_core_arg(corearg, batch); 912*69200Smckusick do_cleanups (ALL_CLEANUPS); 913*69200Smckusick 914*69200Smckusick if (ttyarg != NULL) 915*69200Smckusick if (!setjmp (to_top_level)) 916*69200Smckusick tty_command (ttyarg, !batch); 917*69200Smckusick do_cleanups (ALL_CLEANUPS); 918*69200Smckusick 919*69200Smckusick #ifdef ADDITIONAL_OPTION_HANDLER 920*69200Smckusick ADDITIONAL_OPTION_HANDLER; 921*69200Smckusick #endif 922*69200Smckusick 923*69200Smckusick /* Error messages should no longer be distinguished with extra output. */ 924*69200Smckusick error_pre_print = 0; 925*69200Smckusick warning_pre_print = "warning: "; 926*69200Smckusick 927*69200Smckusick if (!inhibit_gdbinit) 928*69200Smckusick source_init_files(); 929*69200Smckusick 930*69200Smckusick for (i = 0; i < ncmd; i++) 931*69200Smckusick { 932*69200Smckusick if (!setjmp (to_top_level)) 933*69200Smckusick { 934*69200Smckusick if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0') 935*69200Smckusick read_command_file (stdin); 936*69200Smckusick else 937*69200Smckusick source_command (cmdarg[i], !batch); 938*69200Smckusick do_cleanups (ALL_CLEANUPS); 939*69200Smckusick } 940*69200Smckusick } 941*69200Smckusick free ((PTR)cmdarg); 942*69200Smckusick 943*69200Smckusick /* Read in the old history after all the command files have been read. */ 944*69200Smckusick initialize_history(); 945*69200Smckusick 946*69200Smckusick if (batch) 947*69200Smckusick { 948*69200Smckusick /* We have hit the end of the batch file. */ 949*69200Smckusick exit (0); 950*69200Smckusick } 951*69200Smckusick 952*69200Smckusick /* Do any host- or target-specific hacks. This is used for i960 targets 953*69200Smckusick to force the user to set a nindy target and spec its parameters. */ 954*69200Smckusick 955*69200Smckusick #ifdef BEFORE_MAIN_LOOP_HOOK 956*69200Smckusick BEFORE_MAIN_LOOP_HOOK; 957*69200Smckusick #endif 958*69200Smckusick 959*69200Smckusick /* The command loop. */ 960*69200Smckusick 961*69200Smckusick while (1) 962*69200Smckusick { 963*69200Smckusick if (!setjmp (to_top_level)) 964*69200Smckusick { 965*69200Smckusick do_cleanups (ALL_CLEANUPS); /* Do complete cleanup */ 966*69200Smckusick command_loop (); 967*69200Smckusick quit_command ((char *)0, instream == stdin); 968*69200Smckusick } 969*69200Smckusick } 970*69200Smckusick /* No exit -- exit is through quit_command. */ 971*69200Smckusick } 972*69200Smckusick 973*69200Smckusick void 974*69200Smckusick execute_user_command (c, args) 975*69200Smckusick struct cmd_list_element *c; 976*69200Smckusick char *args; 977*69200Smckusick { 978*69200Smckusick register struct command_line *cmdlines; 979*69200Smckusick struct cleanup *old_chain; 980*69200Smckusick 981*69200Smckusick if (args) 982*69200Smckusick error ("User-defined commands cannot take arguments."); 983*69200Smckusick 984*69200Smckusick cmdlines = c->user_commands; 985*69200Smckusick if (cmdlines == 0) 986*69200Smckusick /* Null command */ 987*69200Smckusick return; 988*69200Smckusick 989*69200Smckusick /* Set the instream to 0, indicating execution of a 990*69200Smckusick user-defined function. */ 991*69200Smckusick old_chain = make_cleanup (source_cleanup, instream); 992*69200Smckusick instream = (FILE *) 0; 993*69200Smckusick while (cmdlines) 994*69200Smckusick { 995*69200Smckusick execute_command (cmdlines->line, 0); 996*69200Smckusick cmdlines = cmdlines->next; 997*69200Smckusick } 998*69200Smckusick do_cleanups (old_chain); 999*69200Smckusick } 1000*69200Smckusick 1001*69200Smckusick /* ARGSUSED */ 1002*69200Smckusick static void 1003*69200Smckusick command_loop_marker (foo) 1004*69200Smckusick int foo; 1005*69200Smckusick { 1006*69200Smckusick } 1007*69200Smckusick 1008*69200Smckusick /* Read commands from `instream' and execute them 1009*69200Smckusick until end of file or error reading instream. */ 1010*69200Smckusick static void 1011*69200Smckusick command_loop () 1012*69200Smckusick { 1013*69200Smckusick struct cleanup *old_chain; 1014*69200Smckusick register int toplevel = (instream == stdin); 1015*69200Smckusick register int interactive = (toplevel && ISATTY(stdin)); 1016*69200Smckusick 1017*69200Smckusick while (!feof(instream)) 1018*69200Smckusick { 1019*69200Smckusick register char *cmd_line; 1020*69200Smckusick 1021*69200Smckusick quit_flag = 0; 1022*69200Smckusick if (interactive) 1023*69200Smckusick reinitialize_more_filter (); 1024*69200Smckusick old_chain = make_cleanup (command_loop_marker, 0); 1025*69200Smckusick cmd_line = command_line_input (toplevel ? masterprompt : 0, toplevel); 1026*69200Smckusick if (cmd_line == 0) 1027*69200Smckusick { 1028*69200Smckusick do_cleanups (old_chain); 1029*69200Smckusick return; 1030*69200Smckusick } 1031*69200Smckusick execute_command (cmd_line, toplevel); 1032*69200Smckusick /* Do any commands attached to breakpoint we stopped at. */ 1033*69200Smckusick bpstat_do_actions (&stop_bpstat); 1034*69200Smckusick do_cleanups (old_chain); 1035*69200Smckusick } 1036*69200Smckusick } 1037*69200Smckusick 1038*69200Smckusick /* Read a line from the stream "instream" without command line editing. 1039*69200Smckusick 1040*69200Smckusick It prints PROMPT once at the start. 1041*69200Smckusick Action is compatible with "readline", e.g. space for the result is 1042*69200Smckusick malloc'd and should be freed by the caller. 1043*69200Smckusick 1044*69200Smckusick A NULL return means end of file. */ 1045*69200Smckusick char * 1046*69200Smckusick gdb_readline (prompt) 1047*69200Smckusick char *prompt; 1048*69200Smckusick { 1049*69200Smckusick int c; 1050*69200Smckusick char *result; 1051*69200Smckusick int input_index = 0; 1052*69200Smckusick int result_size = 80; 1053*69200Smckusick 1054*69200Smckusick if (prompt) 1055*69200Smckusick { 1056*69200Smckusick /* Don't use a _filtered function here. It causes the assumed 1057*69200Smckusick character position to be off, since the newline we read from 1058*69200Smckusick the user is not accounted for. */ 1059*69200Smckusick fputs (prompt, stdout); 1060*69200Smckusick fflush (stdout); 1061*69200Smckusick } 1062*69200Smckusick 1063*69200Smckusick result = (char *) xmalloc (result_size); 1064*69200Smckusick 1065*69200Smckusick while (1) 1066*69200Smckusick { 1067*69200Smckusick /* Read from stdin if we are executing a user defined command. 1068*69200Smckusick This is the right thing for prompt_for_continue, at least. */ 1069*69200Smckusick c = fgetc (instream ? instream : stdin); 1070*69200Smckusick 1071*69200Smckusick if (c == EOF) 1072*69200Smckusick { 1073*69200Smckusick free (result); 1074*69200Smckusick return NULL; 1075*69200Smckusick } 1076*69200Smckusick 1077*69200Smckusick if (c == '\n') 1078*69200Smckusick break; 1079*69200Smckusick 1080*69200Smckusick result[input_index++] = c; 1081*69200Smckusick while (input_index >= result_size) 1082*69200Smckusick { 1083*69200Smckusick result_size *= 2; 1084*69200Smckusick result = (char *) xrealloc (result, result_size); 1085*69200Smckusick } 1086*69200Smckusick } 1087*69200Smckusick 1088*69200Smckusick result[input_index++] = '\0'; 1089*69200Smckusick return result; 1090*69200Smckusick } 1091*69200Smckusick 1092*69200Smckusick /* Variables which control command line editing and history 1093*69200Smckusick substitution. These variables are given default values at the end 1094*69200Smckusick of this file. */ 1095*69200Smckusick static int write_history_p; 1096*69200Smckusick static unsigned int history_size; 1097*69200Smckusick static char *history_filename; 1098*69200Smckusick 1099*69200Smckusick /* Variables which are necessary for fancy command line editing. */ 1100*69200Smckusick char *gdb_completer_word_break_characters = 1101*69200Smckusick " \t\n!@#$%^&*()+=|~`}{[]\"';?/>.<,-"; 1102*69200Smckusick 1103*69200Smckusick /* When completing on command names, we remove '-' from the list of 1104*69200Smckusick word break characters, since we use it in command names. If the 1105*69200Smckusick readline library sees one in any of the current completion strings, 1106*69200Smckusick it thinks that the string needs to be quoted and automatically supplies 1107*69200Smckusick a leading quote. */ 1108*69200Smckusick char *gdb_completer_command_word_break_characters = 1109*69200Smckusick " \t\n!@#$%^&*()+=|~`}{[]\"';?/>.<,"; 1110*69200Smckusick 1111*69200Smckusick /* Characters that can be used to quote completion strings. Note that we 1112*69200Smckusick can't include '"' because the gdb C parser treats such quoted sequences 1113*69200Smckusick as strings. */ 1114*69200Smckusick char *gdb_completer_quote_characters = 1115*69200Smckusick "'"; 1116*69200Smckusick 1117*69200Smckusick /* Functions that are used as part of the fancy command line editing. */ 1118*69200Smckusick 1119*69200Smckusick /* This can be used for functions which don't want to complete on symbols 1120*69200Smckusick but don't want to complete on anything else either. */ 1121*69200Smckusick /* ARGSUSED */ 1122*69200Smckusick char ** 1123*69200Smckusick noop_completer (text) 1124*69200Smckusick char *text; 1125*69200Smckusick { 1126*69200Smckusick return NULL; 1127*69200Smckusick } 1128*69200Smckusick 1129*69200Smckusick /* Generate symbol names one by one for the completer. Each time we are 1130*69200Smckusick called return another potential completion to the caller. 1131*69200Smckusick 1132*69200Smckusick TEXT is what we expect the symbol to start with. 1133*69200Smckusick 1134*69200Smckusick MATCHES is the number of matches that have currently been collected from 1135*69200Smckusick calling this completion function. When zero, then we need to initialize, 1136*69200Smckusick otherwise the initialization has already taken place and we can just 1137*69200Smckusick return the next potential completion string. 1138*69200Smckusick 1139*69200Smckusick Returns NULL if there are no more completions, else a pointer to a string 1140*69200Smckusick which is a possible completion. 1141*69200Smckusick 1142*69200Smckusick RL_LINE_BUFFER is available to be looked at; it contains the entire text 1143*69200Smckusick of the line. RL_POINT is the offset in that line of the cursor. You 1144*69200Smckusick should pretend that the line ends at RL_POINT. */ 1145*69200Smckusick 1146*69200Smckusick static char * 1147*69200Smckusick symbol_completion_function (text, matches) 1148*69200Smckusick char *text; 1149*69200Smckusick int matches; 1150*69200Smckusick { 1151*69200Smckusick static char **list = (char **)NULL; /* Cache of completions */ 1152*69200Smckusick static int index; /* Next cached completion */ 1153*69200Smckusick char *output = NULL; 1154*69200Smckusick char *tmp_command, *p; 1155*69200Smckusick struct cmd_list_element *c, *result_list; 1156*69200Smckusick extern char *rl_line_buffer; 1157*69200Smckusick extern int rl_point; 1158*69200Smckusick 1159*69200Smckusick if (matches == 0) 1160*69200Smckusick { 1161*69200Smckusick /* The caller is beginning to accumulate a new set of completions, so 1162*69200Smckusick we need to find all of them now, and cache them for returning one at 1163*69200Smckusick a time on future calls. */ 1164*69200Smckusick 1165*69200Smckusick if (list) 1166*69200Smckusick { 1167*69200Smckusick /* Free the storage used by LIST, but not by the strings inside. 1168*69200Smckusick This is because rl_complete_internal () frees the strings. */ 1169*69200Smckusick free ((PTR)list); 1170*69200Smckusick } 1171*69200Smckusick list = 0; 1172*69200Smckusick index = 0; 1173*69200Smckusick 1174*69200Smckusick /* Choose the default set of word break characters to break completions. 1175*69200Smckusick If we later find out that we are doing completions on command strings 1176*69200Smckusick (as opposed to strings supplied by the individual command completer 1177*69200Smckusick functions, which can be any string) then we will switch to the 1178*69200Smckusick special word break set for command strings, which leaves out the 1179*69200Smckusick '-' character used in some commands. */ 1180*69200Smckusick 1181*69200Smckusick rl_completer_word_break_characters = 1182*69200Smckusick gdb_completer_word_break_characters; 1183*69200Smckusick 1184*69200Smckusick /* Decide whether to complete on a list of gdb commands or on symbols. */ 1185*69200Smckusick tmp_command = (char *) alloca (rl_point + 1); 1186*69200Smckusick p = tmp_command; 1187*69200Smckusick 1188*69200Smckusick strncpy (tmp_command, rl_line_buffer, rl_point); 1189*69200Smckusick tmp_command[rl_point] = '\0'; 1190*69200Smckusick 1191*69200Smckusick if (rl_point == 0) 1192*69200Smckusick { 1193*69200Smckusick /* An empty line we want to consider ambiguous; that is, it 1194*69200Smckusick could be any command. */ 1195*69200Smckusick c = (struct cmd_list_element *) -1; 1196*69200Smckusick result_list = 0; 1197*69200Smckusick } 1198*69200Smckusick else 1199*69200Smckusick { 1200*69200Smckusick c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); 1201*69200Smckusick } 1202*69200Smckusick 1203*69200Smckusick /* Move p up to the next interesting thing. */ 1204*69200Smckusick while (*p == ' ' || *p == '\t') 1205*69200Smckusick { 1206*69200Smckusick p++; 1207*69200Smckusick } 1208*69200Smckusick 1209*69200Smckusick if (!c) 1210*69200Smckusick { 1211*69200Smckusick /* He's typed something unrecognizable. Sigh. */ 1212*69200Smckusick list = NULL; 1213*69200Smckusick } 1214*69200Smckusick else if (c == (struct cmd_list_element *) -1) 1215*69200Smckusick { 1216*69200Smckusick /* If we didn't recognize everything up to the thing that 1217*69200Smckusick needs completing, and we don't know what command it is 1218*69200Smckusick yet, we are in trouble. */ 1219*69200Smckusick 1220*69200Smckusick if (p + strlen(text) != tmp_command + rl_point) 1221*69200Smckusick { 1222*69200Smckusick /* This really should not produce an error. Better would 1223*69200Smckusick be to pretend to hit RETURN here; this would produce a 1224*69200Smckusick response like "Ambiguous command: foo, foobar, etc", 1225*69200Smckusick and leave the line available for re-entry with ^P. 1226*69200Smckusick Instead, this error blows away the user's typed input 1227*69200Smckusick without any way to get it back. */ 1228*69200Smckusick error (" Unrecognized command."); 1229*69200Smckusick } 1230*69200Smckusick 1231*69200Smckusick /* He's typed something ambiguous. This is easier. */ 1232*69200Smckusick if (result_list) 1233*69200Smckusick { 1234*69200Smckusick list = complete_on_cmdlist (*result_list->prefixlist, text); 1235*69200Smckusick } 1236*69200Smckusick else 1237*69200Smckusick { 1238*69200Smckusick list = complete_on_cmdlist (cmdlist, text); 1239*69200Smckusick } 1240*69200Smckusick rl_completer_word_break_characters = 1241*69200Smckusick gdb_completer_command_word_break_characters; 1242*69200Smckusick } 1243*69200Smckusick else 1244*69200Smckusick { 1245*69200Smckusick /* If we've gotten this far, gdb has recognized a full 1246*69200Smckusick command. There are several possibilities: 1247*69200Smckusick 1248*69200Smckusick 1) We need to complete on the command. 1249*69200Smckusick 2) We need to complete on the possibilities coming after 1250*69200Smckusick the command. 1251*69200Smckusick 2) We need to complete the text of what comes after the 1252*69200Smckusick command. */ 1253*69200Smckusick 1254*69200Smckusick if (!*p && *text) 1255*69200Smckusick { 1256*69200Smckusick /* Always (might be longer versions of thie command). */ 1257*69200Smckusick list = complete_on_cmdlist (result_list, text); 1258*69200Smckusick rl_completer_word_break_characters = 1259*69200Smckusick gdb_completer_command_word_break_characters; 1260*69200Smckusick } 1261*69200Smckusick else if (!*p && !*text) 1262*69200Smckusick { 1263*69200Smckusick if (c->prefixlist) 1264*69200Smckusick { 1265*69200Smckusick list = complete_on_cmdlist (*c->prefixlist, ""); 1266*69200Smckusick rl_completer_word_break_characters = 1267*69200Smckusick gdb_completer_command_word_break_characters; 1268*69200Smckusick } 1269*69200Smckusick else 1270*69200Smckusick { 1271*69200Smckusick list = (*c->completer) (""); 1272*69200Smckusick } 1273*69200Smckusick } 1274*69200Smckusick else 1275*69200Smckusick { 1276*69200Smckusick if (c->prefixlist && !c->allow_unknown) 1277*69200Smckusick { 1278*69200Smckusick /* Something like "info adsfkdj". But error() is not the 1279*69200Smckusick proper response; just return no completions instead. */ 1280*69200Smckusick list = NULL; 1281*69200Smckusick } 1282*69200Smckusick else 1283*69200Smckusick { 1284*69200Smckusick list = (*c->completer) (text); 1285*69200Smckusick } 1286*69200Smckusick } 1287*69200Smckusick } 1288*69200Smckusick } 1289*69200Smckusick 1290*69200Smckusick /* If we found a list of potential completions during initialization then 1291*69200Smckusick dole them out one at a time. The vector of completions is NULL 1292*69200Smckusick terminated, so after returning the last one, return NULL (and continue 1293*69200Smckusick to do so) each time we are called after that, until a new list is 1294*69200Smckusick available. */ 1295*69200Smckusick 1296*69200Smckusick if (list) 1297*69200Smckusick { 1298*69200Smckusick output = list[index]; 1299*69200Smckusick if (output) 1300*69200Smckusick { 1301*69200Smckusick index++; 1302*69200Smckusick } 1303*69200Smckusick } 1304*69200Smckusick 1305*69200Smckusick return (output); 1306*69200Smckusick } 1307*69200Smckusick 1308*69200Smckusick /* Skip over a possibly quoted word (as defined by the quote characters 1309*69200Smckusick and word break characters the completer uses). Returns pointer to the 1310*69200Smckusick location after the "word". */ 1311*69200Smckusick 1312*69200Smckusick char * 1313*69200Smckusick skip_quoted (str) 1314*69200Smckusick char *str; 1315*69200Smckusick { 1316*69200Smckusick char quote_char = '\0'; 1317*69200Smckusick char *scan; 1318*69200Smckusick 1319*69200Smckusick for (scan = str; *scan != '\0'; scan++) 1320*69200Smckusick { 1321*69200Smckusick if (quote_char != '\0') 1322*69200Smckusick { 1323*69200Smckusick /* Ignore everything until the matching close quote char */ 1324*69200Smckusick if (*scan == quote_char) 1325*69200Smckusick { 1326*69200Smckusick /* Found matching close quote. */ 1327*69200Smckusick scan++; 1328*69200Smckusick break; 1329*69200Smckusick } 1330*69200Smckusick } 1331*69200Smckusick else if (strchr (gdb_completer_quote_characters, *scan)) 1332*69200Smckusick { 1333*69200Smckusick /* Found start of a quoted string. */ 1334*69200Smckusick quote_char = *scan; 1335*69200Smckusick } 1336*69200Smckusick else if (strchr (gdb_completer_word_break_characters, *scan)) 1337*69200Smckusick { 1338*69200Smckusick break; 1339*69200Smckusick } 1340*69200Smckusick } 1341*69200Smckusick return (scan); 1342*69200Smckusick } 1343*69200Smckusick 1344*69200Smckusick 1345*69200Smckusick #ifdef STOP_SIGNAL 1346*69200Smckusick static void 1347*69200Smckusick stop_sig (signo) 1348*69200Smckusick int signo; 1349*69200Smckusick { 1350*69200Smckusick #if STOP_SIGNAL == SIGTSTP 1351*69200Smckusick signal (SIGTSTP, SIG_DFL); 1352*69200Smckusick sigsetmask (0); 1353*69200Smckusick kill (getpid (), SIGTSTP); 1354*69200Smckusick signal (SIGTSTP, stop_sig); 1355*69200Smckusick #else 1356*69200Smckusick signal (STOP_SIGNAL, stop_sig); 1357*69200Smckusick #endif 1358*69200Smckusick printf ("%s", masterprompt); 1359*69200Smckusick fflush (stdout); 1360*69200Smckusick 1361*69200Smckusick /* Forget about any previous command -- null line now will do nothing. */ 1362*69200Smckusick dont_repeat (); 1363*69200Smckusick } 1364*69200Smckusick #endif /* STOP_SIGNAL */ 1365*69200Smckusick 1366*69200Smckusick /* Initialize signal handlers. */ 1367*69200Smckusick static void 1368*69200Smckusick do_nothing (signo) 1369*69200Smckusick int signo; 1370*69200Smckusick { 1371*69200Smckusick } 1372*69200Smckusick 1373*69200Smckusick static void suspend_sig (); 1374*69200Smckusick 1375*69200Smckusick static void 1376*69200Smckusick init_signals () 1377*69200Smckusick { 1378*69200Smckusick signal (SIGINT, request_quit); 1379*69200Smckusick 1380*69200Smckusick /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get 1381*69200Smckusick passed to the inferior, which we don't want. It would be 1382*69200Smckusick possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but 1383*69200Smckusick on BSD4.3 systems using vfork, that will (apparently) affect the 1384*69200Smckusick GDB process as well as the inferior (the signal handling tables 1385*69200Smckusick being shared between the two, apparently). Since we establish 1386*69200Smckusick a handler for SIGQUIT, when we call exec it will set the signal 1387*69200Smckusick to SIG_DFL for us. */ 1388*69200Smckusick signal (SIGQUIT, do_nothing); 1389*69200Smckusick if (signal (SIGHUP, do_nothing) != SIG_IGN) 1390*69200Smckusick signal (SIGHUP, disconnect); 1391*69200Smckusick signal (SIGFPE, float_handler); 1392*69200Smckusick 1393*69200Smckusick #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) 1394*69200Smckusick signal (SIGWINCH, SIGWINCH_HANDLER); 1395*69200Smckusick #endif 1396*69200Smckusick 1397*69200Smckusick init_term(fileno(stdin)); 1398*69200Smckusick signal(SIGTSTP, suspend_sig); 1399*69200Smckusick } 1400*69200Smckusick 1401*69200Smckusick /* Execute the line P as a command. 1402*69200Smckusick Pass FROM_TTY as second argument to the defining function. */ 1403*69200Smckusick 1404*69200Smckusick void 1405*69200Smckusick execute_command (p, from_tty) 1406*69200Smckusick char *p; 1407*69200Smckusick int from_tty; 1408*69200Smckusick { 1409*69200Smckusick register struct cmd_list_element *c; 1410*69200Smckusick register struct command_line *cmdlines; 1411*69200Smckusick register enum language flang; 1412*69200Smckusick static const struct language_defn *saved_language = 0; 1413*69200Smckusick static int warned = 0; 1414*69200Smckusick 1415*69200Smckusick free_all_values (); 1416*69200Smckusick 1417*69200Smckusick /* This can happen when command_line_input hits end of file. */ 1418*69200Smckusick if (p == NULL) 1419*69200Smckusick return; 1420*69200Smckusick 1421*69200Smckusick while (*p == ' ' || *p == '\t') p++; 1422*69200Smckusick if (*p) { 1423*69200Smckusick char *arg; 1424*69200Smckusick 1425*69200Smckusick c = lookup_cmd(&p, cmdlist, "", 0, 1); 1426*69200Smckusick /* Pass null arg rather than an empty one. */ 1427*69200Smckusick arg = *p ? p : 0; 1428*69200Smckusick if (c->function.cfunc == 0) 1429*69200Smckusick error("That is not a command, just a help topic."); 1430*69200Smckusick else if (c->class == (int) class_user) { 1431*69200Smckusick extern struct cleanup *setup_user_args(); 1432*69200Smckusick 1433*69200Smckusick struct cleanup *old_chain = setup_user_args(p); 1434*69200Smckusick 1435*69200Smckusick if (c->user_commands != 0) 1436*69200Smckusick (void)execute_command_lines(c->user_commands); 1437*69200Smckusick 1438*69200Smckusick do_cleanups(old_chain); 1439*69200Smckusick } else if (c->type == set_cmd || c->type == show_cmd) 1440*69200Smckusick do_setshow_command (arg, from_tty & caution, c); 1441*69200Smckusick else if (c->function.cfunc == NO_FUNCTION) 1442*69200Smckusick error ("That is not a command, just a help topic."); 1443*69200Smckusick else 1444*69200Smckusick (*c->function.cfunc) (arg, from_tty & caution); 1445*69200Smckusick } 1446*69200Smckusick /* Tell the user if the language has changed (except first time). */ 1447*69200Smckusick if (current_language != saved_language) 1448*69200Smckusick { 1449*69200Smckusick if (language_mode == language_mode_auto) { 1450*69200Smckusick if (saved_language) 1451*69200Smckusick language_info (1); 1452*69200Smckusick } 1453*69200Smckusick saved_language = current_language; 1454*69200Smckusick warned = 0; 1455*69200Smckusick } 1456*69200Smckusick 1457*69200Smckusick /* Warn the user if the working language does not match the 1458*69200Smckusick language of the current frame. Only warn the user if we are 1459*69200Smckusick actually running the program, i.e. there is a stack. */ 1460*69200Smckusick /* FIXME: This should be cacheing the frame and only running when 1461*69200Smckusick the frame changes. */ 1462*69200Smckusick if (target_has_stack) 1463*69200Smckusick { 1464*69200Smckusick flang = get_frame_language (); 1465*69200Smckusick if (!warned 1466*69200Smckusick && flang != language_unknown 1467*69200Smckusick && flang != current_language->la_language) 1468*69200Smckusick { 1469*69200Smckusick printf_filtered ("%s\n", lang_frame_mismatch_warn); 1470*69200Smckusick warned = 1; 1471*69200Smckusick } 1472*69200Smckusick } 1473*69200Smckusick } 1474*69200Smckusick 1475*69200Smckusick /* Add an element to the list of info subcommands. */ 1476*69200Smckusick 1477*69200Smckusick void 1478*69200Smckusick add_info (name, fun, doc) 1479*69200Smckusick char *name; 1480*69200Smckusick void (*fun) PARAMS ((char *, int)); 1481*69200Smckusick char *doc; 1482*69200Smckusick { 1483*69200Smckusick add_cmd (name, no_class, fun, doc, &infolist); 1484*69200Smckusick } 1485*69200Smckusick 1486*69200Smckusick /* Add an alias to the list of info subcommands. */ 1487*69200Smckusick 1488*69200Smckusick void 1489*69200Smckusick add_info_alias (name, oldname, abbrev_flag) 1490*69200Smckusick char *name; 1491*69200Smckusick char *oldname; 1492*69200Smckusick int abbrev_flag; 1493*69200Smckusick { 1494*69200Smckusick add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); 1495*69200Smckusick } 1496*69200Smckusick 1497*69200Smckusick /* The "info" command is defined as a prefix, with allow_unknown = 0. 1498*69200Smckusick Therefore, its own definition is called only for "info" with no args. */ 1499*69200Smckusick 1500*69200Smckusick /* ARGSUSED */ 1501*69200Smckusick static void 1502*69200Smckusick info_command (arg, from_tty) 1503*69200Smckusick char *arg; 1504*69200Smckusick int from_tty; 1505*69200Smckusick { 1506*69200Smckusick printf ("\"info\" must be followed by the name of an info command.\n"); 1507*69200Smckusick help_list (infolist, "info ", -1, stdout); 1508*69200Smckusick } 1509*69200Smckusick 1510*69200Smckusick /* The "show" command with no arguments shows all the settings. */ 1511*69200Smckusick 1512*69200Smckusick /* ARGSUSED */ 1513*69200Smckusick static void 1514*69200Smckusick show_command (arg, from_tty) 1515*69200Smckusick char *arg; 1516*69200Smckusick int from_tty; 1517*69200Smckusick { 1518*69200Smckusick cmd_show_list (showlist, from_tty, ""); 1519*69200Smckusick } 1520*69200Smckusick 1521*69200Smckusick /* Add an element to the list of commands. */ 1522*69200Smckusick 1523*69200Smckusick void 1524*69200Smckusick add_com (name, class, fun, doc) 1525*69200Smckusick char *name; 1526*69200Smckusick enum command_class class; 1527*69200Smckusick void (*fun) PARAMS ((char *, int)); 1528*69200Smckusick char *doc; 1529*69200Smckusick { 1530*69200Smckusick add_cmd (name, class, fun, doc, &cmdlist); 1531*69200Smckusick } 1532*69200Smckusick 1533*69200Smckusick /* Add an alias or abbreviation command to the list of commands. */ 1534*69200Smckusick 1535*69200Smckusick void 1536*69200Smckusick add_com_alias (name, oldname, class, abbrev_flag) 1537*69200Smckusick char *name; 1538*69200Smckusick char *oldname; 1539*69200Smckusick enum command_class class; 1540*69200Smckusick int abbrev_flag; 1541*69200Smckusick { 1542*69200Smckusick add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); 1543*69200Smckusick } 1544*69200Smckusick 1545*69200Smckusick void 1546*69200Smckusick error_no_arg (why) 1547*69200Smckusick char *why; 1548*69200Smckusick { 1549*69200Smckusick error ("Argument required (%s).", why); 1550*69200Smckusick } 1551*69200Smckusick 1552*69200Smckusick /* ARGSUSED */ 1553*69200Smckusick static void 1554*69200Smckusick help_command (command, from_tty) 1555*69200Smckusick char *command; 1556*69200Smckusick int from_tty; /* Ignored */ 1557*69200Smckusick { 1558*69200Smckusick help_cmd (command, stdout); 1559*69200Smckusick } 1560*69200Smckusick 1561*69200Smckusick static void 1562*69200Smckusick validate_comname (comname) 1563*69200Smckusick char *comname; 1564*69200Smckusick { 1565*69200Smckusick register char *p; 1566*69200Smckusick 1567*69200Smckusick if (comname == 0) 1568*69200Smckusick error_no_arg ("name of command to define"); 1569*69200Smckusick 1570*69200Smckusick p = comname; 1571*69200Smckusick while (*p) 1572*69200Smckusick { 1573*69200Smckusick if (!isalnum(*p) && *p != '-') 1574*69200Smckusick error ("Junk in argument list: \"%s\"", p); 1575*69200Smckusick p++; 1576*69200Smckusick } 1577*69200Smckusick } 1578*69200Smckusick 1579*69200Smckusick /* This is just a placeholder in the command data structures. */ 1580*69200Smckusick static void 1581*69200Smckusick user_defined_command (ignore, from_tty) 1582*69200Smckusick char *ignore; 1583*69200Smckusick int from_tty; 1584*69200Smckusick { 1585*69200Smckusick } 1586*69200Smckusick 1587*69200Smckusick static void 1588*69200Smckusick define_command (comname, from_tty) 1589*69200Smckusick char *comname; 1590*69200Smckusick int from_tty; 1591*69200Smckusick { 1592*69200Smckusick register struct command_line *cmds; 1593*69200Smckusick register struct cmd_list_element *c, *newc, *hookc = 0; 1594*69200Smckusick char *tem = comname; 1595*69200Smckusick #define HOOK_STRING "hook-" 1596*69200Smckusick #define HOOK_LEN 5 1597*69200Smckusick 1598*69200Smckusick validate_comname (comname); 1599*69200Smckusick 1600*69200Smckusick /* Look it up, and verify that we got an exact match. */ 1601*69200Smckusick c = lookup_cmd (&tem, cmdlist, "", -1, 1); 1602*69200Smckusick if (c && 0 != strcmp (comname, c->name)) 1603*69200Smckusick c = 0; 1604*69200Smckusick 1605*69200Smckusick if (c) 1606*69200Smckusick { 1607*69200Smckusick if (c->class == class_user || c->class == class_alias) 1608*69200Smckusick tem = "Redefine command \"%s\"? "; 1609*69200Smckusick else 1610*69200Smckusick tem = "Really redefine built-in command \"%s\"? "; 1611*69200Smckusick if (!query (tem, c->name)) 1612*69200Smckusick error ("Command \"%s\" not redefined.", c->name); 1613*69200Smckusick } 1614*69200Smckusick 1615*69200Smckusick /* If this new command is a hook, then mark the command which it 1616*69200Smckusick is hooking. Note that we allow hooking `help' commands, so that 1617*69200Smckusick we can hook the `stop' pseudo-command. */ 1618*69200Smckusick 1619*69200Smckusick if (!strncmp (comname, HOOK_STRING, HOOK_LEN)) 1620*69200Smckusick { 1621*69200Smckusick /* Look up cmd it hooks, and verify that we got an exact match. */ 1622*69200Smckusick tem = comname+HOOK_LEN; 1623*69200Smckusick hookc = lookup_cmd (&tem, cmdlist, "", -1, 0); 1624*69200Smckusick if (hookc && 0 != strcmp (comname+HOOK_LEN, hookc->name)) 1625*69200Smckusick hookc = 0; 1626*69200Smckusick if (!hookc) 1627*69200Smckusick { 1628*69200Smckusick warning ("Your new `%s' command does not hook any existing command.", 1629*69200Smckusick comname); 1630*69200Smckusick if (!query ("Proceed? ", (char *)0)) 1631*69200Smckusick error ("Not confirmed."); 1632*69200Smckusick } 1633*69200Smckusick } 1634*69200Smckusick 1635*69200Smckusick comname = savestring (comname, strlen (comname)); 1636*69200Smckusick 1637*69200Smckusick /* If the rest of the commands will be case insensitive, this one 1638*69200Smckusick should behave in the same manner. */ 1639*69200Smckusick for (tem = comname; *tem; tem++) 1640*69200Smckusick if (isupper(*tem)) *tem = tolower(*tem); 1641*69200Smckusick 1642*69200Smckusick if (from_tty) 1643*69200Smckusick { 1644*69200Smckusick printf ("Type commands for definition of \"%s\".\n\ 1645*69200Smckusick End with a line saying just \"end\".\n", comname); 1646*69200Smckusick fflush (stdout); 1647*69200Smckusick } 1648*69200Smckusick 1649*69200Smckusick cmds = read_command_lines (from_tty); 1650*69200Smckusick 1651*69200Smckusick if (c && c->class == class_user) 1652*69200Smckusick free_command_lines (c->user_commands); 1653*69200Smckusick 1654*69200Smckusick newc = add_cmd (comname, class_user, user_defined_command, 1655*69200Smckusick (c && c->class == class_user) 1656*69200Smckusick ? c->doc : savestring ("User-defined.", 13), &cmdlist); 1657*69200Smckusick newc->user_commands = cmds; 1658*69200Smckusick 1659*69200Smckusick /* If this new command is a hook, then mark both commands as being 1660*69200Smckusick tied. */ 1661*69200Smckusick if (hookc) 1662*69200Smckusick { 1663*69200Smckusick hookc->hook = newc; /* Target gets hooked. */ 1664*69200Smckusick newc->hookee = hookc; /* We are marked as hooking target cmd. */ 1665*69200Smckusick } 1666*69200Smckusick } 1667*69200Smckusick 1668*69200Smckusick static void 1669*69200Smckusick document_command (comname, from_tty) 1670*69200Smckusick char *comname; 1671*69200Smckusick int from_tty; 1672*69200Smckusick { 1673*69200Smckusick struct command_line *doclines; 1674*69200Smckusick register struct cmd_list_element *c; 1675*69200Smckusick char *tem = comname; 1676*69200Smckusick 1677*69200Smckusick validate_comname (comname); 1678*69200Smckusick 1679*69200Smckusick c = lookup_cmd (&tem, cmdlist, "", 0, 1); 1680*69200Smckusick 1681*69200Smckusick if (c->class != class_user) 1682*69200Smckusick error ("Command \"%s\" is built-in.", comname); 1683*69200Smckusick 1684*69200Smckusick if (from_tty) 1685*69200Smckusick printf ("Type documentation for \"%s\".\n\ 1686*69200Smckusick End with a line saying just \"end\".\n", comname); 1687*69200Smckusick 1688*69200Smckusick doclines = read_command_lines (from_tty); 1689*69200Smckusick 1690*69200Smckusick if (c->doc) free (c->doc); 1691*69200Smckusick 1692*69200Smckusick { 1693*69200Smckusick register struct command_line *cl1; 1694*69200Smckusick register int len = 0; 1695*69200Smckusick 1696*69200Smckusick for (cl1 = doclines; cl1; cl1 = cl1->next) 1697*69200Smckusick len += strlen (cl1->line) + 1; 1698*69200Smckusick 1699*69200Smckusick c->doc = (char *) xmalloc (len + 1); 1700*69200Smckusick *c->doc = 0; 1701*69200Smckusick 1702*69200Smckusick for (cl1 = doclines; cl1; cl1 = cl1->next) 1703*69200Smckusick { 1704*69200Smckusick strcat (c->doc, cl1->line); 1705*69200Smckusick if (cl1->next) 1706*69200Smckusick strcat (c->doc, "\n"); 1707*69200Smckusick } 1708*69200Smckusick } 1709*69200Smckusick 1710*69200Smckusick free_command_lines (doclines); 1711*69200Smckusick } 1712*69200Smckusick 1713*69200Smckusick static void 1714*69200Smckusick print_gnu_advertisement() 1715*69200Smckusick { 1716*69200Smckusick printf ("\ 1717*69200Smckusick GDB is free software and you are welcome to distribute copies of it\n\ 1718*69200Smckusick under certain conditions; type \"show copying\" to see the conditions.\n\ 1719*69200Smckusick There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\ 1720*69200Smckusick "); 1721*69200Smckusick } 1722*69200Smckusick 1723*69200Smckusick static void 1724*69200Smckusick print_gdb_version () 1725*69200Smckusick { 1726*69200Smckusick printf_filtered ("\ 1727*69200Smckusick GDB %s, Copyright 1992 Free Software Foundation, Inc.", 1728*69200Smckusick version); 1729*69200Smckusick } 1730*69200Smckusick 1731*69200Smckusick /* ARGSUSED */ 1732*69200Smckusick static void 1733*69200Smckusick show_version (args, from_tty) 1734*69200Smckusick char *args; 1735*69200Smckusick int from_tty; 1736*69200Smckusick { 1737*69200Smckusick immediate_quit++; 1738*69200Smckusick print_gnu_advertisement (); 1739*69200Smckusick print_gdb_version (); 1740*69200Smckusick printf_filtered ("\n"); 1741*69200Smckusick immediate_quit--; 1742*69200Smckusick } 1743*69200Smckusick 1744*69200Smckusick /* xgdb calls this to reprint the usual GDB prompt. */ 1745*69200Smckusick 1746*69200Smckusick void 1747*69200Smckusick print_prompt () 1748*69200Smckusick { 1749*69200Smckusick printf ("%s", masterprompt); 1750*69200Smckusick fflush (stdout); 1751*69200Smckusick } 1752*69200Smckusick 1753*69200Smckusick #ifdef HAVE_TERMIO 1754*69200Smckusick #include <termio.h> 1755*69200Smckusick static struct termio norm_tty; 1756*69200Smckusick 1757*69200Smckusick init_term(tty) 1758*69200Smckusick { 1759*69200Smckusick ioctl(tty, TCGETA, &norm_tty); 1760*69200Smckusick } 1761*69200Smckusick 1762*69200Smckusick static void 1763*69200Smckusick suspend_sig() 1764*69200Smckusick { 1765*69200Smckusick int tty = fileno(stdin); 1766*69200Smckusick struct termio cur_tty; 1767*69200Smckusick 1768*69200Smckusick ioctl(tty, TCGETA, &cur_tty); 1769*69200Smckusick ioctl(tty, TCSETAW, &norm_tty); 1770*69200Smckusick 1771*69200Smckusick (void) sigsetmask(0); 1772*69200Smckusick signal(SIGTSTP, SIG_DFL); 1773*69200Smckusick kill(0, SIGTSTP); 1774*69200Smckusick 1775*69200Smckusick /* 1776*69200Smckusick * we've just been resumed -- current tty params become new 1777*69200Smckusick * 'normal' params (in case tset/stty was done while we were 1778*69200Smckusick * suspended). Merge values that readline might have changed 1779*69200Smckusick * into new params, then restore term mode. 1780*69200Smckusick */ 1781*69200Smckusick ioctl(tty, TCGETA, &norm_tty); 1782*69200Smckusick cur_tty.c_lflag = (cur_tty.c_lflag & (ICANON|ECHO|ISIG)) | 1783*69200Smckusick (norm_tty.c_lflag &~ (ICANON|ECHO|ISIG)); 1784*69200Smckusick cur_tty.c_iflag = (cur_tty.c_iflag & (IXON|ISTRIP|INPCK)) | 1785*69200Smckusick (norm_tty.c_iflag &~ (IXON|ISTRIP|INPCK)); 1786*69200Smckusick ioctl(tty, TCSETAW, &cur_tty); 1787*69200Smckusick 1788*69200Smckusick signal(SIGTSTP, suspend_sig); 1789*69200Smckusick print_prompt(); 1790*69200Smckusick 1791*69200Smckusick /* 1792*69200Smckusick * Forget about any previous command -- null line now will do 1793*69200Smckusick * nothing. 1794*69200Smckusick */ 1795*69200Smckusick dont_repeat(); 1796*69200Smckusick } 1797*69200Smckusick 1798*69200Smckusick #else 1799*69200Smckusick 1800*69200Smckusick #include <fcntl.h> 1801*69200Smckusick #include <sgtty.h> 1802*69200Smckusick #include <sys/ioctl.h> /* XXX BSD: must follow sgtty.h for compat to kick in */ 1803*69200Smckusick 1804*69200Smckusick static struct sgttyb norm_tty; 1805*69200Smckusick static struct tchars norm_tchars; 1806*69200Smckusick static struct ltchars norm_ltchars; 1807*69200Smckusick static int norm_lflags; 1808*69200Smckusick 1809*69200Smckusick #ifdef PASS8 1810*69200Smckusick #define RL_TFLAGS (RAW|CRMOD|ECHO|CBREAK|PASS8) 1811*69200Smckusick #else 1812*69200Smckusick #define RL_TFLAGS (RAW|CRMOD|ECHO|CBREAK) 1813*69200Smckusick #endif 1814*69200Smckusick 1815*69200Smckusick init_term(tty) 1816*69200Smckusick int tty; 1817*69200Smckusick { 1818*69200Smckusick ioctl(tty, TIOCGETP, &norm_tty); 1819*69200Smckusick ioctl(tty, TIOCLGET, &norm_lflags); 1820*69200Smckusick ioctl(tty, TIOCGETC, &norm_tchars); 1821*69200Smckusick ioctl(tty, TIOCGLTC, &norm_ltchars); 1822*69200Smckusick } 1823*69200Smckusick 1824*69200Smckusick static void 1825*69200Smckusick suspend_sig() 1826*69200Smckusick { 1827*69200Smckusick int tty = fileno(stdin); 1828*69200Smckusick struct sgttyb cur_tty; 1829*69200Smckusick struct tchars cur_tchars; 1830*69200Smckusick struct ltchars cur_ltchars; 1831*69200Smckusick int cur_lflags; 1832*69200Smckusick int cur_flags; 1833*69200Smckusick 1834*69200Smckusick ioctl(tty, TIOCGETP, &cur_tty); 1835*69200Smckusick ioctl(tty, TIOCGETC, &cur_tchars); 1836*69200Smckusick ioctl(tty, TIOCLGET, &cur_lflags); 1837*69200Smckusick ioctl(tty, TIOCGLTC, &cur_ltchars); 1838*69200Smckusick 1839*69200Smckusick ioctl(tty, TIOCSETP, &norm_tty); 1840*69200Smckusick ioctl(tty, TIOCSETC, &norm_tchars); 1841*69200Smckusick ioctl(tty, TIOCLSET, &norm_lflags); 1842*69200Smckusick ioctl(tty, TIOCSLTC, &norm_ltchars); 1843*69200Smckusick 1844*69200Smckusick (void) sigsetmask(0); 1845*69200Smckusick signal(SIGTSTP, SIG_DFL); 1846*69200Smckusick kill(0, SIGTSTP); 1847*69200Smckusick 1848*69200Smckusick /* 1849*69200Smckusick * we've just been resumed -- current tty params become new 1850*69200Smckusick * 'normal' params (in case tset/stty was done while we were 1851*69200Smckusick * suspended). Merge values that readline might have changed 1852*69200Smckusick * into new params, then restore term mode. 1853*69200Smckusick */ 1854*69200Smckusick ioctl(tty, TIOCGETP, &norm_tty); 1855*69200Smckusick cur_flags = cur_tty.sg_flags; 1856*69200Smckusick cur_tty = norm_tty; 1857*69200Smckusick cur_tty.sg_flags = (cur_tty.sg_flags &~ RL_TFLAGS) 1858*69200Smckusick | (cur_flags & RL_TFLAGS); 1859*69200Smckusick 1860*69200Smckusick ioctl(tty, TIOCLGET, &norm_lflags); 1861*69200Smckusick #ifdef LPASS8 1862*69200Smckusick cur_lflags = (cur_lflags &~ LPASS8) | (cur_flags & LPASS8); 1863*69200Smckusick #endif 1864*69200Smckusick ioctl(tty, TIOCGETC, &norm_tchars); 1865*69200Smckusick ioctl(tty, TIOCGLTC, &norm_ltchars); 1866*69200Smckusick 1867*69200Smckusick ioctl(tty, TIOCSETP, &cur_tty); 1868*69200Smckusick ioctl(tty, TIOCSETC, &cur_tchars); 1869*69200Smckusick ioctl(tty, TIOCLSET, &cur_lflags); 1870*69200Smckusick ioctl(tty, TIOCSLTC, &cur_ltchars); 1871*69200Smckusick 1872*69200Smckusick signal(SIGTSTP, suspend_sig); 1873*69200Smckusick print_prompt(); 1874*69200Smckusick 1875*69200Smckusick /* 1876*69200Smckusick * Forget about any previous command -- null line now will do 1877*69200Smckusick * nothing. 1878*69200Smckusick */ 1879*69200Smckusick dont_repeat(); 1880*69200Smckusick } 1881*69200Smckusick #endif /* HAVE_TERMIO */ 1882*69200Smckusick 1883*69200Smckusick static void 1884*69200Smckusick quit_command (args, from_tty) 1885*69200Smckusick char *args; 1886*69200Smckusick int from_tty; 1887*69200Smckusick { 1888*69200Smckusick /* 1889*69200Smckusick * Don't bother checking if the inferior_pid is 0 because the remote 1890*69200Smckusick * module doesn't muck with it (for pid 0, target_kill should be a nop 1891*69200Smckusick * in the ptrace case anyway -- moreover, inferior_pid should be private 1892*69200Smckusick * to infptrace.c) 1893*69200Smckusick */ 1894*69200Smckusick if (target_has_execution) 1895*69200Smckusick { 1896*69200Smckusick if (inhibit_confirm || query ("The program is running. Quit anyway? ")) 1897*69200Smckusick { 1898*69200Smckusick if (attach_flag) 1899*69200Smckusick target_detach (args, from_tty); 1900*69200Smckusick else 1901*69200Smckusick target_kill (); 1902*69200Smckusick } 1903*69200Smckusick else 1904*69200Smckusick error ("Not confirmed."); 1905*69200Smckusick } 1906*69200Smckusick /* Save the history information if it is appropriate to do so. */ 1907*69200Smckusick if (write_history_p && history_filename) 1908*69200Smckusick write_history (history_filename); 1909*69200Smckusick exit (0); 1910*69200Smckusick } 1911*69200Smckusick 1912*69200Smckusick /* Returns whether GDB is running on a terminal and whether the user 1913*69200Smckusick desires that questions be asked of them on that terminal. */ 1914*69200Smckusick 1915*69200Smckusick int 1916*69200Smckusick input_from_terminal_p () 1917*69200Smckusick { 1918*69200Smckusick return gdb_has_a_terminal && (instream == stdin) & caution; 1919*69200Smckusick } 1920*69200Smckusick 1921*69200Smckusick /* ARGSUSED */ 1922*69200Smckusick static void 1923*69200Smckusick pwd_command (args, from_tty) 1924*69200Smckusick char *args; 1925*69200Smckusick int from_tty; 1926*69200Smckusick { 1927*69200Smckusick if (args) error ("The \"pwd\" command does not take an argument: %s", args); 1928*69200Smckusick getcwd (dirbuf, sizeof (dirbuf)); 1929*69200Smckusick 1930*69200Smckusick if (strcmp (dirbuf, current_directory)) 1931*69200Smckusick printf ("Working directory %s\n (canonically %s).\n", 1932*69200Smckusick current_directory, dirbuf); 1933*69200Smckusick else 1934*69200Smckusick printf ("Working directory %s.\n", current_directory); 1935*69200Smckusick } 1936*69200Smckusick 1937*69200Smckusick static void 1938*69200Smckusick cd_command (dir, from_tty) 1939*69200Smckusick char *dir; 1940*69200Smckusick int from_tty; 1941*69200Smckusick { 1942*69200Smckusick int len; 1943*69200Smckusick int change; 1944*69200Smckusick 1945*69200Smckusick /* If the new directory is absolute, repeat is a no-op; if relative, 1946*69200Smckusick repeat might be useful but is more likely to be a mistake. */ 1947*69200Smckusick dont_repeat (); 1948*69200Smckusick 1949*69200Smckusick if (dir == 0) 1950*69200Smckusick error_no_arg ("new working directory"); 1951*69200Smckusick 1952*69200Smckusick dir = tilde_expand (dir); 1953*69200Smckusick make_cleanup (free, dir); 1954*69200Smckusick 1955*69200Smckusick if (chdir (dir) < 0) 1956*69200Smckusick perror_with_name (dir); 1957*69200Smckusick 1958*69200Smckusick len = strlen (dir); 1959*69200Smckusick dir = savestring (dir, len - (len > 1 && dir[len-1] == '/')); 1960*69200Smckusick if (dir[0] == '/') 1961*69200Smckusick current_directory = dir; 1962*69200Smckusick else 1963*69200Smckusick { 1964*69200Smckusick current_directory = concat (current_directory, "/", dir, NULL); 1965*69200Smckusick free (dir); 1966*69200Smckusick } 1967*69200Smckusick 1968*69200Smckusick /* Now simplify any occurrences of `.' and `..' in the pathname. */ 1969*69200Smckusick 1970*69200Smckusick change = 1; 1971*69200Smckusick while (change) 1972*69200Smckusick { 1973*69200Smckusick char *p; 1974*69200Smckusick change = 0; 1975*69200Smckusick 1976*69200Smckusick for (p = current_directory; *p;) 1977*69200Smckusick { 1978*69200Smckusick if (!strncmp (p, "/./", 2) 1979*69200Smckusick && (p[2] == 0 || p[2] == '/')) 1980*69200Smckusick strcpy (p, p + 2); 1981*69200Smckusick else if (!strncmp (p, "/..", 3) 1982*69200Smckusick && (p[3] == 0 || p[3] == '/') 1983*69200Smckusick && p != current_directory) 1984*69200Smckusick { 1985*69200Smckusick char *q = p; 1986*69200Smckusick while (q != current_directory && q[-1] != '/') q--; 1987*69200Smckusick if (q != current_directory) 1988*69200Smckusick { 1989*69200Smckusick strcpy (q-1, p+3); 1990*69200Smckusick p = q-1; 1991*69200Smckusick } 1992*69200Smckusick } 1993*69200Smckusick else p++; 1994*69200Smckusick } 1995*69200Smckusick } 1996*69200Smckusick 1997*69200Smckusick forget_cached_source_info (); 1998*69200Smckusick 1999*69200Smckusick if (from_tty) 2000*69200Smckusick pwd_command ((char *) 0, 1); 2001*69200Smckusick } 2002*69200Smckusick 2003*69200Smckusick /* ARGSUSED */ 2004*69200Smckusick static void 2005*69200Smckusick source_command (args, from_tty) 2006*69200Smckusick char *args; 2007*69200Smckusick int from_tty; 2008*69200Smckusick { 2009*69200Smckusick FILE *stream; 2010*69200Smckusick struct cleanup *cleanups; 2011*69200Smckusick char *file = args; 2012*69200Smckusick 2013*69200Smckusick if (file == 0) 2014*69200Smckusick /* Let source without arguments read .gdbinit. */ 2015*69200Smckusick file = gdbinit; 2016*69200Smckusick 2017*69200Smckusick file = tilde_expand (file); 2018*69200Smckusick make_cleanup (free, file); 2019*69200Smckusick stream = fopen (file, "r"); 2020*69200Smckusick if (stream == 0) { 2021*69200Smckusick char *cp = xmalloc(strlen(file) + 5); 2022*69200Smckusick 2023*69200Smckusick sprintf(cp, "%s.gdb", file); 2024*69200Smckusick make_cleanup (free, cp); 2025*69200Smckusick stream = fopen(cp, "r"); 2026*69200Smckusick if (stream == 0) 2027*69200Smckusick perror_with_name (file); 2028*69200Smckusick } 2029*69200Smckusick 2030*69200Smckusick cleanups = make_cleanup (fclose, stream); 2031*69200Smckusick 2032*69200Smckusick read_command_file (stream); 2033*69200Smckusick 2034*69200Smckusick do_cleanups (cleanups); 2035*69200Smckusick } 2036*69200Smckusick 2037*69200Smckusick /* ARGSUSED */ 2038*69200Smckusick static void 2039*69200Smckusick echo_command (text, from_tty) 2040*69200Smckusick char *text; 2041*69200Smckusick int from_tty; 2042*69200Smckusick { 2043*69200Smckusick char *p = text; 2044*69200Smckusick register int c; 2045*69200Smckusick 2046*69200Smckusick if (text) 2047*69200Smckusick while (c = *p++) 2048*69200Smckusick { 2049*69200Smckusick if (c == '\\') 2050*69200Smckusick { 2051*69200Smckusick /* \ at end of argument is used after spaces 2052*69200Smckusick so they won't be lost. */ 2053*69200Smckusick if (*p == 0) 2054*69200Smckusick return; 2055*69200Smckusick 2056*69200Smckusick c = parse_escape (&p); 2057*69200Smckusick if (c >= 0) 2058*69200Smckusick printf_filtered ("%c", c); 2059*69200Smckusick } 2060*69200Smckusick else 2061*69200Smckusick printf_filtered ("%c", c); 2062*69200Smckusick } 2063*69200Smckusick 2064*69200Smckusick /* Force this output to appear now. */ 2065*69200Smckusick wrap_here (""); 2066*69200Smckusick fflush (stdout); 2067*69200Smckusick } 2068*69200Smckusick 2069*69200Smckusick 2070*69200Smckusick /* Functions to manipulate command line editing control variables. */ 2071*69200Smckusick 2072*69200Smckusick /* Number of commands to print in each call to show_commands. */ 2073*69200Smckusick #define Hist_print 10 2074*69200Smckusick static void 2075*69200Smckusick show_commands (args, from_tty) 2076*69200Smckusick char *args; 2077*69200Smckusick int from_tty; 2078*69200Smckusick { 2079*69200Smckusick /* Index for history commands. Relative to history_base. */ 2080*69200Smckusick int offset; 2081*69200Smckusick 2082*69200Smckusick /* Number of the history entry which we are planning to display next. 2083*69200Smckusick Relative to history_base. */ 2084*69200Smckusick static int num = 0; 2085*69200Smckusick 2086*69200Smckusick /* The first command in the history which doesn't exist (i.e. one more 2087*69200Smckusick than the number of the last command). Relative to history_base. */ 2088*69200Smckusick int hist_len; 2089*69200Smckusick 2090*69200Smckusick extern struct _hist_entry *history_get PARAMS ((int)); 2091*69200Smckusick extern int history_base; 2092*69200Smckusick 2093*69200Smckusick /* Print out some of the commands from the command history. */ 2094*69200Smckusick /* First determine the length of the history list. */ 2095*69200Smckusick hist_len = history_size; 2096*69200Smckusick for (offset = 0; offset < history_size; offset++) 2097*69200Smckusick { 2098*69200Smckusick if (!history_get (history_base + offset)) 2099*69200Smckusick { 2100*69200Smckusick hist_len = offset; 2101*69200Smckusick break; 2102*69200Smckusick } 2103*69200Smckusick } 2104*69200Smckusick 2105*69200Smckusick if (args) 2106*69200Smckusick { 2107*69200Smckusick if (args[0] == '+' && args[1] == '\0') 2108*69200Smckusick /* "info editing +" should print from the stored position. */ 2109*69200Smckusick ; 2110*69200Smckusick else 2111*69200Smckusick /* "info editing <exp>" should print around command number <exp>. */ 2112*69200Smckusick num = (parse_and_eval_address (args) - history_base) - Hist_print / 2; 2113*69200Smckusick } 2114*69200Smckusick /* "show commands" means print the last Hist_print commands. */ 2115*69200Smckusick else 2116*69200Smckusick { 2117*69200Smckusick num = hist_len - Hist_print; 2118*69200Smckusick } 2119*69200Smckusick 2120*69200Smckusick if (num < 0) 2121*69200Smckusick num = 0; 2122*69200Smckusick 2123*69200Smckusick /* If there are at least Hist_print commands, we want to display the last 2124*69200Smckusick Hist_print rather than, say, the last 6. */ 2125*69200Smckusick if (hist_len - num < Hist_print) 2126*69200Smckusick { 2127*69200Smckusick num = hist_len - Hist_print; 2128*69200Smckusick if (num < 0) 2129*69200Smckusick num = 0; 2130*69200Smckusick } 2131*69200Smckusick 2132*69200Smckusick for (offset = num; offset < num + Hist_print && offset < hist_len; offset++) 2133*69200Smckusick { 2134*69200Smckusick printf_filtered ("%5d %s\n", history_base + offset, 2135*69200Smckusick (history_get (history_base + offset))->line); 2136*69200Smckusick } 2137*69200Smckusick 2138*69200Smckusick /* The next command we want to display is the next one that we haven't 2139*69200Smckusick displayed yet. */ 2140*69200Smckusick num += Hist_print; 2141*69200Smckusick 2142*69200Smckusick /* If the user repeats this command with return, it should do what 2143*69200Smckusick "show commands +" does. This is unnecessary if arg is null, 2144*69200Smckusick because "show commands +" is not useful after "show commands". */ 2145*69200Smckusick if (from_tty && args) 2146*69200Smckusick { 2147*69200Smckusick args[0] = '+'; 2148*69200Smckusick args[1] = '\0'; 2149*69200Smckusick } 2150*69200Smckusick } 2151*69200Smckusick 2152*69200Smckusick /* Called by do_setshow_command. */ 2153*69200Smckusick /* ARGSUSED */ 2154*69200Smckusick static void 2155*69200Smckusick set_history_size_command (args, from_tty, c) 2156*69200Smckusick char *args; 2157*69200Smckusick int from_tty; 2158*69200Smckusick struct cmd_list_element *c; 2159*69200Smckusick { 2160*69200Smckusick if (history_size == UINT_MAX) 2161*69200Smckusick unstifle_history (); 2162*69200Smckusick else if (history_size >= 0) 2163*69200Smckusick stifle_history (history_size); 2164*69200Smckusick else 2165*69200Smckusick { 2166*69200Smckusick history_size = UINT_MAX; 2167*69200Smckusick error ("History size must be non-negative"); 2168*69200Smckusick } 2169*69200Smckusick } 2170*69200Smckusick 2171*69200Smckusick /* ARGSUSED */ 2172*69200Smckusick static void 2173*69200Smckusick set_history (args, from_tty) 2174*69200Smckusick char *args; 2175*69200Smckusick int from_tty; 2176*69200Smckusick { 2177*69200Smckusick printf ("\"set history\" must be followed by the name of a history subcommand.\n"); 2178*69200Smckusick help_list (sethistlist, "set history ", -1, stdout); 2179*69200Smckusick } 2180*69200Smckusick 2181*69200Smckusick /* ARGSUSED */ 2182*69200Smckusick static void 2183*69200Smckusick show_history (args, from_tty) 2184*69200Smckusick char *args; 2185*69200Smckusick int from_tty; 2186*69200Smckusick { 2187*69200Smckusick cmd_show_list (showhistlist, from_tty, ""); 2188*69200Smckusick } 2189*69200Smckusick 2190*69200Smckusick int info_verbose = 0; /* Default verbose msgs off */ 2191*69200Smckusick 2192*69200Smckusick /* Called by do_setshow_command. An elaborate joke. */ 2193*69200Smckusick /* ARGSUSED */ 2194*69200Smckusick static void 2195*69200Smckusick set_verbose (args, from_tty, c) 2196*69200Smckusick char *args; 2197*69200Smckusick int from_tty; 2198*69200Smckusick struct cmd_list_element *c; 2199*69200Smckusick { 2200*69200Smckusick char *cmdname = "verbose"; 2201*69200Smckusick struct cmd_list_element *showcmd; 2202*69200Smckusick 2203*69200Smckusick showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1); 2204*69200Smckusick 2205*69200Smckusick if (info_verbose) 2206*69200Smckusick { 2207*69200Smckusick c->doc = "Set verbose printing of informational messages."; 2208*69200Smckusick showcmd->doc = "Show verbose printing of informational messages."; 2209*69200Smckusick } 2210*69200Smckusick else 2211*69200Smckusick { 2212*69200Smckusick c->doc = "Set verbosity."; 2213*69200Smckusick showcmd->doc = "Show verbosity."; 2214*69200Smckusick } 2215*69200Smckusick } 2216*69200Smckusick 2217*69200Smckusick static void 2218*69200Smckusick float_handler (signo) 2219*69200Smckusick int signo; 2220*69200Smckusick { 2221*69200Smckusick /* This message is based on ANSI C, section 4.7. Note that integer 2222*69200Smckusick divide by zero causes this, so "float" is a misnomer. */ 2223*69200Smckusick error ("Erroneous arithmetic operation."); 2224*69200Smckusick } 2225*69200Smckusick 2226*69200Smckusick /* Return whether we are running a batch file or from terminal. */ 2227*69200Smckusick int 2228*69200Smckusick batch_mode () 2229*69200Smckusick { 2230*69200Smckusick return !(instream == stdin && ISATTY (stdin)); 2231*69200Smckusick } 2232*69200Smckusick 2233*69200Smckusick 2234*69200Smckusick static void 2235*69200Smckusick initialize_cmd_lists () 2236*69200Smckusick { 2237*69200Smckusick cmdlist = NULL; 2238*69200Smckusick infolist = NULL; 2239*69200Smckusick enablelist = NULL; 2240*69200Smckusick disablelist = NULL; 2241*69200Smckusick deletelist = NULL; 2242*69200Smckusick enablebreaklist = NULL; 2243*69200Smckusick setlist = NULL; 2244*69200Smckusick unsetlist = NULL; 2245*69200Smckusick showlist = NULL; 2246*69200Smckusick sethistlist = NULL; 2247*69200Smckusick showhistlist = NULL; 2248*69200Smckusick unsethistlist = NULL; 2249*69200Smckusick #if MAINTENANCE_CMDS 2250*69200Smckusick maintenancelist = NULL; 2251*69200Smckusick maintenanceinfolist = NULL; 2252*69200Smckusick maintenanceprintlist = NULL; 2253*69200Smckusick #endif 2254*69200Smckusick setprintlist = NULL; 2255*69200Smckusick showprintlist = NULL; 2256*69200Smckusick setchecklist = NULL; 2257*69200Smckusick showchecklist = NULL; 2258*69200Smckusick } 2259*69200Smckusick 2260*69200Smckusick /* Init the history buffer. Note that we are called after the init file(s) 2261*69200Smckusick * have been read so that the user can change the history file via his 2262*69200Smckusick * .gdbinit file (for instance). The GDBHISTFILE environment variable 2263*69200Smckusick * overrides all of this. 2264*69200Smckusick */ 2265*69200Smckusick 2266*69200Smckusick static void 2267*69200Smckusick initialize_history() 2268*69200Smckusick { 2269*69200Smckusick char *tmpenv; 2270*69200Smckusick 2271*69200Smckusick tmpenv = getenv ("HISTSIZE"); 2272*69200Smckusick if (tmpenv) 2273*69200Smckusick history_size = atoi (tmpenv); 2274*69200Smckusick else if (!history_size) 2275*69200Smckusick history_size = 256; 2276*69200Smckusick 2277*69200Smckusick stifle_history (history_size); 2278*69200Smckusick 2279*69200Smckusick tmpenv = getenv ("GDBHISTFILE"); 2280*69200Smckusick if (tmpenv) 2281*69200Smckusick history_filename = savestring (tmpenv, strlen(tmpenv)); 2282*69200Smckusick else if (!history_filename) { 2283*69200Smckusick /* We include the current directory so that if the user changes 2284*69200Smckusick directories the file written will be the same as the one 2285*69200Smckusick that was read. */ 2286*69200Smckusick history_filename = concat (current_directory, "/.gdb_history", NULL); 2287*69200Smckusick } 2288*69200Smckusick read_history (history_filename); 2289*69200Smckusick } 2290*69200Smckusick 2291*69200Smckusick void 2292*69200Smckusick set_prompt(s) 2293*69200Smckusick char *s; 2294*69200Smckusick { 2295*69200Smckusick if (masterprompt != 0) 2296*69200Smckusick free (masterprompt); 2297*69200Smckusick masterprompt = strsave (s); 2298*69200Smckusick } 2299*69200Smckusick 2300*69200Smckusick static void 2301*69200Smckusick initialize_main () 2302*69200Smckusick { 2303*69200Smckusick struct cmd_list_element *c; 2304*69200Smckusick 2305*69200Smckusick #ifdef DEFAULT_PROMPT 2306*69200Smckusick set_prompt(DEFAULT_PROMPT); 2307*69200Smckusick #else 2308*69200Smckusick #ifdef KERNELDEBUG 2309*69200Smckusick if (kernel_debugging) set_prompt ("(kgdb) "); else 2310*69200Smckusick #endif 2311*69200Smckusick set_prompt("(gdb) "); 2312*69200Smckusick #endif 2313*69200Smckusick 2314*69200Smckusick /* Set the important stuff up for command editing. */ 2315*69200Smckusick write_history_p = 0; 2316*69200Smckusick 2317*69200Smckusick /* Setup important stuff for command line editing. */ 2318*69200Smckusick rl_completion_entry_function = (int (*)()) symbol_completion_function; 2319*69200Smckusick rl_completer_word_break_characters = gdb_completer_word_break_characters; 2320*69200Smckusick rl_completer_quote_characters = gdb_completer_quote_characters; 2321*69200Smckusick rl_readline_name = "gdb"; 2322*69200Smckusick 2323*69200Smckusick /* Define the classes of commands. 2324*69200Smckusick They will appear in the help list in the reverse of this order. */ 2325*69200Smckusick 2326*69200Smckusick add_cmd ("internals", class_maintenance, NO_FUNCTION, 2327*69200Smckusick "Maintenance commands.\n\ 2328*69200Smckusick Some gdb commands are provided just for use by gdb maintainers.\n\ 2329*69200Smckusick These commands are subject to frequent change, and may not be as\n\ 2330*69200Smckusick well documented as user commands.", 2331*69200Smckusick &cmdlist); 2332*69200Smckusick add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist); 2333*69200Smckusick add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist); 2334*69200Smckusick add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\ 2335*69200Smckusick The commands in this class are those defined by the user.\n\ 2336*69200Smckusick Use the \"define\" command to define a command.", &cmdlist); 2337*69200Smckusick add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist); 2338*69200Smckusick add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist); 2339*69200Smckusick add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist); 2340*69200Smckusick add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist); 2341*69200Smckusick add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist); 2342*69200Smckusick add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\ 2343*69200Smckusick The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\ 2344*69200Smckusick counting from zero for the innermost (currently executing) frame.\n\n\ 2345*69200Smckusick At any time gdb identifies one frame as the \"selected\" frame.\n\ 2346*69200Smckusick Variable lookups are done with respect to the selected frame.\n\ 2347*69200Smckusick When the program being debugged stops, gdb selects the innermost frame.\n\ 2348*69200Smckusick The commands below can be used to select other frames by number or address.", 2349*69200Smckusick &cmdlist); 2350*69200Smckusick add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist); 2351*69200Smckusick 2352*69200Smckusick add_com ("pwd", class_files, pwd_command, 2353*69200Smckusick "Print working directory. This is used for your program as well."); 2354*69200Smckusick add_com ("cd", class_files, cd_command, 2355*69200Smckusick "Set working directory to DIR for debugger and program being debugged.\n\ 2356*69200Smckusick The change does not take effect for the program being debugged\n\ 2357*69200Smckusick until the next time it is started."); 2358*69200Smckusick 2359*69200Smckusick add_show_from_set 2360*69200Smckusick (add_set_cmd ("prompt", class_support, var_string, (char *)&masterprompt, 2361*69200Smckusick "Set gdb's prompt", 2362*69200Smckusick &setlist), 2363*69200Smckusick &showlist); 2364*69200Smckusick 2365*69200Smckusick add_com ("echo", class_support, echo_command, 2366*69200Smckusick "Print a constant string. Give string as argument.\n\ 2367*69200Smckusick C escape sequences may be used in the argument.\n\ 2368*69200Smckusick No newline is added at the end of the argument;\n\ 2369*69200Smckusick use \"\\n\" if you want a newline to be printed.\n\ 2370*69200Smckusick Since leading and trailing whitespace are ignored in command arguments,\n\ 2371*69200Smckusick if you want to print some you must use \"\\\" before leading whitespace\n\ 2372*69200Smckusick to be printed or after trailing whitespace."); 2373*69200Smckusick add_com ("document", class_support, document_command, 2374*69200Smckusick "Document a user-defined command.\n\ 2375*69200Smckusick Give command name as argument. Give documentation on following lines.\n\ 2376*69200Smckusick End with a line of just \"end\"."); 2377*69200Smckusick add_com ("define", class_support, define_command, 2378*69200Smckusick "Define a new command name. Command name is argument.\n\ 2379*69200Smckusick Definition appears on following lines, one command per line.\n\ 2380*69200Smckusick End with a line of just \"end\".\n\ 2381*69200Smckusick Use the \"document\" command to give documentation for the new command.\n\ 2382*69200Smckusick Commands defined in this way do not take arguments."); 2383*69200Smckusick 2384*69200Smckusick #ifdef __STDC__ 2385*69200Smckusick add_com ("source", class_support, source_command, 2386*69200Smckusick "Read commands from a file named FILE.\n\ 2387*69200Smckusick Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\ 2388*69200Smckusick when gdb is started."); 2389*69200Smckusick #else 2390*69200Smckusick /* Punt file name, we can't help it easily. */ 2391*69200Smckusick add_com ("source", class_support, source_command, 2392*69200Smckusick "Read commands from a file named FILE.\n\ 2393*69200Smckusick Note that the file \".gdbinit\" is read automatically in this way\n\ 2394*69200Smckusick when gdb is started."); 2395*69200Smckusick #endif 2396*69200Smckusick 2397*69200Smckusick add_com ("quit", class_support, quit_command, "Exit gdb."); 2398*69200Smckusick add_com ("help", class_support, help_command, "Print list of commands."); 2399*69200Smckusick add_com_alias ("q", "quit", class_support, 1); 2400*69200Smckusick add_com_alias ("h", "help", class_support, 1); 2401*69200Smckusick 2402*69200Smckusick 2403*69200Smckusick c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose, 2404*69200Smckusick "Set ", 2405*69200Smckusick &setlist), 2406*69200Smckusick add_show_from_set (c, &showlist); 2407*69200Smckusick c->function.sfunc = set_verbose; 2408*69200Smckusick set_verbose (NULL, 0, c); 2409*69200Smckusick 2410*69200Smckusick add_prefix_cmd ("history", class_support, set_history, 2411*69200Smckusick "Generic command for setting command history parameters.", 2412*69200Smckusick &sethistlist, "set history ", 0, &setlist); 2413*69200Smckusick add_prefix_cmd ("history", class_support, show_history, 2414*69200Smckusick "Generic command for showing command history parameters.", 2415*69200Smckusick &showhistlist, "show history ", 0, &showlist); 2416*69200Smckusick 2417*69200Smckusick add_show_from_set 2418*69200Smckusick (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p, 2419*69200Smckusick "Set saving of the history record on exit.\n\ 2420*69200Smckusick Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\ 2421*69200Smckusick Without an argument, saving is enabled.", &sethistlist), 2422*69200Smckusick &showhistlist); 2423*69200Smckusick 2424*69200Smckusick c = add_set_cmd ("size", no_class, var_uinteger, (char *)&history_size, 2425*69200Smckusick "Set the size of the command history, \n\ 2426*69200Smckusick ie. the number of previous commands to keep a record of.", &sethistlist); 2427*69200Smckusick add_show_from_set (c, &showhistlist); 2428*69200Smckusick c->function.sfunc = set_history_size_command; 2429*69200Smckusick 2430*69200Smckusick add_show_from_set 2431*69200Smckusick (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename, 2432*69200Smckusick "Set the filename in which to record the command history\n\ 2433*69200Smckusick (the list of previous commands of which a record is kept).", &sethistlist), 2434*69200Smckusick &showhistlist); 2435*69200Smckusick 2436*69200Smckusick add_show_from_set 2437*69200Smckusick (add_set_cmd ("confirm", class_support, var_boolean, 2438*69200Smckusick (char *)&caution, 2439*69200Smckusick "Set whether to confirm potentially dangerous operations.", 2440*69200Smckusick &setlist), 2441*69200Smckusick &showlist); 2442*69200Smckusick 2443*69200Smckusick add_prefix_cmd ("info", class_info, info_command, 2444*69200Smckusick "Generic command for showing things about the program being debugged.", 2445*69200Smckusick &infolist, "info ", 0, &cmdlist); 2446*69200Smckusick add_com_alias ("i", "info", class_info, 1); 2447*69200Smckusick 2448*69200Smckusick add_prefix_cmd ("show", class_info, show_command, 2449*69200Smckusick "Generic command for showing things about the debugger.", 2450*69200Smckusick &showlist, "show ", 0, &cmdlist); 2451*69200Smckusick /* Another way to get at the same thing. */ 2452*69200Smckusick add_info ("set", show_command, "Show all GDB settings."); 2453*69200Smckusick 2454*69200Smckusick add_cmd ("commands", no_class, show_commands, 2455*69200Smckusick "Show the the history of commands you typed.\n\ 2456*69200Smckusick You can supply a command number to start with, or a `+' to start after\n\ 2457*69200Smckusick the previous command number shown.", 2458*69200Smckusick &showlist); 2459*69200Smckusick 2460*69200Smckusick add_cmd ("version", no_class, show_version, 2461*69200Smckusick "Show what version of GDB this is.", &showlist); 2462*69200Smckusick } 2463