1 /* -*- tab-width:8 -*- */
4 Copyright (C) 2011 Peter Zotov <whitequark@whitequark.org>
5 Use of this source code is governed by a BSD-style
6 license that can be found in the LICENSE file.
15 static const char hex[] = "0123456789abcdef";
17 int gdb_send_packet(int fd, char* data) {
18 unsigned length = strlen(data) + 5;
19 char* packet = malloc(length); /* '$' data (hex) '#' cksum (hex) */
21 memset(packet, 0, length);
26 for(int i = 0; i < strlen(data); i++) {
27 packet[i + 1] = data[i];
31 packet[length - 4] = '#';
32 packet[length - 3] = hex[cksum >> 4];
33 packet[length - 2] = hex[cksum & 0xf];
36 if(write(fd, packet, length) != length) {
42 if(read(fd, &ack, 1) != 1) {
54 #define ALLOC_STEP 1024
56 int gdb_recv_packet(int fd, char** buffer) {
57 unsigned packet_size = ALLOC_STEP + 1, packet_idx = 0;
59 char recv_cksum[3] = {0};
60 char* packet_buffer = malloc(packet_size);
75 if(read(fd, &c, 1) != 1) {
92 packet_buffer[packet_idx++] = c;
95 if(packet_idx == packet_size) {
96 packet_size += ALLOC_STEP;
97 packet_buffer = realloc(packet_buffer, packet_size);
114 uint8_t recv_cksum_int = strtoul(recv_cksum, NULL, 16);
115 if(recv_cksum_int != cksum) {
117 if(write(fd, &nack, 1) != 1) {
124 if(write(fd, &ack, 1) != 1) {
129 packet_buffer[packet_idx] = 0;
130 *buffer = packet_buffer;
135 int gdb_wait_for_interrupt(int fd) {
138 if(read(fd, &c, 1) != 1)
141 if(c == '\x03') // ^C