public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: archer@sourceware.org
Subject: BUG? gdb, software watchpoints && multithreading
Date: Tue, 12 Oct 2010 18:59:00 -0000	[thread overview]
Message-ID: <20101012185548.GA19475@redhat.com> (raw)

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.

             reply	other threads:[~2010-10-12 18:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-12 18:59 Oleg Nesterov [this message]
2010-10-19 18:19 ` Oleg Nesterov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101012185548.GA19475@redhat.com \
    --to=oleg@redhat.com \
    --cc=archer@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).