You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1587 lines
36 KiB

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