1 /*===-- BasicBlockTracing.c - Support library for basic block tracing -----===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 |*===----------------------------------------------------------------------===*|
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
14 \*===----------------------------------------------------------------------===*/
16 #include "Profiling.h"
20 static unsigned *ArrayStart
, *ArrayEnd
, *ArrayCursor
;
22 /* WriteAndFlushBBTraceData - write out the currently accumulated trace data
23 * and reset the cursor to point to the beginning of the buffer.
25 static void WriteAndFlushBBTraceData () {
26 write_profiling_data(BBTraceInfo
, ArrayStart
, (ArrayCursor
- ArrayStart
));
27 ArrayCursor
= ArrayStart
;
30 /* BBTraceAtExitHandler - When the program exits, just write out any remaining
31 * data and free the trace buffer.
33 static void BBTraceAtExitHandler() {
34 WriteAndFlushBBTraceData ();
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 ();
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.
49 int llvm_start_basic_block_tracing(int argc
, const char **argv
,
50 unsigned *arrayStart
, unsigned numElements
) {
52 const unsigned BufferSize
= 128 * 1024;
55 Ret
= save_arguments(argc
, argv
);
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
;
63 /* Set up the atexit handler. */
64 atexit (BBTraceAtExitHandler
);