Interfacing with gdb: How to set a gdb watch point from C++ code?

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by morde » Fri, 06 Dec 2002 10:49:50



I have the following code in a third party library:

// GeneralXEventHandler gets called for each event of the types we requested
// in RegisterObject().  We send the event to the Event() routine of the
// object the event belongs to.
void GeneralXEventHandler(Widget, XtPointer object, XEvent *xEvent,
Boolean *)
{
        UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;
        // for debug
^^^^^^^^^^^^^^ need a watchpoint right after assignment.
        UI_WINDOW_OBJECT *uObjectDup = (UI_WINDOW_OBJECT *)object;
        if (!uObject->screenID)
                return;
        UI_EVENT event(E_MOTIF, *xEvent);
        if (xEvent->type == KeyPress && event.rawCode == XK_Tab)
                _lastFocusEvent = KeyPress;
        else if (xEvent->type == ButtonPress)
                _lastFocusEvent = ButtonPress;

        if(uObject != uObjectDup) {
                l_fatal("object %p objectdup %p !!!!", uObject, uObjectDup);
        }
        uObject->Event(event);

Quote:}

I want to set a watchpoint on the contents of uObject after
initialization. The function is called quite a bit and I don't want to
do it manually on every call.
Is there a gcc directive to automate this?
 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by Paul Pluzhniko » Fri, 06 Dec 2002 13:04:54



>    UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;

> I want to set a watchpoint on the contents of uObject after
> initialization.

Why? The value (contents) of the uObject pointer does not change
within this function, so your watchpoint will never trigger.

Perhaps you want to describe what your *real* problem is?

Cheers,
--
In order to understand recursion you must first understand recursion.

 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by morde » Sat, 07 Dec 2002 03:26:05



> morden  writes:

> >       UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;

> >I want to set a watchpoint on the contents of uObject after
> >initialization.

> Why? The value (contents) of the uObject pointer does not change
> within this function, so your watchpoint will never trigger.

It does. Something corrupts the stack and uObject pointer becomes
garbage (0x1c3, that's 467) while uObjectDup does not:

#1  0x82b4808 in GeneralXEventHandler__FP10_WidgetRecPvP7_XEventPc (
     __0__A226=0x879918c, __0object=0x80456ac, __0xEvent=0x4,
__0__A227=0x2 "")
     at m_win2.c:704 (that's the last line of the function: call to Event
                where program SIGSEGVs)
(gdb) print __1uObjectDup
$4 = (struct UI_WINDOW_OBJECT *) 0x8689fb0
(gdb) print __1uObject
$5 = (struct UI_WINDOW_OBJECT *) 0x1c3

Quote:

> Perhaps you want to describe what your *real* problem is?

Stack corruption.
The code again is:

I have the following code in a third party library:

// GeneralXEventHandler gets called for each event of the types we requested
// in RegisterObject().  We send the event to the Event() routine of the
// object the event belongs to.
void GeneralXEventHandler(Widget, XtPointer object, XEvent *xEvent,
Boolean *)
{
     UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;
     // for debug
^^^^^^^^^^^^^^ need a watchpoint right after assignment.
     UI_WINDOW_OBJECT *uObjectDup = (UI_WINDOW_OBJECT *)object;
     if (!uObject->screenID)
         return;
     UI_EVENT event(E_MOTIF, *xEvent);
     if (xEvent->type == KeyPress && event.rawCode == XK_Tab)
         _lastFocusEvent = KeyPress;
     else if (xEvent->type == ButtonPress)
         _lastFocusEvent = ButtonPress;

     if(uObject != uObjectDup) {
         l_fatal("object %p objectdup %p !!!!", uObject, uObjectDup);
     }
     uObject->Event(event);

Quote:}

