2007-12-28 Pedro Alves * main.c (captured_main): Error out if --core and --pid options were issued simultaneously. If an explicit pid option was passed, don't fallback to core file. Detect extra arguments better in the presence of explicit pid or core arguments. --- gdb/main.c | 76 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 28 deletions(-) Index: src/gdb/main.c =================================================================== --- src.orig/gdb/main.c 2008-01-02 17:58:39.000000000 +0000 +++ src/gdb/main.c 2008-01-03 16:43:03.000000000 +0000 @@ -127,7 +127,9 @@ captured_main (void *data) /* Pointers to various arguments from command line. */ char *symarg = NULL; char *execarg = NULL; + char *pidarg = NULL; char *corearg = NULL; + char *pid_or_core_arg = NULL; char *cdarg = NULL; char *ttyarg = NULL; @@ -435,8 +437,7 @@ captured_main (void *data) corearg = optarg; break; case 'p': - /* "corearg" is shared by "--core" and "--pid" */ - corearg = optarg; + pidarg = optarg; break; case 'x': cmdarg[ncmd].type = CMDARG_FILE; @@ -575,23 +576,32 @@ extern int gdbtk_test (char *); /* OK, that's all the options. The other arguments are filenames. */ count = 0; for (; optind < argc; optind++) - switch (++count) - { - case 1: - symarg = argv[optind]; - execarg = argv[optind]; - break; - case 2: - /* The documentation says this can be a "ProcID" as well. - We will try it as both a corefile and a pid. */ - corearg = argv[optind]; - break; - case 3: - fprintf_unfiltered (gdb_stderr, - _("Excess command line arguments ignored. (%s%s)\n"), - argv[optind], (optind == argc - 1) ? "" : " ..."); - break; - } + { + ++count; + if (count == 1) + { + symarg = argv[optind]; + execarg = argv[optind]; + } + else if (count > 2 + /* If we have a --pid or a --core argument, + this argument don't make sense. */ + || pidarg != NULL + || corearg != NULL) + { + fprintf_unfiltered (gdb_stderr, _("\ +Excess command line arguments ignored. (%s%s)\n"), + argv[optind], + (optind == argc - 1) ? "" : " ..."); + optind = argc; + break; + } + else + /* The documentation says this can be a "ProcID" as + well. We will try it later as both a corefile and a + pid. */ + pid_or_core_arg = argv[optind]; + } } if (batch) quiet = 1; @@ -733,21 +743,31 @@ extern int gdbtk_test (char *); catch_command_errors (symbol_file_add_main, symarg, 0, RETURN_MASK_ALL); } + if (corearg && pidarg) + error (_("\ +Can't attach to process and specify a core file at the same time.")); + if (corearg != NULL) - { - /* corearg may be either a corefile or a pid. - If its first character is a digit, try attach first - and then corefile. Otherwise try corefile first. */ + catch_command_errors (core_file_command, corearg, + !batch, RETURN_MASK_ALL); + else if (pidarg != NULL) + catch_command_errors (attach_command, pidarg, + !batch, RETURN_MASK_ALL); + else if (pid_or_core_arg) + { + /* The user specified 'gdb program pid' or gdb program core'. + If pid_or_core_arg's first character is a digit, try attach + first and then corefile. Otherwise try just corefile. */ - if (isdigit (corearg[0])) + if (isdigit (pid_or_core_arg[0])) { - if (catch_command_errors (attach_command, corearg, + if (catch_command_errors (attach_command, pid_or_core_arg, !batch, RETURN_MASK_ALL) == 0) - catch_command_errors (core_file_command, corearg, + catch_command_errors (core_file_command, pid_or_core_arg, !batch, RETURN_MASK_ALL); } - else /* Can't be a pid, better be a corefile. */ - catch_command_errors (core_file_command, corearg, + else /* Can't be a pid, better be a corefile. */ + catch_command_errors (core_file_command, pid_or_core_arg, !batch, RETURN_MASK_ALL); }