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.
 
 
 
 
 
 

2284 lines
54 KiB

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