1 /*
2  * Copyright (c) 2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/init.h>
8 #include <zephyr/kernel.h>
9 
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(gdbstub);
12 
13 #include <zephyr/sys/util.h>
14 
15 #include <ctype.h>
16 #include <stdbool.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <zephyr/toolchain.h>
21 #include <sys/types.h>
22 #include <zephyr/sys/util.h>
23 
24 #include <zephyr/debug/gdbstub.h>
25 #include "gdbstub_backend.h"
26 
27 /* +1 is for the NULL character added during receive */
28 #define GDB_PACKET_SIZE     (CONFIG_GDBSTUB_BUF_SZ + 1)
29 
30 /* GDB remote serial protocol does not define errors value properly
31  * and handle all error packets as the same the code error is not
32  * used. There are informal values used by others gdbstub
33  * implementation, like qemu. Lets use the same here.
34  */
35 #define GDB_ERROR_GENERAL   "E01"
36 #define GDB_ERROR_MEMORY    "E14"
37 #define GDB_ERROR_OVERFLOW  "E22"
38 
39 static bool not_first_start;
40 
41 /* Empty memory region array */
42 __weak const struct gdb_mem_region gdb_mem_region_array[0];
43 
44 /* Number of memory regions */
45 __weak const size_t gdb_mem_num_regions;
46 
47 /**
48  * Given a starting address and length of a memory block, find a memory
49  * region descriptor from the memory region array where the memory block
50  * fits inside the memory region.
51  *
52  * @param addr Starting address of the memory block
53  * @param len  Length of the memory block
54  *
55  * @return Pointer to the memory region description if found.
56  *         NULL if not found.
57  */
58 static inline const
find_memory_region(const uintptr_t addr,const size_t len)59 struct gdb_mem_region *find_memory_region(const uintptr_t addr, const size_t len)
60 {
61 	const struct gdb_mem_region *r, *ret = NULL;
62 	unsigned int idx;
63 
64 	for (idx = 0; idx < gdb_mem_num_regions; idx++) {
65 		r = &gdb_mem_region_array[idx];
66 
67 		if ((addr >= r->start) &&
68 		    (addr < r->end) &&
69 		    ((addr + len) >= r->start) &&
70 		    ((addr + len) < r->end)) {
71 			ret = r;
72 			break;
73 		}
74 	}
75 
76 	return ret;
77 }
78 
gdb_mem_can_read(const uintptr_t addr,const size_t len,uint8_t * align)79 bool gdb_mem_can_read(const uintptr_t addr, const size_t len, uint8_t *align)
80 {
81 	bool ret = false;
82 	const struct gdb_mem_region *r;
83 
84 	if (gdb_mem_num_regions == 0) {
85 		/*
86 		 * No region is defined.
87 		 * Assume memory access is not restricted, and there is
88 		 * no alignment requirement.
89 		 */
90 		*align = 1;
91 		ret = true;
92 	} else {
93 		r = find_memory_region(addr, len);
94 		if (r != NULL) {
95 			if ((r->attributes & GDB_MEM_REGION_READ) ==
96 			    GDB_MEM_REGION_READ) {
97 				if (r->alignment > 0) {
98 					*align = r->alignment;
99 				} else {
100 					*align = 1;
101 				}
102 				ret = true;
103 			}
104 		}
105 	}
106 
107 	return ret;
108 }
109 
gdb_mem_can_write(const uintptr_t addr,const size_t len,uint8_t * align)110 bool gdb_mem_can_write(const uintptr_t addr, const size_t len, uint8_t *align)
111 {
112 	bool ret = false;
113 	const struct gdb_mem_region *r;
114 
115 	if (gdb_mem_num_regions == 0) {
116 		/*
117 		 * No region is defined.
118 		 * Assume memory access is not restricted, and there is
119 		 * no alignment requirement.
120 		 */
121 		*align = 1;
122 		ret = true;
123 	} else {
124 		r = find_memory_region(addr, len);
125 		if (r != NULL) {
126 			if ((r->attributes & GDB_MEM_REGION_WRITE) ==
127 			    GDB_MEM_REGION_WRITE) {
128 				if (r->alignment > 0) {
129 					*align = r->alignment;
130 				} else {
131 					*align = 1;
132 				}
133 
134 				ret = true;
135 			}
136 		}
137 	}
138 
139 	return ret;
140 }
141 
gdb_bin2hex(const uint8_t * buf,size_t buflen,char * hex,size_t hexlen)142 size_t gdb_bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
143 {
144 	if ((hexlen + 1) < buflen * 2) {
145 		return 0;
146 	}
147 
148 	for (size_t i = 0; i < buflen; i++) {
149 		if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) {
150 			return 0;
151 		}
152 		if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) {
153 			return 0;
154 		}
155 	}
156 
157 	return 2 * buflen;
158 }
159 
160 __weak
arch_gdb_add_breakpoint(struct gdb_ctx * ctx,uint8_t type,uintptr_t addr,uint32_t kind)161 int arch_gdb_add_breakpoint(struct gdb_ctx *ctx, uint8_t type,
162 			    uintptr_t addr, uint32_t kind)
163 {
164 	return -2;
165 }
166 
167 __weak
arch_gdb_remove_breakpoint(struct gdb_ctx * ctx,uint8_t type,uintptr_t addr,uint32_t kind)168 int arch_gdb_remove_breakpoint(struct gdb_ctx *ctx, uint8_t type,
169 			       uintptr_t addr, uint32_t kind)
170 {
171 	return -2;
172 }
173 
174 
175 /**
176  * Add preamble and termination to the given data.
177  *
178  * It returns 0 if the packet was acknowledge, -1 otherwise.
179  */
gdb_send_packet(const uint8_t * data,size_t len)180 static int gdb_send_packet(const uint8_t *data, size_t len)
181 {
182 	uint8_t buf[2];
183 	uint8_t checksum = 0;
184 
185 	/* Send packet start */
186 	z_gdb_putchar('$');
187 
188 	/* Send packet data and calculate checksum */
189 	while (len-- > 0) {
190 		checksum += *data;
191 		z_gdb_putchar(*data++);
192 	}
193 
194 	/* Send the checksum */
195 	z_gdb_putchar('#');
196 
197 	if (gdb_bin2hex(&checksum, 1, buf, sizeof(buf)) == 0) {
198 		return -1;
199 	}
200 
201 	z_gdb_putchar(buf[0]);
202 	z_gdb_putchar(buf[1]);
203 
204 	if (z_gdb_getchar() == '+') {
205 		return 0;
206 	}
207 
208 	/* Just got an invalid response */
209 	return -1;
210 }
211 
212 /**
213  * Receives one whole GDB packet.
214  *
215  * @retval  0 Success
216  * @retval -1 Checksum error
217  * @retval -2 Incoming packet too large
218  */
gdb_get_packet(uint8_t * buf,size_t buf_len,size_t * len)219 static int gdb_get_packet(uint8_t *buf, size_t buf_len, size_t *len)
220 {
221 	uint8_t ch = '0';
222 	uint8_t expected_checksum, checksum = 0;
223 	uint8_t checksum_buf[2];
224 
225 	/* Wait for packet start */
226 	checksum = 0;
227 
228 	/* wait for the start character, ignore the rest */
229 	while (ch != '$') {
230 		ch = z_gdb_getchar();
231 	}
232 
233 	*len = 0;
234 	/* Read until receive '#' */
235 	while (true) {
236 		ch = z_gdb_getchar();
237 
238 		if (ch == '#') {
239 			break;
240 		}
241 
242 		/* Only put into buffer if not full */
243 		if (*len < (buf_len - 1)) {
244 			buf[*len] = ch;
245 		}
246 
247 		checksum += ch;
248 		(*len)++;
249 	}
250 
251 	buf[*len] = '\0';
252 
253 	/* Get checksum now */
254 	checksum_buf[0] = z_gdb_getchar();
255 	checksum_buf[1] = z_gdb_getchar();
256 
257 	if (hex2bin(checksum_buf, 2, &expected_checksum, 1) == 0) {
258 		return -1;
259 	}
260 
261 	/* Verify checksum */
262 	if (checksum != expected_checksum) {
263 		LOG_DBG("Bad checksum. Got 0x%x but was expecting: 0x%x",
264 			checksum, expected_checksum);
265 		/* NACK packet */
266 		z_gdb_putchar('-');
267 		return -1;
268 	}
269 
270 	/* ACK packet */
271 	z_gdb_putchar('+');
272 
273 	if (*len >= (buf_len - 1)) {
274 		return -2;
275 	} else {
276 		return 0;
277 	}
278 }
279 
280 /* Read memory byte-by-byte */
gdb_mem_read_unaligned(uint8_t * buf,size_t buf_len,uintptr_t addr,size_t len)281 static inline int gdb_mem_read_unaligned(uint8_t *buf, size_t buf_len,
282 					 uintptr_t addr, size_t len)
283 {
284 	uint8_t data;
285 	size_t pos, count = 0;
286 
287 	/* Read from system memory */
288 	for (pos = 0; pos < len; pos++) {
289 		data = *(uint8_t *)(addr + pos);
290 		count += gdb_bin2hex(&data, 1, buf + count, buf_len - count);
291 	}
292 
293 	return count;
294 }
295 
296 /* Read memory with alignment constraint */
gdb_mem_read_aligned(uint8_t * buf,size_t buf_len,uintptr_t addr,size_t len,uint8_t align)297 static inline int gdb_mem_read_aligned(uint8_t *buf, size_t buf_len,
298 				       uintptr_t addr, size_t len,
299 				       uint8_t align)
300 {
301 	/*
302 	 * Memory bus cannot do byte-by-byte access and
303 	 * each access must be aligned.
304 	 */
305 	size_t read_sz, pos;
306 	size_t remaining = len;
307 	uint8_t *mem_ptr;
308 	size_t count = 0;
309 	int ret;
310 
311 	union {
312 		uint32_t u32;
313 		uint8_t b8[4];
314 	} data;
315 
316 	/* Max alignment */
317 	if (align > 4) {
318 		ret = -1;
319 		goto out;
320 	}
321 
322 	/* Round down according to alignment. */
323 	mem_ptr = UINT_TO_POINTER(ROUND_DOWN(addr, align));
324 
325 	/*
326 	 * Figure out how many bytes to skip (pos) and how many
327 	 * bytes to read at the beginning of aligned memory access.
328 	 */
329 	pos = addr & (align - 1);
330 	read_sz = MIN(len, align - pos);
331 
332 	/* Loop till there is nothing more to read. */
333 	while (remaining > 0) {
334 		data.u32 = *(uint32_t *)mem_ptr;
335 
336 		/*
337 		 * Read read_sz bytes from memory and
338 		 * convert the binary data into hexadecimal.
339 		 */
340 		count += gdb_bin2hex(&data.b8[pos], read_sz,
341 				     buf + count, buf_len - count);
342 
343 		remaining -= read_sz;
344 		if (remaining > align) {
345 			read_sz = align;
346 		} else {
347 			read_sz = remaining;
348 		}
349 
350 		/* Read the next aligned datum. */
351 		mem_ptr += align;
352 
353 		/*
354 		 * Any memory accesses after the first one are
355 		 * aligned by design. So there is no need to skip
356 		 * any bytes.
357 		 */
358 		pos = 0;
359 	};
360 
361 	ret = count;
362 
363 out:
364 	return ret;
365 }
366 
367 /**
368  * Read data from a given memory address and length.
369  *
370  * @return Number of bytes read from memory, or -1 if error
371  */
gdb_mem_read(uint8_t * buf,size_t buf_len,uintptr_t addr,size_t len)372 static int gdb_mem_read(uint8_t *buf, size_t buf_len,
373 			uintptr_t addr, size_t len)
374 {
375 	uint8_t align;
376 	int ret;
377 
378 	/*
379 	 * Make sure there is enough space in the output
380 	 * buffer for hexadecimal representation.
381 	 */
382 	if ((len * 2) > buf_len) {
383 		ret = -1;
384 		goto out;
385 	}
386 
387 	if (!gdb_mem_can_read(addr, len, &align)) {
388 		ret = -1;
389 		goto out;
390 	}
391 
392 	if (align > 1) {
393 		ret = gdb_mem_read_aligned(buf, buf_len,
394 					   addr, len,
395 					   align);
396 	} else {
397 		ret = gdb_mem_read_unaligned(buf, buf_len,
398 					     addr, len);
399 	}
400 
401 out:
402 	return ret;
403 }
404 
405 /* Write memory byte-by-byte */
gdb_mem_write_unaligned(const uint8_t * buf,uintptr_t addr,size_t len)406 static int gdb_mem_write_unaligned(const uint8_t *buf, uintptr_t addr,
407 				   size_t len)
408 {
409 	uint8_t data;
410 	int ret;
411 	size_t count = 0;
412 
413 	while (len > 0) {
414 		size_t cnt = hex2bin(buf, 2, &data, sizeof(data));
415 
416 		if (cnt == 0) {
417 			ret = -1;
418 			goto out;
419 		}
420 
421 		*(uint8_t *)addr = data;
422 
423 		count += cnt;
424 		addr++;
425 		buf += 2;
426 		len--;
427 	}
428 
429 	ret = count;
430 
431 out:
432 	return ret;
433 }
434 
435 /* Write memory with alignment constraint */
gdb_mem_write_aligned(const uint8_t * buf,uintptr_t addr,size_t len,uint8_t align)436 static int gdb_mem_write_aligned(const uint8_t *buf, uintptr_t addr,
437 				 size_t len, uint8_t align)
438 {
439 	size_t pos, write_sz;
440 	uint8_t *mem_ptr;
441 	size_t count = 0;
442 	int ret;
443 
444 	/*
445 	 * Incoming buf is of hexadecimal characters,
446 	 * so binary data size is half of that.
447 	 */
448 	size_t remaining = len;
449 
450 	union {
451 		uint32_t u32;
452 		uint8_t b8[4];
453 	} data;
454 
455 	/* Max alignment */
456 	if (align > 4) {
457 		ret = -1;
458 		goto out;
459 	}
460 
461 	/*
462 	 * Round down according to alignment.
463 	 * Read the data (of aligned size) first
464 	 * as we need to do read-modify-write.
465 	 */
466 	mem_ptr = UINT_TO_POINTER(ROUND_DOWN(addr, align));
467 	data.u32 = *(uint32_t *)mem_ptr;
468 
469 	/*
470 	 * Figure out how many bytes to skip (pos) and how many
471 	 * bytes to write at the beginning of aligned memory access.
472 	 */
473 	pos = addr & (align - 1);
474 	write_sz = MIN(len, align - pos);
475 
476 	/* Loop till there is nothing more to write. */
477 	while (remaining > 0) {
478 		/*
479 		 * Write write_sz bytes from memory and
480 		 * convert the binary data into hexadecimal.
481 		 */
482 		size_t cnt = hex2bin(buf, write_sz * 2,
483 				     &data.b8[pos], write_sz);
484 
485 		if (cnt == 0) {
486 			ret = -1;
487 			goto out;
488 		}
489 
490 		count += cnt;
491 		buf += write_sz * 2;
492 
493 		remaining -= write_sz;
494 		if (remaining > align) {
495 			write_sz = align;
496 		} else {
497 			write_sz = remaining;
498 		}
499 
500 		/* Write data to memory */
501 		*(uint32_t *)mem_ptr = data.u32;
502 
503 		/* Point to the next aligned datum. */
504 		mem_ptr += align;
505 
506 		if (write_sz != align) {
507 			/*
508 			 * Since we are not writing a full aligned datum,
509 			 * we need to do read-modify-write. Hence reading
510 			 * it here before the next hex2bin() call.
511 			 */
512 			data.u32 = *(uint32_t *)mem_ptr;
513 		}
514 
515 		/*
516 		 * Any memory accesses after the first one are
517 		 * aligned by design. So there is no need to skip
518 		 * any bytes.
519 		 */
520 		pos = 0;
521 	};
522 
523 	ret = count;
524 
525 out:
526 	return ret;
527 }
528 
529 /**
530  * Write data to a given memory address and length.
531  *
532  * @return Number of bytes written to memory, or -1 if error
533  */
gdb_mem_write(const uint8_t * buf,uintptr_t addr,size_t len)534 static int gdb_mem_write(const uint8_t *buf, uintptr_t addr,
535 			 size_t len)
536 {
537 	uint8_t align;
538 	int ret;
539 
540 	if (!gdb_mem_can_write(addr, len, &align)) {
541 		ret = -1;
542 		goto out;
543 	}
544 
545 	if (align > 1) {
546 		ret = gdb_mem_write_aligned(buf, addr, len, align);
547 	} else {
548 		ret = gdb_mem_write_unaligned(buf, addr, len);
549 	}
550 
551 out:
552 	return ret;
553 }
554 
555 /**
556  * Send a exception packet "T <value>"
557  */
gdb_send_exception(uint8_t * buf,size_t len,uint8_t exception)558 static int gdb_send_exception(uint8_t *buf, size_t len, uint8_t exception)
559 {
560 	size_t size;
561 
562 #ifdef CONFIG_GDBSTUB_TRACE
563 	printk("gdbstub:%s exception=0x%x\n", __func__, exception);
564 #endif
565 
566 	*buf = 'T';
567 	size = gdb_bin2hex(&exception, 1, buf + 1, len - 1);
568 	if (size == 0) {
569 		return -1;
570 	}
571 
572 	/* Related to 'T' */
573 	size++;
574 
575 	return gdb_send_packet(buf, size);
576 }
577 
gdb_qsupported(uint8_t * buf,size_t len,enum gdb_loop_state * next_state)578 static bool gdb_qsupported(uint8_t *buf, size_t len, enum gdb_loop_state *next_state)
579 {
580 	size_t n = 0;
581 	const char *c_buf = (const char *) buf;
582 
583 	if (strstr(buf, "qSupported") != c_buf) {
584 		return false;
585 	}
586 
587 	gdb_send_packet(buf, n);
588 	return true;
589 }
590 
gdb_q_packet(uint8_t * buf,size_t len,enum gdb_loop_state * next_state)591 static void gdb_q_packet(uint8_t *buf, size_t len, enum gdb_loop_state *next_state)
592 {
593 	if (gdb_qsupported(buf, len, next_state)) {
594 		return;
595 	}
596 
597 	gdb_send_packet(NULL, 0);
598 }
599 
gdb_v_packet(uint8_t * buf,size_t len,enum gdb_loop_state * next_state)600 static void gdb_v_packet(uint8_t *buf, size_t len, enum gdb_loop_state *next_state)
601 {
602 	gdb_send_packet(NULL, 0);
603 }
604 
605 /**
606  * Synchronously communicate with gdb on the host
607  */
z_gdb_main_loop(struct gdb_ctx * ctx)608 int z_gdb_main_loop(struct gdb_ctx *ctx)
609 {
610 	/* 'static' modifier is intentional so the buffer
611 	 * is not declared inside running stack, which may
612 	 * not have enough space.
613 	 */
614 	static uint8_t buf[GDB_PACKET_SIZE];
615 	enum gdb_loop_state state;
616 
617 	state = GDB_LOOP_RECEIVING;
618 
619 	/* Only send exception if this is not the first
620 	 * GDB break.
621 	 */
622 	if (not_first_start) {
623 		gdb_send_exception(buf, sizeof(buf), ctx->exception);
624 	} else {
625 		not_first_start = true;
626 	}
627 
628 #define CHECK_ERROR(condition)			\
629 	{					\
630 		if ((condition)) {		\
631 			state = GDB_LOOP_ERROR;	\
632 			break;			\
633 		}				\
634 	}
635 
636 #define CHECK_SYMBOL(c)					\
637 	{							\
638 		CHECK_ERROR(ptr == NULL || *ptr != (c));	\
639 		ptr++;						\
640 	}
641 
642 #define CHECK_UINT(arg)							\
643 	{								\
644 		arg = strtoul((const char *)ptr, (char **)&ptr, 16);	\
645 		CHECK_ERROR(ptr == NULL);				\
646 	}
647 
648 	while (state == GDB_LOOP_RECEIVING) {
649 		uint8_t *ptr;
650 		size_t data_len, pkt_len;
651 		uintptr_t addr;
652 		uint32_t type;
653 		int ret;
654 
655 		ret = gdb_get_packet(buf, sizeof(buf), &pkt_len);
656 		if ((ret == -1) || (ret == -2)) {
657 			/*
658 			 * Send error and wait for next packet.
659 			 *
660 			 * -1: Checksum error.
661 			 * -2: Packet too big.
662 			 */
663 			gdb_send_packet(GDB_ERROR_GENERAL, 3);
664 			continue;
665 		}
666 
667 		if (pkt_len == 0) {
668 			continue;
669 		}
670 
671 		ptr = buf;
672 
673 #ifdef CONFIG_GDBSTUB_TRACE
674 		printk("gdbstub:%s got '%c'(0x%x) command\n", __func__, *ptr, *ptr);
675 #endif
676 
677 		switch (*ptr++) {
678 
679 		/**
680 		 * Read from the memory
681 		 * Format: m addr,length
682 		 */
683 		case 'm':
684 			CHECK_UINT(addr);
685 			CHECK_SYMBOL(',');
686 			CHECK_UINT(data_len);
687 
688 			/* Read Memory */
689 
690 			/*
691 			 * GDB ask the guest to read parameters when
692 			 * the user request backtrace. If the
693 			 * parameter is a NULL pointer this will cause
694 			 * a fault. Just send a packet informing that
695 			 * this address is invalid
696 			 */
697 			if (addr == 0L) {
698 				gdb_send_packet(GDB_ERROR_MEMORY, 3);
699 				break;
700 			}
701 			ret = gdb_mem_read(buf, sizeof(buf), addr, data_len);
702 			CHECK_ERROR(ret == -1);
703 			gdb_send_packet(buf, ret);
704 			break;
705 
706 		/**
707 		 * Write to memory
708 		 * Format: M addr,length:val
709 		 */
710 		case 'M':
711 			CHECK_UINT(addr);
712 			CHECK_SYMBOL(',');
713 			CHECK_UINT(data_len);
714 			CHECK_SYMBOL(':');
715 
716 			if (addr == 0L) {
717 				gdb_send_packet(GDB_ERROR_MEMORY, 3);
718 				break;
719 			}
720 
721 			/* Write Memory */
722 			pkt_len = gdb_mem_write(ptr, addr, data_len);
723 			CHECK_ERROR(pkt_len == -1);
724 			gdb_send_packet("OK", 2);
725 			break;
726 
727 		/*
728 		 * Continue ignoring the optional address
729 		 * Format: c addr
730 		 */
731 		case 'c':
732 			arch_gdb_continue();
733 			state = GDB_LOOP_CONTINUE;
734 			break;
735 
736 		/*
737 		 * Step one instruction ignoring the optional address
738 		 * s addr..addr
739 		 */
740 		case 's':
741 			arch_gdb_step();
742 			state = GDB_LOOP_CONTINUE;
743 			break;
744 
745 		/*
746 		 * Read all registers
747 		 * Format: g
748 		 */
749 		case 'g':
750 			pkt_len = arch_gdb_reg_readall(ctx, buf, sizeof(buf));
751 			CHECK_ERROR(pkt_len == 0);
752 			gdb_send_packet(buf, pkt_len);
753 			break;
754 
755 		/**
756 		 * Write the value of the CPU registers
757 		 * Format: G XX...
758 		 */
759 		case 'G':
760 			pkt_len = arch_gdb_reg_writeall(ctx, ptr, pkt_len - 1);
761 			CHECK_ERROR(pkt_len == 0);
762 			gdb_send_packet("OK", 2);
763 			break;
764 
765 		/**
766 		 * Read the value of a register
767 		 * Format: p n
768 		 */
769 		case 'p':
770 			CHECK_UINT(addr);
771 
772 			/* Read Register */
773 			pkt_len = arch_gdb_reg_readone(ctx, buf, sizeof(buf), addr);
774 			CHECK_ERROR(pkt_len == 0);
775 			gdb_send_packet(buf, pkt_len);
776 			break;
777 
778 		/**
779 		 * Write data into a specific register
780 		 * Format: P register=value
781 		 */
782 		case 'P':
783 			CHECK_UINT(addr);
784 			CHECK_SYMBOL('=');
785 
786 			pkt_len = arch_gdb_reg_writeone(ctx, ptr, strlen(ptr), addr);
787 			CHECK_ERROR(pkt_len == 0);
788 			gdb_send_packet("OK", 2);
789 			break;
790 
791 		/*
792 		 * Breakpoints and Watchpoints
793 		 */
794 		case 'z':
795 			__fallthrough;
796 		case 'Z':
797 			CHECK_UINT(type);
798 			CHECK_SYMBOL(',');
799 			CHECK_UINT(addr);
800 			CHECK_SYMBOL(',');
801 			CHECK_UINT(data_len);
802 
803 			if (buf[0] == 'Z') {
804 				ret = arch_gdb_add_breakpoint(ctx, type,
805 							      addr, data_len);
806 			} else if (buf[0] == 'z') {
807 				ret = arch_gdb_remove_breakpoint(ctx, type,
808 								 addr, data_len);
809 			}
810 
811 			if (ret == -2) {
812 				/* breakpoint/watchpoint not supported */
813 				gdb_send_packet(NULL, 0);
814 			} else if (ret == -1) {
815 				state = GDB_LOOP_ERROR;
816 			} else {
817 				gdb_send_packet("OK", 2);
818 			}
819 
820 			break;
821 
822 
823 		/* What cause the pause  */
824 		case '?':
825 			gdb_send_exception(buf, sizeof(buf),
826 					   ctx->exception);
827 			break;
828 
829 		/* Query packets*/
830 		case 'q':
831 			__fallthrough;
832 		case 'Q':
833 			gdb_q_packet(buf, sizeof(buf), &state);
834 			break;
835 
836 		/* v packets */
837 		case 'v':
838 			gdb_v_packet(buf, sizeof(buf), &state);
839 			break;
840 
841 		/*
842 		 * Not supported action
843 		 */
844 		default:
845 			gdb_send_packet(NULL, 0);
846 			break;
847 		}
848 
849 		/*
850 		 * If this is an recoverable error, send an error message to
851 		 * GDB and continue the debugging session.
852 		 */
853 		if (state == GDB_LOOP_ERROR) {
854 			gdb_send_packet(GDB_ERROR_GENERAL, 3);
855 			state = GDB_LOOP_RECEIVING;
856 		}
857 	}
858 
859 #undef CHECK_ERROR
860 #undef CHECK_UINT
861 #undef CHECK_SYMBOL
862 
863 	return 0;
864 }
865 
gdb_init(void)866 int gdb_init(void)
867 {
868 #ifdef CONFIG_GDBSTUB_TRACE
869 	printk("gdbstub:%s enter\n", __func__);
870 #endif
871 	if (z_gdb_backend_init() == -1) {
872 		LOG_ERR("Could not initialize gdbstub backend.");
873 		return -1;
874 	}
875 
876 	arch_gdb_init();
877 
878 #ifdef CONFIG_GDBSTUB_TRACE
879 	printk("gdbstub:%s exit\n", __func__);
880 #endif
881 	return 0;
882 }
883 
884 #ifdef CONFIG_XTENSA
885 /*
886  * Interrupt stacks are being setup during init and are not
887  * available before POST_KERNEL. Xtensa needs to trigger
888  * interrupts to get into GDB stub. So this can only be
889  * initialized in POST_KERNEL, or else the interrupt would not be
890  * using the correct interrupt stack and will result in
891  * double exception.
892  */
893 SYS_INIT(gdb_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
894 #else
895 SYS_INIT(gdb_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
896 #endif
897