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