gdb: do autoload before notifying Python side in new_objfile event
authorMichael Weghorn <m.weghorn@posteo.de>
Tue, 27 Apr 2021 14:02:42 +0000 (10:02 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Tue, 27 Apr 2021 15:22:32 +0000 (11:22 -0400)
commit2c473def12b08100e6b56261f01112db7f6aeab5
tree5e2e9f80c07f26ac3f3de8cf509a2fd5db9d8478
parent9a6e099f43a13efb0ee274002de689c2cb1b7e23
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
gdb/ChangeLog
gdb/auto-load.c
gdb/auto-load.h
gdb/python/py-inferior.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py [new file with mode: 0644]
gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc [new file with mode: 0644]
gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.h [new file with mode: 0644]
gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-main.cc [new file with mode: 0644]
gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event.py [new file with mode: 0644]