xref: /kvm-unit-tests/lib/x86/stack.c (revision 804e551c29cc9167bba9d9db76a2f9bf5c8a44af)
1 #include <libcflat.h>
2 #include <stack.h>
3 
4 int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)
5 {
6 	static int walking;
7 	int depth = 0;
8 	const unsigned long *bp = (unsigned long *) frame;
9 
10 	if (walking) {
11 		printf("RECURSIVE STACK WALK!!!\n");
12 		return 0;
13 	}
14 	walking = 1;
15 
16 	for (depth = 0; bp && depth < max_depth; depth++) {
17 		return_addrs[depth] = (void *) bp[1];
18 		if (return_addrs[depth] == 0)
19 			break;
20 		bp = (unsigned long *) bp[0];
21 	}
22 
23 	walking = 0;
24 	return depth;
25 }
26 
27 int backtrace(const void **return_addrs, int max_depth)
28 {
29 	return backtrace_frame(__builtin_frame_address(0), return_addrs,
30 			       max_depth);
31 }
32