cli-interp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* CLI Definitions for GDB, the GNU debugger.
  2. Copyright (C) 2002-2022 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 "cli-interp.h"
  16. #include "interps.h"
  17. #include "event-top.h"
  18. #include "ui-out.h"
  19. #include "cli-out.h"
  20. #include "top.h" /* for "execute_command" */
  21. #include "infrun.h"
  22. #include "observable.h"
  23. #include "gdbthread.h"
  24. #include "thread-fsm.h"
  25. #include "inferior.h"
  26. cli_interp_base::cli_interp_base (const char *name)
  27. : interp (name)
  28. {}
  29. cli_interp_base::~cli_interp_base ()
  30. {}
  31. /* The console interpreter. */
  32. class cli_interp final : public cli_interp_base
  33. {
  34. public:
  35. explicit cli_interp (const char *name);
  36. ~cli_interp ();
  37. void init (bool top_level) override;
  38. void resume () override;
  39. void suspend () override;
  40. gdb_exception exec (const char *command_str) override;
  41. ui_out *interp_ui_out () override;
  42. /* The ui_out for the console interpreter. */
  43. cli_ui_out *cli_uiout;
  44. };
  45. cli_interp::cli_interp (const char *name)
  46. : cli_interp_base (name)
  47. {
  48. /* Create a default uiout builder for the CLI. */
  49. this->cli_uiout = cli_out_new (gdb_stdout);
  50. }
  51. cli_interp::~cli_interp ()
  52. {
  53. delete cli_uiout;
  54. }
  55. /* Suppress notification struct. */
  56. struct cli_suppress_notification cli_suppress_notification;
  57. /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
  58. and returns NULL otherwise. */
  59. static struct cli_interp *
  60. as_cli_interp (struct interp *interp)
  61. {
  62. return dynamic_cast<cli_interp *> (interp);
  63. }
  64. /* Longjmp-safe wrapper for "execute_command". */
  65. static struct gdb_exception safe_execute_command (struct ui_out *uiout,
  66. const char *command,
  67. int from_tty);
  68. /* See cli-interp.h.
  69. Breakpoint hits should always be mirrored to a console. Deciding
  70. what to mirror to a console wrt to breakpoints and random stops
  71. gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
  72. clearly want to mirror the event to the console in this case. But
  73. what about more complicated cases like "s&; thread n; s&", and one
  74. of those steps spawning a new thread, and that thread hitting a
  75. breakpoint? It's impossible in general to track whether the thread
  76. had any relation to the commands that had been executed. So we
  77. just simplify and always mirror breakpoints and random events to
  78. all consoles.
  79. OTOH, we should print the source line to the console when stepping
  80. or other similar commands, iff the step was started by that console
  81. (or in MI's case, by a console command), but not if it was started
  82. with MI's -exec-step or similar. */
  83. int
  84. should_print_stop_to_console (struct interp *console_interp,
  85. struct thread_info *tp)
  86. {
  87. if ((bpstat_what (tp->control.stop_bpstat).main_action
  88. == BPSTAT_WHAT_STOP_NOISY)
  89. || tp->thread_fsm () == nullptr
  90. || tp->thread_fsm ()->command_interp == console_interp
  91. || !tp->thread_fsm ()->finished_p ())
  92. return 1;
  93. return 0;
  94. }
  95. /* Observers for several run control events. If the interpreter is
  96. quiet (i.e., another interpreter is being run with
  97. interpreter-exec), print nothing. */
  98. /* Observer for the normal_stop notification. */
  99. static void
  100. cli_on_normal_stop (struct bpstat *bs, int print_frame)
  101. {
  102. if (!print_frame)
  103. return;
  104. /* This event is suppressed. */
  105. if (cli_suppress_notification.normal_stop)
  106. return;
  107. SWITCH_THRU_ALL_UIS ()
  108. {
  109. struct interp *interp = top_level_interpreter ();
  110. struct cli_interp *cli = as_cli_interp (interp);
  111. struct thread_info *thread;
  112. if (cli == NULL)
  113. continue;
  114. thread = inferior_thread ();
  115. if (should_print_stop_to_console (interp, thread))
  116. print_stop_event (cli->cli_uiout);
  117. }
  118. }
  119. /* Observer for the signal_received notification. */
  120. static void
  121. cli_on_signal_received (enum gdb_signal siggnal)
  122. {
  123. SWITCH_THRU_ALL_UIS ()
  124. {
  125. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  126. if (cli == NULL)
  127. continue;
  128. print_signal_received_reason (cli->cli_uiout, siggnal);
  129. }
  130. }
  131. /* Observer for the end_stepping_range notification. */
  132. static void
  133. cli_on_end_stepping_range (void)
  134. {
  135. SWITCH_THRU_ALL_UIS ()
  136. {
  137. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  138. if (cli == NULL)
  139. continue;
  140. print_end_stepping_range_reason (cli->cli_uiout);
  141. }
  142. }
  143. /* Observer for the signalled notification. */
  144. static void
  145. cli_on_signal_exited (enum gdb_signal siggnal)
  146. {
  147. SWITCH_THRU_ALL_UIS ()
  148. {
  149. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  150. if (cli == NULL)
  151. continue;
  152. print_signal_exited_reason (cli->cli_uiout, siggnal);
  153. }
  154. }
  155. /* Observer for the exited notification. */
  156. static void
  157. cli_on_exited (int exitstatus)
  158. {
  159. SWITCH_THRU_ALL_UIS ()
  160. {
  161. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  162. if (cli == NULL)
  163. continue;
  164. print_exited_reason (cli->cli_uiout, exitstatus);
  165. }
  166. }
  167. /* Observer for the no_history notification. */
  168. static void
  169. cli_on_no_history (void)
  170. {
  171. SWITCH_THRU_ALL_UIS ()
  172. {
  173. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  174. if (cli == NULL)
  175. continue;
  176. print_no_history_reason (cli->cli_uiout);
  177. }
  178. }
  179. /* Observer for the sync_execution_done notification. */
  180. static void
  181. cli_on_sync_execution_done (void)
  182. {
  183. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  184. if (cli == NULL)
  185. return;
  186. display_gdb_prompt (NULL);
  187. }
  188. /* Observer for the command_error notification. */
  189. static void
  190. cli_on_command_error (void)
  191. {
  192. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  193. if (cli == NULL)
  194. return;
  195. display_gdb_prompt (NULL);
  196. }
  197. /* Observer for the user_selected_context_changed notification. */
  198. static void
  199. cli_on_user_selected_context_changed (user_selected_what selection)
  200. {
  201. /* This event is suppressed. */
  202. if (cli_suppress_notification.user_selected_context)
  203. return;
  204. thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : NULL;
  205. SWITCH_THRU_ALL_UIS ()
  206. {
  207. struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
  208. if (cli == NULL)
  209. continue;
  210. if (selection & USER_SELECTED_INFERIOR)
  211. print_selected_inferior (cli->cli_uiout);
  212. if (tp != NULL
  213. && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
  214. print_selected_thread_frame (cli->cli_uiout, selection);
  215. }
  216. }
  217. /* pre_command_loop implementation. */
  218. void
  219. cli_interp_base::pre_command_loop ()
  220. {
  221. display_gdb_prompt (0);
  222. }
  223. /* These implement the cli out interpreter: */
  224. void
  225. cli_interp::init (bool top_level)
  226. {
  227. }
  228. void
  229. cli_interp::resume ()
  230. {
  231. struct ui *ui = current_ui;
  232. struct cli_interp *cli = this;
  233. struct ui_file *stream;
  234. /*sync_execution = 1; */
  235. /* gdb_setup_readline will change gdb_stdout. If the CLI was
  236. previously writing to gdb_stdout, then set it to the new
  237. gdb_stdout afterwards. */
  238. stream = cli->cli_uiout->set_stream (gdb_stdout);
  239. if (stream != gdb_stdout)
  240. {
  241. cli->cli_uiout->set_stream (stream);
  242. stream = NULL;
  243. }
  244. gdb_setup_readline (1);
  245. ui->input_handler = command_line_handler;
  246. if (stream != NULL)
  247. cli->cli_uiout->set_stream (gdb_stdout);
  248. }
  249. void
  250. cli_interp::suspend ()
  251. {
  252. gdb_disable_readline ();
  253. }
  254. gdb_exception
  255. cli_interp::exec (const char *command_str)
  256. {
  257. struct cli_interp *cli = this;
  258. struct ui_file *old_stream;
  259. struct gdb_exception result;
  260. /* gdb_stdout could change between the time cli_uiout was
  261. initialized and now. Since we're probably using a different
  262. interpreter which has a new ui_file for gdb_stdout, use that one
  263. instead of the default.
  264. It is important that it gets reset everytime, since the user
  265. could set gdb to use a different interpreter. */
  266. old_stream = cli->cli_uiout->set_stream (gdb_stdout);
  267. result = safe_execute_command (cli->cli_uiout, command_str, 1);
  268. cli->cli_uiout->set_stream (old_stream);
  269. return result;
  270. }
  271. bool
  272. cli_interp_base::supports_command_editing ()
  273. {
  274. return true;
  275. }
  276. static struct gdb_exception
  277. safe_execute_command (struct ui_out *command_uiout, const char *command,
  278. int from_tty)
  279. {
  280. struct gdb_exception e;
  281. /* Save and override the global ``struct ui_out'' builder. */
  282. scoped_restore saved_uiout = make_scoped_restore (&current_uiout,
  283. command_uiout);
  284. try
  285. {
  286. execute_command (command, from_tty);
  287. }
  288. catch (gdb_exception &exception)
  289. {
  290. e = std::move (exception);
  291. }
  292. /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
  293. caller should print the exception. */
  294. exception_print (gdb_stderr, e);
  295. return e;
  296. }
  297. ui_out *
  298. cli_interp::interp_ui_out ()
  299. {
  300. struct cli_interp *cli = (struct cli_interp *) this;
  301. return cli->cli_uiout;
  302. }
  303. /* These hold the pushed copies of the gdb output files.
  304. If NULL then nothing has yet been pushed. */
  305. struct saved_output_files
  306. {
  307. ui_file *out;
  308. ui_file *err;
  309. ui_file *log;
  310. ui_file *targ;
  311. ui_file *targerr;
  312. ui_file_up file_to_delete;
  313. ui_file_up log_to_delete;
  314. };
  315. static std::unique_ptr<saved_output_files> saved_output;
  316. /* See cli-interp.h. */
  317. void
  318. cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
  319. bool debug_redirect)
  320. {
  321. if (logfile != nullptr)
  322. {
  323. saved_output.reset (new saved_output_files);
  324. saved_output->out = gdb_stdout;
  325. saved_output->err = gdb_stderr;
  326. saved_output->log = gdb_stdlog;
  327. saved_output->targ = gdb_stdtarg;
  328. saved_output->targerr = gdb_stdtargerr;
  329. /* If something is not being redirected, then a tee containing both the
  330. logfile and stdout. */
  331. ui_file *logfile_p = logfile.get ();
  332. ui_file *tee = nullptr;
  333. if (!logging_redirect || !debug_redirect)
  334. {
  335. tee = new tee_file (gdb_stdout, std::move (logfile));
  336. saved_output->file_to_delete.reset (tee);
  337. }
  338. else
  339. saved_output->file_to_delete = std::move (logfile);
  340. saved_output->log_to_delete.reset
  341. (new timestamped_file (debug_redirect ? logfile_p : tee));
  342. gdb_stdout = logging_redirect ? logfile_p : tee;
  343. gdb_stdlog = saved_output->log_to_delete.get ();
  344. gdb_stderr = logging_redirect ? logfile_p : tee;
  345. gdb_stdtarg = logging_redirect ? logfile_p : tee;
  346. gdb_stdtargerr = logging_redirect ? logfile_p : tee;
  347. }
  348. else
  349. {
  350. gdb_stdout = saved_output->out;
  351. gdb_stderr = saved_output->err;
  352. gdb_stdlog = saved_output->log;
  353. gdb_stdtarg = saved_output->targ;
  354. gdb_stdtargerr = saved_output->targerr;
  355. saved_output.reset (nullptr);
  356. }
  357. }
  358. /* Factory for CLI interpreters. */
  359. static struct interp *
  360. cli_interp_factory (const char *name)
  361. {
  362. return new cli_interp (name);
  363. }
  364. /* Standard gdb initialization hook. */
  365. void _initialize_cli_interp ();
  366. void
  367. _initialize_cli_interp ()
  368. {
  369. interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
  370. /* If changing this, remember to update tui-interp.c as well. */
  371. gdb::observers::normal_stop.attach (cli_on_normal_stop, "cli-interp");
  372. gdb::observers::end_stepping_range.attach (cli_on_end_stepping_range,
  373. "cli-interp");
  374. gdb::observers::signal_received.attach (cli_on_signal_received, "cli-interp");
  375. gdb::observers::signal_exited.attach (cli_on_signal_exited, "cli-interp");
  376. gdb::observers::exited.attach (cli_on_exited, "cli-interp");
  377. gdb::observers::no_history.attach (cli_on_no_history, "cli-interp");
  378. gdb::observers::sync_execution_done.attach (cli_on_sync_execution_done,
  379. "cli-interp");
  380. gdb::observers::command_error.attach (cli_on_command_error, "cli-interp");
  381. gdb::observers::user_selected_context_changed.attach
  382. (cli_on_user_selected_context_changed, "cli-interp");
  383. }