First set of licensing changes
[mono.git] / mono / sgen / sgen-protocol.c
1 /*
2  * sgen-protocol.c: Binary protocol of internal activity, to aid
3  * debugging.
4  *
5  * Copyright 2001-2003 Ximian, Inc
6  * Copyright 2003-2010 Novell, Inc.
7  * Copyright (C) 2012 Xamarin Inc
8  *
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11
12 #ifdef HAVE_SGEN_GC
13
14 #include "config.h"
15 #include "sgen-conf.h"
16 #include "sgen-gc.h"
17 #include "sgen-protocol.h"
18 #include "sgen-memory-governor.h"
19 #include "sgen-thread-pool.h"
20 #include "sgen-client.h"
21 #include "mono/utils/mono-membar.h"
22
23 #include <errno.h>
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #include <fcntl.h>
28 #endif
29
30 /* FIXME Implement binary protocol IO on systems that don't have unistd */
31 #ifdef HAVE_UNISTD_H
32 /* If valid, dump binary protocol to this file */
33 static int binary_protocol_file = -1;
34
35 /* We set this to -1 to indicate an exclusive lock */
36 static volatile int binary_protocol_use_count = 0;
37
38 #define BINARY_PROTOCOL_BUFFER_SIZE     (65536 - 2 * 8)
39
40 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
41 struct _BinaryProtocolBuffer {
42         BinaryProtocolBuffer * volatile next;
43         volatile int index;
44         unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
45 };
46
47 static BinaryProtocolBuffer * volatile binary_protocol_buffers = NULL;
48
49 static char* filename_or_prefix = NULL;
50 static int current_file_index = 0;
51 static long long current_file_size = 0;
52 static long long file_size_limit;
53
54 static char*
55 filename_for_index (int index)
56 {
57         char *filename;
58
59         SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
60
61         filename = (char *)sgen_alloc_internal_dynamic (strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
62         sprintf (filename, "%s.%d", filename_or_prefix, index);
63
64         return filename;
65 }
66
67 static void
68 free_filename (char *filename)
69 {
70         SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
71
72         sgen_free_internal_dynamic (filename, strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL);
73 }
74
75 static void
76 binary_protocol_open_file (void)
77 {
78         char *filename;
79
80         if (file_size_limit > 0)
81                 filename = filename_for_index (current_file_index);
82         else
83                 filename = filename_or_prefix;
84
85         do {
86                 binary_protocol_file = open (filename, O_CREAT|O_WRONLY|O_TRUNC, 0644);
87                 if (binary_protocol_file == -1 && errno != EINTR)
88                         break; /* Failed */
89         } while (binary_protocol_file == -1);
90
91         if (binary_protocol_file == -1)
92                 g_error ("sgen binary protocol: failed to open file");
93
94         if (file_size_limit > 0)
95                 free_filename (filename);
96 }
97 #endif
98
99 void
100 binary_protocol_init (const char *filename, long long limit)
101 {
102 #ifdef HAVE_UNISTD_H
103         filename_or_prefix = (char *)sgen_alloc_internal_dynamic (strlen (filename) + 1, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
104         strcpy (filename_or_prefix, filename);
105
106         file_size_limit = limit;
107
108         binary_protocol_open_file ();
109 #else
110         g_error ("sgen binary protocol: not supported");
111 #endif
112 }
113
114 gboolean
115 binary_protocol_is_enabled (void)
116 {
117 #ifdef HAVE_UNISTD_H
118         return binary_protocol_file != -1;
119 #else
120         return FALSE;
121 #endif
122 }
123
124 #ifdef HAVE_UNISTD_H
125
126 static void
127 close_binary_protocol_file (void)
128 {
129         while (close (binary_protocol_file) == -1 && errno == EINTR)
130                 ;
131         binary_protocol_file = -1;
132 }
133
134 static gboolean
135 try_lock_exclusive (void)
136 {
137         do {
138                 if (binary_protocol_use_count)
139                         return FALSE;
140         } while (InterlockedCompareExchange (&binary_protocol_use_count, -1, 0) != 0);
141         mono_memory_barrier ();
142         return TRUE;
143 }
144
145 static void
146 unlock_exclusive (void)
147 {
148         mono_memory_barrier ();
149         SGEN_ASSERT (0, binary_protocol_use_count == -1, "Exclusively locked count must be -1");
150         if (InterlockedCompareExchange (&binary_protocol_use_count, 0, -1) != -1)
151                 SGEN_ASSERT (0, FALSE, "Somebody messed with the exclusive lock");
152 }
153
154 static void
155 lock_recursive (void)
156 {
157         int old_count;
158         do {
159         retry:
160                 old_count = binary_protocol_use_count;
161                 if (old_count < 0) {
162                         /* Exclusively locked - retry */
163                         /* FIXME: short back-off */
164                         goto retry;
165                 }
166         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
167         mono_memory_barrier ();
168 }
169
170 static void
171 unlock_recursive (void)
172 {
173         int old_count;
174         mono_memory_barrier ();
175         do {
176                 old_count = binary_protocol_use_count;
177                 SGEN_ASSERT (0, old_count > 0, "Locked use count must be at least 1");
178         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
179 }
180
181 static void
182 binary_protocol_flush_buffer (BinaryProtocolBuffer *buffer)
183 {
184         ssize_t ret;
185         size_t to_write = buffer->index;
186         size_t written = 0;
187         g_assert (buffer->index > 0);
188
189         while (written < to_write) {
190                 ret = write (binary_protocol_file, buffer->buffer + written, to_write - written);
191                 if (ret >= 0)
192                         written += ret;
193                 else if (errno == EINTR)
194                         continue;
195                 else
196                         close_binary_protocol_file ();
197         }
198
199         current_file_size += buffer->index;
200
201         sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
202 }
203
204 static void
205 binary_protocol_check_file_overflow (void)
206 {
207         if (file_size_limit <= 0 || current_file_size < file_size_limit)
208                 return;
209
210         close_binary_protocol_file ();
211
212         if (current_file_index > 0) {
213                 char *filename = filename_for_index (current_file_index - 1);
214                 unlink (filename);
215                 free_filename (filename);
216         }
217
218         ++current_file_index;
219         current_file_size = 0;
220
221         binary_protocol_open_file ();
222 }
223 #endif
224
225 /*
226  * Flushing buffers takes an exclusive lock, so it must only be done when the world is
227  * stopped, otherwise we might end up with a deadlock because a stopped thread owns the
228  * lock.
229  *
230  * The protocol entries that do flush have `FLUSH()` in their definition.
231  */
232 void
233 binary_protocol_flush_buffers (gboolean force)
234 {
235 #ifdef HAVE_UNISTD_H
236         int num_buffers = 0, i;
237         BinaryProtocolBuffer *buf;
238         BinaryProtocolBuffer **bufs;
239
240         if (binary_protocol_file == -1)
241                 return;
242
243         if (!force && !try_lock_exclusive ())
244                 return;
245
246         for (buf = binary_protocol_buffers; buf != NULL; buf = buf->next)
247                 ++num_buffers;
248         bufs = (BinaryProtocolBuffer **)sgen_alloc_internal_dynamic (num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
249         for (buf = binary_protocol_buffers, i = 0; buf != NULL; buf = buf->next, i++)
250                 bufs [i] = buf;
251         SGEN_ASSERT (0, i == num_buffers, "Binary protocol buffer count error");
252
253         binary_protocol_buffers = NULL;
254
255         for (i = num_buffers - 1; i >= 0; --i) {
256                 binary_protocol_flush_buffer (bufs [i]);
257                 binary_protocol_check_file_overflow ();
258         }
259
260         sgen_free_internal_dynamic (buf, num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL);
261
262         if (!force)
263                 unlock_exclusive ();
264 #endif
265 }
266
267 #ifdef HAVE_UNISTD_H
268 static BinaryProtocolBuffer*
269 binary_protocol_get_buffer (int length)
270 {
271         BinaryProtocolBuffer *buffer, *new_buffer;
272  retry:
273         buffer = binary_protocol_buffers;
274         if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
275                 return buffer;
276
277         new_buffer = (BinaryProtocolBuffer *)sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "debugging memory");
278         new_buffer->next = buffer;
279         new_buffer->index = 0;
280
281         if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
282                 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
283                 goto retry;
284         }
285
286         return new_buffer;
287 }
288 #endif
289
290 static void
291 protocol_entry (unsigned char type, gpointer data, int size)
292 {
293 #ifdef HAVE_UNISTD_H
294         int index;
295         BinaryProtocolBuffer *buffer;
296
297         if (binary_protocol_file == -1)
298                 return;
299
300         if (sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()))
301                 type |= 0x80;
302
303         lock_recursive ();
304
305  retry:
306         buffer = binary_protocol_get_buffer (size + 1);
307  retry_same_buffer:
308         index = buffer->index;
309         if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
310                 goto retry;
311
312         if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
313                 goto retry_same_buffer;
314
315         /* FIXME: if we're interrupted at this point, we have a buffer
316            entry that contains random data. */
317
318         buffer->buffer [index++] = type;
319         memcpy (buffer->buffer + index, data, size);
320         index += size;
321
322         g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
323
324         unlock_recursive ();
325 #endif
326 }
327
328 #define TYPE_INT int
329 #define TYPE_LONGLONG long long
330 #define TYPE_SIZE size_t
331 #define TYPE_POINTER gpointer
332 #define TYPE_BOOL gboolean
333
334 #define BEGIN_PROTOCOL_ENTRY0(method) \
335         void method (void) { \
336                 int __type = PROTOCOL_ID(method); \
337                 gpointer __data = NULL; \
338                 int __size = 0; \
339                 CLIENT_PROTOCOL_NAME (method) ();
340 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
341         void method (t1 f1) { \
342                 PROTOCOL_STRUCT(method) __entry = { f1 }; \
343                 int __type = PROTOCOL_ID(method); \
344                 gpointer __data = &__entry; \
345                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
346                 CLIENT_PROTOCOL_NAME (method) (f1);
347 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
348         void method (t1 f1, t2 f2) { \
349                 PROTOCOL_STRUCT(method) __entry = { f1, f2 }; \
350                 int __type = PROTOCOL_ID(method); \
351                 gpointer __data = &__entry; \
352                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
353                 CLIENT_PROTOCOL_NAME (method) (f1, f2);
354 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
355         void method (t1 f1, t2 f2, t3 f3) { \
356                 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3 }; \
357                 int __type = PROTOCOL_ID(method); \
358                 gpointer __data = &__entry; \
359                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
360                 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3);
361 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
362         void method (t1 f1, t2 f2, t3 f3, t4 f4) { \
363                 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4 }; \
364                 int __type = PROTOCOL_ID(method); \
365                 gpointer __data = &__entry; \
366                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
367                 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4);
368 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
369         void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5) { \
370                 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5 }; \
371                 int __type = PROTOCOL_ID(method); \
372                 gpointer __data = &__entry; \
373                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
374                 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5);
375 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
376         void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5, t6 f6) { \
377                 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5, f6 }; \
378                 int __type = PROTOCOL_ID(method); \
379                 gpointer __data = &__entry; \
380                 int __size = sizeof (PROTOCOL_STRUCT(method)); \
381                 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5, f6);
382
383 #define DEFAULT_PRINT()
384 #define CUSTOM_PRINT(_)
385
386 #define IS_ALWAYS_MATCH(_)
387 #define MATCH_INDEX(_)
388 #define IS_VTABLE_MATCH(_)
389
390 #define END_PROTOCOL_ENTRY \
391                 protocol_entry (__type, __data, __size); \
392         }
393
394 #define END_PROTOCOL_ENTRY_FLUSH \
395                 protocol_entry (__type, __data, __size); \
396                 binary_protocol_flush_buffers (FALSE); \
397         }
398
399 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
400 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
401         BEGIN_PROTOCOL_ENTRY0 (method)
402 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
403         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
404 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
405         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
406 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
407         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
408 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
409         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
410 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
411         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
412 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
413         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
414
415 #define END_PROTOCOL_ENTRY_HEAVY \
416         END_PROTOCOL_ENTRY
417 #else
418 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method)
419 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1)
420 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2)
421 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3)
422 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4)
423 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
424 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
425
426 #define END_PROTOCOL_ENTRY_HEAVY
427 #endif
428
429 #include "sgen-protocol-def.h"
430
431 #undef TYPE_INT
432 #undef TYPE_LONGLONG
433 #undef TYPE_SIZE
434 #undef TYPE_POINTER
435 #undef TYPE_BOOL
436
437 #endif /* HAVE_SGEN_GC */