xref: /minix3/tests/lib/libexecinfo/t_backtrace.c (revision 6e5a113837816c382dd4f2a25924f16fbbb1c493)
1 /*	$NetBSD: t_backtrace.c,v 1.8 2013/07/05 09:55:39 joerg 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 __RCSID("$NetBSD: t_backtrace.c,v 1.8 2013/07/05 09:55:39 joerg Exp $");
33 
34 #include <atf-c.h>
35 #include <atf-c/config.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <execinfo.h>
39 #include <unistd.h>
40 
41 #ifndef __arraycount
42 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
43 #endif
44 
45 volatile int prevent_inline;
46 
47 static void
48 myfunc3(size_t ncalls)
49 {
50 	static const char *top[] = { "myfunc", "atfu_backtrace_fmt_basic_body",
51 	    "atf_tc_run", "atf_tp_run", "atf_tp_main", "main", "___start" };
52 	static bool optional_frame[] = { false, false, false, true, false,
53 	    false, true };
54 	size_t j, nptrs, min_frames, max_frames;
55 	void *buffer[ncalls + 10];
56 	char **strings;
57 	__CTASSERT(__arraycount(top) == __arraycount(optional_frame));
58 
59 	min_frames = 0;
60 	max_frames = 0;
61 	for (j = 0; j < __arraycount(optional_frame); ++j) {
62 		if (!optional_frame[j])
63 			++min_frames;
64 		++max_frames;
65 	}
66 	nptrs = backtrace(buffer, __arraycount(buffer));
67 	ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames);
68 	ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames);
69 
70 	strings = backtrace_symbols_fmt(buffer, nptrs, "%n");
71 
72 	ATF_CHECK(strings != NULL);
73 	ATF_CHECK_STREQ(strings[0], "myfunc3");
74 	ATF_CHECK_STREQ(strings[1], "myfunc2");
75 
76 	for (j = 2; j < ncalls + 2; j++)
77 		ATF_CHECK_STREQ(strings[j], "myfunc1");
78 
79 	for (size_t i = 0; j < nptrs; i++, j++) {
80 		if (optional_frame[i] && strcmp(strings[j], top[i])) {
81 			--i;
82 			continue;
83 		}
84 		ATF_CHECK_STREQ(strings[j], top[i]);
85 	}
86 
87 	free(strings);
88 
89 	if (prevent_inline)
90 		vfork();
91 }
92 
93 static void
94 myfunc2(size_t ncalls)
95 {
96 	myfunc3(ncalls);
97 
98 	if (prevent_inline)
99 		vfork();
100 }
101 
102 static void
103 myfunc1(size_t origcalls, volatile size_t ncalls)
104 {
105 	if (ncalls > 1)
106 		myfunc1(origcalls, ncalls - 1);
107 	else
108 		myfunc2(origcalls);
109 
110 	if (prevent_inline)
111 		vfork();
112 }
113 
114 static void
115 myfunc(size_t ncalls)
116 {
117 	myfunc1(ncalls, ncalls);
118 
119 	if (prevent_inline)
120 		vfork();
121 }
122 
123 ATF_TC(backtrace_fmt_basic);
124 ATF_TC_HEAD(backtrace_fmt_basic, tc)
125 {
126         atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)");
127 }
128 
129 ATF_TC_BODY(backtrace_fmt_basic, tc)
130 {
131 	const char *arch = atf_config_get("atf_arch");
132 
133         if (strcmp(arch, "x86_64") != 0)
134         	atf_tc_skip("PR toolchain/46490: libexecinfo only"
135 		    " works on amd64 currently");
136 
137 	myfunc(12);
138 }
139 
140 ATF_TP_ADD_TCS(tp)
141 {
142 
143 	ATF_TP_ADD_TC(tp, backtrace_fmt_basic);
144 
145 	return atf_no_error();
146 }
147