[PATCH] fix profiling

Brian Dessent brian@dessent.net
Tue Aug 5 05:17:00 GMT 2008

Long story short: some asm()s have missing volatile modifiers.

The mcount() profiling hook is implemented with a short wrapper around
the actual mcount function.  The wrapper's purpose is to get the pc of
the caller as well as the return value of the caller's frame, and pass
those on as arguments to the actual mcount function.  Because it's a
local static function the compiler inlines all this into one function. 
The problem is these asm()s aren't marked volatile and so the compiler
freely rearranges them and interleaves them with the prologue of the
inlined function.  Thus mcount gets some bogus value for the pc and
ignores the data because it's not in the valid range of .text.

Since this code is lifted from the BSDs I did check that this change was
made there as well, e.g.

Unfortuantely there seems to also be some bitrot in the gprof side, as
the codepath to read BSD style gmon.out files is also broken.  I've
posted a separate patch to the binutils list.  With both these fixes,
gprof again works with Cygwin.

-------------- next part --------------
2008-08-04  Brian Dessent  <brian@dessent.net>

	* config/i386/profile.h (mcount): Mark asms volatile.

Index: config/i386/profile.h
RCS file: /cvs/src/src/winsup/cygwin/config/i386/profile.h,v
retrieving revision
diff -u -p -r1.1.1.1 profile.h
--- config/i386/profile.h	17 Feb 2000 19:38:31 -0000
+++ config/i386/profile.h	5 Aug 2008 05:02:25 -0000
@@ -48,11 +48,11 @@ mcount()								\
 	 *								\
 	 * selfpc = pc pushed by mcount call				\
 	 */								\
-	__asm("movl 4(%%ebp),%0" : "=r" (selfpc));			\
+	__asm __volatile ("movl 4(%%ebp),%0" : "=r" (selfpc));		\
 	/*								\
 	 * frompcindex = pc pushed by call into self.			\
 	 */								\
-	__asm("movl (%%ebp),%0;movl 4(%0),%0" : "=r" (frompcindex));	\
+	__asm __volatile ("movl (%%ebp),%0;movl 4(%0),%0" : "=r" (frompcindex));\
 	_mcount(frompcindex, selfpc);					\

More information about the Cygwin-patches mailing list