* fixed malloc corruption in target->debug_reason
[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 #include "configuration.h"
36
37 #include <string.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41
42 #if 0
43 #define _DEBUG_GDB_IO_
44 #endif
45
46 extern int gdb_error(connection_t *connection, int retval);
47 static unsigned short gdb_port;
48 static const char *DIGITS = "0123456789abcdef";
49
50 static void gdb_log_callback(void *priv, const char *file, int line,
51 const char *function, const char *string);
52
53 enum gdb_detach_mode
54 {
55 GDB_DETACH_RESUME,
56 GDB_DETACH_RESET,
57 GDB_DETACH_HALT,
58 GDB_DETACH_NOTHING
59 };
60
61 /* target behaviour on gdb detach */
62 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
63
64 /* set if we are sending a memory map to gdb
65 * via qXfer:memory-map:read packet */
66 int gdb_use_memory_map = 0;
67 int gdb_flash_program = 0;
68
69 /* if set, data aborts cause an error to be reported in memory read packets
70 * see the code in gdb_read_memory_packet() for further explanations */
71 int gdb_report_data_abort = 0;
72
73 int gdb_last_signal(target_t *target)
74 {
75 switch (target->debug_reason)
76 {
77 case DBG_REASON_DBGRQ:
78 return 0x2; /* SIGINT */
79 case DBG_REASON_BREAKPOINT:
80 case DBG_REASON_WATCHPOINT:
81 case DBG_REASON_WPTANDBKPT:
82 return 0x05; /* SIGTRAP */
83 case DBG_REASON_SINGLESTEP:
84 return 0x05; /* SIGTRAP */
85 case DBG_REASON_NOTHALTED:
86 return 0x0; /* no signal... shouldn't happen */
87 default:
88 USER("undefined debug reason %d - target needs reset", target->debug_reason);
89 return 0x0;
90 }
91 }
92
93 #ifndef _WIN32
94 int check_pending(connection_t *connection, int timeout_s, int *got_data)
95 {
96 /* a non-blocking socket will block if there is 0 bytes available on the socket,
97 * but return with as many bytes as are available immediately
98 */
99 struct timeval tv;
100 fd_set read_fds;
101 gdb_connection_t *gdb_con = connection->priv;
102 int t;
103 if (got_data==NULL)
104 got_data=&t;
105 *got_data=0;
106
107 if (gdb_con->buf_cnt>0)
108 {
109 *got_data = 1;
110 return ERROR_OK;
111 }
112
113 FD_ZERO(&read_fds);
114 FD_SET(connection->fd, &read_fds);
115
116 tv.tv_sec = timeout_s;
117 tv.tv_usec = 0;
118 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
119 {
120 /* This can typically be because a "monitor" command took too long
121 * before printing any progress messages
122 */
123 if (timeout_s>0)
124 {
125 return ERROR_GDB_TIMEOUT;
126 } else
127 {
128 return ERROR_OK;
129 }
130 }
131 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
132 return ERROR_OK;
133 }
134 #endif
135
136 int gdb_get_char(connection_t *connection, int* next_char)
137 {
138 gdb_connection_t *gdb_con = connection->priv;
139 int retval=ERROR_OK;
140
141 #ifdef _DEBUG_GDB_IO_
142 char *debug_buffer;
143 #endif
144
145 if (gdb_con->buf_cnt-- > 0)
146 {
147 *next_char = *(gdb_con->buf_p++);
148 if (gdb_con->buf_cnt > 0)
149 connection->input_pending = 1;
150 else
151 connection->input_pending = 0;
152
153 #ifdef _DEBUG_GDB_IO_
154 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
155 #endif
156
157 return ERROR_OK;
158 }
159
160 for (;;)
161 {
162 #ifndef _WIN32
163 retval=check_pending(connection, 1, NULL);
164 if (retval!=ERROR_OK)
165 return retval;
166 #endif
167 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
168 if (gdb_con->buf_cnt > 0)
169 {
170 break;
171 }
172 if (gdb_con->buf_cnt == 0)
173 {
174 gdb_con->closed = 1;
175 return ERROR_SERVER_REMOTE_CLOSED;
176 }
177
178 #ifdef _WIN32
179 errno = WSAGetLastError();
180
181 switch(errno)
182 {
183 case WSAEWOULDBLOCK:
184 usleep(1000);
185 break;
186 case WSAECONNABORTED:
187 gdb_con->closed = 1;
188 return ERROR_SERVER_REMOTE_CLOSED;
189 case WSAECONNRESET:
190 gdb_con->closed = 1;
191 return ERROR_SERVER_REMOTE_CLOSED;
192 default:
193 ERROR("read: %d", errno);
194 exit(-1);
195 }
196 #else
197 switch(errno)
198 {
199 case EAGAIN:
200 usleep(1000);
201 break;
202 case ECONNABORTED:
203 gdb_con->closed = 1;
204 return ERROR_SERVER_REMOTE_CLOSED;
205 case ECONNRESET:
206 gdb_con->closed = 1;
207 return ERROR_SERVER_REMOTE_CLOSED;
208 default:
209 ERROR("read: %s", strerror(errno));
210 gdb_con->closed = 1;
211 return ERROR_SERVER_REMOTE_CLOSED;
212 }
213 #endif
214 }
215
216 #ifdef _DEBUG_GDB_IO_
217 debug_buffer = malloc(gdb_con->buf_cnt + 1);
218 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
219 debug_buffer[gdb_con->buf_cnt] = 0;
220 DEBUG("received '%s'", debug_buffer);
221 free(debug_buffer);
222 #endif
223
224 gdb_con->buf_p = gdb_con->buffer;
225 gdb_con->buf_cnt--;
226 *next_char = *(gdb_con->buf_p++);
227 if (gdb_con->buf_cnt > 0)
228 connection->input_pending = 1;
229 else
230 connection->input_pending = 0;
231 #ifdef _DEBUG_GDB_IO_
232 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
233 #endif
234
235 return retval;
236 }
237
238 int gdb_putback_char(connection_t *connection, int last_char)
239 {
240 gdb_connection_t *gdb_con = connection->priv;
241
242 if (gdb_con->buf_p > gdb_con->buffer)
243 {
244 *(--gdb_con->buf_p) = last_char;
245 gdb_con->buf_cnt++;
246 }
247 else
248 {
249 ERROR("BUG: couldn't put character back");
250 }
251
252 return ERROR_OK;
253 }
254
255 /* The only way we can detect that the socket is closed is the first time
256 * we write to it, we will fail. Subsequent write operations will
257 * succeed. Shudder! */
258 int gdb_write(connection_t *connection, void *data, int len)
259 {
260 gdb_connection_t *gdb_con = connection->priv;
261 if (gdb_con->closed)
262 return ERROR_SERVER_REMOTE_CLOSED;
263
264 if (write_socket(connection->fd, data, len) == len)
265 {
266 return ERROR_OK;
267 }
268 gdb_con->closed = 1;
269 return ERROR_SERVER_REMOTE_CLOSED;
270 }
271
272 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
273 {
274 int i;
275 unsigned char my_checksum = 0;
276 #ifdef _DEBUG_GDB_IO_
277 char *debug_buffer;
278 #endif
279 int reply;
280 int retval;
281 gdb_connection_t *gdb_con = connection->priv;
282
283 for (i = 0; i < len; i++)
284 my_checksum += buffer[i];
285
286 #ifdef _DEBUG_GDB_IO_
287 /*
288 * At this point we should have nothing in the input queue from GDB,
289 * however sometimes '-' is sent even though we've already received
290 * an ACK (+) for everything we've sent off.
291 */
292 #ifndef _WIN32
293 int gotdata;
294 for (;;)
295 {
296 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
297 return retval;
298 if (!gotdata)
299 break;
300 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
301 return retval;
302 WARNING("Discard unexpected char %c", reply);
303 }
304 #endif
305 #endif
306
307 while (1)
308 {
309 #ifdef _DEBUG_GDB_IO_
310 debug_buffer = malloc(len + 1);
311 memcpy(debug_buffer, buffer, len);
312 debug_buffer[len] = 0;
313 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
314 free(debug_buffer);
315 #endif
316
317 void *allocated = NULL;
318 char stackAlloc[1024];
319 char *t = stackAlloc;
320 int totalLen = 1 + len + 1 + 2;
321 if (totalLen > sizeof(stackAlloc))
322 {
323 allocated = malloc(totalLen);
324 t = allocated;
325 if (allocated == NULL)
326 {
327 ERROR("Ran out of memory trying to reply packet %d\n", totalLen);
328 exit(-1);
329 }
330 }
331 t[0] = '$';
332 memcpy(t + 1, buffer, len);
333 t[1 + len] = '#';
334 t[1 + len + 1] = DIGITS[(my_checksum >> 4) & 0xf];
335 t[1 + len + 2] = DIGITS[my_checksum & 0xf];
336
337 gdb_write(connection, t, totalLen);
338
339 if (allocated)
340 {
341 free(allocated);
342 }
343
344 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
345 return retval;
346
347 if (reply == '+')
348 break;
349 else if (reply == '-')
350 {
351 /* Stop sending output packets for now */
352 log_remove_callback(gdb_log_callback, connection);
353 WARNING("negative reply, retrying");
354 }
355 else if (reply == 0x3)
356 {
357 gdb_con->ctrl_c = 1;
358 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
359 return retval;
360 if (reply == '+')
361 break;
362 else if (reply == '-')
363 {
364 /* Stop sending output packets for now */
365 log_remove_callback(gdb_log_callback, connection);
366 WARNING("negative reply, retrying");
367 }
368 else
369 {
370 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
371 gdb_con->closed=1;
372 return ERROR_SERVER_REMOTE_CLOSED;
373 }
374 }
375 else
376 {
377 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
378 gdb_con->closed=1;
379 return ERROR_SERVER_REMOTE_CLOSED;
380 }
381 }
382 if (gdb_con->closed)
383 return ERROR_SERVER_REMOTE_CLOSED;
384
385 return ERROR_OK;
386 }
387
388 int gdb_put_packet(connection_t *connection, char *buffer, int len)
389 {
390 gdb_connection_t *gdb_con = connection->priv;
391 gdb_con->busy = 1;
392 int retval = gdb_put_packet_inner(connection, buffer, len);
393 gdb_con->busy = 0;
394 return retval;
395 }
396
397 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
398 {
399 int character;
400 int count = 0;
401 int retval;
402 char checksum[3];
403 unsigned char my_checksum = 0;
404 gdb_connection_t *gdb_con = connection->priv;
405
406 while (1)
407 {
408 do
409 {
410 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
411 return retval;
412
413 #ifdef _DEBUG_GDB_IO_
414 DEBUG("character: '%c'", character);
415 #endif
416
417 switch (character)
418 {
419 case '$':
420 break;
421 case '+':
422 WARNING("acknowledgment received, but no packet pending");
423 break;
424 case '-':
425 WARNING("negative acknowledgment, but no packet pending");
426 break;
427 case 0x3:
428 gdb_con->ctrl_c = 1;
429 *len = 0;
430 return ERROR_OK;
431 default:
432 WARNING("ignoring character 0x%x", character);
433 break;
434 }
435 } while (character != '$');
436
437 my_checksum = 0;
438
439 count = 0;
440 gdb_connection_t *gdb_con = connection->priv;
441 for (;;)
442 {
443 /* The common case is that we have an entire packet with no escape chars.
444 * We need to leave at least 2 bytes in the buffer to have
445 * gdb_get_char() update various bits and bobs correctly.
446 */
447 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
448 {
449 /* The compiler will struggle a bit with constant propagation and
450 * aliasing, so we help it by showing that these values do not
451 * change inside the loop
452 */
453 int i;
454 char *buf = gdb_con->buf_p;
455 int run = gdb_con->buf_cnt - 2;
456 i = 0;
457 int done = 0;
458 while (i < run)
459 {
460 character = *buf++;
461 i++;
462 if (character == '#')
463 {
464 /* Danger! character can be '#' when esc is
465 * used so we need an explicit boolean for done here.
466 */
467 done = 1;
468 break;
469 }
470
471 if (character == '}')
472 {
473 /* data transmitted in binary mode (X packet)
474 * uses 0x7d as escape character */
475 my_checksum += character & 0xff;
476 character = *buf++;
477 i++;
478 my_checksum += character & 0xff;
479 buffer[count++] = (character ^ 0x20) & 0xff;
480 } else
481 {
482 my_checksum += character & 0xff;
483 buffer[count++] = character & 0xff;
484 }
485 }
486 gdb_con->buf_p += i;
487 gdb_con->buf_cnt -= i;
488 if (done)
489 break;
490 }
491 if (count > *len)
492 {
493 ERROR("packet buffer too small");
494 return ERROR_GDB_BUFFER_TOO_SMALL;
495 }
496
497 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
498 return retval;
499
500 if (character == '#')
501 break;
502
503 if (character == '}')
504 {
505 /* data transmitted in binary mode (X packet)
506 * uses 0x7d as escape character */
507 my_checksum += character & 0xff;
508 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
509 return retval;
510 my_checksum += character & 0xff;
511 buffer[count++] = (character ^ 0x20) & 0xff;
512 }
513 else
514 {
515 my_checksum += character & 0xff;
516 buffer[count++] = character & 0xff;
517 }
518
519 }
520
521 *len = count;
522
523 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
524 return retval;
525 checksum[0] = character;
526 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
527 return retval;
528 checksum[1] = character;
529 checksum[2] = 0;
530
531 if (my_checksum == strtoul(checksum, NULL, 16))
532 {
533 gdb_write(connection, "+", 1);
534 break;
535 }
536
537 WARNING("checksum error, requesting retransmission");
538 gdb_write(connection, "-", 1);
539 }
540 if (gdb_con->closed)
541 return ERROR_SERVER_REMOTE_CLOSED;
542
543 return ERROR_OK;
544 }
545
546 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
547 {
548 gdb_connection_t *gdb_con = connection->priv;
549 gdb_con->busy = 1;
550 int retval = gdb_get_packet_inner(connection, buffer, len);
551 gdb_con->busy = 0;
552 return retval;
553 }
554
555 int gdb_output_con(connection_t *connection, const char* line)
556 {
557 char *hex_buffer;
558 int i, bin_size;
559
560 bin_size = strlen(line);
561
562 hex_buffer = malloc(bin_size*2 + 2);
563 if (hex_buffer == NULL)
564 return ERROR_GDB_BUFFER_TOO_SMALL;
565
566 hex_buffer[0] = 'O';
567 for (i=0; i<bin_size; i++)
568 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
569 hex_buffer[bin_size*2+1] = 0;
570
571 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
572
573 free(hex_buffer);
574 return ERROR_OK;
575 }
576
577 int gdb_output(struct command_context_s *context, char* line)
578 {
579 /* this will be dumped to the log and also sent as an O packet if possible */
580 USER_N("%s", line);
581 return ERROR_OK;
582 }
583
584 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
585 {
586 FILE *script;
587 struct command_context_s *cmd_ctx = priv;
588
589 if (target->gdb_program_script)
590 {
591 script = open_file_from_path(target->gdb_program_script, "r");
592 if (!script)
593 {
594 ERROR("couldn't open script file %s", target->gdb_program_script);
595 return ERROR_OK;
596 }
597
598 INFO("executing gdb_program script '%s'", target->gdb_program_script);
599 command_run_file(cmd_ctx, script, COMMAND_EXEC);
600 fclose(script);
601
602 jtag_execute_queue();
603 }
604
605 return ERROR_OK;
606 }
607
608 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
609 {
610 connection_t *connection = priv;
611 gdb_connection_t *gdb_connection = connection->priv;
612 char sig_reply[4];
613 int signal;
614
615 switch (event)
616 {
617 case TARGET_EVENT_HALTED:
618 /* In the GDB protocol when we are stepping or coninuing execution,
619 * we have a lingering reply. Upon receiving a halted event
620 * when we have that lingering packet, we reply to the original
621 * step or continue packet.
622 *
623 * Executing monitor commands can bring the target in and
624 * out of the running state so we'll see lots of TARGET_EVENT_XXX
625 * that are to be ignored.
626 */
627 if (gdb_connection->frontend_state == TARGET_RUNNING)
628 {
629 /* stop forwarding log packets! */
630 log_remove_callback(gdb_log_callback, connection);
631
632 if (gdb_connection->ctrl_c)
633 {
634 signal = 0x2;
635 gdb_connection->ctrl_c = 0;
636 }
637 else
638 {
639 signal = gdb_last_signal(target);
640 }
641
642 snprintf(sig_reply, 4, "T%2.2x", signal);
643 gdb_put_packet(connection, sig_reply, 3);
644 gdb_connection->frontend_state = TARGET_HALTED;
645 }
646 break;
647 case TARGET_EVENT_GDB_PROGRAM:
648 gdb_program_handler(target, event, connection->cmd_ctx);
649 break;
650 default:
651 break;
652 }
653
654 return ERROR_OK;
655 }
656
657
658 int gdb_new_connection(connection_t *connection)
659 {
660 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
661 gdb_service_t *gdb_service = connection->service->priv;
662 int retval;
663 int initial_ack;
664
665 connection->priv = gdb_connection;
666
667 /* initialize gdb connection information */
668 gdb_connection->buf_p = gdb_connection->buffer;
669 gdb_connection->buf_cnt = 0;
670 gdb_connection->ctrl_c = 0;
671 gdb_connection->frontend_state = TARGET_HALTED;
672 gdb_connection->vflash_image = NULL;
673 gdb_connection->closed = 0;
674 gdb_connection->busy = 0;
675
676 /* send ACK to GDB for debug request */
677 gdb_write(connection, "+", 1);
678
679 /* output goes through gdb connection */
680 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
681
682 /* register callback to be informed about target events */
683 target_register_event_callback(gdb_target_callback_event_handler, connection);
684
685 /* a gdb session just attached, try to put the target in halt mode
686 * or alterantively try to issue a reset.
687 *
688 * GDB connection will fail if e.g. register read packets fail,
689 * otherwise resetting/halting the target could have been left to GDB init
690 * scripts
691 */
692 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
693 (retval != ERROR_TARGET_ALREADY_HALTED))
694 {
695 ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
696 command_run_line(connection->cmd_ctx, "reset");
697 }
698
699 /* remove the initial ACK from the incoming buffer */
700 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
701 return retval;
702
703 /* FIX!!!??? would we actually ever receive a + here???
704 * Not observed.
705 */
706 if (initial_ack != '+')
707 gdb_putback_char(connection, initial_ack);
708
709 return ERROR_OK;
710 }
711
712 int gdb_connection_closed(connection_t *connection)
713 {
714 gdb_service_t *gdb_service = connection->service->priv;
715 gdb_connection_t *gdb_connection = connection->priv;
716
717 /* see if an image built with vFlash commands is left */
718 if (gdb_connection->vflash_image)
719 {
720 image_close(gdb_connection->vflash_image);
721 free(gdb_connection->vflash_image);
722 gdb_connection->vflash_image = NULL;
723 }
724
725 /* if this connection registered a debug-message receiver delete it */
726 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
727
728 if (connection->priv)
729 {
730 free(connection->priv);
731 connection->priv = NULL;
732 }
733 else
734 {
735 ERROR("BUG: connection->priv == NULL");
736 }
737
738 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
739 log_remove_callback(gdb_log_callback, connection);
740
741 return ERROR_OK;
742 }
743
744 void gdb_send_error(connection_t *connection, u8 the_error)
745 {
746 char err[4];
747 snprintf(err, 4, "E%2.2X", the_error );
748 gdb_put_packet(connection, err, 3);
749 }
750
751 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
752 {
753 char sig_reply[4];
754 int signal;
755
756 signal = gdb_last_signal(target);
757
758 snprintf(sig_reply, 4, "S%2.2x", signal);
759 gdb_put_packet(connection, sig_reply, 3);
760
761 return ERROR_OK;
762 }
763
764 /* Convert register to string of bits. NB! The # of bits in the
765 * register might be non-divisible by 8(a byte), in which
766 * case an entire byte is shown. */
767 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
768 {
769 int i;
770
771 u8 *buf;
772 int buf_len;
773 buf = reg->value;
774 buf_len = CEIL(reg->size, 8);
775
776 if (target->endianness == TARGET_LITTLE_ENDIAN)
777 {
778 for (i = 0; i < buf_len; i++)
779 {
780 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
781 tstr[i*2+1] = DIGITS[buf[i]&0xf];
782 }
783 }
784 else
785 {
786 for (i = 0; i < buf_len; i++)
787 {
788 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
789 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
790 }
791 }
792 }
793
794 void gdb_target_to_str(target_t *target, char *tstr, char *str)
795 {
796 int str_len = strlen(tstr);
797 int i;
798
799 if (str_len % 2)
800 {
801 ERROR("BUG: gdb value with uneven number of characters encountered");
802 exit(-1);
803 }
804
805 if (target->endianness == TARGET_LITTLE_ENDIAN)
806 {
807 for (i = 0; i < str_len; i+=2)
808 {
809 str[str_len - i - 1] = tstr[i + 1];
810 str[str_len - i - 2] = tstr[i];
811 }
812 }
813 else
814 {
815 for (i = 0; i < str_len; i++)
816 {
817 str[i] = tstr[i];
818 }
819 }
820 }
821
822 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
823 {
824 reg_t **reg_list;
825 int reg_list_size;
826 int retval;
827 int reg_packet_size = 0;
828 char *reg_packet;
829 char *reg_packet_p;
830 int i;
831
832 #ifdef _DEBUG_GDB_IO_
833 DEBUG("-");
834 #endif
835
836 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
837 {
838 return gdb_error(connection, retval);
839 }
840
841 for (i = 0; i < reg_list_size; i++)
842 {
843 reg_packet_size += reg_list[i]->size;
844 }
845
846 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
847 reg_packet_p = reg_packet;
848
849 for (i = 0; i < reg_list_size; i++)
850 {
851 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
852 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
853 }
854
855 #ifdef _DEBUG_GDB_IO_
856 {
857 char *reg_packet_p;
858 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
859 DEBUG("reg_packet: %s", reg_packet_p);
860 free(reg_packet_p);
861 }
862 #endif
863
864 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
865 free(reg_packet);
866
867 free(reg_list);
868
869 return ERROR_OK;
870 }
871
872 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
873 {
874 int i;
875 reg_t **reg_list;
876 int reg_list_size;
877 int retval;
878 char *packet_p;
879
880 #ifdef _DEBUG_GDB_IO_
881 DEBUG("-");
882 #endif
883
884 /* skip command character */
885 packet++;
886 packet_size--;
887
888 if (packet_size % 2)
889 {
890 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
891 return ERROR_SERVER_REMOTE_CLOSED;
892 }
893
894 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
895 {
896 return gdb_error(connection, retval);
897 }
898
899 packet_p = packet;
900 for (i = 0; i < reg_list_size; i++)
901 {
902 u8 *bin_buf;
903 char *hex_buf;
904 reg_arch_type_t *arch_type;
905
906 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
907 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
908 gdb_target_to_str(target, packet_p, hex_buf);
909
910 /* convert hex-string to binary buffer */
911 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
912 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
913
914 /* get register arch_type, and call set method */
915 arch_type = register_get_arch_type(reg_list[i]->arch_type);
916 if (arch_type == NULL)
917 {
918 ERROR("BUG: encountered unregistered arch type");
919 exit(-1);
920 }
921 arch_type->set(reg_list[i], bin_buf);
922
923 /* advance packet pointer */
924 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
925
926 free(bin_buf);
927 free(hex_buf);
928 }
929
930 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
931 free(reg_list);
932
933 gdb_put_packet(connection, "OK", 2);
934
935 return ERROR_OK;
936 }
937
938 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
939 {
940 char *reg_packet;
941 int reg_num = strtoul(packet + 1, NULL, 16);
942 reg_t **reg_list;
943 int reg_list_size;
944 int retval;
945
946 #ifdef _DEBUG_GDB_IO_
947 DEBUG("-");
948 #endif
949
950 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
951 {
952 return gdb_error(connection, retval);
953 }
954
955 if (reg_list_size <= reg_num)
956 {
957 ERROR("gdb requested a non-existing register");
958 exit(-1);
959 }
960
961 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
962
963 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
964
965 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
966
967 free(reg_list);
968 free(reg_packet);
969
970 return ERROR_OK;
971 }
972
973 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
974 {
975 char *separator;
976 char *hex_buf;
977 u8 *bin_buf;
978 int reg_num = strtoul(packet + 1, &separator, 16);
979 reg_t **reg_list;
980 int reg_list_size;
981 int retval;
982 reg_arch_type_t *arch_type;
983
984 DEBUG("-");
985
986 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
987 {
988 return gdb_error(connection, retval);
989 }
990
991 if (reg_list_size < reg_num)
992 {
993 ERROR("gdb requested a non-existing register");
994 return ERROR_SERVER_REMOTE_CLOSED;
995 }
996
997 if (*separator != '=')
998 {
999 ERROR("GDB 'set register packet', but no '=' following the register number");
1000 return ERROR_SERVER_REMOTE_CLOSED;
1001 }
1002
1003 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1004 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1005 gdb_target_to_str(target, separator + 1, hex_buf);
1006
1007 /* convert hex-string to binary buffer */
1008 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1009 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1010
1011 /* get register arch_type, and call set method */
1012 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1013 if (arch_type == NULL)
1014 {
1015 ERROR("BUG: encountered unregistered arch type");
1016 exit(-1);
1017 }
1018 arch_type->set(reg_list[reg_num], bin_buf);
1019
1020 gdb_put_packet(connection, "OK", 2);
1021
1022 free(bin_buf);
1023 free(hex_buf);
1024 free(reg_list);
1025
1026 return ERROR_OK;
1027 }
1028
1029 int gdb_error(connection_t *connection, int retval)
1030 {
1031 switch (retval)
1032 {
1033 case ERROR_TARGET_DATA_ABORT:
1034 gdb_send_error(connection, EIO);
1035 break;
1036 case ERROR_TARGET_TRANSLATION_FAULT:
1037 gdb_send_error(connection, EFAULT);
1038 break;
1039 case ERROR_TARGET_UNALIGNED_ACCESS:
1040 gdb_send_error(connection, EFAULT);
1041 break;
1042 case ERROR_TARGET_NOT_HALTED:
1043 gdb_send_error(connection, EFAULT);
1044 break;
1045 default:
1046 /* This could be that the target reset itself. */
1047 ERROR("unexpected error %i", retval);
1048 gdb_send_error(connection, EFAULT);
1049 break;
1050 }
1051
1052 return ERROR_OK;
1053 }
1054
1055 /* We don't have to worry about the default 2 second timeout for GDB packets,
1056 * because GDB breaks up large memory reads into smaller reads.
1057 *
1058 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1059 */
1060 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1061 {
1062 char *separator;
1063 u32 addr = 0;
1064 u32 len = 0;
1065
1066 u8 *buffer;
1067 char *hex_buffer;
1068
1069 int retval = ERROR_OK;
1070
1071 /* skip command character */
1072 packet++;
1073
1074 addr = strtoul(packet, &separator, 16);
1075
1076 if (*separator != ',')
1077 {
1078 ERROR("incomplete read memory packet received, dropping connection");
1079 return ERROR_SERVER_REMOTE_CLOSED;
1080 }
1081
1082 len = strtoul(separator+1, NULL, 16);
1083
1084 buffer = malloc(len);
1085
1086 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1087
1088 retval = target_read_buffer(target, addr, len, buffer);
1089
1090 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1091 {
1092 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1093 * At some point this might be fixed in GDB, in which case this code can be removed.
1094 *
1095 * OpenOCD developers are acutely aware of this problem, but there is nothing
1096 * gained by involving the user in this problem that hopefully will get resolved
1097 * eventually
1098 *
1099 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1100 *
1101 * For now, the default is to fix up things to make current GDB versions work.
1102 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1103 */
1104 memset(buffer, 0, len);
1105 retval = ERROR_OK;
1106 }
1107
1108 if (retval == ERROR_OK)
1109 {
1110 hex_buffer = malloc(len * 2 + 1);
1111
1112 int i;
1113 for (i = 0; i < len; i++)
1114 {
1115 u8 t = buffer[i];
1116 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1117 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1118 }
1119
1120 gdb_put_packet(connection, hex_buffer, len * 2);
1121
1122 free(hex_buffer);
1123 }
1124 else
1125 {
1126 retval = gdb_error(connection, retval);
1127 }
1128
1129 free(buffer);
1130
1131 return retval;
1132 }
1133
1134 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1135 {
1136 char *separator;
1137 u32 addr = 0;
1138 u32 len = 0;
1139
1140 u8 *buffer;
1141
1142 int i;
1143 int retval;
1144
1145 /* skip command character */
1146 packet++;
1147
1148 addr = strtoul(packet, &separator, 16);
1149
1150 if (*separator != ',')
1151 {
1152 ERROR("incomplete write memory packet received, dropping connection");
1153 return ERROR_SERVER_REMOTE_CLOSED;
1154 }
1155
1156 len = strtoul(separator+1, &separator, 16);
1157
1158 if (*(separator++) != ':')
1159 {
1160 ERROR("incomplete write memory packet received, dropping connection");
1161 return ERROR_SERVER_REMOTE_CLOSED;
1162 }
1163
1164 buffer = malloc(len);
1165
1166 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1167
1168 for (i=0; i<len; i++)
1169 {
1170 u32 tmp;
1171 sscanf(separator + 2*i, "%2x", &tmp);
1172 buffer[i] = tmp;
1173 }
1174
1175 retval = target_write_buffer(target, addr, len, buffer);
1176
1177 if (retval == ERROR_OK)
1178 {
1179 gdb_put_packet(connection, "OK", 2);
1180 }
1181 else
1182 {
1183 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1184 return retval;
1185 }
1186
1187 free(buffer);
1188
1189 return ERROR_OK;
1190 }
1191
1192 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1193 {
1194 char *separator;
1195 u32 addr = 0;
1196 u32 len = 0;
1197
1198 int retval;
1199
1200 /* skip command character */
1201 packet++;
1202
1203 addr = strtoul(packet, &separator, 16);
1204
1205 if (*separator != ',')
1206 {
1207 ERROR("incomplete write memory binary packet received, dropping connection");
1208 return ERROR_SERVER_REMOTE_CLOSED;
1209 }
1210
1211 len = strtoul(separator+1, &separator, 16);
1212
1213 if (*(separator++) != ':')
1214 {
1215 ERROR("incomplete write memory binary packet received, dropping connection");
1216 return ERROR_SERVER_REMOTE_CLOSED;
1217 }
1218
1219 retval = ERROR_OK;
1220 if (len)
1221 {
1222 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1223
1224 retval = target_write_buffer(target, addr, len, (u8*)separator);
1225 }
1226
1227 if (retval == ERROR_OK)
1228 {
1229 gdb_put_packet(connection, "OK", 2);
1230 }
1231 else
1232 {
1233 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1234 return retval;
1235 }
1236
1237 return ERROR_OK;
1238 }
1239
1240 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1241 {
1242 int current = 0;
1243 u32 address = 0x0;
1244
1245 DEBUG("-");
1246
1247 if (packet_size > 1)
1248 {
1249 packet[packet_size] = 0;
1250 address = strtoul(packet + 1, NULL, 16);
1251 }
1252 else
1253 {
1254 current = 1;
1255 }
1256
1257 if (packet[0] == 'c')
1258 {
1259 DEBUG("continue");
1260 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1261 }
1262 else if (packet[0] == 's')
1263 {
1264 DEBUG("step");
1265 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1266 }
1267 }
1268
1269 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1270 {
1271 int type;
1272 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1273 enum watchpoint_rw wp_type;
1274 u32 address;
1275 u32 size;
1276 char *separator;
1277 int retval;
1278
1279 DEBUG("-");
1280
1281 type = strtoul(packet + 1, &separator, 16);
1282
1283 if (type == 0) /* memory breakpoint */
1284 bp_type = BKPT_SOFT;
1285 else if (type == 1) /* hardware breakpoint */
1286 bp_type = BKPT_HARD;
1287 else if (type == 2) /* write watchpoint */
1288 wp_type = WPT_WRITE;
1289 else if (type == 3) /* read watchpoint */
1290 wp_type = WPT_READ;
1291 else if (type == 4) /* access watchpoint */
1292 wp_type = WPT_ACCESS;
1293
1294 if (*separator != ',')
1295 {
1296 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1297 return ERROR_SERVER_REMOTE_CLOSED;
1298 }
1299
1300 address = strtoul(separator+1, &separator, 16);
1301
1302 if (*separator != ',')
1303 {
1304 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1305 return ERROR_SERVER_REMOTE_CLOSED;
1306 }
1307
1308 size = strtoul(separator+1, &separator, 16);
1309
1310 switch (type)
1311 {
1312 case 0:
1313 case 1:
1314 if (packet[0] == 'Z')
1315 {
1316 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1317 {
1318 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1319 return retval;
1320 }
1321 else
1322 {
1323 gdb_put_packet(connection, "OK", 2);
1324 }
1325 }
1326 else
1327 {
1328 breakpoint_remove(target, address);
1329 gdb_put_packet(connection, "OK", 2);
1330 }
1331 break;
1332 case 2:
1333 case 3:
1334 case 4:
1335 {
1336 if (packet[0] == 'Z')
1337 {
1338 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1339 {
1340 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1341 return retval;
1342 }
1343 else
1344 {
1345 gdb_put_packet(connection, "OK", 2);
1346 }
1347 }
1348 else
1349 {
1350 watchpoint_remove(target, address);
1351 gdb_put_packet(connection, "OK", 2);
1352 }
1353 break;
1354 }
1355 default:
1356 break;
1357 }
1358
1359 return ERROR_OK;
1360 }
1361
1362 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1363 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1364 {
1365 if (*retval != ERROR_OK)
1366 {
1367 return;
1368 }
1369 int first = 1;
1370
1371 for (;;)
1372 {
1373 if ((*xml == NULL) || (!first))
1374 {
1375 /* start by 0 to exercise all the code paths.
1376 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1377
1378 *size = *size * 2 + 2;
1379 char *t = *xml;
1380 *xml = realloc(*xml, *size);
1381 if (*xml == NULL)
1382 {
1383 if (t)
1384 free(t);
1385 *retval = ERROR_SERVER_REMOTE_CLOSED;
1386 return;
1387 }
1388 }
1389
1390 va_list ap;
1391 int ret;
1392 va_start(ap, fmt);
1393 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1394 va_end(ap);
1395 if ((ret > 0) && ((ret + 1) < *size - *pos))
1396 {
1397 *pos += ret;
1398 return;
1399 }
1400 /* there was just enough or not enough space, allocate more. */
1401 first = 0;
1402 }
1403 }
1404
1405 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1406 {
1407 char *separator;
1408
1409 /* Extract and NUL-terminate the annex. */
1410 *annex = buf;
1411 while (*buf && *buf != ':')
1412 buf++;
1413 if (*buf == '\0')
1414 return -1;
1415 *buf++ = 0;
1416
1417 /* After the read marker and annex, qXfer looks like a
1418 * traditional 'm' packet. */
1419
1420 *ofs = strtoul(buf, &separator, 16);
1421
1422 if (*separator != ',')
1423 return -1;
1424
1425 *len = strtoul(separator+1, NULL, 16);
1426
1427 return 0;
1428 }
1429
1430 int gdb_calc_blocksize(flash_bank_t *bank)
1431 {
1432 int i;
1433 int block_size = 0xffffffff;
1434
1435 /* loop through all sectors and return smallest sector size */
1436
1437 for (i = 0; i < bank->num_sectors; i++)
1438 {
1439 if (bank->sectors[i].size < block_size)
1440 block_size = bank->sectors[i].size;
1441 }
1442
1443 return block_size;
1444 }
1445
1446 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1447 {
1448 command_context_t *cmd_ctx = connection->cmd_ctx;
1449
1450 if (strstr(packet, "qRcmd,"))
1451 {
1452 if (packet_size > 6)
1453 {
1454 char *cmd;
1455 int i;
1456 cmd = malloc((packet_size - 6)/2 + 1);
1457 for (i=0; i < (packet_size - 6)/2; i++)
1458 {
1459 u32 tmp;
1460 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1461 cmd[i] = tmp;
1462 }
1463 cmd[(packet_size - 6)/2] = 0x0;
1464
1465 /* We want to print all debug output to GDB connection */
1466 log_add_callback(gdb_log_callback, connection);
1467 target_call_timer_callbacks();
1468 command_run_line(cmd_ctx, cmd);
1469 log_remove_callback(gdb_log_callback, connection);
1470 free(cmd);
1471 }
1472 gdb_put_packet(connection, "OK", 2);
1473 return ERROR_OK;
1474 }
1475 else if (strstr(packet, "qCRC:"))
1476 {
1477 if (packet_size > 5)
1478 {
1479 int retval;
1480 char gdb_reply[10];
1481 char *separator;
1482 u32 checksum;
1483 u32 addr = 0;
1484 u32 len = 0;
1485
1486 /* skip command character */
1487 packet += 5;
1488
1489 addr = strtoul(packet, &separator, 16);
1490
1491 if (*separator != ',')
1492 {
1493 ERROR("incomplete read memory packet received, dropping connection");
1494 return ERROR_SERVER_REMOTE_CLOSED;
1495 }
1496
1497 len = strtoul(separator + 1, NULL, 16);
1498
1499 retval = target_checksum_memory(target, addr, len, &checksum);
1500
1501 if (retval == ERROR_OK)
1502 {
1503 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1504 gdb_put_packet(connection, gdb_reply, 9);
1505 }
1506 else
1507 {
1508 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1509 return retval;
1510 }
1511
1512 return ERROR_OK;
1513 }
1514 }
1515 else if (strstr(packet, "qSupported"))
1516 {
1517 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1518 * disable qXfer:features:read for the moment */
1519 int retval = ERROR_OK;
1520 char *buffer = NULL;
1521 int pos = 0;
1522 int size = 0;
1523
1524 xml_printf(&retval, &buffer, &pos, &size,
1525 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1526 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1527
1528 if (retval != ERROR_OK)
1529 {
1530 gdb_send_error(connection, 01);
1531 return ERROR_OK;
1532 }
1533
1534 gdb_put_packet(connection, buffer, strlen(buffer));
1535 free(buffer);
1536
1537 return ERROR_OK;
1538 }
1539 else if (strstr(packet, "qXfer:memory-map:read::"))
1540 {
1541 /* We get away with only specifying flash here. Regions that are not
1542 * specified are treated as if we provided no memory map(if not we
1543 * could detect the holes and mark them as RAM).
1544 * Normally we only execute this code once, but no big deal if we
1545 * have to regenerate it a couple of times. */
1546
1547 flash_bank_t *p;
1548 char *xml = NULL;
1549 int size = 0;
1550 int pos = 0;
1551 int retval = ERROR_OK;
1552
1553 int offset;
1554 int length;
1555 char *separator;
1556 int blocksize;
1557
1558 /* skip command character */
1559 packet += 23;
1560
1561 offset = strtoul(packet, &separator, 16);
1562 length = strtoul(separator + 1, &separator, 16);
1563
1564 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1565
1566 int i;
1567 for (i=0; i<flash_get_bank_count(); i++)
1568 {
1569 p = get_flash_bank_by_num(i);
1570 if (p == NULL)
1571 break;
1572
1573 /* if device has uneven sector sizes, eg. str7, lpc
1574 * we pass the smallest sector size to gdb memory map */
1575 blocksize = gdb_calc_blocksize(p);
1576
1577 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1578 "<property name=\"blocksize\">0x%x</property>\n" \
1579 "</memory>\n", \
1580 p->base, p->size, blocksize);
1581 }
1582
1583 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1584
1585 if (retval != ERROR_OK)
1586 {
1587 gdb_send_error(connection, retval);
1588 return retval;
1589 }
1590
1591 if (offset + length > pos)
1592 {
1593 length = pos - offset;
1594 }
1595
1596 char *t = malloc(length + 1);
1597 t[0] = 'l';
1598 memcpy(t + 1, xml + offset, length);
1599 gdb_put_packet(connection, t, length + 1);
1600
1601 free(t);
1602 free(xml);
1603 return ERROR_OK;
1604 }
1605 else if (strstr(packet, "qXfer:features:read:"))
1606 {
1607 char *xml = NULL;
1608 int size = 0;
1609 int pos = 0;
1610 int retval = ERROR_OK;
1611
1612 int offset;
1613 unsigned int length;
1614 char *annex;
1615
1616 /* skip command character */
1617 packet += 20;
1618
1619 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1620 {
1621 gdb_send_error(connection, 01);
1622 return ERROR_OK;
1623 }
1624
1625 if (strcmp(annex, "target.xml") != 0)
1626 {
1627 gdb_send_error(connection, 01);
1628 return ERROR_OK;
1629 }
1630
1631 xml_printf(&retval, &xml, &pos, &size, \
1632 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1633
1634 if (retval != ERROR_OK)
1635 {
1636 gdb_send_error(connection, retval);
1637 return retval;
1638 }
1639
1640 gdb_put_packet(connection, xml, strlen(xml) + 1);
1641
1642 free(xml);
1643 return ERROR_OK;
1644 }
1645
1646 gdb_put_packet(connection, "", 0);
1647 return ERROR_OK;
1648 }
1649
1650 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1651 {
1652 gdb_connection_t *gdb_connection = connection->priv;
1653 gdb_service_t *gdb_service = connection->service->priv;
1654 int result;
1655
1656 /* if flash programming disabled - send a empty reply */
1657
1658 if (gdb_flash_program == 0)
1659 {
1660 gdb_put_packet(connection, "", 0);
1661 return ERROR_OK;
1662 }
1663
1664 if (strstr(packet, "vFlashErase:"))
1665 {
1666 unsigned long addr;
1667 unsigned long length;
1668
1669 char *parse = packet + 12;
1670 if (*parse == '\0')
1671 {
1672 ERROR("incomplete vFlashErase packet received, dropping connection");
1673 return ERROR_SERVER_REMOTE_CLOSED;
1674 }
1675
1676 addr = strtoul(parse, &parse, 16);
1677
1678 if (*(parse++) != ',' || *parse == '\0')
1679 {
1680 ERROR("incomplete vFlashErase packet received, dropping connection");
1681 return ERROR_SERVER_REMOTE_CLOSED;
1682 }
1683
1684 length = strtoul(parse, &parse, 16);
1685
1686 if (*parse != '\0')
1687 {
1688 ERROR("incomplete vFlashErase packet received, dropping connection");
1689 return ERROR_SERVER_REMOTE_CLOSED;
1690 }
1691
1692 /* assume all sectors need erasing - stops any problems
1693 * when flash_write is called multiple times */
1694 flash_set_dirty();
1695
1696 /* perform any target specific operations before the erase */
1697 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1698
1699 /* perform erase */
1700 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1701 {
1702 /* GDB doesn't evaluate the actual error number returned,
1703 * treat a failed erase as an I/O error
1704 */
1705 gdb_send_error(connection, EIO);
1706 ERROR("flash_erase returned %i", result);
1707 }
1708 else
1709 gdb_put_packet(connection, "OK", 2);
1710
1711 return ERROR_OK;
1712 }
1713
1714 if (strstr(packet, "vFlashWrite:"))
1715 {
1716 unsigned long addr;
1717 unsigned long length;
1718 char *parse = packet + 12;
1719
1720 if (*parse == '\0')
1721 {
1722 ERROR("incomplete vFlashErase packet received, dropping connection");
1723 return ERROR_SERVER_REMOTE_CLOSED;
1724 }
1725 addr = strtoul(parse, &parse, 16);
1726 if (*(parse++) != ':')
1727 {
1728 ERROR("incomplete vFlashErase packet received, dropping connection");
1729 return ERROR_SERVER_REMOTE_CLOSED;
1730 }
1731 length = packet_size - (parse - packet);
1732
1733 /* create a new image if there isn't already one */
1734 if (gdb_connection->vflash_image == NULL)
1735 {
1736 gdb_connection->vflash_image = malloc(sizeof(image_t));
1737 image_open(gdb_connection->vflash_image, "", "build");
1738 }
1739
1740 /* create new section with content from packet buffer */
1741 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1742
1743 gdb_put_packet(connection, "OK", 2);
1744
1745 return ERROR_OK;
1746 }
1747
1748 if (!strcmp(packet, "vFlashDone"))
1749 {
1750 u32 written;
1751
1752 /* process the flashing buffer. No need to erase as GDB
1753 * always issues a vFlashErase first. */
1754 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1755 {
1756 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1757 gdb_put_packet(connection, "E.memtype", 9);
1758 else
1759 gdb_send_error(connection, EIO);
1760 }
1761 else
1762 {
1763 DEBUG("wrote %u bytes from vFlash image to flash", written);
1764 gdb_put_packet(connection, "OK", 2);
1765 }
1766
1767 image_close(gdb_connection->vflash_image);
1768 free(gdb_connection->vflash_image);
1769 gdb_connection->vflash_image = NULL;
1770
1771 return ERROR_OK;
1772 }
1773
1774 gdb_put_packet(connection, "", 0);
1775 return ERROR_OK;
1776 }
1777
1778 int gdb_detach(connection_t *connection, target_t *target)
1779 {
1780 switch( detach_mode )
1781 {
1782 case GDB_DETACH_RESUME:
1783 target->type->resume(target, 1, 0, 1, 0);
1784 break;
1785
1786 case GDB_DETACH_RESET:
1787 target_process_reset(connection->cmd_ctx);
1788 break;
1789
1790 case GDB_DETACH_HALT:
1791 target->type->halt(target);
1792 break;
1793
1794 case GDB_DETACH_NOTHING:
1795 break;
1796 }
1797
1798 gdb_put_packet(connection, "OK", 2);
1799
1800 return ERROR_OK;
1801 }
1802
1803 static void gdb_log_callback(void *priv, const char *file, int line,
1804 const char *function, const char *string)
1805 {
1806 connection_t *connection = priv;
1807 gdb_connection_t *gdb_con = connection->priv;
1808
1809 if (gdb_con->busy)
1810 {
1811 /* do not reply this using the O packet */
1812 return;
1813 }
1814
1815 gdb_output_con(connection, string);
1816 }
1817
1818 int gdb_input_inner(connection_t *connection)
1819 {
1820 gdb_service_t *gdb_service = connection->service->priv;
1821 target_t *target = gdb_service->target;
1822 char packet[GDB_BUFFER_SIZE];
1823 int packet_size;
1824 int retval;
1825 gdb_connection_t *gdb_con = connection->priv;
1826 static int extended_protocol = 0;
1827
1828 /* drain input buffer */
1829 do
1830 {
1831 packet_size = GDB_BUFFER_SIZE-1;
1832 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1833 {
1834 return retval;
1835 }
1836
1837 /* terminate with zero */
1838 packet[packet_size] = 0;
1839
1840 DEBUG("received packet: '%s'", packet);
1841
1842 if (packet_size > 0)
1843 {
1844 retval = ERROR_OK;
1845 switch (packet[0])
1846 {
1847 case 'H':
1848 /* Hct... -- set thread
1849 * we don't have threads, send empty reply */
1850 gdb_put_packet(connection, NULL, 0);
1851 break;
1852 case 'q':
1853 retval = gdb_query_packet(connection, target, packet, packet_size);
1854 break;
1855 case 'g':
1856 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1857 break;
1858 case 'G':
1859 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1860 break;
1861 case 'p':
1862 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1863 break;
1864 case 'P':
1865 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1866 break;
1867 case 'm':
1868 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1869 break;
1870 case 'M':
1871 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1872 break;
1873 case 'z':
1874 case 'Z':
1875 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1876 break;
1877 case '?':
1878 gdb_last_signal_packet(connection, target, packet, packet_size);
1879 break;
1880 case 'c':
1881 case 's':
1882 {
1883 /* We're running/stepping, in which case we can
1884 * forward log output until the target is halted */
1885 gdb_connection_t *gdb_con = connection->priv;
1886 gdb_con->frontend_state = TARGET_RUNNING;
1887 log_add_callback(gdb_log_callback, connection);
1888 gdb_step_continue_packet(connection, target, packet, packet_size);
1889 }
1890 break;
1891 case 'v':
1892 retval = gdb_v_packet(connection, target, packet, packet_size);
1893 break;
1894 case 'D':
1895 retval = gdb_detach(connection, target);
1896 extended_protocol = 0;
1897 break;
1898 case 'X':
1899 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1900 return retval;
1901 break;
1902 case 'k':
1903 if (extended_protocol != 0)
1904 break;
1905 gdb_put_packet(connection, "OK", 2);
1906 return ERROR_SERVER_REMOTE_CLOSED;
1907 case '!':
1908 /* handle extended remote protocol */
1909 extended_protocol = 1;
1910 gdb_put_packet(connection, "OK", 2);
1911 break;
1912 case 'R':
1913 /* handle extended restart packet */
1914 target_process_reset(connection->cmd_ctx);
1915 break;
1916 default:
1917 /* ignore unkown packets */
1918 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1919 gdb_put_packet(connection, NULL, 0);
1920 break;
1921 }
1922
1923 /* if a packet handler returned an error, exit input loop */
1924 if (retval != ERROR_OK)
1925 return retval;
1926 }
1927
1928 if (gdb_con->ctrl_c)
1929 {
1930 if (target->state == TARGET_RUNNING)
1931 {
1932 target->type->halt(target);
1933 gdb_con->ctrl_c = 0;
1934 }
1935 }
1936
1937 } while (gdb_con->buf_cnt > 0);
1938
1939 return ERROR_OK;
1940 }
1941
1942 int gdb_input(connection_t *connection)
1943 {
1944 int retval = gdb_input_inner(connection);
1945 gdb_connection_t *gdb_con = connection->priv;
1946 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1947 return retval;
1948
1949 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1950 if (gdb_con->closed)
1951 return ERROR_SERVER_REMOTE_CLOSED;
1952
1953 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1954 return ERROR_OK;
1955 }
1956
1957 int gdb_init()
1958 {
1959 gdb_service_t *gdb_service;
1960 target_t *target = targets;
1961 int i = 0;
1962
1963 if (!target)
1964 {
1965 WARNING("no gdb ports allocated as no target has been specified");
1966 return ERROR_OK;
1967 }
1968
1969 if (gdb_port == 0)
1970 {
1971 WARNING("no gdb port specified, using default port 3333");
1972 gdb_port = 3333;
1973 }
1974
1975 while (target)
1976 {
1977 char service_name[8];
1978
1979 snprintf(service_name, 8, "gdb-%2.2i", i);
1980
1981 gdb_service = malloc(sizeof(gdb_service_t));
1982 gdb_service->target = target;
1983
1984 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1985
1986 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1987
1988 i++;
1989 target = target->next;
1990 }
1991
1992 return ERROR_OK;
1993 }
1994
1995 /* daemon configuration command gdb_port */
1996 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1997 {
1998 if (argc == 0)
1999 return ERROR_OK;
2000
2001 /* only if the port wasn't overwritten by cmdline */
2002 if (gdb_port == 0)
2003 gdb_port = strtoul(args[0], NULL, 0);
2004
2005 return ERROR_OK;
2006 }
2007
2008 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2009 {
2010 if (argc == 1)
2011 {
2012 if (strcmp(args[0], "resume") == 0)
2013 {
2014 detach_mode = GDB_DETACH_RESUME;
2015 return ERROR_OK;
2016 }
2017 else if (strcmp(args[0], "reset") == 0)
2018 {
2019 detach_mode = GDB_DETACH_RESET;
2020 return ERROR_OK;
2021 }
2022 else if (strcmp(args[0], "halt") == 0)
2023 {
2024 detach_mode = GDB_DETACH_HALT;
2025 return ERROR_OK;
2026 }
2027 else if (strcmp(args[0], "nothing") == 0)
2028 {
2029 detach_mode = GDB_DETACH_NOTHING;
2030 return ERROR_OK;
2031 }
2032 }
2033
2034 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2035 return ERROR_OK;
2036 }
2037
2038 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2039 {
2040 if (argc == 1)
2041 {
2042 if (strcmp(args[0], "enable") == 0)
2043 {
2044 gdb_use_memory_map = 1;
2045 return ERROR_OK;
2046 }
2047 else if (strcmp(args[0], "disable") == 0)
2048 {
2049 gdb_use_memory_map = 0;
2050 return ERROR_OK;
2051 }
2052 }
2053
2054 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2055 return ERROR_OK;
2056 }
2057
2058 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2059 {
2060 if (argc == 1)
2061 {
2062 if (strcmp(args[0], "enable") == 0)
2063 {
2064 gdb_flash_program = 1;
2065 return ERROR_OK;
2066 }
2067 else if (strcmp(args[0], "disable") == 0)
2068 {
2069 gdb_flash_program = 0;
2070 return ERROR_OK;
2071 }
2072 }
2073
2074 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2075 return ERROR_OK;
2076 }
2077
2078 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2079 {
2080 if (argc == 1)
2081 {
2082 if (strcmp(args[0], "enable") == 0)
2083 {
2084 gdb_report_data_abort = 1;
2085 return ERROR_OK;
2086 }
2087 else if (strcmp(args[0], "disable") == 0)
2088 {
2089 gdb_report_data_abort = 0;
2090 return ERROR_OK;
2091 }
2092 }
2093
2094 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2095 return ERROR_OK;
2096 }
2097
2098 int gdb_register_commands(command_context_t *command_context)
2099 {
2100 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2101 COMMAND_CONFIG, "");
2102 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2103 COMMAND_CONFIG, "");
2104 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2105 COMMAND_CONFIG, "");
2106 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2107 COMMAND_CONFIG, "");
2108 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2109 COMMAND_CONFIG, "");
2110 return ERROR_OK;
2111 }

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)