From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 6Ww+EvU8mWotqSMAWB0awg (envelope-from ) for ; Thu, 03 Sep 2026 05:25:09 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=kBiALPYs; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 45A4B1E09B; Thu, 03 Sep 2026 05:25:09 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 6DC551E09B for ; Thu, 03 Sep 2026 05:25:07 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A21C4BA9027 for ; Thu, 3 Sep 2026 09:25:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A21C4BA9027 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=kBiALPYs Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id ABCC04B9DB6C for ; Thu, 3 Sep 2026 09:24:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ABCC04B9DB6C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ABCC04B9DB6C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::331 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788427449; cv=none; b=BNb7oWURp1/3+AvTiVtOOFv0EJ6Vq5PlhKBrO756vnTvLqaog2GPuR+Gw+ias6cEnStAQk6/B++FI+JmwREk0gGXqVO/+V2i6CbfnCPLGzo/G/3K2Mpj8FnrHmlaYw+1sodcRpmnfHI9yM9T699til3WVAGQAa8pMGDYDt6+KlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788427449; c=relaxed/simple; bh=EdVQQd/hT2iKbLaJWAnDqE0sjLu5EmLJJVzp4Gklkwc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=p8GDymSf7C7ul9R8+9Ym0pbTaOS4dYb9WVItF/4L97JOdHsqqCc/2Sj640+9Ryx084qJvNAf7CMxGEX2R95Y4JCGPfu0m9AdRI+97XK0cJ8fkSRxk8CqU1Ghnqw0ziHtlPguw6qZesXJPXdEngqSIzTQDa1jODeSe8+Km2ECafM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=kBiALPYs DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABCC04B9DB6C Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-49b9320423cso21145625e9.0 for ; Thu, 03 Sep 2026 02:24:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788427448; x=1789032248; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uSfX89DlHthr1INs+EOZUUO/lZSsp9rAg0sH8HYJT3g=; b=kBiALPYs1Gtmhu+kz3d/hwGFnJbNx43PrCT+WcsxzsScyB5HdB8YltHJGhGaEE+5aM j/edHbnuBRVIvNKmLTLkV6DjQRny0za4TsACw9bcd4kTc3td2XGBStr5PtsaF1GaWuVN mkZv09ATw/17/h2XXdN/FOe9/OyEw9elF9EVBstoA35kb38f5B1v8Jt61Dn/kLH3zVZT RBBmxWRjMONLUq3Y2s3vZzl/P+EsKWqvvhfxrWDQUEbWI6GKEMsv+TiRcWXdsl2wp3lp PX/jlSdYwo6b4l3M9Vtw9O0leP24DoqHlTyD1hWTI2zWwqalyuY50spzuCZgGTnRlXFR ARxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788427448; x=1789032248; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uSfX89DlHthr1INs+EOZUUO/lZSsp9rAg0sH8HYJT3g=; b=GUtCxNJJxrFgenQaTGJ49A+Wgs/GgxaM66K5wGQp6qwl1TGwr6FlPvPWtvFnEAWHIw Z2uUfjg889R70ZYiRh/7deSrUD6nt2lj5kApFXuG40nqLvTvdnCPl6v/rg1zcqf3Y2Oo QYhpFfAwLJOLw4B0wbAtR/hzRvi1RR5+2NpZkb2Jrx7y4Gci6K7vlikK/UcpaciXb+da Uwu4eBf8nB/l6ZgqcWRSSvYaYeDSePLM2zUPxbUdyU5+amyyV2Ec+8PBjeDW3Sf+IL5n +pURcnz7NQ7cxqgOF/YEwkOf0ZZwIQbnKogc/JVdyrOwWjUjOVdM4HuBocquCVx6Ay0V Px1g== X-Gm-Message-State: AFuF++nIAlRwRFfl1ae/t/ijTb5TrIinSV1HjotAKDbjCoDikgb7Znz8 pQcibkIe36drna8H1IIVZWVy/aKxTUdR6gGTQb+1pXVMb75IdCw0I3M/teSu8dEgs7M= X-Gm-Gg: AYBFou3NoHMBxAAwGhokh2WhdGEIbYJXF2EjcNODrpJwNi/U6ZogEbMM/bQC2MBg65e BZhCLweuSBXlUy0CkUeOllse5GmTAYgyQDPMQCMg0K077mMwcT5eBDqbdIs+JCvm6lk1g8T6IjK LdKovmLVjTtgjSpMpTvtvNjUA2l6F0T+TCqgY7eYaUNtnpBRwFvy+DT+cjVfRP2paGFOA4DY6e3 ERGTtu5EWsLbN6CpuxoErjiuspY8GD6wg3YyivqBCdfl0u9rSV6rPwgV6syyVwAlwNCqBpgBNVz xGzSG9GU6pUHSZErkByL8rTXg1PV1LfXRxG9sZqprvkdhdmic3Z4oZFphA0W8EaPKkUsV7OKmF4 VGIhqvR/Ns72UcjEDNw/L9ImhF8kvGR5p77ZeeH2xTEe9lllIRWwYeTdHuTxqUuPFvvJIyJxD+m ngzrt/BgkAUesa7jjC8DUGORiiCAnUlRe2LrUysN/JQ3SWcvPrIVj2ugPHHtIMDKE8tGgT8U9Tp J05F0PCDdoBEH3eVxY0z7A= X-Received: by 2002:a05:600c:1909:b0:499:ae94:be05 with SMTP id 5b1f17b1804b1-49ce55cb36amr160508975e9.0.1788427446908; Thu, 03 Sep 2026 02:24:06 -0700 (PDT) Received: from amwld-tmagires1.us.drwholdings.com ([149.14.21.6]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49ce58d1c9esm51532415e9.0.2026.09.03.02.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Sep 2026 02:24:06 -0700 (PDT) From: Tudor-Stefan Magirescu To: gdb-patches@sourceware.org Cc: tudor.magirescu@gmail.com Subject: [PATCH v2 1/2] [gdb] Fix auto-load script re-execution on subsequent runs Date: Thu, 3 Sep 2026 11:22:00 +0200 Message-ID: <20260903092201.663063-2-tudor.magirescu@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260903092201.663063-1-tudor.magirescu@gmail.com> References: <20260901140602.3287196-1-tudor.magirescu@gmail.com> <20260903092201.663063-1-tudor.magirescu@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Update maybe_add_script_file and maybe_add_script_text to unconditionally update the loaded field of an existing hash table entry, and change callers to gate script execution on whether the script is on the safe-path, independently of hash table state. This fixes two related behaviors. For companion -gdb.py scripts, the Loaded field shown by "info auto-load" could become stale if the script was initially blocked by the safe-path but loaded in a subsequent run. For scripts referenced via .debug_gdb_scripts sections, GDB would not re-execute the script the second time the associated object file was loaded, causing per-objfile registered pretty-printers to stop working after the first run of the inferior. Add test variants for the .debug_gdb_scripts filename and inline-text cases to gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event, verifying that a script blocked on the first run is loaded correctly on a subsequent run after the safe-path is widened. Tested on x86_64-linux-gnu and found no regressions. --- gdb/auto-load.c | 43 +++-- ...ers-in-newobjfile-event-objfile.so-gdb.py} | 0 ...s-in-newobjfile-event-dotdebug-filename.py | 43 +++++ ...-newobjfile-event-lib-dotdebug-filename.cc | 39 +++++ ...s-in-newobjfile-event-lib-dotdebug-text.cc | 49 ++++++ ...inters-in-newobjfile-event-lib-objfile.cc} | 0 ...ed-pretty-printers-in-newobjfile-event.exp | 158 ++++++++++++------ 7 files changed, 259 insertions(+), 73 deletions(-) rename gdb/testsuite/gdb.python/{libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py => libpy-autoloaded-pretty-printers-in-newobjfile-event-objfile.so-gdb.py} (100%) create mode 100644 gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-dotdebug-filename.py create mode 100644 gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-filename.cc create mode 100644 gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-text.cc rename gdb/testsuite/gdb.python/{py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc => py-autoloaded-pretty-printers-in-newobjfile-event-lib-objfile.cc} (100%) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index ccbd091e1c2..8a6e818fd69 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -676,8 +676,8 @@ get_auto_load_pspace_data_for_loading (struct program_space *pspace) LOADED is true if the script has been (is going to) be loaded, false otherwise (such as if it has not been found). FULL_PATH is NULL if the script wasn't found. - - The result is true if the script was already in the hash table. */ + Returns the previous value of the loaded field, or false if the entry + did not exist yet. */ static bool maybe_add_script_file (struct auto_load_pspace_info *pspace_info, bool loaded, @@ -714,18 +714,19 @@ maybe_add_script_file (struct auto_load_pspace_info *pspace_info, bool loaded, } else (*slot)->full_path = NULL; - (*slot)->loaded = loaded; (*slot)->language = language; } - return in_hash_table; + bool was_loaded = (*slot)->loaded; + (*slot)->loaded = loaded; + return was_loaded; } /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO. LOADED is true if the script has been (is going to) be loaded, false otherwise (such as if it has not been found). - - The result is true if the script was already in the hash table. */ + Returns the previous value of the loaded field, or false if the entry + did not exist yet. */ static bool maybe_add_script_text (struct auto_load_pspace_info *pspace_info, @@ -753,11 +754,12 @@ maybe_add_script_text (struct auto_load_pspace_info *pspace_info, strcpy (p, name); (*slot)->name = p; (*slot)->full_path = NULL; - (*slot)->loaded = loaded; (*slot)->language = language; } - return in_hash_table; + bool was_loaded = (*slot)->loaded; + (*slot)->loaded = loaded; + return was_loaded; } /* Clear the table of loaded section scripts. */ @@ -845,12 +847,7 @@ auto_load_objfile_script_1 (struct objfile *objfile, const char *realname, = get_auto_load_pspace_data_for_loading (objfile->pspace ()); maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile, language); - - /* To preserve existing behavior we don't check for whether the - script was already in the table, and always load it. - It's highly unlikely that we'd ever load it twice, - and these scripts are required to be idempotent under multiple - loads anyway. */ + /* Execute the script if it is on the safe path. */ if (is_safe) { objfile_script_sourcer_func *sourcer @@ -1003,13 +1000,12 @@ source_script_file (struct auto_load_pspace_info *pspace_info, section_name, offset); } - bool in_hash_table - = maybe_add_script_file (pspace_info, bool (opened), file, - (opened ? opened->full_path.get (): NULL), - language); + maybe_add_script_file (pspace_info, bool (opened), file, + (opened ? opened->full_path.get (): NULL), + language); - /* If this file is not currently loaded, load it. */ - if (opened && !in_hash_table) + /* Execute the script if it is on the safe path. */ + if (opened) sourcer (language, objfile, opened->stream.get (), opened->full_path.get ()); } @@ -1087,11 +1083,10 @@ of file %ps."), bool is_safe = file_is_auto_load_safe (objfile_name (objfile)); - bool in_hash_table - = maybe_add_script_text (pspace_info, is_safe, name, language); + maybe_add_script_text (pspace_info, is_safe, name, language); - /* If this file is not currently loaded, load it. */ - if (is_safe && !in_hash_table) + /* Execute the script if it is on the safe path. */ + if (is_safe) executor (language, objfile, name, script_text); } diff --git a/gdb/testsuite/gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py b/gdb/testsuite/gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event-objfile.so-gdb.py similarity index 100% rename from gdb/testsuite/gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py rename to gdb/testsuite/gdb.python/libpy-autoloaded-pretty-printers-in-newobjfile-event-objfile.so-gdb.py diff --git a/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-dotdebug-filename.py b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-dotdebug-filename.py new file mode 100644 index 00000000000..a353358c20d --- /dev/null +++ b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-dotdebug-filename.py @@ -0,0 +1,43 @@ +# Copyright (C) 2026 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 file is part of the GDB testsuite. It tests that python pretty +# printers defined in a python script that is autoloaded have been +# registered when a custom event handler for the new_objfile event +# is called. + +import gdb.printing + + +class MyClassTestLibPrinter(object): + "Print a MyClassTestLib" + + def __init__(self, val): + self.val = val + + def to_string(self): + return "MyClassTestLib object, id: {}".format(self.val["id"]) + + def display_hint(self): + return "string" + + +def build_pretty_printer(): + pp = gdb.printing.RegexpCollectionPrettyPrinter("my_library") + pp.add_printer("MyClassTestLib", "^MyClassTestLib$", MyClassTestLibPrinter) + return pp + + +gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer()) diff --git a/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-filename.cc b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-filename.cc new file mode 100644 index 00000000000..fe31f7083a0 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-filename.cc @@ -0,0 +1,39 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 "py-autoloaded-pretty-printers-in-newobjfile-event-lib.h" + +#ifndef SCRIPT_FILE +#error "SCRIPT_FILE not defined" +#endif + +asm( +".pushsection \".debug_gdb_scripts\",\"MS\",@progbits,1\n" +".byte 1\n" +".asciz \"" SCRIPT_FILE "\"\n" +".popsection\n" +); + +MyClassTestLib::MyClassTestLib (int theId) +{ + id = theId; +} + +int MyClassTestLib::getId () +{ + return id; +} diff --git a/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-text.cc b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-text.cc new file mode 100644 index 00000000000..12d663b362a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-dotdebug-text.cc @@ -0,0 +1,49 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 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 "py-autoloaded-pretty-printers-in-newobjfile-event-lib.h" + +asm( +".pushsection \".debug_gdb_scripts\",\"MS\",@progbits,1\n" +".byte 4\n" +".ascii \"gdb.inlined-script\\n\"\n" +".ascii \"import gdb.printing\\n\"\n" +".ascii \"class MyClassTestLibPrinter(object):\\n\"\n" +".ascii \" def __init__(self, val):\\n\"\n" +".ascii \" self.val = val\\n\"\n" +".ascii \" def to_string(self):\\n\"\n" +".ascii \" return \\\"MyClassTestLib object, id: {}\\\".format(self.val[\\\"id\\\"])\\n\"\n" +".ascii \" def display_hint(self):\\n\"\n" +".ascii \" return \\\"string\\\"\\n\"\n" +".ascii \"def build_pretty_printer():\\n\"\n" +".ascii \" pp = gdb.printing.RegexpCollectionPrettyPrinter(\\\"my_library\\\")\\n\"\n" +".ascii \" pp.add_printer(\\\"MyClassTestLib\\\", \\\"^MyClassTestLib$\\\", MyClassTestLibPrinter)\\n\"\n" +".ascii \" return pp\\n\"\n" +".ascii \"gdb.printing.register_pretty_printer(gdb.current_objfile(), build_pretty_printer())\\n\"\n" +".byte 0\n" +".popsection\n" +); + +MyClassTestLib::MyClassTestLib (int theId) +{ + id = theId; +} + +int MyClassTestLib::getId () +{ + return id; +} diff --git a/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc b/gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-objfile.cc similarity index 100% rename from gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc rename to gdb/testsuite/gdb.python/py-autoloaded-pretty-printers-in-newobjfile-event-lib-objfile.cc 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 index 9b8a10cf84f..cf0bdb80c51 100644 --- 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 @@ -15,7 +15,9 @@ # 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. +# handler for the new_objfile event is called. It also tests that scripts +# which were not loaded because the auto-load safe-path did not cover them are +# loaded on the next run once the safe-path is widened. load_lib gdb-python.exp @@ -23,70 +25,128 @@ require allow_python_tests 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"] - -# Compile library. -if { [gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} \ - {debug c++}] != "" } { - return -} - -# Compile main program. -if { [gdb_compile ${srcdir}/${subdir}/${srcfile} \ - ${binfile} \ - executable \ - [list debug c++ shlib=$binfile_lib]] != "" } { - return -} - -clean_restart +set libscript "lib${testfile}-objfile.so-gdb.py" +set python_autoload_file "${srcdir}/${subdir}/${libscript}" # 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_load_shlib $binfile_lib +proc newobjfile_event_test { variant } { + global srcdir subdir testfile srcfile + global remote_python_autoload_file remote_python_event_handler_file libscript + global decimal -if { ![runto_main] } { - return -} + with_test_prefix $variant { + if { $variant ne "objfile" && [is_remote host] } { + unsupported "$variant requires non-remote host" + return + } -if { [is_remote target ] } { - set target_sysroot 0 - gdb_test_multiple "show sysroot" "" { - -re -wrap "\r\nThe current system root is \"target:.*\"\\." { - set target_sysroot 1 + set libname "lib${testfile}-${variant}" + set binfile_lib [standard_output_file "${libname}.so"] + set binfile [standard_output_file "${testfile}-${variant}"] + + set shlib_flags [list debug c++] + if { $variant eq "dotdebug-filename" } { + set quoted "\"${srcdir}/${subdir}/${testfile}-${variant}.py\"" + lappend shlib_flags additional_flags=-DSCRIPT_FILE=$quoted } - -re -wrap "" { + + # Compile library. + if { [gdb_compile_shlib \ + "${srcdir}/${subdir}/${testfile}-lib-${variant}.cc" \ + ${binfile_lib} $shlib_flags] != "" } { + return + } + + # Compile main program. + if { [gdb_compile ${srcdir}/${subdir}/${srcfile} \ + ${binfile} \ + executable \ + [list debug c++ shlib=$binfile_lib]] != "" } { + return + } + + if { $variant eq "objfile" } { + set safe_path_allow $remote_python_autoload_file + set script_name $libscript + } elseif { $variant eq "dotdebug-filename" } { + set safe_path_allow "${srcdir}/${subdir}/${testfile}-${variant}.py" + set script_name "${testfile}-${variant}.py" + } else { + set safe_path_allow $binfile_lib + set script_name "gdb.inlined-script" } - } - if { $target_sysroot } { - unsupported "sysroot start with target: -- auto-load not supported" - return + clean_restart + gdb_test_no_output "source $remote_python_event_handler_file" "load python file" + gdb_load ${binfile} + gdb_load_shlib $binfile_lib + + if { [is_remote target ] } { + set target_sysroot 0 + gdb_test_multiple "show sysroot" "" { + -re -wrap "\r\nThe current system root is \"target:.*\"\\." { + set target_sysroot 1 + } + -re -wrap "" { + } + } + + if { $target_sysroot } { + unsupported "sysroot start with target: -- auto-load not supported" + return + } + } + + gdb_test_no_output "set auto-load safe-path /restricted" \ + "set restricted auto-load safe-path" + + if { ![runto_main] } { + return + } + + gdb_test_multiple "info auto-load python-scripts" "verify script not loaded" { + -re -wrap "Yes.*${script_name}.*" { + fail $gdb_test_name + } + -re -wrap "No.*${script_name}.*" { + pass $gdb_test_name + } + } + + gdb_test_no_output \ + "set auto-load safe-path ${safe_path_allow}" \ + "set auto-load safe-path" + + if { ![runto_main] } { + return + } + + gdb_test_multiple "info auto-load python-scripts" "verify script loaded" { + -re -wrap "Yes.*${script_name}.*" { + pass $gdb_test_name + } + -re -wrap "No.*${script_name}.*" { + fail $gdb_test_name + } + } + + gdb_test "print all_good" " = true" + 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.*" } } -# 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.*" +newobjfile_event_test "objfile" +newobjfile_event_test "dotdebug-filename" +newobjfile_event_test "dotdebug-text" -- 2.43.0