From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2009) id 20E353858D32; Sat, 11 Feb 2023 02:11:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20E353858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676081520; bh=nS080k/8wV24hfCKZkJnBKNrogyIp3gcvYDUlEDgAaQ=; h=From:To:Subject:Date:From; b=HtgiS1kg2DXvgsswR1W8V2j5d/4bDE+H6dXBsum9J+75RVhbDIig0sFTlH6PkZnkP 6vH8nQP3XLrSTOUVT4+AHpumju9EuxM01GhnkKtr9VUVWXWdjmC9d9B2E8bQCvhDXP HGCHkj4wbwxGP4B/NM++ryt3rFainoXpYsRZa0tU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Aaron Merey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/source: Fix open_source_file error handling X-Act-Checkin: binutils-gdb X-Git-Author: Aaron Merey X-Git-Refname: refs/heads/master X-Git-Oldrev: 40dfb28b56fe55a370a35495e0f1eb6c95110f35 X-Git-Newrev: 8cc96ee4169f631917d048cbaeec14ddf8ccb90d Message-Id: <20230211021200.20E353858D32@sourceware.org> Date: Sat, 11 Feb 2023 02:11:59 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D8cc96ee4169f= 631917d048cbaeec14ddf8ccb90d commit 8cc96ee4169f631917d048cbaeec14ddf8ccb90d Author: Aaron Merey Date: Thu Feb 9 20:35:32 2023 -0500 gdb/source: Fix open_source_file error handling =20 open_source_file relies on errno to communicate the reason for a missing source file. =20 open_source_file may also call debuginfod_find_source. It is possible for debuginfod_find_source to set errno to a value unrelated to the reason for a failed download. =20 This can result in bogus error messages being reported as the reason for a missing source file. The following error message should instead be "No such file or directory": =20 Temporary breakpoint 1, 0x00005555556f4de0 in main () (gdb) list Downloading source file /usr/src/debug/glibc-2.36-8.fc37.x86_64/elf/<= built-in> 1 /usr/src/debug/glibc-2.36-8.fc37.x86_64/elf/: Direc= tory not empty. =20 Fix this by having open_source_file return a negative errno if it fails to open a source file. Use this value to generate the error message instead of errno. =20 Approved-By: Tom Tromey Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29999 Diff: --- gdb/debuginfod-support.h | 15 +++++++++------ gdb/source-cache.c | 2 +- gdb/source.c | 40 ++++++++++++++++++++++++++++++---------- gdb/source.h | 5 +++-- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/gdb/debuginfod-support.h b/gdb/debuginfod-support.h index 542d1688fa1..633600a79da 100644 --- a/gdb/debuginfod-support.h +++ b/gdb/debuginfod-support.h @@ -33,8 +33,9 @@ source file path is `/my/source/foo.c`, then SRC_PATH should be `/my/build/../source/foo.c`. =20 - If the file is successfully retrieved, its path on the local machine - is stored in DESTNAME. If GDB is not built with debuginfod, this + If the file is successfully retrieved, return a file descriptor and sto= re + the file's local path in DESTNAME. If unsuccessful, print an error mes= sage + and return a negative errno. If GDB is not built with debuginfod, this function returns -ENOSYS. */ =20 extern scoped_fd @@ -51,8 +52,9 @@ debuginfod_source_query (const unsigned char *build_id, FILENAME should be the name or path of the main binary associated with the separate debug info. It is used for printing messages to the user. =20 - If the file is successfully retrieved, its path on the local machine - is stored in DESTNAME. If GDB is not built with debuginfod, this + If the file is successfully retrieved, return a file descriptor and sto= re + the file's local path in DESTNAME. If unsuccessful, print an error mes= sage + and return a negative errno. If GDB is not built with debuginfod, this function returns -ENOSYS. */ =20 extern scoped_fd @@ -69,8 +71,9 @@ debuginfod_debuginfo_query (const unsigned char *build_id, FILENAME should be the name or path associated with the executable. It is used for printing messages to the user. =20 - If the file is successfully retrieved, its path on the local machine - is stored in DESTNAME. If GDB is not built with debuginfod, this + If the file is successfully retrieved, return a file descriptor and sto= re + the file's local path in DESTNAME. If unsuccessful, print an error mes= sage + and return a negative errno. If GDB is not built with debuginfod, this function returns -ENOSYS. */ =20 extern scoped_fd debuginfod_exec_query (const unsigned char *build_id, diff --git a/gdb/source-cache.c b/gdb/source-cache.c index b7410d46293..77b357cb42b 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -95,7 +95,7 @@ source_cache::get_plain_source_lines (struct symtab *s, { scoped_fd desc (open_source_file (s)); if (desc.get () < 0) - perror_with_name (symtab_to_filename_for_display (s)); + perror_with_name (symtab_to_filename_for_display (s), -desc.get ()); =20 struct stat st; if (fstat (desc.get (), &st) < 0) diff --git a/gdb/source.c b/gdb/source.c index be5af180c49..ca0e8d51fbd 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -1069,7 +1069,7 @@ find_and_open_source (const char *filename, the attempt to read this source file failed. GDB will then display the filename and line number instead. */ if (!source_open) - return scoped_fd (-1); + return scoped_fd (-ECANCELED); =20 /* Quick way out if we already know its full name. */ if (*fullname) @@ -1160,11 +1160,15 @@ find_and_open_source (const char *filename, OPEN_MODE, fullname); } =20 + /* If the file wasn't found, then openp will have set errno accordingly.= */ + if (result < 0) + result =3D -errno; + return scoped_fd (result); } =20 /* Open a source file given a symtab S. Returns a file descriptor or - negative number for error. =20 + negative errno for error. =20 This function is a convenience function to find_and_open_source. */ =20 @@ -1172,7 +1176,7 @@ scoped_fd open_source_file (struct symtab *s) { if (!s) - return scoped_fd (-1); + return scoped_fd (-EINVAL); =20 gdb::unique_xmalloc_ptr fullname (s->fullname); s->fullname =3D NULL; @@ -1200,10 +1204,21 @@ open_source_file (struct symtab *s) =20 /* Query debuginfod for the source file. */ if (build_id !=3D nullptr && !srcpath.empty ()) - fd =3D debuginfod_source_query (build_id->data, - build_id->size, - srcpath.c_str (), - &fullname); + { + scoped_fd query_fd + =3D debuginfod_source_query (build_id->data, + build_id->size, + srcpath.c_str (), + &fullname); + + /* Don't return a negative errno from debuginfod_source_query. + It handles the reporting of its own errors. */ + if (query_fd.get () >=3D 0) + { + s->fullname =3D fullname.release (); + return query_fd; + } + } } } =20 @@ -1306,6 +1321,7 @@ print_source_lines_base (struct symtab *s, int line, = int stopline, print_source_lines_flags flags) { bool noprint =3D false; + int errcode =3D ENOENT; int nlines =3D stopline - line; struct ui_out *uiout =3D current_uiout; =20 @@ -1336,7 +1352,10 @@ print_source_lines_base (struct symtab *s, int line,= int stopline, scoped_fd desc =3D open_source_file (s); last_source_error =3D desc.get () < 0; if (last_source_error) - noprint =3D true; + { + noprint =3D true; + errcode =3D -desc.get (); + } } } else @@ -1354,7 +1373,7 @@ print_source_lines_base (struct symtab *s, int line, = int stopline, char *name =3D (char *) alloca (len); =20 xsnprintf (name, len, "%d\t%s", line, filename); - print_sys_errmsg (name, errno); + print_sys_errmsg (name, errcode); } else { @@ -1627,7 +1646,8 @@ search_command_helper (const char *regex, int from_tt= y, bool forward) =20 scoped_fd desc (open_source_file (loc->symtab ())); if (desc.get () < 0) - perror_with_name (symtab_to_filename_for_display (loc->symtab ())); + perror_with_name (symtab_to_filename_for_display (loc->symtab ()), + -desc.get ()); =20 int line =3D (forward ? last_line_listed + 1 diff --git a/gdb/source.h b/gdb/source.h index 61d1bcd883b..dd6f58c579c 100644 --- a/gdb/source.h +++ b/gdb/source.h @@ -67,7 +67,8 @@ extern void init_source_path (void); The caller is responsible for freeing FULLNAME. =20 On Failure - An invalid file descriptor is returned (the return value is negative). + An invalid file descriptor is returned. The value of this file + descriptor is a negative errno indicating the reason for the failure. FULLNAME is set to NULL. */ extern scoped_fd find_and_open_source (const char *filename, const char *dirname, @@ -81,7 +82,7 @@ extern gdb::unique_xmalloc_ptr find_source_or_rewri= te (const char *filename, const char *dirname); =20 /* Open a source file given a symtab S. Returns a file descriptor or - negative number for error. */ + negative errno indicating the reason for the failure. */ extern scoped_fd open_source_file (struct symtab *s); =20 extern gdb::unique_xmalloc_ptr rewrite_source_path (const char *path= );