xref: /freebsd/lib/libexecinfo/tests/sigtramp_test.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 #include <sys/cdefs.h>
7 __FBSDID("$FreeBSD$");
8 
9 #include <sys/param.h>
10 #include <sys/wait.h>
11 
12 #include <execinfo.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 
18 #include <atf-c.h>
19 
20 #define	BT_FUNCTIONS		10
21 
22 void	handler(int);
23 
24 __noinline void
25 handler(int signum __unused)
26 {
27 	void *addresses[BT_FUNCTIONS];
28 	char **symbols;
29 	size_t n, i, match;
30 
31 	n = backtrace(addresses, nitems(addresses));
32 	ATF_REQUIRE(n > 1);
33 	symbols = backtrace_symbols(addresses, n);
34 	ATF_REQUIRE(symbols != NULL);
35 
36 	match = -1;
37 	for (i = 0; i < n; i++) {
38 		printf("%zu: %p, %s\n", i, addresses[i], symbols[i]);
39 		if (strstr(symbols[i], "<main+") != NULL)
40 			match = i;
41 	}
42 	ATF_REQUIRE(match > 0);
43 	printf("match at %zu, symbols %zu\n", match, n);
44 
45 }
46 
47 ATF_TC_WITHOUT_HEAD(test_backtrace_sigtramp);
48 ATF_TC_BODY(test_backtrace_sigtramp, tc)
49 {
50 	struct sigaction act;
51 	pid_t child;
52 	int status;
53 
54 	memset(&act, 0, sizeof(act));
55 	act.sa_handler = handler;
56 	sigemptyset(&act.sa_mask);
57 	sigaction(SIGUSR1, &act, NULL);
58 
59 	child = fork();
60 	ATF_REQUIRE(child != -1);
61 
62 	if (child == 0) {
63 		kill(getppid(), SIGUSR1);
64 		_exit(0);
65 	} else
66 		wait(&status);
67 }
68 
69 ATF_TP_ADD_TCS(tp)
70 {
71 
72 	ATF_TP_ADD_TC(tp, test_backtrace_sigtramp);
73 
74 	return (atf_no_error());
75 }
76