xref: /dflybsd-src/lib/libexecinfo/unwind.c (revision 999f82af142ace93d4ab5a849504a5d3b6892d2d)
1*999f82afSJohn Marino /*	$NetBSD: unwind.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/types.h>
33*999f82afSJohn Marino #include <stdio.h>
34*999f82afSJohn Marino 
35*999f82afSJohn Marino #include "unwind.h"
36*999f82afSJohn Marino #include "execinfo.h"
37*999f82afSJohn Marino 
38*999f82afSJohn Marino struct tracer_context {
39*999f82afSJohn Marino 	void **arr;
40*999f82afSJohn Marino 	size_t len;
41*999f82afSJohn Marino 	size_t n;
42*999f82afSJohn Marino };
43*999f82afSJohn Marino 
44*999f82afSJohn Marino static _Unwind_Reason_Code
tracer(struct _Unwind_Context * ctx,void * arg)45*999f82afSJohn Marino tracer(struct _Unwind_Context *ctx, void *arg)
46*999f82afSJohn Marino {
47*999f82afSJohn Marino 	struct tracer_context *t = arg;
48*999f82afSJohn Marino 	if (t->n == (size_t)~0) {
49*999f82afSJohn Marino 		/* Skip backtrace frame */
50*999f82afSJohn Marino 		t->n = 0;
51*999f82afSJohn Marino 		return 0;
52*999f82afSJohn Marino 	}
53*999f82afSJohn Marino 	if (t->n < t->len)
54*999f82afSJohn Marino 		t->arr[t->n++] = _Unwind_GetIP(ctx);
55*999f82afSJohn Marino 	return 0;
56*999f82afSJohn Marino }
57*999f82afSJohn Marino 
58*999f82afSJohn Marino size_t
backtrace(void ** arr,size_t len)59*999f82afSJohn Marino backtrace(void **arr, size_t len)
60*999f82afSJohn Marino {
61*999f82afSJohn Marino 	struct tracer_context ctx;
62*999f82afSJohn Marino 
63*999f82afSJohn Marino 	ctx.arr = arr;
64*999f82afSJohn Marino 	ctx.len = len;
65*999f82afSJohn Marino 	ctx.n = (size_t)~0;
66*999f82afSJohn Marino 
67*999f82afSJohn Marino 	_Unwind_Backtrace(tracer, &ctx);
68*999f82afSJohn Marino 	if (ctx.n != (size_t)~0 && ctx.n > 0)
69*999f82afSJohn Marino 		ctx.arr[--ctx.n] = NULL;	/* Skip frame below __start */
70*999f82afSJohn Marino 
71*999f82afSJohn Marino 	return ctx.n;
72*999f82afSJohn Marino }
73