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