public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Subject: [PATCHv3 2/2] gdb: handle relative paths to DWO files
Date: Fri, 26 Mar 2021 17:54:04 +0000	[thread overview]
Message-ID: <97f64bcc51f4e752311b6100d179dd72bd9a2ee2.1616781092.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1616781092.git.andrew.burgess@embecosm.com>

DWARF allows .dwo file paths to be relative rather than absolute.

When they are relative, DWARF uses DW_AT_comp_dir to find the .dwo
file.  DW_AT_comp_dir can also be relative, making the entire search
patch for the .dwo file relative.

In this case, GDB currently searches relative to its current working
directory, i.e. the directory from which the debugger was launched,
but not relative to the directory containing the built binary.  This
cannot be right, as the compiler, when generating the relative paths,
knows where it's building the binary but can have no idea where the
debugger will be launched.

The correct thing is to add the directory containing the binary to the
search paths used for resolving relative locations of dwo files. That
is what this patch does.

gdb/ChangeLog:

	* dwarf2/read.c (try_open_dwop_file): Add path for the binary to
	the search paths used resolve relative location of .dwo file.

gdb/testsuite/ChangeLog:

	* gdb.dwarf2/fission-relative-dwo.c: New file.
	* gdb.dwarf2/fission-relative-dwo.exp: New file.
