gdb/cli/cli-logging.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Command-line output logging for GDB, the GNU debugger.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcmd.h"
  16. #include "ui-out.h"
  17. #include "interps.h"

  18. /* These hold the pushed copies of the gdb output files.
  19.    If NULL then nothing has yet been pushed.  */
  20. struct saved_output_files
  21. {
  22.   struct ui_file *out;
  23.   struct ui_file *err;
  24.   struct ui_file *log;
  25.   struct ui_file *targ;
  26.   struct ui_file *targerr;
  27. };
  28. static struct saved_output_files saved_output;
  29. static char *saved_filename;

  30. static char *logging_filename;
  31. static void
  32. show_logging_filename (struct ui_file *file, int from_tty,
  33.                        struct cmd_list_element *c, const char *value)
  34. {
  35.   fprintf_filtered (file, _("The current logfile is \"%s\".\n"),
  36.                     value);
  37. }

  38. static int logging_overwrite;

  39. static void
  40. set_logging_overwrite (char *args, int from_tty, struct cmd_list_element *c)
  41. {
  42.   if (saved_filename)
  43.     warning (_("Currently logging to %s.  Turn the logging off and on to "
  44.                "make the new setting effective."), saved_filename);
  45. }

  46. static void
  47. show_logging_overwrite (struct ui_file *file, int from_tty,
  48.                         struct cmd_list_element *c, const char *value)
  49. {
  50.   fprintf_filtered (file,
  51.                     _("Whether logging overwrites or "
  52.                       "appends to the log file is %s.\n"),
  53.                     value);
  54. }

  55. /* Value as configured by the user.  */
  56. static int logging_redirect;

  57. /* The on-disk file in use if logging is currently active together
  58.    with redirection turned off (and therefore using tee_file_new).
  59.    For active logging with redirection the on-disk file is directly in
  60.    GDB_STDOUT and this variable is NULL.  */
  61. static struct ui_file *logging_no_redirect_file;

  62. static void
  63. set_logging_redirect (char *args, int from_tty, struct cmd_list_element *c)
  64. {
  65.   struct cleanup *cleanups;
  66.   struct ui_file *output, *new_logging_no_redirect_file;
  67.   struct ui_out *uiout = current_uiout;

  68.   if (saved_filename == NULL
  69.       || (logging_redirect != 0 && logging_no_redirect_file == NULL)
  70.       || (logging_redirect == 0 && logging_no_redirect_file != NULL))
  71.     return;

  72.   cleanups = make_cleanup (null_cleanup, NULL);

  73.   if (logging_redirect != 0)
  74.     {
  75.       gdb_assert (logging_no_redirect_file != NULL);

  76.       /* ui_out_redirect still has not been called for next
  77.          gdb_stdout.  */
  78.       make_cleanup_ui_file_delete (gdb_stdout);

  79.       output = logging_no_redirect_file;
  80.       new_logging_no_redirect_file = NULL;

  81.       if (from_tty)
  82.         fprintf_unfiltered (saved_output.out, "Redirecting output to %s.\n",
  83.                             logging_filename);
  84.     }
  85.   else
  86.     {
  87.       gdb_assert (logging_no_redirect_file == NULL);
  88.       output = tee_file_new (saved_output.out, 0, gdb_stdout, 0);
  89.       if (output == NULL)
  90.         perror_with_name (_("set logging"));
  91.       new_logging_no_redirect_file = gdb_stdout;

  92.       if (from_tty)
  93.         fprintf_unfiltered (saved_output.out, "Copying output to %s.\n",
  94.                             logging_filename);
  95.     }

  96.   /* Give the current interpreter a chance to do anything special that
  97.      it might need for logging, such as updating other channels.  */
  98.   if (current_interp_set_logging (1, output, NULL) == 0)
  99.     {
  100.       gdb_stdout = output;
  101.       gdb_stdlog = output;
  102.       gdb_stderr = output;
  103.       gdb_stdtarg = output;
  104.       gdb_stdtargerr = output;
  105.     }

  106.   logging_no_redirect_file = new_logging_no_redirect_file;

  107.   /* There is a former output pushed on the ui_out_redirect stack.  We
  108.      want to replace it by OUTPUT so we must pop the former value
  109.      first.  We should either do both the pop and push or to do
  110.      neither of it.  At least do not try to push OUTPUT if the pop
  111.      already failed.  */

  112.   if (ui_out_redirect (uiout, NULL) < 0
  113.       || ui_out_redirect (uiout, output) < 0)
  114.     warning (_("Current output protocol does not support redirection"));

  115.   do_cleanups (cleanups);
  116. }

  117. static void
  118. show_logging_redirect (struct ui_file *file, int from_tty,
  119.                        struct cmd_list_element *c, const char *value)
  120. {
  121.   fprintf_filtered (file, _("The logging output mode is %s.\n"), value);
  122. }

  123. /* If we've pushed output files, close them and pop them.  */
  124. static void
  125. pop_output_files (void)
  126. {
  127.   if (logging_no_redirect_file)
  128.     {
  129.       ui_file_delete (logging_no_redirect_file);
  130.       logging_no_redirect_file = NULL;
  131.     }

  132.   if (current_interp_set_logging (0, NULL, NULL) == 0)
  133.     {
  134.       /* Only delete one of the files -- they are all set to the same
  135.          value.  */
  136.       ui_file_delete (gdb_stdout);

  137.       gdb_stdout = saved_output.out;
  138.       gdb_stderr = saved_output.err;
  139.       gdb_stdlog = saved_output.log;
  140.       gdb_stdtarg = saved_output.targ;
  141.       gdb_stdtargerr = saved_output.targerr;
  142.     }

  143.   saved_output.out = NULL;
  144.   saved_output.err = NULL;
  145.   saved_output.log = NULL;
  146.   saved_output.targ = NULL;
  147.   saved_output.targerr = NULL;

  148.   ui_out_redirect (current_uiout, NULL);
  149. }

  150. /* This is a helper for the `set logging' command.  */
  151. static void
  152. handle_redirections (int from_tty)
  153. {
  154.   struct cleanup *cleanups;
  155.   struct ui_file *output;
  156.   struct ui_file *no_redirect_file = NULL;

  157.   if (saved_filename != NULL)
  158.     {
  159.       fprintf_unfiltered (gdb_stdout, "Already logging to %s.\n",
  160.                           saved_filename);
  161.       return;
  162.     }

  163.   output = gdb_fopen (logging_filename, logging_overwrite ? "w" : "a");
  164.   if (output == NULL)
  165.     perror_with_name (_("set logging"));
  166.   cleanups = make_cleanup_ui_file_delete (output);

  167.   /* Redirects everything to gdb_stdout while this is running.  */
  168.   if (!logging_redirect)
  169.     {
  170.       no_redirect_file = output;

  171.       output = tee_file_new (gdb_stdout, 0, no_redirect_file, 0);
  172.       if (output == NULL)
  173.         perror_with_name (_("set logging"));
  174.       make_cleanup_ui_file_delete (output);
  175.       if (from_tty)
  176.         fprintf_unfiltered (gdb_stdout, "Copying output to %s.\n",
  177.                             logging_filename);
  178.       logging_no_redirect_file = no_redirect_file;
  179.     }
  180.   else
  181.     {
  182.       gdb_assert (logging_no_redirect_file == NULL);

  183.       if (from_tty)
  184.         fprintf_unfiltered (gdb_stdout, "Redirecting output to %s.\n",
  185.                             logging_filename);
  186.     }

  187.   discard_cleanups (cleanups);

  188.   saved_filename = xstrdup (logging_filename);
  189.   saved_output.out = gdb_stdout;
  190.   saved_output.err = gdb_stderr;
  191.   saved_output.log = gdb_stdlog;
  192.   saved_output.targ = gdb_stdtarg;
  193.   saved_output.targerr = gdb_stdtargerr;

  194.   /* Let the interpreter do anything it needs.  */
  195.   if (current_interp_set_logging (1, output, no_redirect_file) == 0)
  196.     {
  197.       gdb_stdout = output;
  198.       gdb_stdlog = output;
  199.       gdb_stderr = output;
  200.       gdb_stdtarg = output;
  201.       gdb_stdtargerr = output;
  202.     }

  203.   /* Don't do the redirect for MI, it confuses MI's ui-out scheme.  */
  204.   if (!ui_out_is_mi_like_p (current_uiout))
  205.     {
  206.       if (ui_out_redirect (current_uiout, output) < 0)
  207.         warning (_("Current output protocol does not support redirection"));
  208.     }
  209. }

  210. static void
  211. set_logging_on (char *args, int from_tty)
  212. {
  213.   char *rest = args;

  214.   if (rest && *rest)
  215.     {
  216.       xfree (logging_filename);
  217.       logging_filename = xstrdup (rest);
  218.     }
  219.   handle_redirections (from_tty);
  220. }

  221. static void
  222. set_logging_off (char *args, int from_tty)
  223. {
  224.   if (saved_filename == NULL)
  225.     return;

  226.   pop_output_files ();
  227.   if (from_tty)
  228.     fprintf_unfiltered (gdb_stdout, "Done logging to %s.\n", saved_filename);
  229.   xfree (saved_filename);
  230.   saved_filename = NULL;
  231. }

  232. static void
  233. set_logging_command (char *args, int from_tty)
  234. {
  235.   printf_unfiltered (_("\"set logging\" lets you log output to a file.\n"
  236.                        "Usage: set logging on [FILENAME]\n"
  237.                        "       set logging off\n"
  238.                        "       set logging file FILENAME\n"
  239.                        "       set logging overwrite [on|off]\n"
  240.                        "       set logging redirect [on|off]\n"));
  241. }

  242. static void
  243. show_logging_command (char *args, int from_tty)
  244. {
  245.   if (saved_filename)
  246.     printf_unfiltered (_("Currently logging to \"%s\".\n"), saved_filename);
  247.   if (saved_filename == NULL
  248.       || strcmp (logging_filename, saved_filename) != 0)
  249.     printf_unfiltered (_("Future logs will be written to %s.\n"),
  250.                        logging_filename);

  251.   if (logging_overwrite)
  252.     printf_unfiltered (_("Logs will overwrite the log file.\n"));
  253.   else
  254.     printf_unfiltered (_("Logs will be appended to the log file.\n"));

  255.   if (saved_filename)
  256.     {
  257.       if (logging_redirect)
  258.         printf_unfiltered (_("Output is being sent only to the log file.\n"));
  259.       else
  260.         printf_unfiltered (_("Output is being logged and displayed.\n"));
  261.     }
  262.   else
  263.     {
  264.       if (logging_redirect)
  265.         printf_unfiltered (_("Output will be sent only to the log file.\n"));
  266.       else
  267.         printf_unfiltered (_("Output will be logged and displayed.\n"));
  268.     }
  269. }

  270. /* Provide a prototype to silence -Wmissing-prototypes.  */
  271. extern initialize_file_ftype _initialize_cli_logging;

  272. void
  273. _initialize_cli_logging (void)
  274. {
  275.   static struct cmd_list_element *set_logging_cmdlist, *show_logging_cmdlist;

  276.   add_prefix_cmd ("logging", class_support, set_logging_command,
  277.                   _("Set logging options"), &set_logging_cmdlist,
  278.                   "set logging ", 0, &setlist);
  279.   add_prefix_cmd ("logging", class_support, show_logging_command,
  280.                   _("Show logging options"), &show_logging_cmdlist,
  281.                   "show logging ", 0, &showlist);
  282.   add_setshow_boolean_cmd ("overwrite", class_support, &logging_overwrite, _("\
  283. Set whether logging overwrites or appends to the log file."), _("\
  284. Show whether logging overwrites or appends to the log file."), _("\
  285. If set, logging overrides the log file."),
  286.                            set_logging_overwrite,
  287.                            show_logging_overwrite,
  288.                            &set_logging_cmdlist, &show_logging_cmdlist);
  289.   add_setshow_boolean_cmd ("redirect", class_support, &logging_redirect, _("\
  290. Set the logging output mode."), _("\
  291. Show the logging output mode."), _("\
  292. If redirect is off, output will go to both the screen and the log file.\n\
  293. If redirect is on, output will go only to the log file."),
  294.                            set_logging_redirect,
  295.                            show_logging_redirect,
  296.                            &set_logging_cmdlist, &show_logging_cmdlist);
  297.   add_setshow_filename_cmd ("file", class_support, &logging_filename, _("\
  298. Set the current logfile."), _("\
  299. Show the current logfile."), _("\
  300. The logfile is used when directing GDB's output."),
  301.                             NULL,
  302.                             show_logging_filename,
  303.                             &set_logging_cmdlist, &show_logging_cmdlist);
  304.   add_cmd ("on", class_support, set_logging_on,
  305.            _("Enable logging."), &set_logging_cmdlist);
  306.   add_cmd ("off", class_support, set_logging_off,
  307.            _("Disable logging."), &set_logging_cmdlist);

  308.   logging_filename = xstrdup ("gdb.txt");
  309. }