Automatic date update in version.in
[binutils-gdb.git] / gdbserver / debug.cc
blob195c7e9a683870ed04b84787ab7e405dc3c100fb
1 /* Debugging routines for the remote server for GDB.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "server.h"
20 #include <chrono>
22 #if !defined (IN_PROCESS_AGENT)
23 bool remote_debug = false;
24 #endif
26 /* Output file for debugging. Default to standard error. */
27 static FILE *debug_file = stderr;
29 /* See debug.h. */
30 bool debug_threads;
32 /* Include timestamps in debugging output. */
33 int debug_timestamp;
35 #if !defined (IN_PROCESS_AGENT)
37 /* See debug.h. */
39 void
40 debug_set_output (const char *new_debug_file)
42 /* Close any existing file and reset to standard error. */
43 if (debug_file != stderr)
45 fclose (debug_file);
47 debug_file = stderr;
49 /* Catch empty filenames. */
50 if (new_debug_file == nullptr || strlen (new_debug_file) == 0)
51 return;
53 FILE *fptr = fopen (new_debug_file, "w");
55 if (fptr == nullptr)
57 debug_printf ("Cannot open %s for writing. %s. Switching to stderr.\n",
58 new_debug_file, safe_strerror (errno));
59 return;
62 debug_file = fptr;
65 #endif
67 /* See gdbsupport/common-debug.h. */
69 int debug_print_depth = 0;
71 /* Print a debugging message.
72 If the text begins a new line it is preceded by a timestamp.
73 We don't get fancy with newline checking, we just check whether the
74 previous call ended with "\n". */
76 void
77 debug_vprintf (const char *format, va_list ap)
79 #if !defined (IN_PROCESS_AGENT)
80 /* N.B. Not thread safe, and can't be used, as is, with IPA. */
81 static int new_line = 1;
83 if (debug_timestamp && new_line)
85 using namespace std::chrono;
87 steady_clock::time_point now = steady_clock::now ();
88 seconds s = duration_cast<seconds> (now.time_since_epoch ());
89 microseconds us = duration_cast<microseconds> (now.time_since_epoch ()) - s;
91 fprintf (debug_file, "%ld.%06ld ", (long) s.count (), (long) us.count ());
93 #endif
95 vfprintf (debug_file, format, ap);
97 #if !defined (IN_PROCESS_AGENT)
98 if (*format)
99 new_line = format[strlen (format) - 1] == '\n';
100 #endif
103 /* Flush debugging output.
104 This is called, for example, when starting an inferior to ensure all debug
105 output thus far appears before any inferior output. */
107 void
108 debug_flush (void)
110 fflush (debug_file);
113 /* See debug.h. */
115 ssize_t
116 debug_write (const void *buf, size_t nbyte)
118 int fd = fileno (debug_file);
119 return write (fd, buf, nbyte);