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.
 
 
 
 
 
 

2114 lines
49 KiB

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