1/*
2 * Copyright 2002-2015, Axel D��rfler, axeld@pinc-software.de
3 * Distributed under the terms of the MIT 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#if __GNUC__ >= 5 && !defined(__cplusplus)
73#	define STATIC_ASSERT(x) _Static_assert(x, "static assert failed!")
74#elif defined(__cplusplus) && __cplusplus >= 201103L
75#	define STATIC_ASSERT(x) static_assert(x, "static assert failed!")
76#else
77#	define STATIC_ASSERT(x)								\
78		do {												\
79			struct __staticAssertStruct__ {					\
80				char __static_assert_failed__[2*(x) - 1];	\
81			};												\
82		} while (false)
83#endif
84
85#if KDEBUG
86#	define KDEBUG_ONLY(x)				x
87#else
88#	define KDEBUG_ONLY(x)				/* nothing */
89#endif
90
91
92// Macros for for placing marker functions. They can be used to mark the
93// beginning and end of code sections (e.g. used in the slab code).
94#define RANGE_MARKER_FUNCTION(functionName) \
95	void functionName() {}
96#define RANGE_MARKER_FUNCTION_BEGIN(scope) \
97	RANGE_MARKER_FUNCTION(scope##_begin)
98#define RANGE_MARKER_FUNCTION_END(scope) \
99	RANGE_MARKER_FUNCTION(scope##_end)
100
101#define RANGE_MARKER_FUNCTION_PROTOTYPE(functionName) \
102	void functionName();
103#define RANGE_MARKER_FUNCTION_PROTOTYPES(scope)		\
104	RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_begin)	\
105	RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_end)
106#define RANGE_MARKER_FUNCTION_ADDRESS_RANGE(scope) \
107	(addr_t)&scope##_begin, (addr_t)&scope##_end
108
109
110// command return value
111#define B_KDEBUG_ERROR			4
112#define B_KDEBUG_RESTART_PIPE	5
113
114// command flags
115#define B_KDEBUG_DONT_PARSE_ARGUMENTS	(0x01)
116#define B_KDEBUG_PIPE_FINAL_RERUN		(0x02)
117
118
119struct arch_debug_registers;
120
121
122struct debugger_module_info {
123	module_info info;
124
125	void (*enter_debugger)(void);
126	void (*exit_debugger)(void);
127
128	// I/O hooks
129	int (*debugger_puts)(const char *str, int32 length);
130	int (*debugger_getchar)(void);
131	// TODO: add hooks for tunnelling gdb ?
132
133	// Misc. hooks
134	bool (*emergency_key_pressed)(char key);
135};
136
137struct debugger_demangle_module_info {
138	module_info info;
139
140	const char* (*demangle_symbol)(const char* name, char* buffer,
141		size_t bufferSize, bool* _isObjectMethod);
142	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
143		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
144};
145
146
147typedef struct debug_page_fault_info {
148	addr_t	fault_address;
149	addr_t	pc;
150	uint32	flags;
151} debug_page_fault_info;
152
153// debug_page_fault_info::flags
154#define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
155#define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
156											   unknown */
157
158
159#ifdef __cplusplus
160extern "C" {
161#endif
162
163struct kernel_args;
164
165extern void		debug_init(struct kernel_args *args);
166extern void		debug_init_post_vm(struct kernel_args *args);
167extern void		debug_init_post_settings(struct kernel_args *args);
168extern void		debug_init_post_modules(struct kernel_args *args);
169extern void		debug_early_boot_message(const char *string);
170extern void		debug_puts(const char *s, int32 length);
171extern bool		debug_debugger_running(void);
172extern bool		debug_screen_output_enabled(void);
173extern void		debug_stop_screen_debug_output(void);
174extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
175					uint32 flags);
176extern debug_page_fault_info* debug_get_page_fault_info();
177extern void		debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver);
178extern void		debug_double_fault(int32 cpu);
179extern bool		debug_emergency_key_pressed(char key);
180extern bool		debug_is_kernel_memory_accessible(addr_t address, size_t size,
181					uint32 protection);
182extern int		debug_call_with_fault_handler(jmp_buf jumpBuffer,
183					void (*function)(void*), void* parameter);
184extern status_t	debug_memcpy(team_id teamID, void* to, const void* from,
185					size_t size);
186extern ssize_t	debug_strlcpy(team_id teamID, char* to, const char* from,
187					size_t size);
188
189extern char		kgetc(void);
190extern void		kputs(const char *string);
191extern void		kputs_unfiltered(const char *string);
192extern void		kprintf_unfiltered(const char *format, ...)
193					__attribute__ ((format (__printf__, 1, 2)));
194extern void		dprintf_no_syslog(const char *format, ...)
195					__attribute__ ((format (__printf__, 1, 2)));
196
197extern bool		is_debug_variable_defined(const char* variableName);
198extern bool		set_debug_variable(const char* variableName, uint64 value);
199extern uint64	get_debug_variable(const char* variableName,
200					uint64 defaultValue);
201extern bool		unset_debug_variable(const char* variableName);
202extern void		unset_all_debug_variables();
203
204extern bool		evaluate_debug_expression(const char* expression,
205					uint64* result, bool silent);
206extern int		evaluate_debug_command(const char* command);
207extern status_t	parse_next_debug_command_argument(const char** expressionString,
208					char* buffer, size_t bufferSize);
209
210extern status_t	add_debugger_command_etc(const char* name,
211					debugger_command_hook func, const char* description,
212					const char* usage, uint32 flags);
213extern status_t	add_debugger_command_alias(const char* newName,
214					const char* oldName, const char* description);
215extern bool		print_debugger_command_usage(const char* command);
216extern bool		has_debugger_command(const char* command);
217
218extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
219					size_t bufferSize, bool* _isObjectMethod);
220extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
221					const char* symbol, char* name, size_t nameSize,
222					int32* _type, size_t* _argumentLength);
223
224extern BKernel_Thread* debug_set_debugged_thread(BKernel_Thread* thread);
225extern BKernel_Thread* debug_get_debugged_thread();
226extern bool debug_is_debugged_team(team_id teamID);
227
228extern struct arch_debug_registers* debug_get_debug_registers(int32 cpu);
229
230extern status_t _user_kernel_debugger(const char *message);
231extern void _user_register_syslog_daemon(port_id port);
232extern void	_user_debug_output(const char *userString);
233
234#ifdef __cplusplus
235}
236#endif
237
238
239#ifdef __cplusplus
240
241struct DebuggedThreadSetter {
242	DebuggedThreadSetter(Thread* thread)
243		:
244		fPreviousThread(debug_set_debugged_thread(thread))
245	{
246	}
247
248	~DebuggedThreadSetter()
249	{
250		debug_set_debugged_thread(fPreviousThread);
251	}
252
253private:
254	Thread*	fPreviousThread;
255};
256
257
258#endif	// __cplusplus
259
260
261#endif	/* _KERNEL_DEBUG_H */
262