bt-utils.c 4.6 KB

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