exceptions.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "exceptions.h"
  16. #include "breakpoint.h"
  17. #include "target.h"
  18. #include "inferior.h"
  19. #include "annotate.h"
  20. #include "ui-out.h"
  21. #include "serial.h"
  22. #include "gdbthread.h"
  23. #include "top.h"
  24. #include "gdbsupport/gdb_optional.h"
  25. static void
  26. print_flush (void)
  27. {
  28. struct ui *ui = current_ui;
  29. struct serial *gdb_stdout_serial;
  30. if (deprecated_error_begin_hook)
  31. deprecated_error_begin_hook ();
  32. gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
  33. if (target_supports_terminal_ours ())
  34. {
  35. term_state.emplace ();
  36. target_terminal::ours_for_output ();
  37. }
  38. /* We want all output to appear now, before we print the error. We
  39. have 2 levels of buffering we have to flush (it's possible that
  40. some of these should be changed to flush the lower-level ones
  41. too): */
  42. /* 1. The stdio buffer. */
  43. gdb_flush (gdb_stdout);
  44. gdb_flush (gdb_stderr);
  45. /* 2. The system-level buffer. */
  46. gdb_stdout_serial = serial_fdopen (fileno (ui->outstream));
  47. if (gdb_stdout_serial)
  48. {
  49. serial_drain_output (gdb_stdout_serial);
  50. serial_un_fdopen (gdb_stdout_serial);
  51. }
  52. annotate_error_begin ();
  53. }
  54. static void
  55. print_exception (struct ui_file *file, const struct gdb_exception &e)
  56. {
  57. /* KLUDGE: cagney/2005-01-13: Write the string out one line at a time
  58. as that way the MI's behavior is preserved. */
  59. const char *start;
  60. const char *end;
  61. for (start = e.what (); start != NULL; start = end)
  62. {
  63. end = strchr (start, '\n');
  64. if (end == NULL)
  65. gdb_puts (start, file);
  66. else
  67. {
  68. end++;
  69. file->write (start, end - start);
  70. }
  71. }
  72. gdb_printf (file, "\n");
  73. /* Now append the annotation. */
  74. switch (e.reason)
  75. {
  76. case RETURN_QUIT:
  77. annotate_quit ();
  78. break;
  79. case RETURN_ERROR:
  80. /* Assume that these are all errors. */
  81. annotate_error ();
  82. break;
  83. default:
  84. internal_error (__FILE__, __LINE__, _("Bad switch."));
  85. }
  86. }
  87. void
  88. exception_print (struct ui_file *file, const struct gdb_exception &e)
  89. {
  90. if (e.reason < 0 && e.message != NULL)
  91. {
  92. print_flush ();
  93. print_exception (file, e);
  94. }
  95. }
  96. void
  97. exception_fprintf (struct ui_file *file, const struct gdb_exception &e,
  98. const char *prefix, ...)
  99. {
  100. if (e.reason < 0 && e.message != NULL)
  101. {
  102. va_list args;
  103. print_flush ();
  104. /* Print the prefix. */
  105. va_start (args, prefix);
  106. gdb_vprintf (file, prefix, args);
  107. va_end (args);
  108. print_exception (file, e);
  109. }
  110. }