Search
lxdream.org :: lxdream/src/gdbserver.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdbserver.c
changeset 998:1754a8c6a9cf
next1020:04e622ab1635
author nkeynes
date Tue Mar 24 11:15:57 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add preliminary implementation of the GDB remote debugging server - attaches to
either or both the SH4 and ARM
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdbserver.c Tue Mar 24 11:15:57 2009 +0000
1.3 @@ -0,0 +1,523 @@
1.4 +/**
1.5 + * $Id: gdbserver.c 1018 2009-03-19 12:29:06Z nkeynes $
1.6 + *
1.7 + * GDB RDP server stub - SH4 + ARM
1.8 + *
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +
1.23 +#include <errno.h>
1.24 +#include <stdio.h>
1.25 +#include <stdlib.h>
1.26 +#include <stdarg.h>
1.27 +#include <string.h>
1.28 +#include <unistd.h>
1.29 +#include <glib.h>
1.30 +#include <arpa/inet.h>
1.31 +#include "lxdream.h"
1.32 +#include "dreamcast.h"
1.33 +#include "netutil.h"
1.34 +#include "cpu.h"
1.35 +
1.36 +#define DEFAULT_BUFFER_SIZE 1024
1.37 +#define BUFFER_SIZE_MARGIN 32
1.38 +#define MAX_BUFFER_SIZE 65536
1.39 +
1.40 +/* These are just local interpretations - they're not interpreted by GDB
1.41 + * in any way shape or form.
1.42 + */
1.43 +#define GDB_ERROR_FORMAT 1 /* Badly formatted command */
1.44 +#define GDB_ERROR_INVAL 2 /* Invalid data */
1.45 +#define GDB_ERROR_FAIL 3 /* Command failed */
1.46 +struct gdb_server {
1.47 + cpu_desc_t cpu;
1.48 + gboolean mmu;
1.49 + int fd;
1.50 + const gchar *peer_name;
1.51 + char *buf;
1.52 + int buf_size;
1.53 + int buf_posn;
1.54 +};
1.55 +
1.56 +void gdb_server_free( gpointer data )
1.57 +{
1.58 + struct gdb_server *server = (struct gdb_server *)data;
1.59 + free((char *)server->peer_name);
1.60 + free(server->buf);
1.61 + free(data);
1.62 +}
1.63 +
1.64 +int gdb_checksum( char *data, int length )
1.65 +{
1.66 + int i;
1.67 + int result = 0;
1.68 + for( i=0; i<length; i++ )
1.69 + result += data[i];
1.70 + result &= 0xFF;
1.71 + return result;
1.72 +}
1.73 +
1.74 +void gdb_send_frame( struct gdb_server *server, char *data, int length )
1.75 +{
1.76 + char out[length+5];
1.77 + snprintf( out, length+5, "$%.*s#%02x", length, data, gdb_checksum(data,length) );
1.78 + write( server->fd, out, length+4 );
1.79 +}
1.80 +
1.81 +/**
1.82 + * Send bulk data (ie memory dump) as hex, with optional string prefix.
1.83 + * Saves double copying when going through gdb_send_frame.
1.84 + */
1.85 +void gdb_send_hex_data( struct gdb_server *server, char *prefix, unsigned char *data, int datalen )
1.86 +{
1.87 + int prefixlen = 0;
1.88 + if( prefix != NULL )
1.89 + prefixlen = strlen(prefix);
1.90 + int totallen = datalen*2 + prefixlen + 4;
1.91 + char out[totallen+1];
1.92 + char *p = &out[1];
1.93 + int i;
1.94 +
1.95 + out[0] = '$';
1.96 + if( prefix != NULL ) {
1.97 + p += sprintf( p, "%s", prefix );
1.98 + }
1.99 + for( i=0; i<datalen; i++ ) {
1.100 + p += sprintf( p, "%02x", data[i] );
1.101 + }
1.102 + *p++ = '#';
1.103 + sprintf( p, "%02x", gdb_checksum(out+1, datalen*2 + prefixlen) );
1.104 + write( server->fd, out, totallen );
1.105 +}
1.106 +
1.107 +/**
1.108 + * Parse bulk hex data - buffer should be at least datalen/2 bytes long
1.109 + */
1.110 +size_t gdb_read_hex_data( struct gdb_server *server, unsigned char *buf, unsigned char *data, int datalen )
1.111 +{
1.112 + char *p = data;
1.113 + for( int i=0; i<datalen/2; i++ ) {
1.114 + int v;
1.115 + sscanf( p, "%02x", &v );
1.116 + buf[i] = v;
1.117 + p += 2;
1.118 + }
1.119 + return datalen/2;
1.120 +}
1.121 +
1.122 +/**
1.123 + * Parse bulk binary-encoded data - $, #, 0x7D are encoded as 0x7d, char ^ 0x20.
1.124 + * Buffer should be at least datalen bytes longs.
1.125 + */
1.126 +size_t gdb_read_binary_data( struct gdb_server *server, unsigned char *buf, unsigned char *data, int datalen )
1.127 +{
1.128 + unsigned char *q = buf;
1.129 + for( int i=0, j=0; i<datalen; i++ ) {
1.130 + if( data[i] == 0x7D ) {
1.131 + if( i == datalen-1 ) {
1.132 + return -1;
1.133 + } else {
1.134 + *q++ = data[++i] ^ 0x20;
1.135 + }
1.136 + } else {
1.137 + *q++ = data[i];
1.138 + }
1.139 + }
1.140 + return q - buf;
1.141 +}
1.142 +
1.143 +void gdb_printf_frame( struct gdb_server *server, char *msg, ... )
1.144 +{
1.145 + va_list va;
1.146 +
1.147 + va_start(va,msg);
1.148 + int len = vsnprintf( NULL, 0, msg, va );
1.149 + char buf[len+1];
1.150 + vsnprintf( buf, len+1, msg, va);
1.151 + va_end(va);
1.152 + gdb_send_frame( server, buf, len );
1.153 +}
1.154 +
1.155 +int gdb_print_registers( struct gdb_server *server, char *buf, int buflen, int firstreg, int regcount )
1.156 +{
1.157 + int i;
1.158 + char *p = buf;
1.159 + char *endp = buf + (buflen-8);
1.160 + for( i=firstreg; i < firstreg + regcount && p < endp; i++ ) {
1.161 + uint8_t *val = server->cpu->get_register(i);
1.162 + if( val == NULL ) {
1.163 + sprintf( p, "00000000" );
1.164 + } else {
1.165 + sprintf( p, "%02x%02x%02x%02x", val[0], val[1], val[2], val[3] );
1.166 + }
1.167 + p += 8;
1.168 + }
1.169 +
1.170 + return i - firstreg;
1.171 +}
1.172 +
1.173 +void gdb_set_registers( struct gdb_server *server, char *buf, int firstreg, int regcount )
1.174 +{
1.175 + int i;
1.176 + char *p = buf;
1.177 + for( i=firstreg; i < firstreg + regcount; i++ ) {
1.178 + uint8_t *val = server->cpu->get_register(i);
1.179 + if( val != NULL ) {
1.180 + sscanf( p, "%02x%02x%02x%02x", val, val+1, val+2, val+3 );
1.181 + }
1.182 + p += 8;
1.183 + }
1.184 +}
1.185 +
1.186 +/**
1.187 + * Send a 2-digit error code. There's no actual definition for any of the codes
1.188 + * so they're more for our own amusement really.
1.189 + */
1.190 +void gdb_send_error( struct gdb_server *server, int error )
1.191 +{
1.192 + char out[4];
1.193 + snprintf( out, 4, "E%02X", (error&0xFF) );
1.194 + gdb_send_frame( server, out, 3 );
1.195 +}
1.196 +
1.197 +void gdb_server_handle_frame( struct gdb_server *server, int command, char *data, int length )
1.198 +{
1.199 + unsigned int tmp, tmp2, tmp3;
1.200 + char buf[512];
1.201 +
1.202 + switch( command ) {
1.203 + case '!': /* Enable extended mode */
1.204 + gdb_send_frame( server, "OK", 2 );
1.205 + break;
1.206 + case '?': /* Get stop reason - always return 5 (TRAP) */
1.207 + gdb_send_frame( server, "S05", 3 );
1.208 + break;
1.209 + case 'c': /* Continue */
1.210 + dreamcast_run();
1.211 + gdb_send_frame( server, "S05", 3 );
1.212 + break;
1.213 + case 'g': /* Read all general registers */
1.214 + gdb_print_registers( server, buf, sizeof(buf), 0, server->cpu->num_gpr_regs );
1.215 + gdb_send_frame( server, buf, strlen(buf) );
1.216 + break;
1.217 + case 'G': /* Write all general registers */
1.218 + if( length != server->cpu->num_gpr_regs*8 ) {
1.219 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.220 + } else {
1.221 + gdb_set_registers( server, data, 0, server->cpu->num_gpr_regs );
1.222 + gdb_send_frame( server, "OK", 2 );
1.223 + }
1.224 + break;
1.225 + case 'H': /* Set thread - only thread 1 is supported here */
1.226 + if( length < 2 ) {
1.227 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.228 + } else {
1.229 + int thread;
1.230 + sscanf( data+1, "%d", &thread );
1.231 + if( thread >= -1 && thread <= 1 ) {
1.232 + gdb_send_frame( server, "OK", 2 );
1.233 + } else {
1.234 + gdb_send_error( server, GDB_ERROR_INVAL );
1.235 + }
1.236 + }
1.237 + break;
1.238 + case 'k': /* kill - do nothing */
1.239 + gdb_send_frame( server, "", 0 );
1.240 + break;
1.241 + case 'm': /* Read memory */
1.242 + if( sscanf( data, "%x,%x", &tmp, &tmp2 ) != 2 ) {
1.243 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.244 + } else {
1.245 + size_t datalen;
1.246 + char mem[tmp2];
1.247 + if( server->mmu ) {
1.248 + datalen = server->cpu->read_mem_vma(mem, tmp, tmp2);
1.249 + } else {
1.250 + datalen = server->cpu->read_mem_phys(mem, tmp, tmp2);
1.251 + }
1.252 + if( datalen == 0 ) {
1.253 + gdb_send_error( server, GDB_ERROR_INVAL );
1.254 + } else {
1.255 + gdb_send_hex_data( server, NULL, mem, datalen );
1.256 + }
1.257 + }
1.258 + break;
1.259 + case 'M': /* Write memory */
1.260 + if( sscanf( data, "%x,%x:%n", &tmp, &tmp2, &tmp3 ) != 2 ||
1.261 + length-tmp3 != tmp2*2 ) {
1.262 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.263 + } else {
1.264 + size_t len;
1.265 + char mem[tmp2];
1.266 + len = gdb_read_hex_data( server, mem, data+tmp3, length-tmp3 );
1.267 + if( len != tmp2 ) {
1.268 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.269 + } else {
1.270 + if( server->mmu ) {
1.271 + len = server->cpu->write_mem_vma(tmp, mem, tmp2);
1.272 + } else {
1.273 + len = server->cpu->write_mem_phys(tmp, mem, tmp2);
1.274 + }
1.275 + if( len != tmp2 ) {
1.276 + gdb_send_error( server, GDB_ERROR_INVAL );
1.277 + } else {
1.278 + gdb_send_frame( server, "OK", 2 );
1.279 + }
1.280 + }
1.281 + }
1.282 + break;
1.283 + case 'p': /* Read single register */
1.284 + if( sscanf( data, "%x", &tmp ) != 1 ) {
1.285 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.286 + } else if( tmp >= server->cpu->num_gdb_regs ) {
1.287 + gdb_send_error( server, GDB_ERROR_INVAL );
1.288 + } else {
1.289 + gdb_print_registers( server, buf, sizeof(buf), tmp, 1 );
1.290 + gdb_send_frame( server, buf, 8 );
1.291 + }
1.292 + break;
1.293 + case 'P': /* Write single register. */
1.294 + if( sscanf( data, "%x=%n", &tmp, &tmp2 ) != 1 ||
1.295 + length-tmp2 != 8) {
1.296 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.297 + } else if( tmp >= server->cpu->num_gdb_regs ) {
1.298 + gdb_send_error( server, GDB_ERROR_INVAL );
1.299 + } else {
1.300 + gdb_set_registers( server, data+tmp2, tmp, 1 );
1.301 + gdb_send_frame( server, "OK", 2 );
1.302 + }
1.303 + break;
1.304 + case 'q': /* Query data */
1.305 + if( strcmp( data, "C" ) == 0 ) {
1.306 + gdb_send_frame( server, "QC1", 3 );
1.307 + } else if( strcmp( data, "fThreadInfo" ) == 0 ) {
1.308 + gdb_send_frame( server, "m1", 2 );
1.309 + } else if( strcmp( data, "sThreadInfo" ) == 0 ) {
1.310 + gdb_send_frame( server, "l", 1 );
1.311 + } else if( strncmp( data, "Supported", 9 ) == 0 ) {
1.312 + gdb_send_frame( server, "PacketSize=4000", 15 );
1.313 + } else if( strcmp( data, "Symbol::" ) == 0 ) {
1.314 + gdb_send_frame( server, "OK", 2 );
1.315 + } else {
1.316 + gdb_send_frame( server, "", 0 );
1.317 + }
1.318 + break;
1.319 + case 's': /* Single-step */
1.320 + if( length != 0 ) {
1.321 + if( sscanf( data, "%x", &tmp ) != 1 ) {
1.322 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.323 + } else {
1.324 + *server->cpu->pc = tmp;
1.325 + }
1.326 + }
1.327 + server->cpu->step_func();
1.328 + gdb_send_frame( server, "S05", 3 );
1.329 + break;
1.330 + case 'T': /* Thread alive */
1.331 + if( sscanf( data, "%x", &tmp ) != 1 ) {
1.332 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.333 + } else if( tmp != 1 ) {
1.334 + gdb_send_error( server, GDB_ERROR_INVAL );
1.335 + } else {
1.336 + gdb_send_frame( server, "OK", 2 );
1.337 + }
1.338 + break;
1.339 + case 'v': /* Verbose */
1.340 + /* Only current one is vCont, which we don't bother supporting
1.341 + * at the moment, but don't warn about it either */
1.342 + gdb_send_frame( server, "", 0 );
1.343 + break;
1.344 + case 'X': /* Write memory binary */
1.345 + if( sscanf( data, "%x,%x:%n", &tmp, &tmp2, &tmp3 ) != 2 ) {
1.346 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.347 + } else {
1.348 + char mem[length - tmp3];
1.349 + size_t len = gdb_read_binary_data( server, mem, data + tmp3, length-tmp3 );
1.350 + if( len != tmp2 ) {
1.351 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.352 + } else {
1.353 + if( server->mmu ) {
1.354 + len = server->cpu->write_mem_vma(tmp, mem, tmp2);
1.355 + } else {
1.356 + len = server->cpu->write_mem_phys(tmp, mem, tmp2);
1.357 + }
1.358 + if( len != tmp2 ) {
1.359 + gdb_send_error( server, GDB_ERROR_INVAL );
1.360 + } else {
1.361 + gdb_send_frame( server, "OK", 2 );
1.362 + }
1.363 + }
1.364 + }
1.365 + break;
1.366 + case 'z': /* Remove Break/watchpoint */
1.367 + if( sscanf( data, "%d,%x,%x", &tmp, &tmp2, &tmp3 ) != 3 ) {
1.368 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.369 + } else {
1.370 + if( tmp == 0 || tmp == 1 ) { /* soft break or hard break */
1.371 + server->cpu->clear_breakpoint( tmp2, BREAK_KEEP );
1.372 + gdb_send_frame( server, "OK", 2 );
1.373 + } else {
1.374 + gdb_send_frame( server, "", 0 );
1.375 + }
1.376 + }
1.377 + break;
1.378 + case 'Z': /* Insert Break/watchpoint */
1.379 + if( sscanf( data, "%d,%x,%x", &tmp, &tmp2, &tmp3 ) != 3 ) {
1.380 + gdb_send_error( server, GDB_ERROR_FORMAT );
1.381 + } else {
1.382 + if( tmp == 0 || tmp == 1 ) { /* soft break or hard break */
1.383 + server->cpu->set_breakpoint( tmp2, BREAK_KEEP );
1.384 + gdb_send_frame( server, "OK", 2 );
1.385 + } else {
1.386 + gdb_send_frame( server, "", 0 );
1.387 + }
1.388 + }
1.389 + break;
1.390 + default:
1.391 + /* Command unsupported */
1.392 + WARN( "Received unknown GDB command '%c%s'", command, data );
1.393 + gdb_send_frame( server, "", 0 );
1.394 + break;
1.395 + }
1.396 +
1.397 +}
1.398 +
1.399 +/**
1.400 + * Decode out frames from the raw data stream. A frame takes the form of
1.401 + * $<data>#<checksum>
1.402 + * where data may not contain a '#' character, and checksum is a simple
1.403 + * 8-bit modulo sum of all bytes in data, encoded as a 2-char hex number.
1.404 + *
1.405 + * The only other legal wire forms are
1.406 + * +
1.407 + * indicating successful reception of the last message (ignored here), and
1.408 + * -
1.409 + * indicating failed reception of the last message - need to resend.
1.410 + */
1.411 +void gdb_server_process_buffer( struct gdb_server *server )
1.412 +{
1.413 + int i, frame_start = -1, frame_len = -1;
1.414 + for( i=0; i<server->buf_posn; i++ ) {
1.415 + if( frame_start == -1 ) {
1.416 + if( server->buf[i] == '$' ) {
1.417 + frame_start = i;
1.418 + } else if( server->buf[i] == '+' ) {
1.419 + /* Success */
1.420 + continue;
1.421 + } else if( server->buf[i] == '-' ) {
1.422 + /* Request retransmit */
1.423 + } /* Anything else is noise */
1.424 + } else if( server->buf[i] == '#' ) {
1.425 + frame_len = i - frame_start - 1;
1.426 + if( i+2 < server->buf_posn ) {
1.427 + int calc_checksum = gdb_checksum( &server->buf[frame_start+1], frame_len );
1.428 + int frame_checksum = 0;
1.429 + sscanf( &server->buf[i+1], "%02x", &frame_checksum );
1.430 +
1.431 + if( calc_checksum != frame_checksum ) {
1.432 + WARN( "GDB frame checksum failure (expected %02X but was %02X)",
1.433 + calc_checksum, frame_checksum );
1.434 + write( server->fd, "-", 1 );
1.435 + } else if( frame_len == 0 ) {
1.436 + /* Empty frame - should never occur as a request */
1.437 + WARN( "Empty GDB frame received" );
1.438 + write( server->fd, "-", 1 );
1.439 + } else {
1.440 + /* We have a good frame */
1.441 + write( server->fd, "+", 1 );
1.442 + server->buf[i] = '\0';
1.443 + gdb_server_handle_frame( server, server->buf[frame_start+1], &server->buf[frame_start+2], frame_len-1 );
1.444 + }
1.445 + i+=2;
1.446 + frame_start = -1;
1.447 + }
1.448 + }
1.449 + }
1.450 + if( frame_start == -1 ) {
1.451 + server->buf_posn = 0; /* Consumed whole buffer */
1.452 + } else if( frame_start > 0 ) {
1.453 + memmove(&server->buf[0], &server->buf[frame_start], server->buf_posn - frame_start);
1.454 + server->buf_posn -= frame_start;
1.455 + }
1.456 +}
1.457 +
1.458 +gboolean gdb_server_data_callback( int fd, void *data )
1.459 +{
1.460 + struct gdb_server *server = (struct gdb_server *)data;
1.461 +
1.462 + size_t len = read( fd, &server->buf[server->buf_posn], server->buf_size - server->buf_posn );
1.463 + if( len > 0 ) {
1.464 + server->buf_posn += len;
1.465 + gdb_server_process_buffer( server );
1.466 +
1.467 + /* If we have an oversized packet, extend the buffer */
1.468 + if( server->buf_posn > server->buf_size - BUFFER_SIZE_MARGIN &&
1.469 + server->buf_size < MAX_BUFFER_SIZE ) {
1.470 + server->buf_size <<= 1;
1.471 + server->buf = realloc( server->buf, server->buf_size );
1.472 + assert( server->buf != NULL );
1.473 + }
1.474 + return TRUE;
1.475 + } else {
1.476 + INFO( "GDB disconnected" );
1.477 + return FALSE;
1.478 + }
1.479 +}
1.480 +
1.481 +gboolean gdb_server_connect_callback( int fd, gpointer data )
1.482 +{
1.483 + struct sockaddr_in sin;
1.484 + socklen_t sinlen;
1.485 + struct gdb_server *server = (struct gdb_server *)data;
1.486 + int conn_fd = accept( fd, (struct sockaddr *)&sin, &sinlen);
1.487 + if( conn_fd != -1 ) {
1.488 + struct gdb_server *chan_serv = calloc( sizeof(struct gdb_server), 1 );
1.489 + chan_serv->cpu = server->cpu;
1.490 + chan_serv->mmu = server->mmu;
1.491 + chan_serv->fd = conn_fd;
1.492 + chan_serv->peer_name = g_strdup_printf("%s:%d", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1.493 + chan_serv->buf = malloc(1024);
1.494 + chan_serv->buf_size = 1024;
1.495 + chan_serv->buf_posn = 0;
1.496 + net_register_tcp_listener( conn_fd, gdb_server_data_callback, chan_serv, gdb_server_free );
1.497 + INFO( "GDB connected from %s", chan_serv->peer_name );
1.498 + }
1.499 + return TRUE;
1.500 +}
1.501 +
1.502 +/**
1.503 + * Bind a network port for a GDB remote server for the specified cpu. The
1.504 + * port is registered for the system network callback.
1.505 + *
1.506 + * @param interface network interface to bind to, or null for the default (all) interface
1.507 + * @param port
1.508 + * @param cpu CPU to make available over the network port..
1.509 + * @param mmu if TRUE, virtual memory is made available to GDB, otherwise GDB
1.510 + * accesses physical memory.
1.511 + * @return TRUE if the server was bound successfully.
1.512 + */
1.513 +gboolean gdb_init_server( const char *interface, int port, cpu_desc_t cpu, gboolean mmu )
1.514 +{
1.515 + int fd = net_create_server_socket( interface, port );
1.516 + if( fd == -1 ) {
1.517 + return FALSE;
1.518 + }
1.519 +
1.520 + struct gdb_server *server = calloc( sizeof(struct gdb_server), 1 );
1.521 + server->cpu = cpu;
1.522 + server->mmu = mmu;
1.523 + server->fd = fd;
1.524 + net_register_tcp_listener( fd, gdb_server_connect_callback, server, gdb_server_free );
1.525 + INFO( "%s GDB server running on port %d", cpu->name, port );
1.526 +}
.