1# Copyright (C) 2021-2023 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# This file is part of the GDB testsuite. It tests that python pretty
17# printers defined in a python script that is autoloaded have been
18# registered when a custom event handler for the new_objfile event
19# is called.
20
21import gdb
22import os
23
24
25def new_objfile_handler(event):
26    assert isinstance(event, gdb.NewObjFileEvent)
27    objfile = event.new_objfile
28
29    # Only observe the custom test library.
30    libname = "libpy-autoloaded-pretty-printers-in-newobjfile-event"
31    if libname in os.path.basename(objfile.filename):
32        # If everything went well and the pretty-printer auto-load happened
33        # before notifying the Python listeners, we expect to see one pretty
34        # printer, and it must be ours.
35        all_good = (
36            len(objfile.pretty_printers) == 1
37            and objfile.pretty_printers[0].name == "my_library"
38        )
39
40        if all_good:
41            gdb.parse_and_eval("all_good = 1")
42        else:
43            print("Oops, not all good:")
44            print("pretty printer count: {}".format(len(objfile.pretty_printers)))
45
46            for pp in objfile.pretty_printers:
47                print("  - {}".format(pp.name))
48
49
50gdb.events.new_objfile.connect(new_objfile_handler)
51