m68k: add unwind tables to backtrace
[glibc.git] / debug / tst-backtrace4.c
blob41a3f51349a1ff17c1c237100dad2c5960ebf689
1 /* Test backtrace and backtrace_symbols for signal frames.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <execinfo.h>
20 #include <search.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <signal.h>
26 #include <unistd.h>
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 /* Set to a non-zero value if the test fails. */
33 volatile int ret;
35 /* Accesses to X are used to prevent optimization. */
36 volatile int x;
38 /* Called if the test fails. */
39 #define FAIL() \
40 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
42 /* The backtrace should include at least handle_signal, a signal
43 trampoline, 3 * fn, and do_test. */
44 #define NUM_FUNCTIONS 6
46 /* Use this attribute to prevent inlining, so that all expected frames
47 are present. */
48 #define NO_INLINE __attribute__ ((noinline))
50 volatile int sig_handled = 0;
52 void
53 handle_signal (int signum)
55 void *addresses[NUM_FUNCTIONS];
56 char **symbols;
57 int n;
58 int i;
60 sig_handled = 1;
62 /* Get the backtrace addresses. */
63 n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
64 printf ("Obtained backtrace with %d functions\n", n);
65 /* Check that there are at least six functions. */
66 if (n < NUM_FUNCTIONS)
68 FAIL ();
69 return;
71 /* Convert them to symbols. */
72 symbols = backtrace_symbols (addresses, n);
73 /* Check that symbols were obtained. */
74 if (symbols == NULL)
76 FAIL ();
77 return;
79 for (i = 0; i < n; ++i)
80 printf ("Function %d: %s\n", i, symbols[i]);
81 /* Check that the function names obtained are accurate. */
82 if (strstr (symbols[0], "handle_signal") == NULL)
84 FAIL ();
85 return;
87 /* Do not check name for signal trampoline. */
88 for (i = 2; i < n - 1; i++)
89 if (strstr (symbols[i], "fn") == NULL)
91 FAIL ();
92 return;
94 /* Symbol names are not available for static functions, so we do not
95 check do_test. */
98 NO_INLINE int
99 fn (int c)
101 pid_t parent_pid, child_pid;
103 if (c > 0)
105 fn (c - 1);
106 return x;
109 signal (SIGUSR1, handle_signal);
110 parent_pid = getpid ();
112 child_pid = fork ();
113 if (child_pid == (pid_t) -1)
114 abort ();
115 else if (child_pid == 0)
117 sleep (1);
118 kill (parent_pid, SIGUSR1);
119 _exit (0);
122 /* In the parent. */
123 while (sig_handled == 0)
126 return 0;
129 NO_INLINE static int
130 do_test (void)
132 fn (2);
133 return ret;