1 /* MSPDebug - debugging tool for MSP430 MCUs
2  * Copyright (C) 2009, 2010 Daniel Beer
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdint.h>
26 
27 #include "sockets.h"
28 #include "device.h"
29 #include "util.h"
30 #include "opdb.h"
31 #include "gdb.h"
32 #include "output.h"
33 #include "reader.h"
34 #include "expr.h"
35 #include "gdb_proto.h"
36 #include "ctrlc.h"
37 
38 static int register_bytes;
39 
40 /************************************************************************
41  * GDB server
42  */
43 
read_registers(struct gdb_data * data)44 static int read_registers(struct gdb_data *data)
45 {
46 	address_t regs[DEVICE_NUM_REGS];
47 	int i;
48 
49 	printc("Reading registers\n");
50 	if (device_getregs(regs) < 0)
51 		return gdb_send(data, "E00");
52 
53 	gdb_packet_start(data);
54 
55 	for (i = 0; i < DEVICE_NUM_REGS; i++) {
56 		address_t value = regs[i];
57 		int j;
58 
59 		for (j = 0; j < register_bytes; j++) {
60 			gdb_printf(data, "%02x", value & 0xff);
61 			value >>= 8;
62 		}
63 	}
64 
65 	gdb_packet_end(data);
66 	return gdb_flush_ack(data);
67 }
68 
69 struct monitor_buf {
70 	char    buf[GDB_MAX_XFER];
71 	int     len;
72 	int	trunc;
73 };
74 
monitor_capture(void * user_data,const char * text)75 static void monitor_capture(void *user_data, const char *text)
76 {
77 	struct monitor_buf *mb = (struct monitor_buf *)user_data;
78 	int len = strlen(text);
79 
80 	if (mb->trunc)
81 		return;
82 
83 	if (mb->len + len + 64 > sizeof(mb->buf)) {
84 		text = "...<truncated>";
85 		len = strlen(text);
86 		mb->trunc = 1;
87 	}
88 
89 	memcpy(mb->buf + mb->len, text, len);
90 	mb->len += len;
91 	mb->buf[mb->len++] = '\n';
92 }
93 
monitor_command(struct gdb_data * data,char * buf)94 static int monitor_command(struct gdb_data *data, char *buf)
95 {
96 	char cmd[128];
97 	int len = 0;
98 	int i;
99 	struct monitor_buf mbuf;
100 
101 	while (len + 1 < sizeof(cmd) && *buf && buf[1]) {
102 		if (len + 1 >= sizeof(cmd))
103 			break;
104 
105 		cmd[len++] = (hexval(buf[0]) << 4) | hexval(buf[1]);
106 		buf += 2;
107 	}
108 	cmd[len] = 0;
109 
110 	printc("Monitor command received: %s\n", cmd);
111 
112 	mbuf.len = 0;
113 	mbuf.trunc = 0;
114 	capture_start(monitor_capture, &mbuf);
115 	process_command(cmd);
116 	capture_end();
117 
118 	if (!mbuf.len)
119 		return gdb_send(data, "OK");
120 
121 	gdb_packet_start(data);
122 	for (i = 0; i < mbuf.len; i++)
123 		gdb_printf(data, "%02x", mbuf.buf[i]);
124 	gdb_packet_end(data);
125 
126 	return gdb_flush_ack(data);
127 }
128 
write_registers(struct gdb_data * data,char * buf)129 static int write_registers(struct gdb_data *data, char *buf)
130 {
131 	address_t regs[DEVICE_NUM_REGS];
132 	int nibbles = 4;
133 	size_t len = strlen(buf);
134 
135 	if (len >= DEVICE_NUM_REGS * 8)
136 		nibbles = 8;
137 
138 	if (len < DEVICE_NUM_REGS * nibbles) {
139 		printc_err("write_registers: short argument\n");
140 		return gdb_send(data, "E00");
141 	}
142 
143 	printc("Writing registers (%d bits each)\n", nibbles * 4);
144 	for (int i = 0; i < DEVICE_NUM_REGS; i++) {
145 		uint32_t r = 0;
146 
147 		for (int j = 0; j < nibbles; j++)
148 			r = (r << 4) |
149 				hexval(buf[i * nibbles +
150 					nibbles - 1 - (j ^ 1)]);
151 		regs[i] = r;
152 	}
153 
154 	if (device_setregs(regs) < 0)
155 		return gdb_send(data, "E00");
156 
157 	return gdb_send(data, "OK");
158 }
159 
read_memory(struct gdb_data * data,char * text)160 static int read_memory(struct gdb_data *data, char *text)
161 {
162 	char *length_text = strchr(text, ',');
163 	address_t length, addr;
164 	uint8_t buf[GDB_MAX_XFER];
165 	int i;
166 
167 	if (!length_text) {
168 		printc_err("gdb: malformed memory read request\n");
169 		return gdb_send(data, "E00");
170 	}
171 
172 	*(length_text++) = 0;
173 
174 	length = strtoul(length_text, NULL, 16);
175 	addr = strtoul(text, NULL, 16);
176 
177 	if (length > sizeof(buf))
178 		length = sizeof(buf);
179 
180 	printc("Reading %4d bytes from 0x%04x\n", length, addr);
181 
182 	if (device_readmem(addr, buf, length) < 0)
183 		return gdb_send(data, "E00");
184 
185 	gdb_packet_start(data);
186 	for (i = 0; i < length; i++)
187 		gdb_printf(data, "%02x", buf[i]);
188 	gdb_packet_end(data);
189 
190 	return gdb_flush_ack(data);
191 }
192 
write_memory(struct gdb_data * data,char * text)193 static int write_memory(struct gdb_data *data, char *text)
194 {
195 	char *data_text = strchr(text, ':');
196 	char *length_text = strchr(text, ',');
197 	address_t length, addr;
198 	uint8_t buf[GDB_MAX_XFER];
199 	int buflen = 0;
200 
201 	if (!(data_text && length_text)) {
202 		printc_err("gdb: malformed memory write request\n");
203 		return gdb_send(data, "E00");
204 	}
205 
206 	*(data_text++) = 0;
207 	*(length_text++) = 0;
208 
209 	length = strtoul(length_text, NULL, 16);
210 	addr = strtoul(text, NULL, 16);
211 
212 	while (buflen < sizeof(buf) && *data_text && data_text[1]) {
213 		buf[buflen++] = (hexval(data_text[0]) << 4) |
214 			hexval(data_text[1]);
215 		data_text += 2;
216 	}
217 
218 	if (buflen != length) {
219 		printc_err("gdb: length mismatch\n");
220 		return gdb_send(data, "E00");
221 	}
222 
223 	printc("Writing %4d bytes to 0x%04x\n", length, addr);
224 
225 	if (device_writemem(addr, buf, buflen) < 0)
226 		return gdb_send(data, "E00");
227 
228 	return gdb_send(data, "OK");
229 }
230 
run_set_pc(char * buf)231 static int run_set_pc(char *buf)
232 {
233 	address_t regs[DEVICE_NUM_REGS];
234 
235 	if (!*buf)
236 		return 0;
237 
238 	if (device_getregs(regs) < 0)
239 		return -1;
240 
241 	regs[0] = strtoul(buf, NULL, 16);
242 	return device_setregs(regs);
243 }
244 
run_final_status(struct gdb_data * data)245 static int run_final_status(struct gdb_data *data)
246 {
247 	address_t regs[DEVICE_NUM_REGS];
248 	int i;
249 
250 	if (device_getregs(regs) < 0)
251 		return gdb_send(data, "E00");
252 
253 	gdb_packet_start(data);
254 	gdb_printf(data, "T05");
255 	for (i = 0; i < 16; i++) {
256 		address_t value = regs[i];
257 		int j;
258 
259 		/* NOTE: this only gives GDB the lower 16 bits of each
260 		 *       register. It complains if we give the full data.
261 		 */
262 		gdb_printf(data, "%02x:", i);
263 		for (j = 0; j < register_bytes; j++) {
264 			gdb_printf(data, "%02x", value & 0xff);
265 			value >>= 8;
266 		}
267 		gdb_printf(data, ";");
268 	}
269 	gdb_packet_end(data);
270 
271 	return gdb_flush_ack(data);
272 }
273 
single_step(struct gdb_data * data,char * buf)274 static int single_step(struct gdb_data *data, char *buf)
275 {
276 	printc("Single stepping\n");
277 
278 	if (run_set_pc(buf) < 0 ||
279 	    device_ctl(DEVICE_CTL_STEP) < 0)
280 		gdb_send(data, "E00");
281 
282 	return run_final_status(data);
283 }
284 
run(struct gdb_data * data,char * buf)285 static int run(struct gdb_data *data, char *buf)
286 {
287 	printc("Running\n");
288 
289 	if (run_set_pc(buf) < 0 ||
290 	    device_ctl(DEVICE_CTL_RUN) < 0)
291 		return gdb_send(data, "E00");
292 
293 	for (;;) {
294 		device_status_t status = device_poll();
295 
296 		if (status == DEVICE_STATUS_ERROR)
297 			return gdb_send(data, "E00");
298 
299 		if (status == DEVICE_STATUS_HALTED) {
300 			printc("Target halted\n");
301 			goto out;
302 		}
303 
304 		if (status == DEVICE_STATUS_INTR)
305 			goto out;
306 
307 		while (gdb_peek(data, 0)) {
308 			int c = gdb_getc(data);
309 
310 			if (c < 0)
311 				return -1;
312 
313 			if (c == 3) {
314 				printc("Interrupted by gdb\n");
315 				goto out;
316 			}
317 		}
318 	}
319 
320  out:
321 	if (device_ctl(DEVICE_CTL_HALT) < 0)
322 		return gdb_send(data, "E00");
323 
324 	return run_final_status(data);
325 }
326 
set_breakpoint(struct gdb_data * data,int enable,char * buf)327 static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
328 {
329 	char *parts[2];
330 	address_t addr;
331 	device_bptype_t type;
332 	int i;
333 
334 	/* Break up the arguments */
335 	for (i = 0; i < 2; i++)
336 		parts[i] = strsep(&buf, ",");
337 
338 	/* Make sure there's a type argument */
339 	if (!parts[0]) {
340 		printc_err("gdb: breakpoint requested with no type\n");
341 		return gdb_send(data, "E00");
342 	}
343 
344 	switch (atoi(parts[0])) {
345 	case 0:
346 	case 1:
347 		type = DEVICE_BPTYPE_BREAK;
348 		break;
349 
350 	case 2:
351 		type = DEVICE_BPTYPE_WRITE;
352 		break;
353 
354 	case 3:
355 		type = DEVICE_BPTYPE_READ;
356 		break;
357 
358 	case 4:
359 		type = DEVICE_BPTYPE_WATCH;
360 		break;
361 
362 	default:
363 		printc_err("gdb: unsupported breakpoint type: %s\n",
364 			parts[0]);
365 		return gdb_send(data, "");
366 	}
367 
368 	/* There needs to be an address specified */
369 	if (!parts[1]) {
370 		printc_err("gdb: breakpoint address missing\n");
371 		return gdb_send(data, "E00");
372 	}
373 
374 	/* Parse the breakpoint address */
375 	addr = strtoul(parts[1], NULL, 16);
376 
377 	if (enable) {
378 		if (device_setbrk(device_default, -1, 1, addr, type) < 0) {
379 			printc_err("gdb: can't add breakpoint at "
380 				"0x%04x\n", addr);
381 			return gdb_send(data, "E00");
382 		}
383 
384 		printc("Breakpoint set at 0x%04x\n", addr);
385 	} else {
386 		device_setbrk(device_default, -1, 0, addr, type);
387 		printc("Breakpoint cleared at 0x%04x\n", addr);
388 	}
389 
390 	return gdb_send(data, "OK");
391 }
392 
restart_program(struct gdb_data * data)393 static int restart_program(struct gdb_data *data)
394 {
395 	if (device_ctl(DEVICE_CTL_RESET) < 0)
396 		return gdb_send(data, "E00");
397 
398 	return gdb_send(data, "OK");
399 }
400 
gdb_send_empty_threadlist(struct gdb_data * data)401 static int gdb_send_empty_threadlist(struct gdb_data *data)
402 {
403 	return gdb_send(data, "<?xml version=\"1.0\"?><threads></threads>");
404 }
405 
gdb_send_supported(struct gdb_data * data)406 static int gdb_send_supported(struct gdb_data *data)
407 {
408 	gdb_packet_start(data);
409 	gdb_printf(data, "PacketSize=%x", GDB_MAX_XFER * 2);
410 	gdb_packet_end(data);
411 	return gdb_flush_ack(data);
412 }
413 
process_gdb_command(struct gdb_data * data,char * buf)414 static int process_gdb_command(struct gdb_data *data, char *buf)
415 {
416 #ifdef DEBUG_GDB
417 	printc("process_gdb_command: %s\n", buf);
418 #endif
419 	switch (buf[0]) {
420 	case '?': /* Return target halt reason */
421 		return run_final_status(data);
422 
423 	case 'z':
424 	case 'Z':
425 		return set_breakpoint(data, buf[0] == 'Z', buf + 1);
426 
427 	case 'r': /* Restart */
428 	case 'R':
429 		return restart_program(data);
430 
431 	case 'g': /* Read registers */
432 		return read_registers(data);
433 
434 	case 'G': /* Write registers */
435 		return write_registers(data, buf + 1);
436 
437 	case 'q': /* Query */
438 		if (!strncmp(buf, "qRcmd,", 6))
439 			return monitor_command(data, buf + 6);
440 		if (!strncmp(buf, "qSupported", 10)) {
441 			/* This is a hack to distinguish msp430-elf-gdb
442 			 * from msp430-gdb. The former expects 32-bit
443 			 * register fields.
444 			 */
445 			if (strstr(buf, "multiprocess+"))
446 				register_bytes = 4;
447 
448 			return gdb_send_supported(data);
449 		}
450 		if (!strncmp(buf, "qfThreadInfo", 12))
451 			return gdb_send_empty_threadlist(data);
452 		break;
453 
454 	case 'm': /* Read memory */
455 		return read_memory(data, buf + 1);
456 
457 	case 'M': /* Write memory */
458 		return write_memory(data, buf + 1);
459 
460 	case 'c': /* Continue */
461 		return run(data, buf + 1);
462 
463 	case 's': /* Single step */
464 		return single_step(data, buf + 1);
465 	case 'k': /* kill */
466 		return -1;
467 	}
468 
469 #ifdef DEBUG_GDB
470 	printc("process_gdb_command: unknown command %s\n", buf);
471 #endif
472 
473 	/* For unknown/unsupported packets, return an empty reply */
474 	return gdb_send(data, "");
475 }
476 
gdb_reader_loop(struct gdb_data * data)477 static void gdb_reader_loop(struct gdb_data *data)
478 {
479 	while (!ctrlc_check()) {
480 		char buf[GDB_BUF_SIZE];
481 		int len = 0;
482 
483 		len = gdb_read_packet(data, buf);
484 		if (len < 0)
485 			return;
486 		if (len && process_gdb_command(data, buf) < 0)
487 			return;
488 	}
489 }
490 
gdb_server(int port)491 static int gdb_server(int port)
492 {
493 	int sock;
494 	int client;
495 	struct sockaddr_in addr;
496 	socklen_t len;
497 	int arg;
498 	struct gdb_data data;
499 	int i;
500 
501 	sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
502 	if (SOCKET_ISERR(sock)) {
503 		pr_error("gdb: can't create socket");
504 		return -1;
505 	}
506 
507 	arg = 1;
508 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
509 		       (void *)&arg, sizeof(arg)) < 0)
510 		pr_error("gdb: warning: can't reuse socket address");
511 
512 	addr.sin_family = AF_INET;
513 	addr.sin_port = htons(port);
514 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
515 	if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
516 		printc_err("gdb: can't bind to port %d: %s\n",
517 			port, last_error());
518 		closesocket(sock);
519 		return -1;
520 	}
521 
522 	if (listen(sock, 1) < 0) {
523 		pr_error("gdb: can't listen on socket");
524 		closesocket(sock);
525 		return -1;
526 	}
527 
528 	printc("Bound to port %d. Now waiting for connection...\n", port);
529 
530 	len = sizeof(addr);
531 	client = sockets_accept(sock, (struct sockaddr *)&addr, &len);
532 	if (SOCKET_ISERR(client)) {
533 		pr_error("gdb: failed to accept connection");
534 		closesocket(sock);
535 		return -1;
536 	}
537 
538 	closesocket(sock);
539 	printc("Client connected from %s:%d\n",
540 	       inet_ntoa(addr.sin_addr), htons(addr.sin_port));
541 
542 	register_bytes = 2;
543 	gdb_init(&data, client);
544 
545 	/* Put the hardware breakpoint setting into a known state. */
546 	printc("Clearing all breakpoints...\n");
547 	for (i = 0; i < device_default->max_breakpoints; i++)
548 		device_setbrk(device_default, i, 0, 0, 0);
549 
550 #ifdef DEBUG_GDB
551 	printc("starting GDB reader loop...\n");
552 #endif
553 	gdb_reader_loop(&data);
554 #ifdef DEBUG_GDB
555 	printc("... reader loop returned\n");
556 #endif
557 	closesocket(client);
558 
559 	return data.error ? -1 : 0;
560 }
561 
cmd_gdb(char ** arg)562 int cmd_gdb(char **arg)
563 {
564 	char *port_text = get_arg(arg);
565 	address_t port = opdb_get_numeric("gdb_default_port");
566 
567 	if (port_text && expr_eval(port_text, &port) < 0) {
568 		printc_err("gdb: can't parse port: %s\n", port_text);
569 		return -1;
570 	}
571 
572 	if (port <= 0 || port > 65535) {
573 		printc_err("gdb: invalid port: %d\n", port);
574 		return -1;
575 	}
576 
577 	do {
578 		if (gdb_server(port) < 0)
579 			return -1;
580 	} while (opdb_get_boolean("gdb_loop"));
581 
582 	return 0;
583 }
584