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