xref: /haiku/src/system/libroot/os/arch/generic/generic_stack_trace.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Lotz, mmlr@mlotz.ch
7  */
8 
9 #include <arch_config.h>
10 #include <libroot_private.h>
11 
12 #ifdef STACK_GROWS_DOWNWARDS
13 
14 /*!	Captures a stack trace (the return addresses) of the current thread.
15 	\param returnAddresses The array the return address shall be written to.
16 	\param maxCount The maximum number of return addresses to be captured.
17 	\param skipFrames The number of stack frames that shall be skipped.
18 	\param stackBase The base address of the thread stack.
19 	\param stackEnd The first address past the thread stack.
20 	\return The number of return addresses written to the given array.
21 */
22 int32
23 __arch_get_stack_trace(addr_t* returnAddresses, int32 maxCount,
24 	int32 skipFrames, addr_t stackBase, addr_t stackEnd)
25 {
26 	int32 count = 0;
27 	addr_t basePointer = (addr_t)get_stack_frame();
28 
29 	while (basePointer != 0 && count < maxCount) {
30 		if (basePointer < stackBase || basePointer >= stackEnd)
31 			break;
32 
33 		struct stack_frame {
34 			addr_t	previous;
35 			addr_t	return_address;
36 		};
37 
38 		stack_frame* frame = (stack_frame*)basePointer;
39 
40 		if (skipFrames <= 0)
41 			returnAddresses[count++] = frame->return_address;
42 		else
43 			skipFrames--;
44 
45 		basePointer = frame->previous;
46 	}
47 
48 	return count;
49 }
50 
51 #else
52 
53 #error Implementation needed for upwards growing stacks
54 
55 #endif
56