xref: /minix3/lib/libexecinfo/unwind.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: unwind.c,v 1.2 2014/03/24 21:26:01 joerg Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (c) 2012 The NetBSD Foundation, Inc.
584d9c625SLionel Sambuc  * All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
884d9c625SLionel Sambuc  * by Christos Zoulas.
984d9c625SLionel Sambuc  *
1084d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1184d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1284d9c625SLionel Sambuc  * are met:
1384d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1484d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1584d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1684d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1784d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1884d9c625SLionel Sambuc  *
1984d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2084d9c625SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2184d9c625SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2284d9c625SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2384d9c625SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2484d9c625SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2584d9c625SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2684d9c625SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2784d9c625SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2884d9c625SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2984d9c625SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3084d9c625SLionel Sambuc  */
3184d9c625SLionel Sambuc #include <sys/cdefs.h>
3284d9c625SLionel Sambuc #include <sys/types.h>
3384d9c625SLionel Sambuc #include <stdio.h>
3484d9c625SLionel Sambuc 
3584d9c625SLionel Sambuc #include "unwind.h"
3684d9c625SLionel Sambuc #include "execinfo.h"
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc struct tracer_context {
3984d9c625SLionel Sambuc 	void **arr;
4084d9c625SLionel Sambuc 	size_t len;
4184d9c625SLionel Sambuc 	size_t n;
4284d9c625SLionel Sambuc };
4384d9c625SLionel Sambuc 
4484d9c625SLionel Sambuc static _Unwind_Reason_Code
tracer(struct _Unwind_Context * ctx,void * arg)4584d9c625SLionel Sambuc tracer(struct _Unwind_Context *ctx, void *arg)
4684d9c625SLionel Sambuc {
4784d9c625SLionel Sambuc 	struct tracer_context *t = arg;
4884d9c625SLionel Sambuc 	if (t->n == (size_t)~0) {
4984d9c625SLionel Sambuc 		/* Skip backtrace frame */
5084d9c625SLionel Sambuc 		t->n = 0;
5184d9c625SLionel Sambuc 		return 0;
5284d9c625SLionel Sambuc 	}
5384d9c625SLionel Sambuc 	if (t->n < t->len)
54*0a6a1f1dSLionel Sambuc 		t->arr[t->n++] = (void *)_Unwind_GetIP(ctx);
5584d9c625SLionel Sambuc 	return 0;
5684d9c625SLionel Sambuc }
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc size_t
backtrace(void ** arr,size_t len)5984d9c625SLionel Sambuc backtrace(void **arr, size_t len)
6084d9c625SLionel Sambuc {
6184d9c625SLionel Sambuc 	struct tracer_context ctx;
6284d9c625SLionel Sambuc 
6384d9c625SLionel Sambuc 	ctx.arr = arr;
6484d9c625SLionel Sambuc 	ctx.len = len;
6584d9c625SLionel Sambuc 	ctx.n = (size_t)~0;
6684d9c625SLionel Sambuc 
6784d9c625SLionel Sambuc 	_Unwind_Backtrace(tracer, &ctx);
6884d9c625SLionel Sambuc 	if (ctx.n != (size_t)~0 && ctx.n > 0)
6984d9c625SLionel Sambuc 		ctx.arr[--ctx.n] = NULL;	/* Skip frame below __start */
7084d9c625SLionel Sambuc 
7184d9c625SLionel Sambuc 	return ctx.n;
7284d9c625SLionel Sambuc }
73