xref: /freebsd/contrib/libexecinfo/unwind.c (revision 895a7e604db4ddf2b5d58833a82c2b4956585d02)
1499fe48dSConrad Meyer /*	$NetBSD: unwind.c,v 1.3 2019/01/30 22:46:49 mrg Exp $	*/
2287472b3SEd Maste 
3287472b3SEd Maste /*-
4287472b3SEd Maste  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5287472b3SEd Maste  * All rights reserved.
6287472b3SEd Maste  *
7287472b3SEd Maste  * This code is derived from software contributed to The NetBSD Foundation
8287472b3SEd Maste  * by Christos Zoulas.
9287472b3SEd Maste  *
10287472b3SEd Maste  * Redistribution and use in source and binary forms, with or without
11287472b3SEd Maste  * modification, are permitted provided that the following conditions
12287472b3SEd Maste  * are met:
13287472b3SEd Maste  * 1. Redistributions of source code must retain the above copyright
14287472b3SEd Maste  *    notice, this list of conditions and the following disclaimer.
15287472b3SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
16287472b3SEd Maste  *    notice, this list of conditions and the following disclaimer in the
17287472b3SEd Maste  *    documentation and/or other materials provided with the distribution.
18287472b3SEd Maste  *
19287472b3SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20287472b3SEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21287472b3SEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22287472b3SEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23287472b3SEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24287472b3SEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25287472b3SEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26287472b3SEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27287472b3SEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28287472b3SEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29287472b3SEd Maste  * POSSIBILITY OF SUCH DAMAGE.
30287472b3SEd Maste  */
31287472b3SEd Maste #include <sys/cdefs.h>
32287472b3SEd Maste #include <sys/types.h>
33287472b3SEd Maste #include <stdio.h>
34287472b3SEd Maste 
35287472b3SEd Maste #include "unwind.h"
36287472b3SEd Maste #include "execinfo.h"
37287472b3SEd Maste 
38287472b3SEd Maste struct tracer_context {
39287472b3SEd Maste 	void **arr;
40287472b3SEd Maste 	size_t len;
41287472b3SEd Maste 	size_t n;
42287472b3SEd Maste };
43287472b3SEd Maste 
44287472b3SEd Maste static _Unwind_Reason_Code
tracer(struct _Unwind_Context * ctx,void * arg)45287472b3SEd Maste tracer(struct _Unwind_Context *ctx, void *arg)
46287472b3SEd Maste {
47287472b3SEd Maste 	struct tracer_context *t = arg;
48287472b3SEd Maste 	if (t->n == (size_t)~0) {
49287472b3SEd Maste 		/* Skip backtrace frame */
50287472b3SEd Maste 		t->n = 0;
51287472b3SEd Maste 		return 0;
52287472b3SEd Maste 	}
53287472b3SEd Maste 	if (t->n < t->len)
54499fe48dSConrad Meyer 		t->arr[t->n++] = (void *)_Unwind_GetIP(ctx);
55499fe48dSConrad Meyer 	else
56499fe48dSConrad Meyer 		return _URC_END_OF_STACK;
57499fe48dSConrad Meyer 	return _URC_NO_REASON;
58287472b3SEd Maste }
59287472b3SEd Maste 
60287472b3SEd Maste size_t
backtrace(void ** arr,size_t len)61287472b3SEd Maste backtrace(void **arr, size_t len)
62287472b3SEd Maste {
63287472b3SEd Maste 	struct tracer_context ctx;
64287472b3SEd Maste 
65287472b3SEd Maste 	ctx.arr = arr;
66287472b3SEd Maste 	ctx.len = len;
67287472b3SEd Maste 	ctx.n = (size_t)~0;
68287472b3SEd Maste 
69287472b3SEd Maste 	_Unwind_Backtrace(tracer, &ctx);
70*895a7e60SPoul-Henning Kamp 	if (ctx.n == (size_t)~0)
71*895a7e60SPoul-Henning Kamp 		ctx.n = 0;
72*895a7e60SPoul-Henning Kamp 	else if (ctx.n > 0)
73287472b3SEd Maste 		ctx.arr[--ctx.n] = NULL;	/* Skip frame below __start */
74287472b3SEd Maste 
75287472b3SEd Maste 	return ctx.n;
76287472b3SEd Maste }
77