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