Skip to content

Commit 8227276

Browse files
ivanivanov884jeffmahoney
authored andcommitted
gdb-6.6-buildid-locate-core-as-arg.patch
;;=push+jan http://sourceware.org/ml/gdb-patches/2010-01/msg00558.html [ Fixed up since the mail. ] On Thu, 21 Jan 2010 18:17:15 +0100, Doug Evans wrote: > Not an exhaustive list, but if we go down the path of converting "gdb > corefile" to "gdb -c corefile", then we also need to think about "file > corefile" being converted to "core corefile" [or "target core > corefile", "core" is apparently deprecated in favor of "target core"] > and "target exec corefile" -> "target core corefile". Presumably > "file corefile" (and "target exec corefile") would discard the > currently selected executable. But maybe not. Will that be confusing > for users? I don't know. While thinking about it overriding some GDB _commands_ was not my intention. There is a general assumption if I have a shell COMMAND and some FILE I can do $ COMMAND FILE and COMMAND will appropriately load the FILE. FSF GDB currently needs to specify also the executable file for core files which already inhibits this intuitive expectation. OTOH with the build-id locating patch which could allow such intuitive start notneeding the executable file. Still it currently did not work due to the required "-c": $ COMMAND -c COREFILE Entering "file", "core-file" or "attach" commands is already explicit enough so that it IMO should do what the command name says without any autodetections. The second command line argument (captured_main->pid_or_core_arg) is also autodetected (for PID or CORE) but neither "attach" accepts a core file nor "core-file" accepts a PID. The patch makes sense only with the build-id patchset so this is not submit for FSF GDB inclusion yet. I am fine with your patch (+/- Hui Zhu's pending bfd_check_format_matches) as the patch below is its natural extension. Sorry for the delay, Jan 2010-01-25 Jan Kratochvil <jan.kratochvil@redhat.com> * exceptions.h (enum errors <IS_CORE_ERROR>): New. * exec.c: Include exceptions.h. (exec_file_attach <bfd_core>): Call throw_error (IS_CORE_ERROR, ...). * main.c (exec_or_core_file_attach): New. (captured_main <optind < argc>): Set also corearg. (captured_main <strcmp (execarg, symarg) == 0>): New variable func. Call exec_or_core_file_attach if COREARG matches EXECARG. Call symbol_file_add_main only if CORE_BFD remained NULL. Http://sourceware.org/ml/gdb-patches/2010-01/msg00517.html 2010-01-20 Doug Evans <dje@google.com> * exec.c (exec_file_attach): Print a more useful error message if the user did "gdb core".
1 parent bb3d96a commit 8227276

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

gdb/common/common-exceptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ enum errors {
106106
"_ERROR" is appended to the name. */
107107
MAX_COMPLETIONS_REACHED_ERROR,
108108

109+
/* Attempt to load a core file as executable. */
110+
IS_CORE_ERROR,
111+
109112
/* Add more errors here. */
110113
NR_ERRORS
111114
};

gdb/exec.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "gdb_bfd.h"
3737
#include "gcore.h"
3838
#include "source.h"
39+
#include "exceptions.h"
3940

4041
#include <fcntl.h>
4142
#include "readline/readline.h"
@@ -345,12 +346,27 @@ exec_file_attach (const char *filename, int from_tty)
345346

346347
if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
347348
{
349+
int is_core;
350+
351+
/* If the user accidentally did "gdb core", print a useful
352+
error message. Check it only after bfd_object has been checked as
353+
a valid executable may get recognized for example also as
354+
"trad-core". */
355+
is_core = bfd_check_format (exec_bfd, bfd_core);
356+
348357
/* Make sure to close exec_bfd, or else "run" might try to use
349358
it. */
350359
exec_close ();
351-
error (_("\"%s\": not in executable format: %s"),
352-
scratch_pathname,
353-
gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
360+
361+
if (is_core != 0)
362+
throw_error (IS_CORE_ERROR,
363+
_("\"%s\" is a core file.\n"
364+
"Please specify an executable to debug."),
365+
scratch_pathname);
366+
else
367+
error (_("\"%s\": not in executable format: %s"),
368+
scratch_pathname,
369+
gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
354370
}
355371

356372
if (build_section_table (exec_bfd, &sections, &sections_end))

gdb/main.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,34 @@ struct cmdarg
439439
char *string;
440440
};
441441

442+
/* Call exec_file_attach. If it detected FILENAME is a core file call
443+
core_file_command. Print the original exec_file_attach error only if
444+
core_file_command failed to find a matching executable. */
445+
446+
static void
447+
exec_or_core_file_attach (const char *filename, int from_tty)
448+
{
449+
gdb_assert (exec_bfd == NULL);
450+
451+
try
452+
{
453+
exec_file_attach (filename, from_tty);
454+
}
455+
catch (const gdb_exception &e)
456+
{
457+
if (e.error == IS_CORE_ERROR)
458+
{
459+
core_file_command ((char *) filename, from_tty);
460+
461+
/* Iff the core file found its executable suppress the error message
462+
from exec_file_attach. */
463+
if (exec_bfd != NULL)
464+
return;
465+
}
466+
throw_exception (e);
467+
}
468+
}
469+
442470
static void
443471
captured_main_1 (struct captured_main_args *context)
444472
{
@@ -884,6 +912,8 @@ captured_main_1 (struct captured_main_args *context)
884912
{
885913
symarg = argv[optind];
886914
execarg = argv[optind];
915+
if (optind + 1 == argc && corearg == NULL)
916+
corearg = argv[optind];
887917
optind++;
888918
}
889919

@@ -1034,12 +1064,23 @@ captured_main_1 (struct captured_main_args *context)
10341064
&& symarg != NULL
10351065
&& strcmp (execarg, symarg) == 0)
10361066
{
1067+
catch_command_errors_const_ftype *func;
1068+
1069+
/* Call exec_or_core_file_attach only if the file was specified as
1070+
a command line argument (and not an a command line option). */
1071+
if (corearg != NULL && strcmp (corearg, execarg) == 0)
1072+
{
1073+
func = exec_or_core_file_attach;
1074+
corearg = NULL;
1075+
}
1076+
else
1077+
func = exec_file_attach;
1078+
10371079
/* The exec file and the symbol-file are the same. If we can't
10381080
open it, better only print one error message.
10391081
catch_command_errors returns non-zero on success! */
1040-
ret = catch_command_errors (exec_file_attach, execarg,
1041-
!batch_flag);
1042-
if (ret != 0)
1082+
ret = catch_command_errors (func, execarg, !batch_flag);
1083+
if (ret != 0 && core_bfd == NULL)
10431084
ret = catch_command_errors (symbol_file_add_main_adapter,
10441085
symarg, !batch_flag);
10451086
}

0 commit comments

Comments
 (0)