Debian Codegraph @

gdbserver/debug.cc

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