1*c2bd2f1fSchristos /* $NetBSD: t_backtrace.c,v 1.17 2020/09/09 20:04:10 christos Exp $ */
25212b5c3Schristos
35212b5c3Schristos /*-
45212b5c3Schristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
55212b5c3Schristos * All rights reserved.
65212b5c3Schristos *
75212b5c3Schristos * This code is derived from software contributed to The NetBSD Foundation
85212b5c3Schristos * by Christos Zoulas.
95212b5c3Schristos *
105212b5c3Schristos * Redistribution and use in source and binary forms, with or without
115212b5c3Schristos * modification, are permitted provided that the following conditions
125212b5c3Schristos * are met:
135212b5c3Schristos * 1. Redistributions of source code must retain the above copyright
145212b5c3Schristos * notice, this list of conditions and the following disclaimer.
155212b5c3Schristos * 2. Redistributions in binary form must reproduce the above copyright
165212b5c3Schristos * notice, this list of conditions and the following disclaimer in the
175212b5c3Schristos * documentation and/or other materials provided with the distribution.
185212b5c3Schristos *
195212b5c3Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205212b5c3Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215212b5c3Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225212b5c3Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235212b5c3Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245212b5c3Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255212b5c3Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265212b5c3Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275212b5c3Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285212b5c3Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295212b5c3Schristos * POSSIBILITY OF SUCH DAMAGE.
305212b5c3Schristos */
315212b5c3Schristos #include <sys/cdefs.h>
32*c2bd2f1fSchristos __RCSID("$NetBSD: t_backtrace.c,v 1.17 2020/09/09 20:04:10 christos Exp $");
335212b5c3Schristos
345212b5c3Schristos #include <atf-c.h>
355212b5c3Schristos #include <string.h>
36030abcd2Smartin #include <stdio.h>
375212b5c3Schristos #include <stdlib.h>
385212b5c3Schristos #include <execinfo.h>
398f5b02bdSjoerg #include <unistd.h>
405212b5c3Schristos
415212b5c3Schristos #ifndef __arraycount
425212b5c3Schristos #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
435212b5c3Schristos #endif
445212b5c3Schristos
4534f57994Smartin void myfunc3(size_t ncalls);
4634f57994Smartin void myfunc2(size_t ncalls);
4734f57994Smartin void myfunc1(size_t origcalls, volatile size_t ncalls);
4834f57994Smartin void myfunc(size_t ncalls);
4934f57994Smartin
50*c2bd2f1fSchristos static volatile int prevent_inline;
518f5b02bdSjoerg
5234f57994Smartin void
myfunc3(size_t ncalls)535212b5c3Schristos myfunc3(size_t ncalls)
545212b5c3Schristos {
557af46b7fSjoerg static const struct {
567af46b7fSjoerg const char *name;
577af46b7fSjoerg bool is_optional;
587af46b7fSjoerg } frames[] = {
597af46b7fSjoerg { "myfunc", false },
607af46b7fSjoerg { "atfu_backtrace_fmt_basic_body", false },
617af46b7fSjoerg { "atf_tc_run", false },
627af46b7fSjoerg { "atf_tp_run", true },
637af46b7fSjoerg { "run_tc", true },
647af46b7fSjoerg { "controlled_main", true },
657af46b7fSjoerg { "atf_tp_main", false },
667af46b7fSjoerg { "main", true },
677af46b7fSjoerg { "___start", true },
687af46b7fSjoerg };
6944c2dc6eSjoerg size_t j, nptrs, min_frames, max_frames;
705212b5c3Schristos void *buffer[ncalls + 10];
715212b5c3Schristos char **strings;
725212b5c3Schristos
7344c2dc6eSjoerg min_frames = 0;
7444c2dc6eSjoerg max_frames = 0;
757af46b7fSjoerg for (j = 0; j < __arraycount(frames); ++j) {
767af46b7fSjoerg if (!frames[j].is_optional)
7744c2dc6eSjoerg ++min_frames;
7844c2dc6eSjoerg ++max_frames;
7944c2dc6eSjoerg }
805212b5c3Schristos nptrs = backtrace(buffer, __arraycount(buffer));
81728ced6eSjoerg ATF_REQUIRE(nptrs != (size_t)-1);
825212b5c3Schristos strings = backtrace_symbols_fmt(buffer, nptrs, "%n");
835212b5c3Schristos
845212b5c3Schristos ATF_CHECK(strings != NULL);
85030abcd2Smartin
86030abcd2Smartin printf("got nptrs=%zu ncalls=%zu (min_frames: %zu, max_frames: %zu)\n",
87030abcd2Smartin nptrs, ncalls, min_frames, max_frames);
88030abcd2Smartin printf("backtrace is:\n");
89030abcd2Smartin for (j = 0; j < nptrs; j++) {
90030abcd2Smartin printf("#%zu: %s\n", j, strings[j]);
91030abcd2Smartin }
92030abcd2Smartin
93030abcd2Smartin ATF_REQUIRE(nptrs >= ncalls + 2 + min_frames);
94030abcd2Smartin ATF_REQUIRE(nptrs <= ncalls + 2 + max_frames);
955212b5c3Schristos ATF_CHECK_STREQ(strings[0], "myfunc3");
965212b5c3Schristos ATF_CHECK_STREQ(strings[1], "myfunc2");
975212b5c3Schristos
985212b5c3Schristos for (j = 2; j < ncalls + 2; j++)
995212b5c3Schristos ATF_CHECK_STREQ(strings[j], "myfunc1");
1005212b5c3Schristos
10144c2dc6eSjoerg for (size_t i = 0; j < nptrs; i++, j++) {
1027af46b7fSjoerg if (frames[i].is_optional &&
1037af46b7fSjoerg strcmp(strings[j], frames[i].name)) {
10444c2dc6eSjoerg --i;
10544c2dc6eSjoerg continue;
10644c2dc6eSjoerg }
1077af46b7fSjoerg ATF_CHECK_STREQ(strings[j], frames[i].name);
10844c2dc6eSjoerg }
1095212b5c3Schristos
1105212b5c3Schristos free(strings);
111e3f0a6caSjoerg
112e3f0a6caSjoerg if (prevent_inline)
113e3f0a6caSjoerg vfork();
1145212b5c3Schristos }
1155212b5c3Schristos
11634f57994Smartin void
myfunc2(size_t ncalls)1175212b5c3Schristos myfunc2(size_t ncalls)
1185212b5c3Schristos {
119e3f0a6caSjoerg myfunc3(ncalls);
120e3f0a6caSjoerg
1218f5b02bdSjoerg if (prevent_inline)
1228f5b02bdSjoerg vfork();
1235212b5c3Schristos }
1245212b5c3Schristos
12534f57994Smartin void
myfunc1(size_t origcalls,volatile size_t ncalls)126e3f0a6caSjoerg myfunc1(size_t origcalls, volatile size_t ncalls)
1275212b5c3Schristos {
1285212b5c3Schristos if (ncalls > 1)
1295212b5c3Schristos myfunc1(origcalls, ncalls - 1);
1305212b5c3Schristos else
1315212b5c3Schristos myfunc2(origcalls);
132e3f0a6caSjoerg
133e3f0a6caSjoerg if (prevent_inline)
134e3f0a6caSjoerg vfork();
1355212b5c3Schristos }
1365212b5c3Schristos
13734f57994Smartin void
myfunc(size_t ncalls)1385212b5c3Schristos myfunc(size_t ncalls)
1395212b5c3Schristos {
140e3f0a6caSjoerg myfunc1(ncalls, ncalls);
141e3f0a6caSjoerg
1428f5b02bdSjoerg if (prevent_inline)
1438f5b02bdSjoerg vfork();
1445212b5c3Schristos }
1455212b5c3Schristos
1465212b5c3Schristos ATF_TC(backtrace_fmt_basic);
ATF_TC_HEAD(backtrace_fmt_basic,tc)1475212b5c3Schristos ATF_TC_HEAD(backtrace_fmt_basic, tc)
1485212b5c3Schristos {
1495212b5c3Schristos atf_tc_set_md_var(tc, "descr", "Test backtrace_fmt(3)");
1509695f560Sjoerg atf_tc_set_md_var(tc, "require.files", "/proc/self");
1515212b5c3Schristos }
1525212b5c3Schristos
ATF_TC_BODY(backtrace_fmt_basic,tc)1535212b5c3Schristos ATF_TC_BODY(backtrace_fmt_basic, tc)
1545212b5c3Schristos {
1555212b5c3Schristos myfunc(12);
15634f57994Smartin
15734f57994Smartin if (prevent_inline)
15834f57994Smartin vfork();
1595212b5c3Schristos }
1605212b5c3Schristos
ATF_TP_ADD_TCS(tp)1615212b5c3Schristos ATF_TP_ADD_TCS(tp)
1625212b5c3Schristos {
1635212b5c3Schristos
1645212b5c3Schristos ATF_TP_ADD_TC(tp, backtrace_fmt_basic);
1655212b5c3Schristos
1665212b5c3Schristos return atf_no_error();
1675212b5c3Schristos }
168