home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / sim / common / run.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-15  |  3.4 KB  |  161 lines

  1. /* run front end support for all the simulators.
  2.    Copyright (C) 1992, 1993 1994, 1995 Free Software Foundation, Inc.
  3.  
  4. GNU CC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18.  
  19. /* Steve Chamberlain
  20.    sac@cygnus.com */
  21.  
  22. #include <signal.h>
  23. #include <stdio.h>
  24. #include <varargs.h>
  25. #include "bfd.h"
  26. #include "remote-sim.h"
  27. #include "callback.h"
  28. #ifndef SIGQUIT
  29. #define SIGQUIT SIGTERM
  30. #endif
  31.  
  32. void usage();
  33. extern int optind;
  34. extern char *optarg;
  35.  
  36. int target_byte_order;
  37.  
  38. extern host_callback default_callback;
  39. int
  40. main (ac, av)
  41.      int ac;
  42.      char **av;
  43. {
  44.   bfd *abfd;
  45.   bfd_vma start_address;
  46.   asection *s;
  47.   int i;
  48.   int verbose = 0;
  49.   int trace = 0;
  50.   char *name = "";
  51.   enum sim_stop reason;
  52.   int sigrc;
  53.  
  54.   while ((i = getopt (ac, av, "m:p:s:tv")) != EOF) 
  55.     switch (i)
  56.       {
  57.       case 'm':
  58.     sim_size (atoi (optarg));
  59.     break;
  60.       case 'p':
  61.     sim_set_profile (atoi (optarg));
  62.     break;
  63.       case 's':
  64.     sim_set_profile_size (atoi (optarg));
  65.     break;
  66.       case 't':
  67.     trace = 1;
  68.     break;
  69.       case 'v':
  70.     verbose = 1;
  71.     break;
  72.       default:
  73.     usage();
  74.       }
  75.   ac -= optind;
  76.   av += optind;
  77.  
  78.   if (ac != 1)
  79.     usage();
  80.  
  81.   name = *av;
  82.  
  83.   if (verbose)
  84.     {
  85.       printf ("run %s\n", name);
  86.     }
  87.  
  88.   abfd = bfd_openr (name, 0);
  89.   if (!abfd) 
  90.     {
  91.       fprintf (stderr, "run: can't open %s: %s\n", 
  92.           name, bfd_errmsg(bfd_get_error()));
  93.       exit (1);
  94.     }
  95.  
  96.   if (!bfd_check_format (abfd, bfd_object))
  97.     {
  98.       fprintf (stderr, "run: can't load %s: %s\n",
  99.            name, bfd_errmsg(bfd_get_error()));
  100.       exit (1);
  101.     }
  102.  
  103.   sim_set_callbacks (&default_callback);
  104.   default_callback.init (&default_callback);
  105.  
  106.   /* Ensure that any run-time initialisation that needs to be
  107.      performed by the simulator can occur. */
  108.   sim_open(NULL);
  109.  
  110.   for (s = abfd->sections; s; s = s->next)
  111.   if (abfd && (s->flags & SEC_LOAD))
  112.     {
  113.       unsigned char *buffer = (unsigned char *)malloc (bfd_section_size (abfd, s));
  114.       bfd_get_section_contents (abfd,
  115.                 s,
  116.                 buffer,
  117.                 0,
  118.                 bfd_section_size (abfd, s));
  119.       sim_write (s->vma, buffer, bfd_section_size (abfd, s));
  120.     }
  121.  
  122.   start_address = bfd_get_start_address (abfd);
  123.   sim_create_inferior (start_address, NULL, NULL);
  124.  
  125.   target_byte_order = bfd_big_endian (abfd) ? 4321 : 1234;
  126.  
  127.   if (trace)
  128.     {
  129.       int done = 0;
  130.       while (!done)
  131.     {
  132.       done = sim_trace ();
  133.     }
  134.     }
  135.   else
  136.     {
  137.       sim_resume (0, 0);
  138.     }
  139.   if (verbose)
  140.     sim_info (0);
  141.  
  142.   sim_stop_reason (&reason, &sigrc);
  143.  
  144.   sim_close(0);
  145.  
  146.   /* If reason is sim_exited, then sigrc holds the exit code which we want
  147.      to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
  148.      the signal that the simulator received; we want to return that to
  149.      indicate failure.  */
  150.   return sigrc;
  151. }
  152.  
  153. void
  154. usage()
  155. {
  156.   fprintf (stderr, "usage: run [-tv][-m size] program\n");
  157.   exit (1);
  158. }
  159.  
  160.  
  161.