Commit | Line | Data |
---|---|---|
0fac0b41 DJ |
1 | /* Command-line output logging for GDB, the GNU debugger. |
2 | ||
61baf725 | 3 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
0fac0b41 DJ |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
0fac0b41 DJ |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
0fac0b41 DJ |
19 | |
20 | #include "defs.h" | |
21 | #include "gdbcmd.h" | |
22 | #include "ui-out.h" | |
37ce89eb | 23 | #include "interps.h" |
0fac0b41 | 24 | |
0fac0b41 DJ |
25 | /* These hold the pushed copies of the gdb output files. |
26 | If NULL then nothing has yet been pushed. */ | |
27 | struct saved_output_files | |
28 | { | |
29 | struct ui_file *out; | |
30 | struct ui_file *err; | |
31 | struct ui_file *log; | |
32 | struct ui_file *targ; | |
8d4d924b | 33 | struct ui_file *targerr; |
0fac0b41 DJ |
34 | }; |
35 | static struct saved_output_files saved_output; | |
36 | static char *saved_filename; | |
37 | ||
38 | static char *logging_filename; | |
920d2a44 AC |
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 | ||
44be957e | 47 | static int logging_overwrite; |
58b61394 JK |
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 | ||
920d2a44 AC |
57 | static void |
58 | show_logging_overwrite (struct ui_file *file, int from_tty, | |
59 | struct cmd_list_element *c, const char *value) | |
60 | { | |
9a2b4c1b MS |
61 | fprintf_filtered (file, |
62 | _("Whether logging overwrites or " | |
63 | "appends to the log file is %s.\n"), | |
920d2a44 AC |
64 | value); |
65 | } | |
66 | ||
58b61394 | 67 | /* Value as configured by the user. */ |
44be957e | 68 | static int logging_redirect; |
58b61394 | 69 | |
ebcd3b23 MS |
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. */ | |
58b61394 JK |
74 | static struct ui_file *logging_no_redirect_file; |
75 | ||
76 | static void | |
77 | set_logging_redirect (char *args, int from_tty, struct cmd_list_element *c) | |
78 | { | |
e12fefc8 | 79 | struct cleanup *cleanups; |
58b61394 | 80 | struct ui_file *output, *new_logging_no_redirect_file; |
79a45e25 | 81 | struct ui_out *uiout = current_uiout; |
58b61394 JK |
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 | ||
e12fefc8 TT |
88 | cleanups = make_cleanup (null_cleanup, NULL); |
89 | ||
58b61394 JK |
90 | if (logging_redirect != 0) |
91 | { | |
92 | gdb_assert (logging_no_redirect_file != NULL); | |
93 | ||
ebcd3b23 MS |
94 | /* ui_out_redirect still has not been called for next |
95 | gdb_stdout. */ | |
e12fefc8 | 96 | make_cleanup_ui_file_delete (gdb_stdout); |
58b61394 JK |
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", | |
103 | logging_filename); | |
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", | |
115 | logging_filename); | |
116 | } | |
117 | ||
37ce89eb SS |
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 | ||
58b61394 JK |
129 | logging_no_redirect_file = new_logging_no_redirect_file; |
130 | ||
ebcd3b23 MS |
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 | |
7becfd03 SM |
133 | first. Ideally, we should either do both the pop and push or do |
134 | neither of them. */ | |
14dba4b4 | 135 | |
7becfd03 SM |
136 | uiout->redirect (NULL); |
137 | uiout->redirect (output); | |
58b61394 | 138 | |
e12fefc8 | 139 | do_cleanups (cleanups); |
58b61394 JK |
140 | } |
141 | ||
920d2a44 AC |
142 | static void |
143 | show_logging_redirect (struct ui_file *file, int from_tty, | |
144 | struct cmd_list_element *c, const char *value) | |
145 | { | |
146 | fprintf_filtered (file, _("The logging output mode is %s.\n"), value); | |
147 | } | |
0fac0b41 DJ |
148 | |
149 | /* If we've pushed output files, close them and pop them. */ | |
150 | static void | |
83a8ccca | 151 | pop_output_files (void) |
0fac0b41 | 152 | { |
58b61394 JK |
153 | if (logging_no_redirect_file) |
154 | { | |
155 | ui_file_delete (logging_no_redirect_file); | |
156 | logging_no_redirect_file = NULL; | |
157 | } | |
37ce89eb SS |
158 | |
159 | if (current_interp_set_logging (0, NULL, NULL) == 0) | |
160 | { | |
161 | /* Only delete one of the files -- they are all set to the same | |
162 | value. */ | |
163 | ui_file_delete (gdb_stdout); | |
164 | ||
165 | gdb_stdout = saved_output.out; | |
166 | gdb_stderr = saved_output.err; | |
167 | gdb_stdlog = saved_output.log; | |
168 | gdb_stdtarg = saved_output.targ; | |
4a5be5ee | 169 | gdb_stdtargerr = saved_output.targerr; |
37ce89eb SS |
170 | } |
171 | ||
0fac0b41 DJ |
172 | saved_output.out = NULL; |
173 | saved_output.err = NULL; | |
174 | saved_output.log = NULL; | |
175 | saved_output.targ = NULL; | |
8d4d924b | 176 | saved_output.targerr = NULL; |
0fac0b41 | 177 | |
4d6cceb4 | 178 | /* Stay consistent with handle_redirections. */ |
112e8700 SM |
179 | if (!current_uiout->is_mi_like_p ()) |
180 | current_uiout->redirect (NULL); | |
0fac0b41 DJ |
181 | } |
182 | ||
183 | /* This is a helper for the `set logging' command. */ | |
184 | static void | |
185 | handle_redirections (int from_tty) | |
186 | { | |
724b958c | 187 | struct cleanup *cleanups; |
0fac0b41 | 188 | struct ui_file *output; |
37ce89eb | 189 | struct ui_file *no_redirect_file = NULL; |
0fac0b41 DJ |
190 | |
191 | if (saved_filename != NULL) | |
192 | { | |
193 | fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n", | |
194 | saved_filename); | |
195 | return; | |
196 | } | |
197 | ||
198 | output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a"); | |
199 | if (output == NULL) | |
e2e0b3e5 | 200 | perror_with_name (_("set logging")); |
724b958c | 201 | cleanups = make_cleanup_ui_file_delete (output); |
0fac0b41 DJ |
202 | |
203 | /* Redirects everything to gdb_stdout while this is running. */ | |
204 | if (!logging_redirect) | |
205 | { | |
37ce89eb | 206 | no_redirect_file = output; |
58b61394 JK |
207 | |
208 | output = tee_file_new (gdb_stdout, 0, no_redirect_file, 0); | |
0fac0b41 | 209 | if (output == NULL) |
e2e0b3e5 | 210 | perror_with_name (_("set logging")); |
58b61394 | 211 | make_cleanup_ui_file_delete (output); |
0fac0b41 DJ |
212 | if (from_tty) |
213 | fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n", | |
214 | logging_filename); | |
58b61394 JK |
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", | |
223 | logging_filename); | |
0fac0b41 | 224 | } |
0fac0b41 | 225 | |
724b958c TT |
226 | discard_cleanups (cleanups); |
227 | ||
0fac0b41 DJ |
228 | saved_filename = xstrdup (logging_filename); |
229 | saved_output.out = gdb_stdout; | |
230 | saved_output.err = gdb_stderr; | |
231 | saved_output.log = gdb_stdlog; | |
232 | saved_output.targ = gdb_stdtarg; | |
8d4d924b | 233 | saved_output.targerr = gdb_stdtargerr; |
0fac0b41 | 234 | |
37ce89eb SS |
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 | } | |
0fac0b41 | 244 | |
37ce89eb | 245 | /* Don't do the redirect for MI, it confuses MI's ui-out scheme. */ |
112e8700 | 246 | if (!current_uiout->is_mi_like_p ()) |
7becfd03 | 247 | current_uiout->redirect (output); |
0fac0b41 DJ |
248 | } |
249 | ||
250 | static void | |
251 | set_logging_on (char *args, int from_tty) | |
252 | { | |
253 | char *rest = args; | |
cdb27c12 | 254 | |
0fac0b41 DJ |
255 | if (rest && *rest) |
256 | { | |
257 | xfree (logging_filename); | |
258 | logging_filename = xstrdup (rest); | |
259 | } | |
260 | handle_redirections (from_tty); | |
261 | } | |
262 | ||
263 | static void | |
264 | set_logging_off (char *args, int from_tty) | |
265 | { | |
266 | if (saved_filename == NULL) | |
267 | return; | |
268 | ||
269 | pop_output_files (); | |
270 | if (from_tty) | |
271 | fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename); | |
272 | xfree (saved_filename); | |
273 | saved_filename = NULL; | |
274 | } | |
275 | ||
276 | static void | |
277 | set_logging_command (char *args, int from_tty) | |
278 | { | |
9a2b4c1b MS |
279 | printf_unfiltered (_("\"set logging\" lets you log output to a file.\n" |
280 | "Usage: set logging on [FILENAME]\n" | |
281 | " set logging off\n" | |
282 | " set logging file FILENAME\n" | |
283 | " set logging overwrite [on|off]\n" | |
284 | " set logging redirect [on|off]\n")); | |
0fac0b41 DJ |
285 | } |
286 | ||
2c0b251b | 287 | static void |
0fac0b41 DJ |
288 | show_logging_command (char *args, int from_tty) |
289 | { | |
290 | if (saved_filename) | |
a3f17187 | 291 | printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename); |
0fac0b41 DJ |
292 | if (saved_filename == NULL |
293 | || strcmp (logging_filename, saved_filename) != 0) | |
a3f17187 | 294 | printf_unfiltered (_("Future logs will be written to %s.\n"), |
0fac0b41 DJ |
295 | logging_filename); |
296 | ||
297 | if (logging_overwrite) | |
a3f17187 | 298 | printf_unfiltered (_("Logs will overwrite the log file.\n")); |
0fac0b41 | 299 | else |
a3f17187 | 300 | printf_unfiltered (_("Logs will be appended to the log file.\n")); |
0fac0b41 | 301 | |
58b61394 JK |
302 | if (saved_filename) |
303 | { | |
304 | if (logging_redirect) | |
305 | printf_unfiltered (_("Output is being sent only to the log file.\n")); | |
306 | else | |
307 | printf_unfiltered (_("Output is being logged and displayed.\n")); | |
308 | } | |
0fac0b41 | 309 | else |
58b61394 JK |
310 | { |
311 | if (logging_redirect) | |
312 | printf_unfiltered (_("Output will be sent only to the log file.\n")); | |
313 | else | |
314 | printf_unfiltered (_("Output will be logged and displayed.\n")); | |
315 | } | |
0fac0b41 DJ |
316 | } |
317 | ||
2c0b251b PA |
318 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
319 | extern initialize_file_ftype _initialize_cli_logging; | |
320 | ||
0fac0b41 DJ |
321 | void |
322 | _initialize_cli_logging (void) | |
323 | { | |
324 | static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist; | |
325 | ||
0fac0b41 | 326 | add_prefix_cmd ("logging", class_support, set_logging_command, |
1bedd215 | 327 | _("Set logging options"), &set_logging_cmdlist, |
0fac0b41 DJ |
328 | "set logging ", 0, &setlist); |
329 | add_prefix_cmd ("logging", class_support, show_logging_command, | |
1bedd215 | 330 | _("Show logging options"), &show_logging_cmdlist, |
0fac0b41 | 331 | "show logging ", 0, &showlist); |
7915a72c AC |
332 | add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\ |
333 | Set whether logging overwrites or appends to the log file."), _("\ | |
334 | Show whether logging overwrites or appends to the log file."), _("\ | |
335 | If set, logging overrides the log file."), | |
58b61394 | 336 | set_logging_overwrite, |
920d2a44 | 337 | show_logging_overwrite, |
2c5b56ce | 338 | &set_logging_cmdlist, &show_logging_cmdlist); |
7915a72c AC |
339 | add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\ |
340 | Set the logging output mode."), _("\ | |
341 | Show the logging output mode."), _("\ | |
3b64bf98 | 342 | If redirect is off, output will go to both the screen and the log file.\n\ |
7915a72c | 343 | If redirect is on, output will go only to the log file."), |
58b61394 | 344 | set_logging_redirect, |
920d2a44 | 345 | show_logging_redirect, |
2c5b56ce | 346 | &set_logging_cmdlist, &show_logging_cmdlist); |
7915a72c AC |
347 | add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\ |
348 | Set the current logfile."), _("\ | |
349 | Show the current logfile."), _("\ | |
350 | The logfile is used when directing GDB's output."), | |
2c5b56ce | 351 | NULL, |
920d2a44 | 352 | show_logging_filename, |
b3f42336 | 353 | &set_logging_cmdlist, &show_logging_cmdlist); |
0fac0b41 | 354 | add_cmd ("on", class_support, set_logging_on, |
1a966eab | 355 | _("Enable logging."), &set_logging_cmdlist); |
0fac0b41 | 356 | add_cmd ("off", class_support, set_logging_off, |
1a966eab | 357 | _("Disable logging."), &set_logging_cmdlist); |
0fac0b41 DJ |
358 | |
359 | logging_filename = xstrdup ("gdb.txt"); | |
360 | } |