This is the mail archive of the mailing list for the GDB 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]

Re: Checking if addess is on stack?

On Thursday 20 April 2006 16:21, Eli Zaretskii wrote:

> > The rationale is that in the case I've given:
> >
> >   void do_that(My_class* ptr)
> >   {
> >                 ptr->i = .....;
> >                 ........
> >   }
> >
> > user most likely wants to catch all future accesses to variable 'i', and
> > does not care if those accesses go via 'ptr' in 'do_that', or via some
> > other pointer variable in some other function.
> I think setting a watchpoint on `ptr->i' will do what you want here:
> it will watch _any_ accesses to that address, because GDB actually
> computes the address and places a watchpoint there.  So no matter how
> was the address accessed, the watchpoint will trigger.
> Do you have any specific examples where this logic does not work?  If
> so, please show those examples.

void set(int* ptr)
    *ptr = 10;

void modify(int* ptr)
    *ptr = 15;

int main()
    int i;
    return 0;

I get this debug session with gdb 6.4:

(gdb) b set
Breakpoint 1 at 0x8048397: file main.cpp, line 4.
(gdb) r
Starting program: /tmp/mi/a.out

Breakpoint 1, set (ptr=0xbf967374) at main.cpp:4
4           *ptr = 10;
(gdb) n
5       }
(gdb) watch *ptr
Hardware watchpoint 2: *ptr
(gdb) c
Hardware watchpoint 2 deleted because the program has left the block
in which its expression is valid.

Program exited normally.

I don't watch watchpoint 2 to be deleted in this case.

> > Exactly, so I want to detect the case where address in on the stack, and
> > in that case disable the watchpoint when function exists. But there's no
> > easy way to detect if address is on stack, and that's the problem.
> Well, I thought the trick with tb does the equivalent of what you
> wanted.  It automatically inserts the watchpoint when the scope is
> entered, while its deletion is handled by GDB itself.  Next time the
> function is entered, GDB will insert the watchpoint again.  Isn't that
> what you want, as far as the variable-out-of-scope issue is
> considered?  (Whether to watch the address or the expression is a 
> different matter, as mentioned above.)

I want to set breakpoint at address, and be it automatically removed when 
leaving function, if and only if the address is on function stack.

In current gdb, I can either:
1. Set watchpoint on expression, and it will be always deleted, like in above 
2. Set watchpoint on address, and it won't be ever deleted.

- Volodya

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