Do not link ghc stage1 using -threaded, only for stage2 or 3
[ghc-hetmet.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 #include "Rts.h"
12 #include "OSThreads.h"
13 #include "Trace.h"
14 #include "RtsFlags.h"
15 #include "GetTime.h"
16 #include "Stats.h"
17
18 /*
19   Features we want:
20     - multiple log message classes
21     - outpout thread ID & time on each message
22     - thread-safe
23     - trace source locations?
24     - break into the debugger?
25 */
26
27 StgWord32 classes_enabled; // not static due to inline funcs
28
29 #ifdef THREADED_RTS
30 static Mutex trace_utx;
31 #endif
32
33 #define DEBUG_FLAG(name, class) \
34     if (RtsFlags.DebugFlags.name) classes_enabled |= class;
35
36 void initTracing (void)
37 {
38 #ifdef THREADED_RTS
39     initMutex(&trace_utx);
40 #endif
41
42     DEBUG_FLAG(scheduler,    DEBUG_sched);
43     DEBUG_FLAG(interpreter,  DEBUG_interp);
44     DEBUG_FLAG(weak,         DEBUG_weak);
45     DEBUG_FLAG(gccafs,       DEBUG_gccafs);
46     DEBUG_FLAG(gc,           DEBUG_gc);
47     DEBUG_FLAG(block_alloc,  DEBUG_block_alloc);
48     DEBUG_FLAG(sanity,       DEBUG_sanity);
49     DEBUG_FLAG(stable,       DEBUG_stable);
50     DEBUG_FLAG(stm,          DEBUG_stm);
51     DEBUG_FLAG(prof,         DEBUG_prof);
52     DEBUG_FLAG(eventlog,     DEBUG_eventlog);
53     DEBUG_FLAG(gran,         DEBUG_gran);
54     DEBUG_FLAG(par,          DEBUG_par);
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