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 <libroot_private.h>
10
11
12/*!	Captures a stack trace (the return addresses) of the current thread.
13	\param returnAddresses The array the return address shall be written to.
14	\param maxCount The maximum number of return addresses to be captured.
15	\param skipFrames The number of stack frames that shall be skipped.
16	\param stackBase The base address of the thread stack.
17	\param stackEnd The first address past the thread stack.
18	\return The number of return addresses written to the given array.
19*/
20int32
21__arch_get_stack_trace(addr_t* returnAddresses, int32 maxCount,
22	int32 skipFrames, addr_t stackBase, addr_t stackEnd)
23{
24	int32 count = 0;
25	addr_t basePointer = (addr_t)get_stack_frame();
26
27	while (basePointer != 0 && count < maxCount) {
28		if (basePointer < stackBase || basePointer >= stackEnd)
29			break;
30
31		struct stack_frame {
32			addr_t	previous;
33			addr_t	return_address;
34		};
35
36		stack_frame* frame = (stack_frame*)basePointer;
37
38		if (skipFrames <= 0)
39			returnAddresses[count++] = frame->return_address;
40		else
41			skipFrames--;
42
43		basePointer = frame->previous;
44	}
45
46	return count;
47}
48