1 /*
  2  * Copyright (c) 2001, 2021, 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  * handlers
 27  *
 28  * The default event request handler functions
 29  */
 30 
 31 #include "util.h"
 32 #include "eventHandler.h"
 33 #include "threadControl.h"
 34 #include "eventHelper.h"
 35 #include "classTrack.h"
 36 
 37 #include "standardHandlers.h"
 38 
 39 /* HandlerFunction - Invoked from event_callback() */
 40 static void
 41 handleClassPrepare(JNIEnv *env, EventInfo *evinfo,
 42                    HandlerNode *node,
 43                    struct bag *eventBag)
 44 {
 45     jthread thread = evinfo->thread;
 46 
 47     /* We try hard to avoid class loads/prepares in debugger
 48      * threads, but it is still possible for them to happen (most
 49      * likely for exceptions that are thrown within JNI
 50      * methods). If such an event occurs, we must report it, but
 51      * we cannot suspend the debugger thread.
 52      *
 53      * 1) We report the thread as NULL because we don't want the
 54      *    application to get hold of a debugger thread object.
 55      * 2) We try to do the right thing wrt to suspending
 56      *    threads without suspending debugger threads. If the
 57      *    requested suspend policy is NONE, there's no problem. If
 58      *    the requested policy is ALL, we can just suspend all
 59      *    application threads without producing any surprising
 60      *    results by leaving the debugger thread running. However,
 61      *    if the requested policy is EVENT_THREAD, we are forced
 62      *    to do something different than requested. The most
 63      *    useful behavior is to suspend all application threads
 64      *    (just as if the policy was ALL). This allows the
 65      *    application to operate on the class before it gets into
 66      *    circulation and so it is preferable to the other
 67      *    alternative of suspending no threads.
 68      */
 69     if (threadControl_isDebugThread(thread)) {
 70         evinfo->thread = NULL;
 71         if (node->suspendPolicy == JDWP_SUSPEND_POLICY(EVENT_THREAD)) {
 72             node->suspendPolicy = JDWP_SUSPEND_POLICY(ALL);
 73         }
 74     }
 75     eventHelper_recordEvent(evinfo, node->handlerID,
 76                             node->suspendPolicy, eventBag);
 77 }
 78 
 79 /* HandlerFunction - Invoked from event_callback() */
 80 static void
 81 handleGarbageCollectionFinish(JNIEnv *env, EventInfo *evinfo,
 82                   HandlerNode *node,
 83                   struct bag *eventBag)
 84 {
 85     JDI_ASSERT_MSG(JNI_FALSE, "Should never call handleGarbageCollectionFinish");
 86 }
 87 
 88 /* HandlerFunction - Invoked from event_callback() for METHOD_ENTRY and METHOD_EXIT. */
 89 static void
 90 handleFrameEvent(JNIEnv *env, EventInfo *evinfo,
 91                  HandlerNode *node,
 92                  struct bag *eventBag)
 93 {
 94     /*
 95      * The frame id that comes with this event is very transient.
 96      * We can't send the frame to the helper thread because it
 97      * might be useless by the time the helper thread can use it
 98      * (if suspend policy is NONE). So, get the needed info from
 99      * the frame and then use a special command to the helper
100      * thread.
101      */
102 
103     jmethodID method;
104     jlocation location;
105     jvmtiError error;
106     FrameNumber fnum = 0;
107     jvalue returnValue;
108 
109     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation)
110             (gdata->jvmti, evinfo->thread, fnum, &method, &location);
111     if (error != JVMTI_ERROR_NONE) {
112         location = -1;
113     }
114     returnValue = evinfo->u.method_exit.return_value;
115 
116     eventHelper_recordFrameEvent(node->handlerID,
117                                  node->suspendPolicy,
118                                  evinfo->ei,
119                                  evinfo->thread,
120                                  evinfo->clazz,
121                                  evinfo->method,
122                                  location,
123                                  node->needReturnValue,
124                                  returnValue,
125                                  eventBag);
126 }
127 
128 /* HandlerFunction - Invoked from event_callback() */
129 static void
130 genericHandler(JNIEnv *env, EventInfo *evinfo,
131                HandlerNode *node,
132                struct bag *eventBag)
133 {
134     eventHelper_recordEvent(evinfo, node->handlerID, node->suspendPolicy,
135                             eventBag);
136 }
137 
138 HandlerFunction
139 standardHandlers_defaultHandler(EventIndex ei)
140 {
141     switch (ei) {
142         case EI_BREAKPOINT:
143         case EI_EXCEPTION:
144         case EI_FIELD_ACCESS:
145         case EI_FIELD_MODIFICATION:
146         case EI_SINGLE_STEP:
147         case EI_THREAD_START:
148         case EI_THREAD_END:
149         case EI_VM_DEATH:
150         case EI_MONITOR_CONTENDED_ENTER:
151         case EI_MONITOR_CONTENDED_ENTERED:
152         case EI_MONITOR_WAIT:
153         case EI_MONITOR_WAITED:
154             return &genericHandler;
155 
156         case EI_CLASS_PREPARE:
157             return &handleClassPrepare;
158 
159         case EI_GC_FINISH:
160             return &handleGarbageCollectionFinish;
161 
162         case EI_METHOD_ENTRY:
163         case EI_METHOD_EXIT:
164             return &handleFrameEvent;
165 
166         default:
167             /* This NULL will trigger a AGENT_ERROR_INVALID_EVENT_TYPE */
168             return NULL;
169     }
170 }
171 
172 void
173 standardHandlers_onConnect(void)
174 {
175     jboolean installed;
176 
177     /* always report VMDeath to a connected debugger */
178     installed = (eventHandler_createPermanentInternal(
179                         EI_VM_DEATH, genericHandler) != NULL);
180     if (!installed) {
181         EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"Unable to install VM Death event handler");
182     }
183 }
184 
185 void
186 standardHandlers_onDisconnect(void)
187 {
188 }