1254661Semaste/*	$NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $	*/
2254661Semaste
3254661Semaste/*-
4254661Semaste * Copyright (c) 2012 The NetBSD Foundation, Inc.
5254661Semaste * All rights reserved.
6254661Semaste *
7254661Semaste * This code is derived from software contributed to The NetBSD Foundation
8254661Semaste * by Christos Zoulas.
9254661Semaste *
10254661Semaste * Redistribution and use in source and binary forms, with or without
11254661Semaste * modification, are permitted provided that the following conditions
12254661Semaste * are met:
13254661Semaste * 1. Redistributions of source code must retain the above copyright
14254661Semaste *    notice, this list of conditions and the following disclaimer.
15254661Semaste * 2. Redistributions in binary form must reproduce the above copyright
16254661Semaste *    notice, this list of conditions and the following disclaimer in the
17254661Semaste *    documentation and/or other materials provided with the distribution.
18254661Semaste *
19254661Semaste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20254661Semaste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21254661Semaste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22254661Semaste * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23254661Semaste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24254661Semaste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25254661Semaste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26254661Semaste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27254661Semaste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28254661Semaste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29254661Semaste * POSSIBILITY OF SUCH DAMAGE.
30254661Semaste */
31254661Semaste#include <sys/cdefs.h>
32254661Semaste__RCSID("$NetBSD: builtin.c,v 1.1 2012/05/26 22:02:29 christos Exp $");
33254661Semaste
34254661Semaste#include <sys/param.h>
35254661Semaste#include <sys/types.h>
36254661Semaste
37254661Semaste#include "execinfo.h"
38254661Semaste
39254661Semaste#ifdef __MACHINE_STACK_GROWS_UP
40254661Semaste#define BELOW >
41254661Semaste#else
42254661Semaste#define BELOW <
43254661Semaste#endif
44254661Semaste
45254661Semaste#ifdef __lint__
46254661Semaste#define __builtin_frame_address(a)	((void *)a)
47254661Semaste#endif
48254661Semaste
49254661Semastestruct frameinfo {
50254661Semaste	struct frameinfo *next;
51254661Semaste	void *return_address;
52254661Semaste};
53254661Semaste
54254661Semastesize_t
55254661Semastebacktrace(void **trace, size_t len)
56254661Semaste{
57254661Semaste	const struct frameinfo *frame = __builtin_frame_address(0);
58254661Semaste	void *stack = &stack;
59254661Semaste
60254661Semaste	for (size_t i = 0; i < len; i++) {
61254661Semaste		if ((const void *)frame BELOW stack)
62254661Semaste			return i;
63254661Semaste		trace[i] = frame->return_address;
64254661Semaste		frame = frame->next;
65254661Semaste	}
66254661Semaste
67254661Semaste	return len;
68254661Semaste}
69