FIX #2164: check for ThreadRelocated in isAlive()
[ghc.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 TRACE_FLAG(gc, TRACE_gc);
112 }
113
114 static void tracePreface (void)
115 {
116 #ifdef THREADED_RTS
117 debugBelch("%12lx: ", (unsigned long)osThreadId());
118 #endif
119 if (RtsFlags.TraceFlags.timestamp) {
120 debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
121 }
122 }
123
124 void trace (StgWord32 class, const char *str, ...)
125 {
126 va_list ap;
127 va_start(ap,str);
128
129 ACQUIRE_LOCK(&trace_utx);
130
131 if ((classes_enabled & class) != 0) {
132 tracePreface();
133 vdebugBelch(str,ap);
134 debugBelch("\n");
135 }
136
137 RELEASE_LOCK(&trace_utx);
138
139 va_end(ap);
140 }
141
142 void traceBegin (const char *str, ...)
143 {
144 va_list ap;
145 va_start(ap,str);
146
147 ACQUIRE_LOCK(&trace_utx);
148
149 tracePreface();
150 vdebugBelch(str,ap);
151 }
152
153 void traceEnd (void)
154 {
155 debugBelch("\n");
156 RELEASE_LOCK(&trace_utx);
157 }