This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

BUG? gdb, software watchpoints && multithreading


I am trying to understand how ugdb can implement software watchpoints.
I am looking at what gdb does, and I am a bit confused.

Trivial test-case:

	#include <stdio.h>
	#include <unistd.h>
	#include <pthread.h>

	struct {
		long v;
		char pad[256];
	} VAR;

	void *tfunc(void *arg)
	{
		for (;;)
			;
	}

	int main(void)
	{
		pthread_t thr;

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

		pthread_create(&thr, NULL, tfunc, NULL);

		for (;;)
			VAR.v++;

		return 0;
	}

The sub-thread T does nothing but spins in the endless loop, the main
M thread changes VAR.

But, according to gdb, they both change VAR.

	(gdb) attach PID
	...
	(gdb) watch VAR
	(gdb) c -a
	Continuing.
	Watchpoint 1: VAR

	Old value = {v = 394344995, pad = '\000' <repeats 255 times>}
	New value = {v = 394344996, pad = '\000' <repeats 255 times>}
	0x0000000000400634 in tfunc (arg=0x0) at /tmp/BWP.c:11
	11	{
	(gdb) Watchpoint 1: VAR

	Old value = {v = 394344996, pad = '\000' <repeats 255 times>}
	New value = {v = 394344997, pad = '\000' <repeats 255 times>}
	0x0000000000400683 in main () at /tmp/BWP.c:26
	26			VAR.v++;

gdb resumes (steps) both threads. If T reports %Stop while M changes
the memory, gdb notices the change and updates its copy of VAR. Then
it reports that VAR was changed to the user.

After that it inspects the stopped M and reads VAR again. Since the
copy was already updated it concludes it wasn't changed. It resumes
M again and only then notices another change.

Not that I really blame gdb, without hardware support it is not
possible to implement this 100% correctly. But I assume this is
not what we want?

IOW, I think that ugdb should do the following. If any thread changes
VAR, then all threads should stop and report T05watch to gdb.

Correct?

Another question. watch/Z2 is always per-process, there is no
"thread-local" watches, right?

And the last one. If gdb sends '$Z2' to gdbserver, the running
threads do not participate in monitoring, until gdb stops the
thread and resumes it again, correct?

Oleg.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]