blob: d139c106383c4071ecf2580eee8f1d3d65b172af [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/* Test backtrace and backtrace_symbols.
2 Copyright (C) 2009-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include <execinfo.h>
20#include <search.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "tst-backtrace.h"
26
27static int do_test (void);
28#define TEST_FUNCTION do_test ()
29#include "../test-skeleton.c"
30
31/* The backtrace should include at least f1, f2, f3, and do_test. */
32#define NUM_FUNCTIONS 4
33
34NO_INLINE void
35fn1 (void)
36{
37 void *addresses[NUM_FUNCTIONS];
38 char **symbols;
39 int n;
40 int i;
41
42 /* Get the backtrace addresses. */
43 n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
44 printf ("Obtained backtrace with %d functions\n", n);
45 /* Check that there are at least four functions. */
46 if (n < NUM_FUNCTIONS)
47 {
48 FAIL ();
49 return;
50 }
51 /* Convert them to symbols. */
52 symbols = backtrace_symbols (addresses, n);
53 /* Check that symbols were obtained. */
54 if (symbols == NULL)
55 {
56 FAIL ();
57 return;
58 }
59 for (i = 0; i < n; ++i)
60 printf ("Function %d: %s\n", i, symbols[i]);
61 /* Check that the function names obtained are accurate. */
62 if (!match (symbols[0], "fn1"))
63 {
64 FAIL ();
65 return;
66 }
67 /* Symbol names are not available for static functions, so we do not
68 check f2. */
69 if (!match (symbols[2], "fn3"))
70 {
71 FAIL ();
72 return;
73 }
74 /* Symbol names are not available for static functions, so we do not
75 check do_test. */
76}
77
78NO_INLINE static int
79fn2 (void)
80{
81 fn1 ();
82 /* Prevent tail calls. */
83 return x;
84}
85
86NO_INLINE int
87fn3 (void)
88{
89 fn2();
90 /* Prevent tail calls. */
91 return x;
92}
93
94NO_INLINE static int
95do_test (void)
96{
97 /* Test BZ #18084. */
98 void *buffer[1];
99
100 if (backtrace (buffer, 0) != 0)
101 FAIL ();
102
103 fn3 ();
104 return ret;
105}