The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/gdb/gdb_packet.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2004 Marcel Moolenaar
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  *
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/ctype.h>
   35 #include <sys/kdb.h>
   36 #include <sys/libkern.h>
   37 #include <sys/ttydefaults.h>
   38 
   39 #include <machine/gdb_machdep.h>
   40 #include <machine/kdb.h>
   41 
   42 #include <gdb/gdb.h>
   43 #include <gdb/gdb_int.h>
   44 
   45 static char gdb_rxbuf[GDB_BUFSZ];
   46 char *gdb_rxp = NULL;
   47 size_t gdb_rxsz = 0;
   48 static char gdb_txbuf[GDB_BUFSZ];
   49 char *gdb_txp = NULL;                   /* Used in inline functions. */
   50 
   51 #define C2N(c)  (((c) < 'A') ? (c) - '' : \
   52             10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
   53 #define N2C(n)  (((n) < 10) ? (n) + '' : (n) + 'a' - 10)
   54 
   55 /*
   56  * Get a single character
   57  */
   58 
   59 static int
   60 gdb_getc(void)
   61 {
   62         int c;
   63 
   64         do
   65                 c = gdb_cur->gdb_getc();
   66         while (c == -1);
   67 
   68         if (c == CTRL('C')) {
   69                 printf("Received ^C; trying to switch back to ddb.\n");
   70 
   71                 if (kdb_dbbe_select("ddb") != 0)
   72                         printf("The ddb backend could not be selected.\n");
   73                 else {
   74                         printf("using longjmp, hope it works!\n");
   75                         kdb_reenter();
   76                 }
   77         }
   78         return (c);
   79 }
   80 
   81 /*
   82  * Functions to receive and extract from a packet.
   83  */
   84 
   85 int
   86 gdb_rx_begin(void)
   87 {
   88         int c, cksum;
   89 
   90         gdb_rxp = NULL;
   91         do {
   92                 /*
   93                  * Wait for the start character, ignore all others.
   94                  * XXX needs a timeout.
   95                  */
   96                 while ((c = gdb_getc()) != '$')
   97                         ;
   98 
   99                 /* Read until a # or end of buffer is found. */
  100                 cksum = 0;
  101                 gdb_rxsz = 0;
  102                 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
  103                         c = gdb_getc();
  104                         if (c == '#')
  105                                 break;
  106                         gdb_rxbuf[gdb_rxsz++] = c;
  107                         cksum += c;
  108                 }
  109                 gdb_rxbuf[gdb_rxsz] = 0;
  110                 cksum &= 0xff;
  111 
  112                 /* Bail out on a buffer overflow. */
  113                 if (c != '#') {
  114                         gdb_cur->gdb_putc('-');
  115                         return (ENOSPC);
  116                 }
  117 
  118                 c = gdb_getc();
  119                 cksum -= (C2N(c) << 4) & 0xf0;
  120                 c = gdb_getc();
  121                 cksum -= C2N(c) & 0x0f;
  122                 gdb_cur->gdb_putc((cksum == 0) ? '+' : '-');
  123                 if (cksum != 0)
  124                         printf("GDB: packet `%s' has invalid checksum\n",
  125                             gdb_rxbuf);
  126         } while (cksum != 0);
  127 
  128         gdb_rxp = gdb_rxbuf;
  129         return (0);
  130 }
  131 
  132 int
  133 gdb_rx_equal(const char *str)
  134 {
  135         int len;
  136 
  137         len = strlen(str);
  138         if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
  139                 return (0);
  140         gdb_rxp += len;
  141         gdb_rxsz -= len;
  142         return (1);
  143 }
  144 
  145 int
  146 gdb_rx_mem(unsigned char *addr, size_t size)
  147 {
  148         unsigned char *p;
  149         void *prev;
  150         void *wctx;
  151         jmp_buf jb;
  152         size_t cnt;
  153         int ret;
  154         unsigned char c;
  155 
  156         if (size * 2 != gdb_rxsz)
  157                 return (-1);
  158 
  159         wctx = gdb_begin_write();
  160         prev = kdb_jmpbuf(jb);
  161         ret = setjmp(jb);
  162         if (ret == 0) {
  163                 p = addr;
  164                 cnt = size;
  165                 while (cnt-- > 0) {
  166                         c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
  167                         c |= C2N(gdb_rxp[1]) & 0x0f;
  168                         *p++ = c;
  169                         gdb_rxsz -= 2;
  170                         gdb_rxp += 2;
  171                 }
  172                 kdb_cpu_sync_icache(addr, size);
  173         }
  174         (void)kdb_jmpbuf(prev);
  175         gdb_end_write(wctx);
  176         return ((ret == 0) ? 1 : 0);
  177 }
  178 
  179 int
  180 gdb_rx_varhex(uintmax_t *vp)
  181 {
  182         uintmax_t v;
  183         int c, neg;
  184 
  185         c = gdb_rx_char();
  186         neg = (c == '-') ? 1 : 0;
  187         if (neg == 1)
  188                 c = gdb_rx_char();
  189         if (!isxdigit(c)) {
  190                 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
  191                 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
  192                 return (-1);
  193         }
  194         v = 0;
  195         do {
  196                 v <<= 4;
  197                 v += C2N(c);
  198                 c = gdb_rx_char();
  199         } while (isxdigit(c));
  200         if (c != -1) {
  201                 gdb_rxp--;
  202                 gdb_rxsz++;
  203         }
  204         *vp = (neg) ? -v : v;
  205         return (0);
  206 }
  207 
  208 /*
  209  * Function to build and send a package.
  210  */
  211 
  212 void
  213 gdb_tx_begin(char tp)
  214 {
  215 
  216         gdb_txp = gdb_txbuf;
  217         if (tp != '\0')
  218                 gdb_tx_char(tp);
  219 }
  220 
  221 int
  222 gdb_tx_end(void)
  223 {
  224         const char *p;
  225         int runlen;
  226         unsigned char c, cksum;
  227 
  228         do {
  229                 gdb_cur->gdb_putc('$');
  230 
  231                 cksum = 0;
  232                 p = gdb_txbuf;
  233                 while (p < gdb_txp) {
  234                         /* Send a character and start run-length encoding. */
  235                         c = *p++;
  236                         gdb_cur->gdb_putc(c);
  237                         cksum += c;
  238                         runlen = 0;
  239                         /* Determine run-length and update checksum. */
  240                         while (p < gdb_txp && *p == c) {
  241                                 runlen++;
  242                                 p++;
  243                         }
  244                         /* Emit the run-length encoded string. */
  245                         while (runlen >= 97) {
  246                                 gdb_cur->gdb_putc('*');
  247                                 cksum += '*';
  248                                 gdb_cur->gdb_putc(97+29);
  249                                 cksum += 97+29;
  250                                 runlen -= 97;
  251                                 if (runlen > 0) {
  252                                         gdb_cur->gdb_putc(c);
  253                                         cksum += c;
  254                                         runlen--;
  255                                 }
  256                         }
  257                         /* Don't emit '$', '#', '+', '-' or a run length below 3. */
  258                         while (runlen == 1 || runlen == 2 ||
  259                             runlen + 29 == '$' || runlen + 29 == '#' ||
  260                             runlen + 29 == '+' || runlen + 29 == '-') {
  261                                 gdb_cur->gdb_putc(c);
  262                                 cksum += c;
  263                                 runlen--;
  264                         }
  265                         if (runlen == 0)
  266                                 continue;
  267                         gdb_cur->gdb_putc('*');
  268                         cksum += '*';
  269                         gdb_cur->gdb_putc(runlen+29);
  270                         cksum += runlen+29;
  271                 }
  272 
  273                 gdb_cur->gdb_putc('#');
  274                 c = cksum >> 4;
  275                 gdb_cur->gdb_putc(N2C(c));
  276                 c = cksum & 0x0f;
  277                 gdb_cur->gdb_putc(N2C(c));
  278 
  279                 c = gdb_getc();
  280         } while (c != '+');
  281 
  282         return (0);
  283 }
  284 
  285 int
  286 gdb_tx_mem(const unsigned char *addr, size_t size)
  287 {
  288         void *prev;
  289         jmp_buf jb;
  290         int ret;
  291 
  292         prev = kdb_jmpbuf(jb);
  293         ret = setjmp(jb);
  294         if (ret == 0) {
  295                 while (size-- > 0) {
  296                         *gdb_txp++ = N2C(*addr >> 4);
  297                         *gdb_txp++ = N2C(*addr & 0x0f);
  298                         addr++;
  299                 }
  300         }
  301         (void)kdb_jmpbuf(prev);
  302         return ((ret == 0) ? 1 : 0);
  303 }
  304 
  305 void
  306 gdb_tx_reg(int regnum)
  307 {
  308         unsigned char *regp;
  309         size_t regsz;
  310 
  311         regp = gdb_cpu_getreg(regnum, &regsz);
  312         if (regp == NULL) {
  313                 /* Register unavailable. */
  314                 while (regsz--) {
  315                         gdb_tx_char('x');
  316                         gdb_tx_char('x');
  317                 }
  318         } else
  319                 gdb_tx_mem(regp, regsz);
  320 }
  321 
  322 /* Read binary data up until the end of the packet or until we have datalen decoded bytes */
  323 int
  324 gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt)
  325 {
  326         int c;
  327 
  328         *amt = 0;
  329 
  330         while (*amt < datalen) {
  331                 c = gdb_rx_char();
  332                 /* End of packet? */
  333                 if (c == -1)
  334                         break;
  335                 /* Escaped character up next */
  336                 if (c == '}') {
  337                         /* Truncated packet? Bail out */
  338                         if ((c = gdb_rx_char()) == -1)
  339                                 return (1);
  340                         c ^= 0x20;
  341                 }
  342                 *(data++) = c & 0xff;
  343                 (*amt)++;
  344         }
  345 
  346         return (0);
  347 }
  348 
  349 int
  350 gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found)
  351 {
  352         void *prev;
  353         jmp_buf jb;
  354         int ret;
  355 
  356         prev = kdb_jmpbuf(jb);
  357         ret = setjmp(jb);
  358         if (ret == 0)
  359                 *found = memmem(addr, size, pat, patlen);
  360 
  361         (void)kdb_jmpbuf(prev);
  362         return ((ret == 0) ? 1 : 0);
  363 }

Cache object: 78a198378b863fc067c7f2246a70de07


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.