X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:reply-to
	:references:mime-version:content-type:in-reply-to; q=dns; s=
	default; b=hqusmwWJd7ZPM+s1Ye2szb8g1/bSyeJ/rKUrgAscQBv57G8frsUVo
	Pf0vaXIiobvTgOs/sCHFnojYhmY5DIQVfppyVNCKyjLbfwU7atEqYgw/uQw/O2f5
	GnO6cfSvGKPchbCjPnwHr2+MNU/LBwxFn38x/zJpw7Scl72RgPD6Eg=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:date:from:to:subject:message-id:reply-to
	:references:mime-version:content-type:in-reply-to; s=default;
	 bh=eKCaqKmQ4LL6K57qwiyXz2LJWv4=; b=q7Hq3iqhORUC0970ePYMJBvkW72o
	YzleVhsex2J/2VcFukK4e7XJhlSjAqiju5VQ7WNrGkRRzVy+nq79IXPsDXV2Kb+A
	BgCLEaHr5j+WiDBUlw+T/4LPK0wc7Vq1UL8P6aCncu2HFAS8V1jj+1+fptcCaRNe
	96Cy091tAbmb+LY=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-5.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2
X-HELO: calimero.vinschen.de
Date: Thu, 15 Jan 2015 10:34:51 +0100
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: cygwin@cygwin.com
Subject: Re: tracing malloc/free call
Message-ID: <20150115093451.GB10242@calimero.vinschen.de>
Reply-To: cygwin@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
References: <54B6EE1F.60705@gmail.com>
MIME-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha1;	protocol="application/pgp-signature"; boundary="3uo+9/B/ebqu+fSQ"
Content-Disposition: inline
In-Reply-To: <54B6EE1F.60705@gmail.com>
User-Agent: Mutt/1.5.23 (2014-03-12)

--3uo+9/B/ebqu+fSQ
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi Marco,

On Jan 14 23:30, Marco Atzeri wrote:
> Debugging a program I am trying to catch where this call is happening
>=20
> 17 1499678 [main] ncview 1484 free: (0x6000D7961), called by 0x180115A0B
>=20
> 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
>=20
> 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 ();
>     }
> }
>=20
>=20
> 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=3Dmarkup#l146
(2) https://cygwin.com/viewvc/src/winsup/cygwin/cygtls.h?view=3Dmarkup#l205
(3) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=3Dmarkup#l157
(4) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=3Dmarkup#l158
(5) https://cygwin.com/viewvc/src/winsup/cygwin/gendef?view=3Dmarkup#l180

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

--3uo+9/B/ebqu+fSQ
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJUt4m7AAoJEPU2Bp2uRE+g+pUP/ArnZnGV1cAucrsIDPJGuerC
5BgPRaU/XeA8kHv+5k2TDPxIWVAO06zTFywwJpjw/RsTKHzGp1OwaH4BLT/3gMBs
BASnzRyHpDR1zi+QvAYj9v1dN6MLfO2gGQSWyqyNl+awQrhC1ThM8qU2YKoqBVa5
JI3Wg/0E2c4LOPlkcpeM3HX2CYKWAPeydVawdEyIKTkAdgh8jb5jl2sJDpM08RwQ
L5ryEPbWz92JNC89qLv4gCw3EpTYQDctjqw4Y/lYioNQT5RpMk+61AgABGk/rdyZ
jQUdm/xr4JqZUt2oGkf/dryu+B1itgimZ5Fqb4F1wqc0wxHTV8Hfvxn4W6NLb1Ll
mlGfwI3wmmYBYXgJrOgEHpLbcT+shCQUEKV3JugqBbibuxcvpmJSdD/KmbFwqHjp
2zHTA9KVvvwyv1/7hIoYqZRaAk7Stnk+FhzqicJHWjiw6D6GWUAu1xnWg0nF9sZ0
bR8ZQHjist+n5ljWOnZtPlyXkF7GWq9gSJogaMSz8157GbpqJnQzxNN7fPt+6Z+F
Zky0BltPRI7jvGKLcywMRzm36wE/Aw/HvLftVhB47ram4e02WurUBWHBNb5zXe4z
5h+xhmsd46/gBlSRdvm8qPn+iYDJ3CM5MKhslsY6kOHXWzyFZuyz+/1mGitewHSj
ECTU0PUDvX0mMwPANoii
=yl/w
-----END PGP SIGNATURE-----

--3uo+9/B/ebqu+fSQ--
