Date: Fri, 22 Feb 2019 12:10:26 +0200 From: Konstantin Belousov <kostikbel@gmail.com> To: Robert Ayrapetyan <robert.ayrapetyan@gmail.com> Cc: FreeBSD <freebsd-hackers@freebsd.org> Subject: Re: ptrace: SIGTRAP and EXIT race Message-ID: <20190222101026.GX2420@kib.kiev.ua> In-Reply-To: <CAAboi9tT==FFXdqq9XG2v8Lxf8RBuTMx5ns4puZ-hjD5KecFsA@mail.gmail.com> References: <CAAboi9tT==FFXdqq9XG2v8Lxf8RBuTMx5ns4puZ-hjD5KecFsA@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Feb 21, 2019 at 08:43:20PM -0800, Robert Ayrapetyan wrote: > Hello. > > Before creating a bug and providing some test code, would ask a community > here. > When tracing a process using ptrace and there are multiple threads in the > tracing process hitting the same breakpoint, sometimes main thread exits > (WIFEXITED(status) is TRUE) before last queued TRAP_BKPT signal(s) have > been delivered to the tracing process. So a final breakpoint hits counter > is less than it should be. > > So in the example below: > > #include <iostream> > #include <thread> > > #include <pthread_np.h> > > static const int num_threads = 2; > > void foo() { > for (int i = 0; i < 2; ++i) { > printf("hi: %d (tid: %d)\n", i, pthread_getthreadid_np()); > } > } > > int main() { > std::thread t[num_threads]; > > for (int i = 0; i < num_threads; ++i) { > t[i] = std::thread(foo); > } > > for (int i = 0; i < num_threads; ++i) { > t[i].join(); > } > > return 0; > } > > If we set breakpoint to printf, it should be triggered 4 times (tracing > process should receive TRAP_BKPT 4 times). However, in ~1 of 5 runs, it > receives TRAP_BKPT just 2 or 3 times. > > Is this expected? Thanks. I indeed would expect that all four breakpoints triggered before the main thread exits, assuming that the breakpoints were installed before the threads are created. Please provide the stand-alone (and preferrably non-interactive) test to reproduce the issue.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20190222101026.GX2420>