xref: /freebsd/sys/sys/kdb.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
160727d8bSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c4e20cadSPedro F. Giffuni  *
4cbc17435SMarcel Moolenaar  * Copyright (c) 2004 Marcel Moolenaar
5cbc17435SMarcel Moolenaar  * All rights reserved.
6cbc17435SMarcel Moolenaar  *
7cbc17435SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
8cbc17435SMarcel Moolenaar  * modification, are permitted provided that the following conditions
9cbc17435SMarcel Moolenaar  * are met:
10cbc17435SMarcel Moolenaar  *
11cbc17435SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
12cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
13cbc17435SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
14cbc17435SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
15cbc17435SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
16cbc17435SMarcel Moolenaar  *
17cbc17435SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18cbc17435SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19cbc17435SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20cbc17435SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21cbc17435SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22cbc17435SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cbc17435SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cbc17435SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cbc17435SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26cbc17435SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cbc17435SMarcel Moolenaar  */
28cbc17435SMarcel Moolenaar 
29cbc17435SMarcel Moolenaar #ifndef _SYS_KDB_H_
30cbc17435SMarcel Moolenaar #define	_SYS_KDB_H_
31cbc17435SMarcel Moolenaar 
32dda17b36SConrad Meyer #include <sys/linker_set.h>
33cbc17435SMarcel Moolenaar #include <machine/setjmp.h>
34cbc17435SMarcel Moolenaar 
350cc457b0SJohn Baldwin struct pcb;
360cc457b0SJohn Baldwin struct thread;
370cc457b0SJohn Baldwin struct trapframe;
380cc457b0SJohn Baldwin 
39cbc17435SMarcel Moolenaar typedef int dbbe_init_f(void);
40cbc17435SMarcel Moolenaar typedef void dbbe_trace_f(void);
410cc457b0SJohn Baldwin typedef void dbbe_trace_thread_f(struct thread *);
42cbc17435SMarcel Moolenaar typedef int dbbe_trap_f(int, int);
43cbc17435SMarcel Moolenaar 
44cbc17435SMarcel Moolenaar struct kdb_dbbe {
45cbc17435SMarcel Moolenaar 	const char	*dbbe_name;
46cbc17435SMarcel Moolenaar 	dbbe_init_f	*dbbe_init;
47cbc17435SMarcel Moolenaar 	dbbe_trace_f	*dbbe_trace;
480cc457b0SJohn Baldwin 	dbbe_trace_thread_f *dbbe_trace_thread;
49cbc17435SMarcel Moolenaar 	dbbe_trap_f	*dbbe_trap;
50cbc17435SMarcel Moolenaar 	int		dbbe_active;
51cbc17435SMarcel Moolenaar };
52cbc17435SMarcel Moolenaar 
530cc457b0SJohn Baldwin #define	KDB_BACKEND(name, init, trace, trace_thread, trap) \
54cbc17435SMarcel Moolenaar 	static struct kdb_dbbe name##_dbbe = {		\
55cbc17435SMarcel Moolenaar 		.dbbe_name = #name,			\
56cbc17435SMarcel Moolenaar 		.dbbe_init = init,			\
57cbc17435SMarcel Moolenaar 		.dbbe_trace = trace,			\
580cc457b0SJohn Baldwin 		.dbbe_trace_thread = trace_thread,	\
59cbc17435SMarcel Moolenaar 		.dbbe_trap = trap			\
60cbc17435SMarcel Moolenaar 	};						\
61cbc17435SMarcel Moolenaar 	DATA_SET(kdb_dbbe_set, name##_dbbe)
62cbc17435SMarcel Moolenaar 
63dda17b36SConrad Meyer SET_DECLARE(kdb_dbbe_set, struct kdb_dbbe);
64dda17b36SConrad Meyer 
65614e1868SMateusz Guzik extern u_char kdb_active;		/* Non-zero while in debugger. */
66ba0ced82SEric van Gyzen extern int debugger_on_panic;		/* enter the debugger on panic. */
67b317cfd4SJohn Baldwin extern int debugger_on_trap;		/* enter the debugger on trap. */
68cbc17435SMarcel Moolenaar extern struct kdb_dbbe *kdb_dbbe;	/* Default debugger backend or NULL. */
69cbc17435SMarcel Moolenaar extern struct trapframe *kdb_frame;	/* Frame to kdb_trap(). */
70cbc17435SMarcel Moolenaar extern struct pcb *kdb_thrctx;		/* Current context. */
71cbc17435SMarcel Moolenaar extern struct thread *kdb_thread;	/* Current thread. */
72cbc17435SMarcel Moolenaar 
73cbc17435SMarcel Moolenaar int	kdb_alt_break(int, int *);
744cf75455SRobert Watson int	kdb_alt_break_gdb(int, int *);
754cf75455SRobert Watson int	kdb_break(void);
76cbc17435SMarcel Moolenaar void	kdb_backtrace(void);
770cc457b0SJohn Baldwin void	kdb_backtrace_thread(struct thread *);
783bcd2440SMarcel Moolenaar int	kdb_dbbe_select(const char *);
793de213ccSRobert Watson void	kdb_enter(const char *, const char *);
80cbc17435SMarcel Moolenaar void	kdb_init(void);
81cbc17435SMarcel Moolenaar void *	kdb_jmpbuf(jmp_buf);
8243d7128cSPeter Wemm void	kdb_panic(const char *);
8343d7128cSPeter Wemm void	kdb_reboot(void);
84cbc17435SMarcel Moolenaar void	kdb_reenter(void);
85212ff84fSEdward Tomasz Napierala void	kdb_reenter_silent(void);
86cbc17435SMarcel Moolenaar struct pcb *kdb_thr_ctx(struct thread *);
87cbc17435SMarcel Moolenaar struct thread *kdb_thr_first(void);
883d4f3136SMarcel Moolenaar struct thread *kdb_thr_from_pid(pid_t);
893d4f3136SMarcel Moolenaar struct thread *kdb_thr_lookup(lwpid_t);
90cbc17435SMarcel Moolenaar struct thread *kdb_thr_next(struct thread *);
91cbc17435SMarcel Moolenaar int	kdb_thr_select(struct thread *);
92cbc17435SMarcel Moolenaar int	kdb_trap(int, int, struct trapframe *);
93cbc17435SMarcel Moolenaar 
943de213ccSRobert Watson /*
953de213ccSRobert Watson  * KDB enters the debugger via breakpoint(), which leaves the debugger without
963de213ccSRobert Watson  * a lot of information about why it was entered.  This simple enumerated set
973de213ccSRobert Watson  * captures some basic information.
983de213ccSRobert Watson  *
993de213ccSRobert Watson  * It is recommended that values here be short (<16 character) alpha-numeric
1003de213ccSRobert Watson  * strings, as they will be used to construct DDB(4) script names.
1013de213ccSRobert Watson  */
1023de213ccSRobert Watson extern const char * volatile kdb_why;
1033de213ccSRobert Watson #define	KDB_WHY_UNSET		NULL		/* No reason set. */
1043de213ccSRobert Watson #define	KDB_WHY_PANIC		"panic"		/* panic() was called. */
105face9c86SAlfred Perlstein #define	KDB_WHY_KASSERT		"kassert"	/* kassert failed. */
106f3f6ecb4SAndriy Gapon #define	KDB_WHY_TRAP		"trap"		/* Fatal trap. */
1073de213ccSRobert Watson #define	KDB_WHY_SYSCTL		"sysctl"	/* Sysctl entered debugger. */
1083de213ccSRobert Watson #define	KDB_WHY_BOOTFLAGS	"bootflags"	/* Boot flags were set. */
1093de213ccSRobert Watson #define	KDB_WHY_WITNESS		"witness"	/* Witness entered debugger. */
1103de213ccSRobert Watson #define	KDB_WHY_VFSLOCK		"vfslock"	/* VFS detected lock problem. */
1113de213ccSRobert Watson #define	KDB_WHY_NETGRAPH	"netgraph"	/* Netgraph entered debugger. */
1123de213ccSRobert Watson #define	KDB_WHY_BREAK		"break"		/* Console or serial break. */
1133de213ccSRobert Watson #define	KDB_WHY_WATCHDOG	"watchdog"	/* Watchdog entered debugger. */
1143de213ccSRobert Watson #define	KDB_WHY_CAM		"cam"		/* CAM has entered debugger. */
1153de213ccSRobert Watson #define	KDB_WHY_ACPI		"acpi"		/* ACPI entered debugger. */
1164b547324SAttilio Rao #define	KDB_WHY_TRAPSIG		"trapsig"	/* Sparc fault. */
1173de213ccSRobert Watson #define	KDB_WHY_POWERFAIL	"powerfail"	/* Powerfail NMI. */
1183de213ccSRobert Watson #define	KDB_WHY_MAC		"mac"		/* MAC Framework. */
1193de213ccSRobert Watson #define	KDB_WHY_POWERPC		"powerpc"	/* Unhandled powerpc intr. */
1203de213ccSRobert Watson #define	KDB_WHY_UNIONFS		"unionfs"	/* Unionfs bug. */
121238d4dadSJohn Birrell #define	KDB_WHY_DTRACE		"dtrace"	/* DTrace action entered debugger. */
1221ea833a5STom Jones #define	KDB_WHY_REBOOT		"reboot"	/* reboot was requested. */
1233de213ccSRobert Watson 
12443d7128cSPeter Wemm /* Return values for kdb_alt_break */
12543d7128cSPeter Wemm #define	KDB_REQ_DEBUGGER	1	/* User requested Debugger */
12643d7128cSPeter Wemm #define	KDB_REQ_PANIC		2	/* User requested a panic */
12743d7128cSPeter Wemm #define	KDB_REQ_REBOOT		3	/* User requested a clean reboot */
12843d7128cSPeter Wemm 
129763107f2SMitchell Horne /* Debug breakpoint/watchpoint access types */
130763107f2SMitchell Horne #define	KDB_DBG_ACCESS_EXEC	0
131763107f2SMitchell Horne #define	KDB_DBG_ACCESS_R	1
132763107f2SMitchell Horne #define	KDB_DBG_ACCESS_W	2
133763107f2SMitchell Horne #define	KDB_DBG_ACCESS_RW	3
134763107f2SMitchell Horne 
135cbc17435SMarcel Moolenaar #endif /* !_SYS_KDB_H_ */
136