gdb: Introduce setting construct within cmd_list_element
[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.has_value ());
33 gdb_assert (c->var->type () == var_boolean);
34
35 #ifndef GDB_PRINT_INTERNAL_BACKTRACE
36 if (c->var->get<bool> ())
37 {
38 c->var->set<bool> (false);
39 error (_("support for this feature is not compiled into GDB"));
40 }
41 #endif
42 }
43
44 #ifdef GDB_PRINT_INTERNAL_BACKTRACE_USING_LIBBACKTRACE
45
46 /* Callback used by libbacktrace if it encounters an error. */
47
48 static void
49 libbacktrace_error (void *data, const char *errmsg, int errnum)
50 {
51 /* A negative errnum indicates no debug info was available, just
52 skip printing a backtrace in this case. */
53 if (errnum < 0)
54 return;
55
56 const auto sig_write = [] (const char *msg) -> void
57 {
58 gdb_stderr->write_async_safe (msg, strlen (msg));
59 };
60
61 sig_write ("error creating backtrace: ");
62 sig_write (errmsg);
63 if (errnum > 0)
64 {
65 char buf[20];
66 snprintf (buf, sizeof (buf), ": %d", errnum);
67 buf[sizeof (buf) - 1] = '\0';
68
69 sig_write (buf);
70 }
71 sig_write ("\n");
72 }
73
74 /* Callback used by libbacktrace to print a single stack frame. */
75
76 static int
77 libbacktrace_print (void *data, uintptr_t pc, const char *filename,
78 int lineno, const char *function)
79 {
80 const auto sig_write = [] (const char *msg) -> void
81 {
82 gdb_stderr->write_async_safe (msg, strlen (msg));
83 };
84
85 /* Buffer to print addresses and line numbers into. An 8-byte address
86 with '0x' prefix and a null terminator requires 20 characters. This
87 also feels like it should be enough to represent line numbers in most
88 files. We are also careful to ensure we don't overflow this buffer. */
89 char buf[20];
90
91 snprintf (buf, sizeof (buf), "0x%" PRIxPTR " ", pc);
92 buf[sizeof (buf) - 1] = '\0';
93 sig_write (buf);
94 sig_write (function == nullptr ? "???" : function);
95 if (filename != nullptr)
96 {
97 sig_write ("\n\t");
98 sig_write (filename);
99 sig_write (":");
100 snprintf (buf, sizeof (buf), "%d", lineno);
101 buf[sizeof (buf) - 1] = '\0';
102 sig_write (buf);
103 }
104 sig_write ("\n");
105
106 return function != nullptr && strcmp (function, "main") == 0;
107 }
108
109 /* Write a backtrace to GDB's stderr in an async safe manner. This is a
110 backtrace of GDB, not any running inferior, and is to be used when GDB
111 crashes or hits some other error condition. */
112
113 static void
114 gdb_internal_backtrace_1 ()
115 {
116 static struct backtrace_state *state = nullptr;
117
118 if (state == nullptr)
119 state = backtrace_create_state (nullptr, 0, libbacktrace_error, nullptr);
120
121 backtrace_full (state, 0, libbacktrace_print, libbacktrace_error, nullptr);
122 }
123
124 #elif defined GDB_PRINT_INTERNAL_BACKTRACE_USING_EXECINFO
125
126 /* See the comment on previous version of this function. */
127
128 static void
129 gdb_internal_backtrace_1 ()
130 {
131 const auto sig_write = [] (const char *msg) -> void
132 {
133 gdb_stderr->write_async_safe (msg, strlen (msg));
134 };
135
136 /* Allow up to 25 frames of backtrace. */
137 void *buffer[25];
138 int frames = backtrace (buffer, ARRAY_SIZE (buffer));
139
140 backtrace_symbols_fd (buffer, frames, gdb_stderr->fd ());
141 if (frames == ARRAY_SIZE (buffer))
142 sig_write (_("Backtrace might be incomplete.\n"));
143 }
144
145 #endif
146
147 /* See bt-utils.h. */
148
149 void
150 gdb_internal_backtrace ()
151 {
152 if (current_ui == nullptr)
153 return;
154
155 const auto sig_write = [] (const char *msg) -> void
156 {
157 gdb_stderr->write_async_safe (msg, strlen (msg));
158 };
159
160 sig_write (_("----- Backtrace -----\n"));
161
162 #ifdef GDB_PRINT_INTERNAL_BACKTRACE
163 if (gdb_stderr->fd () > -1)
164 gdb_internal_backtrace_1 ();
165 else
166 #endif
167 sig_write (_("Backtrace unavailable\n"));
168
169 sig_write ("---------------------\n");
170 }