< prev index next >

src/jdk.jdwp.agent/share/native/libjdwp/standardHandlers.c

Print this page


   1 /*
   2  * Copyright (c) 2001, 2005, 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 static void
  40 handleClassPrepare(JNIEnv *env, EventInfo *evinfo,
  41                    HandlerNode *node,
  42                    struct bag *eventBag)
  43 {
  44     jthread thread = evinfo->thread;
  45 
  46     /* We try hard to avoid class loads/prepares in debugger
  47      * threads, but it is still possible for them to happen (most
  48      * likely for exceptions that are thrown within JNI
  49      * methods). If such an event occurs, we must report it, but
  50      * we cannot suspend the debugger thread.
  51      *
  52      * 1) We report the thread as NULL because we don't want the
  53      *    application to get hold of a debugger thread object.
  54      * 2) We try to do the right thing wrt to suspending
  55      *    threads without suspending debugger threads. If the
  56      *    requested suspend policy is NONE, there's no problem. If
  57      *    the requested policy is ALL, we can just suspend all
  58      *    application threads without producing any surprising
  59      *    results by leaving the debugger thread running. However,
  60      *    if the requested policy is EVENT_THREAD, we are forced
  61      *    to do something different than requested. The most
  62      *    useful behavior is to suspend all application threads
  63      *    (just as if the policy was ALL). This allows the
  64      *    application to operate on the class before it gets into
  65      *    circulation and so it is preferable to the other
  66      *    alternative of suspending no threads.
  67      */
  68     if (threadControl_isDebugThread(thread)) {
  69         evinfo->thread = NULL;
  70         if (node->suspendPolicy == JDWP_SUSPEND_POLICY(EVENT_THREAD)) {
  71             node->suspendPolicy = JDWP_SUSPEND_POLICY(ALL);
  72         }
  73     }
  74     eventHelper_recordEvent(evinfo, node->handlerID,
  75                             node->suspendPolicy, eventBag);
  76 }
  77 

  78 static void
  79 handleGarbageCollectionFinish(JNIEnv *env, EventInfo *evinfo,
  80                   HandlerNode *node,
  81                   struct bag *eventBag)
  82 {
  83     JDI_ASSERT_MSG(JNI_FALSE, "Should never call handleGarbageCollectionFinish");
  84 }
  85 

  86 static void
  87 handleFrameEvent(JNIEnv *env, EventInfo *evinfo,
  88                  HandlerNode *node,
  89                  struct bag *eventBag)
  90 {
  91     /*
  92      * The frame id that comes with this event is very transient.
  93      * We can't send the frame to the helper thread because it
  94      * might be useless by the time the helper thread can use it
  95      * (if suspend policy is NONE). So, get the needed info from
  96      * the frame and then use a special command to the helper
  97      * thread.
  98      */
  99 
 100     jmethodID method;
 101     jlocation location;
 102     jvmtiError error;
 103     FrameNumber fnum = 0;
 104     jvalue returnValue;
 105 
 106     error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation)
 107             (gdata->jvmti, evinfo->thread, fnum, &method, &location);
 108     if (error != JVMTI_ERROR_NONE) {
 109         location = -1;
 110     }
 111     returnValue = evinfo->u.method_exit.return_value;
 112 
 113     eventHelper_recordFrameEvent(node->handlerID,
 114                                  node->suspendPolicy,
 115                                  evinfo->ei,
 116                                  evinfo->thread,
 117                                  evinfo->clazz,
 118                                  evinfo->method,
 119                                  location,
 120                                  node->needReturnValue,
 121                                  returnValue,
 122                                  eventBag);
 123 }
 124 

 125 static void
 126 genericHandler(JNIEnv *env, EventInfo *evinfo,
 127                HandlerNode *node,
 128                struct bag *eventBag)
 129 {
 130     eventHelper_recordEvent(evinfo, node->handlerID, node->suspendPolicy,
 131                             eventBag);
 132 }
 133 
 134 HandlerFunction
 135 standardHandlers_defaultHandler(EventIndex ei)
 136 {
 137     switch (ei) {
 138         case EI_BREAKPOINT:
 139         case EI_EXCEPTION:
 140         case EI_FIELD_ACCESS:
 141         case EI_FIELD_MODIFICATION:
 142         case EI_SINGLE_STEP:
 143         case EI_THREAD_START:
 144         case EI_THREAD_END:
 145         case EI_VM_DEATH:
 146         case EI_MONITOR_CONTENDED_ENTER:
 147         case EI_MONITOR_CONTENDED_ENTERED:
 148         case EI_MONITOR_WAIT:
 149         case EI_MONITOR_WAITED:






 150             return &genericHandler;
 151 
 152         case EI_CLASS_PREPARE:
 153             return &handleClassPrepare;
 154 
 155         case EI_GC_FINISH:
 156             return &handleGarbageCollectionFinish;
 157 
 158         case EI_METHOD_ENTRY:
 159         case EI_METHOD_EXIT:
 160             return &handleFrameEvent;
 161 
 162         default:
 163             /* This NULL will trigger a AGENT_ERROR_INVALID_EVENT_TYPE */
 164             return NULL;
 165     }
 166 }
 167 
 168 void
 169 standardHandlers_onConnect(void)
   1 /*
   2  * Copyright (c) 2001, 2019, 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() */
  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         case EI_FIBER_SCHEDULED:
 155         case EI_FIBER_TERMINATED:
 156         case EI_FIBER_MOUNT:
 157         case EI_FIBER_UNMOUNT:
 158         case EI_CONTINUATION_RUN:
 159         case EI_CONTINUATION_YIELD:
 160             return &genericHandler;
 161 
 162         case EI_CLASS_PREPARE:
 163             return &handleClassPrepare;
 164 
 165         case EI_GC_FINISH:
 166             return &handleGarbageCollectionFinish;
 167 
 168         case EI_METHOD_ENTRY:
 169         case EI_METHOD_EXIT:
 170             return &handleFrameEvent;
 171 
 172         default:
 173             /* This NULL will trigger a AGENT_ERROR_INVALID_EVENT_TYPE */
 174             return NULL;
 175     }
 176 }
 177 
 178 void
 179 standardHandlers_onConnect(void)
< prev index next >