summaryrefslogtreecommitdiff
path: root/src/st-util/gdb-remote.c
blob: bdf8afd24843d662f61c182d0a3bc8f31bc60ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2011 Peter Zotov <whitequark@whitequark.org>
 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#if defined(_WIN32)
#include <win32_socket.h>
#else
#include <unistd.h>
#include <sys/poll.h>
#endif

#include "gdb-remote.h"

static const char hex[] = "0123456789abcdef";

int gdb_send_packet(int fd, char* data) {
    unsigned int data_length = (unsigned int)strlen(data);
    int length = data_length + 4;
    char* packet = malloc(length); // '$' data (hex) '#' cksum (hex)

    memset(packet, 0, length);

    packet[0] = '$';

    uint8_t cksum = 0;

    for (unsigned int i = 0; i < data_length; i++) {
        packet[i + 1] = data[i];
        cksum += data[i];
    }

    packet[length - 3] = '#';
    packet[length - 2] = hex[cksum >> 4];
    packet[length - 1] = hex[cksum & 0xf];

    while (1) {
        if (write(fd, packet, length) != length) {
            free(packet);
            return(-2);
        }

        char ack;

        if (read(fd, &ack, 1) != 1) {
            free(packet);
            return(-2);
        }

        if (ack == '+') {
            free(packet);
            return(0);
        }
    }
}

#define ALLOC_STEP 1024

int gdb_recv_packet(int fd, char** buffer) {
    unsigned packet_size = ALLOC_STEP + 1, packet_idx = 0;
    uint8_t cksum = 0;
    char recv_cksum[3] = {0};
    char* packet_buffer = malloc(packet_size);
    unsigned state;

    if (packet_buffer == NULL) {
        return(-2);
    }

start:
    state = 0;
    packet_idx = 0;
    /*
     * 0: waiting $
     * 1: data, waiting #
     * 2: cksum 1
     * 3: cksum 2
     * 4: fin
     */

    char c;

    while (state != 4) {
        if (read(fd, &c, 1) != 1) {
            free(packet_buffer);
            return(-2);
        }

        switch (state) {
        case 0:

            if (c != '$') { /* ignore */
            } else {
                state = 1;
            }

            break;

        case 1:

            if (c == '#') {
                state = 2;
            } else {
                packet_buffer[packet_idx++] = c;
                cksum += c;

                if (packet_idx == packet_size) {
                    packet_size += ALLOC_STEP;
                    void* p = realloc(packet_buffer, packet_size);

                    if (p != NULL) {
                        packet_buffer = p;
                    } else {
                        free(packet_buffer);
                        return(-2);
                    }
                }
            }

            break;

        case 2:
            recv_cksum[0] = c;
            state = 3;
            break;

        case 3:
            recv_cksum[1] = c;
            state = 4;
            break;
        }
    }

    uint8_t recv_cksum_int = strtoul(recv_cksum, NULL, 16);

    if (recv_cksum_int != cksum) {
        char nack = '-';

        if (write(fd, &nack, 1) != 1) {
            free(packet_buffer);
            return(-2);
        }

        goto start;
    } else {
        char ack = '+';

        if (write(fd, &ack, 1) != 1) {
            free(packet_buffer);
            return(-2);
        }
    }

    packet_buffer[packet_idx] = 0;
    *buffer = packet_buffer;

    return(packet_idx);
}

/*
 * Here we skip any characters which are not \x03, GDB interrupt.
 * As we use the mode with ACK, in a (very unlikely) situation of a packet lost
 * because of this skipping, it will be resent anyway.
 */
int gdb_check_for_interrupt(int fd) {
    struct pollfd pfd;
    pfd.fd = fd;
    pfd.events = POLLIN;

    if (poll(&pfd, 1, 0) != 0) {
        char c;

        if (read(fd, &c, 1) != 1) {
            return(-2);
        }

        if (c == '\x03') {
            return(1); // ^C
        }
    }

    return(0);
}