- fix gdb packet retransmission omission
[openocd.git] / src / server / gdb_server.c
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
23
24 #include "replacements.h"
25
26 #include "gdb_server.h"
27
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"
35
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40
41 #if 0
42 #define _DEBUG_GDB_IO_
43 #endif
44
45 static unsigned short gdb_port;
46
47 enum gdb_detach_mode
48 {
49 GDB_DETACH_RESUME,
50 GDB_DETACH_RESET,
51 GDB_DETACH_HALT,
52 GDB_DETACH_NOTHING
53 };
54
55 /* target behaviour on gdb detach */
56 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
57
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;
62
63 int gdb_last_signal(target_t *target)
64 {
65 switch (target->debug_reason)
66 {
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);
80 }
81 }
82
83 int gdb_get_char(connection_t *connection, int* next_char)
84 {
85 gdb_connection_t *gdb_con = connection->priv;
86
87 #ifdef _DEBUG_GDB_IO_
88 char *debug_buffer;
89 #endif
90
91 if (gdb_con->buf_cnt-- > 0)
92 {
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;
98
99 #ifdef _DEBUG_GDB_IO_
100 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
101 #endif
102
103 return ERROR_OK;
104 }
105
106 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
107 {
108 if (gdb_con->buf_cnt == 0)
109 return ERROR_SERVER_REMOTE_CLOSED;
110
111 #ifdef _WIN32
112 errno = WSAGetLastError();
113
114 switch(errno)
115 {
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);
126 }
127 #else
128 switch(errno)
129 {
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);
140 }
141 #endif
142 }
143
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
151
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
162
163 return ERROR_OK;
164 }
165
166 int gdb_putback_char(connection_t *connection, int last_char)
167 {
168 gdb_connection_t *gdb_con = connection->priv;
169
170 if (gdb_con->buf_p > gdb_con->buffer)
171 {
172 *(--gdb_con->buf_p) = last_char;
173 gdb_con->buf_cnt++;
174 }
175 else
176 {
177 ERROR("BUG: couldn't put character back");
178 }
179
180 return ERROR_OK;
181 }
182
183 int gdb_put_packet(connection_t *connection, char *buffer, int len)
184 {
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;
194
195 for (i = 0; i < len; i++)
196 my_checksum += buffer[i];
197
198 while (1)
199 {
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);
211
212 snprintf(checksum, 3, "%2.2x", my_checksum);
213
214 write_socket(connection->fd, checksum, 2);
215
216 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
217 return retval;
218
219 if (reply == '+')
220 break;
221 else if (reply == '-')
222 WARNING("negative reply, retrying");
223 else if (reply == 0x3)
224 {
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
233 {
234 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
235 return ERROR_SERVER_REMOTE_CLOSED;
236 }
237 }
238 else
239 {
240 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
241 return ERROR_SERVER_REMOTE_CLOSED;
242 }
243 }
244
245 return ERROR_OK;
246 }
247
248 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
249 {
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;
256
257 while (1)
258 {
259 do
260 {
261 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
262 return retval;
263
264 #ifdef _DEBUG_GDB_IO_
265 DEBUG("character: '%c'", character);
266 #endif
267
268 switch (character)
269 {
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;
285 }
286 } while (character != '$');
287
288 my_checksum = 0;
289 count = 0;
290
291 for (;;)
292 {
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.
296 */
297 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
298 {
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
302 */
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)
309 {
310 character = *buf++;
311 i++;
312 if (character == '#')
313 {
314 /* Danger! character can be '#' when esc is
315 * used so we need an explicit boolean for done here.
316 */
317 done = 1;
318 break;
319 }
320
321 if (character == '}')
322 {
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
331 {
332 my_checksum += character & 0xff;
333 buffer[count++] = character & 0xff;
334 }
335 }
336 gdb_con->buf_p += i;
337 gdb_con->buf_cnt -= i;
338 if (done)
339 break;
340 }
341 if (count > *len)
342 {
343 ERROR("packet buffer too small");
344 return ERROR_GDB_BUFFER_TOO_SMALL;
345 }
346
347 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
348 return retval;
349
350 if (character == '#')
351 break;
352
353 if (character == '}')
354 {
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;
362 }
363 else
364 {
365 my_checksum += character & 0xff;
366 buffer[count++] = character & 0xff;
367 }
368
369 }
370
371 *len = count;
372
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;
380
381 if (my_checksum == strtoul(checksum, NULL, 16))
382 {
383 write_socket(connection->fd, "+", 1);
384 break;
385 }
386
387 WARNING("checksum error, requesting retransmission");
388 write_socket(connection->fd, "-", 1);
389 }
390
391 return ERROR_OK;
392 }
393
394 int gdb_output(struct command_context_s *context, char* line)
395 {
396 connection_t *connection = context->output_handler_priv;
397 gdb_connection_t *gdb_connection = connection->priv;
398
399 char *hex_buffer;
400 int i, bin_size;
401
402 /* check if output is enabled */
403 if (gdb_connection->output_disable)
404 {
405 return ERROR_OK;
406 }
407
408 bin_size = strlen(line);
409
410 hex_buffer = malloc(bin_size*2 + 4);
411
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;
418
419 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
420
421 free(hex_buffer);
422 return ERROR_OK;
423 }
424
425 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
426 {
427 FILE *script;
428 struct command_context_s *cmd_ctx = priv;
429
430 if (target->gdb_program_script)
431 {
432 script = fopen(target->gdb_program_script, "r");
433 if (!script)
434 {
435 ERROR("couldn't open script file %s", target->gdb_program_script);
436 return ERROR_OK;
437 }
438
439 INFO("executing gdb_program script '%s'", target->gdb_program_script);
440 command_run_file(cmd_ctx, script, COMMAND_EXEC);
441 fclose(script);
442
443 jtag_execute_queue();
444 }
445
446 return ERROR_OK;
447 }
448
449 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
450 {
451 connection_t *connection = priv;
452 gdb_connection_t *gdb_connection = connection->priv;
453 char sig_reply[4];
454 int signal;
455
456 switch (event)
457 {
458 case TARGET_EVENT_HALTED:
459 if (gdb_connection->frontend_state == TARGET_RUNNING)
460 {
461 if (gdb_connection->ctrl_c)
462 {
463 signal = 0x2;
464 gdb_connection->ctrl_c = 0;
465 }
466 else
467 {
468 signal = gdb_last_signal(target);
469 }
470
471 snprintf(sig_reply, 4, "T%2.2x", signal);
472 gdb_put_packet(connection, sig_reply, 3);
473 gdb_connection->frontend_state = TARGET_HALTED;
474 }
475 break;
476 case TARGET_EVENT_RESUMED:
477 if (gdb_connection->frontend_state == TARGET_HALTED)
478 {
479 gdb_connection->frontend_state = TARGET_RUNNING;
480 }
481 break;
482 case TARGET_EVENT_GDB_PROGRAM:
483 gdb_program_handler(target, event, connection->cmd_ctx);
484 break;
485 default:
486 break;
487 }
488
489 return ERROR_OK;
490 }
491
492 int gdb_new_connection(connection_t *connection)
493 {
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;
498
499 connection->priv = gdb_connection;
500
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;
508
509 /* output goes through gdb connection */
510 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
511
512 /* register callback to be informed about target events */
513 target_register_event_callback(gdb_target_callback_event_handler, connection);
514
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))
518 {
519 ERROR("error when trying to halt target");
520 exit(-1);
521 }
522
523 while (gdb_service->target->state != TARGET_HALTED)
524 {
525 gdb_service->target->type->poll(gdb_service->target);
526 }
527
528 /* remove the initial ACK from the incoming buffer */
529 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
530 return retval;
531
532 if (initial_ack != '+')
533 gdb_putback_char(connection, initial_ack);
534
535 return ERROR_OK;
536 }
537
538 int gdb_connection_closed(connection_t *connection)
539 {
540 gdb_service_t *gdb_service = connection->service->priv;
541 gdb_connection_t *gdb_connection = connection->priv;
542
543 /* see if an image built with vFlash commands is left */
544 if (gdb_connection->vflash_image)
545 {
546 image_close(gdb_connection->vflash_image);
547 free(gdb_connection->vflash_image);
548 gdb_connection->vflash_image = NULL;
549 }
550
551 /* if this connection registered a debug-message receiver delete it */
552 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
553
554 if (connection->priv)
555 {
556 free(connection->priv);
557 connection->priv = NULL;
558 }
559 else
560 {
561 ERROR("BUG: connection->priv == NULL");
562 }
563
564 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
565
566 return ERROR_OK;
567 }
568
569 void gdb_send_error(connection_t *connection, u8 the_error)
570 {
571 char err[4];
572 snprintf(err, 4, "E%2.2X", the_error );
573 gdb_put_packet(connection, err, 3);
574 }
575
576 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
577 {
578 char sig_reply[4];
579 int signal;
580
581 signal = gdb_last_signal(target);
582
583 snprintf(sig_reply, 4, "S%2.2x", signal);
584 gdb_put_packet(connection, sig_reply, 3);
585
586 return ERROR_OK;
587 }
588
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)
593 {
594 static const char *DIGITS = "0123456789abcdef";
595 int i;
596
597 u8 *buf;
598 int buf_len;
599 buf = reg->value;
600 buf_len = CEIL(reg->size, 8);
601
602 if (target->endianness == TARGET_LITTLE_ENDIAN)
603 {
604 for (i = 0; i < buf_len; i++)
605 {
606 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
607 tstr[i*2+1] = DIGITS[buf[i]&0xf];
608 }
609 }
610 else
611 {
612 for (i = 0; i < buf_len; i++)
613 {
614 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
615 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
616 }
617 }
618 }
619
620 void gdb_target_to_str(target_t *target, char *tstr, char *str)
621 {
622 int str_len = strlen(tstr);
623 int i;
624
625 if (str_len % 2)
626 {
627 ERROR("BUG: gdb value with uneven number of characters encountered");
628 exit(-1);
629 }
630
631 if (target->endianness == TARGET_LITTLE_ENDIAN)
632 {
633 for (i = 0; i < str_len; i+=2)
634 {
635 str[str_len - i - 1] = tstr[i + 1];
636 str[str_len - i - 2] = tstr[i];
637 }
638 }
639 else
640 {
641 for (i = 0; i < str_len; i++)
642 {
643 str[i] = tstr[i];
644 }
645 }
646 }
647
648 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
649 {
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;
657
658 #ifdef _DEBUG_GDB_IO_
659 DEBUG("-");
660 #endif
661
662 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
663 {
664 switch (retval)
665 {
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);
673 }
674 }
675
676 for (i = 0; i < reg_list_size; i++)
677 {
678 reg_packet_size += reg_list[i]->size;
679 }
680
681 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
682 reg_packet_p = reg_packet;
683
684 for (i = 0; i < reg_list_size; i++)
685 {
686 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
687 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
688 }
689
690 #ifdef _DEBUG_GDB_IO_
691 {
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);
696 }
697 #endif
698
699 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
700 free(reg_packet);
701
702 free(reg_list);
703
704 return ERROR_OK;
705 }
706
707 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
708 {
709 int i;
710 reg_t **reg_list;
711 int reg_list_size;
712 int retval;
713 char *packet_p;
714
715 #ifdef _DEBUG_GDB_IO_
716 DEBUG("-");
717 #endif
718
719 /* skip command character */
720 packet++;
721 packet_size--;
722
723 if (packet_size % 2)
724 {
725 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
726 return ERROR_SERVER_REMOTE_CLOSED;
727 }
728
729 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
730 {
731 switch (retval)
732 {
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);
740 }
741 }
742
743 packet_p = packet;
744 for (i = 0; i < reg_list_size; i++)
745 {
746 u8 *bin_buf;
747 char *hex_buf;
748 reg_arch_type_t *arch_type;
749
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);
753
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);
757
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)
761 {
762 ERROR("BUG: encountered unregistered arch type");
763 exit(-1);
764 }
765 arch_type->set(reg_list[i], bin_buf);
766
767 /* advance packet pointer */
768 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
769
770 free(bin_buf);
771 free(hex_buf);
772 }
773
774 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
775 free(reg_list);
776
777 gdb_put_packet(connection, "OK", 2);
778
779 return ERROR_OK;
780 }
781
782 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
783 {
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;
789
790 #ifdef _DEBUG_GDB_IO_
791 DEBUG("-");
792 #endif
793
794 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
795 {
796 switch (retval)
797 {
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);
805 }
806 }
807
808 if (reg_list_size <= reg_num)
809 {
810 ERROR("gdb requested a non-existing register");
811 exit(-1);
812 }
813
814 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
815
816 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
817
818 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
819
820 free(reg_list);
821 free(reg_packet);
822
823 return ERROR_OK;
824 }
825
826 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
827 {
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;
836
837 DEBUG("-");
838
839 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
840 {
841 switch (retval)
842 {
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);
850 }
851 }
852
853 if (reg_list_size < reg_num)
854 {
855 ERROR("gdb requested a non-existing register");
856 return ERROR_SERVER_REMOTE_CLOSED;
857 }
858
859 if (*separator != '=')
860 {
861 ERROR("GDB 'set register packet', but no '=' following the register number");
862 return ERROR_SERVER_REMOTE_CLOSED;
863 }
864
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);
868
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);
872
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)
876 {
877 ERROR("BUG: encountered unregistered arch type");
878 exit(-1);
879 }
880 arch_type->set(reg_list[reg_num], bin_buf);
881
882 gdb_put_packet(connection, "OK", 2);
883
884 free(bin_buf);
885 free(hex_buf);
886 free(reg_list);
887
888 return ERROR_OK;
889 }
890
891 int gdb_memory_packet_error(connection_t *connection, int retval)
892 {
893 switch (retval)
894 {
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);
911 }
912
913 return ERROR_OK;
914 }
915
916 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
917 {
918 char *separator;
919 u32 addr = 0;
920 u32 len = 0;
921
922 u8 *buffer;
923 char *hex_buffer;
924
925 int i;
926 int retval;
927
928 /* skip command character */
929 packet++;
930
931 addr = strtoul(packet, &separator, 16);
932
933 if (*separator != ',')
934 {
935 ERROR("incomplete read memory packet received, dropping connection");
936 return ERROR_SERVER_REMOTE_CLOSED;
937 }
938
939 len = strtoul(separator+1, NULL, 16);
940
941 buffer = malloc(len);
942
943 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
944
945 switch (len)
946 {
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);
964 }
965
966 #if 0
967 if (retval == ERROR_TARGET_DATA_ABORT)
968 {
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
972 */
973 memset(buffer, 0, len);
974 retval = ERROR_OK;
975 }
976 #endif
977
978 if (retval == ERROR_OK)
979 {
980 hex_buffer = malloc(len * 2 + 1);
981
982 for (i=0; i<len; i++)
983 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
984
985 gdb_put_packet(connection, hex_buffer, len * 2);
986
987 free(hex_buffer);
988 }
989 else
990 {
991 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
992 return retval;
993 }
994
995 free(buffer);
996
997 return ERROR_OK;
998 }
999
1000 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1001 {
1002 char *separator;
1003 u32 addr = 0;
1004 u32 len = 0;
1005
1006 u8 *buffer;
1007
1008 int i;
1009 int retval;
1010
1011 /* skip command character */
1012 packet++;
1013
1014 addr = strtoul(packet, &separator, 16);
1015
1016 if (*separator != ',')
1017 {
1018 ERROR("incomplete write memory packet received, dropping connection");
1019 return ERROR_SERVER_REMOTE_CLOSED;
1020 }
1021
1022 len = strtoul(separator+1, &separator, 16);
1023
1024 if (*(separator++) != ':')
1025 {
1026 ERROR("incomplete write memory packet received, dropping connection");
1027 return ERROR_SERVER_REMOTE_CLOSED;
1028 }
1029
1030 buffer = malloc(len);
1031
1032 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1033
1034 for (i=0; i<len; i++)
1035 {
1036 u32 tmp;
1037 sscanf(separator + 2*i, "%2x", &tmp);
1038 buffer[i] = tmp;
1039 }
1040
1041 retval = ERROR_OK;
1042 switch (len)
1043 {
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;
1065 }
1066
1067 if (retval == ERROR_OK)
1068 {
1069 gdb_put_packet(connection, "OK", 2);
1070 }
1071 else
1072 {
1073 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1074 return retval;
1075 }
1076
1077 free(buffer);
1078
1079 return ERROR_OK;
1080 }
1081
1082 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1083 {
1084 char *separator;
1085 u32 addr = 0;
1086 u32 len = 0;
1087
1088 u8 *buffer;
1089 int retval;
1090
1091 /* skip command character */
1092 packet++;
1093
1094 addr = strtoul(packet, &separator, 16);
1095
1096 if (*separator != ',')
1097 {
1098 ERROR("incomplete write memory binary packet received, dropping connection");
1099 return ERROR_SERVER_REMOTE_CLOSED;
1100 }
1101
1102 len = strtoul(separator+1, &separator, 16);
1103
1104 if (*(separator++) != ':')
1105 {
1106 ERROR("incomplete write memory binary packet received, dropping connection");
1107 return ERROR_SERVER_REMOTE_CLOSED;
1108 }
1109
1110 retval = ERROR_OK;
1111 if( len ) {
1112
1113 buffer = malloc(len);
1114
1115 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1116
1117 memcpy( buffer, separator, len );
1118
1119 switch (len)
1120 {
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;
1140 }
1141
1142 free(buffer);
1143 }
1144
1145 if (retval == ERROR_OK)
1146 {
1147 gdb_put_packet(connection, "OK", 2);
1148 }
1149 else
1150 {
1151 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1152 return retval;
1153 }
1154
1155 return ERROR_OK;
1156 }
1157
1158 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1159 {
1160 int current = 0;
1161 u32 address = 0x0;
1162
1163 DEBUG("-");
1164
1165 if (packet_size > 1)
1166 {
1167 packet[packet_size] = 0;
1168 address = strtoul(packet + 1, NULL, 16);
1169 }
1170 else
1171 {
1172 current = 1;
1173 }
1174
1175 if (packet[0] == 'c')
1176 {
1177 DEBUG("continue");
1178 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1179 }
1180 else if (packet[0] == 's')
1181 {
1182 DEBUG("step");
1183 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1184 }
1185 }
1186
1187 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1188 {
1189 switch (retval)
1190 {
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);
1201 }
1202
1203 return ERROR_OK;
1204 }
1205
1206 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1207 {
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;
1215
1216 DEBUG("-");
1217
1218 type = strtoul(packet + 1, &separator, 16);
1219
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;
1230
1231 if (*separator != ',')
1232 {
1233 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1234 return ERROR_SERVER_REMOTE_CLOSED;
1235 }
1236
1237 address = strtoul(separator+1, &separator, 16);
1238
1239 if (*separator != ',')
1240 {
1241 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1242 return ERROR_SERVER_REMOTE_CLOSED;
1243 }
1244
1245 size = strtoul(separator+1, &separator, 16);
1246
1247 switch (type)
1248 {
1249 case 0:
1250 case 1:
1251 if (packet[0] == 'Z')
1252 {
1253 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1254 {
1255 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1256 return retval;
1257 }
1258 else
1259 {
1260 gdb_put_packet(connection, "OK", 2);
1261 }
1262 }
1263 else
1264 {
1265 breakpoint_remove(target, address);
1266 gdb_put_packet(connection, "OK", 2);
1267 }
1268 break;
1269 case 2:
1270 case 3:
1271 case 4:
1272 {
1273 if (packet[0] == 'Z')
1274 {
1275 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1276 {
1277 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1278 return retval;
1279 }
1280 else
1281 {
1282 gdb_put_packet(connection, "OK", 2);
1283 }
1284 }
1285 else
1286 {
1287 watchpoint_remove(target, address);
1288 gdb_put_packet(connection, "OK", 2);
1289 }
1290 break;
1291 }
1292 default:
1293 break;
1294 }
1295
1296 return ERROR_OK;
1297 }
1298
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, ...)
1301 {
1302 if (*retval != ERROR_OK)
1303 {
1304 return;
1305 }
1306 int first = 1;
1307
1308 for (;;)
1309 {
1310 if ((*xml == NULL) || (!first))
1311 {
1312 /* start by 0 to exercise all the code paths.
1313 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1314
1315 *size = *size * 2 + 2;
1316 char *t = *xml;
1317 *xml = realloc(*xml, *size);
1318 if (*xml == NULL)
1319 {
1320 if (t)
1321 free(t);
1322 *retval = ERROR_SERVER_REMOTE_CLOSED;
1323 return;
1324 }
1325 }
1326
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))
1333 {
1334 *pos += ret;
1335 return;
1336 }
1337 /* there was just enough or not enough space, allocate more. */
1338 first = 0;
1339 }
1340 }
1341
1342 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1343 {
1344 char *separator;
1345
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;
1353
1354 /* After the read marker and annex, qXfer looks like a
1355 * traditional 'm' packet. */
1356
1357 *ofs = strtoul(buf, &separator, 16);
1358
1359 if (*separator != ',')
1360 return -1;
1361
1362 *len = strtoul(separator+1, NULL, 16);
1363
1364 return 0;
1365 }
1366
1367 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1368 {
1369 command_context_t *cmd_ctx = connection->cmd_ctx;
1370
1371 if (strstr(packet, "qRcmd,"))
1372 {
1373 if (packet_size > 6)
1374 {
1375 char *cmd;
1376 int i;
1377 cmd = malloc((packet_size - 6)/2 + 1);
1378 for (i=0; i < (packet_size - 6)/2; i++)
1379 {
1380 u32 tmp;
1381 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1382 cmd[i] = tmp;
1383 }
1384 cmd[(packet_size - 6)/2] = 0x0;
1385 target_call_timer_callbacks();
1386 command_run_line(cmd_ctx, cmd);
1387 free(cmd);
1388 }
1389 gdb_put_packet(connection, "OK", 2);
1390 return ERROR_OK;
1391 }
1392 else if (strstr(packet, "qCRC:"))
1393 {
1394 if (packet_size > 5)
1395 {
1396 int retval;
1397 u8 gdb_reply[10];
1398 char *separator;
1399 u32 checksum;
1400 u32 addr = 0;
1401 u32 len = 0;
1402
1403 /* skip command character */
1404 packet += 5;
1405
1406 addr = strtoul(packet, &separator, 16);
1407
1408 if (*separator != ',')
1409 {
1410 ERROR("incomplete read memory packet received, dropping connection");
1411 return ERROR_SERVER_REMOTE_CLOSED;
1412 }
1413
1414 len = strtoul(separator + 1, NULL, 16);
1415
1416 retval = target_checksum_memory(target, addr, len, &checksum);
1417
1418 if (retval == ERROR_OK)
1419 {
1420 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1421 gdb_put_packet(connection, gdb_reply, 9);
1422 }
1423 else
1424 {
1425 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1426 return retval;
1427 }
1428
1429 return ERROR_OK;
1430 }
1431 }
1432 else if (strstr(packet, "qSupported"))
1433 {
1434 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1435 * disable qXfer:features:read for the moment */
1436 int retval = ERROR_OK;
1437 char *buffer = NULL;
1438 int pos = 0;
1439 int size = 0;
1440
1441 xml_printf(&retval, &buffer, &pos, &size,
1442 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1443 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1444
1445 if (retval != ERROR_OK)
1446 {
1447 gdb_send_error(connection, 01);
1448 return ERROR_OK;
1449 }
1450
1451 gdb_put_packet(connection, buffer, strlen(buffer));
1452 free(buffer);
1453
1454 return ERROR_OK;
1455 }
1456 else if (strstr(packet, "qXfer:memory-map:read::"))
1457 {
1458 /* We get away with only specifying flash here. Regions that are not
1459 * specified are treated as if we provided no memory map(if not we
1460 * could detect the holes and mark them as RAM).
1461 * Normally we only execute this code once, but no big deal if we
1462 * have to regenerate it a couple of times. */
1463
1464 flash_bank_t *p;
1465 char *xml = NULL;
1466 int size = 0;
1467 int pos = 0;
1468 int retval = ERROR_OK;
1469
1470 int offset;
1471 int length;
1472 char *separator;
1473
1474 /* skip command character */
1475 packet += 23;
1476
1477 offset = strtoul(packet, &separator, 16);
1478 length = strtoul(separator + 1, &separator, 16);
1479
1480 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1481
1482 int i = 0;
1483 for (;;)
1484 {
1485 p = get_flash_bank_by_num(i);
1486 if (p == NULL)
1487 break;
1488
1489 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1490 "<property name=\"blocksize\">0x%x</property>\n" \
1491 "</memory>\n", \
1492 p->base, p->size, p->size/p->num_sectors);
1493 i++;
1494 }
1495
1496 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1497
1498 if (retval != ERROR_OK)
1499 {
1500 gdb_send_error(connection, retval);
1501 return retval;
1502 }
1503
1504 if (offset + length > pos)
1505 {
1506 length = pos - offset;
1507 }
1508
1509 char *t = malloc(length + 1);
1510 t[0] = 'l';
1511 memcpy(t + 1, xml + offset, length);
1512 gdb_put_packet(connection, t, length + 1);
1513
1514 free(t);
1515 free(xml);
1516 return ERROR_OK;
1517 }
1518 else if (strstr(packet, "qXfer:features:read:"))
1519 {
1520 char *xml = NULL;
1521 int size = 0;
1522 int pos = 0;
1523 int retval = ERROR_OK;
1524
1525 int offset;
1526 int length;
1527 char *annex;
1528
1529 /* skip command character */
1530 packet += 20;
1531
1532 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1533 {
1534 gdb_send_error(connection, 01);
1535 return ERROR_OK;
1536 }
1537
1538 if (strcmp(annex, "target.xml") != 0)
1539 {
1540 gdb_send_error(connection, 01);
1541 return ERROR_OK;
1542 }
1543
1544 xml_printf(&retval, &xml, &pos, &size, \
1545 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1546
1547 if (retval != ERROR_OK)
1548 {
1549 gdb_send_error(connection, retval);
1550 return retval;
1551 }
1552
1553 gdb_put_packet(connection, xml, strlen(xml) + 1);
1554
1555 free(xml);
1556 return ERROR_OK;
1557 }
1558
1559 gdb_put_packet(connection, "", 0);
1560 return ERROR_OK;
1561 }
1562
1563 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1564 {
1565 gdb_connection_t *gdb_connection = connection->priv;
1566 gdb_service_t *gdb_service = connection->service->priv;
1567 int result;
1568
1569 /* if flash programming disabled - send a empty reply */
1570
1571 if (gdb_flash_program == 0)
1572 {
1573 gdb_put_packet(connection, "", 0);
1574 return ERROR_OK;
1575 }
1576
1577 if (strstr(packet, "vFlashErase:"))
1578 {
1579 unsigned long addr;
1580 unsigned long length;
1581
1582 char *parse = packet + 12;
1583 if (*parse == '\0')
1584 {
1585 ERROR("incomplete vFlashErase packet received, dropping connection");
1586 return ERROR_SERVER_REMOTE_CLOSED;
1587 }
1588
1589 addr = strtoul(parse, &parse, 16);
1590
1591 if (*(parse++) != ',' || *parse == '\0')
1592 {
1593 ERROR("incomplete vFlashErase packet received, dropping connection");
1594 return ERROR_SERVER_REMOTE_CLOSED;
1595 }
1596
1597 length = strtoul(parse, &parse, 16);
1598
1599 if (*parse != '\0')
1600 {
1601 ERROR("incomplete vFlashErase packet received, dropping connection");
1602 return ERROR_SERVER_REMOTE_CLOSED;
1603 }
1604
1605 /* disable gdb output while programming */
1606 gdb_connection->output_disable = 1;
1607
1608 /* assume all sectors need erasing - stops any problems
1609 * when flash_write is called multiple times */
1610 flash_set_dirty();
1611
1612 /* perform any target specific operations before the erase */
1613 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1614
1615 /* perform erase */
1616 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1617 {
1618 /* GDB doesn't evaluate the actual error number returned,
1619 * treat a failed erase as an I/O error
1620 */
1621 gdb_send_error(connection, EIO);
1622 ERROR("flash_erase returned %i", result);
1623 }
1624 else
1625 gdb_put_packet(connection, "OK", 2);
1626
1627 /* reenable gdb output */
1628 gdb_connection->output_disable = 0;
1629
1630 return ERROR_OK;
1631 }
1632
1633 if (strstr(packet, "vFlashWrite:"))
1634 {
1635 unsigned long addr;
1636 unsigned long length;
1637 char *parse = packet + 12;
1638
1639 if (*parse == '\0')
1640 {
1641 ERROR("incomplete vFlashErase packet received, dropping connection");
1642 return ERROR_SERVER_REMOTE_CLOSED;
1643 }
1644 addr = strtoul(parse, &parse, 16);
1645 if (*(parse++) != ':')
1646 {
1647 ERROR("incomplete vFlashErase packet received, dropping connection");
1648 return ERROR_SERVER_REMOTE_CLOSED;
1649 }
1650 length = packet_size - (parse - packet);
1651
1652 /* disable gdb output while programming */
1653 gdb_connection->output_disable = 1;
1654
1655 /* create a new image if there isn't already one */
1656 if (gdb_connection->vflash_image == NULL)
1657 {
1658 gdb_connection->vflash_image = malloc(sizeof(image_t));
1659 image_open(gdb_connection->vflash_image, "", "build");
1660 }
1661
1662 /* create new section with content from packet buffer */
1663 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1664
1665 gdb_put_packet(connection, "OK", 2);
1666
1667 /* reenable gdb output */
1668 gdb_connection->output_disable = 0;
1669
1670 return ERROR_OK;
1671 }
1672
1673 if (!strcmp(packet, "vFlashDone"))
1674 {
1675 u32 written;
1676 char *error_str;
1677
1678 /* disable gdb output while programming */
1679 gdb_connection->output_disable = 1;
1680
1681 /* process the flashing buffer. No need to erase as GDB
1682 * always issues a vFlashErase first. */
1683 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1684 {
1685 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1686 gdb_put_packet(connection, "E.memtype", 9);
1687 else
1688 gdb_send_error(connection, EIO);
1689
1690 if (error_str)
1691 {
1692 ERROR("flash writing failed: %s", error_str);
1693 free(error_str);
1694 }
1695 }
1696 else
1697 {
1698 DEBUG("wrote %u bytes from vFlash image to flash", written);
1699 gdb_put_packet(connection, "OK", 2);
1700 }
1701
1702 image_close(gdb_connection->vflash_image);
1703 free(gdb_connection->vflash_image);
1704 gdb_connection->vflash_image = NULL;
1705
1706 /* reenable gdb output */
1707 gdb_connection->output_disable = 0;
1708
1709 return ERROR_OK;
1710 }
1711
1712 gdb_put_packet(connection, "", 0);
1713 return ERROR_OK;
1714 }
1715
1716 int gdb_detach(connection_t *connection, target_t *target)
1717 {
1718 switch( detach_mode )
1719 {
1720 case GDB_DETACH_RESUME:
1721 target->type->resume(target, 1, 0, 1, 0);
1722 break;
1723
1724 case GDB_DETACH_RESET:
1725 target_process_reset(connection->cmd_ctx);
1726 break;
1727
1728 case GDB_DETACH_HALT:
1729 target->type->halt(target);
1730 break;
1731
1732 case GDB_DETACH_NOTHING:
1733 break;
1734 }
1735
1736 gdb_put_packet(connection, "OK", 2);
1737
1738 return ERROR_OK;
1739 }
1740
1741 int gdb_input(connection_t *connection)
1742 {
1743 gdb_service_t *gdb_service = connection->service->priv;
1744 target_t *target = gdb_service->target;
1745 char packet[GDB_BUFFER_SIZE];
1746 int packet_size;
1747 int retval;
1748 gdb_connection_t *gdb_con = connection->priv;
1749 static int extended_protocol = 0;
1750
1751 /* drain input buffer */
1752 do
1753 {
1754 packet_size = GDB_BUFFER_SIZE-1;
1755 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1756 {
1757 switch (retval)
1758 {
1759 case ERROR_GDB_BUFFER_TOO_SMALL:
1760 ERROR("BUG: buffer supplied for gdb packet was too small");
1761 exit(-1);
1762 case ERROR_SERVER_REMOTE_CLOSED:
1763 return ERROR_SERVER_REMOTE_CLOSED;
1764 default:
1765 ERROR("BUG: unexpected error");
1766 exit(-1);
1767 }
1768 }
1769
1770 /* terminate with zero */
1771 packet[packet_size] = 0;
1772
1773 DEBUG("received packet: '%s'", packet);
1774
1775 if (packet_size > 0)
1776 {
1777 retval = ERROR_OK;
1778 switch (packet[0])
1779 {
1780 case 'H':
1781 /* Hct... -- set thread
1782 * we don't have threads, send empty reply */
1783 gdb_put_packet(connection, NULL, 0);
1784 break;
1785 case 'q':
1786 retval = gdb_query_packet(connection, target, packet, packet_size);
1787 break;
1788 case 'g':
1789 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1790 break;
1791 case 'G':
1792 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1793 break;
1794 case 'p':
1795 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1796 break;
1797 case 'P':
1798 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1799 break;
1800 case 'm':
1801 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1802 break;
1803 case 'M':
1804 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1805 break;
1806 case 'z':
1807 case 'Z':
1808 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1809 break;
1810 case '?':
1811 gdb_last_signal_packet(connection, target, packet, packet_size);
1812 break;
1813 case 'c':
1814 case 's':
1815 gdb_step_continue_packet(connection, target, packet, packet_size);
1816 break;
1817 case 'v':
1818 retval = gdb_v_packet(connection, target, packet, packet_size);
1819 break;
1820 case 'D':
1821 retval = gdb_detach(connection, target);
1822 extended_protocol = 0;
1823 break;
1824 case 'X':
1825 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1826 return retval;
1827 break;
1828 case 'k':
1829 if (extended_protocol != 0)
1830 break;
1831 gdb_put_packet(connection, "OK", 2);
1832 return ERROR_SERVER_REMOTE_CLOSED;
1833 case '!':
1834 /* handle extended remote protocol */
1835 extended_protocol = 1;
1836 gdb_put_packet(connection, "OK", 2);
1837 break;
1838 case 'R':
1839 /* handle extended restart packet */
1840 target_process_reset(connection->cmd_ctx);
1841 break;
1842 default:
1843 /* ignore unkown packets */
1844 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1845 gdb_put_packet(connection, NULL, 0);
1846 break;
1847 }
1848
1849 /* if a packet handler returned an error, exit input loop */
1850 if (retval != ERROR_OK)
1851 return retval;
1852 }
1853
1854 if (gdb_con->ctrl_c)
1855 {
1856 if (target->state == TARGET_RUNNING)
1857 {
1858 target->type->halt(target);
1859 gdb_con->ctrl_c = 0;
1860 }
1861 }
1862
1863 } while (gdb_con->buf_cnt > 0);
1864
1865 return ERROR_OK;
1866 }
1867
1868 int gdb_init()
1869 {
1870 gdb_service_t *gdb_service;
1871 target_t *target = targets;
1872 int i = 0;
1873
1874 if (!target)
1875 {
1876 WARNING("no gdb ports allocated as no target has been specified");
1877 return ERROR_OK;
1878 }
1879
1880 if (gdb_port == 0)
1881 {
1882 WARNING("no gdb port specified, using default port 3333");
1883 gdb_port = 3333;
1884 }
1885
1886 while (target)
1887 {
1888 char service_name[8];
1889
1890 snprintf(service_name, 8, "gdb-%2.2i", i);
1891
1892 gdb_service = malloc(sizeof(gdb_service_t));
1893 gdb_service->target = target;
1894
1895 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1896
1897 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1898
1899 i++;
1900 target = target->next;
1901 }
1902
1903 return ERROR_OK;
1904 }
1905
1906 /* daemon configuration command gdb_port */
1907 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1908 {
1909 if (argc == 0)
1910 return ERROR_OK;
1911
1912 /* only if the port wasn't overwritten by cmdline */
1913 if (gdb_port == 0)
1914 gdb_port = strtoul(args[0], NULL, 0);
1915
1916 return ERROR_OK;
1917 }
1918
1919 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1920 {
1921 if (argc == 1)
1922 {
1923 if (strcmp(args[0], "resume") == 0)
1924 {
1925 detach_mode = GDB_DETACH_RESUME;
1926 return ERROR_OK;
1927 }
1928 else if (strcmp(args[0], "reset") == 0)
1929 {
1930 detach_mode = GDB_DETACH_RESET;
1931 return ERROR_OK;
1932 }
1933 else if (strcmp(args[0], "halt") == 0)
1934 {
1935 detach_mode = GDB_DETACH_HALT;
1936 return ERROR_OK;
1937 }
1938 else if (strcmp(args[0], "nothing") == 0)
1939 {
1940 detach_mode = GDB_DETACH_NOTHING;
1941 return ERROR_OK;
1942 }
1943 }
1944
1945 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1946 return ERROR_OK;
1947 }
1948
1949 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1950 {
1951 if (argc == 1)
1952 {
1953 if (strcmp(args[0], "enable") == 0)
1954 {
1955 gdb_use_memory_map = 1;
1956 return ERROR_OK;
1957 }
1958 else if (strcmp(args[0], "disable") == 0)
1959 {
1960 gdb_use_memory_map = 0;
1961 return ERROR_OK;
1962 }
1963 }
1964
1965 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1966 return ERROR_OK;
1967 }
1968
1969 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1970 {
1971 if (argc == 1)
1972 {
1973 if (strcmp(args[0], "enable") == 0)
1974 {
1975 gdb_flash_program = 1;
1976 return ERROR_OK;
1977 }
1978 else if (strcmp(args[0], "disable") == 0)
1979 {
1980 gdb_flash_program = 0;
1981 return ERROR_OK;
1982 }
1983 }
1984
1985 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1986 return ERROR_OK;
1987 }
1988
1989 int gdb_register_commands(command_context_t *command_context)
1990 {
1991 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1992 COMMAND_CONFIG, "");
1993 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1994 COMMAND_CONFIG, "");
1995 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
1996 COMMAND_CONFIG, "");
1997 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
1998 COMMAND_CONFIG, "");
1999 return ERROR_OK;
2000 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)