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.
 
 
 
 
 
 

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