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.
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include "gdb-remote.h"
18 #include "stlink-hw.h"
20 static const char hex[] = "0123456789abcdef";
22 int serve(struct stlink* sl, int port);
24 int main(int argc, char** argv) {
26 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
30 struct stlink *sl = stlink_quirk_open(argv[2], 0);
34 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
35 stlink_enter_swd_mode(sl);
38 printf("Debugging ARM core %08x.\n", sl->core_id);
40 int port = atoi(argv[1]);
42 while(serve(sl, port) == 0);
49 int serve(struct stlink* sl, int port) {
50 int sock = socket(AF_INET, SOCK_STREAM, 0);
57 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
59 struct sockaddr_in serv_addr = {0};
60 serv_addr.sin_family = AF_INET;
61 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
62 serv_addr.sin_port = htons(port);
64 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
69 if(listen(sock, 5) < 0) {
74 stlink_force_debug(sl);
77 printf("Listening at *:%d...\n", port);
79 int client = accept(sock, NULL, NULL);
87 printf("GDB connected.\n");
92 int status = gdb_recv_packet(client, &packet);
94 fprintf(stderr, "cannot recv: %d\n", status);
99 printf("recv: %s\n", packet);
108 printf("Core running, waiting for interrupt.\n");
110 int status = gdb_wait_for_interrupt(client);
112 fprintf(stderr, "cannot wait for int: %d\n", status);
116 stlink_force_debug(sl);
118 reply = strdup("S05"); // TRAP
124 reply = strdup("S05"); // TRAP
128 reply = strdup("S05"); // TRAP
132 stlink_read_all_regs(sl);
134 reply = calloc(8 * 16 + 1, 1);
135 for(int i = 0; i < 16; i++)
136 sprintf(&reply[i * 8], "%08x", htonl(sl->reg.r[i]));
141 unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
144 stlink_read_reg(sl, id);
145 reg = htonl(sl->reg.r[id]);
146 } else if(id == 0x19) {
147 stlink_read_reg(sl, 16);
148 reg = htonl(sl->reg.xpsr);
150 reply = strdup("E00");
153 reply = calloc(8 + 1, 1);
154 sprintf(reply, "%08x", reg);
160 char* s_reg = &packet[1];
161 char* s_value = strstr(&packet[1], "=") + 1;
163 unsigned reg = strtoul(s_reg, NULL, 16);
164 unsigned value = strtoul(s_value, NULL, 16);
167 stlink_write_reg(sl, ntohl(value), reg);
168 } else if(reg == 0x19) {
169 stlink_write_reg(sl, ntohl(value), 16);
171 reply = strdup("E00");
175 reply = strdup("OK");
182 for(int i = 0; i < 16; i++) {
184 strncpy(str, &packet[1 + i * 8], 8);
185 uint32_t reg = strtoul(str, NULL, 16);
186 stlink_write_reg(sl, ntohl(reg), i);
189 reply = strdup("OK");
193 char* s_start = &packet[1];
194 char* s_count = strstr(&packet[1], ",") + 1;
196 stm32_addr_t start = strtoul(s_start, NULL, 16);
197 unsigned count = strtoul(s_count, NULL, 16);
199 unsigned adj_start = start % 4;
201 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
202 count : count + 4 - (count % 4));
204 reply = calloc(count * 2 + 1, 1);
205 for(int i = 0; i < count; i++) {
206 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
207 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
214 char* s_start = &packet[1];
215 char* s_count = strstr(&packet[1], ",") + 1;
216 char* hexdata = strstr(packet, ":") + 1;
218 stm32_addr_t start = strtoul(s_start, NULL, 16);
219 unsigned count = strtoul(s_count, NULL, 16);
221 for(int i = 0; i < count; i ++) {
222 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
223 uint8_t byte = strtoul(hex, NULL, 16);
227 if((count % 4) == 0 && (start % 4) == 0) {
228 stlink_write_mem32(sl, start, count);
230 stlink_write_mem8(sl, start, count);
233 reply = strdup("OK");
239 // After this function will be entered afterwards, the
240 // chip will be reset anyway. So this is a no-op.
252 printf("send: %s\n", reply);
255 int result = gdb_send_packet(client, reply);
257 fprintf(stderr, "cannot send: %d\n", result);