home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / gdbserver / server.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  4.4 KB  |  165 lines

  1. /* Main code for remote server for GDB.
  2.    Copyright (C) 1989, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "server.h"
  21.  
  22. int cont_thread;
  23. int general_thread;
  24. int thread_from_wait;
  25. int old_thread_from_wait;
  26.  
  27. int
  28. main (argc, argv)
  29.      int argc;
  30.      char *argv[];
  31. {
  32.   char ch, status, own_buf[2000], mem_buf[2000];
  33.   int i = 0;
  34.   unsigned char signal;
  35.   unsigned int mem_addr, len;
  36.  
  37.   if (setjmp(toplevel))
  38.     {
  39.       fprintf(stderr, "Exiting\n");
  40.       exit(1);
  41.     }
  42.  
  43.   if (argc < 3)
  44.     error("Usage: gdbserver tty prog [args ...]");
  45.  
  46.   inferior_pid = create_inferior (argv[2], &argv[2]);
  47.   fprintf (stderr, "Process %s created; pid = %d\n", argv[2], inferior_pid);
  48.  
  49.   signal = mywait (&status);    /* Wait till we are at 1st instr in prog */
  50.  
  51.   /* We are now stopped at the first instruction of the target process */
  52.  
  53.   while (1)
  54.     {
  55.       remote_open (argv[1]);
  56.  
  57.       setjmp(toplevel);
  58.       while (getpkt (own_buf) > 0)
  59.     {
  60.       unsigned char sig;
  61.       i = 0;
  62.       ch = own_buf[i++];
  63.       switch (ch)
  64.         {
  65.         case '?':
  66.           prepare_resume_reply (own_buf, status, signal);
  67.           break;
  68.         case 'H':
  69.           switch (own_buf[1])
  70.         {
  71.         case 'g':
  72.           general_thread = strtol (&own_buf[2], NULL, 16);
  73.           write_ok (own_buf);
  74.           fetch_inferior_registers (0);
  75.           break;
  76.         case 'c':
  77.           cont_thread = strtol (&own_buf[2], NULL, 16);
  78.           write_ok (own_buf);
  79.           break;
  80.         default:
  81.           /* Silently ignore it so that gdb can extend the protocol
  82.              without compatibility headaches.  */
  83.           own_buf[0] = '\0';
  84.           break;
  85.         }
  86.           break;
  87.         case 'g':
  88.           convert_int_to_ascii (registers, own_buf, REGISTER_BYTES);
  89.           break;
  90.         case 'G':
  91.           convert_ascii_to_int (&own_buf[1], registers, REGISTER_BYTES);
  92.           store_inferior_registers (-1);
  93.           write_ok (own_buf);
  94.           break;
  95.         case 'm':
  96.           decode_m_packet (&own_buf[1], &mem_addr, &len);
  97.           read_inferior_memory (mem_addr, mem_buf, len);
  98.           convert_int_to_ascii (mem_buf, own_buf, len);
  99.           break;
  100.         case 'M':
  101.           decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
  102.           if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
  103.         write_ok (own_buf);
  104.           else
  105.         write_enn (own_buf);
  106.           break;
  107.         case 'C':
  108.           convert_ascii_to_int (own_buf + 1, &sig, 1);
  109.           myresume (0, sig);
  110.           signal = mywait (&status);
  111.           prepare_resume_reply (own_buf, status, signal);
  112.           break;
  113.         case 'S':
  114.           convert_ascii_to_int (own_buf + 1, &sig, 1);
  115.           myresume (1, sig);
  116.           signal = mywait (&status);
  117.           prepare_resume_reply (own_buf, status, signal);
  118.           break;
  119.         case 'c':
  120.           myresume (0, 0);
  121.           signal = mywait (&status);
  122.           prepare_resume_reply (own_buf, status, signal);
  123.           break;
  124.         case 's':
  125.           myresume (1, 0);
  126.           signal = mywait (&status);
  127.           prepare_resume_reply (own_buf, status, signal);
  128.           break;
  129.         case 'k':
  130.           fprintf (stderr, "Killing inferior\n");
  131.           kill_inferior ();
  132.           fprintf (stderr, "GDBserver exiting\n");
  133.           exit (0);
  134.           break;
  135.         default:
  136.           /* It is a request we don't understand.  Respond with an
  137.          empty packet so that gdb knows that we don't support this
  138.          request.  */
  139.           own_buf[0] = '\0';
  140.           break;
  141.         }
  142.  
  143.       putpkt (own_buf);
  144.  
  145.       if (status == 'W')
  146.         fprintf (stderr,
  147.              "\nChild exited with status %d\n", sig);
  148.       if (status == 'X')
  149.         fprintf (stderr, "\nChild terminated with signal = 0x%x\n", sig);
  150.       if (status == 'W' || status == 'X')
  151.         {
  152.           fprintf (stderr, "GDBserver exiting\n");
  153.           exit (0);
  154.         }
  155.     }
  156.  
  157.       /* We come here when getpkt fails.  Close the connection, and re-open it
  158.          at the top of the loop.  */
  159.  
  160.       fprintf (stderr, "Remote side has terminated connection.  GDBserver will reopen the connection.\n");
  161.  
  162.       remote_close ();
  163.     }
  164. }
  165.