I want to set a watchpoint on the contents of uObject after
initialization. The function is called quite a bit and I don't want to
(I can't) do it manually on every call.
Is there a gcc directive to automate the setup of watchpoints?
 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by Mike Egglesto » Sat, 07 Dec 2002 03:13:05




>> morden  writes:

>> >   UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;

>> >I want to set a watchpoint on the contents of uObject after
>> >initialization.

>> Why? The value (contents) of the uObject pointer does not change
>> within this function, so your watchpoint will never trigger.

> It does. Something corrupts the stack and uObject pointer becomes
> garbage (0x1c3, that's 467) while uObjectDup does not:

> #1  0x82b4808 in GeneralXEventHandler__FP10_WidgetRecPvP7_XEventPc (
>      __0__A226=0x879918c, __0object=0x80456ac, __0xEvent=0x4,
> __0__A227=0x2 "")
>      at m_win2.c:704 (that's the last line of the function: call to Event
>            where program SIGSEGVs)
> (gdb) print __1uObjectDup
> $4 = (struct UI_WINDOW_OBJECT *) 0x8689fb0
> (gdb) print __1uObject
> $5 = (struct UI_WINDOW_OBJECT *) 0x1c3

>> Perhaps you want to describe what your *real* problem is?

> Stack corruption.
> The code again is:

> I have the following code in a third party library:

> // GeneralXEventHandler gets called for each event of the types we requested
> // in RegisterObject().  We send the event to the Event() routine of the
> // object the event belongs to.
> void GeneralXEventHandler(Widget, XtPointer object, XEvent *xEvent,
> Boolean *)
> {
>      UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;
>      // for debug
> ^^^^^^^^^^^^^^ need a watchpoint right after assignment.
>      UI_WINDOW_OBJECT *uObjectDup = (UI_WINDOW_OBJECT *)object;
>      if (!uObject->screenID)
>          return;
>      UI_EVENT event(E_MOTIF, *xEvent);
>      if (xEvent->type == KeyPress && event.rawCode == XK_Tab)
>          _lastFocusEvent = KeyPress;
>      else if (xEvent->type == ButtonPress)
>          _lastFocusEvent = ButtonPress;

>      if(uObject != uObjectDup) {
>          l_fatal("object %p objectdup %p !!!!", uObject, uObjectDup);
>      }
>      uObject->Event(event);
> }

> I want to set a watchpoint on the contents of uObject after
> initialization. The function is called quite a bit and I don't want to
> (I can't) do it manually on every call.
> Is there a gcc directive to automate the setup of watchpoints?

Do you know what the corrupt value is when it happens?
If so do something like:

if(object = 0xDEADBEEF) {
        object = object;

Quote:}

Set a break on the object = object line.

Mike

 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by morde » Sat, 07 Dec 2002 05:37:45





> >>morden  writes:

> >>>       UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;

> >>>I want to set a watchpoint on the contents of uObject after
> >>>initialization.

> >>Why? The value (contents) of the uObject pointer does not change
> >>within this function, so your watchpoint will never trigger.

> >It does. Something corrupts the stack and uObject pointer becomes
> >garbage (0x1c3, that's 467) while uObjectDup does not:

> >#1  0x82b4808 in GeneralXEventHandler__FP10_WidgetRecPvP7_XEventPc (
> >     __0__A226=0x879918c, __0object=0x80456ac, __0xEvent=0x4,
> >__0__A227=0x2 "")
> >     at m_win2.c:704 (that's the last line of the function: call to Event
> >               where program SIGSEGVs)
> >(gdb) print __1uObjectDup
> >$4 = (struct UI_WINDOW_OBJECT *) 0x8689fb0
> >(gdb) print __1uObject
> >$5 = (struct UI_WINDOW_OBJECT *) 0x1c3

> >>Perhaps you want to describe what your *real* problem is?

> >Stack corruption.
> >The code again is:

> >I have the following code in a third party library:

> >// GeneralXEventHandler gets called for each event of the types we
> requested
> >// in RegisterObject().  We send the event to the Event() routine of the
> >// object the event belongs to.
> >void GeneralXEventHandler(Widget, XtPointer object, XEvent *xEvent,
> >Boolean *)
> >{
> >     UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;
> >     // for debug
> >^^^^^^^^^^^^^^ need a watchpoint right after assignment.
> >     UI_WINDOW_OBJECT *uObjectDup = (UI_WINDOW_OBJECT *)object;
> >     if (!uObject->screenID)
> >         return;
> >     UI_EVENT event(E_MOTIF, *xEvent);
> >     if (xEvent->type == KeyPress && event.rawCode == XK_Tab)
> >         _lastFocusEvent = KeyPress;
> >     else if (xEvent->type == ButtonPress)
> >         _lastFocusEvent = ButtonPress;

> >     if(uObject != uObjectDup) {
> >         l_fatal("object %p objectdup %p !!!!", uObject, uObjectDup);
> >     }
> >     uObject->Event(event);
> >}

> >I want to set a watchpoint on the contents of uObject after
> >initialization. The function is called quite a bit and I don't want to
> >(I can't) do it manually on every call.
> >Is there a gcc directive to automate the setup of watchpoints?

> Do you know what the corrupt value is when it happens?
> If so do something like:

> if(object = 0xDEADBEEF) {
>    object = object;
> }

> Set a break on the object = object line.

This won't be useful, because I need to know which function down the
line is corrupting uObject. I could just as well set a breakpoint on
l_fatal line. I need a watchpoint to track down the errant code
and because there are dozens of calls/per second to GeneralXEventHandler
I could not set watchpoints by hand.
Note that I'm not likely to run out of watchpoints because they will go
out of scope as rapidly as they will get set.

In general terms: I need to communicate to gdb from my C++ applications.

 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by Mike Egglesto » Sat, 07 Dec 2002 06:05:50






>> >>morden  writes:

>> >>>   UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;

>> >>>I want to set a watchpoint on the contents of uObject after
>> >>>initialization.

>> >>Why? The value (contents) of the uObject pointer does not change
>> >>within this function, so your watchpoint will never trigger.

>> >It does. Something corrupts the stack and uObject pointer becomes
>> >garbage (0x1c3, that's 467) while uObjectDup does not:

>> >#1  0x82b4808 in GeneralXEventHandler__FP10_WidgetRecPvP7_XEventPc (
>> >     __0__A226=0x879918c, __0object=0x80456ac, __0xEvent=0x4,
>> >__0__A227=0x2 "")
>> >     at m_win2.c:704 (that's the last line of the function: call to Event
>> >           where program SIGSEGVs)
>> >(gdb) print __1uObjectDup
>> >$4 = (struct UI_WINDOW_OBJECT *) 0x8689fb0
>> >(gdb) print __1uObject
>> >$5 = (struct UI_WINDOW_OBJECT *) 0x1c3

>> >>Perhaps you want to describe what your *real* problem is?

>> >Stack corruption.
>> >The code again is:

>> >I have the following code in a third party library:

>> >// GeneralXEventHandler gets called for each event of the types we
>> requested
>> >// in RegisterObject().  We send the event to the Event() routine of the
>> >// object the event belongs to.
>> >void GeneralXEventHandler(Widget, XtPointer object, XEvent *xEvent,
>> >Boolean *)
>> >{
>> >     UI_WINDOW_OBJECT *uObject = (UI_WINDOW_OBJECT *)object;
>> >     // for debug
>> >^^^^^^^^^^^^^^ need a watchpoint right after assignment.
>> >     UI_WINDOW_OBJECT *uObjectDup = (UI_WINDOW_OBJECT *)object;
>> >     if (!uObject->screenID)
>> >         return;
>> >     UI_EVENT event(E_MOTIF, *xEvent);
>> >     if (xEvent->type == KeyPress && event.rawCode == XK_Tab)
>> >         _lastFocusEvent = KeyPress;
>> >     else if (xEvent->type == ButtonPress)
>> >         _lastFocusEvent = ButtonPress;

>> >     if(uObject != uObjectDup) {
>> >         l_fatal("object %p objectdup %p !!!!", uObject, uObjectDup);
>> >     }
>> >     uObject->Event(event);
>> >}

>> >I want to set a watchpoint on the contents of uObject after
>> >initialization. The function is called quite a bit and I don't want to
>> >(I can't) do it manually on every call.
>> >Is there a gcc directive to automate the setup of watchpoints?

>> Do you know what the corrupt value is when it happens?
>> If so do something like:

>> if(object = 0xDEADBEEF) {
>>        object = object;
>> }

>> Set a break on the object = object line.

> This won't be useful, because I need to know which function down the
> line is corrupting uObject. I could just as well set a breakpoint on
> l_fatal line. I need a watchpoint to track down the errant code
> and because there are dozens of calls/per second to GeneralXEventHandler
> I could not set watchpoints by hand.
> Note that I'm not likely to run out of watchpoints because they will go
> out of scope as rapidly as they will get set.

> In general terms: I need to communicate to gdb from my C++ applications.

Stub your code until the problem goes away. Then add functions back until
the problem returns.

Mike

 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by morde » Sat, 07 Dec 2002 06:56:25



> In general terms: I need to communicate to gdb from my C++ applications.

Nevermind. I just realized that's what the Expect was made for, duh.
 
 
 

Interfacing with gdb: How to set a gdb watch point from C++ code?

Post by Paul Pluzhniko » Sat, 07 Dec 2002 13:44:24



> > In general terms: I need to communicate to gdb from my C++ applications.

> Nevermind. I just realized that's what the Expect was made for, duh.

You don't need expect. You just need to set 2 breakpoints: at the
beginning and the end of the function, and attach commands to them:

  commands 1
  silent
  watch *(int **)&uObject
  continue
  end

The reason you need the second breakpoint at the end is that
watchpoints are implemented with debug registers and are attached
to a specific memory location; they do not "go out of scope" ...

While executing 2 breakpoints will most likely make your executable
crawl, it will still be faster than doing it by hand.

Or you could just use Insure++ (www.parasoft.com), which will
likely just tell you where the stack overflow is.

Cheers,
--
In order to understand recursion you must first understand recursion.

 
 
 

1. How to set break points in gdb?

Hi all,

    I am debugging using 'gdb'. I have one file 'main.c' and a file
'func.c', with their relation indicated in the makefile. I want to set a
break point in the file 'func.c'. Can anyone tell me how to do it?

    The following is what I have tried but failed:
----------------------
    gdb main
    break func.c:52

    Then I was told that "No source file named func.c"

----------------------

    Many thanks.

Sent via Deja.com http://www.deja.com/
Before you buy.

2. passwords

3. How to set break points when using gdb?

4. PPP support missing from recent kernels? Mouse buttons in YD Linux?

5. Any plans for gdb to work with C++ code compiled by xlC 5.x

6. Tickle Me, Linux

7. How to set a break point in GDB 4.10 pl1 (solaris 2)?

8. User Read/Write Mounts

9. How to set break points when using gdb?

10. gdb problem (gdb 4.8, gcc 2.6.2)

11. please HELP with GDB output redirect to GDB variable !!!

12. binutils, bugs in gdb, & "fixes" to gdb

13. gdb watch problems