d48101f4591a6b39d3111a39188ed8d03f4262d6
[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_calc_blocksize(flash_bank_t *bank)
1368 {
1369 int i;
1370 int block_size = 0xffffffff;
1371
1372 /* loop through all sectors and return smallest sector size */
1373
1374 for (i = 0; i < bank->num_sectors; i++)
1375 {
1376 if (bank->sectors[i].size < block_size)
1377 block_size = bank->sectors[i].size;
1378 }
1379
1380 return block_size;
1381 }
1382
1383 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1384 {
1385 command_context_t *cmd_ctx = connection->cmd_ctx;
1386
1387 if (strstr(packet, "qRcmd,"))
1388 {
1389 if (packet_size > 6)
1390 {
1391 char *cmd;
1392 int i;
1393 cmd = malloc((packet_size - 6)/2 + 1);
1394 for (i=0; i < (packet_size - 6)/2; i++)
1395 {
1396 u32 tmp;
1397 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1398 cmd[i] = tmp;
1399 }
1400 cmd[(packet_size - 6)/2] = 0x0;
1401 target_call_timer_callbacks();
1402 command_run_line(cmd_ctx, cmd);
1403 free(cmd);
1404 }
1405 gdb_put_packet(connection, "OK", 2);
1406 return ERROR_OK;
1407 }
1408 else if (strstr(packet, "qCRC:"))
1409 {
1410 if (packet_size > 5)
1411 {
1412 int retval;
1413 u8 gdb_reply[10];
1414 char *separator;
1415 u32 checksum;
1416 u32 addr = 0;
1417 u32 len = 0;
1418
1419 /* skip command character */
1420 packet += 5;
1421
1422 addr = strtoul(packet, &separator, 16);
1423
1424 if (*separator != ',')
1425 {
1426 ERROR("incomplete read memory packet received, dropping connection");
1427 return ERROR_SERVER_REMOTE_CLOSED;
1428 }
1429
1430 len = strtoul(separator + 1, NULL, 16);
1431
1432 retval = target_checksum_memory(target, addr, len, &checksum);
1433
1434 if (retval == ERROR_OK)
1435 {
1436 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1437 gdb_put_packet(connection, gdb_reply, 9);
1438 }
1439 else
1440 {
1441 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1442 return retval;
1443 }
1444
1445 return ERROR_OK;
1446 }
1447 }
1448 else if (strstr(packet, "qSupported"))
1449 {
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;
1456
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 ? '+' : '-');
1460
1461 if (retval != ERROR_OK)
1462 {
1463 gdb_send_error(connection, 01);
1464 return ERROR_OK;
1465 }
1466
1467 gdb_put_packet(connection, buffer, strlen(buffer));
1468 free(buffer);
1469
1470 return ERROR_OK;
1471 }
1472 else if (strstr(packet, "qXfer:memory-map:read::"))
1473 {
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. */
1479
1480 flash_bank_t *p;
1481 char *xml = NULL;
1482 int size = 0;
1483 int pos = 0;
1484 int retval = ERROR_OK;
1485
1486 int offset;
1487 int length;
1488 char *separator;
1489 int blocksize;
1490
1491 /* skip command character */
1492 packet += 23;
1493
1494 offset = strtoul(packet, &separator, 16);
1495 length = strtoul(separator + 1, &separator, 16);
1496
1497 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1498
1499 int i = 0;
1500 for (;;)
1501 {
1502 p = get_flash_bank_by_num(i);
1503 if (p == NULL)
1504 break;
1505
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);
1509
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++;
1515 }
1516
1517 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1518
1519 if (retval != ERROR_OK)
1520 {
1521 gdb_send_error(connection, retval);
1522 return retval;
1523 }
1524
1525 if (offset + length > pos)
1526 {
1527 length = pos - offset;
1528 }
1529
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);
1534
1535 free(t);
1536 free(xml);
1537 return ERROR_OK;
1538 }
1539 else if (strstr(packet, "qXfer:features:read:"))
1540 {
1541 char *xml = NULL;
1542 int size = 0;
1543 int pos = 0;
1544 int retval = ERROR_OK;
1545
1546 int offset;
1547 int length;
1548 char *annex;
1549
1550 /* skip command character */
1551 packet += 20;
1552
1553 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1554 {
1555 gdb_send_error(connection, 01);
1556 return ERROR_OK;
1557 }
1558
1559 if (strcmp(annex, "target.xml") != 0)
1560 {
1561 gdb_send_error(connection, 01);
1562 return ERROR_OK;
1563 }
1564
1565 xml_printf(&retval, &xml, &pos, &size, \
1566 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1567
1568 if (retval != ERROR_OK)
1569 {
1570 gdb_send_error(connection, retval);
1571 return retval;
1572 }
1573
1574 gdb_put_packet(connection, xml, strlen(xml) + 1);
1575
1576 free(xml);
1577 return ERROR_OK;
1578 }
1579
1580 gdb_put_packet(connection, "", 0);
1581 return ERROR_OK;
1582 }
1583
1584 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1585 {
1586 gdb_connection_t *gdb_connection = connection->priv;
1587 gdb_service_t *gdb_service = connection->service->priv;
1588 int result;
1589
1590 /* if flash programming disabled - send a empty reply */
1591
1592 if (gdb_flash_program == 0)
1593 {
1594 gdb_put_packet(connection, "", 0);
1595 return ERROR_OK;
1596 }
1597
1598 if (strstr(packet, "vFlashErase:"))
1599 {
1600 unsigned long addr;
1601 unsigned long length;
1602
1603 char *parse = packet + 12;
1604 if (*parse == '\0')
1605 {
1606 ERROR("incomplete vFlashErase packet received, dropping connection");
1607 return ERROR_SERVER_REMOTE_CLOSED;
1608 }
1609
1610 addr = strtoul(parse, &parse, 16);
1611
1612 if (*(parse++) != ',' || *parse == '\0')
1613 {
1614 ERROR("incomplete vFlashErase packet received, dropping connection");
1615 return ERROR_SERVER_REMOTE_CLOSED;
1616 }
1617
1618 length = strtoul(parse, &parse, 16);
1619
1620 if (*parse != '\0')
1621 {
1622 ERROR("incomplete vFlashErase packet received, dropping connection");
1623 return ERROR_SERVER_REMOTE_CLOSED;
1624 }
1625
1626 /* disable gdb output while programming */
1627 gdb_connection->output_disable = 1;
1628
1629 /* assume all sectors need erasing - stops any problems
1630 * when flash_write is called multiple times */
1631 flash_set_dirty();
1632
1633 /* perform any target specific operations before the erase */
1634 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1635
1636 /* perform erase */
1637 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1638 {
1639 /* GDB doesn't evaluate the actual error number returned,
1640 * treat a failed erase as an I/O error
1641 */
1642 gdb_send_error(connection, EIO);
1643 ERROR("flash_erase returned %i", result);
1644 }
1645 else
1646 gdb_put_packet(connection, "OK", 2);
1647
1648 /* reenable gdb output */
1649 gdb_connection->output_disable = 0;
1650
1651 return ERROR_OK;
1652 }
1653
1654 if (strstr(packet, "vFlashWrite:"))
1655 {
1656 unsigned long addr;
1657 unsigned long length;
1658 char *parse = packet + 12;
1659
1660 if (*parse == '\0')
1661 {
1662 ERROR("incomplete vFlashErase packet received, dropping connection");
1663 return ERROR_SERVER_REMOTE_CLOSED;
1664 }
1665 addr = strtoul(parse, &parse, 16);
1666 if (*(parse++) != ':')
1667 {
1668 ERROR("incomplete vFlashErase packet received, dropping connection");
1669 return ERROR_SERVER_REMOTE_CLOSED;
1670 }
1671 length = packet_size - (parse - packet);
1672
1673 /* disable gdb output while programming */
1674 gdb_connection->output_disable = 1;
1675
1676 /* create a new image if there isn't already one */
1677 if (gdb_connection->vflash_image == NULL)
1678 {
1679 gdb_connection->vflash_image = malloc(sizeof(image_t));
1680 image_open(gdb_connection->vflash_image, "", "build");
1681 }
1682
1683 /* create new section with content from packet buffer */
1684 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1685
1686 gdb_put_packet(connection, "OK", 2);
1687
1688 /* reenable gdb output */
1689 gdb_connection->output_disable = 0;
1690
1691 return ERROR_OK;
1692 }
1693
1694 if (!strcmp(packet, "vFlashDone"))
1695 {
1696 u32 written;
1697 char *error_str;
1698
1699 /* disable gdb output while programming */
1700 gdb_connection->output_disable = 1;
1701
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)
1705 {
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);
1710
1711 if (error_str)
1712 {
1713 ERROR("flash writing failed: %s", error_str);
1714 free(error_str);
1715 }
1716 }
1717 else
1718 {
1719 DEBUG("wrote %u bytes from vFlash image to flash", written);
1720 gdb_put_packet(connection, "OK", 2);
1721 }
1722
1723 image_close(gdb_connection->vflash_image);
1724 free(gdb_connection->vflash_image);
1725 gdb_connection->vflash_image = NULL;
1726
1727 /* reenable gdb output */
1728 gdb_connection->output_disable = 0;
1729
1730 return ERROR_OK;
1731 }
1732
1733 gdb_put_packet(connection, "", 0);
1734 return ERROR_OK;
1735 }
1736
1737 int gdb_detach(connection_t *connection, target_t *target)
1738 {
1739 switch( detach_mode )
1740 {
1741 case GDB_DETACH_RESUME:
1742 target->type->resume(target, 1, 0, 1, 0);
1743 break;
1744
1745 case GDB_DETACH_RESET:
1746 target_process_reset(connection->cmd_ctx);
1747 break;
1748
1749 case GDB_DETACH_HALT:
1750 target->type->halt(target);
1751 break;
1752
1753 case GDB_DETACH_NOTHING:
1754 break;
1755 }
1756
1757 gdb_put_packet(connection, "OK", 2);
1758
1759 return ERROR_OK;
1760 }
1761
1762 int gdb_input(connection_t *connection)
1763 {
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;
1771
1772 /* drain input buffer */
1773 do
1774 {
1775 packet_size = GDB_BUFFER_SIZE-1;
1776 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1777 {
1778 switch (retval)
1779 {
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);
1788 }
1789 }
1790
1791 /* terminate with zero */
1792 packet[packet_size] = 0;
1793
1794 DEBUG("received packet: '%s'", packet);
1795
1796 if (packet_size > 0)
1797 {
1798 retval = ERROR_OK;
1799 switch (packet[0])
1800 {
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;
1868 }
1869
1870 /* if a packet handler returned an error, exit input loop */
1871 if (retval != ERROR_OK)
1872 return retval;
1873 }
1874
1875 if (gdb_con->ctrl_c)
1876 {
1877 if (target->state == TARGET_RUNNING)
1878 {
1879 target->type->halt(target);
1880 gdb_con->ctrl_c = 0;
1881 }
1882 }
1883
1884 } while (gdb_con->buf_cnt > 0);
1885
1886 return ERROR_OK;
1887 }
1888
1889 int gdb_init()
1890 {
1891 gdb_service_t *gdb_service;
1892 target_t *target = targets;
1893 int i = 0;
1894
1895 if (!target)
1896 {
1897 WARNING("no gdb ports allocated as no target has been specified");
1898 return ERROR_OK;
1899 }
1900
1901 if (gdb_port == 0)
1902 {
1903 WARNING("no gdb port specified, using default port 3333");
1904 gdb_port = 3333;
1905 }
1906
1907 while (target)
1908 {
1909 char service_name[8];
1910
1911 snprintf(service_name, 8, "gdb-%2.2i", i);
1912
1913 gdb_service = malloc(sizeof(gdb_service_t));
1914 gdb_service->target = target;
1915
1916 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1917
1918 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1919
1920 i++;
1921 target = target->next;
1922 }
1923
1924 return ERROR_OK;
1925 }
1926
1927 /* daemon configuration command gdb_port */
1928 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1929 {
1930 if (argc == 0)
1931 return ERROR_OK;
1932
1933 /* only if the port wasn't overwritten by cmdline */
1934 if (gdb_port == 0)
1935 gdb_port = strtoul(args[0], NULL, 0);
1936
1937 return ERROR_OK;
1938 }
1939
1940 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1941 {
1942 if (argc == 1)
1943 {
1944 if (strcmp(args[0], "resume") == 0)
1945 {
1946 detach_mode = GDB_DETACH_RESUME;
1947 return ERROR_OK;
1948 }
1949 else if (strcmp(args[0], "reset") == 0)
1950 {
1951 detach_mode = GDB_DETACH_RESET;
1952 return ERROR_OK;
1953 }
1954 else if (strcmp(args[0], "halt") == 0)
1955 {
1956 detach_mode = GDB_DETACH_HALT;
1957 return ERROR_OK;
1958 }
1959 else if (strcmp(args[0], "nothing") == 0)
1960 {
1961 detach_mode = GDB_DETACH_NOTHING;
1962 return ERROR_OK;
1963 }
1964 }
1965
1966 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1967 return ERROR_OK;
1968 }
1969
1970 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1971 {
1972 if (argc == 1)
1973 {
1974 if (strcmp(args[0], "enable") == 0)
1975 {
1976 gdb_use_memory_map = 1;
1977 return ERROR_OK;
1978 }
1979 else if (strcmp(args[0], "disable") == 0)
1980 {
1981 gdb_use_memory_map = 0;
1982 return ERROR_OK;
1983 }
1984 }
1985
1986 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1987 return ERROR_OK;
1988 }
1989
1990 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1991 {
1992 if (argc == 1)
1993 {
1994 if (strcmp(args[0], "enable") == 0)
1995 {
1996 gdb_flash_program = 1;
1997 return ERROR_OK;
1998 }
1999 else if (strcmp(args[0], "disable") == 0)
2000 {
2001 gdb_flash_program = 0;
2002 return ERROR_OK;
2003 }
2004 }
2005
2006 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2007 return ERROR_OK;
2008 }
2009
2010 int gdb_register_commands(command_context_t *command_context)
2011 {
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;
2021 }

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)