PR156: Preparation
[cacao.git] / src / vm / hook.hpp
1 /* src/vm/hook.hpp - hook points inside the VM
2
3    Copyright (C) 2009, 2011
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _HOOK_HPP
27 #define _HOOK_HPP
28
29 #include "config.h"
30 #include "vm/globals.hpp"
31
32 #if defined(ENABLE_OPAGENT)
33 #include "vm/jit/oprofile-agent.hpp"
34 #endif
35
36
37 /**
38  * Hook points are inline functions acting as probes scattered throughout
39  * several VM subsystems. They can be used to implement event generation
40  * or statistics gathering without polluting the source code. Hence all
41  * compiler macro and runtime checks should be done in this file. One
42  * example of where hooks are useful is JVMTI event firing.
43  */
44 namespace Hook {
45         void breakpoint     (Breakpoint *bp);
46         bool class_linked   (classinfo *c);
47         void class_loaded   (classinfo *c);
48         void jit_generated  (methodinfo *m, codeinfo *code);
49         void jit_recycled   (methodinfo *m, codeinfo *code);
50         void method_enter   (methodinfo *m);
51         void method_exit    (methodinfo *m);
52         void method_unwind  (methodinfo *m);
53         void native_resolved(methodinfo *m, void *symbol, void **symbolptr);
54         void thread_start   (threadobject *t);
55         void thread_end     (threadobject *t);
56         void vm_init        ();
57         void vm_preinit     ();
58         void vm_shutdown    ();
59
60         // Non-inline functions
61         bool class_linked_dynoffsets(classinfo *c);
62 }
63
64
65 inline void Hook::breakpoint(Breakpoint *bp)
66 {
67 #if defined(ENABLE_JVMTI)
68         methodinfo* m = bp->method;
69         int32_t     l = bp->location;
70
71         log_message_method("JVMTI: Reached breakpoint in method ", m);
72         log_println("JVMTI: Reached breakpoint at location %d", l);
73 #endif
74 }
75
76 inline bool Hook::class_linked(classinfo *c)
77 {
78         if (c == class_java_lang_String)
79                 linker_initialize_deferred_strings();
80
81         return class_linked_dynoffsets(c);
82 }
83
84 inline void Hook::class_loaded(classinfo *c)
85 {
86         /* nop */
87 }
88
89 /**
90  * Hook point just after code was generated. Note that one method can have
91  * multiple code realizations, the hook is fired for each of them. The code
92  * was not yet executed.
93  *
94  * @param m The method for which code was generated.
95  * @param code The fully initialized codeinfo for the generated code.
96  */
97 inline void Hook::jit_generated(methodinfo *m, codeinfo *code)
98 {
99 #if defined(ENABLE_OPAGENT)
100         if (opt_EnableOpagent)
101                 OprofileAgent::newmethod(m);
102 #endif
103 }
104
105 inline void Hook::method_enter(methodinfo *m)
106 {
107         /* nop */
108 }
109
110 inline void Hook::method_exit(methodinfo *m)
111 {
112         /* nop */
113 }
114
115 inline void Hook::method_unwind(methodinfo *m)
116 {
117         /* nop */
118 }
119
120 inline void Hook::native_resolved(methodinfo *m, void *symbol, void **symbolptr)
121 {
122         /* nop */
123 }
124
125 inline void Hook::thread_start(threadobject *t)
126 {
127         /* nop */
128 }
129
130 inline void Hook::thread_end(threadobject *t)
131 {
132         /* nop */
133 }
134
135 /**
136  * Hook point after the VM is initialized. At this point the VM is fully
137  * operating and ready to execute Java code. Final intializations and thread
138  * startup should be done here.
139  */
140 inline void Hook::vm_init()
141 {
142         /* nop */
143 }
144
145 /**
146  * Hook point before the VM is initialized. At this point the VM can not
147  * yet execute Java code but some central native subsystems are initialized.
148  * Only basic initialization steps should be done here.
149  */
150 inline void Hook::vm_preinit()
151 {
152 #if defined(ENABLE_OPAGENT)
153         if (opt_EnableOpagent)
154                 OprofileAgent::initialize();
155 #endif
156 }
157
158 /**
159  * Hook point before the VM is actually destroyed. At this point the VM is
160  * still running, but all non-daemon threads have terminated and resources
161  * are ready to be reclaimed. Final cleanup tasks should be done here.
162  */
163 inline void Hook::vm_shutdown()
164 {
165 #if defined(ENABLE_OPAGENT)
166         if (opt_EnableOpagent)
167                 OprofileAgent::close();
168 #endif
169 }
170
171
172 #endif /* _HOOK_HPP */
173
174
175 /*
176  * These are local overrides for various environment variables in Emacs.
177  * Please do not remove this and leave it at the end of the file, where
178  * Emacs will automagically detect them.
179  * ---------------------------------------------------------------------
180  * Local variables:
181  * mode: c++
182  * indent-tabs-mode: t
183  * c-basic-offset: 4
184  * tab-width: 4
185  * End:
186  * vim:noexpandtab:sw=4:ts=4:
187  */