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