1 /* print_stack_trace() function that prints a stack trace.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 # include <backtrace.h>
21 static struct backtrace_state
*state
/* = NULL */;
24 # if (__GNUC__ >= 3) || (__clang_major__ >= 4)
25 __attribute__ ((always_inline
))
27 print_stack_trace_to (FILE *stream
)
30 state
= backtrace_create_state (NULL
, 0, NULL
, NULL
);
31 fprintf (stream
, "Stack trace:\n");
32 /* Pass skip=0, to work around <https://github.com/ianlancetaylor/libbacktrace/issues/60>. */
33 backtrace_print (state
, 0, stream
);
40 /* We need only one declaration from <sanitizer/asan_interface.h>. */
45 void __sanitizer_print_stack_trace (void);
47 /* The only supported stream, in this case, is stderr. */
49 # if (__GNUC__ >= 3) || (__clang_major__ >= 4)
50 __attribute__ ((always_inline
))
52 print_stack_trace_to (FILE *stream
)
54 fprintf (stream
, "Stack trace:\n");
55 __sanitizer_print_stack_trace ();
62 # include "execinfo.h"
65 # if (__GNUC__ >= 3) || (__clang_major__ >= 4)
66 __attribute__ ((always_inline
))
68 print_stack_trace_to (FILE *stream
)
71 int max_size
= sizeof (buffer
) / sizeof (buffer
[0]);
72 int size
= backtrace (buffer
, max_size
);
75 char **symbols
= backtrace_symbols (buffer
, size
);
80 fprintf (stream
, "Stack trace:\n");
81 for (i
= 0; i
< size
; i
++)
82 fprintf (stream
, "%s\n", symbols
[i
]);