1/*
2 * Copyright 2002-2010, Axel Dörfler, axeld@pinc-software.de
3 * Distributed under the terms of the Haiku License.
4 *
5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
7 */
8#ifndef _KERNEL_DEBUG_H
9#define _KERNEL_DEBUG_H
10
11
12#include <setjmp.h>
13
14#include <KernelExport.h>
15#include <module.h>
16
17#include "kernel_debug_config.h"
18
19
20// We need the BKernel::Thread type below (opaquely) in the exported C
21// functions below. Since this header is currently still included by plain C
22// code, we define a dummy type BKernel_Thread in C mode and a equally named
23// macro in C++ mode.
24#ifdef __cplusplus
25	namespace BKernel {
26		struct Thread;
27	}
28
29	using BKernel::Thread;
30#	define BKernel_Thread	Thread
31#else
32	typedef struct BKernel_Thread BKernel_Thread;
33#endif
34
35
36/*	KDEBUG
37	The kernel debug level.
38	Level 1 is usual asserts, > 1 should be used for very expensive runtime
39	checks
40 */
41#if !defined(KDEBUG)
42#	if DEBUG
43#		define KDEBUG 1
44#	else
45#		define KDEBUG 0
46#	endif
47#endif
48
49#define ASSERT_ALWAYS(x) \
50	do {																	\
51		if (!(x)) {															\
52			panic("ASSERT FAILED (%s:%d): %s", __FILE__, __LINE__, #x);		\
53		}																	\
54	} while (0)
55
56#define ASSERT_ALWAYS_PRINT(x, format, args...) \
57	do {																	\
58		if (!(x)) {															\
59			panic("ASSERT FAILED (%s:%d): %s; " format, __FILE__, __LINE__,	\
60				#x, args);													\
61		}																	\
62	} while (0)
63
64#if KDEBUG
65#	define ASSERT(x)						ASSERT_ALWAYS(x)
66#	define ASSERT_PRINT(x, format, args...)	ASSERT_ALWAYS_PRINT(x, format, args)
67#else
68#	define ASSERT(x)						do { } while(0)
69#	define ASSERT_PRINT(x, format, args...)	do { } while(0)
70#endif
71
72#define STATIC_ASSERT(x)								\
73	do {												\
74		struct __staticAssertStruct__ {					\
75			char __static_assert_failed__[2*(x) - 1];	\
76		};												\
77	} while (false)
78
79#if KDEBUG
80#	define KDEBUG_ONLY(x)				x
81#else
82#	define KDEBUG_ONLY(x)				/* nothing */
83#endif
84
85
86// Macros for for placing marker functions. They can be used to mark the
87// beginning and end of code sections (e.g. used in the slab code).
88#define RANGE_MARKER_FUNCTION(functionName) \
89	void functionName() {}
90#define RANGE_MARKER_FUNCTION_BEGIN(scope) \
91	RANGE_MARKER_FUNCTION(scope##_begin)
92#define RANGE_MARKER_FUNCTION_END(scope) \
93	RANGE_MARKER_FUNCTION(scope##_end)
94
95#define RANGE_MARKER_FUNCTION_PROTOTYPE(functionName) \
96	void functionName();
97#define RANGE_MARKER_FUNCTION_PROTOTYPES(scope)		\
98	RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_begin)	\
99	RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_end)
100#define RANGE_MARKER_FUNCTION_ADDRESS_RANGE(scope) \
101	(addr_t)&scope##_begin, (addr_t)&scope##_end
102
103
104// command return value
105#define B_KDEBUG_ERROR			4
106#define B_KDEBUG_RESTART_PIPE	5
107
108// command flags
109#define B_KDEBUG_DONT_PARSE_ARGUMENTS	(0x01)
110#define B_KDEBUG_PIPE_FINAL_RERUN		(0x02)
111
112
113struct arch_debug_registers;
114
115
116struct debugger_module_info {
117	module_info info;
118
119	void (*enter_debugger)(void);
120	void (*exit_debugger)(void);
121
122	// I/O hooks
123	int (*debugger_puts)(const char *str, int32 length);
124	int (*debugger_getchar)(void);
125	// TODO: add hooks for tunnelling gdb ?
126
127	// Misc. hooks
128	bool (*emergency_key_pressed)(char key);
129};
130
131struct debugger_demangle_module_info {
132	module_info info;
133
134	const char* (*demangle_symbol)(const char* name, char* buffer,
135		size_t bufferSize, bool* _isObjectMethod);
136	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
137		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
138};
139
140
141typedef struct debug_page_fault_info {
142	addr_t	fault_address;
143	addr_t	pc;
144	uint32	flags;
145} debug_page_fault_info;
146
147// debug_page_fault_info::flags
148#define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
149#define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
150											   unknown */
151
152
153#ifdef __cplusplus
154extern "C" {
155#endif
156
157struct kernel_args;
158
159extern void		debug_init(struct kernel_args *args);
160extern void		debug_init_post_vm(struct kernel_args *args);
161extern void		debug_init_post_settings(struct kernel_args *args);
162extern void		debug_init_post_modules(struct kernel_args *args);
163extern void		debug_early_boot_message(const char *string);
164extern void		debug_puts(const char *s, int32 length);
165extern bool		debug_debugger_running(void);
166extern bool		debug_screen_output_enabled(void);
167extern void		debug_stop_screen_debug_output(void);
168extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
169					uint32 flags);
170extern debug_page_fault_info* debug_get_page_fault_info();
171extern void		debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver);
172extern void		debug_double_fault(int32 cpu);
173extern bool		debug_emergency_key_pressed(char key);
174extern bool		debug_is_kernel_memory_accessible(addr_t address, size_t size,
175					uint32 protection);
176extern int		debug_call_with_fault_handler(jmp_buf jumpBuffer,
177					void (*function)(void*), void* parameter);
178extern status_t	debug_memcpy(team_id teamID, void* to, const void* from,
179					size_t size);
180extern ssize_t	debug_strlcpy(team_id teamID, char* to, const char* from,
181					size_t size);
182
183extern char		kgetc(void);
184extern void		kputs(const char *string);
185extern void		kputs_unfiltered(const char *string);
186extern void		kprintf_unfiltered(const char *format, ...)
187					__attribute__ ((format (__printf__, 1, 2)));
188extern void		dprintf_no_syslog(const char *format, ...)
189					__attribute__ ((format (__printf__, 1, 2)));
190
191extern bool		is_debug_variable_defined(const char* variableName);
192extern bool		set_debug_variable(const char* variableName, uint64 value);
193extern uint64	get_debug_variable(const char* variableName,
194					uint64 defaultValue);
195extern bool		unset_debug_variable(const char* variableName);
196extern void		unset_all_debug_variables();
197
198extern bool		evaluate_debug_expression(const char* expression,
199					uint64* result, bool silent);
200extern int		evaluate_debug_command(const char* command);
201extern status_t	parse_next_debug_command_argument(const char** expressionString,
202					char* buffer, size_t bufferSize);
203
204extern status_t	add_debugger_command_etc(const char* name,
205					debugger_command_hook func, const char* description,
206					const char* usage, uint32 flags);
207extern status_t	add_debugger_command_alias(const char* newName,
208					const char* oldName, const char* description);
209extern bool		print_debugger_command_usage(const char* command);
210extern bool		has_debugger_command(const char* command);
211
212extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
213					size_t bufferSize, bool* _isObjectMethod);
214extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
215					const char* symbol, char* name, size_t nameSize,
216					int32* _type, size_t* _argumentLength);
217
218extern BKernel_Thread* debug_set_debugged_thread(BKernel_Thread* thread);
219extern BKernel_Thread* debug_get_debugged_thread();
220extern bool debug_is_debugged_team(team_id teamID);
221
222extern struct arch_debug_registers* debug_get_debug_registers(int32 cpu);
223
224extern status_t	_user_kernel_debugger(const char *message);
225extern void		_user_debug_output(const char *userString);
226
227#ifdef __cplusplus
228}
229#endif
230
231
232#ifdef __cplusplus
233
234struct DebuggedThreadSetter {
235	DebuggedThreadSetter(Thread* thread)
236		:
237		fPreviousThread(debug_set_debugged_thread(thread))
238	{
239	}
240
241	~DebuggedThreadSetter()
242	{
243		debug_set_debugged_thread(fPreviousThread);
244	}
245
246private:
247	Thread*	fPreviousThread;
248};
249
250
251#endif	// __cplusplus
252
253
254#endif	/* _KERNEL_DEBUG_H */
255