Fix the build.
[mono.git] / mono / profiler / mono-cov.c
1 /*
2  * mono-co.c: Coverage profiler
3  *
4
5  * Copyright 2008-2009 Novell, Inc (http://www.novell.com)
6  */
7 #include <mono/metadata/profiler.h>
8 #include <mono/metadata/tokentype.h>
9 #include <mono/metadata/tabledefs.h>
10 #include <mono/metadata/debug-helpers.h>
11 #include <mono/metadata/assembly.h>
12 #include <string.h>
13
14 /*
15  * Coverage profiler. Compile with:
16  * gcc -Wall -shared -o mono-profiler-cov.so mono-cov.c `pkg-config --cflags --libs mono`
17  * Install the binary where the dynamic loader can find it (/usr/local/lib, for example,
18  * or set the env var LD_LIBRARY_PATH to the directory where the file is).
19  * Then run mono with:
20  * mono --profile=cov:yourassembly test_suite.exe
21  * mono --profile=cov:yourassembly/namespace test_suite.exe
22  */
23
24 struct _MonoProfiler {
25         GHashTable *hash;
26         char* assembly_name;
27         char* class_name;
28         MonoAssembly *assembly;
29         GList *bb_coverage;
30 };
31
32 static void
33 get_assembly (MonoAssembly* ass, MonoProfiler *prof)
34 {
35         if (strcmp (prof->assembly_name, mono_image_get_name (mono_assembly_get_image (ass))) == 0)
36                 prof->assembly = ass;
37 }
38
39 static void
40 coverage_callback (MonoProfiler *prof, const MonoProfileCoverageEntry *entry)
41 {
42         char* cmsg;
43
44         if (entry->counter)
45                 return;
46
47         if (entry->filename) {
48                 cmsg = g_strdup_printf ("offset 0x%04x (%s: line: %d, col: %d)", 
49                         entry->iloffset, entry->filename, entry->line, entry->col);
50         } else {
51                 cmsg = g_strdup_printf ("offset 0x%04x", entry->iloffset);
52         }
53         prof->bb_coverage = g_list_append (prof->bb_coverage, cmsg);
54 }
55
56 static void
57 check_partial_coverage (MonoProfiler *prof, MonoMethod *method)
58 {
59         GList *tmp;
60         
61         mono_profiler_coverage_get (prof, method, coverage_callback);
62         if (prof->bb_coverage) {
63                 char *name = mono_method_full_name (method, TRUE);
64                 g_print ("Partial coverage: %s\n", name);
65                 g_free (name);
66                 for (tmp = prof->bb_coverage; tmp; tmp = tmp->next) {
67                         g_print ("\t%s\n", (char*)tmp->data);
68                         g_free (tmp->data);
69                 }
70                 g_list_free (prof->bb_coverage);
71                 prof->bb_coverage = NULL;
72         }
73 }
74
75 /* called at the end of the program */
76 static void
77 cov_shutdown (MonoProfiler *prof)
78 {
79         MonoImage *image;
80         MonoMethod *method;
81         int i;
82         char *name;
83
84         mono_assembly_foreach ((GFunc)get_assembly, prof);
85         if (!prof->assembly) {
86                 g_print ("Assembly '%s' was not loaded\n", prof->assembly_name);
87                 return;
88         }
89         image = mono_assembly_get_image (prof->assembly);
90         for (i = 1; i <= mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
91                 MonoClass *klass;
92                 method = mono_get_method (image, i | MONO_TOKEN_METHOD_DEF, NULL);
93                 if (!method)
94                         continue;
95                 if ((mono_method_get_flags (method, NULL) & METHOD_ATTRIBUTE_ABSTRACT))
96                         continue;
97                 /* FIXME: handle icalls, runtime calls and synchronized methods */
98                 if (prof->class_name && *prof->class_name) {
99                         klass = mono_method_get_class (method);
100                         if (!strstr (mono_class_get_name (klass), prof->class_name) && !strstr (mono_class_get_namespace (klass), prof->class_name))
101                                 continue;
102                 }
103                 /*g_print ("check %s::%s, %p\n", method->klass->name, method->name, method);*/
104                 if (g_hash_table_lookup (prof->hash, method)) {
105                         /* the method was executed: check it was fully covered */
106                         check_partial_coverage (prof, method);
107                         continue;
108                 }
109                 name = mono_method_full_name (method, TRUE);
110                 g_print ("Not covered: %s\n", name);
111                 g_free (name);
112         }
113 }
114
115 static void
116 cov_method_enter (MonoProfiler *prof, MonoMethod *method)
117 {
118         /*g_print ("enter %s::%s, %p\n", method->klass->name, method->name, method);*/
119         g_hash_table_insert (prof->hash, method, GINT_TO_POINTER (1));
120 }
121
122 static void
123 cov_method_leave (MonoProfiler *prof, MonoMethod *method)
124 {
125 }
126
127 void
128 mono_profiler_startup (const char *desc);
129
130 /* the entry point */
131 void
132 mono_profiler_startup (const char *desc)
133 {
134         MonoProfiler *prof;
135
136         prof = g_new0 (MonoProfiler, 1);
137         prof->hash = g_hash_table_new (NULL, NULL);
138         if (strncmp ("cov:", desc, 4) == 0 && desc [4]) {
139                 char *cname;
140                 prof->assembly_name = g_strdup (desc + 4);
141                 cname = strchr (prof->assembly_name, '/');
142                 if (cname) {
143                         *cname = 0;
144                         prof->class_name = cname + 1;
145                 }
146         } else {
147                 prof->assembly_name = g_strdup ("mscorlib");
148         }
149
150         mono_profiler_install (prof, cov_shutdown);
151         
152         mono_profiler_install_enter_leave (cov_method_enter, cov_method_leave);
153
154         mono_profiler_set_events (MONO_PROFILE_ENTER_LEAVE | MONO_PROFILE_COVERAGE);
155 }
156
157