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