t_backtrace.c revision 1.4 1 /* $NetBSD: t_backtrace.c,v 1.4 2012/05/30 15:11:58 jruoho 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.4 2012/05/30 15:11:58 jruoho 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
40 #ifndef __arraycount
41 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
42 #endif
43
44 static void __attribute__((__noinline__))
45 myfunc3(size_t ncalls)
46 {
47 static const char *top[] = { "myfunc", "atfu_backtrace_fmt_basic_body",
48 "atf_tc_run", "atf_tp_main", "main", "___start" };
49 size_t j, nptrs;
50 void *buffer[ncalls + 10];
51 char **strings;
52
53 nptrs = backtrace(buffer, __arraycount(buffer));
54 ATF_REQUIRE_EQ(nptrs, ncalls + 8);
55
56 strings = backtrace_symbols_fmt(buffer, nptrs, "%n");
57
58 ATF_CHECK(strings != NULL);
59 ATF_CHECK_STREQ(strings[0], "myfunc3");
60 ATF_CHECK_STREQ(strings[1], "myfunc2");
61
62 for (j = 2; j < ncalls + 2; j++)
63 ATF_CHECK_STREQ(strings[j], "myfunc1");
64
65 for (size_t i = 0; j < nptrs; i++, j++)
66 ATF_CHECK_STREQ(strings[j], top[i]);
67
68 free(strings);
69 }
70
71 static void __attribute__((__noinline__))
72 myfunc2(size_t ncalls)
73 {
74 myfunc3(ncalls);
75 }
76
77 static void __attribute__((__noinline__))
78 myfunc1(size_t origcalls, size_t ncalls)
79 {
80 if (ncalls > 1)
81 myfunc1(origcalls, ncalls - 1);
82 else
83 myfunc2(origcalls);
84 }
85
86 static void __attribute__((__noinline__))
87 myfunc(size_t ncalls)
88 {
89 myfunc1(ncalls, ncalls);
90 }
91
92 ATF_TC(backtrace_fmt_basic);
93 ATF_TC_HEAD(backtrace_fmt_basic, tc)
94 {
95 atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)");
96 }
97
98 ATF_TC_BODY(backtrace_fmt_basic, tc)
99 {
100 const char *arch = atf_config_get("atf_arch");
101
102 if (strcmp(arch, "amd64") != 0)
103 atf_tc_skip("PR toolchain/46490: libexecinfo only"
104 " works on amd64 currently");
105
106 myfunc(12);
107 }
108
109 ATF_TP_ADD_TCS(tp)
110 {
111
112 ATF_TP_ADD_TC(tp, backtrace_fmt_basic);
113
114 return atf_no_error();
115 }
116