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