[RFC 3/3] [gdb-add-index.sh] Use save gdb-index -verbose

Tom de Vries tdevries@suse.de
Fri Oct 15 13:32:24 GMT 2021


Rewrite gdb-add-index.sh to use gdb-index -verbose.

This works reasonably well, but the only problem is that when the gdb
invocation fails, it may leave behind temporary files, which are not cleaned
up by gdb-add-index.sh.

That could in theory be fixed by calling gdb twice, the first time like this:
...
$ gdb -q -batch -readnever b.out -ex "maint print objfiles"

Object file /home/vries/a.out:  Objfile at 0x329ccd0, bfd at 0x324be00, 33 minsyms
...
to find out which files are going to be generated.

Or similarly, adding a dryrun option to save gdb-index.

But both options are a bit costly.

We could also go for creating a tmp dir using mktemp, and removing the tempdir
as a whole instead, rather than trying to track the individual temp files.
---
 gdb/contrib/gdb-add-index.sh | 65 +++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 34 deletions(-)

diff --git a/gdb/contrib/gdb-add-index.sh b/gdb/contrib/gdb-add-index.sh
index 2ac3fddbf26..0d29ae37b9c 100755
--- a/gdb/contrib/gdb-add-index.sh
+++ b/gdb/contrib/gdb-add-index.sh
@@ -20,7 +20,6 @@
 # If not, or you want others, pass the following in the environment
 GDB=${GDB:=gdb}
 OBJCOPY=${OBJCOPY:=objcopy}
-READELF=${READELF:=readelf}
 
 myname="${0##*/}"
 
@@ -45,19 +44,6 @@ fi
 dir="${file%/*}"
 test "$dir" = "$file" && dir="."
 
-dwz_file=""
-if $READELF -S "$file" | grep -q " \.gnu_debugaltlink "; then
-    dwz_file=$($READELF --string-dump=.gnu_debugaltlink "$file" \
-		   | grep -A1  "'\.gnu_debugaltlink':" \
-		   | tail -n +2 \
-		   | sed 's/.*]//')
-    dwz_file=$(echo $dwz_file)
-    if $READELF -S "$dwz_file" | grep -E -q " \.(gdb_index|debug_names) "; then
-	# Already has an index, skip it.
-	dwz_file=""
-    fi
-fi
-
 set_files ()
 {
     fpath="$1"
@@ -67,30 +53,26 @@ set_files ()
     debugstr="${fpath}.debug_str"
     debugstrmerge="${fpath}.debug_str.merge"
     debugstrerr="${fpath}.debug_str.err"
+    trap "rm -f $index_files $debugstrmerge $debugstrerr" 0
 }
 
-tmp_files=
-for f in "$file" "$dwz_file"; do
-    if [ "$f" = "" ]; then
-	continue
-    fi
-    set_files "$f"
-    tmp_files="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
-done
-
-rm -f $tmp_files
-
-# Ensure intermediate index file is removed when we exit.
-trap "rm -f $tmp_files" 0
-
-$GDB --batch -nx -iex 'set auto-load no' \
-    -ex "file $file" -ex "save gdb-index $dwarf5 $dir" || {
+index_files=$($GDB --batch -nx -iex 'set auto-load no' \
+		   -ex "file $file" -ex "save gdb-index -verbose $dwarf5 $dir") || {
     # Just in case.
     status=$?
     echo "$myname: gdb error generating index for $file" 1>&2
     exit $status
 }
 
+if [ "$index_files" = "" ]; then
+    exit 1
+fi
+
+# Ensure intermediate index file is removed when we exit.
+
+tmp_files=$index_files
+trap 'rm -f $tmp_files' 0
+
 # In some situations gdb can exit without creating an index.  This is
 # not an error.
 # E.g., if $file is stripped.  This behaviour is akin to stripping an
@@ -151,9 +133,24 @@ handle_file ()
     fi
 }
 
-handle_file "$file"
-if [ "$dwz_file" != "" ]; then
-    handle_file "$dwz_file"
-fi
+for f in $index_files; do
+    case $f in
+	*.gdb-index)
+	    f="${f%.gdb-index}"
+	    handle_file "$f"
+	    ;;
+	*.debug_names)
+	    f="${f%.debug_names}"
+	    handle_file "$f"
+	    ;;
+	*.debug_str)
+	    continue
+	    ;;
+	*)
+	    echo "Don't know how to handle generated file: $f"
+	    exit 1
+	    ;;
+    esac
+done
 
 exit $status
-- 
2.26.2



More information about the Gdb-patches mailing list