VTune Amplifier XE 2011 plugin for Mono
[mono.git] / mono / profiler / mono-profiler-vtune.c
1 /*
2  * mono-codeanalyst.c: VTune profiler
3  *
4  * Author:
5  *   Virgile Bello (virgile.bello@gmail.com)
6  *
7  * (C) 2011 Virgile Bello
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #include <mono/metadata/profiler.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/debug-helpers.h>
32 #include <mono/metadata/assembly.h>
33 #include <mono/metadata/mono-debug.h>
34 #include <mono/metadata/debug-mono-symfile.h>
35 #include <string.h>
36 #include <glib.h>
37
38 #define bool char
39
40 #include "jitprofiling.h"
41
42 /* called at the end of the program */
43 static void
44 codeanalyst_shutdown (MonoProfiler *prof)
45 {
46         iJIT_NotifyEvent(iJVM_EVENT_TYPE_SHUTDOWN, NULL);
47 }
48
49 static void
50 method_jit_result (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result) {
51         if (result == MONO_PROFILE_OK) {
52                 int i;
53                 MonoDebugSourceLocation *sourceLoc;
54                 MonoDebugMethodInfo *methodDebugInfo;
55                 MonoDebugMethodJitInfo *dmji;
56                 MonoClass *klass = mono_method_get_class (method);
57                 char *signature = mono_signature_get_desc (mono_method_signature (method), TRUE);
58                 char *name = g_strdup_printf ("%s(%s)", mono_method_get_name (method), signature);
59                 char *classname = g_strdup_printf ("%s%s%s", mono_class_get_namespace (klass), mono_class_get_namespace (klass)[0] != 0 ? "::" : "", mono_class_get_name (klass));
60                 gpointer code_start = mono_jit_info_get_code_start (jinfo);
61                 int code_size = mono_jit_info_get_code_size (jinfo);
62                 
63                 iJIT_Method_Load vtuneMethod;
64                 memset(&vtuneMethod, 0, sizeof(vtuneMethod));
65                 vtuneMethod.method_id = iJIT_GetNewMethodID();
66                 vtuneMethod.method_name = name;
67                 vtuneMethod.method_load_address = code_start;
68                 vtuneMethod.method_size = code_size;
69                 vtuneMethod.class_file_name = classname;
70
71                 dmji = mono_debug_find_method (method, mono_domain_get());
72
73                 if (dmji != NULL)
74                 {
75                         vtuneMethod.line_number_size = dmji->num_line_numbers;
76                         vtuneMethod.line_number_table = (vtuneMethod.line_number_size != 0) ?
77                                 (LineNumberInfo*)malloc(sizeof(LineNumberInfo) * vtuneMethod.line_number_size) : NULL;
78
79                         for (i = 0; i < dmji->num_line_numbers; ++i)
80                         {
81                                 sourceLoc = mono_debug_lookup_source_location (method, dmji->line_numbers[i].native_offset, mono_domain_get());
82                                 if (sourceLoc == NULL)
83                                 {
84                                         free(vtuneMethod.line_number_table);
85                                         vtuneMethod.line_number_table = NULL;
86                                         vtuneMethod.line_number_size = 0;
87                                         break;
88                                 }
89                                 if (i == 0)
90                                         vtuneMethod.source_file_name = strdup(sourceLoc->source_file);
91                                 vtuneMethod.line_number_table[i].Offset = dmji->line_numbers[i].native_offset;
92                                 vtuneMethod.line_number_table[i].LineNumber = sourceLoc->row;
93                                 mono_debug_free_source_location (sourceLoc);
94                         }
95                         mono_debug_free_method_jit_info (dmji);
96                 }
97
98                 iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &vtuneMethod);
99
100                 if (vtuneMethod.source_file_name != NULL)
101                         free(vtuneMethod.source_file_name);
102                 if (vtuneMethod.line_number_table != NULL)
103                         free(vtuneMethod.line_number_table);
104         
105                 g_free (signature);
106                 g_free (name);
107                 g_free (classname);
108         }
109 }
110
111 /* the entry point */
112 void
113 mono_profiler_startup (const char *desc)
114 {
115         iJIT_IsProfilingActiveFlags flags = iJIT_IsProfilingActive();
116         if (flags == iJIT_SAMPLING_ON)
117         {
118                 mono_profiler_install (NULL, codeanalyst_shutdown);
119                 mono_profiler_install_jit_end (method_jit_result);
120                 mono_profiler_set_events (MONO_PROFILE_JIT_COMPILATION);
121         }
122 }