xref: /haiku/headers/os/kernel/debugger.h (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _DEBUGGER_H
6 #define _DEBUGGER_H
7 
8 #include <signal.h>
9 
10 #include <image.h>
11 #include <OS.h>
12 
13 // include architecture specific definitions
14 #ifdef __INTEL__
15 	#include <arch/x86/arch_debugger.h>
16 #elif __POWERPC__
17 	#include <arch/ppc/arch_debugger.h>
18 #elif __M68K__
19 	#include <arch/m68k/arch_debugger.h>
20 #else
21 	#error you need to write a <arch/<cpu>/arch_debugger.h>
22 #endif
23 
24 typedef struct debug_cpu_state debug_cpu_state;
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 extern status_t	install_default_debugger(port_id debuggerPort);
31 extern port_id	install_team_debugger(team_id team, port_id debuggerPort);
32 extern status_t	remove_team_debugger(team_id team);
33 extern status_t	debug_thread(thread_id thread);
34 extern void		wait_for_debugger(void);
35 
36 // EXPERIMENTAL: Self-debugging functions. Will fail when a team debugger is
37 // installed. A breakpoint/watchpoint hit will cause the default debugger to
38 // be installed for the team.
39 extern status_t	set_debugger_breakpoint(void *address);
40 extern status_t	clear_debugger_breakpoint(void *address);
41 extern status_t	set_debugger_watchpoint(void *address, uint32 type,
42 					int32 length);
43 extern status_t	clear_debugger_watchpoint(void *address);
44 
45 
46 // team debugging flags
47 enum {
48 	// event mask: If a flag is set, any of the team's threads will stop when
49 	// the respective event occurs. All flags are enabled by default. Always
50 	// enabled are debugger() calls and hardware exceptions, as well as the
51 	// deletion of the debugged team.
52 	B_TEAM_DEBUG_SIGNALS						= 0x00010000,
53 	B_TEAM_DEBUG_PRE_SYSCALL					= 0x00020000,
54 	B_TEAM_DEBUG_POST_SYSCALL					= 0x00040000,
55 	B_TEAM_DEBUG_TEAM_CREATION					= 0x00080000,
56 	B_TEAM_DEBUG_THREADS						= 0x00100000,
57 	B_TEAM_DEBUG_IMAGES							= 0x00200000,
58 
59 	// new thread handling
60 	B_TEAM_DEBUG_STOP_NEW_THREADS				= 0x01000000,
61 
62 	B_TEAM_DEBUG_USER_FLAG_MASK					= 0xffff0000,
63 };
64 
65 // per-thread debugging flags
66 enum {
67 	// event mask: If a flag is set, the thread will stop when the respective
68 	// event occurs. If there is a corresponding team flag, it is sufficient,
69 	// if either is set. Per default none of the flags is set.
70 	B_THREAD_DEBUG_PRE_SYSCALL					= 0x00010000,
71 	B_THREAD_DEBUG_POST_SYSCALL					= 0x00020000,
72 
73 	// child thread handling
74 	B_THREAD_DEBUG_STOP_CHILD_THREADS			= 0x00100000,
75 	B_THREAD_DEBUG_SYSCALL_TRACE_CHILD_THREADS	= 0x00200000,
76 
77 	B_THREAD_DEBUG_USER_FLAG_MASK				= 0xffff0000,
78 };
79 
80 // in case of a B_EXCEPTION_OCCURRED event: the type of the exception
81 typedef enum {
82 	B_NON_MASKABLE_INTERRUPT	= 0,
83 	B_MACHINE_CHECK_EXCEPTION,
84 	B_SEGMENT_VIOLATION,
85 	B_ALIGNMENT_EXCEPTION,
86 	B_DIVIDE_ERROR,
87 	B_OVERFLOW_EXCEPTION,
88 	B_BOUNDS_CHECK_EXCEPTION,
89 	B_INVALID_OPCODE_EXCEPTION,
90 	B_SEGMENT_NOT_PRESENT,
91 	B_STACK_FAULT,
92 	B_GENERAL_PROTECTION_FAULT,
93 	B_FLOATING_POINT_EXCEPTION,
94 } debug_exception_type;
95 
96 // Value indicating how a stopped thread shall continue.
97 enum {
98 	B_THREAD_DEBUG_HANDLE_EVENT = 0,	// handle the event normally
99 										// (e.g. a signal is delivered, a
100 										// CPU fault kills the team,...)
101 	B_THREAD_DEBUG_IGNORE_EVENT,		// ignore the event and continue as if
102 										// it didn't occur (e.g. a signal or
103 										// a CPU fault will be ignored)
104 };
105 
106 // watchpoint types (ToDo: Check PPC support.)
107 enum {
108 	B_DATA_READ_WATCHPOINT = 0,			// !x86
109 	B_DATA_WRITE_WATCHPOINT,
110 	B_DATA_READ_WRITE_WATCHPOINT,
111 };
112 
113 // how to apply signal ignore masks
114 typedef enum {
115 	B_DEBUG_SIGNAL_MASK_AND	= 0,
116 	B_DEBUG_SIGNAL_MASK_OR,
117 	B_DEBUG_SIGNAL_MASK_SET,
118 } debug_signal_mask_op;
119 
120 #define B_DEBUG_SIGNAL_TO_MASK(signal) (1ULL << ((signal) - 1))
121 
122 // maximal number of bytes to read/write via B_DEBUG_MESSAGE_{READ,WRITE]_MEMORY
123 enum {
124 	B_MAX_READ_WRITE_MEMORY_SIZE	= 1024,
125 };
126 
127 // messages to the debug nub thread
128 typedef enum {
129 	B_DEBUG_MESSAGE_READ_MEMORY	= 0,	// read from the team's memory
130 	B_DEBUG_MESSAGE_WRITE_MEMORY,		// write to the team's memory
131 	B_DEBUG_MESSAGE_SET_TEAM_FLAGS,		// set the team's debugging flags
132 	B_DEBUG_MESSAGE_SET_THREAD_FLAGS,	// set a thread's debugging flags
133 	B_DEBUG_MESSAGE_CONTINUE_THREAD,	// continue a stopped thread
134 	B_DEBUG_MESSAGE_SET_CPU_STATE,		// change a stopped thread's CPU state
135 	B_DEBUG_MESSAGE_GET_CPU_STATE,		// get the thread's current CPU state
136 	B_DEBUG_MESSAGE_SET_BREAKPOINT,		// set a breakpoint
137 	B_DEBUG_MESSAGE_CLEAR_BREAKPOINT,	// clear a breakpoint
138 	B_DEBUG_MESSAGE_SET_WATCHPOINT,		// set a watchpoint
139 	B_DEBUG_MESSAGE_CLEAR_WATCHPOINT,	// clear a watchpoint
140 	B_DEBUG_MESSAGE_SET_SIGNAL_MASKS,	// set/get a thread's masks of signals
141 	B_DEBUG_MESSAGE_GET_SIGNAL_MASKS,	//  the debugger is interested in
142 	B_DEBUG_MESSAGE_SET_SIGNAL_HANDLER,	// set/get a thread's signal handler for
143 	B_DEBUG_MESSAGE_GET_SIGNAL_HANDLER,	//  a signal
144 
145 	B_DEBUG_MESSAGE_PREPARE_HANDOVER,	// prepares the debugged team for being
146 										// handed over to another debugger;
147 										// the new debugger can just invoke
148 										// install_team_debugger()
149 } debug_nub_message;
150 
151 // messages sent to the debugger
152 typedef enum {
153 	B_DEBUGGER_MESSAGE_THREAD_DEBUGGED = 0,	// debugger message in reaction to
154 											// an invocation of debug_thread()
155 	B_DEBUGGER_MESSAGE_DEBUGGER_CALL,		// thread called debugger()
156 	B_DEBUGGER_MESSAGE_BREAKPOINT_HIT,		// thread hit a breakpoint
157 	B_DEBUGGER_MESSAGE_WATCHPOINT_HIT,		// thread hit a watchpoint
158 	B_DEBUGGER_MESSAGE_SINGLE_STEP,			// thread was single-stepped
159 	B_DEBUGGER_MESSAGE_PRE_SYSCALL,			// begin of a syscall
160 	B_DEBUGGER_MESSAGE_POST_SYSCALL,		// end of a syscall
161 	B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED,		// thread received a signal
162 	B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED,	// an exception occurred
163 	B_DEBUGGER_MESSAGE_TEAM_CREATED,		// the debugged team created a new
164 											// one
165 	B_DEBUGGER_MESSAGE_TEAM_DELETED,		// the debugged team is gone
166 	B_DEBUGGER_MESSAGE_THREAD_CREATED,		// a thread has been created
167 	B_DEBUGGER_MESSAGE_THREAD_DELETED,		// a thread has been deleted
168 	B_DEBUGGER_MESSAGE_IMAGE_CREATED,		// an image has been created
169 	B_DEBUGGER_MESSAGE_IMAGE_DELETED,		// an image has been deleted
170 
171 	B_DEBUGGER_MESSAGE_HANDED_OVER,			// the debugged team has been
172 											// handed over to another debugger
173 } debug_debugger_message;
174 
175 
176 // #pragma mark -
177 // #pragma mark ----- messages to the debug nub thread -----
178 
179 // B_DEBUG_MESSAGE_READ_MEMORY
180 
181 typedef struct {
182 	port_id		reply_port;		// port to send the reply to
183 	void		*address;		// address from which to read
184 	int32		size;			// number of bytes to read
185 } debug_nub_read_memory;
186 
187 typedef struct {
188 	status_t	error;			// B_OK, if reading went fine
189 	int32		size;			// the number of bytes actually read
190 								// > 0, iff error == B_OK
191 	char		data[B_MAX_READ_WRITE_MEMORY_SIZE];
192 								// the read data
193 } debug_nub_read_memory_reply;
194 
195 // B_DEBUG_MESSAGE_WRITE_MEMORY
196 
197 typedef struct {
198 	port_id		reply_port;		// port to send the reply to
199 	void		*address;		// address to which to write
200 	int32		size;			// number of bytes to write
201 	char		data[B_MAX_READ_WRITE_MEMORY_SIZE];
202 								// data to write
203 } debug_nub_write_memory;
204 
205 typedef struct {
206 	status_t	error;			// B_OK, if writing went fine
207 	int32		size;			// the number of bytes actually written
208 } debug_nub_write_memory_reply;
209 
210 // B_DEBUG_MESSAGE_SET_TEAM_FLAGS
211 
212 typedef struct {
213 	int32		flags;			// the new team debugging flags
214 } debug_nub_set_team_flags;
215 
216 // B_DEBUG_MESSAGE_SET_THREAD_FLAGS
217 
218 typedef struct {
219 	thread_id	thread;			// the thread
220 	int32		flags;			// the new thread debugging flags
221 } debug_nub_set_thread_flags;
222 
223 // B_DEBUG_MESSAGE_CONTINUE_THREAD
224 
225 typedef struct {
226 	thread_id	thread;			// the thread
227 	uint32		handle_event;	// how to handle the occurred event
228 	bool		single_step;	// true == single step, false == run full speed
229 } debug_nub_continue_thread;
230 
231 // B_DEBUG_MESSAGE_SET_CPU_STATE
232 
233 typedef struct {
234 	thread_id			thread;				// the thread
235 	debug_cpu_state		cpu_state;			// the new CPU state
236 } debug_nub_set_cpu_state;
237 
238 // B_DEBUG_MESSAGE_GET_CPU_STATE
239 
240 typedef struct {
241 	port_id					reply_port;		// port to send the reply to
242 	thread_id				thread;			// the thread
243 } debug_nub_get_cpu_state;
244 
245 typedef struct {
246 	status_t				error;		// != B_OK, if something went wrong
247 										// (bad thread ID, thread not stopped)
248 	debug_debugger_message	message;	// the reason why the thread stopped
249 	debug_cpu_state			cpu_state;	// the thread's CPU state
250 } debug_nub_get_cpu_state_reply;
251 
252 // B_DEBUG_MESSAGE_SET_BREAKPOINT
253 
254 typedef struct {
255 	port_id		reply_port;		// port to send the reply to
256 	void		*address;		// breakpoint address
257 } debug_nub_set_breakpoint;
258 
259 typedef struct {
260 	status_t	error;			// B_OK, if the breakpoint has been set
261 								// successfully
262 } debug_nub_set_breakpoint_reply;
263 
264 // B_DEBUG_MESSAGE_CLEAR_BREAKPOINT
265 
266 typedef struct {
267 	void		*address;		// breakpoint address
268 } debug_nub_clear_breakpoint;
269 
270 // B_DEBUG_MESSAGE_SET_WATCHPOINT
271 
272 typedef struct {
273 	port_id		reply_port;		// port to send the reply to
274 	void		*address;		// watchpoint address
275 	uint32		type;			// watchpoint type (see type constants above)
276 	int32		length;			// number of bytes to watch (typically 1, 2,
277 								// 4); architecture specific alignment
278 								// restrictions apply.
279 } debug_nub_set_watchpoint;
280 
281 typedef struct {
282 	status_t	error;			// B_OK, if the watchpoint has been set
283 								// successfully
284 } debug_nub_set_watchpoint_reply;
285 
286 // B_DEBUG_MESSAGE_CLEAR_WATCHPOINT
287 
288 typedef struct {
289 	void		*address;		// watchpoint address
290 } debug_nub_clear_watchpoint;
291 
292 // B_DEBUG_MESSAGE_SET_SIGNAL_MASKS
293 
294 typedef struct {
295 	thread_id				thread;				// the thread
296 	uint64					ignore_mask;		// the mask for signals the
297 												// debugger wishes not to be
298 												// notified of
299 	uint64					ignore_once_mask;	// the mask for signals the
300 												// debugger wishes not to be
301 												// notified of when they next
302 												// occur
303 	debug_signal_mask_op	ignore_op;			// what to do with ignore_mask
304 	debug_signal_mask_op	ignore_once_op;		// what to do with
305 												// ignore_once_mask
306 } debug_nub_set_signal_masks;
307 
308 // B_DEBUG_MESSAGE_GET_SIGNAL_MASKS
309 
310 typedef struct {
311 	port_id		reply_port;			// port to send the reply to
312 	thread_id	thread;				// the thread
313 } debug_nub_get_signal_masks;
314 
315 typedef struct {
316 	status_t	error;				// B_OK, if the thread exists
317 	uint64		ignore_mask;		// the mask for signals the debugger wishes
318 									// not to be notified of
319 	uint64		ignore_once_mask;	// the mask for signals the debugger wishes
320 									// not to be notified of when they next
321 									// occur
322 } debug_nub_get_signal_masks_reply;
323 
324 // B_DEBUG_MESSAGE_SET_SIGNAL_HANDLER
325 
326 typedef struct {
327 	thread_id			thread;		// the thread
328 	int					signal;		// the signal
329 	struct sigaction	handler;	// the new signal handler
330 } debug_nub_set_signal_handler;
331 
332 // B_DEBUG_MESSAGE_GET_SIGNAL_HANDLER
333 
334 typedef struct {
335 	port_id				reply_port;	// port to send the reply to
336 	thread_id			thread;		// the thread
337 	int					signal;		// the signal
338 } debug_nub_get_signal_handler;
339 
340 typedef struct {
341 	status_t			error;		// B_OK, if the thread exists
342 	struct sigaction	handler;	// the signal handler
343 } debug_nub_get_signal_handler_reply;
344 
345 // B_DEBUG_MESSAGE_PREPARE_HANDOVER
346 
347 // no parameters, no reply
348 
349 // union of all messages structures sent to the debug nub thread
350 typedef union {
351 	debug_nub_read_memory			read_memory;
352 	debug_nub_write_memory			write_memory;
353 	debug_nub_set_team_flags		set_team_flags;
354 	debug_nub_set_thread_flags		set_thread_flags;
355 	debug_nub_continue_thread		continue_thread;
356 	debug_nub_set_cpu_state			set_cpu_state;
357 	debug_nub_get_cpu_state			get_cpu_state;
358 	debug_nub_set_breakpoint		set_breakpoint;
359 	debug_nub_clear_breakpoint		clear_breakpoint;
360 	debug_nub_set_watchpoint		set_watchpoint;
361 	debug_nub_clear_watchpoint		clear_watchpoint;
362 	debug_nub_set_signal_masks		set_signal_masks;
363 	debug_nub_get_signal_masks		get_signal_masks;
364 	debug_nub_set_signal_handler	set_signal_handler;
365 	debug_nub_get_signal_handler	get_signal_handler;
366 } debug_nub_message_data;
367 
368 
369 // #pragma mark -
370 // #pragma mark ----- messages to the debugger -----
371 
372 // first member of all debugger messages -- not a message by itself
373 typedef struct {
374 	thread_id	thread;			// the thread being the event origin
375 	team_id		team;			// the thread's team
376 	port_id		nub_port;		// port to debug nub for this team (only set
377 								// for synchronous messages)
378 } debug_origin;
379 
380 // B_DEBUGGER_MESSAGE_THREAD_DEBUGGED
381 
382 typedef struct {
383 	debug_origin		origin;
384 } debug_thread_debugged;
385 
386 // B_DEBUGGER_MESSAGE_DEBUGGER_CALL
387 
388 typedef struct {
389 	debug_origin		origin;
390 	void				*message;	// address of the message passed to
391 									// debugger()
392 } debug_debugger_call;
393 
394 // B_DEBUGGER_MESSAGE_BREAKPOINT_HIT
395 
396 typedef struct {
397 	debug_origin		origin;
398 	debug_cpu_state		cpu_state;	// cpu state
399 	bool				software;	// true, if the is a software breakpoint
400 									// (i.e. caused by a respective trap
401 									// instruction)
402 } debug_breakpoint_hit;
403 
404 // B_DEBUGGER_MESSAGE_WATCHPOINT_HIT
405 
406 typedef struct {
407 	debug_origin		origin;
408 	debug_cpu_state		cpu_state;	// cpu state
409 } debug_watchpoint_hit;
410 
411 // B_DEBUGGER_MESSAGE_SINGLE_STEP
412 
413 typedef struct {
414 	debug_origin		origin;
415 	debug_cpu_state		cpu_state;	// cpu state
416 } debug_single_step;
417 
418 // B_DEBUGGER_MESSAGE_PRE_SYSCALL
419 
420 typedef struct {
421 	debug_origin	origin;
422 	uint32			syscall;		// the syscall number
423 	uint32			args[16];		// syscall arguments
424 } debug_pre_syscall;
425 
426 // B_DEBUGGER_MESSAGE_POST_SYSCALL
427 
428 typedef struct {
429 	debug_origin	origin;
430 	bigtime_t		start_time;		// time of syscall start
431 	bigtime_t		end_time;		// time of syscall completion
432 	uint64			return_value;	// the syscall's return value
433 	uint32			syscall;		// the syscall number
434 	uint32			args[16];		// syscall arguments
435 } debug_post_syscall;
436 
437 // B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED
438 
439 typedef struct {
440 	debug_origin		origin;
441 	int					signal;		// the signal
442 	struct sigaction	handler;	// the signal handler
443 	bool				deadly;		// true, if handling the signal will kill
444 									// the team
445 } debug_signal_received;
446 
447 // B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED
448 
449 typedef struct {
450 	debug_origin			origin;
451 	debug_exception_type	exception;		// the exception
452 	int						signal;			// the signal that will be sent,
453 											// when the thread continues
454 											// normally
455 } debug_exception_occurred;
456 
457 // B_DEBUGGER_MESSAGE_TEAM_CREATED
458 
459 typedef struct {
460 	debug_origin	origin;
461 	team_id			new_team;		// the newly created team
462 } debug_team_created;
463 
464 // B_DEBUGGER_MESSAGE_TEAM_DELETED
465 
466 typedef struct {
467 	debug_origin	origin;			// thread is < 0, team is the deleted team
468 									// (asynchronous message)
469 } debug_team_deleted;
470 
471 // B_DEBUGGER_MESSAGE_THREAD_CREATED
472 
473 typedef struct {
474 	debug_origin	origin;			// the thread that created the new thread
475 	team_id			new_thread;		// the newly created thread
476 } debug_thread_created;
477 
478 // B_DEBUGGER_MESSAGE_THREAD_DELETED
479 
480 typedef struct {
481 	debug_origin	origin;			// the deleted thread (asynchronous message)
482 } debug_thread_deleted;
483 
484 // B_DEBUGGER_MESSAGE_IMAGE_CREATED
485 
486 typedef struct {
487 	debug_origin	origin;
488 	image_info		info;			// info for the image
489 } debug_image_created;
490 
491 // B_DEBUGGER_MESSAGE_IMAGE_DELETED
492 
493 typedef struct {
494 	debug_origin	origin;
495 	image_info		info;			// info for the image
496 } debug_image_deleted;
497 
498 // B_DEBUGGER_MESSAGE_HANDED_OVER
499 
500 typedef struct {
501 	debug_origin	origin;			// thread is < 0, team is the deleted team
502 									// (asynchronous message)
503 	team_id			debugger;		// the new debugger
504 	port_id			debugger_port;	// the port the new debugger uses
505 } debug_handed_over;
506 
507 // union of all messages structures sent to the debugger
508 typedef union {
509 	debug_thread_debugged			thread_debugged;
510 	debug_debugger_call				debugger_call;
511 	debug_breakpoint_hit			breakpoint_hit;
512 	debug_watchpoint_hit			watchpoint_hit;
513 	debug_single_step				single_step;
514 	debug_pre_syscall				pre_syscall;
515 	debug_post_syscall				post_syscall;
516 	debug_signal_received			signal_received;
517 	debug_exception_occurred		exception_occurred;
518 	debug_team_created				team_created;
519 	debug_team_deleted				team_deleted;
520 	debug_thread_created			thread_created;
521 	debug_thread_deleted			thread_deleted;
522 	debug_image_created				image_created;
523 	debug_image_deleted				image_deleted;
524 	debug_handed_over				handed_over;
525 
526 	debug_origin					origin;	// for convenience (no real message)
527 } debug_debugger_message_data;
528 
529 
530 extern void get_debug_message_string(debug_debugger_message message,
531 		char *buffer, int32 bufferSize);
532 extern void get_debug_exception_string(debug_exception_type exception,
533 		char *buffer, int32 bufferSize);
534 
535 
536 #ifdef __cplusplus
537 }	// extern "C"
538 #endif
539 
540 #endif	// _DEBUGGER_H
541