---
 gdb/ChangeLog                                 |   5 +
 gdb/dwarf2/read.c                             |   7 +
 gdb/testsuite/ChangeLog                       |   5 +
 .../gdb.dwarf2/fission-relative-dwo.c         |  28 ++++
 .../gdb.dwarf2/fission-relative-dwo.exp       | 130 ++++++++++++++++++
 5 files changed, 175 insertions(+)
 create mode 100644 gdb/testsuite/gdb.dwarf2/fission-relative-dwo.c
 create mode 100644 gdb/testsuite/gdb.dwarf2/fission-relative-dwo.exp

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index ee6f3f7f7e1..2b208f69970 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -12721,6 +12721,13 @@ try_open_dwop_file (dwarf2_per_objfile *per_objfile,
   else
     search_path = debug_file_directory;
 
+  /* Add the path for the executable binary to the list of search paths.  */
+  std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
+  search_path_holder.reset (concat (objfile_dir.c_str (),
+				    dirname_separator_string,
+				    search_path, nullptr));
+  search_path = search_path_holder.get ();
+
   openp_flags flags = OPF_RETURN_REALPATH;
   if (is_dwp)
     flags |= OPF_SEARCH_IN_PATH;
diff --git a/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.c b/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.c
new file mode 100644
index 00000000000..27f7f0dfb4b
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.c
@@ -0,0 +1,28 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2021 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/>.  */
+
+/* Our fake object.  */
+int global_var[100];
+
+int
+main (int argc, char **argv)
+{
+  asm ("main_label: .globl main_label");
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.exp b/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.exp
new file mode 100644
index 00000000000..66afa3f6a96
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/fission-relative-dwo.exp
@@ -0,0 +1,130 @@
+# Copyright 2021 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/>.
+
+# This test checks that GDB can find the dwo file when the path to the
+# dwo embedded in the executable is relative.
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if {![dwarf2_support]} {
+    return 0
+}
+
+standard_testfile .c -dw.S
+
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
+    return -1
+}
+
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    global srcfile gdb_test_file_name
+
+    set debug_addr_base -1
+
+    # The information that will be split out into the .dwo file.
+    cu {fission 1} {
+
+	# Capture the current index into .debug_addr so we can fill in
+	# DW_AT_GNU_addr_base later.
+	set debug_addr_base [debug_addr_index]
+
+	compile_unit {
+            {language @DW_LANG_C}
+            {name ${srcfile}}
+	    {DW_AT_comp_dir .}
+	    {DW_AT_GNU_dwo_id 0x1234 DW_FORM_data8}
+        } {
+	    declare_labels int4_type struct_type
+
+	    int4_type: DW_TAG_base_type {
+		{DW_AT_byte_size 4 DW_FORM_sdata}
+		{DW_AT_encoding  @DW_ATE_signed}
+		{DW_AT_name      integer}
+	    }
+
+	    struct_type: DW_TAG_structure_type {
+		{DW_AT_name "foo_t"}
+		{DW_AT_byte_size 12 DW_FORM_sdata}
+	    } {
+		member {
+		    {name "aa"}
+		    {type :$int4_type}
+		    {data_member_location 0 data1}
+		}
+		member {
+		    {name "bb"}
+		    {type :$int4_type}
+		    {data_member_location 4 data1}
+		}
+		member {
+		    {name "cc"}
+		    {type :$int4_type}
+		    {data_member_location 8 data1}
+		}
+	    }
+
+	    DW_TAG_variable {
+		{DW_AT_name global_var}
+		{DW_AT_type :$struct_type}
+		{DW_AT_location {
+		    DW_OP_GNU_addr_index [gdb_target_symbol global_var]
+		} SPECIAL_expr}
+		{external 1 flag}
+	    }
+
+	    subprogram {
+		{external 1 flag}
+		{DW_AT_name main DW_FORM_string}
+		{MACRO_AT_func {main}}
+	    }
+	}
+    }
+
+    # The information that will remain in the .o file.
+    cu {} {
+	compile_unit {
+            {DW_AT_GNU_dwo_name ${gdb_test_file_name}.dwo DW_FORM_strp}
+	    {DW_AT_comp_dir .}
+	    {DW_AT_GNU_dwo_id 0x1234 DW_FORM_data8}
+	    {DW_AT_GNU_addr_base $debug_addr_base}
+        } {
+	    # Nothing.
+	}
+    }
+}
+
+# Compile both source files to create the executable.  As we compile
+# ASM_FILE we split out the debug information into the dwo file.
+set object_file [standard_output_file ${testfile}.o]
+if { [build_executable_and_dwo_files "${testfile}.exp" ${testfile} {nodebug} \
+	  [list $asm_file {nodebug split-dwo} ${object_file}] \
+	  [list $srcfile {nodebug}]] } {
+    perror "failed to compile ${gdb_test_file_name}"
+    return -1
+}
+
+# Now we can start GDB.
+clean_restart ${testfile}
+
+if ![runto_main] {
+    return -1
+}
+
+# Print the type of global_var.  This type information is entirely
+# fictional, it only exists in the DWARF.  If we don't have the DWARF
+# information then there's no way we can print this.
+gdb_test "p global_var" " = \\{aa = 0, bb = 0, cc = 0\\}"
-- 
2.25.4


  parent reply	other threads:[~2021-03-26 17:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-03  0:42 [PATCH] Add directory containing executable to relative search paths for .dwo files Caroline Tice
2021-03-03  9:53 ` Andrew Burgess
2021-03-04 15:48   ` [PATCH, v2] " Caroline Tice
2021-03-11 15:33     ` Caroline Tice
2021-03-18  0:26     ` Andrew Burgess
     [not found]       ` <CABtf2+RmUPwpmsx+1i+a-QynhZKEpAib=EcFR4jDbr6PgH9t3g@mail.gmail.com>
2021-03-21  2:33         ` Caroline Tice
2021-03-25 15:09       ` Caroline Tice
2021-03-25 17:46         ` Andrew Burgess
2021-03-26 17:54           ` [PATCHv3 0/2] " Andrew Burgess
2021-03-26 17:54             ` [PATCHv3 1/2] gdb/testsuite: fix fission support in the Dwarf assembler Andrew Burgess
2021-03-29 16:30               ` Simon Marchi
2021-03-30 11:07                 ` Andrew Burgess
2021-04-01 16:41                   ` Tom Tromey
2021-03-26 17:54             ` Andrew Burgess [this message]
2021-04-01 16:42               ` [PATCHv3 2/2] gdb: handle relative paths to DWO files Tom Tromey
2021-03-31 15:57             ` [PATCHv3 0/2] Add directory containing executable to relative search paths for .dwo files Caroline Tice
2021-03-31 20:04               ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=97f64bcc51f4e752311b6100d179dd72bd9a2ee2.1616781092.git.andrew.burgess@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).