Fix a case missed by 7e222739db7192eb0c5ec17cad18e309482e71b4.
[mono.git] / mono / metadata / sgen-protocol.c
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifdef HAVE_SGEN_GC
26
27 #include "config.h"
28 #include "sgen-gc.h"
29 #include "sgen-protocol.h"
30 #include "sgen-memory-governor.h"
31 #include "utils/mono-mmap.h"
32
33 #ifdef SGEN_BINARY_PROTOCOL
34
35 /* If not null, dump binary protocol to this file */
36 static FILE *binary_protocol_file = NULL;
37
38 static int binary_protocol_use_count = 0;
39
40 #define BINARY_PROTOCOL_BUFFER_SIZE     (65536 - 2 * 8)
41
42 typedef struct _BinaryProtocolBuffer BinaryProtocolBuffer;
43 struct _BinaryProtocolBuffer {
44         BinaryProtocolBuffer *next;
45         int index;
46         unsigned char buffer [BINARY_PROTOCOL_BUFFER_SIZE];
47 };
48
49 static BinaryProtocolBuffer *binary_protocol_buffers = NULL;
50
51 void
52 binary_protocol_init (const char *filename)
53 {
54         binary_protocol_file = fopen (filename, "w");
55 }
56
57 gboolean
58 binary_protocol_is_enabled (void)
59 {
60         return binary_protocol_file != NULL;
61 }
62
63 static void
64 binary_protocol_flush_buffers_rec (BinaryProtocolBuffer *buffer)
65 {
66         if (!buffer)
67                 return;
68
69         binary_protocol_flush_buffers_rec (buffer->next);
70
71         g_assert (buffer->index > 0);
72         fwrite (buffer->buffer, 1, buffer->index, binary_protocol_file);
73
74         sgen_free_os_memory (buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
75 }
76
77 void
78 binary_protocol_flush_buffers (gboolean force)
79 {
80         if (!binary_protocol_file)
81                 return;
82
83         if (!force && binary_protocol_use_count != 0)
84                 return;
85
86         binary_protocol_flush_buffers_rec (binary_protocol_buffers);
87         binary_protocol_buffers = NULL;
88
89         fflush (binary_protocol_file);
90 }
91
92 static BinaryProtocolBuffer*
93 binary_protocol_get_buffer (int length)
94 {
95         BinaryProtocolBuffer *buffer, *new_buffer;
96
97  retry:
98         buffer = binary_protocol_buffers;
99         if (buffer && buffer->index + length <= BINARY_PROTOCOL_BUFFER_SIZE)
100                 return buffer;
101
102         new_buffer = sgen_alloc_os_memory (sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, "debugging memory");
103         new_buffer->next = buffer;
104         new_buffer->index = 0;
105
106         if (InterlockedCompareExchangePointer ((void**)&binary_protocol_buffers, new_buffer, buffer) != buffer) {
107                 sgen_free_os_memory (new_buffer, sizeof (BinaryProtocolBuffer), SGEN_ALLOC_INTERNAL);
108                 goto retry;
109         }
110
111         return new_buffer;
112 }
113
114
115 static void
116 protocol_entry (unsigned char type, gpointer data, int size)
117 {
118         int index;
119         BinaryProtocolBuffer *buffer;
120         int old_count;
121
122         if (!binary_protocol_file)
123                 return;
124
125         do {
126                 old_count = binary_protocol_use_count;
127                 g_assert (old_count >= 0);
128         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count + 1, old_count) != old_count);
129
130  retry:
131         buffer = binary_protocol_get_buffer (size + 1);
132  retry_same_buffer:
133         index = buffer->index;
134         if (index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
135                 goto retry;
136
137         if (InterlockedCompareExchange (&buffer->index, index + 1 + size, index) != index)
138                 goto retry_same_buffer;
139
140         /* FIXME: if we're interrupted at this point, we have a buffer
141            entry that contains random data. */
142
143         buffer->buffer [index++] = type;
144         memcpy (buffer->buffer + index, data, size);
145         index += size;
146
147         g_assert (index <= BINARY_PROTOCOL_BUFFER_SIZE);
148
149         do {
150                 old_count = binary_protocol_use_count;
151                 g_assert (old_count > 0);
152         } while (InterlockedCompareExchange (&binary_protocol_use_count, old_count - 1, old_count) != old_count);
153 }
154
155 void
156 binary_protocol_collection (int index, int generation)
157 {
158         SGenProtocolCollection entry = { index, generation };
159         binary_protocol_flush_buffers (FALSE);
160         protocol_entry (SGEN_PROTOCOL_COLLECTION, &entry, sizeof (SGenProtocolCollection));
161 }
162
163 void
164 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
165 {
166         SGenProtocolAlloc entry = { obj, vtable, size };
167         protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
168 }
169
170 void
171 binary_protocol_alloc_pinned (gpointer obj, gpointer vtable, int size)
172 {
173         SGenProtocolAlloc entry = { obj, vtable, size };
174         protocol_entry (SGEN_PROTOCOL_ALLOC_PINNED, &entry, sizeof (SGenProtocolAlloc));
175 }
176
177 void
178 binary_protocol_alloc_degraded (gpointer obj, gpointer vtable, int size)
179 {
180         SGenProtocolAlloc entry = { obj, vtable, size };
181         protocol_entry (SGEN_PROTOCOL_ALLOC_DEGRADED, &entry, sizeof (SGenProtocolAlloc));
182 }
183
184 void
185 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
186 {
187         SGenProtocolCopy entry = { from, to, vtable, size };
188         protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
189 }
190
191 void
192 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
193 {
194         SGenProtocolPin entry = { obj, vtable, size };
195         protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
196 }
197
198 void
199 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
200 {
201         SGenProtocolMark entry = { obj, vtable, size };
202         protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
203 }
204
205 void
206 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
207 {
208         SGenProtocolWBarrier entry = { ptr, value, value_vtable };
209         protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
210 }
211
212 void
213 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
214 {
215         SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
216         protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
217 }
218
219 void
220 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
221 {
222         SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
223         protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
224 }
225
226 void
227 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
228 {
229         SGenProtocolCleanup entry = { ptr, vtable, size };
230         protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
231 }
232
233 void
234 binary_protocol_empty (gpointer start, int size)
235 {
236         SGenProtocolEmpty entry = { start, size };
237         protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
238 }
239
240 void
241 binary_protocol_thread_suspend (gpointer thread, gpointer stopped_ip)
242 {
243         SGenProtocolThreadSuspend entry = { thread, stopped_ip };
244         protocol_entry (SGEN_PROTOCOL_THREAD_SUSPEND, &entry, sizeof (SGenProtocolThreadSuspend));
245 }
246
247 void
248 binary_protocol_thread_restart (gpointer thread)
249 {
250         SGenProtocolThreadRestart entry = { thread };
251         protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
252 }
253
254 void
255 binary_protocol_thread_register (gpointer thread)
256 {
257         SGenProtocolThreadRegister entry = { thread };
258         protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
259
260 }
261
262 void
263 binary_protocol_thread_unregister (gpointer thread)
264 {
265         SGenProtocolThreadUnregister entry = { thread };
266         protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
267
268 }
269
270 void
271 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
272 {
273         SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
274         protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
275
276 }
277
278 #endif
279
280 #endif /* HAVE_SGEN_GC */