No thread context switch in non-stop mode???

Doug Evans dje@google.com
Sun Mar 16 02:12:00 GMT 2014


Hi.

I'm looking into implementing a wait command, and have come across
something I don't understand.

I understand the goal of this comment in infrun.c:

  /* In non-stop mode, we don't want GDB to switch threads behind the
     user's back, to avoid races where the user is typing a command to
     apply to thread x, but GDB switches to thread y before the user
     finishes entering the command.  */

Sounds reasonable to me.
But I'm finding (apparent) violations of this and wondering if they're all bugs.

E.g.  With the appended testcase I see gdb changing the current thread
each time a breakpoint is hit, despite the above claim.

The above comment is a bit misleading since that code doesn't actually
change anything, it just prints a message notifying the user that the
current thread has changed.  Plus, gdb prints a breakpoint hit
notification but without also printing which thread hit the breakpoint
(even if the current thread isn't changed), the message is less useful
than it could be.

Can anyone add any clarity here?
-------------- next part --------------

#include <sys/syscall.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define NR_THREADS 4

static pid_t
gettid (void)
{
  return syscall (SYS_gettid);
}

static void
woke_up (void)
{
}

static void *
forever_pthread (void *unused)
{
  pid_t tid = gettid ();

  printf ("thread tid %d started\n", tid);

  while (1)
    {
      sleep (10 + random () % 10);
      woke_up ();
      printf ("thread tid %d\n", tid);
      fflush (stdout);
    }
}

static void
main_woke_up (void)
{
}

int
main (int argc, char *argv[])
{
  pthread_t threads[NR_THREADS];
  int i;

  printf ("pid %d started\n", getpid ());

  for (i = 0; i < NR_THREADS; ++i)
    pthread_create (&threads[i], NULL, forever_pthread, NULL);

  while (1)
    {
      sleep (10);
      main_woke_up ();
    }

  return 0;
}

-------------- next part --------------
#set debug infrun 1
set target-async on
set non-stop on
file ~/src/play/test.x64
b main_woke_up
b woke_up
commands
i thr
end
-------------- next part --------------
(gdb) r &
Starting program: /home/dje/src/play/test.x64 
(gdb) [Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
pid 3991 started
[New Thread 0x7ffff7800700 (LWP 3995)]
thread tid 3995 started
[New Thread 0x7ffff6faf700 (LWP 3996)]
thread tid 3996 started
[New Thread 0x7ffff675e700 (LWP 3997)]
thread tid 3997 started
[New Thread 0x7ffff5f0d700 (LWP 3998)]
thread tid 3998 started

Breakpoint 1, main_woke_up () at test.c:59
59	}

Breakpoint 2, woke_up () at test.c:38
38	}
  Id   Target Id         Frame 
  5    Thread 0x7ffff5f0d700 (LWP 3998) "test.x64" (running)
  4    Thread 0x7ffff675e700 (LWP 3997) "test.x64" (running)
  3    Thread 0x7ffff6faf700 (LWP 3996) "test.x64" (running)
* 2    Thread 0x7ffff7800700 (LWP 3995) "test.x64" woke_up ()
    at test.c:38
  1    Thread 0x7ffff7fe3740 (LWP 3991) "test.x64" main_woke_up ()
    at test.c:59

Breakpoint 2, woke_up () at test.c:38
38	}
  Id   Target Id         Frame 
* 5    Thread 0x7ffff5f0d700 (LWP 3998) "test.x64" woke_up ()
    at test.c:38
  4    Thread 0x7ffff675e700 (LWP 3997) "test.x64" (running)
  3    Thread 0x7ffff6faf700 (LWP 3996) "test.x64" (running)
  2    Thread 0x7ffff7800700 (LWP 3995) "test.x64" woke_up ()
    at test.c:38
  1    Thread 0x7ffff7fe3740 (LWP 3991) "test.x64" main_woke_up ()
    at test.c:59

Breakpoint 2, woke_up () at test.c:38
38	}
  Id   Target Id         Frame 
  5    Thread 0x7ffff5f0d700 (LWP 3998) "test.x64" woke_up ()
    at test.c:38
  4    Thread 0x7ffff675e700 (LWP 3997) "test.x64" (running)
* 3    Thread 0x7ffff6faf700 (LWP 3996) "test.x64" woke_up ()
    at test.c:38
  2    Thread 0x7ffff7800700 (LWP 3995) "test.x64" woke_up ()
    at test.c:38
  1    Thread 0x7ffff7fe3740 (LWP 3991) "test.x64" main_woke_up ()
    at test.c:59

Breakpoint 2, woke_up () at test.c:38
38	}
  Id   Target Id         Frame 
  5    Thread 0x7ffff5f0d700 (LWP 3998) "test.x64" woke_up ()
    at test.c:38
* 4    Thread 0x7ffff675e700 (LWP 3997) "test.x64" woke_up ()
    at test.c:38
  3    Thread 0x7ffff6faf700 (LWP 3996) "test.x64" woke_up ()
    at test.c:38
  2    Thread 0x7ffff7800700 (LWP 3995) "test.x64" woke_up ()
    at test.c:38
  1    Thread 0x7ffff7fe3740 (LWP 3991) "test.x64" main_woke_up ()
    at test.c:59


More information about the Gdb-patches mailing list