iPXE
Data Structures | Enumerations | Functions | Variables
gdbstub.c File Reference

GDB stub for remote debugging. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <byteswap.h>
#include <ipxe/gdbstub.h>
#include "gdbmach.h"

Go to the source code of this file.

Data Structures

struct  gdbstub

Enumerations

enum  { POSIX_EINVAL = 0x1c, SIZEOF_PAYLOAD = 512 }

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void gdbstub_state_new (struct gdbstub *stub, char ch)
static void gdbstub_state_data (struct gdbstub *stub, char ch)
static void gdbstub_state_cksum1 (struct gdbstub *stub, char ch)
static void gdbstub_state_cksum2 (struct gdbstub *stub, char ch)
static void gdbstub_state_wait_ack (struct gdbstub *stub, char ch)
static uint8_t gdbstub_from_hex_digit (char ch)
static uint8_t gdbstub_to_hex_digit (uint8_t b)
static void gdbstub_from_hex_buf (char *dst, char *src, int lenbytes)
static void gdbstub_to_hex_buf (char *dst, char *src, int lenbytes)
static uint8_t gdbstub_cksum (char *data, int len)
static void gdbstub_tx_packet (struct gdbstub *stub)
static void gdbstub_send_ok (struct gdbstub *stub)
static void gdbstub_send_num_packet (struct gdbstub *stub, char reply, int num)
static int gdbstub_get_packet_args (struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx)
static void gdbstub_send_errno (struct gdbstub *stub, int errno)
static void gdbstub_report_signal (struct gdbstub *stub)
static void gdbstub_read_regs (struct gdbstub *stub)
static void gdbstub_write_regs (struct gdbstub *stub)
static void gdbstub_read_mem (struct gdbstub *stub)
static void gdbstub_write_mem (struct gdbstub *stub)
static void gdbstub_continue (struct gdbstub *stub, int single_step)
static void gdbstub_breakpoint (struct gdbstub *stub)
static void gdbstub_rx_packet (struct gdbstub *stub)
static void gdbstub_parse (struct gdbstub *stub, char ch)
void gdbstub_handler (int signo, gdbreg_t *regs)
 Interrupt handler.
struct gdb_transportfind_gdb_transport (const char *name)
 Look up GDB transport by name.
void gdbstub_start (struct gdb_transport *trans)
 Break into the debugger using the given transport.

Variables

static struct gdbstub stub

Detailed Description

GDB stub for remote debugging.

Definition in file gdbstub.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
POSIX_EINVAL 
SIZEOF_PAYLOAD 

Definition at line 41 of file gdbstub.c.

     {
        POSIX_EINVAL = 0x1c,  /* used to report bad arguments to GDB */
        SIZEOF_PAYLOAD = 512, /* buffer size of GDB payload data */
};

Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void gdbstub_state_new ( struct gdbstub stub,
char  ch 
) [static]

Definition at line 312 of file gdbstub.c.

References gdbstub_state_data(), gdbstub::len, and gdbstub::parse.

Referenced by gdbstub_state_cksum2(), and gdbstub_state_wait_ack().

                                                                {
        if ( ch == '$' ) {
                stub->len = 0;
                stub->parse = gdbstub_state_data;
        }
}
static void gdbstub_state_data ( struct gdbstub stub,
char  ch 
) [static]

Definition at line 319 of file gdbstub.c.

References ch, gdbstub_state_cksum1(), gdbstub::len, gdbstub::parse, gdbstub::payload, and SIZEOF_PAYLOAD.

Referenced by gdbstub_state_new().

                                                                 {
        if ( ch == '#' ) {
                stub->parse = gdbstub_state_cksum1;
        } else if ( ch == '$' ) {
                stub->len = 0; /* retry new packet */
        } else {
                /* If the length exceeds our buffer, let the checksum fail */
                if ( stub->len < SIZEOF_PAYLOAD ) {
                        stub->payload [ stub->len++ ] = ch;
                }
        }
}
static void gdbstub_state_cksum1 ( struct gdbstub stub,
char  ch 
) [static]
static void gdbstub_state_cksum2 ( struct gdbstub stub,
char  ch 
) [static]

Definition at line 337 of file gdbstub.c.

