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.
 
 
 
 
 
 

2613 lines
63 KiB

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