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