4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
22 #include "sim_network.h"
31 #include "sim_core.h" // for SET_SREG_FROM, READ_SREG_INTO
33 #include "avr_eeprom.h"
38 #define WATCH_LIMIT (32)
41 uint32_t len; /**< How many points are taken (points[0] .. points[len - 1]). */
43 uint32_t addr; /**< Which address is watched. */
44 uint32_t size; /**< How large is the watched segment. */
45 uint32_t kind; /**< Bitmask of enum avr_gdb_watch_type values. */
46 } points[WATCH_LIMIT];
47 } avr_gdb_watchpoints_t;
49 typedef struct avr_gdb_t {
51 int listen; // listen socket
52 int s; // current gdb connection
54 avr_gdb_watchpoints_t breakpoints;
55 avr_gdb_watchpoints_t watchpoints;
60 * Returns the index of the watchpoint if found, -1 otherwise.
62 static int gdb_watch_find(const avr_gdb_watchpoints_t * w, uint32_t addr)
64 for (int i = 0; i < w->len; i++) {
65 if (w->points[i].addr > addr) {
67 } else if (w->points[i].addr == addr) {
76 * Contrary to gdb_watch_find, this actually checks the address against
77 * a watched memory _range_.
79 static int gdb_watch_find_range(const avr_gdb_watchpoints_t * w, uint32_t addr)
81 for (int i = 0; i < w->len; i++) {
82 if (w->points[i].addr > addr) {
84 } else if (w->points[i].addr <= addr && addr < w->points[i].addr + w->points[i].size) {
93 * Returns -1 on error, 0 otherwise.
95 static int gdb_watch_add_or_update(avr_gdb_watchpoints_t * w, enum avr_gdb_watch_type kind, uint32_t addr,
98 /* If the watchpoint exists, update it. */
99 int i = gdb_watch_find(w, addr);
101 w->points[i].size = size;
102 w->points[i].kind |= kind;
106 /* Otherwise add it. */
107 if (w->len == WATCH_LIMIT) {
111 /* Find the insertion point. */
112 for (i = 0; i < w->len; i++) {
113 if (w->points[i].addr > addr) {
120 /* Make space for new element. */
121 for (int j = i + 1; j < w->len; j++) {
122 w->points[j] = w->points[j - 1];
126 w->points[i].kind = kind;
127 w->points[i].addr = addr;
128 w->points[i].size = size;
134 * Returns -1 on error or if the specified point does not exist, 0 otherwise.
136 static int gdb_watch_rm(avr_gdb_watchpoints_t * w, enum avr_gdb_watch_type kind, uint32_t addr)
138 int i = gdb_watch_find(w, addr);
143 w->points[i].kind &= ~kind;
144 if (w->points[i].kind) {
148 for (i = i + 1; i < w->len; i++) {
149 w->points[i - 1] = w->points[i];
157 static void gdb_watch_clear(avr_gdb_watchpoints_t * w)
162 static void gdb_send_reply(avr_gdb_t * g, char * cmd)
165 uint8_t * dst = reply;
172 sprintf((char*)dst, "#%02x", check);
173 DBG(printf("%s '%s'\n", __FUNCTION__, reply);)
174 send(g->s, reply, dst - reply + 3, 0);
177 static void gdb_send_quick_status(avr_gdb_t * g, uint8_t signal)
181 sprintf(cmd, "T%02x20:%02x;21:%02x%02x;22:%02x%02x%02x00;",
182 signal ? signal : 5, g->avr->data[R_SREG],
183 g->avr->data[R_SPL], g->avr->data[R_SPH],
184 g->avr->pc & 0xff, (g->avr->pc>>8)&0xff, (g->avr->pc>>16)&0xff);
185 gdb_send_reply(g, cmd);
188 static int gdb_change_breakpoint(avr_gdb_watchpoints_t * w, int set, enum avr_gdb_watch_type kind,
189 uint32_t addr, uint32_t size)
191 DBG(printf("set %d kind %d addr %08x len %d\n", set, kind, addr, len);)
194 return gdb_watch_add_or_update(w, kind, addr, size);
196 return gdb_watch_rm(w, kind, addr);
202 static int gdb_write_register(avr_gdb_t * g, int regi, uint8_t * src)
206 g->avr->data[regi] = *src;
209 g->avr->data[R_SREG] = *src;
210 SET_SREG_FROM(g->avr, *src);
213 g->avr->data[R_SPL] = src[0];
214 g->avr->data[R_SPH] = src[1];
217 g->avr->pc = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
223 static int gdb_read_register(avr_gdb_t * g, int regi, char * rep)
227 sprintf(rep, "%02x", g->avr->data[regi]);
231 READ_SREG_INTO(g->avr, sreg);
232 sprintf(rep, "%02x", sreg);
236 sprintf(rep, "%02x%02x", g->avr->data[R_SPL], g->avr->data[R_SPH]);
239 sprintf(rep, "%02x%02x%02x00",
240 g->avr->pc & 0xff, (g->avr->pc>>8)&0xff, (g->avr->pc>>16)&0xff);
246 static void gdb_handle_command(avr_gdb_t * g, char * cmd)
248 avr_t * avr = g->avr;
250 uint8_t command = *cmd++;
253 gdb_send_quick_status(g, 0);
255 case 'G': { // set all general purpose registers
256 // get their binary form
257 read_hex_string(cmd, (uint8_t*)rep, strlen(cmd));
258 uint8_t *src = (uint8_t*)rep;
259 for (int i = 0; i < 35; i++)
260 src += gdb_write_register(g, i, src);
261 gdb_send_reply(g, "OK");
263 case 'g': { // read all general purpose registers
265 for (int i = 0; i < 35; i++)
266 dst += gdb_read_register(g, i, dst);
267 gdb_send_reply(g, rep);
269 case 'p': { // read register
270 unsigned int regi = 0;
271 sscanf(cmd, "%x", ®i);
272 gdb_read_register(g, regi, rep);
273 gdb_send_reply(g, rep);
275 case 'P': { // write register
276 unsigned int regi = 0;
277 char * val = strchr(cmd, '=');
281 sscanf(cmd, "%x", ®i);
282 read_hex_string(val, (uint8_t*)rep, strlen(val));
283 gdb_write_register(g, regi, (uint8_t*)rep);
284 gdb_send_reply(g, "OK");
286 case 'm': { // read memory
287 avr_flashaddr_t addr;
289 sscanf(cmd, "%x,%x", &addr, &len);
290 uint8_t * src = NULL;
291 if (addr < avr->flashend) {
292 src = avr->flash + addr;
293 } else if (addr >= 0x800000 && (addr - 0x800000) <= avr->ramend) {
294 src = avr->data + addr - 0x800000;
295 } else if (addr >= 0x810000 && (addr - 0x810000) <= avr->e2end) {
296 avr_eeprom_desc_t ee = {.offset = (addr - 0x810000)};
297 avr_ioctl(avr, AVR_IOCTL_EEPROM_GET, &ee);
301 gdb_send_reply(g, "E01");
304 } else if (addr >= 0x800000 && (addr - 0x800000) == avr->ramend+1 && len == 2) {
305 // Allow GDB to read a value just after end of stack.
306 // This is necessary to make instruction stepping work when stack is empty
307 AVR_LOG(avr, LOG_TRACE, "GDB: read just past end of stack %08x, %08x; returning zero\n", addr, len);
308 gdb_send_reply(g, "0000");
311 AVR_LOG(avr, LOG_ERROR, "GDB: read memory error %08x, %08x (ramend %04x)\n", addr, len, avr->ramend+1);
312 gdb_send_reply(g, "E01");
317 sprintf(dst, "%02x", *src++);
321 gdb_send_reply(g, rep);
323 case 'M': { // write memory
325 sscanf(cmd, "%x,%x", &addr, &len);
326 char * start = strchr(cmd, ':');
328 gdb_send_reply(g, "E01");
332 read_hex_string(start + 1, avr->flash + addr, strlen(start+1));
333 gdb_send_reply(g, "OK");
334 } else if (addr >= 0x800000 && (addr - 0x800000) <= avr->ramend) {
335 read_hex_string(start + 1, avr->data + addr - 0x800000, strlen(start+1));
336 gdb_send_reply(g, "OK");
337 } else if (addr >= 0x810000 && (addr - 0x810000) <= avr->e2end) {
338 read_hex_string(start + 1, (uint8_t*)rep, strlen(start+1));
339 avr_eeprom_desc_t ee = {.offset = (addr - 0x810000), .size = len, .ee = (uint8_t*)rep };
340 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &ee);
341 gdb_send_reply(g, "OK");
343 AVR_LOG(avr, LOG_ERROR, "GDB: write memory error %08x, %08x\n", addr, len);
344 gdb_send_reply(g, "E01");
347 case 'c': { // continue
348 avr->state = cpu_Running;
351 avr->state = cpu_Step;
353 case 'r': { // deprecated, suggested for AVRStudio compatibility
354 avr->state = cpu_StepDone;
357 case 'Z': // set clear break/watchpoint
359 uint32_t kind, addr, len;
360 int set = (command == 'Z');
361 sscanf(cmd, "%d,%x,%x", &kind, &addr, &len);
362 // printf("breakpoint %d, %08x, %08x\n", kind, addr, len);
364 case 0: // software breakpoint
365 case 1: // hardware breakpoint
366 if (addr > avr->flashend ||
367 gdb_change_breakpoint(&g->breakpoints, set, 1 << kind, addr, len) == -1) {
368 gdb_send_reply(g, "E01");
372 gdb_send_reply(g, "OK");
374 case 2: // write watchpoint
375 case 3: // read watchpoint
376 case 4: // access watchpoint
377 /* Mask out the offset applied to SRAM addresses. */
379 if (addr > avr->ramend ||
380 gdb_change_breakpoint(&g->watchpoints, set, 1 << kind, addr, len) == -1) {
381 gdb_send_reply(g, "E01");
385 gdb_send_reply(g, "OK");
388 gdb_send_reply(g, "");
393 gdb_send_reply(g, "");
398 static int gdb_network_handler(avr_gdb_t * g, uint32_t dosleep)
405 FD_SET(g->s, &read_set);
408 FD_SET(g->listen, &read_set);
411 struct timeval timo = { 0, dosleep }; // short, but not too short interval
412 int ret = select(max, &read_set, NULL, NULL, &timo);
417 if (FD_ISSET(g->listen, &read_set)) {
418 g->s = accept(g->listen, NULL, NULL);
421 perror("gdb_network_handler accept");
426 setsockopt (g->s, IPPROTO_TCP, TCP_NODELAY, &i, sizeof (i));
427 g->avr->state = cpu_Stopped;
428 printf("%s connection opened\n", __FUNCTION__);
431 if (g->s != -1 && FD_ISSET(g->s, &read_set)) {
432 uint8_t buffer[1024];
434 ssize_t r = recv(g->s, buffer, sizeof(buffer)-1, 0);
437 printf("%s connection closed\n", __FUNCTION__);
439 gdb_watch_clear(&g->breakpoints);
440 gdb_watch_clear(&g->watchpoints);
441 g->avr->state = cpu_Running; // resume
446 perror("gdb_network_handler recv");
451 // printf("%s: received %d bytes\n'%s'\n", __FUNCTION__, r, buffer);
452 // hdump("gdb", buffer, r);
454 uint8_t * src = buffer;
455 while (*src == '+' || *src == '-')
457 // control C -- lets send the guy a nice status packet
460 g->avr->state = cpu_StepDone;
461 printf("GDB hit control-c\n");
465 uint8_t * end = buffer + r - 1;
466 while (end > src && *end != '#')
470 DBG(printf("GDB command = '%s'\n", src);)
472 send(g->s, "+", 1, 0);
474 gdb_handle_command(g, (char*)src);
481 * If an applicable watchpoint exists for addr, stop the cpu and send a status report.
482 * type is one of AVR_GDB_WATCH_READ, AVR_GDB_WATCH_WRITE depending on the type of access.
484 void avr_gdb_handle_watchpoints(avr_t * avr, uint16_t addr, enum avr_gdb_watch_type type)
486 avr_gdb_t *g = avr->gdb;
488 int i = gdb_watch_find_range(&g->watchpoints, addr);
493 int kind = g->watchpoints.points[i].kind;
495 /* Send gdb reply (see GDB user manual appendix E.3). */
497 sprintf(cmd, "T%02x20:%02x;21:%02x%02x;22:%02x%02x%02x00;%s:%06x;",
498 5, g->avr->data[R_SREG],
499 g->avr->data[R_SPL], g->avr->data[R_SPH],
500 g->avr->pc & 0xff, (g->avr->pc>>8)&0xff, (g->avr->pc>>16)&0xff,
501 kind & AVR_GDB_WATCH_ACCESS ? "awatch" : kind & AVR_GDB_WATCH_WRITE ? "watch" : "rwatch",
503 gdb_send_reply(g, cmd);
505 avr->state = cpu_Stopped;
509 int avr_gdb_processor(avr_t * avr, int sleep)
511 if (!avr || !avr->gdb)
513 avr_gdb_t * g = avr->gdb;
515 if (avr->state == cpu_Running && gdb_watch_find(&g->breakpoints, avr->pc) != -1) {
516 DBG(printf("avr_gdb_processor hit breakpoint at %08x\n", avr->pc);)
517 gdb_send_quick_status(g, 0);
518 avr->state = cpu_Stopped;
519 } else if (avr->state == cpu_StepDone) {
520 gdb_send_quick_status(g, 0);
521 avr->state = cpu_Stopped;
523 // this also sleeps for a bit
524 return gdb_network_handler(g, sleep);
528 int avr_gdb_init(avr_t * avr)
530 avr_gdb_t * g = malloc(sizeof(avr_gdb_t));
531 memset(g, 0, sizeof(avr_gdb_t));
535 if ( network_init() ) {
536 AVR_LOG(avr, LOG_ERROR, "GDB: Can't initialize network");
540 if ((g->listen = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
541 AVR_LOG(avr, LOG_ERROR, "GDB: Can't create socket: %s", strerror(errno));
546 setsockopt(g->listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
548 struct sockaddr_in address = { 0 };
549 address.sin_family = AF_INET;
550 address.sin_port = htons (avr->gdb_port);
552 if (bind(g->listen, (struct sockaddr *) &address, sizeof(address))) {
553 AVR_LOG(avr, LOG_ERROR, "GDB: Can not bind socket: %s", strerror(errno));
556 if (listen(g->listen, 1)) {
560 printf("avr_gdb_init listening on port %d\n", avr->gdb_port);
564 // change default run behaviour to use the slightly slower versions
565 avr->run = avr_callback_run_gdb;
566 avr->sleep = avr_callback_sleep_gdb;
571 void avr_deinit_gdb(avr_t * avr)
573 if (avr->gdb->listen != -1)
574 close(avr->gdb->listen);
575 if (avr->gdb->s != -1)