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