From patchwork Mon Dec 9 11:18:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 102666 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0C9DB385828E for ; Mon, 9 Dec 2024 11:23:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C9DB385828E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=eK9hTe5k X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 794CF3858428 for ; Mon, 9 Dec 2024 11:18:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 794CF3858428 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 794CF3858428 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733743097; cv=none; b=cylDiCGXz2m7MDd+NbfEfTxhzCb63QfdeRZaRjmBiwo7Z+zHG5VVmt0kKUP6iK9qvTRrUk+YnsK8cxY2XkdDFNfyGxZh91ulrQAWEumNDDXfqkf//AFHMgOSAs3EuRBJSCc3YfkO3kqgMf4BTBYW2MMzMK8mfWNC2sfYDnWUrkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733743097; c=relaxed/simple; bh=OtpdqV9J50UDgO2g/Sv7v5OU0Y7CY33zVRLTr0t3ep0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=noJIVaYdatBxPgmV4nTs81rNSSufpe8nwe3HVjSAOyUAKq8ZygYT/AKE/LlV68y6l33iqPd//UF/uDdB32fZuZNT0fElJrvx2Yr7LXGp/yTVKRgBI8R79ytfxBCUD/x7kT1Cjki9WkdbZi6JGJciMN1heKM7VExUbcJO183cdMo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 794CF3858428 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733743097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wZl/op9u0C8+mZgw2HEmWSlFRDTCIx2sm1xjasDUz1E=; b=eK9hTe5kSWKhL8F+t+A+jmCqHZdA96q/GnAHkgBnOLmAeBO1G0UdTj/VOxdSYl7zm3w67U jlYMy5dLjg+jwjYg/f8mwbbpRd7ckwuCrPgdxfONjuHJLoLYkTgk0FTCEfQbOPU6GV/R/l 3cPMw03i0rqcKfBlPmLAMY8QE38pUgQ= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-223-4hTwuZSqMEWctZ6eMUGXCw-1; Mon, 09 Dec 2024 06:18:16 -0500 X-MC-Unique: 4hTwuZSqMEWctZ6eMUGXCw-1 X-Mimecast-MFC-AGG-ID: 4hTwuZSqMEWctZ6eMUGXCw Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-434f40667e5so7216905e9.1 for ; Mon, 09 Dec 2024 03:18:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733743095; x=1734347895; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wZl/op9u0C8+mZgw2HEmWSlFRDTCIx2sm1xjasDUz1E=; b=s92dIWzILr7jkLddJlqmt16/NgVhHD7Gdao468xcN4/UKKEsYoAuzBq6Q436UcGcyb yxYB+WiOs6hxMqMW3xUlO7kA30nZlgA1rmkQujCKcQzpvZUSXRt3DB3O1KIBCFxysYx+ k/1ORgjkpTOFg7Tsaa/ASQjyTHTs/Ccy057ETRWBvrWqOHJCS8NU4kuhPxqOp+tGldF4 iywQTJVts9CooM87RSGs1Hja6DbQD460HOcSgTV241e8UUJMthiswHgMuWp62hf4yCPH e68N7Ra+cyhp08qnl6cdOz6tjGP3QRG22P6Z7uxO8qlBV+OgXwpKW9UriBwb0GsDwnXW hCFQ== X-Gm-Message-State: AOJu0YxxFiQO0OubJAJY16KmPA5cY5qwqw85bkZzgtbb3HnLitq8MwF+ kqGJrFpEnL2tSwcnhsY8OyZ/7gl24xj3/3e+g17L27dQcRhCIMCDAbodW/H1kf8s6i6pt2btG0U P8NH8c5N0jISp04I7c2vvHDLI4a99scdton2BXyvNqVgVEsY87GYyb4fKIIn8548IM32+H5GxMW GzlOfeLKsD6Jc6gY+8MUwGOgf201XefDSnkq9FscMAzm4= X-Gm-Gg: ASbGncvE82mET6YafDuXJcDMaAPwWzBQ765Ex7NR89C3P0ZWLlJe/KTGApbkq4Tt8r6 RM2zUqKRBnzVYQwfsuiH0PGzv3o2RoBDa6DcmOgdghINFncRtkoxR6YcXsd+5ARcYbebqo+JIf+ cJRP8GUEEnPqtCHLBTQDBmX3+DjPNHKGz6ERS/hi9lgJmk6TQd1bGVR0pdu3mqRa1OLXsaywkgM 3/n0FrCTr7MqQzgeOlaoxcVRl60pIHXehYmiZ6otI+fH7Wo8QPU2IAQFTwHvR/4fj+W7YnYWvuH Dw== X-Received: by 2002:adf:e18a:0:b0:385:e2c4:1f8d with SMTP id ffacd0b85a97d-3862b351423mr9047179f8f.19.1733743094772; Mon, 09 Dec 2024 03:18:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IEWeSTA4TiBjsTMW3jGxuoswgj8H2QIpMZEIjFxneRACmh6fPcDgajFGMDMOjKcY/LINNNXpw== X-Received: by 2002:adf:e18a:0:b0:385:e2c4:1f8d with SMTP id ffacd0b85a97d-3862b351423mr9047154f8f.19.1733743094255; Mon, 09 Dec 2024 03:18:14 -0800 (PST) Received: from localhost (197.209.200.146.dyn.plus.net. [146.200.209.197]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38634bb1669sm7615373f8f.60.2024.12.09.03.18.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 03:18:14 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Hannes Domani Subject: [PATCHv5 4/7] gdb: handle dprintf breakpoints when unloading a shared library Date: Mon, 9 Dec 2024 11:18:01 +0000 Message-Id: <540204eab68597c954e84c3014fcaff0a2a107f9.1733742925.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 3pfIRzQDy5SnzuHqDNt-XT3zd-be3ZcI7czpvjJ-4rY_1733743095 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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~patchwork=sourceware.org@sourceware.org While working on the previous commit I realised that GDB would not handle dprintf breakpoints correctly when a shared library was unloaded. Consider this example using the test binary from the previous commit, the function 'foo' where we create a dprintf is in a shared library: (gdb) b 59 Breakpoint 1 at 0x401215: file /tmp/projects/binutils-gdb/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.base/shlib-unload.c, line 59. (gdb) r Starting program: /tmp/projects/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/shlib-unload/shlib-unload Breakpoint 1, main () at /tmp/projects/binutils-gdb/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.base/shlib-unload.c:59 59 res = dlclose (handle); /* Break here. */ (gdb) dprintf foo,"In foo" Dprintf 2 at 0x7ffff7fc50fd: file /tmp/projects/binutils-gdb/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.base/shlib-unload-lib.c, line 23. (gdb) n Error in re-setting breakpoint 2: Function "foo" not defined. warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd warning: error removing breakpoint 2 at 0x7ffff7fc50fd Cannot remove breakpoints because program is no longer writable. Further execution is probably impossible. 60 assert (res == 0); (gdb) What happens here is that as the inferior steps over the dlclose call the shared library containing 'foo' is unloaded and disable_breakpoints_in_unloaded_shlib is called. However in disable_breakpoints_in_unloaded_shlib we have this check: if (b.type != bp_breakpoint && b.type != bp_jit_event && b.type != bp_hardware_breakpoint && !is_tracepoint (&b)) continue; As the dprintf has type bp_dprintf then this check triggers and we ignore the dprintf, meaning the dprintf is not disabled. When the inferior stops after the 'next' GDB tries to remove all breakpoints but the dprintf can no longer be removed, the memory in which it was placed has been unmapped from the inferior. The fix is to start using is_breakpoint() in disable_breakpoints_in_unloaded_shlib instead of the bp_breakpoint and bp_hardware_breakpoint checks. The is_breakpoint() function also checks for bp_dprintf. With this fix in place GDB now correctly disables the breakpoint and we no longer see the warning about removing the breakpoint. Tested-By: Hannes Domani --- gdb/breakpoint.c | 5 ++--- gdb/testsuite/gdb.base/shlib-unload.exp | 29 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index b663277aeb7..99c216b0271 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -8083,9 +8083,8 @@ disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib { bool bp_modified = false; - if (b.type != bp_breakpoint - && b.type != bp_jit_event - && b.type != bp_hardware_breakpoint + if (b.type != bp_jit_event + && !is_breakpoint (&b) && !is_tracepoint (&b)) continue; diff --git a/gdb/testsuite/gdb.base/shlib-unload.exp b/gdb/testsuite/gdb.base/shlib-unload.exp index 791f9f518e8..7516ee2a292 100644 --- a/gdb/testsuite/gdb.base/shlib-unload.exp +++ b/gdb/testsuite/gdb.base/shlib-unload.exp @@ -111,4 +111,33 @@ proc_with_prefix test_bp_modified_events {} { } } +# Check that GDB disables dprintf breakpoints within a shared library +# when the shared library is unloaded. +proc_with_prefix test_dprintf_at_unload {} { + clean_restart $::binfile + + if {![runto_main]} { + return + } + + gdb_breakpoint $::srcfile:$::bp_line + gdb_continue_to_breakpoint "stop before dlclose" + + # Setup a dprintf within the shared library. + gdb_test "dprintf foo,\"In foo\"" + set bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get b/p number"] + + # Unload the shared library, GDB should disable our b/p. + gdb_test "next" $::stop_after_bp_re + + # Check that our b/p is now showing as disabled. + gdb_test "info breakpoints $bp_num" \ + [multi_line \ + "^Num\\s+Type\\s+Disp\\s+Enb\\s+Address\\s+What" \ + "$bp_num\\s+dprintf\\s+keep\\s+y\\s+\\s+foo" \ + "\\s+printf \"In foo\""] +} + test_bp_modified_events +test_dprintf_at_unload