1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <string.h>
25 #include <stdio.h>
26 
27 #include "jvmti.h"
28 
29 #define CLASS_NAME "Lcompiler/jsr292/cr8026328/Test8026328;"
30 #define METHOD_NAME "main"
31 
32 static jvmtiEnv *jvmti = NULL;
33 
34 
35 void JNICALL
classprepare(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jclass klass)36 classprepare(jvmtiEnv* jvmti_env,
37             JNIEnv* jni_env,
38             jthread thread,
39             jclass klass) {
40 
41     char* buf;
42     (*jvmti)->GetClassSignature(jvmti,
43                 klass,
44                 &buf,
45                 NULL);
46     if (strcmp(CLASS_NAME, buf) == 0) {
47         jint nMethods;
48         jmethodID* methods;
49         int i;
50         (*jvmti)->GetClassMethods(jvmti,
51                     klass,
52                     &nMethods,
53                     &methods);
54         for (i = 0; i < nMethods; i++) {
55             char* name;
56             (*jvmti)->GetMethodName(jvmti,
57                         methods[i],
58                         &name,
59                         NULL,
60                         NULL);
61             if (strcmp(METHOD_NAME, name) == 0) {
62                 printf("Setting breakpoint\n");
63                 fflush(stdout);
64                 (*jvmti)->SetBreakpoint(jvmti, methods[i], 0);
65             }
66         }
67     }
68 }
69 
70 
71 void JNICALL
breakpoint(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location)72 breakpoint(jvmtiEnv* jvmti_env,
73             JNIEnv* jni_env,
74             jthread thread,
75             jmethodID method,
76             jlocation location) {
77 
78     jclass declaring_class;
79     char* name;
80     char* cname;
81     (*jvmti)->GetMethodName(jvmti,
82                 method,
83                 &name,
84                 NULL,
85                 NULL);
86     (*jvmti)->GetMethodDeclaringClass(jvmti,
87                 method,
88                 &declaring_class);
89     (*jvmti)->GetClassSignature(jvmti,
90                 declaring_class,
91                 &cname,
92                 NULL);
93     printf("Hit breakpoint at %s::%s:%d\n", cname, name, (int) location);
94     fflush(stdout);
95 }
96 
97 
98 JNIEXPORT jint JNICALL
Agent_OnLoad(JavaVM * vm,char * options,void * reserved)99 Agent_OnLoad(JavaVM* vm,
100              char* options,
101              void* reserved) {
102 
103     jvmtiCapabilities capa;
104     jvmtiEventCallbacks cbs = {0};
105 
106     (*vm)->GetEnv(vm, (void**)&jvmti, JVMTI_VERSION_1_0);
107 
108     memset(&capa, 0, sizeof(capa));
109     capa.can_generate_breakpoint_events = 1;
110     capa.can_generate_single_step_events = 1;
111     (*jvmti)->AddCapabilities(jvmti, &capa);
112 
113     cbs.ClassPrepare = classprepare;
114     cbs.Breakpoint = breakpoint;
115     (*jvmti)->SetEventCallbacks(jvmti, &cbs, sizeof(cbs));
116     (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
117     (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL);
118     printf("Loaded agent\n");
119     fflush(stdout);
120 
121     return 0;
122 }
123