gdb: use libbacktrace to create a better backtrace for fatal signals
[binutils-gdb.git] / gdb / bt-utils.c
1 /* Copyright (C) 2021 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "defs.h"
19 #include "bt-utils.h"
20 #include "command.h"
21 #include "gdbcmd.h"
22 #include "top.h"
23 #include "cli/cli-decode.h"
24
25 /* See bt-utils.h. */
26
27 void
28 gdb_internal_backtrace_set_cmd (const char *args, int from_tty,
29 cmd_list_element *c)
30 {
31 gdb_assert (c->type == set_cmd);
32 gdb_assert (c->var_type == var_boolean);
33 gdb_assert (c->var != nullptr);
34
35 #ifndef GDB_PRINT_INTERNAL_BACKTRACE
36 bool *var_ptr = (bool *) c->var;
37
38 if (*var_ptr)
39 {
40 *var_ptr = false;
41 error (_("support for this feature is not compiled into GDB"));
42 }
43 #endif
44 }
45
46 #ifdef GDB_PRINT_INTERNAL_BACKTRACE_USING_LIBBACKTRACE
47
48 /* Callback used by libbacktrace if it encounters an error. */
49
50 static void
51 libbacktrace_error (void *data, const char *errmsg, int errnum)
52 {
53 /* A negative errnum indicates no debug info was available, just
54 skip printing a backtrace in this case. */
55 if (errnum < 0)
56 return;
57
58 const auto sig_write = [] (const char *msg) -> void
59 {
60 gdb_stderr->write_async_safe (msg, strlen (msg));
61 };
62
63 sig_write ("error creating backtrace: ");
64 sig_write (errmsg);
65 if (errnum > 0)
66 {
67 char buf[20];
68 snprintf (buf, sizeof (buf), ": %d", errnum);
69 buf[sizeof (buf) - 1] = '\0';
70
71 sig_write (buf);
72 }
73 sig_write ("\n");
74 }
75
76 /* Callback used by libbacktrace to print a single stack frame. */
77
78 static int
79 libbacktrace_print (void *data, uintptr_t pc, const char *filename,
80 int lineno, const char *function)
81 {
82 const auto sig_write = [] (const char *msg) -> void
83 {
84 gdb_stderr->write_async_safe (msg, strlen (msg));
85 };
86
87 /* Buffer to print addresses and line numbers into. An 8-byte address
88 with '0x' prefix and a null terminator requires 20 characters. This
89 also feels like it should be enough to represent line numbers in most
90 files. We are also careful to ensure we don't overflow this buffer. */
91 char buf[20];
92
93 snprintf (buf, sizeof (buf), "0x%lx ", pc);
94 buf[sizeof (buf) - 1] = '\0';
95 sig_write (buf);
96 sig_write (function == nullptr ? "???" : function);
97 if (filename != nullptr)
98 {
99 sig_write ("\n\t");
100 sig_write (filename);
101 sig_write (":");
102 snprintf (buf, sizeof (buf), "%d", lineno);
103 buf[sizeof (buf) - 1] = '\0';
104 sig_write (buf);
105 }
106 sig_write ("\n");
107
108 return function != nullptr && strcmp (function, "main") == 0;
109 }
110
111 /* Write a backtrace to GDB's stderr in an async safe manor. This is a
112 backtrace of GDB, not any running inferior, and is to be used when GDB
113 crashes or hits some other error condition. */
114
115 static void
116 gdb_internal_backtrace_1 ()
117 {
118 static struct backtrace_state *state = nullptr;
119
120 if (state == nullptr)
121 state = backtrace_create_state (nullptr, 0, libbacktrace_error, nullptr);
122
123 backtrace_full (state, 0, libbacktrace_print, libbacktrace_error, nullptr);
124 }
125
126 #elif defined GDB_PRINT_INTERNAL_BACKTRACE_USING_EXECINFO
127
128 /* See the comment on previous version of this function. */
129
130 static void
131 gdb_internal_backtrace_1 ()
132 {
133 const auto sig_write = [] (const char *msg) -> void
134 {
135 gdb_stderr->write_async_safe (msg, strlen (msg));
136 };
137
138 /* Allow up to 25 frames of backtrace. */
139 void *buffer[25];
140 int frames = backtrace (buffer, ARRAY_SIZE (buffer));
141
142 backtrace_symbols_fd (buffer, frames, gdb_stderr->fd ());
143 if (frames == ARRAY_SIZE (buffer))
144 sig_write (_("Backtrace might be incomplete.\n"));
145 }
146
147 #endif
148
149 /* See bt-utils.h. */
150
151 void
152 gdb_internal_backtrace ()
153 {
154 if (current_ui == nullptr)
155 return;
156
157 const auto sig_write = [] (const char *msg) -> void
158 {
159 gdb_stderr->write_async_safe (msg, strlen (msg));
160 };
161
162 sig_write (_("----- Backtrace -----\n"));
163
164 if (gdb_stderr->fd () > -1)
165 gdb_internal_backtrace_1 ();
166 else
167 sig_write (_("Backtrace unavailable\n"));
168
169 sig_write ("---------------------\n");
170 }