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