Merge pull request #2124 from mhutch/mono-api-info-forwarders
[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
25 #include "lock-tracer.h"
26
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_mutex_lock(mutex) to mono_locks_acquire (mutex, LockName)
35  *  - change mono_mutex_unlock to mono_locks_release (mutex, LockName)
36  *  - change the decoder to understand the new lock kind.
37  *
38  * TODO:
39  *      - Use unbuffered IO without fsync
40  *  - Switch to a binary log format
41  *  - Enable tracing of more runtime locks
42  *  - Add lock check assertions (must_not_hold_any_lock_but, must_hold_lock, etc)
43  *   This should be used to verify methods that expect that a given lock is held at entrypoint, for example.
44  * 
45  * To use the trace, define LOCK_TRACER in lock-trace.h and when running mono define MONO_ENABLE_LOCK_TRACER.
46  * This will produce a locks.ZZZ where ZZZ is the pid of the mono process.
47  * Use the decoder to verify the result.
48  */
49
50 #ifdef LOCK_TRACER
51
52 #ifdef TARGET_OSX
53 #include <dlfcn.h>
54 #endif
55
56 static FILE *trace_file;
57 static mono_mutex_t tracer_lock;
58 static size_t base_address;
59
60 typedef enum {
61         RECORD_MUST_NOT_HOLD_ANY,
62         RECORD_MUST_NOT_HOLD_ONE,
63         RECORD_MUST_HOLD_ONE,
64         RECORD_LOCK_ACQUIRED,
65         RECORD_LOCK_RELEASED
66 } RecordType;
67
68 void
69 mono_locks_tracer_init (void)
70 {
71         Dl_info info;
72         int res;
73         char *name;
74         mono_mutex_init_recursive (&tracer_lock);
75         if (!g_getenv ("MONO_ENABLE_LOCK_TRACER"))
76                 return;
77         name = g_strdup_printf ("locks.%d", getpid ());
78         trace_file = fopen (name, "w+");
79         g_free (name);
80
81 #ifdef TARGET_OSX
82         res = dladdr ((void*)&mono_locks_tracer_init, &info);
83         /* The 0x1000 offset was found by empirically trying it. */
84         if (res)
85                 base_address = (size_t)info.dli_fbase - 0x1000;
86 #endif
87 }
88
89
90 #ifdef HAVE_EXECINFO_H
91
92 static int
93 mono_backtrace (gpointer array[], int traces)
94 {
95         return backtrace (array, traces);
96 }
97
98 #else
99
100 static int
101 mono_backtrace (gpointer array[], int traces)
102 {
103         return 0;
104 }
105
106 #endif
107
108 static void
109 add_record (RecordType record_kind, RuntimeLocks kind, gpointer lock)
110 {
111         int i = 0;
112         const int no_frames = 6;
113         gpointer frames[no_frames];
114
115         char *msg;
116         if (!trace_file)
117                 return;
118
119         memset (frames, 0, sizeof (gpointer) * no_frames);
120         mono_backtrace (frames, no_frames);
121         for (i = 0; i < no_frames; ++i)
122                 frames [i] = (gpointer)((size_t)frames[i] - base_address);
123
124         /*We only dump 5 frames, which should be more than enough to most analysis.*/
125         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]);
126         fwrite (msg, strlen (msg), 1, trace_file);
127         fflush (trace_file);
128         g_free (msg);
129 }
130
131 void
132 mono_locks_lock_acquired (RuntimeLocks kind, gpointer lock)
133 {
134         add_record (RECORD_LOCK_ACQUIRED, kind, lock);
135 }
136
137 void
138 mono_locks_lock_released (RuntimeLocks kind, gpointer lock)
139 {
140         add_record (RECORD_LOCK_RELEASED, kind, lock);
141 }
142
143 #endif