xref: /netbsd-src/lib/libexecinfo/builtin.c (revision 83d64dcffd8ea47c46bce56ebe00fbcae9c9bd70)
1*83d64dcfSchristos /*	$NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $	*/
2*83d64dcfSchristos 
3*83d64dcfSchristos /*-
4*83d64dcfSchristos  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*83d64dcfSchristos  * All rights reserved.
6*83d64dcfSchristos  *
7*83d64dcfSchristos  * This code is derived from software contributed to The NetBSD Foundation
8*83d64dcfSchristos  * by Christos Zoulas.
9*83d64dcfSchristos  *
10*83d64dcfSchristos  * Redistribution and use in source and binary forms, with or without
11*83d64dcfSchristos  * modification, are permitted provided that the following conditions
12*83d64dcfSchristos  * are met:
13*83d64dcfSchristos  * 1. Redistributions of source code must retain the above copyright
14*83d64dcfSchristos  *    notice, this list of conditions and the following disclaimer.
15*83d64dcfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
16*83d64dcfSchristos  *    notice, this list of conditions and the following disclaimer in the
17*83d64dcfSchristos  *    documentation and/or other materials provided with the distribution.
18*83d64dcfSchristos  *
19*83d64dcfSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*83d64dcfSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*83d64dcfSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*83d64dcfSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*83d64dcfSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*83d64dcfSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*83d64dcfSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*83d64dcfSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*83d64dcfSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*83d64dcfSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*83d64dcfSchristos  * POSSIBILITY OF SUCH DAMAGE.
30*83d64dcfSchristos  */
31*83d64dcfSchristos #include <sys/cdefs.h>
32*83d64dcfSchristos __RCSID("$NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $");
33*83d64dcfSchristos 
34*83d64dcfSchristos #include <sys/param.h>
35*83d64dcfSchristos #include <sys/types.h>
36*83d64dcfSchristos 
37*83d64dcfSchristos #include "execinfo.h"
38*83d64dcfSchristos 
39*83d64dcfSchristos #ifdef __MACHINE_STACK_GROWS_UP
40*83d64dcfSchristos #define BELOW >
41*83d64dcfSchristos #else
42*83d64dcfSchristos #define BELOW <
43*83d64dcfSchristos #endif
44*83d64dcfSchristos 
45*83d64dcfSchristos #ifdef __lint__
46*83d64dcfSchristos #define __builtin_frame_address(a)	((void *)a)
47*83d64dcfSchristos #endif
48*83d64dcfSchristos 
49*83d64dcfSchristos struct frameinfo {
50*83d64dcfSchristos 	struct frameinfo *next;
51*83d64dcfSchristos 	void *return_address;
52*83d64dcfSchristos };
53*83d64dcfSchristos 
54*83d64dcfSchristos size_t
backtrace(void ** trace,size_t len)55*83d64dcfSchristos backtrace(void **trace, size_t len)
56*83d64dcfSchristos {
57*83d64dcfSchristos 	const struct frameinfo *frame = __builtin_frame_address(0);
58*83d64dcfSchristos 	void *stack = &stack;
59*83d64dcfSchristos 
60*83d64dcfSchristos 	for (size_t i = 0; i < len; i++) {
61*83d64dcfSchristos 		if ((const void *)frame BELOW stack)
62*83d64dcfSchristos 			return i;
63*83d64dcfSchristos 		trace[i] = frame->return_address;
64*83d64dcfSchristos 		frame = frame->next;
65*83d64dcfSchristos 	}
66*83d64dcfSchristos 
67*83d64dcfSchristos 	return len;
68*83d64dcfSchristos }
69