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.
 
 
 
 
 
 

2417 lines
58 KiB

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