warn if "source" fails to open the file when from_tty == 0

Consider the following example:

    % gdb -q -batch -ex 'source nonexistant-file'
    [nothing]

One would have at least expected the debugger to warn about
not finding the file, similar to the error shown when using
a more interactive mode. Eg:

    (gdb) source nonexistant-file
    nonexistant-file: No such file or directory.

Not raising an error appears to be intentional, presumably in order
to prevent this situation from stoping the execution of a GDB script.
But the lack of at least a warning makes it harder for a user to
diagnose any issue, if the file was expected to be there and readable.

This patch adds a warning in that case:

    % gdb -q -batch -ex 'source nonexistant-file'
    warning: nonexistant-file: No such file or directory.

gdb/ChangeLog:

        * utils.h (perror_warning_with_name): Add declaration.
        * utils.c (perror_warning_with_name): New function.
        * cli/cli-cmds.c (source_script_with_search): Add call to
        perror_warning_with_name if from_tty is nul.

gdb/testsuite/ChangeLog:

        * gdb.base/source-nofile.gdb: New file.
        * gdb.base/source.exp: Add two tests verifying the behavior when
        the "source" command is given a non-existant filename.
This commit is contained in:
Joel Brobecker 2013-10-11 08:23:11 +00:00
parent 0cf4063e29
commit 7c647d6155
7 changed files with 64 additions and 2 deletions

View File

@ -1,3 +1,10 @@
2013-11-11 Joel Brobecker <brobecker@adacore.com>
* utils.h (perror_warning_with_name): Add declaration.
* utils.c (perror_warning_with_name): New function.
* cli/cli-cmds.c (source_script_with_search): Add call to
perror_warning_with_name if from_tty is nul.
2013-11-11 Joel Brobecker <brobecker@adacore.com> 2013-11-11 Joel Brobecker <brobecker@adacore.com>
* utils.c (perror_string): New function, extracted out of * utils.c (perror_string): New function, extracted out of

View File

@ -571,11 +571,14 @@ source_script_with_search (const char *file, int from_tty, int search_path)
/* The script wasn't found, or was otherwise inaccessible. /* The script wasn't found, or was otherwise inaccessible.
If the source command was invoked interactively, throw an If the source command was invoked interactively, throw an
error. Otherwise (e.g. if it was invoked by a script), error. Otherwise (e.g. if it was invoked by a script),
silently ignore the error. */ just emit a warning, rather than cause an error. */
if (from_tty) if (from_tty)
perror_with_name (file); perror_with_name (file);
else else
return; {
perror_warning_with_name (file);
return;
}
} }
old_cleanups = make_cleanup (xfree, full_path); old_cleanups = make_cleanup (xfree, full_path);

View File

@ -1,3 +1,9 @@
2013-10-11 Joel Brobecker <brobecker@adacore.com>
* gdb.base/source-nofile.gdb: New file.
* gdb.base/source.exp: Add two tests verifying the behavior when
the "source" command is given a non-existant filename.
2013-10-11 Yao Qi <yao@codesourcery.com> 2013-10-11 Yao Qi <yao@codesourcery.com>
* gdb.mi/mi-catch-load.c: Remove the include of "dlfcn.h". * gdb.mi/mi-catch-load.c: Remove the include of "dlfcn.h".

View File

@ -0,0 +1,22 @@
# This testcase is part of GDB, the GNU debugger.
#
# Copyright 2013 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
source for-sure-nonexistant-file
# Just print an message to confirm that failing to source the file
# did not cause this script to abort.
echo source error not fatal\n

View File

@ -57,4 +57,13 @@ gdb_test "source -v -s ./source-test.gdb" \
"echo test source options.*" \ "echo test source options.*" \
"source -v -s" "source -v -s"
# Test sourcing a non-existant file, both when the source command
# comes from the a command entered at the GDB prompt, and when
# it comes from a script being sourced.
gdb_test "source for-sure-nonexistant-file" \
"for-sure-nonexistant-file: No such file or directory\."
gdb_test "source source-nofile.gdb" \
"warning: for-sure-nonexistant-file: No such file or directory\.\[\r\n\]*source error not fatal"
gdb_exit gdb_exit

View File

@ -1006,6 +1006,19 @@ perror_with_name (const char *string)
throw_perror_with_name (GENERIC_ERROR, string); throw_perror_with_name (GENERIC_ERROR, string);
} }
/* Same as perror_with_name except that it prints a warning instead
of throwing an error. */
void
perror_warning_with_name (const char *string)
{
char *combined;
combined = perror_string (string);
warning (_("%s"), combined);
xfree (combined);
}
/* Print the system error message for ERRCODE, and also mention STRING /* Print the system error message for ERRCODE, and also mention STRING
as the file name for which the error was encountered. */ as the file name for which the error was encountered. */

View File

@ -285,6 +285,8 @@ extern void throw_perror_with_name (enum errors errcode, const char *string)
ATTRIBUTE_NORETURN; ATTRIBUTE_NORETURN;
extern void perror_with_name (const char *) ATTRIBUTE_NORETURN; extern void perror_with_name (const char *) ATTRIBUTE_NORETURN;
extern void perror_warning_with_name (const char *string);
extern void print_sys_errmsg (const char *, int); extern void print_sys_errmsg (const char *, int);
/* Warnings and error messages. */ /* Warnings and error messages. */