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 #if defined(__GNUC__)
59 #pragma GCC diagnostic push
60 /* Required due to gdb_mem_region_array having a default size of zero. */
61 #pragma GCC diagnostic ignored "-Warray-bounds"
62 #endif
63
64 static inline const
find_memory_region(const uintptr_t addr,const size_t len)65 struct gdb_mem_region *find_memory_region(const uintptr_t addr, const size_t len)
66 {
67 const struct gdb_mem_region *r, *ret = NULL;
68 unsigned int idx;
69
70 for (idx = 0; idx < gdb_mem_num_regions; idx++) {
71 r = &gdb_mem_region_array[idx];
72
73 if ((addr >= r->start) &&
74 (addr < r->end) &&
75 ((addr + len) >= r->start) &&
76 ((addr + len) < r->end)) {
77 ret = r;
78 break;
79 }
80 }
81
82 return ret;
83 }
84
85 #if defined(__GNUC__)
86 #pragma GCC diagnostic pop
87 #endif
88
gdb_mem_can_read(const uintptr_t addr,const size_t len,uint8_t * align)89 bool gdb_mem_can_read(const uintptr_t addr, const size_t len, uint8_t *align)
90 {
91 bool ret = false;
92 const struct gdb_mem_region *r;
93
94 if (gdb_mem_num_regions == 0) {
95 /*
96 * No region is defined.
97 * Assume memory access is not restricted, and there is
98 * no alignment requirement.
99 */
100 *align = 1;
101 ret = true;
102 } else {
103 r = find_memory_region(addr, len);
104 if (r != NULL) {
105 if ((r->attributes & GDB_MEM_REGION_READ) ==
106 GDB_MEM_REGION_READ) {
107 if (r->alignment > 0) {
108 *align = r->alignment;
109 } else {
110 *align = 1;
111 }
112 ret = true;
113 }
114 }
115 }
116
117 return ret;
118 }
119
gdb_mem_can_write(const uintptr_t addr,const size_t len,uint8_t * align)120 bool gdb_mem_can_write(const uintptr_t addr, const size_t len, uint8_t *align)
121 {
122 bool ret = false;
123 const struct gdb_mem_region *r;
124
125 if (gdb_mem_num_regions == 0) {
126 /*
127 * No region is defined.
128 * Assume memory access is not restricted, and there is
129 * no alignment requirement.
130 */
131 *align = 1;
132 ret = true;
133 } else {
134 r = find_memory_region(addr, len);
135 if (r != NULL) {
136 if ((r->attributes & GDB_MEM_REGION_WRITE) ==
137 GDB_MEM_REGION_WRITE) {
138 if (r->alignment > 0) {
139 *align = r->alignment;
140 } else {
141 *align = 1;
142 }
143
144 ret = true;
145 }
146 }
147 }
148
149 return ret;
150 }
151
gdb_bin2hex(const uint8_t * buf,size_t buflen,char * hex,size_t hexlen)152 size_t gdb_bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
153 {
154 if ((hexlen + 1) < buflen * 2) {
155 return 0;
156 }
157
158 for (size_t i = 0; i < buflen; i++) {
159 if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) {
160 return 0;
161 }
162 if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) {
163 return 0;
164 }
165 }
166
167 return 2 * buflen;
168 }
169
170 __weak
arch_gdb_add_breakpoint(struct gdb_ctx * ctx,uint8_t type,uintptr_t addr,uint32_t kind)171 int arch_gdb_add_breakpoint(struct gdb_ctx *ctx, uint8_t type,
172 uintptr_t addr, uint32_t kind)
173 {
174 return -2;
175 }
176
177 __weak
arch_gdb_remove_breakpoint(struct gdb_ctx * ctx,uint8_t type,uintptr_t addr,uint32_t kind)178 int arch_gdb_remove_breakpoint(struct gdb_ctx *ctx, uint8_t type,
179 uintptr_t addr, uint32_t kind)
180 {
181 return -2;
182 }
183
184
185 /**
186 * Add preamble and termination to the given data.
187 *
188 * It returns 0 if the packet was acknowledge, -1 otherwise.
189 */
gdb_send_packet(const uint8_t * data,size_t len)190 static int gdb_send_packet(const uint8_t *data, size_t len)
191 {
192 uint8_t buf[2];
193 uint8_t checksum = 0;
194
195 /* Send packet start */
196 z_gdb_putchar('$');
197
198 /* Send packet data and calculate checksum */
199 while (len-- > 0) {
200 checksum += *data;
201 z_gdb_putchar(*data++);
202 }
203
204 /* Send the checksum */
205 z_gdb_putchar('#');
206
207 if (gdb_bin2hex(&checksum, 1, buf, sizeof(buf)) == 0) {
208 return -1;
209 }
210
211 z_gdb_putchar(buf[0]);
212 z_gdb_putchar(buf[1]);
213
214 if (z_gdb_getchar() == '+') {
215 return 0;
216 }
217
218 /* Just got an invalid response */
219 return -1;
220 }
221
222 /**
223 * Receives one whole GDB packet.
224 *
225 * @retval 0 Success
226 * @retval -1 Checksum error
227 * @retval -2 Incoming packet too large
228 */
gdb_get_packet(uint8_t * buf,size_t buf_len,size_t * len)229 static int gdb_get_packet(uint8_t *buf, size_t buf_len, size_t *len)
230 {
231 uint8_t ch = '0';
232 uint8_t expected_checksum, checksum = 0;
233 uint8_t checksum_buf[2];
234
235 /* Wait for packet start */
236 checksum = 0;
237
238 /* wait for the start character, ignore the rest */
239 while (ch != '$') {
240 ch = z_gdb_getchar();
241 }
242
243 *len = 0;
244 /* Read until receive '#' */
245 while (true) {
246 ch = z_gdb_getchar();
247
248 if (ch == '#') {
249 break;
250 }
251
252 /* Only put into buffer if not full */
253 if (*len < (buf_len - 1)) {
254 buf[*len] = ch;
255 }
256
257 checksum += ch;
258 (*len)++;
259 }
260
261 buf[*len] = '\0';
262
263 /* Get checksum now */
264 checksum_buf[0] = z_gdb_getchar();
265 checksum_buf[1] = z_gdb_getchar();
266
267 if (hex2bin(checksum_buf, 2, &expected_checksum, 1) == 0) {
268 return -1;
269 }
270
271 /* Verify checksum */
272 if (checksum != expected_checksum) {
273 LOG_DBG("Bad checksum. Got 0x%x but was expecting: 0x%x",
274 checksum, expected_checksum);
275 /* NACK packet */
276 z_gdb_putchar('-');
277 return -1;
278 }
279
280 /* ACK packet */
281 z_gdb_putchar('+');
282
283 if (*len >= (buf_len - 1)) {
284 return -2;
285 } else {
286 return 0;
287 }
288 }
289
290 /* Read memory byte-by-byte */
gdb_mem_read_unaligned(uint8_t * buf,size_t buf_len,uintptr_t addr,size_t len)291 static inline int gdb_mem_read_unaligned(uint8_t *buf, size_t buf_len,
292 uintptr_t addr, size_t len)
293 {
294 uint8_t data;
295 size_t pos, count = 0;
296
297 /* Read from system memory */
298 for (pos = 0; pos < len; pos++) {
299 data = *(uint8_t *)(addr + pos);
300 count += gdb_bin2hex(&data, 1, buf + count, buf_len - count);
301 }
302
303 return count;
304 }
305
306 /* 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)307 static inline int gdb_mem_read_aligned(uint8_t *buf, size_t buf_len,
308 uintptr_t addr, size_t len,
309 uint8_t align)
310 {
311 /*
312 * Memory bus cannot do byte-by-byte access and
313 * each access must be aligned.
314 */
315 size_t read_sz, pos;
316 size_t remaining = len;
317 uint8_t *mem_ptr;
318 size_t count = 0;
319 int ret;
320
321 union {
322 uint32_t u32;
323 uint8_t b8[4];
324 } data;
325
326 /* Max alignment */
327 if (align > 4) {
328 ret = -1;
329 goto out;
330 }
331
332 /* Round down according to alignment. */
333 mem_ptr = UINT_TO_POINTER(ROUND_DOWN(addr, align));
334
335 /*
336 * Figure out how many bytes to skip (pos) and how many
337 * bytes to read at the beginning of aligned memory access.
338 */
339 pos = addr & (align - 1);
340 read_sz = MIN(len, align - pos);
341
342 /* Loop till there is nothing more to read. */
343 while (remaining > 0) {
344 data.u32 = *(uint32_t *)mem_ptr;
345
346 /*
347 * Read read_sz bytes from memory and
348 * convert the binary data into hexadecimal.
349 */
350 count += gdb_bin2hex(&data.b8[pos], read_sz,
351 buf + count, buf_len - count);
352
353 remaining -= read_sz;
354 if (remaining > align) {
355 read_sz = align;
356 } else {
357 read_sz = remaining;
358 }
359
360 /* Read the next aligned datum. */
361 mem_ptr += align;
362
363 /*
364 * Any memory accesses after the first one are
365 * aligned by design. So there is no need to skip
366 * any bytes.
367 */
368 pos = 0;
369 };
370
371 ret = count;
372
373 out:
374 return ret;
375 }
376
377 /**
378 * Read data from a given memory address and length.
379 *
380 * @return Number of bytes read from memory, or -1 if error
381 */
gdb_mem_read(uint8_t * buf,size_t buf_len,uintptr_t addr,size_t len)382 static int gdb_mem_read(uint8_t *buf, size_t buf_len,
383 uintptr_t addr, size_t len)
384 {
385 uint8_t align;
386 int ret;
387
388 /*
389 * Make sure there is enough space in the output
390 * buffer for hexadecimal representation.
391 */
392 if ((len * 2) > buf_len) {
393 ret = -1;
394 goto out;
395 }
396
397 if (!gdb_mem_can_read(addr, len, &align)) {
398 ret = -1;
399 goto out;
400 }
401
402 if (align > 1) {
403 ret = gdb_mem_read_aligned(buf, buf_len,
404 addr, len,
405 align);
406 } else {
407 ret = gdb_mem_read_unaligned(buf, buf_len,
408 addr, len);
409 }
410
411 out:
412 return ret;
413 }
414
415 /* Write memory byte-by-byte */
gdb_mem_write_unaligned(const uint8_t * buf,uintptr_t addr,size_t len)416 static int gdb_mem_write_unaligned(const uint8_t *buf, uintptr_t addr,
417 size_t len)
418 {
419 uint8_t data;
420 int ret;
421 size_t count = 0;
422
423 while (len > 0) {
424 size_t cnt = hex2bin(buf, 2, &data, sizeof(data));
425
426 if (cnt == 0) {
427 ret = -1;
428 goto out;
429 }
430
431 *(uint8_t *)addr = data;
432
433 count += cnt;
434 addr++;
435 buf += 2;
436 len--;
437 }
438
439 ret = count;
440
441 out:
442 return ret;
443 }
444
445 /* Write memory with alignment constraint */
gdb_mem_write_aligned(const uint8_t * buf,uintptr_t addr,size_t len,uint8_t align)446 static int gdb_mem_write_aligned(const uint8_t *buf, uintptr_t addr,
447 size_t len, uint8_t align)
448 {
449 size_t pos, write_sz;
450 uint8_t *mem_ptr;
451 size_t count = 0;
452 int ret;
453
454 /*
455 * Incoming buf is of hexadecimal characters,
456 * so binary data size is half of that.
457 */
458 size_t remaining = len;
459
460 union {
461 uint32_t u32;
462 uint8_t b8[4];
463 } data;
464
465 /* Max alignment */
466 if (align > 4) {
467 ret = -1;
468 goto out;
469 }
470
471 /*
472 * Round down according to alignment.
473 * Read the data (of aligned size) first
474 * as we need to do read-modify-write.
475 */
476 mem_ptr = UINT_TO_POINTER(ROUND_DOWN(addr, align));
477 data.u32 = *(uint32_t *)mem_ptr;
478
479 /*
480 * Figure out how many bytes to skip (pos) and how many
481 * bytes to write at the beginning of aligned memory access.
482 */
483 pos = addr & (align - 1);
484 write_sz = MIN(len, align - pos);
485
486 /* Loop till there is nothing more to write. */
487 while (remaining > 0) {
488 /*
489 * Write write_sz bytes from memory and
490 * convert the binary data into hexadecimal.
491 */
492 size_t cnt = hex2bin(buf, write_sz * 2,
493 &data.b8[pos], write_sz);
494
495 if (cnt == 0) {
496 ret = -1;
497 goto out;
498 }
499
500 count += cnt;
501 buf += write_sz * 2;
502
503 remaining -= write_sz;
504 if (remaining > align) {
505 write_sz = align;
506 } else {
507 write_sz = remaining;
508 }
509
510 /* Write data to memory */
511 *(uint32_t *)mem_ptr = data.u32;
512
513 /* Point to the next aligned datum. */
514 mem_ptr += align;
515
516 if (write_sz != align) {
517 /*
518 * Since we are not writing a full aligned datum,
519 * we need to do read-modify-write. Hence reading
520 * it here before the next hex2bin() call.
521 */
522 data.u32 = *(uint32_t *)mem_ptr;
523 }
524
525 /*
526 * Any memory accesses after the first one are
527 * aligned by design. So there is no need to skip
528 * any bytes.
529 */
530 pos = 0;
531 };
532
533 ret = count;
534
535 out:
536 return ret;
537 }
538
539 /**
540 * Write data to a given memory address and length.
541 *
542 * @return Number of bytes written to memory, or -1 if error
543 */
gdb_mem_write(const uint8_t * buf,uintptr_t addr,size_t len)544 static int gdb_mem_write(const uint8_t *buf, uintptr_t addr,
545 size_t len)
546 {
547 uint8_t align;
548 int ret;
549
550 if (!gdb_mem_can_write(addr, len, &align)) {
551 ret = -1;
552 goto out;
553 }
554
555 if (align > 1) {
556 ret = gdb_mem_write_aligned(buf, addr, len, align);
557 } else {
558 ret = gdb_mem_write_unaligned(buf, addr, len);
559 }
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_XTENSA
895 /*
896 * Interrupt stacks are being setup during init and are not
897 * available before POST_KERNEL. Xtensa needs to trigger
898 * interrupts to get into GDB stub. So this can only be
899 * initialized in POST_KERNEL, or else the interrupt would not be
900 * using the correct interrupt stack and will result in
901 * double exception.
902 */
903 SYS_INIT(gdb_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
904 #else
905 SYS_INIT(gdb_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
906 #endif
907