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.
 
 
 
 
 
 

2465 lines
60 KiB

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