MonoSeqPointInfo no longer uses a GByteArray. GByteArray requires too much memory...
[mono.git] / mono / mini / seq-points.c
1 /*
2  * seq-points.c: Sequence Points functions
3  *
4  * Authors:
5  *   Marcos Henrich (marcos.henrich@xamarin.com)
6  *
7  * Copyright 2014 Xamarin, Inc (http://www.xamarin.com)
8  */
9
10 #include "mini.h"
11 #include "seq-points.h"
12
13 static guint8
14 encode_var_int (guint8* buf, int val)
15 {
16         guint8 size = 0;
17
18         do {
19                 guint8 byte = val & 0x7f;
20                 g_assert (size < 4 && "value has more than 28 bits");
21                 val >>= 7;
22                 if(val) byte |= 0x80;
23                 *(buf++) = byte;
24                 size++;
25         } while (val);
26
27         return size;
28 }
29
30 static guint8
31 decode_var_int (guint8* buf, int* val)
32 {
33         guint8* p = buf;
34
35         int low;
36         int b;
37         b = *(p++); low   = (b & 0x7f)      ; if(!(b & 0x80)) goto done;
38         b = *(p++); low  |= (b & 0x7f) <<  7; if(!(b & 0x80)) goto done;
39         b = *(p++); low  |= (b & 0x7f) << 14; if(!(b & 0x80)) goto done;
40         b = *(p++); low  |= (b & 0x7f) << 21; if(!(b & 0x80)) goto done;
41
42         g_assert (FALSE && "value has more than 28 bits");
43
44 done:
45
46         *val = low;
47         return p-buf;
48 }
49
50 static guint32
51 encode_zig_zag (int val)
52 {
53         return (val << 1) ^ (val >> 31);
54 }
55
56 static int
57 decode_zig_zag (guint32 val)
58 {
59         int n = val;
60         return (n >> 1) ^ (-(n & 1));
61 }
62
63 static void
64 allocated_seq_point_size_add (MonoSeqPointInfo *info)
65 {
66         int size = sizeof (MonoSeqPointInfo) + info->len;
67
68         mono_jit_stats.allocated_seq_points_size += size;
69 }
70
71 static int
72 seq_point_read (SeqPoint* seq_point, guint8* ptr, guint8* buffer_ptr, gboolean has_debug_data)
73 {
74         int value;
75         guint8* ptr0 = ptr;
76
77         ptr += decode_var_int (ptr, &value);
78         seq_point->il_offset += decode_zig_zag (value);
79
80         ptr += decode_var_int (ptr, &value);
81         seq_point->native_offset += decode_zig_zag (value);
82
83         if (has_debug_data) {
84                 ptr += decode_var_int (ptr, &value);
85                 seq_point->flags = value;
86
87                 ptr += decode_var_int (ptr, &value);
88                 seq_point->next_len = value;
89
90                 if (seq_point->next_len) {
91                         // store next offset and skip it
92                         seq_point->next_offset = ptr - buffer_ptr;
93                         ptr += seq_point->next_len;
94                 }
95         }
96
97         return ptr - ptr0;
98 }
99
100 static MonoSeqPointInfo*
101 seq_point_info_new (int len, gboolean alloc_data, gboolean has_debug_data)
102 {
103         MonoSeqPointInfo* info = g_new0 (MonoSeqPointInfo, 1);
104         info->has_debug_data = has_debug_data;
105         info->alloc_data = alloc_data;
106         info->len = len;
107
108         if (info->alloc_data)
109                 info->data = g_new0 (guint8, len);
110
111         return info;
112 }
113
114 void
115 seq_point_info_free (gpointer ptr)
116 {
117         MonoSeqPointInfo* info = (MonoSeqPointInfo*) ptr;
118         if (info->alloc_data)
119                 g_free (info->data);
120         g_free (info);
121 }
122
123 static gboolean
124 seq_point_info_add_seq_point (GByteArray* array, SeqPoint *sp, SeqPoint *last_seq_point, GSList *next, gboolean has_debug_data)
125 {
126         int il_delta, native_delta;
127         GSList *l;
128         guint8 buffer[4];
129         guint8 len;
130
131         if (!has_debug_data &&
132                 (sp->il_offset == METHOD_ENTRY_IL_OFFSET || sp->il_offset == METHOD_EXIT_IL_OFFSET))
133                 return FALSE;
134
135         il_delta = sp->il_offset - last_seq_point->il_offset;
136         native_delta = sp->native_offset - last_seq_point->native_offset;
137
138         len = encode_var_int (buffer, encode_zig_zag (il_delta));
139         g_byte_array_append (array, buffer, len);
140
141         len = encode_var_int (buffer, encode_zig_zag (native_delta));
142         g_byte_array_append (array, buffer, len);
143
144         if (has_debug_data) {
145                 sp->next_offset = array->len;
146                 sp->next_len = g_slist_length (next);
147
148                 len = encode_var_int (buffer, sp->flags);
149                 g_byte_array_append (array, buffer, len);
150
151                 len = encode_var_int (buffer, sp->next_len);
152                 g_byte_array_append (array, buffer, len);
153
154                 for (l = next; l; l = l->next) {
155                         int next_index = GPOINTER_TO_UINT (l->data);
156                         guint8 buffer[4];
157                         int len = encode_var_int (buffer, next_index);
158                         g_byte_array_append (array, buffer, len);
159                 }
160         }
161
162         return TRUE;
163 }
164
165 static void
166 collect_pred_seq_points (MonoBasicBlock *bb, MonoInst *ins, GSList **next, int depth)
167 {
168         int i;
169         MonoBasicBlock *in_bb;
170         GSList *l;
171
172         for (i = 0; i < bb->in_count; ++i) {
173                 in_bb = bb->in_bb [i];
174
175                 if (in_bb->last_seq_point) {
176                         int src_index = in_bb->last_seq_point->backend.size;
177                         int dst_index = ins->backend.size;
178
179                         /* bb->in_bb might contain duplicates */
180                         for (l = next [src_index]; l; l = l->next)
181                                 if (GPOINTER_TO_UINT (l->data) == dst_index)
182                                         break;
183                         if (!l)
184                                 next [src_index] = g_slist_append (next [src_index], GUINT_TO_POINTER (dst_index));
185                 } else {
186                         /* Have to look at its predecessors */
187                         if (depth < 5)
188                                 collect_pred_seq_points (in_bb, ins, next, depth + 1);
189                 }
190         }
191 }
192
193 void
194 mono_save_seq_point_info (MonoCompile *cfg)
195 {
196         MonoBasicBlock *bb;
197         GSList *bb_seq_points, *l;
198         MonoInst *last;
199         MonoDomain *domain = cfg->domain;
200         int i;
201         GSList **next;
202         SeqPoint* seq_points;
203         GByteArray* array;
204         gboolean has_debug_data = cfg->gen_seq_points_debug_data;
205
206         if (!cfg->seq_points)
207                 return;
208
209         seq_points = g_new0 (SeqPoint, cfg->seq_points->len);
210
211         for (i = 0; i < cfg->seq_points->len; ++i) {
212                 SeqPoint *sp = &seq_points [i];
213                 MonoInst *ins = g_ptr_array_index (cfg->seq_points, i);
214
215                 sp->il_offset = ins->inst_imm;
216                 sp->native_offset = ins->inst_offset;
217                 if (ins->flags & MONO_INST_NONEMPTY_STACK)
218                         sp->flags |= MONO_SEQ_POINT_FLAG_NONEMPTY_STACK;
219
220                 /* Used below */
221                 ins->backend.size = i;
222         }
223
224         if (has_debug_data) {
225                 /*
226                  * For each sequence point, compute the list of sequence points immediately
227                  * following it, this is needed to implement 'step over' in the debugger agent.
228                  */
229                 next = g_new0 (GSList*, cfg->seq_points->len);
230                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
231                         bb_seq_points = g_slist_reverse (bb->seq_points);
232                         last = NULL;
233                         for (l = bb_seq_points; l; l = l->next) {
234                                 MonoInst *ins = l->data;
235
236                                 if (ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET)
237                                 /* Used to implement method entry/exit events */
238                                         continue;
239                                 if (ins->inst_offset == SEQ_POINT_NATIVE_OFFSET_DEAD_CODE)
240                                         continue;
241
242                                 if (last != NULL) {
243                                         /* Link with the previous seq point in the same bb */
244                                         next [last->backend.size] = g_slist_append (next [last->backend.size], GUINT_TO_POINTER (ins->backend.size));
245                                 } else {
246                                         /* Link with the last bb in the previous bblocks */
247                                         collect_pred_seq_points (bb, ins, next, 0);
248                                 }
249
250                                 last = ins;
251                         }
252
253                         if (bb->last_ins && bb->last_ins->opcode == OP_ENDFINALLY && bb->seq_points) {
254                                 MonoBasicBlock *bb2;
255                                 MonoInst *endfinally_seq_point = NULL;
256
257                                 /*
258                                  * The ENDFINALLY branches are not represented in the cfg, so link it with all seq points starting bbs.
259                                  */
260                                 l = g_slist_last (bb->seq_points);
261                                 if (l) {
262                                         endfinally_seq_point = l->data;
263
264                                         for (bb2 = cfg->bb_entry; bb2; bb2 = bb2->next_bb) {
265                                                 GSList *l = g_slist_last (bb2->seq_points);
266
267                                                 if (l) {
268                                                         MonoInst *ins = l->data;
269
270                                                         if (!(ins->inst_imm == METHOD_ENTRY_IL_OFFSET || ins->inst_imm == METHOD_EXIT_IL_OFFSET) && ins != endfinally_seq_point)
271                                                                 next [endfinally_seq_point->backend.size] = g_slist_append (next [endfinally_seq_point->backend.size], GUINT_TO_POINTER (ins->backend.size));
272                                                 }
273                                         }
274                                 }
275                         }
276                 }
277
278                 if (cfg->verbose_level > 2) {
279                         printf ("\nSEQ POINT MAP: \n");
280
281                         for (i = 0; i < cfg->seq_points->len; ++i) {
282                                 SeqPoint *sp = &seq_points [i];
283                                 GSList *l;
284
285                                 if (!next [i])
286                                         continue;
287
288                                 printf ("\tIL0x%x ->", sp->il_offset);
289                                 for (l = next [i]; l; l = l->next) {
290                                         int next_index = GPOINTER_TO_UINT (l->data);
291                                         printf (" IL0x%x", seq_points [next_index].il_offset);
292                                 }
293                                 printf ("\n");
294                         }
295                 }
296         }
297
298         array = g_byte_array_new ();
299
300         { /* Add sequence points to seq_point_info */
301                 SeqPoint zero_seq_point = {0};
302                 SeqPoint* last_seq_point = &zero_seq_point;
303
304                 for (i = 0; i < cfg->seq_points->len; ++i) {
305                         SeqPoint *sp = &seq_points [i];
306                         GSList* next_list = NULL;
307
308                         if (has_debug_data)
309                                 next_list = next[i];
310
311                         if (seq_point_info_add_seq_point (array, sp, last_seq_point, next_list, has_debug_data))
312                                 last_seq_point = sp;
313
314                         if (has_debug_data)
315                                 g_slist_free (next [i]);
316                 }
317         }
318
319         if (has_debug_data)
320                 g_free (next);
321
322         cfg->seq_point_info = seq_point_info_new (array->len, TRUE, has_debug_data);
323         memcpy (cfg->seq_point_info->data, array->data, array->len);
324         g_byte_array_free (array, TRUE);
325
326         allocated_seq_point_size_add (cfg->seq_point_info);
327
328         // FIXME: dynamic methods
329         if (!cfg->compile_aot) {
330                 mono_domain_lock (domain);
331                 // FIXME: How can the lookup succeed ?
332                 if (!g_hash_table_lookup (domain_jit_info (domain)->seq_points, cfg->method_to_register))
333                         g_hash_table_insert (domain_jit_info (domain)->seq_points, cfg->method_to_register, cfg->seq_point_info);
334                 mono_domain_unlock (domain);
335         }
336
337         g_ptr_array_free (cfg->seq_points, TRUE);
338         cfg->seq_points = NULL;
339 }
340
341 MonoSeqPointInfo*
342 get_seq_points (MonoDomain *domain, MonoMethod *method)
343 {
344         MonoSeqPointInfo *seq_points;
345
346         mono_domain_lock (domain);
347         seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, method);
348         if (!seq_points && method->is_inflated) {
349                 /* generic sharing + aot */
350                 seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mono_method_get_declaring_generic_method (method));
351                 if (!seq_points)
352                         seq_points = g_hash_table_lookup (domain_jit_info (domain)->seq_points, mini_get_shared_method (method));
353         }
354         mono_domain_unlock (domain);
355
356         return seq_points;
357 }
358
359 static gboolean
360 seq_point_find_next_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
361 {
362         SeqPointIterator it;
363         seq_point_iterator_init (&it, info);
364         while (seq_point_iterator_next (&it)) {
365                 if (it.seq_point.native_offset >= native_offset) {
366                         memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
367                         return TRUE;
368                 }
369         }
370
371         return FALSE;
372 }
373
374 static gboolean
375 seq_point_find_prev_by_native_offset (MonoSeqPointInfo* info, int native_offset, SeqPoint* seq_point)
376 {
377         SeqPoint prev_seq_point;
378         gboolean  is_first = TRUE;
379         SeqPointIterator it;
380         seq_point_iterator_init (&it, info);
381         while (seq_point_iterator_next (&it) && it.seq_point.native_offset <= native_offset) {
382                 memcpy (&prev_seq_point, &it.seq_point, sizeof (SeqPoint));
383                 is_first = FALSE;
384         }
385
386         if (!is_first && prev_seq_point.native_offset <= native_offset) {
387                 memcpy (seq_point, &prev_seq_point, sizeof (SeqPoint));
388                 return TRUE;
389         }
390
391         return FALSE;
392 }
393
394 static gboolean
395 seq_point_find_by_il_offset (MonoSeqPointInfo* info, int il_offset, SeqPoint* seq_point)
396 {
397         SeqPointIterator it;
398         seq_point_iterator_init (&it, info);
399         while (seq_point_iterator_next (&it)) {
400                 if (it.seq_point.il_offset == il_offset) {
401                         memcpy (seq_point, &it.seq_point, sizeof (SeqPoint));
402                         return TRUE;
403                 }
404         }
405
406         return FALSE;
407 }
408
409 /*
410  * find_next_seq_point_for_native_offset:
411  *
412  *   Find the first sequence point after NATIVE_OFFSET.
413  */
414 gboolean
415 find_next_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
416 {
417         MonoSeqPointInfo *seq_points;
418
419         seq_points = get_seq_points (domain, method);
420         if (!seq_points) {
421                 if (info)
422                         *info = NULL;
423                 return FALSE;
424         }
425         if (info)
426                 *info = seq_points;
427
428         return seq_point_find_next_by_native_offset (seq_points, native_offset, seq_point);
429 }
430
431 /*
432  * find_prev_seq_point_for_native_offset:
433  *
434  *   Find the first sequence point before NATIVE_OFFSET.
435  */
436 gboolean
437 find_prev_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset, MonoSeqPointInfo **info, SeqPoint* seq_point)
438 {
439         MonoSeqPointInfo *seq_points;
440
441         seq_points = get_seq_points (domain, method);
442         if (!seq_points) {
443                 if (info)
444                         *info = NULL;
445                 return FALSE;
446         }
447         if (info)
448                 *info = seq_points;
449
450         return seq_point_find_prev_by_native_offset (seq_points, native_offset, seq_point);
451 }
452
453 /*
454  * find_seq_point:
455  *
456  *   Find the sequence point corresponding to the IL offset IL_OFFSET, which
457  * should be the location of a sequence point.
458  */
459 gboolean
460 find_seq_point (MonoDomain *domain, MonoMethod *method, gint32 il_offset, MonoSeqPointInfo **info, SeqPoint *seq_point)
461 {
462         MonoSeqPointInfo *seq_points;
463
464         seq_points = get_seq_points (domain, method);
465         if (!seq_points) {
466                 if (info)
467                         *info = NULL;
468                 return FALSE;
469         }
470         if (info)
471                 *info = seq_points;
472
473         return seq_point_find_by_il_offset (seq_points, il_offset, seq_point);
474 }
475
476 void
477 seq_point_init_next (MonoSeqPointInfo* info, SeqPoint sp, SeqPoint* next)
478 {
479         int i;
480         guint8* ptr;
481         SeqPointIterator it;
482         GArray* seq_points = g_array_new (FALSE, TRUE, sizeof (SeqPoint));
483
484         g_assert (info->has_debug_data);
485
486         seq_point_iterator_init (&it, info);
487         while (seq_point_iterator_next (&it))
488                 g_array_append_vals (seq_points, &it.seq_point, 1);
489
490         ptr = info->data + sp.next_offset;
491         for (i = 0; i < sp.next_len; i++) {
492                 int next_index;
493                 ptr += decode_var_int (ptr, &next_index);
494                 g_assert (next_index < seq_points->len);
495                 memcpy (&next[i], seq_points->data + next_index * sizeof (SeqPoint), sizeof (SeqPoint));
496         }
497
498         g_array_free (seq_points, TRUE);
499 }
500
501 gboolean
502 seq_point_iterator_next (SeqPointIterator* it)
503 {
504         if (it->ptr >= it->info->data + it->info->len)
505                 return FALSE;
506
507         it->ptr += seq_point_read (&it->seq_point, it->ptr, it->info->data, it->info->has_debug_data);
508
509         return TRUE;
510 }
511
512 void
513 seq_point_iterator_init (SeqPointIterator* it, MonoSeqPointInfo* info)
514 {
515         it->info = info;
516         it->ptr = it->info->data;
517         memset(&it->seq_point, 0, sizeof(SeqPoint));
518 }
519
520 int
521 seq_point_info_write (MonoSeqPointInfo* info, guint8* buffer)
522 {
523         guint8* buffer0 = buffer;
524
525         memcpy (buffer, &info->has_debug_data, 1);
526         buffer++;
527
528         //Write sequence points
529         buffer += encode_var_int (buffer, info->len);
530         memcpy (buffer, info->data, info->len);
531         buffer += info->len;
532
533         return buffer - buffer0;
534 }
535
536 int
537 seq_point_info_read (MonoSeqPointInfo** info, guint8* buffer, gboolean copy)
538 {
539         guint8* buffer0 = buffer;
540         int size;
541         gboolean has_debug_data;
542
543         memcpy (&has_debug_data, buffer, 1);
544         buffer++;
545
546         buffer += decode_var_int (buffer, &size);
547         (*info) = seq_point_info_new (size, copy, has_debug_data);
548         if (copy)
549                 memcpy ((*info)->data, buffer, size);
550         else
551                 (*info)->data = buffer;
552         buffer += size;
553
554         return buffer - buffer0;
555 }
556
557 /*
558  * Returns the maximum size of mono_seq_point_info_write.
559  */
560 int
561 seq_point_info_write_size (MonoSeqPointInfo* info)
562 {
563         //4 is the maximum size required to store the size of the data.
564         //1 is the byte used to store has_debug_data.
565         int size = 4 + 1 + info->len;
566
567         return size;
568 }