main.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 "top.h"
  16. #include "target.h"
  17. #include "inferior.h"
  18. #include "symfile.h"
  19. #include "gdbcore.h"
  20. #include "getopt.h"
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <ctype.h>
  24. #include "gdbsupport/event-loop.h"
  25. #include "ui-out.h"
  26. #include "interps.h"
  27. #include "main.h"
  28. #include "source.h"
  29. #include "cli/cli-cmds.h"
  30. #include "objfiles.h"
  31. #include "auto-load.h"
  32. #include "maint.h"
  33. #include "filenames.h"
  34. #include "gdbsupport/filestuff.h"
  35. #include <signal.h>
  36. #include "event-top.h"
  37. #include "infrun.h"
  38. #include "gdbsupport/signals-state-save-restore.h"
  39. #include <algorithm>
  40. #include <vector>
  41. #include "gdbsupport/pathstuff.h"
  42. #include "cli/cli-style.h"
  43. #ifdef GDBTK
  44. #include "gdbtk/generic/gdbtk.h"
  45. #endif
  46. #include "gdbsupport/alt-stack.h"
  47. #include "observable.h"
  48. #include "serial.h"
  49. /* The selected interpreter. This will be used as a set command
  50. variable, so it should always be malloc'ed - since
  51. do_setshow_command will free it. */
  52. char *interpreter_p;
  53. /* System root path, used to find libraries etc. */
  54. std::string gdb_sysroot;
  55. /* GDB datadir, used to store data files. */
  56. std::string gdb_datadir;
  57. /* Non-zero if GDB_DATADIR was provided on the command line.
  58. This doesn't track whether data-directory is set later from the
  59. command line, but we don't reread system.gdbinit when that happens. */
  60. static int gdb_datadir_provided = 0;
  61. /* If gdb was configured with --with-python=/path,
  62. the possibly relocated path to python's lib directory. */
  63. std::string python_libdir;
  64. /* Target IO streams. */
  65. struct ui_file *gdb_stdtargin;
  66. struct ui_file *gdb_stdtarg;
  67. struct ui_file *gdb_stdtargerr;
  68. /* True if --batch or --batch-silent was seen. */
  69. int batch_flag = 0;
  70. /* Support for the --batch-silent option. */
  71. int batch_silent = 0;
  72. /* Support for --return-child-result option.
  73. Set the default to -1 to return error in the case
  74. that the program does not run or does not complete. */
  75. int return_child_result = 0;
  76. int return_child_result_value = -1;
  77. /* GDB as it has been invoked from the command line (i.e. argv[0]). */
  78. static char *gdb_program_name;
  79. /* Return read only pointer to GDB_PROGRAM_NAME. */
  80. const char *
  81. get_gdb_program_name (void)
  82. {
  83. return gdb_program_name;
  84. }
  85. static void print_gdb_help (struct ui_file *);
  86. /* Set the data-directory parameter to NEW_DATADIR.
  87. If NEW_DATADIR is not a directory then a warning is printed.
  88. We don't signal an error for backward compatibility. */
  89. void
  90. set_gdb_data_directory (const char *new_datadir)
  91. {
  92. struct stat st;
  93. if (stat (new_datadir, &st) < 0)
  94. {
  95. int save_errno = errno;
  96. gdb_printf (gdb_stderr, "Warning: ");
  97. print_sys_errmsg (new_datadir, save_errno);
  98. }
  99. else if (!S_ISDIR (st.st_mode))
  100. warning (_("%ps is not a directory."),
  101. styled_string (file_name_style.style (), new_datadir));
  102. gdb_datadir = gdb_realpath (new_datadir).get ();
  103. /* gdb_realpath won't return an absolute path if the path doesn't exist,
  104. but we still want to record an absolute path here. If the user entered
  105. "../foo" and "../foo" doesn't exist then we'll record $(pwd)/../foo which
  106. isn't canonical, but that's ok. */
  107. if (!IS_ABSOLUTE_PATH (gdb_datadir.c_str ()))
  108. {
  109. gdb::unique_xmalloc_ptr<char> abs_datadir
  110. = gdb_abspath (gdb_datadir.c_str ());
  111. gdb_datadir = abs_datadir.get ();
  112. }
  113. }
  114. /* Relocate a file or directory. PROGNAME is the name by which gdb
  115. was invoked (i.e., argv[0]). INITIAL is the default value for the
  116. file or directory. RELOCATABLE is true if the value is relocatable,
  117. false otherwise. This may return an empty string under the same
  118. conditions as make_relative_prefix returning NULL. */
  119. static std::string
  120. relocate_path (const char *progname, const char *initial, bool relocatable)
  121. {
  122. if (relocatable)
  123. {
  124. gdb::unique_xmalloc_ptr<char> str (make_relative_prefix (progname,
  125. BINDIR,
  126. initial));
  127. if (str != nullptr)
  128. return str.get ();
  129. return std::string ();
  130. }
  131. return initial;
  132. }
  133. /* Like relocate_path, but specifically checks for a directory.
  134. INITIAL is relocated according to the rules of relocate_path. If
  135. the result is a directory, it is used; otherwise, INITIAL is used.
  136. The chosen directory is then canonicalized using lrealpath. */
  137. std::string
  138. relocate_gdb_directory (const char *initial, bool relocatable)
  139. {
  140. std::string dir = relocate_path (gdb_program_name, initial, relocatable);
  141. if (!dir.empty ())
  142. {
  143. struct stat s;
  144. if (stat (dir.c_str (), &s) != 0 || !S_ISDIR (s.st_mode))
  145. {
  146. dir.clear ();
  147. }
  148. }
  149. if (dir.empty ())
  150. dir = initial;
  151. /* Canonicalize the directory. */
  152. if (!dir.empty ())
  153. {
  154. gdb::unique_xmalloc_ptr<char> canon_sysroot (lrealpath (dir.c_str ()));
  155. if (canon_sysroot)
  156. dir = canon_sysroot.get ();
  157. }
  158. return dir;
  159. }
  160. /* Given a gdbinit path in FILE, adjusts it according to the gdb_datadir
  161. parameter if it is in the data dir, or passes it through relocate_path
  162. otherwise. */
  163. static std::string
  164. relocate_file_path_maybe_in_datadir (const std::string &file,
  165. bool relocatable)
  166. {
  167. size_t datadir_len = strlen (GDB_DATADIR);
  168. std::string relocated_path;
  169. /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
  170. has been provided, search for SYSTEM_GDBINIT there. */
  171. if (gdb_datadir_provided
  172. && datadir_len < file.length ()
  173. && filename_ncmp (file.c_str (), GDB_DATADIR, datadir_len) == 0
  174. && IS_DIR_SEPARATOR (file[datadir_len]))
  175. {
  176. /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
  177. to gdb_datadir. */
  178. size_t start = datadir_len;
  179. for (; IS_DIR_SEPARATOR (file[start]); ++start)
  180. ;
  181. relocated_path = gdb_datadir + SLASH_STRING + file.substr (start);
  182. }
  183. else
  184. {
  185. relocated_path = relocate_path (gdb_program_name, file.c_str (),
  186. relocatable);
  187. }
  188. return relocated_path;
  189. }
  190. /* A class to wrap up the logic for finding the three different types of
  191. initialisation files GDB uses, system wide, home directory, and current
  192. working directory. */
  193. class gdb_initfile_finder
  194. {
  195. public:
  196. /* Constructor. Finds initialisation files named FILENAME in the home
  197. directory or local (current working) directory. System initialisation
  198. files are found in both SYSTEM_FILENAME and SYSTEM_DIRNAME if these
  199. are not nullptr (either or both can be). The matching *_RELOCATABLE
  200. flag is passed through to RELOCATE_FILE_PATH_MAYBE_IN_DATADIR.
  201. If FILENAME starts with a '.' then when looking in the home directory
  202. this first '.' can be ignored in some cases. */
  203. explicit gdb_initfile_finder (const char *filename,
  204. const char *system_filename,
  205. bool system_filename_relocatable,
  206. const char *system_dirname,
  207. bool system_dirname_relocatable,
  208. bool lookup_local_file)
  209. {
  210. struct stat s;
  211. if (system_filename != nullptr && system_filename[0] != '\0')
  212. {
  213. std::string relocated_filename
  214. = relocate_file_path_maybe_in_datadir (system_filename,
  215. system_filename_relocatable);
  216. if (!relocated_filename.empty ()
  217. && stat (relocated_filename.c_str (), &s) == 0)
  218. m_system_files.push_back (relocated_filename);
  219. }
  220. if (system_dirname != nullptr && system_dirname[0] != '\0')
  221. {
  222. std::string relocated_dirname
  223. = relocate_file_path_maybe_in_datadir (system_dirname,
  224. system_dirname_relocatable);
  225. if (!relocated_dirname.empty ())
  226. {
  227. gdb_dir_up dir (opendir (relocated_dirname.c_str ()));
  228. if (dir != nullptr)
  229. {
  230. std::vector<std::string> files;
  231. while (true)
  232. {
  233. struct dirent *ent = readdir (dir.get ());
  234. if (ent == nullptr)
  235. break;
  236. std::string name (ent->d_name);
  237. if (name == "." || name == "..")
  238. continue;
  239. /* ent->d_type is not available on all systems
  240. (e.g. mingw, Solaris), so we have to call stat(). */
  241. std::string tmp_filename
  242. = relocated_dirname + SLASH_STRING + name;
  243. if (stat (tmp_filename.c_str (), &s) != 0
  244. || !S_ISREG (s.st_mode))
  245. continue;
  246. const struct extension_language_defn *extlang
  247. = get_ext_lang_of_file (tmp_filename.c_str ());
  248. /* We effectively don't support "set script-extension
  249. off/soft", because we are loading system init files
  250. here, so it does not really make sense to depend on
  251. a setting. */
  252. if (extlang != nullptr && ext_lang_present_p (extlang))
  253. files.push_back (std::move (tmp_filename));
  254. }
  255. std::sort (files.begin (), files.end ());
  256. m_system_files.insert (m_system_files.end (),
  257. files.begin (), files.end ());
  258. }
  259. }
  260. }
  261. /* If the .gdbinit file in the current directory is the same as
  262. the $HOME/.gdbinit file, it should not be sourced. homebuf
  263. and cwdbuf are used in that purpose. Make sure that the stats
  264. are zero in case one of them fails (this guarantees that they
  265. won't match if either exists). */
  266. struct stat homebuf, cwdbuf;
  267. memset (&homebuf, 0, sizeof (struct stat));
  268. memset (&cwdbuf, 0, sizeof (struct stat));
  269. m_home_file = find_gdb_home_config_file (filename, &homebuf);
  270. if (lookup_local_file && stat (filename, &cwdbuf) == 0)
  271. {
  272. if (m_home_file.empty ()
  273. || memcmp ((char *) &homebuf, (char *) &cwdbuf,
  274. sizeof (struct stat)))
  275. m_local_file = filename;
  276. }
  277. }
  278. DISABLE_COPY_AND_ASSIGN (gdb_initfile_finder);
  279. /* Return a list of system initialisation files. The list could be
  280. empty. */
  281. const std::vector<std::string> &system_files () const
  282. { return m_system_files; }
  283. /* Return the path to the home initialisation file. The string can be
  284. empty if there is no such file. */
  285. const std::string &home_file () const
  286. { return m_home_file; }
  287. /* Return the path to the local initialisation file. The string can be
  288. empty if there is no such file. */
  289. const std::string &local_file () const
  290. { return m_local_file; }
  291. private:
  292. /* Vector of all system init files in the order they should be processed.
  293. Could be empty. */
  294. std::vector<std::string> m_system_files;
  295. /* Initialization file from the home directory. Could be the empty
  296. string if there is no such file found. */
  297. std::string m_home_file;
  298. /* Initialization file from the current working directory. Could be the
  299. empty string if there is no such file found. */
  300. std::string m_local_file;
  301. };
  302. /* Compute the locations of init files that GDB should source and return
  303. them in SYSTEM_GDBINIT, HOME_GDBINIT, LOCAL_GDBINIT. The SYSTEM_GDBINIT
  304. can be returned as an empty vector, and HOME_GDBINIT and LOCAL_GDBINIT
  305. can be returned as empty strings if there is no init file of that
  306. type. */
  307. static void
  308. get_init_files (std::vector<std::string> *system_gdbinit,
  309. std::string *home_gdbinit,
  310. std::string *local_gdbinit)
  311. {
  312. /* Cache the file lookup object so we only actually search for the files
  313. once. */
  314. static gdb::optional<gdb_initfile_finder> init_files;
  315. if (!init_files.has_value ())
  316. init_files.emplace (GDBINIT, SYSTEM_GDBINIT, SYSTEM_GDBINIT_RELOCATABLE,
  317. SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE,
  318. true);
  319. *system_gdbinit = init_files->system_files ();
  320. *home_gdbinit = init_files->home_file ();
  321. *local_gdbinit = init_files->local_file ();
  322. }
  323. /* Compute the location of the early init file GDB should source and return
  324. it in HOME_GDBEARLYINIT. HOME_GDBEARLYINIT could be returned as an
  325. empty string if there is no early init file found. */
  326. static void
  327. get_earlyinit_files (std::string *home_gdbearlyinit)
  328. {
  329. /* Cache the file lookup object so we only actually search for the files
  330. once. */
  331. static gdb::optional<gdb_initfile_finder> init_files;
  332. if (!init_files.has_value ())
  333. init_files.emplace (GDBEARLYINIT, nullptr, false, nullptr, false, false);
  334. *home_gdbearlyinit = init_files->home_file ();
  335. }
  336. /* Start up the event loop. This is the entry point to the event loop
  337. from the command loop. */
  338. static void
  339. start_event_loop ()
  340. {
  341. /* Loop until there is nothing to do. This is the entry point to
  342. the event loop engine. gdb_do_one_event will process one event
  343. for each invocation. It blocks waiting for an event and then
  344. processes it. */
  345. while (1)
  346. {
  347. int result = 0;
  348. try
  349. {
  350. result = gdb_do_one_event ();
  351. }
  352. catch (const gdb_exception &ex)
  353. {
  354. exception_print (gdb_stderr, ex);
  355. /* If any exception escaped to here, we better enable
  356. stdin. Otherwise, any command that calls async_disable_stdin,
  357. and then throws, will leave stdin inoperable. */
  358. SWITCH_THRU_ALL_UIS ()
  359. {
  360. async_enable_stdin ();
  361. }
  362. /* If we long-jumped out of do_one_event, we probably didn't
  363. get around to resetting the prompt, which leaves readline
  364. in a messed-up state. Reset it here. */
  365. current_ui->prompt_state = PROMPT_NEEDED;
  366. gdb::observers::command_error.notify ();
  367. /* This call looks bizarre, but it is required. If the user
  368. entered a command that caused an error,
  369. after_char_processing_hook won't be called from
  370. rl_callback_read_char_wrapper. Using a cleanup there
  371. won't work, since we want this function to be called
  372. after a new prompt is printed. */
  373. if (after_char_processing_hook)
  374. (*after_char_processing_hook) ();
  375. /* Maybe better to set a flag to be checked somewhere as to
  376. whether display the prompt or not. */
  377. }
  378. if (result < 0)
  379. break;
  380. }
  381. /* We are done with the event loop. There are no more event sources
  382. to listen to. So we exit GDB. */
  383. return;
  384. }
  385. /* Call command_loop. */
  386. /* Prevent inlining this function for the benefit of GDB's selftests
  387. in the testsuite. Those tests want to run GDB under GDB and stop
  388. here. */
  389. static void captured_command_loop () __attribute__((noinline));
  390. static void
  391. captured_command_loop ()
  392. {
  393. struct ui *ui = current_ui;
  394. /* Top-level execution commands can be run in the background from
  395. here on. */
  396. current_ui->async = 1;
  397. /* Give the interpreter a chance to print a prompt, if necessary */
  398. if (ui->prompt_state != PROMPT_BLOCKED)
  399. interp_pre_command_loop (top_level_interpreter ());
  400. /* Now it's time to start the event loop. */
  401. start_event_loop ();
  402. /* If the command_loop returned, normally (rather than threw an
  403. error) we try to quit. If the quit is aborted, our caller
  404. catches the signal and restarts the command loop. */
  405. quit_command (NULL, ui->instream == ui->stdin_stream);
  406. }
  407. /* Handle command errors thrown from within catch_command_errors. */
  408. static int
  409. handle_command_errors (const struct gdb_exception &e)
  410. {
  411. if (e.reason < 0)
  412. {
  413. exception_print (gdb_stderr, e);
  414. /* If any exception escaped to here, we better enable stdin.
  415. Otherwise, any command that calls async_disable_stdin, and
  416. then throws, will leave stdin inoperable. */
  417. async_enable_stdin ();
  418. return 0;
  419. }
  420. return 1;
  421. }
  422. /* Type of the command callback passed to the const
  423. catch_command_errors. */
  424. typedef void (catch_command_errors_const_ftype) (const char *, int);
  425. /* Wrap calls to commands run before the event loop is started. */
  426. static int
  427. catch_command_errors (catch_command_errors_const_ftype command,
  428. const char *arg, int from_tty,
  429. bool do_bp_actions = false)
  430. {
  431. try
  432. {
  433. int was_sync = current_ui->prompt_state == PROMPT_BLOCKED;
  434. command (arg, from_tty);
  435. maybe_wait_sync_command_done (was_sync);
  436. /* Do any commands attached to breakpoint we stopped at. */
  437. if (do_bp_actions)
  438. bpstat_do_actions ();
  439. }
  440. catch (const gdb_exception &e)
  441. {
  442. return handle_command_errors (e);
  443. }
  444. return 1;
  445. }
  446. /* Adapter for symbol_file_add_main that translates 'from_tty' to a
  447. symfile_add_flags. */
  448. static void
  449. symbol_file_add_main_adapter (const char *arg, int from_tty)
  450. {
  451. symfile_add_flags add_flags = 0;
  452. if (from_tty)
  453. add_flags |= SYMFILE_VERBOSE;
  454. symbol_file_add_main (arg, add_flags);
  455. }
  456. /* Perform validation of the '--readnow' and '--readnever' flags. */
  457. static void
  458. validate_readnow_readnever ()
  459. {
  460. if (readnever_symbol_files && readnow_symbol_files)
  461. {
  462. error (_("%s: '--readnow' and '--readnever' cannot be "
  463. "specified simultaneously"),
  464. gdb_program_name);
  465. }
  466. }
  467. /* Type of this option. */
  468. enum cmdarg_kind
  469. {
  470. /* Option type -x. */
  471. CMDARG_FILE,
  472. /* Option type -ex. */
  473. CMDARG_COMMAND,
  474. /* Option type -ix. */
  475. CMDARG_INIT_FILE,
  476. /* Option type -iex. */
  477. CMDARG_INIT_COMMAND,
  478. /* Option type -eix. */
  479. CMDARG_EARLYINIT_FILE,
  480. /* Option type -eiex. */
  481. CMDARG_EARLYINIT_COMMAND
  482. };
  483. /* Arguments of --command option and its counterpart. */
  484. struct cmdarg
  485. {
  486. cmdarg (cmdarg_kind type_, char *string_)
  487. : type (type_), string (string_)
  488. {}
  489. /* Type of this option. */
  490. enum cmdarg_kind type;
  491. /* Value of this option - filename or the GDB command itself. String memory
  492. is not owned by this structure despite it is 'const'. */
  493. char *string;
  494. };
  495. /* From CMDARG_VEC execute command files (matching FILE_TYPE) or commands
  496. (matching CMD_TYPE). Update the value in *RET if and scripts or
  497. commands are executed. */
  498. static void
  499. execute_cmdargs (const std::vector<struct cmdarg> *cmdarg_vec,
  500. cmdarg_kind file_type, cmdarg_kind cmd_type,
  501. int *ret)
  502. {
  503. for (const auto &cmdarg_p : *cmdarg_vec)
  504. {
  505. if (cmdarg_p.type == file_type)
  506. *ret = catch_command_errors (source_script, cmdarg_p.string,
  507. !batch_flag);
  508. else if (cmdarg_p.type == cmd_type)
  509. *ret = catch_command_errors (execute_command, cmdarg_p.string,
  510. !batch_flag, true);
  511. }
  512. }
  513. static void
  514. captured_main_1 (struct captured_main_args *context)
  515. {
  516. int argc = context->argc;
  517. char **argv = context->argv;
  518. static int quiet = 0;
  519. static int set_args = 0;
  520. static int inhibit_home_gdbinit = 0;
  521. /* Pointers to various arguments from command line. */
  522. char *symarg = NULL;
  523. char *execarg = NULL;
  524. char *pidarg = NULL;
  525. char *corearg = NULL;
  526. char *pid_or_core_arg = NULL;
  527. char *cdarg = NULL;
  528. char *ttyarg = NULL;
  529. /* These are static so that we can take their address in an
  530. initializer. */
  531. static int print_help;
  532. static int print_version;
  533. static int print_configuration;
  534. /* Pointers to all arguments of --command option. */
  535. std::vector<struct cmdarg> cmdarg_vec;
  536. /* All arguments of --directory option. */
  537. std::vector<char *> dirarg;
  538. int i;
  539. int save_auto_load;
  540. int ret = 1;
  541. #ifdef HAVE_USEFUL_SBRK
  542. /* Set this before constructing scoped_command_stats. */
  543. lim_at_start = (char *) sbrk (0);
  544. #endif
  545. scoped_command_stats stat_reporter (false);
  546. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  547. setlocale (LC_MESSAGES, "");
  548. #endif
  549. #if defined (HAVE_SETLOCALE)
  550. setlocale (LC_CTYPE, "");
  551. #endif
  552. #ifdef ENABLE_NLS
  553. bindtextdomain (PACKAGE, LOCALEDIR);
  554. textdomain (PACKAGE);
  555. #endif
  556. notice_open_fds ();
  557. #ifdef __MINGW32__
  558. /* Ensure stderr is unbuffered. A Cygwin pty or pipe is implemented
  559. as a Windows pipe, and Windows buffers on pipes. */
  560. setvbuf (stderr, NULL, _IONBF, BUFSIZ);
  561. #endif
  562. /* Note: `error' cannot be called before this point, because the
  563. caller will crash when trying to print the exception. */
  564. main_ui = new ui (stdin, stdout, stderr);
  565. current_ui = main_ui;
  566. gdb_stdtargerr = gdb_stderr; /* for moment */
  567. gdb_stdtargin = gdb_stdin; /* for moment */
  568. if (bfd_init () != BFD_INIT_MAGIC)
  569. error (_("fatal error: libbfd ABI mismatch"));
  570. #ifdef __MINGW32__
  571. /* On Windows, argv[0] is not necessarily set to absolute form when
  572. GDB is found along PATH, without which relocation doesn't work. */
  573. gdb_program_name = windows_get_absolute_argv0 (argv[0]);
  574. #else
  575. gdb_program_name = xstrdup (argv[0]);
  576. #endif
  577. /* Prefix warning messages with the command name. */
  578. gdb::unique_xmalloc_ptr<char> tmp_warn_preprint
  579. = xstrprintf ("%s: warning: ", gdb_program_name);
  580. warning_pre_print = tmp_warn_preprint.get ();
  581. current_directory = getcwd (NULL, 0);
  582. if (current_directory == NULL)
  583. perror_warning_with_name (_("error finding working directory"));
  584. /* Set the sysroot path. */
  585. gdb_sysroot = relocate_gdb_directory (TARGET_SYSTEM_ROOT,
  586. TARGET_SYSTEM_ROOT_RELOCATABLE);
  587. if (gdb_sysroot.empty ())
  588. gdb_sysroot = TARGET_SYSROOT_PREFIX;
  589. debug_file_directory
  590. = relocate_gdb_directory (DEBUGDIR, DEBUGDIR_RELOCATABLE);
  591. gdb_datadir = relocate_gdb_directory (GDB_DATADIR,
  592. GDB_DATADIR_RELOCATABLE);
  593. #ifdef WITH_PYTHON_LIBDIR
  594. python_libdir = relocate_gdb_directory (WITH_PYTHON_LIBDIR,
  595. PYTHON_LIBDIR_RELOCATABLE);
  596. #endif
  597. #ifdef RELOC_SRCDIR
  598. add_substitute_path_rule (RELOC_SRCDIR,
  599. make_relative_prefix (gdb_program_name, BINDIR,
  600. RELOC_SRCDIR));
  601. #endif
  602. /* There will always be an interpreter. Either the one passed into
  603. this captured main, or one specified by the user at start up, or
  604. the console. Initialize the interpreter to the one requested by
  605. the application. */
  606. interpreter_p = xstrdup (context->interpreter_p);
  607. /* Parse arguments and options. */
  608. {
  609. int c;
  610. /* When var field is 0, use flag field to record the equivalent
  611. short option (or arbitrary numbers starting at 10 for those
  612. with no equivalent). */
  613. enum {
  614. OPT_SE = 10,
  615. OPT_CD,
  616. OPT_ANNOTATE,
  617. OPT_STATISTICS,
  618. OPT_TUI,
  619. OPT_NOWINDOWS,
  620. OPT_WINDOWS,
  621. OPT_IX,
  622. OPT_IEX,
  623. OPT_EIX,
  624. OPT_EIEX,
  625. OPT_READNOW,
  626. OPT_READNEVER
  627. };
  628. /* This struct requires int* in the struct, but write_files is a bool.
  629. So use this temporary int that we write back after argument parsing. */
  630. int write_files_1 = 0;
  631. static struct option long_options[] =
  632. {
  633. {"tui", no_argument, 0, OPT_TUI},
  634. {"readnow", no_argument, NULL, OPT_READNOW},
  635. {"readnever", no_argument, NULL, OPT_READNEVER},
  636. {"r", no_argument, NULL, OPT_READNOW},
  637. {"quiet", no_argument, &quiet, 1},
  638. {"q", no_argument, &quiet, 1},
  639. {"silent", no_argument, &quiet, 1},
  640. {"nh", no_argument, &inhibit_home_gdbinit, 1},
  641. {"nx", no_argument, &inhibit_gdbinit, 1},
  642. {"n", no_argument, &inhibit_gdbinit, 1},
  643. {"batch-silent", no_argument, 0, 'B'},
  644. {"batch", no_argument, &batch_flag, 1},
  645. /* This is a synonym for "--annotate=1". --annotate is now
  646. preferred, but keep this here for a long time because people
  647. will be running emacses which use --fullname. */
  648. {"fullname", no_argument, 0, 'f'},
  649. {"f", no_argument, 0, 'f'},
  650. {"annotate", required_argument, 0, OPT_ANNOTATE},
  651. {"help", no_argument, &print_help, 1},
  652. {"se", required_argument, 0, OPT_SE},
  653. {"symbols", required_argument, 0, 's'},
  654. {"s", required_argument, 0, 's'},
  655. {"exec", required_argument, 0, 'e'},
  656. {"e", required_argument, 0, 'e'},
  657. {"core", required_argument, 0, 'c'},
  658. {"c", required_argument, 0, 'c'},
  659. {"pid", required_argument, 0, 'p'},
  660. {"p", required_argument, 0, 'p'},
  661. {"command", required_argument, 0, 'x'},
  662. {"eval-command", required_argument, 0, 'X'},
  663. {"version", no_argument, &print_version, 1},
  664. {"configuration", no_argument, &print_configuration, 1},
  665. {"x", required_argument, 0, 'x'},
  666. {"ex", required_argument, 0, 'X'},
  667. {"init-command", required_argument, 0, OPT_IX},
  668. {"init-eval-command", required_argument, 0, OPT_IEX},
  669. {"ix", required_argument, 0, OPT_IX},
  670. {"iex", required_argument, 0, OPT_IEX},
  671. {"early-init-command", required_argument, 0, OPT_EIX},
  672. {"early-init-eval-command", required_argument, 0, OPT_EIEX},
  673. {"eix", required_argument, 0, OPT_EIX},
  674. {"eiex", required_argument, 0, OPT_EIEX},
  675. #ifdef GDBTK
  676. {"tclcommand", required_argument, 0, 'z'},
  677. {"enable-external-editor", no_argument, 0, 'y'},
  678. {"editor-command", required_argument, 0, 'w'},
  679. #endif
  680. {"ui", required_argument, 0, 'i'},
  681. {"interpreter", required_argument, 0, 'i'},
  682. {"i", required_argument, 0, 'i'},
  683. {"directory", required_argument, 0, 'd'},
  684. {"d", required_argument, 0, 'd'},
  685. {"data-directory", required_argument, 0, 'D'},
  686. {"D", required_argument, 0, 'D'},
  687. {"cd", required_argument, 0, OPT_CD},
  688. {"tty", required_argument, 0, 't'},
  689. {"baud", required_argument, 0, 'b'},
  690. {"b", required_argument, 0, 'b'},
  691. {"nw", no_argument, NULL, OPT_NOWINDOWS},
  692. {"nowindows", no_argument, NULL, OPT_NOWINDOWS},
  693. {"w", no_argument, NULL, OPT_WINDOWS},
  694. {"windows", no_argument, NULL, OPT_WINDOWS},
  695. {"statistics", no_argument, 0, OPT_STATISTICS},
  696. {"write", no_argument, &write_files_1, 1},
  697. {"args", no_argument, &set_args, 1},
  698. {"l", required_argument, 0, 'l'},
  699. {"return-child-result", no_argument, &return_child_result, 1},
  700. {0, no_argument, 0, 0}
  701. };
  702. while (1)
  703. {
  704. int option_index;
  705. c = getopt_long_only (argc, argv, "",
  706. long_options, &option_index);
  707. if (c == EOF || set_args)
  708. break;
  709. /* Long option that takes an argument. */
  710. if (c == 0 && long_options[option_index].flag == 0)
  711. c = long_options[option_index].val;
  712. switch (c)
  713. {
  714. case 0:
  715. /* Long option that just sets a flag. */
  716. break;
  717. case OPT_SE:
  718. symarg = optarg;
  719. execarg = optarg;
  720. break;
  721. case OPT_CD:
  722. cdarg = optarg;
  723. break;
  724. case OPT_ANNOTATE:
  725. /* FIXME: what if the syntax is wrong (e.g. not digits)? */
  726. annotation_level = atoi (optarg);
  727. break;
  728. case OPT_STATISTICS:
  729. /* Enable the display of both time and space usage. */
  730. set_per_command_time (1);
  731. set_per_command_space (1);
  732. break;
  733. case OPT_TUI:
  734. /* --tui is equivalent to -i=tui. */
  735. #ifdef TUI
  736. xfree (interpreter_p);
  737. interpreter_p = xstrdup (INTERP_TUI);
  738. #else
  739. error (_("%s: TUI mode is not supported"), gdb_program_name);
  740. #endif
  741. break;
  742. case OPT_WINDOWS:
  743. /* FIXME: cagney/2003-03-01: Not sure if this option is
  744. actually useful, and if it is, what it should do. */
  745. #ifdef GDBTK
  746. /* --windows is equivalent to -i=insight. */
  747. xfree (interpreter_p);
  748. interpreter_p = xstrdup (INTERP_INSIGHT);
  749. #endif
  750. break;
  751. case OPT_NOWINDOWS:
  752. /* -nw is equivalent to -i=console. */
  753. xfree (interpreter_p);
  754. interpreter_p = xstrdup (INTERP_CONSOLE);
  755. break;
  756. case 'f':
  757. annotation_level = 1;
  758. break;
  759. case 's':
  760. symarg = optarg;
  761. break;
  762. case 'e':
  763. execarg = optarg;
  764. break;
  765. case 'c':
  766. corearg = optarg;
  767. break;
  768. case 'p':
  769. pidarg = optarg;
  770. break;
  771. case 'x':
  772. cmdarg_vec.emplace_back (CMDARG_FILE, optarg);
  773. break;
  774. case 'X':
  775. cmdarg_vec.emplace_back (CMDARG_COMMAND, optarg);
  776. break;
  777. case OPT_IX:
  778. cmdarg_vec.emplace_back (CMDARG_INIT_FILE, optarg);
  779. break;
  780. case OPT_IEX:
  781. cmdarg_vec.emplace_back (CMDARG_INIT_COMMAND, optarg);
  782. break;
  783. case OPT_EIX:
  784. cmdarg_vec.emplace_back (CMDARG_EARLYINIT_FILE, optarg);
  785. break;
  786. case OPT_EIEX:
  787. cmdarg_vec.emplace_back (CMDARG_EARLYINIT_COMMAND, optarg);
  788. break;
  789. case 'B':
  790. batch_flag = batch_silent = 1;
  791. gdb_stdout = new null_file ();
  792. break;
  793. case 'D':
  794. if (optarg[0] == '\0')
  795. error (_("%s: empty path for `--data-directory'"),
  796. gdb_program_name);
  797. set_gdb_data_directory (optarg);
  798. gdb_datadir_provided = 1;
  799. break;
  800. #ifdef GDBTK
  801. case 'z':
  802. {
  803. if (!gdbtk_test (optarg))
  804. error (_("%s: unable to load tclcommand file \"%s\""),
  805. gdb_program_name, optarg);
  806. break;
  807. }
  808. case 'y':
  809. /* Backwards compatibility only. */
  810. break;
  811. case 'w':
  812. {
  813. /* Set the external editor commands when gdb is farming out files
  814. to be edited by another program. */
  815. external_editor_command = xstrdup (optarg);
  816. break;
  817. }
  818. #endif /* GDBTK */
  819. case 'i':
  820. xfree (interpreter_p);
  821. interpreter_p = xstrdup (optarg);
  822. break;
  823. case 'd':
  824. dirarg.push_back (optarg);
  825. break;
  826. case 't':
  827. ttyarg = optarg;
  828. break;
  829. case 'q':
  830. quiet = 1;
  831. break;
  832. case 'b':
  833. {
  834. int rate;
  835. char *p;
  836. rate = strtol (optarg, &p, 0);
  837. if (rate == 0 && p == optarg)
  838. warning (_("could not set baud rate to `%s'."),
  839. optarg);
  840. else
  841. baud_rate = rate;
  842. }
  843. break;
  844. case 'l':
  845. {
  846. int timeout;
  847. char *p;
  848. timeout = strtol (optarg, &p, 0);
  849. if (timeout == 0 && p == optarg)
  850. warning (_("could not set timeout limit to `%s'."),
  851. optarg);
  852. else
  853. remote_timeout = timeout;
  854. }
  855. break;
  856. case OPT_READNOW:
  857. {
  858. readnow_symbol_files = 1;
  859. validate_readnow_readnever ();
  860. }
  861. break;
  862. case OPT_READNEVER:
  863. {
  864. readnever_symbol_files = 1;
  865. validate_readnow_readnever ();
  866. }
  867. break;
  868. case '?':
  869. error (_("Use `%s --help' for a complete list of options."),
  870. gdb_program_name);
  871. }
  872. }
  873. write_files = (write_files_1 != 0);
  874. if (batch_flag)
  875. {
  876. quiet = 1;
  877. /* Disable all output styling when running in batch mode. */
  878. cli_styling = 0;
  879. }
  880. }
  881. save_original_signals_state (quiet);
  882. /* Try to set up an alternate signal stack for SIGSEGV handlers. */
  883. gdb::alternate_signal_stack signal_stack;
  884. /* Initialize all files. */
  885. gdb_init ();
  886. /* Process early init files and early init options from the command line. */
  887. if (!inhibit_gdbinit)
  888. {
  889. std::string home_gdbearlyinit;
  890. get_earlyinit_files (&home_gdbearlyinit);
  891. if (!home_gdbearlyinit.empty () && !inhibit_home_gdbinit)
  892. ret = catch_command_errors (source_script,
  893. home_gdbearlyinit.c_str (), 0);
  894. }
  895. execute_cmdargs (&cmdarg_vec, CMDARG_EARLYINIT_FILE,
  896. CMDARG_EARLYINIT_COMMAND, &ret);
  897. /* Initialize the extension languages. */
  898. ext_lang_initialization ();
  899. /* Recheck if we're starting up quietly after processing the startup
  900. scripts and commands. */
  901. if (!quiet)
  902. quiet = check_quiet_mode ();
  903. /* Now that gdb_init has created the initial inferior, we're in
  904. position to set args for that inferior. */
  905. if (set_args)
  906. {
  907. /* The remaining options are the command-line options for the
  908. inferior. The first one is the sym/exec file, and the rest
  909. are arguments. */
  910. if (optind >= argc)
  911. error (_("%s: `--args' specified but no program specified"),
  912. gdb_program_name);
  913. symarg = argv[optind];
  914. execarg = argv[optind];
  915. ++optind;
  916. set_inferior_args_vector (argc - optind, &argv[optind]);
  917. }
  918. else
  919. {
  920. /* OK, that's all the options. */
  921. /* The first argument, if specified, is the name of the
  922. executable. */
  923. if (optind < argc)
  924. {
  925. symarg = argv[optind];
  926. execarg = argv[optind];
  927. optind++;
  928. }
  929. /* If the user hasn't already specified a PID or the name of a
  930. core file, then a second optional argument is allowed. If
  931. present, this argument should be interpreted as either a
  932. PID or a core file, whichever works. */
  933. if (pidarg == NULL && corearg == NULL && optind < argc)
  934. {
  935. pid_or_core_arg = argv[optind];
  936. optind++;
  937. }
  938. /* Any argument left on the command line is unexpected and
  939. will be ignored. Inform the user. */
  940. if (optind < argc)
  941. gdb_printf (gdb_stderr,
  942. _("Excess command line "
  943. "arguments ignored. (%s%s)\n"),
  944. argv[optind],
  945. (optind == argc - 1) ? "" : " ...");
  946. }
  947. /* Lookup gdbinit files. Note that the gdbinit file name may be
  948. overridden during file initialization, so get_init_files should be
  949. called after gdb_init. */
  950. std::vector<std::string> system_gdbinit;
  951. std::string home_gdbinit;
  952. std::string local_gdbinit;
  953. get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
  954. /* Do these (and anything which might call wrap_here or *_filtered)
  955. after initialize_all_files() but before the interpreter has been
  956. installed. Otherwize the help/version messages will be eaten by
  957. the interpreter's output handler. */
  958. if (print_version)
  959. {
  960. print_gdb_version (gdb_stdout, false);
  961. gdb_printf ("\n");
  962. exit (0);
  963. }
  964. if (print_help)
  965. {
  966. print_gdb_help (gdb_stdout);
  967. exit (0);
  968. }
  969. if (print_configuration)
  970. {
  971. print_gdb_configuration (gdb_stdout);
  972. gdb_printf ("\n");
  973. exit (0);
  974. }
  975. /* FIXME: cagney/2003-02-03: The big hack (part 1 of 2) that lets
  976. GDB retain the old MI1 interpreter startup behavior. Output the
  977. copyright message before the interpreter is installed. That way
  978. it isn't encapsulated in MI output. */
  979. if (!quiet && strcmp (interpreter_p, INTERP_MI1) == 0)
  980. {
  981. /* Print all the junk at the top, with trailing "..." if we are
  982. about to read a symbol file (possibly slowly). */
  983. print_gdb_version (gdb_stdout, true);
  984. if (symarg)
  985. gdb_printf ("..");
  986. gdb_printf ("\n");
  987. gdb_flush (gdb_stdout); /* Force to screen during slow
  988. operations. */
  989. }
  990. /* Install the default UI. All the interpreters should have had a
  991. look at things by now. Initialize the default interpreter. */
  992. set_top_level_interpreter (interpreter_p);
  993. /* FIXME: cagney/2003-02-03: The big hack (part 2 of 2) that lets
  994. GDB retain the old MI1 interpreter startup behavior. Output the
  995. copyright message after the interpreter is installed when it is
  996. any sane interpreter. */
  997. if (!quiet && !current_interp_named_p (INTERP_MI1))
  998. {
  999. /* Print all the junk at the top, with trailing "..." if we are
  1000. about to read a symbol file (possibly slowly). */
  1001. print_gdb_version (gdb_stdout, true);
  1002. if (symarg)
  1003. gdb_printf ("..");
  1004. gdb_printf ("\n");
  1005. gdb_flush (gdb_stdout); /* Force to screen during slow
  1006. operations. */
  1007. }
  1008. /* Set off error and warning messages with a blank line. */
  1009. tmp_warn_preprint.reset ();
  1010. warning_pre_print = _("\nwarning: ");
  1011. /* Read and execute the system-wide gdbinit file, if it exists.
  1012. This is done *before* all the command line arguments are
  1013. processed; it sets global parameters, which are independent of
  1014. what file you are debugging or what directory you are in. */
  1015. if (!system_gdbinit.empty () && !inhibit_gdbinit)
  1016. {
  1017. for (const std::string &file : system_gdbinit)
  1018. ret = catch_command_errors (source_script, file.c_str (), 0);
  1019. }
  1020. /* Read and execute $HOME/.gdbinit file, if it exists. This is done
  1021. *before* all the command line arguments are processed; it sets
  1022. global parameters, which are independent of what file you are
  1023. debugging or what directory you are in. */
  1024. if (!home_gdbinit.empty () && !inhibit_gdbinit && !inhibit_home_gdbinit)
  1025. ret = catch_command_errors (source_script, home_gdbinit.c_str (), 0);
  1026. /* Process '-ix' and '-iex' options early. */
  1027. execute_cmdargs (&cmdarg_vec, CMDARG_INIT_FILE, CMDARG_INIT_COMMAND, &ret);
  1028. /* Now perform all the actions indicated by the arguments. */
  1029. if (cdarg != NULL)
  1030. {
  1031. ret = catch_command_errors (cd_command, cdarg, 0);
  1032. }
  1033. for (i = 0; i < dirarg.size (); i++)
  1034. ret = catch_command_errors (directory_switch, dirarg[i], 0);
  1035. /* Skip auto-loading section-specified scripts until we've sourced
  1036. local_gdbinit (which is often used to augment the source search
  1037. path). */
  1038. save_auto_load = global_auto_load;
  1039. global_auto_load = 0;
  1040. if (execarg != NULL
  1041. && symarg != NULL
  1042. && strcmp (execarg, symarg) == 0)
  1043. {
  1044. /* The exec file and the symbol-file are the same. If we can't
  1045. open it, better only print one error message.
  1046. catch_command_errors returns non-zero on success! */
  1047. ret = catch_command_errors (exec_file_attach, execarg,
  1048. !batch_flag);
  1049. if (ret != 0)
  1050. ret = catch_command_errors (symbol_file_add_main_adapter,
  1051. symarg, !batch_flag);
  1052. }
  1053. else
  1054. {
  1055. if (execarg != NULL)
  1056. ret = catch_command_errors (exec_file_attach, execarg,
  1057. !batch_flag);
  1058. if (symarg != NULL)
  1059. ret = catch_command_errors (symbol_file_add_main_adapter,
  1060. symarg, !batch_flag);
  1061. }
  1062. if (corearg && pidarg)
  1063. error (_("Can't attach to process and specify "
  1064. "a core file at the same time."));
  1065. if (corearg != NULL)
  1066. {
  1067. ret = catch_command_errors (core_file_command, corearg,
  1068. !batch_flag);
  1069. }
  1070. else if (pidarg != NULL)
  1071. {
  1072. ret = catch_command_errors (attach_command, pidarg, !batch_flag);
  1073. }
  1074. else if (pid_or_core_arg)
  1075. {
  1076. /* The user specified 'gdb program pid' or gdb program core'.
  1077. If pid_or_core_arg's first character is a digit, try attach
  1078. first and then corefile. Otherwise try just corefile. */
  1079. if (isdigit (pid_or_core_arg[0]))
  1080. {
  1081. ret = catch_command_errors (attach_command, pid_or_core_arg,
  1082. !batch_flag);
  1083. if (ret == 0)
  1084. ret = catch_command_errors (core_file_command,
  1085. pid_or_core_arg,
  1086. !batch_flag);
  1087. }
  1088. else
  1089. {
  1090. /* Can't be a pid, better be a corefile. */
  1091. ret = catch_command_errors (core_file_command,
  1092. pid_or_core_arg,
  1093. !batch_flag);
  1094. }
  1095. }
  1096. if (ttyarg != NULL)
  1097. current_inferior ()->set_tty (ttyarg);
  1098. /* Error messages should no longer be distinguished with extra output. */
  1099. warning_pre_print = _("warning: ");
  1100. /* Read the .gdbinit file in the current directory, *if* it isn't
  1101. the same as the $HOME/.gdbinit file (it should exist, also). */
  1102. if (!local_gdbinit.empty ())
  1103. {
  1104. auto_load_local_gdbinit_pathname
  1105. = gdb_realpath (local_gdbinit.c_str ()).release ();
  1106. if (!inhibit_gdbinit && auto_load_local_gdbinit)
  1107. {
  1108. auto_load_debug_printf ("Loading .gdbinit file \"%s\".",
  1109. local_gdbinit.c_str ());
  1110. if (file_is_auto_load_safe (local_gdbinit.c_str ()))
  1111. {
  1112. auto_load_local_gdbinit_loaded = 1;
  1113. ret = catch_command_errors (source_script, local_gdbinit.c_str (), 0);
  1114. }
  1115. }
  1116. }
  1117. /* Now that all .gdbinit's have been read and all -d options have been
  1118. processed, we can read any scripts mentioned in SYMARG.
  1119. We wait until now because it is common to add to the source search
  1120. path in local_gdbinit. */
  1121. global_auto_load = save_auto_load;
  1122. for (objfile *objfile : current_program_space->objfiles ())
  1123. load_auto_scripts_for_objfile (objfile);
  1124. /* Process '-x' and '-ex' options. */
  1125. execute_cmdargs (&cmdarg_vec, CMDARG_FILE, CMDARG_COMMAND, &ret);
  1126. /* Read in the old history after all the command files have been
  1127. read. */
  1128. init_history ();
  1129. if (batch_flag)
  1130. {
  1131. int error_status = EXIT_FAILURE;
  1132. int *exit_arg = ret == 0 ? &error_status : NULL;
  1133. /* We have hit the end of the batch file. */
  1134. quit_force (exit_arg, 0);
  1135. }
  1136. }
  1137. static void
  1138. captured_main (void *data)
  1139. {
  1140. struct captured_main_args *context = (struct captured_main_args *) data;
  1141. captured_main_1 (context);
  1142. /* NOTE: cagney/1999-11-07: There is probably no reason for not
  1143. moving this loop and the code found in captured_command_loop()
  1144. into the command_loop() proper. The main thing holding back that
  1145. change - SET_TOP_LEVEL() - has been eliminated. */
  1146. while (1)
  1147. {
  1148. try
  1149. {
  1150. captured_command_loop ();
  1151. }
  1152. catch (const gdb_exception &ex)
  1153. {
  1154. exception_print (gdb_stderr, ex);
  1155. }
  1156. }
  1157. /* No exit -- exit is through quit_command. */
  1158. }
  1159. int
  1160. gdb_main (struct captured_main_args *args)
  1161. {
  1162. try
  1163. {
  1164. captured_main (args);
  1165. }
  1166. catch (const gdb_exception &ex)
  1167. {
  1168. exception_print (gdb_stderr, ex);
  1169. }
  1170. /* The only way to end up here is by an error (normal exit is
  1171. handled by quit_force()), hence always return an error status. */
  1172. return 1;
  1173. }
  1174. /* Don't use *_filtered for printing help. We don't want to prompt
  1175. for continue no matter how small the screen or how much we're going
  1176. to print. */
  1177. static void
  1178. print_gdb_help (struct ui_file *stream)
  1179. {
  1180. std::vector<std::string> system_gdbinit;
  1181. std::string home_gdbinit;
  1182. std::string local_gdbinit;
  1183. std::string home_gdbearlyinit;
  1184. get_init_files (&system_gdbinit, &home_gdbinit, &local_gdbinit);
  1185. get_earlyinit_files (&home_gdbearlyinit);
  1186. /* Note: The options in the list below are only approximately sorted
  1187. in the alphabetical order, so as to group closely related options
  1188. together. */
  1189. gdb_puts (_("\
  1190. This is the GNU debugger. Usage:\n\n\
  1191. gdb [options] [executable-file [core-file or process-id]]\n\
  1192. gdb [options] --args executable-file [inferior-arguments ...]\n\n\
  1193. "), stream);
  1194. gdb_puts (_("\
  1195. Selection of debuggee and its files:\n\n\
  1196. --args Arguments after executable-file are passed to inferior.\n\
  1197. --core=COREFILE Analyze the core dump COREFILE.\n\
  1198. --exec=EXECFILE Use EXECFILE as the executable.\n\
  1199. --pid=PID Attach to running process PID.\n\
  1200. --directory=DIR Search for source files in DIR.\n\
  1201. --se=FILE Use FILE as symbol file and executable file.\n\
  1202. --symbols=SYMFILE Read symbols from SYMFILE.\n\
  1203. --readnow Fully read symbol files on first access.\n\
  1204. --readnever Do not read symbol files.\n\
  1205. --write Set writing into executable and core files.\n\n\
  1206. "), stream);
  1207. gdb_puts (_("\
  1208. Initial commands and command files:\n\n\
  1209. --command=FILE, -x Execute GDB commands from FILE.\n\
  1210. --init-command=FILE, -ix\n\
  1211. Like -x but execute commands before loading inferior.\n\
  1212. --eval-command=COMMAND, -ex\n\
  1213. Execute a single GDB command.\n\
  1214. May be used multiple times and in conjunction\n\
  1215. with --command.\n\
  1216. --init-eval-command=COMMAND, -iex\n\
  1217. Like -ex but before loading inferior.\n\
  1218. --nh Do not read ~/.gdbinit.\n\
  1219. --nx Do not read any .gdbinit files in any directory.\n\n\
  1220. "), stream);
  1221. gdb_puts (_("\
  1222. Output and user interface control:\n\n\
  1223. --fullname Output information used by emacs-GDB interface.\n\
  1224. --interpreter=INTERP\n\
  1225. Select a specific interpreter / user interface.\n\
  1226. --tty=TTY Use TTY for input/output by the program being debugged.\n\
  1227. -w Use the GUI interface.\n\
  1228. --nw Do not use the GUI interface.\n\
  1229. "), stream);
  1230. #if defined(TUI)
  1231. gdb_puts (_("\
  1232. --tui Use a terminal user interface.\n\
  1233. "), stream);
  1234. #endif
  1235. gdb_puts (_("\
  1236. -q, --quiet, --silent\n\
  1237. Do not print version number on startup.\n\n\
  1238. "), stream);
  1239. gdb_puts (_("\
  1240. Operating modes:\n\n\
  1241. --batch Exit after processing options.\n\
  1242. --batch-silent Like --batch, but suppress all gdb stdout output.\n\
  1243. --return-child-result\n\
  1244. GDB exit code will be the child's exit code.\n\
  1245. --configuration Print details about GDB configuration and then exit.\n\
  1246. --help Print this message and then exit.\n\
  1247. --version Print version information and then exit.\n\n\
  1248. Remote debugging options:\n\n\
  1249. -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
  1250. -l TIMEOUT Set timeout in seconds for remote debugging.\n\n\
  1251. Other options:\n\n\
  1252. --cd=DIR Change current directory to DIR.\n\
  1253. --data-directory=DIR, -D\n\
  1254. Set GDB's data-directory to DIR.\n\
  1255. "), stream);
  1256. gdb_puts (_("\n\
  1257. At startup, GDB reads the following early init files and executes their\n\
  1258. commands:\n\
  1259. "), stream);
  1260. if (!home_gdbearlyinit.empty ())
  1261. gdb_printf (stream, _("\
  1262. * user-specific early init file: %s\n\
  1263. "), home_gdbearlyinit.c_str ());
  1264. if (home_gdbearlyinit.empty ())
  1265. gdb_printf (stream, _("\
  1266. None found.\n"));
  1267. gdb_puts (_("\n\
  1268. At startup, GDB reads the following init files and executes their commands:\n\
  1269. "), stream);
  1270. if (!system_gdbinit.empty ())
  1271. {
  1272. std::string output;
  1273. for (size_t idx = 0; idx < system_gdbinit.size (); ++idx)
  1274. {
  1275. output += system_gdbinit[idx];
  1276. if (idx < system_gdbinit.size () - 1)
  1277. output += ", ";
  1278. }
  1279. gdb_printf (stream, _("\
  1280. * system-wide init files: %s\n\
  1281. "), output.c_str ());
  1282. }
  1283. if (!home_gdbinit.empty ())
  1284. gdb_printf (stream, _("\
  1285. * user-specific init file: %s\n\
  1286. "), home_gdbinit.c_str ());
  1287. if (!local_gdbinit.empty ())
  1288. gdb_printf (stream, _("\
  1289. * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
  1290. "), local_gdbinit.c_str ());
  1291. if (system_gdbinit.empty () && home_gdbinit.empty ()
  1292. && local_gdbinit.empty ())
  1293. gdb_printf (stream, _("\
  1294. None found.\n"));
  1295. gdb_puts (_("\n\
  1296. For more information, type \"help\" from within GDB, or consult the\n\
  1297. GDB manual (available as on-line info or a printed manual).\n\
  1298. "), stream);
  1299. if (REPORT_BUGS_TO[0] && stream == gdb_stdout)
  1300. gdb_printf (stream, _("\n\
  1301. Report bugs to %ps.\n\
  1302. "), styled_string (file_name_style.style (), REPORT_BUGS_TO));
  1303. if (stream == gdb_stdout)
  1304. gdb_printf (stream, _("\n\
  1305. You can ask GDB-related questions on the GDB users mailing list\n\
  1306. (gdb@sourceware.org) or on GDB's IRC channel (#gdb on Freenode).\n"));
  1307. }