31#include "gdbsupport/event-loop.h"
43#include "gdbsupport/filestuff.h"
47#include "gdbsupport/signals-state-save-restore.h"
50#include "gdbsupport/pathstuff.h"
53#include "gdbtk/generic/gdbtk.h"
55#include "gdbsupport/alt-stack.h"
116 if (stat (new_datadir, &st) < 0)
118 int save_errno = errno;
123 else if (!S_ISDIR (st.st_mode))
124 warning (_(
"%ps is not a directory."),
148 gdb::unique_xmalloc_ptr<char> str (make_relative_prefix (progname,
153 return std::string ();
171 if (stat (dir.c_str (), &s) != 0 || !S_ISDIR (s.st_mode))
182 gdb::unique_xmalloc_ptr<char> canon_sysroot (lrealpath (dir.c_str ()));
185 dir = canon_sysroot.get ();
201 std::string relocated_path;
206 && datadir_len < file.length ()
207 && filename_ncmp (file.c_str (),
GDB_DATADIR, datadir_len) == 0
208 && IS_DIR_SEPARATOR (file[datadir_len]))
213 size_t start = datadir_len;
214 for (; IS_DIR_SEPARATOR (file[start]); ++start)
216 relocated_path =
gdb_datadir + SLASH_STRING + file.substr (start);
223 return relocated_path;
242 const char *system_filename,
243 bool system_filename_relocatable,
244 const char *system_dirname,
245 bool system_dirname_relocatable,
246 bool lookup_local_file)
250 if (system_filename !=
nullptr && system_filename[0] !=
'\0')
252 std::string relocated_filename
254 system_filename_relocatable);
255 if (!relocated_filename.empty ()
256 && stat (relocated_filename.c_str (), &s) == 0)
260 if (system_dirname !=
nullptr && system_dirname[0] !=
'\0')
262 std::string relocated_dirname
264 system_dirname_relocatable);
265 if (!relocated_dirname.empty ())
267 gdb_dir_up dir (opendir (relocated_dirname.c_str ()));
270 std::vector<std::string> files;
273 struct dirent *ent = readdir (dir.get ());
276 std::string
name (ent->d_name);
281 std::string tmp_filename
282 = relocated_dirname + SLASH_STRING +
name;
283 if (stat (tmp_filename.c_str (), &s) != 0
284 || !S_ISREG (s.st_mode))
293 files.push_back (std::move (tmp_filename));
295 std::sort (files.begin (), files.end ());
297 files.begin (), files.end ());
308 struct stat homebuf, cwdbuf;
309 memset (&homebuf, 0,
sizeof (
struct stat));
310 memset (&cwdbuf, 0,
sizeof (
struct stat));
312 m_home_file = find_gdb_home_config_file (filename, &homebuf);
314 if (lookup_local_file && stat (filename, &cwdbuf) == 0)
317 || memcmp ((
char *) &homebuf, (
char *) &cwdbuf,
318 sizeof (
struct stat)))
363 std::string *home_gdbinit,
364 std::string *local_gdbinit)
368 static gdb::optional<gdb_initfile_finder> init_files;
369 if (!init_files.has_value ())
374 *system_gdbinit = init_files->system_files ();
375 *home_gdbinit = init_files->home_file ();
376 *local_gdbinit = init_files->local_file ();
388 static gdb::optional<gdb_initfile_finder> init_files;
389 if (!init_files.has_value ())
390 init_files.emplace (
GDBEARLYINIT,
nullptr,
false,
nullptr,
false,
false);
392 *home_gdbearlyinit = init_files->home_file ();
411 result = gdb_do_one_event ();
413 catch (
const gdb_exception &ex)
436 (*after_char_processing_hook) ();
506 const char *arg,
int from_tty,
507 bool do_bp_actions =
false)
513 command (arg, from_tty);
521 catch (
const gdb_exception &e)
535 symfile_add_flags add_flags = 0;
550 error (_(
"%s: '--readnow' and '--readnever' cannot be "
551 "specified simultaneously"),
602 for (
const auto &cmdarg_p : *cmdarg_vec)
604 if (cmdarg_p.type == file_type)
607 else if (cmdarg_p.type == cmd_type)
616 int argc = context->
argc;
617 char **argv = context->
argv;
619 static int quiet = 0;
620 static int set_args = 0;
621 static int inhibit_home_gdbinit = 0;
625 char *execarg = NULL;
627 char *corearg = NULL;
628 char *pid_or_core_arg = NULL;
634 static int print_help;
635 static int print_version;
636 static int print_configuration;
639 std::vector<struct cmdarg> cmdarg_vec;
642 std::vector<char *> dirarg;
648#ifdef HAVE_USEFUL_SBRK
655#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
656 setlocale (LC_MESSAGES,
"");
658#if defined (HAVE_SETLOCALE)
659 setlocale (LC_CTYPE,
"");
662 bindtextdomain (
PACKAGE, LOCALEDIR);
671 setvbuf (stderr, NULL, _IONBF, BUFSIZ);
676 main_ui =
new ui (stdin, stdout, stderr);
683 if (bfd_init () != BFD_INIT_MAGIC)
684 error (_(
"fatal error: libbfd ABI mismatch"));
695 gdb::unique_xmalloc_ptr<char> tmp_warn_preprint
699 current_directory = getcwd (NULL, 0);
700 if (current_directory == NULL)
716#ifdef WITH_PYTHON_LIBDIR
756 int write_files_1 = 0;
757 static struct option long_options[] =
759 {
"tui", no_argument, 0, OPT_TUI},
760 {
"readnow", no_argument, NULL, OPT_READNOW},
761 {
"readnever", no_argument, NULL, OPT_READNEVER},
762 {
"r", no_argument, NULL, OPT_READNOW},
763 {
"quiet", no_argument, &quiet, 1},
764 {
"q", no_argument, &quiet, 1},
765 {
"silent", no_argument, &quiet, 1},
766 {
"nh", no_argument, &inhibit_home_gdbinit, 1},
769 {
"batch-silent", no_argument, 0,
'B'},
775 {
"fullname", no_argument, 0,
'f'},
776 {
"f", no_argument, 0,
'f'},
778 {
"annotate", required_argument, 0, OPT_ANNOTATE},
779 {
"help", no_argument, &print_help, 1},
780 {
"se", required_argument, 0, OPT_SE},
781 {
"symbols", required_argument, 0,
's'},
782 {
"s", required_argument, 0,
's'},
783 {
"exec", required_argument, 0,
'e'},
784 {
"e", required_argument, 0,
'e'},
785 {
"core", required_argument, 0,
'c'},
786 {
"c", required_argument, 0,
'c'},
787 {
"pid", required_argument, 0,
'p'},
788 {
"p", required_argument, 0,
'p'},
789 {
"command", required_argument, 0,
'x'},
790 {
"eval-command", required_argument, 0,
'X'},
791 {
"version", no_argument, &print_version, 1},
792 {
"configuration", no_argument, &print_configuration, 1},
793 {
"x", required_argument, 0,
'x'},
794 {
"ex", required_argument, 0,
'X'},
795 {
"init-command", required_argument, 0, OPT_IX},
796 {
"init-eval-command", required_argument, 0, OPT_IEX},
797 {
"ix", required_argument, 0, OPT_IX},
798 {
"iex", required_argument, 0, OPT_IEX},
799 {
"early-init-command", required_argument, 0, OPT_EIX},
800 {
"early-init-eval-command", required_argument, 0, OPT_EIEX},
801 {
"eix", required_argument, 0, OPT_EIX},
802 {
"eiex", required_argument, 0, OPT_EIEX},
804 {
"tclcommand", required_argument, 0,
'z'},
805 {
"enable-external-editor", no_argument, 0,
'y'},
806 {
"editor-command", required_argument, 0,
'w'},
808 {
"ui", required_argument, 0,
'i'},
809 {
"interpreter", required_argument, 0,
'i'},
810 {
"i", required_argument, 0,
'i'},
811 {
"directory", required_argument, 0,
'd'},
812 {
"d", required_argument, 0,
'd'},
813 {
"data-directory", required_argument, 0,
'D'},
814 {
"D", required_argument, 0,
'D'},
815 {
"cd", required_argument, 0, OPT_CD},
816 {
"tty", required_argument, 0,
't'},
817 {
"baud", required_argument, 0,
'b'},
818 {
"b", required_argument, 0,
'b'},
819 {
"nw", no_argument, NULL, OPT_NOWINDOWS},
820 {
"nowindows", no_argument, NULL, OPT_NOWINDOWS},
821 {
"w", no_argument, NULL, OPT_WINDOWS},
822 {
"windows", no_argument, NULL, OPT_WINDOWS},
823 {
"statistics", no_argument, 0, OPT_STATISTICS},
824 {
"write", no_argument, &write_files_1, 1},
825 {
"args", no_argument, &set_args, 1},
826 {
"l", required_argument, 0,
'l'},
828 {0, no_argument, 0, 0}
835 c = getopt_long_only (argc, argv,
"",
836 long_options, &option_index);
837 if (c == EOF || set_args)
841 if (c == 0 && long_options[option_index].flag == 0)
842 c = long_options[option_index].val;
923 if (optarg[0] ==
'\0')
924 error (_(
"%s: empty path for `--data-directory'"),
932 if (!gdbtk_test (optarg))
933 error (_(
"%s: unable to load tclcommand file \"%s\""),
944 external_editor_command = xstrdup (optarg);
952 dirarg.push_back (optarg);
965 rate = strtol (optarg, &p, 0);
966 if (
rate == 0 && p == optarg)
967 warning (_(
"could not set baud rate to `%s'."),
978 timeout = strtol (optarg, &p, 0);
979 if (timeout == 0 && p == optarg)
980 warning (_(
"could not set timeout limit to `%s'."),
1002 error (_(
"Use `%s --help' for a complete list of options."),
1017 save_original_signals_state (quiet);
1020 gdb::alternate_signal_stack signal_stack;
1028 std::string home_gdbearlyinit;
1030 if (!home_gdbearlyinit.empty () && !inhibit_home_gdbinit)
1032 home_gdbearlyinit.c_str (), 0);
1053 error (_(
"%s: `--args' specified but no program specified"),
1056 symarg = argv[optind];
1057 execarg = argv[optind];
1069 symarg = argv[optind];
1070 execarg = argv[optind];
1078 if (pidarg == NULL && corearg == NULL && optind < argc)
1080 pid_or_core_arg = argv[optind];
1088 _(
"Excess command line "
1089 "arguments ignored. (%s%s)\n"),
1091 (optind == argc - 1) ?
"" :
" ...");
1097 std::vector<std::string> system_gdbinit;
1098 std::string home_gdbinit;
1099 std::string local_gdbinit;
1120 if (print_configuration)
1164 tmp_warn_preprint.reset ();
1173 for (
const std::string &file : system_gdbinit)
1182 if (!home_gdbinit.empty () && !
inhibit_gdbinit && !inhibit_home_gdbinit)
1194 for (i = 0; i < dirarg.size (); i++)
1205 && strcmp (execarg, symarg) == 0)
1218 if (execarg != NULL)
1226 if (corearg && pidarg)
1227 error (_(
"Can't attach to process and specify "
1228 "a core file at the same time."));
1230 if (corearg != NULL)
1235 else if (pidarg != NULL)
1239 else if (pid_or_core_arg)
1245 if (isdigit (pid_or_core_arg[0]))
1271 if (!local_gdbinit.empty ())
1274 = gdb_realpath (local_gdbinit.c_str ()).release ();
1279 local_gdbinit.c_str ());
1307 int error_status = EXIT_FAILURE;
1308 int *exit_arg = ret == 0 ? &error_status : NULL;
1332 catch (
const gdb_exception &ex)
1347 catch (
const gdb_exception &ex)
1365 std::vector<std::string> system_gdbinit;
1366 std::string home_gdbinit;
1367 std::string local_gdbinit;
1368 std::string home_gdbearlyinit;
1377This is the GNU debugger. Usage:\n\n\
1378 gdb [options] [executable-file [core-file or process-id]]\n\
1379 gdb [options] --args executable-file [inferior-arguments ...]\n\n\
1382Selection of debuggee and its files:\n\n\
1383 --args Arguments after executable-file are passed to inferior.\n\
1384 --core=COREFILE Analyze the core dump COREFILE.\n\
1385 --exec=EXECFILE Use EXECFILE as the executable.\n\
1386 --pid=PID Attach to running process PID.\n\
1387 --directory=DIR Search for source files in DIR.\n\
1388 --se=FILE Use FILE as symbol file and executable file.\n\
1389 --symbols=SYMFILE Read symbols from SYMFILE.\n\
1390 --readnow Fully read symbol files on first access.\n\
1391 --readnever Do not read symbol files.\n\
1392 --write Set writing into executable and core files.\n\n\
1395Initial commands and command files:\n\n\
1396 --command=FILE, -x Execute GDB commands from FILE.\n\
1397 --init-command=FILE, -ix\n\
1398 Like -x but execute commands before loading inferior.\n\
1399 --eval-command=COMMAND, -ex\n\
1400 Execute a single GDB command.\n\
1401 May be used multiple times and in conjunction\n\
1403 --init-eval-command=COMMAND, -iex\n\
1404 Like -ex but before loading inferior.\n\
1405 --nh Do not read ~/.gdbinit.\n\
1406 --nx Do not read any .gdbinit files in any directory.\n\n\
1409Output and user interface control:\n\n\
1410 --fullname Output information used by emacs-GDB interface.\n\
1411 --interpreter=INTERP\n\
1412 Select a specific interpreter / user interface.\n\
1413 --tty=TTY Use TTY for input/output by the program being debugged.\n\
1414 -w Use the GUI interface.\n\
1415 --nw Do not use the GUI interface.\n\
1419 --tui Use a terminal user interface.\n\
1423 -q, --quiet, --silent\n\
1424 Do not print version number on startup.\n\n\
1427Operating modes:\n\n\
1428 --batch Exit after processing options.\n\
1429 --batch-silent Like --batch, but suppress all gdb stdout output.\n\
1430 --return-child-result\n\
1431 GDB exit code will be the child's exit code.\n\
1432 --configuration Print details about GDB configuration and then exit.\n\
1433 --help Print this message and then exit.\n\
1434 --version Print version information and then exit.\n\n\
1435Remote debugging options:\n\n\
1436 -b BAUDRATE Set serial port baud rate used for remote debugging.\n\
1437 -l TIMEOUT Set timeout in seconds for remote debugging.\n\n\
1439 --cd=DIR Change current directory to DIR.\n\
1440 --data-directory=DIR, -D\n\
1441 Set GDB's data-directory to DIR.\n\
1444At startup, GDB reads the following early init files and executes their\n\
1447 if (!home_gdbearlyinit.empty ())
1449 * user-specific early init file: %s\n\
1450"), home_gdbearlyinit.c_str ());
1451 if (home_gdbearlyinit.empty ())
1455At startup, GDB reads the following init files and executes their commands:\n\
1457 if (!system_gdbinit.empty ())
1460 for (
size_t idx = 0; idx < system_gdbinit.size (); ++idx)
1462 output += system_gdbinit[idx];
1463 if (idx < system_gdbinit.size () - 1)
1467 * system-wide init files: %s\n\
1468"), output.c_str ());
1470 if (!home_gdbinit.empty ())
1472 * user-specific init file: %s\n\
1473"), home_gdbinit.c_str ());
1474 if (!local_gdbinit.empty ())
1476 * local init file (see also 'set auto-load local-gdbinit'): ./%s\n\
1477"), local_gdbinit.c_str ());
1478 if (system_gdbinit.empty () && home_gdbinit.empty ()
1479 && local_gdbinit.empty ())
1483For more information, type \"help\" from within GDB, or consult the\n\
1484GDB manual (available as on-line info or a printed manual).\n\
1488Report bugs to %ps.\n\
1492You can ask GDB-related questions on the GDB users mailing list\n\
1493(gdb@sourceware.org) or on GDB's IRC channel (#gdb on Freenode).\n"));
long __attribute__((__aligned__(4)))
bool file_is_auto_load_safe(const char *filename)
void load_auto_scripts_for_objfile(struct objfile *objfile)
bool auto_load_local_gdbinit_loaded
bool auto_load_local_gdbinit
char * auto_load_local_gdbinit_pathname
#define auto_load_debug_printf(fmt,...)
void bpstat_do_actions(void)
ui_file_style style() const
gdb_initfile_finder(const char *filename, const char *system_filename, bool system_filename_relocatable, const char *system_dirname, bool system_dirname_relocatable, bool lookup_local_file)
const std::string & home_file() const
std::vector< std::string > m_system_files
DISABLE_COPY_AND_ASSIGN(gdb_initfile_finder)
const std::string & local_file() const
const std::vector< std::string > & system_files() const
void set_tty(std::string terminal_name)
void cd_command(const char *dir, int from_tty)
void source_script(const char *file, int from_tty)
void quit_command(const char *args, int from_tty)
cli_style_option file_name_style
#define PYTHON_LIBDIR_RELOCATABLE
#define SYSTEM_GDBINIT_RELOCATABLE
#define TARGET_SYSTEM_ROOT_RELOCATABLE
#define SYSTEM_GDBINIT_DIR_RELOCATABLE
#define WITH_PYTHON_LIBDIR
#define GDB_DATADIR_RELOCATABLE
#define TARGET_SYSTEM_ROOT
#define DEBUGDIR_RELOCATABLE
#define SYSTEM_GDBINIT_DIR
void core_file_command(const char *filename, int from_tty)
std::string debug_file_directory
void async_enable_stdin(void)
void(* after_char_processing_hook)(void)
void exception_print(struct ui_file *file, const struct gdb_exception &e)
void exec_file_attach(const char *filename, int from_tty)
int ext_lang_present_p(const struct extension_language_defn *extlang)
void ext_lang_initialization(void)
const struct extension_language_defn * get_ext_lang_of_file(const char *file)
#define TARGET_SYSROOT_PREFIX
void execute_command(const char *, int)
void attach_command(const char *args, int from_tty)
void set_inferior_args_vector(int argc, char **argv)
struct inferior * current_inferior(void)
void interp_pre_command_loop(struct interp *interp)
int current_interp_named_p(const char *interp_name)
struct interp * top_level_interpreter(void)
void set_top_level_interpreter(const char *name)
void set_gdb_data_directory(const char *new_datadir)
static void captured_command_loop() __attribute__((noinline))
static int handle_command_errors(const struct gdb_exception &e)
static void execute_cmdargs(const std::vector< struct cmdarg > *cmdarg_vec, cmdarg_kind file_type, cmdarg_kind cmd_type, int *ret)
int return_child_result_value
static void captured_main_1(struct captured_main_args *context)
std::string interpreter_p
static char * gdb_program_name
const char * get_gdb_program_name(void)
static void print_gdb_help(struct ui_file *)
std::string python_libdir
std::string relocate_gdb_directory(const char *initial, bool relocatable)
static std::string relocate_path(const char *progname, const char *initial, bool relocatable)
struct ui_file * gdb_stdtarg
struct ui_file * gdb_stdtargerr
static void captured_main(void *data)
static void get_init_files(std::vector< std::string > *system_gdbinit, std::string *home_gdbinit, std::string *local_gdbinit)
static std::string relocate_file_path_maybe_in_datadir(const std::string &file, bool relocatable)
struct ui_file * gdb_stdtargin
int gdb_main(struct captured_main_args *args)
static int gdb_datadir_provided
static void start_event_loop()
static void validate_readnow_readnever()
static void get_earlyinit_files(std::string *home_gdbearlyinit)
@ CMDARG_EARLYINIT_COMMAND
static void symbol_file_add_main_adapter(const char *arg, int from_tty)
void() catch_command_errors_const_ftype(const char *, int)
static int catch_command_errors(catch_command_errors_const_ftype command, const char *arg, int from_tty, bool do_bp_actions=false)
char * windows_get_absolute_argv0(const char *argv0)
void set_per_command_space(int new_value)
void set_per_command_time(int new_value)
struct program_space * current_program_space
void directory_switch(const char *dirname, int from_tty)
void add_substitute_path_rule(const char *from, const char *to)
const char * interpreter_p
cmdarg(cmdarg_kind type_, char *string_)
objfiles_range objfiles()
enum prompt_state prompt_state
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
int readnever_symbol_files
void print_gdb_version(struct ui_file *stream, bool interactive)
void quit_force(int *exit_arg, int from_tty)
void print_gdb_configuration(struct ui_file *stream)
void maybe_wait_sync_command_done(int was_sync)
#define SWITCH_THRU_ALL_UIS()
static styled_string_s * styled_string(const ui_file_style &style, const char *str, styled_string_s &&tmp={})
void perror_warning_with_name(const char *string)
void print_sys_errmsg(const char *string, int errcode)
void gdb_printf(struct ui_file *stream, const char *format,...)
void gdb_flush(struct ui_file *stream)
const char * warning_pre_print
void gdb_puts(const char *linebuffer, struct ui_file *stream)