gdb: remove print_sys_errmsg

This started with me running into this comment in symfile.c:

  /* FIXME, should use print_sys_errmsg but it's not filtered.  */
  gdb_printf (_("`%ps' has disappeared; keeping its symbols.\n"),
              styled_string (file_name_style.style (), filename));

In this particular case I think I disagree with the comment; I think
the output should be a warning rather than just a message printed to
gdb_stdout, I think when the executable, or some other objfile that is
currently being debugged, disappears from disk, this is likely an
unexpected situation, and worth warning the user about.

So, in theory, I could just call print_sys_errmsg and remove the
comment, but that would mean loosing the filename styling in the
output... so in the end I remove the comment and updated the code to
call warning.

But that got me looking at print_sys_errmsg and how it's used.

Currently the function takes a string and an errno, and prints, to
stderr, the string followed by the result of calling strerror on the
errno.

In some places the string passed to print_sys_errmsg is just a
filename, and this is used when something goes wrong.  In these cases,
I think calling warning rather than gdb_printf to gdb_stderr, would be
better, and in fact, in a couple of places we manually print a
"warning" prefix, and then call print_sys_errmsg.  And so, for these
users I have added a new function warning_filename_and_errno, which
takes a filename, which is printed with styling, and an errno, which
is passed through strerror and the resulting string printed.  This new
function calls warning to print its output.  I then updated some of
the print_sys_errmsg users to use this new function.

Some other users of print_sys_errmsg are also emitting what is clearly
a warning, however, the string being passed in is more than just a
filename, so the new warning_filename_and_errno function can't be
used, it would style the whole string.  For these users I have
switched to calling warning directly, this allows me to style the
warning message correctly.

Finally, in inflow.c there is one last call to print_sys_errmsg, in
this case I just inlined the definition of print_sys_errmsg.  This is
a really weird case, as after printing this message GDB just does a
hard exit.  This is pretty old code, dating back to the initial GDB
import, I guess it should be updated to call error() maybe, but I'm
reluctant to make this change as part of this commit, just in case
there's some reason why we can't throw an error at this point.

With that done there are now no users of print_sys_errmsg, and so the
old function can be removed.

While I was doing all of the above I added some additional filename
styling in soure.c, this is in an else block where the if contained
the print_sys_errmsg call, so these felt related.

And finally, while I was updating the uses of print_sys_errmsg in
procfs.c, I noticed that we used a static errmsg buffer to format some
error strings.  As the above changes got rid of one of the users of
errmsg I also removed the other two users, and the static buffer.

There were a couple of tests that depended on the existing output
message format that needed updating.  In one case we gained an extra
'warning: ' prefix, and in the other 'Warning: ' becomes 'warning: ',
I think in both cases the new output is an improvement.

Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
Andrew Burgess 2023-09-24 12:37:40 +01:00
parent b09dba5a5f
commit 3d38b301bb
10 changed files with 34 additions and 38 deletions

View File

@ -766,7 +766,8 @@ check_syscall (const char *msg, int result)
{ {
if (result < 0) if (result < 0)
{ {
print_sys_errmsg (msg, errno); gdb_printf (gdb_stderr, "%s:%s.\n", msg,
safe_strerror (errno));
_exit (1); _exit (1);
} }
} }

View File

@ -115,12 +115,7 @@ set_gdb_data_directory (const char *new_datadir)
struct stat st; struct stat st;
if (stat (new_datadir, &st) < 0) if (stat (new_datadir, &st) < 0)
{ warning_filename_and_errno (new_datadir, errno);
int save_errno = errno;
gdb_printf (gdb_stderr, "Warning: ");
print_sys_errmsg (new_datadir, save_errno);
}
else if (!S_ISDIR (st.st_mode)) else if (!S_ISDIR (st.st_mode))
warning (_("%ps is not a directory."), warning (_("%ps is not a directory."),
styled_string (file_name_style.style (), new_datadir)); styled_string (file_name_style.style (), new_datadir));

View File

