1 /* $NetBSD: db_interface.c,v 1.39 2026/11/10 03:43:26 thorpej Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1992 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie Mellon 26 * the rights to redistribute these changes. 27 */ 28 29 /* 30 * Interface to the "ddb" kernel debugger. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.39 2026/11/10 03:43:26 thorpej Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/reboot.h> 39 #include <sys/systm.h> 40 41 #include <uvm/uvm_extern.h> 42 43 #include <dev/cons.h> 44 45 #include <machine/trap.h> 46 #include <machine/db_machdep.h> 47 48 #include <ddb/db_active.h> 49 #include <ddb/db_command.h> 50 #include <ddb/db_sym.h> 51 #ifdef _KERNEL 52 #include <ddb/db_extern.h> 53 #endif 54 55 db_regs_t ddb_regs; 56 57 #ifdef _KERNEL 58 static void kdbprinttrap(int, int); 59 60 /* 61 * Received keyboard interrupt sequence. 62 */ 63 void 64 kdb_kintr(db_regs_t *regs) 65 { 66 67 if (db_active == 0 && (boothowto & RB_KDB)) { 68 printf("\n\nkernel: keyboard interrupt\n"); 69 kdb_trap(-1, regs); 70 } 71 } 72 73 /* 74 * kdb_trap - field a TRACE or BPT trap 75 * Return non-zero if we "handled" the trap. 76 */ 77 int 78 kdb_trap(int type, db_regs_t *regs) 79 { 80 81 switch (type) { 82 case T_TRACE: /* single-step */ 83 case T_BREAKPOINT: /* breakpoint */ 84 /* case T_WATCHPOINT:*/ 85 break; 86 case -1: 87 break; 88 default: 89 kdbprinttrap(type, 0); 90 if (db_recover != 0) { 91 /* This will longjmp back to db_command_loop */ 92 db_error("Caught exception in ddb.\n"); 93 /*NOTREACHED*/ 94 } 95 /* 96 * Tell caller "We did NOT handle the trap." 97 * Caller should panic or whatever. 98 */ 99 return 0; 100 } 101 102 /* 103 * We'd like to be on a separate debug stack here, but 104 * that's easier to do in locore.s before we get here. 105 * See sun3/locore.s:T_TRACE for stack switch code. 106 */ 107 108 ddb_regs = *regs; 109 110 db_active++; 111 cnpollc(true); /* set polling mode, unblank video */ 112 113 db_trap(type, 0); /* where the work happens */ 114 115 cnpollc(false); /* resume interrupt mode */ 116 db_active--; 117 118 *regs = ddb_regs; 119 120 /* 121 * Indicate that single_step is for KDB. 122 * But lock out interrupts to prevent TRACE_KDB from setting the 123 * trace bit in the current SR (and trapping while exiting KDB). 124 */ 125 (void)spl7(); 126 127 /* 128 * Tell caller "We HAVE handled the trap." 129 * Caller will return to locore and rte. 130 */ 131 return 1; 132 } 133 134 /* 135 * Print trap reason. 136 */ 137 static void 138 kdbprinttrap(int type, int code) 139 { 140 printf("kernel: %s\n", trap_desc(type)); 141 } 142 143 void 144 cpu_Debugger(void) 145 { 146 147 __asm ("trap #15"); 148 } 149 #endif 150