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 * 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;
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.
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.
27 #include "sgen-protocol.h"
28 #include "sgen-memory-governor.h"
29 #include "utils/mono-mmap.h"
31 #ifdef SGEN_BINARY_PROTOCOL
33 /* If not null, dump binary protocol to this file */
34 static FILE *binary_protocol_file = NULL;
36 static int binary_protocol_use_count = 0;
38 #define BINARY_PROTOCOL_BUFFER_SIZE (65536 - 2 * 8)
40 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
41 struct _BinaryProtocolBuffer {
42 BinaryProtocolBuffer *next;
44 unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
47 static BinaryProtocolBuffer *binary_protocol_buffers = NULL;
50 binary_protocol_init (const char *filename)
52 binary_protocol_file = fopen (filename, "w");
56 binary_protocol_is_enabled (void)
58 return binary_protocol_file != NULL;
62 binary_protocol_flush_buffers_rec (BinaryProtocolBuffer *buffer)
67 binary_protocol_flush_buffers_rec (buffer->next);
69 g_assert (buffer->index > 0);
70 fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
72 sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
76 binary_protocol_flush_buffers (gboolean force)
78 if (!binary_protocol_file)
81 if (!force && binary_protocol_use_count != 0)
84 binary_protocol_flush_buffers_rec (binary_protocol_buffers);
85 binary_protocol_buffers = NULL;
87 fflush (binary_protocol_file);
90 static BinaryProtocolBuffer*
91 binary_protocol_get_buffer (int length)
93 BinaryProtocolBuffer *buffer, *new_buffer;
96 buffer = binary_protocol_buffers;
97 if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
100 new_buffer = sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "debugging memory");
101 new_buffer->next = buffer;
102 new_buffer->index = 0;
104 if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
105 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
114 protocol_entry (unsigned char type, gpointer data, int size)
117 BinaryProtocolBuffer *buffer;
120 if (!binary_protocol_file)
124 old_count = binary_protocol_use_count;
125 g_assert (old_count >= 0);
126 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
129 buffer = binary_protocol_get_buffer (size + 1);
131 index = buffer->index;
132 if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
135 if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
136 goto retry_same_buffer;
138 /* FIXME: if we're interrupted at this point, we have a buffer
139 entry that contains random data. */
141 buffer->buffer [index++] = type;
142 memcpy (buffer->buffer + index, data, size);
145 g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
148 old_count = binary_protocol_use_count;
149 g_assert (old_count > 0);
150 } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
154 binary_protocol_collection_begin (int index, int generation)
156 SGenProtocolCollection entry = { index, generation };
157 binary_protocol_flush_buffers (FALSE);
158 protocol_entry (SGEN_PROTOCOL_COLLECTION_BEGIN, &entry, sizeof (SGenProtocolCollection));
162 binary_protocol_collection_end (int index, int generation)
164 SGenProtocolCollection entry = { index, generation };
165 binary_protocol_flush_buffers (FALSE);
166 protocol_entry (SGEN_PROTOCOL_COLLECTION_END, &entry, sizeof (SGenProtocolCollection));
170 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
172 SGenProtocolAlloc entry = { obj, vtable, size };
173 protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
177 binary_protocol_alloc_pinned (gpointer obj, gpointer vtable, int size)
179 SGenProtocolAlloc entry = { obj, vtable, size };
180 protocol_entry (SGEN_PROTOCOL_ALLOC_PINNED, &entry, sizeof (SGenProtocolAlloc));
184 binary_protocol_alloc_degraded (gpointer obj, gpointer vtable, int size)
186 SGenProtocolAlloc entry = { obj, vtable, size };
187 protocol_entry (SGEN_PROTOCOL_ALLOC_DEGRADED, &entry, sizeof (SGenProtocolAlloc));
191 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
193 SGenProtocolCopy entry = { from, to, vtable, size };
194 protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
198 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
200 SGenProtocolPin entry = { obj, vtable, size };
201 protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
205 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
207 SGenProtocolMark entry = { obj, vtable, size };
208 protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
212 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
214 SGenProtocolWBarrier entry = { ptr, value, value_vtable };
215 protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
219 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
221 SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
222 protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
226 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
228 SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
229 protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
233 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
235 SGenProtocolCleanup entry = { ptr, vtable, size };
236 protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
240 binary_protocol_empty (gpointer start, int size)
242 SGenProtocolEmpty entry = { start, size };
243 protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
247 binary_protocol_thread_suspend (gpointer thread, gpointer stopped_ip)
249 SGenProtocolThreadSuspend entry = { thread, stopped_ip };
250 protocol_entry (SGEN_PROTOCOL_THREAD_SUSPEND, &entry, sizeof (SGenProtocolThreadSuspend));
254 binary_protocol_thread_restart (gpointer thread)
256 SGenProtocolThreadRestart entry = { thread };
257 protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
261 binary_protocol_thread_register (gpointer thread)
263 SGenProtocolThreadRegister entry = { thread };
264 protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
269 binary_protocol_thread_unregister (gpointer thread)
271 SGenProtocolThreadUnregister entry = { thread };
272 protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
277 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
279 SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
280 protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
286 #endif /* HAVE_SGEN_GC */