From patchwork Sat Jul 20 17:03:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94256 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 74A923861003 for ; Sat, 20 Jul 2024 17:04:04 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 08F8A3858C50 for ; Sat, 20 Jul 2024 17:03:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 08F8A3858C50 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 08F8A3858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495010; cv=none; b=NNfbM3giOZvcyxzaarr0/rtv/qo5EkS8elXAio1TP17AwVGvrstB/Xf04KDjf9KERvyZ1KMtx+VjmhgZJ9sBlJPiO7vLbbU2ucWKVriyQwfPeY/NmHNSCw5VBXmO/x3/C2FSdeL2jVNnfKT3oN4rEmGO+1/ieJoQCMXOwwTwzFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495010; c=relaxed/simple; bh=p8+n+Pr2YEHLF/qTOsMtbnZO1+bX9onpITGDJFJ9CpA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=BI4PTarg2m6qN5LpG3DQEkKfu4CHdGmZWBY1GPXgK4kfyArXZMOfXWvZEfY7HfY+Vr9A8ru3BI9YVdOpn/jXjEESBcRoaO2YcOdTOOqp1O89g/lQXlLfNOy8qgEZu01MhOQ3kg2clf2j7W9L3cpxP+ynWvu0tlbim/ubraUVtu0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495008; 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=6wYVsr5dSSq/N/emvvls2jIOM9xtQeSUdpocp6Y/7Jo=; b=ggYFN0pnM4lSAYAaDl5Zye4Z1ogG8ob0DGsDN8Kf7Wwz8OmWwRz1AqtPeLDXakUaPeIfqm lXFWV2RrxuitK7GG9VOVDH07Pdz06tWjTWpAYqUXyiETfDx0MYlclbSe8zjXjYxuh0P9Ye 0PRXaVIOxzlfyn442h/je3t59MMnsBA= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-524-5IVsXTGcPj2WZrgQ8d4yNw-1; Sat, 20 Jul 2024 13:03:27 -0400 X-MC-Unique: 5IVsXTGcPj2WZrgQ8d4yNw-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-42725d3ae3eso20410355e9.3 for ; Sat, 20 Jul 2024 10:03:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495004; x=1722099804; 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=6wYVsr5dSSq/N/emvvls2jIOM9xtQeSUdpocp6Y/7Jo=; b=mZ4eTnJo7a8IMh8/7nPK4KJN/BSnTmHKQW3lwXe8YaxRLzgxa/AY0Kvvym6HRloiwV iERqMgbyHCnOFfRMm0+Hi8swC+qJ1XZAHMWdou7DoZj0/ZrnOuL+9FVqIpj1Q9x257Rq jY610FfnlFh7JqQGUonTBmxN4gH+WHVPuokJl/JbVroiJnZPKMZ9ka/hgp0hb9jqrDxc +fQ09e1bvxbxetiPzxzQ8AQNUbbuyLVna7Rl/SLtoCMUMeKOLUIg3jxctJp9bmwQfjEB Jdi7xH0BNKgmxHTJpqhwxcCqdTjTYvPnFL9pqzsSTni8qBT+9dCJ5l6zpnlQCi6A/ZOz andA== X-Gm-Message-State: AOJu0Yx8c0KScQ6Q3K3ScKB/Tp+WPINiLKdXGi8kJGrAme1hYCYHqWK1 HIadpg4kW/Ls7p2mABNL39J1NdmGmvdHObcL7cJXmygSBfh1PeXR0mCD4oBeF8epLl07lJkUQw+ 1E7Wy1HkfL++/re66J4dJj0wm385kRTnI7/agGG1qsd0O/0gwYjiSQBM7Q/+NZCy1xZO1+TZe6R 8QaR5gfC1DEUGomFFHZj90j+EA7E5drs8iUkwo47nfeJM= X-Received: by 2002:a05:600c:4f96:b0:426:5b22:4d61 with SMTP id 5b1f17b1804b1-427dc52547emr13503645e9.22.1721495004681; Sat, 20 Jul 2024 10:03:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHySmCr3LhgMI46JtSBWzMKWTjgvX65AaanylWRgXF5p31K03bZS+XQVnEnjhtSGwKwmUysAg== X-Received: by 2002:a05:600c:4f96:b0:426:5b22:4d61 with SMTP id 5b1f17b1804b1-427dc52547emr13503535e9.22.1721495004223; Sat, 20 Jul 2024 10:03:24 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d69073d5sm63163285e9.25.2024.07.20.10.03.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:22 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv12 1/6] gdb: make breakpoint_debug_printf global Date: Sat, 20 Jul 2024 18:03:14 +0100 Message-Id: <0b6eb9fb840dc82b6ef99e8e79f9571713b8203d.1721494871.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-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H4, RCVD_IN_MSPIKE_WL, 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 This commit makes breakpoint_debug_printf available outside of breakpoint.c. In a later commit I'll want to use this macro from another file. This is just a refactor, there should be no user visible changes after this commit. --- gdb/breakpoint.c | 9 ++------- gdb/breakpoint.h | 8 ++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 670a3f7acf7..4516d47e7a1 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -574,13 +574,8 @@ show_always_inserted_mode (struct ui_file *file, int from_tty, value); } -/* True if breakpoint debug output is enabled. */ -static bool debug_breakpoint = false; - -/* Print a "breakpoint" debug statement. */ -#define breakpoint_debug_printf(fmt, ...) \ - debug_prefixed_printf_cond (debug_breakpoint, "breakpoint", fmt, \ - ##__VA_ARGS__) +/* See breakpoint.h. */ +bool debug_breakpoint = false; /* "show debug breakpoint" implementation. */ static void diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h index ddc37196661..d6d42c705c6 100644 --- a/gdb/breakpoint.h +++ b/gdb/breakpoint.h @@ -46,6 +46,14 @@ struct linespec_result; struct linespec_sals; struct inferior; +/* True if breakpoint debug output is enabled. */ +extern bool debug_breakpoint; + +/* Print a "breakpoint" debug statement. */ +#define breakpoint_debug_printf(fmt, ...) \ + debug_prefixed_printf_cond (debug_breakpoint, "breakpoint", fmt, \ + ##__VA_ARGS__) + /* Enum for exception-handling support in 'catch throw', 'catch rethrow', 'catch catch' and the MI equivalent. */ From patchwork Sat Jul 20 17:03:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94257 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 0DB6D3860C36 for ; Sat, 20 Jul 2024 17:04:18 +0000 (GMT) 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 ESMTPS id 22A59385B82F for ; Sat, 20 Jul 2024 17:03:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22A59385B82F 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 22A59385B82F 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=1721495011; cv=none; b=dGWXpp73UQuxwIFLiEcdNypaG/KqpdZ4sVsQxp7hJdqNGdgtMta6/By9RLCQxmHy8n+0lgzhXv22opYFW6PFgtwWpt+MtAD5gtCFVEymotiI72PJOTXi1jL1Xf1tpfAjs3scwS4K1pVXU19ccGmLHOS7h9KmzN+eACehpD076EE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495011; c=relaxed/simple; bh=kCtIQjYOvaN9Ldf0PrTb3F8N/TZPT0vgCnsnW7DHdWQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ihG2tODiMbgGNKexToMQSadrzsM9RXi4vgPvWDsLIDePWuQeU6M3nk18um17r1re++EJjSUC5zFzmqWHcCfF9pj2Ao6JgJkuPhLD2mj7iikf0KopgBWkp1+fWFl8TrShMS05h8CKMhcbbKoLkzmZb67BWRpUBl5/zGN/elyi/RM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495009; 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=LgKtFOpjUVEPGsjxTEdDvisJj2JfoHfouWzF2LNmNQI=; b=NSAp7R6UtwdWKSq7WWAipl8/UUm7w0iXbOrNK/fozovVcH5xSP8VmMwlsW+ndnAp8Z+Sx0 URgc7UwodcamJ/0GtlE53rvFrHtsSYkPbgrwVfDnueVmlbjdrJ5CCLq3WFMGlcjuWEw0GZ tJJY1qAMKkXQhHTZwILAAmoKJ1/7YCA= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-J4SFS0ECPG6RXZkt87WiOg-1; Sat, 20 Jul 2024 13:03:28 -0400 X-MC-Unique: J4SFS0ECPG6RXZkt87WiOg-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-369bf135b49so218634f8f.0 for ; Sat, 20 Jul 2024 10:03:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495007; x=1722099807; 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=LgKtFOpjUVEPGsjxTEdDvisJj2JfoHfouWzF2LNmNQI=; b=ThJXccBhYkwc0IlkUkVEXV5tFNjqZaz43jOQCgqZ39CHb1tUAU820mcqKgqVYQU2ht 38wGpsBqKMtbNRddZ4PkqXsmPvejv/FePOiyQPAKvEGxV2K1Yrl6T4Ujfp3ytnWFPTlv JAfAwU2FvjKHe7CThNXwBsDHpJdnK4qjDLcmI6kmDO26ZTtWO0B/I69xBgAzt7L/l/Ov 3VcED3NII+DI0juyb9bLABYLShyoHS9jIUZVLkZqjNujqUIwacZ/gZTzoJHYBi+o3k60 BCUu0t2PhqOrryXkA1tRmESkWNisI2PLLWkeMgYf3XYIdLoy3tHg02MoMy/tAGqY4VOM wh4Q== X-Gm-Message-State: AOJu0YwujeyYtNz5ZHF9DWA+U6mbQDztIRzOZ1oYYo67fP1yj53Phs3p fdYNWzU9v+cZkbhZseJH5zb5ErZZOB0zOk//3GaXt2bfyt7FpLO8Wl3isFuolJE935+2fukGF9x 4EJ8MJS34BpSd1W8zaRAjPZHTV/F86E2uUSTaas8GmbHeMXBJedtZjkfbMPgS1XRBzGktV+om7K a2wBE6yCQV1zh62fEPGalab8Mcj88UgwVxYRqUt+y/A5I= X-Received: by 2002:a05:6000:2a1:b0:368:6bb:f79e with SMTP id ffacd0b85a97d-369b66a161dmr2246051f8f.4.1721495006792; Sat, 20 Jul 2024 10:03:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHM+LqNt3UKC42scMXjvegRScy92xtaFnsWkSXTx47jkIqTmBBa1atIccP/sa9HBR4TbrCj3Q== X-Received: by 2002:a05:6000:2a1:b0:368:6bb:f79e with SMTP id ffacd0b85a97d-369b66a161dmr2246035f8f.4.1721495006354; Sat, 20 Jul 2024 10:03:26 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-368787ebab0sm4301812f8f.92.2024.07.20.10.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:25 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv12 2/6] gdb: add another overload of startswith Date: Sat, 20 Jul 2024 18:03:15 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H3, RCVD_IN_MSPIKE_WL, 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 We already have one overload of the startswith function that takes a std::string_view for both arguments. A later patch in this series is going to be improved by having an overload that takes one argument as a std::string_view and the other argument as a plain 'char *'. This commit adds the new overload, but doesn't make use of it (yet). There should be no user visible changes after this commit. --- gdbsupport/common-utils.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h index 23cd40c0207..2fb22916409 100644 --- a/gdbsupport/common-utils.h +++ b/gdbsupport/common-utils.h @@ -100,6 +100,16 @@ startswith (std::string_view string, std::string_view pattern) && strncmp (string.data (), pattern.data (), pattern.length ()) == 0); } +/* Version of startswith that takes a string_view for only one of its + arguments. Return true if STR starts with PREFIX, otherwise return + false. */ + +static inline bool +startswith (const char *str, const std::string_view &prefix) +{ + return strncmp (str, prefix.data (), prefix.length ()) == 0; +} + /* Return true if the strings are equal. */ static inline bool From patchwork Sat Jul 20 17:03:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94258 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 E23523860C2E for ; Sat, 20 Jul 2024 17:04:29 +0000 (GMT) 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 ESMTPS id 7B767385DDC5 for ; Sat, 20 Jul 2024 17:03:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B767385DDC5 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 7B767385DDC5 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=1721495015; cv=none; b=MpCj/7pmD1HqeX3xd1SnV/Uo2VleN9uOY/ccwc4RmP35zMiotlIIF+hS+ji1khwl5vh6tACE77E6JvXHZSzGpclaupC1OCw9nEdYwPd4yr9+1nMJRa/GzxJ8hY+rwS2GgJcCf9pPFIozsdRwkH39vdT459mE8d72ssKW22pofr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495015; c=relaxed/simple; bh=jh23DrAGXRf/J5IJCgsGJgLTuXYsHcekPe+xhK61FZk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vvo30/nR5jIZ4O2qz7R2wOVgI1ZnTWQBb1vi0knxzlXyQ9PexWhovGgmBrCfI17MkMuRKTdRFBwvOy4sVy2yPnCHxcc4DhPHU/uSEyy9fnpuMkzUMQIiMfq29qM883NrbBg5yl4jes8uZQsOntnafIH6ZMVi02Kg26aGtG7HVYM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495012; 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=lpmME500ed2+Y8bv3sr9GAlMXXiFj/s+mBwz6x2HYKs=; b=RTltM1puORKNM2S1L40DTEUYFfOzvO3HwfWT4kIyeu7c49vmoNvXMP7ZkQA4SzraZCeYNl FlicVaV19XKh0WoEEU2K1iiTxcMlIu9P15ZxDKofFrFaEaeC3YWZ1fJQ7vB1yZMO82TuiI zE8zrrlzqp0almypWlE3xhiFJ2AKy7c= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-501-FZhNgYGyNFyN0X4dfGAcKw-1; Sat, 20 Jul 2024 13:03:30 -0400 X-MC-Unique: FZhNgYGyNFyN0X4dfGAcKw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42668857c38so20474245e9.1 for ; Sat, 20 Jul 2024 10:03:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495008; x=1722099808; 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=lpmME500ed2+Y8bv3sr9GAlMXXiFj/s+mBwz6x2HYKs=; b=vW3Bo80sX47vItlWn9dNyVMjJTcDJ6MpgC6PKswIaoyXfopWuEDniFSUrYsfb+9sSI UF0YlrL+EQ4xXcMpfDR16mfhiuzn6X1T5FyOukdD01mOC4PZwaS9wAMhVqlTlX9jn++E XaiuPkrVu13Bu+xdJc5/svi4YX8JlFtDLkHj9ns/zsKNQCr/RUzkprbUduFfe84xWp2X pa7aNrZp99FYCfWXQaxp/ZsnPGTTk/NAnTjSjlJxO98ceyXYvG6fqmzqgPMCgSX+Ym/L WcugwQ+JdGJzfOmg7QSGQrECV439iIrQ1mMFLISAHqbIudunDxDGXhbH9VG1PfdlFxt/ EQtA== X-Gm-Message-State: AOJu0YwVBHFNS/MSzhpPB+WFcivFryeN5zATmeEUlp+y0cLX2fTEmbcf 0IpvRENGD/Qd+653Wtnu7f4iZj+7lyqARqZ8RNcNWP7azdDE/sufvqekewRvnrpOnSri5RD05Dv e5uBFPE0kpzZFtwOfSXLa1QR+Ev+Ht3/rEo6vABmIYq4448dFfAaUlc5f9e15yaI9dLC1YbXli6 THrfVGsaHT9SDrWL4JqsVV8WuuYEJD7Eidb0K21fay4Rg= X-Received: by 2002:a05:600c:4fd0:b0:426:65bf:5cc2 with SMTP id 5b1f17b1804b1-427dc512d12mr12025125e9.1.1721495008372; Sat, 20 Jul 2024 10:03:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEiSaeJpCRyK2ZTs4GkJXlCsvXy50l9it85Nr3kZJYl1ErIBA+KnXQZBmMq9aqFwpvpBjU5MQ== X-Received: by 2002:a05:600c:4fd0:b0:426:65bf:5cc2 with SMTP id 5b1f17b1804b1-427dc512d12mr12024985e9.1.1721495007939; Sat, 20 Jul 2024 10:03:27 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d2a5bf8esm87718605e9.12.2024.07.20.10.03.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:27 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv12 3/6] gdb: create new is_thread_id helper function Date: Sat, 20 Jul 2024 18:03:16 +0100 Message-Id: <3b8497f9ce2a244c28ade552fd25039ca499262e.1721494871.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-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H3, RCVD_IN_MSPIKE_WL, 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 This is a refactoring commit that splits the existing parse_thread_id function into two parts, and then adds a new is_thread_id function. The core of parse_thread_id is split into parse_thread_id_1, which is responsible for actually parsing a thread-id. Then parse_thread_id is responsible for taking a parsed thread-id and validating that it references an actually existing inferior thread. The new is_thread_id function also uses parse_thread_id_1, but doesn't actually check that the inferior thread exists, instead, this new function simply checks that a string looks like a thread-id. This commit does not add a use for is_thread_id, this will be added in the next commit. This is a refactoring commit, there should be no user visible changes after this commit. --- gdb/tid-parse.c | 82 +++++++++++++++++++++++++++++++++++++------------ gdb/tid-parse.h | 8 +++++ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/gdb/tid-parse.c b/gdb/tid-parse.c index 1b8f343f080..442d5b33693 100644 --- a/gdb/tid-parse.c +++ b/gdb/tid-parse.c @@ -47,40 +47,43 @@ get_positive_number_trailer (const char **pp, int trailer, const char *string) return num; } -/* See tid-parse.h. */ +/* Parse TIDSTR as a per-inferior thread ID, in either INF_NUM.THR_NUM + or THR_NUM form, and return a pair, the first item of the pair is + INF_NUM and the second item is THR_NUM. -struct thread_info * -parse_thread_id (const char *tidstr, const char **end) + If TIDSTR does not include an INF_NUM component, then the first item in + the pair will be 0 (which is an invalid inferior number), this indicates + that TIDSTR references the current inferior. + + This function does not validate the INF_NUM and THR_NUM are actually + valid numbers, that is, they might reference inferiors or threads that + don't actually exist; this function just splits the string into its + component parts. + + If there is an error parsing TIDSTR then this function will raise an + exception. */ + +static std::pair +parse_thread_id_1 (const char *tidstr, const char **end) { const char *number = tidstr; const char *dot, *p1; - struct inferior *inf; - int thr_num; - int explicit_inf_id = 0; + int thr_num, inf_num; dot = strchr (number, '.'); if (dot != NULL) { /* Parse number to the left of the dot. */ - int inf_num; - p1 = number; inf_num = get_positive_number_trailer (&p1, '.', number); if (inf_num == 0) invalid_thread_id_error (number); - - inf = find_inferior_id (inf_num); - if (inf == NULL) - error (_("No inferior number '%d'"), inf_num); - - explicit_inf_id = 1; p1 = dot + 1; } else { - inf = current_inferior (); - + inf_num = 0; p1 = number; } @@ -88,6 +91,32 @@ parse_thread_id (const char *tidstr, const char **end) if (thr_num == 0) invalid_thread_id_error (number); + if (end != nullptr) + *end = p1; + + return { inf_num, thr_num }; +} + +/* See tid-parse.h. */ + +struct thread_info * +parse_thread_id (const char *tidstr, const char **end) +{ + const auto [inf_num, thr_num] = parse_thread_id_1 (tidstr, end); + + inferior *inf; + bool explicit_inf_id = false; + + if (inf_num != 0) + { + inf = find_inferior_id (inf_num); + if (inf == nullptr) + error (_("No inferior number '%d'"), inf_num); + explicit_inf_id = true; + } + else + inf = current_inferior (); + thread_info *tp = nullptr; for (thread_info *it : inf->threads ()) if (it->per_inf_num == thr_num) @@ -96,7 +125,7 @@ parse_thread_id (const char *tidstr, const char **end) break; } - if (tp == NULL) + if (tp == nullptr) { if (show_inferior_qualified_tids () || explicit_inf_id) error (_("Unknown thread %d.%d."), inf->num, thr_num); @@ -104,14 +133,27 @@ parse_thread_id (const char *tidstr, const char **end) error (_("Unknown thread %d."), thr_num); } - if (end != NULL) - *end = p1; - return tp; } /* See tid-parse.h. */ +bool +is_thread_id (const char *tidstr, const char **end) +{ + try + { + (void) parse_thread_id_1 (tidstr, end); + return true; + } + catch (const gdb_exception_error &) + { + return false; + } +} + +/* See tid-parse.h. */ + tid_range_parser::tid_range_parser (const char *tidlist, int default_inferior) { diff --git a/gdb/tid-parse.h b/gdb/tid-parse.h index 7bbdeabe85b..0ea58bb552d 100644 --- a/gdb/tid-parse.h +++ b/gdb/tid-parse.h @@ -36,6 +36,14 @@ struct thread_info; thrown. */ struct thread_info *parse_thread_id (const char *tidstr, const char **end); +/* Return true if TIDSTR is pointing to a string that looks like a + thread-id. This doesn't mean that TIDSTR identifies a valid thread, but + the string does at least look like a valid thread-id. If END is not + NULL, parse_thread_id stores the address of the first character after + the thread-id into *END. */ + +bool is_thread_id (const char *tidstr, const char **end); + /* Parse a thread ID or a thread range list. A range will be of the form From patchwork Sat Jul 20 17:03:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94261 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 7C8F6385DDF0 for ; Sat, 20 Jul 2024 17:05:22 +0000 (GMT) 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 ESMTPS id 7C090385840D for ; Sat, 20 Jul 2024 17:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C090385840D 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 7C090385840D 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=1721495024; cv=none; b=A5LAGTxvLkWgKhIqwuJC/hrkgEwHWvxrCApn9JKdxrQnNJKDMt9VRAqduhG8ymU9zdpC1vS9Y6E2Yvb9GmRKw82hUi7hj0s0P4YrSHPxkkMYYoFBHAw2pI2bOFiZB7aqvb5jEG41EYZ6M4cfh7sO8mzE+O8GHQMATEO9b6NILzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495024; c=relaxed/simple; bh=QxDEDpciGpALhTi0LQQkhhPYaVsiuV2hb3Z5Na7orQ0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rulKX9d3QbtTgu8clXg9it/LuP+cAcIxg46r0p3gDQcjcKePrB1la7GauGe50RgcLm/w0R0yTDhYYarGr7raZtfNagr+QPINKFQTq/fGbywnuYvxRvAWT/ICwLd6ePPvp2ZDf6w9dQGBEPrAghYKpNIk2cNEt6IN7Y5OfVLKUpA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495017; 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=tMZ2k17NzyPMteJaGxmlGMwSjKjOq6ZAFm6oy2q7oXY=; b=CWgWn0vTWi58s+RPt4Llq4gHLJwtVspSQjfKp+nwsfY1NrGSgg4YgjNJqGfM7euRVpLle7 PrFfJzlxsD0AOFQK1PDIbjNXT8vVkN1so0F/hHom/qYWMIIdtBCEwKTLdHikQ8bdKe4HnJ sN8Svc1z4ydgUC0OyL+EkXrmI0IN0es= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-253-p0xMJZO0PeSzyXzn8vpN-g-1; Sat, 20 Jul 2024 13:03:35 -0400 X-MC-Unique: p0xMJZO0PeSzyXzn8vpN-g-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42668796626so21123255e9.0 for ; Sat, 20 Jul 2024 10:03:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495014; x=1722099814; 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=tMZ2k17NzyPMteJaGxmlGMwSjKjOq6ZAFm6oy2q7oXY=; b=nXmX2t94i1+kCv7ld0pyP7ow032xdkSsgdCAbQ/WQp9Kxig4hEjnVkEqXxQPjPX3x8 IK3lRtX0d+iQuLhyTsCuTPJqTOCsoRc+DxnS4ChKZvOLPKwY6MaTMzqOjQSNwrXFT99A pjI6nDmG8Pgncmp9yuuTgW73nJlQJcdACFz56pFuJ5k7T5RddjzFzrLPIl9s64JYG7Zt t1T8p3+dkEKIU+cvQd92H41XgntgIET8aJDz8bKU0N9ubkZwiYXSaj48PxX4lDlP36qj aKEbLDIP73yKbKcOkPVWoqbfZepXIjjcRfOEEwUgWrz+903pVs20Rwok4mfx9TkBn2sa WMgQ== X-Gm-Message-State: AOJu0YyOhVrCzz0x92GvUK9bdIE1N6TEQQ84dbuVsQNma86FlWUYHUn4 t4sdoZ9OETpoqUqRPtfbcFVJ5bz1/eeiKut9qT8BWZ8PNBwJ/UcsEuDhMQJf7llQMHdg3wirxVA FDXtx3WOtqf1KWEvdJ6o12zNoDsmCf0NWoo22En6muhHyXTBeoGq/UN2lWWMRdNmHQU4wuEgcry 7bNUh8WyBRJReUOvNVuo39HmPPcrTgmpsmtRWIv1A5U0k= X-Received: by 2002:a05:6000:dc8:b0:368:3b21:6643 with SMTP id ffacd0b85a97d-369bb2eaf53mr1385426f8f.48.1721495012554; Sat, 20 Jul 2024 10:03:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFur+KVLR2dodP/kQ3FY5EJAwO+RY61YXqKpSyNAfMhab9c1a9th5mzS+Q/iWDVl5rnYZMiog== X-Received: by 2002:a05:6000:dc8:b0:368:3b21:6643 with SMTP id ffacd0b85a97d-369bb2eaf53mr1385378f8f.48.1721495010398; Sat, 20 Jul 2024 10:03:30 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-368787ebd50sm4260220f8f.93.2024.07.20.10.03.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:28 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv12 4/6] gdb: parse pending breakpoint thread/task immediately Date: Sat, 20 Jul 2024 18:03:17 +0100 Message-Id: <2bc9c004977ef070289f0659b1baae78c332c05d.1721494871.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-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 The initial motivation for this commit was to allow thread or inferior specific breakpoints to only be inserted within the appropriate inferior's program-space. The benefit of this is that inferiors for which the breakpoint does not apply will no longer need to stop, and then resume, for such breakpoints. This commit does not make this change, but is a refactor to allow this to happen in a later commit. The problem we currently have is that when a thread-specific (or inferior-specific) breakpoint is created, the thread (or inferior) number is only parsed by calling find_condition_and_thread_for_sals. This function is only called for non-pending breakpoints, and requires that we know the locations at which the breakpoint will be placed (for expression checking in case the breakpoint is also conditional). A consequence of this is that by the time we figure out the breakpoint is thread-specific we have already looked up locations in all program spaces. This feels wasteful -- if we knew the thread-id earlier then we could reduce the work GDB does by only looking up locations within the program space for which the breakpoint applies. Another consequence of how find_condition_and_thread_for_sals is called is that pending breakpoints don't currently know they are thread-specific, nor even that they are conditional! Additionally, by delaying parsing the thread-id, pending breakpoints can be created for non-existent threads, this is different to how non-pending breakpoints are handled, so I can do this: $ gdb -q ./gdb/testsuite/outputs/gdb.multi/pending-bp/pending-bp Reading symbols from ./gdb/testsuite/outputs/gdb.multi/pending-bp/pending-bp... (gdb) break foo thread 99 Function "foo" not defined. Make breakpoint pending on future shared library load? (y or [n]) y Breakpoint 1 (foo thread 99) pending. (gdb) r Starting program: /tmp/gdb/testsuite/outputs/gdb.multi/pending-bp/pending-bp [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Error in re-setting breakpoint 1: Unknown thread 99. [Inferior 1 (process 3329749) exited normally] (gdb) GDB only checked the validity of 'thread 99' at the point the 'foo' location became non-pending. In contrast, if I try this: $ gdb -q ./gdb/testsuite/outputs/gdb.multi/pending-bp/pending-bp Reading symbols from ./gdb/testsuite/outputs/gdb.multi/pending-bp/pending-bp... (gdb) break main thread 99 Unknown thread 99. (gdb) GDB immediately checks if 'thread 99' exists. I think inconsistencies like this are confusing, and should be fixed if possible. In this commit the create_breakpoint function is updated so that the extra_string, which contains the thread, inferior, task, and/or condition information, is parsed immediately, even for pending breakpoints. Obviously, the condition still can't be validated until the breakpoint becomes non-pending, but the thread, inferior, and task information can be pulled from the extra-string, and can be validated early on, even for pending breakpoints. The -force-condition flag is also parsed as part of this early parsing change. There are a couple of benefits to doing this: 1. Printing of breakpoints is more consistent now. Consider creating a conditional breakpoint before this commit: (gdb) set breakpoint pending on (gdb) break pendingfunc if (0) Function "pendingfunc" not defined. Breakpoint 1 (pendingfunc if (0)) pending. (gdb) break main if (0) Breakpoint 2 at 0x401198: file /tmp/hello.c, line 18. (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y pendingfunc if (0) 2 breakpoint keep y 0x0000000000401198 in main at /tmp/hello.c:18 stop only if (0) (gdb) And after this commit: (gdb) set breakpoint pending on (gdb) break pendingfunc if (0) Function "pendingfunc" not defined. Breakpoint 1 (pendingfunc) pending. (gdb) break main if (0) Breakpoint 2 at 0x401198: file /home/andrew/tmp/hello.c, line 18. (gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y pendingfunc stop only if (0) 2 breakpoint keep y 0x0000000000401198 in main at /home/andrew/tmp/hello.c:18 stop only if (0) (gdb) Notice that the display of the condition is now the same for the pending and non-pending breakpoints. The same is true for the thread, inferior, or task information in thread, inferior, or task specific breakpoints; this information is displayed on its own line rather than being part of the 'What' field. 2. We can check that the thread exists as soon as the pending breakpoint is created. Currently there is a weird difference between pending and non-pending breakpoints when creating a thread-specific breakpoint. A pending thread-specific breakpoint only checks its thread when it becomes non-pending, at which point the thread the breakpoint was intended for might have exited. Here's the behaviour before this commit: (gdb) set breakpoint pending on (gdb) break foo thread 2 Function "foo" not defined. Breakpoint 2 (foo thread 2) pending. (gdb) c Continuing. [Thread 0x7ffff7c56700 (LWP 2948835) exited] Error in re-setting breakpoint 2: Unknown thread 2. [Inferior 1 (process 2948832) exited normally] (gdb) Notice the 'Error in re-setting breakpoint 2: Unknown thread 2.' line, this was triggered when GDB tried to make the breakpoint non-pending, and GDB discovers that the thread no longer exists. Compare that to the behaviour after this commit: (gdb) set breakpoint pending on (gdb) break foo thread 2 Function "foo" not defined. Breakpoint 2 (foo) pending. (gdb) c Continuing. [Thread 0x7ffff7c56700 (LWP 2949243) exited] Thread-specific breakpoint 2 deleted - thread 2 no longer in the thread list. [Inferior 1 (process 2949240) exited normally] (gdb) Now the behaviour for pending breakpoints is identical to non-pending breakpoints, the thread specific breakpoint is removed as soon as the thread the breakpoint is associated with exits. There is an additional change; when the pending breakpoint is created prior to this patch we see this line: Breakpoint 2 (foo thread 2) pending. While after this patch we get this line: Breakpoint 2 (foo) pending. Notice that 'thread 2' has disappeared. This might look like a regression, but I don't think it is. That we said 'thread 2' before was just a consequence of the lazy parsing of the breakpoint specification, while with this patch GDB understands, and has parsed away the 'thread 2' bit of the spec. If folk think the old information was useful then this would be trivial to add back in code_breakpoint::say_where. As a result of this commit the breakpoints 'extra_string' field is now only used by bp_dprintf type breakpoints to hold the printf format and arguments. This string should always be empty for other breakpoint types. This allows some cleanup in print_breakpoint_location. In code_breakpoint::code_breakpoint I've changed an error case into an assert. This is because the error is now handled earlier in create_breakpoint. As a result we know that by this point, the extra_string will always be nullptr for anything other than a bp_dprintf style breakpoint. The find_condition_and_thread_for_sals function is now no longer needed, this was previously doing the delayed splitting of the extra string into thread, task, and condition, but this is now all done in create_breakpoint, so find_condition_and_thread_for_sals can be deleted, and the code that calls this in code_breakpoint::location_spec_to_sals can be removed. With this update this code would only ever be reached for bp_dprintf style breakpoints, and in these cases the extra_string should not contain anything other than format and args. The most interesting changes are all in create_breakpoint and in the new file break-cond-parse.c. We have a new block of code early on in create_breakpoint that is responsible for splitting the extra_string into its component parts by calling create_breakpoint_parse_arg_string a function in the new break-cond-parse.c file. This means that some of the later code can be simplified a little. The new break-cond-parse.c file implements the splitting up the extra_string and finding all the parts, as well as some self-tests for the new function. Finally, now we know all the breakpoint details, these can be stored within the breakpoint object if we end up creating a deferred breakpoint. Additionally, if we are creating a deferred bp_dprintf we can parse the extra_string to build the printf command. The implementation here aims to maintain backwards compatibility as much as possible, this means that: 1. We support abbreviations of 'thread', 'task', and 'inferior' in some places on the breakpoint line. The handling of abbreviations has (before this patch) been a little weird, so this works: (gdb) break *main th 1 And creates a breakpoint at '*main' for thread 1 only, while this does not work: (gdb) break main th 1 In this case GDB will try to find the symbol 'main th 1'. This weirdness exists before and after this patch. 2. The handling of '-force-condition' is odd, if this flag appears immediately after a condition then it will be treated as part of the condition, e.g.: (gdb) break main if 0 -force-condition No symbol "force" in current context. But we are fine with these alternatives: (gdb) break main if 0 thread 1 -force-condition (gdb) break main -force-condition if 0 Again, this is just a quirk of how the breakpoint line used to be parsed, but I've maintained this for backward compatibility. During review it was suggested that -force-condition should become an actual breakpoint flag (i.e. only valid after the 'break' command but before the function name), and I don't think that would be a terrible idea, however, that's not currently a trivial change, and I think should be done as a separate piece of work. For now, this patch just maintains the current behaviour. The implementation works by first splitting the breakpoint condition string (everything after the location specification) into a list of tokens, each token has a type and a value. (e.g. we have a THREAD token where the value is the thread-id string). The list of tokens is validated, and in some cases, tokens are merged. Then the values are extracted from the remaining token list. Consider this breakpoint command: (gdb) break main thread 1 if argc == 2 The condition string passed to create_breakpoint_parse_arg_string is going to be 'thread 1 if argc == 2', which is then split into the tokens: { THREAD: "1" } { CONDITION: "argc == 2" } The thread-id (1) and the condition string 'argc == 2' are extracted from these tokens and returns back to create_breakpoint. Now consider this breakpoint command: (gdb) break some_function if ( some_var == thread ) Here the user wants a breakpoint if 'some_var' is equal to the variable 'thread'. However, when this is initially parsed we will find these tokens: { CONDITION: "( some_var == " } { THREAD: ")" } This is a consequence of how we have to try and figure out the contents of the 'if' condition without actually parsing the expression; parsing the expression requires that we know the location in order to lookup the variables by name, and this can't be done for pending breakpoints (their location isn't known yet), and one of the points of this work is that we extract things like thread-id for pending breakpoints. And so, it is in this case that token merging takes place. We check if the value of a token appearing immediately after the CONDITION token looks valid. In this case, does ')' look like a valid thread-id. Clearly, in this case ')' does not, and so me merge the THREAD token into the condition token, giving: { CONDITION: "( some_var == thread )" } Which is what we want. I'm sure that we might still be able to come up with some edge cases where the parser makes the wrong choice. I think long term the best way to work around these would be to move the thread, inferior, task, and -force-condition flags to be "real" command options for the break command. I am looking into doing this, but can't guarantee if/when that work would be completed, so this patch should be reviewed assume that the work will never arrive (though I hope it will). Reviewed-By: Eli Zaretskii Reviewed-By: Eli Zaretskii --- gdb/Makefile.in | 2 + gdb/NEWS | 4 + gdb/break-cond-parse.c | 702 ++++++++++++++++++ gdb/break-cond-parse.h | 52 ++ gdb/breakpoint.c | 372 ++-------- gdb/testsuite/gdb.ada/tasks.exp | 6 +- gdb/testsuite/gdb.base/condbreak.exp | 57 +- gdb/testsuite/gdb.base/pending.exp | 30 +- gdb/testsuite/gdb.linespec/explicit.exp | 14 +- gdb/testsuite/gdb.linespec/keywords.exp | 8 +- gdb/testsuite/gdb.mi/mi-dprintf-pending.exp | 3 +- .../gdb.multi/inferior-specific-bp.exp | 4 +- .../gdb.threads/del-pending-thread-bp-lib.c | 22 + .../gdb.threads/del-pending-thread-bp.c | 85 +++ .../gdb.threads/del-pending-thread-bp.exp | 98 +++ 15 files changed, 1152 insertions(+), 307 deletions(-) create mode 100644 gdb/break-cond-parse.c create mode 100644 gdb/break-cond-parse.h create mode 100644 gdb/testsuite/gdb.threads/del-pending-thread-bp-lib.c create mode 100644 gdb/testsuite/gdb.threads/del-pending-thread-bp.c create mode 100644 gdb/testsuite/gdb.threads/del-pending-thread-bp.exp diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 1c697bf0ab1..afbb449a941 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1052,6 +1052,7 @@ COMMON_SFILES = \ break-catch-sig.c \ break-catch-syscall.c \ break-catch-throw.c \ + break-cond-parse.c \ breakpoint.c \ bt-utils.c \ btrace.c \ @@ -1321,6 +1322,7 @@ HFILES_NO_SRCDIR = \ bfd-target.h \ bfin-tdep.h \ block.h \ + break-cond-parse.h \ breakpoint.h \ bsd-kvm.h \ bsd-uthread.h \ diff --git a/gdb/NEWS b/gdb/NEWS index b56ba9b36ce..f140a28b795 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -28,6 +28,10 @@ the background, resulting in faster startup. This can be controlled using "maint set dwarf synchronous". +* For breakpoints that are created in the 'pending' state, any + 'thread' or 'task' keywords are parsed at the time the breakpoint is + created, rather than at the time the breakpoint becomes non-pending. + * Changed commands disassemble diff --git a/gdb/break-cond-parse.c b/gdb/break-cond-parse.c new file mode 100644 index 00000000000..f5fe308a923 --- /dev/null +++ b/gdb/break-cond-parse.c @@ -0,0 +1,702 @@ +/* Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 "defs.h" +#include "gdbsupport/gdb_assert.h" +#include "gdbsupport/selftest.h" +#include "test-target.h" +#include "scoped-mock-context.h" +#include "break-cond-parse.h" +#include "tid-parse.h" +#include "ada-lang.h" +#include "exceptions.h" + +/* When parsing tokens from a string, which direction are we parsing? + + Given the following string and pointer 'ptr': + + ABC DEF GHI JKL + ^ + ptr + + Parsing 'forward' will return the token 'GHI' and update 'ptr' to point + between GHI and JKL. Parsing 'backward' will return the token 'DEF' and + update 'ptr' to point between ABC and DEF. +*/ + +enum class parse_direction +{ + /* Parse the next token forwards. */ + forward, + + /* Parse the previous token backwards. */ + backward +}; + +/* Find the next token in DIRECTION from *CURR. */ + +static std::string_view +find_next_token (const char **curr, parse_direction direction) +{ + const char *tok_start, *tok_end; + + gdb_assert (**curr != '\0'); + + if (direction == parse_direction::forward) + { + *curr = skip_spaces (*curr); + tok_start = *curr; + *curr = skip_to_space (*curr); + tok_end = *curr - 1; + } + else + { + gdb_assert (direction == parse_direction::backward); + + while (isspace (**curr)) + --(*curr); + + tok_end = *curr; + + while (!isspace (**curr)) + --(*curr); + + tok_start = (*curr) + 1; + } + + return std::string_view (tok_start, tok_end - tok_start + 1); +} + +/* A class that represents a complete parsed token. Each token has a type + and a std::string_view into the original breakpoint condition string. */ + +struct token +{ + /* The types a token might take. */ + enum class type + { + /* These are the token types for the 'if', 'thread', 'inferior', and + 'task' keywords. The m_content for these token types is the value + passed to the keyword, not the keyword itself. */ + CONDITION, + THREAD, + INFERIOR, + TASK, + + /* This is the token used when we find unknown content, the m_content + for this token is the rest of the input string. */ + REST, + + /* This is the token for the -force-condition token, the m_content for + this token contains the keyword itself. */ + FORCE + }; + + token (enum type type, std::string_view content) + : m_type (type), + m_content (std::move (content)) + { + /* Nothing. */ + } + + /* Return a string representing this token. Only used for debug. */ + std::string to_string () const + { + switch (m_type) + { + case type::CONDITION: + return string_printf ("{ CONDITION: \"%s\" }", + std::string (m_content).c_str ()); + case type::THREAD: + return string_printf ("{ THREAD: \"%s\" }", + std::string (m_content).c_str ()); + case type::INFERIOR: + return string_printf ("{ INFERIOR: \"%s\" }", + std::string (m_content).c_str ()); + case type::TASK: + return string_printf ("{ TASK: \"%s\" }", + std::string (m_content).c_str ()); + case type::REST: + return string_printf ("{ REST: \"%s\" }", + std::string (m_content).c_str ()); + case type::FORCE: + return string_printf ("{ FORCE }"); + default: + return "** unknown **"; + } + } + + /* The type of this token. */ + const type &get_type () const + { + return m_type; + } + + /* Return the value of this token. */ + const std::string_view &get_value () const + { + gdb_assert (m_content.size () > 0); + return m_content; + } + + /* Extend this token with the contents of OTHER. This only makes sense + if OTHER is the next token after this one in the original string, + however, enforcing that restriction is left to the caller of this + function. + + When OTHER is a keyword/value token, e.g. 'thread 1', the m_content + for OTHER will only point to the '1'. However, as the m_content is a + std::string_view, then when we merge the m_content of OTHER into this + token we automatically merge in the 'thread' part too, as it + naturally sits between this token and OTHER. */ + + void + extend (const token &other) + { + m_content = std::string_view (this->m_content.data (), + (other.m_content.data () + - this->m_content.data () + + other.m_content.size ())); + } + +private: + /* The type of this token. */ + type m_type; + + /* The important content part of this token. The extend member function + depends on this being a std::string_view. */ + std::string_view m_content; +}; + +/* Split STR, a breakpoint condition string, into a vector of tokens where + each token represents a component of the condition. Tokens are first + parsed from the front of STR until we encounter an 'if' token. At this + point tokens are parsed from the end of STR until we encounter an + unknown token, which we assume is the other end of the 'if' condition. + If when scanning forward we encounter an unknown token then the + remainder of STR is placed into a 'rest' token (the rest of the + string), and no backward scan is performed. */ + +static std::vector +parse_all_tokens (const char *str) +{ + gdb_assert (str != nullptr); + + std::vector forward_results; + std::vector backward_results; + + const char *cond_start = nullptr; + const char *cond_end = nullptr; + parse_direction direction = parse_direction::forward; + std::vector *curr_results = &forward_results; + while (*str != '\0') + { + /* Find the next token. If moving backward and this token starts at + the same location as the condition then we must have found the + other end of the condition string -- we're done. */ + std::string_view t = find_next_token (&str, direction); + if (direction == parse_direction::backward && t.data () <= cond_start) + { + cond_end = &t.back (); + break; + } + + /* We only have a single flag option to check for. All the other + options take a value so require an additional token to be found. + Additionally, we require that this flag be at least '-f', we + don't allow it to be abbreviated to '-'. */ + if (t.length () > 1 && startswith ("-force-condition", t)) + { + curr_results->emplace_back (token::type::FORCE, t); + continue; + } + + /* Maybe the first token was the last token in the string. If this + is the case then we definitely can't try to extract a value + token. This also means that the token T is meaningless. Reset + TOK to point at the start of the unknown content and break out of + the loop. We'll record the unknown part of the string outside of + the scanning loop (below). */ + if (direction == parse_direction::forward && *str == '\0') + { + str = t.data (); + break; + } + + /* As before, find the next token and, if we are scanning backwards, + check that we have not reached the start of the condition string. */ + std::string_view v = find_next_token (&str, direction); + if (direction == parse_direction::backward && v.data () <= cond_start) + { + /* Use token T here as that must also be part of the condition + string. */ + cond_end = &t.back (); + break; + } + + /* When moving backward we will first parse the value token then the + keyword token, so swap them now. */ + if (direction == parse_direction::backward) + std::swap (t, v); + + /* Check for valid option in token T. If we find a valid option then + parse the value from the token V. Except for 'if', that's handled + differently. + + For the 'if' token we need to capture the entire condition + string, so record the start of the condition string and then + start scanning backwards looking for the end of the condition + string. + + The order of these checks is important, at least the check for + 'thread' must occur before the check for 'task'. We accept + abbreviations of these token names, and 't' should resolve to + 'thread', which will only happen if we check 'thread' first. */ + if (direction == parse_direction::forward && startswith ("if", t)) + { + cond_start = v.data (); + str = str + strlen (str); + gdb_assert (*str == '\0'); + --str; + direction = parse_direction::backward; + curr_results = &backward_results; + continue; + } + else if (startswith ("thread", t)) + curr_results->emplace_back (token::type::THREAD, v); + else if (startswith ("inferior", t)) + curr_results->emplace_back (token::type::INFERIOR, v); + else if (startswith ("task", t)) + curr_results->emplace_back (token::type::TASK, v); + else + { + /* An unknown token. If we are scanning forward then reset TOK + to point at the start of the unknown content, we record this + outside of the scanning loop (below). + + If we are scanning backward then unknown content is assumed to + be the other end of the condition string, obviously, this is + just a heuristic, we could be looking at a mistyped command + line, but this will be spotted when the condition is + eventually evaluated. + + Either way, no more scanning is required after this. */ + if (direction == parse_direction::forward) + str = t.data (); + else + { + gdb_assert (direction == parse_direction::backward); + cond_end = &v.back (); + } + break; + } + } + + if (cond_start != nullptr) + { + /* If we found the start of a condition string then we should have + switched to backward scan mode, and found the end of the condition + string. Capture the whole condition string into COND_STRING + now. */ + gdb_assert (direction == parse_direction::backward); + gdb_assert (cond_end != nullptr); + + std::string_view v (cond_start, cond_end - cond_start + 1); + + forward_results.emplace_back (token::type::CONDITION, v); + } + else if (*str != '\0') + { + /* If we didn't have a condition start pointer then we should still + be in forward scanning mode. If we didn't reach the end of the + input string (TOK is not at the null character) then the rest of + the input string is garbage that we didn't understand. + + Record the unknown content into REST. The caller of this function + will report this as an error later on. We could report the error + here, but we prefer to allow the caller to run other checks, and + prioritise other errors before reporting this problem. */ + gdb_assert (direction == parse_direction::forward); + gdb_assert (cond_end == nullptr); + + std::string_view v (str, strlen (str)); + + forward_results.emplace_back (token::type::REST, v); + } + + /* If we have tokens in the BACKWARD_RESULTS vector then this means that + we found an 'if' condition (which will be the last thing in the + FORWARD_RESULTS vector), and then we started a backward scan. + + The last tokens from the input string (those after the 'if' condition) + will be the first tokens added to the BACKWARD_RESULTS vector, so the + last items in the BACKWARD_RESULTS vector are those next to the 'if' + condition. + + Check the tokens in the BACKWARD_RESULTS vector from back to front. + If the tokens look invalid then we assume that they are actually part + of the 'if' condition, and merge the token with the 'if' condition. + If it turns out that this was incorrect and that instead the user just + messed up entering the token value, then this will show as an error + when parsing the 'if' condition. + + Doing this allows us to handle things like: + + break function if ( variable == thread ) + + Where 'thread' is a local variable within 'function'. When parsing + this we will initially see 'thread )' as a thread token with ')' as + the value. However, the following code will spot that ')' is not a + valid thread-id, and so we merge 'thread )' into the 'if' condition + string. + + This code also handles the special treatment for '-force-condition', + which exists for backwards compatibility reasons. Traditionally this + flag, if it occurred immediately after the 'if' condition, would be + treated as part of the 'if' condition. When the breakpoint condition + parsing code was rewritten, this behaviour was retained. */ + gdb_assert (backward_results.empty () + || (forward_results.back ().get_type () + == token::type::CONDITION)); + while (!backward_results.empty ()) + { + token &t = backward_results.back (); + + if (t.get_type () == token::type::FORCE) + forward_results.back ().extend (std::move (t)); + else if (t.get_type () == token::type::THREAD) + { + const char *end; + std::string v (t.get_value ()); + if (is_thread_id (v.c_str (), &end) && *end == '\0') + break; + forward_results.back ().extend (std::move (t)); + } + else if (t.get_type () == token::type::INFERIOR + || t.get_type () == token::type::TASK) + { + /* Place the token's value into a null-terminated string, parse + the string as a number and check that the entire string was + parsed. If this is true then this looks like a valid inferior + or task number, otherwise, assume an invalid id, and merge + this token with the 'if' token. */ + char *end; + std::string v (t.get_value ()); + (void) strtol (v.c_str (), &end, 0); + if (end > v.c_str () && *end == '\0') + break; + forward_results.back ().extend (std::move (t)); + } + else + gdb_assert_not_reached ("unexpected token type"); + + /* If we found an actual valid token above then we will have broken + out of the loop. We only get here if the token was merged with + the 'if' condition, in which case we can discard the last token + and then check the token before that. */ + backward_results.pop_back (); + } + + /* If after the above checks we still have some tokens in the + BACKWARD_RESULTS vector, then these need to be appended to the + FORWARD_RESULTS vector. However, we first reverse the order so that + FORWARD_RESULTS retains the tokens in the order they appeared in the + input string. */ + if (!backward_results.empty ()) + forward_results.insert (forward_results.end (), + backward_results.rbegin (), + backward_results.rend ()); + + return forward_results; +} + +/* Called when the global debug_breakpoint is true. Prints VEC to the + debug output stream. */ + +static void +dump_condition_tokens (const std::vector &vec) +{ + gdb_assert (debug_breakpoint); + + bool first = true; + std::string str = "Tokens: "; + for (const token &t : vec) + { + if (!first) + str += " "; + first = false; + str += t.to_string (); + } + breakpoint_debug_printf ("%s", str.c_str ()); +} + +/* See break-cond-parse.h. */ + +void +create_breakpoint_parse_arg_string + (const char *str, gdb::unique_xmalloc_ptr *cond_string_ptr, + int *thread_ptr, int *inferior_ptr, int *task_ptr, + gdb::unique_xmalloc_ptr *rest_ptr, bool *force_ptr) +{ + /* Set up the defaults. */ + cond_string_ptr->reset (); + rest_ptr->reset (); + *thread_ptr = -1; + *inferior_ptr = -1; + *task_ptr = -1; + *force_ptr = false; + + if (str == nullptr) + return; + + /* Split STR into a series of tokens. */ + std::vector tokens = parse_all_tokens (str); + if (debug_breakpoint) + dump_condition_tokens (tokens); + + /* Temporary variables. Initialised to the default state, then updated + as we parse TOKENS. If all of TOKENS is parsed successfully then the + state from these variables is copied into the output arguments before + the function returns. */ + int thread = -1, inferior = -1, task = -1; + bool force = false; + gdb::unique_xmalloc_ptr cond_string, rest; + + for (const token &t : tokens) + { + switch (t.get_type ()) + { + case token::type::FORCE: + force = true; + break; + case token::type::THREAD: + { + if (thread != -1) + error ("You can specify only one thread."); + if (task != -1 || inferior != -1) + error ("You can specify only one of thread, inferior, or task."); + const char *tmptok; + thread_info *thr + = parse_thread_id (std::string (t.get_value ()).c_str (), + &tmptok); + gdb_assert (*tmptok == '\0'); + thread = thr->global_num; + } + break; + case token::type::INFERIOR: + { + if (inferior != -1) + error ("You can specify only one inferior."); + if (task != -1 || thread != -1) + error ("You can specify only one of thread, inferior, or task."); + char *tmptok; + long inferior_id + = strtol (std::string (t.get_value ()).c_str (), &tmptok, 0); + if (*tmptok != '\0') + error (_("Junk '%s' after inferior keyword."), tmptok); + if (inferior_id > INT_MAX) + error (_("No inferior number '%ld'"), inferior_id); + inferior = static_cast (inferior_id); + struct inferior *inf = find_inferior_id (inferior); + if (inf == nullptr) + error (_("No inferior number '%d'"), inferior); + } + break; + case token::type::TASK: + { + if (task != -1) + error ("You can specify only one task."); + if (inferior != -1 || thread != -1) + error ("You can specify only one of thread, inferior, or task."); + char *tmptok; + long task_id + = strtol (std::string (t.get_value ()).c_str (), &tmptok, 0); + if (*tmptok != '\0') + error (_("Junk '%s' after task keyword."), tmptok); + if (task_id > INT_MAX) + error (_("Unknown task %ld"), task_id); + task = static_cast (task_id); + if (!valid_task_id (task)) + error (_("Unknown task %d."), task); + } + break; + case token::type::CONDITION: + cond_string.reset (savestring (t.get_value ().data (), + t.get_value ().size ())); + break; + case token::type::REST: + rest.reset (savestring (t.get_value ().data (), + t.get_value ().size ())); + break; + } + } + + /* Move results into the output locations. */ + *force_ptr = force; + *thread_ptr = thread; + *inferior_ptr = inferior; + *task_ptr = task; + rest_ptr->reset (rest.release ()); + cond_string_ptr->reset (cond_string.release ()); +} + +#if GDB_SELF_TEST + +namespace selftests { + +/* Run a single test of the create_breakpoint_parse_arg_string function. + INPUT is passed to create_breakpoint_parse_arg_string while all other + arguments are the expected output from + create_breakpoint_parse_arg_string. */ + +static void +test (const char *input, const char *condition, int thread = -1, + int inferior = -1, int task = -1, bool force = false, + const char *rest = nullptr, const char *error_msg = nullptr) +{ + gdb::unique_xmalloc_ptr extracted_condition; + gdb::unique_xmalloc_ptr extracted_rest; + int extracted_thread, extracted_inferior, extracted_task; + bool extracted_force_condition; + std::string exception_msg, error_str; + + if (error_msg != nullptr) + error_str = std::string (error_msg) + "\n"; + + try + { + create_breakpoint_parse_arg_string (input, &extracted_condition, + &extracted_thread, + &extracted_inferior, + &extracted_task, &extracted_rest, + &extracted_force_condition); + } + catch (const gdb_exception_error &ex) + { + string_file buf; + + exception_print (&buf, ex); + exception_msg = buf.release (); + } + + if ((condition == nullptr) != (extracted_condition.get () == nullptr) + || (condition != nullptr + && strcmp (condition, extracted_condition.get ()) != 0) + || (rest == nullptr) != (extracted_rest.get () == nullptr) + || (rest != nullptr && strcmp (rest, extracted_rest.get ()) != 0) + || thread != extracted_thread + || inferior != extracted_inferior + || task != extracted_task + || force != extracted_force_condition + || exception_msg != error_str) + { + if (run_verbose ()) + { + debug_printf ("input: '%s'\n", input); + debug_printf ("condition: '%s'\n", extracted_condition.get ()); + debug_printf ("rest: '%s'\n", extracted_rest.get ()); + debug_printf ("thread: %d\n", extracted_thread); + debug_printf ("inferior: %d\n", extracted_inferior); + debug_printf ("task: %d\n", extracted_task); + debug_printf ("forced: %s\n", + extracted_force_condition ? "true" : "false"); + debug_printf ("exception: '%s'\n", exception_msg.c_str ()); + } + + /* Report the failure. */ + SELF_CHECK (false); + } +} + +/* Wrapper for test function. Pass through the default values for all + parameters, except the last parameter, which indicates that we expect + INPUT to trigger an error. */ + +static void +test_error (const char *input, const char *error_msg) +{ + test (input, nullptr, -1, -1, -1, false, nullptr, error_msg); +} + +/* Test the create_breakpoint_parse_arg_string function. Just wraps + multiple calls to the test function above. */ + +static void +create_breakpoint_parse_arg_string_tests () +{ + gdbarch *arch = current_inferior ()->arch (); + scoped_restore_current_pspace_and_thread restore; + scoped_mock_context mock_target (arch); + + int global_thread_num = mock_target.mock_thread.global_num; + + /* Test parsing valid breakpoint condition strings. */ + test (" if blah ", "blah"); + test (" if blah thread 1", "blah", global_thread_num); + test (" if blah inferior 1", "blah", -1, 1); + test (" if blah thread 1 ", "blah", global_thread_num); + test ("thread 1 woof", nullptr, global_thread_num, -1, -1, false, "woof"); + test ("thread 1 X", nullptr, global_thread_num, -1, -1, false, "X"); + test (" if blah thread 1 -force-condition", "blah", global_thread_num, + -1, -1, true); + test (" -force-condition if blah thread 1", "blah", global_thread_num, + -1, -1, true); + test (" -force-condition if blah thread 1 ", "blah", global_thread_num, + -1, -1, true); + test ("thread 1 -force-condition if blah", "blah", global_thread_num, + -1, -1, true); + test ("if (A::outer::func ())", "(A::outer::func ())"); + test ("if ( foo == thread )", "( foo == thread )"); + test ("if ( foo == thread ) inferior 1", "( foo == thread )", -1, 1); + test ("if ( foo == thread ) thread 1", "( foo == thread )", + global_thread_num); + test ("if foo == thread", "foo == thread"); + test ("if foo == thread 1", "foo ==", global_thread_num); + + /* Test parsing some invalid breakpoint condition strings. */ + test_error ("thread 1 if foo == 123 thread 1", + "You can specify only one thread."); + test_error ("thread 1 if foo == 123 inferior 1", + "You can specify only one of thread, inferior, or task."); + test_error ("thread 1 if foo == 123 task 1", + "You can specify only one of thread, inferior, or task."); + test_error ("inferior 1 if foo == 123 inferior 1", + "You can specify only one inferior."); + test_error ("inferior 1 if foo == 123 thread 1", + "You can specify only one of thread, inferior, or task."); + test_error ("inferior 1 if foo == 123 task 1", + "You can specify only one of thread, inferior, or task."); + test_error ("thread 1.2.3", "Invalid thread ID: 1.2.3"); + test_error ("thread 1/2", "Invalid thread ID: 1/2"); + test_error ("thread 1xxx", "Invalid thread ID: 1xxx"); + test_error ("inferior 1xxx", "Junk 'xxx' after inferior keyword."); + test_error ("task 1xxx", "Junk 'xxx' after task keyword."); +} + +} // namespace selftests +#endif /* GDB_SELF_TEST */ + +void _initialize_break_cond_parse (); +void +_initialize_break_cond_parse () +{ +#if GDB_SELF_TEST + selftests::register_test + ("create_breakpoint_parse_arg_string", + selftests::create_breakpoint_parse_arg_string_tests); +#endif +} diff --git a/gdb/break-cond-parse.h b/gdb/break-cond-parse.h new file mode 100644 index 00000000000..cbee70f4e9e --- /dev/null +++ b/gdb/break-cond-parse.h @@ -0,0 +1,52 @@ +/* Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of GDB. + + 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 . */ + +#if !defined (BREAK_COND_PARSE_H) +#define BREAK_COND_PARSE_H 1 + +/* Given TOK, a string possibly containing a condition, thread, inferior, + task and force-condition flag, as accepted by the 'break' command, + extract the condition string, thread, inferior, task number, and the + force_condition flag, then set *COND_STRING, *THREAD, *INFERIOR, *TASK, + and *FORCE. + + As TOK is parsed, if an unknown keyword is encountered before the 'if' + keyword then everything starting from the unknown keyword is placed into + *REST. + + Both *COND and *REST are initialized to nullptr. If no 'if' keyword is + found then *COND will be returned as nullptr. If no unknown content is + found then *REST is returned as nullptr. + + If no thread is found, *THREAD is set to -1. If no inferior is found, + *INFERIOR is set to -1. If no task is found, *TASK is set to -1. If + the -force-condition flag is not found then *FORCE is set to false. + + Due to the free-form nature that the string TOK might take (a 'thread' + keyword can appear before or after an 'if' condition) then we end up + having to check for keywords from both the start of TOK and the end of + TOK. + + If TOK is nullptr, or TOK is the empty string, then the output variables + are all given their default values. */ + +extern void create_breakpoint_parse_arg_string + (const char *tok, gdb::unique_xmalloc_ptr *cond_string, + int *thread, int *inferior, int *task, + gdb::unique_xmalloc_ptr *rest, bool *force); + +#endif diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 4516d47e7a1..6271e785535 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -70,6 +70,7 @@ #include "cli/cli-style.h" #include "cli/cli-decode.h" #include +#include "break-cond-parse.h" /* readline include files */ #include "readline/tilde.h" @@ -6323,20 +6324,7 @@ print_breakpoint_location (const breakpoint *b, const bp_location *loc) uiout->field_stream ("at", stb); } else - { - uiout->field_string ("pending", b->locspec->to_string ()); - /* If extra_string is available, it could be holding a condition - or dprintf arguments. In either case, make sure it is printed, - too, but only for non-MI streams. */ - if (!uiout->is_mi_like_p () && b->extra_string != NULL) - { - if (b->type == bp_dprintf) - uiout->text (","); - else - uiout->text (" "); - uiout->text (b->extra_string.get ()); - } - } + uiout->field_string ("pending", b->locspec->to_string ()); if (loc && is_breakpoint (b) && breakpoint_condition_evaluation_mode () == condition_evaluation_target @@ -8701,8 +8689,8 @@ code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_, command line, otherwise it's an error. */ if (type == bp_dprintf) update_dprintf_command_list (this); - else if (extra_string != nullptr) - error (_("Garbage '%s' at end of command"), extra_string.get ()); + else + gdb_assert (extra_string == nullptr); /* The order of the locations is now stable. Set the location condition using the location's number. */ @@ -8930,197 +8918,6 @@ check_fast_tracepoint_sals (struct gdbarch *gdbarch, } } -/* Given TOK, a string specification of condition and thread, as accepted - by the 'break' command, extract the condition string into *COND_STRING. - If no condition string is found then *COND_STRING is set to nullptr. - - If the breakpoint specification has an associated thread, task, or - inferior, these are extracted into *THREAD, *TASK, and *INFERIOR - respectively, otherwise these arguments are set to -1 (for THREAD and - INFERIOR) or 0 (for TASK). - - PC identifies the context at which the condition should be parsed. */ - -static void -find_condition_and_thread (const char *tok, CORE_ADDR pc, - gdb::unique_xmalloc_ptr *cond_string, - int *thread, int *inferior, int *task, - gdb::unique_xmalloc_ptr *rest) -{ - cond_string->reset (); - *thread = -1; - *inferior = -1; - *task = -1; - rest->reset (); - bool force = false; - - while (tok && *tok) - { - const char *end_tok; - int toklen; - const char *cond_start = NULL; - const char *cond_end = NULL; - - tok = skip_spaces (tok); - - if ((*tok == '"' || *tok == ',') && rest) - { - rest->reset (savestring (tok, strlen (tok))); - break; - } - - end_tok = skip_to_space (tok); - - toklen = end_tok - tok; - - if (toklen >= 1 && strncmp (tok, "if", toklen) == 0) - { - tok = cond_start = end_tok + 1; - try - { - parse_exp_1 (&tok, pc, block_for_pc (pc), 0); - } - catch (const gdb_exception_error &) - { - if (!force) - throw; - else - tok = tok + strlen (tok); - } - cond_end = tok; - cond_string->reset (savestring (cond_start, cond_end - cond_start)); - } - else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0) - { - tok = tok + toklen; - force = true; - } - else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0) - { - const char *tmptok; - struct thread_info *thr; - - if (*thread != -1) - error(_("You can specify only one thread.")); - - if (*task != -1) - error (_("You can specify only one of thread or task.")); - - if (*inferior != -1) - error (_("You can specify only one of inferior or thread.")); - - tok = end_tok + 1; - thr = parse_thread_id (tok, &tmptok); - if (tok == tmptok) - error (_("Junk after thread keyword.")); - *thread = thr->global_num; - tok = tmptok; - } - else if (toklen >= 1 && strncmp (tok, "inferior", toklen) == 0) - { - if (*inferior != -1) - error(_("You can specify only one inferior.")); - - if (*task != -1) - error (_("You can specify only one of inferior or task.")); - - if (*thread != -1) - error (_("You can specify only one of inferior or thread.")); - - char *tmptok; - tok = end_tok + 1; - *inferior = strtol (tok, &tmptok, 0); - if (tok == tmptok) - error (_("Junk after inferior keyword.")); - if (!valid_global_inferior_id (*inferior)) - error (_("Unknown inferior number %d."), *inferior); - tok = tmptok; - } - else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0) - { - char *tmptok; - - if (*task != -1) - error(_("You can specify only one task.")); - - if (*thread != -1) - error (_("You can specify only one of thread or task.")); - - if (*inferior != -1) - error (_("You can specify only one of inferior or task.")); - - tok = end_tok + 1; - *task = strtol (tok, &tmptok, 0); - if (tok == tmptok) - error (_("Junk after task keyword.")); - if (!valid_task_id (*task)) - error (_("Unknown task %d."), *task); - tok = tmptok; - } - else if (rest) - { - rest->reset (savestring (tok, strlen (tok))); - break; - } - else - error (_("Junk at end of arguments.")); - } -} - -/* Call 'find_condition_and_thread' for each sal in SALS until a parse - succeeds. The parsed values are written to COND_STRING, THREAD, - TASK, and REST. See the comment of 'find_condition_and_thread' - for the description of these parameters and INPUT. */ - -static void -find_condition_and_thread_for_sals (const std::vector &sals, - const char *input, - gdb::unique_xmalloc_ptr *cond_string, - int *thread, int *inferior, int *task, - gdb::unique_xmalloc_ptr *rest) -{ - int num_failures = 0; - for (auto &sal : sals) - { - gdb::unique_xmalloc_ptr cond; - int thread_id = -1; - int inferior_id = -1; - int task_id = -1; - gdb::unique_xmalloc_ptr remaining; - - /* Here we want to parse 'arg' to separate condition from thread - number. But because parsing happens in a context and the - contexts of sals might be different, try each until there is - success. Finding one successful parse is sufficient for our - goal. When setting the breakpoint we'll re-parse the - condition in the context of each sal. */ - try - { - find_condition_and_thread (input, sal.pc, &cond, &thread_id, - &inferior_id, &task_id, &remaining); - *cond_string = std::move (cond); - /* A value of -1 indicates that these fields are unset. At most - one of these fields should be set (to a value other than -1) - at this point. */ - gdb_assert (((thread_id == -1 ? 1 : 0) - + (task_id == -1 ? 1 : 0) - + (inferior_id == -1 ? 1 : 0)) >= 2); - *thread = thread_id; - *inferior = inferior_id; - *task = task_id; - *rest = std::move (remaining); - break; - } - catch (const gdb_exception_error &e) - { - num_failures++; - /* If no sal remains, do not continue. */ - if (num_failures == sals.size ()) - throw; - } - } -} - /* Decode a static tracepoint marker spec. */ static std::vector @@ -9237,6 +9034,46 @@ create_breakpoint (struct gdbarch *gdbarch, ? (extra_string != nullptr && !parse_extra) : (extra_string == nullptr || parse_extra)); + /* Will hold either copies of the similarly named function argument, or + will hold a modified version of the function argument, depending on + the value of PARSE_EXTRA. */ + gdb::unique_xmalloc_ptr cond_string_copy; + gdb::unique_xmalloc_ptr extra_string_copy; + + if (parse_extra) + { + /* Parse EXTRA_STRING splitting the parts out. */ + create_breakpoint_parse_arg_string (extra_string, &cond_string_copy, + &thread, &inferior, &task, + &extra_string_copy, + &force_condition); + + /* We could check that EXTRA_STRING_COPY is empty at this point -- it + should be, as we only get here for things that are not bp_dprintf, + however, we prefer to give the location spec parser a chance to + run first, this means the user will get errors about invalid + location spec instead of an error about garbage at the end of the + command line. + + We still do the EXTRA_STRING_COPY is empty check, just later in + this function. */ + + gdb_assert (thread == -1 || thread > 0); + gdb_assert (task == -1 || task > 0); + gdb_assert (inferior == -1 || inferior > 0); + } + else + { + if (cond_string != nullptr) + cond_string_copy.reset (xstrdup (cond_string)); + if (extra_string != nullptr) + extra_string_copy.reset (xstrdup (extra_string)); + } + + /* Clear these. Updated values are now held in the *_copy locals. */ + cond_string = nullptr; + extra_string = nullptr; + try { ops->create_sals_from_location_spec (locspec, &canonical); @@ -9272,6 +9109,13 @@ create_breakpoint (struct gdbarch *gdbarch, throw; } + /* Only bp_dprintf breakpoints should have anything in EXTRA_STRING_COPY + by this point. For all other breakpoints this indicates an error. We + could place this check earlier in the function, but we prefer to see + errors from the location spec parser before we see this error message. */ + if (type_wanted != bp_dprintf && extra_string_copy.get () != nullptr) + error (_("Garbage '%s' at end of command"), extra_string_copy.get ()); + if (!pending && canonical.lsals.empty ()) return 0; @@ -9295,63 +9139,31 @@ create_breakpoint (struct gdbarch *gdbarch, breakpoint. */ if (!pending) { - gdb::unique_xmalloc_ptr cond_string_copy; - gdb::unique_xmalloc_ptr extra_string_copy; - - if (parse_extra) + /* Check the validity of the condition. We should error out if the + condition is invalid at all of the locations and if it is not + forced. In the PARSE_EXTRA case above, this check is done when + parsing the EXTRA_STRING. */ + if (cond_string_copy.get () != nullptr && !force_condition) { - gdb_assert (type_wanted != bp_dprintf); - - gdb::unique_xmalloc_ptr rest; - gdb::unique_xmalloc_ptr cond; - + int num_failures = 0; const linespec_sals &lsal = canonical.lsals[0]; - - find_condition_and_thread_for_sals (lsal.sals, extra_string, - &cond, &thread, &inferior, - &task, &rest); - - if (rest.get () != nullptr && *(rest.get ()) != '\0') - error (_("Garbage '%s' at end of command"), rest.get ()); - - cond_string_copy = std::move (cond); - extra_string_copy = std::move (rest); - } - else - { - /* Check the validity of the condition. We should error out - if the condition is invalid at all of the locations and - if it is not forced. In the PARSE_EXTRA case above, this - check is done when parsing the EXTRA_STRING. */ - if (cond_string != nullptr && !force_condition) + for (const auto &sal : lsal.sals) { - int num_failures = 0; - const linespec_sals &lsal = canonical.lsals[0]; - for (const auto &sal : lsal.sals) + const char *cond = cond_string_copy.get (); + try { - const char *cond = cond_string; - try - { - parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0); - /* One success is sufficient to keep going. */ - break; - } - catch (const gdb_exception_error &) - { - num_failures++; - /* If this is the last sal, error out. */ - if (num_failures == lsal.sals.size ()) - throw; - } + parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0); + /* One success is sufficient to keep going. */ + break; + } + catch (const gdb_exception_error &) + { + num_failures++; + /* If this is the last sal, error out. */ + if (num_failures == lsal.sals.size ()) + throw; } } - - /* Create a private copy of condition string. */ - if (cond_string) - cond_string_copy.reset (xstrdup (cond_string)); - /* Create a private copy of any extra string. */ - if (extra_string) - extra_string_copy.reset (xstrdup (extra_string)); } ops->create_breakpoints_sal (gdbarch, &canonical, @@ -9368,21 +9180,16 @@ create_breakpoint (struct gdbarch *gdbarch, type_wanted); b->locspec = locspec->clone (); - if (parse_extra) - b->cond_string = NULL; - else - { - /* Create a private copy of condition string. */ - b->cond_string.reset (cond_string != NULL - ? xstrdup (cond_string) - : NULL); - b->thread = thread; - } + /* Create a private copy of the condition string. */ + b->cond_string = std::move (cond_string_copy); + + b->thread = thread; + b->task = task; + b->inferior = inferior; /* Create a private copy of any extra string. */ - b->extra_string.reset (extra_string != NULL - ? xstrdup (extra_string) - : NULL); + b->extra_string = std::move (extra_string_copy); + b->ignore_count = ignore_count; b->disposition = tempflag ? disp_del : disp_donttouch; b->condition_not_parsed = 1; @@ -9391,9 +9198,12 @@ create_breakpoint (struct gdbarch *gdbarch, && type_wanted != bp_hardware_breakpoint) || thread != -1) b->pspace = current_program_space; + if (b->type == bp_dprintf) + update_dprintf_command_list (b.get ()); + install_breakpoint (internal, std::move (b), 0); } - + if (canonical.lsals.size () > 1) { warning (_("Multiple breakpoints were set.\nUse the " @@ -13124,24 +12934,6 @@ code_breakpoint::location_spec_to_sals (location_spec *locspec, { for (auto &sal : sals) resolve_sal_pc (&sal); - if (condition_not_parsed && extra_string != NULL) - { - gdb::unique_xmalloc_ptr local_cond, local_extra; - int local_thread, local_task, local_inferior; - - find_condition_and_thread_for_sals (sals, extra_string.get (), - &local_cond, &local_thread, - &local_inferior, - &local_task, &local_extra); - gdb_assert (cond_string == nullptr); - if (local_cond != nullptr) - cond_string = std::move (local_cond); - thread = local_thread; - task = local_task; - if (local_extra != nullptr) - extra_string = std::move (local_extra); - condition_not_parsed = 0; - } if (type == bp_static_tracepoint) { diff --git a/gdb/testsuite/gdb.ada/tasks.exp b/gdb/testsuite/gdb.ada/tasks.exp index 6e588408c9d..ecbfbd5522f 100644 --- a/gdb/testsuite/gdb.ada/tasks.exp +++ b/gdb/testsuite/gdb.ada/tasks.exp @@ -55,11 +55,11 @@ gdb_test "watch j task 1 task 3" "You can specify only one task\\." # Check that attempting to combine 'task' and 'thread' gives an error. gdb_test "break break_me task 1 thread 1" \ - "You can specify only one of thread or task\\." + "You can specify only one of thread, inferior, or task\\." gdb_test "break break_me thread 1 task 1" \ - "You can specify only one of thread or task\\." + "You can specify only one of thread, inferior, or task\\." gdb_test "break break_me inferior 1 task 1" \ - "You can specify only one of inferior or task\\." + "You can specify only one of thread, inferior, or task\\." gdb_test "watch j task 1 thread 1" \ "You can specify only one of thread or task\\." gdb_test "watch j thread 1 task 1" \ diff --git a/gdb/testsuite/gdb.base/condbreak.exp b/gdb/testsuite/gdb.base/condbreak.exp index 65d19b3e7a9..3b619e47bfc 100644 --- a/gdb/testsuite/gdb.base/condbreak.exp +++ b/gdb/testsuite/gdb.base/condbreak.exp @@ -179,6 +179,10 @@ gdb_test "break -q main if (1==1) thread 999" \ "Unknown thread 999\\." gdb_test "break -q main thread 999 if (1==1)" \ "Unknown thread 999\\." +gdb_test "break -q main if (1==1) thread 999 -force-condition" \ + "Unknown thread 999\\." +gdb_test "break -q main thread 999 if (1==1) -force-condition" \ + "Unknown thread 999\\." # Verify that both if and thread can be distinguished from a breakpoint # address expression. @@ -186,20 +190,71 @@ gdb_test "break *main if (1==1) thread 999" \ "Unknown thread 999\\." gdb_test "break *main thread 999 if (1==1)" \ "Unknown thread 999\\." +gdb_test "break *main if (1==1) thread 999 -force-condition" \ + "Unknown thread 999\\." +gdb_test "break *main thread 999 if (1==1) -force-condition" \ + "Unknown thread 999\\." # Similarly for task. gdb_test "break *main if (1==1) task 999" \ "Unknown task 999\\." gdb_test "break *main task 999 if (1==1)" \ "Unknown task 999\\." +gdb_test "break *main if (1==1) task 999 -force-condition" \ + "Unknown task 999\\." +gdb_test "break *main task 999 if (1==1) -force-condition" \ + "Unknown task 999\\." -# GDB accepts abbreviations for "thread" and "task". +# GDB accepts abbreviations for "thread", "task" and +# "-force-condition", when these keywords appear after +# the breakpoint condition. gdb_test "break *main if (1==1) t 999" \ "Unknown thread 999\\." gdb_test "break *main if (1==1) th 999" \ "Unknown thread 999\\." gdb_test "break *main if (1==1) ta 999" \ "Unknown task 999\\." +gdb_test "break *main if (1==1) t 999 -force" \ + "Unknown thread 999\\." +gdb_test "break *main if (1==1) th 999 -force" \ + "Unknown thread 999\\." +gdb_test "break *main if (1==1) ta 999 -force" \ + "Unknown task 999\\." + +# Check the use of abbreviations before the condition. This works +# because, when the location spec starts with '*' GDB is able to +# figure out that the complete location is '*main'. +gdb_test "break *main t 999 if (1==1)" \ + "Unknown thread 999\\." +gdb_test "break *main th 999 if (1==1)" \ + "Unknown thread 999\\." +gdb_test "break *main ta 999 if (1==1)" \ + "Unknown task 999\\." +gdb_test "break *main t 999 -force if (1==1)" \ + "Unknown thread 999\\." +gdb_test "break *main th 999 -force if (1==1)" \ + "Unknown thread 999\\." +gdb_test "break *main ta 999 -force if (1==1)" \ + "Unknown task 999\\." + +# However, when the location spec doesn't start with '*' GDB relies on +# the linespec parser to spot the keyword which marks the end of the +# linespec, and this parser doesn't check for abbreviations. +gdb_test "with breakpoint pending off -- break main t 999 if (1==1)" \ + "Function \"main t 999\" not defined\\." +gdb_test "with breakpoint pending off -- break main th 999 if (1==1)" \ + "Function \"main th 999\" not defined\\." +gdb_test "with breakpoint pending off -- break main ta 999 if (1==1)" \ + "Function \"main ta 999\" not defined\\." + +# GDB does not treat a "-force-condition" flag that appears +# immediately after the condition as the flag, but instead treats it +# as " - force - condition", that is, subtraction of the symbol +# "force" followed by subtraction of symbol "context". This is really +# just a quirk of how this used to be implemented, and should maybe be +# changed in the future. However, for now GDB retains this behaviour. +gdb_test "break *main if (1==1) -force-condition" \ + "No symbol \"force\" in current context\\." set test "run until breakpoint at marker3" gdb_test_multiple "continue" $test { diff --git a/gdb/testsuite/gdb.base/pending.exp b/gdb/testsuite/gdb.base/pending.exp index 737b0c4e5d0..fdfb5e3a486 100644 --- a/gdb/testsuite/gdb.base/pending.exp +++ b/gdb/testsuite/gdb.base/pending.exp @@ -170,7 +170,8 @@ gdb_test "info break" \ \[\t \]+stop only if k == 1.* \[\t \]+print k.* \[0-9\]+\[\t \]+breakpoint keep y.* in main at .*$srcfile:$mainline.* -\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp2_loc if x > 3.*" \ +\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp2_loc.* +\\s+stop only if x > 3.*" \ "multiple pending breakpoints" @@ -195,8 +196,10 @@ gdb_test "info break" \ \[\t \]+stop only if k == 1.* \[\t \]+print k.* \[0-9\]+\[\t \]+breakpoint keep y.* in main at .*$srcfile:$mainline.* -\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp2_loc if x > 3.* -\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp3_loc.*ignore next 2 hits.*" \ +\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp2_loc.* +\\s+stop only if x > 3.* +\[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*pendshr.c:$bp3_loc.* +\\s+ignore next 2 hits.*" \ "multiple pending breakpoints 2" # @@ -267,3 +270,24 @@ gdb_test "info break" \ \[0-9\]+\[\t \]+breakpoint keep y.* in main at .*$srcfile:$mainline.* \[0-9\]+\[\t \]+breakpoint keep y.*PENDING.*imaginary.*" \ "verify pending breakpoint after restart" + +# Test GDB's parsing of pending breakpoint thread and condition. + +gdb_test_no_output "set breakpoint pending on" +gdb_test "break foo if (unknown_var && another_unknown_var) thread 1" \ + "Breakpoint $decimal \\(foo\\) pending\\." +set bpnum [get_integer_valueof "\$bpnum" "*INVALID" \ + "get number for foo breakpoint"] + +if {[gdb_protocol_is_remote]} { + set evals_re "(?: \\(\[^) \]+ evals\\))?" +} else { + set evals_re "" +} + +gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+\\s+foo" \ + "\\s+stop only if \\(unknown_var && another_unknown_var\\)${evals_re}" \ + "\\s+stop only in thread 1"] \ + "check pending breakpoint on foo" diff --git a/gdb/testsuite/gdb.linespec/explicit.exp b/gdb/testsuite/gdb.linespec/explicit.exp index 625f9cee0fc..6cbe1377c53 100644 --- a/gdb/testsuite/gdb.linespec/explicit.exp +++ b/gdb/testsuite/gdb.linespec/explicit.exp @@ -575,22 +575,30 @@ namespace eval $testfile { allow-pending]} { fail "set $tst" } else { - gdb_test "info break" ".*PENDING.*myfunction if foofoofoo == 1.*" $tst + gdb_test "info break" ".*PENDING.*myfunction\r\n\\s+stop only if foofoofoo == 1.*" $tst } gdb_exit gdb_start + if {[target_info gdb_protocol] == "extended-remote"} { + set evals_re "(?: \\(\[^) \]+ evals\\))?" + } else { + set evals_re "" + } + set tst "pending valid conditional explicit breakpoint" if {![gdb_breakpoint "-func myfunction if arg == 0" \ allow-pending]} { fail "set $tst" } else { - gdb_test "info break" ".*PENDING.*myfunction if arg == 0" $tst + gdb_test "info break" \ + ".*PENDING.*myfunction\r\n\\s+stop only if arg == 0${evals_re}" \ + $tst gdb_load [standard_output_file $exefile] gdb_test "info break" \ - ".*in myfunction at .*$srcfile:.*stop only if arg == 0.*" \ + ".*in myfunction at .*$srcfile:.*stop only if arg == 0${evals_re}" \ "$tst resolved" } diff --git a/gdb/testsuite/gdb.linespec/keywords.exp b/gdb/testsuite/gdb.linespec/keywords.exp index 36a919c8be2..d2596d2b357 100644 --- a/gdb/testsuite/gdb.linespec/keywords.exp +++ b/gdb/testsuite/gdb.linespec/keywords.exp @@ -55,7 +55,7 @@ with_test_prefix "trailing whitespace" { gdb_test "break thread 123" "Unknown thread 123\\." gdb_test "break thread foo" "Invalid thread ID: foo" gdb_test "break task 123" "Unknown task 123\\." -gdb_test "break task foo" "Junk after task keyword\\." +gdb_test "break task foo" "Junk 'foo' after task keyword\\." gdb_breakpoint "thread if 0" "message" # These are also NULL locations, but using a subsequent keyword @@ -63,9 +63,9 @@ gdb_breakpoint "thread if 0" "message" gdb_test "break thread thread" "Invalid thread ID: thread" gdb_test "break thread task" "Invalid thread ID: task" gdb_test "break thread if" "Invalid thread ID: if" -gdb_test "break task task" "Junk after task keyword\\." -gdb_test "break task thread" "Junk after task keyword\\." -gdb_test "break task if" "Junk after task keyword\\." +gdb_test "break task task" "Junk 'task' after task keyword\\." +gdb_test "break task thread" "Junk 'thread' after task keyword\\." +gdb_test "break task if" "Junk 'if' after task keyword\\." # Test locations containing keyword followed by keyword. gdb_test "break thread thread 123" "Unknown thread 123\\." diff --git a/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp b/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp index fd5684bd2b1..4cf6dec4b41 100644 --- a/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp +++ b/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp @@ -50,7 +50,8 @@ set bp_location1 [gdb_get_line_number "set breakpoint 1 here"] # Set pending dprintf via MI. set bp [mi_make_breakpoint_pending -number "1" -type "dprintf" \ -disp "keep" -enabled "y" -pending "pendfunc1" \ - -original-location "pendfunc1"] + -original-location "pendfunc1" \ + -script {\["printf \\\"hello\\\""\]}] mi_gdb_test "-dprintf-insert -f pendfunc1 \"hello\"" \ ".*\\^done,$bp" "mi set dprintf" diff --git a/gdb/testsuite/gdb.multi/inferior-specific-bp.exp b/gdb/testsuite/gdb.multi/inferior-specific-bp.exp index 5cc451b0ecc..46efe6f54bc 100644 --- a/gdb/testsuite/gdb.multi/inferior-specific-bp.exp +++ b/gdb/testsuite/gdb.multi/inferior-specific-bp.exp @@ -51,9 +51,9 @@ if {![runto_main]} { # this should fail. Try with the keywords in both orders just in case the # parser has a bug. gdb_test "break foo thread 1.1 inferior 1" \ - "You can specify only one of inferior or thread\\." + "You can specify only one of thread, inferior, or task\\." gdb_test "break foo inferior 1 thread 1.1" \ - "You can specify only one of inferior or thread\\." + "You can specify only one of thread, inferior, or task\\." # Try to create a breakpoint using the 'inferior' keyword multiple times. gdb_test "break foo inferior 1 inferior 2" \ diff --git a/gdb/testsuite/gdb.threads/del-pending-thread-bp-lib.c b/gdb/testsuite/gdb.threads/del-pending-thread-bp-lib.c new file mode 100644 index 00000000000..15d1b9833dd --- /dev/null +++ b/gdb/testsuite/gdb.threads/del-pending-thread-bp-lib.c @@ -0,0 +1,22 @@ +/* Copyright 2023 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 . */ + +int global_var = 0; + +void +foo (int arg) +{ + global_var = arg; +} diff --git a/gdb/testsuite/gdb.threads/del-pending-thread-bp.c b/gdb/testsuite/gdb.threads/del-pending-thread-bp.c new file mode 100644 index 00000000000..6fc76dbf08c --- /dev/null +++ b/gdb/testsuite/gdb.threads/del-pending-thread-bp.c @@ -0,0 +1,85 @@ +/* Copyright 2023 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 +#include +#include + +pthread_barrier_t barrier; + +static void +barrier_wait (pthread_barrier_t *b) +{ + int res = pthread_barrier_wait (b); + if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD) + abort (); +} + +static void * +thread_worker (void *arg) +{ + barrier_wait (&barrier); + return NULL; +} + +void +breakpt (void) +{ + /* Nothing. */ +} + +int +main (void) +{ + void *handle; + void (*func)(int); + pthread_t thread; + + if (pthread_barrier_init (&barrier, NULL, 2) != 0) + abort (); + + if (pthread_create (&thread, NULL, thread_worker, NULL) != 0) + abort (); + + breakpt (); + + /* Allow the worker thread to complete. */ + barrier_wait (&barrier); + + if (pthread_join (thread, NULL) != 0) + abort (); + + breakpt (); + + /* Now load the shared library. */ + handle = dlopen (SHLIB_NAME, RTLD_LAZY); + if (handle == NULL) + abort (); + + /* Find the function symbol. */ + func = (void (*)(int)) dlsym (handle, "foo"); + + /* Call the library function. */ + func (1); + + /* Unload the shared library. */ + if (dlclose (handle) != 0) + abort (); + + breakpt (); + + return 0; +} + diff --git a/gdb/testsuite/gdb.threads/del-pending-thread-bp.exp b/gdb/testsuite/gdb.threads/del-pending-thread-bp.exp new file mode 100644 index 00000000000..14a91a40d76 --- /dev/null +++ b/gdb/testsuite/gdb.threads/del-pending-thread-bp.exp @@ -0,0 +1,98 @@ +# Copyright 2023 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 test checks that pending thread-specific breakpoints are +# correctly deleted when the thread the breakpoint is for goes out of +# scope. +# +# We also check that we can't create a pending thread-specific +# breakpoint for a non-existent thread. + +require allow_shlib_tests + +standard_testfile + +set libname $testfile-lib +set srcfile_lib $srcdir/$subdir/$libname.c +set binfile_lib [standard_output_file $libname.so] + +if { [gdb_compile_shlib $srcfile_lib $binfile_lib {}] != "" } { + untested "failed to compile shared library 1" + return -1 +} + +set binfile_lib_target [gdb_download_shlib $binfile_lib] + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile \ + [list debug \ + additional_flags=-DSHLIB_NAME=\"$binfile_lib_target\" \ + shlib_load pthreads]] } { + return -1 +} + +gdb_locate_shlib $binfile_lib + +if ![runto_main] { + return 0 +} + +# Run until we have two threads. +gdb_breakpoint "breakpt" +gdb_continue_to_breakpoint "first breakpt call" + +# Confirm that we have a thread '2'. +gdb_test "info threads" "\r\n\\s+2\\s+\[^\r\n\]+" + +# Create a pending, thread-specific, breakpoint on 'foo'. +gdb_breakpoint "foo thread 2" allow-pending +set bpnum [get_integer_valueof "\$bpnum" "*INVALID*" \ + "get breakpoint number"] + +# Check we can't create a pending thread-specific breakpoint for a +# non-existent thread. +gdb_test "with breakpoint pending on -- break foo thread 99" \ + "Unknown thread 99\\." + +# Continue to 'breakpt' again. Don't use gdb_continue_to_breakpoint +# as we are looking for the thread exited and breakpoint deleted +# messages. +gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + "\\\[Thread \[^\r\n\]+ exited\\\]" \ + "Thread-specific breakpoint $bpnum deleted - thread 2 no longer in the thread list\\." \ + "" \ + "Thread 1 \[^\r\n\]+, breakpt \\(\\) at \[^\r\n\]+" \ + "$decimal\\s+\[^\r\n\]+"] \ + "second breakpt call" + +# Confirm breakpoint has been deleted. +gdb_test "info breakpoints $bpnum" \ + "No breakpoint, watchpoint, tracepoint, or catchpoint matching '$bpnum'\\." + +# Continue again. This will pass through 'foo'. We should not stop +# in 'foo', the breakpoint has been deleted. We should next stop in +# breakpt again. +gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + "" \ + "Thread 1 \[^\r\n\]+ hit Breakpoint $decimal, breakpt \\(\\) at \[^\r\n\]+" \ + "$decimal\\s+\[^\r\n\]+"] \ + "third breakpt call" +gdb_test "bt 1" \ + [multi_line \ + "#0\\s+breakpt \\(\\) at \[^\r\n\]+" \ + "\\(More stack frames follow\\.\\.\\.\\)"] From patchwork Sat Jul 20 17:03:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94260 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 CD573385DDCB for ; Sat, 20 Jul 2024 17:05:12 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 78B483860C34 for ; Sat, 20 Jul 2024 17:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 78B483860C34 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 78B483860C34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495019; cv=none; b=gxbFoxPZIj6FEJ7plhJQP+FlpESOoT/+g1YrO+EOIJ2q10NvO6SP89Db2up96eqmqquZM3w8eVw12mijqUe1j48MEEp2PTEnhOOXBGgGV+Qsm0V2K1/xWe+BdVHXQdBOMBHRZg8shkd723qyXu+oBgGIk9jpCoTEmGPEtZuqklc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495019; c=relaxed/simple; bh=sx37wvX/BqN5fuGSWEDqjXySrsFWLfl2CXDXGDxxnYY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=MKL9+V9lfmslq67p8mKUgWWNtFbiRQCRa2MeJzWX86AVqmeti0Zh2NydnO3HAN/ZsooASHtoi3YzIbrvzYMWmd4bC4TAvvhEJl8Gae/opmluXdN1SnHmwnbm+zkt15wCY4pufQad2mBj6wgtGbUyFVm5SJh4xcz6UIObAcnoVAU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495016; 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=f58xktCuJ8J6Zi+NWqg1TRBauBlchv7HP8pv5Qsgz4Q=; b=OGMaMEIyu2GpbqWYr+m9yzfOTUSGOyGk5b69RmPSMKHSPOiBClgkBzAtgmPb43AW6axpel h9otHYOM9z95oorh8X+frHzxMehWngaRTHTQJPr6G4xloZ97gnTHeTdkUPRT8qaNe9K6o9 0iarpftSo0owollmMF9XjnxJbGEc9E0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-6xlpB1abOMO7W5APGeVEwg-1; Sat, 20 Jul 2024 13:03:34 -0400 X-MC-Unique: 6xlpB1abOMO7W5APGeVEwg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4279c75c44dso19711725e9.2 for ; Sat, 20 Jul 2024 10:03:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495012; x=1722099812; 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=f58xktCuJ8J6Zi+NWqg1TRBauBlchv7HP8pv5Qsgz4Q=; b=mJIMFQbTZpCSp14Q8U73iyLMaLr5xGQe4l4kplozTQn3/F8wERM9uY+3yZf7ICK/uW mGm8DzCDzR1fKNj2S/FLEt9/ZOHi4UWirv+gofl2ifdBUsIrv14pJ9z9jbbKaxQaDK0d xmUw/ljwIWEMlWvygFXgbOt99ctfY+09rpjG1tHDa+MYTZ4XzVl1mei6YFYMmuWL1qZs +JojEw8cn+nCFbsGBJnnh71X0OvADMmRaONVn0Xxb+eRARcViDB21fhof1HPixz1GSVp Lh5f7Hy2pr2aZCnhnu/p/v7cXveP/vhwlKc7O4XDPoXHsCLjjmOFftk12jYePvN5ETps MyAA== X-Gm-Message-State: AOJu0YzRt4sbCLsdHnMkTNtFUouQdpIow1QLQ73GW7yzh+bgWcPBbL5k 19Y2qAMOcWIOCCyYlctMx+CJ4LBlMQE2RrJtByPA8Si3/78d65q+Bvmp+qYQCtwUnD0aTNrSjRU wnOhTzmX8uFr3QY8IJEk5pg3ntnDVqgTeyLYbfjvO6J5ibJ2duYVA1Pgnm5t+clFpRiW5zuudPZ mWVDbpOqPix6IG2+dsrO3VdOLSox/D9+a0L8DXHBrt0is= X-Received: by 2002:a05:6000:7:b0:367:f104:d9e8 with SMTP id ffacd0b85a97d-369bb2ba010mr1136332f8f.47.1721495012044; Sat, 20 Jul 2024 10:03:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFUVGw+X3EeEuE+q+i0FWc1Ye1aMkKjC+QAbbSMi4g+NFXsH/1SCFbBtOp5+Qhr3bNOPUYTVQ== X-Received: by 2002:a05:6000:7:b0:367:f104:d9e8 with SMTP id ffacd0b85a97d-369bb2ba010mr1136319f8f.47.1721495011453; Sat, 20 Jul 2024 10:03:31 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3687869439esm4234911f8f.52.2024.07.20.10.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:31 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv12 5/6] gdb: don't set breakpoint::pspace in create_breakpoint Date: Sat, 20 Jul 2024 18:03:18 +0100 Message-Id: <1b97a55c0921ca65fac809e4b5450f032b5b96e1.1721494871.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-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 I spotted this code within create_breakpoint: if ((type_wanted != bp_breakpoint && type_wanted != bp_hardware_breakpoint) || thread != -1) b->pspace = current_program_space; this code is only executed when creating a pending breakpoint, and sets the breakpoint::pspace member variable. The above code gained the 'thread != -1' clause with this commit: commit cc72b2a2da6d6372cbdb1d14639a5fce84e1a325 Date: Fri Dec 23 17:06:16 2011 +0000 Introduce gdb.FinishBreakpoint in Python While the type_wanted checks were added with this commit: commit f8eba3c61629b3c03ac1f33853eab4d8507adb9c Date: Tue Dec 6 18:54:43 2011 +0000 the "ambiguous linespec" series Before this breakpoint::pspace was set unconditionally. If we look at how breakpoint::pspace is used today, some breakpoint types specifically set this field, either in their constructors, or in a wrapper function that calls the constructor. So, the watchpoint type and its sub-class set this variable, as does the catchpoint type, and all it's sub-classes. However, code_breakpoint doesn't specifically set this field within its constructor, though some sub-classes of code_breakpoint (ada_catchpoint, exception_catchpoint, internal_breakpoint, and momentary_breakpoint) do set this field. When I examine all the places that breakpoint::pspace is used, I believe that in every place where it is expected that this field is set, the breakpoint type will be one that specifically sets this field. Next, I observe two problems with the existing code. First, the above code is only hit for pending breakpoints, there's no equivalent code for non-pending breakpoints. This opens up the possibility of GDB entering non-consistent states; if a breakpoint is first created pending and then later gets a location, the pspace field will be set, while if the breakpoint is immediately non-pending, then the pspace field will never be set. Second, if we look at how breakpoint::pspace is used in the function breakpoint_program_space_exit, we see that when a program space is removed, any breakpoint with breakpoint::pspace set to the removed program space, will be deleted. This makes sense, but does mean we need to ensure breakpoint::pspace is only set for breakpoints that apply to a single program space. So, if I create a pending dprintf breakpoint (type bp_dprintf) then the breakpoint::pspace variable will be set even though the dprintf is not really tied to that one program space. As a result, when the matching program space is removed the dprintf is incorrectly removed. Also, if I create a thread specific breakpoint, then, thanks to the 'thread != -1' clause the wrong program space will be stored in breakpoint::pspace (the current program space is always used, which might not be the program space that corresponds to the selected thread), as a result, the thread specific breakpoint will be deleted when the matching program space is removed. If we look at commit cc72b2a2da6d which added the 'thread != -1' clause, we can see this change was entirely redundant, the breakpoint::pspace is also set in bpfinishpy_init after create_breakpoint has been called. As such, I think we can safely drop the 'thread != -1' clause. For the other problems, I'm proposing to be pretty aggressive - I'd like to drop the breakpoint::pspace assignment completely from create_breakpoint. Having looked at how this variable is used, I believe that it is already set elsewhere in all the cases that it is needed. Maybe this code was needed at one time, but I can't see how it's needed any more. There's tests to expose the issues I've spotted with this code, and there's no regressions in testing. --- gdb/breakpoint.c | 3 - gdb/breakpoint.h | 18 +- .../gdb.multi/pending-bp-del-inferior.c | 28 +++ .../gdb.multi/pending-bp-del-inferior.exp | 216 ++++++++++++++++++ 4 files changed, 259 insertions(+), 6 deletions(-) create mode 100644 gdb/testsuite/gdb.multi/pending-bp-del-inferior.c create mode 100644 gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 6271e785535..c483d80c98d 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -9194,9 +9194,6 @@ create_breakpoint (struct gdbarch *gdbarch, b->disposition = tempflag ? disp_del : disp_donttouch; b->condition_not_parsed = 1; b->enable_state = enabled ? bp_enabled : bp_disabled; - if ((type_wanted != bp_breakpoint - && type_wanted != bp_hardware_breakpoint) || thread != -1) - b->pspace = current_program_space; if (b->type == bp_dprintf) update_dprintf_command_list (b.get ()); diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h index d6d42c705c6..62389ba3d6e 100644 --- a/gdb/breakpoint.h +++ b/gdb/breakpoint.h @@ -829,9 +829,21 @@ struct breakpoint : public intrusive_list_node equals this. */ struct frame_id frame_id = null_frame_id; - /* The program space used to set the breakpoint. This is only set - for breakpoints which are specific to a program space; for - non-thread-specific ordinary breakpoints this is NULL. */ + /* The program space used to set the breakpoint. This is only set for + breakpoints that are not type bp_breakpoint or bp_hardware_breakpoint. + For thread or inferior specific breakpoints, the breakpoints are + managed via the thread and inferior member variables. */ + + /* If not nullptr then this is the program space for which this + breakpoint was created. All watchpoint and catchpoint sub-types set + this field, but not all of the code_breakpoint sub-types do; + generally, user created breakpoint types don't set this field, though + things might be more consistent if they did. + + When this variable is nullptr then a breakpoint might be associated + with multiple program spaces, though you need to check the thread, + inferior and task variables to see if a breakpoint was created for a + specific thread, inferior, or Ada task respectively. */ program_space *pspace = NULL; /* The location specification we used to set the breakpoint. */ diff --git a/gdb/testsuite/gdb.multi/pending-bp-del-inferior.c b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.c new file mode 100644 index 00000000000..1d030007819 --- /dev/null +++ b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.c @@ -0,0 +1,28 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 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 . */ + +int +foo (void) +{ + return 0; +} + +int +main (void) +{ + return foo (); +} diff --git a/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp new file mode 100644 index 00000000000..5fcd1ef2e39 --- /dev/null +++ b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp @@ -0,0 +1,216 @@ +# Copyright 2023 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 . + +# Setup two inferiors. Select one inferior and create a pending +# thread specific breakpoint in the other inferior. +# +# Delete the selected inferior (the one for which the thread specific +# breakpoint doesn't apply), and check that the breakpoint still exists. +# +# Repeat this process, but this time, create an inferior specific +# breakpoint. + +# The plain remote target can't do multiple inferiors. +require !use_gdb_stub + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return -1 +} + +# Setup for the tests. Create two inferiors, both running the global +# BINFILE, and proceed to main in both inferiors. Delete all +# breakpoints, and check that we do have two threads. +# +# Return true after a successful setup, otherwise, return false. +proc test_setup {} { + clean_restart $::binfile + + if {![runto_main]} { + return 0 + } + + gdb_test "add-inferior -exec ${::binfile}" "Added inferior 2.*" \ + "add inferior 2" + gdb_test "inferior 2" "Switching to inferior 2 .*" \ + "select inferior 2" + + if {![runto_main]} { + return 0 + } + + delete_breakpoints + + gdb_test "info threads" \ + [multi_line \ + " Id\\s+Target Id\\s+Frame\\s*" \ + " 1\\.1\\s+\[^\r\n\]+" \ + "\\* 2\\.1\\s+\[^\r\n\]+"] \ + "check we have the expected threads" + + return 1 +} + +# Assuming inferior 2 is already selected, kill the current inferior +# (inferior 2), select inferior 1, and then remove inferior 2. +proc kill_and_remove_inferior_2 {} { + gdb_test "kill" "" "kill inferior 2" \ + "Kill the program being debugged.*y or n. $" "y" + + gdb_test "inferior 1" "Switching to inferior 1 .*" \ + "select inferior 1" + + gdb_test_no_output "remove-inferiors 2" +} + +# Setup two inferiors, then create a breakpoint. If BP_PENDING is +# true then the breakpoint will be pending, otherwise, the breakpoint +# will be non-pending. +# +# BP_TYPE is either 'thread' or 'inferior', and indicates if the +# created breakpoint should be thread or inferior specific. +# +# The breakpoint is created while inferior 2 is selected, and the +# thread/inferior restriction always identifies inferior 1. +# +# Then inferior 2 is killed and removed. +# +# Finally, check that the breakpoint still exists and correctly refers +# to inferior 1. +proc do_bp_test { bp_type bp_pending } { + if {![test_setup]} { + return + } + + if { $bp_pending } { + set bp_func "bar" + } else { + set bp_func "foo" + } + + if { $bp_type eq "thread" } { + set bp_restriction "thread 1.1" + } else { + set bp_restriction "inferior 1" + } + + gdb_breakpoint "$bp_func $bp_restriction" allow-pending + set bp_number [get_integer_valueof "\$bpnum" "INVALID" \ + "get b/p number for previous breakpoint"] + + if { $bp_restriction eq "thread 1.1" } { + set bp_after_restriction "thread 1" + } else { + set bp_after_restriction $bp_restriction + } + + if { $bp_pending } { + set bp_pattern_before \ + [multi_line \ + "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+\\s+${bp_func}" \ + "\\s+stop only in [string_to_regexp $bp_restriction]"] + set bp_pattern_after \ + [multi_line \ + "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+\\s+${bp_func}" \ + "\\s+stop only in [string_to_regexp $bp_after_restriction]"] + } else { + set bp_pattern_before \ + [multi_line \ + "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+\\s*" \ + "\\s+stop only in [string_to_regexp $bp_restriction]" \ + "$bp_number\\.1\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 1" \ + "$bp_number\\.2\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 2"] + + set bp_pattern_after \ + [multi_line \ + "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+" \ + "\\s+stop only in [string_to_regexp $bp_after_restriction]"] + } + + gdb_test "info breakpoints" $bp_pattern_before \ + "info breakpoints before inferior removal" + + kill_and_remove_inferior_2 + + gdb_test "info breakpoints" $bp_pattern_after \ + "info breakpoints after inferior removal" +} + +# Setup two inferiors, then create a dprintf. If BP_PENDING is +# true then the dprintf will be pending, otherwise, the dprintf +# will be non-pending. +# +# The dprintf is created while inferior 2 is selected. Then inferior +# 2 is killed and removed. +# +# Finally, check that the dprintf still exists. +proc do_dprintf_test { bp_pending } { + if {![test_setup]} { + return + } + + if { $bp_pending } { + set bp_func "bar" + + gdb_test "dprintf $bp_func,\"in $bp_func\"" ".*" \ + "create dprintf breakpoint" \ + "Make dprintf pending on future shared library load\\? \\(y or .n.\\) $" "y" + } else { + set bp_func "foo" + + gdb_test "dprintf $bp_func,\"in $bp_func\"" ".*" \ + "create dprintf breakpoint" + } + + set bp_number [get_integer_valueof "\$bpnum" "INVALID" \ + "get b/p number for previous breakpoint"] + + if { $bp_pending } { + set bp_pattern_before \ + [multi_line \ + "$bp_number\\s+dprintf\\s+keep\\s+y\\s+\\s+${bp_func}" \ + "\\s+printf \"in $bp_func\""] + set bp_pattern_after $bp_pattern_before + } else { + set bp_pattern_before \ + [multi_line \ + "$bp_number\\s+dprintf\\s+keep\\s+y\\s+\\s*" \ + "\\s+printf \"in $bp_func\"" \ + "$bp_number\\.1\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 1" \ + "$bp_number\\.2\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 2"] + + set bp_pattern_after \ + [multi_line \ + "$bp_number\\s+dprintf\\s+keep\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+" \ + "\\s+printf \"in $bp_func\""] + } + + gdb_test "info breakpoints" $bp_pattern_before \ + "info breakpoints before inferior removal" + + kill_and_remove_inferior_2 + + gdb_test "info breakpoints" $bp_pattern_after \ + "info breakpoints after inferior removal" +} + +foreach_with_prefix bp_pending { true false } { + foreach_with_prefix bp_type { thread inferior } { + do_bp_test $bp_type $bp_pending + } + + do_dprintf_test $bp_pending +} From patchwork Sat Jul 20 17:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94259 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 AAC5B3860761 for ; Sat, 20 Jul 2024 17:04:50 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id E9711385DDE5 for ; Sat, 20 Jul 2024 17:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E9711385DDE5 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 E9711385DDE5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495024; cv=none; b=ZT4nt4DZCTF6EMv4kKuTSeGTbXwtX5+fwZ2p1gyYxOQcOYm0bjCj99fxij61gReVma1gl85Dsz+IF8DBDZ4KKJtyLnmLBJvPRSN0uy2EdieC4zdFgueu2zaHdHtMQP0UUcrRM4dySny0l4qromwiAJKhW1d1RiaFPEsloTgF4ew= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495024; c=relaxed/simple; bh=T/eJX5wzHCL26tJYrfu3n3BWQT0LQAM78w+Hbq6BzT0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FX3xursLOuvhlPun53rU13+mEbdIb384U1kHN4J0cb1Rd7GWbNUcJcdfCNjkPHHfso/yI0R7hvj+Vimh9ZNLUjaNEe/BBsLLkYGUV76VgKyfNn9KB4dbYBv2RoQ77xYmQfuOHwaQ0TXwCuNhZ3Xkfp9Mk8cNiSt653wpegoJYnk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495017; 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=hKrdb3l+UWYrnOWhKqWibmudXjOFGPoSC/BpvFmIo+w=; b=SiU/Fx7XB5vbqyFtTfEalHJCxMMrbBS2EbJoR+ULFY4VoKW9xvt+CtNQUUS6lhDRf5ap+Y GzGCgw0erwNS5bBFEAB1Tt34kbwbBvZaKFQl39FfG/9xI8r6tBXNIsgs+Xb6O/MNPaQvdb KilcktlOHcSkGFqhMiwYEV4VqnLG3PU= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-216-g1F-BH2wOg60V-TX7GQyGA-1; Sat, 20 Jul 2024 13:03:36 -0400 X-MC-Unique: g1F-BH2wOg60V-TX7GQyGA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4272718b9b0so21035605e9.1 for ; Sat, 20 Jul 2024 10:03:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495014; x=1722099814; 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=kMkJBau1vt8HjE3i21Yivz8d64p2mLv3a6zMcqlBQiI=; b=QWQ322mzBTxj2Lcig+9ID7Ven4nF2bfpZFkCYFwr+WgvLdtYa8x7YsymxpLKOGpEfQ n1DSS4muawhBefTu4yrDGg4Bx7N5Ajy0nqP9rL7zo4L0Qv79E+Oswe79G003ATXVysgc UKj1LpQvArJGUt94UFnXAjtxNOW+OSrbGflq8ZkJ9gOzPiQj+Wlvz64EvIWoIWHVqy/V gT5SHssbeuyb3ztxBz5jQRjL3aug4r3xFexCbsmCtnNpzX9c/06gB3xccr9j85vD8Qjv pSDerq63JbOpIT1RluvQ9DbxwSZMYj8idvYWiGbEUgATHgkMDuT3lEVpQ6lEUwlxXGGQ rd0A== X-Gm-Message-State: AOJu0Yympcmf1iAuQMDYS9t5QSaQ5C11Tl7+ca8Z28AlWdPQjomVKC7M 2MUpB3+KmQsGtJrKKP9IBXtWA8mQhFK5Tmxz4hfrxpaaySsihJYTPWTU9E84TK1E0lgt5ERb1S2 zjY6XrOO1AldEHbJliTiy+hE3kP+RaHY5F6gdcId6ms3an77fJP6xo9yjBoxG73130B9c8Pxh8m 5kgBAbn6vvnmsyNDP9T2UWewm1nynx2QbDEa5DUlnXgK8= X-Received: by 2002:a05:600c:1987:b0:426:6f3e:feef with SMTP id 5b1f17b1804b1-427dc576736mr14618015e9.29.1721495013546; Sat, 20 Jul 2024 10:03:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZi7YirsDuEXCXefKr0x3CqL1+gGT8mW4yqmJqVxjoe2+Yox9zFQUM8fls6s0xdLZ4pvb/WQ== X-Received: by 2002:a05:600c:1987:b0:426:6f3e:feef with SMTP id 5b1f17b1804b1-427dc576736mr14617735e9.29.1721495012625; Sat, 20 Jul 2024 10:03:32 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d2a6efc5sm90385275e9.21.2024.07.20.10.03.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:32 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv12 6/6] gdb: only insert thread-specific breakpoints in the relevant inferior Date: Sat, 20 Jul 2024 18:03:19 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 This commit updates GDB so that thread or inferior specific breakpoints are only inserted into the program space in which the specific thread or inferior is running. In terms of implementation, getting this basically working is easy enough, now that a breakpoint's thread or inferior field is setup prior to GDB looking for locations, we can easily use this information to find a suitable program_space and pass this to as a filter when creating the sals. Or we could if breakpoint_ops::create_sals_from_location_spec allowed us to pass in a filter program_space. So, this commit extends breakpoint_ops::create_sals_from_location_spec to take a program_space argument, and uses this to filter the set of returned sals. This accounts for about half the change in this patch. The second set of changes starts from breakpoint_set_thread and breakpoint_set_inferior, this is called when the thread or inferior for a breakpoint changes, e.g. from the Python API. Previously this call would never result in the locations of a breakpoint changing, after all, locations were inserted in every program space, and we just use the thread or inferior variable to decide when we should stop. Now though, changing a breakpoint's thread or inferior can mean we need to figure out a new set of breakpoint locations. To support this I've added a new breakpoint_re_set_one function, which is like breakpoint_re_set, but takes a single breakpoint, and just updates the locations for that one breakpoint. We only need to call this function if the program_space in which a breakpoint's thread (or inferior) is running actually changes. If the program_space does change then we call the new breakpoint_re_set_one function passing in the program_space which should be used to filter the new locations (or nullptr to indicate we should set locations in all program spaces). This filter program_space needs to propagate down to all the re_set methods, this accounts for the remaining half of the changes in this patch. There were a couple of existing tests that created thread or inferior specific breakpoints and then checked the 'info breakpoints' output, these needed updating. These were: gdb.mi/user-selected-context-sync.exp gdb.multi/bp-thread-specific.exp gdb.multi/multi-target-continue.exp gdb.multi/multi-target-ping-pong-next.exp gdb.multi/tids.exp gdb.mi/new-ui-bp-deleted.exp gdb.multi/inferior-specific-bp.exp gdb.multi/pending-bp-del-inferior.exp I've also added some additional tests to: gdb.multi/pending-bp.exp I've updated the documentation and added a NEWS entry. Reviewed-By: Eli Zaretskii Reviewed-By: Eli Zaretskii --- gdb/NEWS | 7 + gdb/ada-lang.c | 6 +- gdb/break-catch-throw.c | 6 +- gdb/breakpoint.c | 280 ++++++++++++++---- gdb/breakpoint.h | 29 +- gdb/testsuite/gdb.mi/new-ui-bp-deleted.exp | 8 +- .../gdb.mi/user-selected-context-sync.exp | 14 +- .../gdb.multi/bp-thread-specific.exp | 7 +- .../gdb.multi/inferior-specific-bp.exp | 12 +- .../gdb.multi/multi-target-continue.exp | 2 +- .../gdb.multi/multi-target-ping-pong-next.exp | 4 +- .../gdb.multi/pending-bp-del-inferior.exp | 6 +- gdb/testsuite/gdb.multi/pending-bp.exp | 206 +++++++++++++ gdb/testsuite/gdb.multi/tids.exp | 6 +- 14 files changed, 486 insertions(+), 107 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index f140a28b795..b86c483a7e8 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -32,6 +32,13 @@ 'thread' or 'task' keywords are parsed at the time the breakpoint is created, rather than at the time the breakpoint becomes non-pending. +* Thread-specific breakpoints are only inserted into the program space + in which the thread of interest is running. In most cases program + spaces are unique for each inferior, so this means that + thread-specific breakpoints will usually only be inserted for the + inferior containing the thread of interest. The breakpoint will + be hit no less than before. + * Changed commands disassemble diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index e2be7f97224..723510c9d7a 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -12078,11 +12078,11 @@ struct ada_catchpoint : public code_breakpoint enable_state = enabled ? bp_enabled : bp_disabled; language = language_ada; - re_set (); + re_set (pspace); } struct bp_location *allocate_location () override; - void re_set () override; + void re_set (program_space *pspace) override; void check_status (struct bpstat *bs) override; enum print_stop_action print_it (const bpstat *bs) const override; bool print_one (const bp_location **) const override; @@ -12127,7 +12127,7 @@ static struct symtab_and_line ada_exception_sal catchpoint kinds. */ void -ada_catchpoint::re_set () +ada_catchpoint::re_set (program_space *pspace) { std::vector sals; try diff --git a/gdb/break-catch-throw.c b/gdb/break-catch-throw.c index b7e29a79dd8..c1c88d96e10 100644 --- a/gdb/break-catch-throw.c +++ b/gdb/break-catch-throw.c @@ -81,10 +81,10 @@ struct exception_catchpoint : public code_breakpoint _("invalid type-matching regexp"))) { pspace = current_program_space; - re_set (); + re_set (pspace); } - void re_set () override; + void re_set (program_space *pspace) override; enum print_stop_action print_it (const bpstat *bs) const override; bool print_one (const bp_location **) const override; void print_mention () const override; @@ -197,7 +197,7 @@ exception_catchpoint::check_status (struct bpstat *bs) /* Implement the 're_set' method. */ void -exception_catchpoint::re_set () +exception_catchpoint::re_set (program_space *pspace) { std::vector sals; struct program_space *filter_pspace = current_program_space; diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index c483d80c98d..60c27cf78e8 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -91,9 +91,12 @@ static void map_breakpoint_numbers (const char *, gdb::function_view); -static void - create_sals_from_location_spec_default (location_spec *locspec, - linespec_result *canonical); +static void parse_breakpoint_sals (location_spec *locspec, + linespec_result *canonical, + program_space *search_pspace); + +static void breakpoint_re_set_one (breakpoint *b, + program_space *filter_pspace); static void create_breakpoints_sal (struct gdbarch *, struct linespec_result *, @@ -283,11 +286,12 @@ static bool strace_marker_p (struct breakpoint *b); static void bkpt_probe_create_sals_from_location_spec (location_spec *locspec, - struct linespec_result *canonical); + struct linespec_result *canonical, + struct program_space *search_pspace); const struct breakpoint_ops code_breakpoint_ops = { - create_sals_from_location_spec_default, + parse_breakpoint_sals, create_breakpoints_sal, }; @@ -352,7 +356,7 @@ struct internal_breakpoint : public code_breakpoint disposition = disp_donttouch; } - void re_set () override; + void re_set (program_space *pspace) override; void check_status (struct bpstat *bs) override; enum print_stop_action print_it (const bpstat *bs) const override; void print_mention () const override; @@ -389,7 +393,7 @@ struct momentary_breakpoint : public code_breakpoint gdb_assert (inferior == -1); } - void re_set () override; + void re_set (program_space *pspace) override; void check_status (struct bpstat *bs) override; enum print_stop_action print_it (const bpstat *bs) const override; void print_mention () const override; @@ -400,7 +404,7 @@ struct dprintf_breakpoint : public ordinary_breakpoint { using ordinary_breakpoint::ordinary_breakpoint; - void re_set () override; + void re_set (program_space *pspace) override; int breakpoint_hit (const struct bp_location *bl, const address_space *aspace, CORE_ADDR bp_addr, @@ -1549,7 +1553,36 @@ breakpoint_set_thread (struct breakpoint *b, int thread) int old_thread = b->thread; b->thread = thread; if (old_thread != thread) - notify_breakpoint_modified (b); + { + /* If THREAD is in a different program_space than OLD_THREAD, or the + breakpoint has switched to or from being thread-specific, then we + need to re-set the locations of this breakpoint. First, figure + out the program_space for the old and new threads, use a value of + nullptr to indicate the breakpoint is in all program spaces. */ + program_space *old_pspace = nullptr; + if (old_thread != -1) + { + struct thread_info *thr = find_thread_global_id (old_thread); + gdb_assert (thr != nullptr); + old_pspace = thr->inf->pspace; + } + + program_space *new_pspace = nullptr; + if (thread != -1) + { + struct thread_info *thr = find_thread_global_id (thread); + gdb_assert (thr != nullptr); + new_pspace = thr->inf->pspace; + } + + /* If the program space has changed for this breakpoint, then + re-evaluate it's locations. */ + if (old_pspace != new_pspace) + breakpoint_re_set_one (b, new_pspace); + + /* Let others know the breakpoint has changed. */ + notify_breakpoint_modified (b); + } } /* See breakpoint.h. */ @@ -1568,7 +1601,34 @@ breakpoint_set_inferior (struct breakpoint *b, int inferior) int old_inferior = b->inferior; b->inferior = inferior; if (old_inferior != inferior) - notify_breakpoint_modified (b); + { + /* If INFERIOR is in a different program_space than OLD_INFERIOR, or + the breakpoint has switch to or from inferior-specific, then we + need to re-set the locations of this breakpoint. First, figure + out the program_space for the old and new inferiors, use a value + of nullptr to indicate the breakpoint is in all program + spaces. */ + program_space *old_pspace = nullptr; + if (old_inferior != -1) + { + struct inferior *inf = find_inferior_id (old_inferior); + gdb_assert (inf != nullptr); + old_pspace = inf->pspace; + } + + program_space *new_pspace = nullptr; + if (inferior != -1) + { + struct inferior *inf = find_inferior_id (inferior); + gdb_assert (inf != nullptr); + new_pspace = inf->pspace; + } + + if (old_pspace != new_pspace) + breakpoint_re_set_one (b, new_pspace); + + notify_breakpoint_modified (b); + } } /* See breakpoint.h. */ @@ -8804,7 +8864,8 @@ create_breakpoints_sal (struct gdbarch *gdbarch, static void parse_breakpoint_sals (location_spec *locspec, - struct linespec_result *canonical) + struct linespec_result *canonical, + struct program_space *search_pspace) { if (locspec->type () == LINESPEC_LOCATION_SPEC) { @@ -8868,7 +8929,7 @@ parse_breakpoint_sals (location_spec *locspec, && strchr ("+-", spec[0]) != NULL && spec[1] != '[')) { - decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL, + decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace, get_last_displayed_symtab (), get_last_displayed_line (), canonical, NULL, NULL); @@ -8876,7 +8937,7 @@ parse_breakpoint_sals (location_spec *locspec, } } - decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL, + decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace, cursal.symtab, cursal.line, canonical, NULL, NULL); } @@ -8975,6 +9036,39 @@ breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type, } } +/* Return the program space to use as a filter when searching for locations + of a breakpoint specific to THREAD or INFERIOR. If THREAD and INFERIOR + are both -1, meaning all threads/inferiors, then this function returns + nullptr, indicating no program space filtering should be performed. + Otherwise, this function returns the program space for the inferior that + contains THREAD (when THREAD is not -1), or the program space for + INFERIOR (when INFERIOR is not -1). */ + +static struct program_space * +find_program_space_for_breakpoint (int thread, int inferior) +{ + if (thread != -1) + { + gdb_assert (inferior == -1); + + struct thread_info *thr = find_thread_global_id (thread); + gdb_assert (thr != nullptr); + gdb_assert (thr->inf != nullptr); + return thr->inf->pspace; + } + else if (inferior != -1) + { + gdb_assert (thread == -1); + + struct inferior *inf = find_inferior_id (inferior); + gdb_assert (inf != nullptr); + + return inf->pspace; + } + + return nullptr; +} + /* See breakpoint.h. */ const struct breakpoint_ops * @@ -9076,7 +9170,10 @@ create_breakpoint (struct gdbarch *gdbarch, try { - ops->create_sals_from_location_spec (locspec, &canonical); + struct program_space *search_pspace + = find_program_space_for_breakpoint (thread, inferior); + ops->create_sals_from_location_spec (locspec, &canonical, + search_pspace); } catch (const gdb_exception_error &e) { @@ -9549,7 +9646,7 @@ break_range_command (const char *arg, int from_tty) arg_start = arg; location_spec_up start_locspec = string_to_location_spec (&arg, current_language); - parse_breakpoint_sals (start_locspec.get (), &canonical_start); + parse_breakpoint_sals (start_locspec.get (), &canonical_start, nullptr); if (arg[0] != ',') error (_("Too few arguments.")); @@ -9650,7 +9747,7 @@ watchpoint_exp_is_const (const struct expression *exp) /* Implement the "re_set" method for watchpoints. */ void -watchpoint::re_set () +watchpoint::re_set (struct program_space *pspace) { /* Watchpoint can be either on expression using entirely global variables, or it can be on local variables. @@ -11761,7 +11858,7 @@ breakpoint::print_recreate (struct ui_file *fp) const /* Default breakpoint_ops methods. */ void -code_breakpoint::re_set () +code_breakpoint::re_set (struct program_space *pspace) { /* FIXME: is this still reachable? */ if (breakpoint_location_spec_empty_p (this)) @@ -11771,7 +11868,7 @@ code_breakpoint::re_set () return; } - re_set_default (); + re_set_default (pspace); } int @@ -11977,7 +12074,7 @@ code_breakpoint::decode_location_spec (location_spec *locspec, /* Virtual table for internal breakpoints. */ void -internal_breakpoint::re_set () +internal_breakpoint::re_set (struct program_space *pspace) { switch (type) { @@ -12070,7 +12167,7 @@ internal_breakpoint::print_mention () const /* Virtual table for momentary breakpoints */ void -momentary_breakpoint::re_set () +momentary_breakpoint::re_set (struct program_space *pspace) { /* Keep temporary breakpoints, which can be encountered when we step over a dlopen call and solib_add is resetting the breakpoints. @@ -12111,12 +12208,13 @@ longjmp_breakpoint::~longjmp_breakpoint () static void bkpt_probe_create_sals_from_location_spec (location_spec *locspec, - struct linespec_result *canonical) + struct linespec_result *canonical, + struct program_space *search_pspace) { struct linespec_sals lsal; - lsal.sals = parse_probes (locspec, NULL, canonical); + lsal.sals = parse_probes (locspec, search_pspace, canonical); lsal.canonical = xstrdup (canonical->locspec->to_string ()); canonical->lsals.push_back (std::move (lsal)); } @@ -12206,9 +12304,9 @@ tracepoint::print_recreate (struct ui_file *fp) const } void -dprintf_breakpoint::re_set () +dprintf_breakpoint::re_set (struct program_space *pspace) { - re_set_default (); + re_set_default (pspace); /* 1 - connect to target 1, that can run breakpoint commands. 2 - create a dprintf, which resolves fine. @@ -12262,8 +12360,10 @@ dprintf_breakpoint::after_condition_true (struct bpstat *bs) markers (`-m'). */ static void -strace_marker_create_sals_from_location_spec (location_spec *locspec, - struct linespec_result *canonical) +strace_marker_create_sals_from_location_spec + (location_spec *locspec, + struct linespec_result *canonical, + struct program_space *search_pspace) { struct linespec_sals lsal; const char *arg_start, *arg; @@ -12780,12 +12880,32 @@ update_breakpoint_locations (code_breakpoint *b, all locations are in the same shared library, that was unloaded. We'd like to retain the location, so that when the library is loaded again, we don't loose the enabled/disabled status of the - individual locations. */ + individual locations. + + Thread specific breakpoints will also trigger this case if the thread + is changed to a different program space, and all of the old locations + go out of scope. In this case we do (currently) discard the old + locations -- we assume the change in thread is permanent and the old + locations will never come back into scope. */ if (all_locations_are_pending (b, filter_pspace) && sals.empty ()) - return; + { + if (b->thread != -1) + b->clear_locations (); + return; + } bp_location_list existing_locations = b->steal_locations (filter_pspace); + /* If this is a thread-specific breakpoint then any locations left on the + breakpoint are for a program space in which the thread of interest + does not operate. This can happen when the user changes the thread of + a thread-specific breakpoint. + + We assume that the change in thread is permanent, and that the old + locations will never be used again, so discard them now. */ + if (b->thread != -1) + b->clear_locations (); + for (const auto &sal : sals) { struct bp_location *new_loc; @@ -12951,40 +13071,45 @@ code_breakpoint::location_spec_to_sals (location_spec *locspec, locations. */ void -code_breakpoint::re_set_default () +code_breakpoint::re_set_default (struct program_space *filter_pspace) { - struct program_space *filter_pspace = current_program_space; std::vector expanded, expanded_end; - int found; - std::vector sals = location_spec_to_sals (locspec.get (), - filter_pspace, - &found); - if (found) - expanded = std::move (sals); - - if (locspec_range_end != nullptr) - { - std::vector sals_end - = location_spec_to_sals (locspec_range_end.get (), - filter_pspace, &found); + /* If this breakpoint is thread-specific then find the program space in + which the specific thread exists. Otherwise, for breakpoints that are + not thread-specific THREAD_PSPACE will be nullptr. */ + program_space *bp_pspace + = find_program_space_for_breakpoint (this->thread, this->inferior); + + /* If this is not a thread or inferior specific breakpoint, or it is a + thread or inferior specific breakpoint but we are looking for new + locations in the program space that the specific thread or inferior is + running, then look for new locations for this breakpoint. */ + if (bp_pspace == nullptr || filter_pspace == bp_pspace) + { + int found; + std::vector sals + = location_spec_to_sals (locspec.get (), filter_pspace, &found); if (found) - expanded_end = std::move (sals_end); + expanded = std::move (sals); + + if (locspec_range_end != nullptr) + { + std::vector sals_end + = location_spec_to_sals (locspec_range_end.get (), + filter_pspace, &found); + if (found) + expanded_end = std::move (sals_end); + } } + /* Update the locations for this breakpoint. For thread-specific + breakpoints this will remove any old locations that are for the wrong + program space -- this can happen if the user changes the thread of a + thread-specific breakpoint. */ update_breakpoint_locations (this, filter_pspace, expanded, expanded_end); } -/* Default method for creating SALs from an address string. It basically - calls parse_breakpoint_sals. Return 1 for success, zero for failure. */ - -static void -create_sals_from_location_spec_default (location_spec *locspec, - struct linespec_result *canonical) -{ - parse_breakpoint_sals (locspec, canonical); -} - /* Re-set breakpoint locations for the current program space. Locations bound to other program spaces are left untouched. */ @@ -13019,7 +13144,7 @@ breakpoint_re_set (void) { input_radix = b.input_radix; set_language (b.language); - b.re_set (); + b.re_set (current_program_space); } catch (const gdb_exception &ex) { @@ -13040,6 +13165,53 @@ breakpoint_re_set (void) /* Now we can insert. */ update_global_location_list (UGLL_MAY_INSERT); } + +/* Re-set locations for breakpoint B in FILTER_PSPACE. If FILTER_PSPACE is + nullptr then re-set locations for B in all program spaces. Locations + bound to program spaces other than FILTER_PSPACE are left untouched. */ + +static void +breakpoint_re_set_one (breakpoint *b, program_space *filter_pspace) +{ + { + scoped_restore_current_language save_language; + scoped_restore save_input_radix = make_scoped_restore (&input_radix); + scoped_restore_current_pspace_and_thread restore_pspace_thread; + + /* To ::re_set each breakpoint we set the current_language to the + language of the breakpoint before re-evaluating the breakpoint's + location. This change can unfortunately get undone by accident if + the language_mode is set to auto, and we either switch frames, or + more likely in this context, we select the current frame. + + We prevent this by temporarily turning the language_mode to + language_mode_manual. We restore it once all breakpoints + have been reset. */ + scoped_restore save_language_mode = make_scoped_restore (&language_mode); + language_mode = language_mode_manual; + + /* Note: we must not try to insert locations until after all + breakpoints have been re-set. Otherwise, e.g., when re-setting + breakpoint 1, we'd insert the locations of breakpoint 2, which + hadn't been re-set yet, and thus may have stale locations. */ + + try + { + input_radix = b->input_radix; + set_language (b->language); + b->re_set (filter_pspace); + } + catch (const gdb_exception &ex) + { + exception_fprintf (gdb_stderr, ex, + "Error in re-setting breakpoint %d: ", + b->number); + } + } + + /* Now we can insert. */ + update_global_location_list (UGLL_MAY_INSERT); +} /* Reset the thread number of this breakpoint: diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h index 62389ba3d6e..ca4dd9ae790 100644 --- a/gdb/breakpoint.h +++ b/gdb/breakpoint.h @@ -568,15 +568,15 @@ enum print_stop_action struct breakpoint_ops { - /* Create SALs from location spec, storing the result in - linespec_result. - - For an explanation about the arguments, see the function - `create_sals_from_location_spec_default'. + /* Create SALs from LOCSPEC, storing the result in linespec_result + CANONICAL. If SEARCH_PSPACE is not nullptr then only results in the + corresponding program space are returned. If SEARCH_PSPACE is nullptr + then results for all program spaces are returned. This function is called inside `create_breakpoint'. */ void (*create_sals_from_location_spec) (location_spec *locspec, - struct linespec_result *canonical); + linespec_result *canonical, + program_space *search_pspace); /* This method will be responsible for creating a breakpoint given its SALs. Usually, it just calls `create_breakpoints_sal' (for ordinary @@ -708,8 +708,15 @@ struct breakpoint : public intrusive_list_node /* Reevaluate a breakpoint. This is necessary after symbols change (e.g., an executable or DSO was loaded, or the inferior just - started). */ - virtual void re_set () + started). + + If not nullptr, then FILTER_PSPACE is the program space in which + symbols may have changed, we only need to add new locations in + FILTER_PSPACE. + + If FILTER_PSPACE is nullptr then all program spaces may have changed, + new locations need to be searched for in every program space. */ + virtual void re_set (program_space *filter_pspace) { /* Nothing to re-set. */ } @@ -953,7 +960,7 @@ struct code_breakpoint : public breakpoint /* Add a location for SAL to this breakpoint. */ bp_location *add_location (const symtab_and_line &sal); - void re_set () override; + void re_set (program_space *pspace) override; int insert_location (struct bp_location *) override; int remove_location (struct bp_location *, enum remove_bp_reason reason) override; @@ -975,7 +982,7 @@ struct code_breakpoint : public breakpoint struct program_space *search_pspace); /* Helper method that does the basic work of re_set. */ - void re_set_default (); + void re_set_default (program_space *pspace); /* Find the SaL locations corresponding to the given LOCATION. On return, FOUND will be 1 if any SaL was found, zero otherwise. */ @@ -997,7 +1004,7 @@ struct watchpoint : public breakpoint { using breakpoint::breakpoint; - void re_set () override; + void re_set (program_space *pspace) override; int insert_location (struct bp_location *) override; int remove_location (struct bp_location *, enum remove_bp_reason reason) override; diff --git a/gdb/testsuite/gdb.mi/new-ui-bp-deleted.exp b/gdb/testsuite/gdb.mi/new-ui-bp-deleted.exp index f736994d234..938e6deec05 100644 --- a/gdb/testsuite/gdb.mi/new-ui-bp-deleted.exp +++ b/gdb/testsuite/gdb.mi/new-ui-bp-deleted.exp @@ -76,8 +76,12 @@ foreach_mi_ui_mode mode { set loc2 [make_bp_loc "$::decimal\\.2"] # Create the inferior-specific breakpoint. - mi_create_breakpoint_multi "-g i2 foo" "create breakpoint in inferior 2" \ - -inferior "2" -locations "\\\[$loc1,$loc2\\\]" + mi_create_breakpoint "-g i2 foo" "create breakpoint in inferior 2" \ + -number "$decimal" \ + -type "breakpoint" \ + -enabled "y" \ + -func "foo" \ + -inferior "2" set bpnum [mi_get_valueof "/d" "\$bpnum" "INVALID"] if {$mode eq "separate"} { diff --git a/gdb/testsuite/gdb.mi/user-selected-context-sync.exp b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp index 93b91b42f92..e168a5eee45 100644 --- a/gdb/testsuite/gdb.mi/user-selected-context-sync.exp +++ b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp @@ -364,15 +364,9 @@ proc test_continue_to_start { mode inf } { # Consume MI output. with_spawn_id $mi_spawn_id { - if { $inf == 1} { - mi_expect_stop "breakpoint-hit" "child_sub_function" \ - "" "$srcfile" "$decimal" {"" "disp=\"del\""} \ - "thread $inf.2 stops MI" - } else { - mi_expect_stop "breakpoint-hit" "child_sub_function" \ - "" "$srcfile" "$decimal" {"" "disp=\"del\"" "locno=\"[0-9]+\""} \ - "thread $inf.2 stops MI" - } + mi_expect_stop "breakpoint-hit" "child_sub_function" \ + "" "$srcfile" "$decimal" {"" "disp=\"del\""} \ + "thread $inf.2 stops MI" } } } @@ -439,7 +433,7 @@ proc_with_prefix test_setup { mode } { with_spawn_id $mi_spawn_id { mi_expect_stop "breakpoint-hit" "main" "" "$srcfile" "$decimal" \ - {"" "disp=\"del\"" "locno=\"[0-9]+\""} "main stop" + {"" "disp=\"del\""} "main stop" } # Consume CLI output. diff --git a/gdb/testsuite/gdb.multi/bp-thread-specific.exp b/gdb/testsuite/gdb.multi/bp-thread-specific.exp index 7635e84b913..c1d87521ee9 100644 --- a/gdb/testsuite/gdb.multi/bp-thread-specific.exp +++ b/gdb/testsuite/gdb.multi/bp-thread-specific.exp @@ -50,7 +50,7 @@ gdb_test "info threads" \ # locations ('foo' in both inferiors) even though only one of those # locations will ever trigger ('foo' in inferior 2). gdb_test "break foo thread 2.1" \ - "Breakpoint $decimal at $hex: foo\\. \\(2 locations\\)" + "Breakpoint $decimal at $hex: file \[^\r\n\]+$srcfile, line $decimal\\." set bpnum [get_integer_valueof "\$bpnum" "INVALID"] @@ -58,10 +58,7 @@ set bpnum [get_integer_valueof "\$bpnum" "INVALID"] # earlier breakpoint. Check that the thread-id used when describing # the earlier breakpoints is correct. gdb_test "break foo thread 1.1" \ - [multi_line \ - "Note: breakpoint $bpnum \\(thread 2.1\\) also set at pc $hex\\." \ - "Note: breakpoint $bpnum \\(thread 2.1\\) also set at pc $hex\\." \ - "Breakpoint $decimal at $hex: foo\\. \\(2 locations\\)"] + "Breakpoint $decimal at $hex: file \[^\r\n\]+$srcfile, line $decimal\\." # Save the breakpoints into a file. if {[is_remote host]} { diff --git a/gdb/testsuite/gdb.multi/inferior-specific-bp.exp b/gdb/testsuite/gdb.multi/inferior-specific-bp.exp index 46efe6f54bc..52f84183589 100644 --- a/gdb/testsuite/gdb.multi/inferior-specific-bp.exp +++ b/gdb/testsuite/gdb.multi/inferior-specific-bp.exp @@ -105,16 +105,8 @@ proc check_info_breakpoints { testname bp_number expected_loc_count } { # Create an inferior-specific breakpoint. Use gdb_test instead of # gdb_breakpoint here as we want to check the breakpoint was placed in # multiple locations. -# -# Currently GDB still places inferior specific breakpoints into every -# inferior, just like it does with thread specific breakpoints. -# Hopefully this will change in the future, at which point, this test -# will need updating. -# -# Two of these locations are in inferior 1, while the third is in -# inferior 2. gdb_test "break foo inferior 1" \ - "Breakpoint $decimal at $hex: foo\\. \\(3 locations\\)" + "Breakpoint $decimal at $hex: foo\\. \\(2 locations\\)" set bp_number [get_integer_valueof "\$bpnum" "INVALID" \ "get b/p number for inferior specific breakpoint"] @@ -123,7 +115,7 @@ set location_count 0 set saw_inf_cond false check_info_breakpoints "first check for inferior specific breakpoint" \ - $bp_number 3 + $bp_number 2 # Create a multi-inferior breakpoint to stop at. gdb_breakpoint "stop_breakpt" message diff --git a/gdb/testsuite/gdb.multi/multi-target-continue.exp b/gdb/testsuite/gdb.multi/multi-target-continue.exp index d2201061713..d4b2fc28133 100644 --- a/gdb/testsuite/gdb.multi/multi-target-continue.exp +++ b/gdb/testsuite/gdb.multi/multi-target-continue.exp @@ -30,7 +30,7 @@ proc test_continue {non-stop} { proc set_break {inf} { gdb_test "break function${inf} thread ${inf}.1" \ - "Breakpoint .* function${inf}\\..*" + "Breakpoint ${::decimal} at ${::hex}: file .*, line ${::decimal}\\." } # Select inferior INF, and then run to a breakpoint on inferior diff --git a/gdb/testsuite/gdb.multi/multi-target-ping-pong-next.exp b/gdb/testsuite/gdb.multi/multi-target-ping-pong-next.exp index 0aff708c0f3..36f9d24a917 100644 --- a/gdb/testsuite/gdb.multi/multi-target-ping-pong-next.exp +++ b/gdb/testsuite/gdb.multi/multi-target-ping-pong-next.exp @@ -52,12 +52,12 @@ proc test_ping_pong_next {} { gdb_test "thread 1.1" "Switching to thread 1.1 .*" gdb_test "break $srcfile:$line1 thread 1.1" \ - "Breakpoint .*$srcfile:$line1\\..*" + "Breakpoint .*$srcfile, line $line1\\." gdb_test "continue" "hit Breakpoint .*" gdb_test "break $srcfile:$line2 thread 2.1" \ - "Breakpoint .*$srcfile:$line2\\..*" + "Breakpoint .*$srcfile, line $line2\\." # Now block inferior 1 and issue "next". We should stop at the # breakpoint for inferior 2, given schedlock off. diff --git a/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp index 5fcd1ef2e39..12c0a84bb02 100644 --- a/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp +++ b/gdb/testsuite/gdb.multi/pending-bp-del-inferior.exp @@ -129,10 +129,8 @@ proc do_bp_test { bp_type bp_pending } { } else { set bp_pattern_before \ [multi_line \ - "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+\\s*" \ - "\\s+stop only in [string_to_regexp $bp_restriction]" \ - "$bp_number\\.1\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 1" \ - "$bp_number\\.2\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 2"] + "$bp_number\\s+breakpoint\\s+keep\\s+y\\s+$::hex in $bp_func at \[^\r\n\]+ inf 1" \ + "\\s+stop only in [string_to_regexp $bp_restriction]"] set bp_pattern_after \ [multi_line \ diff --git a/gdb/testsuite/gdb.multi/pending-bp.exp b/gdb/testsuite/gdb.multi/pending-bp.exp index 13f76f49102..2a0644bf48e 100644 --- a/gdb/testsuite/gdb.multi/pending-bp.exp +++ b/gdb/testsuite/gdb.multi/pending-bp.exp @@ -72,6 +72,48 @@ proc do_test_setup { inf_1_stop inf_2_stop } { return true } +# Create a breakpoint on the function 'foo' in THREAD. It is expected +# that the breakpoint created will be pending, this is checked by +# running the 'info breakpoints' command. +# +# Returns the number for the newly created breakpoint. +proc do_create_pending_foo_breakpoint { {thread "1.1"} } { + gdb_test "break foo thread $thread" \ + [multi_line \ + "Function \"foo\" not defined\\." \ + "Breakpoint $::decimal \\(foo\\) pending\."] \ + "set pending thread-specific breakpoint" + set bpnum [get_integer_valueof "\$bpnum" "*INVALID*" \ + "get number for thread-specific breakpoint on foo"] + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+\\s+foo" \ + "\\s+stop only in thread [string_to_regexp $thread]"] \ + "check thread-specific breakpoint is initially pending" + + return $bpnum +} + +# Create a breakpoint on the function 'foo' in THREAD. It is expected +# that the breakpoint created will not be pending, this is checked by +# running the 'info breakpoints' command. +# +# Returns the number for the newly created breakpoint. +proc do_create_foo_breakpoint { {thread "1.1"} } { + gdb_test "break foo thread $thread" \ + "Breakpoint $::decimal at $::hex" \ + "set thread-specific breakpoint" + set bpnum [get_integer_valueof "\$bpnum" "*INVALID*" \ + "get number for thread-specific breakpoint on foo"] + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+$::hex\\s+\]*> inf $::decimal" \ + "\\s+stop only in thread [string_to_regexp $thread]"] \ + "check thread-specific breakpoint is initially pending" + + return $bpnum +} + # Check that when a breakpoint is in the pending state, but that breakpoint # does have some locations (those locations themselves are pending), GDB # doesn't display the inferior list in the 'info breakpoints' output. @@ -122,5 +164,169 @@ proc_with_prefix test_no_inf_display {} { "check info breakpoints while breakpoint is pending" } +# Setup two inferiors. In #1 the symbol 'foo' has not yet been +# loaded, while in #2 the symbol 'foo' has been loaded. +# +# Create a thread-specific breakpoint on 'foo' tied to a thread in +# inferior #1, the breakpoint should be pending -- 'foo' is not yet +# loaded in #1. +# +# Now move inferior #1 forward until 'foo' is loaded, check the +# breakpoint is no longer pending. +# +# Move inferior #1 forward more until 'foo' is unloaded, check that +# the breakpoint returns to the pending state. +proc_with_prefix test_pending_toggle { } { + + do_test_setup "Break before open" "Break before close" + + set bpnum [do_create_pending_foo_breakpoint] + + # Now return to inferior 1 and continue until the shared library is + # loaded, the breakpoint should become non-pending. + gdb_test "inferior 1" "Switching to inferior 1 .*" \ + "switch back to inferior 1" + gdb_continue_to_breakpoint "stop in foo in inferior 1" "foo \\(\\) .*" + + gdb_test "info breakpoint $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+$::hex \]*> inf 1" \ + "\\s+stop only in thread 1\\.1" \ + "\\s+breakpoint already hit 1 time"] \ + "check thread-specific breakpoint is no longer pending" + + gdb_breakpoint [gdb_get_line_number "Break after close"] temporary + gdb_continue_to_breakpoint "close library" + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+\\s+foo" \ + "\\s+stop only in thread 1\\.1" \ + "\\s+breakpoint already hit 1 time"] \ + "check thread-specific breakpoint is pending again" +} + +# Create a Python variable VAR and set it to the gdb.Breakpoint object +# corresponding to the breakpoint numbered BPNUM. If THREAD is not +# the empty string then THREAD should be an integer, check that +# gdb.Breakpoint.thread is set to the value of THREAD. Otherwise, if +# THREAD is the empty string, check that gdb.Breakpoint.thread is set +# to None. +proc py_find_breakpoint { var bpnum {thread ""} } { + gdb_test_no_output \ + "python ${var}=\[b for b in gdb.breakpoints() if b.number == $bpnum\]\[0\]" \ + "find Python gdb.Breakpoint object" + if { $thread ne "" } { + gdb_test_no_output "python assert(${var}.thread == ${thread})" \ + "check thread attribute is currently correct" + } else { + gdb_test_no_output "python assert(${var}.thread is None)" \ + "check thread attribute is currently correct" + } +} + +# Setup two inferiors. In #1 the symbol 'foo' has not yet been +# loaded, while in #2 the symbol 'foo' has been loaded. +# +# Create a thread-specific breakpoint on 'foo' tied to a thread in +# inferior #1, the breakpoint should be pending -- 'foo' is not yet +# loaded in #1. +# +# Use Python to change the thread of the thread-specific breakpoint to +# a thread in inferior #2, at this point the thread should gain a +# location and become non-pending. +# +# Set the thread back to a thread in inferior #1, the breakpoint +# should return to the pending state. +proc_with_prefix py_test_toggle_thread {} { + do_test_setup "Break before open" "Break after open" + + set bpnum [do_create_pending_foo_breakpoint] + + py_find_breakpoint "bp" $bpnum 1 + + gdb_test_no_output "python bp.thread = 2" \ + "change thread on thread-specific breakpoint" + gdb_test "info breakpoint $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+$::hex \]*> inf 2" \ + "\\s+stop only in thread 2\\.1"] \ + "check thread-specific breakpoint now has a location" + + gdb_test_no_output "set call_count = 2" "set call_count in inferior 2" + gdb_continue_to_breakpoint "stop at foo in inferior 2" "foo \\(\\) .*" + + gdb_test_no_output "python bp.thread = 1" \ + "restore thread on thread-specific breakpoint" + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "$bpnum\\s+breakpoint\\s+keep\\s+y\\s+\\s+foo" \ + "\\s+stop only in thread 1\\.1" \ + "\\s+breakpoint already hit 1 time"] \ + "check thread-specific breakpoint has returned to pending" + + gdb_breakpoint [gdb_get_line_number "Break after close"] temporary + gdb_continue_to_breakpoint "stop after close in inferior 2" \ + ".* Break after close\\. .*" + + gdb_test "inferior 1" "Switching to inferior 1 .*" \ + "switch to inferior 1" + gdb_continue_to_breakpoint "stop at foo in inferior 1" "foo \\(\\) .*" +} + +# Setup two inferiors. Both inferiors have the symbol 'foo' +# available. +# +# Create a thread-specific breakpoint on 'foo' tied to a thread in +# inferior #1, the breakpoint should not be pending, but will only +# have a single location, the location in inferior #1. +# +# Use Python to change the thread of the thread-specific breakpoint to +# None. At this point the breakpoint should gain a second location, a +# location in inferior #2. +proc_with_prefix py_test_clear_thread {} { + do_test_setup "Break after open" "Break after open" + + set bpnum [do_create_foo_breakpoint] + + py_find_breakpoint "bp" $bpnum 1 + + gdb_test_no_output "python bp.thread = None" \ + "clear thread on thread-specific breakpoint" + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "${bpnum}\\s+breakpoint\\s+keep y\\s+\\s*" \ + "${bpnum}\\.1\\s+y\\s+${::hex}\\s+\]*> inf $::decimal" \ + "${bpnum}\\.2\\s+y\\s+${::hex}\\s+\]*> inf $::decimal"] \ + "check for a location in both inferiors" + + gdb_continue_to_breakpoint "stop at foo in inferior 2" "foo \\(\\) .*" + gdb_test_no_output "set call_count = 2" "set call_count in inferior 2" + + gdb_test "inferior 1" "Switching to inferior 1 .*" \ + "switch to inferior 1" + gdb_continue_to_breakpoint "stop at foo in inferior 1" "foo \\(\\) .*" + gdb_test_no_output "set call_count = 2" "set call_count in inferior 1" + + gdb_test_no_output "python bp.thread = 2" + gdb_test "info breakpoints $bpnum" \ + [multi_line \ + "${bpnum}\\s+breakpoint\\s+keep y\\s+${::hex}\\s+\]*> inf 2" \ + "\\s+stop only in thread 2\\.1" \ + "\\s+breakpoint already hit 2 times"] \ + "check for a location only in inferior 2" + + gdb_breakpoint [gdb_get_line_number "Break after close"] temporary + gdb_continue_to_breakpoint "stop after close in inferior 1" \ + ".* Break after close\\. .*" + + gdb_test "inferior 2" "Switching to inferior 2 .*" \ + "switch back to inferior 2" + gdb_continue_to_breakpoint "stop at foo again in inferior 2" \ + "foo \\(\\) .*" +} + # Run all the tests. test_no_inf_display +test_pending_toggle +py_test_toggle_thread +py_test_clear_thread diff --git a/gdb/testsuite/gdb.multi/tids.exp b/gdb/testsuite/gdb.multi/tids.exp index 573b02fdd42..4f788844ee4 100644 --- a/gdb/testsuite/gdb.multi/tids.exp +++ b/gdb/testsuite/gdb.multi/tids.exp @@ -433,11 +433,13 @@ if { [allow_python_tests] } { gdb_py_test_silent_cmd "python bp = gdb.breakpoints()\[0\]" \ "get python breakpoint" 0 - gdb_test "python bp.thread = 6" "thread = 6" \ + gdb_test_no_output "python bp.thread = 6" \ "make breakpoint thread-specific with python" # Check that the inferior-qualified ID is correct. gdb_test "info breakpoint" \ - "stop only in thread 1.3\r\n.*" \ + [multi_line \ + "$decimal\\s+\[^\r\n\]+ in thread_function1 at \[^\r\n\]+" \ + "\\s+stop only in thread 1\\.3"] \ "thread specific breakpoint right thread" } }