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