* src/vm/vm.c (vm_create): make agentlib/agentpath work
[cacao.git] / src / native / jvmti / VMjdwp.c
1 /* src/native/vm/VMjdwp.c - jvmti->jdwp interface
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Author: Martin Platter
28
29    Changes:             
30
31
32    $Id: VMjdwp.c 4892 2006-05-06 18:29:55Z motse $
33
34 */
35
36 #include "native/jvmti/jvmti.h"
37 #include "native/jvmti/cacaodbg.h"
38 #include "native/jvmti/VMjdwp.h"
39 #include "vm/loader.h"
40 #include "vm/exceptions.h"
41 #include "vm/jit/asmpart.h"
42
43 #include <stdlib.h>
44
45
46 static methodinfo *notifymid = NULL;
47 static classinfo *Jdwpclass = NULL;
48
49
50 #define FINDCLASS(jni_env,class,classname) \
51   class = load_class_from_sysloader(utf_new_char(classname)); \
52   if (!class) throw_main_exception_exit();
53  
54
55 #define GETJNIMETHOD(jni_env,class,classname,method,methodname,methodsig) \
56   FINDCLASS(jni_env,class,classname) \
57   method = class_resolveclassmethod (class,utf_new_char(methodname), \
58                                                                          utf_new_char(methodsig),        \
59                                                                          class_java_lang_Object,true);   \
60   if (!method) throw_main_exception_exit();
61  
62 #define GETJNISTATICMETHOD(jni_env,class,classname,method,methodname,methodsig) \
63   FINDCLASS(jni_env,class,classname) \
64   method = class_resolveclassmethod (class,utf_new_char(methodname), \
65                                                                          utf_new_char(methodsig),        \
66                                                                          class_java_lang_Object,true);   \
67   if (!method) throw_main_exception_exit();  
68
69
70 static void notify (JNIEnv* env, jobject event){
71         log_text("VMjdwp notfiy called");
72
73     if (notifymid == NULL) {
74                 GETJNISTATICMETHOD(env,Jdwpclass,"gnu/classpath/jdwp/Jdwp",notifymid,
75                                                    "notify","(Lgnu/classpath/jdwp/event/Event;)V");
76                 
77     }
78     
79         vm_call_method(notifymid, NULL ,event);
80         if (*exceptionptr)
81                 throw_main_exception_exit();
82
83 }
84
85
86 static void SingleStep (jvmtiEnv *jvmti_env,
87                         JNIEnv* jni_env,
88                         jthread thread,
89                         jmethodID method,
90                         jlocation location) {
91   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
92 }
93
94 static void Breakpoint (jvmtiEnv *jvmti_env,
95                         JNIEnv* jni_env,
96                         jthread thread,
97                         jmethodID method,
98                         jlocation location) {
99   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
100 }
101
102 static void FieldAccess    (jvmtiEnv *jvmti_env,
103                             JNIEnv* jni_env,
104                             jthread thread,
105                             jmethodID method,
106                             jlocation location,
107                             jclass field_klass,
108                             jobject object,
109                             jfieldID field)
110 {
111   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
112 }
113
114 static void FieldModification (jvmtiEnv *jvmti_env,
115                                JNIEnv* jni_env,
116                                jthread thread,
117                                jmethodID method,
118                                jlocation location,
119                                jclass field_klass,
120                                jobject object,
121                                jfieldID field,
122                                char signature_type,
123                                jvalue new_value) {
124   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
125 }
126
127 static void FramePop (jvmtiEnv *jvmti_env,
128                       JNIEnv* jni_env,
129                       jthread thread,
130                       jmethodID method,
131                       jboolean was_popped_by_exception){
132   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
133 }
134
135 static void MethodEntry (jvmtiEnv *jvmti_env,
136                          JNIEnv* jni_env,
137                          jthread thread,
138                          jmethodID method){
139   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
140 }
141
142 static void MethodExit (jvmtiEnv *jvmti_env,
143                         JNIEnv* jni_env,
144                         jthread thread,
145                         jmethodID method,
146                         jboolean was_popped_by_exception,
147                         jvalue return_value){
148   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
149 }
150
151 static void NativeMethodBind (jvmtiEnv *jvmti_env,
152                               JNIEnv* jni_env,
153                               jthread thread,
154                               jmethodID method,
155                               void* address,
156                               void** new_address_ptr){
157   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
158 }
159
160 static void Exception (jvmtiEnv *jvmti_env,
161                        JNIEnv* jni_env,
162                        jthread thread,
163                        jmethodID method,
164                        jlocation location,
165                        jobject exception,
166                        jmethodID catch_method,
167                        jlocation catch_location){
168   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
169 }
170
171 static void ExceptionCatch (jvmtiEnv *jvmti_env,
172                             JNIEnv* jni_env,
173                             jthread thread,
174                             jmethodID method,
175                             jlocation location,
176                             jobject exception){
177   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
178 }
179
180 static void ThreadStart (jvmtiEnv *jvmti_env,
181                          JNIEnv* jni_env,
182                          jthread thread){
183         jclass cl;
184         jmethodID cc;
185         jobject obj;
186
187         GETJNIMETHOD(jni_env,cl,"gnu/classpath/jdwp/event/ThreadStartEvent",cc,"<init>","(Ljava/lang/Thread;)V"); 
188
189         obj = builtin_new(cl);
190         if (!obj) throw_main_exception_exit();
191
192         fprintf(stderr,"VMjdwp:ThreadStart: thread %p\n",thread);
193         fflush(stderr);
194
195         vm_call_method((methodinfo*)cc, obj, thread);
196
197         if (*exceptionptr)
198                 throw_main_exception_exit();
199
200         notify (jni_env,obj);
201 }
202
203 static void ThreadEnd (jvmtiEnv *jvmti_env,
204                        JNIEnv* jni_env,
205                        jthread thread){
206   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
207 }
208
209 static void ClassLoad (jvmtiEnv *jvmti_env,
210                        JNIEnv* jni_env,
211                        jthread thread,
212                        jclass klass){
213   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
214 }
215
216 static void ClassPrepare (jvmtiEnv *jvmti_env,
217                           JNIEnv* jni_env,
218                           jthread thread,
219                           jclass klass){
220   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
221 }
222
223 static void ClassFileLoadHook (jvmtiEnv *jvmti_env,
224                                JNIEnv* jni_env,
225                                jclass class_being_redefined,
226                                jobject loader,
227                                const char* name,
228                                jobject protection_domain,
229                                jint class_data_len,
230                                const unsigned char* class_data,
231                                jint* new_class_data_len,
232                                unsigned char** new_class_data){
233   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
234 }
235
236 static void VMStart (jvmtiEnv *jvmti_env,
237                      JNIEnv* jni_env) {
238   log_text ("JVMTI-Event:VMStart IMPLEMENT ME!!!");
239 }
240
241 static void VMInit (jvmtiEnv *jvmti_env, 
242                     JNIEnv* jni_env,
243                     jthread thread) {
244         classinfo* cl;
245         methodinfo* cc;
246         java_objectheader* obj;
247
248         log_text ("JVMTI-Event:VMInit");
249
250         GETJNIMETHOD(jni_env,cl,"gnu/classpath/jdwp/event/VmInitEvent",cc,"<init>",
251                                  "(Ljava/lang/Thread;)V"); 
252
253         fprintf(stderr,"VMjdwp:VMInit: 1\n");
254
255         obj = builtin_new(cl);
256         if (!obj) throw_main_exception_exit();
257
258         fprintf(stderr,"VMjdwp:VMInit: thread %p\n",thread);
259         fflush(stderr);
260
261         vm_call_method((methodinfo*)cc, obj, thread);
262
263         if (*exceptionptr)
264                 throw_main_exception_exit();
265
266         notify (jni_env,obj);
267 }
268
269 static void VMDeath (jvmtiEnv *jvmti_env,
270                      JNIEnv* jni_env) {
271   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
272 }
273
274
275 static void CompiledMethodLoad    (jvmtiEnv *jvmti_env,
276                                    jmethodID method,
277                                    jint code_size,
278                                    const void* code_addr,
279                                    jint map_length,
280                                    const jvmtiAddrLocationMap* map,
281                                    const void* compile_info) {
282   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
283 }
284
285 static void CompiledMethodUnload (jvmtiEnv *jvmti_env,
286                                   jmethodID method,
287                                   const void* code_addr){
288   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
289 }
290
291 static void DynamicCodeGenerated (jvmtiEnv *jvmti_env,
292                                   const char* name,
293                                   const void* address,
294                                   jint length){
295   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
296 }
297
298 static void DataDumpRequest (jvmtiEnv *jvmti_env){
299   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
300 }
301
302 static void MonitorContendedEnter (jvmtiEnv *jvmti_env,
303                                    JNIEnv* jni_env,
304                                    jthread thread,
305                                    jobject object){
306   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
307 }
308
309 static void MonitorContendedEntered (jvmtiEnv *jvmti_env,
310                                      JNIEnv* jni_env,
311                                      jthread thread,
312                                      jobject object){
313   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
314 }
315
316 static void MonitorWait (jvmtiEnv *jvmti_env,
317                          JNIEnv* jni_env,
318                          jthread thread,
319                          jobject object,
320                          jlong timeout){
321   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
322 }
323
324 static void MonitorWaited (jvmtiEnv *jvmti_env,
325                            JNIEnv* jni_env,
326                            jthread thread,
327                            jobject object,
328                            jboolean timed_out){
329   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
330 }
331
332 static void VMObjectAlloc (jvmtiEnv *jvmti_env,
333                            JNIEnv* jni_env,
334                            jthread thread,
335                            jobject object,
336                            jclass object_klass,
337                            jlong size){
338   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
339 }
340
341 static void ObjectFree (jvmtiEnv *jvmti_env,
342                         jlong tag){
343   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
344 }
345
346 static void GarbageCollectionStart (jvmtiEnv *jvmti_env){
347   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
348 }
349
350 static void GarbageCollectionFinish (jvmtiEnv *jvmti_env){
351   log_text ("JVMTI-Event: IMPLEMENT ME!!!");
352 }
353
354
355 /* it would be more apropriate to call this function from gnu-cp jdwp */
356 bool VMjdwpInit() {
357         int end, i=0;
358         jvmtiCapabilities cap;
359         jvmtiError e;
360
361         log_text("cacao vm - create new jvmti environment");
362         jvmtienv = new_jvmtienv();
363
364         /* set eventcallbacks */
365         if (JVMTI_ERROR_NONE != 
366                 (*jvmtienv)->SetEventCallbacks(jvmtienv,
367                                                            &jvmti_jdwp_EventCallbacks,
368                                                            sizeof(jvmti_jdwp_EventCallbacks))){
369                 log_text("unable to setup event callbacks");
370                 return false;
371         }
372         
373         e = (*jvmtienv)->GetPotentialCapabilities(jvmtienv, &cap);
374         if (e == JVMTI_ERROR_NONE) {
375                 e = (*jvmtienv)->AddCapabilities(jvmtienv, &cap);
376         }
377         if (e != JVMTI_ERROR_NONE) {
378                 log_text("error adding jvmti capabilities");
379                 exit(1);
380         }
381
382         end = sizeof(jvmti_jdwp_EventCallbacks) / sizeof(void*);
383         for (i = 0; i < end; i++) {
384                 /* enable standard VM callbacks  */
385                 if (((void**)&jvmti_jdwp_EventCallbacks)[i] != NULL) {
386                         e = (*jvmtienv)->SetEventNotificationMode(jvmtienv,
387                                                                                                 JVMTI_ENABLE,
388                                                                                                 JVMTI_EVENT_START_ENUM+i,
389                                                                                                 NULL);
390
391                         if (JVMTI_ERROR_NONE != e) {
392                                 log_text("unable to setup event notification mode");
393                                 return false;
394                         }
395                 }
396         }
397         return true;
398 }
399         
400         jvmtiEventCallbacks jvmti_jdwp_EventCallbacks = {
401     &VMInit,
402     &VMDeath,
403     &ThreadStart,
404     &ThreadEnd,
405     &ClassFileLoadHook,
406     &ClassLoad,
407     &ClassPrepare,
408     NULL, /* &VMStart */
409     &Exception,
410     &ExceptionCatch,
411     &SingleStep,
412     &FramePop,
413     &Breakpoint,
414     &FieldAccess,
415     &FieldModification,
416     &MethodEntry,
417     &MethodExit,
418     &NativeMethodBind,
419     &CompiledMethodLoad,
420     &CompiledMethodUnload,
421     &DynamicCodeGenerated,
422     &DataDumpRequest,
423     NULL,
424     &MonitorWait,
425     &MonitorWaited,
426     &MonitorContendedEnter,
427     &MonitorContendedEntered,
428     NULL,
429     NULL,
430     NULL,
431     NULL,
432     &GarbageCollectionStart,
433     &GarbageCollectionFinish,
434     &ObjectFree,
435     &VMObjectAlloc,
436 };
437
438
439 /*
440  * These are local overrides for various environment variables in Emacs.
441  * Please do not remove this and leave it at the end of the file, where
442  * Emacs will automagically detect them.
443  * ---------------------------------------------------------------------
444  * Local variables:
445  * mode: c
446  * indent-tabs-mode: t
447  * c-basic-offset: 4
448  * tab-width: 4
449  * End:
450  */