From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 C4D4E3858C00 for ; Wed, 2 Nov 2022 14:34:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C4D4E3858C00 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667399662; 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; bh=Zg0z5wwI19H8QlgYk023woPDrOhKeXUyj/As8SfLqwQ=; b=KOMxu1rO1tlMEGPLhSAElVZN7Mp78sh3GVJV5jwMDuIA5wutAV8v+yXgfNJ6suqUT8Tyww 9zHVuFuqY9q5kQeO2dqUaXywoAA6h3GzKzegarQAPT/DBQW7ucuKFF3fHh6Ums8Z0BOnKN dCP0+jMHoooIPSqFxOhebizNsx7mg70= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-637-uvCOCz-COgGN3yTh3px58A-1; Wed, 02 Nov 2022 10:34:21 -0400 X-MC-Unique: uvCOCz-COgGN3yTh3px58A-1 Received: by mail-qv1-f71.google.com with SMTP id ob9-20020a0562142f8900b004bba5363ad9so9705761qvb.8 for ; Wed, 02 Nov 2022 07:34:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zg0z5wwI19H8QlgYk023woPDrOhKeXUyj/As8SfLqwQ=; b=3lEla3Tq4Dgqs05kvPBAauKCsYWErpWXMg9bC30y3r4v9ZVgABmTf+LJ5CPbBcT7xL p7tXAjtwd2oMj1ULQ+cDwIwlmSKrigbApsX3qfMKnfYY/rnhK61Ndpa6y2bBoWXsGUuc tiToy+k1ptZ78cM6oG3c6p79+03g53EkDWk+oHQEkSAqtcBC6+rAnoCb9VGLKJOGXz7N nfKnuCRylA1inJiezPO9FBaQlYUgpV9o4g9UHWFcEh3VAbxbiDtGj4CvHeZKeuSOoNC5 VbUEQweENiSPm16mKt5o/mINDE/ffKwybD9AlmY/l8Abp7FkjEEZVQ9DpRC7vO+TaRRf IUzA== X-Gm-Message-State: ACrzQf06+aUC5ZD+9mG3v9jae9pKhXGGi3nQhg+8eOHUVuwZdrhXp+su Ynq8jdKeRBEAyCEJ//ZqMAJCM2npHbE+KLzNpCQDPYYeK+ESE7HGxoJVIqqpB/9g8d3gul+CZse BblOme2rH6YH+XONMB4I2N3ja0c/oiT9TnPZPGu5YL0FL4M6ZIPY6rUeyeJkGiRlSUqj37kIpvw == X-Received: by 2002:ad4:5ced:0:b0:4bb:e946:fb5e with SMTP id iv13-20020ad45ced000000b004bbe946fb5emr15912566qvb.33.1667399660475; Wed, 02 Nov 2022 07:34:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7fjrasYMfzEcsEYPXYSP4rndT1gipcUUdROLEISqbsOuH3nw7jla3rXRsp7qN+l0nLckgRMQ== X-Received: by 2002:ad4:5ced:0:b0:4bb:e946:fb5e with SMTP id iv13-20020ad45ced000000b004bbe946fb5emr15912542qvb.33.1667399660148; Wed, 02 Nov 2022 07:34:20 -0700 (PDT) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id bq32-20020a05620a46a000b006c479acd82fsm8526731qkb.7.2022.11.02.07.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Nov 2022 07:34:19 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb: new $_inferior_threads convenience variable Date: Wed, 2 Nov 2022 14:34:16 +0000 Message-Id: <20221102143416.3307157-1-aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Add a new convenience variable $_inferior_threads that contains the number of (non-exited) threads in the current inferior. This can be used in command scripts, or breakpoint conditions, etc to adjust the behaviour for multi-threaded inferiors. --- gdb/NEWS | 3 +++ gdb/doc/gdb.texinfo | 9 +++++++-- gdb/testsuite/gdb.base/default.exp | 1 + gdb/testsuite/gdb.multi/tids.exp | 20 ++++++++++++++++++++ gdb/thread.c | 25 +++++++++++++++++++++++++ gdbsupport/iterator-range.h | 4 ++++ 6 files changed, 60 insertions(+), 2 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 8b519a648f7..3642d42e55a 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -71,6 +71,9 @@ For both /r and /b GDB is now better at using whitespace in order to align the disassembled instruction text. +* New convenience variable $_inferior_threads contains the number of + non-exited threads in the current inferior. + * New commands maintenance set ignore-prologue-end-flag on|off diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index ea66f4ee42d..977cabd3771 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -3642,8 +3642,10 @@ @samp{$_gthread} contain, respectively, the per-inferior thread number and the global thread number of the current thread. You may find this useful in writing breakpoint conditional expressions, command scripts, -and so forth. @xref{Convenience Vars,, Convenience Variables}, for -general information on convenience variables. +and so forth. The convenience variable @samp{$_inferior_threads} +contains the number of non-exited threads in the current inferior. +@xref{Convenience Vars,, Convenience Variables}, for general +information on convenience variables. If @value{GDBN} detects the program is multi-threaded, it augments the usual message about stopping at a breakpoint with the ID and name of @@ -12655,6 +12657,9 @@ @item $_gthread The global number of the current thread. @xref{global thread numbers}. +@item $_inferior_threads +The number of non-exited threads in the current inferior. @xref{Threads}. + @item $_gdb_major @itemx $_gdb_minor @vindex $_gdb_major@r{, convenience variable} diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp index 29e76642830..cae131cc59b 100644 --- a/gdb/testsuite/gdb.base/default.exp +++ b/gdb/testsuite/gdb.base/default.exp @@ -584,6 +584,7 @@ set show_conv_list \ {$_thread = 0} \ {$_gthread = 0} \ {$_inferior = 1} \ + {$_inferior_threads = 0} \ {$_exception = } \ {$_probe_argc = } \ {$_probe_arg0 = } \ diff --git a/gdb/testsuite/gdb.multi/tids.exp b/gdb/testsuite/gdb.multi/tids.exp index fb7c2a29a71..1bcba02a1c9 100644 --- a/gdb/testsuite/gdb.multi/tids.exp +++ b/gdb/testsuite/gdb.multi/tids.exp @@ -123,6 +123,8 @@ with_test_prefix "single inferior" { info_threads "" "1" gdb_test "thread" "Current thread is 1 .*" + + gdb_test "print \$_inferior_threads" " = 1" } # "info threads" while there are multiple inferiors should show @@ -140,8 +142,14 @@ with_test_prefix "two inferiors" { gdb_test "inferior 2" "Switching to inferior 2 .*" "switch to inferior 2" gdb_test "file ${binfile}" ".*" "load file in inferior 2" + gdb_test "print \$_inferior_threads" " = 0" \ + "no threads before we start the second inferior" + runto_main + gdb_test "print \$_inferior_threads" " = 1" \ + "no other threads started yet" + # Now that we've added another inferior, thread IDs now show the # inferior number. info_threads "" "1.1 2.1" \ @@ -153,8 +161,14 @@ with_test_prefix "two inferiors" { gdb_breakpoint "thread_function1" gdb_continue_to_breakpoint "once" + gdb_test "print \$_inferior_threads" " = 2" \ + "second thread started in inferior 2" gdb_test "inferior 1" "Switching to inferior 1 .*" + gdb_test "print \$_inferior_threads" " = 1" \ + "still only one thread in inferior 1" gdb_continue_to_breakpoint "twice" + gdb_test "print \$_inferior_threads" " = 2" \ + "second thread started in inferior 1" info_threads "" "1.1 1.2 2.1 2.2" \ "info threads again" @@ -187,9 +201,15 @@ with_test_prefix "two inferiors" { gdb_test "inferior 2" "Switching to inferior 2 .*" gdb_continue_to_breakpoint "once" + gdb_test "print \$_inferior_threads" " = 3" \ + "third thread started in inferior 2" gdb_test "inferior 1" "Switching to inferior 1 .*" + gdb_test "print \$_inferior_threads" " = 2" \ + "still only two threads in inferior 1" gdb_continue_to_breakpoint "twice" + gdb_test "print \$_inferior_threads" " = 3" \ + "third thread started in inferior 1" } thr_apply_info_thr "1" \ diff --git a/gdb/thread.c b/gdb/thread.c index 349fc01dd48..f8942f1c5e4 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -2123,6 +2123,21 @@ global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var, return thread_num_make_value_helper (gdbarch, 1); } +/* Return a new value for the number of threads in the current inferior. + If there are no threads in the current inferior return a value of 0. */ + +static struct value * +inferior_threads_make_value (struct gdbarch *gdbarch, struct internalvar *var, + void *ignore) +{ + int int_val = 0; + + if (inferior_ptid != null_ptid) + int_val = current_inferior ()->non_exited_threads ().size (); + + return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val); +} + /* Commands with a prefix of `thread'. */ struct cmd_list_element *thread_cmd_list = NULL; @@ -2142,6 +2157,14 @@ static const struct internalvar_funcs gthread_funcs = NULL, }; +/* Implementation of `_inferior_threads` convenience variable. */ + +static const struct internalvar_funcs inferior_threads_funcs = +{ + inferior_threads_make_value, + NULL, +}; + void _initialize_thread (); void _initialize_thread () @@ -2258,4 +2281,6 @@ When on messages about thread creation and deletion are printed."), create_internalvar_type_lazy ("_thread", &thread_funcs, NULL); create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL); + create_internalvar_type_lazy ("_inferior_threads", + &inferior_threads_funcs, NULL); } diff --git a/gdbsupport/iterator-range.h b/gdbsupport/iterator-range.h index d35b03a3392..c99bdc8de07 100644 --- a/gdbsupport/iterator-range.h +++ b/gdbsupport/iterator-range.h @@ -53,6 +53,10 @@ struct iterator_range IteratorType end () const { return m_end; } + /* The number of items in this iterator_range. */ + std::size_t size () const + { return std::distance (m_begin, m_end); } + private: IteratorType m_begin, m_end; }; -- 2.25.4