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