xref: /kvm-unit-tests/lib/arm/stack.c (revision 6444ae208ce0085d0f5c5ffb15909ca3bbd49c84)
1 /*
2  * backtrace support (this is a modified lib/x86/stack.c)
3  *
4  * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 #include <libcflat.h>
9 #include <stack.h>
10 
arch_backtrace_frame(const void * frame,const void ** return_addrs,int max_depth,bool current_frame)11 int arch_backtrace_frame(const void *frame, const void **return_addrs,
12 			 int max_depth, bool current_frame)
13 {
14 	static int walking;
15 	int depth;
16 	const unsigned long *fp = (unsigned long *)frame;
17 
18 	if (current_frame)
19 		fp = __builtin_frame_address(0);
20 
21 	if (walking) {
22 		printf("RECURSIVE STACK WALK!!!\n");
23 		return 0;
24 	}
25 	walking = 1;
26 
27 	for (depth = 0; depth < max_depth; depth++) {
28 		if (!fp)
29 			break;
30 		return_addrs[depth] = (void *)fp[0];
31 		if (return_addrs[depth] == 0)
32 			break;
33 		fp = (unsigned long *)fp[-1];
34 	}
35 
36 	walking = 0;
37 	return depth;
38 }
39