tools/reorder/mcount.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/reorder/mcount.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,38 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 +
    1.10 +  A library that can be LD_PRELOAD-ed to dump a function's address on
    1.11 +  function entry.
    1.12 +
    1.13 + */
    1.14 +
    1.15 +#include <unistd.h>
    1.16 +
    1.17 +/**
    1.18 + * When compiled with `-pg' (or even just `-p'), gcc inserts a call to
    1.19 + * |mcount| in each function prologue. This implementation of |mcount|
    1.20 + * will grab the return address from the stack, and write it to stdout
    1.21 + * as a binary stream, creating a gross execution trace the
    1.22 + * instrumented program.
    1.23 + *
    1.24 + * For more info on gcc inline assembly, see:
    1.25 + *
    1.26 + *   <http://www.ibm.com/developerworks/linux/library/l-ia.html?dwzone=linux>
    1.27 + *
    1.28 + */
    1.29 +void
    1.30 +mcount()
    1.31 +{
    1.32 +    register unsigned int caller;
    1.33 +    unsigned int buf[1];
    1.34 +
    1.35 +    // Grab the return address. 
    1.36 +    asm("movl 4(%%esp),%0"
    1.37 +        : "=r"(caller));
    1.38 +
    1.39 +    buf[0] = caller;
    1.40 +    write(STDOUT_FILENO, buf, sizeof buf[0]);
    1.41 +}

mercurial