References gdbstub::cksum1, gdbstub_cksum(), gdbstub_from_hex_digit(), gdbstub_rx_packet(), gdbstub_state_new(), gdbstub::len, gdbstub::parse, gdbstub::payload, gdb_transport::send, and gdbstub::trans.

Referenced by gdbstub_state_cksum1().

                                                                   {
        uint8_t their_cksum;
        uint8_t our_cksum;

        stub->parse = gdbstub_state_new;
        their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
        our_cksum = gdbstub_cksum ( stub->payload, stub->len );
        if ( their_cksum == our_cksum ) {
                stub->trans->send ( "+", 1 );
                if ( stub->len > 0 ) {
                        gdbstub_rx_packet ( stub );
                }
        } else {
                stub->trans->send ( "-", 1 );
        }
}
static void gdbstub_state_wait_ack ( struct gdbstub stub,
char  ch 
) [static]

Definition at line 354 of file gdbstub.c.

References gdbstub_state_new(), gdbstub_tx_packet(), and gdbstub::parse.

Referenced by gdbstub_tx_packet().

                                                                     {
        if ( ch == '+' ) {
                stub->parse = gdbstub_state_new;
        } else {
                /* This retransmit is very aggressive but necessary to keep
                 * in sync with GDB. */
                gdbstub_tx_packet ( stub );
        }
}
static uint8_t gdbstub_from_hex_digit ( char  ch) [static]

Definition at line 71 of file gdbstub.c.

References isdigit(), and tolower().

Referenced by gdbstub_from_hex_buf(), gdbstub_get_packet_args(), gdbstub_state_cksum1(), and gdbstub_state_cksum2().

                                                  {
        return ( isdigit ( ch ) ? ch - '0' : tolower ( ch ) - 'a' + 0xa ) & 0xf;
}
static uint8_t gdbstub_to_hex_digit ( uint8_t  b) [static]

Definition at line 75 of file gdbstub.c.

Referenced by gdbstub_send_num_packet(), gdbstub_to_hex_buf(), and gdbstub_tx_packet().

                                                  {
        b &= 0xf;
        return ( b < 0xa ? '0' : 'a' - 0xa ) + b;
}
static void gdbstub_from_hex_buf ( char *  dst,
char *  src,
int  lenbytes 
) [static]

Definition at line 85 of file gdbstub.c.

References cpu_to_le16, cpu_to_le32, and gdbstub_from_hex_digit().

Referenced by gdbstub_write_mem(), and gdbstub_write_regs().

                                                                        {
        if ( lenbytes == 2 && ( ( unsigned long ) dst & 0x1 ) == 0 ) {
                uint16_t i = gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
                        gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
                        gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
                        gdbstub_from_hex_digit ( src [ 1 ] );
                * ( uint16_t * ) dst = cpu_to_le16 ( i );
        } else if ( lenbytes == 4 && ( ( unsigned long ) dst & 0x3 ) == 0 ) {
                uint32_t i = gdbstub_from_hex_digit ( src [ 6 ] ) << 28 |
                        gdbstub_from_hex_digit ( src [ 7 ] ) << 24 |
                        gdbstub_from_hex_digit ( src [ 4 ] ) << 20 |
                        gdbstub_from_hex_digit ( src [ 5 ] ) << 16 |
                        gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
                        gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
                        gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
                        gdbstub_from_hex_digit ( src [ 1 ] );
                * ( uint32_t * ) dst = cpu_to_le32 ( i );
        } else {
                while ( lenbytes-- > 0 ) {
                        *dst++ = gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
                                gdbstub_from_hex_digit ( src [ 1 ] );
                        src += 2;
                }
        }
}
static void gdbstub_to_hex_buf ( char *  dst,
char *  src,
int  lenbytes 
) [static]

Definition at line 111 of file gdbstub.c.

References cpu_to_le16, cpu_to_le32, and gdbstub_to_hex_digit().

