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