xref: /dragonfly/contrib/gdb-7/gdb/cli/cli-logging.c (revision e5a92d33)
1 /* Command-line output logging for GDB, the GNU debugger.
2 
3    Copyright (C) 2003-2013 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 "gdb_assert.h"
25 
26 #include "gdb_string.h"
27 
28 /* These hold the pushed copies of the gdb output files.
29    If NULL then nothing has yet been pushed.  */
30 struct saved_output_files
31 {
32   struct ui_file *out;
33   struct ui_file *err;
34   struct ui_file *log;
35   struct ui_file *targ;
36   struct ui_file *targerr;
37 };
38 static struct saved_output_files saved_output;
39 static char *saved_filename;
40 
41 static char *logging_filename;
42 static void
43 show_logging_filename (struct ui_file *file, int from_tty,
44 		       struct cmd_list_element *c, const char *value)
45 {
46   fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
47 		    value);
48 }
49 
50 static int logging_overwrite;
51 
52 static void
53 set_logging_overwrite (char *args, int from_tty, struct cmd_list_element *c)
54 {
55   if (saved_filename)
56     warning (_("Currently logging to %s.  Turn the logging off and on to "
57 	       "make the new setting effective."), saved_filename);
58 }
59 
60 static void
61 show_logging_overwrite (struct ui_file *file, int from_tty,
62 			struct cmd_list_element *c, const char *value)
63 {
64   fprintf_filtered (file,
65 		    _("Whether logging overwrites or "
66 		      "appends to the log file is %s.\n"),
67 		    value);
68 }
69 
70 /* Value as configured by the user.  */
71 static int logging_redirect;
72 
73 /* The on-disk file in use if logging is currently active together
74    with redirection turned off (and therefore using tee_file_new).
75    For active logging with redirection the on-disk file is directly in
76    GDB_STDOUT and this variable is NULL.  */
77 static struct ui_file *logging_no_redirect_file;
78 
79 static void
80 set_logging_redirect (char *args, int from_tty, struct cmd_list_element *c)
81 {
82   struct cleanup *cleanups = NULL;
83   struct ui_file *output, *new_logging_no_redirect_file;
84   struct ui_out *uiout = current_uiout;
85 
86   if (saved_filename == NULL
87       || (logging_redirect != 0 && logging_no_redirect_file == NULL)
88       || (logging_redirect == 0 && logging_no_redirect_file != NULL))
89     return;
90 
91   if (logging_redirect != 0)
92     {
93       gdb_assert (logging_no_redirect_file != NULL);
94 
95       /* ui_out_redirect still has not been called for next
96 	 gdb_stdout.  */
97       cleanups = make_cleanup_ui_file_delete (gdb_stdout);
98 
99       output = logging_no_redirect_file;
100       new_logging_no_redirect_file = NULL;
101 
102       if (from_tty)
103 	fprintf_unfiltered (saved_output.out, "Redirecting output to %s.\n",
104 			    logging_filename);
105     }
106   else
107     {
108       gdb_assert (logging_no_redirect_file == NULL);
109       output = tee_file_new (saved_output.out, 0, gdb_stdout, 0);
110       if (output == NULL)
111 	perror_with_name (_("set logging"));
112       new_logging_no_redirect_file = gdb_stdout;
113 
114       if (from_tty)
115 	fprintf_unfiltered (saved_output.out, "Copying output to %s.\n",
116 			    logging_filename);
117     }
118 
119   /* Give the current interpreter a chance to do anything special that
120      it might need for logging, such as updating other channels.  */
121   if (current_interp_set_logging (1, output, NULL) == 0)
122     {
123       gdb_stdout = output;
124       gdb_stdlog = output;
125       gdb_stderr = output;
126       gdb_stdtarg = output;
127       gdb_stdtargerr = output;
128     }
129 
130   logging_no_redirect_file = new_logging_no_redirect_file;
131 
132   /* There is a former output pushed on the ui_out_redirect stack.  We
133      want to replace it by OUTPUT so we must pop the former value
134      first.  We should either do both the pop and push or to do
135      neither of it.  At least do not try to push OUTPUT if the pop
136      already failed.  */
137 
138   if (ui_out_redirect (uiout, NULL) < 0
139       || ui_out_redirect (uiout, output) < 0)
140     warning (_("Current output protocol does not support redirection"));
141 
142   if (logging_redirect != 0)
143     do_cleanups (cleanups);
144 }
145 
146 static void
147 show_logging_redirect (struct ui_file *file, int from_tty,
148 		       struct cmd_list_element *c, const char *value)
149 {
150   fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
151 }
152 
153 /* If we've pushed output files, close them and pop them.  */
154 static void
155 pop_output_files (void)
156 {
157   if (logging_no_redirect_file)
158     {
159       ui_file_delete (logging_no_redirect_file);
160       logging_no_redirect_file = NULL;
161     }
162 
163   if (current_interp_set_logging (0, NULL, NULL) == 0)
164     {
165       /* Only delete one of the files -- they are all set to the same
166 	 value.  */
167       ui_file_delete (gdb_stdout);
168 
169       gdb_stdout = saved_output.out;
170       gdb_stderr = saved_output.err;
171       gdb_stdlog = saved_output.log;
172       gdb_stdtarg = saved_output.targ;
173       gdb_stdtargerr = saved_output.targ;
174     }
175 
176   saved_output.out = NULL;
177   saved_output.err = NULL;
178   saved_output.log = NULL;
179   saved_output.targ = NULL;
180   saved_output.targerr = NULL;
181 
182   ui_out_redirect (current_uiout, NULL);
183 }
184 
185 /* This is a helper for the `set logging' command.  */
186 static void
187 handle_redirections (int from_tty)
188 {
189   struct cleanup *cleanups;
190   struct ui_file *output;
191   struct ui_file *no_redirect_file = NULL;
192 
193   if (saved_filename != NULL)
194     {
195       fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
196 			  saved_filename);
197       return;
198     }
199 
200   output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
201   if (output == NULL)
202     perror_with_name (_("set logging"));
203   cleanups = make_cleanup_ui_file_delete (output);
204 
205   /* Redirects everything to gdb_stdout while this is running.  */
206   if (!logging_redirect)
207     {
208       no_redirect_file = output;
209 
210       output = tee_file_new (gdb_stdout, 0, no_redirect_file, 0);
211       if (output == NULL)
212 	perror_with_name (_("set logging"));
213       make_cleanup_ui_file_delete (output);
214       if (from_tty)
215 	fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
216 			    logging_filename);
217       logging_no_redirect_file = no_redirect_file;
218     }
219   else
220     {
221       gdb_assert (logging_no_redirect_file == NULL);
222 
223       if (from_tty)
224 	fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
225 			    logging_filename);
226     }
227 
228   discard_cleanups (cleanups);
229 
230   saved_filename = xstrdup (logging_filename);
231   saved_output.out = gdb_stdout;
232   saved_output.err = gdb_stderr;
233   saved_output.log = gdb_stdlog;
234   saved_output.targ = gdb_stdtarg;
235   saved_output.targerr = gdb_stdtargerr;
236 
237   /* Let the interpreter do anything it needs.  */
238   if (current_interp_set_logging (1, output, no_redirect_file) == 0)
239     {
240       gdb_stdout = output;
241       gdb_stdlog = output;
242       gdb_stderr = output;
243       gdb_stdtarg = output;
244       gdb_stdtargerr = output;
245     }
246 
247   /* Don't do the redirect for MI, it confuses MI's ui-out scheme.  */
248   if (!ui_out_is_mi_like_p (current_uiout))
249     {
250       if (ui_out_redirect (current_uiout, output) < 0)
251 	warning (_("Current output protocol does not support redirection"));
252     }
253 }
254 
255 static void
256 set_logging_on (char *args, int from_tty)
257 {
258   char *rest = args;
259 
260   if (rest && *rest)
261     {
262       xfree (logging_filename);
263       logging_filename = xstrdup (rest);
264     }
265   handle_redirections (from_tty);
266 }
267 
268 static void
269 set_logging_off (char *args, int from_tty)
270 {
271   if (saved_filename == NULL)
272     return;
273 
274   pop_output_files ();
275   if (from_tty)
276     fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
277   xfree (saved_filename);
278   saved_filename = NULL;
279 }
280 
281 static void
282 set_logging_command (char *args, int from_tty)
283 {
284   printf_unfiltered (_("\"set logging\" lets you log output to a file.\n"
285 		       "Usage: set logging on [FILENAME]\n"
286 		       "       set logging off\n"
287 		       "       set logging file FILENAME\n"
288 		       "       set logging overwrite [on|off]\n"
289 		       "       set logging redirect [on|off]\n"));
290 }
291 
292 static void
293 show_logging_command (char *args, int from_tty)
294 {
295   if (saved_filename)
296     printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
297   if (saved_filename == NULL
298       || strcmp (logging_filename, saved_filename) != 0)
299     printf_unfiltered (_("Future logs will be written to %s.\n"),
300 		       logging_filename);
301 
302   if (logging_overwrite)
303     printf_unfiltered (_("Logs will overwrite the log file.\n"));
304   else
305     printf_unfiltered (_("Logs will be appended to the log file.\n"));
306 
307   if (saved_filename)
308     {
309       if (logging_redirect)
310 	printf_unfiltered (_("Output is being sent only to the log file.\n"));
311       else
312 	printf_unfiltered (_("Output is being logged and displayed.\n"));
313     }
314   else
315     {
316       if (logging_redirect)
317 	printf_unfiltered (_("Output will be sent only to the log file.\n"));
318       else
319 	printf_unfiltered (_("Output will be logged and displayed.\n"));
320     }
321 }
322 
323 /* Provide a prototype to silence -Wmissing-prototypes.  */
324 extern initialize_file_ftype _initialize_cli_logging;
325 
326 void
327 _initialize_cli_logging (void)
328 {
329   static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;
330 
331   add_prefix_cmd ("logging", class_support, set_logging_command,
332 		  _("Set logging options"), &set_logging_cmdlist,
333 		  "set logging ", 0, &setlist);
334   add_prefix_cmd ("logging", class_support, show_logging_command,
335 		  _("Show logging options"), &show_logging_cmdlist,
336 		  "show logging ", 0, &showlist);
337   add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
338 Set whether logging overwrites or appends to the log file."), _("\
339 Show whether logging overwrites or appends to the log file."), _("\
340 If set, logging overrides the log file."),
341 			   set_logging_overwrite,
342 			   show_logging_overwrite,
343 			   &set_logging_cmdlist, &show_logging_cmdlist);
344   add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
345 Set the logging output mode."), _("\
346 Show the logging output mode."), _("\
347 If redirect is off, output will go to both the screen and the log file.\n\
348 If redirect is on, output will go only to the log file."),
349 			   set_logging_redirect,
350 			   show_logging_redirect,
351 			   &set_logging_cmdlist, &show_logging_cmdlist);
352   add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
353 Set the current logfile."), _("\
354 Show the current logfile."), _("\
355 The logfile is used when directing GDB's output."),
356 			    NULL,
357 			    show_logging_filename,
358 			    &set_logging_cmdlist, &show_logging_cmdlist);
359   add_cmd ("on", class_support, set_logging_on,
360 	   _("Enable logging."), &set_logging_cmdlist);
361   add_cmd ("off", class_support, set_logging_off,
362 	   _("Disable logging."), &set_logging_cmdlist);
363 
364   logging_filename = xstrdup ("gdb.txt");
365 }
366