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