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.
 
 
 
 
 
 

2464 lines
66 KiB

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