gdb: call update_thread_list for $_inferior_thread_count function
authorAndrew Burgess <aburgess@redhat.com>
Tue, 10 Oct 2023 09:17:35 +0000 (10:17 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 8 Nov 2023 13:28:09 +0000 (13:28 +0000)
commit75b2eb97a49bf3b539d7e016b2d9a2339170a680
tree2c9d050f04915be73cb5372c7bdc46784c828860
parentb20885b0a439ff8bd0e4fd00c666589ffca10fa2
gdb: call update_thread_list for $_inferior_thread_count function

I noticed that sometimes the value returned by $_inferior_thread_count
can become out of sync with the actual thread count of the inferior,
and will disagree with the number of threads reported by 'info
threads'.  This commit fixes this issue.

The cause of the problem is that 'info threads' includes a call to
update_thread_list, this can be seen in print_thread_info_1 in
thread.c, while $_inferior_thread_count doesn't include a similar
call, see the function inferior_thread_count_make_value also in
thread.c.

Of course, this is only a problem when GDB is running on a target that
relies on update_thread_list calls to learn about new threads,
e.g. remote or extended-remote targets.  Native targets generally
learn about new threads as soon as they appear and will not have this
problem.

I ran into this issue when writing a test for the next commit which
uses inferior function calls to add an remove threads from an
inferior.  But for testing I've made use of non-stop mode and
asynchronous inferior execution; by reading the inferior state I can
know when a new thread has been created, at which point I can print
$_inferior_thread_count while the inferior is still running.  This is
important, if I stop the inferior then GDB will pass through an
update_thread_list call in the normal stop code, which will
synchronise the thread list, after which $_inferior_thread_count will
report the correct value.

With this change in place $_inferior_thread_count is now correct.
gdb/testsuite/gdb.threads/inf-thr-count.c [new file with mode: 0644]
gdb/testsuite/gdb.threads/inf-thr-count.exp [new file with mode: 0644]
gdb/thread.c