Ensure runhaskell is rebuild in stage2
[ghc-hetmet.git] / rts / Trace.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 2006
4  *
5  * Debug and performance tracing
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #include "Rts.h"
10 #include "OSThreads.h"
11 #include "Trace.h"
12 #include "RtsFlags.h"
13 #include "GetTime.h"
14 #include "Stats.h"
15
16 /*
17   Features we want:
18     - multiple log message classes
19     - outpout thread ID & time on each message
20     - thread-safe
21     - trace source locations?
22     - break into the debugger?
23 */
24
25 StgWord32 classes_enabled; // not static due to inline funcs
26
27 #ifdef THREADED_RTS
28 static Mutex trace_utx;
29 #endif
30
31 #ifdef DEBUG
32 #define DEBUG_FLAG(name, class) \
33     if (RtsFlags.DebugFlags.name) classes_enabled |= class;
34 #else
35 #define DEBUG_FLAG(name, class) \
36     /* nothing */
37 #endif
38
39 #ifdef PAR
40 #define PAR_FLAG(name, class) \
41     if (RtsFlags.ParFlags.Debug.name) classes_enabled |= class;
42 #else
43 #define PAR_FLAG(name, class) \
44     /* nothing */
45 #endif
46
47 #ifdef GRAN
48 #define GRAN_FLAG(name, class) \
49     if (RtsFlags.GranFlags.Debug.name) classes_enabled |= class;
50 #else
51 #define GRAN_FLAG(name, class) \
52     /* nothing */
53 #endif
54
55 #define TRACE_FLAG(name, class) \
56     if (RtsFlags.TraceFlags.name) classes_enabled |= class;
57
58
59 void initTracing (void)
60 {
61 #ifdef THREADED_RTS
62     initMutex(&trace_utx);
63 #endif
64
65     DEBUG_FLAG(scheduler,    DEBUG_sched);
66     DEBUG_FLAG(interpreter,  DEBUG_interp);
67     DEBUG_FLAG(weak,         DEBUG_weak);
68     DEBUG_FLAG(gccafs,       DEBUG_gccafs);
69     DEBUG_FLAG(gc,           DEBUG_gc);
70     DEBUG_FLAG(block_alloc,  DEBUG_block_alloc);
71     DEBUG_FLAG(sanity,       DEBUG_sanity);
72     DEBUG_FLAG(stable,       DEBUG_stable);
73     DEBUG_FLAG(stm,          DEBUG_stm);
74     DEBUG_FLAG(prof,         DEBUG_prof);
75     DEBUG_FLAG(gran,         DEBUG_gran);
76     DEBUG_FLAG(par,          DEBUG_par);
77     DEBUG_FLAG(linker,       DEBUG_linker);
78     DEBUG_FLAG(squeeze,      DEBUG_squeeze);
79     DEBUG_FLAG(hpc,          DEBUG_hpc);
80
81     PAR_FLAG(verbose,        PAR_DEBUG_verbose);
82     PAR_FLAG(bq,             PAR_DEBUG_bq);
83     PAR_FLAG(schedule,       PAR_DEBUG_schedule);
84     PAR_FLAG(free,           PAR_DEBUG_free);
85     PAR_FLAG(resume,         PAR_DEBUG_resume);
86     PAR_FLAG(weight,         PAR_DEBUG_weight);
87     PAR_FLAG(fetch,          PAR_DEBUG_fetch);
88     PAR_FLAG(fish,           PAR_DEBUG_fish);
89     PAR_FLAG(tables,         PAR_DEBUG_tables);
90     PAR_FLAG(packet,         PAR_DEBUG_packet);
91     PAR_FLAG(pack,           PAR_DEBUG_pack);
92     PAR_FLAG(paranoia,       PAR_DEBUG_paranoia);
93
94     GRAN_FLAG(event_trace,   GRAN_DEBUG_event_trace);
95     GRAN_FLAG(event_stats,   GRAN_DEBUG_event_stats);
96     GRAN_FLAG(bq,            GRAN_DEBUG_bq);
97     GRAN_FLAG(pack,          GRAN_DEBUG_pack);
98     GRAN_FLAG(checkSparkQ,   GRAN_DEBUG_checkSparkQ);
99     GRAN_FLAG(thunkStealing, GRAN_DEBUG_thunkStealing);
100     GRAN_FLAG(randomSteal,   GRAN_DEBUG_randomSteal);
101     GRAN_FLAG(findWork,      GRAN_DEBUG_findWork);
102     GRAN_FLAG(unused,        GRAN_DEBUG_unused);
103     GRAN_FLAG(pri,           GRAN_DEBUG_pri);
104     GRAN_FLAG(checkLight,    GRAN_DEBUG_checkLight);
105     GRAN_FLAG(sortedQ,       GRAN_DEBUG_sortedQ);
106     GRAN_FLAG(blockOnFetch,  GRAN_DEBUG_blockOnFetch);
107     GRAN_FLAG(packBuffer,    GRAN_DEBUG_packBuffer);
108     GRAN_FLAG(blockedOnFetch_sanity, GRAN_DEBUG_BOF_sanity);
109
110     TRACE_FLAG(sched, TRACE_sched);
111 }
112
113 static void tracePreface (void)
114 {
115 #ifdef THREADED_RTS
116     debugBelch("%12lx: ", (unsigned long)osThreadId());
117 #endif
118     if (RtsFlags.TraceFlags.timestamp) {
119         debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
120     }
121 }
122
123 void trace (StgWord32 class, const char *str, ...)
124 {
125     va_list ap;
126     va_start(ap,str);
127
128     ACQUIRE_LOCK(&trace_utx);
129
130     if ((classes_enabled & class) != 0) {
131         tracePreface();
132         vdebugBelch(str,ap);
133         debugBelch("\n");
134     }
135
136     RELEASE_LOCK(&trace_utx);
137
138     va_end(ap);
139 }
140
141 void traceBegin (const char *str, ...)
142 {
143     va_list ap;
144     va_start(ap,str);
145
146     ACQUIRE_LOCK(&trace_utx);
147
148     tracePreface();
149     vdebugBelch(str,ap);
150 }
151
152 void traceEnd (void)
153 {
154     debugBelch("\n");
155     RELEASE_LOCK(&trace_utx);
156 }