xref: /freebsd/contrib/netbsd-tests/lib/libexecinfo/t_backtrace.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*	$NetBSD: t_backtrace.c,v 1.17 2020/09/09 20:04:10 christos 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.17 2020/09/09 20:04:10 christos Exp $");
33 
34 #include <atf-c.h>
35 #include <string.h>
36 #include <stdio.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 void myfunc3(size_t ncalls);
46 void myfunc2(size_t ncalls);
47 void myfunc1(size_t origcalls, volatile size_t ncalls);
48 void myfunc(size_t ncalls);
49 
50 static volatile int prevent_inline;
51 
52 void
53 myfunc3(size_t ncalls)
54 {
55 	static const struct {
56 		const char *name;
57 		bool is_optional;
58 	} frames[] = {
59 	    { "myfunc", false },
60 	    { "atfu_backtrace_fmt_basic_body", false },
61 	    { "atf_tc_run", false },
62 	    { "atf_tp_run", true },
63 	    { "run_tc", true },
64 	    { "controlled_main", true },
65 	    { "atf_tp_main", false },
66 	    { "main", true },
67 	    { "___start", true },
68 	};
69 	size_t j, nptrs, min_frames, max_frames;
70 	void *buffer[ncalls + 10];
71 	char **strings;
72 
73 	min_frames = 0;
74 	max_frames = 0;
75 	for (j = 0; j < __arraycount(frames); ++j) {
76 		if (!frames[j].is_optional)
77 			++min_frames;
78 		++max_frames;
79 	}
80 	nptrs = backtrace(buffer, __arraycount(buffer));
81 	ATF_REQUIRE(nptrs != (size_t)-1);
82 	strings = backtrace_symbols_fmt(buffer, nptrs, "%n");
83 
84 	ATF_CHECK(strings != NULL);
85 
86 	printf("got nptrs=%zu ncalls=%zu (min_frames: %zu, max_frames: %zu)\n",
87 	    nptrs, ncalls, min_frames, max_frames);
88 	printf("backtrace is:\n");
89 	for (j = 0; j < nptrs; j++) {
90 		printf("#%zu: %s\n", j, strings[j]);
91 	}
92 
93 	ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames);
94 	ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames);
95 	ATF_CHECK_STREQ(strings[0], "myfunc3");
96 	ATF_CHECK_STREQ(strings[1], "myfunc2");
97 
98 	for (j = 2; j < ncalls + 2; j++)
99 		ATF_CHECK_STREQ(strings[j], "myfunc1");
100 
101 	for (size_t i = 0; j < nptrs; i++, j++) {
102 		if (frames[i].is_optional &&
103 		    strcmp(strings[j], frames[i].name)) {
104 			--i;
105 			continue;
106 		}
107 		ATF_CHECK_STREQ(strings[j], frames[i].name);
108 	}
109 
110 	free(strings);
111 
112 	if (prevent_inline)
113 		vfork();
114 }
115 
116 void
117 myfunc2(size_t ncalls)
118 {
119 	myfunc3(ncalls);
120 
121 	if (prevent_inline)
122 		vfork();
123 }
124 
125 void
126 myfunc1(size_t origcalls, volatile size_t ncalls)
127 {
128 	if (ncalls > 1)
129 		myfunc1(origcalls, ncalls - 1);
130 	else
131 		myfunc2(origcalls);
132 
133 	if (prevent_inline)
134 		vfork();
135 }
136 
137 void
138 myfunc(size_t ncalls)
139 {
140 	myfunc1(ncalls, ncalls);
141 
142 	if (prevent_inline)
143 		vfork();
144 }
145 
146 ATF_TC(backtrace_fmt_basic);
147 ATF_TC_HEAD(backtrace_fmt_basic, tc)
148 {
149 	atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)");
150 }
151 
152 ATF_TC_BODY(backtrace_fmt_basic, tc)
153 {
154 	myfunc(12);
155 
156 	if (prevent_inline)
157 		vfork();
158 }
159 
160 ATF_TP_ADD_TCS(tp)
161 {
162 
163 	ATF_TP_ADD_TC(tp, backtrace_fmt_basic);
164 
165 	return atf_no_error();
166 }
167