@ -46,6 +46,7 @@
#include "gdbsupport/scoped_fd.h" #include "gdbsupport/scoped_fd.h"
#include "gdbsupport/pathstuff.h" #include "gdbsupport/pathstuff.h"
#include "gdbsupport/buildargv.h" #include "gdbsupport/buildargv.h"
#include "cli/cli-style.h"
/* This module provides the interface between GDB and the /* This module provides the interface between GDB and the
/proc file system, which is used on many versions of Unix /proc file system, which is used on many versions of Unix
@ -558,7 +559,7 @@ enum { NOKILL, KILL };
static void static void
dead_procinfo (procinfo *pi, const char *msg, int kill_p) dead_procinfo (procinfo *pi, const char *msg, int kill_p)
{ {
print_sys_errmsg (pi->pathname, errno); warning_filename_and_errno (pi->pathname, errno);
if (kill_p == KILL) if (kill_p == KILL)
kill (pi->pid, SIGKILL); kill (pi->pid, SIGKILL);
@ -594,18 +595,20 @@ static void
proc_warn (procinfo *pi, const char *func, int line) proc_warn (procinfo *pi, const char *func, int line)
{ {
int saved_errno = errno; int saved_errno = errno;
std::string errmsg warning ("procfs: %s line %d, %ps: %s",
= string_printf ("procfs: %s line %d, %s", func, line, pi->pathname); func, line, styled_string (file_name_style.style (),
print_sys_errmsg (errmsg.c_str (), saved_errno); pi->pathname),
safe_strerror (saved_errno));
} }
static void static void
proc_error (procinfo *pi, const char *func, int line) proc_error (procinfo *pi, const char *func, int line)
{ {
int saved_errno = errno; int saved_errno = errno;
std::string errmsg error ("procfs: %s line %d, %ps: %s",
= string_printf ("procfs: %s line %d, %s", func, line, pi->pathname); func, line, styled_string (file_name_style.style (),
perror_with_name (errmsg.c_str (), saved_errno); pi->pathname),
safe_strerror (saved_errno));
} }
/* Updates the status struct in the procinfo. There is a 'valid' /* Updates the status struct in the procinfo. There is a 'valid'

View File

@ -587,12 +587,7 @@ add_path (const char *dirname, char **which_path, int parse_separators)
a directory/etc, then having them in the path should be a directory/etc, then having them in the path should be
harmless. */ harmless. */
if (stat (name, &st) < 0) if (stat (name, &st) < 0)
{ warning_filename_and_errno (name, errno);
int save_errno = errno;
gdb_printf (gdb_stderr, "Warning: ");
print_sys_errmsg (name, save_errno);
}
else if ((st.st_mode & S_IFMT) != S_IFDIR) else if ((st.st_mode & S_IFMT) != S_IFDIR)
warning (_("%ps is not a directory."), warning (_("%ps is not a directory."),
styled_string (file_name_style.style (), name)); styled_string (file_name_style.style (), name));
@ -1341,11 +1336,9 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
if (!(flags & PRINT_SOURCE_LINES_NOERROR)) if (!(flags & PRINT_SOURCE_LINES_NOERROR))
{ {
const char *filename = symtab_to_filename_for_display (s); const char *filename = symtab_to_filename_for_display (s);
int len = strlen (filename) + 100; warning (_("%d\t%ps: %s"), line,
char *name = (char *) alloca (len); styled_string (file_name_style.style (), filename),
safe_strerror (errcode));
xsnprintf (name, len, "%d\t%s", line, filename);
print_sys_errmsg (name, errcode);
} }
else else
{ {

View File

@ -2489,9 +2489,8 @@ reread_symbols (int from_tty)
int res = stat (filename, &new_statbuf); int res = stat (filename, &new_statbuf);
if (res != 0) if (res != 0)
{ {
/* FIXME, should use print_sys_errmsg but it's not filtered. */ warning (_("`%ps' has disappeared; keeping its symbols."),
gdb_printf (_("`%ps' has disappeared; keeping its symbols.\n"), styled_string (file_name_style.style (), filename));
styled_string (file_name_style.style (), filename));
continue; continue;
} }
time_t new_modtime = new_statbuf.st_mtime; time_t new_modtime = new_statbuf.st_mtime;

View File

@ -392,7 +392,7 @@ proc test_catch_syscall_fail_nodatadir {} {
# Make sure GDB doesn't load the syscalls xml from the system # Make sure GDB doesn't load the syscalls xml from the system
# data directory. # data directory.
gdb_test "set data-directory /the/path/to/nowhere" \ gdb_test "set data-directory /the/path/to/nowhere" \
"Warning: /the/path/to/nowhere: .*" "warning: /the/path/to/nowhere: .*"
# Testing to see if we receive a warning when calling "catch # Testing to see if we receive a warning when calling "catch
# syscall" without XML support (without datadir). # syscall" without XML support (without datadir).
@ -658,7 +658,7 @@ proc do_syscall_tests_without_xml {} {
# Make sure GDB doesn't load the syscalls xml from the system data # Make sure GDB doesn't load the syscalls xml from the system data
# directory. # directory.
gdb_test "set data-directory /the/path/to/nowhere" \ gdb_test "set data-directory /the/path/to/nowhere" \
"Warning: /the/path/to/nowhere: .*" "warning: /the/path/to/nowhere: .*"
# Let's test if we can catch syscalls without XML support. # Let's test if we can catch syscalls without XML support.
# We should succeed, but GDB is not supposed to print syscall names. # We should succeed, but GDB is not supposed to print syscall names.

View File

@ -167,7 +167,7 @@ if { $psymtabs_p } {
} }
gdb_test "l imported_unit.c:1" \ gdb_test "l imported_unit.c:1" \
"1\timported_unit.c: No such file or directory\." "warning: 1\timported_unit.c: No such file or directory"
gdb_test "info source" "\r\nCurrent source file is imported_unit.c\r\n.*" \ gdb_test "info source" "\r\nCurrent source file is imported_unit.c\r\n.*" \
"info source for imported_unit.c" "info source for imported_unit.c"

View File

@ -619,14 +619,13 @@ perror_warning_with_name (const char *string)
warning (_("%s"), combined.c_str ()); warning (_("%s"), combined.c_str ());
} }
/* Print the system error message for ERRCODE, and also mention STRING /* See utils.h. */
as the file name for which the error was encountered. */
void void
print_sys_errmsg (const char *string, int errcode) warning_filename_and_errno (const char *filename, int saved_errno)
{ {
const char *err = safe_strerror (errcode); warning (_("%ps: %s"), styled_string (file_name_style.style (), filename),
gdb_printf (gdb_stderr, "%s: %s.\n", string, err); safe_strerror (saved_errno));
} }
/* Control C eventually causes this to be called, at a convenient time. */ /* Control C eventually causes this to be called, at a convenient time. */

View File

@ -275,7 +275,13 @@ extern void fprintf_symbol (struct ui_file *, const char *,
extern void perror_warning_with_name (const char *string); extern void perror_warning_with_name (const char *string);
extern void print_sys_errmsg (const char *, int); /* Issue a warning formatted as '<filename>: <explanation>', where
<filename> is FILENAME with filename styling applied. As such, don't
pass anything more than a filename in this string. The <explanation>
is a string returned from calling safe_strerror(SAVED_ERRNO). */
extern void warning_filename_and_errno (const char *filename,
int saved_errno);
/* Warnings and error messages. */ /* Warnings and error messages. */

View File

@ -2625,7 +2625,7 @@ windows_nat_target::create_inferior (const char *exec_file,
tty = open (inferior_tty.c_str (), O_RDWR | O_NOCTTY); tty = open (inferior_tty.c_str (), O_RDWR | O_NOCTTY);
if (tty < 0) if (tty < 0)
{ {
print_sys_errmsg (inferior_tty.c_str (), errno); warning_filename_and_errno (inferior_tty.c_str (), errno);
ostdin = ostdout = ostderr = -1; ostdin = ostdout = ostderr = -1;
} }
else else