tracing malloc/free call

Marco Atzeri marco.atzeri@gmail.com
Fri Jan 16 14:24:00 GMT 2015


On 1/15/2015 10:34 AM, Corinna Vinschen wrote:
> 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...
>
[cut]
>
> 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.

Hi Corinna,

in reality I found it is "*(_my_tls.stackptr-1)"

-  malloc_printf ("(%p), called by %p", p, __builtin_return_address (0));
+  malloc_printf ("(%p), called by %p", p, *(_my_tls.stackptr-1));

Attached patch that allows tracking of original caller,
for the 4 memory allocation calls.

Tested on 64 bit.

  $ grep 0x6000D6AA1 ncview.strace4
    20 1605112 [main] ncview 4408 free: (0x6000D6AA1), called by 0x10040E744


  $ addr2line.exe -a 0x10040E744 -e /usr/bin/ncview.exe
0x000000010040e744
/usr/src/debug/ncview-2.1.4-2/src/file_netcdf.c:271


Regards
Marco







-------------- next part --------------
--- src_new/winsup/cygwin/malloc_wrapper.cc	2014-06-26 23:52:46.537847400 +0200
+++ src/winsup/cygwin/malloc_wrapper.cc	2015-01-16 14:41:15.766384600 +0100
@@ -17,6 +17,7 @@
 #include "dtable.h"
 #include "perprocess.h"
 #include "miscfuncs.h"
+#include "cygtls.h"
 #include "cygmalloc.h"
 #ifndef MALLOC_DEBUG
 #include <malloc.h>
@@ -38,7 +39,7 @@
 extern "C" void
 free (void *p)
 {
-  malloc_printf ("(%p), called by %p", p, __builtin_return_address (0));
+  malloc_printf ("(%p), called by %p", p, *(_my_tls.stackptr-1));
   if (!use_internal)
     user_data->free (p);
   else
@@ -61,7 +62,7 @@
       res = dlmalloc (size);
       __malloc_unlock ();
     }
-  malloc_printf ("(%ld) = %p, called by %p", size, res, __builtin_return_address (0));
+  malloc_printf ("(%ld) = %p, called by %p", size, res, *(_my_tls.stackptr-1));
   return res;
 }
 
@@ -77,7 +78,7 @@
       res = dlrealloc (p, size);
       __malloc_unlock ();
     }
-  malloc_printf ("(%p, %ld) = %p, called by %p", p, size, res, __builtin_return_address (0));
+  malloc_printf ("(%p, %ld) = %p, called by %p", p, size, res, *(_my_tls.stackptr-1));
   return res;
 }
 
@@ -104,7 +105,7 @@
       res = dlcalloc (nmemb, size);
       __malloc_unlock ();
     }
-  malloc_printf ("(%ld, %ld) = %p, called by %p", nmemb, size, res, __builtin_return_address (0));
+  malloc_printf ("(%ld, %ld) = %p, called by %p", nmemb, size, res, *(_my_tls.stackptr-1));
   return res;
 }
 


More information about the Cygwin-patches mailing list