home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-13  |  14.3 KB  |  546 lines

  1. /* Top level stuff for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include <setjmp.h>
  23. #include "top.h"
  24. #include "target.h"
  25. #include "inferior.h"
  26. #include "call-cmds.h"
  27.  
  28. #include "getopt.h"
  29.  
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <ctype.h>
  33.  
  34. #include <string.h>
  35. /* R_OK lives in either unistd.h or sys/file.h.  */
  36. #ifdef USG
  37. #include <unistd.h>
  38. #endif
  39. #ifndef    NO_SYS_FILE
  40. #include <sys/file.h>
  41. #endif
  42.  
  43. /* Temporary variable for SET_TOP_LEVEL.  */
  44. static int top_level_val;
  45.  
  46. /* Do a setjmp on error_return and quit_return.  catch_errors is
  47.    generally a cleaner way to do this, but main() would look pretty
  48.    ugly if it had to use catch_errors each time.  */
  49.  
  50. #define SET_TOP_LEVEL() \
  51.   (((top_level_val = setjmp (error_return)) \
  52.     ? (PTR) 0 : (PTR) memcpy (quit_return, error_return, sizeof (jmp_buf))) \
  53.    , top_level_val)
  54.  
  55. extern void gdb_init PARAMS ((void));
  56.  
  57. int
  58. main (argc, argv)
  59.      int argc;
  60.      char **argv;
  61. {
  62.   int count;
  63.   static int quiet = 0;
  64.   static int batch = 0;
  65.  
  66.   /* Pointers to various arguments from command line.  */
  67.   char *symarg = NULL;
  68.   char *execarg = NULL;
  69.   char *corearg = NULL;
  70.   char *cdarg = NULL;
  71.   char *ttyarg = NULL;
  72.  
  73.   /* These are static so that we can take their address in an initializer.  */
  74.   static int print_help;
  75.   static int print_version;
  76.  
  77.   /* Pointers to all arguments of --command option.  */
  78.   char **cmdarg;
  79.   /* Allocated size of cmdarg.  */
  80.   int cmdsize;
  81.   /* Number of elements of cmdarg used.  */
  82.   int ncmd;
  83.  
  84.   /* Indices of all arguments of --directory option.  */
  85.   char **dirarg;
  86.   /* Allocated size.  */
  87.   int dirsize;
  88.   /* Number of elements used.  */
  89.   int ndir;
  90.   
  91.   struct stat homebuf, cwdbuf;
  92.   char *homedir, *homeinit;
  93.  
  94.   register int i;
  95.  
  96.   /* This needs to happen before the first use of malloc.  */
  97.   init_malloc ((PTR) NULL);
  98.  
  99. #if defined (ALIGN_STACK_ON_STARTUP)
  100.   i = (int) &count & 0x3;
  101.   if (i != 0)
  102.     alloca (4 - i);
  103. #endif
  104.  
  105.   /* If error() is called from initialization code, just exit */
  106.   if (SET_TOP_LEVEL ()) {
  107.     exit(1);
  108.   }
  109.  
  110.   cmdsize = 1;
  111.   cmdarg = (char **) xmalloc (cmdsize * sizeof (*cmdarg));
  112.   ncmd = 0;
  113.   dirsize = 1;
  114.   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
  115.   ndir = 0;
  116.  
  117.   quit_flag = 0;
  118.   line = (char *) xmalloc (linesize);
  119.   line[0] = '\0';        /* Terminate saved (now empty) cmd line */
  120.   instream = stdin;
  121.  
  122.   getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
  123.   current_directory = gdb_dirbuf;
  124.  
  125.   /* Parse arguments and options.  */
  126.   {
  127.     int c;
  128.     /* When var field is 0, use flag field to record the equivalent
  129.        short option (or arbitrary numbers starting at 10 for those
  130.        with no equivalent).  */
  131.     static struct option long_options[] =
  132.       {
  133.     {"readnow", no_argument, &readnow_symbol_files, 1},
  134.     {"r", no_argument, &readnow_symbol_files, 1},
  135.     {"mapped", no_argument, &mapped_symbol_files, 1},
  136.     {"m", no_argument, &mapped_symbol_files, 1},
  137.     {"quiet", no_argument, &quiet, 1},
  138.     {"q", no_argument, &quiet, 1},
  139.     {"silent", no_argument, &quiet, 1},
  140.     {"nx", no_argument, &inhibit_gdbinit, 1},
  141.     {"n", no_argument, &inhibit_gdbinit, 1},
  142.     {"batch", no_argument, &batch, 1},
  143.     {"epoch", no_argument, &epoch_interface, 1},
  144.  
  145.     /* This is a synonym for "--annotate=1".  --annotate is now preferred,
  146.        but keep this here for a long time because people will be running
  147.        emacses which use --fullname.  */
  148.     {"fullname", no_argument, 0, 'f'},
  149.     {"f", no_argument, 0, 'f'},
  150.  
  151.     {"annotate", required_argument, 0, 12},
  152.     {"help", no_argument, &print_help, 1},
  153.     {"se", required_argument, 0, 10},
  154.     {"symbols", required_argument, 0, 's'},
  155.     {"s", required_argument, 0, 's'},
  156.     {"exec", required_argument, 0, 'e'},
  157.     {"e", required_argument, 0, 'e'},
  158.     {"core", required_argument, 0, 'c'},
  159.     {"c", required_argument, 0, 'c'},
  160.     {"command", required_argument, 0, 'x'},
  161.     {"version", no_argument, &print_version, 1},
  162.     {"x", required_argument, 0, 'x'},
  163.     {"directory", required_argument, 0, 'd'},
  164.     {"cd", required_argument, 0, 11},
  165.     {"tty", required_argument, 0, 't'},
  166.     {"baud", required_argument, 0, 'b'},
  167.     {"b", required_argument, 0, 'b'},
  168. /* Allow machine descriptions to add more options... */
  169. #ifdef ADDITIONAL_OPTIONS
  170.     ADDITIONAL_OPTIONS
  171. #endif
  172.     {0, no_argument, 0, 0},
  173.       };
  174.  
  175.     while (1)
  176.       {
  177.     int option_index;
  178.  
  179.     c = getopt_long_only (argc, argv, "",
  180.                   long_options, &option_index);
  181.     if (c == EOF)
  182.       break;
  183.  
  184.     /* Long option that takes an argument.  */
  185.     if (c == 0 && long_options[option_index].flag == 0)
  186.       c = long_options[option_index].val;
  187.  
  188.     switch (c)
  189.       {
  190.       case 0:
  191.         /* Long option that just sets a flag.  */
  192.         break;
  193.       case 10:
  194.         symarg = optarg;
  195.         execarg = optarg;
  196.         break;
  197.       case 11:
  198.         cdarg = optarg;
  199.         break;
  200.       case 12:
  201.         /* FIXME: what if the syntax is wrong (e.g. not digits)?  */
  202.         annotation_level = atoi (optarg);
  203.         break;
  204.       case 'f':
  205.         annotation_level = 1;
  206.         break;
  207.       case 's':
  208.         symarg = optarg;
  209.         break;
  210.       case 'e':
  211.         execarg = optarg;
  212.         break;
  213.       case 'c':
  214.         corearg = optarg;
  215.         break;
  216.       case 'x':
  217.         cmdarg[ncmd++] = optarg;
  218.         if (ncmd >= cmdsize)
  219.           {
  220.         cmdsize *= 2;
  221.         cmdarg = (char **) xrealloc ((char *)cmdarg,
  222.                          cmdsize * sizeof (*cmdarg));
  223.           }
  224.         break;
  225.       case 'd':
  226.         dirarg[ndir++] = optarg;
  227.         if (ndir >= dirsize)
  228.           {
  229.         dirsize *= 2;
  230.         dirarg = (char **) xrealloc ((char *)dirarg,
  231.                          dirsize * sizeof (*dirarg));
  232.           }
  233.         break;
  234.       case 't':
  235.         ttyarg = optarg;
  236.         break;
  237.       case 'q':
  238.         quiet = 1;
  239.         break;
  240.       case 'b':
  241.         {
  242.           int i;
  243.           char *p;
  244.  
  245.           i = strtol (optarg, &p, 0);
  246.           if (i == 0 && p == optarg)
  247.  
  248.         /* Don't use *_filtered or warning() (which relies on
  249.                    current_target) until after initialize_all_files(). */
  250.  
  251.         fprintf_unfiltered
  252.           (gdb_stderr,
  253.            "warning: could not set baud rate to `%s'.\n", optarg);
  254.           else
  255.         baud_rate = i;
  256.         }
  257.         break;
  258.  
  259. #ifdef ADDITIONAL_OPTION_CASES
  260.       ADDITIONAL_OPTION_CASES
  261. #endif
  262.       case '?':
  263.         fprintf_unfiltered (gdb_stderr,
  264.              "Use `%s --help' for a complete list of options.\n",
  265.              argv[0]);
  266.         exit (1);
  267.       }
  268.       }
  269.  
  270.     /* OK, that's all the options.  The other arguments are filenames.  */
  271.     count = 0;
  272.     for (; optind < argc; optind++)
  273.       switch (++count)
  274.     {
  275.     case 1:
  276.       symarg = argv[optind];
  277.       execarg = argv[optind];
  278.       break;
  279.     case 2:
  280.       corearg = argv[optind];
  281.       break;
  282.     case 3:
  283.       fprintf_unfiltered (gdb_stderr,
  284.            "Excess command line arguments ignored. (%s%s)\n",
  285.            argv[optind], (optind == argc - 1) ? "" : " ...");
  286.       break;
  287.     }
  288.     if (batch)
  289.       quiet = 1;
  290.   }
  291.  
  292.   gdb_init ();
  293.  
  294.   /* Do these (and anything which might call wrap_here or *_filtered)
  295.      after initialize_all_files.  */
  296.   if (print_version)
  297.     {
  298.       print_gdb_version (gdb_stdout);
  299.       wrap_here ("");
  300.       printf_filtered ("\n");
  301.       exit (0);
  302.     }
  303.  
  304.   if (print_help)
  305.     {
  306.       /* --version is intentionally not documented here, because we
  307.      are printing the version here, and the help is long enough
  308.      already.  */
  309.  
  310.       print_gdb_version (gdb_stdout);
  311.       /* Make sure the output gets printed.  */
  312.       wrap_here ("");
  313.       printf_filtered ("\n");
  314.  
  315.       /* But don't use *_filtered here.  We don't want to prompt for continue
  316.      no matter how small the screen or how much we're going to print.  */
  317.       fputs_unfiltered ("\
  318. This is the GNU debugger.  Usage:\n\
  319.     gdb [options] [executable-file [core-file or process-id]]\n\
  320. Options:\n\
  321.   --help             Print this message.\n\
  322.   --quiet            Do not print version number on startup.\n\
  323.   --fullname         Output information used by emacs-GDB interface.\n\
  324.   --epoch            Output information used by epoch emacs-GDB interface.\n\
  325.   --batch            Exit after processing options.\n\
  326.   --nx               Do not read .gdbinit file.\n\
  327.   --tty=TTY          Use TTY for input/output by the program being debugged.\n\
  328.   --cd=DIR           Change current directory to DIR.\n\
  329.   --directory=DIR    Search for source files in DIR.\n\
  330.   --command=FILE     Execute GDB commands from FILE.\n\
  331.   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
  332.   --exec=EXECFILE    Use EXECFILE as the executable.\n\
  333.   --se=FILE          Use FILE as symbol file and executable file.\n\
  334.   --core=COREFILE    Analyze the core dump COREFILE.\n\
  335.   -b BAUDRATE        Set serial port baud rate used for remote debugging.\n\
  336.   --mapped           Use mapped symbol files if supported on this system.\n\
  337.   --readnow          Fully read symbol files on first access.\n\
  338. ", gdb_stdout);
  339. #ifdef ADDITIONAL_OPTION_HELP
  340.       fputs_unfiltered (ADDITIONAL_OPTION_HELP, gdb_stdout);
  341. #endif
  342.       fputs_unfiltered ("\n\
  343. For more information, type \"help\" from within GDB, or consult the\n\
  344. GDB manual (available as on-line info or a printed manual).\n", gdb_stdout);
  345.       exit (0);
  346.     }
  347.     
  348.   if (!quiet)
  349.     {
  350.       /* Print all the junk at the top, with trailing "..." if we are about
  351.      to read a symbol file (possibly slowly).  */
  352.       print_gnu_advertisement ();
  353.       print_gdb_version (gdb_stdout);
  354.       if (symarg)
  355.     printf_filtered ("..");
  356.       wrap_here("");
  357.       gdb_flush (gdb_stdout);        /* Force to screen during slow operations */
  358.     }
  359.  
  360.   error_pre_print = "\n\n";
  361.   /* We may get more than one warning, don't double space all of them... */
  362.   warning_pre_print = "\nwarning: ";
  363.  
  364.   /* Read and execute $HOME/.gdbinit file, if it exists.  This is done
  365.      *before* all the command line arguments are processed; it sets
  366.      global parameters, which are independent of what file you are
  367.      debugging or what directory you are in.  */
  368.   homedir = getenv ("HOME");
  369.   if (homedir)
  370.     {
  371.       homeinit = (char *) alloca (strlen (getenv ("HOME")) +
  372.                   strlen (gdbinit) + 10);
  373.       strcpy (homeinit, getenv ("HOME"));
  374.       strcat (homeinit, "/");
  375.       strcat (homeinit, gdbinit);
  376.       if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  377.     {
  378.       if (!SET_TOP_LEVEL ())
  379.         source_command (homeinit, 0);
  380.     }
  381.       do_cleanups (ALL_CLEANUPS);
  382.  
  383.       /* Do stats; no need to do them elsewhere since we'll only
  384.      need them if homedir is set.  Make sure that they are
  385.      zero in case one of them fails (this guarantees that they
  386.      won't match if either exists).  */
  387.       
  388.       memset (&homebuf, 0, sizeof (struct stat));
  389.       memset (&cwdbuf, 0, sizeof (struct stat));
  390.       
  391.       stat (homeinit, &homebuf);
  392.       stat (gdbinit, &cwdbuf); /* We'll only need this if
  393.                        homedir was set.  */
  394.     }
  395.   
  396.   /* Now perform all the actions indicated by the arguments.  */
  397.   if (cdarg != NULL)
  398.     {
  399.       if (!SET_TOP_LEVEL ())
  400.     {
  401.       cd_command (cdarg, 0);
  402.     }
  403.     }
  404.   do_cleanups (ALL_CLEANUPS);
  405.  
  406.   for (i = 0; i < ndir; i++)
  407.     if (!SET_TOP_LEVEL ())
  408.       directory_command (dirarg[i], 0);
  409.   free ((PTR)dirarg);
  410.   do_cleanups (ALL_CLEANUPS);
  411.  
  412.   if (execarg != NULL
  413.       && symarg != NULL
  414.       && STREQ (execarg, symarg))
  415.     {
  416.       /* The exec file and the symbol-file are the same.  If we can't open
  417.      it, better only print one error message.  */
  418.       if (!SET_TOP_LEVEL ())
  419.     {
  420.       exec_file_command (execarg, !batch);
  421.       symbol_file_command (symarg, 0);
  422.     }
  423.     }
  424.   else
  425.     {
  426.       if (execarg != NULL)
  427.     if (!SET_TOP_LEVEL ())
  428.       exec_file_command (execarg, !batch);
  429.       if (symarg != NULL)
  430.     if (!SET_TOP_LEVEL ())
  431.       symbol_file_command (symarg, 0);
  432.     }
  433.   do_cleanups (ALL_CLEANUPS);
  434.  
  435.   /* After the symbol file has been read, print a newline to get us
  436.      beyond the copyright line...  But errors should still set off
  437.      the error message with a (single) blank line.  */
  438.   if (!quiet)
  439.     printf_filtered ("\n");
  440.   error_pre_print = "\n";
  441.   warning_pre_print = "\nwarning: ";
  442.  
  443.   if (corearg != NULL)
  444.     if (!SET_TOP_LEVEL ())
  445.       core_file_command (corearg, !batch);
  446.     else if (isdigit (corearg[0]) && !SET_TOP_LEVEL ())
  447.       attach_command (corearg, !batch);
  448.   do_cleanups (ALL_CLEANUPS);
  449.  
  450.   if (ttyarg != NULL)
  451.     if (!SET_TOP_LEVEL ())
  452.       tty_command (ttyarg, !batch);
  453.   do_cleanups (ALL_CLEANUPS);
  454.  
  455. #ifdef ADDITIONAL_OPTION_HANDLER
  456.   ADDITIONAL_OPTION_HANDLER;
  457. #endif
  458.  
  459.   /* Error messages should no longer be distinguished with extra output. */
  460.   error_pre_print = 0;
  461.   warning_pre_print = "warning: ";
  462.  
  463.   /* Read the .gdbinit file in the current directory, *if* it isn't
  464.      the same as the $HOME/.gdbinit file (it should exist, also).  */
  465.   
  466.   if (!homedir
  467.       || memcmp ((char *) &homebuf, (char *) &cwdbuf, sizeof (struct stat)))
  468.     if (!inhibit_gdbinit && access (gdbinit, R_OK) == 0)
  469.       {
  470.     if (!SET_TOP_LEVEL ())
  471.       source_command (gdbinit, 0);
  472.       }
  473.   do_cleanups (ALL_CLEANUPS);
  474.  
  475.   for (i = 0; i < ncmd; i++)
  476.     {
  477.       if (!SET_TOP_LEVEL ())
  478.     {
  479.       if (cmdarg[i][0] == '-' && cmdarg[i][1] == '\0')
  480.         read_command_file (stdin);
  481.       else
  482.         source_command (cmdarg[i], !batch);
  483.       do_cleanups (ALL_CLEANUPS);
  484.     }
  485.     }
  486.   free ((PTR)cmdarg);
  487.  
  488.   /* Read in the old history after all the command files have been read. */
  489.   init_history();
  490.  
  491.   if (batch)
  492.     {
  493.       /* We have hit the end of the batch file.  */
  494.       exit (0);
  495.     }
  496.  
  497.   /* Do any host- or target-specific hacks.  This is used for i960 targets
  498.      to force the user to set a nindy target and spec its parameters.  */
  499.  
  500. #ifdef BEFORE_MAIN_LOOP_HOOK
  501.   BEFORE_MAIN_LOOP_HOOK;
  502. #endif
  503.  
  504.   /* The command loop.  */
  505.  
  506.   while (1)
  507.     {
  508.       if (!SET_TOP_LEVEL ())
  509.     {
  510.       do_cleanups (ALL_CLEANUPS);        /* Do complete cleanup */
  511.       command_loop ();
  512.           quit_command ((char *)0, instream == stdin);
  513.     }
  514.     }
  515.   /* No exit -- exit is through quit_command.  */
  516. }
  517.  
  518. void
  519. init_proc ()
  520. {
  521. }
  522.  
  523. int
  524. proc_wait (pid, status)
  525.      int pid;
  526.      int *status;
  527. {
  528. #ifndef __GO32__
  529.   return wait (status);
  530. #endif
  531. }
  532.  
  533. void
  534. proc_remove_foreign (pid)
  535.      int pid;
  536. {
  537. }
  538.  
  539. void
  540. fputs_unfiltered (linebuffer, stream)
  541.      const char *linebuffer;
  542.      FILE *stream;
  543. {
  544.   fputs (linebuffer, stream);
  545. }
  546.