Referenced by gdbstub_read_mem(), and gdbstub_read_regs().

                                                                      {
        if ( lenbytes == 2 && ( ( unsigned long ) src & 0x1 ) == 0 ) {
                uint16_t i = cpu_to_le16 ( * ( uint16_t * ) src );
                dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
                dst [ 1 ] = gdbstub_to_hex_digit ( i );
                dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
                dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
        } else if ( lenbytes == 4 && ( ( unsigned long ) src & 0x3 ) == 0 ) {
                uint32_t i = cpu_to_le32 ( * ( uint32_t * ) src );
                dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
                dst [ 1 ] = gdbstub_to_hex_digit ( i );
                dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
                dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
                dst [ 4 ] = gdbstub_to_hex_digit ( i >> 20 );
                dst [ 5 ] = gdbstub_to_hex_digit ( i >> 16);
                dst [ 6 ] = gdbstub_to_hex_digit ( i >> 28 );
                dst [ 7 ] = gdbstub_to_hex_digit ( i >> 24 );
        } else {
                while ( lenbytes-- > 0 ) {
                        *dst++ = gdbstub_to_hex_digit ( *src >> 4 );
                        *dst++ = gdbstub_to_hex_digit ( *src );
                        src++;
                }
        }
}
static uint8_t gdbstub_cksum ( char *  data,
int  len 
) [static]

Definition at line 137 of file gdbstub.c.

Referenced by gdbstub_state_cksum2(), and gdbstub_tx_packet().

                                                     {
        uint8_t cksum = 0;
        while ( len-- > 0 ) {
                cksum += ( uint8_t ) *data++;
        }
        return cksum;
}
static void gdbstub_tx_packet ( struct gdbstub stub) [static]

Definition at line 145 of file gdbstub.c.

References gdbstub::buf, gdbstub_cksum(), gdbstub_state_wait_ack(), gdbstub_to_hex_digit(), gdbstub::len, gdbstub::parse, gdbstub::payload, gdb_transport::send, and gdbstub::trans.

Referenced by gdbstub_breakpoint(), gdbstub_read_mem(), gdbstub_read_regs(), gdbstub_rx_packet(), gdbstub_send_num_packet(), gdbstub_send_ok(), and gdbstub_state_wait_ack().

                                                       {
        uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
        stub->buf [ 0 ] = '$';
        stub->buf [ stub->len + 1 ] = '#';
        stub->buf [ stub->len + 2 ] = gdbstub_to_hex_digit ( cksum >> 4 );
        stub->buf [ stub->len + 3 ] = gdbstub_to_hex_digit ( cksum );
        stub->trans->send ( stub->buf, stub->len + 4 );
        stub->parse = gdbstub_state_wait_ack;
}
static void gdbstub_send_ok ( struct gdbstub stub) [static]

Definition at line 156 of file gdbstub.c.

References gdbstub_tx_packet(), gdbstub::len, and gdbstub::payload.

Referenced by gdbstub_breakpoint(), gdbstub_rx_packet(), gdbstub_write_mem(), and gdbstub_write_regs().

                                                     {
        stub->payload [ 0 ] = 'O';
        stub->payload [ 1 ] = 'K';
        stub->len = 2;
        gdbstub_tx_packet ( stub );
}
static void gdbstub_send_num_packet ( struct gdbstub stub,
char  reply,
int  num 
) [static]

Definition at line 163 of file gdbstub.c.

References gdbstub_to_hex_digit(), gdbstub_tx_packet(), gdbstub::len, and gdbstub::payload.

Referenced by gdbstub_report_signal(), and gdbstub_send_errno().

                                                                                  {
        stub->payload [ 0 ] = reply;
        stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
        stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
        stub->len = 3;
        gdbstub_tx_packet ( stub );
}
static int gdbstub_get_packet_args ( struct gdbstub stub,
unsigned long *  args,
int  nargs,
int *  stop_idx 
) [static]

Definition at line 172 of file gdbstub.c.

References ch, gdbstub_from_hex_digit(), gdbstub::len, gdbstub::payload, and val.

