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