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 "gdb-remote.h"
17 #include "stlink-hw.h"
19 static const char hex[] = "0123456789abcdef";
21 int serve(struct stlink* sl, int port);
23 int main(int argc, char** argv) {
25 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
29 struct stlink *sl = stlink_quirk_open(argv[2], 0);
33 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
34 stlink_enter_swd_mode(sl);
37 printf("Debugging ARM core %08x.\n", sl->core_id);
39 int port = atoi(argv[1]);
41 while(serve(sl, port) == 0);
48 int serve(struct stlink* sl, int port) {
49 int sock = socket(AF_INET, SOCK_STREAM, 0);
56 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
58 struct sockaddr_in serv_addr = {0};
59 serv_addr.sin_family = AF_INET;
60 serv_addr.sin_addr.s_addr = INADDR_ANY;
61 serv_addr.sin_port = htons(port);
63 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
68 if(listen(sock, 5) < 0) {
73 stlink_force_debug(sl);
76 printf("Listening at *:%d...\n", port);
78 int client = accept(sock, NULL, NULL);
86 printf("GDB connected.\n");
91 int status = gdb_recv_packet(client, &packet);
93 fprintf(stderr, "cannot recv: %d\n", status);
97 //printf("recv: %s\n", packet);
105 printf("Core running, waiting for interrupt.\n");
107 int status = gdb_wait_for_interrupt(client);
109 fprintf(stderr, "cannot wait for int: %d\n", status);
113 stlink_force_debug(sl);
115 reply = strdup("S05"); // TRAP
121 reply = strdup("S05"); // TRAP
125 reply = strdup("S05"); // TRAP
129 stlink_read_all_regs(sl);
131 reply = calloc(8 * 16 + 1, 1);
132 for(int i = 0; i < 16; i++)
133 sprintf(&reply[i * 8], "%08x", htonl(sl->reg.r[i]));
138 unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
141 stlink_read_reg(sl, id);
142 reg = htonl(sl->reg.r[id]);
143 } else if(id == 0x19) {
144 stlink_read_reg(sl, 16);
145 reg = htonl(sl->reg.xpsr);
147 reply = strdup("E00");
150 reply = calloc(8 + 1, 1);
151 sprintf(reply, "%08x", reg);
157 char* s_reg = &packet[1];
158 char* s_value = strstr(&packet[1], "=") + 1;
160 unsigned reg = strtoul(s_reg, NULL, 16);
161 unsigned value = strtoul(s_value, NULL, 16);
164 stlink_write_reg(sl, ntohl(value), reg);
165 } else if(reg == 0x19) {
166 stlink_write_reg(sl, ntohl(value), 16);
168 reply = strdup("E00");
172 reply = strdup("OK");
179 for(int i = 0; i < 16; i++) {
181 strncpy(str, &packet[1 + i * 8], 8);
182 uint32_t reg = strtoul(str, NULL, 16);
183 stlink_write_reg(sl, ntohl(reg), i);
186 reply = strdup("OK");
190 char* s_start = &packet[1];
191 char* s_count = strstr(&packet[1], ",") + 1;
193 stm32_addr_t start = strtoul(s_start, NULL, 16);
194 unsigned count = strtoul(s_count, NULL, 16);
196 unsigned adj_start = start % 4;
198 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
199 count : count + 4 - (count % 4));
201 reply = calloc(count * 2 + 1, 1);
202 for(int i = 0; i < count; i++) {
203 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
204 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
211 char* s_start = &packet[1];
212 char* s_count = strstr(&packet[1], ",") + 1;
213 char* hexdata = strstr(packet, ":") + 1;
215 stm32_addr_t start = strtoul(s_start, NULL, 16);
216 unsigned count = strtoul(s_count, NULL, 16);
218 for(int i = 0; i < count; i ++) {
219 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
220 uint8_t byte = strtoul(hex, NULL, 16);
224 if((count % 4) == 0 && (start % 4) == 0) {
225 stlink_write_mem32(sl, start, count);
227 stlink_write_mem8(sl, start, count);
230 reply = strdup("OK");
236 // After this function will be entered afterwards, the
237 // chip will be reset anyway. So this is a no-op.
248 //printf("send: %s\n", reply);
250 int result = gdb_send_packet(client, reply);
252 fprintf(stderr, "cannot send: %d\n", result);