event-top.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. /* Top level stuff for GDB, the GNU debugger.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "top.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "target.h"
  20. #include "terminal.h"
  21. #include "gdbsupport/event-loop.h"
  22. #include "event-top.h"
  23. #include "interps.h"
  24. #include <signal.h>
  25. #include "cli/cli-script.h" /* for reset_command_nest_depth */
  26. #include "main.h"
  27. #include "gdbthread.h"
  28. #include "observable.h"
  29. #include "gdbcmd.h" /* for dont_repeat() */
  30. #include "annotate.h"
  31. #include "maint.h"
  32. #include "gdbsupport/buffer.h"
  33. #include "ser-event.h"
  34. #include "gdbsupport/gdb_select.h"
  35. #include "gdbsupport/gdb-sigmask.h"
  36. #include "async-event.h"
  37. #include "bt-utils.h"
  38. #include "pager.h"
  39. /* readline include files. */
  40. #include "readline/readline.h"
  41. #include "readline/history.h"
  42. /* readline defines this. */
  43. #undef savestring
  44. static std::string top_level_prompt ();
  45. /* Signal handlers. */
  46. #ifdef SIGQUIT
  47. static void handle_sigquit (int sig);
  48. #endif
  49. #ifdef SIGHUP
  50. static void handle_sighup (int sig);
  51. #endif
  52. /* Functions to be invoked by the event loop in response to
  53. signals. */
  54. #if defined (SIGQUIT) || defined (SIGHUP)
  55. static void async_do_nothing (gdb_client_data);
  56. #endif
  57. #ifdef SIGHUP
  58. static void async_disconnect (gdb_client_data);
  59. #endif
  60. #ifdef SIGTSTP
  61. static void async_sigtstp_handler (gdb_client_data);
  62. #endif
  63. static void async_sigterm_handler (gdb_client_data arg);
  64. /* Instead of invoking (and waiting for) readline to read the command
  65. line and pass it back for processing, we use readline's alternate
  66. interface, via callback functions, so that the event loop can react
  67. to other event sources while we wait for input. */
  68. /* Important variables for the event loop. */
  69. /* This is used to determine if GDB is using the readline library or
  70. its own simplified form of readline. It is used by the asynchronous
  71. form of the set editing command.
  72. ezannoni: as of 1999-04-29 I expect that this
  73. variable will not be used after gdb is changed to use the event
  74. loop as default engine, and event-top.c is merged into top.c. */
  75. bool set_editing_cmd_var;
  76. /* This is used to display the notification of the completion of an
  77. asynchronous execution command. */
  78. bool exec_done_display_p = false;
  79. /* Used by the stdin event handler to compensate for missed stdin events.
  80. Setting this to a non-zero value inside an stdin callback makes the callback
  81. run again. */
  82. int call_stdin_event_handler_again_p;
  83. /* When true GDB will produce a minimal backtrace when a fatal signal is
  84. reached (within GDB code). */
  85. static bool bt_on_fatal_signal = GDB_PRINT_INTERNAL_BACKTRACE_INIT_ON;
  86. /* Implement 'maintenance show backtrace-on-fatal-signal'. */
  87. static void
  88. show_bt_on_fatal_signal (struct ui_file *file, int from_tty,
  89. struct cmd_list_element *cmd, const char *value)
  90. {
  91. gdb_printf (file, _("Backtrace on a fatal signal is %s.\n"), value);
  92. }
  93. /* Signal handling variables. */
  94. /* Each of these is a pointer to a function that the event loop will
  95. invoke if the corresponding signal has received. The real signal
  96. handlers mark these functions as ready to be executed and the event
  97. loop, in a later iteration, calls them. See the function
  98. invoke_async_signal_handler. */
  99. static struct async_signal_handler *sigint_token;
  100. #ifdef SIGHUP
  101. static struct async_signal_handler *sighup_token;
  102. #endif
  103. #ifdef SIGQUIT
  104. static struct async_signal_handler *sigquit_token;
  105. #endif
  106. #ifdef SIGTSTP
  107. static struct async_signal_handler *sigtstp_token;
  108. #endif
  109. static struct async_signal_handler *async_sigterm_token;
  110. /* This hook is called by gdb_rl_callback_read_char_wrapper after each
  111. character is processed. */
  112. void (*after_char_processing_hook) (void);
  113. /* Wrapper function for calling into the readline library. This takes
  114. care of a couple things:
  115. - The event loop expects the callback function to have a parameter,
  116. while readline expects none.
  117. - Propagation of GDB exceptions/errors thrown from INPUT_HANDLER
  118. across readline requires special handling.
  119. On the exceptions issue:
  120. DWARF-based unwinding cannot cross code built without -fexceptions.
  121. Any exception that tries to propagate through such code will fail
  122. and the result is a call to std::terminate. While some ABIs, such
  123. as x86-64, require all code to be built with exception tables,
  124. others don't.
  125. This is a problem when GDB calls some non-EH-aware C library code,
  126. that calls into GDB again through a callback, and that GDB callback
  127. code throws a C++ exception. Turns out this is exactly what
  128. happens with GDB's readline callback.
  129. In such cases, we must catch and save any C++ exception that might
  130. be thrown from the GDB callback before returning to the
  131. non-EH-aware code. When the non-EH-aware function itself returns
  132. back to GDB, we then rethrow the original C++ exception.
  133. In the readline case however, the right thing to do is to longjmp
  134. out of the callback, rather than do a normal return -- there's no
  135. way for the callback to return to readline an indication that an
  136. error happened, so a normal return would have rl_callback_read_char
  137. potentially continue processing further input, redisplay the
  138. prompt, etc. Instead of raw setjmp/longjmp however, we use our
  139. sjlj-based TRY/CATCH mechanism, which knows to handle multiple
  140. levels of active setjmp/longjmp frames, needed in order to handle
  141. the readline callback recursing, as happens with e.g., secondary
  142. prompts / queries, through gdb_readline_wrapper. This must be
  143. noexcept in order to avoid problems with mixing sjlj and
  144. (sjlj-based) C++ exceptions. */
  145. static struct gdb_exception
  146. gdb_rl_callback_read_char_wrapper_noexcept () noexcept
  147. {
  148. struct gdb_exception gdb_expt;
  149. /* C++ exceptions can't normally be thrown across readline (unless
  150. it is built with -fexceptions, but it won't by default on many
  151. ABIs). So we instead wrap the readline call with a sjlj-based
  152. TRY/CATCH, and rethrow the GDB exception once back in GDB. */
  153. TRY_SJLJ
  154. {
  155. rl_callback_read_char ();
  156. if (after_char_processing_hook)
  157. (*after_char_processing_hook) ();
  158. }
  159. CATCH_SJLJ (ex, RETURN_MASK_ALL)
  160. {
  161. gdb_expt = std::move (ex);
  162. }
  163. END_CATCH_SJLJ
  164. return gdb_expt;
  165. }
  166. static void
  167. gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
  168. {
  169. struct gdb_exception gdb_expt
  170. = gdb_rl_callback_read_char_wrapper_noexcept ();
  171. /* Rethrow using the normal EH mechanism. */
  172. if (gdb_expt.reason < 0)
  173. throw_exception (std::move (gdb_expt));
  174. }
  175. /* GDB's readline callback handler. Calls the current INPUT_HANDLER,
  176. and propagates GDB exceptions/errors thrown from INPUT_HANDLER back
  177. across readline. See gdb_rl_callback_read_char_wrapper. This must
  178. be noexcept in order to avoid problems with mixing sjlj and
  179. (sjlj-based) C++ exceptions. */
  180. static void
  181. gdb_rl_callback_handler (char *rl) noexcept
  182. {
  183. /* This is static to avoid undefined behavior when calling longjmp
  184. -- gdb_exception has a destructor with side effects. */
  185. static struct gdb_exception gdb_rl_expt;
  186. struct ui *ui = current_ui;
  187. try
  188. {
  189. /* Ensure the exception is reset on each call. */
  190. gdb_rl_expt = {};
  191. ui->input_handler (gdb::unique_xmalloc_ptr<char> (rl));
  192. }
  193. catch (gdb_exception &ex)
  194. {
  195. gdb_rl_expt = std::move (ex);
  196. }
  197. /* If we caught a GDB exception, longjmp out of the readline
  198. callback. There's no other way for the callback to signal to
  199. readline that an error happened. A normal return would have
  200. readline potentially continue processing further input, redisplay
  201. the prompt, etc. (This is what GDB historically did when it was
  202. a C program.) Note that since we're long jumping, local variable
  203. dtors are NOT run automatically. */
  204. if (gdb_rl_expt.reason < 0)
  205. throw_exception_sjlj (gdb_rl_expt);
  206. }
  207. /* Change the function to be invoked every time there is a character
  208. ready on stdin. This is used when the user sets the editing off,
  209. therefore bypassing readline, and letting gdb handle the input
  210. itself, via gdb_readline_no_editing_callback. Also it is used in
  211. the opposite case in which the user sets editing on again, by
  212. restoring readline handling of the input.
  213. NOTE: this operates on input_fd, not instream. If we are reading
  214. commands from a file, instream will point to the file. However, we
  215. always read commands from a file with editing off. This means that
  216. the 'set editing on/off' will have effect only on the interactive
  217. session. */
  218. void
  219. change_line_handler (int editing)
  220. {
  221. struct ui *ui = current_ui;
  222. /* We can only have one instance of readline, so we only allow
  223. editing on the main UI. */
  224. if (ui != main_ui)
  225. return;
  226. /* Don't try enabling editing if the interpreter doesn't support it
  227. (e.g., MI). */
  228. if (!interp_supports_command_editing (top_level_interpreter ())
  229. || !interp_supports_command_editing (command_interp ()))
  230. return;
  231. if (editing)
  232. {
  233. gdb_assert (ui == main_ui);
  234. /* Turn on editing by using readline. */
  235. ui->call_readline = gdb_rl_callback_read_char_wrapper;
  236. }
  237. else
  238. {
  239. /* Turn off editing by using gdb_readline_no_editing_callback. */
  240. if (ui->command_editing)
  241. gdb_rl_callback_handler_remove ();
  242. ui->call_readline = gdb_readline_no_editing_callback;
  243. }
  244. ui->command_editing = editing;
  245. }
  246. /* The functions below are wrappers for rl_callback_handler_remove and
  247. rl_callback_handler_install that keep track of whether the callback
  248. handler is installed in readline. This is necessary because after
  249. handling a target event of a background execution command, we may
  250. need to reinstall the callback handler if it was removed due to a
  251. secondary prompt. See gdb_readline_wrapper_line. We don't
  252. unconditionally install the handler for every target event because
  253. that also clears the line buffer, thus installing it while the user
  254. is typing would lose input. */
  255. /* Whether we've registered a callback handler with readline. */
  256. static bool callback_handler_installed;
  257. /* See event-top.h, and above. */
  258. void
  259. gdb_rl_callback_handler_remove (void)
  260. {
  261. gdb_assert (current_ui == main_ui);
  262. rl_callback_handler_remove ();
  263. callback_handler_installed = false;
  264. }
  265. /* See event-top.h, and above. Note this wrapper doesn't have an
  266. actual callback parameter because we always install
  267. INPUT_HANDLER. */
  268. void
  269. gdb_rl_callback_handler_install (const char *prompt)
  270. {
  271. gdb_assert (current_ui == main_ui);
  272. /* Calling rl_callback_handler_install resets readline's input
  273. buffer. Calling this when we were already processing input
  274. therefore loses input. */
  275. gdb_assert (!callback_handler_installed);
  276. rl_callback_handler_install (prompt, gdb_rl_callback_handler);
  277. callback_handler_installed = true;
  278. }
  279. /* See event-top.h, and above. */
  280. void
  281. gdb_rl_callback_handler_reinstall (void)
  282. {
  283. gdb_assert (current_ui == main_ui);
  284. if (!callback_handler_installed)
  285. {
  286. /* Passing NULL as prompt argument tells readline to not display
  287. a prompt. */
  288. gdb_rl_callback_handler_install (NULL);
  289. }
  290. }
  291. /* Displays the prompt. If the argument NEW_PROMPT is NULL, the
  292. prompt that is displayed is the current top level prompt.
  293. Otherwise, it displays whatever NEW_PROMPT is as a local/secondary
  294. prompt.
  295. This is used after each gdb command has completed, and in the
  296. following cases:
  297. 1. When the user enters a command line which is ended by '\'
  298. indicating that the command will continue on the next line. In
  299. that case the prompt that is displayed is the empty string.
  300. 2. When the user is entering 'commands' for a breakpoint, or
  301. actions for a tracepoint. In this case the prompt will be '>'
  302. 3. On prompting for pagination. */
  303. void
  304. display_gdb_prompt (const char *new_prompt)
  305. {
  306. std::string actual_gdb_prompt;
  307. annotate_display_prompt ();
  308. /* Reset the nesting depth used when trace-commands is set. */
  309. reset_command_nest_depth ();
  310. /* Do not call the python hook on an explicit prompt change as
  311. passed to this function, as this forms a secondary/local prompt,
  312. IE, displayed but not set. */
  313. if (! new_prompt)
  314. {
  315. struct ui *ui = current_ui;
  316. if (ui->prompt_state == PROMPTED)
  317. internal_error (__FILE__, __LINE__, _("double prompt"));
  318. else if (ui->prompt_state == PROMPT_BLOCKED)
  319. {
  320. /* This is to trick readline into not trying to display the
  321. prompt. Even though we display the prompt using this
  322. function, readline still tries to do its own display if
  323. we don't call rl_callback_handler_install and
  324. rl_callback_handler_remove (which readline detects
  325. because a global variable is not set). If readline did
  326. that, it could mess up gdb signal handlers for SIGINT.
  327. Readline assumes that between calls to rl_set_signals and
  328. rl_clear_signals gdb doesn't do anything with the signal
  329. handlers. Well, that's not the case, because when the
  330. target executes we change the SIGINT signal handler. If
  331. we allowed readline to display the prompt, the signal
  332. handler change would happen exactly between the calls to
  333. the above two functions. Calling
  334. rl_callback_handler_remove(), does the job. */
  335. if (current_ui->command_editing)
  336. gdb_rl_callback_handler_remove ();
  337. return;
  338. }
  339. else if (ui->prompt_state == PROMPT_NEEDED)
  340. {
  341. /* Display the top level prompt. */
  342. actual_gdb_prompt = top_level_prompt ();
  343. ui->prompt_state = PROMPTED;
  344. }
  345. }
  346. else
  347. actual_gdb_prompt = new_prompt;
  348. if (current_ui->command_editing)
  349. {
  350. gdb_rl_callback_handler_remove ();
  351. gdb_rl_callback_handler_install (actual_gdb_prompt.c_str ());
  352. }
  353. /* new_prompt at this point can be the top of the stack or the one
  354. passed in. It can't be NULL. */
  355. else
  356. {
  357. /* Don't use a _filtered function here. It causes the assumed
  358. character position to be off, since the newline we read from
  359. the user is not accounted for. */
  360. printf_unfiltered ("%s", actual_gdb_prompt.c_str ());
  361. gdb_flush (gdb_stdout);
  362. }
  363. }
  364. /* Return the top level prompt, as specified by "set prompt", possibly
  365. overridden by the python gdb.prompt_hook hook, and then composed
  366. with the prompt prefix and suffix (annotations). */
  367. static std::string
  368. top_level_prompt (void)
  369. {
  370. /* Give observers a chance of changing the prompt. E.g., the python
  371. `gdb.prompt_hook' is installed as an observer. */
  372. gdb::observers::before_prompt.notify (get_prompt ().c_str ());
  373. const std::string &prompt = get_prompt ();
  374. if (annotation_level >= 2)
  375. {
  376. /* Prefix needs to have new line at end. */
  377. const char prefix[] = "\n\032\032pre-prompt\n";
  378. /* Suffix needs to have a new line at end and \032 \032 at
  379. beginning. */
  380. const char suffix[] = "\n\032\032prompt\n";
  381. return std::string (prefix) + prompt.c_str () + suffix;
  382. }
  383. return prompt;
  384. }
  385. /* See top.h. */
  386. struct ui *main_ui;
  387. struct ui *current_ui;
  388. struct ui *ui_list;
  389. /* Get a pointer to the current UI's line buffer. This is used to
  390. construct a whole line of input from partial input. */
  391. static struct buffer *
  392. get_command_line_buffer (void)
  393. {
  394. return &current_ui->line_buffer;
  395. }
  396. /* When there is an event ready on the stdin file descriptor, instead
  397. of calling readline directly throught the callback function, or
  398. instead of calling gdb_readline_no_editing_callback, give gdb a
  399. chance to detect errors and do something. */
  400. void
  401. stdin_event_handler (int error, gdb_client_data client_data)
  402. {
  403. struct ui *ui = (struct ui *) client_data;
  404. if (error)
  405. {
  406. /* Switch to the main UI, so diagnostics always go there. */
  407. current_ui = main_ui;
  408. delete_file_handler (ui->input_fd);
  409. if (main_ui == ui)
  410. {
  411. /* If stdin died, we may as well kill gdb. */
  412. gdb_printf (gdb_stderr, _("error detected on stdin\n"));
  413. quit_command ((char *) 0, 0);
  414. }
  415. else
  416. {
  417. /* Simply delete the UI. */
  418. delete ui;
  419. }
  420. }
  421. else
  422. {
  423. /* Switch to the UI whose input descriptor woke up the event
  424. loop. */
  425. current_ui = ui;
  426. /* This makes sure a ^C immediately followed by further input is
  427. always processed in that order. E.g,. with input like
  428. "^Cprint 1\n", the SIGINT handler runs, marks the async
  429. signal handler, and then select/poll may return with stdin
  430. ready, instead of -1/EINTR. The
  431. gdb.base/double-prompt-target-event-error.exp test exercises
  432. this. */
  433. QUIT;
  434. do
  435. {
  436. call_stdin_event_handler_again_p = 0;
  437. ui->call_readline (client_data);
  438. }
  439. while (call_stdin_event_handler_again_p != 0);
  440. }
  441. }
  442. /* See top.h. */
  443. void
  444. ui_register_input_event_handler (struct ui *ui)
  445. {
  446. add_file_handler (ui->input_fd, stdin_event_handler, ui,
  447. string_printf ("ui-%d", ui->num), true);
  448. }
  449. /* See top.h. */
  450. void
  451. ui_unregister_input_event_handler (struct ui *ui)
  452. {
  453. delete_file_handler (ui->input_fd);
  454. }
  455. /* Re-enable stdin after the end of an execution command in
  456. synchronous mode, or after an error from the target, and we aborted
  457. the exec operation. */
  458. void
  459. async_enable_stdin (void)
  460. {
  461. struct ui *ui = current_ui;
  462. if (ui->prompt_state == PROMPT_BLOCKED)
  463. {
  464. target_terminal::ours ();
  465. ui_register_input_event_handler (ui);
  466. ui->prompt_state = PROMPT_NEEDED;
  467. }
  468. }
  469. /* Disable reads from stdin (the console) marking the command as
  470. synchronous. */
  471. void
  472. async_disable_stdin (void)
  473. {
  474. struct ui *ui = current_ui;
  475. ui->prompt_state = PROMPT_BLOCKED;
  476. delete_file_handler (ui->input_fd);
  477. }
  478. /* Handle a gdb command line. This function is called when
  479. handle_line_of_input has concatenated one or more input lines into
  480. a whole command. */
  481. void
  482. command_handler (const char *command)
  483. {
  484. struct ui *ui = current_ui;
  485. const char *c;
  486. if (ui->instream == ui->stdin_stream)
  487. reinitialize_more_filter ();
  488. scoped_command_stats stat_reporter (true);
  489. /* Do not execute commented lines. */
  490. for (c = command; *c == ' ' || *c == '\t'; c++)
  491. ;
  492. if (c[0] != '#')
  493. {
  494. execute_command (command, ui->instream == ui->stdin_stream);
  495. /* Do any commands attached to breakpoint we stopped at. */
  496. bpstat_do_actions ();
  497. }
  498. }
  499. /* Append RL, an input line returned by readline or one of its
  500. emulations, to CMD_LINE_BUFFER. Returns the command line if we
  501. have a whole command line ready to be processed by the command
  502. interpreter or NULL if the command line isn't complete yet (input
  503. line ends in a backslash). */
  504. static char *
  505. command_line_append_input_line (struct buffer *cmd_line_buffer, const char *rl)
  506. {
  507. char *cmd;
  508. size_t len;
  509. len = strlen (rl);
  510. if (len > 0 && rl[len - 1] == '\\')
  511. {
  512. /* Don't copy the backslash and wait for more. */
  513. buffer_grow (cmd_line_buffer, rl, len - 1);
  514. cmd = NULL;
  515. }
  516. else
  517. {
  518. /* Copy whole line including terminating null, and we're
  519. done. */
  520. buffer_grow (cmd_line_buffer, rl, len + 1);
  521. cmd = cmd_line_buffer->buffer;
  522. }
  523. return cmd;
  524. }
  525. /* Handle a line of input coming from readline.
  526. If the read line ends with a continuation character (backslash),
  527. save the partial input in CMD_LINE_BUFFER (except the backslash),
  528. and return NULL. Otherwise, save the partial input and return a
  529. pointer to CMD_LINE_BUFFER's buffer (null terminated), indicating a
  530. whole command line is ready to be executed.
  531. Returns EOF on end of file.
  532. If REPEAT, handle command repetitions:
  533. - If the input command line is NOT empty, the command returned is
  534. saved using save_command_line () so that it can be repeated later.
  535. - OTOH, if the input command line IS empty, return the saved
  536. command instead of the empty input line.
  537. */
  538. char *
  539. handle_line_of_input (struct buffer *cmd_line_buffer,
  540. const char *rl, int repeat,
  541. const char *annotation_suffix)
  542. {
  543. struct ui *ui = current_ui;
  544. int from_tty = ui->instream == ui->stdin_stream;
  545. char *p1;
  546. char *cmd;
  547. if (rl == NULL)
  548. return (char *) EOF;
  549. cmd = command_line_append_input_line (cmd_line_buffer, rl);
  550. if (cmd == NULL)
  551. return NULL;
  552. /* We have a complete command line now. Prepare for the next
  553. command, but leave ownership of memory to the buffer . */
  554. cmd_line_buffer->used_size = 0;
  555. if (from_tty && annotation_level > 1)
  556. printf_unfiltered (("\n\032\032post-%s\n"), annotation_suffix);
  557. #define SERVER_COMMAND_PREFIX "server "
  558. server_command = startswith (cmd, SERVER_COMMAND_PREFIX);
  559. if (server_command)
  560. {
  561. /* Note that we don't call `save_command_line'. Between this
  562. and the check in dont_repeat, this insures that repeating
  563. will still do the right thing. */
  564. return cmd + strlen (SERVER_COMMAND_PREFIX);
  565. }
  566. /* Do history expansion if that is wished. */
  567. if (history_expansion_p && from_tty && input_interactive_p (current_ui))
  568. {
  569. char *cmd_expansion;
  570. int expanded;
  571. expanded = history_expand (cmd, &cmd_expansion);
  572. gdb::unique_xmalloc_ptr<char> history_value (cmd_expansion);
  573. if (expanded)
  574. {
  575. size_t len;
  576. /* Print the changes. */
  577. printf_unfiltered ("%s\n", history_value.get ());
  578. /* If there was an error, call this function again. */
  579. if (expanded < 0)
  580. return cmd;
  581. /* history_expand returns an allocated string. Just replace
  582. our buffer with it. */
  583. len = strlen (history_value.get ());
  584. xfree (buffer_finish (cmd_line_buffer));
  585. cmd_line_buffer->buffer = history_value.get ();
  586. cmd_line_buffer->buffer_size = len + 1;
  587. cmd = history_value.release ();
  588. }
  589. }
  590. /* If we just got an empty line, and that is supposed to repeat the
  591. previous command, return the previously saved command. */
  592. for (p1 = cmd; *p1 == ' ' || *p1 == '\t'; p1++)
  593. ;
  594. if (repeat && *p1 == '\0')
  595. return get_saved_command_line ();
  596. /* Add command to history if appropriate. Note: lines consisting
  597. solely of comments are also added to the command history. This
  598. is useful when you type a command, and then realize you don't
  599. want to execute it quite yet. You can comment out the command
  600. and then later fetch it from the value history and remove the
  601. '#'. The kill ring is probably better, but some people are in
  602. the habit of commenting things out. */
  603. if (*cmd != '\0' && from_tty && input_interactive_p (current_ui))
  604. gdb_add_history (cmd);
  605. /* Save into global buffer if appropriate. */
  606. if (repeat)
  607. {
  608. save_command_line (cmd);
  609. return get_saved_command_line ();
  610. }
  611. else
  612. return cmd;
  613. }
  614. /* Handle a complete line of input. This is called by the callback
  615. mechanism within the readline library. Deal with incomplete
  616. commands as well, by saving the partial input in a global
  617. buffer.
  618. NOTE: This is the asynchronous version of the command_line_input
  619. function. */
  620. void
  621. command_line_handler (gdb::unique_xmalloc_ptr<char> &&rl)
  622. {
  623. struct buffer *line_buffer = get_command_line_buffer ();
  624. struct ui *ui = current_ui;
  625. char *cmd;
  626. cmd = handle_line_of_input (line_buffer, rl.get (), 1, "prompt");
  627. if (cmd == (char *) EOF)
  628. {
  629. /* stdin closed. The connection with the terminal is gone.
  630. This happens at the end of a testsuite run, after Expect has
  631. hung up but GDB is still alive. In such a case, we just quit
  632. gdb killing the inferior program too. This also happens if the
  633. user sends EOF, which is usually bound to ctrl+d.
  634. What we want to do in this case is print "quit" after the GDB
  635. prompt, as if the user had just typed "quit" and pressed return.
  636. This used to work just fine, but unfortunately, doesn't play well
  637. with readline's bracketed paste mode. By the time we get here,
  638. readline has already sent the control sequence to leave bracketed
  639. paste mode, and this sequence ends with a '\r' character. As a
  640. result, if bracketed paste mode is on, and we print quit here,
  641. then this will overwrite the prompt.
  642. To work around this issue, when bracketed paste mode is enabled,
  643. we first print '\n' to move to the next line, and then print the
  644. quit. This isn't ideal, but avoids corrupting the prompt. */
  645. const char *value = rl_variable_value ("enable-bracketed-paste");
  646. if (value != nullptr && strcmp (value, "on") == 0)
  647. printf_unfiltered ("\n");
  648. printf_unfiltered ("quit\n");
  649. execute_command ("quit", 1);
  650. }
  651. else if (cmd == NULL)
  652. {
  653. /* We don't have a full line yet. Print an empty prompt. */
  654. display_gdb_prompt ("");
  655. }
  656. else
  657. {
  658. ui->prompt_state = PROMPT_NEEDED;
  659. command_handler (cmd);
  660. if (ui->prompt_state != PROMPTED)
  661. display_gdb_prompt (0);
  662. }
  663. }
  664. /* Does reading of input from terminal w/o the editing features
  665. provided by the readline library. Calls the line input handler
  666. once we have a whole input line. */
  667. void
  668. gdb_readline_no_editing_callback (gdb_client_data client_data)
  669. {
  670. int c;
  671. char *result;
  672. struct buffer line_buffer;
  673. struct ui *ui = current_ui;
  674. buffer_init (&line_buffer);
  675. FILE *stream = ui->instream != nullptr ? ui->instream : ui->stdin_stream;
  676. gdb_assert (stream != nullptr);
  677. /* Unbuffer the input stream, so that, later on, the calls to fgetc
  678. fetch only one char at the time from the stream. The fgetc's will
  679. get up to the first newline, but there may be more chars in the
  680. stream after '\n'. If we buffer the input and fgetc drains the
  681. stream, getting stuff beyond the newline as well, a select, done
  682. afterwards will not trigger.
  683. This unbuffering was, at one point, not applied if the input stream
  684. was a tty, however, the buffering can cause problems, even for a tty,
  685. in some cases. Please ensure that any changes in this area run the MI
  686. tests with the FORCE_SEPARATE_MI_TTY=1 flag being passed. */
  687. setbuf (stream, NULL);
  688. /* We still need the while loop here, even though it would seem
  689. obvious to invoke gdb_readline_no_editing_callback at every
  690. character entered. If not using the readline library, the
  691. terminal is in cooked mode, which sends the characters all at
  692. once. Poll will notice that the input fd has changed state only
  693. after enter is pressed. At this point we still need to fetch all
  694. the chars entered. */
  695. while (1)
  696. {
  697. /* Read from stdin if we are executing a user defined command.
  698. This is the right thing for prompt_for_continue, at least. */
  699. c = fgetc (stream);
  700. if (c == EOF)
  701. {
  702. if (line_buffer.used_size > 0)
  703. {
  704. /* The last line does not end with a newline. Return it, and
  705. if we are called again fgetc will still return EOF and
  706. we'll return NULL then. */
  707. break;
  708. }
  709. xfree (buffer_finish (&line_buffer));
  710. ui->input_handler (NULL);
  711. return;
  712. }
  713. if (c == '\n')
  714. {
  715. if (line_buffer.used_size > 0
  716. && line_buffer.buffer[line_buffer.used_size - 1] == '\r')
  717. line_buffer.used_size--;
  718. break;
  719. }
  720. buffer_grow_char (&line_buffer, c);
  721. }
  722. buffer_grow_char (&line_buffer, '\0');
  723. result = buffer_finish (&line_buffer);
  724. ui->input_handler (gdb::unique_xmalloc_ptr<char> (result));
  725. }
  726. /* Attempt to unblock signal SIG, return true if the signal was unblocked,
  727. otherwise, return false. */
  728. static bool
  729. unblock_signal (int sig)
  730. {
  731. #if HAVE_SIGPROCMASK
  732. sigset_t sigset;
  733. sigemptyset (&sigset);
  734. sigaddset (&sigset, sig);
  735. gdb_sigmask (SIG_UNBLOCK, &sigset, 0);
  736. return true;
  737. #endif
  738. return false;
  739. }
  740. /* Called to handle fatal signals. SIG is the signal number. */
  741. static void ATTRIBUTE_NORETURN
  742. handle_fatal_signal (int sig)
  743. {
  744. #ifdef GDB_PRINT_INTERNAL_BACKTRACE
  745. const auto sig_write = [] (const char *msg) -> void
  746. {
  747. gdb_stderr->write_async_safe (msg, strlen (msg));
  748. };
  749. if (bt_on_fatal_signal)
  750. {
  751. sig_write ("\n\n");
  752. sig_write (_("Fatal signal: "));
  753. sig_write (strsignal (sig));
  754. sig_write ("\n");
  755. gdb_internal_backtrace ();
  756. sig_write (_("A fatal error internal to GDB has been detected, "
  757. "further\ndebugging is not possible. GDB will now "
  758. "terminate.\n\n"));
  759. sig_write (_("This is a bug, please report it."));
  760. if (REPORT_BUGS_TO[0] != '\0')
  761. {
  762. sig_write (_(" For instructions, see:\n"));
  763. sig_write (REPORT_BUGS_TO);
  764. sig_write (".");
  765. }
  766. sig_write ("\n\n");
  767. gdb_stderr->flush ();
  768. }
  769. #endif
  770. /* If possible arrange for SIG to have its default behaviour (which
  771. should be to terminate the current process), unblock SIG, and reraise
  772. the signal. This ensures GDB terminates with the expected signal. */
  773. if (signal (sig, SIG_DFL) != SIG_ERR
  774. && unblock_signal (sig))
  775. raise (sig);
  776. /* The above failed, so try to use SIGABRT to terminate GDB. */
  777. #ifdef SIGABRT
  778. signal (SIGABRT, SIG_DFL);
  779. #endif
  780. abort (); /* ARI: abort */
  781. }
  782. /* The SIGSEGV handler for this thread, or NULL if there is none. GDB
  783. always installs a global SIGSEGV handler, and then lets threads
  784. indicate their interest in handling the signal by setting this
  785. thread-local variable.
  786. This is a static variable instead of extern because on various platforms
  787. (notably Cygwin) extern thread_local variables cause link errors. So
  788. instead, we have scoped_segv_handler_restore, which also makes it impossible
  789. to accidentally forget to restore it to the original value. */
  790. static thread_local void (*thread_local_segv_handler) (int);
  791. static void handle_sigsegv (int sig);
  792. /* Install the SIGSEGV handler. */
  793. static void
  794. install_handle_sigsegv ()
  795. {
  796. #if defined (HAVE_SIGACTION)
  797. struct sigaction sa;
  798. sa.sa_handler = handle_sigsegv;
  799. sigemptyset (&sa.sa_mask);
  800. #ifdef HAVE_SIGALTSTACK
  801. sa.sa_flags = SA_ONSTACK;
  802. #else
  803. sa.sa_flags = 0;
  804. #endif
  805. sigaction (SIGSEGV, &sa, nullptr);
  806. #else
  807. signal (SIGSEGV, handle_sigsegv);
  808. #endif
  809. }
  810. /* Handler for SIGSEGV. */
  811. static void
  812. handle_sigsegv (int sig)
  813. {
  814. install_handle_sigsegv ();
  815. if (thread_local_segv_handler == nullptr)
  816. handle_fatal_signal (sig);
  817. thread_local_segv_handler (sig);
  818. }
  819. /* The serial event associated with the QUIT flag. set_quit_flag sets
  820. this, and check_quit_flag clears it. Used by interruptible_select
  821. to be able to do interruptible I/O with no race with the SIGINT
  822. handler. */
  823. static struct serial_event *quit_serial_event;
  824. /* Initialization of signal handlers and tokens. There are a number of
  825. different strategies for handling different signals here.
  826. For SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGTSTP, there is a function
  827. handle_sig* for each of these signals. These functions are the actual
  828. signal handlers associated to the signals via calls to signal(). The
  829. only job for these functions is to enqueue the appropriate
  830. event/procedure with the event loop. The event loop will take care of
  831. invoking the queued procedures to perform the usual tasks associated
  832. with the reception of the signal.
  833. For SIGSEGV the handle_sig* function does all the work for handling this
  834. signal.
  835. For SIGFPE, SIGBUS, and SIGABRT, these signals will all cause GDB to
  836. terminate immediately. */
  837. void
  838. gdb_init_signals (void)
  839. {
  840. initialize_async_signal_handlers ();
  841. quit_serial_event = make_serial_event ();
  842. sigint_token =
  843. create_async_signal_handler (async_request_quit, NULL, "sigint");
  844. signal (SIGINT, handle_sigint);
  845. async_sigterm_token
  846. = create_async_signal_handler (async_sigterm_handler, NULL, "sigterm");
  847. signal (SIGTERM, handle_sigterm);
  848. #ifdef SIGQUIT
  849. sigquit_token =
  850. create_async_signal_handler (async_do_nothing, NULL, "sigquit");
  851. signal (SIGQUIT, handle_sigquit);
  852. #endif
  853. #ifdef SIGHUP
  854. if (signal (SIGHUP, handle_sighup) != SIG_IGN)
  855. sighup_token =
  856. create_async_signal_handler (async_disconnect, NULL, "sighup");
  857. else
  858. sighup_token =
  859. create_async_signal_handler (async_do_nothing, NULL, "sighup");
  860. #endif
  861. #ifdef SIGTSTP
  862. sigtstp_token =
  863. create_async_signal_handler (async_sigtstp_handler, NULL, "sigtstp");
  864. #endif
  865. #ifdef SIGFPE
  866. signal (SIGFPE, handle_fatal_signal);
  867. #endif
  868. #ifdef SIGBUS
  869. signal (SIGBUS, handle_fatal_signal);
  870. #endif
  871. #ifdef SIGABRT
  872. signal (SIGABRT, handle_fatal_signal);
  873. #endif
  874. install_handle_sigsegv ();
  875. }
  876. /* See defs.h. */
  877. void
  878. quit_serial_event_set (void)
  879. {
  880. serial_event_set (quit_serial_event);
  881. }
  882. /* See defs.h. */
  883. void
  884. quit_serial_event_clear (void)
  885. {
  886. serial_event_clear (quit_serial_event);
  887. }
  888. /* Return the selectable file descriptor of the serial event
  889. associated with the quit flag. */
  890. static int
  891. quit_serial_event_fd (void)
  892. {
  893. return serial_event_fd (quit_serial_event);
  894. }
  895. /* See defs.h. */
  896. void
  897. default_quit_handler (void)
  898. {
  899. if (check_quit_flag ())
  900. {
  901. if (target_terminal::is_ours ())
  902. quit ();
  903. else
  904. target_pass_ctrlc ();
  905. }
  906. }
  907. /* See defs.h. */
  908. quit_handler_ftype *quit_handler = default_quit_handler;
  909. /* Handle a SIGINT. */
  910. void
  911. handle_sigint (int sig)
  912. {
  913. signal (sig, handle_sigint);
  914. /* We could be running in a loop reading in symfiles or something so
  915. it may be quite a while before we get back to the event loop. So
  916. set quit_flag to 1 here. Then if QUIT is called before we get to
  917. the event loop, we will unwind as expected. */
  918. set_quit_flag ();
  919. /* In case nothing calls QUIT before the event loop is reached, the
  920. event loop handles it. */
  921. mark_async_signal_handler (sigint_token);
  922. }
  923. /* See gdb_select.h. */
  924. int
  925. interruptible_select (int n,
  926. fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  927. struct timeval *timeout)
  928. {
  929. fd_set my_readfds;
  930. int fd;
  931. int res;
  932. if (readfds == NULL)
  933. {
  934. readfds = &my_readfds;
  935. FD_ZERO (&my_readfds);
  936. }
  937. fd = quit_serial_event_fd ();
  938. FD_SET (fd, readfds);
  939. if (n <= fd)
  940. n = fd + 1;
  941. do
  942. {
  943. res = gdb_select (n, readfds, writefds, exceptfds, timeout);
  944. }
  945. while (res == -1 && errno == EINTR);
  946. if (res == 1 && FD_ISSET (fd, readfds))
  947. {
  948. errno = EINTR;
  949. return -1;
  950. }
  951. return res;
  952. }
  953. /* Handle GDB exit upon receiving SIGTERM if target_can_async_p (). */
  954. static void
  955. async_sigterm_handler (gdb_client_data arg)
  956. {
  957. quit_force (NULL, 0);
  958. }
  959. /* See defs.h. */
  960. volatile int sync_quit_force_run;
  961. /* Quit GDB if SIGTERM is received.
  962. GDB would quit anyway, but this way it will clean up properly. */
  963. void
  964. handle_sigterm (int sig)
  965. {
  966. signal (sig, handle_sigterm);
  967. sync_quit_force_run = 1;
  968. set_quit_flag ();
  969. mark_async_signal_handler (async_sigterm_token);
  970. }
  971. /* Do the quit. All the checks have been done by the caller. */
  972. void
  973. async_request_quit (gdb_client_data arg)
  974. {
  975. /* If the quit_flag has gotten reset back to 0 by the time we get
  976. back here, that means that an exception was thrown to unwind the
  977. current command before we got back to the event loop. So there
  978. is no reason to call quit again here. */
  979. QUIT;
  980. }
  981. #ifdef SIGQUIT
  982. /* Tell the event loop what to do if SIGQUIT is received.
  983. See event-signal.c. */
  984. static void
  985. handle_sigquit (int sig)
  986. {
  987. mark_async_signal_handler (sigquit_token);
  988. signal (sig, handle_sigquit);
  989. }
  990. #endif
  991. #if defined (SIGQUIT) || defined (SIGHUP)
  992. /* Called by the event loop in response to a SIGQUIT or an
  993. ignored SIGHUP. */
  994. static void
  995. async_do_nothing (gdb_client_data arg)
  996. {
  997. /* Empty function body. */
  998. }
  999. #endif
  1000. #ifdef SIGHUP
  1001. /* Tell the event loop what to do if SIGHUP is received.
  1002. See event-signal.c. */
  1003. static void
  1004. handle_sighup (int sig)
  1005. {
  1006. mark_async_signal_handler (sighup_token);
  1007. signal (sig, handle_sighup);
  1008. }
  1009. /* Called by the event loop to process a SIGHUP. */
  1010. static void
  1011. async_disconnect (gdb_client_data arg)
  1012. {
  1013. try
  1014. {
  1015. quit_cover ();
  1016. }
  1017. catch (const gdb_exception &exception)
  1018. {
  1019. gdb_puts ("Could not kill the program being debugged",
  1020. gdb_stderr);
  1021. exception_print (gdb_stderr, exception);
  1022. }
  1023. for (inferior *inf : all_inferiors ())
  1024. {
  1025. switch_to_inferior_no_thread (inf);
  1026. try
  1027. {
  1028. pop_all_targets ();
  1029. }
  1030. catch (const gdb_exception &exception)
  1031. {
  1032. }
  1033. }
  1034. signal (SIGHUP, SIG_DFL); /*FIXME: ??????????? */
  1035. raise (SIGHUP);
  1036. }
  1037. #endif
  1038. #ifdef SIGTSTP
  1039. void
  1040. handle_sigtstp (int sig)
  1041. {
  1042. mark_async_signal_handler (sigtstp_token);
  1043. signal (sig, handle_sigtstp);
  1044. }
  1045. static void
  1046. async_sigtstp_handler (gdb_client_data arg)
  1047. {
  1048. const std::string &prompt = get_prompt ();
  1049. signal (SIGTSTP, SIG_DFL);
  1050. unblock_signal (SIGTSTP);
  1051. raise (SIGTSTP);
  1052. signal (SIGTSTP, handle_sigtstp);
  1053. printf_unfiltered ("%s", prompt.c_str ());
  1054. gdb_flush (gdb_stdout);
  1055. /* Forget about any previous command -- null line now will do
  1056. nothing. */
  1057. dont_repeat ();
  1058. }
  1059. #endif /* SIGTSTP */
  1060. /* Set things up for readline to be invoked via the alternate
  1061. interface, i.e. via a callback function
  1062. (gdb_rl_callback_read_char), and hook up instream to the event
  1063. loop. */
  1064. void
  1065. gdb_setup_readline (int editing)
  1066. {
  1067. struct ui *ui = current_ui;
  1068. /* This function is a noop for the sync case. The assumption is
  1069. that the sync setup is ALL done in gdb_init, and we would only
  1070. mess it up here. The sync stuff should really go away over
  1071. time. */
  1072. if (!batch_silent)
  1073. gdb_stdout = new pager_file (new stdio_file (ui->outstream));
  1074. gdb_stderr = new stderr_file (ui->errstream);
  1075. gdb_stdlog = new timestamped_file (gdb_stderr);
  1076. gdb_stdtarg = gdb_stderr; /* for moment */
  1077. gdb_stdtargerr = gdb_stderr; /* for moment */
  1078. /* If the input stream is connected to a terminal, turn on editing.
  1079. However, that is only allowed on the main UI, as we can only have
  1080. one instance of readline. */
  1081. if (ISATTY (ui->instream) && editing && ui == main_ui)
  1082. {
  1083. /* Tell gdb that we will be using the readline library. This
  1084. could be overwritten by a command in .gdbinit like 'set
  1085. editing on' or 'off'. */
  1086. ui->command_editing = 1;
  1087. /* When a character is detected on instream by select or poll,
  1088. readline will be invoked via this callback function. */
  1089. ui->call_readline = gdb_rl_callback_read_char_wrapper;
  1090. /* Tell readline to use the same input stream that gdb uses. */
  1091. rl_instream = ui->instream;
  1092. }
  1093. else
  1094. {
  1095. ui->command_editing = 0;
  1096. ui->call_readline = gdb_readline_no_editing_callback;
  1097. }
  1098. /* Now create the event source for this UI's input file descriptor.
  1099. Another source is going to be the target program (inferior), but
  1100. that must be registered only when it actually exists (I.e. after
  1101. we say 'run' or after we connect to a remote target. */
  1102. ui_register_input_event_handler (ui);
  1103. }
  1104. /* Disable command input through the standard CLI channels. Used in
  1105. the suspend proc for interpreters that use the standard gdb readline
  1106. interface, like the cli & the mi. */
  1107. void
  1108. gdb_disable_readline (void)
  1109. {
  1110. struct ui *ui = current_ui;
  1111. /* FIXME - It is too heavyweight to delete and remake these every
  1112. time you run an interpreter that needs readline. It is probably
  1113. better to have the interpreters cache these, which in turn means
  1114. that this needs to be moved into interpreter specific code. */
  1115. #if 0
  1116. ui_file_delete (gdb_stdout);
  1117. ui_file_delete (gdb_stderr);
  1118. gdb_stdlog = NULL;
  1119. gdb_stdtarg = NULL;
  1120. gdb_stdtargerr = NULL;
  1121. #endif
  1122. if (ui->command_editing)
  1123. gdb_rl_callback_handler_remove ();
  1124. delete_file_handler (ui->input_fd);
  1125. }
  1126. scoped_segv_handler_restore::scoped_segv_handler_restore (segv_handler_t new_handler)
  1127. {
  1128. m_old_handler = thread_local_segv_handler;
  1129. thread_local_segv_handler = new_handler;
  1130. }
  1131. scoped_segv_handler_restore::~scoped_segv_handler_restore()
  1132. {
  1133. thread_local_segv_handler = m_old_handler;
  1134. }
  1135. static const char debug_event_loop_off[] = "off";
  1136. static const char debug_event_loop_all_except_ui[] = "all-except-ui";
  1137. static const char debug_event_loop_all[] = "all";
  1138. static const char *debug_event_loop_enum[] = {
  1139. debug_event_loop_off,
  1140. debug_event_loop_all_except_ui,
  1141. debug_event_loop_all,
  1142. nullptr
  1143. };
  1144. static const char *debug_event_loop_value = debug_event_loop_off;
  1145. static void
  1146. set_debug_event_loop_command (const char *args, int from_tty,
  1147. cmd_list_element *c)
  1148. {
  1149. if (debug_event_loop_value == debug_event_loop_off)
  1150. debug_event_loop = debug_event_loop_kind::OFF;
  1151. else if (debug_event_loop_value == debug_event_loop_all_except_ui)
  1152. debug_event_loop = debug_event_loop_kind::ALL_EXCEPT_UI;
  1153. else if (debug_event_loop_value == debug_event_loop_all)
  1154. debug_event_loop = debug_event_loop_kind::ALL;
  1155. else
  1156. gdb_assert_not_reached ("Invalid debug event look kind value.");
  1157. }
  1158. static void
  1159. show_debug_event_loop_command (struct ui_file *file, int from_tty,
  1160. struct cmd_list_element *cmd, const char *value)
  1161. {
  1162. gdb_printf (file, _("Event loop debugging is %s.\n"), value);
  1163. }
  1164. void _initialize_event_top ();
  1165. void
  1166. _initialize_event_top ()
  1167. {
  1168. add_setshow_enum_cmd ("event-loop", class_maintenance,
  1169. debug_event_loop_enum,
  1170. &debug_event_loop_value,
  1171. _("Set event-loop debugging."),
  1172. _("Show event-loop debugging."),
  1173. _("\
  1174. Control whether to show event loop-related debug messages."),
  1175. set_debug_event_loop_command,
  1176. show_debug_event_loop_command,
  1177. &setdebuglist, &showdebuglist);
  1178. add_setshow_boolean_cmd ("backtrace-on-fatal-signal", class_maintenance,
  1179. &bt_on_fatal_signal, _("\
  1180. Set whether to produce a backtrace if GDB receives a fatal signal."), _("\
  1181. Show whether GDB will produce a backtrace if it receives a fatal signal."), _("\
  1182. Use \"on\" to enable, \"off\" to disable.\n\
  1183. If enabled, GDB will produce a minimal backtrace if it encounters a fatal\n\
  1184. signal from within GDB itself. This is a mechanism to help diagnose\n\
  1185. crashes within GDB, not a mechanism for debugging inferiors."),
  1186. gdb_internal_backtrace_set_cmd,
  1187. show_bt_on_fatal_signal,
  1188. &maintenance_set_cmdlist,
  1189. &maintenance_show_cmdlist);
  1190. }