2003-02-21 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / loader.c
1 /*
2  * loader.c: Image Loader 
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Miguel de Icaza (miguel@ximian.com)
7  *   Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  *
11  * This file is used by the interpreter and the JIT engine to locate
12  * assemblies.  Used to load AssemblyRef and later to resolve various
13  * kinds of `Refs'.
14  *
15  * TODO:
16  *   This should keep track of the assembly versions that we are loading.
17  *
18  */
19 #include <config.h>
20 #include <glib.h>
21 #include <gmodule.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/image.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/cil-coff.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35
36 static gboolean dummy_icall = TRUE;
37
38 MonoDefaults mono_defaults;
39
40 static GHashTable *icall_hash = NULL;
41
42 void
43 mono_add_internal_call (const char *name, gconstpointer method)
44 {
45         if (!icall_hash) {
46                 dummy_icall = FALSE;
47                 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
48         }
49
50         g_hash_table_insert (icall_hash, g_strdup (name), (gpointer) method);
51 }
52
53 static void
54 ves_icall_dummy (void)
55 {
56         g_warning ("the mono runtime is not initialized");
57         g_assert_not_reached ();
58 }
59
60 gpointer
61 mono_lookup_internal_call (MonoMethod *method)
62 {
63         char *name;
64         char *tmpsig;
65         gpointer res;
66
67         if (dummy_icall)
68                 return ves_icall_dummy;
69
70         if (!method) {
71                 g_warning ("can't resolve internal call, method is null");
72         }
73
74         if (!icall_hash) {
75                 g_warning ("icall_hash not initialized");
76                 g_assert_not_reached ();
77         }
78
79         if (*method->klass->name_space)
80                 name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, NULL);
81         else
82                 name = g_strconcat (method->klass->name, "::", method->name, NULL);
83         if (!(res = g_hash_table_lookup (icall_hash, name))) {
84                 /* trying to resolve with full signature */
85                 g_free (name);
86         
87                 tmpsig = mono_signature_get_desc(method->signature, TRUE);
88                 if (*method->klass->name_space)
89                         name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
90                 else
91                         name = g_strconcat (method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
92                 if (!(res = g_hash_table_lookup (icall_hash, name))) {
93                         g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", name);
94                         g_print ("\nYour mono runtime and corlib are out of sync.\n");
95                         g_print ("When you update one from cvs you need to update, compile and install\nthe other too.\n");
96                         g_print ("Do not report this as a bug unless you're sure you have updated correctly:\nyou probably have a broken mono install.\n");
97                         g_print ("If you see other errors or faults after this message they are probably related\n");
98                         g_print ("and you need to fix your mono install first.\n");
99
100                         g_free (name);
101                         g_free (tmpsig);
102
103                         return NULL;
104                 }
105
106                 g_free(tmpsig);
107         }
108
109         g_free (name);
110
111         return res;
112 }
113
114 MonoClassField*
115 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
116 {
117         MonoClass *klass;
118         MonoTableInfo *tables = image->tables;
119         guint32 cols[6];
120         guint32 nindex, class;
121         const char *fname;
122         const char *ptr;
123         guint32 idx = mono_metadata_token_index (token);
124
125         if (image->assembly->dynamic) {
126                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
127                 *retklass = result->parent;
128                 return result;
129         }
130
131         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
132         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
133         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
134
135         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
136         
137         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
138         mono_metadata_decode_blob_size (ptr, &ptr);
139         /* we may want to check the signature here... */
140
141         switch (class) {
142         case MEMBERREF_PARENT_TYPEREF:
143                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
144                 if (!klass) {
145                         g_warning ("Missing field %s in typeref index %d", fname, nindex);
146                         return NULL;
147                 }
148                 mono_class_init (klass);
149                 if (retklass)
150                         *retklass = klass;
151                 return mono_class_get_field_from_name (klass, fname);
152         default:
153                 return NULL;
154         }
155 }
156
157 MonoClassField*
158 mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass)
159 {
160         MonoClass *k;
161         guint32 type;
162
163         if (image->assembly->dynamic) {
164                 MonoClassField *result = mono_lookup_dynamic_token (image, token);
165                 *retklass = result->parent;
166                 return result;
167         }
168
169         if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
170                 return mono_field_from_memberref (image, token, retklass);
171
172         type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
173         if (!type)
174                 return NULL;
175         k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
176         mono_class_init (k);
177         if (!k)
178                 return NULL;
179         if (retklass)
180                 *retklass = k;
181         return mono_class_get_field (k, token);
182 }
183
184 static MonoMethod *
185 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
186 {
187         int i;
188         while (klass) {
189                 /* mostly dumb search for now */
190                 for (i = 0; i < klass->method.count; ++i) {
191                         MonoMethod *m = klass->methods [i];
192                         if (!strcmp (name, m->name)) {
193                                 if (mono_metadata_signature_equal (sig, m->signature))
194                                         return m;
195                         }
196                 }
197                 klass = klass->parent;
198         }
199         return NULL;
200
201 }
202
203 static MonoMethod *
204 method_from_memberref (MonoImage *image, guint32 idx)
205 {
206         MonoClass *klass;
207         MonoMethod *method;
208         MonoTableInfo *tables = image->tables;
209         guint32 cols[6];
210         guint32 nindex, class;
211         const char *mname;
212         MonoMethodSignature *sig;
213         const char *ptr;
214
215         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
216         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
217         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
218         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
219                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
220
221         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
222         
223         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
224         mono_metadata_decode_blob_size (ptr, &ptr);
225         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
226
227         switch (class) {
228         case MEMBERREF_PARENT_TYPEREF:
229                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
230                 if (!klass) {
231                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
232                         mono_metadata_free_method_signature (sig);
233                         return NULL;
234                 }
235                 mono_class_init (klass);
236                 method = find_method (klass, mname, sig);
237                 if (!method)
238                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
239                 mono_metadata_free_method_signature (sig);
240                 return method;
241         case MEMBERREF_PARENT_TYPESPEC: {
242                 guint32 bcols [MONO_TYPESPEC_SIZE];
243                 guint32 len;
244                 MonoType *type;
245                 MonoMethod *result;
246
247                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
248                                           bcols, MONO_TYPESPEC_SIZE);
249                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
250                 len = mono_metadata_decode_value (ptr, &ptr);   
251                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
252
253                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
254                         klass = mono_class_from_mono_type (type);
255                         mono_class_init (klass);
256                         method = find_method (klass, mname, sig);
257                         if (!method)
258                                 g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
259                         mono_metadata_free_method_signature (sig);
260                         return method;
261                 }
262
263                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
264                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
265                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
266                 result->signature = sig;
267                 result->name = mname;
268
269                 if (!strcmp (mname, ".ctor")) {
270                         /* we special-case this in the runtime. */
271                         result->addr = NULL;
272                         return result;
273                 }
274                 
275                 if (!strcmp (mname, "Set")) {
276                         g_assert (sig->hasthis);
277                         g_assert (type->data.array->rank + 1 == sig->param_count);
278                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
279                         result->addr = NULL;
280                         return result;
281                 }
282
283                 if (!strcmp (mname, "Get")) {
284                         g_assert (sig->hasthis);
285                         g_assert (type->data.array->rank == sig->param_count);
286                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
287                         result->addr = NULL;
288                         return result;
289                 }
290
291                 if (!strcmp (mname, "Address")) {
292                         g_assert (sig->hasthis);
293                         g_assert (type->data.array->rank == sig->param_count);
294                         result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
295                         result->addr = NULL;
296                         return result;
297                 }
298
299                 g_assert_not_reached ();
300                 break;
301         }
302         case MEMBERREF_PARENT_TYPEDEF:
303                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
304                 if (!klass) {
305                         g_warning ("Missing method %s in assembly %s typedef index %d", mname, image->name, nindex);
306                         mono_metadata_free_method_signature (sig);
307                         return NULL;
308                 }
309                 mono_class_init (klass);
310                 method = find_method (klass, mname, sig);
311                 if (!method)
312                         g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
313                 mono_metadata_free_method_signature (sig);
314                 return method;
315         default:
316                 g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
317                 g_assert_not_reached ();
318         }
319
320         return NULL;
321 }
322
323 typedef struct MonoDllMap MonoDllMap;
324
325 struct MonoDllMap {
326         char *name;
327         char *target;
328         char *dll;
329         MonoDllMap *next;
330 };
331
332 static GHashTable *dll_map;
333
334 int 
335 mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const char **rfunc) {
336         MonoDllMap *map, *tmp;
337
338         if (!dll_map)
339                 return 0;
340         map = g_hash_table_lookup (dll_map, dll);
341         if (!map)
342                 return 0;
343         *rdll = map->target? map->target: dll;
344                 
345         for (tmp = map->next; tmp; tmp = tmp->next) {
346                 if (strcmp (func, tmp->name) == 0) {
347                         *rfunc = tmp->name;
348                         if (tmp->dll)
349                                 *rdll = tmp->dll;
350                         return 1;
351                 }
352         }
353         *rfunc = func;
354         return 1;
355 }
356
357 void
358 mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const char *tfunc) {
359         MonoDllMap *map, *entry;
360
361         if (!dll_map)
362                 dll_map = g_hash_table_new (g_str_hash, g_str_equal);
363
364         map = g_hash_table_lookup (dll_map, dll);
365         if (!map) {
366                 map = g_new0 (MonoDllMap, 1);
367                 map->dll = g_strdup (dll);
368                 if (tdll)
369                         map->target = g_strdup (tdll);
370                 g_hash_table_insert (dll_map, map->dll, map);
371         }
372         if (func) {
373                 entry = g_new0 (MonoDllMap, 1);
374                 entry->name = g_strdup (func);
375                 if (tfunc)
376                         entry->target = g_strdup (tfunc);
377                 if (tdll && map->target && strcmp (map->target, tdll))
378                         entry->dll = g_strdup (tdll);
379                 entry->next = map->next;
380                 map->next = entry;
381         }
382 }
383
384 gpointer
385 mono_lookup_pinvoke_call (MonoMethod *method)
386 {
387         MonoImage *image = method->klass->image;
388         MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
389         MonoTableInfo *tables = image->tables;
390         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
391         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
392         guint32 im_cols [MONO_IMPLMAP_SIZE];
393         guint32 scope_token;
394         const char *import = NULL;
395         const char *scope = NULL;
396         char *full_name;
397         GModule *gmodule;
398
399         g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
400
401         if (method->addr)
402                 return method->addr;
403         if (!piinfo->implmap_idx)
404                 return NULL;
405         
406         mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
407
408         piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
409         import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
410         scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
411         scope = mono_metadata_string_heap (image, scope_token);
412
413         mono_dllmap_lookup (scope, import, &scope, &import);
414
415         full_name = g_module_build_path (NULL, scope);
416         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
417
418         if (!gmodule) {
419                 gchar *error = g_strdup (g_module_error ());
420                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
421                         g_warning ("Failed to load library %s (%s): %s", full_name, scope, error);
422                         g_free (error);
423                         g_free (full_name);
424                         return NULL;
425                 }
426                 g_free (error);
427         }
428         g_free (full_name);
429
430         g_module_symbol (gmodule, import, &method->addr); 
431
432         if (!method->addr) {
433                 g_warning ("Failed to load function %s from %s", import, scope);
434                 return NULL;
435         }
436         return method->addr;
437 }
438
439 MonoMethod *
440 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
441 {
442         MonoMethod *result;
443         int table = mono_metadata_token_table (token);
444         int idx = mono_metadata_token_index (token);
445         MonoTableInfo *tables = image->tables;
446         const char *loc, *sig = NULL;
447         int size;
448         guint32 cols [MONO_TYPEDEF_SIZE];
449
450         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
451                         return result;
452
453         if (image->assembly->dynamic)
454                 return mono_lookup_dynamic_token (image, token);
455
456         if (table != MONO_TABLE_METHOD) {
457                 if (table != MONO_TABLE_MEMBERREF)
458                         g_print("got wrong token: 0x%08x\n", token);
459                 g_assert (table == MONO_TABLE_MEMBERREF);
460                 result = method_from_memberref (image, idx);
461                 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
462                 return result;
463         }
464
465         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
466
467         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
468             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
469                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
470         else 
471                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
472         
473         result->slot = -1;
474         result->klass = klass;
475         result->flags = cols [2];
476         result->iflags = cols [1];
477         result->token = token;
478         result->name = mono_metadata_string_heap (image, cols [3]);
479
480         if (!sig) /* already taken from the methodref */
481                 sig = mono_metadata_blob_heap (image, cols [4]);
482         size = mono_metadata_decode_blob_size (sig, &sig);
483         result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
484
485         if (!result->klass) {
486                 guint32 type = mono_metadata_typedef_from_method (image, token);
487                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
488         }
489
490         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
491                 if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
492                         result->string_ctor = 1;
493
494                 result->addr = mono_lookup_internal_call (result);
495                 result->signature->pinvoke = 1;
496         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
497                 result->signature->pinvoke = 1;
498                 ((MonoMethodPInvoke *)result)->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
499         } else {
500                 /* if this is a methodref from another module/assembly, this fails */
501                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
502
503                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
504                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
505                         g_assert (loc);
506                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
507                 }
508         }
509
510         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
511
512         return result;
513 }
514
515 void
516 mono_free_method  (MonoMethod *method)
517 {
518         mono_metadata_free_method_signature (method->signature);
519         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
520                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
521                 g_free (piinfo->code);
522         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
523                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
524         }
525
526         g_free (method);
527 }
528
529 void
530 mono_method_get_param_names (MonoMethod *method, const char **names)
531 {
532         int i, lastp;
533         MonoClass *klass = method->klass;
534         MonoTableInfo *methodt;
535         MonoTableInfo *paramt;
536
537         if (!method->signature->param_count)
538                 return;
539         for (i = 0; i < method->signature->param_count; ++i)
540                 names [i] = "";
541
542         mono_class_init (klass);
543
544         if (klass->wastypebuilder) /* copy the names later */
545                 return;
546
547         methodt = &klass->image->tables [MONO_TABLE_METHOD];
548         paramt = &klass->image->tables [MONO_TABLE_PARAM];
549         for (i = 0; i < klass->method.count; ++i) {
550                 if (method == klass->methods [i]) {
551                         guint32 idx = klass->method.first + i;
552                         guint32 cols [MONO_PARAM_SIZE];
553                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
554
555                         if (idx + 1 < methodt->rows)
556                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
557                         else
558                                 lastp = paramt->rows + 1;
559                         for (i = param_index; i < lastp; ++i) {
560                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
561                                 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
562                                         names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);
563                         }
564                         return;
565                 }
566         }
567 }
568
569 void
570 mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
571 {
572         int i, lastp;
573         MonoClass *klass = method->klass;
574         MonoTableInfo *methodt;
575         MonoTableInfo *paramt;
576
577         for (i = 0; i < method->signature->param_count + 1; ++i)
578                 mspecs [i] = NULL;
579
580         mono_class_init (klass);
581
582         methodt = &klass->image->tables [MONO_TABLE_METHOD];
583         paramt = &klass->image->tables [MONO_TABLE_PARAM];
584
585         for (i = 0; i < klass->method.count; ++i) {
586                 if (method == klass->methods [i]) {
587                         guint32 idx = klass->method.first + i;
588                         guint32 cols [MONO_PARAM_SIZE];
589                         guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
590
591                         if (idx + 1 < methodt->rows)
592                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
593                         else
594                                 lastp = paramt->rows + 1;
595
596                         for (i = param_index; i < lastp; ++i) {
597                                 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
598
599                                 if (cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
600                                         const char *tp;
601                                         tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
602                                         g_assert (tp);
603                                         mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
604                                 }
605                         }
606                         return;
607                 }
608         }
609 }
610
611 gpointer
612 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
613 {
614         GList *l;
615         g_assert (method != NULL);
616         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
617
618         if (!(l = g_list_nth (((MonoMethodWrapper *)method)->data, id - 1)))
619                 g_assert_not_reached ();
620
621         return l->data;
622 }
623
624 static void
625 default_stack_walk (MonoStackWalk func, gpointer user_data) {
626         g_error ("stack walk not installed");
627 }
628
629 static MonoStackWalkImpl stack_walk = default_stack_walk;
630
631 void
632 mono_stack_walk (MonoStackWalk func, gpointer user_data)
633 {
634         stack_walk (func, user_data);
635 }
636
637 void
638 mono_install_stack_walk (MonoStackWalkImpl func)
639 {
640         stack_walk = func;
641 }
642
643 static gboolean
644 last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
645 {
646         MonoMethod **dest = data;
647         *dest = m;
648         /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
649
650         return managed;
651 }
652
653 MonoMethod*
654 mono_method_get_last_managed (void)
655 {
656         MonoMethod *m = NULL;
657         stack_walk (last_managed, &m);
658         return m;
659 }
660
661
662