README.libm-test: Use testrun.sh to run libm test.
[glibc.git] / debug / tst-backtrace5.c
blobca47437d92bbc645825fa3c2d93d75d20466275b
1 /* Test backtrace and backtrace_symbols for signal frames, where a
2 system call was interrupted by a signal.
3 Copyright (C) 2011-2013 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <execinfo.h>
21 #include <search.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <unistd.h>
29 #include "tst-backtrace.h"
31 static int do_test (void);
32 #define TEST_FUNCTION do_test ()
33 #include "../test-skeleton.c"
35 /* The backtrace should include at least handle_signal, a signal
36 trampoline, read, 3 * fn, and do_test. */
37 #define NUM_FUNCTIONS 7
39 void
40 handle_signal (int signum)
42 void *addresses[NUM_FUNCTIONS];
43 char **symbols;
44 int n;
45 int i;
47 /* Get the backtrace addresses. */
48 n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
49 printf ("Obtained backtrace with %d functions\n", n);
50 /* Check that there are at least seven functions. */
51 if (n < NUM_FUNCTIONS)
53 FAIL ();
54 return;
56 /* Convert them to symbols. */
57 symbols = backtrace_symbols (addresses, n);
58 /* Check that symbols were obtained. */
59 if (symbols == NULL)
61 FAIL ();
62 return;
64 for (i = 0; i < n; ++i)
65 printf ("Function %d: %s\n", i, symbols[i]);
66 /* Check that the function names obtained are accurate. */
67 if (!match (symbols[0], "handle_signal"))
69 FAIL ();
70 return;
72 /* Do not check name for signal trampoline. */
73 i = 2;
74 if (!match (symbols[i++], "read"))
76 /* Perhaps symbols[2] is __kernel_vsyscall? */
77 if (!match (symbols[i++], "read"))
79 FAIL ();
80 return;
83 for (; i < n - 1; i++)
84 if (!match (symbols[i], "fn"))
86 FAIL ();
87 return;
89 /* Symbol names are not available for static functions, so we do not
90 check do_test. */
93 NO_INLINE int
94 fn (int c)
96 pid_t parent_pid, child_pid;
97 int pipefd[2];
98 char r[1];
99 struct sigaction act;
101 if (c > 0)
103 fn (c - 1);
104 return x;
107 memset (&act, 0, sizeof (act));
108 act.sa_handler = handle_signal;
109 sigemptyset (&act.sa_mask);
110 sigaction (SIGUSR1, &act, NULL);
111 parent_pid = getpid ();
112 if (pipe (pipefd) == -1)
113 abort ();
115 child_pid = fork ();
116 if (child_pid == (pid_t) -1)
117 abort ();
118 else if (child_pid == 0)
120 sleep (1);
121 kill (parent_pid, SIGUSR1);
122 _exit (0);
125 /* In the parent. */
126 read (pipefd[0], r, 1);
128 return 0;
131 NO_INLINE static int
132 do_test (void)
134 fn (2);
135 return ret;