debug.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Debugging routines for the remote server for GDB.
  2. Copyright (C) 2014-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 "server.h"
  15. #include <chrono>
  16. #if !defined (IN_PROCESS_AGENT)
  17. bool remote_debug = false;
  18. #endif
  19. /* Output file for debugging. Default to standard error. */
  20. static FILE *debug_file = stderr;
  21. /* See debug.h. */
  22. bool debug_threads;
  23. /* Include timestamps in debugging output. */
  24. int debug_timestamp;
  25. #if !defined (IN_PROCESS_AGENT)
  26. /* See debug.h. */
  27. void
  28. debug_set_output (const char *new_debug_file)
  29. {
  30. /* Close any existing file and reset to standard error. */
  31. if (debug_file != stderr)
  32. {
  33. fclose (debug_file);
  34. }
  35. debug_file = stderr;
  36. /* Catch empty filenames. */
  37. if (new_debug_file == nullptr || strlen (new_debug_file) == 0)
  38. return;
  39. FILE *fptr = fopen (new_debug_file, "w");
  40. if (fptr == nullptr)
  41. {
  42. debug_printf ("Cannot open %s for writing. %s. Switching to stderr.\n",
  43. new_debug_file, safe_strerror (errno));
  44. return;
  45. }
  46. debug_file = fptr;
  47. }
  48. #endif
  49. /* See gdbsupport/common-debug.h. */
  50. int debug_print_depth = 0;
  51. /* Print a debugging message.
  52. If the text begins a new line it is preceded by a timestamp.
  53. We don't get fancy with newline checking, we just check whether the
  54. previous call ended with "\n". */
  55. void
  56. debug_vprintf (const char *format, va_list ap)
  57. {
  58. #if !defined (IN_PROCESS_AGENT)
  59. /* N.B. Not thread safe, and can't be used, as is, with IPA. */
  60. static int new_line = 1;
  61. if (debug_timestamp && new_line)
  62. {
  63. using namespace std::chrono;
  64. steady_clock::time_point now = steady_clock::now ();
  65. seconds s = duration_cast<seconds> (now.time_since_epoch ());
  66. microseconds us = duration_cast<microseconds> (now.time_since_epoch ()) - s;
  67. fprintf (debug_file, "%ld.%06ld ", (long) s.count (), (long) us.count ());
  68. }
  69. #endif
  70. vfprintf (debug_file, format, ap);
  71. #if !defined (IN_PROCESS_AGENT)
  72. if (*format)
  73. new_line = format[strlen (format) - 1] == '\n';
  74. #endif
  75. }
  76. /* Flush debugging output.
  77. This is called, for example, when starting an inferior to ensure all debug
  78. output thus far appears before any inferior output. */
  79. void
  80. debug_flush (void)
  81. {
  82. fflush (debug_file);
  83. }
  84. /* See debug.h. */
  85. ssize_t
  86. debug_write (const void *buf, size_t nbyte)
  87. {
  88. int fd = fileno (debug_file);
  89. return write (fd, buf, nbyte);
  90. }