Build mono runtime under none desktop Windows API family, adjustments and cleanup.
[mono.git] / mono / metadata / lock-tracer.c
1 /*
2  * lock-tracer.c: Runtime simple lock tracer
3  *
4  * Authors:
5  *      Rodrigo Kumpera (rkumpera@novell.com)
6  * 
7  */
8
9 #include <config.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <sys/types.h>
14
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18
19 #ifdef HAVE_EXECINFO_H
20 #include <execinfo.h>
21 #endif
22
23 #include <mono/io-layer/io-layer.h>
24 #include <mono/utils/mono-compiler.h>
25
26 #include "lock-tracer.h"
27
28 /*
29  * This is a very simple lock trace implementation. It can be used to verify that the runtime is
30  * correctly following all locking rules.
31  * 
32  * To log more kind of locks just do the following:
33  *      - add an entry into the RuntimeLocks enum
34  *  - change mono_os_mutex_lock(mutex) to mono_locks_os_acquire (mutex, LockName)
35  *  - change mono_os_mutex_unlock(mutex) to mono_locks_os_release (mutex, LockName)
36  *  - change mono_coop_mutex_lock(mutex) to mono_locks_coop_acquire (mutex, LockName)
37  *  - change mono_coop_mutex_unlock(mutex) to mono_locks_coop_release (mutex, LockName)
38  *  - change the decoder to understand the new lock kind.
39  *
40  * TODO:
41  *      - Use unbuffered IO without fsync
42  *  - Switch to a binary log format
43  *  - Enable tracing of more runtime locks
44  *  - Add lock check assertions (must_not_hold_any_lock_but, must_hold_lock, etc)
45  *   This should be used to verify methods that expect that a given lock is held at entrypoint, for example.
46  * 
47  * To use the trace, define LOCK_TRACER in lock-trace.h and when running mono define MONO_ENABLE_LOCK_TRACER.
48  * This will produce a locks.ZZZ where ZZZ is the pid of the mono process.
49  * Use the decoder to verify the result.
50  */
51
52 #ifdef LOCK_TRACER
53
54 #ifdef TARGET_OSX
55 #include <dlfcn.h>
56 #endif
57
58 static FILE *trace_file;
59 static mono_mutex_t tracer_lock;
60 static size_t base_address;
61
62 typedef enum {
63         RECORD_MUST_NOT_HOLD_ANY,
64         RECORD_MUST_NOT_HOLD_ONE,
65         RECORD_MUST_HOLD_ONE,
66         RECORD_LOCK_ACQUIRED,
67         RECORD_LOCK_RELEASED
68 } RecordType;
69
70 void
71 mono_locks_tracer_init (void)
72 {
73         Dl_info info;
74         int res;
75         char *name;
76         mono_os_mutex_init_recursive (&tracer_lock);
77         if (!g_getenv ("MONO_ENABLE_LOCK_TRACER"))
78                 return;
79         name = g_strdup_printf ("locks.%d", getpid ());
80         trace_file = fopen (name, "w+");
81         g_free (name);
82
83 #ifdef TARGET_OSX
84         res = dladdr ((void*)&mono_locks_tracer_init, &info);
85         /* The 0x1000 offset was found by empirically trying it. */
86         if (res)
87                 base_address = (size_t)info.dli_fbase - 0x1000;
88 #endif
89 }
90
91
92 #ifdef HAVE_EXECINFO_H
93
94 static int
95 mono_backtrace (gpointer array[], int traces)
96 {
97         return backtrace (array, traces);
98 }
99
100 #else
101
102 static int
103 mono_backtrace (gpointer array[], int traces)
104 {
105         return 0;
106 }
107
108 #endif
109
110 static void
111 add_record (RecordType record_kind, RuntimeLocks kind, gpointer lock)
112 {
113         int i = 0;
114         const int no_frames = 6;
115         gpointer frames[no_frames];
116
117         char *msg;
118         if (!trace_file)
119                 return;
120
121         memset (frames, 0, sizeof (gpointer) * no_frames);
122         mono_backtrace (frames, no_frames);
123         for (i = 0; i < no_frames; ++i)
124                 frames [i] = (gpointer)((size_t)frames[i] - base_address);
125
126         /*We only dump 5 frames, which should be more than enough to most analysis.*/
127         msg = g_strdup_printf ("%x,%d,%d,%p,%p,%p,%p,%p,%p\n", (guint32)mono_native_thread_id_get (), record_kind, kind, lock, frames [1], frames [2], frames [3], frames [4], frames [5]);
128         fwrite (msg, strlen (msg), 1, trace_file);
129         fflush (trace_file);
130         g_free (msg);
131 }
132
133 void
134 mono_locks_lock_acquired (RuntimeLocks kind, gpointer lock)
135 {
136         add_record (RECORD_LOCK_ACQUIRED, kind, lock);
137 }
138
139 void
140 mono_locks_lock_released (RuntimeLocks kind, gpointer lock)
141 {
142         add_record (RECORD_LOCK_RELEASED, kind, lock);
143 }
144 #else
145
146 MONO_EMPTY_SOURCE_FILE (lock_tracer);
147 #endif /* LOCK_TRACER */