This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
Re: gdb, pthreads, and sleep
On Mon, Sep 22, 2003 at 04:47:06PM -0400, Michael Elizabeth Chastain wrote:
> The problem is an interaction between sleep, pthread_create, and gdb.
> When gdb is running, pthread_create eventually calls
> pthread_restart_new, which sends the pthread_sig_restart signal. gdb
> notices this signal. But as a side effect, the "sleep (9)" is
> interrupted and returns early.
>
> When gdb is used, usually all the threads go all the way to exit, but
> sometimes some threads do not (especially the newest thread created).
> (2) Change the program under test to be more correct:
>
> int unslept = 9;
> while (unslept)
> unslept = sleep (unslept);
>
> This is the proper way to call 'sleep' in a program that may
> receive signals. The return value of 'sleep' is documented in
> Single Unix Spec, v2, so it is portable. If this code leads to a
> problem, then it means that the test program has found a bug in the
> operating system's implementation of "sleep". Tickling bugs is
> a *good* thing for a test program.
Yes.
> The gotcha here is that gdb should work with buggy test programs.
> Currently, pthreads.c is written poorly (ignores the return value of
> 'sleep'), but it's natural that people write code like this.
See the archives (it's in a PR somewhere, and in gdb@ also) for more
information. Kernel 2.6 will restart the syscall in this case. Many
other interruptible syscalls will not be restarted but that's the best
we can do.
> Also, I think we need some documentation in the gdb threads section.
> gdb makes some threaded programs behave differently because the signals
> for gdb are not perfectly transparent. Watch what happens when I run
> gdb.mi/pthreads with and without gdb:
>
> /* without gdb */
> % ./pthreads
> hello
> hello
>
> /* with gdb */
> (gdb) run
> Starting program: /berman/home/mgnu/gdb/pthread-select/pthreads
> [New Thread 8192 (LWP 12564)]
> [New Thread 16385 (LWP 12568)]
> [New Thread 8194 (LWP 12569)]
> [New Thread 16387 (LWP 12570)]
> hello thread
> [New Thread 24580 (LWP 12571)]
> hello thread
> [New Thread 32773 (LWP 12572)]
> hello thread
> [New Thread 40966 (LWP 12573)]
> hello thread
> hello
> hello
>
> Program exited normally.
>
> The 'hello thread' output happens only under the debugger.
> I think people will be surprised when their 'sleep' calls actually
> sleep without gdb, but return with a lot of unslept time with gdb.
> I doubt we can fix this, although maybe the pthreads implementors
> can fix it. But we can least document it.
Feel free to write up some documentation about this problem :)
--
Daniel Jacobowitz
MontaVista Software Debian GNU/Linux Developer