1 /*
2 * Copyright (c) 1999, 2017, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <stdatomic.h>
27
28 #include "vmDebug.h"
29
30 #include "JDWP.h"
31 #include "debugLoop.h"
32 #include "transport.h"
33 #include "util.h"
34 #include "eventHelper.h"
35 #include "threadControl.h"
36
37 static _Atomic(jlong) lastDebuggerActivity = ATOMIC_VAR_INIT(0LL);
38 static _Atomic(jboolean) hasSeenDebuggerActivity = ATOMIC_VAR_INIT(JNI_FALSE);
39
40 // Reset the tracking variables.
vmDebug_onDisconnect()41 void vmDebug_onDisconnect()
42 {
43 atomic_store(&lastDebuggerActivity, 0LL);
44 atomic_store(&hasSeenDebuggerActivity, JNI_FALSE);
45 }
46
47 // Mark us as having seen actual debugger activity (so isDebuggerConnected can return true) and that
48 // we are currently doing something as the debugger.
vmDebug_notifyDebuggerActivityStart()49 void vmDebug_notifyDebuggerActivityStart()
50 {
51 atomic_store(&lastDebuggerActivity, 0LL);
52 atomic_store(&hasSeenDebuggerActivity, JNI_TRUE);
53 }
54
55 // Update the timestamp for the last debugger activity.
vmDebug_notifyDebuggerActivityEnd()56 void vmDebug_notifyDebuggerActivityEnd()
57 {
58 atomic_store(&lastDebuggerActivity, milliTime());
59 }
60
61 // For backwards compatibility we are only considered 'connected' as far as VMDebug is concerned if
62 // we have gotten at least one non-ddms JDWP packet.
63 static jboolean
isDebuggerConnected()64 isDebuggerConnected()
65 {
66 return transport_is_open() && atomic_load(&hasSeenDebuggerActivity);
67 }
68
69 static jboolean JNICALL
VMDebug_isDebuggerConnected(JNIEnv * env,jclass klass)70 VMDebug_isDebuggerConnected(JNIEnv* env, jclass klass)
71 {
72 return isDebuggerConnected();
73 }
74
75 static void JNICALL
VMDebug_suspendAllAndSendVmStart(JNIEnv * env,jclass klass)76 VMDebug_suspendAllAndSendVmStart(JNIEnv* env, jclass klass)
77 {
78 jthread currentThread;
79 JVMTI_FUNC_PTR(gdata->jvmti, GetCurrentThread)(gdata->jvmti, ¤tThread);
80 eventHelper_reportVMInit(getEnv(), 0, currentThread, JDWP_SUSPEND_POLICY(ALL));
81 }
82
83 static jboolean JNICALL
VMDebug_isDebuggingEnabled(JNIEnv * env,jclass klass)84 VMDebug_isDebuggingEnabled(JNIEnv* env, jclass klass)
85 {
86 // We are running the debugger so debugging is definitely enabled.
87 return JNI_TRUE;
88 }
89
90 static jlong JNICALL
VMDebug_lastDebuggerActivity(JNIEnv * env,jclass klass)91 VMDebug_lastDebuggerActivity(JNIEnv* env, jclass klass)
92 {
93 if (!isDebuggerConnected()) {
94 LOG_ERROR(("VMDebug.lastDebuggerActivity called without active debugger"));
95 return -1;
96 }
97 jlong last_time = atomic_load(&lastDebuggerActivity);
98 if (last_time == 0) {
99 LOG_MISC(("debugger is performing an action"));
100 return 0;
101 }
102
103 jlong cur_time = milliTime();
104
105 if (cur_time < last_time) {
106 LOG_ERROR(("Time seemed to go backwards: last was %lld, current is %lld",
107 last_time, cur_time));
108 return 0;
109 }
110 jlong res = cur_time - last_time;
111 LOG_MISC(("Debugger interval is %lld", res));
112 return res;
113 }
114
115 void
vmDebug_initalize(JNIEnv * env)116 vmDebug_initalize(JNIEnv* env)
117 {
118 WITH_LOCAL_REFS(env, 1) {
119 jclass vmdebug_class = JNI_FUNC_PTR(env,FindClass)(env, "dalvik/system/VMDebug");
120 if (vmdebug_class == NULL) {
121 // The VMDebug class isn't available. We don't need to do anything.
122 LOG_MISC(("dalvik.system.VMDebug does not seem to be available on this runtime."));
123 // Get rid of the ClassNotFoundException.
124 JNI_FUNC_PTR(env,ExceptionClear)(env);
125 goto finish;
126 }
127
128 JNINativeMethod methods[4];
129
130 // Take over the implementation of these functions.
131 methods[0].name = "lastDebuggerActivity";
132 methods[0].signature = "()J";
133 methods[0].fnPtr = (void*)VMDebug_lastDebuggerActivity;
134
135 methods[1].name = "isDebuggingEnabled";
136 methods[1].signature = "()Z";
137 methods[1].fnPtr = (void*)VMDebug_isDebuggingEnabled;
138
139 methods[2].name = "isDebuggerConnected";
140 methods[2].signature = "()Z";
141 methods[2].fnPtr = (void*)VMDebug_isDebuggerConnected;
142
143 methods[3].name = "suspendAllAndSendVmStart";
144 methods[3].signature = "()V";
145 methods[3].fnPtr = (void*)VMDebug_suspendAllAndSendVmStart;
146
147 jint res = JNI_FUNC_PTR(env,RegisterNatives)(env,
148 vmdebug_class,
149 methods,
150 sizeof(methods) / sizeof(JNINativeMethod));
151 if (res != JNI_OK) {
152 EXIT_ERROR(JVMTI_ERROR_INTERNAL,
153 "RegisterNatives returned failure for VMDebug class");
154 }
155
156 finish: ;
157 } END_WITH_LOCAL_REFS(env);
158 }
159
160