gprof for RTEMS and leon2/3

Aitor.Viana.Sanchez at Aitor.Viana.Sanchez at
Tue May 18 07:35:54 UTC 2010

Hi all,

While ago looking for the a possible implementation of _mcount() and 
profil() specific routines for RTEMS and LEON2/3 I found this old thread

Didn't find anything else on the topic so I decided to implement _mcount() 
and profil() functionality over RTEMS and LEON2/3 (file attached).

It's not been integrated within RTEMS distribution as I was not interested 
in profile RTEMS but application software but the implementation is pretty 
portable as it uses __builtin_return_address() to implement mcount. 

mcount() is just responsible for recording in memory the call graph but 
there is another functionality that had to be implemented and this is the 
one provided by the profil() routine under UNIX-like systems.

profil() routine keeps an histogram of where the program counter happens 
to be every now and then. This routine is not provided in the newlib for 
SPARC so I had to implement its functionality. As I am working at 
application level this part was a bit more tricky as I need to look at and 
store the PC every clock tick.
To do that I register a new handler for the clock (replace Clock_isr). 
This new handler just saves the PC and calls Clock_isr(). But this is not 
enough as we have to store also the PC in the histogram and some checks 
and histogram scale factors need to be applied. The problem was that I 
have to do all this in the clock isr avoiding any "save" instruction but 
if the handler performs to many operations the compiler introduces "save" 
at the beginning to allocate stack space for the function, the register 
window is moved and so the PC is lost as it is stored in the local 
registers (%L1)

I don't wanna get into details here but in short the implementation 
replaces the Clock_isr() by a new one. This saves the PC in a global 
variable and calls Clock_isr(). It is also needed to wrap Clock_isr() -- 
using -Wl,--wrap linker options --  so when it is called the previously 
saved PC can be properly stored into the histogram.

Bellow the new clock handler

static rtems_isr profile_clock_isr(rtems_vector_number vector)

and the Clock_isr() wrapper routine

rtems_isr __wrap_Clock_isr()
    if( s_profil && s_profil_sz )
        if( pc )
            pc -= (int)s_lowpc;
            pc = (pc >> HISTFRACTION_LOG2);
            if( pc < s_profil_sz )  s_profil[pc]++;
    pc = 0;


I believe that introducing this code into RTEMS source tree will ease all 
these as all can be handled directly from the Clock_isr() routine. I am 
thinking about Clock_isr() acting as a bottom handler to save the PC (%l1) 
register in a global variable and then calling another function which 
would be the top handler to perform the histogram.

The dump of the profiling information is made in the mcleanup() routine 
(serial line or UDP). The routine is called at exit.

Don't know if anybody tackled this before but I did not find anything on 
the RTEMS list so here is the code


Aitor Viana Sánchez

ESA - European Space Technology Centre (ESTEC)
On-board Computer Engineer - TEC-EDS
ESA/ESTEC P.O. Box 299 / 2200AG Noordwijk ZH, The Netherlands
Tel (+31) 71 565 6727
Email: aitor.viana.sanchez at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Type: application/zip
Size: 9314 bytes
Desc: not available
URL: <>

More information about the users mailing list