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.
 
 
 
 
 
 

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