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