00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
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
00069 #ifndef DOXYGEN
00070 enum
00071 {
00072 MAX_BUF = 400,
00073 MAX_READ_RETRY = 10,
00074
00075
00076 #if defined(USE_EEPROM_SPACE)
00077 MEM_SPACE_MASK = 0x00ff0000,
00078
00079 FLASH_OFFSET = 0x00000000,
00080 SRAM_OFFSET = 0x00800000,
00081 EEPROM_OFFSET = 0x00810000,
00082 #else
00083 MEM_SPACE_MASK = 0x00f00000,
00084
00085 FLASH_OFFSET = 0x00000000,
00086 SRAM_OFFSET = 0x00800000,
00087 #endif
00088
00089 GDB_BLOCKING_OFF = 0,
00090 GDB_BLOCKING_ON = 1,
00091
00092 GDB_RET_CTRL_C = -2,
00093
00094 GDB_RET_KILL_REQUEST = -1,
00095 GDB_RET_OK = 0,
00096
00097
00098 SPL_ADDR = 0x5d,
00099 SPH_ADDR = 0x5e,
00100 };
00101 #endif
00102
00103
00104
00105
00106 static char HEX_DIGIT[] = "0123456789abcdef";
00107
00108
00109
00110 static int global_server_quit = 0;
00111
00112
00113 static int global_debug_on;
00114
00115
00116
00117 static int gdb_pre_parse_packet (GdbComm_T *comm, int fd, int blocking);
00118
00119
00120
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
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;
00152 }
00153
00154
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
00169 avr_error ("Invalid hexidecimal digit: 0x%02x", hex);
00170
00171 return 0;
00172 }
00173
00174
00175
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
00185
00186 if (res < 0)
00187 avr_error ("write failed: %s", strerror (errno));
00188
00189
00190
00191
00192 if (res != count)
00193 avr_error ("write only wrote %d of %d bytes", res, count);
00194 }
00195
00196
00197
00198
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
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
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
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
00268 if (bytes == (MAX_BUF - 3))
00269 {
00270
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
00287
00288
00289
00290
00291
00292 static void
00293 gdb_read_registers (GdbComm_T *comm, int fd)
00294 {
00295 int i;
00296 uint32_t val;
00297
00298
00299 size_t buf_sz = (32 + 1 + 2 + 4) * 2 + 1;
00300 char *buf;
00301
00302 buf = avr_new0 (char, buf_sz);
00303
00304
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
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
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
00330
00331
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
00354
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;
00362
00363
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
00372 bval = hex2nib (*pkt++) << 4;
00373 bval += hex2nib (*pkt++);
00374 comm->write_sreg (comm->user_data, bval);
00375
00376
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
00386
00387
00388
00389
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
00408
00409
00410
00411
00412
00413
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;
00422
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
00439
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 {
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)
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)
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)
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
00486
00487
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++;
00497
00498
00499 dval = hex2nib (*pkt++) << 4;
00500 dval += hex2nib (*pkt++);
00501
00502 if ((reg >= 0) && (reg < 33))
00503 {
00504
00505 if (reg == 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
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
00526
00527
00528
00529
00530
00531
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
00554
00555
00556
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
00567 while (*pkt != a_end)
00568 *addr = (*addr << 4) + hex2nib (*pkt++);
00569 pkt++;
00570
00571
00572 while (*pkt != l_end)
00573 *len = (*len << 4) + hex2nib (*pkt++);
00574 pkt++;
00575
00576
00577
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
00600
00601 addr = addr & ~MEM_SPACE_MASK;
00602
00603
00604
00605
00606
00607
00608
00609
00610
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
00628
00629 addr = addr & ~MEM_SPACE_MASK;
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
00670
00671 addr = addr & ~MEM_SPACE_MASK;
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
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
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
00708
00709 addr = addr & ~MEM_SPACE_MASK;
00710
00711
00712
00713
00714
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
00732
00733
00734
00735 if (comm->write_flash && comm->write_flash_lo8
00736 && comm->write_flash_hi8)
00737 {
00738 addr = addr & ~MEM_SPACE_MASK;
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;
00754 wval += hex2nib (*pkt++);
00755 wval += hex2nib (*pkt++) << 12;
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
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
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
00781
00782 addr = addr & ~MEM_SPACE_MASK;
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
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
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
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);
00825 char t = *pkt++;
00826 pkt++;
00827
00828 gdb_get_addr_len (pkt, ',', '\0', &addr, &len);
00829
00830 switch (t)
00831 {
00832 case '0':
00833
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':
00849 case '2':
00850 case '3':
00851 case '4':
00852 gdb_send_reply (fd, "");
00853 return;
00854 }
00855
00856 gdb_send_reply (fd, "OK");
00857 }
00858
00859
00860
00861
00862
00863
00864
00865
00866
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
00883 gdb_send_reply (fd, "40");
00884 }
00885
00886 else if (pkt[0] == ':')
00887 {
00888
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);
00904 }
00905
00906 else
00907 gdb_send_reply (fd, "E01");
00908
00909 }
00910
00911 else
00912 gdb_send_reply (fd, "");
00913
00914 }
00915
00916
00917
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
00939
00940
00941
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);
00950 int signo = SIGTRAP;
00951
00952 static int is_running = 0;
00953
00954
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
00966
00967
00968
00969
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
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
01002 if ((step == 's') || (step == 'S'))
01003 break;
01004 }
01005
01006
01007 if (reply[0] == '\0')
01008 {
01009
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
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
01031
01032
01033
01034
01035
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
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
01052
01053
01054
01055 switch (signo)
01056 {
01057 case SIGHUP:
01058
01059
01060
01061 comm->reset (comm->user_data);
01062 gdb_send_reply (fd, "S05");
01063 return;
01064 default:
01065
01066
01067
01068
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
01079
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
01100
01101
01102
01103 static int
01104 gdb_parse_packet (GdbComm_T *comm, int fd, char *pkt)
01105 {
01106 switch (*pkt++)
01107 {
01108 case '?':
01109 gdb_send_reply (fd, "S05");
01110 break;
01111
01112 case 'g':
01113 gdb_read_registers (comm, fd);
01114 break;
01115
01116 case 'G':
01117 gdb_write_registers (comm, fd, pkt);
01118 break;
01119
01120 case 'p':
01121 gdb_read_register (comm, fd, pkt);
01122 break;
01123
01124 case 'P':
01125 gdb_write_register (comm, fd, pkt);
01126 break;
01127
01128 case 'm':
01129 gdb_read_memory (comm, fd, pkt);
01130 break;
01131
01132 case 'M':
01133 gdb_write_memory (comm, fd, pkt);
01134 break;
01135
01136 case 'k':
01137 case 'D':
01138
01139
01140
01141 comm->reset (comm->user_data);
01142 gdb_send_reply (fd, "OK");
01143 return GDB_RET_KILL_REQUEST;
01144
01145 case 'C':
01146 case 'S':
01147 gdb_continue_with_signal (comm, fd, pkt);
01148 break;
01149
01150 case 'c':
01151 case 's':
01152 gdb_continue (comm, fd, pkt);
01153 break;
01154
01155 case 'z':
01156 case 'Z':
01157 gdb_break_point (comm, fd, pkt);
01158 break;
01159
01160 case 'q':
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
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
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
01189
01190
01191
01192
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;
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 '$':
01214
01215 memset (pkt_buf, 0, sizeof (pkt_buf));
01216
01217
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
01233
01234
01235
01236
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
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
01267
01268
01269 return GDB_RET_CTRL_C;
01270
01271 case -1:
01272
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
01310
01311
01312
01313
01314
01315
01316
01317
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
01332
01333
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;
01355
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
01365 addrLength[0] = sizeof (struct sockaddr);
01366
01367
01368
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;
01377
01378 }
01379 avr_error ("Accept connection failed: %s",
01380 strerror (saved_errno));
01381 }
01382
01383
01384
01385
01386
01387
01388
01389
01390 i = 1;
01391 setsockopt (conn, IPPROTO_TCP, TCP_NODELAY, &i, sizeof (i));
01392
01393
01394
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
01406
01407
01408
01409
01410
01411 if (signal_has_occurred (SIGINT))
01412 {
01413 break;
01414 }
01415 }
01416
01417 signal_watch_stop (SIGINT);
01418
01419 close (sock);
01420 }