gdbserver.c

Go to the documentation of this file.
00001 /*
00002  * $Id: gdbserver.c,v 1.50 2004/10/19 18:43:50 zfrdh Exp $
00003  *
00004  ****************************************************************************
00005  *
00006  * gdbserver.c - Provide interface to a remote debugging target of gdb.
00007  * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  ****************************************************************************
00024  */
00025 
00026 /**
00027  * \file gdbserver.c
00028  * \brief Provide an interface to gdb's remote serial protocol.
00029  *
00030  * This module allows a program to be used by gdb as a remote target. The
00031  * remote target and gdb communicate via gdb's remote serial protocol. The
00032  * protocol is documented in the gdb manual and will not be repeated here.
00033  *
00034  * Hitting Ctrl-c in gdb can be used to interrupt the remote target while it
00035  * is processing instructions and return control back to gdb.
00036  *
00037  * Issuing a 'signal SIGxxx' command from gdb will send the signal to the
00038  * remote target via a "continue with signal" packet. The target will process
00039  * and interpret the signal, but not pass it on to the AVR program running in
00040  * the target since it really makes no sense to do so. In some circumstances,
00041  * it may make sense to use the gdb signal mechanism as a way to initiate some
00042  * sort of external stimulus to be passed on to the virtual hardware system.
00043  *
00044  * Signals from gdb which are processed have the following meanings:
00045  *
00046  * \li \c SIGHUP Initiate a reset of the target. (Simulates a hardware reset)
00047  */
00048 
00049 #include <config.h>
00050 
00051 #include <sys/types.h>
00052 #include <netinet/in.h>
00053 #include <netinet/tcp.h>
00054 #include <arpa/inet.h>
00055 #include <sys/socket.h>
00056 #include <stdio.h>
00057 #include <string.h>
00058 #include <errno.h>
00059 #include <unistd.h>
00060 #include <fcntl.h>
00061 #include <signal.h>
00062 
00063 #include "avrerror.h"
00064 #include "avrmalloc.h"
00065 #include "gdb.h"
00066 #include "sig.h"
00067 
00068 /* *INDENT-OFF* */
00069 #ifndef DOXYGEN                 /* have doxygen system ignore this. */
00070 enum
00071 {
00072     MAX_BUF        = 400,       /* Maximum size of read/write buffers. */
00073     MAX_READ_RETRY = 10,        /* Maximum number of retries if a read is
00074                                    incomplete. */
00075 
00076 #if defined(USE_EEPROM_SPACE)
00077     MEM_SPACE_MASK = 0x00ff0000, /* mask to get bits which determine memory
00078                                     space */
00079     FLASH_OFFSET   = 0x00000000, /* Data in flash has this offset from gdb */
00080     SRAM_OFFSET    = 0x00800000, /* Data in sram has this offset from gdb */
00081     EEPROM_OFFSET  = 0x00810000, /* Data in eeprom has this offset from gdb */
00082 #else
00083     MEM_SPACE_MASK = 0x00f00000, /* mask to get bits which determine memory
00084                                     space */
00085     FLASH_OFFSET   = 0x00000000, /* Data in flash has this offset from gdb */
00086     SRAM_OFFSET    = 0x00800000, /* Data in sram has this offset from gdb */
00087 #endif
00088 
00089     GDB_BLOCKING_OFF = 0,       /* Signify that a read is non-blocking. */
00090     GDB_BLOCKING_ON  = 1,       /* Signify that a read will block. */
00091 
00092     GDB_RET_CTRL_C = -2,        /* gdb has sent Ctrl-C to interrupt what is
00093                                    doing */
00094     GDB_RET_KILL_REQUEST = -1,  /* gdb has requested that sim be killed */
00095     GDB_RET_OK = 0,             /* continue normal processing of gdb
00096                                    requests */
00097 
00098     SPL_ADDR = 0x5d,
00099     SPH_ADDR = 0x5e,
00100 };
00101 #endif /* not DOXYGEN */
00102 /* *INDENT-ON* */
00103 
00104 /* Use HEX_DIGIT as a lookup table to convert a nibble to hex 
00105    digit. */
00106 static char HEX_DIGIT[] = "0123456789abcdef";
00107 
00108 /* There are a couple of nested infinite loops, this allows escaping them
00109    all. */
00110 static int global_server_quit = 0;
00111 
00112 /* Flag if debug messages should be printed out. */
00113 static int global_debug_on;
00114 
00115 /* prototypes */
00116 
00117 static int gdb_pre_parse_packet (GdbComm_T *comm, int fd, int blocking);
00118 
00119 /* Wrap read(2) so we can read a byte without having
00120    to do a shit load of error checking every time. */
00121 
00122 static int
00123 gdb_read_byte (int fd)
00124 {
00125     char c;
00126     int res;
00127     int cnt = MAX_READ_RETRY;
00128 
00129     while (cnt--)
00130     {
00131         res = read (fd, &c, 1);
00132         if (res < 0)
00133         {
00134             if (errno == EAGAIN)
00135                 /* fd was set to non-blocking and no data was available */
00136                 return -1;
00137 
00138             avr_error ("read failed: %s", strerror (errno));
00139         }
00140 
00141         if (res == 0)
00142         {
00143             avr_warning ("incomplete read\n");
00144             continue;
00145         }
00146 
00147         return c;
00148     }
00149     avr_error ("Maximum read reties reached");
00150 
00151     return 0;                   /* make compiler happy */
00152 }
00153 
00154 /* Convert a hexidecimal digit to a 4 bit nibble. */
00155 
00156 static uint8_t
00157 hex2nib (char hex)
00158 {
00159     if ((hex >= 'A') && (hex <= 'F'))
00160         return (10 + (hex - 'A'));
00161 
00162     else if ((hex >= 'a') && (hex <= 'f'))
00163         return (10 + (hex - 'a'));
00164 
00165     else if ((hex >= '0') && (hex <= '9'))
00166         return (hex - '0');
00167 
00168     /* Shouldn't get here unless the developer screwed up ;) */
00169     avr_error ("Invalid hexidecimal digit: 0x%02x", hex);
00170 
00171     return 0;                   /* make compiler happy */
00172 }
00173 
00174 /* Wrapper for write(2) which hides all the repetitive error
00175    checking crap. */
00176 
00177 static void
00178 gdb_write (int fd, const void *buf, size_t count)
00179 {
00180     int res;
00181 
00182     res = write (fd, buf, count);
00183 
00184     /* FIXME: should we try and catch interrupted system calls here? */
00185 
00186     if (res < 0)
00187         avr_error ("write failed: %s", strerror (errno));
00188 
00189     /* FIXME: if this happens a lot, we could try to resend the
00190        unsent bytes. */
00191 
00192     if (res != count)
00193         avr_error ("write only wrote %d of %d bytes", res, count);
00194 }
00195 
00196 /* Use a single function for storing/getting the last reply message.
00197    If reply is NULL, return pointer to the last reply saved.
00198    Otherwise, make a copy of the buffer pointed to by reply. */
00199 
00200 static char *
00201 gdb_last_reply (char *reply)
00202 {
00203     static char *last_reply = NULL;
00204 
00205     if (reply == NULL)
00206     {
00207         if (last_reply == NULL)
00208             return "";
00209         else
00210             return last_reply;
00211     }
00212 
00213     avr_free (last_reply);
00214     last_reply = avr_strdup (reply);
00215 
00216     return last_reply;
00217 }
00218 
00219 /* Acknowledge a packet from GDB */
00220 
00221 static void
00222 gdb_send_ack (int fd)
00223 {
00224     if (global_debug_on)
00225         fprintf (stderr, " Ack -> gdb\n");
00226 
00227     gdb_write (fd, "+", 1);
00228 }
00229 
00230 /* Send a reply to GDB. */
00231 
00232 static void
00233 gdb_send_reply (int fd, char *reply)
00234 {
00235     int cksum = 0;
00236     int bytes;
00237 
00238     static char buf[MAX_BUF];
00239 
00240     /* Save the reply to last reply so we can resend if need be. */
00241     gdb_last_reply (reply);
00242 
00243     if (global_debug_on)
00244         fprintf (stderr, "Sent: $%s#", reply);
00245 
00246     if (*reply == '\0')
00247     {
00248         gdb_write (fd, "$#00", 4);
00249 
00250         if (global_debug_on)
00251             fprintf (stderr, "%02x\n", cksum & 0xff);
00252     }
00253     else
00254     {
00255         memset (buf, '\0', sizeof (buf));
00256 
00257         buf[0] = '$';
00258         bytes = 1;
00259 
00260         while (*reply)
00261         {
00262             cksum += (unsigned char)*reply;
00263             buf[bytes] = *reply;
00264             bytes++;
00265             reply++;
00266 
00267             /* must account for "#cc" to be added */
00268             if (bytes == (MAX_BUF - 3))
00269             {
00270                 /* FIXME: TRoth 2002/02/18 - splitting reply would be better */
00271                 avr_error ("buffer overflow");
00272             }
00273         }
00274 
00275         if (global_debug_on)
00276             fprintf (stderr, "%02x\n", cksum & 0xff);
00277 
00278         buf[bytes++] = '#';
00279         buf[bytes++] = HEX_DIGIT[(cksum >> 4) & 0xf];
00280         buf[bytes++] = HEX_DIGIT[cksum & 0xf];
00281 
00282         gdb_write (fd, buf, bytes);
00283     }
00284 }
00285 
00286 /* GDB needs the 32 8-bit, gpw registers (r00 - r31), the 
00287    8-bit SREG, the 16-bit SP (stack pointer) and the 32-bit PC
00288    (program counter). Thus need to send a reply with
00289    r00, r01, ..., r31, SREG, SPL, SPH, PCL, PCH
00290    Low bytes before High since AVR is little endian. */
00291 
00292 static void
00293 gdb_read_registers (GdbComm_T *comm, int fd)
00294 {
00295     int i;
00296     uint32_t val;               /* ensure it's 32 bit value */
00297 
00298     /* (32 gpwr, SREG, SP, PC) * 2 hex bytes + terminator */
00299     size_t buf_sz = (32 + 1 + 2 + 4) * 2 + 1;
00300     char *buf;
00301 
00302     buf = avr_new0 (char, buf_sz);
00303 
00304     /* 32 gen purpose working registers */
00305     for (i = 0; i < 32; i++)
00306     {
00307         val = comm->read_reg (comm->user_data, i);
00308         buf[i * 2] = HEX_DIGIT[(val >> 4) & 0xf];
00309         buf[i * 2 + 1] = HEX_DIGIT[val & 0xf];
00310     }
00311 
00312     /* GDB thinks SREG is register number 32 */
00313     val = comm->read_sreg (comm->user_data);
00314     buf[i * 2] = HEX_DIGIT[(val >> 4) & 0xf];
00315     buf[i * 2 + 1] = HEX_DIGIT[val & 0xf];
00316     i++;
00317 
00318     /* GDB thinks SP is register number 33 */
00319     val = comm->read_sram (comm->user_data, SPL_ADDR);
00320     buf[i * 2] = HEX_DIGIT[(val >> 4) & 0xf];
00321     buf[i * 2 + 1] = HEX_DIGIT[val & 0xf];
00322     i++;
00323 
00324     val = comm->read_sram (comm->user_data, SPH_ADDR);
00325     buf[i * 2] = HEX_DIGIT[(val >> 4) & 0xf];
00326     buf[i * 2 + 1] = HEX_DIGIT[val & 0xf];
00327     i++;
00328 
00329     /* GDB thinks PC is register number 34.
00330        GDB stores PC in a 32 bit value (only uses 23 bits though).
00331        GDB thinks PC is bytes into flash, not words like in simulavr. */
00332 
00333     val = comm->read_pc (comm->user_data) * 2;
00334     buf[i * 2] = HEX_DIGIT[(val >> 4) & 0xf];
00335     buf[i * 2 + 1] = HEX_DIGIT[val & 0xf];
00336 
00337     val >>= 8;
00338     buf[i * 2 + 2] = HEX_DIGIT[(val >> 4) & 0xf];
00339     buf[i * 2 + 3] = HEX_DIGIT[val & 0xf];
00340 
00341     val >>= 8;
00342     buf[i * 2 + 4] = HEX_DIGIT[(val >> 4) & 0xf];
00343     buf[i * 2 + 5] = HEX_DIGIT[val & 0xf];
00344 
00345     val >>= 8;
00346     buf[i * 2 + 6] = HEX_DIGIT[(val >> 4) & 0xf];
00347     buf[i * 2 + 7] = HEX_DIGIT[val & 0xf];
00348 
00349     gdb_send_reply (fd, buf);
00350     avr_free (buf);
00351 }
00352 
00353 /* GDB is sending values to be written to the registers. Registers are the
00354    same and in the same order as described in gdb_read_registers() above. */
00355 
00356 static void
00357 gdb_write_registers (GdbComm_T *comm, int fd, char *pkt)
00358 {
00359     int i;
00360     uint8_t bval;
00361     uint32_t val;               /* ensure it's a 32 bit value */
00362 
00363     /* 32 gen purpose working registers */
00364     for (i = 0; i < 32; i++)
00365     {
00366         bval = hex2nib (*pkt++) << 4;
00367         bval += hex2nib (*pkt++);
00368         comm->write_reg (comm->user_data, i, bval);
00369     }
00370 
00371     /* GDB thinks SREG is register number 32 */
00372     bval = hex2nib (*pkt++) << 4;
00373     bval += hex2nib (*pkt++);
00374     comm->write_sreg (comm->user_data, bval);
00375 
00376     /* GDB thinks SP is register number 33 */
00377     bval = hex2nib (*pkt++) << 4;
00378     bval += hex2nib (*pkt++);
00379     comm->write_sram (comm->user_data, SPL_ADDR, bval);
00380 
00381     bval = hex2nib (*pkt++) << 4;
00382     bval += hex2nib (*pkt++);
00383     comm->write_sram (comm->user_data, SPH_ADDR, bval);
00384 
00385     /* GDB thinks PC is register number 34.
00386        GDB stores PC in a 32 bit value (only uses 23 bits though).
00387        GDB thinks PC is bytes into flash, not words like in simulavr.
00388 
00389        Must cast to uint32_t so as not to get mysterious truncation. */
00390 
00391     val = ((uint32_t) hex2nib (*pkt++)) << 4;
00392     val += ((uint32_t) hex2nib (*pkt++));
00393 
00394     val += ((uint32_t) hex2nib (*pkt++)) << 12;
00395     val += ((uint32_t) hex2nib (*pkt++)) << 8;
00396 
00397     val += ((uint32_t) hex2nib (*pkt++)) << 20;
00398     val += ((uint32_t) hex2nib (*pkt++)) << 16;
00399 
00400     val += ((uint32_t) hex2nib (*pkt++)) << 28;
00401     val += ((uint32_t) hex2nib (*pkt++)) << 24;
00402     comm->write_pc (comm->user_data, val / 2);
00403 
00404     gdb_send_reply (fd, "OK");
00405 }
00406 
00407 /* Extract a hexidecimal number from the pkt. Keep scanning pkt until stop
00408    char is reached or size of int is exceeded or a NULL is reached. pkt is
00409    modified to point to stop char when done.
00410 
00411    Use this function to extract a num with an arbitrary num of hex
00412    digits. This should _not_ be used to extract n digits from a m len string
00413    of digits (n <= m). */
00414 
00415 static int
00416 gdb_extract_hex_num (char **pkt, char stop)
00417 {
00418     int i = 0;
00419     int num = 0;
00420     char *p = *pkt;
00421     int max_shifts = sizeof (int) * 2 - 1; /* max number of nibbles to shift
00422                                               through */
00423 
00424     while ((*p != stop) && (*p != '\0'))
00425     {
00426         if (i > max_shifts)
00427             avr_error ("number too large");
00428 
00429         num = (num << 4)  | hex2nib (*p);
00430         i++;
00431         p++;
00432     }
00433 
00434     *pkt = p;
00435     return num;
00436 }
00437 
00438 /* Read a single register. Packet form: 'pn' where n is a hex number with no
00439    zero padding. */
00440 
00441 static void
00442 gdb_read_register (GdbComm_T *comm, int fd, char *pkt)
00443 {
00444     int reg;
00445 
00446     char reply[MAX_BUF];
00447 
00448     memset (reply, '\0', sizeof (reply));
00449 
00450     reg = gdb_extract_hex_num (&pkt, '\0');
00451 
00452     if ((reg >= 0) && (reg < 32))
00453     {                           /* general regs */
00454         uint8_t val = comm->read_reg (comm->user_data, reg);
00455         snprintf (reply, sizeof (reply) - 1, "%02x", val);
00456     }
00457     else if (reg == 32)         /* sreg */
00458     {
00459         uint8_t val = comm->read_sreg (comm->user_data);
00460         snprintf (reply, sizeof (reply) - 1, "%02x", val);
00461     }
00462     else if (reg == 33)         /* SP */
00463     {
00464         uint8_t spl, sph;
00465         spl = comm->read_sram (comm->user_data, SPL_ADDR);
00466         sph = comm->read_sram (comm->user_data, SPH_ADDR);
00467         snprintf (reply, sizeof (reply) - 1, "%02x%02x", spl, sph);
00468     }
00469     else if (reg == 34)         /* PC */
00470     {
00471         int val = comm->read_pc (comm->user_data) * 2;
00472         snprintf (reply, sizeof (reply) - 1, "%02x%02x" "%02x%02x",
00473                   val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff,
00474                   (val >> 24) & 0xff);
00475     }
00476     else
00477     {
00478         avr_warning ("Bad register value: %d\n", reg);
00479         gdb_send_reply (fd, "E00");
00480         return;
00481     }
00482     gdb_send_reply (fd, reply);
00483 }
00484 
00485 /* Write a single register. Packet form: 'Pn=r' where n is a hex number with
00486    no zero padding and r is two hex digits for each byte in register (target
00487    byte order). */
00488 
00489 static void
00490 gdb_write_register (GdbComm_T *comm, int fd, char *pkt)
00491 {
00492     int reg;
00493     uint32_t dval, hval;
00494 
00495     reg = gdb_extract_hex_num (&pkt, '=');
00496     pkt++;                      /* skip over '=' character */
00497 
00498     /* extract the low byte of value from pkt */
00499     dval = hex2nib (*pkt++) << 4;
00500     dval += hex2nib (*pkt++);
00501 
00502     if ((reg >= 0) && (reg < 33))
00503     {
00504         /* r0 to r31 and SREG */
00505         if (reg == 32)          /* gdb thinks SREG is register 32 */
00506         {
00507             comm->write_sreg (comm->user_data, dval & 0xff);
00508         }
00509         else
00510         {
00511             comm->write_reg (comm->user_data, reg, dval & 0xff);
00512         }
00513     }
00514     else if (reg == 33)
00515     {
00516         /* SP is 2 bytes long so extract upper byte */
00517         hval = hex2nib (*pkt++) << 4;
00518         hval += hex2nib (*pkt++);
00519 
00520         comm->write_sram (comm->user_data, SPL_ADDR, dval & 0xff);
00521         comm->write_sram (comm->user_data, SPH_ADDR, hval & 0xff);
00522     }
00523     else if (reg == 34)
00524     {
00525         /* GDB thinks PC is register number 34.
00526            GDB stores PC in a 32 bit value (only uses 23 bits though).
00527            GDB thinks PC is bytes into flash, not words like in simulavr.
00528 
00529            Must cast to uint32_t so as not to get mysterious truncation. */
00530 
00531         /* we already read the first two nibbles */
00532 
00533         dval += ((uint32_t) hex2nib (*pkt++)) << 12;
00534         dval += ((uint32_t) hex2nib (*pkt++)) << 8;
00535 
00536         dval += ((uint32_t) hex2nib (*pkt++)) << 20;
00537         dval += ((uint32_t) hex2nib (*pkt++)) << 16;
00538 
00539         dval += ((uint32_t) hex2nib (*pkt++)) << 28;
00540         dval += ((uint32_t) hex2nib (*pkt++)) << 24;
00541         comm->write_pc (comm->user_data, dval / 2);
00542     }
00543     else
00544     {
00545         avr_warning ("Bad register value: %d\n", reg);
00546         gdb_send_reply (fd, "E00");
00547         return;
00548     }
00549 
00550     gdb_send_reply (fd, "OK");
00551 }
00552 
00553 /* Parse the pkt string for the addr and length.
00554    a_end is first char after addr.
00555    l_end is first char after len.
00556    Returns number of characters to advance pkt. */
00557 
00558 static int
00559 gdb_get_addr_len (char *pkt, char a_end, char l_end, int *addr, int *len)
00560 {
00561     char *orig_pkt = pkt;
00562 
00563     *addr = 0;
00564     *len = 0;
00565 
00566     /* Get the addr from the packet */
00567     while (*pkt != a_end)
00568         *addr = (*addr << 4) + hex2nib (*pkt++);
00569     pkt++;                      /* skip over a_end */
00570 
00571     /* Get the length from the packet */
00572     while (*pkt != l_end)
00573         *len = (*len << 4) + hex2nib (*pkt++);
00574     pkt++;                      /* skip over l_end */
00575 
00576 /*      fprintf( stderr, "+++++++++++++ addr = 0x%08x\n", *addr ); */
00577 /*      fprintf( stderr, "+++++++++++++ len  = %d\n", *len ); */
00578 
00579     return (pkt - orig_pkt);
00580 }
00581 
00582 static void
00583 gdb_read_memory (GdbComm_T *comm, int fd, char *pkt)
00584 {
00585     int addr = 0;
00586     int len = 0;
00587     uint8_t *buf;
00588     uint8_t bval;
00589     uint16_t wval;
00590     int i;
00591     int is_odd_addr;
00592 
00593     pkt += gdb_get_addr_len (pkt, ',', '\0', &addr, &len);
00594 
00595     buf = avr_new0 (uint8_t, (len * 2) + 1);
00596 
00597     if ((addr & MEM_SPACE_MASK) == SRAM_OFFSET)
00598     {
00599         /* addressing sram */
00600 
00601         addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00602 
00603         /* Return an error to gdb if it tries to read or write any of the 32
00604            general purpse registers. This allows gdb to know when a zero
00605            pointer has been dereferenced. */
00606 
00607         /* FIXME: [TRoth 2002/03/31] This isn't working quite the way I
00608            thought it would so I've removed it for now. */
00609 
00610         /* if ( (addr >= 0) && (addr < 32) ) */
00611         if (0)
00612         {
00613             snprintf (buf, len * 2, "E%02x", EIO);
00614         }
00615         else
00616         {
00617             for (i = 0; i < len; i++)
00618             {
00619                 bval = comm->read_sram (comm->user_data, addr + i);
00620                 buf[i * 2] = HEX_DIGIT[bval >> 4];
00621                 buf[i * 2 + 1] = HEX_DIGIT[bval & 0xf];
00622             }
00623         }
00624     }
00625     else if ((addr & MEM_SPACE_MASK) == FLASH_OFFSET)
00626     {
00627         /* addressing flash */
00628 
00629         addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00630 
00631         is_odd_addr = addr % 2;
00632         i = 0;
00633 
00634         if (is_odd_addr)
00635         {
00636             bval = comm->read_flash (comm->user_data, addr / 2) >> 8;
00637             buf[i++] = HEX_DIGIT[bval >> 4];
00638             buf[i++] = HEX_DIGIT[bval & 0xf];
00639             addr++;
00640             len--;
00641         }
00642 
00643         while (len > 1)
00644         {
00645             wval = comm->read_flash (comm->user_data, addr / 2);
00646 
00647             bval = wval & 0xff;
00648             buf[i++] = HEX_DIGIT[bval >> 4];
00649             buf[i++] = HEX_DIGIT[bval & 0xf];
00650 
00651             bval = wval >> 8;
00652             buf[i++] = HEX_DIGIT[bval >> 4];
00653             buf[i++] = HEX_DIGIT[bval & 0xf];
00654 
00655             len -= 2;
00656             addr += 2;
00657         }
00658 
00659         if (len == 1)
00660         {
00661             bval = comm->read_flash (comm->user_data, addr / 2) & 0xff;
00662             buf[i++] = HEX_DIGIT[bval >> 4];
00663             buf[i++] = HEX_DIGIT[bval & 0xf];
00664         }
00665     }
00666 #if defined(USE_EEPROM_SPACE)
00667     else if ((addr & MEM_SPACE_MASK) == EEPROM_OFFSET)
00668     {
00669         /* addressing eeprom */
00670 
00671         addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00672 
00673         avr_warning ("reading of eeprom not yet implemented: 0x%x.\n", addr);
00674         snprintf (buf, len * 2, "E%02x", EIO);
00675     }
00676 #endif
00677     else
00678     {
00679         /* gdb asked for memory space which doesn't exist */
00680         avr_warning ("Invalid memory address: 0x%x.\n", addr);
00681         snprintf (buf, len * 2, "E%02x", EIO);
00682     }
00683 
00684     gdb_send_reply (fd, buf);
00685 
00686     avr_free (buf);
00687 }
00688 
00689 static void
00690 gdb_write_memory (GdbComm_T *comm, int fd, char *pkt)
00691 {
00692     int addr = 0;
00693     int len = 0;
00694     uint8_t bval;
00695     uint16_t wval;
00696     int is_odd_addr;
00697     int i;
00698     char reply[10];
00699 
00700     /* Set the default reply. */
00701     strncpy (reply, "OK", sizeof (reply));
00702 
00703     pkt += gdb_get_addr_len (pkt, ',', ':', &addr, &len);
00704 
00705     if ((addr & MEM_SPACE_MASK) == SRAM_OFFSET)
00706     {
00707         /* addressing sram */
00708 
00709         addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00710 
00711         /* Return error. See gdb_read_memory for reasoning. */
00712         /* FIXME: [TRoth 2002/03/31] This isn't working quite the way I
00713            thought it would so I've removed it for now. */
00714         /* if ( (addr >= 0) && (addr < 32) ) */
00715         if (0)
00716         {
00717             snprintf (reply, sizeof (reply), "E%02x", EIO);
00718         }
00719         else
00720         {
00721             for (i = addr; i < addr + len; i++)
00722             {
00723                 bval = hex2nib (*pkt++) << 4;
00724                 bval += hex2nib (*pkt++);
00725                 comm->write_sram (comm->user_data, i, bval);
00726             }
00727         }
00728     }
00729     else if ((addr & MEM_SPACE_MASK) == FLASH_OFFSET)
00730     {
00731         /* addressing flash */
00732 
00733         /* Some targets might not allow writing to flash */
00734 
00735         if (comm->write_flash && comm->write_flash_lo8
00736             && comm->write_flash_hi8)
00737         {
00738             addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00739 
00740             is_odd_addr = addr % 2;
00741 
00742             if (is_odd_addr)
00743             {
00744                 bval = hex2nib (*pkt++) << 4;
00745                 bval += hex2nib (*pkt++);
00746                 comm->write_flash_hi8 (comm->user_data, addr / 2, bval);
00747                 len--;
00748                 addr++;
00749             }
00750 
00751             while (len > 1)
00752             {
00753                 wval = hex2nib (*pkt++) << 4; /* low byte first */
00754                 wval += hex2nib (*pkt++);
00755                 wval += hex2nib (*pkt++) << 12; /* high byte last */
00756                 wval += hex2nib (*pkt++) << 8;
00757                 comm->write_flash (comm->user_data, addr / 2, wval);
00758                 len -= 2;
00759                 addr += 2;
00760             }
00761 
00762             if (len == 1)
00763             {
00764                 /* one more byte to write */
00765                 bval = hex2nib (*pkt++) << 4;
00766                 bval += hex2nib (*pkt++);
00767                 comm->write_flash_lo8 (comm->user_data, addr / 2, bval);
00768             }
00769         }
00770         else
00771         {
00772             /* target can't write to flash, so complain to gdb */
00773             avr_warning ("Gdb asked to write to flash and target can't.\n");
00774             snprintf (reply, sizeof (reply), "E%02x", EIO);
00775         }
00776     }
00777 #if defined (USE_EEPROM_SPACE)
00778     else if ((addr & MEM_SPACE_MASK) == EEPROM_OFFSET)
00779     {
00780         /* addressing eeprom */
00781 
00782         addr = addr & ~MEM_SPACE_MASK; /* remove the offset bits */
00783 
00784         avr_warning ("writing of eeprom not yet implemented: 0x%x.\n", addr);
00785         snprintf (reply, sizeof (reply), "E%02x", EIO);
00786     }
00787 #endif
00788     else
00789     {
00790         /* gdb asked for memory space which doesn't exist */
00791         avr_warning ("Invalid memory address: 0x%x.\n", addr);
00792         snprintf (reply, sizeof (reply), "E%02x", EIO);
00793     }
00794 
00795     gdb_send_reply (fd, reply);
00796 }
00797 
00798 /* Format of breakpoint commands (both insert and remove):
00799 
00800    "z<t>,<addr>,<length>"  -  remove break/watch point
00801    "Z<t>,<add>r,<length>"  -  insert break/watch point
00802 
00803    In both cases t can be the following:
00804    t = '0'  -  software breakpoint
00805    t = '1'  -  hardware breakpoint
00806    t = '2'  -  write watch point
00807    t = '3'  -  read watch point
00808    t = '4'  -  access watch point
00809 
00810    addr is address.
00811    length is in bytes
00812 
00813    For a software breakpoint, length specifies the size of the instruction to
00814    be patched. For hardware breakpoints and watchpoints, length specifies the
00815    memory region to be monitored. To avoid potential problems, the operations
00816    should be implemented in an idempotent way. -- GDB 5.0 manual. */
00817 
00818 static void
00819 gdb_break_point (GdbComm_T *comm, int fd, char *pkt)
00820 {
00821     int addr = 0;
00822     int len = 0;
00823 
00824     char z = *(pkt - 1);        /* get char parser already looked at */
00825     char t = *pkt++;
00826     pkt++;                      /* skip over first ',' */
00827 
00828     gdb_get_addr_len (pkt, ',', '\0', &addr, &len);
00829 
00830     switch (t)
00831     {
00832         case '0':              /* software breakpoint */
00833             /* addr/2 since addr refers to PC */
00834             if (comm->max_pc
00835                 && ((addr / 2) >= comm->max_pc (comm->user_data)))
00836             {
00837                 avr_warning ("Attempt to set break at invalid addr\n");
00838                 gdb_send_reply (fd, "E01");
00839                 return;
00840             }
00841 
00842             if (z == 'z')
00843                 comm->remove_break (comm->user_data, addr / 2);
00844             else
00845                 comm->insert_break (comm->user_data, addr / 2);
00846             break;
00847 
00848         case '1':              /* hardware breakpoint */
00849         case '2':              /* write watchpoint */
00850         case '3':              /* read watchpoint */
00851         case '4':              /* access watchpoint */
00852             gdb_send_reply (fd, "");
00853             return;             /* unsupported yet */
00854     }
00855 
00856     gdb_send_reply (fd, "OK");
00857 }
00858 
00859 /* Handle an io registers query. Query has two forms:
00860    "avr.io_reg" and "avr.io_reg:addr,len".
00861 
00862    The "avr.io_reg" has already been stripped off at this point. 
00863 
00864    The first form means, "return the number of io registers for this target
00865    device." Second form means, "send data len io registers starting with
00866    register addr." */
00867 
00868 static void
00869 gdb_fetch_io_registers (GdbComm_T *comm, int fd, char *pkt)
00870 {
00871     int addr, len;
00872     int i;
00873     uint8_t val;
00874     char reply[400];
00875     char reg_name[80];
00876     int pos = 0;
00877 
00878     if (comm->io_fetch)
00879     {
00880         if (pkt[0] == '\0')
00881         {
00882             /* gdb is asking how many io registers the device has. */
00883             gdb_send_reply (fd, "40");
00884         }
00885 
00886         else if (pkt[0] == ':')
00887         {
00888             /* gdb is asking for io registers addr to (addr + len) */
00889 
00890             gdb_get_addr_len (pkt + 1, ',', '\0', &addr, &len);
00891 
00892             memset (reply, '\0', sizeof (reply));
00893 
00894             for (i = 0; i < len; i++)
00895             {
00896                 comm->io_fetch (comm->user_data, addr + i, &val, reg_name,
00897                                 sizeof (reg_name));
00898                 pos +=
00899                     snprintf (reply + pos, sizeof (reply) - pos, "%s,%x;",
00900                               reg_name, val);
00901             }
00902 
00903             gdb_send_reply (fd, reply); /* do nothing for now */
00904         }
00905 
00906         else
00907             gdb_send_reply (fd, "E01"); /* An error occurred */
00908 
00909     }
00910 
00911     else
00912         gdb_send_reply (fd, ""); /* tell gdb we don't handle info io
00913                                     command. */
00914 }
00915 
00916 /* Dispatch various query request to specific handler functions. If a query is
00917    not handled, send an empry reply. */
00918 
00919 static void
00920 gdb_query_request (GdbComm_T *comm, int fd, char *pkt)
00921 {
00922     int len;
00923 
00924     switch (*pkt++)
00925     {
00926         case 'R':
00927             len = strlen ("avr.io_reg");
00928             if (strncmp (pkt, "avr.io_reg", len) == 0)
00929             {
00930                 gdb_fetch_io_registers (comm, fd, pkt + len);
00931                 return;
00932             }
00933     }
00934 
00935     gdb_send_reply (fd, "");
00936 }
00937 
00938 /* Continue command format: "c<addr>" or "s<addr>"
00939 
00940    If addr is given, resume at that address, otherwise, resume at current
00941    address. */
00942 
00943 static void
00944 gdb_continue (GdbComm_T *comm, int fd, char *pkt)
00945 {
00946     char reply[MAX_BUF + 1];
00947     int res;
00948     int pc;
00949     char step = *(pkt - 1);     /* called from 'c' or 's'? */
00950     int signo = SIGTRAP;
00951 
00952     static int is_running = 0;
00953 
00954     /* This allows gdb_continue to be reentrant while it's running. */
00955     if (is_running == 1)
00956     {
00957         return;
00958     }
00959     is_running = 1;
00960 
00961     memset (reply, 0, sizeof (reply));
00962 
00963     if (*pkt != '\0')
00964     {
00965         /* NOTE: from what I've read on the gdb lists, gdb never uses the
00966            "continue at address" functionality. That may change, so let's
00967            catch that case. */
00968 
00969         /* get addr to resume at */
00970         avr_error ("attempt to resume at other than current");
00971     }
00972 
00973     while (1)
00974     {
00975         if (signal_has_occurred (SIGINT))
00976         {
00977             global_server_quit = 1;
00978             break;
00979         }
00980 
00981         res = comm->step (comm->user_data);
00982 
00983         if (res == BREAK_POINT)
00984         {
00985             if (comm->disable_breakpts)
00986                 comm->disable_breakpts (comm->user_data);
00987             break;
00988         }
00989 
00990         /* check if gdb sent any messages */
00991         res = gdb_pre_parse_packet (comm, fd, GDB_BLOCKING_OFF);
00992         if (res < 0)
00993         {
00994             if (res == GDB_RET_CTRL_C)
00995             {
00996                 signo = SIGINT;
00997             }
00998             break;
00999         }
01000 
01001         /* If called from 's' or 'S', only want to step once */
01002         if ((step == 's') || (step == 'S'))
01003             break;
01004     }
01005 
01006     /* If reply hasn't been set, respond as if a breakpoint was hit. */
01007     if (reply[0] == '\0')
01008     {
01009         /* Send gdb SREG, SP, PC */
01010         int bytes = 0;
01011 
01012         pc = comm->read_pc (comm->user_data) * 2;
01013 
01014         bytes = snprintf (reply, MAX_BUF, "T%02x", signo);
01015 
01016         /* SREG, SP & PC */
01017         snprintf (reply + bytes, MAX_BUF - bytes,
01018                   "20:%02x;" "21:%02x%02x;" "22:%02x%02x%02x%02x;",
01019                   comm->read_sreg (comm->user_data),
01020                   comm->read_sram (comm->user_data, SPL_ADDR),
01021                   comm->read_sram (comm->user_data, SPH_ADDR), pc & 0xff,
01022                   (pc >> 8) & 0xff, (pc >> 16) & 0xff, (pc >> 24) & 0xff);
01023     }
01024 
01025     gdb_send_reply (fd, reply);
01026 
01027     is_running = 0;
01028 }
01029 
01030 /* Continue with signal command format: "C<sig>;<addr>" or "S<sig>;<addr>"
01031    "<sig>" should always be 2 hex digits, possibly zero padded.
01032    ";<addr>" part is optional.
01033 
01034    If addr is given, resume at that address, otherwise, resume at current
01035    address. */
01036 
01037 static void
01038 gdb_continue_with_signal (GdbComm_T *comm, int fd, char *pkt)
01039 {
01040     int signo;
01041     char step = *(pkt - 1);
01042 
01043     /* strip out the signal part of the packet */
01044 
01045     signo = (hex2nib (*pkt++) << 4);
01046     signo += (hex2nib (*pkt++) & 0xf);
01047 
01048     if (global_debug_on)
01049         fprintf (stderr, "GDB sent signal: %d\n", signo);
01050 
01051     /* Process signals send via remote protocol from gdb. Signals really don't
01052        make sense to the program running in the simulator, so we are using
01053        them sort of as an 'out of band' data. */
01054 
01055     switch (signo)
01056     {
01057         case SIGHUP:
01058             /* Gdb user issuing the 'signal SIGHUP' command tells sim to reset
01059                itself. We reply with a SIGTRAP the same as we do when gdb
01060                makes first connection with simulator. */
01061             comm->reset (comm->user_data);
01062             gdb_send_reply (fd, "S05");
01063             return;
01064         default:
01065             /* Gdb user issuing the 'signal <signum>' command where signum is
01066                >= 80 is interpreted as a request to trigger an interrupt  
01067                vector. The vector to trigger is signo-80. 
01068                (there's an offset of 14)  */
01069             if (signo >= 94)
01070             {
01071                 if (comm->irq_raise)
01072                 {
01073                     comm->irq_raise (comm->user_data, signo - 94);
01074                 }
01075             }
01076     }
01077 
01078     /* Modify pkt to look like what gdb_continue() expects and send it to
01079        gdb_continue(): *pkt should now be either '\0' or ';' */
01080 
01081     if (*pkt == '\0')
01082     {
01083         *(pkt - 1) = step;
01084     }
01085     else if (*pkt == ';')
01086     {
01087         *pkt = step;
01088     }
01089     else
01090     {
01091         avr_warning ("Malformed packet: \"%s\"\n", pkt);
01092         gdb_send_reply (fd, "");
01093         return;
01094     }
01095 
01096     gdb_continue (comm, fd, pkt);
01097 }
01098 
01099 /* Parse the packet. Assumes that packet is null terminated.
01100    Return GDB_RET_KILL_REQUEST if packet is 'kill' command,
01101    GDB_RET_OK otherwise. */
01102 
01103 static int
01104 gdb_parse_packet (GdbComm_T *comm, int fd, char *pkt)
01105 {
01106     switch (*pkt++)
01107     {
01108         case '?':              /* last signal */
01109             gdb_send_reply (fd, "S05"); /* signal # 5 is SIGTRAP */
01110             break;
01111 
01112         case 'g':              /* read registers */
01113             gdb_read_registers (comm, fd);
01114             break;
01115 
01116         case 'G':              /* write registers */
01117             gdb_write_registers (comm, fd, pkt);
01118             break;
01119 
01120         case 'p':              /* read a single register */
01121             gdb_read_register (comm, fd, pkt);
01122             break;
01123 
01124         case 'P':              /* write single register */
01125             gdb_write_register (comm, fd, pkt);
01126             break;
01127 
01128         case 'm':              /* read memory */
01129             gdb_read_memory (comm, fd, pkt);
01130             break;
01131 
01132         case 'M':              /* write memory */
01133             gdb_write_memory (comm, fd, pkt);
01134             break;
01135 
01136         case 'k':              /* kill request */
01137         case 'D':              /* Detach request */
01138             /* Reset the simulator since there may be another connection
01139                before the simulator stops running. */
01140 
01141             comm->reset (comm->user_data);
01142             gdb_send_reply (fd, "OK");
01143             return GDB_RET_KILL_REQUEST;
01144 
01145         case 'C':              /* continue with signal */
01146         case 'S':              /* step with signal */
01147             gdb_continue_with_signal (comm, fd, pkt);
01148             break;
01149 
01150         case 'c':              /* continue */
01151         case 's':              /* step */
01152             gdb_continue (comm, fd, pkt);
01153             break;
01154 
01155         case 'z':              /* remove break/watch point */
01156         case 'Z':              /* insert break/watch point */
01157             gdb_break_point (comm, fd, pkt);
01158             break;
01159 
01160         case 'q':              /* query requests */
01161             gdb_query_request (comm, fd, pkt);
01162             break;
01163 
01164         default:
01165             gdb_send_reply (fd, "");
01166     }
01167 
01168     return GDB_RET_OK;
01169 }
01170 
01171 static void
01172 gdb_set_blocking_mode (int fd, int mode)
01173 {
01174     if (mode)
01175     {
01176         /* turn non-blocking mode off */
01177         if (fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) & ~O_NONBLOCK) < 0)
01178             avr_warning ("fcntl failed: %s\n", strerror (errno));
01179     }
01180     else
01181     {
01182         /* turn non-blocking mode on */
01183         if (fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK) < 0)
01184             avr_warning ("fcntl failed: %s\n", strerror (errno));
01185     }
01186 }
01187 
01188 /* Perform pre-packet parsing. This will handle messages from gdb which are
01189    outside the realm of packets or prepare a packet for parsing.
01190 
01191    Use the static block_on flag to reduce the over head of turning blocking on
01192    and off every time this function is called. */
01193 
01194 static int
01195 gdb_pre_parse_packet (GdbComm_T *comm, int fd, int blocking)
01196 {
01197     int i, res;
01198     int c;
01199     char pkt_buf[MAX_BUF + 1];
01200     int cksum, pkt_cksum;
01201     static int block_on = 1;    /* default is blocking mode */
01202 
01203     if (block_on != blocking)
01204     {
01205         gdb_set_blocking_mode (fd, blocking);
01206         block_on = blocking;
01207     }
01208 
01209     c = gdb_read_byte (fd);
01210 
01211     switch (c)
01212     {
01213         case '$':              /* read a packet */
01214             /* insure that packet is null terminated. */
01215             memset (pkt_buf, 0, sizeof (pkt_buf));
01216 
01217             /* make sure we block on fd */
01218             gdb_set_blocking_mode (fd, GDB_BLOCKING_ON);
01219 
01220             pkt_cksum = i = 0;
01221             c = gdb_read_byte (fd);
01222             while ((c != '#') && (i < MAX_BUF))
01223             {
01224                 pkt_buf[i++] = c;
01225                 pkt_cksum += (unsigned char)c;
01226                 c = gdb_read_byte (fd);
01227             }
01228 
01229             cksum = hex2nib (gdb_read_byte (fd)) << 4;
01230             cksum |= hex2nib (gdb_read_byte (fd));
01231 
01232             /* FIXME: Should send "-" (Nak) instead of aborting when we get
01233                checksum errors. Leave this as an error until it is actually
01234                seen (I've yet to see a bad checksum - TRoth). It's not a
01235                simple matter of sending (Nak) since you don't want to get into
01236                an infinite loop of (bad cksum, nak, resend, repeat). */
01237 
01238             if ((pkt_cksum & 0xff) != cksum)
01239                 avr_error ("Bad checksum: sent 0x%x <--> computed 0x%x",
01240                            cksum, pkt_cksum);
01241 
01242             if (global_debug_on)
01243                 fprintf (stderr, "Recv: \"$%s#%02x\"\n", pkt_buf, cksum);
01244 
01245             /* always acknowledge a well formed packet immediately */
01246             gdb_send_ack (fd);
01247 
01248             res = gdb_parse_packet (comm, fd, pkt_buf);
01249             if (res < 0)
01250                 return res;
01251 
01252             break;
01253 
01254         case '-':
01255             if (global_debug_on)
01256                 fprintf (stderr, " gdb -> Nak\n");
01257             gdb_send_reply (fd, gdb_last_reply (NULL));
01258             break;
01259 
01260         case '+':
01261             if (global_debug_on)
01262                 fprintf (stderr, " gdb -> Ack\n");
01263             break;
01264 
01265         case 0x03:
01266             /* Gdb sends this when the user hits C-c. This is gdb's way of
01267                telling the simulator to interrupt what it is doing and return
01268                control back to gdb. */
01269             return GDB_RET_CTRL_C;
01270 
01271         case -1:
01272             /* fd is non-blocking and no data to read */
01273             break;
01274 
01275         default:
01276             avr_warning ("Unknown request from gdb: %c (0x%02x)\n", c, c);
01277     }
01278 
01279     return GDB_RET_OK;
01280 }
01281 
01282 static void
01283 gdb_main_loop (GdbComm_T *comm, int fd)
01284 {
01285     int res;
01286     char reply[MAX_BUF];
01287 
01288     while (1)
01289     {
01290         res = gdb_pre_parse_packet (comm, fd, GDB_BLOCKING_ON);
01291         switch (res)
01292         {
01293             case GDB_RET_KILL_REQUEST:
01294                 return;
01295 
01296             case GDB_RET_CTRL_C:
01297                 gdb_send_ack (fd);
01298                 snprintf (reply, MAX_BUF, "S%02x", SIGINT);
01299                 gdb_send_reply (fd, reply);
01300                 break;
01301 
01302             default:
01303                 break;
01304         }
01305     }
01306 }
01307 
01308 /**
01309  * \brief Start interacting with gdb.
01310  * \param comm     A previously initialized simulator comm
01311  * \param port     Port which server will listen for connections on.
01312  * \param debug_on Turn on gdb debug diagnostic messages.
01313  * 
01314  * Start a tcp server socket on localhost listening for connections on the
01315  * given port. Once a connection is established, enter an infinite loop and
01316  * process command requests from gdb using the remote serial protocol. Only a
01317  * single connection is allowed at a time.
01318  */
01319 void
01320 gdb_interact (GdbComm_T *comm, int port, int debug_on)
01321 {
01322     struct sockaddr_in address[1];
01323     int sock, conn, i;
01324     socklen_t addrLength[1];
01325 
01326     global_debug_on = debug_on;
01327 
01328     if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
01329         avr_error ("Can't create socket: %s", strerror (errno));
01330 
01331     /* Let the kernel reuse the socket address. This lets us run
01332        twice in a row, without waiting for the (ip, port) tuple
01333        to time out. */
01334     i = 1;
01335     setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i));
01336 
01337     address->sin_family = AF_INET;
01338     address->sin_port = htons (port);
01339     memset (&address->sin_addr, 0, sizeof (address->sin_addr));
01340 
01341     if (bind (sock, (struct sockaddr *)address, sizeof (address)))
01342         avr_error ("Can not bind socket: %s", strerror (errno));
01343 
01344     signal_watch_start (SIGINT);
01345 
01346     while (global_server_quit == 0)
01347     {
01348         if (listen (sock, 1))
01349         {
01350             int saved_errno = errno;
01351 
01352             if (signal_has_occurred (SIGINT))
01353             {
01354                 break;          /* SIGINT will cause listen to be
01355                                    interrupted */
01356             }
01357             avr_error ("Can not listen on socket: %s",
01358                        strerror (saved_errno));
01359         }
01360 
01361         fprintf (stderr, "Waiting on port %d for gdb client to connect...\n",
01362                  port);
01363 
01364         /* accept() needs this set, or it fails (sometimes) */
01365         addrLength[0] = sizeof (struct sockaddr);
01366 
01367         /* We only want to accept a single connection, thus don't need a
01368            loop. */
01369         conn = accept (sock, (struct sockaddr *)address, addrLength);
01370         if (conn < 0)
01371         {
01372             int saved_errno = errno;
01373 
01374             if (signal_has_occurred (SIGINT))
01375             {
01376                 break;          /* SIGINT will cause accept to be
01377                                    interrupted */
01378             }
01379             avr_error ("Accept connection failed: %s",
01380                        strerror (saved_errno));
01381         }
01382 
01383         /* Tell TCP not to delay small packets.  This greatly speeds up
01384            interactive response. WARNING: If TCP_NODELAY is set on, then gdb
01385            may timeout in mid-packet if the (gdb)packet is not sent within a
01386            single (tcp)packet, thus all outgoing (gdb)packets _must_ be sent
01387            with a single call to write. (see Stevens "Unix Network
01388            Programming", Vol 1, 2nd Ed, page 202 for more info) */
01389 
01390         i = 1;
01391         setsockopt (conn, IPPROTO_TCP, TCP_NODELAY, &i, sizeof (i));
01392 
01393         /* If we got this far, we now have a client connected and can start 
01394            processing. */
01395 
01396         fprintf (stderr, "Connection opened by host %s, port %hd.\n",
01397                  inet_ntoa (address->sin_addr), ntohs (address->sin_port));
01398 
01399         gdb_main_loop (comm, conn);
01400 
01401         comm->reset (comm->user_data);
01402 
01403         close (conn);
01404 
01405         /* FIXME: How do we correctly break out of this loop? This keeps the
01406            simulator server up so you don't have to restart it with every gdb
01407            session. To exit the server loop, you have to hit C-c or send some
01408            signal which causes the program to terminate, in which case, you
01409            won't get a dump of the simulator's state. This might actually be
01410            acceptable behavior. */
01411         if (signal_has_occurred (SIGINT))
01412         {
01413             break;
01414         }
01415     }
01416 
01417     signal_watch_stop (SIGINT);
01418 
01419     close (sock);
01420 }

Automatically generated by Doxygen 1.5.2 on 1 Dec 2007.