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