0a47ea38608c1b579847529045f9387c9d1aac9d
[ghc.git] / rts / Trace.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 2006-2009
4 *
5 * Debug and performance tracing
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifdef DEBUG
10
11 // external headers
12 #include "Rts.h"
13 #include "rts/Flags.h"
14
15 // internal headers
16 #include "Trace.h"
17 #include "GetTime.h"
18 #include "Stats.h"
19
20 /*
21 Features we want:
22 - multiple log message classes
23 - outpout thread ID & time on each message
24 - thread-safe
25 - trace source locations?
26 - break into the debugger?
27 */
28
29 StgWord32 classes_enabled; // not static due to inline funcs
30
31 #ifdef THREADED_RTS
32 static Mutex trace_utx;
33 #endif
34
35 #define DEBUG_FLAG(name, class) \
36 if (RtsFlags.DebugFlags.name) classes_enabled |= class;
37
38 void initTracing (void)
39 {
40 #ifdef THREADED_RTS
41 initMutex(&trace_utx);
42 #endif
43
44 DEBUG_FLAG(scheduler, DEBUG_sched);
45 DEBUG_FLAG(interpreter, DEBUG_interp);
46 DEBUG_FLAG(weak, DEBUG_weak);
47 DEBUG_FLAG(gccafs, DEBUG_gccafs);
48 DEBUG_FLAG(gc, DEBUG_gc);
49 DEBUG_FLAG(block_alloc, DEBUG_block_alloc);
50 DEBUG_FLAG(sanity, DEBUG_sanity);
51 DEBUG_FLAG(stable, DEBUG_stable);
52 DEBUG_FLAG(stm, DEBUG_stm);
53 DEBUG_FLAG(prof, DEBUG_prof);
54 DEBUG_FLAG(eventlog, DEBUG_eventlog);
55 DEBUG_FLAG(linker, DEBUG_linker);
56 DEBUG_FLAG(squeeze, DEBUG_squeeze);
57 DEBUG_FLAG(hpc, DEBUG_hpc);
58 }
59
60 static void tracePreface (void)
61 {
62 #ifdef THREADED_RTS
63 debugBelch("%12lx: ", (unsigned long)osThreadId());
64 #endif
65 if (RtsFlags.DebugFlags.timestamp) {
66 debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
67 }
68 }
69
70 void trace (StgWord32 class, const char *str, ...)
71 {
72 va_list ap;
73 va_start(ap,str);
74
75 ACQUIRE_LOCK(&trace_utx);
76
77 if ((classes_enabled & class) != 0) {
78 tracePreface();
79 vdebugBelch(str,ap);
80 debugBelch("\n");
81 }
82
83 RELEASE_LOCK(&trace_utx);
84
85 va_end(ap);
86 }
87
88 void traceBegin (const char *str, ...)
89 {
90 va_list ap;
91 va_start(ap,str);
92
93 ACQUIRE_LOCK(&trace_utx);
94
95 tracePreface();
96 vdebugBelch(str,ap);
97 }
98
99 void traceEnd (void)
100 {
101 debugBelch("\n");
102 RELEASE_LOCK(&trace_utx);
103 }
104
105 #endif