| @@ -1,148 +1,156 @@ | | | @@ -1,148 +1,156 @@ |
1 | /* $NetBSD: t_backtrace.c,v 1.10 2013/08/16 11:57:15 martin Exp $ */ | | 1 | /* $NetBSD: t_backtrace.c,v 1.11 2014/01/11 19:13:41 martin Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2012 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2012 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Christos Zoulas. | | 8 | * by Christos Zoulas. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 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 | | 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 | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 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 | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 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 | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | #include <sys/cdefs.h> | | 31 | #include <sys/cdefs.h> |
32 | __RCSID("$NetBSD: t_backtrace.c,v 1.10 2013/08/16 11:57:15 martin Exp $"); | | 32 | __RCSID("$NetBSD: t_backtrace.c,v 1.11 2014/01/11 19:13:41 martin Exp $"); |
33 | | | 33 | |
34 | #include <atf-c.h> | | 34 | #include <atf-c.h> |
35 | #include <atf-c/config.h> | | 35 | #include <atf-c/config.h> |
36 | #include <string.h> | | 36 | #include <string.h> |
| | | 37 | #include <stdio.h> |
37 | #include <stdlib.h> | | 38 | #include <stdlib.h> |
38 | #include <execinfo.h> | | 39 | #include <execinfo.h> |
39 | #include <unistd.h> | | 40 | #include <unistd.h> |
40 | | | 41 | |
41 | #ifndef __arraycount | | 42 | #ifndef __arraycount |
42 | #define __arraycount(a) (sizeof(a) / sizeof(a[0])) | | 43 | #define __arraycount(a) (sizeof(a) / sizeof(a[0])) |
43 | #endif | | 44 | #endif |
44 | | | 45 | |
45 | void myfunc3(size_t ncalls); | | 46 | void myfunc3(size_t ncalls); |
46 | void myfunc2(size_t ncalls); | | 47 | void myfunc2(size_t ncalls); |
47 | void myfunc1(size_t origcalls, volatile size_t ncalls); | | 48 | void myfunc1(size_t origcalls, volatile size_t ncalls); |
48 | void myfunc(size_t ncalls); | | 49 | void myfunc(size_t ncalls); |
49 | | | 50 | |
50 | volatile int prevent_inline; | | 51 | volatile int prevent_inline; |
51 | | | 52 | |
52 | void | | 53 | void |
53 | myfunc3(size_t ncalls) | | 54 | myfunc3(size_t ncalls) |
54 | { | | 55 | { |
55 | static const char *top[] = { "myfunc", "atfu_backtrace_fmt_basic_body", | | 56 | static const char *top[] = { "myfunc", "atfu_backtrace_fmt_basic_body", |
56 | "atf_tc_run", "atf_tp_run", "atf_tp_main", "main", "___start" }; | | 57 | "atf_tc_run", "atf_tp_run", "atf_tp_main", "main", "___start" }; |
57 | static bool optional_frame[] = { false, false, false, true, false, | | 58 | static bool optional_frame[] = { false, false, false, true, false, |
58 | false, true }; | | 59 | false, true }; |
59 | size_t j, nptrs, min_frames, max_frames; | | 60 | size_t j, nptrs, min_frames, max_frames; |
60 | void *buffer[ncalls + 10]; | | 61 | void *buffer[ncalls + 10]; |
61 | char **strings; | | 62 | char **strings; |
62 | __CTASSERT(__arraycount(top) == __arraycount(optional_frame)); | | 63 | __CTASSERT(__arraycount(top) == __arraycount(optional_frame)); |
63 | | | 64 | |
64 | min_frames = 0; | | 65 | min_frames = 0; |
65 | max_frames = 0; | | 66 | max_frames = 0; |
66 | for (j = 0; j < __arraycount(optional_frame); ++j) { | | 67 | for (j = 0; j < __arraycount(optional_frame); ++j) { |
67 | if (!optional_frame[j]) | | 68 | if (!optional_frame[j]) |
68 | ++min_frames; | | 69 | ++min_frames; |
69 | ++max_frames; | | 70 | ++max_frames; |
70 | } | | 71 | } |
71 | nptrs = backtrace(buffer, __arraycount(buffer)); | | 72 | nptrs = backtrace(buffer, __arraycount(buffer)); |
72 | ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames); | | | |
73 | ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames); | | | |
74 | | | | |
75 | strings = backtrace_symbols_fmt(buffer, nptrs, "%n"); | | 73 | strings = backtrace_symbols_fmt(buffer, nptrs, "%n"); |
76 | | | 74 | |
77 | ATF_CHECK(strings != NULL); | | 75 | ATF_CHECK(strings != NULL); |
| | | 76 | |
| | | 77 | printf("got nptrs=%zu ncalls=%zu (min_frames: %zu, max_frames: %zu)\n", |
| | | 78 | nptrs, ncalls, min_frames, max_frames); |
| | | 79 | printf("backtrace is:\n"); |
| | | 80 | for (j = 0; j < nptrs; j++) { |
| | | 81 | printf("#%zu: %s\n", j, strings[j]); |
| | | 82 | } |
| | | 83 | |
| | | 84 | ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames); |
| | | 85 | ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames); |
78 | ATF_CHECK_STREQ(strings[0], "myfunc3"); | | 86 | ATF_CHECK_STREQ(strings[0], "myfunc3"); |
79 | ATF_CHECK_STREQ(strings[1], "myfunc2"); | | 87 | ATF_CHECK_STREQ(strings[1], "myfunc2"); |
80 | | | 88 | |
81 | for (j = 2; j < ncalls + 2; j++) | | 89 | for (j = 2; j < ncalls + 2; j++) |
82 | ATF_CHECK_STREQ(strings[j], "myfunc1"); | | 90 | ATF_CHECK_STREQ(strings[j], "myfunc1"); |
83 | | | 91 | |
84 | for (size_t i = 0; j < nptrs; i++, j++) { | | 92 | for (size_t i = 0; j < nptrs; i++, j++) { |
85 | if (optional_frame[i] && strcmp(strings[j], top[i])) { | | 93 | if (optional_frame[i] && strcmp(strings[j], top[i])) { |
86 | --i; | | 94 | --i; |
87 | continue; | | 95 | continue; |
88 | } | | 96 | } |
89 | ATF_CHECK_STREQ(strings[j], top[i]); | | 97 | ATF_CHECK_STREQ(strings[j], top[i]); |
90 | } | | 98 | } |
91 | | | 99 | |
92 | free(strings); | | 100 | free(strings); |
93 | | | 101 | |
94 | if (prevent_inline) | | 102 | if (prevent_inline) |
95 | vfork(); | | 103 | vfork(); |
96 | } | | 104 | } |
97 | | | 105 | |
98 | void | | 106 | void |
99 | myfunc2(size_t ncalls) | | 107 | myfunc2(size_t ncalls) |
100 | { | | 108 | { |
101 | myfunc3(ncalls); | | 109 | myfunc3(ncalls); |
102 | | | 110 | |
103 | if (prevent_inline) | | 111 | if (prevent_inline) |
104 | vfork(); | | 112 | vfork(); |
105 | } | | 113 | } |
106 | | | 114 | |
107 | void | | 115 | void |
108 | myfunc1(size_t origcalls, volatile size_t ncalls) | | 116 | myfunc1(size_t origcalls, volatile size_t ncalls) |
109 | { | | 117 | { |
110 | if (ncalls > 1) | | 118 | if (ncalls > 1) |
111 | myfunc1(origcalls, ncalls - 1); | | 119 | myfunc1(origcalls, ncalls - 1); |
112 | else | | 120 | else |
113 | myfunc2(origcalls); | | 121 | myfunc2(origcalls); |
114 | | | 122 | |
115 | if (prevent_inline) | | 123 | if (prevent_inline) |
116 | vfork(); | | 124 | vfork(); |
117 | } | | 125 | } |
118 | | | 126 | |
119 | void | | 127 | void |
120 | myfunc(size_t ncalls) | | 128 | myfunc(size_t ncalls) |
121 | { | | 129 | { |
122 | myfunc1(ncalls, ncalls); | | 130 | myfunc1(ncalls, ncalls); |
123 | | | 131 | |
124 | if (prevent_inline) | | 132 | if (prevent_inline) |
125 | vfork(); | | 133 | vfork(); |
126 | } | | 134 | } |
127 | | | 135 | |
128 | ATF_TC(backtrace_fmt_basic); | | 136 | ATF_TC(backtrace_fmt_basic); |
129 | ATF_TC_HEAD(backtrace_fmt_basic, tc) | | 137 | ATF_TC_HEAD(backtrace_fmt_basic, tc) |
130 | { | | 138 | { |
131 | atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)"); | | 139 | atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)"); |
132 | } | | 140 | } |
133 | | | 141 | |
134 | ATF_TC_BODY(backtrace_fmt_basic, tc) | | 142 | ATF_TC_BODY(backtrace_fmt_basic, tc) |
135 | { | | 143 | { |
136 | myfunc(12); | | 144 | myfunc(12); |
137 | | | 145 | |
138 | if (prevent_inline) | | 146 | if (prevent_inline) |
139 | vfork(); | | 147 | vfork(); |
140 | } | | 148 | } |
141 | | | 149 | |
142 | ATF_TP_ADD_TCS(tp) | | 150 | ATF_TP_ADD_TCS(tp) |
143 | { | | 151 | { |
144 | | | 152 | |
145 | ATF_TP_ADD_TC(tp, backtrace_fmt_basic); | | 153 | ATF_TP_ADD_TC(tp, backtrace_fmt_basic); |
146 | | | 154 | |
147 | return atf_no_error(); | | 155 | return atf_no_error(); |
148 | } | | 156 | } |