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