top.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  1. /* Top level stuff for GDB, the GNU debugger.
  2. Copyright (C) 1986-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 "gdbcmd.h"
  16. #include "cli/cli-cmds.h"
  17. #include "cli/cli-script.h"
  18. #include "cli/cli-setshow.h"
  19. #include "cli/cli-decode.h"
  20. #include "symtab.h"
  21. #include "inferior.h"
  22. #include "infrun.h"
  23. #include <signal.h>
  24. #include "target.h"
  25. #include "target-dcache.h"
  26. #include "breakpoint.h"
  27. #include "gdbtypes.h"
  28. #include "expression.h"
  29. #include "value.h"
  30. #include "language.h"
  31. #include "terminal.h"
  32. #include "gdbsupport/job-control.h"
  33. #include "annotate.h"
  34. #include "completer.h"
  35. #include "top.h"
  36. #include "gdbsupport/version.h"
  37. #include "serial.h"
  38. #include "main.h"
  39. #include "gdbsupport/event-loop.h"
  40. #include "gdbthread.h"
  41. #include "extension.h"
  42. #include "interps.h"
  43. #include "observable.h"
  44. #include "maint.h"
  45. #include "filenames.h"
  46. #include "frame.h"
  47. #include "gdbsupport/buffer.h"
  48. #include "gdbsupport/gdb_select.h"
  49. #include "gdbsupport/scope-exit.h"
  50. #include "gdbarch.h"
  51. #include "gdbsupport/pathstuff.h"
  52. #include "cli/cli-style.h"
  53. #include "pager.h"
  54. /* readline include files. */
  55. #include "readline/readline.h"
  56. #include "readline/history.h"
  57. /* readline defines this. */
  58. #undef savestring
  59. #include <sys/types.h>
  60. #include "event-top.h"
  61. #include <sys/stat.h>
  62. #include <ctype.h>
  63. #include "ui-out.h"
  64. #include "cli-out.h"
  65. #include "tracepoint.h"
  66. #include "inf-loop.h"
  67. #if defined(TUI)
  68. # include "tui/tui.h"
  69. #endif
  70. #ifndef O_NOCTTY
  71. # define O_NOCTTY 0
  72. #endif
  73. extern void initialize_all_files (void);
  74. #define PROMPT(X) the_prompts.prompt_stack[the_prompts.top + X].prompt
  75. #define PREFIX(X) the_prompts.prompt_stack[the_prompts.top + X].prefix
  76. #define SUFFIX(X) the_prompts.prompt_stack[the_prompts.top + X].suffix
  77. /* Default command line prompt. This is overridden in some configs. */
  78. #ifndef DEFAULT_PROMPT
  79. #define DEFAULT_PROMPT "(gdb) "
  80. #endif
  81. struct ui_file **
  82. current_ui_gdb_stdout_ptr ()
  83. {
  84. return &current_ui->m_gdb_stdout;
  85. }
  86. struct ui_file **
  87. current_ui_gdb_stdin_ptr ()
  88. {
  89. return &current_ui->m_gdb_stdin;
  90. }
  91. struct ui_file **
  92. current_ui_gdb_stderr_ptr ()
  93. {
  94. return &current_ui->m_gdb_stderr;
  95. }
  96. struct ui_file **
  97. current_ui_gdb_stdlog_ptr ()
  98. {
  99. return &current_ui->m_gdb_stdlog;
  100. }
  101. struct ui_out **
  102. current_ui_current_uiout_ptr ()
  103. {
  104. return &current_ui->m_current_uiout;
  105. }
  106. int inhibit_gdbinit = 0;
  107. /* Flag for whether we want to confirm potentially dangerous
  108. operations. Default is yes. */
  109. bool confirm = true;
  110. static void
  111. show_confirm (struct ui_file *file, int from_tty,
  112. struct cmd_list_element *c, const char *value)
  113. {
  114. gdb_printf (file, _("Whether to confirm potentially "
  115. "dangerous operations is %s.\n"),
  116. value);
  117. }
  118. /* The last command line executed on the console. Used for command
  119. repetitions when the user enters an empty line. */
  120. static char *saved_command_line;
  121. /* If not NULL, the arguments that should be passed if
  122. saved_command_line is repeated. */
  123. static const char *repeat_arguments;
  124. /* The previous last command line executed on the console. Used for command
  125. repetitions when a command wants to relaunch the previously launched
  126. command. We need this as when a command is running, saved_command_line
  127. already contains the line of the currently executing command. */
  128. static char *previous_saved_command_line;
  129. /* If not NULL, the arguments that should be passed if the
  130. previous_saved_command_line is repeated. */
  131. static const char *previous_repeat_arguments;
  132. /* Nonzero if the current command is modified by "server ". This
  133. affects things like recording into the command history, commands
  134. repeating on RETURN, etc. This is so a user interface (emacs, GUI,
  135. whatever) can issue its own commands and also send along commands
  136. from the user, and have the user not notice that the user interface
  137. is issuing commands too. */
  138. bool server_command;
  139. /* Timeout limit for response from target. */
  140. /* The default value has been changed many times over the years. It
  141. was originally 5 seconds. But that was thought to be a long time
  142. to sit and wait, so it was changed to 2 seconds. That was thought
  143. to be plenty unless the connection was going through some terminal
  144. server or multiplexer or other form of hairy serial connection.
  145. In mid-1996, remote_timeout was moved from remote.c to top.c and
  146. it began being used in other remote-* targets. It appears that the
  147. default was changed to 20 seconds at that time, perhaps because the
  148. Renesas E7000 ICE didn't always respond in a timely manner.
  149. But if 5 seconds is a long time to sit and wait for retransmissions,
  150. 20 seconds is far worse. This demonstrates the difficulty of using
  151. a single variable for all protocol timeouts.
  152. As remote.c is used much more than remote-e7000.c, it was changed
  153. back to 2 seconds in 1999. */
  154. int remote_timeout = 2;
  155. /* Sbrk location on entry to main. Used for statistics only. */
  156. #ifdef HAVE_USEFUL_SBRK
  157. char *lim_at_start;
  158. #endif
  159. /* Hooks for alternate command interfaces. */
  160. /* This hook is called from within gdb's many mini-event loops which
  161. could steal control from a real user interface's event loop. It
  162. returns non-zero if the user is requesting a detach, zero
  163. otherwise. */
  164. int (*deprecated_ui_loop_hook) (int);
  165. /* Called from print_frame_info to list the line we stopped in. */
  166. void (*deprecated_print_frame_info_listing_hook) (struct symtab * s,
  167. int line,
  168. int stopline,
  169. int noerror);
  170. /* Replaces most of query. */
  171. int (*deprecated_query_hook) (const char *, va_list);
  172. /* Replaces most of warning. */
  173. void (*deprecated_warning_hook) (const char *, va_list);
  174. /* These three functions support getting lines of text from the user.
  175. They are used in sequence. First deprecated_readline_begin_hook is
  176. called with a text string that might be (for example) a message for
  177. the user to type in a sequence of commands to be executed at a
  178. breakpoint. If this function calls back to a GUI, it might take
  179. this opportunity to pop up a text interaction window with this
  180. message. Next, deprecated_readline_hook is called with a prompt
  181. that is emitted prior to collecting the user input. It can be
  182. called multiple times. Finally, deprecated_readline_end_hook is
  183. called to notify the GUI that we are done with the interaction
  184. window and it can close it. */
  185. void (*deprecated_readline_begin_hook) (const char *, ...);
  186. char *(*deprecated_readline_hook) (const char *);
  187. void (*deprecated_readline_end_hook) (void);
  188. /* Called as appropriate to notify the interface that we have attached
  189. to or detached from an already running process. */
  190. void (*deprecated_attach_hook) (void);
  191. void (*deprecated_detach_hook) (void);
  192. /* Used by UI as a wrapper around command execution. May do various
  193. things like enabling/disabling buttons, etc... */
  194. void (*deprecated_call_command_hook) (struct cmd_list_element * c,
  195. const char *cmd, int from_tty);
  196. /* Called when the current thread changes. Argument is thread id. */
  197. void (*deprecated_context_hook) (int id);
  198. /* The highest UI number ever assigned. */
  199. static int highest_ui_num;
  200. /* See top.h. */
  201. ui::ui (FILE *instream_, FILE *outstream_, FILE *errstream_)
  202. : next (nullptr),
  203. num (++highest_ui_num),
  204. call_readline (nullptr),
  205. input_handler (nullptr),
  206. command_editing (0),
  207. interp_info (nullptr),
  208. async (0),
  209. secondary_prompt_depth (0),
  210. stdin_stream (instream_),
  211. instream (instream_),
  212. outstream (outstream_),
  213. errstream (errstream_),
  214. input_fd (fileno (instream)),
  215. input_interactive_p (ISATTY (instream)),
  216. prompt_state (PROMPT_NEEDED),
  217. m_gdb_stdout (new pager_file (new stdio_file (outstream))),
  218. m_gdb_stdin (new stdio_file (instream)),
  219. m_gdb_stderr (new stderr_file (errstream)),
  220. m_gdb_stdlog (m_gdb_stderr),
  221. m_current_uiout (nullptr)
  222. {
  223. buffer_init (&line_buffer);
  224. if (ui_list == NULL)
  225. ui_list = this;
  226. else
  227. {
  228. struct ui *last;
  229. for (last = ui_list; last->next != NULL; last = last->next)
  230. ;
  231. last->next = this;
  232. }
  233. }
  234. ui::~ui ()
  235. {
  236. struct ui *ui, *uiprev;
  237. uiprev = NULL;
  238. for (ui = ui_list; ui != NULL; uiprev = ui, ui = ui->next)
  239. if (ui == this)
  240. break;
  241. gdb_assert (ui != NULL);
  242. if (uiprev != NULL)
  243. uiprev->next = next;
  244. else
  245. ui_list = next;
  246. delete m_gdb_stdin;
  247. delete m_gdb_stdout;
  248. delete m_gdb_stderr;
  249. }
  250. /* Open file named NAME for read/write, making sure not to make it the
  251. controlling terminal. */
  252. static gdb_file_up
  253. open_terminal_stream (const char *name)
  254. {
  255. scoped_fd fd = gdb_open_cloexec (name, O_RDWR | O_NOCTTY, 0);
  256. if (fd.get () < 0)
  257. perror_with_name (_("opening terminal failed"));
  258. return fd.to_file ("w+");
  259. }
  260. /* Implementation of the "new-ui" command. */
  261. static void
  262. new_ui_command (const char *args, int from_tty)
  263. {
  264. int argc;
  265. const char *interpreter_name;
  266. const char *tty_name;
  267. dont_repeat ();
  268. gdb_argv argv (args);
  269. argc = argv.count ();
  270. if (argc < 2)
  271. error (_("Usage: new-ui INTERPRETER TTY"));
  272. interpreter_name = argv[0];
  273. tty_name = argv[1];
  274. {
  275. scoped_restore save_ui = make_scoped_restore (&current_ui);
  276. /* Open specified terminal. Note: we used to open it three times,
  277. once for each of stdin/stdout/stderr, but that does not work
  278. with Windows named pipes. */
  279. gdb_file_up stream = open_terminal_stream (tty_name);
  280. std::unique_ptr<ui> ui
  281. (new struct ui (stream.get (), stream.get (), stream.get ()));
  282. ui->async = 1;
  283. current_ui = ui.get ();
  284. set_top_level_interpreter (interpreter_name);
  285. interp_pre_command_loop (top_level_interpreter ());
  286. /* Make sure the file is not closed. */
  287. stream.release ();
  288. ui.release ();
  289. }
  290. gdb_printf ("New UI allocated\n");
  291. }
  292. /* Handler for SIGHUP. */
  293. #ifdef SIGHUP
  294. /* NOTE 1999-04-29: This function will be static again, once we modify
  295. gdb to use the event loop as the default command loop and we merge
  296. event-top.c into this file, top.c. */
  297. /* static */ void
  298. quit_cover (void)
  299. {
  300. /* Stop asking user for confirmation --- we're exiting. This
  301. prevents asking the user dumb questions. */
  302. confirm = 0;
  303. quit_command ((char *) 0, 0);
  304. }
  305. #endif /* defined SIGHUP */
  306. /* Line number we are currently in, in a file which is being sourced. */
  307. /* NOTE 1999-04-29: This variable will be static again, once we modify
  308. gdb to use the event loop as the default command loop and we merge
  309. event-top.c into this file, top.c. */
  310. /* static */ int source_line_number;
  311. /* Name of the file we are sourcing. */
  312. /* NOTE 1999-04-29: This variable will be static again, once we modify
  313. gdb to use the event loop as the default command loop and we merge
  314. event-top.c into this file, top.c. */
  315. /* static */ std::string source_file_name;
  316. /* Read commands from STREAM. */
  317. void
  318. read_command_file (FILE *stream)
  319. {
  320. struct ui *ui = current_ui;
  321. scoped_restore save_instream
  322. = make_scoped_restore (&ui->instream, stream);
  323. /* Read commands from `instream' and execute them until end of file
  324. or error reading instream. */
  325. while (ui->instream != NULL && !feof (ui->instream))
  326. {
  327. const char *command;
  328. /* Get a command-line. This calls the readline package. */
  329. command = command_line_input (NULL, NULL);
  330. if (command == NULL)
  331. break;
  332. command_handler (command);
  333. }
  334. }
  335. #ifdef __MSDOS__
  336. static void
  337. do_chdir_cleanup (void *old_dir)
  338. {
  339. chdir ((const char *) old_dir);
  340. xfree (old_dir);
  341. }
  342. #endif
  343. scoped_value_mark
  344. prepare_execute_command ()
  345. {
  346. /* With multiple threads running while the one we're examining is
  347. stopped, the dcache can get stale without us being able to detect
  348. it. For the duration of the command, though, use the dcache to
  349. help things like backtrace. */
  350. if (non_stop)
  351. target_dcache_invalidate ();
  352. return scoped_value_mark ();
  353. }
  354. /* Tell the user if the language has changed (except first time) after
  355. executing a command. */
  356. void
  357. check_frame_language_change (void)
  358. {
  359. static int warned = 0;
  360. struct frame_info *frame;
  361. /* First make sure that a new frame has been selected, in case the
  362. command or the hooks changed the program state. */
  363. frame = deprecated_safe_get_selected_frame ();
  364. if (current_language != expected_language)
  365. {
  366. if (language_mode == language_mode_auto && info_verbose)
  367. {
  368. /* Print what changed. */
  369. language_info ();
  370. }
  371. warned = 0;
  372. }
  373. /* Warn the user if the working language does not match the language
  374. of the current frame. Only warn the user if we are actually
  375. running the program, i.e. there is a stack. */
  376. /* FIXME: This should be cacheing the frame and only running when
  377. the frame changes. */
  378. if (has_stack_frames ())
  379. {
  380. enum language flang;
  381. flang = get_frame_language (frame);
  382. if (!warned
  383. && flang != language_unknown
  384. && flang != current_language->la_language)
  385. {
  386. gdb_printf ("%s\n", _(lang_frame_mismatch_warn));
  387. warned = 1;
  388. }
  389. }
  390. }
  391. /* See top.h. */
  392. void
  393. wait_sync_command_done (void)
  394. {
  395. /* Processing events may change the current UI. */
  396. scoped_restore save_ui = make_scoped_restore (&current_ui);
  397. struct ui *ui = current_ui;
  398. /* We're about to wait until the target stops after having resumed
  399. it so must force-commit resumptions, in case we're being called
  400. in some context where a scoped_disable_commit_resumed object is
  401. active. I.e., this function is a commit-resumed sync/flush
  402. point. */
  403. scoped_enable_commit_resumed enable ("sync wait");
  404. while (gdb_do_one_event () >= 0)
  405. if (ui->prompt_state != PROMPT_BLOCKED)
  406. break;
  407. }
  408. /* See top.h. */
  409. void
  410. maybe_wait_sync_command_done (int was_sync)
  411. {
  412. /* If the interpreter is in sync mode (we're running a user
  413. command's list, running command hooks or similars), and we
  414. just ran a synchronous command that started the target, wait
  415. for that command to end. */
  416. if (!current_ui->async
  417. && !was_sync
  418. && current_ui->prompt_state == PROMPT_BLOCKED)
  419. wait_sync_command_done ();
  420. }
  421. /* See command.h. */
  422. void
  423. set_repeat_arguments (const char *args)
  424. {
  425. repeat_arguments = args;
  426. }
  427. /* Execute the line P as a command, in the current user context.
  428. Pass FROM_TTY as second argument to the defining function. */
  429. void
  430. execute_command (const char *p, int from_tty)
  431. {
  432. struct cmd_list_element *c;
  433. const char *line;
  434. const char *cmd_start = p;
  435. auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
  436. scoped_value_mark cleanup = prepare_execute_command ();
  437. /* This can happen when command_line_input hits end of file. */
  438. if (p == NULL)
  439. {
  440. cleanup_if_error.release ();
  441. return;
  442. }
  443. target_log_command (p);
  444. while (*p == ' ' || *p == '\t')
  445. p++;
  446. if (*p)
  447. {
  448. const char *cmd = p;
  449. const char *arg;
  450. std::string default_args;
  451. std::string default_args_and_arg;
  452. int was_sync = current_ui->prompt_state == PROMPT_BLOCKED;
  453. line = p;
  454. /* If trace-commands is set then this will print this command. */
  455. print_command_trace ("%s", p);
  456. c = lookup_cmd (&cmd, cmdlist, "", &default_args, 0, 1);
  457. p = cmd;
  458. scoped_restore save_repeat_args
  459. = make_scoped_restore (&repeat_arguments, nullptr);
  460. const char *args_pointer = p;
  461. if (!default_args.empty ())
  462. {
  463. if (*p != '\0')
  464. default_args_and_arg = default_args + ' ' + p;
  465. else
  466. default_args_and_arg = default_args;
  467. arg = default_args_and_arg.c_str ();
  468. }
  469. else
  470. {
  471. /* Pass null arg rather than an empty one. */
  472. arg = *p == '\0' ? nullptr : p;
  473. }
  474. /* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
  475. while the is_complete_command(cfunc) test is just plain
  476. bogus. They should both be replaced by a test of the form
  477. c->strip_trailing_white_space_p. */
  478. /* NOTE: cagney/2002-02-02: The function.cfunc in the below
  479. can't be replaced with func. This is because it is the
  480. cfunc, and not the func, that has the value that the
  481. is_complete_command hack is testing for. */
  482. /* Clear off trailing whitespace, except for set and complete
  483. command. */
  484. std::string without_whitespace;
  485. if (arg
  486. && c->type != set_cmd
  487. && !is_complete_command (c))
  488. {
  489. const char *old_end = arg + strlen (arg) - 1;
  490. p = old_end;
  491. while (p >= arg && (*p == ' ' || *p == '\t'))
  492. p--;
  493. if (p != old_end)
  494. {
  495. without_whitespace = std::string (arg, p + 1);
  496. arg = without_whitespace.c_str ();
  497. }
  498. }
  499. /* If this command has been pre-hooked, run the hook first. */
  500. execute_cmd_pre_hook (c);
  501. if (c->deprecated_warn_user)
  502. deprecated_cmd_warning (line, cmdlist);
  503. /* c->user_commands would be NULL in the case of a python command. */
  504. if (c->theclass == class_user && c->user_commands)
  505. execute_user_command (c, arg);
  506. else if (c->theclass == class_user
  507. && c->is_prefix () && !c->allow_unknown)
  508. /* If this is a user defined prefix that does not allow unknown
  509. (in other words, C is a prefix command and not a command
  510. that can be followed by its args), report the list of
  511. subcommands. */
  512. {
  513. std::string prefixname = c->prefixname ();
  514. std::string prefixname_no_space
  515. = prefixname.substr (0, prefixname.length () - 1);
  516. gdb_printf
  517. ("\"%s\" must be followed by the name of a subcommand.\n",
  518. prefixname_no_space.c_str ());
  519. help_list (*c->subcommands, prefixname.c_str (), all_commands,
  520. gdb_stdout);
  521. }
  522. else if (c->type == set_cmd)
  523. do_set_command (arg, from_tty, c);
  524. else if (c->type == show_cmd)
  525. do_show_command (arg, from_tty, c);
  526. else if (c->is_command_class_help ())
  527. error (_("That is not a command, just a help topic."));
  528. else if (deprecated_call_command_hook)
  529. deprecated_call_command_hook (c, arg, from_tty);
  530. else
  531. cmd_func (c, arg, from_tty);
  532. maybe_wait_sync_command_done (was_sync);
  533. /* If this command has been post-hooked, run the hook last. */
  534. execute_cmd_post_hook (c);
  535. if (repeat_arguments != NULL && cmd_start == saved_command_line)
  536. {
  537. gdb_assert (strlen (args_pointer) >= strlen (repeat_arguments));
  538. strcpy (saved_command_line + (args_pointer - cmd_start),
  539. repeat_arguments);
  540. }
  541. }
  542. /* Only perform the frame-language-change check if the command
  543. we just finished executing did not resume the inferior's execution.
  544. If it did resume the inferior, we will do that check after
  545. the inferior stopped. */
  546. if (has_stack_frames () && inferior_thread ()->state != THREAD_RUNNING)
  547. check_frame_language_change ();
  548. cleanup_if_error.release ();
  549. }
  550. /* See gdbcmd.h. */
  551. void
  552. execute_fn_to_ui_file (struct ui_file *file, std::function<void(void)> fn)
  553. {
  554. /* GDB_STDOUT should be better already restored during these
  555. restoration callbacks. */
  556. set_batch_flag_and_restore_page_info save_page_info;
  557. scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
  558. {
  559. current_uiout->redirect (file);
  560. ui_out_redirect_pop redirect_popper (current_uiout);
  561. scoped_restore save_stdout
  562. = make_scoped_restore (&gdb_stdout, file);
  563. scoped_restore save_stderr
  564. = make_scoped_restore (&gdb_stderr, file);
  565. scoped_restore save_stdlog
  566. = make_scoped_restore (&gdb_stdlog, file);
  567. scoped_restore save_stdtarg
  568. = make_scoped_restore (&gdb_stdtarg, file);
  569. scoped_restore save_stdtargerr
  570. = make_scoped_restore (&gdb_stdtargerr, file);
  571. fn ();
  572. }
  573. }
  574. /* See gdbcmd.h. */
  575. void
  576. execute_fn_to_string (std::string &res, std::function<void(void)> fn,
  577. bool term_out)
  578. {
  579. string_file str_file (term_out);
  580. try
  581. {
  582. execute_fn_to_ui_file (&str_file, fn);
  583. }
  584. catch (...)
  585. {
  586. /* Finally. */
  587. res = std::move (str_file.string ());
  588. throw;
  589. }
  590. /* And finally. */
  591. res = std::move (str_file.string ());
  592. }
  593. /* See gdbcmd.h. */
  594. void
  595. execute_command_to_ui_file (struct ui_file *file,
  596. const char *p, int from_tty)
  597. {
  598. execute_fn_to_ui_file (file, [=]() { execute_command (p, from_tty); });
  599. }
  600. /* See gdbcmd.h. */
  601. void
  602. execute_command_to_string (std::string &res, const char *p, int from_tty,
  603. bool term_out)
  604. {
  605. execute_fn_to_string (res, [=]() { execute_command (p, from_tty); },
  606. term_out);
  607. }
  608. /* See gdbcmd.h. */
  609. void
  610. execute_command_to_string (const char *p, int from_tty,
  611. bool term_out)
  612. {
  613. std::string dummy;
  614. execute_fn_to_string (dummy, [=]() { execute_command (p, from_tty); },
  615. term_out);
  616. }
  617. /* When nonzero, cause dont_repeat to do nothing. This should only be
  618. set via prevent_dont_repeat. */
  619. static int suppress_dont_repeat = 0;
  620. /* See command.h */
  621. void
  622. dont_repeat (void)
  623. {
  624. struct ui *ui = current_ui;
  625. if (suppress_dont_repeat || server_command)
  626. return;
  627. /* If we aren't reading from standard input, we are saving the last
  628. thing read from stdin in line and don't want to delete it. Null
  629. lines won't repeat here in any case. */
  630. if (ui->instream == ui->stdin_stream)
  631. {
  632. *saved_command_line = 0;
  633. repeat_arguments = NULL;
  634. }
  635. }
  636. /* See command.h */
  637. const char *
  638. repeat_previous ()
  639. {
  640. /* Do not repeat this command, as this command is a repeating command. */
  641. dont_repeat ();
  642. /* We cannot free saved_command_line, as this line is being executed,
  643. so swap it with previous_saved_command_line. */
  644. std::swap (previous_saved_command_line, saved_command_line);
  645. std::swap (previous_repeat_arguments, repeat_arguments);
  646. const char *prev = skip_spaces (get_saved_command_line ());
  647. if (*prev == '\0')
  648. error (_("No previous command to relaunch"));
  649. return prev;
  650. }
  651. /* See command.h. */
  652. scoped_restore_tmpl<int>
  653. prevent_dont_repeat (void)
  654. {
  655. return make_scoped_restore (&suppress_dont_repeat, 1);
  656. }
  657. /* See command.h. */
  658. char *
  659. get_saved_command_line ()
  660. {
  661. return saved_command_line;
  662. }
  663. /* See command.h. */
  664. void
  665. save_command_line (const char *cmd)
  666. {
  667. xfree (previous_saved_command_line);
  668. previous_saved_command_line = saved_command_line;
  669. previous_repeat_arguments = repeat_arguments;
  670. saved_command_line = xstrdup (cmd);
  671. repeat_arguments = NULL;
  672. }
  673. /* Read a line from the stream "instream" without command line editing.
  674. It prints PROMPT once at the start.
  675. Action is compatible with "readline", e.g. space for the result is
  676. malloc'd and should be freed by the caller.
  677. A NULL return means end of file. */
  678. static char *
  679. gdb_readline_no_editing (const char *prompt)
  680. {
  681. struct buffer line_buffer;
  682. struct ui *ui = current_ui;
  683. /* Read from stdin if we are executing a user defined command. This
  684. is the right thing for prompt_for_continue, at least. */
  685. FILE *stream = ui->instream != NULL ? ui->instream : stdin;
  686. int fd = fileno (stream);
  687. buffer_init (&line_buffer);
  688. if (prompt != NULL)
  689. {
  690. /* Don't use a _filtered function here. It causes the assumed
  691. character position to be off, since the newline we read from
  692. the user is not accounted for. */
  693. printf_unfiltered ("%s", prompt);
  694. gdb_flush (gdb_stdout);
  695. }
  696. while (1)
  697. {
  698. int c;
  699. fd_set readfds;
  700. QUIT;
  701. /* Wait until at least one byte of data is available. Control-C
  702. can interrupt interruptible_select, but not fgetc. */
  703. FD_ZERO (&readfds);
  704. FD_SET (fd, &readfds);
  705. if (interruptible_select (fd + 1, &readfds, NULL, NULL, NULL) == -1)
  706. {
  707. if (errno == EINTR)
  708. {
  709. /* If this was ctrl-c, the QUIT above handles it. */
  710. continue;
  711. }
  712. perror_with_name (("select"));
  713. }
  714. c = fgetc (stream);
  715. if (c == EOF)
  716. {
  717. if (line_buffer.used_size > 0)
  718. /* The last line does not end with a newline. Return it, and
  719. if we are called again fgetc will still return EOF and
  720. we'll return NULL then. */
  721. break;
  722. xfree (buffer_finish (&line_buffer));
  723. return NULL;
  724. }
  725. if (c == '\n')
  726. {
  727. if (line_buffer.used_size > 0
  728. && line_buffer.buffer[line_buffer.used_size - 1] == '\r')
  729. line_buffer.used_size--;
  730. break;
  731. }
  732. buffer_grow_char (&line_buffer, c);
  733. }
  734. buffer_grow_char (&line_buffer, '\0');
  735. return buffer_finish (&line_buffer);
  736. }
  737. /* Variables which control command line editing and history
  738. substitution. These variables are given default values at the end
  739. of this file. */
  740. static bool command_editing_p;
  741. /* NOTE 1999-04-29: This variable will be static again, once we modify
  742. gdb to use the event loop as the default command loop and we merge
  743. event-top.c into this file, top.c. */
  744. /* static */ bool history_expansion_p;
  745. /* Should we write out the command history on exit? In order to write out
  746. the history both this flag must be true, and the history_filename
  747. variable must be set to something sensible. */
  748. static bool write_history_p;
  749. /* The name of the file in which GDB history will be written. If this is
  750. set to NULL, of the empty string then history will not be written. */
  751. static std::string history_filename;
  752. /* Implement 'show history save'. */
  753. static void
  754. show_write_history_p (struct ui_file *file, int from_tty,
  755. struct cmd_list_element *c, const char *value)
  756. {
  757. if (!write_history_p || !history_filename.empty ())
  758. gdb_printf (file, _("Saving of the history record on exit is %s.\n"),
  759. value);
  760. else
  761. gdb_printf (file, _("Saving of the history is disabled due to "
  762. "the value of 'history filename'.\n"));
  763. }
  764. /* The variable associated with the "set/show history size"
  765. command. The value -1 means unlimited, and -2 means undefined. */
  766. static int history_size_setshow_var = -2;
  767. static void
  768. show_history_size (struct ui_file *file, int from_tty,
  769. struct cmd_list_element *c, const char *value)
  770. {
  771. gdb_printf (file, _("The size of the command history is %s.\n"),
  772. value);
  773. }
  774. /* Variable associated with the "history remove-duplicates" option.
  775. The value -1 means unlimited. */
  776. static int history_remove_duplicates = 0;
  777. static void
  778. show_history_remove_duplicates (struct ui_file *file, int from_tty,
  779. struct cmd_list_element *c, const char *value)
  780. {
  781. gdb_printf (file,
  782. _("The number of history entries to look back at for "
  783. "duplicates is %s.\n"),
  784. value);
  785. }
  786. /* Implement 'show history filename'. */
  787. static void
  788. show_history_filename (struct ui_file *file, int from_tty,
  789. struct cmd_list_element *c, const char *value)
  790. {
  791. if (!history_filename.empty ())
  792. gdb_printf (file, _("The filename in which to record "
  793. "the command history is \"%ps\".\n"),
  794. styled_string (file_name_style.style (), value));
  795. else
  796. gdb_printf (file, _("There is no filename currently set for "
  797. "recording the command history in.\n"));
  798. }
  799. /* This is like readline(), but it has some gdb-specific behavior.
  800. gdb may want readline in both the synchronous and async modes during
  801. a single gdb invocation. At the ordinary top-level prompt we might
  802. be using the async readline. That means we can't use
  803. rl_pre_input_hook, since it doesn't work properly in async mode.
  804. However, for a secondary prompt (" >", such as occurs during a
  805. `define'), gdb wants a synchronous response.
  806. We used to call readline() directly, running it in synchronous
  807. mode. But mixing modes this way is not supported, and as of
  808. readline 5.x it no longer works; the arrow keys come unbound during
  809. the synchronous call. So we make a nested call into the event
  810. loop. That's what gdb_readline_wrapper is for. */
  811. /* A flag set as soon as gdb_readline_wrapper_line is called; we can't
  812. rely on gdb_readline_wrapper_result, which might still be NULL if
  813. the user types Control-D for EOF. */
  814. static int gdb_readline_wrapper_done;
  815. /* The result of the current call to gdb_readline_wrapper, once a newline
  816. is seen. */
  817. static char *gdb_readline_wrapper_result;
  818. /* Any intercepted hook. Operate-and-get-next sets this, expecting it
  819. to be called after the newline is processed (which will redisplay
  820. the prompt). But in gdb_readline_wrapper we will not get a new
  821. prompt until the next call, or until we return to the event loop.
  822. So we disable this hook around the newline and restore it before we
  823. return. */
  824. static void (*saved_after_char_processing_hook) (void);
  825. /* See top.h. */
  826. int
  827. gdb_in_secondary_prompt_p (struct ui *ui)
  828. {
  829. return ui->secondary_prompt_depth > 0;
  830. }
  831. /* This function is called when readline has seen a complete line of
  832. text. */
  833. static void
  834. gdb_readline_wrapper_line (gdb::unique_xmalloc_ptr<char> &&line)
  835. {
  836. gdb_assert (!gdb_readline_wrapper_done);
  837. gdb_readline_wrapper_result = line.release ();
  838. gdb_readline_wrapper_done = 1;
  839. /* Prevent operate-and-get-next from acting too early. */
  840. saved_after_char_processing_hook = after_char_processing_hook;
  841. after_char_processing_hook = NULL;
  842. /* Prevent parts of the prompt from being redisplayed if annotations
  843. are enabled, and readline's state getting out of sync. We'll
  844. reinstall the callback handler, which puts the terminal in raw
  845. mode (or in readline lingo, in prepped state), when we're next
  846. ready to process user input, either in display_gdb_prompt, or if
  847. we're handling an asynchronous target event and running in the
  848. background, just before returning to the event loop to process
  849. further input (or more target events). */
  850. if (current_ui->command_editing)
  851. gdb_rl_callback_handler_remove ();
  852. }
  853. class gdb_readline_wrapper_cleanup
  854. {
  855. public:
  856. gdb_readline_wrapper_cleanup ()
  857. : m_handler_orig (current_ui->input_handler),
  858. m_already_prompted_orig (current_ui->command_editing
  859. ? rl_already_prompted : 0),
  860. m_target_is_async_orig (target_is_async_p ()),
  861. m_save_ui (&current_ui)
  862. {
  863. current_ui->input_handler = gdb_readline_wrapper_line;
  864. current_ui->secondary_prompt_depth++;
  865. if (m_target_is_async_orig)
  866. target_async (0);
  867. }
  868. ~gdb_readline_wrapper_cleanup ()
  869. {
  870. struct ui *ui = current_ui;
  871. if (ui->command_editing)
  872. rl_already_prompted = m_already_prompted_orig;
  873. gdb_assert (ui->input_handler == gdb_readline_wrapper_line);
  874. ui->input_handler = m_handler_orig;
  875. /* Don't restore our input handler in readline yet. That would make
  876. readline prep the terminal (putting it in raw mode), while the
  877. line we just read may trigger execution of a command that expects
  878. the terminal in the default cooked/canonical mode, such as e.g.,
  879. running Python's interactive online help utility. See
  880. gdb_readline_wrapper_line for when we'll reinstall it. */
  881. gdb_readline_wrapper_result = NULL;
  882. gdb_readline_wrapper_done = 0;
  883. ui->secondary_prompt_depth--;
  884. gdb_assert (ui->secondary_prompt_depth >= 0);
  885. after_char_processing_hook = saved_after_char_processing_hook;
  886. saved_after_char_processing_hook = NULL;
  887. if (m_target_is_async_orig)
  888. target_async (1);
  889. }
  890. DISABLE_COPY_AND_ASSIGN (gdb_readline_wrapper_cleanup);
  891. private:
  892. void (*m_handler_orig) (gdb::unique_xmalloc_ptr<char> &&);
  893. int m_already_prompted_orig;
  894. /* Whether the target was async. */
  895. int m_target_is_async_orig;
  896. /* Processing events may change the current UI. */
  897. scoped_restore_tmpl<struct ui *> m_save_ui;
  898. };
  899. char *
  900. gdb_readline_wrapper (const char *prompt)
  901. {
  902. struct ui *ui = current_ui;
  903. gdb_readline_wrapper_cleanup cleanup;
  904. /* Display our prompt and prevent double prompt display. Don't pass
  905. down a NULL prompt, since that has special meaning for
  906. display_gdb_prompt -- it indicates a request to print the primary
  907. prompt, while we want a secondary prompt here. */
  908. display_gdb_prompt (prompt != NULL ? prompt : "");
  909. if (ui->command_editing)
  910. rl_already_prompted = 1;
  911. if (after_char_processing_hook)
  912. (*after_char_processing_hook) ();
  913. gdb_assert (after_char_processing_hook == NULL);
  914. while (gdb_do_one_event () >= 0)
  915. if (gdb_readline_wrapper_done)
  916. break;
  917. return gdb_readline_wrapper_result;
  918. }
  919. /* The current saved history number from operate-and-get-next.
  920. This is -1 if not valid. */
  921. static int operate_saved_history = -1;
  922. /* This is put on the appropriate hook and helps operate-and-get-next
  923. do its work. */
  924. static void
  925. gdb_rl_operate_and_get_next_completion (void)
  926. {
  927. int delta = where_history () - operate_saved_history;
  928. /* The `key' argument to rl_get_previous_history is ignored. */
  929. rl_get_previous_history (delta, 0);
  930. operate_saved_history = -1;
  931. /* readline doesn't automatically update the display for us. */
  932. rl_redisplay ();
  933. after_char_processing_hook = NULL;
  934. rl_pre_input_hook = NULL;
  935. }
  936. /* This is a gdb-local readline command handler. It accepts the
  937. current command line (like RET does) and, if this command was taken
  938. from the history, arranges for the next command in the history to
  939. appear on the command line when the prompt returns.
  940. We ignore the arguments. */
  941. static int
  942. gdb_rl_operate_and_get_next (int count, int key)
  943. {
  944. int where;
  945. /* Use the async hook. */
  946. after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
  947. /* Find the current line, and find the next line to use. */
  948. where = where_history();
  949. if ((history_is_stifled () && (history_length >= history_max_entries))
  950. || (where >= history_length - 1))
  951. operate_saved_history = where;
  952. else
  953. operate_saved_history = where + 1;
  954. return rl_newline (1, key);
  955. }
  956. /* Number of user commands executed during this session. */
  957. static int command_count = 0;
  958. /* Add the user command COMMAND to the input history list. */
  959. void
  960. gdb_add_history (const char *command)
  961. {
  962. command_count++;
  963. if (history_remove_duplicates != 0)
  964. {
  965. int lookbehind;
  966. int lookbehind_threshold;
  967. /* The lookbehind threshold for finding a duplicate history entry is
  968. bounded by command_count because we can't meaningfully delete
  969. history entries that are already stored in the history file since
  970. the history file is appended to. */
  971. if (history_remove_duplicates == -1
  972. || history_remove_duplicates > command_count)
  973. lookbehind_threshold = command_count;
  974. else
  975. lookbehind_threshold = history_remove_duplicates;
  976. using_history ();
  977. for (lookbehind = 0; lookbehind < lookbehind_threshold; lookbehind++)
  978. {
  979. HIST_ENTRY *temp = previous_history ();
  980. if (temp == NULL)
  981. break;
  982. if (strcmp (temp->line, command) == 0)
  983. {
  984. HIST_ENTRY *prev = remove_history (where_history ());
  985. command_count--;
  986. free_history_entry (prev);
  987. break;
  988. }
  989. }
  990. using_history ();
  991. }
  992. add_history (command);
  993. }
  994. /* Safely append new history entries to the history file in a corruption-free
  995. way using an intermediate local history file. */
  996. static void
  997. gdb_safe_append_history (void)
  998. {
  999. int ret, saved_errno;
  1000. std::string local_history_filename
  1001. = string_printf ("%s-gdb%ld~", history_filename.c_str (), (long) getpid ());
  1002. ret = rename (history_filename.c_str (), local_history_filename.c_str ());
  1003. saved_errno = errno;
  1004. if (ret < 0 && saved_errno != ENOENT)
  1005. {
  1006. warning (_("Could not rename %ps to %ps: %s"),
  1007. styled_string (file_name_style.style (),
  1008. history_filename.c_str ()),
  1009. styled_string (file_name_style.style (),
  1010. local_history_filename.c_str ()),
  1011. safe_strerror (saved_errno));
  1012. }
  1013. else
  1014. {
  1015. if (ret < 0)
  1016. {
  1017. /* If the rename failed with ENOENT then either the global history
  1018. file never existed in the first place or another GDB process is
  1019. currently appending to it (and has thus temporarily renamed it).
  1020. Since we can't distinguish between these two cases, we have to
  1021. conservatively assume the first case and therefore must write out
  1022. (not append) our known history to our local history file and try
  1023. to move it back anyway. Otherwise a global history file would
  1024. never get created! */
  1025. gdb_assert (saved_errno == ENOENT);
  1026. write_history (local_history_filename.c_str ());
  1027. }
  1028. else
  1029. {
  1030. append_history (command_count, local_history_filename.c_str ());
  1031. if (history_is_stifled ())
  1032. history_truncate_file (local_history_filename.c_str (),
  1033. history_max_entries);
  1034. }
  1035. ret = rename (local_history_filename.c_str (), history_filename.c_str ());
  1036. saved_errno = errno;
  1037. if (ret < 0 && saved_errno != EEXIST)
  1038. warning (_("Could not rename %s to %s: %s"),
  1039. local_history_filename.c_str (), history_filename.c_str (),
  1040. safe_strerror (saved_errno));
  1041. }
  1042. }
  1043. /* Read one line from the command input stream `instream' into a local
  1044. static buffer. The buffer is made bigger as necessary. Returns
  1045. the address of the start of the line.
  1046. NULL is returned for end of file.
  1047. This routine either uses fancy command line editing or simple input
  1048. as the user has requested. */
  1049. const char *
  1050. command_line_input (const char *prompt_arg, const char *annotation_suffix)
  1051. {
  1052. static struct buffer cmd_line_buffer;
  1053. static int cmd_line_buffer_initialized;
  1054. struct ui *ui = current_ui;
  1055. const char *prompt = prompt_arg;
  1056. char *cmd;
  1057. int from_tty = ui->instream == ui->stdin_stream;
  1058. /* The annotation suffix must be non-NULL. */
  1059. if (annotation_suffix == NULL)
  1060. annotation_suffix = "";
  1061. if (from_tty && annotation_level > 1)
  1062. {
  1063. char *local_prompt;
  1064. local_prompt
  1065. = (char *) alloca ((prompt == NULL ? 0 : strlen (prompt))
  1066. + strlen (annotation_suffix) + 40);
  1067. if (prompt == NULL)
  1068. local_prompt[0] = '\0';
  1069. else
  1070. strcpy (local_prompt, prompt);
  1071. strcat (local_prompt, "\n\032\032");
  1072. strcat (local_prompt, annotation_suffix);
  1073. strcat (local_prompt, "\n");
  1074. prompt = local_prompt;
  1075. }
  1076. if (!cmd_line_buffer_initialized)
  1077. {
  1078. buffer_init (&cmd_line_buffer);
  1079. cmd_line_buffer_initialized = 1;
  1080. }
  1081. /* Starting a new command line. */
  1082. cmd_line_buffer.used_size = 0;
  1083. #ifdef SIGTSTP
  1084. if (job_control)
  1085. signal (SIGTSTP, handle_sigtstp);
  1086. #endif
  1087. while (1)
  1088. {
  1089. gdb::unique_xmalloc_ptr<char> rl;
  1090. /* Make sure that all output has been output. Some machines may
  1091. let you get away with leaving out some of the gdb_flush, but
  1092. not all. */
  1093. gdb_flush (gdb_stdout);
  1094. gdb_flush (gdb_stderr);
  1095. if (!source_file_name.empty ())
  1096. ++source_line_number;
  1097. if (from_tty && annotation_level > 1)
  1098. printf_unfiltered ("\n\032\032pre-%s\n", annotation_suffix);
  1099. /* Don't use fancy stuff if not talking to stdin. */
  1100. if (deprecated_readline_hook
  1101. && from_tty
  1102. && input_interactive_p (current_ui))
  1103. {
  1104. rl.reset ((*deprecated_readline_hook) (prompt));
  1105. }
  1106. else if (command_editing_p
  1107. && from_tty
  1108. && input_interactive_p (current_ui))
  1109. {
  1110. rl.reset (gdb_readline_wrapper (prompt));
  1111. }
  1112. else
  1113. {
  1114. rl.reset (gdb_readline_no_editing (prompt));
  1115. }
  1116. cmd = handle_line_of_input (&cmd_line_buffer, rl.get (),
  1117. 0, annotation_suffix);
  1118. if (cmd == (char *) EOF)
  1119. {
  1120. cmd = NULL;
  1121. break;
  1122. }
  1123. if (cmd != NULL)
  1124. break;
  1125. /* Got partial input. I.e., got a line that ends with a
  1126. continuation character (backslash). Suppress printing the
  1127. prompt again. */
  1128. prompt = NULL;
  1129. }
  1130. #ifdef SIGTSTP
  1131. if (job_control)
  1132. signal (SIGTSTP, SIG_DFL);
  1133. #endif
  1134. return cmd;
  1135. }
  1136. /* See top.h. */
  1137. void
  1138. print_gdb_version (struct ui_file *stream, bool interactive)
  1139. {
  1140. /* From GNU coding standards, first line is meant to be easy for a
  1141. program to parse, and is just canonical program name and version
  1142. number, which starts after last space. */
  1143. std::string v_str = string_printf ("GNU gdb %s%s", PKGVERSION, version);
  1144. gdb_printf (stream, "%ps\n",
  1145. styled_string (version_style.style (), v_str.c_str ()));
  1146. /* Second line is a copyright notice. */
  1147. gdb_printf (stream,
  1148. "Copyright (C) 2022 Free Software Foundation, Inc.\n");
  1149. /* Following the copyright is a brief statement that the program is
  1150. free software, that users are free to copy and change it on
  1151. certain conditions, that it is covered by the GNU GPL, and that
  1152. there is no warranty. */
  1153. gdb_printf (stream, "\
  1154. License GPLv3+: GNU GPL version 3 or later <%ps>\
  1155. \nThis is free software: you are free to change and redistribute it.\n\
  1156. There is NO WARRANTY, to the extent permitted by law.",
  1157. styled_string (file_name_style.style (),
  1158. "http://gnu.org/licenses/gpl.html"));
  1159. if (!interactive)
  1160. return;
  1161. gdb_printf (stream, ("\nType \"show copying\" and "
  1162. "\"show warranty\" for details.\n"));
  1163. /* After the required info we print the configuration information. */
  1164. gdb_printf (stream, "This GDB was configured as \"");
  1165. if (strcmp (host_name, target_name) != 0)
  1166. {
  1167. gdb_printf (stream, "--host=%s --target=%s",
  1168. host_name, target_name);
  1169. }
  1170. else
  1171. {
  1172. gdb_printf (stream, "%s", host_name);
  1173. }
  1174. gdb_printf (stream, "\".\n");
  1175. gdb_printf (stream, _("Type \"show configuration\" "
  1176. "for configuration details.\n"));
  1177. if (REPORT_BUGS_TO[0])
  1178. {
  1179. gdb_printf (stream,
  1180. _("For bug reporting instructions, please see:\n"));
  1181. gdb_printf (stream, "%ps.\n",
  1182. styled_string (file_name_style.style (),
  1183. REPORT_BUGS_TO));
  1184. }
  1185. gdb_printf (stream,
  1186. _("Find the GDB manual and other documentation \
  1187. resources online at:\n <%ps>."),
  1188. styled_string (file_name_style.style (),
  1189. "http://www.gnu.org/software/gdb/documentation/"));
  1190. gdb_printf (stream, "\n\n");
  1191. gdb_printf (stream, _("For help, type \"help\".\n"));
  1192. gdb_printf (stream,
  1193. _("Type \"apropos word\" to search for commands \
  1194. related to \"word\"."));
  1195. }
  1196. /* Print the details of GDB build-time configuration. */
  1197. void
  1198. print_gdb_configuration (struct ui_file *stream)
  1199. {
  1200. gdb_printf (stream, _("\
  1201. This GDB was configured as follows:\n\
  1202. configure --host=%s --target=%s\n\
  1203. "), host_name, target_name);
  1204. gdb_printf (stream, _("\
  1205. --with-auto-load-dir=%s\n\
  1206. --with-auto-load-safe-path=%s\n\
  1207. "), AUTO_LOAD_DIR, AUTO_LOAD_SAFE_PATH);
  1208. #if HAVE_LIBEXPAT
  1209. gdb_printf (stream, _("\
  1210. --with-expat\n\
  1211. "));
  1212. #else
  1213. gdb_printf (stream, _("\
  1214. --without-expat\n\
  1215. "));
  1216. #endif
  1217. if (GDB_DATADIR[0])
  1218. gdb_printf (stream, _("\
  1219. --with-gdb-datadir=%s%s\n\
  1220. "), GDB_DATADIR, GDB_DATADIR_RELOCATABLE ? " (relocatable)" : "");
  1221. #ifdef ICONV_BIN
  1222. gdb_printf (stream, _("\
  1223. --with-iconv-bin=%s%s\n\
  1224. "), ICONV_BIN, ICONV_BIN_RELOCATABLE ? " (relocatable)" : "");
  1225. #endif
  1226. if (JIT_READER_DIR[0])
  1227. gdb_printf (stream, _("\
  1228. --with-jit-reader-dir=%s%s\n\
  1229. "), JIT_READER_DIR, JIT_READER_DIR_RELOCATABLE ? " (relocatable)" : "");
  1230. #if HAVE_LIBUNWIND_IA64_H
  1231. gdb_printf (stream, _("\
  1232. --with-libunwind-ia64\n\
  1233. "));
  1234. #else
  1235. gdb_printf (stream, _("\
  1236. --without-libunwind-ia64\n\
  1237. "));
  1238. #endif
  1239. #if HAVE_LIBLZMA
  1240. gdb_printf (stream, _("\
  1241. --with-lzma\n\
  1242. "));
  1243. #else
  1244. gdb_printf (stream, _("\
  1245. --without-lzma\n\
  1246. "));
  1247. #endif
  1248. #if HAVE_LIBBABELTRACE
  1249. gdb_printf (stream, _("\
  1250. --with-babeltrace\n\
  1251. "));
  1252. #else
  1253. gdb_printf (stream, _("\
  1254. --without-babeltrace\n\
  1255. "));
  1256. #endif
  1257. #if HAVE_LIBIPT
  1258. gdb_printf (stream, _("\
  1259. --with-intel-pt\n\
  1260. "));
  1261. #else
  1262. gdb_printf (stream, _("\
  1263. --without-intel-pt\n\
  1264. "));
  1265. #endif
  1266. #if HAVE_LIBMPFR
  1267. gdb_printf (stream, _("\
  1268. --with-mpfr\n\
  1269. "));
  1270. #else
  1271. gdb_printf (stream, _("\
  1272. --without-mpfr\n\
  1273. "));
  1274. #endif
  1275. #if HAVE_LIBXXHASH
  1276. gdb_printf (stream, _("\
  1277. --with-xxhash\n\
  1278. "));
  1279. #else
  1280. gdb_printf (stream, _("\
  1281. --without-xxhash\n\
  1282. "));
  1283. #endif
  1284. #ifdef WITH_PYTHON_PATH
  1285. gdb_printf (stream, _("\
  1286. --with-python=%s%s\n\
  1287. "), WITH_PYTHON_PATH, PYTHON_PATH_RELOCATABLE ? " (relocatable)" : "");
  1288. #else
  1289. gdb_printf (stream, _("\
  1290. --without-python\n\
  1291. "));
  1292. #endif
  1293. #ifdef WITH_PYTHON_LIBDIR
  1294. gdb_printf (stream, _("\
  1295. --with-python-libdir=%s%s\n\
  1296. "), WITH_PYTHON_LIBDIR, PYTHON_LIBDIR_RELOCATABLE ? " (relocatable)" : "");
  1297. #else
  1298. gdb_printf (stream, _("\
  1299. --without-python-libdir\n\
  1300. "));
  1301. #endif
  1302. #if HAVE_LIBDEBUGINFOD
  1303. gdb_printf (stream, _("\
  1304. --with-debuginfod\n\
  1305. "));
  1306. #else
  1307. gdb_printf (stream, _("\
  1308. --without-debuginfod\n\
  1309. "));
  1310. #endif
  1311. #if HAVE_GUILE
  1312. gdb_printf (stream, _("\
  1313. --with-guile\n\
  1314. "));
  1315. #else
  1316. gdb_printf (stream, _("\
  1317. --without-guile\n\
  1318. "));
  1319. #endif
  1320. #if HAVE_SOURCE_HIGHLIGHT
  1321. gdb_printf (stream, _("\
  1322. --enable-source-highlight\n\
  1323. "));
  1324. #else
  1325. gdb_printf (stream, _("\
  1326. --disable-source-highlight\n\
  1327. "));
  1328. #endif
  1329. #if CXX_STD_THREAD
  1330. gdb_printf (stream, _("\
  1331. --enable-threading\n\
  1332. "));
  1333. #else
  1334. gdb_printf (stream, _("\
  1335. --disable-threading\n\
  1336. "));
  1337. #endif
  1338. #ifdef RELOC_SRCDIR
  1339. gdb_printf (stream, _("\
  1340. --with-relocated-sources=%s\n\
  1341. "), RELOC_SRCDIR);
  1342. #endif
  1343. if (DEBUGDIR[0])
  1344. gdb_printf (stream, _("\
  1345. --with-separate-debug-dir=%s%s\n\
  1346. "), DEBUGDIR, DEBUGDIR_RELOCATABLE ? " (relocatable)" : "");
  1347. if (TARGET_SYSTEM_ROOT[0])
  1348. gdb_printf (stream, _("\
  1349. --with-sysroot=%s%s\n\
  1350. "), TARGET_SYSTEM_ROOT, TARGET_SYSTEM_ROOT_RELOCATABLE ? " (relocatable)" : "");
  1351. if (SYSTEM_GDBINIT[0])
  1352. gdb_printf (stream, _("\
  1353. --with-system-gdbinit=%s%s\n\
  1354. "), SYSTEM_GDBINIT, SYSTEM_GDBINIT_RELOCATABLE ? " (relocatable)" : "");
  1355. if (SYSTEM_GDBINIT_DIR[0])
  1356. gdb_printf (stream, _("\
  1357. --with-system-gdbinit-dir=%s%s\n\
  1358. "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
  1359. /* We assume "relocatable" will be printed at least once, thus we always
  1360. print this text. It's a reasonably safe assumption for now. */
  1361. gdb_printf (stream, _("\n\
  1362. (\"Relocatable\" means the directory can be moved with the GDB installation\n\
  1363. tree, and GDB will still find it.)\n\
  1364. "));
  1365. }
  1366. /* The current top level prompt, settable with "set prompt", and/or
  1367. with the python `gdb.prompt_hook' hook. */
  1368. static std::string top_prompt;
  1369. /* Access method for the GDB prompt string. */
  1370. const std::string &
  1371. get_prompt ()
  1372. {
  1373. return top_prompt;
  1374. }
  1375. /* Set method for the GDB prompt string. */
  1376. void
  1377. set_prompt (const char *s)
  1378. {
  1379. top_prompt = s;
  1380. }
  1381. /* Kills or detaches the given inferior, depending on how we originally
  1382. gained control of it. */
  1383. static void
  1384. kill_or_detach (inferior *inf, int from_tty)
  1385. {
  1386. if (inf->pid == 0)
  1387. return;
  1388. thread_info *thread = any_thread_of_inferior (inf);
  1389. if (thread != NULL)
  1390. {
  1391. switch_to_thread (thread);
  1392. /* Leave core files alone. */
  1393. if (target_has_execution ())
  1394. {
  1395. if (inf->attach_flag)
  1396. target_detach (inf, from_tty);
  1397. else
  1398. target_kill ();
  1399. }
  1400. }
  1401. }
  1402. /* Prints info about what GDB will do to inferior INF on a "quit". OUT is
  1403. where to collect the output. */
  1404. static void
  1405. print_inferior_quit_action (inferior *inf, ui_file *out)
  1406. {
  1407. if (inf->pid == 0)
  1408. return;
  1409. if (inf->attach_flag)
  1410. gdb_printf (out,
  1411. _("\tInferior %d [%s] will be detached.\n"), inf->num,
  1412. target_pid_to_str (ptid_t (inf->pid)).c_str ());
  1413. else
  1414. gdb_printf (out,
  1415. _("\tInferior %d [%s] will be killed.\n"), inf->num,
  1416. target_pid_to_str (ptid_t (inf->pid)).c_str ());
  1417. }
  1418. /* If necessary, make the user confirm that we should quit. Return
  1419. non-zero if we should quit, zero if we shouldn't. */
  1420. int
  1421. quit_confirm (void)
  1422. {
  1423. /* Don't even ask if we're only debugging a core file inferior. */
  1424. if (!have_live_inferiors ())
  1425. return 1;
  1426. /* Build the query string as a single string. */
  1427. string_file stb;
  1428. stb.puts (_("A debugging session is active.\n\n"));
  1429. for (inferior *inf : all_inferiors ())
  1430. print_inferior_quit_action (inf, &stb);
  1431. stb.puts (_("\nQuit anyway? "));
  1432. return query ("%s", stb.c_str ());
  1433. }
  1434. /* Prepare to exit GDB cleanly by undoing any changes made to the
  1435. terminal so that we leave the terminal in the state we acquired it. */
  1436. static void
  1437. undo_terminal_modifications_before_exit (void)
  1438. {
  1439. struct ui *saved_top_level = current_ui;
  1440. target_terminal::ours ();
  1441. current_ui = main_ui;
  1442. #if defined(TUI)
  1443. tui_disable ();
  1444. #endif
  1445. gdb_disable_readline ();
  1446. current_ui = saved_top_level;
  1447. }
  1448. /* Quit without asking for confirmation. */
  1449. void
  1450. quit_force (int *exit_arg, int from_tty)
  1451. {
  1452. int exit_code = 0;
  1453. /* An optional expression may be used to cause gdb to terminate with the
  1454. value of that expression. */
  1455. if (exit_arg)
  1456. exit_code = *exit_arg;
  1457. else if (return_child_result)
  1458. exit_code = return_child_result_value;
  1459. gdb::observers::gdb_exiting.notify (exit_code);
  1460. undo_terminal_modifications_before_exit ();
  1461. /* We want to handle any quit errors and exit regardless. */
  1462. /* Get out of tfind mode, and kill or detach all inferiors. */
  1463. try
  1464. {
  1465. disconnect_tracing ();
  1466. for (inferior *inf : all_inferiors ())
  1467. kill_or_detach (inf, from_tty);
  1468. }
  1469. catch (const gdb_exception &ex)
  1470. {
  1471. exception_print (gdb_stderr, ex);
  1472. }
  1473. /* Give all pushed targets a chance to do minimal cleanup, and pop
  1474. them all out. */
  1475. for (inferior *inf : all_inferiors ())
  1476. {
  1477. switch_to_inferior_no_thread (inf);
  1478. try
  1479. {
  1480. pop_all_targets ();
  1481. }
  1482. catch (const gdb_exception &ex)
  1483. {
  1484. exception_print (gdb_stderr, ex);
  1485. }
  1486. }
  1487. /* Save the history information if it is appropriate to do so. */
  1488. try
  1489. {
  1490. if (write_history_p && !history_filename.empty ())
  1491. {
  1492. int save = 0;
  1493. /* History is currently shared between all UIs. If there's
  1494. any UI with a terminal, save history. */
  1495. for (ui *ui : all_uis ())
  1496. {
  1497. if (input_interactive_p (ui))
  1498. {
  1499. save = 1;
  1500. break;
  1501. }
  1502. }
  1503. if (save)
  1504. gdb_safe_append_history ();
  1505. }
  1506. }
  1507. catch (const gdb_exception &ex)
  1508. {
  1509. exception_print (gdb_stderr, ex);
  1510. }
  1511. /* Destroy any values currently allocated now instead of leaving it
  1512. to global destructors, because that may be too late. For
  1513. example, the destructors of xmethod values call into the Python
  1514. runtime, which is finalized via a final cleanup. */
  1515. finalize_values ();
  1516. /* Do any final cleanups before exiting. */
  1517. try
  1518. {
  1519. do_final_cleanups ();
  1520. }
  1521. catch (const gdb_exception &ex)
  1522. {
  1523. exception_print (gdb_stderr, ex);
  1524. }
  1525. exit (exit_code);
  1526. }
  1527. /* The value of the "interactive-mode" setting. */
  1528. static enum auto_boolean interactive_mode = AUTO_BOOLEAN_AUTO;
  1529. /* Implement the "show interactive-mode" option. */
  1530. static void
  1531. show_interactive_mode (struct ui_file *file, int from_tty,
  1532. struct cmd_list_element *c,
  1533. const char *value)
  1534. {
  1535. if (interactive_mode == AUTO_BOOLEAN_AUTO)
  1536. gdb_printf (file, "Debugger's interactive mode "
  1537. "is %s (currently %s).\n",
  1538. value, input_interactive_p (current_ui) ? "on" : "off");
  1539. else
  1540. gdb_printf (file, "Debugger's interactive mode is %s.\n", value);
  1541. }
  1542. /* Returns whether GDB is running on an interactive terminal. */
  1543. int
  1544. input_interactive_p (struct ui *ui)
  1545. {
  1546. if (batch_flag)
  1547. return 0;
  1548. if (interactive_mode != AUTO_BOOLEAN_AUTO)
  1549. return interactive_mode == AUTO_BOOLEAN_TRUE;
  1550. return ui->input_interactive_p;
  1551. }
  1552. static void
  1553. dont_repeat_command (const char *ignored, int from_tty)
  1554. {
  1555. /* Can't call dont_repeat here because we're not necessarily reading
  1556. from stdin. */
  1557. *saved_command_line = 0;
  1558. }
  1559. /* Functions to manipulate command line editing control variables. */
  1560. /* Number of commands to print in each call to show_commands. */
  1561. #define Hist_print 10
  1562. void
  1563. show_commands (const char *args, int from_tty)
  1564. {
  1565. /* Index for history commands. Relative to history_base. */
  1566. int offset;
  1567. /* Number of the history entry which we are planning to display next.
  1568. Relative to history_base. */
  1569. static int num = 0;
  1570. /* Print out some of the commands from the command history. */
  1571. if (args)
  1572. {
  1573. if (args[0] == '+' && args[1] == '\0')
  1574. /* "info editing +" should print from the stored position. */
  1575. ;
  1576. else
  1577. /* "info editing <exp>" should print around command number <exp>. */
  1578. num = (parse_and_eval_long (args) - history_base) - Hist_print / 2;
  1579. }
  1580. /* "show commands" means print the last Hist_print commands. */
  1581. else
  1582. {
  1583. num = history_length - Hist_print;
  1584. }
  1585. if (num < 0)
  1586. num = 0;
  1587. /* If there are at least Hist_print commands, we want to display the last
  1588. Hist_print rather than, say, the last 6. */
  1589. if (history_length - num < Hist_print)
  1590. {
  1591. num = history_length - Hist_print;
  1592. if (num < 0)
  1593. num = 0;
  1594. }
  1595. for (offset = num;
  1596. offset < num + Hist_print && offset < history_length;
  1597. offset++)
  1598. {
  1599. gdb_printf ("%5d %s\n", history_base + offset,
  1600. (history_get (history_base + offset))->line);
  1601. }
  1602. /* The next command we want to display is the next one that we haven't
  1603. displayed yet. */
  1604. num += Hist_print;
  1605. /* If the user repeats this command with return, it should do what
  1606. "show commands +" does. This is unnecessary if arg is null,
  1607. because "show commands +" is not useful after "show commands". */
  1608. if (from_tty && args)
  1609. set_repeat_arguments ("+");
  1610. }
  1611. /* Update the size of our command history file to HISTORY_SIZE.
  1612. A HISTORY_SIZE of -1 stands for unlimited. */
  1613. static void
  1614. set_readline_history_size (int history_size)
  1615. {
  1616. gdb_assert (history_size >= -1);
  1617. if (history_size == -1)
  1618. unstifle_history ();
  1619. else
  1620. stifle_history (history_size);
  1621. }
  1622. /* Called by do_setshow_command. */
  1623. static void
  1624. set_history_size_command (const char *args,
  1625. int from_tty, struct cmd_list_element *c)
  1626. {
  1627. set_readline_history_size (history_size_setshow_var);
  1628. }
  1629. bool info_verbose = false; /* Default verbose msgs off. */
  1630. /* Called by do_set_command. An elaborate joke. */
  1631. void
  1632. set_verbose (const char *args, int from_tty, struct cmd_list_element *c)
  1633. {
  1634. const char *cmdname = "verbose";
  1635. struct cmd_list_element *showcmd;
  1636. showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, NULL, 1);
  1637. gdb_assert (showcmd != NULL && showcmd != CMD_LIST_AMBIGUOUS);
  1638. if (c->doc && c->doc_allocated)
  1639. xfree ((char *) c->doc);
  1640. if (showcmd->doc && showcmd->doc_allocated)
  1641. xfree ((char *) showcmd->doc);
  1642. if (info_verbose)
  1643. {
  1644. c->doc = _("Set verbose printing of informational messages.");
  1645. showcmd->doc = _("Show verbose printing of informational messages.");
  1646. }
  1647. else
  1648. {
  1649. c->doc = _("Set verbosity.");
  1650. showcmd->doc = _("Show verbosity.");
  1651. }
  1652. c->doc_allocated = 0;
  1653. showcmd->doc_allocated = 0;
  1654. }
  1655. /* Init the history buffer. Note that we are called after the init file(s)
  1656. have been read so that the user can change the history file via his
  1657. .gdbinit file (for instance). The GDBHISTFILE environment variable
  1658. overrides all of this. */
  1659. void
  1660. init_history (void)
  1661. {
  1662. const char *tmpenv;
  1663. tmpenv = getenv ("GDBHISTSIZE");
  1664. if (tmpenv)
  1665. {
  1666. long var;
  1667. int saved_errno;
  1668. char *endptr;
  1669. tmpenv = skip_spaces (tmpenv);
  1670. errno = 0;
  1671. var = strtol (tmpenv, &endptr, 10);
  1672. saved_errno = errno;
  1673. endptr = skip_spaces (endptr);
  1674. /* If GDBHISTSIZE is non-numeric then ignore it. If GDBHISTSIZE is the
  1675. empty string, a negative number or a huge positive number (larger than
  1676. INT_MAX) then set the history size to unlimited. Otherwise set our
  1677. history size to the number we have read. This behavior is consistent
  1678. with how bash handles HISTSIZE. */
  1679. if (*endptr != '\0')
  1680. ;
  1681. else if (*tmpenv == '\0'
  1682. || var < 0
  1683. || var > INT_MAX
  1684. /* On targets where INT_MAX == LONG_MAX, we have to look at
  1685. errno after calling strtol to distinguish between a value that
  1686. is exactly INT_MAX and an overflowing value that was clamped
  1687. to INT_MAX. */
  1688. || (var == INT_MAX && saved_errno == ERANGE))
  1689. history_size_setshow_var = -1;
  1690. else
  1691. history_size_setshow_var = var;
  1692. }
  1693. /* If neither the init file nor GDBHISTSIZE has set a size yet, pick the
  1694. default. */
  1695. if (history_size_setshow_var == -2)
  1696. history_size_setshow_var = 256;
  1697. set_readline_history_size (history_size_setshow_var);
  1698. if (!history_filename.empty ())
  1699. read_history (history_filename.c_str ());
  1700. }
  1701. static void
  1702. show_prompt (struct ui_file *file, int from_tty,
  1703. struct cmd_list_element *c, const char *value)
  1704. {
  1705. gdb_printf (file, _("Gdb's prompt is \"%s\".\n"), value);
  1706. }
  1707. /* "set editing" command. */
  1708. static void
  1709. set_editing (const char *args, int from_tty, struct cmd_list_element *c)
  1710. {
  1711. change_line_handler (set_editing_cmd_var);
  1712. /* Update the control variable so that MI's =cmd-param-changed event
  1713. shows the correct value. */
  1714. set_editing_cmd_var = current_ui->command_editing;
  1715. }
  1716. static void
  1717. show_editing (struct ui_file *file, int from_tty,
  1718. struct cmd_list_element *c, const char *value)
  1719. {
  1720. gdb_printf (file, _("Editing of command lines as "
  1721. "they are typed is %s.\n"),
  1722. current_ui->command_editing ? _("on") : _("off"));
  1723. }
  1724. static void
  1725. show_annotation_level (struct ui_file *file, int from_tty,
  1726. struct cmd_list_element *c, const char *value)
  1727. {
  1728. gdb_printf (file, _("Annotation_level is %s.\n"), value);
  1729. }
  1730. static void
  1731. show_exec_done_display_p (struct ui_file *file, int from_tty,
  1732. struct cmd_list_element *c, const char *value)
  1733. {
  1734. gdb_printf (file, _("Notification of completion for "
  1735. "asynchronous execution commands is %s.\n"),
  1736. value);
  1737. }
  1738. /* New values of the "data-directory" parameter are staged here.
  1739. Extension languages, for example Python's gdb.parameter API, will read
  1740. the value directory from this variable, so we must ensure that this
  1741. always contains the correct value. */
  1742. static std::string staged_gdb_datadir;
  1743. /* "set" command for the gdb_datadir configuration variable. */
  1744. static void
  1745. set_gdb_datadir (const char *args, int from_tty, struct cmd_list_element *c)
  1746. {
  1747. set_gdb_data_directory (staged_gdb_datadir.c_str ());
  1748. /* SET_GDB_DATA_DIRECTORY will resolve relative paths in
  1749. STAGED_GDB_DATADIR, so we now copy the value from GDB_DATADIR
  1750. back into STAGED_GDB_DATADIR so the extension languages can read the
  1751. correct value. */
  1752. staged_gdb_datadir = gdb_datadir;
  1753. gdb::observers::gdb_datadir_changed.notify ();
  1754. }
  1755. /* "show" command for the gdb_datadir configuration variable. */
  1756. static void
  1757. show_gdb_datadir (struct ui_file *file, int from_tty,
  1758. struct cmd_list_element *c, const char *value)
  1759. {
  1760. gdb_printf (file, _("GDB's data directory is \"%ps\".\n"),
  1761. styled_string (file_name_style.style (),
  1762. gdb_datadir.c_str ()));
  1763. }
  1764. /* Implement 'set history filename'. */
  1765. static void
  1766. set_history_filename (const char *args,
  1767. int from_tty, struct cmd_list_element *c)
  1768. {
  1769. /* We include the current directory so that if the user changes
  1770. directories the file written will be the same as the one
  1771. that was read. */
  1772. if (!history_filename.empty ()
  1773. && !IS_ABSOLUTE_PATH (history_filename.c_str ()))
  1774. {
  1775. gdb::unique_xmalloc_ptr<char> temp
  1776. (gdb_abspath (history_filename.c_str ()));
  1777. history_filename = temp.get ();
  1778. }
  1779. }
  1780. /* Whether we're in quiet startup mode. */
  1781. static bool startup_quiet;
  1782. /* See top.h. */
  1783. bool
  1784. check_quiet_mode ()
  1785. {
  1786. return startup_quiet;
  1787. }
  1788. /* Show whether GDB should start up in quiet mode. */
  1789. static void
  1790. show_startup_quiet (struct ui_file *file, int from_tty,
  1791. struct cmd_list_element *c, const char *value)
  1792. {
  1793. gdb_printf (file, _("Whether to start up quietly is %s.\n"),
  1794. value);
  1795. }
  1796. static void
  1797. init_gdb_version_vars (void)
  1798. {
  1799. struct internalvar *major_version_var = create_internalvar ("_gdb_major");
  1800. struct internalvar *minor_version_var = create_internalvar ("_gdb_minor");
  1801. int vmajor = 0, vminor = 0, vrevision = 0;
  1802. sscanf (version, "%d.%d.%d", &vmajor, &vminor, &vrevision);
  1803. set_internalvar_integer (major_version_var, vmajor);
  1804. set_internalvar_integer (minor_version_var, vminor + (vrevision > 0));
  1805. }
  1806. static void
  1807. init_main (void)
  1808. {
  1809. struct cmd_list_element *c;
  1810. /* Initialize the prompt to a simple "(gdb) " prompt or to whatever
  1811. the DEFAULT_PROMPT is. */
  1812. set_prompt (DEFAULT_PROMPT);
  1813. /* Set the important stuff up for command editing. */
  1814. command_editing_p = 1;
  1815. history_expansion_p = 0;
  1816. write_history_p = 0;
  1817. /* Setup important stuff for command line editing. */
  1818. rl_completion_word_break_hook = gdb_completion_word_break_characters;
  1819. rl_attempted_completion_function = gdb_rl_attempted_completion_function;
  1820. set_rl_completer_word_break_characters (default_word_break_characters ());
  1821. rl_completer_quote_characters = get_gdb_completer_quote_characters ();
  1822. rl_completion_display_matches_hook = cli_display_match_list;
  1823. rl_readline_name = "gdb";
  1824. rl_terminal_name = getenv ("TERM");
  1825. /* The name for this defun comes from Bash, where it originated.
  1826. 15 is Control-o, the same binding this function has in Bash. */
  1827. rl_add_defun ("operate-and-get-next", gdb_rl_operate_and_get_next, 15);
  1828. add_setshow_string_cmd ("prompt", class_support,
  1829. &top_prompt,
  1830. _("Set gdb's prompt."),
  1831. _("Show gdb's prompt."),
  1832. NULL, NULL,
  1833. show_prompt,
  1834. &setlist, &showlist);
  1835. add_com ("dont-repeat", class_support, dont_repeat_command, _("\
  1836. Don't repeat this command.\nPrimarily \
  1837. used inside of user-defined commands that should not be repeated when\n\
  1838. hitting return."));
  1839. add_setshow_boolean_cmd ("editing", class_support,
  1840. &set_editing_cmd_var, _("\
  1841. Set editing of command lines as they are typed."), _("\
  1842. Show editing of command lines as they are typed."), _("\
  1843. Use \"on\" to enable the editing, and \"off\" to disable it.\n\
  1844. Without an argument, command line editing is enabled. To edit, use\n\
  1845. EMACS-like or VI-like commands like control-P or ESC."),
  1846. set_editing,
  1847. show_editing,
  1848. &setlist, &showlist);
  1849. add_setshow_boolean_cmd ("save", no_class, &write_history_p, _("\
  1850. Set saving of the history record on exit."), _("\
  1851. Show saving of the history record on exit."), _("\
  1852. Use \"on\" to enable the saving, and \"off\" to disable it.\n\
  1853. Without an argument, saving is enabled."),
  1854. NULL,
  1855. show_write_history_p,
  1856. &sethistlist, &showhistlist);
  1857. add_setshow_zuinteger_unlimited_cmd ("size", no_class,
  1858. &history_size_setshow_var, _("\
  1859. Set the size of the command history."), _("\
  1860. Show the size of the command history."), _("\
  1861. This is the number of previous commands to keep a record of.\n\
  1862. If set to \"unlimited\", the number of commands kept in the history\n\
  1863. list is unlimited. This defaults to the value of the environment\n\
  1864. variable \"GDBHISTSIZE\", or to 256 if this variable is not set."),
  1865. set_history_size_command,
  1866. show_history_size,
  1867. &sethistlist, &showhistlist);
  1868. add_setshow_zuinteger_unlimited_cmd ("remove-duplicates", no_class,
  1869. &history_remove_duplicates, _("\
  1870. Set how far back in history to look for and remove duplicate entries."), _("\
  1871. Show how far back in history to look for and remove duplicate entries."), _("\
  1872. If set to a nonzero value N, GDB will look back at the last N history entries\n\
  1873. and remove the first history entry that is a duplicate of the most recent\n\
  1874. entry, each time a new history entry is added.\n\
  1875. If set to \"unlimited\", this lookbehind is unbounded.\n\
  1876. Only history entries added during this session are considered for removal.\n\
  1877. If set to 0, removal of duplicate history entries is disabled.\n\
  1878. By default this option is set to 0."),
  1879. NULL,
  1880. show_history_remove_duplicates,
  1881. &sethistlist, &showhistlist);
  1882. add_setshow_optional_filename_cmd ("filename", no_class, &history_filename, _("\
  1883. Set the filename in which to record the command history."), _("\
  1884. Show the filename in which to record the command history."), _("\
  1885. (the list of previous commands of which a record is kept)."),
  1886. set_history_filename,
  1887. show_history_filename,
  1888. &sethistlist, &showhistlist);
  1889. add_setshow_boolean_cmd ("confirm", class_support, &confirm, _("\
  1890. Set whether to confirm potentially dangerous operations."), _("\
  1891. Show whether to confirm potentially dangerous operations."), NULL,
  1892. NULL,
  1893. show_confirm,
  1894. &setlist, &showlist);
  1895. add_setshow_zinteger_cmd ("annotate", class_obscure, &annotation_level, _("\
  1896. Set annotation_level."), _("\
  1897. Show annotation_level."), _("\
  1898. 0 == normal; 1 == fullname (for use when running under emacs)\n\
  1899. 2 == output annotated suitably for use by programs that control GDB."),
  1900. NULL,
  1901. show_annotation_level,
  1902. &setlist, &showlist);
  1903. add_setshow_boolean_cmd ("exec-done-display", class_support,
  1904. &exec_done_display_p, _("\
  1905. Set notification of completion for asynchronous execution commands."), _("\
  1906. Show notification of completion for asynchronous execution commands."), _("\
  1907. Use \"on\" to enable the notification, and \"off\" to disable it."),
  1908. NULL,
  1909. show_exec_done_display_p,
  1910. &setlist, &showlist);
  1911. add_setshow_filename_cmd ("data-directory", class_maintenance,
  1912. &staged_gdb_datadir, _("Set GDB's data directory."),
  1913. _("Show GDB's data directory."),
  1914. _("\
  1915. When set, GDB uses the specified path to search for data files."),
  1916. set_gdb_datadir, show_gdb_datadir,
  1917. &setlist,
  1918. &showlist);
  1919. /* Prime the initial value for data-directory. */
  1920. staged_gdb_datadir = gdb_datadir;
  1921. add_setshow_auto_boolean_cmd ("interactive-mode", class_support,
  1922. &interactive_mode, _("\
  1923. Set whether GDB's standard input is a terminal."), _("\
  1924. Show whether GDB's standard input is a terminal."), _("\
  1925. If on, GDB assumes that standard input is a terminal. In practice, it\n\
  1926. means that GDB should wait for the user to answer queries associated to\n\
  1927. commands entered at the command prompt. If off, GDB assumes that standard\n\
  1928. input is not a terminal, and uses the default answer to all queries.\n\
  1929. If auto (the default), determine which mode to use based on the standard\n\
  1930. input settings."),
  1931. NULL,
  1932. show_interactive_mode,
  1933. &setlist, &showlist);
  1934. add_setshow_boolean_cmd ("startup-quietly", class_support,
  1935. &startup_quiet, _("\
  1936. Set whether GDB should start up quietly."), _(" \
  1937. Show whether GDB should start up quietly."), _("\
  1938. This setting will not affect the current session. Instead this command\n\
  1939. should be added to the .gdbearlyinit file in the users home directory to\n\
  1940. affect future GDB sessions."),
  1941. NULL,
  1942. show_startup_quiet,
  1943. &setlist, &showlist);
  1944. c = add_cmd ("new-ui", class_support, new_ui_command, _("\
  1945. Create a new UI.\n\
  1946. Usage: new-ui INTERPRETER TTY\n\
  1947. The first argument is the name of the interpreter to run.\n\
  1948. The second argument is the terminal the UI runs on."), &cmdlist);
  1949. set_cmd_completer (c, interpreter_completer);
  1950. }
  1951. /* See top.h. */
  1952. void
  1953. gdb_init ()
  1954. {
  1955. saved_command_line = xstrdup ("");
  1956. previous_saved_command_line = xstrdup ("");
  1957. /* Run the init function of each source file. */
  1958. #ifdef __MSDOS__
  1959. /* Make sure we return to the original directory upon exit, come
  1960. what may, since the OS doesn't do that for us. */
  1961. make_final_cleanup (do_chdir_cleanup, xstrdup (current_directory));
  1962. #endif
  1963. init_cmd_lists (); /* This needs to be done first. */
  1964. init_page_info ();
  1965. /* Here is where we call all the _initialize_foo routines. */
  1966. initialize_all_files ();
  1967. /* This creates the current_program_space. Do this after all the
  1968. _initialize_foo routines have had a chance to install their
  1969. per-sspace data keys. Also do this before
  1970. initialize_current_architecture is called, because it accesses
  1971. exec_bfd of the current program space. */
  1972. initialize_progspace ();
  1973. initialize_inferiors ();
  1974. initialize_current_architecture ();
  1975. init_main (); /* But that omits this file! Do it now. */
  1976. initialize_stdin_serial ();
  1977. /* Take a snapshot of our tty state before readline/ncurses have had a chance
  1978. to alter it. */
  1979. set_initial_gdb_ttystate ();
  1980. gdb_init_signals ();
  1981. /* We need a default language for parsing expressions, so simple
  1982. things like "set width 0" won't fail if no language is explicitly
  1983. set in a config file or implicitly set by reading an executable
  1984. during startup. */
  1985. set_language (language_c);
  1986. expected_language = current_language; /* Don't warn about the change. */
  1987. /* Create $_gdb_major and $_gdb_minor convenience variables. */
  1988. init_gdb_version_vars ();
  1989. }
  1990. void _initialize_top ();
  1991. void
  1992. _initialize_top ()
  1993. {
  1994. /* Determine a default value for the history filename. */
  1995. const char *tmpenv = getenv ("GDBHISTFILE");
  1996. if (tmpenv != nullptr)
  1997. history_filename = tmpenv;
  1998. else
  1999. {
  2000. /* We include the current directory so that if the user changes
  2001. directories the file written will be the same as the one
  2002. that was read. */
  2003. #ifdef __MSDOS__
  2004. /* No leading dots in file names are allowed on MSDOS. */
  2005. const char *fname = "_gdb_history";
  2006. #else
  2007. const char *fname = ".gdb_history";
  2008. #endif
  2009. gdb::unique_xmalloc_ptr<char> temp (gdb_abspath (fname));
  2010. history_filename = temp.get ();
  2011. }
  2012. }