1/*	$NetBSD: unwind.c,v 1.3 2019/01/30 22:46:49 mrg Exp $	*/
2
3/*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31#include <sys/cdefs.h>
32#include <sys/types.h>
33#include <stdio.h>
34
35#include "execinfo.h"
36
37struct tracer_context {
38	void **arr;
39	size_t len;
40	size_t n;
41};
42
43typedef enum {
44  _URC_NO_REASON = 0,
45#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
46    !defined(__ARM_DWARF_EH__) && !defined(__SEH__)
47  _URC_OK = 0, /* used by ARM EHABI */
48#endif
49  _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
50
51  _URC_FATAL_PHASE2_ERROR = 2,
52  _URC_FATAL_PHASE1_ERROR = 3,
53  _URC_NORMAL_STOP = 4,
54
55  _URC_END_OF_STACK = 5,
56  _URC_HANDLER_FOUND = 6,
57  _URC_INSTALL_CONTEXT = 7,
58  _URC_CONTINUE_UNWIND = 8,
59#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
60    !defined(__ARM_DWARF_EH__) && !defined(__SEH__)
61  _URC_FAILURE = 9 /* used by ARM EHABI */
62#endif
63} _Unwind_Reason_Code;
64
65struct _Unwind_Context;
66typedef unsigned int _Unwind_Word __attribute__((__mode__(__unwind_word__)));
67
68_Unwind_Word _Unwind_GetGR(struct _Unwind_Context *, int);
69_Unwind_Word _Unwind_GetIP(struct _Unwind_Context *);
70typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
71                                                void *);
72extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *);
73static _Unwind_Reason_Code
74tracer(struct _Unwind_Context *ctx, void *arg)
75{
76	struct tracer_context *t = arg;
77	if (t->n == (size_t)~0) {
78		/* Skip backtrace frame */
79		t->n = 0;
80		return 0;
81	}
82	if (t->n < t->len)
83		t->arr[t->n++] = (void *)_Unwind_GetIP(ctx);
84	else
85		return _URC_END_OF_STACK;
86	return _URC_NO_REASON;
87}
88
89size_t
90backtrace(void **arr, size_t len)
91{
92	struct tracer_context ctx;
93
94	ctx.arr = arr;
95	ctx.len = len;
96	ctx.n = (size_t)~0;
97
98	_Unwind_Backtrace(tracer, &ctx);
99	if (ctx.n == (size_t)~0)
100		ctx.n = 0;
101
102	return ctx.n;
103}
104