aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp
diff options
context:
space:
mode:
authorMichael Weghorn <m.weghorn@posteo.de>2021-04-27 10:02:42 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-04-27 11:22:32 -0400
commit2c473def12b08100e6b56261f01112db7f6aeab5 (patch)
tree5e2e9f80c07f26ac3f3de8cf509a2fd5db9d8478 /gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp
parent9a6e099f43a13efb0ee274002de689c2cb1b7e23 (diff)
downloadbinutils-2c473def12b08100e6b56261f01112db7f6aeab5.zip
binutils-2c473def12b08100e6b56261f01112db7f6aeab5.tar.gz
binutils-2c473def12b08100e6b56261f01112db7f6aeab5.tar.bz2
gdb: do autoload before notifying Python side in new_objfile event
Without any explicit dependencies specified, the observers attached to the 'gdb::observers::new_objfile' observable are always notified in the order in which they have been attached. The new_objfile observer callback to auto-load scripts is attached in '_initialize_auto_load'. The new_objfile observer callback that propagates the new_objfile event to the Python side is attached in 'gdbpy_initialize_inferior', which is called via '_initialize_python'. With '_initialize_python' happening before '_initialize_auto_load', the consequence was that the new_objfile event was emitted on the Python side before autoloaded scripts had been executed when a new objfile was loaded. As a result, trying to access the objfile's pretty printers (defined in the autoloaded script) from a handler for the Python-side 'new_objfile' event would fail. Those would only be initialized later on (when the 'auto_load_new_objfile' callback was called). To make sure that the objfile passed to the Python event handler is properly initialized (including its 'pretty_printers' member), make sure that the 'auto_load_new_objfile' observer is notified before the 'python_new_objfile' one that propagates the event to the Python side. To do this, make use of the mechanism to explicitly specify dependencies between observers (introduced in a preparatory commit). Add a corresponding testcase that involves a test library with an autoloaded Python script and a handler for the Python 'new_objfile' event. (The real world use case where I came across this issue was in an attempt to extend handling for GDB pretty printers for dynamically loaded objfiles in the Qt Creator IDE, s. [1] and [2] for more background.) [1] https://bugreports.qt.io/browse/QTCREATORBUG-25339 [2] https://codereview.qt-project.org/c/qt-creator/qt-creator/+/333857/1 Tested on x86_64-linux (Debian testing). gdb/ChangeLog: * gdb/auto-load.c (_initialize_auto_load): 'Specify token when attaching the 'auto_load_new_objfile' observer, so other observers can specify it as a dependency. * gdb/auto-load.h (struct token): Declare 'auto_load_new_objfile_observer_token' as token to be used for the 'auto_load_new_objfile' observer. * gdb/python/py-inferior.c (gdbpy_initialize_inferior): Make 'python_new_objfile' observer depend on 'auto_load_new_objfile' observer, so it gets notified after the latter. gdb/testsuite/ChangeLog: * gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py: New test. * gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc: New test. * gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.h: New test. * gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-main.cc: New test. * gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp: New test. * gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.py: New test. Change-Id: I8275b3f4c3bec32e56dd7892f9a59d89544edf89
Diffstat (limited to 'gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp')
-rw-r--r--gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp80
1 files changed, 80 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp
new file mode 100644
index 0000000..96ae41b
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp
@@ -0,0 +1,80 @@
+# 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 <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite. It tests that Python pretty-printers
+# defined in a Python script that is autoloaded are registered when an event
+# handler for the new_objfile event is called.
+
+load_lib gdb-python.exp
+
+standard_testfile -main.cc
+
+set srcfile_lib "${testfile}-lib.cc"
+set python_event_handler_file "${srcdir}/${subdir}/${testfile}.py"
+set libname "lib${testfile}"
+set python_autoload_file "${srcdir}/${subdir}/${libname}.so-gdb.py"
+set binfile_lib [standard_output_file "${libname}.so"]
+
+# Start GDB first - needed for skip_python_tests.
+clean_restart
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+# Compile library.
+if { [gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} \
+ {debug c++}] != "" } {
+ return -1
+}
+
+# Compile main program.
+if { [gdb_compile ${srcdir}/${subdir}/${srcfile} \
+ ${binfile} \
+ executable \
+ [list debug c++ shlib=$binfile_lib]] != "" } {
+ return -1
+}
+
+# Make the -gdb.py script available to gdb, it is automatically loaded by
+# gdb if it is put in the same directory as the library.
+set remote_python_autoload_file \
+ [gdb_remote_download host $python_autoload_file]
+
+gdb_test_no_output \
+ "set auto-load safe-path ${remote_python_autoload_file}" \
+ "set auto-load safe-path"
+
+# Load the Python file that defines a handler for the new_objfile event.
+set remote_python_event_handler_file\
+ [gdb_remote_download host $python_event_handler_file]
+gdb_test_no_output "source ${remote_python_event_handler_file}" "load python file"
+
+gdb_load ${binfile}
+
+gdb_test_no_output "set print pretty on"
+
+if { ![runto_main] } {
+ fail "failed to run to main"
+ return
+}
+
+# Check that the new_objfile handler saw the pretty-printer.
+gdb_test "print all_good" " = true"
+
+# Check that the pretty-printer actually works.
+gdb_test "info pretty-printer" "my_library.*MyClassTestLib.*"
+gdb_breakpoint [gdb_get_line_number "break to inspect"]
+gdb_test "continue" "Breakpoint $decimal, main .*"
+gdb_test "print test" "MyClassTestLib object, id: 1.*"