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.
 
 
 
 
 
 

3319 lines
91 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. * Copyright (C) 2013 Andes Technology *
  18. * Hsiangkai Wang <hkwang@andestech.com> *
  19. * *
  20. * Copyright (C) 2013 Franck Jullien *
  21. * elec4fun@gmail.com *
  22. * *
  23. * This program is free software; you can redistribute it and/or modify *
  24. * it under the terms of the GNU General Public License as published by *
  25. * the Free Software Foundation; either version 2 of the License, or *
  26. * (at your option) any later version. *
  27. * *
  28. * This program is distributed in the hope that it will be useful, *
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  31. * GNU General Public License for more details. *
  32. * *
  33. * You should have received a copy of the GNU General Public License *
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  35. ***************************************************************************/
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include <target/breakpoints.h>
  40. #include <target/target_request.h>
  41. #include <target/register.h>
  42. #include "server.h"
  43. #include <flash/nor/core.h>
  44. #include "gdb_server.h"
  45. #include <target/image.h>
  46. #include <jtag/jtag.h>
  47. #include "rtos/rtos.h"
  48. #include "target/smp.h"
  49. /**
  50. * @file
  51. * GDB server implementation.
  52. *
  53. * This implements the GDB Remote Serial Protocol, over TCP connections,
  54. * giving GDB access to the JTAG or other hardware debugging facilities
  55. * found in most modern embedded processors.
  56. */
  57. struct target_desc_format {
  58. char *tdesc;
  59. uint32_t tdesc_length;
  60. };
  61. /* private connection data for GDB */
  62. struct gdb_connection {
  63. char buffer[GDB_BUFFER_SIZE];
  64. char *buf_p;
  65. int buf_cnt;
  66. int ctrl_c;
  67. enum target_state frontend_state;
  68. struct image *vflash_image;
  69. int closed;
  70. int busy;
  71. int noack_mode;
  72. /* set flag to true if you want the next stepi to return immediately.
  73. * allowing GDB to pick up a fresh set of register values from the target
  74. * without modifying the target state. */
  75. bool sync;
  76. /* We delay reporting memory write errors until next step/continue or memory
  77. * write. This improves performance of gdb load significantly as the GDB packet
  78. * can be replied immediately and a new GDB packet will be ready without delay
  79. * (ca. 10% or so...). */
  80. bool mem_write_error;
  81. /* with extended-remote it seems we need to better emulate attach/detach.
  82. * what this means is we reply with a W stop reply after a kill packet,
  83. * normally we reply with a S reply via gdb_last_signal_packet.
  84. * as a side note this behaviour only effects gdb > 6.8 */
  85. bool attached;
  86. /* temporarily used for target description support */
  87. struct target_desc_format target_desc;
  88. /* temporarily used for thread list support */
  89. char *thread_list;
  90. };
  91. #if 0
  92. #define _DEBUG_GDB_IO_
  93. #endif
  94. static struct gdb_connection *current_gdb_connection;
  95. static int gdb_breakpoint_override;
  96. static enum breakpoint_type gdb_breakpoint_override_type;
  97. static int gdb_error(struct connection *connection, int retval);
  98. static char *gdb_port;
  99. static char *gdb_port_next;
  100. static void gdb_log_callback(void *priv, const char *file, unsigned line,
  101. const char *function, const char *string);
  102. /* number of gdb connections, mainly to suppress gdb related debugging spam
  103. * in helper/log.c when no gdb connections are actually active */
  104. int gdb_actual_connections;
  105. /* set if we are sending a memory map to gdb
  106. * via qXfer:memory-map:read packet */
  107. /* enabled by default*/
  108. static int gdb_use_memory_map = 1;
  109. /* enabled by default*/
  110. static int gdb_flash_program = 1;
  111. /* if set, data aborts cause an error to be reported in memory read packets
  112. * see the code in gdb_read_memory_packet() for further explanations.
  113. * Disabled by default.
  114. */
  115. static int gdb_report_data_abort;
  116. /* set if we are sending target descriptions to gdb
  117. * via qXfer:features:read packet */
  118. /* enabled by default */
  119. static int gdb_use_target_description = 1;
  120. /* current processing free-run type, used by file-I/O */
  121. static char gdb_running_type;
  122. static int gdb_last_signal(struct target *target)
  123. {
  124. switch (target->debug_reason) {
  125. case DBG_REASON_DBGRQ:
  126. return 0x2; /* SIGINT */
  127. case DBG_REASON_BREAKPOINT:
  128. case DBG_REASON_WATCHPOINT:
  129. case DBG_REASON_WPTANDBKPT:
  130. return 0x05; /* SIGTRAP */
  131. case DBG_REASON_SINGLESTEP:
  132. return 0x05; /* SIGTRAP */
  133. case DBG_REASON_NOTHALTED:
  134. return 0x0; /* no signal... shouldn't happen */
  135. default:
  136. LOG_USER("undefined debug reason %d - target needs reset",
  137. target->debug_reason);
  138. return 0x0;
  139. }
  140. }
  141. static int check_pending(struct connection *connection,
  142. int timeout_s, int *got_data)
  143. {
  144. /* a non-blocking socket will block if there is 0 bytes available on the socket,
  145. * but return with as many bytes as are available immediately
  146. */
  147. struct timeval tv;
  148. fd_set read_fds;
  149. struct gdb_connection *gdb_con = connection->priv;
  150. int t;
  151. if (got_data == NULL)
  152. got_data = &t;
  153. *got_data = 0;
  154. if (gdb_con->buf_cnt > 0) {
  155. *got_data = 1;
  156. return ERROR_OK;
  157. }
  158. FD_ZERO(&read_fds);
  159. FD_SET(connection->fd, &read_fds);
  160. tv.tv_sec = timeout_s;
  161. tv.tv_usec = 0;
  162. if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
  163. /* This can typically be because a "monitor" command took too long
  164. * before printing any progress messages
  165. */
  166. if (timeout_s > 0)
  167. return ERROR_GDB_TIMEOUT;
  168. else
  169. return ERROR_OK;
  170. }
  171. *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
  172. return ERROR_OK;
  173. }
  174. static int gdb_get_char_inner(struct connection *connection, int *next_char)
  175. {
  176. struct gdb_connection *gdb_con = connection->priv;
  177. int retval = ERROR_OK;
  178. #ifdef _DEBUG_GDB_IO_
  179. char *debug_buffer;
  180. #endif
  181. for (;; ) {
  182. if (connection->service->type != CONNECTION_TCP)
  183. gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
  184. else {
  185. retval = check_pending(connection, 1, NULL);
  186. if (retval != ERROR_OK)
  187. return retval;
  188. gdb_con->buf_cnt = read_socket(connection->fd,
  189. gdb_con->buffer,
  190. GDB_BUFFER_SIZE);
  191. }
  192. if (gdb_con->buf_cnt > 0)
  193. break;
  194. if (gdb_con->buf_cnt == 0) {
  195. gdb_con->closed = 1;
  196. return ERROR_SERVER_REMOTE_CLOSED;
  197. }
  198. #ifdef _WIN32
  199. errno = WSAGetLastError();
  200. switch (errno) {
  201. case WSAEWOULDBLOCK:
  202. usleep(1000);
  203. break;
  204. case WSAECONNABORTED:
  205. gdb_con->closed = 1;
  206. return ERROR_SERVER_REMOTE_CLOSED;
  207. case WSAECONNRESET:
  208. gdb_con->closed = 1;
  209. return ERROR_SERVER_REMOTE_CLOSED;
  210. default:
  211. LOG_ERROR("read: %d", errno);
  212. exit(-1);
  213. }
  214. #else
  215. switch (errno) {
  216. case EAGAIN:
  217. usleep(1000);
  218. break;
  219. case ECONNABORTED:
  220. gdb_con->closed = 1;
  221. return ERROR_SERVER_REMOTE_CLOSED;
  222. case ECONNRESET:
  223. gdb_con->closed = 1;
  224. return ERROR_SERVER_REMOTE_CLOSED;
  225. default:
  226. LOG_ERROR("read: %s", strerror(errno));
  227. gdb_con->closed = 1;
  228. return ERROR_SERVER_REMOTE_CLOSED;
  229. }
  230. #endif
  231. }
  232. #ifdef _DEBUG_GDB_IO_
  233. debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
  234. LOG_DEBUG("received '%s'", debug_buffer);
  235. free(debug_buffer);
  236. #endif
  237. gdb_con->buf_p = gdb_con->buffer;
  238. gdb_con->buf_cnt--;
  239. *next_char = *(gdb_con->buf_p++);
  240. if (gdb_con->buf_cnt > 0)
  241. connection->input_pending = 1;
  242. else
  243. connection->input_pending = 0;
  244. #ifdef _DEBUG_GDB_IO_
  245. LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  246. #endif
  247. return retval;
  248. }
  249. /**
  250. * The cool thing about this fn is that it allows buf_p and buf_cnt to be
  251. * held in registers in the inner loop.
  252. *
  253. * For small caches and embedded systems this is important!
  254. */
  255. static inline int gdb_get_char_fast(struct connection *connection,
  256. int *next_char, char **buf_p, int *buf_cnt)
  257. {
  258. int retval = ERROR_OK;
  259. if ((*buf_cnt)-- > 0) {
  260. *next_char = **buf_p;
  261. (*buf_p)++;
  262. if (*buf_cnt > 0)
  263. connection->input_pending = 1;
  264. else
  265. connection->input_pending = 0;
  266. #ifdef _DEBUG_GDB_IO_
  267. LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  268. #endif
  269. return ERROR_OK;
  270. }
  271. struct gdb_connection *gdb_con = connection->priv;
  272. gdb_con->buf_p = *buf_p;
  273. gdb_con->buf_cnt = *buf_cnt;
  274. retval = gdb_get_char_inner(connection, next_char);
  275. *buf_p = gdb_con->buf_p;
  276. *buf_cnt = gdb_con->buf_cnt;
  277. return retval;
  278. }
  279. static int gdb_get_char(struct connection *connection, int *next_char)
  280. {
  281. struct gdb_connection *gdb_con = connection->priv;
  282. return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
  283. }
  284. static int gdb_putback_char(struct connection *connection, int last_char)
  285. {
  286. struct gdb_connection *gdb_con = connection->priv;
  287. if (gdb_con->buf_p > gdb_con->buffer) {
  288. *(--gdb_con->buf_p) = last_char;
  289. gdb_con->buf_cnt++;
  290. } else
  291. LOG_ERROR("BUG: couldn't put character back");
  292. return ERROR_OK;
  293. }
  294. /* The only way we can detect that the socket is closed is the first time
  295. * we write to it, we will fail. Subsequent write operations will
  296. * succeed. Shudder! */
  297. static int gdb_write(struct connection *connection, void *data, int len)
  298. {
  299. struct gdb_connection *gdb_con = connection->priv;
  300. if (gdb_con->closed)
  301. return ERROR_SERVER_REMOTE_CLOSED;
  302. if (connection_write(connection, data, len) == len)
  303. return ERROR_OK;
  304. gdb_con->closed = 1;
  305. return ERROR_SERVER_REMOTE_CLOSED;
  306. }
  307. static int gdb_put_packet_inner(struct connection *connection,
  308. char *buffer, int len)
  309. {
  310. int i;
  311. unsigned char my_checksum = 0;
  312. #ifdef _DEBUG_GDB_IO_
  313. char *debug_buffer;
  314. #endif
  315. int reply;
  316. int retval;
  317. struct gdb_connection *gdb_con = connection->priv;
  318. for (i = 0; i < len; i++)
  319. my_checksum += buffer[i];
  320. #ifdef _DEBUG_GDB_IO_
  321. /*
  322. * At this point we should have nothing in the input queue from GDB,
  323. * however sometimes '-' is sent even though we've already received
  324. * an ACK (+) for everything we've sent off.
  325. */
  326. int gotdata;
  327. for (;; ) {
  328. retval = check_pending(connection, 0, &gotdata);
  329. if (retval != ERROR_OK)
  330. return retval;
  331. if (!gotdata)
  332. break;
  333. retval = gdb_get_char(connection, &reply);
  334. if (retval != ERROR_OK)
  335. return retval;
  336. if (reply == '$') {
  337. /* fix a problem with some IAR tools */
  338. gdb_putback_char(connection, reply);
  339. LOG_DEBUG("Unexpected start of new packet");
  340. break;
  341. }
  342. LOG_WARNING("Discard unexpected char %c", reply);
  343. }
  344. #endif
  345. while (1) {
  346. #ifdef _DEBUG_GDB_IO_
  347. debug_buffer = strndup(buffer, len);
  348. LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
  349. free(debug_buffer);
  350. #endif
  351. char local_buffer[1024];
  352. local_buffer[0] = '$';
  353. if ((size_t)len + 4 <= sizeof(local_buffer)) {
  354. /* performance gain on smaller packets by only a single call to gdb_write() */
  355. memcpy(local_buffer + 1, buffer, len++);
  356. len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
  357. retval = gdb_write(connection, local_buffer, len);
  358. if (retval != ERROR_OK)
  359. return retval;
  360. } else {
  361. /* larger packets are transmitted directly from caller supplied buffer
  362. * by several calls to gdb_write() to avoid dynamic allocation */
  363. snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
  364. retval = gdb_write(connection, local_buffer, 1);
  365. if (retval != ERROR_OK)
  366. return retval;
  367. retval = gdb_write(connection, buffer, len);
  368. if (retval != ERROR_OK)
  369. return retval;
  370. retval = gdb_write(connection, local_buffer + 1, 3);
  371. if (retval != ERROR_OK)
  372. return retval;
  373. }
  374. if (gdb_con->noack_mode)
  375. break;
  376. retval = gdb_get_char(connection, &reply);
  377. if (retval != ERROR_OK)
  378. return retval;
  379. if (reply == '+')
  380. break;
  381. else if (reply == '-') {
  382. /* Stop sending output packets for now */
  383. log_remove_callback(gdb_log_callback, connection);
  384. LOG_WARNING("negative reply, retrying");
  385. } else if (reply == 0x3) {
  386. gdb_con->ctrl_c = 1;
  387. retval = gdb_get_char(connection, &reply);
  388. if (retval != ERROR_OK)
  389. return retval;
  390. if (reply == '+')
  391. break;
  392. else if (reply == '-') {
  393. /* Stop sending output packets for now */
  394. log_remove_callback(gdb_log_callback, connection);
  395. LOG_WARNING("negative reply, retrying");
  396. } else if (reply == '$') {
  397. LOG_ERROR("GDB missing ack(1) - assumed good");
  398. gdb_putback_char(connection, reply);
  399. return ERROR_OK;
  400. } else {
  401. LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
  402. gdb_con->closed = 1;
  403. return ERROR_SERVER_REMOTE_CLOSED;
  404. }
  405. } else if (reply == '$') {
  406. LOG_ERROR("GDB missing ack(2) - assumed good");
  407. gdb_putback_char(connection, reply);
  408. return ERROR_OK;
  409. } else {
  410. LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
  411. reply);
  412. gdb_con->closed = 1;
  413. return ERROR_SERVER_REMOTE_CLOSED;
  414. }
  415. }
  416. if (gdb_con->closed)
  417. return ERROR_SERVER_REMOTE_CLOSED;
  418. return ERROR_OK;
  419. }
  420. int gdb_put_packet(struct connection *connection, char *buffer, int len)
  421. {
  422. struct gdb_connection *gdb_con = connection->priv;
  423. gdb_con->busy = 1;
  424. int retval = gdb_put_packet_inner(connection, buffer, len);
  425. gdb_con->busy = 0;
  426. /* we sent some data, reset timer for keep alive messages */
  427. kept_alive();
  428. return retval;
  429. }
  430. static inline int fetch_packet(struct connection *connection,
  431. int *checksum_ok, int noack, int *len, char *buffer)
  432. {
  433. unsigned char my_checksum = 0;
  434. char checksum[3];
  435. int character;
  436. int retval = ERROR_OK;
  437. struct gdb_connection *gdb_con = connection->priv;
  438. my_checksum = 0;
  439. int count = 0;
  440. count = 0;
  441. /* move this over into local variables to use registers and give the
  442. * more freedom to optimize */
  443. char *buf_p = gdb_con->buf_p;
  444. int buf_cnt = gdb_con->buf_cnt;
  445. for (;; ) {
  446. /* The common case is that we have an entire packet with no escape chars.
  447. * We need to leave at least 2 bytes in the buffer to have
  448. * gdb_get_char() update various bits and bobs correctly.
  449. */
  450. if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
  451. /* The compiler will struggle a bit with constant propagation and
  452. * aliasing, so we help it by showing that these values do not
  453. * change inside the loop
  454. */
  455. int i;
  456. char *buf = buf_p;
  457. int run = buf_cnt - 2;
  458. i = 0;
  459. int done = 0;
  460. while (i < run) {
  461. character = *buf++;
  462. i++;
  463. if (character == '#') {
  464. /* Danger! character can be '#' when esc is
  465. * used so we need an explicit boolean for done here. */
  466. done = 1;
  467. break;
  468. }
  469. if (character == '}') {
  470. /* data transmitted in binary mode (X packet)
  471. * uses 0x7d as escape character */
  472. my_checksum += character & 0xff;
  473. character = *buf++;
  474. i++;
  475. my_checksum += character & 0xff;
  476. buffer[count++] = (character ^ 0x20) & 0xff;
  477. } else {
  478. my_checksum += character & 0xff;
  479. buffer[count++] = character & 0xff;
  480. }
  481. }
  482. buf_p += i;
  483. buf_cnt -= i;
  484. if (done)
  485. break;
  486. }
  487. if (count > *len) {
  488. LOG_ERROR("packet buffer too small");
  489. retval = ERROR_GDB_BUFFER_TOO_SMALL;
  490. break;
  491. }
  492. retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
  493. if (retval != ERROR_OK)
  494. break;
  495. if (character == '#')
  496. break;
  497. if (character == '}') {
  498. /* data transmitted in binary mode (X packet)
  499. * uses 0x7d as escape character */
  500. my_checksum += character & 0xff;
  501. retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
  502. if (retval != ERROR_OK)
  503. break;
  504. my_checksum += character & 0xff;
  505. buffer[count++] = (character ^ 0x20) & 0xff;
  506. } else {
  507. my_checksum += character & 0xff;
  508. buffer[count++] = character & 0xff;
  509. }
  510. }
  511. gdb_con->buf_p = buf_p;
  512. gdb_con->buf_cnt = buf_cnt;
  513. if (retval != ERROR_OK)
  514. return retval;
  515. *len = count;
  516. retval = gdb_get_char(connection, &character);
  517. if (retval != ERROR_OK)
  518. return retval;
  519. checksum[0] = character;
  520. retval = gdb_get_char(connection, &character);
  521. if (retval != ERROR_OK)
  522. return retval;
  523. checksum[1] = character;
  524. checksum[2] = 0;
  525. if (!noack)
  526. *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
  527. return ERROR_OK;
  528. }
  529. static int gdb_get_packet_inner(struct connection *connection,
  530. char *buffer, int *len)
  531. {
  532. int character;
  533. int retval;
  534. struct gdb_connection *gdb_con = connection->priv;
  535. while (1) {
  536. do {
  537. retval = gdb_get_char(connection, &character);
  538. if (retval != ERROR_OK)
  539. return retval;
  540. #ifdef _DEBUG_GDB_IO_
  541. LOG_DEBUG("character: '%c'", character);
  542. #endif
  543. switch (character) {
  544. case '$':
  545. break;
  546. case '+':
  547. /* According to the GDB documentation
  548. * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
  549. * "gdb sends a final `+` acknowledgment of the stub's `OK`
  550. * response, which can be safely ignored by the stub."
  551. * However OpenOCD server already is in noack mode at this
  552. * point and instead of ignoring this it was emitting a
  553. * warning. This code makes server ignore the first ACK
  554. * that will be received after going into noack mode,
  555. * warning only about subsequent ACK's. */
  556. if (gdb_con->noack_mode > 1) {
  557. LOG_WARNING("acknowledgment received, but no packet pending");
  558. } else if (gdb_con->noack_mode) {
  559. LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
  560. gdb_con->noack_mode = 2;
  561. }
  562. break;
  563. case '-':
  564. LOG_WARNING("negative acknowledgment, but no packet pending");
  565. break;
  566. case 0x3:
  567. gdb_con->ctrl_c = 1;
  568. *len = 0;
  569. return ERROR_OK;
  570. default:
  571. LOG_WARNING("ignoring character 0x%x", character);
  572. break;
  573. }
  574. } while (character != '$');
  575. int checksum_ok = 0;
  576. /* explicit code expansion here to get faster inlined code in -O3 by not
  577. * calculating checksum */
  578. if (gdb_con->noack_mode) {
  579. retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
  580. if (retval != ERROR_OK)
  581. return retval;
  582. } else {
  583. retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
  584. if (retval != ERROR_OK)
  585. return retval;
  586. }
  587. if (gdb_con->noack_mode) {
  588. /* checksum is not checked in noack mode */
  589. break;
  590. }
  591. if (checksum_ok) {
  592. retval = gdb_write(connection, "+", 1);
  593. if (retval != ERROR_OK)
  594. return retval;
  595. break;
  596. }
  597. }
  598. if (gdb_con->closed)
  599. return ERROR_SERVER_REMOTE_CLOSED;
  600. return ERROR_OK;
  601. }
  602. static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
  603. {
  604. struct gdb_connection *gdb_con = connection->priv;
  605. gdb_con->busy = 1;
  606. int retval = gdb_get_packet_inner(connection, buffer, len);
  607. gdb_con->busy = 0;
  608. return retval;
  609. }
  610. static int gdb_output_con(struct connection *connection, const char *line)
  611. {
  612. char *hex_buffer;
  613. int bin_size;
  614. bin_size = strlen(line);
  615. hex_buffer = malloc(bin_size * 2 + 2);
  616. if (hex_buffer == NULL)
  617. return ERROR_GDB_BUFFER_TOO_SMALL;
  618. hex_buffer[0] = 'O';
  619. size_t pkt_len = hexify(hex_buffer + 1, (const uint8_t *)line, bin_size,
  620. bin_size * 2 + 1);
  621. int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
  622. free(hex_buffer);
  623. return retval;
  624. }
  625. static int gdb_output(struct command_context *context, const char *line)
  626. {
  627. /* this will be dumped to the log and also sent as an O packet if possible */
  628. LOG_USER_N("%s", line);
  629. return ERROR_OK;
  630. }
  631. static void gdb_signal_reply(struct target *target, struct connection *connection)
  632. {
  633. struct gdb_connection *gdb_connection = connection->priv;
  634. char sig_reply[45];
  635. char stop_reason[20];
  636. char current_thread[25];
  637. int sig_reply_len;
  638. int signal_var;
  639. rtos_update_threads(target);
  640. if (target->debug_reason == DBG_REASON_EXIT) {
  641. sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
  642. } else {
  643. if (gdb_connection->ctrl_c) {
  644. signal_var = 0x2;
  645. gdb_connection->ctrl_c = 0;
  646. } else
  647. signal_var = gdb_last_signal(target);
  648. stop_reason[0] = '\0';
  649. if (target->debug_reason == DBG_REASON_WATCHPOINT) {
  650. enum watchpoint_rw hit_wp_type;
  651. target_addr_t hit_wp_address;
  652. if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
  653. switch (hit_wp_type) {
  654. case WPT_WRITE:
  655. snprintf(stop_reason, sizeof(stop_reason),
  656. "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
  657. break;
  658. case WPT_READ:
  659. snprintf(stop_reason, sizeof(stop_reason),
  660. "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
  661. break;
  662. case WPT_ACCESS:
  663. snprintf(stop_reason, sizeof(stop_reason),
  664. "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
  665. break;
  666. default:
  667. break;
  668. }
  669. }
  670. }
  671. current_thread[0] = '\0';
  672. if (target->rtos != NULL) {
  673. snprintf(current_thread, sizeof(current_thread), "thread:%016" PRIx64 ";", target->rtos->current_thread);
  674. target->rtos->current_threadid = target->rtos->current_thread;
  675. }
  676. sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
  677. signal_var, stop_reason, current_thread);
  678. }
  679. gdb_put_packet(connection, sig_reply, sig_reply_len);
  680. gdb_connection->frontend_state = TARGET_HALTED;
  681. }
  682. static void gdb_fileio_reply(struct target *target, struct connection *connection)
  683. {
  684. struct gdb_connection *gdb_connection = connection->priv;
  685. char fileio_command[256];
  686. int command_len;
  687. bool program_exited = false;
  688. if (strcmp(target->fileio_info->identifier, "open") == 0)
  689. sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  690. target->fileio_info->param_1,
  691. target->fileio_info->param_2,
  692. target->fileio_info->param_3,
  693. target->fileio_info->param_4);
  694. else if (strcmp(target->fileio_info->identifier, "close") == 0)
  695. sprintf(fileio_command, "F%s,%" PRIx32, target->fileio_info->identifier,
  696. target->fileio_info->param_1);
  697. else if (strcmp(target->fileio_info->identifier, "read") == 0)
  698. sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  699. target->fileio_info->param_1,
  700. target->fileio_info->param_2,
  701. target->fileio_info->param_3);
  702. else if (strcmp(target->fileio_info->identifier, "write") == 0)
  703. sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  704. target->fileio_info->param_1,
  705. target->fileio_info->param_2,
  706. target->fileio_info->param_3);
  707. else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
  708. sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  709. target->fileio_info->param_1,
  710. target->fileio_info->param_2,
  711. target->fileio_info->param_3);
  712. else if (strcmp(target->fileio_info->identifier, "rename") == 0)
  713. sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
  714. target->fileio_info->param_1,
  715. target->fileio_info->param_2,
  716. target->fileio_info->param_3,
  717. target->fileio_info->param_4);
  718. else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
  719. sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
  720. target->fileio_info->param_1,
  721. target->fileio_info->param_2);
  722. else if (strcmp(target->fileio_info->identifier, "stat") == 0)
  723. sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  724. target->fileio_info->param_1,
  725. target->fileio_info->param_2,
  726. target->fileio_info->param_3);
  727. else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
  728. sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  729. target->fileio_info->param_1,
  730. target->fileio_info->param_2);
  731. else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
  732. sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
  733. target->fileio_info->param_1,
  734. target->fileio_info->param_2);
  735. else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
  736. sprintf(fileio_command, "F%s,%" PRIx32, target->fileio_info->identifier,
  737. target->fileio_info->param_1);
  738. else if (strcmp(target->fileio_info->identifier, "system") == 0)
  739. sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
  740. target->fileio_info->param_1,
  741. target->fileio_info->param_2);
  742. else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
  743. /* If target hits exit syscall, report to GDB the program is terminated.
  744. * In addition, let target run its own exit syscall handler. */
  745. program_exited = true;
  746. sprintf(fileio_command, "W%02" PRIx32, target->fileio_info->param_1);
  747. } else {
  748. LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
  749. /* encounter unknown syscall, continue */
  750. gdb_connection->frontend_state = TARGET_RUNNING;
  751. target_resume(target, 1, 0x0, 0, 0);
  752. return;
  753. }
  754. command_len = strlen(fileio_command);
  755. gdb_put_packet(connection, fileio_command, command_len);
  756. if (program_exited) {
  757. /* Use target_resume() to let target run its own exit syscall handler. */
  758. gdb_connection->frontend_state = TARGET_RUNNING;
  759. target_resume(target, 1, 0x0, 0, 0);
  760. } else {
  761. gdb_connection->frontend_state = TARGET_HALTED;
  762. rtos_update_threads(target);
  763. }
  764. }
  765. static void gdb_frontend_halted(struct target *target, struct connection *connection)
  766. {
  767. struct gdb_connection *gdb_connection = connection->priv;
  768. /* In the GDB protocol when we are stepping or continuing execution,
  769. * we have a lingering reply. Upon receiving a halted event
  770. * when we have that lingering packet, we reply to the original
  771. * step or continue packet.
  772. *
  773. * Executing monitor commands can bring the target in and
  774. * out of the running state so we'll see lots of TARGET_EVENT_XXX
  775. * that are to be ignored.
  776. */
  777. if (gdb_connection->frontend_state == TARGET_RUNNING) {
  778. /* stop forwarding log packets! */
  779. log_remove_callback(gdb_log_callback, connection);
  780. /* check fileio first */
  781. if (target_get_gdb_fileio_info(target, target->fileio_info) == ERROR_OK)
  782. gdb_fileio_reply(target, connection);
  783. else
  784. gdb_signal_reply(target, connection);
  785. }
  786. }
  787. static int gdb_target_callback_event_handler(struct target *target,
  788. enum target_event event, void *priv)
  789. {
  790. int retval;
  791. struct connection *connection = priv;
  792. struct gdb_service *gdb_service = connection->service->priv;
  793. if (gdb_service->target != target)
  794. return ERROR_OK;
  795. switch (event) {
  796. case TARGET_EVENT_GDB_HALT:
  797. gdb_frontend_halted(target, connection);
  798. break;
  799. case TARGET_EVENT_HALTED:
  800. target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
  801. break;
  802. case TARGET_EVENT_GDB_FLASH_ERASE_START:
  803. retval = jtag_execute_queue();
  804. if (retval != ERROR_OK)
  805. return retval;
  806. break;
  807. default:
  808. break;
  809. }
  810. return ERROR_OK;
  811. }
  812. static int gdb_new_connection(struct connection *connection)
  813. {
  814. struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
  815. struct gdb_service *gdb_service = connection->service->priv;
  816. int retval;
  817. int initial_ack;
  818. connection->priv = gdb_connection;
  819. /* initialize gdb connection information */
  820. gdb_connection->buf_p = gdb_connection->buffer;
  821. gdb_connection->buf_cnt = 0;
  822. gdb_connection->ctrl_c = 0;
  823. gdb_connection->frontend_state = TARGET_HALTED;
  824. gdb_connection->vflash_image = NULL;
  825. gdb_connection->closed = 0;
  826. gdb_connection->busy = 0;
  827. gdb_connection->noack_mode = 0;
  828. gdb_connection->sync = false;
  829. gdb_connection->mem_write_error = false;
  830. gdb_connection->attached = true;
  831. gdb_connection->target_desc.tdesc = NULL;
  832. gdb_connection->target_desc.tdesc_length = 0;
  833. gdb_connection->thread_list = NULL;
  834. /* send ACK to GDB for debug request */
  835. gdb_write(connection, "+", 1);
  836. /* output goes through gdb connection */
  837. command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
  838. /* we must remove all breakpoints registered to the target as a previous
  839. * GDB session could leave dangling breakpoints if e.g. communication
  840. * timed out.
  841. */
  842. breakpoint_clear_target(gdb_service->target);
  843. watchpoint_clear_target(gdb_service->target);
  844. /* clean previous rtos session if supported*/
  845. if ((gdb_service->target->rtos) && (gdb_service->target->rtos->type->clean))
  846. gdb_service->target->rtos->type->clean(gdb_service->target);
  847. /* remove the initial ACK from the incoming buffer */
  848. retval = gdb_get_char(connection, &initial_ack);
  849. if (retval != ERROR_OK)
  850. return retval;
  851. /* FIX!!!??? would we actually ever receive a + here???
  852. * Not observed.
  853. */
  854. if (initial_ack != '+')
  855. gdb_putback_char(connection, initial_ack);
  856. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
  857. if (gdb_use_memory_map) {
  858. /* Connect must fail if the memory map can't be set up correctly.
  859. *
  860. * This will cause an auto_probe to be invoked, which is either
  861. * a no-op or it will fail when the target isn't ready(e.g. not halted).
  862. */
  863. int i;
  864. for (i = 0; i < flash_get_bank_count(); i++) {
  865. struct flash_bank *p;
  866. p = get_flash_bank_by_num_noprobe(i);
  867. if (p->target != gdb_service->target)
  868. continue;
  869. retval = get_flash_bank_by_num(i, &p);
  870. if (retval != ERROR_OK) {
  871. LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
  872. "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
  873. return retval;
  874. }
  875. }
  876. }
  877. gdb_actual_connections++;
  878. LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
  879. gdb_actual_connections,
  880. target_name(gdb_service->target),
  881. target_state_name(gdb_service->target));
  882. /* DANGER! If we fail subsequently, we must remove this handler,
  883. * otherwise we occasionally see crashes as the timer can invoke the
  884. * callback fn.
  885. *
  886. * register callback to be informed about target events */
  887. target_register_event_callback(gdb_target_callback_event_handler, connection);
  888. return ERROR_OK;
  889. }
  890. static int gdb_connection_closed(struct connection *connection)
  891. {
  892. struct gdb_service *gdb_service = connection->service->priv;
  893. struct gdb_connection *gdb_connection = connection->priv;
  894. /* we're done forwarding messages. Tear down callback before
  895. * cleaning up connection.
  896. */
  897. log_remove_callback(gdb_log_callback, connection);
  898. gdb_actual_connections--;
  899. LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
  900. target_name(gdb_service->target),
  901. target_state_name(gdb_service->target),
  902. gdb_actual_connections);
  903. /* see if an image built with vFlash commands is left */
  904. if (gdb_connection->vflash_image) {
  905. image_close(gdb_connection->vflash_image);
  906. free(gdb_connection->vflash_image);
  907. gdb_connection->vflash_image = NULL;
  908. }
  909. /* if this connection registered a debug-message receiver delete it */
  910. delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
  911. if (connection->priv) {
  912. free(connection->priv);
  913. connection->priv = NULL;
  914. } else
  915. LOG_ERROR("BUG: connection->priv == NULL");
  916. target_unregister_event_callback(gdb_target_callback_event_handler, connection);
  917. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
  918. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
  919. return ERROR_OK;
  920. }
  921. static void gdb_send_error(struct connection *connection, uint8_t the_error)
  922. {
  923. char err[4];
  924. snprintf(err, 4, "E%2.2X", the_error);
  925. gdb_put_packet(connection, err, 3);
  926. }
  927. static int gdb_last_signal_packet(struct connection *connection,
  928. char const *packet, int packet_size)
  929. {
  930. struct target *target = get_target_from_connection(connection);
  931. struct gdb_connection *gdb_con = connection->priv;
  932. char sig_reply[4];
  933. int signal_var;
  934. if (!gdb_con->attached) {
  935. /* if we are here we have received a kill packet
  936. * reply W stop reply otherwise gdb gets very unhappy */
  937. gdb_put_packet(connection, "W00", 3);
  938. return ERROR_OK;
  939. }
  940. signal_var = gdb_last_signal(target);
  941. snprintf(sig_reply, 4, "S%2.2x", signal_var);
  942. gdb_put_packet(connection, sig_reply, 3);
  943. return ERROR_OK;
  944. }
  945. static inline int gdb_reg_pos(struct target *target, int pos, int len)
  946. {
  947. if (target->endianness == TARGET_LITTLE_ENDIAN)
  948. return pos;
  949. else
  950. return len - 1 - pos;
  951. }
  952. /* Convert register to string of bytes. NB! The # of bits in the
  953. * register might be non-divisible by 8(a byte), in which
  954. * case an entire byte is shown.
  955. *
  956. * NB! the format on the wire is the target endianness
  957. *
  958. * The format of reg->value is little endian
  959. *
  960. */
  961. static void gdb_str_to_target(struct target *target,
  962. char *tstr, struct reg *reg)
  963. {
  964. int i;
  965. uint8_t *buf;
  966. int buf_len;
  967. buf = reg->value;
  968. buf_len = DIV_ROUND_UP(reg->size, 8);
  969. for (i = 0; i < buf_len; i++) {
  970. int j = gdb_reg_pos(target, i, buf_len);
  971. tstr += sprintf(tstr, "%02x", buf[j]);
  972. }
  973. }
  974. /* copy over in register buffer */
  975. static void gdb_target_to_reg(struct target *target,
  976. char const *tstr, int str_len, uint8_t *bin)
  977. {
  978. if (str_len % 2) {
  979. LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
  980. exit(-1);
  981. }
  982. int i;
  983. for (i = 0; i < str_len; i += 2) {
  984. unsigned t;
  985. if (sscanf(tstr + i, "%02x", &t) != 1) {
  986. LOG_ERROR("BUG: unable to convert register value");
  987. exit(-1);
  988. }
  989. int j = gdb_reg_pos(target, i/2, str_len/2);
  990. bin[j] = t;
  991. }
  992. }
  993. static int gdb_get_registers_packet(struct connection *connection,
  994. char const *packet, int packet_size)
  995. {
  996. struct target *target = get_target_from_connection(connection);
  997. struct reg **reg_list;
  998. int reg_list_size;
  999. int retval;
  1000. int reg_packet_size = 0;
  1001. char *reg_packet;
  1002. char *reg_packet_p;
  1003. int i;
  1004. #ifdef _DEBUG_GDB_IO_
  1005. LOG_DEBUG("-");
  1006. #endif
  1007. if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
  1008. return ERROR_OK;
  1009. retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
  1010. REG_CLASS_GENERAL);
  1011. if (retval != ERROR_OK)
  1012. return gdb_error(connection, retval);
  1013. for (i = 0; i < reg_list_size; i++)
  1014. reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
  1015. assert(reg_packet_size > 0);
  1016. reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
  1017. if (reg_packet == NULL)
  1018. return ERROR_FAIL;
  1019. reg_packet_p = reg_packet;
  1020. for (i = 0; i < reg_list_size; i++) {
  1021. if (!reg_list[i]->valid)
  1022. reg_list[i]->type->get(reg_list[i]);
  1023. gdb_str_to_target(target, reg_packet_p, reg_list[i]);
  1024. reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
  1025. }
  1026. #ifdef _DEBUG_GDB_IO_
  1027. {
  1028. char *reg_packet_p_debug;
  1029. reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
  1030. LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
  1031. free(reg_packet_p_debug);
  1032. }
  1033. #endif
  1034. gdb_put_packet(connection, reg_packet, reg_packet_size);
  1035. free(reg_packet);
  1036. free(reg_list);
  1037. return ERROR_OK;
  1038. }
  1039. static int gdb_set_registers_packet(struct connection *connection,
  1040. char const *packet, int packet_size)
  1041. {
  1042. struct target *target = get_target_from_connection(connection);
  1043. int i;
  1044. struct reg **reg_list;
  1045. int reg_list_size;
  1046. int retval;
  1047. char const *packet_p;
  1048. #ifdef _DEBUG_GDB_IO_
  1049. LOG_DEBUG("-");
  1050. #endif
  1051. /* skip command character */
  1052. packet++;
  1053. packet_size--;
  1054. if (packet_size % 2) {
  1055. LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
  1056. return ERROR_SERVER_REMOTE_CLOSED;
  1057. }
  1058. retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
  1059. REG_CLASS_GENERAL);
  1060. if (retval != ERROR_OK)
  1061. return gdb_error(connection, retval);
  1062. packet_p = packet;
  1063. for (i = 0; i < reg_list_size; i++) {
  1064. uint8_t *bin_buf;
  1065. int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
  1066. if (packet_p + chars > packet + packet_size)
  1067. LOG_ERROR("BUG: register packet is too small for registers");
  1068. bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
  1069. gdb_target_to_reg(target, packet_p, chars, bin_buf);
  1070. reg_list[i]->type->set(reg_list[i], bin_buf);
  1071. /* advance packet pointer */
  1072. packet_p += chars;
  1073. free(bin_buf);
  1074. }
  1075. /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
  1076. free(reg_list);
  1077. gdb_put_packet(connection, "OK", 2);
  1078. return ERROR_OK;
  1079. }
  1080. static int gdb_get_register_packet(struct connection *connection,
  1081. char const *packet, int packet_size)
  1082. {
  1083. struct target *target = get_target_from_connection(connection);
  1084. char *reg_packet;
  1085. int reg_num = strtoul(packet + 1, NULL, 16);
  1086. struct reg **reg_list;
  1087. int reg_list_size;
  1088. int retval;
  1089. #ifdef _DEBUG_GDB_IO_
  1090. LOG_DEBUG("-");
  1091. #endif
  1092. retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
  1093. REG_CLASS_ALL);
  1094. if (retval != ERROR_OK)
  1095. return gdb_error(connection, retval);
  1096. if (reg_list_size <= reg_num) {
  1097. LOG_ERROR("gdb requested a non-existing register");
  1098. return ERROR_SERVER_REMOTE_CLOSED;
  1099. }
  1100. if (!reg_list[reg_num]->valid)
  1101. reg_list[reg_num]->type->get(reg_list[reg_num]);
  1102. reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */
  1103. gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
  1104. gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
  1105. free(reg_list);
  1106. free(reg_packet);
  1107. return ERROR_OK;
  1108. }
  1109. static int gdb_set_register_packet(struct connection *connection,
  1110. char const *packet, int packet_size)
  1111. {
  1112. struct target *target = get_target_from_connection(connection);
  1113. char *separator;
  1114. uint8_t *bin_buf;
  1115. int reg_num = strtoul(packet + 1, &separator, 16);
  1116. struct reg **reg_list;
  1117. int reg_list_size;
  1118. int retval;
  1119. LOG_DEBUG("-");
  1120. retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
  1121. REG_CLASS_ALL);
  1122. if (retval != ERROR_OK)
  1123. return gdb_error(connection, retval);
  1124. if (reg_list_size <= reg_num) {
  1125. LOG_ERROR("gdb requested a non-existing register");
  1126. return ERROR_SERVER_REMOTE_CLOSED;
  1127. }
  1128. if (*separator != '=') {
  1129. LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
  1130. return ERROR_SERVER_REMOTE_CLOSED;
  1131. }
  1132. /* convert from GDB-string (target-endian) to hex-string (big-endian) */
  1133. bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
  1134. int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
  1135. if ((unsigned int)chars != strlen(separator + 1)) {
  1136. LOG_ERROR("gdb sent a packet with wrong register size");
  1137. free(bin_buf);
  1138. return ERROR_SERVER_REMOTE_CLOSED;
  1139. }
  1140. gdb_target_to_reg(target, separator + 1, chars, bin_buf);
  1141. reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
  1142. gdb_put_packet(connection, "OK", 2);
  1143. free(bin_buf);
  1144. free(reg_list);
  1145. return ERROR_OK;
  1146. }
  1147. /* No attempt is made to translate the "retval" to
  1148. * GDB speak. This has to be done at the calling
  1149. * site as no mapping really exists.
  1150. */
  1151. static int gdb_error(struct connection *connection, int retval)
  1152. {
  1153. LOG_DEBUG("Reporting %i to GDB as generic error", retval);
  1154. gdb_send_error(connection, EFAULT);
  1155. return ERROR_OK;
  1156. }
  1157. /* We don't have to worry about the default 2 second timeout for GDB packets,
  1158. * because GDB breaks up large memory reads into smaller reads.
  1159. *
  1160. * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
  1161. */
  1162. static int gdb_read_memory_packet(struct connection *connection,
  1163. char const *packet, int packet_size)
  1164. {
  1165. struct target *target = get_target_from_connection(connection);
  1166. char *separator;
  1167. uint32_t addr = 0;
  1168. uint32_t len = 0;
  1169. uint8_t *buffer;
  1170. char *hex_buffer;
  1171. int retval = ERROR_OK;
  1172. /* skip command character */
  1173. packet++;
  1174. addr = strtoul(packet, &separator, 16);
  1175. if (*separator != ',') {
  1176. LOG_ERROR("incomplete read memory packet received, dropping connection");
  1177. return ERROR_SERVER_REMOTE_CLOSED;
  1178. }
  1179. len = strtoul(separator + 1, NULL, 16);
  1180. if (!len) {
  1181. LOG_WARNING("invalid read memory packet received (len == 0)");
  1182. gdb_put_packet(connection, NULL, 0);
  1183. return ERROR_OK;
  1184. }
  1185. buffer = malloc(len);
  1186. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1187. retval = target_read_buffer(target, addr, len, buffer);
  1188. if ((retval != ERROR_OK) && !gdb_report_data_abort) {
  1189. /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
  1190. * At some point this might be fixed in GDB, in which case this code can be removed.
  1191. *
  1192. * OpenOCD developers are acutely aware of this problem, but there is nothing
  1193. * gained by involving the user in this problem that hopefully will get resolved
  1194. * eventually
  1195. *
  1196. * http://sourceware.org/cgi-bin/gnatsweb.pl? \
  1197. * cmd = view%20audit-trail&database = gdb&pr = 2395
  1198. *
  1199. * For now, the default is to fix up things to make current GDB versions work.
  1200. * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
  1201. */
  1202. memset(buffer, 0, len);
  1203. retval = ERROR_OK;
  1204. }
  1205. if (retval == ERROR_OK) {
  1206. hex_buffer = malloc(len * 2 + 1);
  1207. size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
  1208. gdb_put_packet(connection, hex_buffer, pkt_len);
  1209. free(hex_buffer);
  1210. } else
  1211. retval = gdb_error(connection, retval);
  1212. free(buffer);
  1213. return retval;
  1214. }
  1215. static int gdb_write_memory_packet(struct connection *connection,
  1216. char const *packet, int packet_size)
  1217. {
  1218. struct target *target = get_target_from_connection(connection);
  1219. char *separator;
  1220. uint32_t addr = 0;
  1221. uint32_t len = 0;
  1222. uint8_t *buffer;
  1223. int retval;
  1224. /* skip command character */
  1225. packet++;
  1226. addr = strtoul(packet, &separator, 16);
  1227. if (*separator != ',') {
  1228. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1229. return ERROR_SERVER_REMOTE_CLOSED;
  1230. }
  1231. len = strtoul(separator + 1, &separator, 16);
  1232. if (*(separator++) != ':') {
  1233. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1234. return ERROR_SERVER_REMOTE_CLOSED;
  1235. }
  1236. buffer = malloc(len);
  1237. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1238. if (unhexify(buffer, separator, len) != len)
  1239. LOG_ERROR("unable to decode memory packet");
  1240. retval = target_write_buffer(target, addr, len, buffer);
  1241. if (retval == ERROR_OK)
  1242. gdb_put_packet(connection, "OK", 2);
  1243. else
  1244. retval = gdb_error(connection, retval);
  1245. free(buffer);
  1246. return retval;
  1247. }
  1248. static int gdb_write_memory_binary_packet(struct connection *connection,
  1249. char const *packet, int packet_size)
  1250. {
  1251. struct target *target = get_target_from_connection(connection);
  1252. char *separator;
  1253. uint32_t addr = 0;
  1254. uint32_t len = 0;
  1255. int retval = ERROR_OK;
  1256. /* Packets larger than fast_limit bytes will be acknowledged instantly on
  1257. * the assumption that we're in a download and it's important to go as fast
  1258. * as possible. */
  1259. uint32_t fast_limit = 8;
  1260. /* skip command character */
  1261. packet++;
  1262. addr = strtoul(packet, &separator, 16);
  1263. if (*separator != ',') {
  1264. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1265. return ERROR_SERVER_REMOTE_CLOSED;
  1266. }
  1267. len = strtoul(separator + 1, &separator, 16);
  1268. if (*(separator++) != ':') {
  1269. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1270. return ERROR_SERVER_REMOTE_CLOSED;
  1271. }
  1272. struct gdb_connection *gdb_connection = connection->priv;
  1273. if (gdb_connection->mem_write_error)
  1274. retval = ERROR_FAIL;
  1275. if (retval == ERROR_OK) {
  1276. if (len >= fast_limit) {
  1277. /* By replying the packet *immediately* GDB will send us a new packet
  1278. * while we write the last one to the target.
  1279. * We only do this for larger writes, so that users who do something like:
  1280. * p *((int*)0xdeadbeef)=8675309
  1281. * will get immediate feedback that that write failed.
  1282. */
  1283. gdb_put_packet(connection, "OK", 2);
  1284. }
  1285. } else {
  1286. retval = gdb_error(connection, retval);
  1287. /* now that we have reported the memory write error, we can clear the condition */
  1288. gdb_connection->mem_write_error = false;
  1289. if (retval != ERROR_OK)
  1290. return retval;
  1291. }
  1292. if (len) {
  1293. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1294. retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
  1295. if (retval != ERROR_OK)
  1296. gdb_connection->mem_write_error = true;
  1297. }
  1298. if (len < fast_limit) {
  1299. if (retval != ERROR_OK) {
  1300. gdb_error(connection, retval);
  1301. gdb_connection->mem_write_error = false;
  1302. } else {
  1303. gdb_put_packet(connection, "OK", 2);
  1304. }
  1305. }
  1306. return ERROR_OK;
  1307. }
  1308. static int gdb_step_continue_packet(struct connection *connection,
  1309. char const *packet, int packet_size)
  1310. {
  1311. struct target *target = get_target_from_connection(connection);
  1312. int current = 0;
  1313. uint32_t address = 0x0;
  1314. int retval = ERROR_OK;
  1315. LOG_DEBUG("-");
  1316. if (packet_size > 1)
  1317. address = strtoul(packet + 1, NULL, 16);
  1318. else
  1319. current = 1;
  1320. gdb_running_type = packet[0];
  1321. if (packet[0] == 'c') {
  1322. LOG_DEBUG("continue");
  1323. /* resume at current address, don't handle breakpoints, not debugging */
  1324. retval = target_resume(target, current, address, 0, 0);
  1325. } else if (packet[0] == 's') {
  1326. LOG_DEBUG("step");
  1327. /* step at current or address, don't handle breakpoints */
  1328. retval = target_step(target, current, address, 0);
  1329. }
  1330. return retval;
  1331. }
  1332. static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
  1333. char const *packet, int packet_size)
  1334. {
  1335. struct target *target = get_target_from_connection(connection);
  1336. int type;
  1337. enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
  1338. enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
  1339. uint32_t address;
  1340. uint32_t size;
  1341. char *separator;
  1342. int retval;
  1343. LOG_DEBUG("-");
  1344. type = strtoul(packet + 1, &separator, 16);
  1345. if (type == 0) /* memory breakpoint */
  1346. bp_type = BKPT_SOFT;
  1347. else if (type == 1) /* hardware breakpoint */
  1348. bp_type = BKPT_HARD;
  1349. else if (type == 2) /* write watchpoint */
  1350. wp_type = WPT_WRITE;
  1351. else if (type == 3) /* read watchpoint */
  1352. wp_type = WPT_READ;
  1353. else if (type == 4) /* access watchpoint */
  1354. wp_type = WPT_ACCESS;
  1355. else {
  1356. LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
  1357. return ERROR_SERVER_REMOTE_CLOSED;
  1358. }
  1359. if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
  1360. bp_type = gdb_breakpoint_override_type;
  1361. if (*separator != ',') {
  1362. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1363. return ERROR_SERVER_REMOTE_CLOSED;
  1364. }
  1365. address = strtoul(separator + 1, &separator, 16);
  1366. if (*separator != ',') {
  1367. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1368. return ERROR_SERVER_REMOTE_CLOSED;
  1369. }
  1370. size = strtoul(separator + 1, &separator, 16);
  1371. switch (type) {
  1372. case 0:
  1373. case 1:
  1374. if (packet[0] == 'Z') {
  1375. retval = breakpoint_add(target, address, size, bp_type);
  1376. if (retval != ERROR_OK) {
  1377. retval = gdb_error(connection, retval);
  1378. if (retval != ERROR_OK)
  1379. return retval;
  1380. } else
  1381. gdb_put_packet(connection, "OK", 2);
  1382. } else {
  1383. breakpoint_remove(target, address);
  1384. gdb_put_packet(connection, "OK", 2);
  1385. }
  1386. break;
  1387. case 2:
  1388. case 3:
  1389. case 4:
  1390. {
  1391. if (packet[0] == 'Z') {
  1392. retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
  1393. if (retval != ERROR_OK) {
  1394. retval = gdb_error(connection, retval);
  1395. if (retval != ERROR_OK)
  1396. return retval;
  1397. } else
  1398. gdb_put_packet(connection, "OK", 2);
  1399. } else {
  1400. watchpoint_remove(target, address);
  1401. gdb_put_packet(connection, "OK", 2);
  1402. }
  1403. break;
  1404. }
  1405. default:
  1406. break;
  1407. }
  1408. return ERROR_OK;
  1409. }
  1410. /* print out a string and allocate more space as needed,
  1411. * mainly used for XML at this point
  1412. */
  1413. static void xml_printf(int *retval, char **xml, int *pos, int *size,
  1414. const char *fmt, ...)
  1415. {
  1416. if (*retval != ERROR_OK)
  1417. return;
  1418. int first = 1;
  1419. for (;; ) {
  1420. if ((*xml == NULL) || (!first)) {
  1421. /* start by 0 to exercise all the code paths.
  1422. * Need minimum 2 bytes to fit 1 char and 0 terminator. */
  1423. *size = *size * 2 + 2;
  1424. char *t = *xml;
  1425. *xml = realloc(*xml, *size);
  1426. if (*xml == NULL) {
  1427. if (t)
  1428. free(t);
  1429. *retval = ERROR_SERVER_REMOTE_CLOSED;
  1430. return;
  1431. }
  1432. }
  1433. va_list ap;
  1434. int ret;
  1435. va_start(ap, fmt);
  1436. ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
  1437. va_end(ap);
  1438. if ((ret > 0) && ((ret + 1) < *size - *pos)) {
  1439. *pos += ret;
  1440. return;
  1441. }
  1442. /* there was just enough or not enough space, allocate more. */
  1443. first = 0;
  1444. }
  1445. }
  1446. static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
  1447. {
  1448. /* Locate the annex. */
  1449. const char *annex_end = strchr(buf, ':');
  1450. if (annex_end == NULL)
  1451. return ERROR_FAIL;
  1452. /* After the read marker and annex, qXfer looks like a
  1453. * traditional 'm' packet. */
  1454. char *separator;
  1455. *ofs = strtoul(annex_end + 1, &separator, 16);
  1456. if (*separator != ',')
  1457. return ERROR_FAIL;
  1458. *len = strtoul(separator + 1, NULL, 16);
  1459. /* Extract the annex if needed */
  1460. if (annex != NULL) {
  1461. *annex = strndup(buf, annex_end - buf);
  1462. if (*annex == NULL)
  1463. return ERROR_FAIL;
  1464. }
  1465. return ERROR_OK;
  1466. }
  1467. static int compare_bank(const void *a, const void *b)
  1468. {
  1469. struct flash_bank *b1, *b2;
  1470. b1 = *((struct flash_bank **)a);
  1471. b2 = *((struct flash_bank **)b);
  1472. if (b1->base == b2->base)
  1473. return 0;
  1474. else if (b1->base > b2->base)
  1475. return 1;
  1476. else
  1477. return -1;
  1478. }
  1479. static int gdb_memory_map(struct connection *connection,
  1480. char const *packet, int packet_size)
  1481. {
  1482. /* We get away with only specifying flash here. Regions that are not
  1483. * specified are treated as if we provided no memory map(if not we
  1484. * could detect the holes and mark them as RAM).
  1485. * Normally we only execute this code once, but no big deal if we
  1486. * have to regenerate it a couple of times.
  1487. */
  1488. struct target *target = get_target_from_connection(connection);
  1489. struct flash_bank *p;
  1490. char *xml = NULL;
  1491. int size = 0;
  1492. int pos = 0;
  1493. int retval = ERROR_OK;
  1494. struct flash_bank **banks;
  1495. int offset;
  1496. int length;
  1497. char *separator;
  1498. uint32_t ram_start = 0;
  1499. int i;
  1500. int target_flash_banks = 0;
  1501. /* skip command character */
  1502. packet += 23;
  1503. offset = strtoul(packet, &separator, 16);
  1504. length = strtoul(separator + 1, &separator, 16);
  1505. xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
  1506. /* Sort banks in ascending order. We need to report non-flash
  1507. * memory as ram (or rather read/write) by default for GDB, since
  1508. * it has no concept of non-cacheable read/write memory (i/o etc).
  1509. *
  1510. * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
  1511. * Current versions of GDB assume unlisted addresses are RAM...
  1512. */
  1513. banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
  1514. for (i = 0; i < flash_get_bank_count(); i++) {
  1515. p = get_flash_bank_by_num_noprobe(i);
  1516. if (p->target != target)
  1517. continue;
  1518. retval = get_flash_bank_by_num(i, &p);
  1519. if (retval != ERROR_OK) {
  1520. free(banks);
  1521. gdb_error(connection, retval);
  1522. return retval;
  1523. }
  1524. banks[target_flash_banks++] = p;
  1525. }
  1526. qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
  1527. compare_bank);
  1528. for (i = 0; i < target_flash_banks; i++) {
  1529. int j;
  1530. unsigned sector_size = 0;
  1531. uint32_t start;
  1532. p = banks[i];
  1533. start = p->base;
  1534. if (ram_start < p->base)
  1535. xml_printf(&retval, &xml, &pos, &size,
  1536. "<memory type=\"ram\" start=\"0x%x\" "
  1537. "length=\"0x%x\"/>\n",
  1538. ram_start, p->base - ram_start);
  1539. /* Report adjacent groups of same-size sectors. So for
  1540. * example top boot CFI flash will list an initial region
  1541. * with several large sectors (maybe 128KB) and several
  1542. * smaller ones at the end (maybe 32KB). STR7 will have
  1543. * regions with 8KB, 32KB, and 64KB sectors; etc.
  1544. */
  1545. for (j = 0; j < p->num_sectors; j++) {
  1546. unsigned group_len;
  1547. /* Maybe start a new group of sectors. */
  1548. if (sector_size == 0) {
  1549. start = p->base + p->sectors[j].offset;
  1550. xml_printf(&retval, &xml, &pos, &size,
  1551. "<memory type=\"flash\" "
  1552. "start=\"0x%x\" ",
  1553. start);
  1554. sector_size = p->sectors[j].size;
  1555. }
  1556. /* Does this finish a group of sectors?
  1557. * If not, continue an already-started group.
  1558. */
  1559. if (j == p->num_sectors - 1)
  1560. group_len = (p->base + p->size) - start;
  1561. else if (p->sectors[j + 1].size != sector_size)
  1562. group_len = p->base + p->sectors[j + 1].offset
  1563. - start;
  1564. else
  1565. continue;
  1566. xml_printf(&retval, &xml, &pos, &size,
  1567. "length=\"0x%x\">\n"
  1568. "<property name=\"blocksize\">"
  1569. "0x%x</property>\n"
  1570. "</memory>\n",
  1571. group_len,
  1572. sector_size);
  1573. sector_size = 0;
  1574. }
  1575. ram_start = p->base + p->size;
  1576. }
  1577. if (ram_start != 0)
  1578. xml_printf(&retval, &xml, &pos, &size,
  1579. "<memory type=\"ram\" start=\"0x%x\" "
  1580. "length=\"0x%x\"/>\n",
  1581. ram_start, 0-ram_start);
  1582. /* ELSE a flash chip could be at the very end of the 32 bit address
  1583. * space, in which case ram_start will be precisely 0
  1584. */
  1585. free(banks);
  1586. banks = NULL;
  1587. xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
  1588. if (retval != ERROR_OK) {
  1589. gdb_error(connection, retval);
  1590. return retval;
  1591. }
  1592. if (offset + length > pos)
  1593. length = pos - offset;
  1594. char *t = malloc(length + 1);
  1595. t[0] = 'l';
  1596. memcpy(t + 1, xml + offset, length);
  1597. gdb_put_packet(connection, t, length + 1);
  1598. free(t);
  1599. free(xml);
  1600. return ERROR_OK;
  1601. }
  1602. static const char *gdb_get_reg_type_name(enum reg_type type)
  1603. {
  1604. switch (type) {
  1605. case REG_TYPE_INT:
  1606. return "int";
  1607. case REG_TYPE_INT8:
  1608. return "int8";
  1609. case REG_TYPE_INT16:
  1610. return "int16";
  1611. case REG_TYPE_INT32:
  1612. return "int32";
  1613. case REG_TYPE_INT64:
  1614. return "int64";
  1615. case REG_TYPE_INT128:
  1616. return "int128";
  1617. case REG_TYPE_UINT8:
  1618. return "uint8";
  1619. case REG_TYPE_UINT16:
  1620. return "uint16";
  1621. case REG_TYPE_UINT32:
  1622. return "uint32";
  1623. case REG_TYPE_UINT64:
  1624. return "uint64";
  1625. case REG_TYPE_UINT128:
  1626. return "uint128";
  1627. case REG_TYPE_CODE_PTR:
  1628. return "code_ptr";
  1629. case REG_TYPE_DATA_PTR:
  1630. return "data_ptr";
  1631. case REG_TYPE_FLOAT:
  1632. return "float";
  1633. case REG_TYPE_IEEE_SINGLE:
  1634. return "ieee_single";
  1635. case REG_TYPE_IEEE_DOUBLE:
  1636. return "ieee_double";
  1637. case REG_TYPE_ARCH_DEFINED:
  1638. return "int"; /* return arbitrary string to avoid compile warning. */
  1639. }
  1640. return "int"; /* "int" as default value */
  1641. }
  1642. static int gdb_generate_reg_type_description(struct target *target,
  1643. char **tdesc, int *pos, int *size, struct reg_data_type *type)
  1644. {
  1645. int retval = ERROR_OK;
  1646. if (type->type_class == REG_TYPE_CLASS_VECTOR) {
  1647. /* <vector id="id" type="type" count="count"/> */
  1648. xml_printf(&retval, tdesc, pos, size,
  1649. "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
  1650. type->id, type->reg_type_vector->type->id,
  1651. type->reg_type_vector->count);
  1652. } else if (type->type_class == REG_TYPE_CLASS_UNION) {
  1653. /* <union id="id">
  1654. * <field name="name" type="type"/> ...
  1655. * </union> */
  1656. xml_printf(&retval, tdesc, pos, size,
  1657. "<union id=\"%s\">\n",
  1658. type->id);
  1659. struct reg_data_type_union_field *field;
  1660. field = type->reg_type_union->fields;
  1661. while (field != NULL) {
  1662. xml_printf(&retval, tdesc, pos, size,
  1663. "<field name=\"%s\" type=\"%s\"/>\n",
  1664. field->name, field->type->id);
  1665. field = field->next;
  1666. }
  1667. xml_printf(&retval, tdesc, pos, size,
  1668. "</union>\n");
  1669. } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
  1670. struct reg_data_type_struct_field *field;
  1671. field = type->reg_type_struct->fields;
  1672. if (field->use_bitfields) {
  1673. /* <struct id="id" size="size">
  1674. * <field name="name" start="start" end="end"/> ...
  1675. * </struct> */
  1676. xml_printf(&retval, tdesc, pos, size,
  1677. "<struct id=\"%s\" size=\"%d\">\n",
  1678. type->id, type->reg_type_struct->size);
  1679. while (field != NULL) {
  1680. xml_printf(&retval, tdesc, pos, size,
  1681. "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
  1682. field->name, field->bitfield->start,
  1683. field->bitfield->end);
  1684. field = field->next;
  1685. }
  1686. } else {
  1687. /* <struct id="id">
  1688. * <field name="name" type="type"/> ...
  1689. * </struct> */
  1690. xml_printf(&retval, tdesc, pos, size,
  1691. "<struct id=\"%s\">\n",
  1692. type->id);
  1693. while (field != NULL) {
  1694. xml_printf(&retval, tdesc, pos, size,
  1695. "<field name=\"%s\" type=\"%s\"/>\n",
  1696. field->name, field->type->id);
  1697. field = field->next;
  1698. }
  1699. }
  1700. xml_printf(&retval, tdesc, pos, size,
  1701. "</struct>\n");
  1702. } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
  1703. /* <flags id="id" size="size">
  1704. * <field name="name" start="start" end="end"/> ...
  1705. * </flags> */
  1706. xml_printf(&retval, tdesc, pos, size,
  1707. "<flags id=\"%s\" size=\"%d\">\n",
  1708. type->id, type->reg_type_flags->size);
  1709. struct reg_data_type_flags_field *field;
  1710. field = type->reg_type_flags->fields;
  1711. while (field != NULL) {
  1712. xml_printf(&retval, tdesc, pos, size,
  1713. "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
  1714. field->name, field->bitfield->start, field->bitfield->end);
  1715. field = field->next;
  1716. }
  1717. xml_printf(&retval, tdesc, pos, size,
  1718. "</flags>\n");
  1719. }
  1720. return ERROR_OK;
  1721. }
  1722. /* Get a list of available target registers features. feature_list must
  1723. * be freed by caller.
  1724. */
  1725. static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
  1726. struct reg **reg_list, int reg_list_size)
  1727. {
  1728. int tbl_sz = 0;
  1729. /* Start with only one element */
  1730. *feature_list = calloc(1, sizeof(char *));
  1731. for (int i = 0; i < reg_list_size; i++) {
  1732. if (reg_list[i]->exist == false)
  1733. continue;
  1734. if (reg_list[i]->feature != NULL
  1735. && reg_list[i]->feature->name != NULL
  1736. && (strcmp(reg_list[i]->feature->name, ""))) {
  1737. /* We found a feature, check if the feature is already in the
  1738. * table. If not, allocate a new entry for the table and
  1739. * put the new feature in it.
  1740. */
  1741. for (int j = 0; j < (tbl_sz + 1); j++) {
  1742. if (!((*feature_list)[j])) {
  1743. (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
  1744. *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
  1745. (*feature_list)[tbl_sz] = NULL;
  1746. break;
  1747. } else {
  1748. if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
  1749. break;
  1750. }
  1751. }
  1752. }
  1753. }
  1754. if (feature_list_size)
  1755. *feature_list_size = tbl_sz;
  1756. return ERROR_OK;
  1757. }
  1758. static int gdb_generate_target_description(struct target *target, char **tdesc_out)
  1759. {
  1760. int retval = ERROR_OK;
  1761. struct reg **reg_list = NULL;
  1762. int reg_list_size;
  1763. char const **features = NULL;
  1764. int feature_list_size = 0;
  1765. char *tdesc = NULL;
  1766. int pos = 0;
  1767. int size = 0;
  1768. retval = target_get_gdb_reg_list(target, &reg_list,
  1769. &reg_list_size, REG_CLASS_ALL);
  1770. if (retval != ERROR_OK) {
  1771. LOG_ERROR("get register list failed");
  1772. retval = ERROR_FAIL;
  1773. goto error;
  1774. }
  1775. if (reg_list_size <= 0) {
  1776. LOG_ERROR("get register list failed");
  1777. retval = ERROR_FAIL;
  1778. goto error;
  1779. }
  1780. /* Get a list of available target registers features */
  1781. retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
  1782. if (retval != ERROR_OK) {
  1783. LOG_ERROR("Can't get the registers feature list");
  1784. retval = ERROR_FAIL;
  1785. goto error;
  1786. }
  1787. /* If we found some features associated with registers, create sections */
  1788. int current_feature = 0;
  1789. xml_printf(&retval, &tdesc, &pos, &size,
  1790. "<?xml version=\"1.0\"?>\n"
  1791. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
  1792. "<target version=\"1.0\">\n");
  1793. /* generate target description according to register list */
  1794. if (features != NULL) {
  1795. while (features[current_feature]) {
  1796. xml_printf(&retval, &tdesc, &pos, &size,
  1797. "<feature name=\"%s\">\n",
  1798. features[current_feature]);
  1799. int i;
  1800. for (i = 0; i < reg_list_size; i++) {
  1801. if (reg_list[i]->exist == false)
  1802. continue;
  1803. if (strcmp(reg_list[i]->feature->name, features[current_feature]))
  1804. continue;
  1805. const char *type_str;
  1806. if (reg_list[i]->reg_data_type != NULL) {
  1807. if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
  1808. /* generate <type... first, if there are architecture-defined types. */
  1809. gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
  1810. reg_list[i]->reg_data_type);
  1811. type_str = reg_list[i]->reg_data_type->id;
  1812. } else {
  1813. /* predefined type */
  1814. type_str = gdb_get_reg_type_name(
  1815. reg_list[i]->reg_data_type->type);
  1816. }
  1817. } else {
  1818. /* Default type is "int" */
  1819. type_str = "int";
  1820. }
  1821. xml_printf(&retval, &tdesc, &pos, &size,
  1822. "<reg name=\"%s\"", reg_list[i]->name);
  1823. xml_printf(&retval, &tdesc, &pos, &size,
  1824. " bitsize=\"%d\"", reg_list[i]->size);
  1825. xml_printf(&retval, &tdesc, &pos, &size,
  1826. " regnum=\"%d\"", reg_list[i]->number);
  1827. if (reg_list[i]->caller_save)
  1828. xml_printf(&retval, &tdesc, &pos, &size,
  1829. " save-restore=\"yes\"");
  1830. else
  1831. xml_printf(&retval, &tdesc, &pos, &size,
  1832. " save-restore=\"no\"");
  1833. xml_printf(&retval, &tdesc, &pos, &size,
  1834. " type=\"%s\"", type_str);
  1835. if (reg_list[i]->group != NULL)
  1836. xml_printf(&retval, &tdesc, &pos, &size,
  1837. " group=\"%s\"", reg_list[i]->group);
  1838. xml_printf(&retval, &tdesc, &pos, &size,
  1839. "/>\n");
  1840. }
  1841. xml_printf(&retval, &tdesc, &pos, &size,
  1842. "</feature>\n");
  1843. current_feature++;
  1844. }
  1845. }
  1846. xml_printf(&retval, &tdesc, &pos, &size,
  1847. "</target>\n");
  1848. error:
  1849. free(features);
  1850. free(reg_list);
  1851. if (retval == ERROR_OK)
  1852. *tdesc_out = tdesc;
  1853. else
  1854. free(tdesc);
  1855. return retval;
  1856. }
  1857. static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
  1858. char **chunk, int32_t offset, uint32_t length)
  1859. {
  1860. if (target_desc == NULL) {
  1861. LOG_ERROR("Unable to Generate Target Description");
  1862. return ERROR_FAIL;
  1863. }
  1864. char *tdesc = target_desc->tdesc;
  1865. uint32_t tdesc_length = target_desc->tdesc_length;
  1866. if (tdesc == NULL) {
  1867. int retval = gdb_generate_target_description(target, &tdesc);
  1868. if (retval != ERROR_OK) {
  1869. LOG_ERROR("Unable to Generate Target Description");
  1870. return ERROR_FAIL;
  1871. }
  1872. tdesc_length = strlen(tdesc);
  1873. }
  1874. char transfer_type;
  1875. if (length < (tdesc_length - offset))
  1876. transfer_type = 'm';
  1877. else
  1878. transfer_type = 'l';
  1879. *chunk = malloc(length + 2);
  1880. if (*chunk == NULL) {
  1881. LOG_ERROR("Unable to allocate memory");
  1882. return ERROR_FAIL;
  1883. }
  1884. (*chunk)[0] = transfer_type;
  1885. if (transfer_type == 'm') {
  1886. strncpy((*chunk) + 1, tdesc + offset, length);
  1887. (*chunk)[1 + length] = '\0';
  1888. } else {
  1889. strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
  1890. (*chunk)[1 + (tdesc_length - offset)] = '\0';
  1891. /* After gdb-server sends out last chunk, invalidate tdesc. */
  1892. free(tdesc);
  1893. tdesc = NULL;
  1894. tdesc_length = 0;
  1895. }
  1896. target_desc->tdesc = tdesc;
  1897. target_desc->tdesc_length = tdesc_length;
  1898. return ERROR_OK;
  1899. }
  1900. static int gdb_target_description_supported(struct target *target, int *supported)
  1901. {
  1902. int retval = ERROR_OK;
  1903. struct reg **reg_list = NULL;
  1904. int reg_list_size = 0;
  1905. char const **features = NULL;
  1906. int feature_list_size = 0;
  1907. retval = target_get_gdb_reg_list(target, &reg_list,
  1908. &reg_list_size, REG_CLASS_ALL);
  1909. if (retval != ERROR_OK) {
  1910. LOG_ERROR("get register list failed");
  1911. goto error;
  1912. }
  1913. if (reg_list_size <= 0) {
  1914. LOG_ERROR("get register list failed");
  1915. retval = ERROR_FAIL;
  1916. goto error;
  1917. }
  1918. /* Get a list of available target registers features */
  1919. retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
  1920. if (retval != ERROR_OK) {
  1921. LOG_ERROR("Can't get the registers feature list");
  1922. goto error;
  1923. }
  1924. if (supported) {
  1925. if (feature_list_size)
  1926. *supported = 1;
  1927. else
  1928. *supported = 0;
  1929. }
  1930. error:
  1931. free(features);
  1932. free(reg_list);
  1933. return retval;
  1934. }
  1935. static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
  1936. {
  1937. struct rtos *rtos = target->rtos;
  1938. int retval = ERROR_OK;
  1939. char *thread_list = NULL;
  1940. int pos = 0;
  1941. int size = 0;
  1942. xml_printf(&retval, &thread_list, &pos, &size,
  1943. "<?xml version=\"1.0\"?>\n"
  1944. "<threads>\n");
  1945. if (rtos != NULL) {
  1946. for (int i = 0; i < rtos->thread_count; i++) {
  1947. struct thread_detail *thread_detail = &rtos->thread_details[i];
  1948. if (!thread_detail->exists)
  1949. continue;
  1950. xml_printf(&retval, &thread_list, &pos, &size,
  1951. "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
  1952. if (thread_detail->thread_name_str != NULL)
  1953. xml_printf(&retval, &thread_list, &pos, &size,
  1954. "Name: %s", thread_detail->thread_name_str);
  1955. if (thread_detail->extra_info_str != NULL) {
  1956. if (thread_detail->thread_name_str != NULL)
  1957. xml_printf(&retval, &thread_list, &pos, &size,
  1958. ", ");
  1959. xml_printf(&retval, &thread_list, &pos, &size,
  1960. thread_detail->extra_info_str);
  1961. }
  1962. xml_printf(&retval, &thread_list, &pos, &size,
  1963. "</thread>\n");
  1964. }
  1965. }
  1966. xml_printf(&retval, &thread_list, &pos, &size,
  1967. "</threads>\n");
  1968. if (retval == ERROR_OK)
  1969. *thread_list_out = thread_list;
  1970. else
  1971. free(thread_list);
  1972. return retval;
  1973. }
  1974. static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
  1975. char **chunk, int32_t offset, uint32_t length)
  1976. {
  1977. if (*thread_list == NULL) {
  1978. int retval = gdb_generate_thread_list(target, thread_list);
  1979. if (retval != ERROR_OK) {
  1980. LOG_ERROR("Unable to Generate Thread List");
  1981. return ERROR_FAIL;
  1982. }
  1983. }
  1984. size_t thread_list_length = strlen(*thread_list);
  1985. char transfer_type;
  1986. length = MIN(length, thread_list_length - offset);
  1987. if (length < (thread_list_length - offset))
  1988. transfer_type = 'm';
  1989. else
  1990. transfer_type = 'l';
  1991. *chunk = malloc(length + 2);
  1992. if (*chunk == NULL) {
  1993. LOG_ERROR("Unable to allocate memory");
  1994. return ERROR_FAIL;
  1995. }
  1996. (*chunk)[0] = transfer_type;
  1997. strncpy((*chunk) + 1, (*thread_list) + offset, length);
  1998. (*chunk)[1 + length] = '\0';
  1999. /* After gdb-server sends out last chunk, invalidate thread list. */
  2000. if (transfer_type == 'l') {
  2001. free(*thread_list);
  2002. *thread_list = NULL;
  2003. }
  2004. return ERROR_OK;
  2005. }
  2006. static int gdb_query_packet(struct connection *connection,
  2007. char const *packet, int packet_size)
  2008. {
  2009. struct command_context *cmd_ctx = connection->cmd_ctx;
  2010. struct gdb_connection *gdb_connection = connection->priv;
  2011. struct target *target = get_target_from_connection(connection);
  2012. if (strncmp(packet, "qRcmd,", 6) == 0) {
  2013. if (packet_size > 6) {
  2014. char *cmd;
  2015. cmd = malloc((packet_size - 6) / 2 + 1);
  2016. size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
  2017. cmd[len] = 0;
  2018. /* We want to print all debug output to GDB connection */
  2019. log_add_callback(gdb_log_callback, connection);
  2020. target_call_timer_callbacks_now();
  2021. /* some commands need to know the GDB connection, make note of current
  2022. * GDB connection. */
  2023. current_gdb_connection = gdb_connection;
  2024. command_run_line(cmd_ctx, cmd);
  2025. current_gdb_connection = NULL;
  2026. target_call_timer_callbacks_now();
  2027. log_remove_callback(gdb_log_callback, connection);
  2028. free(cmd);
  2029. }
  2030. gdb_put_packet(connection, "OK", 2);
  2031. return ERROR_OK;
  2032. } else if (strncmp(packet, "qCRC:", 5) == 0) {
  2033. if (packet_size > 5) {
  2034. int retval;
  2035. char gdb_reply[10];
  2036. char *separator;
  2037. uint32_t checksum;
  2038. uint32_t addr = 0;
  2039. uint32_t len = 0;
  2040. /* skip command character */
  2041. packet += 5;
  2042. addr = strtoul(packet, &separator, 16);
  2043. if (*separator != ',') {
  2044. LOG_ERROR("incomplete read memory packet received, dropping connection");
  2045. return ERROR_SERVER_REMOTE_CLOSED;
  2046. }
  2047. len = strtoul(separator + 1, NULL, 16);
  2048. retval = target_checksum_memory(target, addr, len, &checksum);
  2049. if (retval == ERROR_OK) {
  2050. snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
  2051. gdb_put_packet(connection, gdb_reply, 9);
  2052. } else {
  2053. retval = gdb_error(connection, retval);
  2054. if (retval != ERROR_OK)
  2055. return retval;
  2056. }
  2057. return ERROR_OK;
  2058. }
  2059. } else if (strncmp(packet, "qSupported", 10) == 0) {
  2060. /* we currently support packet size and qXfer:memory-map:read (if enabled)
  2061. * qXfer:features:read is supported for some targets */
  2062. int retval = ERROR_OK;
  2063. char *buffer = NULL;
  2064. int pos = 0;
  2065. int size = 0;
  2066. int gdb_target_desc_supported = 0;
  2067. /* we need to test that the target supports target descriptions */
  2068. retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
  2069. if (retval != ERROR_OK) {
  2070. LOG_INFO("Failed detecting Target Description Support, disabling");
  2071. gdb_target_desc_supported = 0;
  2072. }
  2073. /* support may be disabled globally */
  2074. if (gdb_use_target_description == 0) {
  2075. if (gdb_target_desc_supported)
  2076. LOG_WARNING("Target Descriptions Supported, but disabled");
  2077. gdb_target_desc_supported = 0;
  2078. }
  2079. xml_printf(&retval,
  2080. &buffer,
  2081. &pos,
  2082. &size,
  2083. "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+",
  2084. (GDB_BUFFER_SIZE - 1),
  2085. ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
  2086. (gdb_target_desc_supported == 1) ? '+' : '-');
  2087. if (retval != ERROR_OK) {
  2088. gdb_send_error(connection, 01);
  2089. return ERROR_OK;
  2090. }
  2091. gdb_put_packet(connection, buffer, strlen(buffer));
  2092. free(buffer);
  2093. return ERROR_OK;
  2094. } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
  2095. && (flash_get_bank_count() > 0))
  2096. return gdb_memory_map(connection, packet, packet_size);
  2097. else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
  2098. char *xml = NULL;
  2099. int retval = ERROR_OK;
  2100. int offset;
  2101. unsigned int length;
  2102. /* skip command character */
  2103. packet += 20;
  2104. if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
  2105. gdb_send_error(connection, 01);
  2106. return ERROR_OK;
  2107. }
  2108. /* Target should prepare correct target description for annex.
  2109. * The first character of returned xml is 'm' or 'l'. 'm' for
  2110. * there are *more* chunks to transfer. 'l' for it is the *last*
  2111. * chunk of target description.
  2112. */
  2113. retval = gdb_get_target_description_chunk(target, &gdb_connection->target_desc,
  2114. &xml, offset, length);
  2115. if (retval != ERROR_OK) {
  2116. gdb_error(connection, retval);
  2117. return retval;
  2118. }
  2119. gdb_put_packet(connection, xml, strlen(xml));
  2120. free(xml);
  2121. return ERROR_OK;
  2122. } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
  2123. char *xml = NULL;
  2124. int retval = ERROR_OK;
  2125. int offset;
  2126. unsigned int length;
  2127. /* skip command character */
  2128. packet += 19;
  2129. if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
  2130. gdb_send_error(connection, 01);
  2131. return ERROR_OK;
  2132. }
  2133. /* Target should prepare correct thread list for annex.
  2134. * The first character of returned xml is 'm' or 'l'. 'm' for
  2135. * there are *more* chunks to transfer. 'l' for it is the *last*
  2136. * chunk of target description.
  2137. */
  2138. retval = gdb_get_thread_list_chunk(target, &gdb_connection->thread_list,
  2139. &xml, offset, length);
  2140. if (retval != ERROR_OK) {
  2141. gdb_error(connection, retval);
  2142. return retval;
  2143. }
  2144. gdb_put_packet(connection, xml, strlen(xml));
  2145. free(xml);
  2146. return ERROR_OK;
  2147. } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
  2148. gdb_connection->noack_mode = 1;
  2149. gdb_put_packet(connection, "OK", 2);
  2150. return ERROR_OK;
  2151. }
  2152. gdb_put_packet(connection, "", 0);
  2153. return ERROR_OK;
  2154. }
  2155. static int gdb_v_packet(struct connection *connection,
  2156. char const *packet, int packet_size)
  2157. {
  2158. struct gdb_connection *gdb_connection = connection->priv;
  2159. struct gdb_service *gdb_service = connection->service->priv;
  2160. int result;
  2161. /* if flash programming disabled - send a empty reply */
  2162. if (gdb_flash_program == 0) {
  2163. gdb_put_packet(connection, "", 0);
  2164. return ERROR_OK;
  2165. }
  2166. if (strncmp(packet, "vFlashErase:", 12) == 0) {
  2167. unsigned long addr;
  2168. unsigned long length;
  2169. char const *parse = packet + 12;
  2170. if (*parse == '\0') {
  2171. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  2172. return ERROR_SERVER_REMOTE_CLOSED;
  2173. }
  2174. addr = strtoul(parse, (char **)&parse, 16);
  2175. if (*(parse++) != ',' || *parse == '\0') {
  2176. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  2177. return ERROR_SERVER_REMOTE_CLOSED;
  2178. }
  2179. length = strtoul(parse, (char **)&parse, 16);
  2180. if (*parse != '\0') {
  2181. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  2182. return ERROR_SERVER_REMOTE_CLOSED;
  2183. }
  2184. /* assume all sectors need erasing - stops any problems
  2185. * when flash_write is called multiple times */
  2186. flash_set_dirty();
  2187. /* perform any target specific operations before the erase */
  2188. target_call_event_callbacks(gdb_service->target,
  2189. TARGET_EVENT_GDB_FLASH_ERASE_START);
  2190. /* vFlashErase:addr,length messages require region start and
  2191. * end to be "block" aligned ... if padding is ever needed,
  2192. * GDB will have become dangerously confused.
  2193. */
  2194. result = flash_erase_address_range(gdb_service->target,
  2195. false, addr, length);
  2196. /* perform any target specific operations after the erase */
  2197. target_call_event_callbacks(gdb_service->target,
  2198. TARGET_EVENT_GDB_FLASH_ERASE_END);
  2199. /* perform erase */
  2200. if (result != ERROR_OK) {
  2201. /* GDB doesn't evaluate the actual error number returned,
  2202. * treat a failed erase as an I/O error
  2203. */
  2204. gdb_send_error(connection, EIO);
  2205. LOG_ERROR("flash_erase returned %i", result);
  2206. } else
  2207. gdb_put_packet(connection, "OK", 2);
  2208. return ERROR_OK;
  2209. }
  2210. if (strncmp(packet, "vFlashWrite:", 12) == 0) {
  2211. int retval;
  2212. unsigned long addr;
  2213. unsigned long length;
  2214. char const *parse = packet + 12;
  2215. if (*parse == '\0') {
  2216. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  2217. return ERROR_SERVER_REMOTE_CLOSED;
  2218. }
  2219. addr = strtoul(parse, (char **)&parse, 16);
  2220. if (*(parse++) != ':') {
  2221. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  2222. return ERROR_SERVER_REMOTE_CLOSED;
  2223. }
  2224. length = packet_size - (parse - packet);
  2225. /* create a new image if there isn't already one */
  2226. if (gdb_connection->vflash_image == NULL) {
  2227. gdb_connection->vflash_image = malloc(sizeof(struct image));
  2228. image_open(gdb_connection->vflash_image, "", "build");
  2229. }
  2230. /* create new section with content from packet buffer */
  2231. retval = image_add_section(gdb_connection->vflash_image,
  2232. addr, length, 0x0, (uint8_t const *)parse);
  2233. if (retval != ERROR_OK)
  2234. return retval;
  2235. gdb_put_packet(connection, "OK", 2);
  2236. return ERROR_OK;
  2237. }
  2238. if (strncmp(packet, "vFlashDone", 10) == 0) {
  2239. uint32_t written;
  2240. /* process the flashing buffer. No need to erase as GDB
  2241. * always issues a vFlashErase first. */
  2242. target_call_event_callbacks(gdb_service->target,
  2243. TARGET_EVENT_GDB_FLASH_WRITE_START);
  2244. result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
  2245. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
  2246. if (result != ERROR_OK) {
  2247. if (result == ERROR_FLASH_DST_OUT_OF_BANK)
  2248. gdb_put_packet(connection, "E.memtype", 9);
  2249. else
  2250. gdb_send_error(connection, EIO);
  2251. } else {
  2252. LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
  2253. gdb_put_packet(connection, "OK", 2);
  2254. }
  2255. image_close(gdb_connection->vflash_image);
  2256. free(gdb_connection->vflash_image);
  2257. gdb_connection->vflash_image = NULL;
  2258. return ERROR_OK;
  2259. }
  2260. gdb_put_packet(connection, "", 0);
  2261. return ERROR_OK;
  2262. }
  2263. static int gdb_detach(struct connection *connection)
  2264. {
  2265. struct gdb_service *gdb_service = connection->service->priv;
  2266. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
  2267. return gdb_put_packet(connection, "OK", 2);
  2268. }
  2269. /* The format of 'F' response packet is
  2270. * Fretcode,errno,Ctrl-C flag;call-specific attachment
  2271. */
  2272. static int gdb_fileio_response_packet(struct connection *connection,
  2273. char const *packet, int packet_size)
  2274. {
  2275. struct target *target = get_target_from_connection(connection);
  2276. char *separator;
  2277. char *parsing_point;
  2278. int fileio_retcode = strtoul(packet + 1, &separator, 16);
  2279. int fileio_errno = 0;
  2280. bool fileio_ctrl_c = false;
  2281. int retval;
  2282. LOG_DEBUG("-");
  2283. if (*separator == ',') {
  2284. parsing_point = separator + 1;
  2285. fileio_errno = strtoul(parsing_point, &separator, 16);
  2286. if (*separator == ',') {
  2287. if (*(separator + 1) == 'C') {
  2288. /* TODO: process ctrl-c */
  2289. fileio_ctrl_c = true;
  2290. }
  2291. }
  2292. }
  2293. LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
  2294. fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
  2295. retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
  2296. if (retval != ERROR_OK)
  2297. return ERROR_FAIL;
  2298. /* After File-I/O ends, keep continue or step */
  2299. if (gdb_running_type == 'c')
  2300. retval = target_resume(target, 1, 0x0, 0, 0);
  2301. else if (gdb_running_type == 's')
  2302. retval = target_step(target, 1, 0x0, 0);
  2303. else
  2304. retval = ERROR_FAIL;
  2305. if (retval != ERROR_OK)
  2306. return ERROR_FAIL;
  2307. return ERROR_OK;
  2308. }
  2309. static void gdb_log_callback(void *priv, const char *file, unsigned line,
  2310. const char *function, const char *string)
  2311. {
  2312. struct connection *connection = priv;
  2313. struct gdb_connection *gdb_con = connection->priv;
  2314. if (gdb_con->busy) {
  2315. /* do not reply this using the O packet */
  2316. return;
  2317. }
  2318. gdb_output_con(connection, string);
  2319. }
  2320. static void gdb_sig_halted(struct connection *connection)
  2321. {
  2322. char sig_reply[4];
  2323. snprintf(sig_reply, 4, "T%2.2x", 2);
  2324. gdb_put_packet(connection, sig_reply, 3);
  2325. }
  2326. static int gdb_input_inner(struct connection *connection)
  2327. {
  2328. /* Do not allocate this on the stack */
  2329. static char gdb_packet_buffer[GDB_BUFFER_SIZE];
  2330. struct gdb_service *gdb_service = connection->service->priv;
  2331. struct target *target = gdb_service->target;
  2332. char const *packet = gdb_packet_buffer;
  2333. int packet_size;
  2334. int retval;
  2335. struct gdb_connection *gdb_con = connection->priv;
  2336. static int extended_protocol;
  2337. /* drain input buffer. If one of the packets fail, then an error
  2338. * packet is replied, if applicable.
  2339. *
  2340. * This loop will terminate and the error code is returned.
  2341. *
  2342. * The calling fn will check if this error is something that
  2343. * can be recovered from, or if the connection must be closed.
  2344. *
  2345. * If the error is recoverable, this fn is called again to
  2346. * drain the rest of the buffer.
  2347. */
  2348. do {
  2349. packet_size = GDB_BUFFER_SIZE-1;
  2350. retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
  2351. if (retval != ERROR_OK)
  2352. return retval;
  2353. /* terminate with zero */
  2354. gdb_packet_buffer[packet_size] = '\0';
  2355. if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
  2356. if (packet[0] == 'X') {
  2357. /* binary packets spew junk into the debug log stream */
  2358. char buf[50];
  2359. int x;
  2360. for (x = 0; (x < 49) && (packet[x] != ':'); x++)
  2361. buf[x] = packet[x];
  2362. buf[x] = 0;
  2363. LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
  2364. } else
  2365. LOG_DEBUG("received packet: '%s'", packet);
  2366. }
  2367. if (packet_size > 0) {
  2368. retval = ERROR_OK;
  2369. switch (packet[0]) {
  2370. case 'T': /* Is thread alive? */
  2371. gdb_thread_packet(connection, packet, packet_size);
  2372. break;
  2373. case 'H': /* Set current thread ( 'c' for step and continue,
  2374. * 'g' for all other operations ) */
  2375. gdb_thread_packet(connection, packet, packet_size);
  2376. break;
  2377. case 'q':
  2378. case 'Q':
  2379. retval = gdb_thread_packet(connection, packet, packet_size);
  2380. if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
  2381. retval = gdb_query_packet(connection, packet, packet_size);
  2382. break;
  2383. case 'g':
  2384. retval = gdb_get_registers_packet(connection, packet, packet_size);
  2385. break;
  2386. case 'G':
  2387. retval = gdb_set_registers_packet(connection, packet, packet_size);
  2388. break;
  2389. case 'p':
  2390. retval = gdb_get_register_packet(connection, packet, packet_size);
  2391. break;
  2392. case 'P':
  2393. retval = gdb_set_register_packet(connection, packet, packet_size);
  2394. break;
  2395. case 'm':
  2396. retval = gdb_read_memory_packet(connection, packet, packet_size);
  2397. break;
  2398. case 'M':
  2399. retval = gdb_write_memory_packet(connection, packet, packet_size);
  2400. break;
  2401. case 'z':
  2402. case 'Z':
  2403. retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
  2404. break;
  2405. case '?':
  2406. gdb_last_signal_packet(connection, packet, packet_size);
  2407. break;
  2408. case 'c':
  2409. case 's':
  2410. {
  2411. gdb_thread_packet(connection, packet, packet_size);
  2412. log_add_callback(gdb_log_callback, connection);
  2413. if (gdb_con->mem_write_error) {
  2414. LOG_ERROR("Memory write failure!");
  2415. /* now that we have reported the memory write error,
  2416. * we can clear the condition */
  2417. gdb_con->mem_write_error = false;
  2418. }
  2419. bool nostep = false;
  2420. bool already_running = false;
  2421. if (target->state == TARGET_RUNNING) {
  2422. LOG_WARNING("WARNING! The target is already running. "
  2423. "All changes GDB did to registers will be discarded! "
  2424. "Waiting for target to halt.");
  2425. already_running = true;
  2426. } else if (target->state != TARGET_HALTED) {
  2427. LOG_WARNING("The target is not in the halted nor running stated, " \
  2428. "stepi/continue ignored.");
  2429. nostep = true;
  2430. } else if ((packet[0] == 's') && gdb_con->sync) {
  2431. /* Hmm..... when you issue a continue in GDB, then a "stepi" is
  2432. * sent by GDB first to OpenOCD, thus defeating the check to
  2433. * make only the single stepping have the sync feature...
  2434. */
  2435. nostep = true;
  2436. LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
  2437. "from the target.");
  2438. }
  2439. gdb_con->sync = false;
  2440. if (!already_running && nostep) {
  2441. /* Either the target isn't in the halted state, then we can't
  2442. * step/continue. This might be early setup, etc.
  2443. *
  2444. * Or we want to allow GDB to pick up a fresh set of
  2445. * register values without modifying the target state.
  2446. *
  2447. */
  2448. gdb_sig_halted(connection);
  2449. /* stop forwarding log packets! */
  2450. log_remove_callback(gdb_log_callback, connection);
  2451. } else {
  2452. /* We're running/stepping, in which case we can
  2453. * forward log output until the target is halted
  2454. */
  2455. gdb_con->frontend_state = TARGET_RUNNING;
  2456. target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
  2457. if (!already_running) {
  2458. /* Here we don't want packet processing to stop even if this fails,
  2459. * so we use a local variable instead of retval. */
  2460. retval = gdb_step_continue_packet(connection, packet, packet_size);
  2461. if (retval != ERROR_OK) {
  2462. /* we'll never receive a halted
  2463. * condition... issue a false one..
  2464. */
  2465. gdb_frontend_halted(target, connection);
  2466. }
  2467. }
  2468. }
  2469. }
  2470. break;
  2471. case 'v':
  2472. retval = gdb_v_packet(connection, packet, packet_size);
  2473. break;
  2474. case 'D':
  2475. retval = gdb_detach(connection);
  2476. extended_protocol = 0;
  2477. break;
  2478. case 'X':
  2479. retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
  2480. if (retval != ERROR_OK)
  2481. return retval;
  2482. break;
  2483. case 'k':
  2484. if (extended_protocol != 0) {
  2485. gdb_con->attached = false;
  2486. break;
  2487. }
  2488. gdb_put_packet(connection, "OK", 2);
  2489. return ERROR_SERVER_REMOTE_CLOSED;
  2490. case '!':
  2491. /* handle extended remote protocol */
  2492. extended_protocol = 1;
  2493. gdb_put_packet(connection, "OK", 2);
  2494. break;
  2495. case 'R':
  2496. /* handle extended restart packet */
  2497. breakpoint_clear_target(gdb_service->target);
  2498. watchpoint_clear_target(gdb_service->target);
  2499. command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
  2500. target_name(target));
  2501. /* set connection as attached after reset */
  2502. gdb_con->attached = true;
  2503. /* info rtos parts */
  2504. gdb_thread_packet(connection, packet, packet_size);
  2505. break;
  2506. case 'j':
  2507. /* packet supported only by smp target i.e cortex_a.c*/
  2508. /* handle smp packet replying coreid played to gbd */
  2509. gdb_read_smp_packet(connection, packet, packet_size);
  2510. break;
  2511. case 'J':
  2512. /* packet supported only by smp target i.e cortex_a.c */
  2513. /* handle smp packet setting coreid to be played at next
  2514. * resume to gdb */
  2515. gdb_write_smp_packet(connection, packet, packet_size);
  2516. break;
  2517. case 'F':
  2518. /* File-I/O extension */
  2519. /* After gdb uses host-side syscall to complete target file
  2520. * I/O, gdb sends host-side syscall return value to target
  2521. * by 'F' packet.
  2522. * The format of 'F' response packet is
  2523. * Fretcode,errno,Ctrl-C flag;call-specific attachment
  2524. */
  2525. gdb_con->frontend_state = TARGET_RUNNING;
  2526. log_add_callback(gdb_log_callback, connection);
  2527. gdb_fileio_response_packet(connection, packet, packet_size);
  2528. break;
  2529. default:
  2530. /* ignore unknown packets */
  2531. LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
  2532. gdb_put_packet(connection, NULL, 0);
  2533. break;
  2534. }
  2535. /* if a packet handler returned an error, exit input loop */
  2536. if (retval != ERROR_OK)
  2537. return retval;
  2538. }
  2539. if (gdb_con->ctrl_c) {
  2540. if (target->state == TARGET_RUNNING) {
  2541. retval = target_halt(target);
  2542. if (retval != ERROR_OK)
  2543. target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
  2544. gdb_con->ctrl_c = 0;
  2545. } else {
  2546. LOG_INFO("The target is not running when halt was requested, stopping GDB.");
  2547. target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
  2548. }
  2549. }
  2550. } while (gdb_con->buf_cnt > 0);
  2551. return ERROR_OK;
  2552. }
  2553. static int gdb_input(struct connection *connection)
  2554. {
  2555. int retval = gdb_input_inner(connection);
  2556. struct gdb_connection *gdb_con = connection->priv;
  2557. if (retval == ERROR_SERVER_REMOTE_CLOSED)
  2558. return retval;
  2559. /* logging does not propagate the error, yet can set the gdb_con->closed flag */
  2560. if (gdb_con->closed)
  2561. return ERROR_SERVER_REMOTE_CLOSED;
  2562. /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
  2563. return ERROR_OK;
  2564. }
  2565. static int gdb_target_start(struct target *target, const char *port)
  2566. {
  2567. struct gdb_service *gdb_service;
  2568. int ret;
  2569. gdb_service = malloc(sizeof(struct gdb_service));
  2570. if (NULL == gdb_service)
  2571. return -ENOMEM;
  2572. gdb_service->target = target;
  2573. gdb_service->core[0] = -1;
  2574. gdb_service->core[1] = -1;
  2575. target->gdb_service = gdb_service;
  2576. ret = add_service("gdb",
  2577. port, 1, &gdb_new_connection, &gdb_input,
  2578. &gdb_connection_closed, gdb_service);
  2579. /* initialialize all targets gdb service with the same pointer */
  2580. {
  2581. struct target_list *head;
  2582. struct target *curr;
  2583. head = target->head;
  2584. while (head != (struct target_list *)NULL) {
  2585. curr = head->target;
  2586. if (curr != target)
  2587. curr->gdb_service = gdb_service;
  2588. head = head->next;
  2589. }
  2590. }
  2591. return ret;
  2592. }
  2593. static int gdb_target_add_one(struct target *target)
  2594. {
  2595. if (strcmp(gdb_port, "disabled") == 0) {
  2596. LOG_INFO("gdb port disabled");
  2597. return ERROR_OK;
  2598. }
  2599. /* one gdb instance per smp list */
  2600. if ((target->smp) && (target->gdb_service))
  2601. return ERROR_OK;
  2602. int retval = gdb_target_start(target, gdb_port_next);
  2603. if (retval == ERROR_OK) {
  2604. long portnumber;
  2605. /* If we can parse the port number
  2606. * then we increment the port number for the next target.
  2607. */
  2608. char *end;
  2609. portnumber = strtol(gdb_port_next, &end, 0);
  2610. if (!*end) {
  2611. if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
  2612. free(gdb_port_next);
  2613. gdb_port_next = alloc_printf("%d", portnumber+1);
  2614. }
  2615. }
  2616. }
  2617. return retval;
  2618. }
  2619. int gdb_target_add_all(struct target *target)
  2620. {
  2621. if (strcmp(gdb_port, "disabled") == 0) {
  2622. LOG_INFO("gdb server disabled");
  2623. return ERROR_OK;
  2624. }
  2625. if (NULL == target) {
  2626. LOG_WARNING("gdb services need one or more targets defined");
  2627. return ERROR_OK;
  2628. }
  2629. while (NULL != target) {
  2630. int retval = gdb_target_add_one(target);
  2631. if (ERROR_OK != retval)
  2632. return retval;
  2633. target = target->next;
  2634. }
  2635. return ERROR_OK;
  2636. }
  2637. COMMAND_HANDLER(handle_gdb_sync_command)
  2638. {
  2639. if (CMD_ARGC != 0)
  2640. return ERROR_COMMAND_SYNTAX_ERROR;
  2641. if (current_gdb_connection == NULL) {
  2642. command_print(CMD_CTX,
  2643. "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
  2644. return ERROR_FAIL;
  2645. }
  2646. current_gdb_connection->sync = true;
  2647. return ERROR_OK;
  2648. }
  2649. /* daemon configuration command gdb_port */
  2650. COMMAND_HANDLER(handle_gdb_port_command)
  2651. {
  2652. int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
  2653. if (ERROR_OK == retval) {
  2654. free(gdb_port_next);
  2655. gdb_port_next = strdup(gdb_port);
  2656. }
  2657. return retval;
  2658. }
  2659. COMMAND_HANDLER(handle_gdb_memory_map_command)
  2660. {
  2661. if (CMD_ARGC != 1)
  2662. return ERROR_COMMAND_SYNTAX_ERROR;
  2663. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
  2664. return ERROR_OK;
  2665. }
  2666. COMMAND_HANDLER(handle_gdb_flash_program_command)
  2667. {
  2668. if (CMD_ARGC != 1)
  2669. return ERROR_COMMAND_SYNTAX_ERROR;
  2670. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
  2671. return ERROR_OK;
  2672. }
  2673. COMMAND_HANDLER(handle_gdb_report_data_abort_command)
  2674. {
  2675. if (CMD_ARGC != 1)
  2676. return ERROR_COMMAND_SYNTAX_ERROR;
  2677. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
  2678. return ERROR_OK;
  2679. }
  2680. /* gdb_breakpoint_override */
  2681. COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
  2682. {
  2683. if (CMD_ARGC == 0) {
  2684. /* nothing */
  2685. } else if (CMD_ARGC == 1) {
  2686. gdb_breakpoint_override = 1;
  2687. if (strcmp(CMD_ARGV[0], "hard") == 0)
  2688. gdb_breakpoint_override_type = BKPT_HARD;
  2689. else if (strcmp(CMD_ARGV[0], "soft") == 0)
  2690. gdb_breakpoint_override_type = BKPT_SOFT;
  2691. else if (strcmp(CMD_ARGV[0], "disable") == 0)
  2692. gdb_breakpoint_override = 0;
  2693. } else
  2694. return ERROR_COMMAND_SYNTAX_ERROR;
  2695. if (gdb_breakpoint_override)
  2696. LOG_USER("force %s breakpoints",
  2697. (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
  2698. else
  2699. LOG_USER("breakpoint type is not overridden");
  2700. return ERROR_OK;
  2701. }
  2702. COMMAND_HANDLER(handle_gdb_target_description_command)
  2703. {
  2704. if (CMD_ARGC != 1)
  2705. return ERROR_COMMAND_SYNTAX_ERROR;
  2706. COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_target_description);
  2707. return ERROR_OK;
  2708. }
  2709. COMMAND_HANDLER(handle_gdb_save_tdesc_command)
  2710. {
  2711. char *tdesc;
  2712. uint32_t tdesc_length;
  2713. struct target *target = get_current_target(CMD_CTX);
  2714. int retval = gdb_generate_target_description(target, &tdesc);
  2715. if (retval != ERROR_OK) {
  2716. LOG_ERROR("Unable to Generate Target Description");
  2717. return ERROR_FAIL;
  2718. }
  2719. tdesc_length = strlen(tdesc);
  2720. struct fileio *fileio;
  2721. size_t size_written;
  2722. char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
  2723. if (tdesc_filename == NULL) {
  2724. retval = ERROR_FAIL;
  2725. goto out;
  2726. }
  2727. retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
  2728. if (retval != ERROR_OK) {
  2729. LOG_ERROR("Can't open %s for writing", tdesc_filename);
  2730. goto out;
  2731. }
  2732. retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
  2733. fileio_close(fileio);
  2734. if (retval != ERROR_OK)
  2735. LOG_ERROR("Error while writing the tdesc file");
  2736. out:
  2737. free(tdesc_filename);
  2738. free(tdesc);
  2739. return retval;
  2740. }
  2741. static const struct command_registration gdb_command_handlers[] = {
  2742. {
  2743. .name = "gdb_sync",
  2744. .handler = handle_gdb_sync_command,
  2745. .mode = COMMAND_ANY,
  2746. .help = "next stepi will return immediately allowing "
  2747. "GDB to fetch register state without affecting "
  2748. "target state",
  2749. .usage = ""
  2750. },
  2751. {
  2752. .name = "gdb_port",
  2753. .handler = handle_gdb_port_command,
  2754. .mode = COMMAND_ANY,
  2755. .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
  2756. "server listens for the next port number after the "
  2757. "base port number specified. "
  2758. "No arguments reports GDB port. \"pipe\" means listen to stdin "
  2759. "output to stdout, an integer is base port number, \"disabled\" disables "
  2760. "port. Any other string is are interpreted as named pipe to listen to. "
  2761. "Output pipe is the same name as input pipe, but with 'o' appended.",
  2762. .usage = "[port_num]",
  2763. },
  2764. {
  2765. .name = "gdb_memory_map",
  2766. .handler = handle_gdb_memory_map_command,
  2767. .mode = COMMAND_CONFIG,
  2768. .help = "enable or disable memory map",
  2769. .usage = "('enable'|'disable')"
  2770. },
  2771. {
  2772. .name = "gdb_flash_program",
  2773. .handler = handle_gdb_flash_program_command,
  2774. .mode = COMMAND_CONFIG,
  2775. .help = "enable or disable flash program",
  2776. .usage = "('enable'|'disable')"
  2777. },
  2778. {
  2779. .name = "gdb_report_data_abort",
  2780. .handler = handle_gdb_report_data_abort_command,
  2781. .mode = COMMAND_CONFIG,
  2782. .help = "enable or disable reporting data aborts",
  2783. .usage = "('enable'|'disable')"
  2784. },
  2785. {
  2786. .name = "gdb_breakpoint_override",
  2787. .handler = handle_gdb_breakpoint_override_command,
  2788. .mode = COMMAND_ANY,
  2789. .help = "Display or specify type of breakpoint "
  2790. "to be used by gdb 'break' commands.",
  2791. .usage = "('hard'|'soft'|'disable')"
  2792. },
  2793. {
  2794. .name = "gdb_target_description",
  2795. .handler = handle_gdb_target_description_command,
  2796. .mode = COMMAND_CONFIG,
  2797. .help = "enable or disable target description",
  2798. .usage = "('enable'|'disable')"
  2799. },
  2800. {
  2801. .name = "gdb_save_tdesc",
  2802. .handler = handle_gdb_save_tdesc_command,
  2803. .mode = COMMAND_EXEC,
  2804. .help = "Save the target description file",
  2805. },
  2806. COMMAND_REGISTRATION_DONE
  2807. };
  2808. int gdb_register_commands(struct command_context *cmd_ctx)
  2809. {
  2810. gdb_port = strdup("3333");
  2811. gdb_port_next = strdup("3333");
  2812. return register_commands(cmd_ctx, NULL, gdb_command_handlers);
  2813. }