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.
 
 
 
 
 
 

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