Home | History | Annotate | Download | only in libprofile
      1 /*===-- BasicBlockTracing.c - Support library for basic block tracing -----===*\
      2 |*
      3 |*                     The LLVM Compiler Infrastructure
      4 |*
      5 |* This file is distributed under the University of Illinois Open Source
      6 |* License. See LICENSE.TXT for details.
      7 |*
      8 |*===----------------------------------------------------------------------===*|
      9 |*
     10 |* This file implements the call back routines for the basic block tracing
     11 |* instrumentation pass.  This should be used with the -trace-basic-blocks
     12 |* LLVM pass.
     13 |*
     14 \*===----------------------------------------------------------------------===*/
     15 
     16 #include "Profiling.h"
     17 #include <stdlib.h>
     18 #include <stdio.h>
     19 
     20 static unsigned *ArrayStart, *ArrayEnd, *ArrayCursor;
     21 
     22 /* WriteAndFlushBBTraceData - write out the currently accumulated trace data
     23  * and reset the cursor to point to the beginning of the buffer.
     24  */
     25 static void WriteAndFlushBBTraceData () {
     26   write_profiling_data(BBTraceInfo, ArrayStart, (ArrayCursor - ArrayStart));
     27   ArrayCursor = ArrayStart;
     28 }
     29 
     30 /* BBTraceAtExitHandler - When the program exits, just write out any remaining
     31  * data and free the trace buffer.
     32  */
     33 static void BBTraceAtExitHandler(void) {
     34   WriteAndFlushBBTraceData ();
     35   free (ArrayStart);
     36 }
     37 
     38 /* llvm_trace_basic_block - called upon hitting a new basic block. */
     39 void llvm_trace_basic_block (unsigned BBNum) {
     40   *ArrayCursor++ = BBNum;
     41   if (ArrayCursor == ArrayEnd)
     42     WriteAndFlushBBTraceData ();
     43 }
     44 
     45 /* llvm_start_basic_block_tracing - This is the main entry point of the basic
     46  * block tracing library.  It is responsible for setting up the atexit
     47  * handler and allocating the trace buffer.
     48  */
     49 int llvm_start_basic_block_tracing(int argc, const char **argv,
     50                               unsigned *arrayStart, unsigned numElements) {
     51   int Ret;
     52   const unsigned BufferSize = 128 * 1024;
     53   unsigned ArraySize;
     54 
     55   Ret = save_arguments(argc, argv);
     56 
     57   /* Allocate a buffer to contain BB tracing data */
     58   ArraySize = BufferSize / sizeof (unsigned);
     59   ArrayStart = malloc (ArraySize * sizeof (unsigned));
     60   ArrayEnd = ArrayStart + ArraySize;
     61   ArrayCursor = ArrayStart;
     62 
     63   /* Set up the atexit handler. */
     64   atexit (BBTraceAtExitHandler);
     65 
     66   return Ret;
     67 }
     68