Referenced by gdbstub_breakpoint(), gdbstub_continue(), gdbstub_read_mem(), and gdbstub_write_mem().

                                                                                                           {
        int i;
        char ch = 0;
        int argc = 0;
        unsigned long val = 0;
        for ( i = 1; i < stub->len && argc < nargs; i++ ) {
                ch = stub->payload [ i ];
                if ( ch == ':' ) {
                        break;
                } else if ( ch == ',' ) {
                        args [ argc++ ] = val;
                        val = 0;
                } else {
                        val = ( val << 4 ) | gdbstub_from_hex_digit ( ch );
                }
        }
        if ( stop_idx ) {
                *stop_idx = i;
        }
        if ( argc < nargs ) {
                args [ argc++ ] = val;
        }
        return ( ( i == stub->len || ch == ':' ) && argc == nargs );
}
static void gdbstub_send_errno ( struct gdbstub stub,
int  errno 
) [static]
static void gdbstub_report_signal ( struct gdbstub stub) [static]

Definition at line 201 of file gdbstub.c.

References gdbstub_send_num_packet(), and gdbstub::signo.

Referenced by gdbstub_handler(), and gdbstub_rx_packet().

                                                           {
        gdbstub_send_num_packet ( stub, 'S', stub->signo );
}
static void gdbstub_read_regs ( struct gdbstub stub) [static]
static void gdbstub_write_regs ( struct gdbstub stub) [static]
static void gdbstub_read_mem ( struct gdbstub stub) [static]

Definition at line 220 of file gdbstub.c.

References gdbstub_get_packet_args(), gdbstub_send_errno(), gdbstub_to_hex_buf(), gdbstub_tx_packet(), gdbstub::len, NULL, gdbstub::payload, POSIX_EINVAL, and SIZEOF_PAYLOAD.

Referenced by gdbstub_rx_packet().

                                                      {
        unsigned long args [ 2 ];
        if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
                gdbstub_send_errno ( stub, POSIX_EINVAL );
                return;
        }
        args [ 1 ] = ( args [ 1 ] < SIZEOF_PAYLOAD / 2 ) ? args [ 1 ] : SIZEOF_PAYLOAD / 2;
        gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
        stub->len = args [ 1 ] * 2;
        gdbstub_tx_packet ( stub );
}
static void gdbstub_write_mem ( struct gdbstub stub) [static]

Definition at line 232 of file gdbstub.c.

References gdbstub_from_hex_buf(), gdbstub_get_packet_args(), gdbstub_send_errno(), gdbstub_send_ok(), gdbstub::len, gdbstub::payload, and POSIX_EINVAL.

Referenced by gdbstub_rx_packet().

                                                       {
        unsigned long args [ 2 ];
        int colon;
        if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
                        colon >= stub->len || stub->payload [ colon ] != ':' ||
                        ( stub->len - colon - 1 ) % 2 != 0 ) {
                gdbstub_send_errno ( stub, POSIX_EINVAL );
                return;
        }
        gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], ( stub->len - colon - 1 ) / 2 );
        gdbstub_send_ok ( stub );
}
static void gdbstub_continue ( struct gdbstub stub,
int  single_step 
) [static]

Definition at line 245 of file gdbstub.c.

References gdbstub::exit_handler, gdbmach_set_pc(), gdbmach_set_single_step(), gdbstub_get_packet_args(), gdbstub::len, NULL, and gdbstub::regs.

Referenced by gdbstub_rx_packet().

                                                                       {
        gdbreg_t pc;
        if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
                gdbmach_set_pc ( stub->regs, pc );
        }
        gdbmach_set_single_step ( stub->regs, single_step );
        stub->exit_handler = 1;
        /* Reply will be sent when we hit the next breakpoint or interrupt */
}
static void gdbstub_breakpoint ( struct gdbstub stub) [static]

Definition at line 255 of file gdbstub.c.

References gdbmach_set_breakpoint(), gdbstub_get_packet_args(), gdbstub_send_errno(), gdbstub_send_ok(), gdbstub_tx_packet(), gdbstub::len, NULL, gdbstub::payload, POSIX_EINVAL, and rc.

Referenced by gdbstub_rx_packet().

                                                        {
        unsigned long args [ 3 ];
        int enable = stub->payload [ 0 ] == 'Z' ? 1 : 0;
        int rc;

        if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
                gdbstub_send_errno ( stub, POSIX_EINVAL );
                return;
        }
        if ( ( rc = gdbmach_set_breakpoint ( args [ 0 ], args [ 1 ],
                                             args [ 2 ], enable ) ) != 0 ) {
                /* Not supported */
                stub->len = 0;
                gdbstub_tx_packet ( stub );
                return;
        }
        gdbstub_send_ok ( stub );
}
static void gdbstub_rx_packet ( struct gdbstub stub) [static]

