2 * sgen-protocol.c: Binary protocol of internal activity, to aid
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright (C) 2012 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "sgen-conf.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 #include "mono/utils/mono-proclib.h"
31 /* FIXME Implement binary protocol IO on systems that don't have unistd */
33 /* If valid, dump binary protocol to this file */
34 static int binary_protocol_file = -1;
36 /* We set this to -1 to indicate an exclusive lock */
37 static volatile int binary_protocol_use_count = 0;
39 #define BINARY_PROTOCOL_BUFFER_SIZE (65536 - 2 * 8)
41 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
42 struct _BinaryProtocolBuffer {
43 BinaryProtocolBuffer * volatile next;
45 unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
48 static BinaryProtocolBuffer * volatile binary_protocol_buffers = NULL;
50 static char* filename_or_prefix = NULL;
51 static int current_file_index = 0;
52 static long long current_file_size = 0;
53 static long long file_size_limit;
56 filename_for_index (int index)
60 SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
62 filename = (char *)sgen_alloc_internal_dynamic (strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
63 sprintf (filename, "%s.%d", filename_or_prefix, index);
69 free_filename (char *filename)
71 SGEN_ASSERT (0, file_size_limit > 0, "Indexed binary protocol filename must only be used with file size limit");
73 sgen_free_internal_dynamic (filename, strlen (filename_or_prefix) + 32, INTERNAL_MEM_BINARY_PROTOCOL);
77 binary_protocol_open_file (gboolean assert_on_failure)
82 lock.l_type = F_WRLCK;
83 lock.l_whence = SEEK_SET;
88 if (file_size_limit > 0)
89 filename = filename_for_index (current_file_index);
91 filename = filename_or_prefix;
94 binary_protocol_file = open (filename, O_CREAT | O_WRONLY, 0644);
95 if (binary_protocol_file == -1) {
99 } else if (fcntl (binary_protocol_file, F_SETLK, &lock) == -1) {
100 /* The lock for the file is already taken. Fail */
101 close (binary_protocol_file);
102 binary_protocol_file = -1;
106 /* We have acquired the lock. Truncate the file */
107 ftruncate (binary_protocol_file, 0);
109 } while (binary_protocol_file == -1);
111 if (binary_protocol_file == -1 && assert_on_failure)
112 g_error ("sgen binary protocol: failed to open file");
114 if (file_size_limit > 0)
115 free_filename (filename);
120 binary_protocol_init (const char *filename, long long limit)
123 file_size_limit = limit;
125 /* Original name length + . + pid length in hex + null terminator */
126 filename_or_prefix = g_strdup_printf ("%s", filename);
127 binary_protocol_open_file (FALSE);
129 if (binary_protocol_file == -1) {
130 /* Another process owns the file, try adding the pid suffix to the filename */
131 gint32 pid = mono_process_current_pid ();
132 g_free (filename_or_prefix);
133 filename_or_prefix = g_strdup_printf ("%s.%x", filename, pid);
134 binary_protocol_open_file (TRUE);
137 /* If we have a file size limit, we might need to open additional files */
138 if (file_size_limit == 0)
139 g_free (filename_or_prefix);
141 binary_protocol_header (PROTOCOL_HEADER_CHECK, PROTOCOL_HEADER_VERSION, SIZEOF_VOID_P, G_BYTE_ORDER == G_LITTLE_ENDIAN);
143 g_error ("sgen binary protocol: not supported");
148 binary_protocol_is_enabled (void)
151 return binary_protocol_file != -1;
160 close_binary_protocol_file (void)
162 while (close (binary_protocol_file) == -1 && errno == EINTR)
164 binary_protocol_file = -1;
168 try_lock_exclusive (void)
171 if (binary_protocol_use_count)
173 } while (InterlockedCompareExchange (&binary_protocol_use_count, -1, 0) != 0);
174 mono_memory_barrier ();
179 unlock_exclusive (void)
181 mono_memory_barrier ();
182 SGEN_ASSERT (0, binary_protocol_use_count == -1, "Exclusively locked count must be -1");
183 if (InterlockedCompareExchange (&binary_protocol_use_count, 0, -1) != -1)
184 SGEN_ASSERT (0, FALSE, "Somebody messed with the exclusive lock");
188 lock_recursive (void)
193 old_count = binary_protocol_use_count;
195 /* Exclusively locked - retry */
196 /* FIXME: short back-off */
199 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
200 mono_memory_barrier ();
204 unlock_recursive (void)
207 mono_memory_barrier ();
209 old_count = binary_protocol_use_count;
210 SGEN_ASSERT (0, old_count > 0, "Locked use count must be at least 1");
211 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
215 binary_protocol_flush_buffer (BinaryProtocolBuffer *buffer)
218 size_t to_write = buffer->index;
220 g_assert (buffer->index > 0);
222 while (written < to_write) {
223 ret = write (binary_protocol_file, buffer->buffer + written, to_write - written);
226 else if (errno == EINTR)
229 close_binary_protocol_file ();
232 current_file_size += buffer->index;
234 sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
238 binary_protocol_check_file_overflow (void)
240 if (file_size_limit <= 0 || current_file_size < file_size_limit)
243 close_binary_protocol_file ();
245 if (current_file_index > 0) {
246 char *filename = filename_for_index (current_file_index - 1);
248 free_filename (filename);
251 ++current_file_index;
252 current_file_size = 0;
254 binary_protocol_open_file (TRUE);
259 * Flushing buffers takes an exclusive lock, so it must only be done when the world is
260 * stopped, otherwise we might end up with a deadlock because a stopped thread owns the
263 * The protocol entries that do flush have `FLUSH()` in their definition.
266 binary_protocol_flush_buffers (gboolean force)
269 int num_buffers = 0, i;
270 BinaryProtocolBuffer *header;
271 BinaryProtocolBuffer *buf;
272 BinaryProtocolBuffer **bufs;
274 if (binary_protocol_file == -1)
277 if (!force && !try_lock_exclusive ())
280 header = binary_protocol_buffers;
281 for (buf = header; buf != NULL; buf = buf->next)
283 bufs = (BinaryProtocolBuffer **)sgen_alloc_internal_dynamic (num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL, TRUE);
284 for (buf = header, i = 0; buf != NULL; buf = buf->next, i++)
286 SGEN_ASSERT (0, i == num_buffers, "Binary protocol buffer count error");
289 * This might be incorrect when forcing, but all bets are off in that case, anyway,
290 * because we're trying to figure out a bug in the debugger.
292 binary_protocol_buffers = NULL;
294 for (i = num_buffers - 1; i >= 0; --i) {
295 binary_protocol_flush_buffer (bufs [i]);
296 binary_protocol_check_file_overflow ();
299 sgen_free_internal_dynamic (buf, num_buffers * sizeof (BinaryProtocolBuffer*), INTERNAL_MEM_BINARY_PROTOCOL);
309 static BinaryProtocolBuffer*
310 binary_protocol_get_buffer (int length)
312 BinaryProtocolBuffer *buffer, *new_buffer;
314 buffer = binary_protocol_buffers;
315 if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
318 new_buffer = (BinaryProtocolBuffer *)sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), (SgenAllocFlags)(SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE), "debugging memory");
319 new_buffer->next = buffer;
320 new_buffer->index = 0;
322 if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
323 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
332 protocol_entry (unsigned char type, gpointer data, int size)
336 BinaryProtocolBuffer *buffer;
338 if (binary_protocol_file == -1)
341 if (sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()))
347 buffer = binary_protocol_get_buffer (size + 1);
349 index = buffer->index;
350 if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
353 if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
354 goto retry_same_buffer;
356 /* FIXME: if we're interrupted at this point, we have a buffer
357 entry that contains random data. */
359 buffer->buffer [index++] = type;
360 memcpy (buffer->buffer + index, data, size);
363 g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
370 #define TYPE_LONGLONG long long
371 #define TYPE_SIZE size_t
372 #define TYPE_POINTER gpointer
373 #define TYPE_BOOL gboolean
375 #define BEGIN_PROTOCOL_ENTRY0(method) \
376 void method (void) { \
377 int __type = PROTOCOL_ID(method); \
378 gpointer __data = NULL; \
380 CLIENT_PROTOCOL_NAME (method) ();
381 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
382 void method (t1 f1) { \
383 PROTOCOL_STRUCT(method) __entry = { f1 }; \
384 int __type = PROTOCOL_ID(method); \
385 gpointer __data = &__entry; \
386 int __size = sizeof (PROTOCOL_STRUCT(method)); \
387 CLIENT_PROTOCOL_NAME (method) (f1);
388 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
389 void method (t1 f1, t2 f2) { \
390 PROTOCOL_STRUCT(method) __entry = { f1, f2 }; \
391 int __type = PROTOCOL_ID(method); \
392 gpointer __data = &__entry; \
393 int __size = sizeof (PROTOCOL_STRUCT(method)); \
394 CLIENT_PROTOCOL_NAME (method) (f1, f2);
395 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
396 void method (t1 f1, t2 f2, t3 f3) { \
397 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3 }; \
398 int __type = PROTOCOL_ID(method); \
399 gpointer __data = &__entry; \
400 int __size = sizeof (PROTOCOL_STRUCT(method)); \
401 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3);
402 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
403 void method (t1 f1, t2 f2, t3 f3, t4 f4) { \
404 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4 }; \
405 int __type = PROTOCOL_ID(method); \
406 gpointer __data = &__entry; \
407 int __size = sizeof (PROTOCOL_STRUCT(method)); \
408 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4);
409 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
410 void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5) { \
411 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5 }; \
412 int __type = PROTOCOL_ID(method); \
413 gpointer __data = &__entry; \
414 int __size = sizeof (PROTOCOL_STRUCT(method)); \
415 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5);
416 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
417 void method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5, t6 f6) { \
418 PROTOCOL_STRUCT(method) __entry = { f1, f2, f3, f4, f5, f6 }; \
419 int __type = PROTOCOL_ID(method); \
420 gpointer __data = &__entry; \
421 int __size = sizeof (PROTOCOL_STRUCT(method)); \
422 CLIENT_PROTOCOL_NAME (method) (f1, f2, f3, f4, f5, f6);
424 #define DEFAULT_PRINT()
425 #define CUSTOM_PRINT(_)
427 #define IS_ALWAYS_MATCH(_)
428 #define MATCH_INDEX(_)
429 #define IS_VTABLE_MATCH(_)
431 #define END_PROTOCOL_ENTRY \
432 protocol_entry (__type, __data, __size); \
435 #define END_PROTOCOL_ENTRY_FLUSH \
436 protocol_entry (__type, __data, __size); \
437 binary_protocol_flush_buffers (FALSE); \
440 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
441 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
442 BEGIN_PROTOCOL_ENTRY0 (method)
443 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
444 BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
445 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
446 BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
447 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
448 BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
449 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
450 BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
451 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
452 BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
453 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
454 BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
456 #define END_PROTOCOL_ENTRY_HEAVY \
459 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method)
460 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1)
461 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2)
462 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3)
463 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4)
464 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
465 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
467 #define END_PROTOCOL_ENTRY_HEAVY
470 #include "sgen-protocol-def.h"
478 #endif /* HAVE_SGEN_GC */