gdbserver: make thread_info non-POD
[deliverable/binutils-gdb.git] / gdbserver / debug.cc
CommitLineData
87ce2a04 1/* Debugging routines for the remote server for GDB.
3666a048 2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
87ce2a04
DE
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 "server.h"
dcb07cfa 20#include <chrono>
87ce2a04 21
c1bc0935
AH
22#if !defined (IN_PROCESS_AGENT)
23int remote_debug = 0;
24#endif
25
aeb2e706 26/* Output file for debugging. Default to standard error. */
6bd434d6 27static FILE *debug_file = stderr;
aeb2e706 28
f9d949fb 29/* See debug.h. */
87ce2a04
DE
30int debug_threads;
31
32/* Include timestamps in debugging output. */
33int debug_timestamp;
34
aeb2e706
AH
35#if !defined (IN_PROCESS_AGENT)
36
37/* See debug.h. */
38
39void
40debug_set_output (const char *new_debug_file)
41{
42 /* Close any existing file and reset to standard error. */
43 if (debug_file != stderr)
44 {
45 fclose (debug_file);
46 }
47 debug_file = stderr;
48
49 /* Catch empty filenames. */
50 if (new_debug_file == nullptr || strlen (new_debug_file) == 0)
51 return;
52
53 FILE *fptr = fopen (new_debug_file, "w");
54
55 if (fptr == nullptr)
56 {
57 debug_printf ("Cannot open %s for writing. %s. Switching to stderr.\n",
6d91ce9a 58 new_debug_file, safe_strerror (errno));
aeb2e706
AH
59 return;
60 }
61
62 debug_file = fptr;
63}
64
65#endif
66
3ec3145c
SM
67/* See gdbsupport/common-debug.h. */
68
69int debug_print_depth = 0;
70
87ce2a04 71/* Print a debugging message.
dcb07cfa 72 If the text begins a new line it is preceded by a timestamp.
87ce2a04
DE
73 We don't get fancy with newline checking, we just check whether the
74 previous call ended with "\n". */
75
76void
34abf635 77debug_vprintf (const char *format, va_list ap)
87ce2a04 78{
0b04e523 79#if !defined (IN_PROCESS_AGENT)
87ce2a04
DE
80 /* N.B. Not thread safe, and can't be used, as is, with IPA. */
81 static int new_line = 1;
82
83 if (debug_timestamp && new_line)
84 {
dcb07cfa 85 using namespace std::chrono;
87ce2a04 86
dcb07cfa
PA
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;
87ce2a04 90
aeb2e706 91 fprintf (debug_file, "%ld.%06ld ", (long) s.count (), (long) us.count ());
87ce2a04
DE
92 }
93#endif
94
aeb2e706 95 vfprintf (debug_file, format, ap);
87ce2a04 96
0b04e523 97#if !defined (IN_PROCESS_AGENT)
34abf635
GB
98 if (*format)
99 new_line = format[strlen (format) - 1] == '\n';
87ce2a04
DE
100#endif
101}
102
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. */
106
107void
108debug_flush (void)
109{
aeb2e706 110 fflush (debug_file);
87ce2a04
DE
111}
112
113/* Notify the user that the code is entering FUNCTION_NAME.
114 FUNCTION_NAME is the name of the calling function, or NULL if unknown.
115
116 This is intended to be called via the debug_enter macro. */
117
118void
119do_debug_enter (const char *function_name)
120{
121 if (function_name != NULL)
122 debug_printf (">>>> entering %s\n", function_name);
123}
124
125/* Notify the user that the code is exiting FUNCTION_NAME.
126 FUNCTION_NAME is the name of the calling function, or NULL if unknown.
127
128 This is intended to be called via the debug_exit macro. */
129
130void
131do_debug_exit (const char *function_name)
132{
133 if (function_name != NULL)
134 debug_printf ("<<<< exiting %s\n", function_name);
135}
a7e559cc
AH
136
137/* See debug.h. */
138
8d6a48df 139ssize_t
a7e559cc
AH
140debug_write (const void *buf, size_t nbyte)
141{
142 int fd = fileno (debug_file);
143 return write (fd, buf, nbyte);
144}
This page took 0.563916 seconds and 4 git commands to generate.