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.
 
 
 
 
 
 

2397 lines
57 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 coninuing 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. return ERROR_OK;
  713. }
  714. int gdb_connection_closed(connection_t *connection)
  715. {
  716. gdb_service_t *gdb_service = connection->service->priv;
  717. gdb_connection_t *gdb_connection = connection->priv;
  718. gdb_actual_connections--;
  719. /* see if an image built with vFlash commands is left */
  720. if (gdb_connection->vflash_image)
  721. {
  722. image_close(gdb_connection->vflash_image);
  723. free(gdb_connection->vflash_image);
  724. gdb_connection->vflash_image = NULL;
  725. }
  726. /* if this connection registered a debug-message receiver delete it */
  727. delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
  728. if (connection->priv)
  729. {
  730. free(connection->priv);
  731. connection->priv = NULL;
  732. }
  733. else
  734. {
  735. LOG_ERROR("BUG: connection->priv == NULL");
  736. }
  737. target_unregister_event_callback(gdb_target_callback_event_handler, connection);
  738. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
  739. log_remove_callback(gdb_log_callback, connection);
  740. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
  741. return ERROR_OK;
  742. }
  743. void gdb_send_error(connection_t *connection, uint8_t the_error)
  744. {
  745. char err[4];
  746. snprintf(err, 4, "E%2.2X", the_error );
  747. gdb_put_packet(connection, err, 3);
  748. }
  749. int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
  750. {
  751. char sig_reply[4];
  752. int signal;
  753. signal = gdb_last_signal(target);
  754. snprintf(sig_reply, 4, "S%2.2x", signal);
  755. gdb_put_packet(connection, sig_reply, 3);
  756. return ERROR_OK;
  757. }
  758. static int gdb_reg_pos(target_t *target, int pos, int len)
  759. {
  760. if (target->endianness == TARGET_LITTLE_ENDIAN)
  761. return pos;
  762. else
  763. return len - 1 - pos;
  764. }
  765. /* Convert register to string of bytes. NB! The # of bits in the
  766. * register might be non-divisible by 8(a byte), in which
  767. * case an entire byte is shown.
  768. *
  769. * NB! the format on the wire is the target endianess
  770. *
  771. * The format of reg->value is little endian
  772. *
  773. */
  774. void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
  775. {
  776. int i;
  777. uint8_t *buf;
  778. int buf_len;
  779. buf = reg->value;
  780. buf_len = CEIL(reg->size, 8);
  781. for (i = 0; i < buf_len; i++)
  782. {
  783. int j = gdb_reg_pos(target, i, buf_len);
  784. tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
  785. tstr[i*2+1] = DIGITS[buf[j]&0xf];
  786. }
  787. }
  788. static int hextoint(char c)
  789. {
  790. if (c>='0'&&c<='9')
  791. {
  792. return c-'0';
  793. }
  794. c=toupper(c);
  795. if (c>='A'&&c<='F')
  796. {
  797. return c-'A'+10;
  798. }
  799. LOG_ERROR("BUG: invalid register value %08x", c);
  800. return 0;
  801. }
  802. /* copy over in register buffer */
  803. void gdb_target_to_reg(target_t *target, char *tstr, int str_len, uint8_t *bin)
  804. {
  805. if (str_len % 2)
  806. {
  807. LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
  808. exit(-1);
  809. }
  810. int i;
  811. for (i = 0; i < str_len; i+=2)
  812. {
  813. uint8_t t = hextoint(tstr[i])<<4;
  814. t |= hextoint(tstr[i+1]);
  815. int j = gdb_reg_pos(target, i/2, str_len/2);
  816. bin[j] = t;
  817. }
  818. }
  819. int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
  820. {
  821. reg_t **reg_list;
  822. int reg_list_size;
  823. int retval;
  824. int reg_packet_size = 0;
  825. char *reg_packet;
  826. char *reg_packet_p;
  827. int i;
  828. #ifdef _DEBUG_GDB_IO_
  829. LOG_DEBUG("-");
  830. #endif
  831. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  832. {
  833. return gdb_error(connection, retval);
  834. }
  835. for (i = 0; i < reg_list_size; i++)
  836. {
  837. reg_packet_size += reg_list[i]->size;
  838. }
  839. reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
  840. reg_packet_p = reg_packet;
  841. for (i = 0; i < reg_list_size; i++)
  842. {
  843. gdb_str_to_target(target, reg_packet_p, reg_list[i]);
  844. reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
  845. }
  846. #ifdef _DEBUG_GDB_IO_
  847. {
  848. char *reg_packet_p;
  849. reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
  850. LOG_DEBUG("reg_packet: %s", reg_packet_p);
  851. free(reg_packet_p);
  852. }
  853. #endif
  854. gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
  855. free(reg_packet);
  856. free(reg_list);
  857. return ERROR_OK;
  858. }
  859. int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  860. {
  861. int i;
  862. reg_t **reg_list;
  863. int reg_list_size;
  864. int retval;
  865. char *packet_p;
  866. #ifdef _DEBUG_GDB_IO_
  867. LOG_DEBUG("-");
  868. #endif
  869. /* skip command character */
  870. packet++;
  871. packet_size--;
  872. if (packet_size % 2)
  873. {
  874. LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
  875. return ERROR_SERVER_REMOTE_CLOSED;
  876. }
  877. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  878. {
  879. return gdb_error(connection, retval);
  880. }
  881. packet_p = packet;
  882. for (i = 0; i < reg_list_size; i++)
  883. {
  884. uint8_t *bin_buf;
  885. int chars = (CEIL(reg_list[i]->size, 8) * 2);
  886. if (packet_p + chars > packet + packet_size)
  887. {
  888. LOG_ERROR("BUG: register packet is too small for registers");
  889. }
  890. reg_arch_type_t *arch_type;
  891. bin_buf = malloc(CEIL(reg_list[i]->size, 8));
  892. gdb_target_to_reg(target, packet_p, chars, bin_buf);
  893. /* get register arch_type, and call set method */
  894. arch_type = register_get_arch_type(reg_list[i]->arch_type);
  895. arch_type->set(reg_list[i], bin_buf);
  896. /* advance packet pointer */
  897. packet_p += chars;
  898. free(bin_buf);
  899. }
  900. /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
  901. free(reg_list);
  902. gdb_put_packet(connection, "OK", 2);
  903. return ERROR_OK;
  904. }
  905. int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  906. {
  907. char *reg_packet;
  908. int reg_num = strtoul(packet + 1, NULL, 16);
  909. reg_t **reg_list;
  910. int reg_list_size;
  911. int retval;
  912. #ifdef _DEBUG_GDB_IO_
  913. LOG_DEBUG("-");
  914. #endif
  915. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  916. {
  917. return gdb_error(connection, retval);
  918. }
  919. if (reg_list_size <= reg_num)
  920. {
  921. LOG_ERROR("gdb requested a non-existing register");
  922. exit(-1);
  923. }
  924. reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
  925. gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
  926. gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
  927. free(reg_list);
  928. free(reg_packet);
  929. return ERROR_OK;
  930. }
  931. int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  932. {
  933. char *separator;
  934. uint8_t *bin_buf;
  935. int reg_num = strtoul(packet + 1, &separator, 16);
  936. reg_t **reg_list;
  937. int reg_list_size;
  938. int retval;
  939. reg_arch_type_t *arch_type;
  940. LOG_DEBUG("-");
  941. if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  942. {
  943. return gdb_error(connection, retval);
  944. }
  945. if (reg_list_size < reg_num)
  946. {
  947. LOG_ERROR("gdb requested a non-existing register");
  948. return ERROR_SERVER_REMOTE_CLOSED;
  949. }
  950. if (*separator != '=')
  951. {
  952. LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
  953. return ERROR_SERVER_REMOTE_CLOSED;
  954. }
  955. /* convert from GDB-string (target-endian) to hex-string (big-endian) */
  956. bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
  957. int chars = (CEIL(reg_list[reg_num]->size, 8) * 2);
  958. /* fix!!! add some sanity checks on packet size here */
  959. gdb_target_to_reg(target, separator + 1, chars, bin_buf);
  960. /* get register arch_type, and call set method */
  961. arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
  962. arch_type->set(reg_list[reg_num], bin_buf);
  963. gdb_put_packet(connection, "OK", 2);
  964. free(bin_buf);
  965. free(reg_list);
  966. return ERROR_OK;
  967. }
  968. int gdb_error(connection_t *connection, int retval)
  969. {
  970. switch (retval)
  971. {
  972. case ERROR_TARGET_DATA_ABORT:
  973. gdb_send_error(connection, EIO);
  974. break;
  975. case ERROR_TARGET_TRANSLATION_FAULT:
  976. gdb_send_error(connection, EFAULT);
  977. break;
  978. case ERROR_TARGET_UNALIGNED_ACCESS:
  979. gdb_send_error(connection, EFAULT);
  980. break;
  981. case ERROR_TARGET_NOT_HALTED:
  982. gdb_send_error(connection, EFAULT);
  983. break;
  984. default:
  985. /* This could be that the target reset itself. */
  986. LOG_ERROR("unexpected error %i", retval);
  987. gdb_send_error(connection, EFAULT);
  988. break;
  989. }
  990. return ERROR_OK;
  991. }
  992. /* We don't have to worry about the default 2 second timeout for GDB packets,
  993. * because GDB breaks up large memory reads into smaller reads.
  994. *
  995. * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
  996. */
  997. int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  998. {
  999. char *separator;
  1000. uint32_t addr = 0;
  1001. uint32_t len = 0;
  1002. uint8_t *buffer;
  1003. char *hex_buffer;
  1004. int retval = ERROR_OK;
  1005. /* skip command character */
  1006. packet++;
  1007. addr = strtoul(packet, &separator, 16);
  1008. if (*separator != ',')
  1009. {
  1010. LOG_ERROR("incomplete read memory packet received, dropping connection");
  1011. return ERROR_SERVER_REMOTE_CLOSED;
  1012. }
  1013. len = strtoul(separator+1, NULL, 16);
  1014. buffer = malloc(len);
  1015. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1016. retval = target_read_buffer(target, addr, len, buffer);
  1017. if ((retval!=ERROR_OK)&&!gdb_report_data_abort)
  1018. {
  1019. /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
  1020. * At some point this might be fixed in GDB, in which case this code can be removed.
  1021. *
  1022. * OpenOCD developers are acutely aware of this problem, but there is nothing
  1023. * gained by involving the user in this problem that hopefully will get resolved
  1024. * eventually
  1025. *
  1026. * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
  1027. *
  1028. * For now, the default is to fix up things to make current GDB versions work.
  1029. * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
  1030. */
  1031. memset(buffer, 0, len);
  1032. retval = ERROR_OK;
  1033. }
  1034. if (retval == ERROR_OK)
  1035. {
  1036. hex_buffer = malloc(len * 2 + 1);
  1037. uint32_t i;
  1038. for (i = 0; i < len; i++)
  1039. {
  1040. uint8_t t = buffer[i];
  1041. hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
  1042. hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
  1043. }
  1044. gdb_put_packet(connection, hex_buffer, len * 2);
  1045. free(hex_buffer);
  1046. }
  1047. else
  1048. {
  1049. retval = gdb_error(connection, retval);
  1050. }
  1051. free(buffer);
  1052. return retval;
  1053. }
  1054. int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1055. {
  1056. char *separator;
  1057. uint32_t addr = 0;
  1058. uint32_t len = 0;
  1059. uint8_t *buffer;
  1060. uint32_t i;
  1061. int retval;
  1062. /* skip command character */
  1063. packet++;
  1064. addr = strtoul(packet, &separator, 16);
  1065. if (*separator != ',')
  1066. {
  1067. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1068. return ERROR_SERVER_REMOTE_CLOSED;
  1069. }
  1070. len = strtoul(separator+1, &separator, 16);
  1071. if (*(separator++) != ':')
  1072. {
  1073. LOG_ERROR("incomplete write memory packet received, dropping connection");
  1074. return ERROR_SERVER_REMOTE_CLOSED;
  1075. }
  1076. buffer = malloc(len);
  1077. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1078. for (i=0; i<len; i++)
  1079. {
  1080. uint32_t tmp;
  1081. sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
  1082. buffer[i] = tmp;
  1083. }
  1084. retval = target_write_buffer(target, addr, len, buffer);
  1085. if (retval == ERROR_OK)
  1086. {
  1087. gdb_put_packet(connection, "OK", 2);
  1088. }
  1089. else
  1090. {
  1091. retval = gdb_error(connection, retval);
  1092. }
  1093. free(buffer);
  1094. return retval;
  1095. }
  1096. int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1097. {
  1098. char *separator;
  1099. uint32_t addr = 0;
  1100. uint32_t len = 0;
  1101. int retval;
  1102. /* skip command character */
  1103. packet++;
  1104. addr = strtoul(packet, &separator, 16);
  1105. if (*separator != ',')
  1106. {
  1107. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1108. return ERROR_SERVER_REMOTE_CLOSED;
  1109. }
  1110. len = strtoul(separator+1, &separator, 16);
  1111. if (*(separator++) != ':')
  1112. {
  1113. LOG_ERROR("incomplete write memory binary packet received, dropping connection");
  1114. return ERROR_SERVER_REMOTE_CLOSED;
  1115. }
  1116. retval = ERROR_OK;
  1117. if (len)
  1118. {
  1119. LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
  1120. retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
  1121. }
  1122. if (retval == ERROR_OK)
  1123. {
  1124. gdb_put_packet(connection, "OK", 2);
  1125. }
  1126. else
  1127. {
  1128. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1129. return retval;
  1130. }
  1131. return ERROR_OK;
  1132. }
  1133. int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1134. {
  1135. int current = 0;
  1136. uint32_t address = 0x0;
  1137. int retval=ERROR_OK;
  1138. LOG_DEBUG("-");
  1139. if (packet_size > 1)
  1140. {
  1141. packet[packet_size] = 0;
  1142. address = strtoul(packet + 1, NULL, 16);
  1143. }
  1144. else
  1145. {
  1146. current = 1;
  1147. }
  1148. if (packet[0] == 'c')
  1149. {
  1150. LOG_DEBUG("continue");
  1151. target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
  1152. retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
  1153. }
  1154. else if (packet[0] == 's')
  1155. {
  1156. LOG_DEBUG("step");
  1157. /* step at current or address, don't handle breakpoints */
  1158. retval = target_step(target, current, address, 0);
  1159. }
  1160. return retval;
  1161. }
  1162. int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1163. {
  1164. int type;
  1165. enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
  1166. enum watchpoint_rw wp_type;
  1167. uint32_t address;
  1168. uint32_t size;
  1169. char *separator;
  1170. int retval;
  1171. LOG_DEBUG("-");
  1172. type = strtoul(packet + 1, &separator, 16);
  1173. if (type == 0) /* memory breakpoint */
  1174. bp_type = BKPT_SOFT;
  1175. else if (type == 1) /* hardware breakpoint */
  1176. bp_type = BKPT_HARD;
  1177. else if (type == 2) /* write watchpoint */
  1178. wp_type = WPT_WRITE;
  1179. else if (type == 3) /* read watchpoint */
  1180. wp_type = WPT_READ;
  1181. else if (type == 4) /* access watchpoint */
  1182. wp_type = WPT_ACCESS;
  1183. if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
  1184. {
  1185. bp_type=gdb_breakpoint_override_type;
  1186. }
  1187. if (*separator != ',')
  1188. {
  1189. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1190. return ERROR_SERVER_REMOTE_CLOSED;
  1191. }
  1192. address = strtoul(separator+1, &separator, 16);
  1193. if (*separator != ',')
  1194. {
  1195. LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  1196. return ERROR_SERVER_REMOTE_CLOSED;
  1197. }
  1198. size = strtoul(separator+1, &separator, 16);
  1199. switch (type)
  1200. {
  1201. case 0:
  1202. case 1:
  1203. if (packet[0] == 'Z')
  1204. {
  1205. if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
  1206. {
  1207. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1208. return retval;
  1209. }
  1210. else
  1211. {
  1212. gdb_put_packet(connection, "OK", 2);
  1213. }
  1214. }
  1215. else
  1216. {
  1217. breakpoint_remove(target, address);
  1218. gdb_put_packet(connection, "OK", 2);
  1219. }
  1220. break;
  1221. case 2:
  1222. case 3:
  1223. case 4:
  1224. {
  1225. if (packet[0] == 'Z')
  1226. {
  1227. if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
  1228. {
  1229. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1230. return retval;
  1231. }
  1232. else
  1233. {
  1234. gdb_put_packet(connection, "OK", 2);
  1235. }
  1236. }
  1237. else
  1238. {
  1239. watchpoint_remove(target, address);
  1240. gdb_put_packet(connection, "OK", 2);
  1241. }
  1242. break;
  1243. }
  1244. default:
  1245. break;
  1246. }
  1247. return ERROR_OK;
  1248. }
  1249. /* print out a string and allocate more space as needed, mainly used for XML at this point */
  1250. void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
  1251. {
  1252. if (*retval != ERROR_OK)
  1253. {
  1254. return;
  1255. }
  1256. int first = 1;
  1257. for (;;)
  1258. {
  1259. if ((*xml == NULL) || (!first))
  1260. {
  1261. /* start by 0 to exercise all the code paths.
  1262. * Need minimum 2 bytes to fit 1 char and 0 terminator. */
  1263. *size = *size * 2 + 2;
  1264. char *t = *xml;
  1265. *xml = realloc(*xml, *size);
  1266. if (*xml == NULL)
  1267. {
  1268. if (t)
  1269. free(t);
  1270. *retval = ERROR_SERVER_REMOTE_CLOSED;
  1271. return;
  1272. }
  1273. }
  1274. va_list ap;
  1275. int ret;
  1276. va_start(ap, fmt);
  1277. ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
  1278. va_end(ap);
  1279. if ((ret > 0) && ((ret + 1) < *size - *pos))
  1280. {
  1281. *pos += ret;
  1282. return;
  1283. }
  1284. /* there was just enough or not enough space, allocate more. */
  1285. first = 0;
  1286. }
  1287. }
  1288. static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
  1289. {
  1290. char *separator;
  1291. /* Extract and NUL-terminate the annex. */
  1292. *annex = buf;
  1293. while (*buf && *buf != ':')
  1294. buf++;
  1295. if (*buf == '\0')
  1296. return -1;
  1297. *buf++ = 0;
  1298. /* After the read marker and annex, qXfer looks like a
  1299. * traditional 'm' packet. */
  1300. *ofs = strtoul(buf, &separator, 16);
  1301. if (*separator != ',')
  1302. return -1;
  1303. *len = strtoul(separator+1, NULL, 16);
  1304. return 0;
  1305. }
  1306. int gdb_calc_blocksize(flash_bank_t *bank)
  1307. {
  1308. uint32_t i;
  1309. uint32_t block_size = 0xffffffff;
  1310. /* loop through all sectors and return smallest sector size */
  1311. for (i = 0; i < (uint32_t)bank->num_sectors; i++)
  1312. {
  1313. if (bank->sectors[i].size < block_size)
  1314. block_size = bank->sectors[i].size;
  1315. }
  1316. return block_size;
  1317. }
  1318. static int compare_bank (const void * a, const void * b)
  1319. {
  1320. flash_bank_t *b1, *b2;
  1321. b1=*((flash_bank_t **)a);
  1322. b2=*((flash_bank_t **)b);
  1323. if (b1->base==b2->base)
  1324. {
  1325. return 0;
  1326. } else if (b1->base>b2->base)
  1327. {
  1328. return 1;
  1329. } else
  1330. {
  1331. return -1;
  1332. }
  1333. }
  1334. int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1335. {
  1336. command_context_t *cmd_ctx = connection->cmd_ctx;
  1337. gdb_connection_t *gdb_connection = connection->priv;
  1338. if (strstr(packet, "qRcmd,"))
  1339. {
  1340. if (packet_size > 6)
  1341. {
  1342. char *cmd;
  1343. int i;
  1344. cmd = malloc((packet_size - 6)/2 + 1);
  1345. for (i=0; i < (packet_size - 6)/2; i++)
  1346. {
  1347. uint32_t tmp;
  1348. sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
  1349. cmd[i] = tmp;
  1350. }
  1351. cmd[(packet_size - 6)/2] = 0x0;
  1352. /* We want to print all debug output to GDB connection */
  1353. log_add_callback(gdb_log_callback, connection);
  1354. target_call_timer_callbacks_now();
  1355. command_run_line(cmd_ctx, cmd);
  1356. target_call_timer_callbacks_now();
  1357. log_remove_callback(gdb_log_callback, connection);
  1358. free(cmd);
  1359. }
  1360. gdb_put_packet(connection, "OK", 2);
  1361. return ERROR_OK;
  1362. }
  1363. else if (strstr(packet, "qCRC:"))
  1364. {
  1365. if (packet_size > 5)
  1366. {
  1367. int retval;
  1368. char gdb_reply[10];
  1369. char *separator;
  1370. uint32_t checksum;
  1371. uint32_t addr = 0;
  1372. uint32_t len = 0;
  1373. /* skip command character */
  1374. packet += 5;
  1375. addr = strtoul(packet, &separator, 16);
  1376. if (*separator != ',')
  1377. {
  1378. LOG_ERROR("incomplete read memory packet received, dropping connection");
  1379. return ERROR_SERVER_REMOTE_CLOSED;
  1380. }
  1381. len = strtoul(separator + 1, NULL, 16);
  1382. retval = target_checksum_memory(target, addr, len, &checksum);
  1383. if (retval == ERROR_OK)
  1384. {
  1385. snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
  1386. gdb_put_packet(connection, gdb_reply, 9);
  1387. }
  1388. else
  1389. {
  1390. if ((retval = gdb_error(connection, retval)) != ERROR_OK)
  1391. return retval;
  1392. }
  1393. return ERROR_OK;
  1394. }
  1395. }
  1396. else if (strstr(packet, "qSupported"))
  1397. {
  1398. /* we currently support packet size and qXfer:memory-map:read (if enabled)
  1399. * disable qXfer:features:read for the moment */
  1400. int retval = ERROR_OK;
  1401. char *buffer = NULL;
  1402. int pos = 0;
  1403. int size = 0;
  1404. xml_printf(&retval, &buffer, &pos, &size,
  1405. "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
  1406. (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
  1407. if (retval != ERROR_OK)
  1408. {
  1409. gdb_send_error(connection, 01);
  1410. return ERROR_OK;
  1411. }
  1412. gdb_put_packet(connection, buffer, strlen(buffer));
  1413. free(buffer);
  1414. return ERROR_OK;
  1415. }
  1416. else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
  1417. {
  1418. /* We get away with only specifying flash here. Regions that are not
  1419. * specified are treated as if we provided no memory map(if not we
  1420. * could detect the holes and mark them as RAM).
  1421. * Normally we only execute this code once, but no big deal if we
  1422. * have to regenerate it a couple of times. */
  1423. flash_bank_t *p;
  1424. char *xml = NULL;
  1425. int size = 0;
  1426. int pos = 0;
  1427. int retval = ERROR_OK;
  1428. int offset;
  1429. int length;
  1430. char *separator;
  1431. int blocksize;
  1432. /* skip command character */
  1433. packet += 23;
  1434. offset = strtoul(packet, &separator, 16);
  1435. length = strtoul(separator + 1, &separator, 16);
  1436. xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
  1437. /*
  1438. sort banks in ascending order, we need to make non-flash memory be ram(or rather
  1439. read/write) by default for GDB.
  1440. GDB does not have a concept of non-cacheable read/write memory.
  1441. */
  1442. flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
  1443. int i;
  1444. for (i=0; i<flash_get_bank_count(); i++)
  1445. {
  1446. p = get_flash_bank_by_num(i);
  1447. if (p == NULL)
  1448. {
  1449. free(banks);
  1450. retval = ERROR_FAIL;
  1451. gdb_send_error(connection, retval);
  1452. return retval;
  1453. }
  1454. banks[i]=p;
  1455. }
  1456. qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
  1457. uint32_t ram_start=0;
  1458. for (i=0; i<flash_get_bank_count(); i++)
  1459. {
  1460. p = banks[i];
  1461. if (ram_start<p->base)
  1462. {
  1463. xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
  1464. ram_start, p->base-ram_start);
  1465. }
  1466. /* if device has uneven sector sizes, eg. str7, lpc
  1467. * we pass the smallest sector size to gdb memory map */
  1468. blocksize = gdb_calc_blocksize(p);
  1469. xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
  1470. "<property name=\"blocksize\">0x%x</property>\n" \
  1471. "</memory>\n", \
  1472. p->base, p->size, blocksize);
  1473. ram_start=p->base+p->size;
  1474. }
  1475. if (ram_start!=0)
  1476. {
  1477. xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
  1478. ram_start, 0-ram_start);
  1479. } else
  1480. {
  1481. /* a flash chip could be at the very end of the 32 bit address space, in which case
  1482. ram_start will be precisely 0 */
  1483. }
  1484. free(banks);
  1485. banks = NULL;
  1486. xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
  1487. if (retval != ERROR_OK)
  1488. {
  1489. gdb_send_error(connection, retval);
  1490. return retval;
  1491. }
  1492. if (offset + length > pos)
  1493. {
  1494. length = pos - offset;
  1495. }
  1496. char *t = malloc(length + 1);
  1497. t[0] = 'l';
  1498. memcpy(t + 1, xml + offset, length);
  1499. gdb_put_packet(connection, t, length + 1);
  1500. free(t);
  1501. free(xml);
  1502. return ERROR_OK;
  1503. }
  1504. else if (strstr(packet, "qXfer:features:read:"))
  1505. {
  1506. char *xml = NULL;
  1507. int size = 0;
  1508. int pos = 0;
  1509. int retval = ERROR_OK;
  1510. int offset;
  1511. unsigned int length;
  1512. char *annex;
  1513. /* skip command character */
  1514. packet += 20;
  1515. if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
  1516. {
  1517. gdb_send_error(connection, 01);
  1518. return ERROR_OK;
  1519. }
  1520. if (strcmp(annex, "target.xml") != 0)
  1521. {
  1522. gdb_send_error(connection, 01);
  1523. return ERROR_OK;
  1524. }
  1525. xml_printf(&retval, &xml, &pos, &size, \
  1526. "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
  1527. if (retval != ERROR_OK)
  1528. {
  1529. gdb_send_error(connection, retval);
  1530. return retval;
  1531. }
  1532. gdb_put_packet(connection, xml, strlen(xml));
  1533. free(xml);
  1534. return ERROR_OK;
  1535. }
  1536. else if (strstr(packet, "QStartNoAckMode"))
  1537. {
  1538. gdb_connection->noack_mode = 1;
  1539. gdb_put_packet(connection, "OK", 2);
  1540. return ERROR_OK;
  1541. }
  1542. gdb_put_packet(connection, "", 0);
  1543. return ERROR_OK;
  1544. }
  1545. int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  1546. {
  1547. gdb_connection_t *gdb_connection = connection->priv;
  1548. gdb_service_t *gdb_service = connection->service->priv;
  1549. int result;
  1550. /* if flash programming disabled - send a empty reply */
  1551. if (gdb_flash_program == 0)
  1552. {
  1553. gdb_put_packet(connection, "", 0);
  1554. return ERROR_OK;
  1555. }
  1556. if (strstr(packet, "vFlashErase:"))
  1557. {
  1558. unsigned long addr;
  1559. unsigned long length;
  1560. char *parse = packet + 12;
  1561. if (*parse == '\0')
  1562. {
  1563. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1564. return ERROR_SERVER_REMOTE_CLOSED;
  1565. }
  1566. addr = strtoul(parse, &parse, 16);
  1567. if (*(parse++) != ',' || *parse == '\0')
  1568. {
  1569. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1570. return ERROR_SERVER_REMOTE_CLOSED;
  1571. }
  1572. length = strtoul(parse, &parse, 16);
  1573. if (*parse != '\0')
  1574. {
  1575. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1576. return ERROR_SERVER_REMOTE_CLOSED;
  1577. }
  1578. /* assume all sectors need erasing - stops any problems
  1579. * when flash_write is called multiple times */
  1580. flash_set_dirty();
  1581. /* perform any target specific operations before the erase */
  1582. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
  1583. result = flash_erase_address_range(gdb_service->target, addr, length );
  1584. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
  1585. /* perform erase */
  1586. if (result != ERROR_OK)
  1587. {
  1588. /* GDB doesn't evaluate the actual error number returned,
  1589. * treat a failed erase as an I/O error
  1590. */
  1591. gdb_send_error(connection, EIO);
  1592. LOG_ERROR("flash_erase returned %i", result);
  1593. }
  1594. else
  1595. gdb_put_packet(connection, "OK", 2);
  1596. return ERROR_OK;
  1597. }
  1598. if (strstr(packet, "vFlashWrite:"))
  1599. {
  1600. int retval;
  1601. unsigned long addr;
  1602. unsigned long length;
  1603. char *parse = packet + 12;
  1604. if (*parse == '\0')
  1605. {
  1606. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1607. return ERROR_SERVER_REMOTE_CLOSED;
  1608. }
  1609. addr = strtoul(parse, &parse, 16);
  1610. if (*(parse++) != ':')
  1611. {
  1612. LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
  1613. return ERROR_SERVER_REMOTE_CLOSED;
  1614. }
  1615. length = packet_size - (parse - packet);
  1616. /* create a new image if there isn't already one */
  1617. if (gdb_connection->vflash_image == NULL)
  1618. {
  1619. gdb_connection->vflash_image = malloc(sizeof(image_t));
  1620. image_open(gdb_connection->vflash_image, "", "build");
  1621. }
  1622. /* create new section with content from packet buffer */
  1623. if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
  1624. {
  1625. return retval;
  1626. }
  1627. gdb_put_packet(connection, "OK", 2);
  1628. return ERROR_OK;
  1629. }
  1630. if (!strcmp(packet, "vFlashDone"))
  1631. {
  1632. uint32_t written;
  1633. /* process the flashing buffer. No need to erase as GDB
  1634. * always issues a vFlashErase first. */
  1635. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
  1636. result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
  1637. target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
  1638. if ( result != ERROR_OK)
  1639. {
  1640. if (result == ERROR_FLASH_DST_OUT_OF_BANK)
  1641. gdb_put_packet(connection, "E.memtype", 9);
  1642. else
  1643. gdb_send_error(connection, EIO);
  1644. }
  1645. else
  1646. {
  1647. LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
  1648. gdb_put_packet(connection, "OK", 2);
  1649. }
  1650. image_close(gdb_connection->vflash_image);
  1651. free(gdb_connection->vflash_image);
  1652. gdb_connection->vflash_image = NULL;
  1653. return ERROR_OK;
  1654. }
  1655. gdb_put_packet(connection, "", 0);
  1656. return ERROR_OK;
  1657. }
  1658. int gdb_detach(connection_t *connection, target_t *target)
  1659. {
  1660. switch ( detach_mode )
  1661. {
  1662. case GDB_DETACH_RESUME:
  1663. target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
  1664. target_resume(target, 1, 0, 1, 0);
  1665. break;
  1666. case GDB_DETACH_RESET:
  1667. /* FIX?? make this configurable?? */
  1668. target_process_reset(connection->cmd_ctx, RESET_HALT);
  1669. break;
  1670. case GDB_DETACH_HALT:
  1671. target_halt(target);
  1672. break;
  1673. case GDB_DETACH_NOTHING:
  1674. break;
  1675. }
  1676. gdb_put_packet(connection, "OK", 2);
  1677. return ERROR_OK;
  1678. }
  1679. static void gdb_log_callback(void *priv, const char *file, int line,
  1680. const char *function, const char *string)
  1681. {
  1682. connection_t *connection = priv;
  1683. gdb_connection_t *gdb_con = connection->priv;
  1684. if (gdb_con->busy)
  1685. {
  1686. /* do not reply this using the O packet */
  1687. return;
  1688. }
  1689. gdb_output_con(connection, string);
  1690. }
  1691. /* Do not allocate this on the stack */
  1692. char gdb_packet_buffer[GDB_BUFFER_SIZE];
  1693. static void gdb_sig_halted(connection_t *connection)
  1694. {
  1695. char sig_reply[4];
  1696. snprintf(sig_reply, 4, "T%2.2x", 2);
  1697. gdb_put_packet(connection, sig_reply, 3);
  1698. }
  1699. int gdb_input_inner(connection_t *connection)
  1700. {
  1701. gdb_service_t *gdb_service = connection->service->priv;
  1702. target_t *target = gdb_service->target;
  1703. char *packet=gdb_packet_buffer;
  1704. int packet_size;
  1705. int retval;
  1706. gdb_connection_t *gdb_con = connection->priv;
  1707. static int extended_protocol = 0;
  1708. /* drain input buffer */
  1709. do
  1710. {
  1711. packet_size = GDB_BUFFER_SIZE-1;
  1712. if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
  1713. {
  1714. return retval;
  1715. }
  1716. /* terminate with zero */
  1717. packet[packet_size] = 0;
  1718. if ( LOG_LEVEL_IS( LOG_LVL_DEBUG ) ){
  1719. if ( packet[0] == 'X' ){
  1720. // binary packets spew junk into the debug log stream
  1721. char buf[ 50 ];
  1722. int x;
  1723. for ( x = 0 ; (x < 49) && (packet[x] != ':') ; x++ ){
  1724. buf[x] = packet[x];
  1725. }
  1726. buf[x] = 0;
  1727. LOG_DEBUG("received packet: '%s:<binary-data>'", buf );
  1728. } else {
  1729. LOG_DEBUG("received packet: '%s'", packet );
  1730. }
  1731. }
  1732. if (packet_size > 0)
  1733. {
  1734. retval = ERROR_OK;
  1735. switch (packet[0])
  1736. {
  1737. case 'H':
  1738. /* Hct... -- set thread
  1739. * we don't have threads, send empty reply */
  1740. gdb_put_packet(connection, NULL, 0);
  1741. break;
  1742. case 'q':
  1743. case 'Q':
  1744. retval = gdb_query_packet(connection, target, packet, packet_size);
  1745. break;
  1746. case 'g':
  1747. retval = gdb_get_registers_packet(connection, target, packet, packet_size);
  1748. break;
  1749. case 'G':
  1750. retval = gdb_set_registers_packet(connection, target, packet, packet_size);
  1751. break;
  1752. case 'p':
  1753. retval = gdb_get_register_packet(connection, target, packet, packet_size);
  1754. break;
  1755. case 'P':
  1756. retval = gdb_set_register_packet(connection, target, packet, packet_size);
  1757. break;
  1758. case 'm':
  1759. retval = gdb_read_memory_packet(connection, target, packet, packet_size);
  1760. break;
  1761. case 'M':
  1762. retval = gdb_write_memory_packet(connection, target, packet, packet_size);
  1763. break;
  1764. case 'z':
  1765. case 'Z':
  1766. retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
  1767. break;
  1768. case '?':
  1769. gdb_last_signal_packet(connection, target, packet, packet_size);
  1770. break;
  1771. case 'c':
  1772. case 's':
  1773. {
  1774. if (target->state != TARGET_HALTED)
  1775. {
  1776. /* If the target isn't in the halted state, then we can't
  1777. * step/continue. This might be early setup, etc.
  1778. */
  1779. gdb_sig_halted(connection);
  1780. } else
  1781. {
  1782. /* We're running/stepping, in which case we can
  1783. * forward log output until the target is halted
  1784. */
  1785. gdb_connection_t *gdb_con = connection->priv;
  1786. gdb_con->frontend_state = TARGET_RUNNING;
  1787. log_add_callback(gdb_log_callback, connection);
  1788. target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
  1789. int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
  1790. if (retval!=ERROR_OK)
  1791. {
  1792. /* we'll never receive a halted condition... issue a false one.. */
  1793. gdb_frontend_halted(target, connection);
  1794. }
  1795. }
  1796. }
  1797. break;
  1798. case 'v':
  1799. retval = gdb_v_packet(connection, target, packet, packet_size);
  1800. break;
  1801. case 'D':
  1802. retval = gdb_detach(connection, target);
  1803. extended_protocol = 0;
  1804. break;
  1805. case 'X':
  1806. if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
  1807. return retval;
  1808. break;
  1809. case 'k':
  1810. if (extended_protocol != 0)
  1811. break;
  1812. gdb_put_packet(connection, "OK", 2);
  1813. return ERROR_SERVER_REMOTE_CLOSED;
  1814. case '!':
  1815. /* handle extended remote protocol */
  1816. extended_protocol = 1;
  1817. gdb_put_packet(connection, "OK", 2);
  1818. break;
  1819. case 'R':
  1820. /* handle extended restart packet */
  1821. breakpoint_clear_target(gdb_service->target);
  1822. watchpoint_clear_target(gdb_service->target);
  1823. command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
  1824. break;
  1825. default:
  1826. /* ignore unkown packets */
  1827. LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
  1828. gdb_put_packet(connection, NULL, 0);
  1829. break;
  1830. }
  1831. /* if a packet handler returned an error, exit input loop */
  1832. if (retval != ERROR_OK)
  1833. return retval;
  1834. }
  1835. if (gdb_con->ctrl_c)
  1836. {
  1837. if (target->state == TARGET_RUNNING)
  1838. {
  1839. target_halt(target);
  1840. gdb_con->ctrl_c = 0;
  1841. }
  1842. }
  1843. } while (gdb_con->buf_cnt > 0);
  1844. return ERROR_OK;
  1845. }
  1846. int gdb_input(connection_t *connection)
  1847. {
  1848. int retval = gdb_input_inner(connection);
  1849. gdb_connection_t *gdb_con = connection->priv;
  1850. if (retval == ERROR_SERVER_REMOTE_CLOSED)
  1851. return retval;
  1852. /* logging does not propagate the error, yet can set the gdb_con->closed flag */
  1853. if (gdb_con->closed)
  1854. return ERROR_SERVER_REMOTE_CLOSED;
  1855. /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
  1856. return ERROR_OK;
  1857. }
  1858. int gdb_init(void)
  1859. {
  1860. gdb_service_t *gdb_service;
  1861. target_t *target = all_targets;
  1862. if (!target)
  1863. {
  1864. LOG_WARNING("no gdb ports allocated as no target has been specified");
  1865. return ERROR_OK;
  1866. }
  1867. if (gdb_port == 0 && server_use_pipes == 0)
  1868. {
  1869. LOG_INFO("gdb port disabled");
  1870. return ERROR_OK;
  1871. }
  1872. if (server_use_pipes)
  1873. {
  1874. /* only a single gdb connection when using a pipe */
  1875. gdb_service = malloc(sizeof(gdb_service_t));
  1876. gdb_service->target = target;
  1877. add_service("gdb", CONNECTION_PIPE, 0, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
  1878. LOG_DEBUG("gdb service for target %s using pipes",
  1879. target_get_name(target));
  1880. }
  1881. else
  1882. {
  1883. while (target)
  1884. {
  1885. gdb_service = malloc(sizeof(gdb_service_t));
  1886. gdb_service->target = target;
  1887. add_service("gdb", CONNECTION_TCP, gdb_port + target->target_number, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
  1888. LOG_DEBUG("gdb service for target %s at port %i",
  1889. target_get_name(target),
  1890. gdb_port + target->target_number);
  1891. target = target->next;
  1892. }
  1893. }
  1894. return ERROR_OK;
  1895. }
  1896. /* daemon configuration command gdb_port */
  1897. int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1898. {
  1899. if (argc == 0)
  1900. {
  1901. command_print(cmd_ctx, "%d", gdb_port);
  1902. return ERROR_OK;
  1903. }
  1904. /* only if the port wasn't overwritten by cmdline */
  1905. if (gdb_port == 0)
  1906. gdb_port = strtoul(args[0], NULL, 0);
  1907. return ERROR_OK;
  1908. }
  1909. int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1910. {
  1911. if (argc == 1)
  1912. {
  1913. if (strcmp(args[0], "resume") == 0)
  1914. {
  1915. detach_mode = GDB_DETACH_RESUME;
  1916. return ERROR_OK;
  1917. }
  1918. else if (strcmp(args[0], "reset") == 0)
  1919. {
  1920. detach_mode = GDB_DETACH_RESET;
  1921. return ERROR_OK;
  1922. }
  1923. else if (strcmp(args[0], "halt") == 0)
  1924. {
  1925. detach_mode = GDB_DETACH_HALT;
  1926. return ERROR_OK;
  1927. }
  1928. else if (strcmp(args[0], "nothing") == 0)
  1929. {
  1930. detach_mode = GDB_DETACH_NOTHING;
  1931. return ERROR_OK;
  1932. }
  1933. else
  1934. LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
  1935. }
  1936. return ERROR_COMMAND_SYNTAX_ERROR;
  1937. }
  1938. int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1939. {
  1940. if (argc == 1)
  1941. {
  1942. if (strcmp(args[0], "enable") == 0)
  1943. {
  1944. gdb_use_memory_map = 1;
  1945. return ERROR_OK;
  1946. }
  1947. else if (strcmp(args[0], "disable") == 0)
  1948. {
  1949. gdb_use_memory_map = 0;
  1950. return ERROR_OK;
  1951. }
  1952. else
  1953. LOG_WARNING("invalid gdb_memory_map configuration directive %s", args[0]);
  1954. }
  1955. return ERROR_COMMAND_SYNTAX_ERROR;
  1956. }
  1957. int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1958. {
  1959. if (argc == 1)
  1960. {
  1961. if (strcmp(args[0], "enable") == 0)
  1962. {
  1963. gdb_flash_program = 1;
  1964. return ERROR_OK;
  1965. }
  1966. else if (strcmp(args[0], "disable") == 0)
  1967. {
  1968. gdb_flash_program = 0;
  1969. return ERROR_OK;
  1970. }
  1971. else
  1972. LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args[0]);
  1973. }
  1974. return ERROR_COMMAND_SYNTAX_ERROR;
  1975. }
  1976. int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1977. {
  1978. if (argc == 1)
  1979. {
  1980. if (strcmp(args[0], "enable") == 0)
  1981. {
  1982. gdb_report_data_abort = 1;
  1983. return ERROR_OK;
  1984. }
  1985. else if (strcmp(args[0], "disable") == 0)
  1986. {
  1987. gdb_report_data_abort = 0;
  1988. return ERROR_OK;
  1989. }
  1990. else
  1991. LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
  1992. }
  1993. return ERROR_COMMAND_SYNTAX_ERROR;
  1994. }
  1995. /* gdb_breakpoint_override */
  1996. int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1997. {
  1998. if (argc == 0)
  1999. {
  2000. } else if (argc==1)
  2001. {
  2002. gdb_breakpoint_override = 1;
  2003. if (strcmp(args[0], "hard")==0)
  2004. {
  2005. gdb_breakpoint_override_type=BKPT_HARD;
  2006. } else if (strcmp(args[0], "soft")==0)
  2007. {
  2008. gdb_breakpoint_override_type=BKPT_SOFT;
  2009. } else if (strcmp(args[0], "disable") == 0)
  2010. {
  2011. gdb_breakpoint_override = 0;
  2012. }
  2013. } else
  2014. {
  2015. return ERROR_COMMAND_SYNTAX_ERROR;
  2016. }
  2017. if (gdb_breakpoint_override)
  2018. {
  2019. LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
  2020. } else
  2021. {
  2022. LOG_USER("breakpoint type is not overriden");
  2023. }
  2024. return ERROR_OK;
  2025. }
  2026. int gdb_register_commands(command_context_t *command_context)
  2027. {
  2028. register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
  2029. COMMAND_ANY, "daemon configuration command gdb_port");
  2030. register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
  2031. COMMAND_CONFIG, "resume/reset/halt/nothing - "
  2032. "specify behavior when GDB detaches from the target");
  2033. register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
  2034. COMMAND_CONFIG, "enable or disable memory map");
  2035. register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
  2036. COMMAND_CONFIG, "enable or disable flash program");
  2037. register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
  2038. COMMAND_CONFIG, "enable or disable reporting data aborts");
  2039. register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
  2040. COMMAND_EXEC, "hard/soft/disable - force breakpoint type for gdb 'break' commands.");
  2041. return ERROR_OK;
  2042. }