From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id CB80E384A01A for ; Fri, 12 Mar 2021 12:22:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CB80E384A01A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wr1-x429.google.com with SMTP id 7so4708553wrz.0 for ; Fri, 12 Mar 2021 04:22:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jeGiiL5fZYLjbHd8a5pzqj/o4Axf7IIgwlvxPrJ6e3g=; b=B44adc7SSa7kbbSOSg7/7VsW4yxHkVZgvxqus4s1Y822yf8m+n5DD27c42rebAqLsC K/LbH64yD2vjQE1VVKmnXhtSBsTmSSlfb9k0p19xt0iF7Xr1LRkz5Aue6ZIh0N7Ab2zN hMBJBDpRQuufx9+WZxb5YXrmqNAOlkSgxYIIe2npxiGrQBPSVWZeY5ScqgyDj5RP1c9z 39e/x/oJaHvewZ3VQ1fs4MZMQ+qdj26FlKhuopC6eKuvnR5/1HGslHS+hXFwQudSvBFE lRET6/LZD4G9O2k4KnNptZs2Xz7K084eZPXpYjxCVlGzytbuk4L5e87XsJDJwSSqCcwV 6EWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=jeGiiL5fZYLjbHd8a5pzqj/o4Axf7IIgwlvxPrJ6e3g=; b=I8rJh3WtTMu/Of5J5cXSMX28F4Y9aFwXSgqC/Z+Q1vbWiFqd3Mm2HoZhIcPxkhI8ZU yAfv3QrQtcy/rdeR3qAp1W7iWMN7oM/j5fb1HmX+BP8WaEWJh/n6tzB25g52Ut+l628F P1hyjyltCDQymScnnAHoEltj3ath7lCgwKxN5QJDOsLzFBN0fKJ+0AQdt8+qGaAHs8WJ C1XZ0ywyn+j9BQp2sNP8sTpiHBrkSI6hJsUpsCKW/pjyO1NoJbrQiJJ+mUxNvM1fxExb IfDIl9fVhluRSrWgagrWxg1Fyc2v3ZAxjHVIwDnLiUaGHS0Cjn75c3rc+0PkcDayW9vB rVSA== X-Gm-Message-State: AOAM532OzIKBwnm4LnrBH9ZySYLxlBJGbCPndLE1Q8MUsbiCpN6WT+GD dky48RT0gdeCdsmruYCv0qoWPzw9cUq9Nw== X-Google-Smtp-Source: ABdhPJzE7dY/wvT7FNfUFlMY7/r+bokGbYGrnlqTRnS1KwkLRjN6zPY3HIqk2d0hFCuAaKR4NvNBCg== X-Received: by 2002:a5d:55c4:: with SMTP id i4mr13847174wrw.84.1615551742453; Fri, 12 Mar 2021 04:22:22 -0800 (PST) Received: from localhost (host86-134-238-232.range86-134.btcentralplus.com. [86.134.238.232]) by smtp.gmail.com with ESMTPSA id s23sm1936588wmc.35.2021.03.12.04.22.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Mar 2021 04:22:22 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Subject: [PATCH] gdb: use make_scoped_restore to restore gdbpy_current_objfile Date: Fri, 12 Mar 2021 12:22:17 +0000 Message-Id: <20210312122217.2883834-1-andrew.burgess@embecosm.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Mar 2021 12:22:26 -0000 The current mechanism by which the Python gdb.current_objfile is maintained does not allow for nested auto-load events. It is assumed that one an auto-load script has finished loading then the current objfile should be set back to NULL. In a nested situation, we should be restoring the previous value. We already have an RAII class to handle save/restore type behaviour, so lets just switch to use that. The test is a little contrived, but is simple enough, and triggers the bug. The real use case might involve the auto-load script calling functions (either in the just-loaded object file, or in the main executable), which in turn trigger further auto-loads to occur. gdb/ChangeLog: * python/python.c (gdbpy_source_objfile_script): Use make_scoped_restore to restore gdbpy_current_objfile. (gdbpy_execute_objfile_script): Likewise. gdb/testsuite/ChangeLog: * gdb.python/py-auto-load-chaining-f1.c: New file. * gdb.python/py-auto-load-chaining-f1.o-gdb.py: New file. * gdb.python/py-auto-load-chaining-f2.c: New file. * gdb.python/py-auto-load-chaining-f2.o-gdb.py: New file. * gdb.python/py-auto-load-chaining.c: New file. * gdb.python/py-auto-load-chaining.exp: New file. --- gdb/ChangeLog | 6 ++ gdb/python/python.c | 10 +-- gdb/testsuite/ChangeLog | 9 +++ .../gdb.python/py-auto-load-chaining-f1.c | 24 ++++++ .../py-auto-load-chaining-f1.o-gdb.py | 37 +++++++++ .../gdb.python/py-auto-load-chaining-f2.c | 24 ++++++ .../py-auto-load-chaining-f2.o-gdb.py | 24 ++++++ .../gdb.python/py-auto-load-chaining.c | 58 ++++++++++++++ .../gdb.python/py-auto-load-chaining.exp | 78 +++++++++++++++++++ 9 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining-f1.c create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining-f2.c create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining.c create mode 100644 gdb/testsuite/gdb.python/py-auto-load-chaining.exp diff --git a/gdb/python/python.c b/gdb/python/python.c index 009c0c4c6d7..9eed258c181 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1388,11 +1388,10 @@ gdbpy_source_objfile_script (const struct extension_language_defn *extlang, return; gdbpy_enter enter_py (objfile->arch (), current_language); - gdbpy_current_objfile = objfile; + scoped_restore restire_current_objfile + = make_scoped_restore (&gdbpy_current_objfile, objfile); python_run_simple_file (file, filename); - - gdbpy_current_objfile = NULL; } /* Set the current objfile to OBJFILE and then execute SCRIPT @@ -1410,11 +1409,10 @@ gdbpy_execute_objfile_script (const struct extension_language_defn *extlang, return; gdbpy_enter enter_py (objfile->arch (), current_language); - gdbpy_current_objfile = objfile; + scoped_restore restire_current_objfile + = make_scoped_restore (&gdbpy_current_objfile, objfile); PyRun_SimpleString (script); - - gdbpy_current_objfile = NULL; } /* Return the current Objfile, or None if there isn't one. */ diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.c b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.c new file mode 100644 index 00000000000..77250bd91d6 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.c @@ -0,0 +1,24 @@ +/* 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 . */ + +/* Just a dummy function. */ + +int +f1 () +{ + return 1; +} diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py new file mode 100644 index 00000000000..4f5a4e78594 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f1.o-gdb.py @@ -0,0 +1,37 @@ +# Copyright (C) 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 . + +# This script is auto-loaded when the py-auto-load-chaining-f1.o +# object is loaded. + +import re + +print ("Entering f1.o auto-load script") + +print ("Current objfile is: %s" + % gdb.current_objfile ().filename) + +print ("Chain loading f2.o...") + +filename = gdb.current_objfile ().filename +filename = re.sub (r"-f1.o$", "-f2.o", filename) +r2 = gdb.lookup_global_symbol ('region_2').value () +gdb.execute ("add-symbol-file %s 0x%x" % (filename, r2)) + +print ("After loading f2.o...") +print ("Current objfile is: %s" + % gdb.current_objfile ().filename) + +print ("Leaving f1.o auto-load script") diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.c b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.c new file mode 100644 index 00000000000..f2eeab8f389 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.c @@ -0,0 +1,24 @@ +/* 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 . */ + +/* Just a dummy function. */ + +int +f2 () +{ + return 2; +} diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py new file mode 100644 index 00000000000..f5c29cd792f --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining-f2.o-gdb.py @@ -0,0 +1,24 @@ +# Copyright (C) 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 . + +# This script is auto-loaded when the py-auto-load-chaining-f2.o +# object is loaded. + +print ("Entering f2.o auto-load script") + +print ("Current objfile is: %s" + % gdb.current_objfile ().filename) + +print ("Leaving f2.o auto-load script") diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining.c b/gdb/testsuite/gdb.python/py-auto-load-chaining.c new file mode 100644 index 00000000000..5b7feed90aa --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining.c @@ -0,0 +1,58 @@ +/* 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 . */ + +#include +#include +#include +#include + +/* These will hold the addresses for two memory regions. */ + +void *region_1; +void *region_2; + +/* Allocate a page of memory using mmap, and return a pointer. */ + +void * +allocate_page (void) +{ + void *addr; + int pgsize = sysconf(_SC_PAGE_SIZE); + addr = mmap (NULL, pgsize, PROT_EXEC | PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (addr == MAP_FAILED) + perror ("mmap"); +} + +/* Only called so we can create a breakpoint. */ + +void +breakpt (void) +{ + /* Nothing. */ +} + +/* The test. */ + +int +main (void) +{ + region_1 = allocate_page (); + region_2 = allocate_page (); + + breakpt (); /* Break Here. */ +} diff --git a/gdb/testsuite/gdb.python/py-auto-load-chaining.exp b/gdb/testsuite/gdb.python/py-auto-load-chaining.exp new file mode 100644 index 00000000000..29414e8a775 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-auto-load-chaining.exp @@ -0,0 +1,78 @@ +# Copyright (C) 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 . + +# Test that the value of gdb.current_objfile is correct even when one +# auto-load script loads a second objfile, which triggers the +# execution of another (nested) objfile script. + +load_lib gdb-python.exp + +standard_testfile .c -f1.c -f2.c + +# Two additional object files needed for this test. +set f1_o [standard_output_file ${gdb_test_file_name}-f1.o] +set f2_o [standard_output_file ${gdb_test_file_name}-f2.o] + +# Now build the object files. +if {[gdb_compile "${srcdir}/${subdir}/${srcfile2}" ${f1_o} object {}] != ""} { + untested "failed to compile object file f1.o" + return -1 +} + +if {[gdb_compile "${srcdir}/${subdir}/${srcfile3}" ${f2_o} object {}] != ""} { + untested "failed to compile object file f2.o" + return -1 +} + +# Copy the two Python scripts to where the tests are being run. +set remote_python_file [gdb_remote_download host \ + ${srcdir}/${subdir}/${testfile}-f1.o-gdb.py] +set remote_python_file [gdb_remote_download host \ + ${srcdir}/${subdir}/${testfile}-f2.o-gdb.py] + +# Build the main test executable and start GDB. +if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile}]} { + return -1 +} + +# Skip all tests if Python scripting is not enabled. +if { [skip_python_tests] } { continue } + +if ![runto_main] { + return -1 +} + +set safe_path [standard_output_file ""] +gdb_test_no_output "set auto-load safe-path ${safe_path}" \ + "set auto-load safe-path" + +gdb_breakpoint [gdb_get_line_number "Break Here"] +gdb_continue_to_breakpoint "run to test breakpoint" + +gdb_test_no_output "set confirm off" +gdb_test "add-symbol-file ${f1_o} region_1" \ + [multi_line \ + "Entering f1\\.o auto-load script" \ + "Current objfile is: \[^\r\n\]+/py-auto-load-chaining-f1\\.o" \ + "Chain loading f2\\.o\\.\\.\\." \ + "add symbol table from file \"\[^\r\n\]+/py-auto-load-chaining-f2\\.o\" at" \ + "\\s+\\.text_addr = $hex" \ + "Entering f2\\.o auto-load script" \ + "Current objfile is: \[^\r\n\]+/py-auto-load-chaining-f2\\.o" \ + "Leaving f2\\.o auto-load script" \ + "After loading f2\\.o\\.\\.\\." \ + "Current objfile is: \[^\r\n\]+/py-auto-load-chaining-f1\\.o" \ + "Leaving f1\\.o auto-load script"] \ + "add-symbol-file f1.o" -- 2.25.4