gdb/cli/cli-interp.c - gdb

Global variables defined

Functions defined

Source code

  1. /* CLI Definitions for GDB, the GNU debugger.

  2.    Copyright (C) 2002-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 "interps.h"
  16. #include "event-top.h"
  17. #include "ui-out.h"
  18. #include "cli-out.h"
  19. #include "top.h"                /* for "execute_command" */
  20. #include "infrun.h"
  21. #include "observer.h"

  22. /* These are the ui_out and the interpreter for the console
  23.    interpreter.  */
  24. struct ui_out *cli_uiout;
  25. static struct interp *cli_interp;

  26. /* Longjmp-safe wrapper for "execute_command".  */
  27. static struct gdb_exception safe_execute_command (struct ui_out *uiout,
  28.                                                   char *command,
  29.                                                   int from_tty);

  30. /* Observers for several run control events.  If the interpreter is
  31.    quiet (i.e., another interpreter is being run with
  32.    interpreter-exec), print nothing.  */

  33. /* Observer for the signal_received notification.  */

  34. static void
  35. cli_on_signal_received (enum gdb_signal siggnal)
  36. {
  37.   if (!interp_quiet_p (cli_interp))
  38.     print_signal_received_reason (cli_uiout, siggnal);
  39. }

  40. /* Observer for the end_stepping_range notification.  */

  41. static void
  42. cli_on_end_stepping_range (void)
  43. {
  44.   if (!interp_quiet_p (cli_interp))
  45.     print_end_stepping_range_reason (cli_uiout);
  46. }

  47. /* Observer for the signalled notification.  */

  48. static void
  49. cli_on_signal_exited (enum gdb_signal siggnal)
  50. {
  51.   if (!interp_quiet_p (cli_interp))
  52.     print_signal_exited_reason (cli_uiout, siggnal);
  53. }

  54. /* Observer for the exited notification.  */

  55. static void
  56. cli_on_exited (int exitstatus)
  57. {
  58.   if (!interp_quiet_p (cli_interp))
  59.     print_exited_reason (cli_uiout, exitstatus);
  60. }

  61. /* Observer for the no_history notification.  */

  62. static void
  63. cli_on_no_history (void)
  64. {
  65.   if (!interp_quiet_p (cli_interp))
  66.     print_no_history_reason (cli_uiout);
  67. }

  68. /* Observer for the sync_execution_done notification.  */

  69. static void
  70. cli_on_sync_execution_done (void)
  71. {
  72.   if (!interp_quiet_p (cli_interp))
  73.     display_gdb_prompt (NULL);
  74. }

  75. /* Observer for the command_error notification.  */

  76. static void
  77. cli_on_command_error (void)
  78. {
  79.   if (!interp_quiet_p (cli_interp))
  80.     display_gdb_prompt (NULL);
  81. }

  82. /* These implement the cli out interpreter: */

  83. static void *
  84. cli_interpreter_init (struct interp *self, int top_level)
  85. {
  86.   /* If changing this, remember to update tui-interp.c as well.  */
  87.   observer_attach_end_stepping_range (cli_on_end_stepping_range);
  88.   observer_attach_signal_received (cli_on_signal_received);
  89.   observer_attach_signal_exited (cli_on_signal_exited);
  90.   observer_attach_exited (cli_on_exited);
  91.   observer_attach_no_history (cli_on_no_history);
  92.   observer_attach_sync_execution_done (cli_on_sync_execution_done);
  93.   observer_attach_command_error (cli_on_command_error);

  94.   return NULL;
  95. }

  96. static int
  97. cli_interpreter_resume (void *data)
  98. {
  99.   struct ui_file *stream;

  100.   /*sync_execution = 1; */

  101.   /* gdb_setup_readline will change gdb_stdout.  If the CLI was
  102.      previously writing to gdb_stdout, then set it to the new
  103.      gdb_stdout afterwards.  */

  104.   stream = cli_out_set_stream (cli_uiout, gdb_stdout);
  105.   if (stream != gdb_stdout)
  106.     {
  107.       cli_out_set_stream (cli_uiout, stream);
  108.       stream = NULL;
  109.     }

  110.   gdb_setup_readline ();

  111.   if (stream != NULL)
  112.     cli_out_set_stream (cli_uiout, gdb_stdout);

  113.   return 1;
  114. }

  115. static int
  116. cli_interpreter_suspend (void *data)
  117. {
  118.   gdb_disable_readline ();
  119.   return 1;
  120. }

  121. static struct gdb_exception
  122. cli_interpreter_exec (void *data, const char *command_str)
  123. {
  124.   struct ui_file *old_stream;
  125.   struct gdb_exception result;

  126.   /* FIXME: cagney/2003-02-01: Need to const char *propogate
  127.      safe_execute_command.  */
  128.   char *str = strcpy (alloca (strlen (command_str) + 1), command_str);

  129.   /* gdb_stdout could change between the time cli_uiout was
  130.      initialized and now.  Since we're probably using a different
  131.      interpreter which has a new ui_file for gdb_stdout, use that one
  132.      instead of the default.

  133.      It is important that it gets reset everytime, since the user
  134.      could set gdb to use a different interpreter.  */
  135.   old_stream = cli_out_set_stream (cli_uiout, gdb_stdout);
  136.   result = safe_execute_command (cli_uiout, str, 1);
  137.   cli_out_set_stream (cli_uiout, old_stream);
  138.   return result;
  139. }

  140. static struct gdb_exception
  141. safe_execute_command (struct ui_out *command_uiout, char *command, int from_tty)
  142. {
  143.   volatile struct gdb_exception e;
  144.   struct ui_out *saved_uiout;

  145.   /* Save and override the global ``struct ui_out'' builder.  */
  146.   saved_uiout = current_uiout;
  147.   current_uiout = command_uiout;

  148.   TRY_CATCH (e, RETURN_MASK_ALL)
  149.     {
  150.       execute_command (command, from_tty);
  151.     }

  152.   /* Restore the global builder.  */
  153.   current_uiout = saved_uiout;

  154.   /* FIXME: cagney/2005-01-13: This shouldn't be needed.  Instead the
  155.      caller should print the exception.  */
  156.   exception_print (gdb_stderr, e);
  157.   return e;
  158. }

  159. static struct ui_out *
  160. cli_ui_out (struct interp *self)
  161. {
  162.   return cli_uiout;
  163. }

  164. /* Standard gdb initialization hook.  */
  165. extern initialize_file_ftype _initialize_cli_interp; /* -Wmissing-prototypes */

  166. void
  167. _initialize_cli_interp (void)
  168. {
  169.   static const struct interp_procs procs = {
  170.     cli_interpreter_init,        /* init_proc */
  171.     cli_interpreter_resume,        /* resume_proc */
  172.     cli_interpreter_suspend,        /* suspend_proc */
  173.     cli_interpreter_exec,        /* exec_proc */
  174.     cli_ui_out,                        /* ui_out_proc */
  175.     NULL,                       /* set_logging_proc */
  176.     cli_command_loop            /* command_loop_proc */
  177.   };

  178.   /* Create a default uiout builder for the CLI.  */
  179.   cli_uiout = cli_out_new (gdb_stdout);
  180.   cli_interp = interp_new (INTERP_CONSOLE, &procs);

  181.   interp_add (cli_interp);
  182. }