- cleaned up str7, str9 and stm32 flash drivers
[fw/openocd] / src / server / gdb_server.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "gdb_server.h"
27
28 #include "server.h"
29 #include "log.h"
30 #include "binarybuffer.h"
31 #include "breakpoints.h"
32 #include "flash.h"
33 #include "target_request.h"
34
35 #define __USE_GNU
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40
41 #if 0
42 #define _DEBUG_GDB_IO_
43 #endif
44
45 static unsigned short gdb_port;
46
47 enum gdb_detach_mode
48 {
49         GDB_DETACH_RESUME,
50         GDB_DETACH_RESET,
51         GDB_DETACH_HALT,
52         GDB_DETACH_NOTHING
53 };
54
55 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
56
57 int gdb_last_signal(target_t *target)
58 {
59         switch (target->debug_reason)
60         {
61                 case DBG_REASON_DBGRQ:
62                         return 0x2; /* SIGINT */
63                 case DBG_REASON_BREAKPOINT:
64                 case DBG_REASON_WATCHPOINT:
65                 case DBG_REASON_WPTANDBKPT:
66                         return 0x05; /* SIGTRAP */
67                 case DBG_REASON_SINGLESTEP:
68                         return 0x05; /* SIGTRAP */
69                 case DBG_REASON_NOTHALTED:
70                         return 0x0; /* no signal... shouldn't happen */
71                 default:
72                         ERROR("BUG: undefined debug reason");
73                         exit(-1);
74         }
75 }
76
77 int gdb_get_char(connection_t *connection, int* next_char)
78 {
79         gdb_connection_t *gdb_con = connection->priv;
80         char *debug_buffer;
81
82         if (gdb_con->buf_cnt-- > 0)
83         {
84                 *next_char = *(gdb_con->buf_p++);
85                 if (gdb_con->buf_cnt > 0)
86                         connection->input_pending = 1;
87                 else
88                         connection->input_pending = 0;
89
90 #ifdef _DEBUG_GDB_IO_
91                 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
92 #endif
93
94                 return ERROR_OK;
95         }
96
97         while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
98         {
99                 if (gdb_con->buf_cnt == 0)
100                         return ERROR_SERVER_REMOTE_CLOSED;
101
102 #ifdef _WIN32
103                 errno = WSAGetLastError();
104
105                 switch(errno)
106                 {
107                         case WSAEWOULDBLOCK:
108                                 usleep(1000);
109                                 break;
110                         case WSAECONNABORTED:
111                                 return ERROR_SERVER_REMOTE_CLOSED;
112                         default:
113                                 ERROR("read: %d", errno);
114                                 exit(-1);
115                 }
116 #else
117                 switch(errno)
118                 {
119                         case EAGAIN:
120                                 usleep(1000);
121                                 break;
122                         case ECONNABORTED:
123                                 return ERROR_SERVER_REMOTE_CLOSED;
124                         case ECONNRESET:
125                                 return ERROR_SERVER_REMOTE_CLOSED;
126                         default:
127                                 ERROR("read: %s", strerror(errno));
128                                 exit(-1);
129                 }
130 #endif
131         }
132
133         debug_buffer = malloc(gdb_con->buf_cnt + 1);
134         memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
135         debug_buffer[gdb_con->buf_cnt] = 0;
136         DEBUG("received '%s'", debug_buffer);
137         free(debug_buffer);
138
139         gdb_con->buf_p = gdb_con->buffer;
140         gdb_con->buf_cnt--;
141         *next_char = *(gdb_con->buf_p++);
142         if (gdb_con->buf_cnt > 0)
143                 connection->input_pending = 1;
144         else
145                 connection->input_pending = 0;  
146 #ifdef _DEBUG_GDB_IO_
147         DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
148 #endif
149
150         return ERROR_OK;
151 }
152
153 int gdb_putback_char(connection_t *connection, int last_char)
154 {
155         gdb_connection_t *gdb_con = connection->priv;
156
157         if (gdb_con->buf_p > gdb_con->buffer)
158         {
159                 *(--gdb_con->buf_p) = last_char;
160                 gdb_con->buf_cnt++;
161         }
162         else
163         {
164                 ERROR("BUG: couldn't put character back");      
165         }
166
167         return ERROR_OK;
168 }
169
170 int gdb_put_packet(connection_t *connection, char *buffer, int len)
171 {
172         int i;
173         unsigned char my_checksum = 0;
174         char checksum[3];
175         char *debug_buffer;
176         int reply;
177         int retval;
178         gdb_connection_t *gdb_con = connection->priv;
179
180         for (i = 0; i < len; i++)
181                 my_checksum += buffer[i];
182
183         while (1)
184         {
185                 debug_buffer = malloc(len + 1);
186                 memcpy(debug_buffer, buffer, len);
187                 debug_buffer[len] = 0;
188                 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
189                 free(debug_buffer);
190
191                 write_socket(connection->fd, "$", 1);
192                 if (len > 0)
193                         write_socket(connection->fd, buffer, len);
194                 write_socket(connection->fd, "#", 1);
195
196                 snprintf(checksum, 3, "%2.2x", my_checksum);
197
198                 write_socket(connection->fd, checksum, 2);
199
200                 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
201                         return retval;
202
203                 if (reply == '+')
204                         break;
205                 else if (reply == '-')
206                         WARNING("negative reply, retrying");
207                 else if (reply == 0x3)
208                 {
209                         gdb_con->ctrl_c = 1;
210                         if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
211                                 return retval;
212                         if (reply == '+')
213                                 break;
214                         else if (reply == '-')
215                                 WARNING("negative reply, retrying");
216                         else
217                         {
218                                 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
219                                 return ERROR_SERVER_REMOTE_CLOSED;
220                         }
221                 }
222                 else
223                 {
224                         ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
225                         return ERROR_SERVER_REMOTE_CLOSED;
226                 }
227         }
228
229         return ERROR_OK;
230 }
231
232 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
233 {
234         int character;
235         int count = 0;
236         int retval;
237         char checksum[3];
238         unsigned char my_checksum = 0;
239         gdb_connection_t *gdb_con = connection->priv;
240
241         while (1)
242         {
243                 do
244                 {
245                         if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
246                                 return retval;
247
248                         DEBUG("character: '%c'", character);
249
250                         switch (character)
251                         {
252                                 case '$':
253                                         break;
254                                 case '+':
255                                         WARNING("acknowledgment received, but no packet pending");
256                                         break;
257                                 case '-':
258                                         WARNING("negative acknowledgment, but no packet pending");
259                                         break;
260                                 case 0x3:
261                                         gdb_con->ctrl_c = 1;
262                                         *len = 0;
263                                         return ERROR_OK;
264                                 default:
265                                         WARNING("ignoring character 0x%x", character);
266                                         break;
267                         }
268                 } while (character != '$');
269
270                 my_checksum = 0;
271
272                 do
273                 {
274                         if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
275                                 return retval;
276
277                         if (character == '#') break;
278
279                         if (character == '}')
280                         {
281                                 /* data transmitted in binary mode (X packet)
282                                  * uses 0x7d as escape character */
283                                 my_checksum += character & 0xff;
284                                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
285                                         return retval;
286                                 my_checksum += character & 0xff;
287                                 buffer[count++] = (character ^ 0x20) & 0xff;
288                         }
289                         else
290                         {
291                                 my_checksum += character & 0xff;
292                                 buffer[count++] = character & 0xff;
293                         }
294
295                         if (count > *len)
296                         {
297                                 ERROR("packet buffer too small");
298                                 return ERROR_GDB_BUFFER_TOO_SMALL;
299                         }
300                 } while (1);
301
302                 *len = count;
303
304                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
305                         return retval;
306                 checksum[0] = character;
307                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
308                         return retval;
309                 checksum[1] = character;
310                 checksum[2] = 0;
311
312                 if (my_checksum == strtoul(checksum, NULL, 16))
313                 {
314                         write_socket(connection->fd, "+", 1);
315                         break;
316                 }
317
318                 WARNING("checksum error, requesting retransmission");
319                 write_socket(connection->fd, "-", 1);
320         }
321
322         return ERROR_OK;
323 }
324
325 int gdb_output(struct command_context_s *context, char* line)
326 {
327         connection_t *connection = context->output_handler_priv;
328         char *hex_buffer;
329         int i, bin_size;
330
331         bin_size = strlen(line);
332
333         hex_buffer = malloc(bin_size*2 + 4);
334
335         hex_buffer[0] = 'O';
336         for (i=0; i<bin_size; i++)
337                 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
338         hex_buffer[bin_size*2+1] = '0';
339         hex_buffer[bin_size*2+2] = 'a';
340         hex_buffer[bin_size*2+3] = 0x0;
341
342         gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
343
344         free(hex_buffer);
345         return ERROR_OK;
346 }
347
348 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
349 {
350         connection_t *connection = priv;
351         gdb_connection_t *gdb_connection = connection->priv;
352         char sig_reply[4];
353         int signal;
354
355         switch (event)
356         {
357                 case TARGET_EVENT_HALTED:
358                         if (gdb_connection->frontend_state == TARGET_RUNNING)
359                         {
360                                 if (gdb_connection->ctrl_c)
361                                 {
362                                         signal = 0x2;
363                                         gdb_connection->ctrl_c = 0;
364                                 }
365                                 else
366                                 {
367                                         signal = gdb_last_signal(target);
368                                 }
369
370                                 snprintf(sig_reply, 4, "T%2.2x", signal);
371                                 gdb_put_packet(connection, sig_reply, 3);
372                                 gdb_connection->frontend_state = TARGET_HALTED;
373                         }
374                         break;
375                 case TARGET_EVENT_RESUMED:
376                         if (gdb_connection->frontend_state == TARGET_HALTED)
377                         {
378                                 gdb_connection->frontend_state = TARGET_RUNNING;
379                         }
380                         break;
381                 default:
382                         break;
383         }
384
385         return ERROR_OK;
386 }
387
388 int gdb_new_connection(connection_t *connection)
389 {
390         gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
391         gdb_service_t *gdb_service = connection->service->priv;
392         int retval;
393         int initial_ack;
394
395         connection->priv = gdb_connection;
396
397         /* initialize gdb connection information */
398         gdb_connection->buf_p = gdb_connection->buffer;
399         gdb_connection->buf_cnt = 0;
400         gdb_connection->ctrl_c = 0;
401         gdb_connection->frontend_state = TARGET_HALTED;
402         gdb_connection->vflash_image = NULL;
403
404         /* output goes through gdb connection */
405         command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
406
407         /* register callback to be informed about target events */
408         target_register_event_callback(gdb_target_callback_event_handler, connection);  
409
410         /* a gdb session just attached, put the target in halt mode */
411         if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
412                         (retval != ERROR_TARGET_ALREADY_HALTED))
413         {
414                 ERROR("error when trying to halt target");
415                 exit(-1);
416         }
417
418         while (gdb_service->target->state != TARGET_HALTED)
419         {
420                 gdb_service->target->type->poll(gdb_service->target);
421         }
422
423         /* remove the initial ACK from the incoming buffer */
424         if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
425                 return retval;
426
427         if (initial_ack != '+')
428                 gdb_putback_char(connection, initial_ack);
429
430         return ERROR_OK;
431 }
432
433 int gdb_connection_closed(connection_t *connection)
434 {
435         gdb_service_t *gdb_service = connection->service->priv;
436         gdb_connection_t *gdb_connection = connection->priv;
437
438         /* see if an image built with vFlash commands is left */
439         if (gdb_connection->vflash_image)
440         {
441                 image_close(gdb_connection->vflash_image);
442                 free(gdb_connection->vflash_image);
443                 gdb_connection->vflash_image = NULL;
444         }
445
446         /* if this connection registered a debug-message receiver delete it */
447         delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
448         
449         if (connection->priv)
450                 free(connection->priv);
451         else
452                 ERROR("BUG: connection->priv == NULL");
453
454         target_unregister_event_callback(gdb_target_callback_event_handler, connection);
455
456         return ERROR_OK;
457 }
458
459 void gdb_send_error(connection_t *connection, u8 the_error)
460 {
461         char err[4];
462         snprintf(err, 4, "E%2.2X", the_error );
463         gdb_put_packet(connection, err, 3);
464 }
465
466 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
467 {
468         char sig_reply[4];
469         int signal;
470
471         signal = gdb_last_signal(target);
472
473         snprintf(sig_reply, 4, "S%2.2x", signal);
474         gdb_put_packet(connection, sig_reply, 3);
475
476         return ERROR_OK;
477 }
478
479 void gdb_str_to_target(target_t *target, char *str, char *tstr)
480 {
481         int str_len = strlen(str);
482         int i;
483
484         if (str_len % 2)
485         {
486                 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str);
487                 exit(-1);
488         }
489
490         if (target->endianness == TARGET_LITTLE_ENDIAN)
491         {
492                 for (i = 0; i < str_len; i+=2)
493                 {
494                         tstr[str_len - i - 1] = str[i + 1];
495                         tstr[str_len - i - 2] = str[i];
496                 }
497         }
498         else
499         {
500                 for (i = 0; i < str_len; i++)
501                 {
502                         tstr[i] = str[i];
503                 }
504         }       
505 }
506
507 void gdb_target_to_str(target_t *target, char *tstr, char *str)
508 {
509         int str_len = strlen(tstr);
510         int i;
511
512         if (str_len % 2)
513         {
514                 ERROR("BUG: gdb value with uneven number of characters encountered");
515                 exit(-1);
516         }
517
518         if (target->endianness == TARGET_LITTLE_ENDIAN)
519         {
520                 for (i = 0; i < str_len; i+=2)
521                 {
522                         str[str_len - i - 1] = tstr[i + 1];
523                         str[str_len - i - 2] = tstr[i];
524                 }
525         }
526         else
527         {
528                 for (i = 0; i < str_len; i++)
529                 {
530                         str[i] = tstr[i];
531                 }
532         }       
533 }
534
535 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
536 {
537         reg_t **reg_list;
538         int reg_list_size;
539         int retval;
540         int reg_packet_size = 0;
541         char *reg_packet;
542         char *reg_packet_p;
543         int i;
544
545         DEBUG("-");
546
547         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
548         {
549                 switch (retval)
550                 {
551                         case ERROR_TARGET_NOT_HALTED:
552                                 ERROR("gdb requested registers but we're not halted, dropping connection");
553                                 return ERROR_SERVER_REMOTE_CLOSED;
554                         default:
555                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
556                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
557                                 exit(-1);
558                 }
559         }
560
561         for (i = 0; i < reg_list_size; i++)
562         {
563                 reg_packet_size += reg_list[i]->size;
564         }
565
566         reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
567         reg_packet_p = reg_packet;
568
569         for (i = 0; i < reg_list_size; i++)
570         {
571                 char *hex_buf = buf_to_str(reg_list[i]->value, reg_list[i]->size, 16);
572                 DEBUG("hex_buf: %s", hex_buf);
573                 gdb_str_to_target(target, hex_buf, reg_packet_p);
574                 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
575                 free(hex_buf);
576         }
577
578         reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
579         DEBUG("reg_packet: %s", reg_packet_p);
580         free(reg_packet_p);
581
582         gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
583         free(reg_packet);
584
585         free(reg_list);
586
587         return ERROR_OK;
588 }
589
590 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
591 {
592         int i;
593         reg_t **reg_list;
594         int reg_list_size;
595         int retval;
596         char *packet_p;
597
598         DEBUG("-");
599
600         /* skip command character */
601         packet++;
602         packet_size--;
603
604         if (packet_size % 2)
605         {
606                 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
607                 return ERROR_SERVER_REMOTE_CLOSED;
608         }
609
610         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
611         {
612                 switch (retval)
613                 {
614                         case ERROR_TARGET_NOT_HALTED:
615                                 ERROR("gdb tried to registers but we're not halted, dropping connection");
616                                 return ERROR_SERVER_REMOTE_CLOSED;
617                         default:
618                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
619                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
620                                 exit(-1);
621                 }
622         }
623
624         packet_p = packet;
625         for (i = 0; i < reg_list_size; i++)
626         {
627                 u8 *bin_buf;
628                 char *hex_buf;
629                 reg_arch_type_t *arch_type;
630
631                 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
632                 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
633                 gdb_target_to_str(target, packet_p, hex_buf);
634
635                 /* convert hex-string to binary buffer */
636                 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
637                 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
638
639                 /* get register arch_type, and call set method */       
640                 arch_type = register_get_arch_type(reg_list[i]->arch_type);
641                 if (arch_type == NULL)
642                 {
643                         ERROR("BUG: encountered unregistered arch type");
644                         exit(-1);
645                 }
646                 arch_type->set(reg_list[i], bin_buf);
647
648                 /* advance packet pointer */            
649                 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
650
651                 free(bin_buf);
652                 free(hex_buf);
653         }
654
655         /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */ 
656         free(reg_list);
657
658         gdb_put_packet(connection, "OK", 2);
659
660         return ERROR_OK;
661 }
662
663 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
664 {
665         char *reg_packet;
666         int reg_num = strtoul(packet + 1, NULL, 16);
667         reg_t **reg_list;
668         int reg_list_size;
669         int retval;
670         char *hex_buf;
671
672         DEBUG("-");
673
674         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
675         {
676                 switch (retval)
677                 {
678                         case ERROR_TARGET_NOT_HALTED:
679                                 ERROR("gdb requested registers but we're not halted, dropping connection");
680                                 return ERROR_SERVER_REMOTE_CLOSED;
681                         default:
682                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
683                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
684                                 exit(-1);
685                 }
686         }
687
688         if (reg_list_size <= reg_num)
689         {
690                 ERROR("gdb requested a non-existing register");
691                 exit(-1);
692         }
693
694         reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
695
696         hex_buf = buf_to_str(reg_list[reg_num]->value, reg_list[reg_num]->size, 16);
697
698         gdb_str_to_target(target, hex_buf, reg_packet);
699
700         gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
701
702         free(reg_list);
703         free(reg_packet);
704         free(hex_buf);
705
706         return ERROR_OK;
707 }
708
709 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
710 {
711         char *separator;
712         char *hex_buf;
713         u8 *bin_buf;
714         int reg_num = strtoul(packet + 1, &separator, 16);
715         reg_t **reg_list;
716         int reg_list_size;
717         int retval;
718         reg_arch_type_t *arch_type;
719
720         DEBUG("-");
721
722         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
723         {
724                 switch (retval)
725                 {
726                         case ERROR_TARGET_NOT_HALTED:
727                                 ERROR("gdb tried to set a register but we're not halted, dropping connection");
728                                 return ERROR_SERVER_REMOTE_CLOSED;
729                         default:
730                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
731                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
732                                 exit(-1);
733                 }
734         }
735
736         if (reg_list_size < reg_num)
737         {
738                 ERROR("gdb requested a non-existing register");
739                 return ERROR_SERVER_REMOTE_CLOSED;
740         }
741
742         if (*separator != '=')
743         {
744                 ERROR("GDB 'set register packet', but no '=' following the register number");
745                 return ERROR_SERVER_REMOTE_CLOSED;
746         }
747
748         /* convert from GDB-string (target-endian) to hex-string (big-endian) */
749         hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
750         gdb_target_to_str(target, separator + 1, hex_buf);
751
752         /* convert hex-string to binary buffer */
753         bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
754         str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
755
756         /* get register arch_type, and call set method */       
757         arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
758         if (arch_type == NULL)
759         {
760                 ERROR("BUG: encountered unregistered arch type");
761                 exit(-1);
762         }
763         arch_type->set(reg_list[reg_num], bin_buf);
764
765         gdb_put_packet(connection, "OK", 2);
766
767         free(bin_buf);
768         free(hex_buf);
769         free(reg_list);
770
771         return ERROR_OK;
772 }
773
774 int gdb_memory_packet_error(connection_t *connection, int retval)
775 {
776         switch (retval)
777         {
778                 case ERROR_TARGET_NOT_HALTED:
779                         ERROR("gdb tried to read memory but we're not halted, dropping connection");
780                         return ERROR_SERVER_REMOTE_CLOSED;
781                         break;
782                 case ERROR_TARGET_DATA_ABORT:
783                         gdb_send_error(connection, EIO);
784                         break;
785                 case ERROR_TARGET_TRANSLATION_FAULT:
786                         gdb_send_error(connection, EFAULT);
787                         break;
788                 case ERROR_TARGET_UNALIGNED_ACCESS:
789                         gdb_send_error(connection, EFAULT);
790                         break;
791                 default:
792                         ERROR("BUG: unexpected error %i", retval);
793                         exit(-1);
794         }
795
796         return ERROR_OK;
797 }
798
799 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
800 {
801         char *separator;
802         u32 addr = 0;
803         u32 len = 0;
804
805         u8 *buffer;
806         char *hex_buffer;
807
808         int i;
809         int retval;
810
811         /* skip command character */
812         packet++;
813
814         addr = strtoul(packet, &separator, 16);
815
816         if (*separator != ',')
817         {
818                 ERROR("incomplete read memory packet received, dropping connection");
819                 return ERROR_SERVER_REMOTE_CLOSED;
820         }
821
822         len = strtoul(separator+1, NULL, 16);
823
824         buffer = malloc(len);
825
826         DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
827
828         switch (len)
829         {
830                 case 4:
831                         if ((addr % 4) == 0)
832                                 retval = target->type->read_memory(target, addr, 4, 1, buffer);
833                         else
834                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
835                         break;
836                 case 2:
837                         if ((addr % 2) == 0)
838                                 retval = target->type->read_memory(target, addr, 2, 1, buffer);
839                         else
840                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
841                         break;
842                 default:
843                         if (((addr % 4) == 0) && ((len % 4) == 0))
844                                 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
845                         else
846                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
847         }
848
849         if (retval == ERROR_OK)
850         {
851                 hex_buffer = malloc(len * 2 + 1);
852
853                 for (i=0; i<len; i++)
854                         snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
855
856                 gdb_put_packet(connection, hex_buffer, len * 2);
857
858                 free(hex_buffer);
859         }
860         else
861         {
862                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
863                         return retval; 
864         }
865
866         free(buffer);
867
868         return ERROR_OK;
869 }
870
871 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
872 {
873         char *separator;
874         u32 addr = 0;
875         u32 len = 0;
876
877         u8 *buffer;
878
879         int i;
880         int retval;
881
882         /* skip command character */
883         packet++;
884
885         addr = strtoul(packet, &separator, 16);
886
887         if (*separator != ',')
888         {
889                 ERROR("incomplete write memory packet received, dropping connection");
890                 return ERROR_SERVER_REMOTE_CLOSED;
891         }
892
893         len = strtoul(separator+1, &separator, 16);
894
895         if (*(separator++) != ':')
896         {
897                 ERROR("incomplete write memory packet received, dropping connection");
898                 return ERROR_SERVER_REMOTE_CLOSED;
899         }
900
901         buffer = malloc(len);
902
903         DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
904
905         for (i=0; i<len; i++)
906         {
907                 u32 tmp;
908                 sscanf(separator + 2*i, "%2x", &tmp);
909                 buffer[i] = tmp;
910         }
911
912         retval = ERROR_OK;
913         switch (len)
914         {
915                 /* handle sized writes */
916                 case 4:
917                         if ((addr % 4) == 0)
918                                 retval = target->type->write_memory(target, addr, 4, 1, buffer);
919                         else
920                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
921                         break;
922                 case 2:
923                         if ((addr % 2) == 0)
924                                 retval = target->type->write_memory(target, addr, 2, 1, buffer);
925                         else
926                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
927                         break;
928                 case 3:
929                 case 1:
930                         retval = target->type->write_memory(target, addr, 1, len, buffer);
931                         break;
932                         /* handle bulk writes */
933                 default:
934                         retval = target_write_buffer(target, addr, len, buffer);
935                         break;
936         }
937
938         if (retval == ERROR_OK)
939         {
940                 gdb_put_packet(connection, "OK", 2);
941         }
942         else
943         {
944                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
945                         return retval; 
946         }
947
948         free(buffer);
949
950         return ERROR_OK;
951 }
952
953 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
954 {
955         char *separator;
956         u32 addr = 0;
957         u32 len = 0;
958
959         u8 *buffer;
960         int retval;
961
962         /* skip command character */
963         packet++;
964
965         addr = strtoul(packet, &separator, 16);
966
967         if (*separator != ',')
968         {
969                 ERROR("incomplete write memory binary packet received, dropping connection");
970                 return ERROR_SERVER_REMOTE_CLOSED;
971         }
972
973         len = strtoul(separator+1, &separator, 16);
974
975         if (*(separator++) != ':')
976         {
977                 ERROR("incomplete write memory binary packet received, dropping connection");
978                 return ERROR_SERVER_REMOTE_CLOSED;
979         }
980
981         retval = ERROR_OK;
982         if( len ) {
983
984                 buffer = malloc(len);
985
986                 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
987
988                 memcpy( buffer, separator, len );
989
990                 switch (len)
991                 {
992                         case 4:
993                                 if ((addr % 4) == 0)
994                                         retval = target->type->write_memory(target, addr, 4, 1, buffer);
995                                 else
996                                         retval = target->type->write_memory(target, addr, 1, len, buffer);
997                                 break;
998                         case 2:
999                                 if ((addr % 2) == 0)
1000                                         retval = target->type->write_memory(target, addr, 2, 1, buffer);
1001                                 else
1002                                         retval = target->type->write_memory(target, addr, 1, len, buffer);
1003                                 break;
1004                         case 3:
1005                         case 1:
1006                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
1007                                 break;
1008                         default:
1009                                 retval = target_write_buffer(target, addr, len, buffer);
1010                                 break;
1011                 }
1012
1013                 free(buffer);
1014         }
1015
1016         if (retval == ERROR_OK)
1017         {
1018                 gdb_put_packet(connection, "OK", 2);
1019         }
1020         else
1021         {
1022                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1023                         return retval; 
1024         }
1025
1026         return ERROR_OK;
1027 }
1028
1029 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1030 {
1031         int current = 0;
1032         u32 address = 0x0;
1033
1034         DEBUG("-");
1035
1036         if (packet_size > 1)
1037         {
1038                 packet[packet_size] = 0;
1039                 address = strtoul(packet + 1, NULL, 16);
1040         }
1041         else
1042         {
1043                 current = 1;
1044         }
1045
1046         if (packet[0] == 'c')
1047         {
1048                 DEBUG("continue");
1049                 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1050         }
1051         else if (packet[0] == 's')
1052         {
1053                 DEBUG("step");
1054                 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1055         }
1056 }
1057
1058 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1059 {
1060         switch (retval)
1061         {
1062                 case ERROR_TARGET_NOT_HALTED:
1063                         ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1064                         return ERROR_SERVER_REMOTE_CLOSED;
1065                         break;
1066                 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1067                         gdb_send_error(connection, EBUSY);
1068                         break;
1069                 default:
1070                         ERROR("BUG: unexpected error %i", retval);
1071                         exit(-1);
1072         }
1073
1074         return ERROR_OK;
1075 }
1076
1077 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1078 {
1079         int type;
1080         enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1081         enum watchpoint_rw wp_type;
1082         u32 address;
1083         u32 size;
1084         char *separator;
1085         int retval;
1086
1087         DEBUG("-");
1088
1089         type = strtoul(packet + 1, &separator, 16);
1090
1091         if (type == 0)  /* memory breakpoint */
1092                 bp_type = BKPT_SOFT;
1093         else if (type == 1) /* hardware breakpoint */
1094                 bp_type = BKPT_HARD;
1095         else if (type == 2) /* write watchpoint */
1096                 wp_type = WPT_WRITE;
1097         else if (type == 3) /* read watchpoint */
1098                 wp_type = WPT_READ;
1099         else if (type == 4) /* access watchpoint */
1100                 wp_type = WPT_ACCESS;
1101
1102         if (*separator != ',')
1103         {
1104                 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1105                 return ERROR_SERVER_REMOTE_CLOSED;
1106         }
1107
1108         address = strtoul(separator+1, &separator, 16);
1109
1110         if (*separator != ',')
1111         {
1112                 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1113                 return ERROR_SERVER_REMOTE_CLOSED;
1114         }
1115
1116         size = strtoul(separator+1, &separator, 16);
1117
1118         switch (type)
1119         {
1120                 case 0:
1121                 case 1:
1122                         if (packet[0] == 'Z')
1123                         {
1124                                 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1125                                 {
1126                                         if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1127                                                 return retval;
1128                                 }
1129                                 else
1130                                 {
1131                                         gdb_put_packet(connection, "OK", 2);
1132                                 }
1133                         }
1134                         else
1135                         {
1136                                 breakpoint_remove(target, address);
1137                                 gdb_put_packet(connection, "OK", 2);
1138                         }
1139                         break;
1140                 case 2:
1141                 case 3:
1142                 case 4:
1143                 {
1144                         if (packet[0] == 'Z')
1145                         {
1146                                 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1147                                 {
1148                                         if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1149                                                 return retval;
1150                                 }
1151                                 else
1152                                 {
1153                                         gdb_put_packet(connection, "OK", 2);
1154                                 }
1155                         }
1156                         else
1157                         {
1158                                 watchpoint_remove(target, address);
1159                                 gdb_put_packet(connection, "OK", 2);
1160                         }
1161                         break;
1162                 }
1163                 default:
1164                         break;
1165         }
1166
1167         return ERROR_OK;
1168 }
1169
1170 void gdb_query_packet(connection_t *connection, char *packet, int packet_size)
1171 {
1172         command_context_t *cmd_ctx = connection->cmd_ctx;
1173
1174         if (strstr(packet, "qRcmd,"))
1175         {
1176                 if (packet_size > 6)
1177                 {
1178                         char *cmd;
1179                         int i;
1180                         cmd = malloc((packet_size - 6)/2 + 1);
1181                         for (i=0; i < (packet_size - 6)/2; i++)
1182                         {
1183                                 u32 tmp;
1184                                 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1185                                 cmd[i] = tmp;
1186                         }
1187                         cmd[(packet_size - 6)/2] = 0x0;
1188                         command_run_line(cmd_ctx, cmd);
1189                         free(cmd);
1190                 }
1191                 gdb_put_packet(connection, "OK", 2);
1192                 return;
1193         }
1194
1195         gdb_put_packet(connection, "", 0);
1196 }
1197
1198 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1199 {
1200         gdb_connection_t *gdb_connection = connection->priv;
1201         gdb_service_t *gdb_service = connection->service->priv;
1202         int result;
1203
1204         if (strstr(packet, "vFlashErase:"))
1205         {
1206                 unsigned long addr;
1207                 unsigned long length;
1208                 char *parse = packet + 12;
1209                 if (*parse == '\0')
1210                 {
1211                         ERROR("incomplete vFlashErase packet received, dropping connection");
1212                         return ERROR_SERVER_REMOTE_CLOSED;
1213                 }
1214
1215                 addr = strtoul(parse, &parse, 16);
1216
1217                 if (*(parse++) != ',' || *parse == '\0')
1218                 {
1219                         ERROR("incomplete vFlashErase packet received, dropping connection");
1220                         return ERROR_SERVER_REMOTE_CLOSED;
1221                 }
1222
1223                 length = strtoul(parse, &parse, 16);
1224
1225                 if (*parse != '\0')
1226                 {
1227                         ERROR("incomplete vFlashErase packet received, dropping connection");
1228                         return ERROR_SERVER_REMOTE_CLOSED;
1229                 }
1230
1231                 /* perform erase */
1232                 if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
1233                 {
1234                         /* GDB doesn't evaluate the actual error number returned,
1235                          * treat a failed erase as an I/O error
1236                          */
1237                         gdb_send_error(connection, EIO);
1238                         ERROR("flash_erase returned %i", result);
1239                 }
1240                 else
1241                         gdb_put_packet(connection, "OK", 2);
1242
1243                 return ERROR_OK;
1244         }
1245
1246         if (strstr(packet, "vFlashWrite:"))
1247         {
1248                 unsigned long addr;
1249                 unsigned long length;
1250                 char *parse = packet + 12;
1251
1252                 if (*parse == '\0')
1253                 {
1254                         ERROR("incomplete vFlashErase packet received, dropping connection");
1255                         return ERROR_SERVER_REMOTE_CLOSED;
1256                 }
1257                 addr = strtoul(parse, &parse, 16);
1258                 if (*(parse++) != ':')
1259                 {
1260                         ERROR("incomplete vFlashErase packet received, dropping connection");
1261                         return ERROR_SERVER_REMOTE_CLOSED;
1262                 }
1263                 length = packet_size - (parse - packet);
1264                 
1265                 /* create a new image if there isn't already one */
1266                 if (gdb_connection->vflash_image == NULL)
1267                 {
1268                         gdb_connection->vflash_image = malloc(sizeof(image_t));
1269                         image_open(gdb_connection->vflash_image, "", "build");
1270                 }
1271
1272                 /* create new section with content from packet buffer */
1273                 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1274
1275                 gdb_put_packet(connection, "OK", 2);
1276
1277                 return ERROR_OK;
1278         }
1279
1280         if (!strcmp(packet, "vFlashDone"))
1281         {
1282                 u32 written;
1283                 char *error_str;
1284
1285                 /* process the flashing buffer */
1286                 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1287                 {
1288                         if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1289                                 gdb_put_packet(connection, "E.memtype", 9);
1290                         else
1291                                 gdb_send_error(connection, EIO);
1292                         
1293                         if (error_str)
1294                         {
1295                                 ERROR("flash writing failed: %s", error_str);
1296                                 free(error_str);
1297                         }
1298                 }
1299                 else
1300                 {
1301                         DEBUG("wrote %u bytes from vFlash image to flash", written);
1302                         gdb_put_packet(connection, "OK", 2);
1303                 }
1304                 
1305                 image_close(gdb_connection->vflash_image);
1306                 free(gdb_connection->vflash_image);
1307                 gdb_connection->vflash_image = NULL;
1308
1309                 return ERROR_OK;
1310         }
1311
1312         gdb_put_packet(connection, "", 0);
1313         return ERROR_OK;
1314 }
1315
1316 int gdb_detach(connection_t *connection, target_t *target)
1317 {
1318         switch( detach_mode )
1319         {
1320                 case GDB_DETACH_RESUME:
1321                         target->type->resume(target, 1, 0, 1, 0);
1322                         break;
1323                 
1324                 case GDB_DETACH_RESET:
1325                         target_process_reset(connection->cmd_ctx);
1326                         break;
1327                 
1328                 case GDB_DETACH_HALT:
1329                         target->type->halt(target);
1330                         break;
1331                 
1332                 case GDB_DETACH_NOTHING:
1333                         break;
1334         }
1335         
1336         gdb_put_packet(connection, "OK", 2);
1337         
1338         return ERROR_OK;
1339 }
1340
1341 int gdb_input(connection_t *connection)
1342 {
1343         gdb_service_t *gdb_service = connection->service->priv;
1344         target_t *target = gdb_service->target;
1345         char packet[GDB_BUFFER_SIZE];
1346         int packet_size;
1347         int retval;
1348         gdb_connection_t *gdb_con = connection->priv;
1349
1350         /* drain input buffer */
1351         do
1352         {
1353                 packet_size = GDB_BUFFER_SIZE-1;
1354                 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1355                 {
1356                         switch (retval)
1357                         {
1358                                 case ERROR_GDB_BUFFER_TOO_SMALL:
1359                                         ERROR("BUG: buffer supplied for gdb packet was too small");
1360                                         exit(-1);
1361                                 case ERROR_SERVER_REMOTE_CLOSED:
1362                                         return ERROR_SERVER_REMOTE_CLOSED;
1363                                 default:
1364                                         ERROR("BUG: unexpected error");
1365                                         exit(-1);
1366                         }
1367                 }
1368
1369                 /* terminate with zero */
1370                 packet[packet_size] = 0;
1371
1372                 DEBUG("recevied packet: '%s'", packet);
1373
1374                 if (packet_size > 0)
1375                 {
1376                         retval = ERROR_OK;
1377                         switch (packet[0])
1378                         {
1379                                 case 'H':
1380                                         /* Hct... -- set thread 
1381                                          * we don't have threads, send empty reply */
1382                                         gdb_put_packet(connection, NULL, 0);
1383                                         break;
1384                                 case 'q':
1385                                         gdb_query_packet(connection, packet, packet_size);
1386                                         break;
1387                                 case 'g':
1388                                         retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1389                                         break;
1390                                 case 'G':
1391                                         retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1392                                         break;
1393                                 case 'p':
1394                                         retval = gdb_get_register_packet(connection, target, packet, packet_size);
1395                                         break;
1396                                 case 'P':
1397                                         retval = gdb_set_register_packet(connection, target, packet, packet_size);
1398                                         break;
1399                                 case 'm':
1400                                         retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1401                                         break;
1402                                 case 'M':
1403                                         retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1404                                         break;
1405                                 case 'z':
1406                                 case 'Z':
1407                                         retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1408                                         break;
1409                                 case '?':
1410                                         gdb_last_signal_packet(connection, target, packet, packet_size);
1411                                         break;
1412                                 case 'c':
1413                                 case 's':
1414                                         gdb_step_continue_packet(connection, target, packet, packet_size);
1415                                         break;
1416                                 case 'v':
1417                                         retval = gdb_v_packet(connection, target, packet, packet_size);
1418                                         break;
1419                                 case 'D':
1420                                         retval = gdb_detach(connection, target);
1421                                         break;
1422                                 case 'X':
1423                                         if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1424                                                 return retval;
1425                                         break;
1426                                 case 'k':
1427                                         gdb_put_packet(connection, "OK", 2);
1428                                         return ERROR_SERVER_REMOTE_CLOSED;
1429                                 default:
1430                                         /* ignore unkown packets */
1431                                         DEBUG("ignoring 0x%2.2x packet", packet[0]);
1432                                         gdb_put_packet(connection, NULL, 0);
1433                                         break;
1434                         }
1435
1436                         /* if a packet handler returned an error, exit input loop */
1437                         if (retval != ERROR_OK)
1438                                 return retval;
1439                 }
1440
1441                 if (gdb_con->ctrl_c)
1442                 {
1443                         if (target->state == TARGET_RUNNING)
1444                         {
1445                                 target->type->halt(target);
1446                                 gdb_con->ctrl_c = 0;
1447                         }
1448                 }
1449
1450         } while (gdb_con->buf_cnt > 0);
1451
1452         return ERROR_OK;
1453 }
1454
1455 int gdb_init()
1456 {
1457         gdb_service_t *gdb_service;
1458         target_t *target = targets;
1459         int i = 0;
1460
1461         if (!target)
1462         {
1463                 WARNING("no gdb ports allocated as no target has been specified");
1464                 return ERROR_OK;
1465         }
1466
1467         if (gdb_port == 0)
1468         {
1469                 WARNING("no gdb port specified, using default port 3333");
1470                 gdb_port = 3333;
1471         }
1472
1473         while (target)
1474         {
1475                 char service_name[8];
1476
1477                 snprintf(service_name, 8, "gdb-%2.2i", i);
1478
1479                 gdb_service = malloc(sizeof(gdb_service_t));
1480                 gdb_service->target = target;
1481
1482                 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1483
1484                 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1485
1486                 i++;
1487                 target = target->next;
1488         }
1489
1490         return ERROR_OK;
1491 }
1492
1493 /* daemon configuration command gdb_port */
1494 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1495 {
1496         if (argc == 0)
1497                 return ERROR_OK;
1498
1499         /* only if the port wasn't overwritten by cmdline */
1500         if (gdb_port == 0)
1501                 gdb_port = strtoul(args[0], NULL, 0);
1502
1503         return ERROR_OK;
1504 }
1505
1506 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1507 {
1508         if (argc == 1)
1509         {
1510                 if (strcmp(args[0], "resume") == 0)
1511                 {
1512                         detach_mode = GDB_DETACH_RESUME;
1513                         return ERROR_OK;
1514                 }
1515                 else if (strcmp(args[0], "reset") == 0)
1516                 {
1517                         detach_mode = GDB_DETACH_RESET;
1518                         return ERROR_OK;
1519                 }
1520                 else if (strcmp(args[0], "halt") == 0)
1521                 {
1522                         detach_mode = GDB_DETACH_HALT;
1523                         return ERROR_OK;
1524                 }
1525                 else if (strcmp(args[0], "nothing") == 0)
1526                 {
1527                         detach_mode = GDB_DETACH_NOTHING;
1528                         return ERROR_OK;
1529                 }
1530         }
1531         
1532         WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1533         return ERROR_OK;
1534 }
1535
1536 int gdb_register_commands(command_context_t *command_context)
1537 {
1538         register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1539                         COMMAND_CONFIG, "");
1540         register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1541                         COMMAND_CONFIG, "");
1542         
1543         return ERROR_OK;
1544 }