Definition at line 274 of file gdbstub.c.

References gdbstub_breakpoint(), gdbstub_continue(), gdbstub_read_mem(), gdbstub_read_regs(), gdbstub_report_signal(), gdbstub_send_ok(), gdbstub_tx_packet(), gdbstub_write_mem(), gdbstub_write_regs(), gdbstub::len, and gdbstub::payload.

Referenced by gdbstub_state_cksum2().

                                                       {
        switch ( stub->payload [ 0 ] ) {
                case '?':
                        gdbstub_report_signal ( stub );
                        break;
                case 'g':
                        gdbstub_read_regs ( stub );
                        break;
                case 'G':
                        gdbstub_write_regs ( stub );
                        break;
                case 'm':
                        gdbstub_read_mem ( stub );
                        break;
                case 'M':
                        gdbstub_write_mem ( stub );
                        break;
                case 'c': /* Continue */
                case 'k': /* Kill */
                case 's': /* Step */
                case 'D': /* Detach */
                        gdbstub_continue ( stub, stub->payload [ 0 ] == 's' );
                        if ( stub->payload [ 0 ] == 'D' ) {
                                gdbstub_send_ok ( stub );
                        }
                        break;
                case 'Z': /* Insert breakpoint */
                case 'z': /* Remove breakpoint */
                        gdbstub_breakpoint ( stub );
                        break;
                default:
                        stub->len = 0;
                        gdbstub_tx_packet ( stub );
                        break;
        }
}
static void gdbstub_parse ( struct gdbstub stub,
char  ch 
) [static]

Definition at line 364 of file gdbstub.c.

References gdbstub::parse.

Referenced by gdbstub_handler().

                                                            {
        stub->parse ( stub, ch );
}
void gdbstub_handler ( int  signo,
gdbreg_t regs 
)

Interrupt handler.

POSIX signal number CPU register snapshot

Definition at line 372 of file gdbstub.c.

References gdbstub::exit_handler, gdbstub_parse(), gdbstub_report_signal(), len, gdb_transport::recv, gdbstub::regs, regs, gdbstub::signo, SIZEOF_PAYLOAD, and gdbstub::trans.

Referenced by gdbmach_handler().

                                                   {
        char packet [ SIZEOF_PAYLOAD + 4 ];
        size_t len, i;

        /* A transport must be set up */
        if ( !stub.trans ) {
                return;
        }

        stub.signo = signo;
        stub.regs = regs;
        stub.exit_handler = 0;
        gdbstub_report_signal ( &stub );
        while ( !stub.exit_handler && ( len = stub.trans->recv ( packet, sizeof ( packet ) ) ) > 0 ) {
                for ( i = 0; i < len; i++ ) {
                        gdbstub_parse ( &stub, packet [ i ] );
                }
        }
}
struct gdb_transport* find_gdb_transport ( const char *  name) [read]

Look up GDB transport by name.

Parameters:
nameName of transport
Return values:
GDBtransport or NULL

Definition at line 392 of file gdbstub.c.

References for_each_table_entry, GDB_TRANSPORTS, gdb_transport::name, NULL, and strcmp().

Referenced by parse_gdb_transport().

                                                              {
        struct gdb_transport *trans;

        for_each_table_entry ( trans, GDB_TRANSPORTS ) {
                if ( strcmp ( trans->name, name ) == 0 ) {
                        return trans;
                }
        }
        return NULL;
}
void gdbstub_start ( struct gdb_transport trans)

Break into the debugger using the given transport.

Parameters:
transGDB transport

Definition at line 403 of file gdbstub.c.

References gdbstub::buf, gdbmach_breakpoint(), gdbmach_init(), gdbstub::payload, and gdbstub::trans.

Referenced by gdbstub_exec().

                                                   {
        stub.trans = trans;
        stub.payload = &stub.buf [ 1 ];
        gdbmach_init();
        gdbmach_breakpoint();
}

Variable Documentation

struct gdbstub stub [static]
Initial value:
 {
        .parse = gdbstub_state_new
}

Definition at line 368 of file gdbstub.c.