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.
 
 
 
 
 
 

2650 lines
66 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. * Copyright (C) ST-Ericsson SA 2011 *
  15. * michel.jaouen@stericsson.com : smp minimum support *
  16. * *
  17. * This program is free software; you can redistribute it and/or modify *
  18. * it under the terms of the GNU General Public License as published by *
  19. * the Free Software Foundation; either version 2 of the License, or *
  20. * (at your option) any later version. *
  21. * *
  22. * This program is distributed in the hope that it will be useful, *
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  25. * GNU General Public License for more details. *
  26. * *
  27. * You should have received a copy of the GNU General Public License *
  28. * along with this program; if not, write to the *
  29. * Free Software Foundation, Inc., *
  30. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  31. ***************************************************************************/
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include <target/breakpoints.h>
  36. #include <target/target_request.h>
  37. #include <target/register.h>
  38. #include "server.h"
  39. #include <flash/nor/core.h>
  40. #include "gdb_server.h"
  41. #include <target/image.h>
  42. #include <jtag/jtag.h>
  43. #include "rtos/rtos.h"
  44. #include "target/smp.h"
  45. /**
  46. * @file
  47. * GDB server implementation.
  48. *
  49. * This implements the GDB Remote Serial Protocol, over TCP connections,
  50. * giving GDB access to the JTAG or other hardware debugging facilities
  51. * found in most modern embedded processors.
  52. */
  53. /* private connection data for GDB */
  54. struct gdb_connection
  55. {
  56. char buffer[GDB_BUFFER_SIZE];
  57. char *buf_p;
  58. int buf_cnt;
  59. int ctrl_c;
  60. enum target_state frontend_state;
  61. struct image *vflash_image;
  62. int closed;
  63. int busy;
  64. int noack_mode;
  65. bool sync; /* set flag to true if you want the next stepi to return immediately.
  66. allowing GDB to pick up a fresh set of register values from the target
  67. without modifying the target state. */
  68. /* We delay reporting memory write errors until next step/continue or memory
  69. * write. This improves performance of gdb load significantly as the GDB packet
  70. * can be replied immediately and a new GDB packet will be ready without delay
  71. * (ca. 10% or so...).
  72. */
  73. bool mem_write_error;
  74. };
  75. #if 0
  76. #define _DEBUG_GDB_IO_
  77. #endif
  78. static struct gdb_connection *current_gdb_connection;
  79. static int gdb_breakpoint_override;
  80. static enum breakpoint_type gdb_breakpoint_override_type;
  81. static int gdb_error(struct connection *connection, int retval);
  82. static const char *gdb_port;
  83. static const char *gdb_port_next;
  84. static const char DIGITS[16] = "0123456789abcdef";
  85. static void gdb_log_callback(void *priv, const char *file, unsigned line,
  86. const char *function, const char *string);
  87. /* number of gdb connections, mainly to suppress gdb related debugging spam
  88. * in helper/log.c when no gdb connections are actually active */
  89. int gdb_actual_connections;
  90. /* set if we are sending a memory map to gdb
  91. * via qXfer:memory-map:read packet */
  92. /* enabled by default*/
  93. static int gdb_use_memory_map = 1;
  94. /* enabled by default*/
  95. static int gdb_flash_program = 1;
  96. /* if set, data aborts cause an error to be reported in memory read packets
  97. * see the code in gdb_read_memory_packet() for further explanations.
  98. * Disabled by default.
  99. */
  100. static int gdb_report_data_abort;
  101. static int gdb_last_signal(struct target *target)
  102. {
  103. switch (target->debug_reason)
  104. {
  105. case DBG_REASON_DBGRQ:
  106. return 0x2; /* SIGINT */
  107. case DBG_REASON_BREAKPOINT:
  108. case DBG_REASON_WATCHPOINT:
  109. case DBG_REASON_WPTANDBKPT:
  110. return 0x05; /* SIGTRAP */
  111. case DBG_REASON_SINGLESTEP:
  112. return 0x05; /* SIGTRAP */
  113. case DBG_REASON_NOTHALTED:
  114. return 0x0; /* no signal... shouldn't happen */
  115. default:
  116. LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
  117. return 0x0;
  118. }
  119. }
  120. static int check_pending(struct connection *connection,
  121. int timeout_s, int *got_data)
  122. {
  123. /* a non-blocking socket will block if there is 0 bytes available on the socket,
  124. * but return with as many bytes as are available immediately
  125. */
  126. struct timeval tv;
  127. fd_set read_fds;
  128. struct gdb_connection *gdb_con = connection->priv;
  129. int t;
  130. if (got_data == NULL)
  131. got_data=&t;
  132. *got_data = 0;
  133. if (gdb_con->buf_cnt > 0)
  134. {
  135. *got_data = 1;
  136. return ERROR_OK;
  137. }
  138. FD_ZERO(&read_fds);
  139. FD_SET(connection->fd, &read_fds);
  140. tv.tv_sec = timeout_s;
  141. tv.tv_usec = 0;
  142. if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
  143. {
  144. /* This can typically be because a "monitor" command took too long
  145. * before printing any progress messages
  146. */
  147. if (timeout_s > 0)
  148. {
  149. return ERROR_GDB_TIMEOUT;
  150. } else
  151. {
  152. return ERROR_OK;
  153. }
  154. }
  155. *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
  156. return ERROR_OK;
  157. }
  158. static int gdb_get_char_inner(struct connection *connection, int* next_char)
  159. {
  160. struct gdb_connection *gdb_con = connection->priv;
  161. int retval = ERROR_OK;
  162. #ifdef _DEBUG_GDB_IO_
  163. char *debug_buffer;
  164. #endif
  165. for (;;)
  166. {
  167. if (connection->service->type != CONNECTION_TCP)
  168. {
  169. gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
  170. }
  171. else
  172. {
  173. retval = check_pending(connection, 1, NULL);
  174. if (retval != ERROR_OK)
  175. return retval;
  176. gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
  177. }
  178. if (gdb_con->buf_cnt > 0)
  179. {
  180. break;
  181. }
  182. if (gdb_con->buf_cnt == 0)
  183. {
  184. gdb_con->closed = 1;
  185. return ERROR_SERVER_REMOTE_CLOSED;
  186. }
  187. #ifdef _WIN32
  188. errno = WSAGetLastError();
  189. switch (errno)
  190. {
  191. case WSAEWOULDBLOCK:
  192. usleep(1000);
  193. break;
  194. case WSAECONNABORTED:
  195. gdb_con->closed = 1;
  196. return ERROR_SERVER_REMOTE_CLOSED;
  197. case WSAECONNRESET:
  198. gdb_con->closed = 1;
  199. return ERROR_SERVER_REMOTE_CLOSED;
  200. default:
  201. LOG_ERROR("read: %d", errno);
  202. exit(-1);
  203. }
  204. #else
  205. switch (errno)
  206. {
  207. case EAGAIN:
  208. usleep(1000);
  209. break;
  210. case ECONNABORTED:
  211. gdb_con->closed = 1;
  212. return ERROR_SERVER_REMOTE_CLOSED;
  213. case ECONNRESET:
  214. gdb_con->closed = 1;
  215. return ERROR_SERVER_REMOTE_CLOSED;
  216. default:
  217. LOG_ERROR("read: %s", strerror(errno));
  218. gdb_con->closed = 1;
  219. return ERROR_SERVER_REMOTE_CLOSED;
  220. }
  221. #endif
  222. }
  223. #ifdef _DEBUG_GDB_IO_
  224. debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
  225. LOG_DEBUG("received '%s'", debug_buffer);
  226. free(debug_buffer);
  227. #endif
  228. gdb_con->buf_p = gdb_con->buffer;
  229. gdb_con->buf_cnt--;
  230. *next_char = *(gdb_con->buf_p++);
  231. if (gdb_con->buf_cnt > 0)
  232. connection->input_pending = 1;
  233. else
  234. connection->input_pending = 0;
  235. #ifdef _DEBUG_GDB_IO_
  236. LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  237. #endif
  238. return retval;
  239. }
  240. /**
  241. * The cool thing about this fn is that it allows buf_p and buf_cnt to be
  242. * held in registers in the inner loop.
  243. *
  244. * For small caches and embedded systems this is important!
  245. */
  246. static inline int gdb_get_char_fast(struct connection *connection, int* next_char, char **buf_p, int *buf_cnt)
  247. {
  248. int retval = ERROR_OK;
  249. if ((*buf_cnt)-- > 0)
  250. {
  251. *next_char = **buf_p;
  252. (*buf_p)++;
  253. if (*buf_cnt > 0)
  254. connection->input_pending = 1;
  255. else
  256. connection->input_pending = 0;
  257. #ifdef _DEBUG_GDB_IO_
  258. LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  259. #endif
  260. return ERROR_OK;
  261. }
  262. struct gdb_connection *gdb_con = connection->priv;
  263. gdb_con->buf_p = *buf_p;
  264. gdb_con->buf_cnt = *buf_cnt;
  265. retval = gdb_get_char_inner(connection, next_char);
  266. *buf_p = gdb_con->buf_p;
  267. *buf_cnt = gdb_con->buf_cnt;
  268. return retval;
  269. }
  270. static int gdb_get_char(struct connection *connection, int* next_char)
  271. {
  272. struct gdb_connection *gdb_con = connection->priv;
  273. return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
  274. }
  275. static int gdb_putback_char(struct connection *connection, int last_char)
  276. {
  277. struct gdb_connection *gdb_con = connection->priv;
  278. if (gdb_con->buf_p > gdb_con->buffer)
  279. {
  280. *(--gdb_con->buf_p) = last_char;
  281. gdb_con->buf_cnt++;
  282. }
  283. else
  284. {
  285. LOG_ERROR("BUG: couldn't put character back");
  286. }
  287. return ERROR_OK;
  288. }
  289. /* The only way we can detect that the socket is closed is the first time
  290. * we write to it, we will fail. Subsequent write operations will
  291. * succeed. Shudder! */
  292. static int gdb_write(struct connection *connection, void *data, int len)
  293. {
  294. struct gdb_connection *gdb_con = connection->priv;
  295. if (gdb_con->closed)
  296. return ERROR_SERVER_REMOTE_CLOSED;
  297. if (connection_write(connection, data, len) == len)
  298. {
  299. return ERROR_OK;
  300. }
  301. gdb_con->closed = 1;
  302. return ERROR_SERVER_REMOTE_CLOSED;
  303. }
  304. static int gdb_put_packet_inner(struct connection *connection,
  305. char *buffer, int len)
  306. {
  307. int i;
  308. unsigned char my_checksum = 0;
  309. #ifdef _DEBUG_GDB_IO_
  310. char *debug_buffer;
  311. #endif
  312. int reply;
  313. int retval;
  314. struct gdb_connection *gdb_con = connection->priv;
  315. for (i = 0; i < len; i++)
  316. my_checksum += buffer[i];
  317. #ifdef _DEBUG_GDB_IO_
  318. /*
  319. * At this point we should have nothing in the input queue from GDB,
  320. * however sometimes '-' is sent even though we've already received
  321. * an ACK (+) for everything we've sent off.
  322. */
  323. int gotdata;
  324. for (;;)
  325. {
  326. retval = check_pending(connection, 0, &gotdata);
  327. if (retval != ERROR_OK)
  328. return retval;
  329. if (!gotdata)
  330. break;
  331. if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
  332. return retval;
  333. if (reply == '$') {
  334. /* fix a problem with some IAR tools */
  335. gdb_putback_char(connection, reply);
  336. LOG_DEBUG("Unexpected start of new packet");
  337. break;
  338. }
  339. LOG_WARNING("Discard unexpected char %c", reply);
  340. }
  341. #endif
  342. while (1)
  343. {
  344. #ifdef _DEBUG_GDB_IO_
  345. debug_buffer = strndup(buffer, len);
  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. char* packet, int packet_size)
  823. {
  824. struct target *target = get_target_from_connection(connection);
  825. char sig_reply[4];
  826. int signal_var;
  827. signal_var = gdb_last_signal(target);
  828. snprintf(sig_reply, 4, "S%2.2x", signal_var);
  829. gdb_put_packet(connection, sig_reply, 3);
  830. return ERROR_OK;
  831. }
  832. static int gdb_reg_pos(struct target *target, int pos, int len)
  833. {
  834. if (target->endianness == TARGET_LITTLE_ENDIAN)
  835. return pos;
  836. else
  837. return len - 1 - pos;
  838. }
  839. /* Convert register to string of bytes. NB! The # of bits in the
  840. * register might be non-divisible by 8(a byte), in which
  841. * case an entire byte is shown.
  842. *
  843. * NB! the format on the wire is the target endianness
  844. *
  845. * The format of reg->value is little endian
  846. *
  847. */
  848. static void gdb_str_to_target(struct target *target,
  849. char *tstr, struct reg *reg)
  850. {
  851. int i;
  852. uint8_t *buf;
  853. int buf_len;
  854. buf = reg->value;
  855. buf_len = DIV_ROUND_UP(reg->size, 8);
  856. for (i = 0; i < buf_len; i++)
  857. {
  858. int j = gdb_reg_pos(target, i, buf_len);
  859. tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
  860. tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
  861. }
  862. }
  863. static int hextoint(int c)
  864. {
  865. if (c>='0'&&c<='9')
  866. {
  867. return c-'0';
  868. }
  869. c = toupper(c);
  870. if (c>='A'&&c<='F')
  871. {
  872. return c-'A'+10;
  873. }
  874. LOG_ERROR("BUG: invalid register value %08x", c);
  875. return 0;
  876. }
  877. /* copy over in register buffer */
  878. static void gdb_target_to_reg(struct target *target,
  879. char *tstr, int str_len, uint8_t *bin)
  880. {
  881. if (str_len % 2)
  882. {
  883. LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
  884. exit(-1);
  885. }
  886. int i;
  887. for (i = 0; i < str_len; i += 2)
  888. {
  889. uint8_t t = hextoint(tstr[i]) << 4;
  890. t |= hextoint(tstr[i + 1]);
  891. int j = gdb_reg_pos(target, i/2, str_len/2);
  892. bin[j] = t;
  893. }
  894. }
  895. static int gdb_get_registers_packet(struct connection *connection,
  896. char* packet, int packet_size)
  897. {
  898. struct target *target = get_target_from_connection(connection);
  899. struct reg **reg_list;
  900. int reg_list_size;
  901. int retval;
  902. int reg_packet_size = 0;
  903. char *reg_packet;
  904. char *reg_packet_p;
  905. int i;
  906. #ifdef _DEBUG_GDB_IO_
  907. LOG_DEBUG("-");
  908. #endif
  909. if ( ( target->rtos != NULL ) &&
  910. ( ERROR_FAIL != rtos_get_gdb_reg_list( connection, &reg_list, &reg_list_size) ) )
  911. {
  912. return ERROR_OK;
  913. }
  914. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  915. {
  916. return gdb_error(connection, retval);
  917. }
  918. for (i = 0; i < reg_list_size; i++)
  919. {
  920. reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
  921. }
  922. assert(reg_packet_size > 0);
  923. reg_packet = malloc(reg_packet_size);
  924. reg_packet_p = reg_packet;
  925. for (i = 0; i < reg_list_size; i++)
  926. {
  927. if (!reg_list[i]->valid)
  928. reg_list[i]->type->get(reg_list[i]);
  929. gdb_str_to_target(target, reg_packet_p, reg_list[i]);
  930. reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
  931. }
  932. #ifdef _DEBUG_GDB_IO_
  933. {
  934. char *reg_packet_p_debug;
  935. reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
  936. LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
  937. free(reg_packet_p_debug);
  938. }
  939. #endif
  940. gdb_put_packet(connection, reg_packet, reg_packet_size);
  941. free(reg_packet);
  942. free(reg_list);
  943. return ERROR_OK;
  944. }
  945. static int gdb_set_registers_packet(struct connection *connection,
  946. char *packet, int packet_size)
  947. {
  948. struct target *target = get_target_from_connection(connection);
  949. int i;
  950. struct reg **reg_list;
  951. int reg_list_size;
  952. int retval;
  953. char *packet_p;
  954. #ifdef _DEBUG_GDB_IO_
  955. LOG_DEBUG("-");
  956. #endif
  957. /* skip command character */
  958. packet++;
  959. packet_size--;
  960. if (packet_size % 2)
  961. {
  962. LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
  963. return ERROR_SERVER_REMOTE_CLOSED;
  964. }
  965. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  966. {
  967. return gdb_error(connection, retval);
  968. }
  969. packet_p = packet;
  970. for (i = 0; i < reg_list_size; i++)
  971. {
  972. uint8_t *bin_buf;
  973. int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
  974. if (packet_p + chars > packet + packet_size)
  975. {
  976. LOG_ERROR("BUG: register packet is too small for registers");
  977. }
  978. bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
  979. gdb_target_to_reg(target, packet_p, chars, bin_buf);
  980. reg_list[i]->type->set(reg_list[i], bin_buf);
  981. /* advance packet pointer */
  982. packet_p += chars;
  983. free(bin_buf);
  984. }
  985. /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
  986. free(reg_list);
  987. gdb_put_packet(connection, "OK", 2);
  988. return ERROR_OK;
  989. }
  990. static int gdb_get_register_packet(struct connection *connection,
  991. char *packet, int packet_size)
  992. {
  993. struct target *target = get_target_from_connection(connection);
  994. char *reg_packet;
  995. int reg_num = strtoul(packet + 1, NULL, 16);
  996. struct reg **reg_list;
  997. int reg_list_size;
  998. int retval;
  999. #ifdef _DEBUG_GDB_IO_
  1000. LOG_DEBUG("-");
  1001. #endif
  1002. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  1003. {
  1004. return gdb_error(connection, retval);
  1005. }
  1006. if (reg_list_size <= reg_num)
  1007. {
  1008. LOG_ERROR("gdb requested a non-existing register");
  1009. exit(-1);
  1010. }
  1011. if (!reg_list[reg_num]->valid)
  1012. reg_list[reg_num]->type->get(reg_list[reg_num]);
  1013. reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
  1014. gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
  1015. gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
  1016. free(reg_list);
  1017. free(reg_packet);
  1018. return ERROR_OK;
  1019. }
  1020. static int gdb_set_register_packet(struct connection *connection,
  1021. char *packet, int packet_size)
  1022. {
  1023. struct target *target = get_target_from_connection(connection);
  1024. char *separator;
  1025. uint8_t *bin_buf;
  1026. int reg_num = strtoul(packet + 1, &separator, 16);
  1027. struct reg **reg_list;
  1028. int reg_list_size;
  1029. int retval;
  1030. LOG_DEBUG("-");
  1031. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  1032. {
  1033. return gdb_error(connection, retval);
  1034. }
  1035. if (reg_list_size < reg_num)
  1036. {
  1037. LOG_ERROR("gdb requested a non-existing register");
  1038. return ERROR_SERVER_REMOTE_CLOSED;
  1039. }
  1040. if (*separator != '=')
  1041. {
  1042. LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
  1043. return ERROR_SERVER_REMOTE_CLOSED;
  1044. }
  1045. /* convert from GDB-string (target-endian) to hex-string (big-endian) */
  1046. bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
  1047. int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
  1048. /* fix!!! add some sanity checks on packet size here */
  1049. gdb_target_to_reg(target, separator + 1, chars, bin_buf);
  1050. reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
  1051. gdb_put_packet(connection, "OK", 2);
  1052. free(bin_buf);
  1053. free(reg_list);
  1054. return ERROR_OK;
  1055. }
  1056. /* No attempt is made to translate the "retval" to
  1057. * GDB speak. This has to be done at the calling
  1058. * site as no mapping really exists.
  1059. */
  1060. static int gdb_error(struct connection *connection, int retval)
  1061. {
  1062. LOG_DEBUG("Reporting %i to GDB as generic error", retval);
  1063. gdb_send_error(connection, EFAULT);
  1064. return ERROR_OK;
  1065. }
  1066. /* We don't have to worry about the default 2 second timeout for GDB packets,
  1067. * because GDB breaks up large memory reads into smaller reads.
  1068. *
  1069. * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
  1070. */
  1071. static int gdb_read_memory_packet(struct connection *connection,
  1072. char *packet, int packet_size)
  1073. {
  1074. struct target *target = get_target_from_connection(connection);
  1075. char *separator;
  1076. uint32_t addr = 0;
  1077. uint32_t len = 0;
  1078. uint8_t *buffer;
  1079. char *hex_buffer;
  1080. int retval = ERROR_OK;
  1081. /* skip command character */
  1082. packet++;
  1083. addr = strtoul(packet, &separator, 16);
  1084. if (*separator != ',')
  1085. {
  1086. LOG_ERROR("incomplete read memory packet received, dropping connection");
  1087. return ERROR_SERVER_REMOTE_CLOSED;
  1088. }
  1089. len = strtoul(separator + 1, NULL, 16);
  1090. buffer = malloc(len);
  1091. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1092. retval = target_read_buffer(target, addr, len, buffer);
  1093. if ((retval != ERROR_OK)&&!gdb_report_data_abort)
  1094. {
  1095. /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
  1096. * At some point this might be fixed in GDB, in which case this code can be removed.
  1097. *
  1098. * OpenOCD developers are acutely aware of this problem, but there is nothing
  1099. * gained by involving the user in this problem that hopefully will get resolved
  1100. * eventually
  1101. *
  1102. * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
  1103. *
  1104. * For now, the default is to fix up things to make current GDB versions work.
  1105. * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
  1106. */
  1107. memset(buffer, 0, len);
  1108. retval = ERROR_OK;
  1109. }
  1110. if (retval == ERROR_OK)
  1111. {
  1112. hex_buffer = malloc(len * 2 + 1);
  1113. uint32_t i;
  1114. for (i = 0; i < len; i++)
  1115. {
  1116. uint8_t t = buffer[i];
  1117. hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
  1118. hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
  1119. }
  1120. gdb_put_packet(connection, hex_buffer, len * 2);
  1121. free(hex_buffer);
  1122. }
  1123. else
  1124. {
  1125. retval = gdb_error(connection, retval);
  1126. }
  1127. free(buffer);
  1128. return retval;
  1129. }
  1130. static int gdb_write_memory_packet(struct connection *connection,
  1131. char *packet, int packet_size)
  1132. {
  1133. struct target *target = get_target_from_connection(connection);
  1134. char *separator;
  1135. uint32_t addr = 0;
  1136. uint32_t len = 0;
  1137. uint8_t *buffer;
  1138. uint32_t i;
  1139. int retval;
  1140. /* skip command character */
  1141. packet++;
  1142. addr = strtoul(packet, &separator, 16);
  1143. if (*separator != ',')
  1144. {
  1145. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1146. return ERROR_SERVER_REMOTE_CLOSED;
  1147. }
  1148. len = strtoul(separator + 1, &separator, 16);
  1149. if (*(separator++) != ':')
  1150. {
  1151. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1152. return ERROR_SERVER_REMOTE_CLOSED;
  1153. }
  1154. buffer = malloc(len);
  1155. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1156. for (i = 0; i < len; i++)
  1157. {
  1158. uint32_t tmp;
  1159. sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
  1160. buffer[i] = tmp;
  1161. }
  1162. retval = target_write_buffer(target, addr, len, buffer);
  1163. if (retval == ERROR_OK)
  1164. {
  1165. gdb_put_packet(connection, "OK", 2);
  1166. }
  1167. else
  1168. {
  1169. retval = gdb_error(connection, retval);
  1170. }
  1171. free(buffer);
  1172. return retval;
  1173. }
  1174. static int gdb_write_memory_binary_packet(struct connection *connection,
  1175. char *packet, int packet_size)
  1176. {
  1177. struct target *target = get_target_from_connection(connection);
  1178. char *separator;
  1179. uint32_t addr = 0;
  1180. uint32_t len = 0;
  1181. int retval = ERROR_OK;
  1182. /* skip command character */
  1183. packet++;
  1184. addr = strtoul(packet, &separator, 16);
  1185. if (*separator != ',')
  1186. {
  1187. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1188. return ERROR_SERVER_REMOTE_CLOSED;
  1189. }
  1190. len = strtoul(separator + 1, &separator, 16);
  1191. if (*(separator++) != ':')
  1192. {
  1193. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1194. return ERROR_SERVER_REMOTE_CLOSED;
  1195. }
  1196. struct gdb_connection *gdb_connection = connection->priv;
  1197. if (gdb_connection->mem_write_error)
  1198. {
  1199. retval = ERROR_FAIL;
  1200. /* now that we have reported the memory write error, we can clear the condition */
  1201. gdb_connection->mem_write_error = false;
  1202. }
  1203. /* By replying the packet *immediately* GDB will send us a new packet
  1204. * while we write the last one to the target.
  1205. */
  1206. if (retval == ERROR_OK)
  1207. {
  1208. gdb_put_packet(connection, "OK", 2);
  1209. }
  1210. else
  1211. {
  1212. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1213. return retval;
  1214. }
  1215. if (len)
  1216. {
  1217. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1218. retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
  1219. if (retval != ERROR_OK)
  1220. {
  1221. gdb_connection->mem_write_error = true;
  1222. }
  1223. }
  1224. return ERROR_OK;
  1225. }
  1226. static int gdb_step_continue_packet(struct connection *connection,
  1227. char *packet, int packet_size)
  1228. {
  1229. struct target *target = get_target_from_connection(connection);
  1230. int current = 0;
  1231. uint32_t address = 0x0;
  1232. int retval = ERROR_OK;
  1233. LOG_DEBUG("-");
  1234. if (packet_size > 1)
  1235. {
  1236. packet[packet_size] = 0;
  1237. address = strtoul(packet + 1, NULL, 16);
  1238. }
  1239. else
  1240. {
  1241. current = 1;
  1242. }
  1243. if (packet[0] == 'c')
  1244. {
  1245. LOG_DEBUG("continue");
  1246. target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
  1247. retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
  1248. }
  1249. else if (packet[0] == 's')
  1250. {
  1251. LOG_DEBUG("step");
  1252. /* step at current or address, don't handle breakpoints */
  1253. retval = target_step(target, current, address, 0);
  1254. }
  1255. return retval;
  1256. }
  1257. static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
  1258. char *packet, int packet_size)
  1259. {
  1260. struct target *target = get_target_from_connection(connection);
  1261. int type;
  1262. enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
  1263. enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
  1264. uint32_t address;
  1265. uint32_t size;
  1266. char *separator;
  1267. int retval;
  1268. LOG_DEBUG("-");
  1269. type = strtoul(packet + 1, &separator, 16);
  1270. if (type == 0) /* memory breakpoint */
  1271. bp_type = BKPT_SOFT;
  1272. else if (type == 1) /* hardware breakpoint */
  1273. bp_type = BKPT_HARD;
  1274. else if (type == 2) /* write watchpoint */
  1275. wp_type = WPT_WRITE;
  1276. else if (type == 3) /* read watchpoint */
  1277. wp_type = WPT_READ;
  1278. else if (type == 4) /* access watchpoint */
  1279. wp_type = WPT_ACCESS;
  1280. else
  1281. {
  1282. LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
  1283. return ERROR_SERVER_REMOTE_CLOSED;
  1284. }
  1285. if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
  1286. {
  1287. bp_type = gdb_breakpoint_override_type;
  1288. }
  1289. if (*separator != ',')
  1290. {
  1291. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1292. return ERROR_SERVER_REMOTE_CLOSED;
  1293. }
  1294. address = strtoul(separator + 1, &separator, 16);
  1295. if (*separator != ',')
  1296. {
  1297. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1298. return ERROR_SERVER_REMOTE_CLOSED;
  1299. }
  1300. size = strtoul(separator + 1, &separator, 16);
  1301. switch (type)
  1302. {
  1303. case 0:
  1304. case 1:
  1305. if (packet[0] == 'Z')
  1306. {
  1307. if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
  1308. {
  1309. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1310. return retval;
  1311. }
  1312. else
  1313. {
  1314. gdb_put_packet(connection, "OK", 2);
  1315. }
  1316. }
  1317. else
  1318. {
  1319. breakpoint_remove(target, address);
  1320. gdb_put_packet(connection, "OK", 2);
  1321. }
  1322. break;
  1323. case 2:
  1324. case 3:
  1325. case 4:
  1326. {
  1327. if (packet[0] == 'Z')
  1328. {
  1329. if ((retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu)) != ERROR_OK)
  1330. {
  1331. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1332. return retval;
  1333. }
  1334. else
  1335. {
  1336. gdb_put_packet(connection, "OK", 2);
  1337. }
  1338. }
  1339. else
  1340. {
  1341. watchpoint_remove(target, address);
  1342. gdb_put_packet(connection, "OK", 2);
  1343. }
  1344. break;
  1345. }
  1346. default:
  1347. break;
  1348. }
  1349. return ERROR_OK;
  1350. }
  1351. /* print out a string and allocate more space as needed,
  1352. * mainly used for XML at this point
  1353. */
  1354. static void xml_printf(int *retval, char **xml, int *pos, int *size,
  1355. const char *fmt, ...)
  1356. {
  1357. if (*retval != ERROR_OK)
  1358. {
  1359. return;
  1360. }
  1361. int first = 1;
  1362. for (;;)
  1363. {
  1364. if ((*xml == NULL) || (!first))
  1365. {
  1366. /* start by 0 to exercise all the code paths.
  1367. * Need minimum 2 bytes to fit 1 char and 0 terminator. */
  1368. *size = *size * 2 + 2;
  1369. char *t = *xml;
  1370. *xml = realloc(*xml, *size);
  1371. if (*xml == NULL)
  1372. {
  1373. if (t)
  1374. free(t);
  1375. *retval = ERROR_SERVER_REMOTE_CLOSED;
  1376. return;
  1377. }
  1378. }
  1379. va_list ap;
  1380. int ret;
  1381. va_start(ap, fmt);
  1382. ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
  1383. va_end(ap);
  1384. if ((ret > 0) && ((ret + 1) < *size - *pos))
  1385. {
  1386. *pos += ret;
  1387. return;
  1388. }
  1389. /* there was just enough or not enough space, allocate more. */
  1390. first = 0;
  1391. }
  1392. }
  1393. static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
  1394. {
  1395. char *separator;
  1396. /* Extract and NUL-terminate the annex. */
  1397. *annex = buf;
  1398. while (*buf && *buf != ':')
  1399. buf++;
  1400. if (*buf == '\0')
  1401. return -1;
  1402. *buf++ = 0;
  1403. /* After the read marker and annex, qXfer looks like a
  1404. * traditional 'm' packet. */
  1405. *ofs = strtoul(buf, &separator, 16);
  1406. if (*separator != ',')
  1407. return -1;
  1408. *len = strtoul(separator + 1, NULL, 16);
  1409. return 0;
  1410. }
  1411. static int compare_bank (const void * a, const void * b)
  1412. {
  1413. struct flash_bank *b1, *b2;
  1414. b1=*((struct flash_bank **)a);
  1415. b2=*((struct flash_bank **)b);
  1416. if (b1->base == b2->base)
  1417. {
  1418. return 0;
  1419. } else if (b1->base > b2->base)
  1420. {
  1421. return 1;
  1422. } else
  1423. {
  1424. return -1;
  1425. }
  1426. }
  1427. static int gdb_memory_map(struct connection *connection,
  1428. char *packet, int packet_size)
  1429. {
  1430. /* We get away with only specifying flash here. Regions that are not
  1431. * specified are treated as if we provided no memory map(if not we
  1432. * could detect the holes and mark them as RAM).
  1433. * Normally we only execute this code once, but no big deal if we
  1434. * have to regenerate it a couple of times.
  1435. */
  1436. struct target *target = get_target_from_connection(connection);
  1437. struct flash_bank *p;
  1438. char *xml = NULL;
  1439. int size = 0;
  1440. int pos = 0;
  1441. int retval = ERROR_OK;
  1442. struct flash_bank **banks;
  1443. int offset;
  1444. int length;
  1445. char *separator;
  1446. uint32_t ram_start = 0;
  1447. int i;
  1448. int target_flash_banks = 0;
  1449. /* skip command character */
  1450. packet += 23;
  1451. offset = strtoul(packet, &separator, 16);
  1452. length = strtoul(separator + 1, &separator, 16);
  1453. xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
  1454. /* Sort banks in ascending order. We need to report non-flash
  1455. * memory as ram (or rather read/write) by default for GDB, since
  1456. * it has no concept of non-cacheable read/write memory (i/o etc).
  1457. *
  1458. * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
  1459. * Current versions of GDB assume unlisted addresses are RAM...
  1460. */
  1461. banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
  1462. for (i = 0; i < flash_get_bank_count(); i++) {
  1463. retval = get_flash_bank_by_num(i, &p);
  1464. if (retval != ERROR_OK)
  1465. {
  1466. free(banks);
  1467. gdb_error(connection, retval);
  1468. return retval;
  1469. }
  1470. if(p->target == target)
  1471. banks[target_flash_banks++] = p;
  1472. }
  1473. qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
  1474. compare_bank);
  1475. for (i = 0; i < target_flash_banks; i++) {
  1476. int j;
  1477. unsigned sector_size = 0;
  1478. uint32_t start;
  1479. p = banks[i];
  1480. start = p->base;
  1481. if (ram_start < p->base)
  1482. xml_printf(&retval, &xml, &pos, &size,
  1483. "<memory type=\"ram\" start=\"0x%x\" "
  1484. "length=\"0x%x\"/>\n",
  1485. ram_start, p->base - ram_start);
  1486. /* Report adjacent groups of same-size sectors. So for
  1487. * example top boot CFI flash will list an initial region
  1488. * with several large sectors (maybe 128KB) and several
  1489. * smaller ones at the end (maybe 32KB). STR7 will have
  1490. * regions with 8KB, 32KB, and 64KB sectors; etc.
  1491. */
  1492. for (j = 0; j < p->num_sectors; j++) {
  1493. unsigned group_len;
  1494. /* Maybe start a new group of sectors. */
  1495. if (sector_size == 0) {
  1496. start = p->base + p->sectors[j].offset;
  1497. xml_printf(&retval, &xml, &pos, &size,
  1498. "<memory type=\"flash\" "
  1499. "start=\"0x%x\" ",
  1500. start);
  1501. sector_size = p->sectors[j].size;
  1502. }
  1503. /* Does this finish a group of sectors?
  1504. * If not, continue an already-started group.
  1505. */
  1506. if (j == p->num_sectors -1)
  1507. group_len = (p->base + p->size) - start;
  1508. else if (p->sectors[j + 1].size != sector_size)
  1509. group_len = p->base + p->sectors[j + 1].offset
  1510. - start;
  1511. else
  1512. continue;
  1513. xml_printf(&retval, &xml, &pos, &size,
  1514. "length=\"0x%x\">\n"
  1515. "<property name=\"blocksize\">"
  1516. "0x%x</property>\n"
  1517. "</memory>\n",
  1518. group_len,
  1519. sector_size);
  1520. sector_size = 0;
  1521. }
  1522. ram_start = p->base + p->size;
  1523. }
  1524. if (ram_start != 0)
  1525. xml_printf(&retval, &xml, &pos, &size,
  1526. "<memory type=\"ram\" start=\"0x%x\" "
  1527. "length=\"0x%x\"/>\n",
  1528. ram_start, 0-ram_start);
  1529. /* ELSE a flash chip could be at the very end of the 32 bit address
  1530. * space, in which case ram_start will be precisely 0
  1531. */
  1532. free(banks);
  1533. banks = NULL;
  1534. xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
  1535. if (retval != ERROR_OK) {
  1536. gdb_error(connection, retval);
  1537. return retval;
  1538. }
  1539. if (offset + length > pos)
  1540. length = pos - offset;
  1541. char *t = malloc(length + 1);
  1542. t[0] = 'l';
  1543. memcpy(t + 1, xml + offset, length);
  1544. gdb_put_packet(connection, t, length + 1);
  1545. free(t);
  1546. free(xml);
  1547. return ERROR_OK;
  1548. }
  1549. static int gdb_query_packet(struct connection *connection,
  1550. char *packet, int packet_size)
  1551. {
  1552. struct command_context *cmd_ctx = connection->cmd_ctx;
  1553. struct gdb_connection *gdb_connection = connection->priv;
  1554. struct target *target = get_target_from_connection(connection);
  1555. if (strstr(packet, "qRcmd,"))
  1556. {
  1557. if (packet_size > 6)
  1558. {
  1559. char *cmd;
  1560. int i;
  1561. cmd = malloc((packet_size - 6)/2 + 1);
  1562. for (i = 0; i < (packet_size - 6)/2; i++)
  1563. {
  1564. uint32_t tmp;
  1565. sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
  1566. cmd[i] = tmp;
  1567. }
  1568. cmd[(packet_size - 6)/2] = 0x0;
  1569. /* We want to print all debug output to GDB connection */
  1570. log_add_callback(gdb_log_callback, connection);
  1571. target_call_timer_callbacks_now();
  1572. /* some commands need to know the GDB connection, make note of current
  1573. * GDB connection. */
  1574. current_gdb_connection = gdb_connection;
  1575. command_run_line(cmd_ctx, cmd);
  1576. current_gdb_connection = NULL;
  1577. target_call_timer_callbacks_now();
  1578. log_remove_callback(gdb_log_callback, connection);
  1579. free(cmd);
  1580. }
  1581. gdb_put_packet(connection, "OK", 2);
  1582. return ERROR_OK;
  1583. }
  1584. else if (strstr(packet, "qCRC:"))
  1585. {
  1586. if (packet_size > 5)
  1587. {
  1588. int retval;
  1589. char gdb_reply[10];
  1590. char *separator;
  1591. uint32_t checksum;
  1592. uint32_t addr = 0;
  1593. uint32_t len = 0;
  1594. /* skip command character */
  1595. packet += 5;
  1596. addr = strtoul(packet, &separator, 16);
  1597. if (*separator != ',')
  1598. {
  1599. LOG_ERROR("incomplete read memory packet received, dropping connection");
  1600. return ERROR_SERVER_REMOTE_CLOSED;
  1601. }
  1602. len = strtoul(separator + 1, NULL, 16);
  1603. retval = target_checksum_memory(target, addr, len, &checksum);
  1604. if (retval == ERROR_OK)
  1605. {
  1606. snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
  1607. gdb_put_packet(connection, gdb_reply, 9);
  1608. }
  1609. else
  1610. {
  1611. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1612. return retval;
  1613. }
  1614. return ERROR_OK;
  1615. }
  1616. }
  1617. else if (strstr(packet, "qSupported"))
  1618. {
  1619. /* we currently support packet size and qXfer:memory-map:read (if enabled)
  1620. * disable qXfer:features:read for the moment */
  1621. int retval = ERROR_OK;
  1622. char *buffer = NULL;
  1623. int pos = 0;
  1624. int size = 0;
  1625. xml_printf(&retval, &buffer, &pos, &size,
  1626. "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
  1627. (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
  1628. if (retval != ERROR_OK)
  1629. {
  1630. gdb_send_error(connection, 01);
  1631. return ERROR_OK;
  1632. }
  1633. gdb_put_packet(connection, buffer, strlen(buffer));
  1634. free(buffer);
  1635. return ERROR_OK;
  1636. }
  1637. else if (strstr(packet, "qXfer:memory-map:read::")
  1638. && (flash_get_bank_count() > 0))
  1639. return gdb_memory_map(connection, packet, packet_size);
  1640. else if (strstr(packet, "qXfer:features:read:"))
  1641. {
  1642. char *xml = NULL;
  1643. int size = 0;
  1644. int pos = 0;
  1645. int retval = ERROR_OK;
  1646. int offset;
  1647. unsigned int length;
  1648. char *annex;
  1649. /* skip command character */
  1650. packet += 20;
  1651. if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
  1652. {
  1653. gdb_send_error(connection, 01);
  1654. return ERROR_OK;
  1655. }
  1656. if (strcmp(annex, "target.xml") != 0)
  1657. {
  1658. gdb_send_error(connection, 01);
  1659. return ERROR_OK;
  1660. }
  1661. xml_printf(&retval, &xml, &pos, &size, \
  1662. "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
  1663. if (retval != ERROR_OK)
  1664. {
  1665. gdb_error(connection, retval);
  1666. return retval;
  1667. }
  1668. gdb_put_packet(connection, xml, strlen(xml));
  1669. free(xml);
  1670. return ERROR_OK;
  1671. }
  1672. else if (strstr(packet, "QStartNoAckMode"))
  1673. {
  1674. gdb_connection->noack_mode = 1;
  1675. gdb_put_packet(connection, "OK", 2);
  1676. return ERROR_OK;
  1677. }
  1678. gdb_put_packet(connection, "", 0);
  1679. return ERROR_OK;
  1680. }
  1681. static int gdb_v_packet(struct connection *connection,
  1682. char *packet, int packet_size)
  1683. {
  1684. struct gdb_connection *gdb_connection = connection->priv;
  1685. struct gdb_service *gdb_service = connection->service->priv;
  1686. int result;
  1687. /* if flash programming disabled - send a empty reply */
  1688. if (gdb_flash_program == 0)
  1689. {
  1690. gdb_put_packet(connection, "", 0);
  1691. return ERROR_OK;
  1692. }
  1693. if (strstr(packet, "vFlashErase:"))
  1694. {
  1695. unsigned long addr;
  1696. unsigned long length;
  1697. char *parse = packet + 12;
  1698. if (*parse == '\0')
  1699. {
  1700. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1701. return ERROR_SERVER_REMOTE_CLOSED;
  1702. }
  1703. addr = strtoul(parse, &parse, 16);
  1704. if (*(parse++) != ',' || *parse == '\0')
  1705. {
  1706. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1707. return ERROR_SERVER_REMOTE_CLOSED;
  1708. }
  1709. length = strtoul(parse, &parse, 16);
  1710. if (*parse != '\0')
  1711. {
  1712. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1713. return ERROR_SERVER_REMOTE_CLOSED;
  1714. }
  1715. /* assume all sectors need erasing - stops any problems
  1716. * when flash_write is called multiple times */
  1717. flash_set_dirty();
  1718. /* perform any target specific operations before the erase */
  1719. target_call_event_callbacks(gdb_service->target,
  1720. TARGET_EVENT_GDB_FLASH_ERASE_START);
  1721. /* vFlashErase:addr,length messages require region start and
  1722. * end to be "block" aligned ... if padding is ever needed,
  1723. * GDB will have become dangerously confused.
  1724. */
  1725. result = flash_erase_address_range(gdb_service->target,
  1726. false, addr, length);
  1727. /* perform any target specific operations after the erase */
  1728. target_call_event_callbacks(gdb_service->target,
  1729. TARGET_EVENT_GDB_FLASH_ERASE_END);
  1730. /* perform erase */
  1731. if (result != ERROR_OK)
  1732. {
  1733. /* GDB doesn't evaluate the actual error number returned,
  1734. * treat a failed erase as an I/O error
  1735. */
  1736. gdb_send_error(connection, EIO);
  1737. LOG_ERROR("flash_erase returned %i", result);
  1738. }
  1739. else
  1740. gdb_put_packet(connection, "OK", 2);
  1741. return ERROR_OK;
  1742. }
  1743. if (strstr(packet, "vFlashWrite:"))
  1744. {
  1745. int retval;
  1746. unsigned long addr;
  1747. unsigned long length;
  1748. char *parse = packet + 12;
  1749. if (*parse == '\0')
  1750. {
  1751. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1752. return ERROR_SERVER_REMOTE_CLOSED;
  1753. }
  1754. addr = strtoul(parse, &parse, 16);
  1755. if (*(parse++) != ':')
  1756. {
  1757. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1758. return ERROR_SERVER_REMOTE_CLOSED;
  1759. }
  1760. length = packet_size - (parse - packet);
  1761. /* create a new image if there isn't already one */
  1762. if (gdb_connection->vflash_image == NULL)
  1763. {
  1764. gdb_connection->vflash_image = malloc(sizeof(struct image));
  1765. image_open(gdb_connection->vflash_image, "", "build");
  1766. }
  1767. /* create new section with content from packet buffer */
  1768. if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
  1769. {
  1770. return retval;
  1771. }
  1772. gdb_put_packet(connection, "OK", 2);
  1773. return ERROR_OK;
  1774. }
  1775. if (!strcmp(packet, "vFlashDone"))
  1776. {
  1777. uint32_t written;
  1778. /* process the flashing buffer. No need to erase as GDB
  1779. * always issues a vFlashErase first. */
  1780. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
  1781. result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
  1782. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
  1783. if (result != ERROR_OK)
  1784. {
  1785. if (result == ERROR_FLASH_DST_OUT_OF_BANK)
  1786. gdb_put_packet(connection, "E.memtype", 9);
  1787. else
  1788. gdb_send_error(connection, EIO);
  1789. }
  1790. else
  1791. {
  1792. LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
  1793. gdb_put_packet(connection, "OK", 2);
  1794. }
  1795. image_close(gdb_connection->vflash_image);
  1796. free(gdb_connection->vflash_image);
  1797. gdb_connection->vflash_image = NULL;
  1798. return ERROR_OK;
  1799. }
  1800. gdb_put_packet(connection, "", 0);
  1801. return ERROR_OK;
  1802. }
  1803. static int gdb_detach(struct connection *connection)
  1804. {
  1805. struct gdb_service *gdb_service = connection->service->priv;
  1806. target_call_event_callbacks(gdb_service->target,
  1807. TARGET_EVENT_GDB_DETACH);
  1808. return gdb_put_packet(connection, "OK", 2);
  1809. }
  1810. static void gdb_log_callback(void *priv, const char *file, unsigned line,
  1811. const char *function, const char *string)
  1812. {
  1813. struct connection *connection = priv;
  1814. struct gdb_connection *gdb_con = connection->priv;
  1815. if (gdb_con->busy)
  1816. {
  1817. /* do not reply this using the O packet */
  1818. return;
  1819. }
  1820. gdb_output_con(connection, string);
  1821. }
  1822. static void gdb_sig_halted(struct connection *connection)
  1823. {
  1824. char sig_reply[4];
  1825. snprintf(sig_reply, 4, "T%2.2x", 2);
  1826. gdb_put_packet(connection, sig_reply, 3);
  1827. }
  1828. static int gdb_input_inner(struct connection *connection)
  1829. {
  1830. /* Do not allocate this on the stack */
  1831. static char gdb_packet_buffer[GDB_BUFFER_SIZE];
  1832. struct gdb_service *gdb_service = connection->service->priv;
  1833. struct target *target = gdb_service->target;
  1834. char *packet = gdb_packet_buffer;
  1835. int packet_size;
  1836. int retval;
  1837. struct gdb_connection *gdb_con = connection->priv;
  1838. static int extended_protocol = 0;
  1839. /* drain input buffer. If one of the packets fail, then an error
  1840. * packet is replied, if applicable.
  1841. *
  1842. * This loop will terminate and the error code is returned.
  1843. *
  1844. * The calling fn will check if this error is something that
  1845. * can be recovered from, or if the connection must be closed.
  1846. *
  1847. * If the error is recoverable, this fn is called again to
  1848. * drain the rest of the buffer.
  1849. */
  1850. do
  1851. {
  1852. packet_size = GDB_BUFFER_SIZE-1;
  1853. retval = gdb_get_packet(connection, packet, &packet_size);
  1854. if (retval != ERROR_OK)
  1855. return retval;
  1856. /* terminate with zero */
  1857. packet[packet_size] = 0;
  1858. if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
  1859. if (packet[0] == 'X') {
  1860. // binary packets spew junk into the debug log stream
  1861. char buf[ 50 ];
  1862. int x;
  1863. for (x = 0 ; (x < 49) && (packet[x] != ':') ; x++) {
  1864. buf[x] = packet[x];
  1865. }
  1866. buf[x] = 0;
  1867. LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
  1868. } else {
  1869. LOG_DEBUG("received packet: '%s'", packet);
  1870. }
  1871. }
  1872. if (packet_size > 0)
  1873. {
  1874. retval = ERROR_OK;
  1875. switch (packet[0])
  1876. {
  1877. case 'T': // Is thread alive?
  1878. gdb_thread_packet(connection, packet, packet_size);
  1879. break;
  1880. case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
  1881. gdb_thread_packet(connection, packet, packet_size);
  1882. break;
  1883. case 'q':
  1884. case 'Q':
  1885. retval = gdb_thread_packet(connection, packet, packet_size);
  1886. if ( retval == GDB_THREAD_PACKET_NOT_CONSUMED )
  1887. {
  1888. retval = gdb_query_packet(connection, packet, packet_size);
  1889. }
  1890. break;
  1891. case 'g':
  1892. retval = gdb_get_registers_packet(connection, packet, packet_size);
  1893. break;
  1894. case 'G':
  1895. retval = gdb_set_registers_packet(connection, packet, packet_size);
  1896. break;
  1897. case 'p':
  1898. retval = gdb_get_register_packet(connection, packet, packet_size);
  1899. break;
  1900. case 'P':
  1901. retval = gdb_set_register_packet(connection, packet, packet_size);
  1902. break;
  1903. case 'm':
  1904. retval = gdb_read_memory_packet(connection, packet, packet_size);
  1905. break;
  1906. case 'M':
  1907. retval = gdb_write_memory_packet(connection, packet, packet_size);
  1908. break;
  1909. case 'z':
  1910. case 'Z':
  1911. retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
  1912. break;
  1913. case '?':
  1914. gdb_last_signal_packet(connection, packet, packet_size);
  1915. break;
  1916. case 'c':
  1917. case 's':
  1918. {
  1919. log_add_callback(gdb_log_callback, connection);
  1920. if (gdb_con->mem_write_error)
  1921. {
  1922. LOG_ERROR("Memory write failure!");
  1923. /* now that we have reported the memory write error, we can clear the condition */
  1924. gdb_con->mem_write_error = false;
  1925. }
  1926. bool nostep = false;
  1927. bool already_running = false;
  1928. if (target->state == TARGET_RUNNING)
  1929. {
  1930. LOG_WARNING("WARNING! The target is already running. "
  1931. "All changes GDB did to registers will be discarded! "
  1932. "Waiting for target to halt.");
  1933. already_running = true;
  1934. } else if (target->state != TARGET_HALTED)
  1935. {
  1936. LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
  1937. nostep = true;
  1938. } else if ((packet[0] == 's') && gdb_con->sync)
  1939. {
  1940. /* Hmm..... when you issue a continue in GDB, then a "stepi" is
  1941. * sent by GDB first to OpenOCD, thus defeating the check to
  1942. * make only the single stepping have the sync feature...
  1943. */
  1944. nostep = true;
  1945. LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
  1946. }
  1947. gdb_con->sync = false;
  1948. if (!already_running && nostep)
  1949. {
  1950. /* Either the target isn't in the halted state, then we can't
  1951. * step/continue. This might be early setup, etc.
  1952. *
  1953. * Or we want to allow GDB to pick up a fresh set of
  1954. * register values without modifying the target state.
  1955. *
  1956. */
  1957. gdb_sig_halted(connection);
  1958. /* stop forwarding log packets! */
  1959. log_remove_callback(gdb_log_callback, connection);
  1960. } else
  1961. {
  1962. /* We're running/stepping, in which case we can
  1963. * forward log output until the target is halted
  1964. */
  1965. gdb_con->frontend_state = TARGET_RUNNING;
  1966. target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
  1967. if (!already_running)
  1968. {
  1969. /* Here we don't want packet processing to stop even if this fails,
  1970. * so we use a local variable instead of retval. */
  1971. retval = gdb_step_continue_packet(connection, packet, packet_size);
  1972. if (retval != ERROR_OK)
  1973. {
  1974. /* we'll never receive a halted condition... issue a false one.. */
  1975. gdb_frontend_halted(target, connection);
  1976. }
  1977. }
  1978. }
  1979. }
  1980. break;
  1981. case 'v':
  1982. retval = gdb_v_packet(connection, packet, packet_size);
  1983. break;
  1984. case 'D':
  1985. retval = gdb_detach(connection);
  1986. extended_protocol = 0;
  1987. break;
  1988. case 'X':
  1989. retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
  1990. if (retval != ERROR_OK)
  1991. return retval;
  1992. break;
  1993. case 'k':
  1994. if (extended_protocol != 0)
  1995. break;
  1996. gdb_put_packet(connection, "OK", 2);
  1997. return ERROR_SERVER_REMOTE_CLOSED;
  1998. case '!':
  1999. /* handle extended remote protocol */
  2000. extended_protocol = 1;
  2001. gdb_put_packet(connection, "OK", 2);
  2002. break;
  2003. case 'R':
  2004. /* handle extended restart packet */
  2005. breakpoint_clear_target(gdb_service->target);
  2006. watchpoint_clear_target(gdb_service->target);
  2007. command_run_linef(connection->cmd_ctx,
  2008. "ocd_gdb_restart %s",
  2009. target_name(target));
  2010. gdb_put_packet(connection, "OK", 2);
  2011. break;
  2012. case 'j':
  2013. /* packet supported only by smp target i.e cortex_a.c*/
  2014. /* handle smp packet replying coreid played to gbd */
  2015. gdb_read_smp_packet(connection, packet, packet_size);
  2016. break;
  2017. case 'J':
  2018. /* packet supported only by smp target i.e cortex_a.c */
  2019. /* handle smp packet setting coreid to be played at next
  2020. * resume to gdb */
  2021. gdb_write_smp_packet(connection, packet, packet_size);
  2022. break;
  2023. default:
  2024. /* ignore unknown packets */
  2025. LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
  2026. gdb_put_packet(connection, NULL, 0);
  2027. break;
  2028. }
  2029. /* if a packet handler returned an error, exit input loop */
  2030. if (retval != ERROR_OK)
  2031. return retval;
  2032. }
  2033. if (gdb_con->ctrl_c)
  2034. {
  2035. if (target->state == TARGET_RUNNING)
  2036. {
  2037. retval = target_halt(target);
  2038. if (retval != ERROR_OK)
  2039. {
  2040. target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
  2041. }
  2042. gdb_con->ctrl_c = 0;
  2043. } else
  2044. {
  2045. LOG_INFO("The target is not running when halt was requested, stopping GDB.");
  2046. target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
  2047. }
  2048. }
  2049. } while (gdb_con->buf_cnt > 0);
  2050. return ERROR_OK;
  2051. }
  2052. static int gdb_input(struct connection *connection)
  2053. {
  2054. int retval = gdb_input_inner(connection);
  2055. struct gdb_connection *gdb_con = connection->priv;
  2056. if (retval == ERROR_SERVER_REMOTE_CLOSED)
  2057. return retval;
  2058. /* logging does not propagate the error, yet can set the gdb_con->closed flag */
  2059. if (gdb_con->closed)
  2060. return ERROR_SERVER_REMOTE_CLOSED;
  2061. /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
  2062. return ERROR_OK;
  2063. }
  2064. static int gdb_target_start(struct target *target, const char *port)
  2065. {
  2066. struct gdb_service *gdb_service;
  2067. int ret;
  2068. gdb_service = malloc(sizeof(struct gdb_service));
  2069. if (NULL == gdb_service)
  2070. return -ENOMEM;
  2071. gdb_service->target = target;
  2072. gdb_service->core[0] = -1;
  2073. gdb_service->core[1] = -1;
  2074. target->gdb_service = gdb_service;
  2075. ret = add_service("gdb",
  2076. port, 1, &gdb_new_connection, &gdb_input,
  2077. &gdb_connection_closed, gdb_service);
  2078. /* initialialize all targets gdb service with the same pointer */
  2079. {
  2080. struct target_list *head;
  2081. struct target *curr;
  2082. head = target->head;
  2083. while(head != (struct target_list*)NULL)
  2084. {
  2085. curr = head->target;
  2086. if (curr != target) curr->gdb_service = gdb_service;
  2087. head = head->next;
  2088. }
  2089. }
  2090. return ret;
  2091. }
  2092. static int gdb_target_add_one(struct target *target)
  2093. {
  2094. /* one gdb instance per smp list */
  2095. if ((target->smp) && (target->gdb_service)) return ERROR_OK;
  2096. int retval = gdb_target_start(target, gdb_port_next);
  2097. if (retval == ERROR_OK)
  2098. {
  2099. long portnumber;
  2100. /* If we can parse the port number
  2101. * then we increment the port number for the next target.
  2102. */
  2103. char *end;
  2104. portnumber = strtol(gdb_port_next, &end, 0);
  2105. if (!*end)
  2106. {
  2107. if (parse_long(gdb_port_next, &portnumber) == ERROR_OK)
  2108. {
  2109. free((void *)gdb_port_next);
  2110. gdb_port_next = alloc_printf("%d", portnumber+1);
  2111. }
  2112. }
  2113. }
  2114. return retval;
  2115. }
  2116. int gdb_target_add_all(struct target *target)
  2117. {
  2118. if (NULL == target)
  2119. {
  2120. LOG_WARNING("gdb services need one or more targets defined");
  2121. return ERROR_OK;
  2122. }
  2123. while (NULL != target)
  2124. {
  2125. int retval = gdb_target_add_one(target);
  2126. if (ERROR_OK != retval)
  2127. return retval;
  2128. target = target->next;
  2129. }
  2130. return ERROR_OK;
  2131. }
  2132. COMMAND_HANDLER(handle_gdb_sync_command)
  2133. {
  2134. if (CMD_ARGC != 0)
  2135. {
  2136. return ERROR_COMMAND_SYNTAX_ERROR;
  2137. }
  2138. if (current_gdb_connection == NULL)
  2139. {
  2140. command_print(CMD_CTX,
  2141. "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
  2142. return ERROR_FAIL;
  2143. }
  2144. current_gdb_connection->sync = true;
  2145. return ERROR_OK;
  2146. }
  2147. /* daemon configuration command gdb_port */
  2148. COMMAND_HANDLER(handle_gdb_port_command)
  2149. {
  2150. int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
  2151. if (ERROR_OK == retval) {
  2152. free((void*)gdb_port_next);
  2153. gdb_port_next = strdup(gdb_port);
  2154. }
  2155. return retval;
  2156. }
  2157. COMMAND_HANDLER(handle_gdb_memory_map_command)
  2158. {
  2159. if (CMD_ARGC != 1)
  2160. return ERROR_COMMAND_SYNTAX_ERROR;
  2161. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
  2162. return ERROR_OK;
  2163. }
  2164. COMMAND_HANDLER(handle_gdb_flash_program_command)
  2165. {
  2166. if (CMD_ARGC != 1)
  2167. return ERROR_COMMAND_SYNTAX_ERROR;
  2168. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
  2169. return ERROR_OK;
  2170. }
  2171. COMMAND_HANDLER(handle_gdb_report_data_abort_command)
  2172. {
  2173. if (CMD_ARGC != 1)
  2174. return ERROR_COMMAND_SYNTAX_ERROR;
  2175. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
  2176. return ERROR_OK;
  2177. }
  2178. /* gdb_breakpoint_override */
  2179. COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
  2180. {
  2181. if (CMD_ARGC == 0)
  2182. {
  2183. } else if (CMD_ARGC == 1)
  2184. {
  2185. gdb_breakpoint_override = 1;
  2186. if (strcmp(CMD_ARGV[0], "hard") == 0)
  2187. {
  2188. gdb_breakpoint_override_type = BKPT_HARD;
  2189. } else if (strcmp(CMD_ARGV[0], "soft") == 0)
  2190. {
  2191. gdb_breakpoint_override_type = BKPT_SOFT;
  2192. } else if (strcmp(CMD_ARGV[0], "disable") == 0)
  2193. {
  2194. gdb_breakpoint_override = 0;
  2195. }
  2196. } else
  2197. {
  2198. return ERROR_COMMAND_SYNTAX_ERROR;
  2199. }
  2200. if (gdb_breakpoint_override)
  2201. {
  2202. LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
  2203. } else
  2204. {
  2205. LOG_USER("breakpoint type is not overridden");
  2206. }
  2207. return ERROR_OK;
  2208. }
  2209. static const struct command_registration gdb_command_handlers[] = {
  2210. {
  2211. .name = "gdb_sync",
  2212. .handler = handle_gdb_sync_command,
  2213. .mode = COMMAND_ANY,
  2214. .help = "next stepi will return immediately allowing "
  2215. "GDB to fetch register state without affecting "
  2216. "target state",
  2217. },
  2218. {
  2219. .name = "gdb_port",
  2220. .handler = handle_gdb_port_command,
  2221. .mode = COMMAND_ANY,
  2222. .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
  2223. "server listens for the next port number after the "
  2224. "base port number specified. "
  2225. "No arguments reports GDB port. \"pipe\" means listen to stdin "
  2226. "output to stdout, an integer is base port number, \"disable\" disables "
  2227. "port. Any other string is are interpreted as named pipe to listen to. "
  2228. "Output pipe is the same name as input pipe, but with 'o' appended.",
  2229. .usage = "[port_num]",
  2230. },
  2231. {
  2232. .name = "gdb_memory_map",
  2233. .handler = handle_gdb_memory_map_command,
  2234. .mode = COMMAND_CONFIG,
  2235. .help = "enable or disable memory map",
  2236. .usage = "('enable'|'disable')"
  2237. },
  2238. {
  2239. .name = "gdb_flash_program",
  2240. .handler = handle_gdb_flash_program_command,
  2241. .mode = COMMAND_CONFIG,
  2242. .help = "enable or disable flash program",
  2243. .usage = "('enable'|'disable')"
  2244. },
  2245. {
  2246. .name = "gdb_report_data_abort",
  2247. .handler = handle_gdb_report_data_abort_command,
  2248. .mode = COMMAND_CONFIG,
  2249. .help = "enable or disable reporting data aborts",
  2250. .usage = "('enable'|'disable')"
  2251. },
  2252. {
  2253. .name = "gdb_breakpoint_override",
  2254. .handler = handle_gdb_breakpoint_override_command,
  2255. .mode = COMMAND_ANY,
  2256. .help = "Display or specify type of breakpoint "
  2257. "to be used by gdb 'break' commands.",
  2258. .usage = "('hard'|'soft'|'disable')"
  2259. },
  2260. COMMAND_REGISTRATION_DONE
  2261. };
  2262. int gdb_register_commands(struct command_context *cmd_ctx)
  2263. {
  2264. gdb_port = strdup("3333");
  2265. gdb_port_next = strdup("3333");
  2266. return register_commands(cmd_ctx, NULL, gdb_command_handlers);
  2267. }