[binutils-gdb] gdb/testsuite: improve corefile-no-threads.py script

Andrew Burgess via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f681a0b4290dbcd6818765cd2c615dfb9db9d7ef

commit f681a0b4290dbcd6818765cd2c615dfb9db9d7ef
Author: Andrew Burgess <[email protected]>
Date:   Wed Mar 11 11:56:10 2026 +0000

    gdb/testsuite: improve corefile-no-threads.py script
    
    Extend the Python script gdb.base/corefile-no-threads.py so that core
    file note types can be specified by name, e.g. NT_PRSTATUS, rather
    than having to use their hex value.
    
    I've only added a few names for now.  The existing test only needs
    NT_PRSTATUS, but I plan to reuse this script for a new test, in which
    case I'll also need NT_FILE.  Additional names can be added in the
    future as needed.
    
    I then updated the gdb.base/corefile-no-threads.exp script to make use
    of this functionality, and I improved the test pattern so that it
    actually checks that a note was updated.
    
    There should be no change in what is tested after this commit.
    
    Reviewed-By: Keith Seitz <[email protected]>

Diff:
---
 gdb/testsuite/gdb.base/corefile-no-threads.exp | 10 ++++--
 gdb/testsuite/gdb.base/corefile-no-threads.py  | 47 +++++++++++++++++++-------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/gdb/testsuite/gdb.base/corefile-no-threads.exp b/gdb/testsuite/gdb.base/corefile-no-threads.exp
index dfb2f806e9e..d21d7cf4030 100644
--- a/gdb/testsuite/gdb.base/corefile-no-threads.exp
+++ b/gdb/testsuite/gdb.base/corefile-no-threads.exp
@@ -15,8 +15,8 @@
 
 # Check how GDB handles a core file which appears to have no threads
 # within it.  We do this by asking the kernel to create a "normal"
-# core file, then use a Python script modify the core file, hiding the
-# NT_PRSTATUS notes.  The NT_PRSTATUS notes contain the general
+# core file, then use a Python script to modify the core file, hiding
+# the NT_PRSTATUS notes.  The NT_PRSTATUS notes contain the general
 # purpose registers for each thread, so with these gone GDB will
 # believe the core file has no threads.
 #
@@ -52,7 +52,11 @@ clean_restart
 # Load the Python script, and run the command which modifies the core
 # file.
 gdb_test_no_output "source $::pyfile" "import python scripts"
-gdb_test "modify-core-file \"$corefile\" 0x1" ".*" \
+gdb_test "modify-core-file \"$corefile\" NT_PRSTATUS" \
+    [multi_line \
+	 "Located PT_NOTE segment: \[^\r\n\]+" \
+	 "\\s+Found note with type $hex at file offset $hex\\..*" \
+	 "Successfully updated $decimal note\\(s\\) in \[^\r\n\]+"] \
     "update core file"
 
 set saw_no_regs_warning false
diff --git a/gdb/testsuite/gdb.base/corefile-no-threads.py b/gdb/testsuite/gdb.base/corefile-no-threads.py
index 5ba82ae9486..fb2a91e57d0 100644
--- a/gdb/testsuite/gdb.base/corefile-no-threads.py
+++ b/gdb/testsuite/gdb.base/corefile-no-threads.py
@@ -18,10 +18,10 @@
 #
 # modify-core-file <corefile> <note type> [ <name regex> ]
 #
-# Find all notes in the core file <corefile> that have the type value
-# <note type>.  Change the type of those notes to 0xffffffff, which
-# should prevent GDB from processing the note.  The <corefile> is
-# modified in place.
+# Find all notes in the core file <corefile> that have the type
+# <note type>, either a name (e.g. NT_PRSTATUS) or an integer.
+# Change the type of those notes to 0xffffffff, which should prevent
+# GDB from processing the note.  The <corefile> is modified in place.
 #
 # The optional argument <name regex> is a regular expression to match
 # against the name of the note in addition to the type value check.
@@ -130,7 +130,7 @@ def invalidate_corefile_notes(core_filepath, type_val, name_re):
     ei_data = read_field(core_data, 5, "=b")
     ei_version = read_field(core_data, 6, "=b")
 
-    # Based on the endinanness of the core file, select a character to
+    # Based on the endianness of the core file, select a character to
     # use with the 'struct' module for unpacking multi-byte fields.
     if ei_data == ELF_DATA_2_LSB:
         endian_char = "<"
@@ -194,7 +194,7 @@ def invalidate_corefile_notes(core_filepath, type_val, name_re):
     # Read the e_type field and check this is a core file.
     e_type = read_field(core_data, ELF_HDR_TYPE_OFFSET, ELF_HALF_FMT)
     if e_type != ELF_TYPE_CORE:
-        raise gdb.GdbError("Unsuported ELF e_type %d" % (e_type))
+        raise gdb.GdbError("Unsupported ELF e_type %d" % (e_type))
 
     # Read the offset to the program header table, and the number of
     # entries in the program header table.
@@ -308,6 +308,31 @@ def invalidate_corefile_notes(core_filepath, type_val, name_re):
             )
 
 
+# A limited set of note names.  Extend this as needed.
+NOTE_TYPES = {
+    "NT_PRSTATUS": 1,
+    "NT_FPREGSET": 2,
+    "NT_PRPSINFO": 3,
+    "NT_TASKSTRUCT": 4,
+    "NT_AUXV": 6,
+    "NT_SIGINFO": 0x53494749,
+    "NT_FILE": 0x46494C45,
+    "NT_PRXFPREG": 0x46E62B7F,
+}
+
+
+def parse_type(type_str):
+    """Resolves string name to integer or parses raw hex/int."""
+    if type_str in NOTE_TYPES:
+        return NOTE_TYPES[type_str]
+    try:
+        return int(type_str, 0)
+    except ValueError as e:
+        raise gdb.GdbError(
+            "Error: Unable to parse '%s' as number: %s" % (type_str, str(e))
+        )
+
+
 class modify_core_file(gdb.Command):
     """Update notes within a core file.
 
@@ -315,7 +340,8 @@ class modify_core_file(gdb.Command):
     modify-core-file COREFILE NOTE_TYPE [ NAME_REGEX ]
 
     Within COREFILE, find any notes matching NOTE_TYPE, which should
-    be an integer.  Change the type of these notes to 0xffffffff.
+    be either a note type name (e.g. NT_PRSTATUS) or an integer.
+    Change the type of these notes to 0xffffffff.
 
     If NAME_REGEX is supplied, and is not the empty string, then only
     notes whose type matches NOTE_TYPE, and whose name matches
@@ -352,12 +378,7 @@ class modify_core_file(gdb.Command):
                 "Error: Cannot write to '%s'. Check file permissions." % (filename)
             )
 
-        try:
-            type_val = int(type_str, 0)
-        except ValueError as e:
-            raise gdb.GdbError(
-                "Error: Unable to parse '%s' as number: %s" % (type_str, str(e))
-            )
+        type_val = parse_type(type_str)
 
         if name_re_str != "":
             name_re = re.compile(name_re_str)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.