xref: /kvm-unit-tests/lib/arm/stack.c (revision bd744d46591007f4dbb2ec1b9e05a55a145c8684)
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 
11 int backtrace_frame(const void *frame, const void **return_addrs,
12 		    int max_depth)
13 {
14 	static int walking;
15 	int depth;
16 	const unsigned long *fp = (unsigned long *)frame;
17 
18 	if (walking) {
19 		printf("RECURSIVE STACK WALK!!!\n");
20 		return 0;
21 	}
22 	walking = 1;
23 
24 	for (depth = 0; depth < max_depth; depth++) {
25 		if (!fp)
26 			break;
27 		return_addrs[depth] = (void *)fp[0];
28 		if (return_addrs[depth] == 0)
29 			break;
30 		fp = (unsigned long *)fp[-1];
31 	}
32 
33 	walking = 0;
34 	return depth;
35 }
36 
37 int backtrace(const void **return_addrs, int max_depth)
38 {
39 	return backtrace_frame(__builtin_frame_address(0),
40 			       return_addrs, max_depth);
41 }
42