- added autoprobe functionality
[openocd.git] / src / server / gdb_server.c
blobd48101f4591a6b39d3111a39188ed8d03f4262d6
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "replacements.h"
26 #include "gdb_server.h"
28 #include "server.h"
29 #include "log.h"
30 #include "binarybuffer.h"
31 #include "jtag.h"
32 #include "breakpoints.h"
33 #include "flash.h"
34 #include "target_request.h"
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
41 #if 0
42 #define _DEBUG_GDB_IO_
43 #endif
45 static unsigned short gdb_port;
47 enum gdb_detach_mode
49 GDB_DETACH_RESUME,
50 GDB_DETACH_RESET,
51 GDB_DETACH_HALT,
52 GDB_DETACH_NOTHING
55 /* target behaviour on gdb detach */
56 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
58 /* set if we are sending a memory map to gdb
59 * via qXfer:memory-map:read packet */
60 int gdb_use_memory_map = 0;
61 int gdb_flash_program = 0;
63 int gdb_last_signal(target_t *target)
65 switch (target->debug_reason)
67 case DBG_REASON_DBGRQ:
68 return 0x2; /* SIGINT */
69 case DBG_REASON_BREAKPOINT:
70 case DBG_REASON_WATCHPOINT:
71 case DBG_REASON_WPTANDBKPT:
72 return 0x05; /* SIGTRAP */
73 case DBG_REASON_SINGLESTEP:
74 return 0x05; /* SIGTRAP */
75 case DBG_REASON_NOTHALTED:
76 return 0x0; /* no signal... shouldn't happen */
77 default:
78 ERROR("BUG: undefined debug reason");
79 exit(-1);
83 int gdb_get_char(connection_t *connection, int* next_char)
85 gdb_connection_t *gdb_con = connection->priv;
87 #ifdef _DEBUG_GDB_IO_
88 char *debug_buffer;
89 #endif
91 if (gdb_con->buf_cnt-- > 0)
93 *next_char = *(gdb_con->buf_p++);
94 if (gdb_con->buf_cnt > 0)
95 connection->input_pending = 1;
96 else
97 connection->input_pending = 0;
99 #ifdef _DEBUG_GDB_IO_
100 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
101 #endif
103 return ERROR_OK;
106 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
108 if (gdb_con->buf_cnt == 0)
109 return ERROR_SERVER_REMOTE_CLOSED;
111 #ifdef _WIN32
112 errno = WSAGetLastError();
114 switch(errno)
116 case WSAEWOULDBLOCK:
117 usleep(1000);
118 break;
119 case WSAECONNABORTED:
120 return ERROR_SERVER_REMOTE_CLOSED;
121 case WSAECONNRESET:
122 return ERROR_SERVER_REMOTE_CLOSED;
123 default:
124 ERROR("read: %d", errno);
125 exit(-1);
127 #else
128 switch(errno)
130 case EAGAIN:
131 usleep(1000);
132 break;
133 case ECONNABORTED:
134 return ERROR_SERVER_REMOTE_CLOSED;
135 case ECONNRESET:
136 return ERROR_SERVER_REMOTE_CLOSED;
137 default:
138 ERROR("read: %s", strerror(errno));
139 exit(-1);
141 #endif
144 #ifdef _DEBUG_GDB_IO_
145 debug_buffer = malloc(gdb_con->buf_cnt + 1);
146 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
147 debug_buffer[gdb_con->buf_cnt] = 0;
148 DEBUG("received '%s'", debug_buffer);
149 free(debug_buffer);
150 #endif
152 gdb_con->buf_p = gdb_con->buffer;
153 gdb_con->buf_cnt--;
154 *next_char = *(gdb_con->buf_p++);
155 if (gdb_con->buf_cnt > 0)
156 connection->input_pending = 1;
157 else
158 connection->input_pending = 0;
159 #ifdef _DEBUG_GDB_IO_
160 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
161 #endif
163 return ERROR_OK;
166 int gdb_putback_char(connection_t *connection, int last_char)
168 gdb_connection_t *gdb_con = connection->priv;
170 if (gdb_con->buf_p > gdb_con->buffer)
172 *(--gdb_con->buf_p) = last_char;
173 gdb_con->buf_cnt++;
175 else
177 ERROR("BUG: couldn't put character back");
180 return ERROR_OK;
183 int gdb_put_packet(connection_t *connection, char *buffer, int len)
185 int i;
186 unsigned char my_checksum = 0;
187 char checksum[3];
188 #ifdef _DEBUG_GDB_IO_
189 char *debug_buffer;
190 #endif
191 int reply;
192 int retval;
193 gdb_connection_t *gdb_con = connection->priv;
195 for (i = 0; i < len; i++)
196 my_checksum += buffer[i];
198 while (1)
200 #ifdef _DEBUG_GDB_IO_
201 debug_buffer = malloc(len + 1);
202 memcpy(debug_buffer, buffer, len);
203 debug_buffer[len] = 0;
204 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
205 free(debug_buffer);
206 #endif
207 write_socket(connection->fd, "$", 1);
208 if (len > 0)
209 write_socket(connection->fd, buffer, len);
210 write_socket(connection->fd, "#", 1);
212 snprintf(checksum, 3, "%2.2x", my_checksum);
214 write_socket(connection->fd, checksum, 2);
216 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
217 return retval;
219 if (reply == '+')
220 break;
221 else if (reply == '-')
222 WARNING("negative reply, retrying");
223 else if (reply == 0x3)
225 gdb_con->ctrl_c = 1;
226 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
227 return retval;
228 if (reply == '+')
229 break;
230 else if (reply == '-')
231 WARNING("negative reply, retrying");
232 else
234 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
235 return ERROR_SERVER_REMOTE_CLOSED;
238 else
240 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
241 return ERROR_SERVER_REMOTE_CLOSED;
245 return ERROR_OK;
248 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
250 int character;
251 int count;
252 int retval;
253 char checksum[3];
254 unsigned char my_checksum = 0;
255 gdb_connection_t *gdb_con = connection->priv;
257 while (1)
261 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
262 return retval;
264 #ifdef _DEBUG_GDB_IO_
265 DEBUG("character: '%c'", character);
266 #endif
268 switch (character)
270 case '$':
271 break;
272 case '+':
273 WARNING("acknowledgment received, but no packet pending");
274 break;
275 case '-':
276 WARNING("negative acknowledgment, but no packet pending");
277 break;
278 case 0x3:
279 gdb_con->ctrl_c = 1;
280 *len = 0;
281 return ERROR_OK;
282 default:
283 WARNING("ignoring character 0x%x", character);
284 break;
286 } while (character != '$');
288 my_checksum = 0;
289 count = 0;
291 for (;;)
293 /* The common case is that we have an entire packet with no escape chars.
294 * We need to leave at least 2 bytes in the buffer to have
295 * gdb_get_char() update various bits and bobs correctly.
297 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
299 /* The compiler will struggle a bit with constant propagation and
300 * aliasing, so we help it by showing that these values do not
301 * change inside the loop
303 int i;
304 char *buf = gdb_con->buf_p;
305 int run = gdb_con->buf_cnt - 2;
306 i = 0;
307 int done = 0;
308 while (i < run)
310 character = *buf++;
311 i++;
312 if (character == '#')
314 /* Danger! character can be '#' when esc is
315 * used so we need an explicit boolean for done here.
317 done = 1;
318 break;
321 if (character == '}')
323 /* data transmitted in binary mode (X packet)
324 * uses 0x7d as escape character */
325 my_checksum += character & 0xff;
326 character = *buf++;
327 i++;
328 my_checksum += character & 0xff;
329 buffer[count++] = (character ^ 0x20) & 0xff;
330 } else
332 my_checksum += character & 0xff;
333 buffer[count++] = character & 0xff;
336 gdb_con->buf_p += i;
337 gdb_con->buf_cnt -= i;
338 if (done)
339 break;
341 if (count > *len)
343 ERROR("packet buffer too small");
344 return ERROR_GDB_BUFFER_TOO_SMALL;
347 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
348 return retval;
350 if (character == '#')
351 break;
353 if (character == '}')
355 /* data transmitted in binary mode (X packet)
356 * uses 0x7d as escape character */
357 my_checksum += character & 0xff;
358 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
359 return retval;
360 my_checksum += character & 0xff;
361 buffer[count++] = (character ^ 0x20) & 0xff;
363 else
365 my_checksum += character & 0xff;
366 buffer[count++] = character & 0xff;
371 *len = count;
373 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
374 return retval;
375 checksum[0] = character;
376 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
377 return retval;
378 checksum[1] = character;
379 checksum[2] = 0;
381 if (my_checksum == strtoul(checksum, NULL, 16))
383 write_socket(connection->fd, "+", 1);
384 break;
387 WARNING("checksum error, requesting retransmission");
388 write_socket(connection->fd, "-", 1);
391 return ERROR_OK;
394 int gdb_output(struct command_context_s *context, char* line)
396 connection_t *connection = context->output_handler_priv;
397 gdb_connection_t *gdb_connection = connection->priv;
399 char *hex_buffer;
400 int i, bin_size;
402 /* check if output is enabled */
403 if (gdb_connection->output_disable)
405 return ERROR_OK;
408 bin_size = strlen(line);
410 hex_buffer = malloc(bin_size*2 + 4);
412 hex_buffer[0] = 'O';
413 for (i=0; i<bin_size; i++)
414 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
415 hex_buffer[bin_size*2+1] = '0';
416 hex_buffer[bin_size*2+2] = 'a';
417 hex_buffer[bin_size*2+3] = 0x0;
419 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
421 free(hex_buffer);
422 return ERROR_OK;
425 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
427 FILE *script;
428 struct command_context_s *cmd_ctx = priv;
430 if (target->gdb_program_script)
432 script = fopen(target->gdb_program_script, "r");
433 if (!script)
435 ERROR("couldn't open script file %s", target->gdb_program_script);
436 return ERROR_OK;
439 INFO("executing gdb_program script '%s'", target->gdb_program_script);
440 command_run_file(cmd_ctx, script, COMMAND_EXEC);
441 fclose(script);
443 jtag_execute_queue();
446 return ERROR_OK;
449 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
451 connection_t *connection = priv;
452 gdb_connection_t *gdb_connection = connection->priv;
453 char sig_reply[4];
454 int signal;
456 switch (event)
458 case TARGET_EVENT_HALTED:
459 if (gdb_connection->frontend_state == TARGET_RUNNING)
461 if (gdb_connection->ctrl_c)
463 signal = 0x2;
464 gdb_connection->ctrl_c = 0;
466 else
468 signal = gdb_last_signal(target);
471 snprintf(sig_reply, 4, "T%2.2x", signal);
472 gdb_put_packet(connection, sig_reply, 3);
473 gdb_connection->frontend_state = TARGET_HALTED;
475 break;
476 case TARGET_EVENT_RESUMED:
477 if (gdb_connection->frontend_state == TARGET_HALTED)
479 gdb_connection->frontend_state = TARGET_RUNNING;
481 break;
482 case TARGET_EVENT_GDB_PROGRAM:
483 gdb_program_handler(target, event, connection->cmd_ctx);
484 break;
485 default:
486 break;
489 return ERROR_OK;
492 int gdb_new_connection(connection_t *connection)
494 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
495 gdb_service_t *gdb_service = connection->service->priv;
496 int retval;
497 int initial_ack;
499 connection->priv = gdb_connection;
501 /* initialize gdb connection information */
502 gdb_connection->buf_p = gdb_connection->buffer;
503 gdb_connection->buf_cnt = 0;
504 gdb_connection->ctrl_c = 0;
505 gdb_connection->frontend_state = TARGET_HALTED;
506 gdb_connection->vflash_image = NULL;
507 gdb_connection->output_disable = 0;
509 /* output goes through gdb connection */
510 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
512 /* register callback to be informed about target events */
513 target_register_event_callback(gdb_target_callback_event_handler, connection);
515 /* a gdb session just attached, put the target in halt mode */
516 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
517 (retval != ERROR_TARGET_ALREADY_HALTED))
519 ERROR("error when trying to halt target");
520 exit(-1);
523 while (gdb_service->target->state != TARGET_HALTED)
525 gdb_service->target->type->poll(gdb_service->target);
528 /* remove the initial ACK from the incoming buffer */
529 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
530 return retval;
532 if (initial_ack != '+')
533 gdb_putback_char(connection, initial_ack);
535 return ERROR_OK;
538 int gdb_connection_closed(connection_t *connection)
540 gdb_service_t *gdb_service = connection->service->priv;
541 gdb_connection_t *gdb_connection = connection->priv;
543 /* see if an image built with vFlash commands is left */
544 if (gdb_connection->vflash_image)
546 image_close(gdb_connection->vflash_image);
547 free(gdb_connection->vflash_image);
548 gdb_connection->vflash_image = NULL;
551 /* if this connection registered a debug-message receiver delete it */
552 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
554 if (connection->priv)
556 free(connection->priv);
557 connection->priv = NULL;
559 else
561 ERROR("BUG: connection->priv == NULL");
564 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
566 return ERROR_OK;
569 void gdb_send_error(connection_t *connection, u8 the_error)
571 char err[4];
572 snprintf(err, 4, "E%2.2X", the_error );
573 gdb_put_packet(connection, err, 3);
576 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
578 char sig_reply[4];
579 int signal;
581 signal = gdb_last_signal(target);
583 snprintf(sig_reply, 4, "S%2.2x", signal);
584 gdb_put_packet(connection, sig_reply, 3);
586 return ERROR_OK;
589 /* Convert register to string of bits. NB! The # of bits in the
590 * register might be non-divisible by 8(a byte), in which
591 * case an entire byte is shown. */
592 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
594 static const char *DIGITS = "0123456789abcdef";
595 int i;
597 u8 *buf;
598 int buf_len;
599 buf = reg->value;
600 buf_len = CEIL(reg->size, 8);
602 if (target->endianness == TARGET_LITTLE_ENDIAN)
604 for (i = 0; i < buf_len; i++)
606 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
607 tstr[i*2+1] = DIGITS[buf[i]&0xf];
610 else
612 for (i = 0; i < buf_len; i++)
614 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
615 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
620 void gdb_target_to_str(target_t *target, char *tstr, char *str)
622 int str_len = strlen(tstr);
623 int i;
625 if (str_len % 2)
627 ERROR("BUG: gdb value with uneven number of characters encountered");
628 exit(-1);
631 if (target->endianness == TARGET_LITTLE_ENDIAN)
633 for (i = 0; i < str_len; i+=2)
635 str[str_len - i - 1] = tstr[i + 1];
636 str[str_len - i - 2] = tstr[i];
639 else
641 for (i = 0; i < str_len; i++)
643 str[i] = tstr[i];
648 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
650 reg_t **reg_list;
651 int reg_list_size;
652 int retval;
653 int reg_packet_size = 0;
654 char *reg_packet;
655 char *reg_packet_p;
656 int i;
658 #ifdef _DEBUG_GDB_IO_
659 DEBUG("-");
660 #endif
662 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
664 switch (retval)
666 case ERROR_TARGET_NOT_HALTED:
667 ERROR("gdb requested registers but we're not halted, dropping connection");
668 return ERROR_SERVER_REMOTE_CLOSED;
669 default:
670 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
671 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
672 exit(-1);
676 for (i = 0; i < reg_list_size; i++)
678 reg_packet_size += reg_list[i]->size;
681 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
682 reg_packet_p = reg_packet;
684 for (i = 0; i < reg_list_size; i++)
686 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
687 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
690 #ifdef _DEBUG_GDB_IO_
692 char *reg_packet_p;
693 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
694 DEBUG("reg_packet: %s", reg_packet_p);
695 free(reg_packet_p);
697 #endif
699 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
700 free(reg_packet);
702 free(reg_list);
704 return ERROR_OK;
707 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
709 int i;
710 reg_t **reg_list;
711 int reg_list_size;
712 int retval;
713 char *packet_p;
715 #ifdef _DEBUG_GDB_IO_
716 DEBUG("-");
717 #endif
719 /* skip command character */
720 packet++;
721 packet_size--;
723 if (packet_size % 2)
725 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
726 return ERROR_SERVER_REMOTE_CLOSED;
729 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
731 switch (retval)
733 case ERROR_TARGET_NOT_HALTED:
734 ERROR("gdb tried to registers but we're not halted, dropping connection");
735 return ERROR_SERVER_REMOTE_CLOSED;
736 default:
737 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
738 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
739 exit(-1);
743 packet_p = packet;
744 for (i = 0; i < reg_list_size; i++)
746 u8 *bin_buf;
747 char *hex_buf;
748 reg_arch_type_t *arch_type;
750 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
751 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
752 gdb_target_to_str(target, packet_p, hex_buf);
754 /* convert hex-string to binary buffer */
755 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
756 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
758 /* get register arch_type, and call set method */
759 arch_type = register_get_arch_type(reg_list[i]->arch_type);
760 if (arch_type == NULL)
762 ERROR("BUG: encountered unregistered arch type");
763 exit(-1);
765 arch_type->set(reg_list[i], bin_buf);
767 /* advance packet pointer */
768 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
770 free(bin_buf);
771 free(hex_buf);
774 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
775 free(reg_list);
777 gdb_put_packet(connection, "OK", 2);
779 return ERROR_OK;
782 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
784 char *reg_packet;
785 int reg_num = strtoul(packet + 1, NULL, 16);
786 reg_t **reg_list;
787 int reg_list_size;
788 int retval;
790 #ifdef _DEBUG_GDB_IO_
791 DEBUG("-");
792 #endif
794 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
796 switch (retval)
798 case ERROR_TARGET_NOT_HALTED:
799 ERROR("gdb requested registers but we're not halted, dropping connection");
800 return ERROR_SERVER_REMOTE_CLOSED;
801 default:
802 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
803 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
804 exit(-1);
808 if (reg_list_size <= reg_num)
810 ERROR("gdb requested a non-existing register");
811 exit(-1);
814 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
816 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
818 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
820 free(reg_list);
821 free(reg_packet);
823 return ERROR_OK;
826 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
828 char *separator;
829 char *hex_buf;
830 u8 *bin_buf;
831 int reg_num = strtoul(packet + 1, &separator, 16);
832 reg_t **reg_list;
833 int reg_list_size;
834 int retval;
835 reg_arch_type_t *arch_type;
837 DEBUG("-");
839 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
841 switch (retval)
843 case ERROR_TARGET_NOT_HALTED:
844 ERROR("gdb tried to set a register but we're not halted, dropping connection");
845 return ERROR_SERVER_REMOTE_CLOSED;
846 default:
847 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
848 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
849 exit(-1);
853 if (reg_list_size < reg_num)
855 ERROR("gdb requested a non-existing register");
856 return ERROR_SERVER_REMOTE_CLOSED;
859 if (*separator != '=')
861 ERROR("GDB 'set register packet', but no '=' following the register number");
862 return ERROR_SERVER_REMOTE_CLOSED;
865 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
866 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
867 gdb_target_to_str(target, separator + 1, hex_buf);
869 /* convert hex-string to binary buffer */
870 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
871 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
873 /* get register arch_type, and call set method */
874 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
875 if (arch_type == NULL)
877 ERROR("BUG: encountered unregistered arch type");
878 exit(-1);
880 arch_type->set(reg_list[reg_num], bin_buf);
882 gdb_put_packet(connection, "OK", 2);
884 free(bin_buf);
885 free(hex_buf);
886 free(reg_list);
888 return ERROR_OK;
891 int gdb_memory_packet_error(connection_t *connection, int retval)
893 switch (retval)
895 case ERROR_TARGET_NOT_HALTED:
896 ERROR("gdb tried to read memory but we're not halted, dropping connection");
897 return ERROR_SERVER_REMOTE_CLOSED;
898 break;
899 case ERROR_TARGET_DATA_ABORT:
900 gdb_send_error(connection, EIO);
901 break;
902 case ERROR_TARGET_TRANSLATION_FAULT:
903 gdb_send_error(connection, EFAULT);
904 break;
905 case ERROR_TARGET_UNALIGNED_ACCESS:
906 gdb_send_error(connection, EFAULT);
907 break;
908 default:
909 ERROR("BUG: unexpected error %i", retval);
910 exit(-1);
913 return ERROR_OK;
916 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
918 char *separator;
919 u32 addr = 0;
920 u32 len = 0;
922 u8 *buffer;
923 char *hex_buffer;
925 int i;
926 int retval;
928 /* skip command character */
929 packet++;
931 addr = strtoul(packet, &separator, 16);
933 if (*separator != ',')
935 ERROR("incomplete read memory packet received, dropping connection");
936 return ERROR_SERVER_REMOTE_CLOSED;
939 len = strtoul(separator+1, NULL, 16);
941 buffer = malloc(len);
943 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
945 switch (len)
947 case 4:
948 if ((addr % 4) == 0)
949 retval = target->type->read_memory(target, addr, 4, 1, buffer);
950 else
951 retval = target->type->read_memory(target, addr, 1, len, buffer);
952 break;
953 case 2:
954 if ((addr % 2) == 0)
955 retval = target->type->read_memory(target, addr, 2, 1, buffer);
956 else
957 retval = target->type->read_memory(target, addr, 1, len, buffer);
958 break;
959 default:
960 if (((addr % 4) == 0) && ((len % 4) == 0))
961 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
962 else
963 retval = target->type->read_memory(target, addr, 1, len, buffer);
966 #if 0
967 if (retval == ERROR_TARGET_DATA_ABORT)
969 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
970 * At some point this might be fixed in GDB, in which case this code can be removed.
971 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
973 memset(buffer, 0, len);
974 retval = ERROR_OK;
976 #endif
978 if (retval == ERROR_OK)
980 hex_buffer = malloc(len * 2 + 1);
982 for (i=0; i<len; i++)
983 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
985 gdb_put_packet(connection, hex_buffer, len * 2);
987 free(hex_buffer);
989 else
991 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
992 return retval;
995 free(buffer);
997 return ERROR_OK;
1000 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1002 char *separator;
1003 u32 addr = 0;
1004 u32 len = 0;
1006 u8 *buffer;
1008 int i;
1009 int retval;
1011 /* skip command character */
1012 packet++;
1014 addr = strtoul(packet, &separator, 16);
1016 if (*separator != ',')
1018 ERROR("incomplete write memory packet received, dropping connection");
1019 return ERROR_SERVER_REMOTE_CLOSED;
1022 len = strtoul(separator+1, &separator, 16);
1024 if (*(separator++) != ':')
1026 ERROR("incomplete write memory packet received, dropping connection");
1027 return ERROR_SERVER_REMOTE_CLOSED;
1030 buffer = malloc(len);
1032 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1034 for (i=0; i<len; i++)
1036 u32 tmp;
1037 sscanf(separator + 2*i, "%2x", &tmp);
1038 buffer[i] = tmp;
1041 retval = ERROR_OK;
1042 switch (len)
1044 /* handle sized writes */
1045 case 4:
1046 if ((addr % 4) == 0)
1047 retval = target->type->write_memory(target, addr, 4, 1, buffer);
1048 else
1049 retval = target->type->write_memory(target, addr, 1, len, buffer);
1050 break;
1051 case 2:
1052 if ((addr % 2) == 0)
1053 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1054 else
1055 retval = target->type->write_memory(target, addr, 1, len, buffer);
1056 break;
1057 case 3:
1058 case 1:
1059 retval = target->type->write_memory(target, addr, 1, len, buffer);
1060 break;
1061 /* handle bulk writes */
1062 default:
1063 retval = target_write_buffer(target, addr, len, buffer);
1064 break;
1067 if (retval == ERROR_OK)
1069 gdb_put_packet(connection, "OK", 2);
1071 else
1073 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1074 return retval;
1077 free(buffer);
1079 return ERROR_OK;
1082 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1084 char *separator;
1085 u32 addr = 0;
1086 u32 len = 0;
1088 u8 *buffer;
1089 int retval;
1091 /* skip command character */
1092 packet++;
1094 addr = strtoul(packet, &separator, 16);
1096 if (*separator != ',')
1098 ERROR("incomplete write memory binary packet received, dropping connection");
1099 return ERROR_SERVER_REMOTE_CLOSED;
1102 len = strtoul(separator+1, &separator, 16);
1104 if (*(separator++) != ':')
1106 ERROR("incomplete write memory binary packet received, dropping connection");
1107 return ERROR_SERVER_REMOTE_CLOSED;
1110 retval = ERROR_OK;
1111 if( len ) {
1113 buffer = malloc(len);
1115 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1117 memcpy( buffer, separator, len );
1119 switch (len)
1121 case 4:
1122 if ((addr % 4) == 0)
1123 retval = target->type->write_memory(target, addr, 4, 1, buffer);
1124 else
1125 retval = target->type->write_memory(target, addr, 1, len, buffer);
1126 break;
1127 case 2:
1128 if ((addr % 2) == 0)
1129 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1130 else
1131 retval = target->type->write_memory(target, addr, 1, len, buffer);
1132 break;
1133 case 3:
1134 case 1:
1135 retval = target->type->write_memory(target, addr, 1, len, buffer);
1136 break;
1137 default:
1138 retval = target_write_buffer(target, addr, len, buffer);
1139 break;
1142 free(buffer);
1145 if (retval == ERROR_OK)
1147 gdb_put_packet(connection, "OK", 2);
1149 else
1151 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1152 return retval;
1155 return ERROR_OK;
1158 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1160 int current = 0;
1161 u32 address = 0x0;
1163 DEBUG("-");
1165 if (packet_size > 1)
1167 packet[packet_size] = 0;
1168 address = strtoul(packet + 1, NULL, 16);
1170 else
1172 current = 1;
1175 if (packet[0] == 'c')
1177 DEBUG("continue");
1178 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1180 else if (packet[0] == 's')
1182 DEBUG("step");
1183 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1187 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1189 switch (retval)
1191 case ERROR_TARGET_NOT_HALTED:
1192 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1193 return ERROR_SERVER_REMOTE_CLOSED;
1194 break;
1195 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1196 gdb_send_error(connection, EBUSY);
1197 break;
1198 default:
1199 ERROR("BUG: unexpected error %i", retval);
1200 exit(-1);
1203 return ERROR_OK;
1206 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1208 int type;
1209 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1210 enum watchpoint_rw wp_type;
1211 u32 address;
1212 u32 size;
1213 char *separator;
1214 int retval;
1216 DEBUG("-");
1218 type = strtoul(packet + 1, &separator, 16);
1220 if (type == 0) /* memory breakpoint */
1221 bp_type = BKPT_SOFT;
1222 else if (type == 1) /* hardware breakpoint */
1223 bp_type = BKPT_HARD;
1224 else if (type == 2) /* write watchpoint */
1225 wp_type = WPT_WRITE;
1226 else if (type == 3) /* read watchpoint */
1227 wp_type = WPT_READ;
1228 else if (type == 4) /* access watchpoint */
1229 wp_type = WPT_ACCESS;
1231 if (*separator != ',')
1233 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1234 return ERROR_SERVER_REMOTE_CLOSED;
1237 address = strtoul(separator+1, &separator, 16);
1239 if (*separator != ',')
1241 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1242 return ERROR_SERVER_REMOTE_CLOSED;
1245 size = strtoul(separator+1, &separator, 16);
1247 switch (type)
1249 case 0:
1250 case 1:
1251 if (packet[0] == 'Z')
1253 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1255 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1256 return retval;
1258 else
1260 gdb_put_packet(connection, "OK", 2);
1263 else
1265 breakpoint_remove(target, address);
1266 gdb_put_packet(connection, "OK", 2);
1268 break;
1269 case 2:
1270 case 3:
1271 case 4:
1273 if (packet[0] == 'Z')
1275 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1277 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1278 return retval;
1280 else
1282 gdb_put_packet(connection, "OK", 2);
1285 else
1287 watchpoint_remove(target, address);
1288 gdb_put_packet(connection, "OK", 2);
1290 break;
1292 default:
1293 break;
1296 return ERROR_OK;
1299 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1300 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1302 if (*retval != ERROR_OK)
1304 return;
1306 int first = 1;
1308 for (;;)
1310 if ((*xml == NULL) || (!first))
1312 /* start by 0 to exercise all the code paths.
1313 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1315 *size = *size * 2 + 2;
1316 char *t = *xml;
1317 *xml = realloc(*xml, *size);
1318 if (*xml == NULL)
1320 if (t)
1321 free(t);
1322 *retval = ERROR_SERVER_REMOTE_CLOSED;
1323 return;
1327 va_list ap;
1328 int ret;
1329 va_start(ap, fmt);
1330 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1331 va_end(ap);
1332 if ((ret > 0) && ((ret + 1) < *size - *pos))
1334 *pos += ret;
1335 return;
1337 /* there was just enough or not enough space, allocate more. */
1338 first = 0;
1342 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1344 char *separator;
1346 /* Extract and NUL-terminate the annex. */
1347 *annex = buf;
1348 while (*buf && *buf != ':')
1349 buf++;
1350 if (*buf == '\0')
1351 return -1;
1352 *buf++ = 0;
1354 /* After the read marker and annex, qXfer looks like a
1355 * traditional 'm' packet. */
1357 *ofs = strtoul(buf, &separator, 16);
1359 if (*separator != ',')
1360 return -1;
1362 *len = strtoul(separator+1, NULL, 16);
1364 return 0;
1367 int gdb_calc_blocksize(flash_bank_t *bank)
1369 int i;
1370 int block_size = 0xffffffff;
1372 /* loop through all sectors and return smallest sector size */
1374 for (i = 0; i < bank->num_sectors; i++)
1376 if (bank->sectors[i].size < block_size)
1377 block_size = bank->sectors[i].size;
1380 return block_size;
1383 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1385 command_context_t *cmd_ctx = connection->cmd_ctx;
1387 if (strstr(packet, "qRcmd,"))
1389 if (packet_size > 6)
1391 char *cmd;
1392 int i;
1393 cmd = malloc((packet_size - 6)/2 + 1);
1394 for (i=0; i < (packet_size - 6)/2; i++)
1396 u32 tmp;
1397 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1398 cmd[i] = tmp;
1400 cmd[(packet_size - 6)/2] = 0x0;
1401 target_call_timer_callbacks();
1402 command_run_line(cmd_ctx, cmd);
1403 free(cmd);
1405 gdb_put_packet(connection, "OK", 2);
1406 return ERROR_OK;
1408 else if (strstr(packet, "qCRC:"))
1410 if (packet_size > 5)
1412 int retval;
1413 u8 gdb_reply[10];
1414 char *separator;
1415 u32 checksum;
1416 u32 addr = 0;
1417 u32 len = 0;
1419 /* skip command character */
1420 packet += 5;
1422 addr = strtoul(packet, &separator, 16);
1424 if (*separator != ',')
1426 ERROR("incomplete read memory packet received, dropping connection");
1427 return ERROR_SERVER_REMOTE_CLOSED;
1430 len = strtoul(separator + 1, NULL, 16);
1432 retval = target_checksum_memory(target, addr, len, &checksum);
1434 if (retval == ERROR_OK)
1436 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1437 gdb_put_packet(connection, gdb_reply, 9);
1439 else
1441 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1442 return retval;
1445 return ERROR_OK;
1448 else if (strstr(packet, "qSupported"))
1450 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1451 * disable qXfer:features:read for the moment */
1452 int retval = ERROR_OK;
1453 char *buffer = NULL;
1454 int pos = 0;
1455 int size = 0;
1457 xml_printf(&retval, &buffer, &pos, &size,
1458 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1459 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1461 if (retval != ERROR_OK)
1463 gdb_send_error(connection, 01);
1464 return ERROR_OK;
1467 gdb_put_packet(connection, buffer, strlen(buffer));
1468 free(buffer);
1470 return ERROR_OK;
1472 else if (strstr(packet, "qXfer:memory-map:read::"))
1474 /* We get away with only specifying flash here. Regions that are not
1475 * specified are treated as if we provided no memory map(if not we
1476 * could detect the holes and mark them as RAM).
1477 * Normally we only execute this code once, but no big deal if we
1478 * have to regenerate it a couple of times. */
1480 flash_bank_t *p;
1481 char *xml = NULL;
1482 int size = 0;
1483 int pos = 0;
1484 int retval = ERROR_OK;
1486 int offset;
1487 int length;
1488 char *separator;
1489 int blocksize;
1491 /* skip command character */
1492 packet += 23;
1494 offset = strtoul(packet, &separator, 16);
1495 length = strtoul(separator + 1, &separator, 16);
1497 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1499 int i = 0;
1500 for (;;)
1502 p = get_flash_bank_by_num(i);
1503 if (p == NULL)
1504 break;
1506 /* if device has uneven sector sizes, eg. str7, lpc
1507 * we pass the smallest sector size to gdb memory map */
1508 blocksize = gdb_calc_blocksize(p);
1510 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1511 "<property name=\"blocksize\">0x%x</property>\n" \
1512 "</memory>\n", \
1513 p->base, p->size, blocksize);
1514 i++;
1517 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1519 if (retval != ERROR_OK)
1521 gdb_send_error(connection, retval);
1522 return retval;
1525 if (offset + length > pos)
1527 length = pos - offset;
1530 char *t = malloc(length + 1);
1531 t[0] = 'l';
1532 memcpy(t + 1, xml + offset, length);
1533 gdb_put_packet(connection, t, length + 1);
1535 free(t);
1536 free(xml);
1537 return ERROR_OK;
1539 else if (strstr(packet, "qXfer:features:read:"))
1541 char *xml = NULL;
1542 int size = 0;
1543 int pos = 0;
1544 int retval = ERROR_OK;
1546 int offset;
1547 int length;
1548 char *annex;
1550 /* skip command character */
1551 packet += 20;
1553 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1555 gdb_send_error(connection, 01);
1556 return ERROR_OK;
1559 if (strcmp(annex, "target.xml") != 0)
1561 gdb_send_error(connection, 01);
1562 return ERROR_OK;
1565 xml_printf(&retval, &xml, &pos, &size, \
1566 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1568 if (retval != ERROR_OK)
1570 gdb_send_error(connection, retval);
1571 return retval;
1574 gdb_put_packet(connection, xml, strlen(xml) + 1);
1576 free(xml);
1577 return ERROR_OK;
1580 gdb_put_packet(connection, "", 0);
1581 return ERROR_OK;
1584 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1586 gdb_connection_t *gdb_connection = connection->priv;
1587 gdb_service_t *gdb_service = connection->service->priv;
1588 int result;
1590 /* if flash programming disabled - send a empty reply */
1592 if (gdb_flash_program == 0)
1594 gdb_put_packet(connection, "", 0);
1595 return ERROR_OK;
1598 if (strstr(packet, "vFlashErase:"))
1600 unsigned long addr;
1601 unsigned long length;
1603 char *parse = packet + 12;
1604 if (*parse == '\0')
1606 ERROR("incomplete vFlashErase packet received, dropping connection");
1607 return ERROR_SERVER_REMOTE_CLOSED;
1610 addr = strtoul(parse, &parse, 16);
1612 if (*(parse++) != ',' || *parse == '\0')
1614 ERROR("incomplete vFlashErase packet received, dropping connection");
1615 return ERROR_SERVER_REMOTE_CLOSED;
1618 length = strtoul(parse, &parse, 16);
1620 if (*parse != '\0')
1622 ERROR("incomplete vFlashErase packet received, dropping connection");
1623 return ERROR_SERVER_REMOTE_CLOSED;
1626 /* disable gdb output while programming */
1627 gdb_connection->output_disable = 1;
1629 /* assume all sectors need erasing - stops any problems
1630 * when flash_write is called multiple times */
1631 flash_set_dirty();
1633 /* perform any target specific operations before the erase */
1634 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1636 /* perform erase */
1637 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1639 /* GDB doesn't evaluate the actual error number returned,
1640 * treat a failed erase as an I/O error
1642 gdb_send_error(connection, EIO);
1643 ERROR("flash_erase returned %i", result);
1645 else
1646 gdb_put_packet(connection, "OK", 2);
1648 /* reenable gdb output */
1649 gdb_connection->output_disable = 0;
1651 return ERROR_OK;
1654 if (strstr(packet, "vFlashWrite:"))
1656 unsigned long addr;
1657 unsigned long length;
1658 char *parse = packet + 12;
1660 if (*parse == '\0')
1662 ERROR("incomplete vFlashErase packet received, dropping connection");
1663 return ERROR_SERVER_REMOTE_CLOSED;
1665 addr = strtoul(parse, &parse, 16);
1666 if (*(parse++) != ':')
1668 ERROR("incomplete vFlashErase packet received, dropping connection");
1669 return ERROR_SERVER_REMOTE_CLOSED;
1671 length = packet_size - (parse - packet);
1673 /* disable gdb output while programming */
1674 gdb_connection->output_disable = 1;
1676 /* create a new image if there isn't already one */
1677 if (gdb_connection->vflash_image == NULL)
1679 gdb_connection->vflash_image = malloc(sizeof(image_t));
1680 image_open(gdb_connection->vflash_image, "", "build");
1683 /* create new section with content from packet buffer */
1684 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1686 gdb_put_packet(connection, "OK", 2);
1688 /* reenable gdb output */
1689 gdb_connection->output_disable = 0;
1691 return ERROR_OK;
1694 if (!strcmp(packet, "vFlashDone"))
1696 u32 written;
1697 char *error_str;
1699 /* disable gdb output while programming */
1700 gdb_connection->output_disable = 1;
1702 /* process the flashing buffer. No need to erase as GDB
1703 * always issues a vFlashErase first. */
1704 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1706 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1707 gdb_put_packet(connection, "E.memtype", 9);
1708 else
1709 gdb_send_error(connection, EIO);
1711 if (error_str)
1713 ERROR("flash writing failed: %s", error_str);
1714 free(error_str);
1717 else
1719 DEBUG("wrote %u bytes from vFlash image to flash", written);
1720 gdb_put_packet(connection, "OK", 2);
1723 image_close(gdb_connection->vflash_image);
1724 free(gdb_connection->vflash_image);
1725 gdb_connection->vflash_image = NULL;
1727 /* reenable gdb output */
1728 gdb_connection->output_disable = 0;
1730 return ERROR_OK;
1733 gdb_put_packet(connection, "", 0);
1734 return ERROR_OK;
1737 int gdb_detach(connection_t *connection, target_t *target)
1739 switch( detach_mode )
1741 case GDB_DETACH_RESUME:
1742 target->type->resume(target, 1, 0, 1, 0);
1743 break;
1745 case GDB_DETACH_RESET:
1746 target_process_reset(connection->cmd_ctx);
1747 break;
1749 case GDB_DETACH_HALT:
1750 target->type->halt(target);
1751 break;
1753 case GDB_DETACH_NOTHING:
1754 break;
1757 gdb_put_packet(connection, "OK", 2);
1759 return ERROR_OK;
1762 int gdb_input(connection_t *connection)
1764 gdb_service_t *gdb_service = connection->service->priv;
1765 target_t *target = gdb_service->target;
1766 char packet[GDB_BUFFER_SIZE];
1767 int packet_size;
1768 int retval;
1769 gdb_connection_t *gdb_con = connection->priv;
1770 static int extended_protocol = 0;
1772 /* drain input buffer */
1775 packet_size = GDB_BUFFER_SIZE-1;
1776 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1778 switch (retval)
1780 case ERROR_GDB_BUFFER_TOO_SMALL:
1781 ERROR("BUG: buffer supplied for gdb packet was too small");
1782 exit(-1);
1783 case ERROR_SERVER_REMOTE_CLOSED:
1784 return ERROR_SERVER_REMOTE_CLOSED;
1785 default:
1786 ERROR("BUG: unexpected error");
1787 exit(-1);
1791 /* terminate with zero */
1792 packet[packet_size] = 0;
1794 DEBUG("received packet: '%s'", packet);
1796 if (packet_size > 0)
1798 retval = ERROR_OK;
1799 switch (packet[0])
1801 case 'H':
1802 /* Hct... -- set thread
1803 * we don't have threads, send empty reply */
1804 gdb_put_packet(connection, NULL, 0);
1805 break;
1806 case 'q':
1807 retval = gdb_query_packet(connection, target, packet, packet_size);
1808 break;
1809 case 'g':
1810 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1811 break;
1812 case 'G':
1813 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1814 break;
1815 case 'p':
1816 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1817 break;
1818 case 'P':
1819 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1820 break;
1821 case 'm':
1822 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1823 break;
1824 case 'M':
1825 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1826 break;
1827 case 'z':
1828 case 'Z':
1829 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1830 break;
1831 case '?':
1832 gdb_last_signal_packet(connection, target, packet, packet_size);
1833 break;
1834 case 'c':
1835 case 's':
1836 gdb_step_continue_packet(connection, target, packet, packet_size);
1837 break;
1838 case 'v':
1839 retval = gdb_v_packet(connection, target, packet, packet_size);
1840 break;
1841 case 'D':
1842 retval = gdb_detach(connection, target);
1843 extended_protocol = 0;
1844 break;
1845 case 'X':
1846 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1847 return retval;
1848 break;
1849 case 'k':
1850 if (extended_protocol != 0)
1851 break;
1852 gdb_put_packet(connection, "OK", 2);
1853 return ERROR_SERVER_REMOTE_CLOSED;
1854 case '!':
1855 /* handle extended remote protocol */
1856 extended_protocol = 1;
1857 gdb_put_packet(connection, "OK", 2);
1858 break;
1859 case 'R':
1860 /* handle extended restart packet */
1861 target_process_reset(connection->cmd_ctx);
1862 break;
1863 default:
1864 /* ignore unkown packets */
1865 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1866 gdb_put_packet(connection, NULL, 0);
1867 break;
1870 /* if a packet handler returned an error, exit input loop */
1871 if (retval != ERROR_OK)
1872 return retval;
1875 if (gdb_con->ctrl_c)
1877 if (target->state == TARGET_RUNNING)
1879 target->type->halt(target);
1880 gdb_con->ctrl_c = 0;
1884 } while (gdb_con->buf_cnt > 0);
1886 return ERROR_OK;
1889 int gdb_init()
1891 gdb_service_t *gdb_service;
1892 target_t *target = targets;
1893 int i = 0;
1895 if (!target)
1897 WARNING("no gdb ports allocated as no target has been specified");
1898 return ERROR_OK;
1901 if (gdb_port == 0)
1903 WARNING("no gdb port specified, using default port 3333");
1904 gdb_port = 3333;
1907 while (target)
1909 char service_name[8];
1911 snprintf(service_name, 8, "gdb-%2.2i", i);
1913 gdb_service = malloc(sizeof(gdb_service_t));
1914 gdb_service->target = target;
1916 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1918 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1920 i++;
1921 target = target->next;
1924 return ERROR_OK;
1927 /* daemon configuration command gdb_port */
1928 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1930 if (argc == 0)
1931 return ERROR_OK;
1933 /* only if the port wasn't overwritten by cmdline */
1934 if (gdb_port == 0)
1935 gdb_port = strtoul(args[0], NULL, 0);
1937 return ERROR_OK;
1940 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1942 if (argc == 1)
1944 if (strcmp(args[0], "resume") == 0)
1946 detach_mode = GDB_DETACH_RESUME;
1947 return ERROR_OK;
1949 else if (strcmp(args[0], "reset") == 0)
1951 detach_mode = GDB_DETACH_RESET;
1952 return ERROR_OK;
1954 else if (strcmp(args[0], "halt") == 0)
1956 detach_mode = GDB_DETACH_HALT;
1957 return ERROR_OK;
1959 else if (strcmp(args[0], "nothing") == 0)
1961 detach_mode = GDB_DETACH_NOTHING;
1962 return ERROR_OK;
1966 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1967 return ERROR_OK;
1970 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1972 if (argc == 1)
1974 if (strcmp(args[0], "enable") == 0)
1976 gdb_use_memory_map = 1;
1977 return ERROR_OK;
1979 else if (strcmp(args[0], "disable") == 0)
1981 gdb_use_memory_map = 0;
1982 return ERROR_OK;
1986 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1987 return ERROR_OK;
1990 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1992 if (argc == 1)
1994 if (strcmp(args[0], "enable") == 0)
1996 gdb_flash_program = 1;
1997 return ERROR_OK;
1999 else if (strcmp(args[0], "disable") == 0)
2001 gdb_flash_program = 0;
2002 return ERROR_OK;
2006 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2007 return ERROR_OK;
2010 int gdb_register_commands(command_context_t *command_context)
2012 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2013 COMMAND_CONFIG, "");
2014 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2015 COMMAND_CONFIG, "");
2016 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2017 COMMAND_CONFIG, "");
2018 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2019 COMMAND_CONFIG, "");
2020 return ERROR_OK;