This is the mail archive of the cygwin mailing list for the Cygwin 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: tracing malloc/free call


Hi Marco,

On Jan 14 23:30, Marco Atzeri wrote:
> Debugging a program I am trying to catch where this call is happening
> 
> 17 1499678 [main] ncview 1484 free: (0x6000D7961), called by 0x180115A0B
> 
> unfortunately the 0x180115A0B address is not real caller address

No, the return address is the address of the _sigbe function defined in
the gendef script...

> /usr/src/debug/cygwin-1.7.33-1/winsup/cygwin/malloc_wrapper.cc
> 
> extern "C" void
> free (void *p)
> {
>   malloc_printf ("(%p), called by %p", p, __builtin_return_address (0));
>   if (!use_internal)
>     user_data->free (p);
>   else
>     {
>       __malloc_lock ();
>       dlfree (p);
>       __malloc_unlock ();
>     }
> }
> 
> 
> Any way to improve the tracing ?

When the Cygwin "free" function gets called, the caller actually calls an
autogenerated wrapper function "__sigfe_free".  This in turn jumps to the
_sigfe function(1) which stores the actual return address on top of the
cygtls stack(2)(3) and replaces the return address on the stack with
the address of the _sigbe function(4).  The _sigbe function, when called
by returning from the "free" function, restores the correct return address
from the cygtls stack(5).

Bottom line, you should be able to fetch the original return address by
printing the value at

  *(void*)_my_tls->stackptr

which points to the uppermost entry on the stack.


HTH,
Corinna


(1) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=markup#l146
(2) https://cygwin.com/viewvc/src/winsup/cygwin/cygtls.h?view=markup#l205
(3) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=markup#l157
(4) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=markup#l158
(5) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=markup#l180

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

Attachment: pgpWNc5PxlmV3.pgp
Description: PGP signature


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