m68k: add unwind tables to backtrace
[glibc.git] / debug / tst-backtrace2.c
blobe1d4572c358cea2ebd2d237d17f3bcd6307fba17
1 /* Test backtrace and backtrace_symbols.
2 Copyright (C) 2009-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>
25 static int do_test (void);
26 #define TEST_FUNCTION do_test ()
27 #include "../test-skeleton.c"
29 /* Set to a non-zero value if the test fails. */
30 int ret;
32 /* Accesses to X are used to prevent optimization. */
33 volatile int x;
35 /* Called if the test fails. */
36 #define FAIL() \
37 do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
39 /* The backtrace should include at least f1, f2, f3, and do_test. */
40 #define NUM_FUNCTIONS 4
42 /* Use this attribute to prevent inlining, so that all expected frames
43 are present. */
44 #define NO_INLINE __attribute__ ((noinline))
46 NO_INLINE void
47 fn1 (void)
49 void *addresses[NUM_FUNCTIONS];
50 char **symbols;
51 int n;
52 int i;
54 /* Get the backtrace addresses. */
55 n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
56 printf ("Obtained backtrace with %d functions\n", n);
57 /* Check that there are at least four functions. */
58 if (n < NUM_FUNCTIONS)
60 FAIL ();
61 return;
63 /* Convert them to symbols. */
64 symbols = backtrace_symbols (addresses, n);
65 /* Check that symbols were obtained. */
66 if (symbols == NULL)
68 FAIL ();
69 return;
71 for (i = 0; i < n; ++i)
72 printf ("Function %d: %s\n", i, symbols[i]);
73 /* Check that the function names obtained are accurate. */
74 if (strstr (symbols[0], "fn1") == NULL)
76 FAIL ();
77 return;
79 /* Symbol names are not available for static functions, so we do not
80 check f2. */
81 if (strstr (symbols[2], "fn3") == NULL)
83 FAIL ();
84 return;
86 /* Symbol names are not available for static functions, so we do not
87 check do_test. */
90 NO_INLINE static int
91 fn2 (void)
93 fn1 ();
94 /* Prevent tail calls. */
95 return x;
98 NO_INLINE int
99 fn3 (void)
101 fn2();
102 /* Prevent tail calls. */
103 return x;
106 NO_INLINE static int
107 do_test (void)
109 fn3 ();
110 return ret;