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.
 
 
 
 
 
 

1480 lines
34 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 "breakpoints.h"
  29. #include "flash.h"
  30. #include "target_request.h"
  31. #define __USE_GNU
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #if 0
  37. #define _DEBUG_GDB_IO_
  38. #endif
  39. static unsigned short gdb_port;
  40. int gdb_last_signal(target_t *target)
  41. {
  42. switch (target->debug_reason)
  43. {
  44. case DBG_REASON_DBGRQ:
  45. return 0x2; /* SIGINT */
  46. case DBG_REASON_BREAKPOINT:
  47. case DBG_REASON_WATCHPOINT:
  48. case DBG_REASON_WPTANDBKPT:
  49. return 0x05; /* SIGTRAP */
  50. case DBG_REASON_SINGLESTEP:
  51. return 0x05; /* SIGTRAP */
  52. case DBG_REASON_NOTHALTED:
  53. return 0x0; /* no signal... shouldn't happen */
  54. default:
  55. ERROR("BUG: undefined debug reason");
  56. exit(-1);
  57. }
  58. }
  59. int gdb_get_char(connection_t *connection, int* next_char)
  60. {
  61. gdb_connection_t *gdb_con = connection->priv;
  62. char *debug_buffer;
  63. if (gdb_con->buf_cnt-- > 0)
  64. {
  65. *next_char = *(gdb_con->buf_p++);
  66. if (gdb_con->buf_cnt > 0)
  67. connection->input_pending = 1;
  68. else
  69. connection->input_pending = 0;
  70. #ifdef _DEBUG_GDB_IO_
  71. DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  72. #endif
  73. return ERROR_OK;
  74. }
  75. while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
  76. {
  77. if (gdb_con->buf_cnt == 0)
  78. return ERROR_SERVER_REMOTE_CLOSED;
  79. #ifdef _WIN32
  80. errno = WSAGetLastError();
  81. switch(errno)
  82. {
  83. case WSAEWOULDBLOCK:
  84. usleep(1000);
  85. break;
  86. case WSAECONNABORTED:
  87. return ERROR_SERVER_REMOTE_CLOSED;
  88. default:
  89. ERROR("read: %d", errno);
  90. exit(-1);
  91. }
  92. #else
  93. switch(errno)
  94. {
  95. case EAGAIN:
  96. usleep(1000);
  97. break;
  98. case ECONNABORTED:
  99. return ERROR_SERVER_REMOTE_CLOSED;
  100. case ECONNRESET:
  101. return ERROR_SERVER_REMOTE_CLOSED;
  102. default:
  103. ERROR("read: %s", strerror(errno));
  104. exit(-1);
  105. }
  106. #endif
  107. }
  108. debug_buffer = malloc(gdb_con->buf_cnt + 1);
  109. memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
  110. debug_buffer[gdb_con->buf_cnt] = 0;
  111. DEBUG("received '%s'", debug_buffer);
  112. free(debug_buffer);
  113. gdb_con->buf_p = gdb_con->buffer;
  114. gdb_con->buf_cnt--;
  115. *next_char = *(gdb_con->buf_p++);
  116. if (gdb_con->buf_cnt > 0)
  117. connection->input_pending = 1;
  118. else
  119. connection->input_pending = 0;
  120. #ifdef _DEBUG_GDB_IO_
  121. DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
  122. #endif
  123. return ERROR_OK;
  124. }
  125. int gdb_putback_char(connection_t *connection, int last_char)
  126. {
  127. gdb_connection_t *gdb_con = connection->priv;
  128. if (gdb_con->buf_p > gdb_con->buffer)
  129. {
  130. *(--gdb_con->buf_p) = last_char;
  131. gdb_con->buf_cnt++;
  132. }
  133. else
  134. {
  135. ERROR("BUG: couldn't put character back");
  136. }
  137. return ERROR_OK;
  138. }
  139. int gdb_put_packet(connection_t *connection, char *buffer, int len)
  140. {
  141. int i;
  142. unsigned char my_checksum = 0;
  143. char checksum[3];
  144. char *debug_buffer;
  145. int reply;
  146. int retval;
  147. gdb_connection_t *gdb_con = connection->priv;
  148. for (i = 0; i < len; i++)
  149. my_checksum += buffer[i];
  150. while (1)
  151. {
  152. debug_buffer = malloc(len + 1);
  153. memcpy(debug_buffer, buffer, len);
  154. debug_buffer[len] = 0;
  155. DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
  156. free(debug_buffer);
  157. write_socket(connection->fd, "$", 1);
  158. if (len > 0)
  159. write_socket(connection->fd, buffer, len);
  160. write_socket(connection->fd, "#", 1);
  161. snprintf(checksum, 3, "%2.2x", my_checksum);
  162. write_socket(connection->fd, checksum, 2);
  163. if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
  164. return retval;
  165. if (reply == '+')
  166. break;
  167. else if (reply == '-')
  168. WARNING("negative reply, retrying");
  169. else if (reply == 0x3)
  170. {
  171. gdb_con->ctrl_c = 1;
  172. if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
  173. return retval;
  174. if (reply == '+')
  175. break;
  176. else if (reply == '-')
  177. WARNING("negative reply, retrying");
  178. else
  179. {
  180. ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
  181. return ERROR_SERVER_REMOTE_CLOSED;
  182. }
  183. }
  184. else
  185. {
  186. ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
  187. return ERROR_SERVER_REMOTE_CLOSED;
  188. }
  189. }
  190. return ERROR_OK;
  191. }
  192. int gdb_get_packet(connection_t *connection, char *buffer, int *len)
  193. {
  194. int character;
  195. int count = 0;
  196. int retval;
  197. char checksum[3];
  198. unsigned char my_checksum = 0;
  199. gdb_connection_t *gdb_con = connection->priv;
  200. while (1)
  201. {
  202. do
  203. {
  204. if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
  205. return retval;
  206. DEBUG("character: '%c'", character);
  207. switch (character)
  208. {
  209. case '$':
  210. break;
  211. case '+':
  212. WARNING("acknowledgment received, but no packet pending");
  213. break;
  214. case '-':
  215. WARNING("negative acknowledgment, but no packet pending");
  216. break;
  217. case 0x3:
  218. gdb_con->ctrl_c = 1;
  219. *len = 0;
  220. return ERROR_OK;
  221. default:
  222. WARNING("ignoring character 0x%x", character);
  223. break;
  224. }
  225. } while (character != '$');
  226. my_checksum = 0;
  227. do
  228. {
  229. if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
  230. return retval;
  231. if (character == '#') break;
  232. if (character == '}')
  233. {
  234. /* data transmitted in binary mode (X packet)
  235. * uses 0x7d as escape character */
  236. my_checksum += character & 0xff;
  237. if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
  238. return retval;
  239. my_checksum += character & 0xff;
  240. buffer[count++] = (character ^ 0x20) & 0xff;
  241. }
  242. else
  243. {
  244. my_checksum += character & 0xff;
  245. buffer[count++] = character & 0xff;
  246. }
  247. if (count > *len)
  248. {
  249. ERROR("packet buffer too small");
  250. return ERROR_GDB_BUFFER_TOO_SMALL;
  251. }
  252. } while (1);
  253. *len = count;
  254. if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
  255. return retval;
  256. checksum[0] = character;
  257. if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
  258. return retval;
  259. checksum[1] = character;
  260. checksum[2] = 0;
  261. if (my_checksum == strtoul(checksum, NULL, 16))
  262. {
  263. write_socket(connection->fd, "+", 1);
  264. break;
  265. }
  266. WARNING("checksum error, requesting retransmission");
  267. write_socket(connection->fd, "-", 1);
  268. }
  269. return ERROR_OK;
  270. }
  271. int gdb_output(struct command_context_s *context, char* line)
  272. {
  273. connection_t *connection = context->output_handler_priv;
  274. char *hex_buffer;
  275. int i, bin_size;
  276. bin_size = strlen(line);
  277. hex_buffer = malloc(bin_size*2 + 4);
  278. hex_buffer[0] = 'O';
  279. for (i=0; i<bin_size; i++)
  280. snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
  281. hex_buffer[bin_size*2+1] = '0';
  282. hex_buffer[bin_size*2+2] = 'a';
  283. hex_buffer[bin_size*2+3] = 0x0;
  284. gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
  285. free(hex_buffer);
  286. return ERROR_OK;
  287. }
  288. int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
  289. {
  290. connection_t *connection = priv;
  291. gdb_connection_t *gdb_connection = connection->priv;
  292. char sig_reply[4];
  293. int signal;
  294. switch (event)
  295. {
  296. case TARGET_EVENT_HALTED:
  297. if (gdb_connection->frontend_state == TARGET_RUNNING)
  298. {
  299. if (gdb_connection->ctrl_c)
  300. {
  301. signal = 0x2;
  302. gdb_connection->ctrl_c = 0;
  303. }
  304. else
  305. {
  306. signal = gdb_last_signal(target);
  307. }
  308. snprintf(sig_reply, 4, "T%2.2x", signal);
  309. gdb_put_packet(connection, sig_reply, 3);
  310. gdb_connection->frontend_state = TARGET_HALTED;
  311. }
  312. break;
  313. case TARGET_EVENT_RESUMED:
  314. if (gdb_connection->frontend_state == TARGET_HALTED)
  315. {
  316. gdb_connection->frontend_state = TARGET_RUNNING;
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322. return ERROR_OK;
  323. }
  324. int gdb_new_connection(connection_t *connection)
  325. {
  326. gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
  327. gdb_service_t *gdb_service = connection->service->priv;
  328. int retval;
  329. int initial_ack;
  330. connection->priv = gdb_connection;
  331. /* initialize gdb connection information */
  332. gdb_connection->buf_p = gdb_connection->buffer;
  333. gdb_connection->buf_cnt = 0;
  334. gdb_connection->ctrl_c = 0;
  335. gdb_connection->frontend_state = TARGET_HALTED;
  336. gdb_connection->vflash_image = NULL;
  337. /* output goes through gdb connection */
  338. command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
  339. /* register callback to be informed about target events */
  340. target_register_event_callback(gdb_target_callback_event_handler, connection);
  341. /* a gdb session just attached, put the target in halt mode */
  342. if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
  343. (retval != ERROR_TARGET_ALREADY_HALTED))
  344. {
  345. ERROR("error when trying to halt target");
  346. exit(-1);
  347. }
  348. while (gdb_service->target->state != TARGET_HALTED)
  349. {
  350. gdb_service->target->type->poll(gdb_service->target);
  351. }
  352. /* remove the initial ACK from the incoming buffer */
  353. if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
  354. return retval;
  355. if (initial_ack != '+')
  356. gdb_putback_char(connection, initial_ack);
  357. return ERROR_OK;
  358. }
  359. int gdb_connection_closed(connection_t *connection)
  360. {
  361. gdb_service_t *gdb_service = connection->service->priv;
  362. gdb_connection_t *gdb_connection = connection->priv;
  363. /* see if an image built with vFlash commands is left */
  364. if (gdb_connection->vflash_image)
  365. {
  366. image_close(gdb_connection->vflash_image);
  367. free(gdb_connection->vflash_image);
  368. gdb_connection->vflash_image = NULL;
  369. }
  370. /* if this connection registered a debug-message receiver delete it */
  371. delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
  372. if (connection->priv)
  373. free(connection->priv);
  374. else
  375. ERROR("BUG: connection->priv == NULL");
  376. target_unregister_event_callback(gdb_target_callback_event_handler, connection);
  377. return ERROR_OK;
  378. }
  379. void gdb_send_error(connection_t *connection, u8 the_error)
  380. {
  381. char err[4];
  382. snprintf(err, 4, "E%2.2X", the_error );
  383. gdb_put_packet(connection, err, 3);
  384. }
  385. int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
  386. {
  387. char sig_reply[4];
  388. int signal;
  389. signal = gdb_last_signal(target);
  390. snprintf(sig_reply, 4, "S%2.2x", signal);
  391. gdb_put_packet(connection, sig_reply, 3);
  392. return ERROR_OK;
  393. }
  394. void gdb_str_to_target(target_t *target, char *str, char *tstr)
  395. {
  396. int str_len = strlen(str);
  397. int i;
  398. if (str_len % 2)
  399. {
  400. ERROR("BUG: gdb value with uneven number of characters encountered: %s", str);
  401. exit(-1);
  402. }
  403. if (target->endianness == TARGET_LITTLE_ENDIAN)
  404. {
  405. for (i = 0; i < str_len; i+=2)
  406. {
  407. tstr[str_len - i - 1] = str[i + 1];
  408. tstr[str_len - i - 2] = str[i];
  409. }
  410. }
  411. else
  412. {
  413. for (i = 0; i < str_len; i++)
  414. {
  415. tstr[i] = str[i];
  416. }
  417. }
  418. }
  419. void gdb_target_to_str(target_t *target, char *tstr, char *str)
  420. {
  421. int str_len = strlen(tstr);
  422. int i;
  423. if (str_len % 2)
  424. {
  425. ERROR("BUG: gdb value with uneven number of characters encountered");
  426. exit(-1);
  427. }
  428. if (target->endianness == TARGET_LITTLE_ENDIAN)
  429. {
  430. for (i = 0; i < str_len; i+=2)
  431. {
  432. str[str_len - i - 1] = tstr[i + 1];
  433. str[str_len - i - 2] = tstr[i];
  434. }
  435. }
  436. else
  437. {
  438. for (i = 0; i < str_len; i++)
  439. {
  440. str[i] = tstr[i];
  441. }
  442. }
  443. }
  444. int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
  445. {
  446. reg_t **reg_list;
  447. int reg_list_size;
  448. int retval;
  449. int reg_packet_size = 0;
  450. char *reg_packet;
  451. char *reg_packet_p;
  452. int i;
  453. DEBUG("-");
  454. if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  455. {
  456. switch (retval)
  457. {
  458. case ERROR_TARGET_NOT_HALTED:
  459. ERROR("gdb requested registers but we're not halted, dropping connection");
  460. return ERROR_SERVER_REMOTE_CLOSED;
  461. default:
  462. /* this is a bug condition - get_gdb_reg_list() may not return any other error */
  463. ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
  464. exit(-1);
  465. }
  466. }
  467. for (i = 0; i < reg_list_size; i++)
  468. {
  469. reg_packet_size += reg_list[i]->size;
  470. }
  471. reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
  472. reg_packet_p = reg_packet;
  473. for (i = 0; i < reg_list_size; i++)
  474. {
  475. char *hex_buf = buf_to_str(reg_list[i]->value, reg_list[i]->size, 16);
  476. DEBUG("hex_buf: %s", hex_buf);
  477. gdb_str_to_target(target, hex_buf, reg_packet_p);
  478. reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
  479. free(hex_buf);
  480. }
  481. reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
  482. DEBUG("reg_packet: %s", reg_packet_p);
  483. free(reg_packet_p);
  484. gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
  485. free(reg_packet);
  486. free(reg_list);
  487. return ERROR_OK;
  488. }
  489. int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  490. {
  491. int i;
  492. reg_t **reg_list;
  493. int reg_list_size;
  494. int retval;
  495. char *packet_p;
  496. DEBUG("-");
  497. /* skip command character */
  498. packet++;
  499. packet_size--;
  500. if (packet_size % 2)
  501. {
  502. WARNING("GDB set_registers packet with uneven characters received, dropping connection");
  503. return ERROR_SERVER_REMOTE_CLOSED;
  504. }
  505. if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  506. {
  507. switch (retval)
  508. {
  509. case ERROR_TARGET_NOT_HALTED:
  510. ERROR("gdb tried to registers but we're not halted, dropping connection");
  511. return ERROR_SERVER_REMOTE_CLOSED;
  512. default:
  513. /* this is a bug condition - get_gdb_reg_list() may not return any other error */
  514. ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
  515. exit(-1);
  516. }
  517. }
  518. packet_p = packet;
  519. for (i = 0; i < reg_list_size; i++)
  520. {
  521. u8 *bin_buf;
  522. char *hex_buf;
  523. reg_arch_type_t *arch_type;
  524. /* convert from GDB-string (target-endian) to hex-string (big-endian) */
  525. hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
  526. gdb_target_to_str(target, packet_p, hex_buf);
  527. /* convert hex-string to binary buffer */
  528. bin_buf = malloc(CEIL(reg_list[i]->size, 8));
  529. str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
  530. /* get register arch_type, and call set method */
  531. arch_type = register_get_arch_type(reg_list[i]->arch_type);
  532. if (arch_type == NULL)
  533. {
  534. ERROR("BUG: encountered unregistered arch type");
  535. exit(-1);
  536. }
  537. arch_type->set(reg_list[i], bin_buf);
  538. /* advance packet pointer */
  539. packet_p += (CEIL(reg_list[i]->size, 8) * 2);
  540. free(bin_buf);
  541. free(hex_buf);
  542. }
  543. /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
  544. free(reg_list);
  545. gdb_put_packet(connection, "OK", 2);
  546. return ERROR_OK;
  547. }
  548. int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  549. {
  550. char *reg_packet;
  551. int reg_num = strtoul(packet + 1, NULL, 16);
  552. reg_t **reg_list;
  553. int reg_list_size;
  554. int retval;
  555. char *hex_buf;
  556. DEBUG("-");
  557. if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  558. {
  559. switch (retval)
  560. {
  561. case ERROR_TARGET_NOT_HALTED:
  562. ERROR("gdb requested registers but we're not halted, dropping connection");
  563. return ERROR_SERVER_REMOTE_CLOSED;
  564. default:
  565. /* this is a bug condition - get_gdb_reg_list() may not return any other error */
  566. ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
  567. exit(-1);
  568. }
  569. }
  570. if (reg_list_size <= reg_num)
  571. {
  572. ERROR("gdb requested a non-existing register");
  573. exit(-1);
  574. }
  575. reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
  576. hex_buf = buf_to_str(reg_list[reg_num]->value, reg_list[reg_num]->size, 16);
  577. gdb_str_to_target(target, hex_buf, reg_packet);
  578. gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
  579. free(reg_list);
  580. free(reg_packet);
  581. free(hex_buf);
  582. return ERROR_OK;
  583. }
  584. int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  585. {
  586. char *separator;
  587. char *hex_buf;
  588. u8 *bin_buf;
  589. int reg_num = strtoul(packet + 1, &separator, 16);
  590. reg_t **reg_list;
  591. int reg_list_size;
  592. int retval;
  593. reg_arch_type_t *arch_type;
  594. DEBUG("-");
  595. if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
  596. {
  597. switch (retval)
  598. {
  599. case ERROR_TARGET_NOT_HALTED:
  600. ERROR("gdb tried to set a register but we're not halted, dropping connection");
  601. return ERROR_SERVER_REMOTE_CLOSED;
  602. default:
  603. /* this is a bug condition - get_gdb_reg_list() may not return any other error */
  604. ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
  605. exit(-1);
  606. }
  607. }
  608. if (reg_list_size < reg_num)
  609. {
  610. ERROR("gdb requested a non-existing register");
  611. return ERROR_SERVER_REMOTE_CLOSED;
  612. }
  613. if (*separator != '=')
  614. {
  615. ERROR("GDB 'set register packet', but no '=' following the register number");
  616. return ERROR_SERVER_REMOTE_CLOSED;
  617. }
  618. /* convert from GDB-string (target-endian) to hex-string (big-endian) */
  619. hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
  620. gdb_target_to_str(target, separator + 1, hex_buf);
  621. /* convert hex-string to binary buffer */
  622. bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
  623. str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
  624. /* get register arch_type, and call set method */
  625. arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
  626. if (arch_type == NULL)
  627. {
  628. ERROR("BUG: encountered unregistered arch type");
  629. exit(-1);
  630. }
  631. arch_type->set(reg_list[reg_num], bin_buf);
  632. gdb_put_packet(connection, "OK", 2);
  633. free(bin_buf);
  634. free(hex_buf);
  635. free(reg_list);
  636. return ERROR_OK;
  637. }
  638. int gdb_memory_packet_error(connection_t *connection, int retval)
  639. {
  640. switch (retval)
  641. {
  642. case ERROR_TARGET_NOT_HALTED:
  643. ERROR("gdb tried to read memory but we're not halted, dropping connection");
  644. return ERROR_SERVER_REMOTE_CLOSED;
  645. break;
  646. case ERROR_TARGET_DATA_ABORT:
  647. gdb_send_error(connection, EIO);
  648. break;
  649. case ERROR_TARGET_TRANSLATION_FAULT:
  650. gdb_send_error(connection, EFAULT);
  651. break;
  652. case ERROR_TARGET_UNALIGNED_ACCESS:
  653. gdb_send_error(connection, EFAULT);
  654. break;
  655. default:
  656. ERROR("BUG: unexpected error %i", retval);
  657. exit(-1);
  658. }
  659. return ERROR_OK;
  660. }
  661. int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  662. {
  663. char *separator;
  664. u32 addr = 0;
  665. u32 len = 0;
  666. u8 *buffer;
  667. char *hex_buffer;
  668. int i;
  669. int retval;
  670. /* skip command character */
  671. packet++;
  672. addr = strtoul(packet, &separator, 16);
  673. if (*separator != ',')
  674. {
  675. ERROR("incomplete read memory packet received, dropping connection");
  676. return ERROR_SERVER_REMOTE_CLOSED;
  677. }
  678. len = strtoul(separator+1, NULL, 16);
  679. buffer = malloc(len);
  680. DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
  681. switch (len)
  682. {
  683. case 4:
  684. if ((addr % 4) == 0)
  685. retval = target->type->read_memory(target, addr, 4, 1, buffer);
  686. else
  687. retval = target->type->read_memory(target, addr, 1, len, buffer);
  688. break;
  689. case 2:
  690. if ((addr % 2) == 0)
  691. retval = target->type->read_memory(target, addr, 2, 1, buffer);
  692. else
  693. retval = target->type->read_memory(target, addr, 1, len, buffer);
  694. break;
  695. default:
  696. if (((addr % 4) == 0) && ((len % 4) == 0))
  697. retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
  698. else
  699. retval = target->type->read_memory(target, addr, 1, len, buffer);
  700. }
  701. if (retval == ERROR_OK)
  702. {
  703. hex_buffer = malloc(len * 2 + 1);
  704. for (i=0; i<len; i++)
  705. snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
  706. gdb_put_packet(connection, hex_buffer, len * 2);
  707. free(hex_buffer);
  708. }
  709. else
  710. {
  711. if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
  712. return retval;
  713. }
  714. free(buffer);
  715. return ERROR_OK;
  716. }
  717. int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  718. {
  719. char *separator;
  720. u32 addr = 0;
  721. u32 len = 0;
  722. u8 *buffer;
  723. int i;
  724. int retval;
  725. /* skip command character */
  726. packet++;
  727. addr = strtoul(packet, &separator, 16);
  728. if (*separator != ',')
  729. {
  730. ERROR("incomplete write memory packet received, dropping connection");
  731. return ERROR_SERVER_REMOTE_CLOSED;
  732. }
  733. len = strtoul(separator+1, &separator, 16);
  734. if (*(separator++) != ':')
  735. {
  736. ERROR("incomplete write memory packet received, dropping connection");
  737. return ERROR_SERVER_REMOTE_CLOSED;
  738. }
  739. buffer = malloc(len);
  740. DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
  741. for (i=0; i<len; i++)
  742. {
  743. u32 tmp;
  744. sscanf(separator + 2*i, "%2x", &tmp);
  745. buffer[i] = tmp;
  746. }
  747. retval = ERROR_OK;
  748. switch (len)
  749. {
  750. /* handle sized writes */
  751. case 4:
  752. if ((addr % 4) == 0)
  753. retval = target->type->write_memory(target, addr, 4, 1, buffer);
  754. else
  755. retval = target->type->write_memory(target, addr, 1, len, buffer);
  756. break;
  757. case 2:
  758. if ((addr % 2) == 0)
  759. retval = target->type->write_memory(target, addr, 2, 1, buffer);
  760. else
  761. retval = target->type->write_memory(target, addr, 1, len, buffer);
  762. break;
  763. case 3:
  764. case 1:
  765. retval = target->type->write_memory(target, addr, 1, len, buffer);
  766. break;
  767. /* handle bulk writes */
  768. default:
  769. retval = target_write_buffer(target, addr, len, buffer);
  770. break;
  771. }
  772. if (retval == ERROR_OK)
  773. {
  774. gdb_put_packet(connection, "OK", 2);
  775. }
  776. else
  777. {
  778. if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
  779. return retval;
  780. }
  781. free(buffer);
  782. return ERROR_OK;
  783. }
  784. int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  785. {
  786. char *separator;
  787. u32 addr = 0;
  788. u32 len = 0;
  789. u8 *buffer;
  790. int retval;
  791. /* skip command character */
  792. packet++;
  793. addr = strtoul(packet, &separator, 16);
  794. if (*separator != ',')
  795. {
  796. ERROR("incomplete write memory binary packet received, dropping connection");
  797. return ERROR_SERVER_REMOTE_CLOSED;
  798. }
  799. len = strtoul(separator+1, &separator, 16);
  800. if (*(separator++) != ':')
  801. {
  802. ERROR("incomplete write memory binary packet received, dropping connection");
  803. return ERROR_SERVER_REMOTE_CLOSED;
  804. }
  805. retval = ERROR_OK;
  806. if( len ) {
  807. buffer = malloc(len);
  808. DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
  809. memcpy( buffer, separator, len );
  810. switch (len)
  811. {
  812. case 4:
  813. if ((addr % 4) == 0)
  814. retval = target->type->write_memory(target, addr, 4, 1, buffer);
  815. else
  816. retval = target->type->write_memory(target, addr, 1, len, buffer);
  817. break;
  818. case 2:
  819. if ((addr % 2) == 0)
  820. retval = target->type->write_memory(target, addr, 2, 1, buffer);
  821. else
  822. retval = target->type->write_memory(target, addr, 1, len, buffer);
  823. break;
  824. case 3:
  825. case 1:
  826. retval = target->type->write_memory(target, addr, 1, len, buffer);
  827. break;
  828. default:
  829. retval = target_write_buffer(target, addr, len, buffer);
  830. break;
  831. }
  832. free(buffer);
  833. }
  834. if (retval == ERROR_OK)
  835. {
  836. gdb_put_packet(connection, "OK", 2);
  837. }
  838. else
  839. {
  840. if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
  841. return retval;
  842. }
  843. return ERROR_OK;
  844. }
  845. void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  846. {
  847. int current = 0;
  848. u32 address = 0x0;
  849. DEBUG("-");
  850. if (packet_size > 1)
  851. {
  852. packet[packet_size] = 0;
  853. address = strtoul(packet + 1, NULL, 16);
  854. }
  855. else
  856. {
  857. current = 1;
  858. }
  859. if (packet[0] == 'c')
  860. {
  861. DEBUG("continue");
  862. target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
  863. }
  864. else if (packet[0] == 's')
  865. {
  866. DEBUG("step");
  867. target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
  868. }
  869. }
  870. int gdb_bp_wp_packet_error(connection_t *connection, int retval)
  871. {
  872. switch (retval)
  873. {
  874. case ERROR_TARGET_NOT_HALTED:
  875. ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
  876. return ERROR_SERVER_REMOTE_CLOSED;
  877. break;
  878. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  879. gdb_send_error(connection, EBUSY);
  880. break;
  881. default:
  882. ERROR("BUG: unexpected error %i", retval);
  883. exit(-1);
  884. }
  885. return ERROR_OK;
  886. }
  887. int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  888. {
  889. int type;
  890. enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
  891. enum watchpoint_rw wp_type;
  892. u32 address;
  893. u32 size;
  894. char *separator;
  895. int retval;
  896. DEBUG("-");
  897. type = strtoul(packet + 1, &separator, 16);
  898. if (type == 0) /* memory breakpoint */
  899. bp_type = BKPT_SOFT;
  900. else if (type == 1) /* hardware breakpoint */
  901. bp_type = BKPT_HARD;
  902. else if (type == 2) /* write watchpoint */
  903. wp_type = WPT_WRITE;
  904. else if (type == 3) /* read watchpoint */
  905. wp_type = WPT_READ;
  906. else if (type == 4) /* access watchpoint */
  907. wp_type = WPT_ACCESS;
  908. if (*separator != ',')
  909. {
  910. ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  911. return ERROR_SERVER_REMOTE_CLOSED;
  912. }
  913. address = strtoul(separator+1, &separator, 16);
  914. if (*separator != ',')
  915. {
  916. ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
  917. return ERROR_SERVER_REMOTE_CLOSED;
  918. }
  919. size = strtoul(separator+1, &separator, 16);
  920. switch (type)
  921. {
  922. case 0:
  923. case 1:
  924. if (packet[0] == 'Z')
  925. {
  926. if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
  927. {
  928. if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
  929. return retval;
  930. }
  931. else
  932. {
  933. gdb_put_packet(connection, "OK", 2);
  934. }
  935. }
  936. else
  937. {
  938. breakpoint_remove(target, address);
  939. gdb_put_packet(connection, "OK", 2);
  940. }
  941. break;
  942. case 2:
  943. case 3:
  944. case 4:
  945. {
  946. if (packet[0] == 'Z')
  947. {
  948. if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
  949. {
  950. if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
  951. return retval;
  952. }
  953. else
  954. {
  955. gdb_put_packet(connection, "OK", 2);
  956. }
  957. }
  958. else
  959. {
  960. watchpoint_remove(target, address);
  961. gdb_put_packet(connection, "OK", 2);
  962. }
  963. break;
  964. }
  965. default:
  966. break;
  967. }
  968. return ERROR_OK;
  969. }
  970. void gdb_query_packet(connection_t *connection, char *packet, int packet_size)
  971. {
  972. command_context_t *cmd_ctx = connection->cmd_ctx;
  973. if (strstr(packet, "qRcmd,"))
  974. {
  975. if (packet_size > 6)
  976. {
  977. char *cmd;
  978. int i;
  979. cmd = malloc((packet_size - 6)/2 + 1);
  980. for (i=0; i < (packet_size - 6)/2; i++)
  981. {
  982. u32 tmp;
  983. sscanf(packet + 6 + 2*i, "%2x", &tmp);
  984. cmd[i] = tmp;
  985. }
  986. cmd[(packet_size - 6)/2] = 0x0;
  987. command_run_line(cmd_ctx, cmd);
  988. free(cmd);
  989. }
  990. gdb_put_packet(connection, "OK", 2);
  991. return;
  992. }
  993. gdb_put_packet(connection, "", 0);
  994. }
  995. int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
  996. {
  997. gdb_connection_t *gdb_connection = connection->priv;
  998. gdb_service_t *gdb_service = connection->service->priv;
  999. int result;
  1000. if (strstr(packet, "vFlashErase:"))
  1001. {
  1002. unsigned long addr;
  1003. unsigned long length;
  1004. char *parse = packet + 12;
  1005. if (*parse == '\0')
  1006. {
  1007. ERROR("incomplete vFlashErase packet received, dropping connection");
  1008. return ERROR_SERVER_REMOTE_CLOSED;
  1009. }
  1010. addr = strtoul(parse, &parse, 16);
  1011. if (*(parse++) != ',' || *parse == '\0')
  1012. {
  1013. ERROR("incomplete vFlashErase packet received, dropping connection");
  1014. return ERROR_SERVER_REMOTE_CLOSED;
  1015. }
  1016. length = strtoul(parse, &parse, 16);
  1017. if (*parse != '\0')
  1018. {
  1019. ERROR("incomplete vFlashErase packet received, dropping connection");
  1020. return ERROR_SERVER_REMOTE_CLOSED;
  1021. }
  1022. /* perform erase */
  1023. if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
  1024. {
  1025. /* GDB doesn't evaluate the actual error number returned,
  1026. * treat a failed erase as an I/O error
  1027. */
  1028. gdb_send_error(connection, EIO);
  1029. ERROR("flash_erase returned %i", result);
  1030. }
  1031. else
  1032. gdb_put_packet(connection, "OK", 2);
  1033. return ERROR_OK;
  1034. }
  1035. if (strstr(packet, "vFlashWrite:"))
  1036. {
  1037. unsigned long addr;
  1038. unsigned long length;
  1039. char *parse = packet + 12;
  1040. if (*parse == '\0')
  1041. {
  1042. ERROR("incomplete vFlashErase packet received, dropping connection");
  1043. return ERROR_SERVER_REMOTE_CLOSED;
  1044. }
  1045. addr = strtoul(parse, &parse, 16);
  1046. if (*(parse++) != ':')
  1047. {
  1048. ERROR("incomplete vFlashErase packet received, dropping connection");
  1049. return ERROR_SERVER_REMOTE_CLOSED;
  1050. }
  1051. length = packet_size - (parse - packet);
  1052. /* create a new image if there isn't already one */
  1053. if (gdb_connection->vflash_image == NULL)
  1054. {
  1055. gdb_connection->vflash_image = malloc(sizeof(image_t));
  1056. image_open(gdb_connection->vflash_image, "", "build");
  1057. }
  1058. /* create new section with content from packet buffer */
  1059. image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
  1060. gdb_put_packet(connection, "OK", 2);
  1061. return ERROR_OK;
  1062. }
  1063. if (!strcmp(packet, "vFlashDone"))
  1064. {
  1065. u32 image_size;
  1066. char *error_str;
  1067. u32 *failed = malloc(sizeof(u32) * gdb_connection->vflash_image->num_sections);
  1068. /* process the flashing buffer */
  1069. if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &image_size, &error_str, failed)) != ERROR_OK)
  1070. {
  1071. if (result == ERROR_FLASH_DST_OUT_OF_BANK)
  1072. gdb_put_packet(connection, "E.memtype", 9);
  1073. else
  1074. gdb_send_error(connection, EIO);
  1075. ERROR("flash writing failed: %s", error_str);
  1076. free(error_str);
  1077. }
  1078. else
  1079. {
  1080. DEBUG("wrote %u bytes from vFlash image to flash", image_size);
  1081. gdb_put_packet(connection, "OK", 2);
  1082. }
  1083. free(failed);
  1084. image_close(gdb_connection->vflash_image);
  1085. free(gdb_connection->vflash_image);
  1086. gdb_connection->vflash_image = NULL;
  1087. return ERROR_OK;
  1088. }
  1089. gdb_put_packet(connection, "", 0);
  1090. return ERROR_OK;
  1091. }
  1092. int gdb_input(connection_t *connection)
  1093. {
  1094. gdb_service_t *gdb_service = connection->service->priv;
  1095. target_t *target = gdb_service->target;
  1096. char packet[GDB_BUFFER_SIZE];
  1097. int packet_size;
  1098. int retval;
  1099. gdb_connection_t *gdb_con = connection->priv;
  1100. /* drain input buffer */
  1101. do
  1102. {
  1103. packet_size = GDB_BUFFER_SIZE-1;
  1104. if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
  1105. {
  1106. switch (retval)
  1107. {
  1108. case ERROR_GDB_BUFFER_TOO_SMALL:
  1109. ERROR("BUG: buffer supplied for gdb packet was too small");
  1110. exit(-1);
  1111. case ERROR_SERVER_REMOTE_CLOSED:
  1112. return ERROR_SERVER_REMOTE_CLOSED;
  1113. default:
  1114. ERROR("BUG: unexpected error");
  1115. exit(-1);
  1116. }
  1117. }
  1118. /* terminate with zero */
  1119. packet[packet_size] = 0;
  1120. DEBUG("recevied packet: '%s'", packet);
  1121. if (packet_size > 0)
  1122. {
  1123. retval = ERROR_OK;
  1124. switch (packet[0])
  1125. {
  1126. case 'H':
  1127. /* Hct... -- set thread
  1128. * we don't have threads, send empty reply */
  1129. gdb_put_packet(connection, NULL, 0);
  1130. break;
  1131. case 'q':
  1132. gdb_query_packet(connection, packet, packet_size);
  1133. break;
  1134. case 'g':
  1135. retval = gdb_get_registers_packet(connection, target, packet, packet_size);
  1136. break;
  1137. case 'G':
  1138. retval = gdb_set_registers_packet(connection, target, packet, packet_size);
  1139. break;
  1140. case 'p':
  1141. retval = gdb_get_register_packet(connection, target, packet, packet_size);
  1142. break;
  1143. case 'P':
  1144. retval = gdb_set_register_packet(connection, target, packet, packet_size);
  1145. break;
  1146. case 'm':
  1147. retval = gdb_read_memory_packet(connection, target, packet, packet_size);
  1148. break;
  1149. case 'M':
  1150. retval = gdb_write_memory_packet(connection, target, packet, packet_size);
  1151. break;
  1152. case 'z':
  1153. case 'Z':
  1154. retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
  1155. break;
  1156. case '?':
  1157. gdb_last_signal_packet(connection, target, packet, packet_size);
  1158. break;
  1159. case 'c':
  1160. case 's':
  1161. gdb_step_continue_packet(connection, target, packet, packet_size);
  1162. break;
  1163. case 'v':
  1164. retval = gdb_v_packet(connection, target, packet, packet_size);
  1165. break;
  1166. case 'D':
  1167. target->type->resume(target, 1, 0, 1, 0);
  1168. gdb_put_packet(connection, "OK", 2);
  1169. break;
  1170. case 'X':
  1171. if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
  1172. return retval;
  1173. break;
  1174. case 'k':
  1175. gdb_put_packet(connection, "OK", 2);
  1176. return ERROR_SERVER_REMOTE_CLOSED;
  1177. default:
  1178. /* ignore unkown packets */
  1179. DEBUG("ignoring 0x%2.2x packet", packet[0]);
  1180. gdb_put_packet(connection, NULL, 0);
  1181. break;
  1182. }
  1183. /* if a packet handler returned an error, exit input loop */
  1184. if (retval != ERROR_OK)
  1185. return retval;
  1186. }
  1187. if (gdb_con->ctrl_c)
  1188. {
  1189. if (target->state == TARGET_RUNNING)
  1190. {
  1191. target->type->halt(target);
  1192. gdb_con->ctrl_c = 0;
  1193. }
  1194. }
  1195. } while (gdb_con->buf_cnt > 0);
  1196. return ERROR_OK;
  1197. }
  1198. int gdb_init()
  1199. {
  1200. gdb_service_t *gdb_service;
  1201. target_t *target = targets;
  1202. int i = 0;
  1203. if (!target)
  1204. {
  1205. WARNING("no gdb ports allocated as no target has been specified");
  1206. return ERROR_OK;
  1207. }
  1208. if (gdb_port == 0)
  1209. {
  1210. WARNING("no gdb port specified, using default port 3333");
  1211. gdb_port = 3333;
  1212. }
  1213. while (target)
  1214. {
  1215. char service_name[8];
  1216. snprintf(service_name, 8, "gdb-%2.2i", i);
  1217. gdb_service = malloc(sizeof(gdb_service_t));
  1218. gdb_service->target = target;
  1219. add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
  1220. DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
  1221. i++;
  1222. target = target->next;
  1223. }
  1224. return ERROR_OK;
  1225. }
  1226. /* daemon configuration command gdb_port */
  1227. int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1228. {
  1229. if (argc == 0)
  1230. return ERROR_OK;
  1231. /* only if the port wasn't overwritten by cmdline */
  1232. if (gdb_port == 0)
  1233. gdb_port = strtoul(args[0], NULL, 0);
  1234. return ERROR_OK;
  1235. }
  1236. int gdb_register_commands(command_context_t *command_context)
  1237. {
  1238. register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
  1239. COMMAND_CONFIG, "");
  1240. return ERROR_OK;
  1241. }