xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/cli/cli-logging.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /* Command-line output logging for GDB, the GNU debugger.
2 
3    Copyright (C) 2003-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "ui-out.h"
23 #include "interps.h"
24 #include "cli/cli-style.h"
25 #include "cli/cli-decode.h"
26 
27 static std::string saved_filename;
28 
29 static std::string logging_filename = "gdb.txt";
30 static void
31 show_logging_filename (struct ui_file *file, int from_tty,
32 		       struct cmd_list_element *c, const char *value)
33 {
34   gdb_printf (file, _("The current logfile is \"%ps\".\n"),
35 	      styled_string (file_name_style.style (), value));
36 }
37 
38 static bool logging_overwrite;
39 
40 static void
41 maybe_warn_already_logging ()
42 {
43   if (!saved_filename.empty ())
44     warning (_("Currently logging to %s.  Turn the logging off and on to "
45 	       "make the new setting effective."), saved_filename.c_str ());
46 }
47 
48 static void
49 set_logging_overwrite (const char *args,
50 		       int from_tty, struct cmd_list_element *c)
51 {
52   maybe_warn_already_logging ();
53 }
54 
55 static void
56 show_logging_overwrite (struct ui_file *file, int from_tty,
57 			struct cmd_list_element *c, const char *value)
58 {
59   if (logging_overwrite)
60     gdb_printf (file, _("on: Logging overwrites the log file.\n"));
61   else
62     gdb_printf (file, _("off: Logging appends to the log file.\n"));
63 }
64 
65 /* Value as configured by the user.  */
66 static bool logging_redirect;
67 static bool debug_redirect;
68 
69 static void
70 set_logging_redirect (const char *args,
71 		      int from_tty, struct cmd_list_element *c)
72 {
73   maybe_warn_already_logging ();
74 }
75 
76 static void
77 show_logging_redirect (struct ui_file *file, int from_tty,
78 		       struct cmd_list_element *c, const char *value)
79 {
80   if (logging_redirect)
81     gdb_printf (file, _("on: Output will go only to the log file.\n"));
82   else
83     gdb_printf
84       (file,
85        _("off: Output will go to both the screen and the log file.\n"));
86 }
87 
88 static void
89 show_logging_debug_redirect (struct ui_file *file, int from_tty,
90 		       struct cmd_list_element *c, const char *value)
91 {
92   if (debug_redirect)
93     gdb_printf (file, _("on: Debug output will go only to the log file.\n"));
94   else
95     gdb_printf
96       (file,
97        _("off: Debug output will go to both the screen and the log file.\n"));
98 }
99 
100 /* If we've pushed output files, close them and pop them.  */
101 static void
102 pop_output_files (void)
103 {
104   current_interp_set_logging (NULL, false, false);
105 
106   /* Stay consistent with handle_redirections.  */
107   if (!current_uiout->is_mi_like_p ())
108     current_uiout->redirect (NULL);
109 }
110 
111 /* This is a helper for the `set logging' command.  */
112 static void
113 handle_redirections (int from_tty)
114 {
115   if (!saved_filename.empty ())
116     {
117       gdb_printf ("Already logging to %s.\n",
118 		  saved_filename.c_str ());
119       return;
120     }
121 
122   stdio_file_up log (new no_terminal_escape_file ());
123   if (!log->open (logging_filename.c_str (), logging_overwrite ? "w" : "a"))
124     perror_with_name (_("set logging"));
125 
126   /* Redirects everything to gdb_stdout while this is running.  */
127   if (from_tty)
128     {
129       if (!logging_redirect)
130 	gdb_printf ("Copying output to %s.\n",
131 		    logging_filename.c_str ());
132       else
133 	gdb_printf ("Redirecting output to %s.\n",
134 		    logging_filename.c_str ());
135 
136       if (!debug_redirect)
137 	gdb_printf ("Copying debug output to %s.\n",
138 		    logging_filename.c_str ());
139       else
140 	gdb_printf ("Redirecting debug output to %s.\n",
141 		    logging_filename.c_str ());
142     }
143 
144   saved_filename = logging_filename;
145 
146   /* Let the interpreter do anything it needs.  */
147   current_interp_set_logging (std::move (log), logging_redirect,
148 			      debug_redirect);
149 
150   /* Redirect the current ui-out object's output to the log.  Use
151      gdb_stdout, not log, since the interpreter may have created a tee
152      that wraps the log.  Don't do the redirect for MI, it confuses
153      MI's ui-out scheme.  Note that we may get here with MI as current
154      interpreter, but with the current ui_out as a CLI ui_out, with
155      '-interpreter-exec console "set logging on"'.  */
156   if (!current_uiout->is_mi_like_p ())
157     current_uiout->redirect (gdb_stdout);
158 }
159 
160 static void
161 set_logging_on (const char *args, int from_tty)
162 {
163   const char *rest = args;
164 
165   if (rest && *rest)
166     logging_filename = rest;
167 
168   handle_redirections (from_tty);
169 }
170 
171 static void
172 set_logging_off (const char *args, int from_tty)
173 {
174   if (saved_filename.empty ())
175     return;
176 
177   pop_output_files ();
178   if (from_tty)
179     gdb_printf ("Done logging to %s.\n",
180 		saved_filename.c_str ());
181   saved_filename.clear ();
182 }
183 
184 static bool logging_enabled;
185 
186 static void
187 set_logging_enabled (const char *args,
188 		     int from_tty, struct cmd_list_element *c)
189 {
190   if (logging_enabled)
191     set_logging_on (args, from_tty);
192   else
193     set_logging_off (args, from_tty);
194 }
195 
196 static void
197 show_logging_enabled (struct ui_file *file, int from_tty,
198 		       struct cmd_list_element *c, const char *value)
199 {
200   if (logging_enabled)
201     gdb_printf (file, _("on: Logging is enabled.\n"));
202   else
203     gdb_printf (file, _("off: Logging is disabled.\n"));
204 }
205 
206 void _initialize_cli_logging ();
207 void
208 _initialize_cli_logging ()
209 {
210   static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
211 
212   /* Set/show logging.  */
213   add_setshow_prefix_cmd ("logging", class_support,
214 			  _("Set logging options."),
215 			  _("Show logging options."),
216 			  &set_logging_cmdlist, &show_logging_cmdlist,
217 			  &setlist, &showlist);
218 
219   /* Set/show logging overwrite.  */
220   add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
221 Set whether logging overwrites or appends to the log file."), _("\
222 Show whether logging overwrites or appends to the log file."), _("\
223 If set, logging overwrites the log file."),
224 			   set_logging_overwrite,
225 			   show_logging_overwrite,
226 			   &set_logging_cmdlist, &show_logging_cmdlist);
227 
228   /* Set/show logging redirect.  */
229   add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
230 Set the logging output mode."), _("\
231 Show the logging output mode."), _("\
232 If redirect is off, output will go to both the screen and the log file.\n\
233 If redirect is on, output will go only to the log file."),
234 			   set_logging_redirect,
235 			   show_logging_redirect,
236 			   &set_logging_cmdlist, &show_logging_cmdlist);
237 
238   /* Set/show logging debugredirect.  */
239   add_setshow_boolean_cmd ("debugredirect", class_support,
240 			   &debug_redirect, _("\
241 Set the logging debug output mode."), _("\
242 Show the logging debug output mode."), _("\
243 If debug redirect is off, debug will go to both the screen and the log file.\n\
244 If debug redirect is on, debug will go only to the log file."),
245 			   set_logging_redirect,
246 			   show_logging_debug_redirect,
247 			   &set_logging_cmdlist, &show_logging_cmdlist);
248 
249   /* Set/show logging file.  */
250   add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
251 Set the current logfile."), _("\
252 Show the current logfile."), _("\
253 The logfile is used when directing GDB's output."),
254 			    NULL,
255 			    show_logging_filename,
256 			    &set_logging_cmdlist, &show_logging_cmdlist);
257 
258   /* Set/show logging enabled.  */
259   set_show_commands setshow_logging_enabled_cmds
260     = add_setshow_boolean_cmd ("enabled", class_support, &logging_enabled,
261 			       _("Enable logging."),
262 			       _("Show whether logging is enabled."),
263 			       _("When on, enable logging."),
264 			       set_logging_enabled,
265 			       show_logging_enabled,
266 			       &set_logging_cmdlist, &show_logging_cmdlist);
267 
268   /* Set logging on, deprecated alias.  */
269   cmd_list_element *set_logging_on_cmd
270     = add_alias_cmd ("on", setshow_logging_enabled_cmds.set, class_support,
271 		     false, &set_logging_cmdlist);
272   deprecate_cmd (set_logging_on_cmd, "set logging enabled on");
273   set_logging_on_cmd->default_args = "on";
274 
275   /* Set logging off, deprecated alias.  */
276   cmd_list_element *set_logging_off_cmd
277     = add_alias_cmd ("off", setshow_logging_enabled_cmds.set, class_support,
278 		     false, &set_logging_cmdlist);
279   deprecate_cmd (set_logging_off_cmd, "set logging enabled off");
280   set_logging_off_cmd->default_args = "off";
281 }
282