From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id E680F3AA801E; Thu, 17 Nov 2022 14:49:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E680F3AA801E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668696593; bh=QuHi2TZ8XsVz2n4hTG1PPgCSrFmT+rmGhdZcze5zk3c=; h=From:To:Subject:Date:From; b=QBdQX5P6OCJYJZXbHEbV99Nu8e6BVXlvpOqxxeYXr5d/LIkocCKKg0lo3TxrZNC3X qElFrczikwxTHKWnlByb509oJkk1peqgX9C+e1O4p4GWW9T3vofwSZ0KBOkst5+AQn 3E2KaD/dyUMfWX2DahA/Z3rRsV3fyc/1tUa8HB/g= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: new $_inferior_thread_count convenience variable X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 91f63aa2e2002a1533991b0e52579b2e026f50da X-Git-Newrev: cbda14deaf49b48450207cfb0990c71c11d07a64 Message-Id: <20221117144953.E680F3AA801E@sourceware.org> Date: Thu, 17 Nov 2022 14:49:53 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dcbda14deaf49= b48450207cfb0990c71c11d07a64 commit cbda14deaf49b48450207cfb0990c71c11d07a64 Author: Andrew Burgess Date: Wed Nov 2 13:48:42 2022 +0000 gdb: new $_inferior_thread_count convenience variable =20 Add a new convenience variable $_inferior_thread_count that contains the number of live (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. =20 This value is only stable in all-stop mode. In non-stop mode, where new threads can be started, and existing threads exit, at any time, this convenience variable can give a different value each time it is evaluated. Diff: --- gdb/NEWS | 3 +++ gdb/doc/gdb.texinfo | 14 ++++++++++++-- gdb/testsuite/gdb.base/default.exp | 1 + gdb/testsuite/gdb.multi/tids.exp | 20 ++++++++++++++++++++ gdb/thread.c | 26 ++++++++++++++++++++++++++ gdbsupport/iterator-range.h | 4 ++++ 6 files changed, 66 insertions(+), 2 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 3f31515297c..424e5ffd829 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -78,6 +78,9 @@ re-enable styling using the new "set style tui-current-position" command. =20 +* New convenience variable $_inferior_thread_count contains the number + of live threads in the current inferior. + * New commands =20 maintenance set ignore-prologue-end-flag on|off diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index a41f0f7ccef..bb7b2764408 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -3642,8 +3642,15 @@ The debugger convenience variables @samp{$_thread} a= nd @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_thread_count} +contains the number of live threads in the current inferior. +@xref{Convenience Vars,, Convenience Variables}, for general +information on convenience variables. + +When running in non-stop mode (@pxref{Non-Stop Mode}), where new +threads can be created, and existing threads exit, at any time, +@samp{$_inferior_thread_count} could return a different value each +time it is evaluated. =20 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 +12662,9 @@ The thread number of the current thread. @xref{thr= ead numbers}. @item $_gthread The global number of the current thread. @xref{global thread numbers}. =20 +@item $_inferior_thread_count +The number of live 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/de= fault.exp index 29e76642830..f3e4b2867ef 100644 --- a/gdb/testsuite/gdb.base/default.exp +++ b/gdb/testsuite/gdb.base/default.exp @@ -584,6 +584,7 @@ set show_conv_list \ {$_thread =3D 0} \ {$_gthread =3D 0} \ {$_inferior =3D 1} \ + {$_inferior_thread_count =3D 0} \ {$_exception =3D } \ {$_probe_argc =3D } \ {$_probe_arg0 =3D } \ diff --git a/gdb/testsuite/gdb.multi/tids.exp b/gdb/testsuite/gdb.multi/tid= s.exp index fb7c2a29a71..23668caf18f 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" =20 gdb_test "thread" "Current thread is 1 .*" + + gdb_test "print \$_inferior_thread_count" " =3D 1" } =20 # "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" =20 + gdb_test "print \$_inferior_thread_count" " =3D 0" \ + "no threads before we start the second inferior" + runto_main =20 + gdb_test "print \$_inferior_thread_count" " =3D 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" =20 gdb_continue_to_breakpoint "once" + gdb_test "print \$_inferior_thread_count" " =3D 2" \ + "second thread started in inferior 2" gdb_test "inferior 1" "Switching to inferior 1 .*" + gdb_test "print \$_inferior_thread_count" " =3D 1" \ + "still only one thread in inferior 1" gdb_continue_to_breakpoint "twice" + gdb_test "print \$_inferior_thread_count" " =3D 2" \ + "second thread started in inferior 1" =20 info_threads "" "1.1 1.2 2.1 2.2" \ "info threads again" @@ -187,9 +201,15 @@ with_test_prefix "two inferiors" { =20 gdb_test "inferior 2" "Switching to inferior 2 .*" gdb_continue_to_breakpoint "once" + gdb_test "print \$_inferior_thread_count" " =3D 3" \ + "third thread started in inferior 2" =20 gdb_test "inferior 1" "Switching to inferior 1 .*" + gdb_test "print \$_inferior_thread_count" " =3D 2" \ + "still only two threads in inferior 1" gdb_continue_to_breakpoint "twice" + gdb_test "print \$_inferior_thread_count" " =3D 3" \ + "third thread started in inferior 1" } =20 thr_apply_info_thr "1" \ diff --git a/gdb/thread.c b/gdb/thread.c index 349fc01dd48..d176d1d9895 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -2123,6 +2123,22 @@ global_thread_id_make_value (struct gdbarch *gdbarch= , struct internalvar *var, return thread_num_make_value_helper (gdbarch, 1); } =20 +/* Return a new value for the number of non-exited threads in the current + inferior. If there are no threads in the current inferior return a + value of 0. */ + +static struct value * +inferior_thread_count_make_value (struct gdbarch *gdbarch, + struct internalvar *var, void *ignore) +{ + int int_val =3D 0; + + if (inferior_ptid !=3D null_ptid) + int_val =3D 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 =3D NULL; =20 @@ -2142,6 +2158,14 @@ static const struct internalvar_funcs gthread_funcs = =3D NULL, }; =20 +/* Implementation of `_inferior_thread_count` convenience variable. */ + +static const struct internalvar_funcs inferior_thread_count_funcs =3D +{ + inferior_thread_count_make_value, + NULL, +}; + void _initialize_thread (); void _initialize_thread () @@ -2258,4 +2282,6 @@ When on messages about thread creation and deletion a= re printed."), =20 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL); create_internalvar_type_lazy ("_gthread", >hread_funcs, NULL); + create_internalvar_type_lazy ("_inferior_thread_count", + &inferior_thread_count_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; } =20 + /* 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; };