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.
 
 
 
 
 
 

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