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