Joe Knapka
8/9/2004 7:42:00 PM
Joe Knapka <jknapka@kneuro.net> writes:
> Hi folks,
>
> (Platform is Fedore Core 1, unmodified, running on a crufty old
> no-name P2/233.)
>
> Google has not helped me answer the following question:
>
> sem_wait(sem_t*) seems to consistently return -1 when my code executes
> inside gdb, while acting normally (that is, returning 0 as documented)
> when run outside the debugger.
More specifically, this behavior occurs when the program is being
stepped in gdb, but only (as far as I can tell) if a "step" command is
performed in a different thread than the one doing the sem_wait()
call. Here is some code that demonstrates:
// File: semtest.c
#include <semaphore.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
void* entry_point(void* arg) {
sem_t* psem = (sem_t*)arg;
int rc = sem_wait(psem);
if (rc != 0) printf("Strange: sem_wait() returned %d\n",rc);
return 0;
}
int main(void) {
pthread_t th;
sem_t sem;
int rc = sem_init(&sem,0,0);
if (rc != 0) {
if (rc != 0) printf("sem_init() failed: %d\n",rc);
return 0;
}
pthread_create(&th,0,entry_point,(void*)&sem);
sleep(120);
sem_destroy(&sem);
return 0;
}
If I compile and run that code (g++ -c -o semtest -lpthread semtest.c)
without breakpoints, either by itself or within GDB, all
is well. If I set a breakpoint on the sem_init() call and then step
through main(), I see the "Strange" message when I step
over the sleep(120) call, or sometimes immediately after the
pthread_create() call. However, if I set a breakpoint
in entry_point() and step over the sem_wait() call itself,
it seems to work fine.
Am I mis-using the semaphore API in some way? Or is this just an
unfortunate interaction between gdb and semaphores? If so, is there a
workaround?
Thanks,
-- Joe
--
"We sat and watched as this whole <-- (Died Pretty -- "Springenfall")
blue sky turned to black..."
.... Re-defeat Bush in '04.
--
pub 1024D/BA496D2B 2004-05-14 Joseph A Knapka
Key fingerprint = 3BA2 FE72 3CBA D4C2 21E4 C9B4 3230 94D7 BA49 6D2B
If you really want to get my attention, send mail to
jknapka .at. kneuro .dot. net.