0N/A/*
2362N/A * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "util.h"
0N/A#include "eventHandler.h"
0N/A#include "threadControl.h"
0N/A#include "commonRef.h"
0N/A#include "eventHelper.h"
0N/A#include "stepControl.h"
0N/A#include "invoker.h"
0N/A#include "bag.h"
0N/A
0N/A#define HANDLING_EVENT(node) ((node)->current_ei != 0)
0N/A
0N/A/*
0N/A * Collection of info for properly handling co-located events.
0N/A * If the ei field is non-zero, then one of the possible
0N/A * co-located events has been posted and the other fields describe
0N/A * the event's location.
0N/A */
0N/Atypedef struct CoLocatedEventInfo_ {
0N/A EventIndex ei;
0N/A jclass clazz;
0N/A jmethodID method;
0N/A jlocation location;
0N/A} CoLocatedEventInfo;
0N/A
0N/A/**
0N/A * The main data structure in threadControl is the ThreadNode.
0N/A * This is a per-thread structure that is allocated on the
0N/A * first event that occurs in a thread. It is freed after the
0N/A * thread's thread end event has completed processing. The
0N/A * structure contains state information on its thread including
0N/A * suspend counts. It also acts as a repository for other
0N/A * per-thread state such as the current method invocation or
0N/A * current step.
0N/A *
0N/A * suspendCount is the number of outstanding suspends
0N/A * from the debugger. suspends from the app itself are
0N/A * not included in this count.
0N/A */
0N/Atypedef struct ThreadNode {
0N/A jthread thread;
0N/A unsigned int toBeResumed : 1;
0N/A unsigned int pendingInterrupt : 1;
0N/A unsigned int isDebugThread : 1;
0N/A unsigned int suspendOnStart : 1;
0N/A unsigned int isStarted : 1;
0N/A unsigned int popFrameEvent : 1;
0N/A unsigned int popFrameProceed : 1;
0N/A unsigned int popFrameThread : 1;
0N/A EventIndex current_ei;
0N/A jobject pendingStop;
0N/A jint suspendCount;
0N/A jint resumeFrameDepth; /* !=0 => This thread is in a call to Thread.resume() */
0N/A jvmtiEventMode instructionStepMode;
0N/A StepRequest currentStep;
0N/A InvokeRequest currentInvoke;
0N/A struct bag *eventBag;
0N/A CoLocatedEventInfo cleInfo;
0N/A struct ThreadNode *next;
0N/A struct ThreadNode *prev;
0N/A jlong frameGeneration;
0N/A struct ThreadList *list; /* Tells us what list this thread is in */
0N/A} ThreadNode;
0N/A
0N/Astatic jint suspendAllCount;
0N/A
0N/Atypedef struct ThreadList {
0N/A ThreadNode *first;
0N/A} ThreadList;
0N/A
0N/A/*
0N/A * popFrameEventLock is used to notify that the event has been received
0N/A */
0N/Astatic jrawMonitorID popFrameEventLock = NULL;
0N/A
0N/A/*
0N/A * popFrameProceedLock is used to assure that the event thread is
0N/A * re-suspended immediately after the event is acknowledged.
0N/A */
0N/Astatic jrawMonitorID popFrameProceedLock = NULL;
0N/A
0N/Astatic jrawMonitorID threadLock;
0N/Astatic jlocation resumeLocation;
0N/Astatic HandlerNode *breakpointHandlerNode;
0N/Astatic HandlerNode *framePopHandlerNode;
0N/Astatic HandlerNode *catchHandlerNode;
0N/A
0N/Astatic jvmtiError threadControl_removeDebugThread(jthread thread);
0N/A
0N/A/*
0N/A * Threads which have issued thread start events and not yet issued thread
0N/A * end events are maintained in the "runningThreads" list. All other threads known
0N/A * to this module are kept in the "otherThreads" list.
0N/A */
0N/Astatic ThreadList runningThreads;
0N/Astatic ThreadList otherThreads;
0N/A
0N/A#define MAX_DEBUG_THREADS 10
0N/Astatic int debugThreadCount;
0N/Astatic jthread debugThreads[MAX_DEBUG_THREADS];
0N/A
0N/Atypedef struct DeferredEventMode {
0N/A EventIndex ei;
0N/A jvmtiEventMode mode;
0N/A jthread thread;
0N/A struct DeferredEventMode *next;
0N/A} DeferredEventMode;
0N/A
0N/Atypedef struct {
0N/A DeferredEventMode *first;
0N/A DeferredEventMode *last;
0N/A} DeferredEventModeList;
0N/A
0N/Astatic DeferredEventModeList deferredEventModes;
0N/A
0N/Astatic jint
0N/AgetStackDepth(jthread thread)
0N/A{
0N/A jint count = 0;
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameCount)
0N/A (gdata->jvmti, thread, &count);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A EXIT_ERROR(error, "getting frame count");
0N/A }
0N/A return count;
0N/A}
0N/A
0N/A/* Get the state of the thread direct from JVMTI */
0N/Astatic jvmtiError
0N/AthreadState(jthread thread, jint *pstate)
0N/A{
0N/A *pstate = 0;
0N/A return JVMTI_FUNC_PTR(gdata->jvmti,GetThreadState)
0N/A (gdata->jvmti, thread, pstate);
0N/A}
0N/A
0N/A/* Set TLS on a specific jthread to the ThreadNode* */
0N/Astatic void
0N/AsetThreadLocalStorage(jthread thread, ThreadNode *node)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SetThreadLocalStorage)
0N/A (gdata->jvmti, thread, (void*)node);
0N/A if ( error == JVMTI_ERROR_THREAD_NOT_ALIVE ) {
0N/A /* Just return, thread hasn't started yet */
0N/A return;
0N/A } else if ( error != JVMTI_ERROR_NONE ) {
0N/A /* The jthread object must be valid, so this must be a fatal error */
0N/A EXIT_ERROR(error, "cannot set thread local storage");
0N/A }
0N/A}
0N/A
0N/A/* Get TLS on a specific jthread, which is the ThreadNode* */
0N/Astatic ThreadNode *
0N/AgetThreadLocalStorage(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A ThreadNode *node;
0N/A
0N/A node = NULL;
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,GetThreadLocalStorage)
0N/A (gdata->jvmti, thread, (void**)&node);
0N/A if ( error == JVMTI_ERROR_THREAD_NOT_ALIVE ) {
0N/A /* Just return NULL, thread hasn't started yet */
0N/A return NULL;
0N/A } else if ( error != JVMTI_ERROR_NONE ) {
0N/A /* The jthread object must be valid, so this must be a fatal error */
0N/A EXIT_ERROR(error, "cannot get thread local storage");
0N/A }
0N/A return node;
0N/A}
0N/A
0N/A/* Search list for nodes that don't have TLS set and match this thread.
0N/A * It assumed that this logic is never dealing with terminated threads,
0N/A * since the ThreadEnd events always delete the ThreadNode while the
0N/A * jthread is still alive. So we can only look at the ThreadNode's that
0N/A * have never had their TLS set, making the search much faster.
0N/A * But keep in mind, this kind of search should rarely be needed.
0N/A */
0N/Astatic ThreadNode *
0N/AnonTlsSearch(JNIEnv *env, ThreadList *list, jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A for (node = list->first; node != NULL; node = node->next) {
0N/A if (isSameObject(env, node->thread, thread)) {
0N/A break;
0N/A }
0N/A }
0N/A return node;
0N/A}
0N/A
0N/A/*
0N/A * These functions maintain the linked list of currently running threads.
0N/A * All assume that the threadLock is held before calling.
0N/A * If list==NULL, search both lists.
0N/A */
0N/Astatic ThreadNode *
0N/AfindThread(ThreadList *list, jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A /* Get thread local storage for quick thread -> node access */
0N/A node = getThreadLocalStorage(thread);
0N/A
0N/A /* In some rare cases we might get NULL, so we check the list manually for
0N/A * any threads that we could match.
0N/A */
0N/A if ( node == NULL ) {
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A if ( list != NULL ) {
0N/A node = nonTlsSearch(env, list, thread);
0N/A } else {
0N/A node = nonTlsSearch(env, &runningThreads, thread);
0N/A if ( node == NULL ) {
0N/A node = nonTlsSearch(env, &otherThreads, thread);
0N/A }
0N/A }
0N/A if ( node != NULL ) {
0N/A /* Here we make another attempt to set TLS, it's ok if this fails */
0N/A setThreadLocalStorage(thread, (void*)node);
0N/A }
0N/A }
0N/A
0N/A /* If a list is supplied, only return ones in this list */
0N/A if ( node != NULL && list != NULL && node->list != list ) {
0N/A return NULL;
0N/A }
0N/A return node;
0N/A}
0N/A
0N/A/* Remove a ThreadNode from a ThreadList */
0N/Astatic void
0N/AremoveNode(ThreadList *list, ThreadNode *node)
0N/A{
0N/A ThreadNode *prev;
0N/A ThreadNode *next;
0N/A
0N/A prev = node->prev;
0N/A next = node->next;
0N/A if ( prev != NULL ) {
0N/A prev->next = next;
0N/A }
0N/A if ( next != NULL ) {
0N/A next->prev = prev;
0N/A }
0N/A if ( prev == NULL ) {
0N/A list->first = next;
0N/A }
0N/A node->next = NULL;
0N/A node->prev = NULL;
0N/A node->list = NULL;
0N/A}
0N/A
0N/A/* Add a ThreadNode to a ThreadList */
0N/Astatic void
0N/AaddNode(ThreadList *list, ThreadNode *node)
0N/A{
0N/A node->next = NULL;
0N/A node->prev = NULL;
0N/A node->list = NULL;
0N/A if ( list->first == NULL ) {
0N/A list->first = node;
0N/A } else {
0N/A list->first->prev = node;
0N/A node->next = list->first;
0N/A list->first = node;
0N/A }
0N/A node->list = list;
0N/A}
0N/A
0N/Astatic ThreadNode *
0N/AinsertThread(JNIEnv *env, ThreadList *list, jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A struct bag *eventBag;
0N/A
0N/A node = findThread(list, thread);
0N/A if (node == NULL) {
0N/A node = jvmtiAllocate(sizeof(*node));
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"thread table entry");
0N/A return NULL;
0N/A }
0N/A (void)memset(node, 0, sizeof(*node));
0N/A eventBag = eventHelper_createEventBag();
0N/A if (eventBag == NULL) {
0N/A jvmtiDeallocate(node);
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"thread table entry");
0N/A return NULL;
0N/A }
0N/A
0N/A /*
0N/A * Init all flags false, all refs NULL, all counts 0
0N/A */
0N/A
0N/A saveGlobalRef(env, thread, &(node->thread));
0N/A if (node->thread == NULL) {
0N/A jvmtiDeallocate(node);
0N/A bagDestroyBag(eventBag);
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"thread table entry");
0N/A return NULL;
0N/A }
0N/A /*
0N/A * Remember if it is a debug thread
0N/A */
0N/A if (threadControl_isDebugThread(node->thread)) {
0N/A node->isDebugThread = JNI_TRUE;
0N/A } else if (suspendAllCount > 0){
0N/A /*
0N/A * If there is a pending suspendAll, all new threads should
0N/A * be initialized as if they were suspended by the suspendAll,
0N/A * and the thread will need to be suspended when it starts.
0N/A */
0N/A node->suspendCount = suspendAllCount;
0N/A node->suspendOnStart = JNI_TRUE;
0N/A }
0N/A node->current_ei = 0;
0N/A node->instructionStepMode = JVMTI_DISABLE;
0N/A node->eventBag = eventBag;
0N/A addNode(list, node);
0N/A
0N/A /* Set thread local storage for quick thread -> node access.
0N/A * Some threads may not be in a state that allows setting of TLS,
0N/A * which is ok, see findThread, it deals with threads without TLS set.
0N/A */
0N/A setThreadLocalStorage(node->thread, (void*)node);
0N/A }
0N/A
0N/A return node;
0N/A}
0N/A
0N/Astatic void
0N/AclearThread(JNIEnv *env, ThreadNode *node)
0N/A{
0N/A if (node->pendingStop != NULL) {
0N/A tossGlobalRef(env, &(node->pendingStop));
0N/A }
0N/A stepControl_clearRequest(node->thread, &node->currentStep);
0N/A if (node->isDebugThread) {
0N/A (void)threadControl_removeDebugThread(node->thread);
0N/A }
0N/A /* Clear out TLS on this thread (just a cleanup action) */
0N/A setThreadLocalStorage(node->thread, NULL);
0N/A tossGlobalRef(env, &(node->thread));
0N/A bagDestroyBag(node->eventBag);
0N/A jvmtiDeallocate(node);
0N/A}
0N/A
0N/Astatic void
0N/AremoveThread(JNIEnv *env, ThreadList *list, jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(list, thread);
0N/A if (node != NULL) {
0N/A removeNode(list, node);
0N/A clearThread(env, node);
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AremoveResumed(JNIEnv *env, ThreadList *list)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A node = list->first;
0N/A while (node != NULL) {
0N/A ThreadNode *temp = node->next;
0N/A if (node->suspendCount == 0) {
0N/A removeThread(env, list, node->thread);
0N/A }
0N/A node = temp;
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AmoveNode(ThreadList *source, ThreadList *dest, ThreadNode *node)
0N/A{
0N/A removeNode(source, node);
0N/A JDI_ASSERT(findThread(dest, node->thread) == NULL);
0N/A addNode(dest, node);
0N/A}
0N/A
0N/Atypedef jvmtiError (*ThreadEnumerateFunction)(JNIEnv *, ThreadNode *, void *);
0N/A
0N/Astatic jvmtiError
0N/AenumerateOverThreadList(JNIEnv *env, ThreadList *list,
0N/A ThreadEnumerateFunction function, void *arg)
0N/A{
0N/A ThreadNode *node;
0N/A jvmtiError error = JVMTI_ERROR_NONE;
0N/A
0N/A for (node = list->first; node != NULL; node = node->next) {
0N/A error = (*function)(env, node, arg);
0N/A if ( error != JVMTI_ERROR_NONE ) {
0N/A break;
0N/A }
0N/A }
0N/A return error;
0N/A}
0N/A
0N/Astatic void
0N/AinsertEventMode(DeferredEventModeList *list, DeferredEventMode *eventMode)
0N/A{
0N/A if (list->last != NULL) {
0N/A list->last->next = eventMode;
0N/A } else {
0N/A list->first = eventMode;
0N/A }
0N/A list->last = eventMode;
0N/A}
0N/A
0N/Astatic void
0N/AremoveEventMode(DeferredEventModeList *list, DeferredEventMode *eventMode, DeferredEventMode *prev)
0N/A{
0N/A if (prev == NULL) {
0N/A list->first = eventMode->next;
0N/A } else {
0N/A prev->next = eventMode->next;
0N/A }
0N/A if (eventMode->next == NULL) {
0N/A list->last = prev;
0N/A }
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AaddDeferredEventMode(JNIEnv *env, jvmtiEventMode mode, EventIndex ei, jthread thread)
0N/A{
0N/A DeferredEventMode *eventMode;
0N/A
0N/A /*LINTED*/
0N/A eventMode = jvmtiAllocate((jint)sizeof(DeferredEventMode));
0N/A if (eventMode == NULL) {
0N/A return AGENT_ERROR_OUT_OF_MEMORY;
0N/A }
0N/A eventMode->thread = NULL;
0N/A saveGlobalRef(env, thread, &(eventMode->thread));
0N/A eventMode->mode = mode;
0N/A eventMode->ei = ei;
0N/A eventMode->next = NULL;
0N/A insertEventMode(&deferredEventModes, eventMode);
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/Astatic void
0N/AfreeDeferredEventModes(JNIEnv *env)
0N/A{
0N/A DeferredEventMode *eventMode;
0N/A eventMode = deferredEventModes.first;
0N/A while (eventMode != NULL) {
0N/A DeferredEventMode *next;
0N/A next = eventMode->next;
0N/A tossGlobalRef(env, &(eventMode->thread));
0N/A jvmtiDeallocate(eventMode);
0N/A eventMode = next;
0N/A }
0N/A deferredEventModes.first = NULL;
0N/A deferredEventModes.last = NULL;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AthreadSetEventNotificationMode(ThreadNode *node,
0N/A jvmtiEventMode mode, EventIndex ei, jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A /* record single step mode */
0N/A if (ei == EI_SINGLE_STEP) {
0N/A node->instructionStepMode = mode;
0N/A }
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventNotificationMode)
0N/A (gdata->jvmti, mode, eventIndex2jvmti(ei), thread);
0N/A return error;
0N/A}
0N/A
0N/Astatic void
0N/AprocessDeferredEventModes(JNIEnv *env, jthread thread, ThreadNode *node)
0N/A{
0N/A jvmtiError error;
0N/A DeferredEventMode *eventMode;
0N/A DeferredEventMode *prev;
0N/A
0N/A prev = NULL;
0N/A eventMode = deferredEventModes.first;
0N/A while (eventMode != NULL) {
0N/A DeferredEventMode *next = eventMode->next;
0N/A if (isSameObject(env, thread, eventMode->thread)) {
0N/A error = threadSetEventNotificationMode(node,
0N/A eventMode->mode, eventMode->ei, eventMode->thread);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A EXIT_ERROR(error, "cannot process deferred thread event notifications at thread start");
0N/A }
0N/A removeEventMode(&deferredEventModes, eventMode, prev);
0N/A tossGlobalRef(env, &(eventMode->thread));
0N/A jvmtiDeallocate(eventMode);
0N/A } else {
0N/A prev = eventMode;
0N/A }
0N/A eventMode = next;
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AgetLocks(void)
0N/A{
0N/A /*
0N/A * Anything which might be locked as part of the handling of
0N/A * a JVMTI event (which means: might be locked by an application
0N/A * thread) needs to be grabbed here. This allows thread control
0N/A * code to safely suspend and resume the application threads
0N/A * while ensuring they don't hold a critical lock.
0N/A */
0N/A
0N/A eventHandler_lock();
0N/A invoker_lock();
0N/A eventHelper_lock();
0N/A stepControl_lock();
0N/A commonRef_lock();
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A}
0N/A
0N/Astatic void
0N/AreleaseLocks(void)
0N/A{
0N/A debugMonitorExit(threadLock);
0N/A commonRef_unlock();
0N/A stepControl_unlock();
0N/A eventHelper_unlock();
0N/A invoker_unlock();
0N/A eventHandler_unlock();
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_initialize(void)
0N/A{
0N/A jlocation unused;
0N/A jvmtiError error;
0N/A
0N/A suspendAllCount = 0;
0N/A runningThreads.first = NULL;
0N/A otherThreads.first = NULL;
0N/A debugThreadCount = 0;
0N/A threadLock = debugMonitorCreate("JDWP Thread Lock");
0N/A if (gdata->threadClass==NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER, "no java.lang.thread class");
0N/A }
0N/A if (gdata->threadResume==0) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER, "cannot resume thread");
0N/A }
0N/A /* Get the java.lang.Thread.resume() method beginning location */
0N/A error = methodLocation(gdata->threadResume, &resumeLocation, &unused);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A EXIT_ERROR(error, "getting method location");
0N/A }
0N/A}
0N/A
0N/Astatic jthread
0N/AgetResumee(jthread resumingThread)
0N/A{
0N/A jthread resumee = NULL;
0N/A jvmtiError error;
0N/A jobject object;
0N/A FrameNumber fnum = 0;
0N/A
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,GetLocalObject)
0N/A (gdata->jvmti, resumingThread, fnum, 0, &object);
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A resumee = object;
0N/A }
0N/A return resumee;
0N/A}
0N/A
0N/A
0N/Astatic jboolean
0N/ApendingAppResume(jboolean includeSuspended)
0N/A{
0N/A ThreadList *list;
0N/A ThreadNode *node;
0N/A
0N/A list = &runningThreads;
0N/A node = list->first;
0N/A while (node != NULL) {
0N/A if (node->resumeFrameDepth > 0) {
0N/A if (includeSuspended) {
0N/A return JNI_TRUE;
0N/A } else {
0N/A jvmtiError error;
0N/A jint state;
0N/A
0N/A error = threadState(node->thread, &state);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A EXIT_ERROR(error, "getting thread state");
0N/A }
0N/A if (!(state & JVMTI_THREAD_STATE_SUSPENDED)) {
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A }
0N/A node = node->next;
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/Astatic void
0N/AnotifyAppResumeComplete(void)
0N/A{
0N/A debugMonitorNotifyAll(threadLock);
0N/A if (!pendingAppResume(JNI_TRUE)) {
0N/A if (framePopHandlerNode != NULL) {
0N/A (void)eventHandler_free(framePopHandlerNode);
0N/A framePopHandlerNode = NULL;
0N/A }
0N/A if (catchHandlerNode != NULL) {
0N/A (void)eventHandler_free(catchHandlerNode);
0N/A catchHandlerNode = NULL;
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AhandleAppResumeCompletion(JNIEnv *env, EventInfo *evinfo,
0N/A HandlerNode *handlerNode,
0N/A struct bag *eventBag)
0N/A{
0N/A ThreadNode *node;
0N/A jthread thread;
0N/A
0N/A thread = evinfo->thread;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A if (node->resumeFrameDepth > 0) {
0N/A jint compareDepth = getStackDepth(thread);
0N/A if (evinfo->ei == EI_FRAME_POP) {
0N/A compareDepth--;
0N/A }
0N/A if (compareDepth < node->resumeFrameDepth) {
0N/A node->resumeFrameDepth = 0;
0N/A notifyAppResumeComplete();
0N/A }
0N/A }
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Astatic void
0N/AblockOnDebuggerSuspend(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node != NULL) {
0N/A while (node && node->suspendCount > 0) {
0N/A debugMonitorWait(threadLock);
0N/A node = findThread(NULL, thread);
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AtrackAppResume(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A FrameNumber fnum;
0N/A ThreadNode *node;
0N/A
0N/A fnum = 0;
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A JDI_ASSERT(node->resumeFrameDepth == 0);
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,NotifyFramePop)
0N/A (gdata->jvmti, thread, fnum);
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A jint frameDepth = getStackDepth(thread);
0N/A if ((frameDepth > 0) && (framePopHandlerNode == NULL)) {
0N/A framePopHandlerNode = eventHandler_createInternalThreadOnly(
0N/A EI_FRAME_POP,
0N/A handleAppResumeCompletion,
0N/A thread);
0N/A catchHandlerNode = eventHandler_createInternalThreadOnly(
0N/A EI_EXCEPTION_CATCH,
0N/A handleAppResumeCompletion,
0N/A thread);
0N/A if ((framePopHandlerNode == NULL) ||
0N/A (catchHandlerNode == NULL)) {
0N/A (void)eventHandler_free(framePopHandlerNode);
0N/A framePopHandlerNode = NULL;
0N/A (void)eventHandler_free(catchHandlerNode);
0N/A catchHandlerNode = NULL;
0N/A }
0N/A }
0N/A if ((framePopHandlerNode != NULL) &&
0N/A (catchHandlerNode != NULL) &&
0N/A (frameDepth > 0)) {
0N/A node->resumeFrameDepth = frameDepth;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/AhandleAppResumeBreakpoint(JNIEnv *env, EventInfo *evinfo,
0N/A HandlerNode *handlerNode,
0N/A struct bag *eventBag)
0N/A{
0N/A jthread resumer = evinfo->thread;
0N/A jthread resumee = getResumee(resumer);
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A if (resumee != NULL) {
0N/A /*
0N/A * Hold up any attempt to resume as long as the debugger
0N/A * has suspended the resumee.
0N/A */
0N/A blockOnDebuggerSuspend(resumee);
0N/A }
0N/A
0N/A if (resumer != NULL) {
0N/A /*
0N/A * Track the resuming thread by marking it as being within
0N/A * a resume and by setting up for notification on
0N/A * a frame pop or exception. We won't allow the debugger
0N/A * to suspend threads while any thread is within a
0N/A * call to resume. This (along with the block above)
0N/A * ensures that when the debugger
0N/A * suspends a thread it will remain suspended.
0N/A */
0N/A trackAppResume(resumer);
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_onConnect(void)
0N/A{
0N/A breakpointHandlerNode = eventHandler_createInternalBreakpoint(
0N/A handleAppResumeBreakpoint, NULL,
0N/A gdata->threadClass, gdata->threadResume, resumeLocation);
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_onDisconnect(void)
0N/A{
0N/A if (breakpointHandlerNode != NULL) {
0N/A (void)eventHandler_free(breakpointHandlerNode);
0N/A breakpointHandlerNode = NULL;
0N/A }
0N/A if (framePopHandlerNode != NULL) {
0N/A (void)eventHandler_free(framePopHandlerNode);
0N/A framePopHandlerNode = NULL;
0N/A }
0N/A if (catchHandlerNode != NULL) {
0N/A (void)eventHandler_free(catchHandlerNode);
0N/A catchHandlerNode = NULL;
0N/A }
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_onHook(void)
0N/A{
0N/A /*
0N/A * As soon as the event hook is in place, we need to initialize
0N/A * the thread list with already-existing threads. The threadLock
0N/A * has been held since initialize, so we don't need to worry about
0N/A * insertions or deletions from the event handlers while we do this
0N/A */
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A
0N/A /*
0N/A * Prevent any event processing until OnHook has been called
0N/A */
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A WITH_LOCAL_REFS(env, 1) {
0N/A
0N/A jint threadCount;
0N/A jthread *threads;
0N/A
0N/A threads = allThreads(&threadCount);
0N/A if (threads == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"thread table");
0N/A } else {
0N/A
0N/A int i;
0N/A
0N/A for (i = 0; i < threadCount; i++) {
0N/A ThreadNode *node;
0N/A jthread thread = threads[i];
0N/A node = insertThread(env, &runningThreads, thread);
0N/A
0N/A /*
0N/A * This is a tiny bit risky. We have to assume that the
0N/A * pre-existing threads have been started because we
0N/A * can't rely on a thread start event for them. The chances
0N/A * of a problem related to this are pretty slim though, and
0N/A * there's really no choice because without setting this flag
0N/A * there is no way to enable stepping and other events on
0N/A * the threads that already exist (e.g. the finalizer thread).
0N/A */
0N/A node->isStarted = JNI_TRUE;
0N/A }
0N/A }
0N/A
0N/A } END_WITH_LOCAL_REFS(env)
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AcommonSuspendByNode(ThreadNode *node)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A LOG_MISC(("thread=%p suspended", node->thread));
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SuspendThread)
0N/A (gdata->jvmti, node->thread);
0N/A
0N/A /*
0N/A * Mark for resume only if suspend succeeded
0N/A */
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A node->toBeResumed = JNI_TRUE;
0N/A }
0N/A
0N/A /*
0N/A * If the thread was suspended by another app thread,
0N/A * do nothing and report no error (we won't resume it later).
0N/A */
0N/A if (error == JVMTI_ERROR_THREAD_SUSPENDED) {
0N/A error = JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A return error;
0N/A}
0N/A
0N/A/*
0N/A * Deferred suspends happen when the suspend is attempted on a thread
0N/A * that is not started. Bookkeeping (suspendCount,etc.)
0N/A * is handled by the original request, and once the thread actually
0N/A * starts, an actual suspend is attempted. This function does the
0N/A * deferred suspend without changing the bookkeeping that is already
0N/A * in place.
0N/A */
0N/Astatic jint
0N/AdeferredSuspendThreadByNode(ThreadNode *node)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_ERROR_NONE;
0N/A if (node->isDebugThread) {
0N/A /* Ignore requests for suspending debugger threads */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*
0N/A * Do the actual suspend only if a subsequent resume hasn't
0N/A * made it irrelevant.
0N/A */
0N/A if (node->suspendCount > 0) {
0N/A error = commonSuspendByNode(node);
0N/A
0N/A /*
0N/A * Attempt to clean up from any error by decrementing the
0N/A * suspend count. This compensates for the increment that
0N/A * happens when suspendOnStart is set to true.
0N/A */
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A node->suspendCount--;
0N/A }
0N/A }
0N/A
0N/A node->suspendOnStart = JNI_FALSE;
0N/A
0N/A debugMonitorNotifyAll(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AsuspendThreadByNode(ThreadNode *node)
0N/A{
0N/A jvmtiError error = JVMTI_ERROR_NONE;
0N/A if (node->isDebugThread) {
0N/A /* Ignore requests for suspending debugger threads */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*
0N/A * Just increment the suspend count if we are waiting
0N/A * for a deferred suspend.
0N/A */
0N/A if (node->suspendOnStart) {
0N/A node->suspendCount++;
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A if (node->suspendCount == 0) {
0N/A error = commonSuspendByNode(node);
0N/A
0N/A if (error == JVMTI_ERROR_THREAD_NOT_ALIVE) {
0N/A /*
0N/A * This error means that the thread is either a zombie or not yet
0N/A * started. In either case, we ignore the error. If the thread
0N/A * is a zombie, suspend/resume are no-ops. If the thread is not
0N/A * started, it will be suspended for real during the processing
0N/A * of its thread start event.
0N/A */
0N/A node->suspendOnStart = JNI_TRUE;
0N/A error = JVMTI_ERROR_NONE;
0N/A }
0N/A }
0N/A
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A node->suspendCount++;
0N/A }
0N/A
0N/A debugMonitorNotifyAll(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AresumeThreadByNode(ThreadNode *node)
0N/A{
0N/A jvmtiError error = JVMTI_ERROR_NONE;
0N/A
0N/A if (node->isDebugThread) {
0N/A /* never suspended by debugger => don't ever try to resume */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A if (node->suspendCount > 0) {
0N/A node->suspendCount--;
0N/A debugMonitorNotifyAll(threadLock);
0N/A if ((node->suspendCount == 0) && node->toBeResumed &&
0N/A !node->suspendOnStart) {
0N/A LOG_MISC(("thread=%p resumed", node->thread));
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,ResumeThread)
0N/A (gdata->jvmti, node->thread);
0N/A node->frameGeneration++; /* Increment on each resume */
0N/A node->toBeResumed = JNI_FALSE;
0N/A if (error == JVMTI_ERROR_THREAD_NOT_ALIVE && !node->isStarted) {
0N/A /*
0N/A * We successfully "suspended" this thread, but
0N/A * we never received a THREAD_START event for it.
0N/A * Since the thread never ran, we can ignore our
0N/A * failure to resume the thread.
0N/A */
0N/A error = JVMTI_ERROR_NONE;
0N/A }
0N/A }
0N/A }
0N/A
0N/A return error;
0N/A}
0N/A
0N/A/*
0N/A * Functions which respond to user requests to suspend/resume
0N/A * threads.
0N/A * Suspends and resumes add and subtract from a count respectively.
0N/A * The thread is only suspended when the count goes from 0 to 1 and
0N/A * resumed only when the count goes from 1 to 0.
0N/A *
0N/A * These functions suspend and resume application threads
0N/A * without changing the
0N/A * state of threads that were already suspended beforehand.
0N/A * They must not be called from an application thread because
0N/A * that thread may be suspended somewhere in the middle of things.
0N/A */
0N/Astatic void
0N/ApreSuspend(void)
0N/A{
0N/A getLocks(); /* Avoid debugger deadlocks */
0N/A
0N/A /*
0N/A * Delay any suspend while a call to java.lang.Thread.resume is in
0N/A * progress (not including those in suspended threads). The wait is
0N/A * timed because the threads suspended through
0N/A * java.lang.Thread.suspend won't result in a notify even though
0N/A * it may change the result of pendingAppResume()
0N/A */
0N/A while (pendingAppResume(JNI_FALSE)) {
0N/A /*
0N/A * This is ugly but we need to release the locks from getLocks
0N/A * or else the notify will never happen. The locks must be
0N/A * released and reacquired in the right order. else deadlocks
0N/A * can happen. It is possible that, during this dance, the
0N/A * notify will be missed, but since the wait needs to be timed
0N/A * anyway, it won't be a disaster. Note that this code will
0N/A * execute only on very rare occasions anyway.
0N/A */
0N/A releaseLocks();
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A debugMonitorTimedWait(threadLock, 1000);
0N/A debugMonitorExit(threadLock);
0N/A
0N/A getLocks();
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/ApostSuspend(void)
0N/A{
0N/A releaseLocks();
0N/A}
0N/A
0N/A/*
0N/A * This function must be called after preSuspend and before postSuspend.
0N/A */
0N/Astatic jvmtiError
0N/AcommonSuspend(JNIEnv *env, jthread thread, jboolean deferred)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A /*
0N/A * If the thread is not between its start and end events, we should
0N/A * still suspend it. To keep track of things, add the thread
0N/A * to a separate list of threads so that we'll resume it later.
0N/A */
0N/A node = findThread(&runningThreads, thread);
0N/A if (node == NULL) {
0N/A node = insertThread(env, &otherThreads, thread);
0N/A }
0N/A
0N/A if ( deferred ) {
0N/A return deferredSuspendThreadByNode(node);
0N/A } else {
0N/A return suspendThreadByNode(node);
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic jvmtiError
0N/AresumeCopyHelper(JNIEnv *env, ThreadNode *node, void *arg)
0N/A{
0N/A if (node->isDebugThread) {
0N/A /* never suspended by debugger => don't ever try to resume */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A if (node->suspendCount > 1) {
0N/A node->suspendCount--;
0N/A /* nested suspend so just undo one level */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*
0N/A * This thread was marked for suspension since its THREAD_START
0N/A * event came in during a suspendAll, but the helper hasn't
0N/A * completed the job yet. We decrement the count so the helper
0N/A * won't suspend this thread after we are done with the resumeAll.
0N/A * Another case to be handled here is when the debugger suspends
0N/A * the thread while the app has it suspended. In this case,
0N/A * the toBeResumed flag has been cleared indicating that
0N/A * the thread should not be resumed when the debugger does a resume.
0N/A * In this case, we also have to decrement the suspend count.
0N/A * If we don't then when the app resumes the thread and our Thread.resume
0N/A * bkpt handler is called, blockOnDebuggerSuspend will not resume
0N/A * the thread because suspendCount will be 1 meaning that the
0N/A * debugger has the thread suspended. See bug 6224859.
0N/A */
0N/A if (node->suspendCount == 1 && (!node->toBeResumed || node->suspendOnStart)) {
0N/A node->suspendCount--;
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A if (arg == NULL) {
0N/A /* nothing to hard resume so we're done */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*
0N/A * This is tricky. A suspendCount of 1 and toBeResumed means that
0N/A * JVM/DI SuspendThread() or JVM/DI SuspendThreadList() was called
0N/A * on this thread. The check for !suspendOnStart is paranoia that
0N/A * we inherited from resumeThreadByNode().
0N/A */
0N/A if (node->suspendCount == 1 && node->toBeResumed && !node->suspendOnStart) {
0N/A jthread **listPtr = (jthread **)arg;
0N/A
0N/A **listPtr = node->thread;
0N/A (*listPtr)++;
0N/A }
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/A
0N/Astatic jvmtiError
0N/AresumeCountHelper(JNIEnv *env, ThreadNode *node, void *arg)
0N/A{
0N/A if (node->isDebugThread) {
0N/A /* never suspended by debugger => don't ever try to resume */
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*
0N/A * This is tricky. A suspendCount of 1 and toBeResumed means that
0N/A * JVM/DI SuspendThread() or JVM/DI SuspendThreadList() was called
0N/A * on this thread. The check for !suspendOnStart is paranoia that
0N/A * we inherited from resumeThreadByNode().
0N/A */
0N/A if (node->suspendCount == 1 && node->toBeResumed && !node->suspendOnStart) {
0N/A jint *counter = (jint *)arg;
0N/A
0N/A (*counter)++;
0N/A }
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/Astatic void *
0N/AnewArray(jint length, size_t nbytes)
0N/A{
0N/A void *ptr;
0N/A ptr = jvmtiAllocate(length*(jint)nbytes);
0N/A if ( ptr != NULL ) {
0N/A (void)memset(ptr, 0, length*nbytes);
0N/A }
0N/A return ptr;
0N/A}
0N/A
0N/Astatic void
0N/AdeleteArray(void *ptr)
0N/A{
0N/A jvmtiDeallocate(ptr);
0N/A}
0N/A
0N/A/*
0N/A * This function must be called with the threadLock held.
0N/A *
0N/A * Two facts conspire to make this routine complicated:
0N/A *
0N/A * 1) the VM doesn't support nested external suspend
0N/A * 2) the original resumeAll code structure doesn't retrieve the
0N/A * entire thread list from JVMTI so we use the runningThreads
0N/A * list and two helpers to get the job done.
0N/A *
0N/A * Because we hold the threadLock, state seen by resumeCountHelper()
0N/A * is the same state seen in resumeCopyHelper(). resumeCountHelper()
0N/A * just counts up the number of threads to be hard resumed.
0N/A * resumeCopyHelper() does the accounting for nested suspends and
0N/A * special cases and, finally, populates the list of hard resume
0N/A * threads to be passed to ResumeThreadList().
0N/A *
0N/A * At first glance, you might think that the accounting could be done
0N/A * in resumeCountHelper(), but then resumeCopyHelper() would see
0N/A * "post-resume" state in the accounting values (suspendCount and
0N/A * toBeResumed) and would not be able to distinguish between a thread
0N/A * that needs a hard resume versus a thread that is already running.
0N/A */
0N/Astatic jvmtiError
0N/AcommonResumeList(JNIEnv *env)
0N/A{
0N/A jvmtiError error;
0N/A jint i;
0N/A jint reqCnt;
0N/A jthread *reqList;
0N/A jthread *reqPtr;
0N/A jvmtiError *results;
0N/A
0N/A reqCnt = 0;
0N/A
0N/A /* count number of threads to hard resume */
0N/A (void) enumerateOverThreadList(env, &runningThreads, resumeCountHelper,
0N/A &reqCnt);
0N/A if (reqCnt == 0) {
0N/A /* nothing to hard resume so do just the accounting part */
0N/A (void) enumerateOverThreadList(env, &runningThreads, resumeCopyHelper,
0N/A NULL);
0N/A return JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /*LINTED*/
0N/A reqList = newArray(reqCnt, sizeof(jthread));
0N/A if (reqList == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"resume request list");
0N/A }
0N/A /*LINTED*/
0N/A results = newArray(reqCnt, sizeof(jvmtiError));
0N/A if (results == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"resume list");
0N/A }
0N/A
0N/A /* copy the jthread values for threads to hard resume */
0N/A reqPtr = reqList;
0N/A (void) enumerateOverThreadList(env, &runningThreads, resumeCopyHelper,
0N/A &reqPtr);
0N/A
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,ResumeThreadList)
0N/A (gdata->jvmti, reqCnt, reqList, results);
0N/A for (i = 0; i < reqCnt; i++) {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(&runningThreads, reqList[i]);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_INVALID_THREAD,"missing entry in running thread table");
0N/A }
0N/A LOG_MISC(("thread=%p resumed as part of list", node->thread));
0N/A
0N/A /*
0N/A * resumeThreadByNode() assumes that JVM/DI ResumeThread()
0N/A * always works and does all the accounting updates. We do
0N/A * the same here. We also don't clear the error.
0N/A */
0N/A node->suspendCount--;
0N/A node->toBeResumed = JNI_FALSE;
0N/A node->frameGeneration++; /* Increment on each resume */
0N/A }
0N/A deleteArray(results);
0N/A deleteArray(reqList);
0N/A
0N/A debugMonitorNotifyAll(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * This function must be called after preSuspend and before postSuspend.
0N/A */
0N/Astatic jvmtiError
0N/AcommonSuspendList(JNIEnv *env, jint initCount, jthread *initList)
0N/A{
0N/A jvmtiError error;
0N/A jint i;
0N/A jint reqCnt;
0N/A jthread *reqList;
0N/A
0N/A error = JVMTI_ERROR_NONE;
0N/A reqCnt = 0;
0N/A reqList = newArray(initCount, sizeof(jthread));
0N/A if (reqList == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"request list");
0N/A }
0N/A
0N/A /*
0N/A * Go through the initial list and see if we have anything to suspend.
0N/A */
0N/A for (i = 0; i < initCount; i++) {
0N/A ThreadNode *node;
0N/A
0N/A /*
0N/A * If the thread is not between its start and end events, we should
0N/A * still suspend it. To keep track of things, add the thread
0N/A * to a separate list of threads so that we'll resume it later.
0N/A */
0N/A node = findThread(&runningThreads, initList[i]);
0N/A if (node == NULL) {
0N/A node = insertThread(env, &otherThreads, initList[i]);
0N/A }
0N/A
0N/A if (node->isDebugThread) {
0N/A /* Ignore requests for suspending debugger threads */
0N/A continue;
0N/A }
0N/A
0N/A /*
0N/A * Just increment the suspend count if we are waiting
0N/A * for a deferred suspend or if this is a nested suspend.
0N/A */
0N/A if (node->suspendOnStart || node->suspendCount > 0) {
0N/A node->suspendCount++;
0N/A continue;
0N/A }
0N/A
0N/A if (node->suspendCount == 0) {
0N/A /* thread is not suspended yet so put it on the request list */
0N/A reqList[reqCnt++] = initList[i];
0N/A }
0N/A }
0N/A
0N/A if (reqCnt > 0) {
0N/A jvmtiError *results = newArray(reqCnt, sizeof(jvmtiError));
0N/A
0N/A if (results == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"suspend list results");
0N/A }
0N/A
0N/A /*
0N/A * We have something to suspend so try to do it.
0N/A */
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SuspendThreadList)
0N/A (gdata->jvmti, reqCnt, reqList, results);
0N/A for (i = 0; i < reqCnt; i++) {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, reqList[i]);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_INVALID_THREAD,"missing entry in thread tables");
0N/A }
0N/A LOG_MISC(("thread=%p suspended as part of list", node->thread));
0N/A
0N/A if (results[i] == JVMTI_ERROR_NONE) {
0N/A /* thread was suspended as requested */
0N/A node->toBeResumed = JNI_TRUE;
0N/A } else if (results[i] == JVMTI_ERROR_THREAD_SUSPENDED) {
0N/A /*
0N/A * If the thread was suspended by another app thread,
0N/A * do nothing and report no error (we won't resume it later).
0N/A */
0N/A results[i] = JVMTI_ERROR_NONE;
0N/A } else if (results[i] == JVMTI_ERROR_THREAD_NOT_ALIVE) {
0N/A /*
0N/A * This error means that the suspend request failed
0N/A * because the thread is either a zombie or not yet
0N/A * started. In either case, we ignore the error. If the
0N/A * thread is a zombie, suspend/resume are no-ops. If the
0N/A * thread is not started, it will be suspended for real
0N/A * during the processing of its thread start event.
0N/A */
0N/A node->suspendOnStart = JNI_TRUE;
0N/A results[i] = JVMTI_ERROR_NONE;
0N/A }
0N/A
0N/A /* count real, app and deferred (suspendOnStart) suspensions */
0N/A if (results[i] == JVMTI_ERROR_NONE) {
0N/A node->suspendCount++;
0N/A }
0N/A }
0N/A deleteArray(results);
0N/A }
0N/A deleteArray(reqList);
0N/A
0N/A debugMonitorNotifyAll(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/A
0N/Astatic jvmtiError
0N/AcommonResume(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A ThreadNode *node;
0N/A
0N/A /*
0N/A * The thread is normally between its start and end events, but if
0N/A * not, check the auxiliary list used by threadControl_suspendThread.
0N/A */
0N/A node = findThread(NULL, thread);
0N/A
0N/A /*
0N/A * If the node is in neither list, the debugger never suspended
0N/A * this thread, so do nothing.
0N/A */
0N/A error = JVMTI_ERROR_NONE;
0N/A if (node != NULL) {
0N/A error = resumeThreadByNode(node);
0N/A }
0N/A return error;
0N/A}
0N/A
0N/A
0N/AjvmtiError
0N/AthreadControl_suspendThread(jthread thread, jboolean deferred)
0N/A{
0N/A jvmtiError error;
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A
0N/A log_debugee_location("threadControl_suspendThread()", thread, NULL, 0);
0N/A
0N/A preSuspend();
0N/A error = commonSuspend(env, thread, deferred);
0N/A postSuspend();
0N/A
0N/A return error;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_resumeThread(jthread thread, jboolean do_unblock)
0N/A{
0N/A jvmtiError error;
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A
0N/A log_debugee_location("threadControl_resumeThread()", thread, NULL, 0);
0N/A
0N/A eventHandler_lock(); /* for proper lock order */
0N/A debugMonitorEnter(threadLock);
0N/A error = commonResume(thread);
0N/A removeResumed(env, &otherThreads);
0N/A debugMonitorExit(threadLock);
0N/A eventHandler_unlock();
0N/A
0N/A if (do_unblock) {
0N/A /* let eventHelper.c: commandLoop() know we resumed one thread */
0N/A unblockCommandLoop();
0N/A }
0N/A
0N/A return error;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_suspendCount(jthread thread, jint *count)
0N/A{
0N/A jvmtiError error;
0N/A ThreadNode *node;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node == NULL) {
0N/A node = findThread(&otherThreads, thread);
0N/A }
0N/A
0N/A error = JVMTI_ERROR_NONE;
0N/A if (node != NULL) {
0N/A *count = node->suspendCount;
0N/A } else {
0N/A /*
0N/A * If the node is in neither list, the debugger never suspended
0N/A * this thread, so the suspend count is 0.
0N/A */
0N/A *count = 0;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/Astatic jboolean
0N/Acontains(JNIEnv *env, jthread *list, jint count, jthread item)
0N/A{
0N/A int i;
0N/A
0N/A for (i = 0; i < count; i++) {
0N/A if (isSameObject(env, list[i], item)) {
0N/A return JNI_TRUE;
0N/A }
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/A
0N/Atypedef struct {
0N/A jthread *list;
0N/A jint count;
0N/A} SuspendAllArg;
0N/A
0N/Astatic jvmtiError
0N/AsuspendAllHelper(JNIEnv *env, ThreadNode *node, void *arg)
0N/A{
0N/A SuspendAllArg *saArg = (SuspendAllArg *)arg;
0N/A jvmtiError error = JVMTI_ERROR_NONE;
0N/A jthread *list = saArg->list;
0N/A jint count = saArg->count;
0N/A if (!contains(env, list, count, node->thread)) {
0N/A error = commonSuspend(env, node->thread, JNI_FALSE);
0N/A }
0N/A return error;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_suspendAll(void)
0N/A{
0N/A jvmtiError error;
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A
0N/A log_debugee_location("threadControl_suspendAll()", NULL, NULL, 0);
0N/A
0N/A preSuspend();
0N/A
0N/A /*
0N/A * Get a list of all threads and suspend them.
0N/A */
0N/A WITH_LOCAL_REFS(env, 1) {
0N/A
0N/A jthread *threads;
0N/A jint count;
0N/A
0N/A threads = allThreads(&count);
0N/A if (threads == NULL) {
0N/A error = AGENT_ERROR_OUT_OF_MEMORY;
0N/A goto err;
0N/A }
0N/A if (canSuspendResumeThreadLists()) {
0N/A error = commonSuspendList(env, count, threads);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A goto err;
0N/A }
0N/A } else {
0N/A
0N/A int i;
0N/A
0N/A for (i = 0; i < count; i++) {
0N/A error = commonSuspend(env, threads[i], JNI_FALSE);
0N/A
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A goto err;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Update the suspend count of any threads not yet (or no longer)
0N/A * in the thread list above.
0N/A */
0N/A {
0N/A SuspendAllArg arg;
0N/A arg.list = threads;
0N/A arg.count = count;
0N/A error = enumerateOverThreadList(env, &otherThreads,
0N/A suspendAllHelper, &arg);
0N/A }
0N/A
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A suspendAllCount++;
0N/A }
0N/A
0N/A err: ;
0N/A
0N/A } END_WITH_LOCAL_REFS(env)
0N/A
0N/A postSuspend();
0N/A
0N/A return error;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AresumeHelper(JNIEnv *env, ThreadNode *node, void *ignored)
0N/A{
0N/A /*
0N/A * Since this helper is called with the threadLock held, we
0N/A * don't need to recheck to see if the node is still on one
0N/A * of the two thread lists.
0N/A */
0N/A return resumeThreadByNode(node);
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_resumeAll(void)
0N/A{
0N/A jvmtiError error;
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A
0N/A log_debugee_location("threadControl_resumeAll()", NULL, NULL, 0);
0N/A
0N/A eventHandler_lock(); /* for proper lock order */
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A /*
0N/A * Resume only those threads that the debugger has suspended. All
0N/A * such threads must have a node in one of the thread lists, so there's
0N/A * no need to get the whole thread list from JVMTI (unlike
0N/A * suspendAll).
0N/A */
0N/A if (canSuspendResumeThreadLists()) {
0N/A error = commonResumeList(env);
0N/A } else {
0N/A error = enumerateOverThreadList(env, &runningThreads,
0N/A resumeHelper, NULL);
0N/A }
0N/A if ((error == JVMTI_ERROR_NONE) && (otherThreads.first != NULL)) {
0N/A error = enumerateOverThreadList(env, &otherThreads,
0N/A resumeHelper, NULL);
0N/A removeResumed(env, &otherThreads);
0N/A }
0N/A
0N/A if (suspendAllCount > 0) {
0N/A suspendAllCount--;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A eventHandler_unlock();
0N/A /* let eventHelper.c: commandLoop() know we are resuming */
0N/A unblockCommandLoop();
0N/A
0N/A return error;
0N/A}
0N/A
0N/A
0N/AStepRequest *
0N/AthreadControl_getStepRequest(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A StepRequest *step;
0N/A
0N/A step = NULL;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A step = &node->currentStep;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return step;
0N/A}
0N/A
0N/AInvokeRequest *
0N/AthreadControl_getInvokeRequest(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A InvokeRequest *request;
0N/A
0N/A request = NULL;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A request = &node->currentInvoke;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return request;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_addDebugThread(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A if (debugThreadCount >= MAX_DEBUG_THREADS) {
0N/A error = AGENT_ERROR_OUT_OF_MEMORY;
0N/A } else {
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A debugThreads[debugThreadCount] = NULL;
0N/A saveGlobalRef(env, thread, &(debugThreads[debugThreadCount]));
0N/A if (debugThreads[debugThreadCount] == NULL) {
0N/A error = AGENT_ERROR_OUT_OF_MEMORY;
0N/A } else {
0N/A debugThreadCount++;
0N/A error = JVMTI_ERROR_NONE;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A return error;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AthreadControl_removeDebugThread(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A JNIEnv *env;
0N/A int i;
0N/A
0N/A error = AGENT_ERROR_INVALID_THREAD;
0N/A env = getEnv();
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A for (i = 0; i< debugThreadCount; i++) {
0N/A if (isSameObject(env, thread, debugThreads[i])) {
0N/A int j;
0N/A
0N/A tossGlobalRef(env, &(debugThreads[i]));
0N/A for (j = i+1; j < debugThreadCount; j++) {
0N/A debugThreads[j-1] = debugThreads[j];
0N/A }
0N/A debugThreadCount--;
0N/A error = JVMTI_ERROR_NONE;
0N/A break;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A return error;
0N/A}
0N/A
0N/Ajboolean
0N/AthreadControl_isDebugThread(jthread thread)
0N/A{
0N/A int i;
0N/A jboolean rc;
0N/A JNIEnv *env;
0N/A
0N/A rc = JNI_FALSE;
0N/A env = getEnv();
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A for (i = 0; i < debugThreadCount; i++) {
0N/A if (isSameObject(env, thread, debugThreads[i])) {
0N/A rc = JNI_TRUE;
0N/A break;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A return rc;
0N/A}
0N/A
0N/Astatic void
0N/AinitLocks(void)
0N/A{
0N/A if (popFrameEventLock == NULL) {
0N/A popFrameEventLock = debugMonitorCreate("JDWP PopFrame Event Lock");
0N/A popFrameProceedLock = debugMonitorCreate("JDWP PopFrame Proceed Lock");
0N/A }
0N/A}
0N/A
0N/Astatic jboolean
0N/AgetPopFrameThread(jthread thread)
0N/A{
0N/A jboolean popFrameThread;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A popFrameThread = JNI_FALSE;
0N/A } else {
0N/A popFrameThread = node->popFrameThread;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return popFrameThread;
0N/A}
0N/A
0N/Astatic void
0N/AsetPopFrameThread(jthread thread, jboolean value)
0N/A{
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"entry in thread table");
0N/A } else {
0N/A node->popFrameThread = value;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Astatic jboolean
0N/AgetPopFrameEvent(jthread thread)
0N/A{
0N/A jboolean popFrameEvent;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A popFrameEvent = JNI_FALSE;
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"entry in thread table");
0N/A } else {
0N/A popFrameEvent = node->popFrameEvent;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return popFrameEvent;
0N/A}
0N/A
0N/Astatic void
0N/AsetPopFrameEvent(jthread thread, jboolean value)
0N/A{
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"entry in thread table");
0N/A } else {
0N/A node->popFrameEvent = value;
0N/A node->frameGeneration++; /* Increment on each resume */
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Astatic jboolean
0N/AgetPopFrameProceed(jthread thread)
0N/A{
0N/A jboolean popFrameProceed;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A popFrameProceed = JNI_FALSE;
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"entry in thread table");
0N/A } else {
0N/A popFrameProceed = node->popFrameProceed;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return popFrameProceed;
0N/A}
0N/A
0N/Astatic void
0N/AsetPopFrameProceed(jthread thread, jboolean value)
0N/A{
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"entry in thread table");
0N/A } else {
0N/A node->popFrameProceed = value;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/A/**
0N/A * Special event handler for events on the popped thread
0N/A * that occur during the pop operation.
0N/A */
0N/Astatic void
0N/ApopFrameCompleteEvent(jthread thread)
0N/A{
0N/A debugMonitorEnter(popFrameProceedLock);
0N/A {
0N/A /* notify that we got the event */
0N/A debugMonitorEnter(popFrameEventLock);
0N/A {
0N/A setPopFrameEvent(thread, JNI_TRUE);
0N/A debugMonitorNotify(popFrameEventLock);
0N/A }
0N/A debugMonitorExit(popFrameEventLock);
0N/A
0N/A /* make sure we get suspended again */
0N/A setPopFrameProceed(thread, JNI_FALSE);
0N/A while (getPopFrameProceed(thread) == JNI_FALSE) {
0N/A debugMonitorWait(popFrameProceedLock);
0N/A }
0N/A }
0N/A debugMonitorExit(popFrameProceedLock);
0N/A}
0N/A
0N/A/**
0N/A * Pop one frame off the stack of thread.
0N/A * popFrameEventLock is already held
0N/A */
0N/Astatic jvmtiError
0N/ApopOneFrame(jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,PopFrame)(gdata->jvmti, thread);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A return error;
0N/A }
0N/A
0N/A /* resume the popped thread so that the pop occurs and so we */
0N/A /* will get the event (step or method entry) after the pop */
0N/A LOG_MISC(("thread=%p resumed in popOneFrame", thread));
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,ResumeThread)(gdata->jvmti, thread);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A return error;
0N/A }
0N/A
0N/A /* wait for the event to occur */
0N/A setPopFrameEvent(thread, JNI_FALSE);
0N/A while (getPopFrameEvent(thread) == JNI_FALSE) {
0N/A debugMonitorWait(popFrameEventLock);
0N/A }
0N/A
0N/A /* make sure not to suspend until the popped thread is on the wait */
0N/A debugMonitorEnter(popFrameProceedLock);
0N/A {
0N/A /* return popped thread to suspended state */
0N/A LOG_MISC(("thread=%p suspended in popOneFrame", thread));
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SuspendThread)(gdata->jvmti, thread);
0N/A
0N/A /* notify popped thread so it can proceed when resumed */
0N/A setPopFrameProceed(thread, JNI_TRUE);
0N/A debugMonitorNotify(popFrameProceedLock);
0N/A }
0N/A debugMonitorExit(popFrameProceedLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/A/**
0N/A * pop frames of the stack of 'thread' until 'frame' is popped.
0N/A */
0N/AjvmtiError
0N/AthreadControl_popFrames(jthread thread, FrameNumber fnum)
0N/A{
0N/A jvmtiError error;
0N/A jvmtiEventMode prevStepMode;
0N/A jint framesPopped = 0;
0N/A jint popCount;
0N/A jboolean prevInvokeRequestMode;
0N/A
0N/A log_debugee_location("threadControl_popFrames()", thread, NULL, 0);
0N/A
0N/A initLocks();
0N/A
0N/A /* compute the number of frames to pop */
0N/A popCount = fnum+1;
0N/A if (popCount < 1) {
0N/A return AGENT_ERROR_NO_MORE_FRAMES;
0N/A }
0N/A
0N/A /* enable instruction level single step, but first note prev value */
0N/A prevStepMode = threadControl_getInstructionStepMode(thread);
0N/A
0N/A /*
0N/A * Fix bug 6517249. The pop processing will disable invokes,
0N/A * so remember if invokes are enabled now and restore
0N/A * that state after we finish popping.
0N/A */
0N/A prevInvokeRequestMode = invoker_isEnabled(thread);
0N/A
0N/A error = threadControl_setEventMode(JVMTI_ENABLE,
0N/A EI_SINGLE_STEP, thread);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A return error;
0N/A }
0N/A
0N/A /* Inform eventHandler logic we are in a popFrame for this thread */
0N/A debugMonitorEnter(popFrameEventLock);
0N/A {
0N/A setPopFrameThread(thread, JNI_TRUE);
0N/A /* pop frames using single step */
0N/A while (framesPopped++ < popCount) {
0N/A error = popOneFrame(thread);
0N/A if (error != JVMTI_ERROR_NONE) {
0N/A break;
0N/A }
0N/A }
0N/A setPopFrameThread(thread, JNI_FALSE);
0N/A }
0N/A debugMonitorExit(popFrameEventLock);
0N/A
0N/A /* Reset StepRequest info (fromLine and stackDepth) after popframes
0N/A * only if stepping is enabled.
0N/A */
0N/A if (prevStepMode == JVMTI_ENABLE) {
0N/A stepControl_resetRequest(thread);
0N/A }
0N/A
0N/A if (prevInvokeRequestMode) {
0N/A invoker_enableInvokeRequests(thread);
0N/A }
0N/A
0N/A /* restore state */
0N/A (void)threadControl_setEventMode(prevStepMode,
0N/A EI_SINGLE_STEP, thread);
0N/A
0N/A return error;
0N/A}
0N/A
0N/A/* Check to see if any events are being consumed by a popFrame(). */
0N/Astatic jboolean
0N/AcheckForPopFrameEvents(JNIEnv *env, EventIndex ei, jthread thread)
0N/A{
0N/A if ( getPopFrameThread(thread) ) {
0N/A switch (ei) {
0N/A case EI_THREAD_START:
0N/A /* Excuse me? */
0N/A EXIT_ERROR(AGENT_ERROR_INTERNAL, "thread start during pop frame");
0N/A break;
0N/A case EI_THREAD_END:
0N/A /* Thread wants to end? let it. */
0N/A setPopFrameThread(thread, JNI_FALSE);
0N/A popFrameCompleteEvent(thread);
0N/A break;
0N/A case EI_SINGLE_STEP:
0N/A /* This is an event we requested to mark the */
0N/A /* completion of the pop frame */
0N/A popFrameCompleteEvent(thread);
0N/A return JNI_TRUE;
0N/A case EI_BREAKPOINT:
0N/A case EI_EXCEPTION:
0N/A case EI_FIELD_ACCESS:
0N/A case EI_FIELD_MODIFICATION:
0N/A case EI_METHOD_ENTRY:
0N/A case EI_METHOD_EXIT:
0N/A /* Tell event handler to assume event has been consumed. */
0N/A return JNI_TRUE;
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A /* Pretend we were never called */
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/Astruct bag *
0N/AthreadControl_onEventHandlerEntry(jbyte sessionID, EventIndex ei, jthread thread, jobject currentException)
0N/A{
0N/A ThreadNode *node;
0N/A JNIEnv *env;
0N/A struct bag *eventBag;
0N/A jthread threadToSuspend;
0N/A jboolean consumed;
0N/A
0N/A env = getEnv();
0N/A threadToSuspend = NULL;
0N/A
0N/A log_debugee_location("threadControl_onEventHandlerEntry()", thread, NULL, 0);
0N/A
0N/A /* Events during pop commands may need to be ignored here. */
0N/A consumed = checkForPopFrameEvents(env, ei, thread);
0N/A if ( consumed ) {
0N/A /* Always restore any exception (see below). */
0N/A if (currentException != NULL) {
0N/A JNI_FUNC_PTR(env,Throw)(env, currentException);
0N/A } else {
0N/A JNI_FUNC_PTR(env,ExceptionClear)(env);
0N/A }
0N/A return NULL;
0N/A }
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A /*
0N/A * Check the list of unknown threads maintained by suspend
0N/A * and resume. If this thread is currently present in the
0N/A * list, it should be
0N/A * moved to the runningThreads list, since it is a
0N/A * well-known thread now.
0N/A */
0N/A node = findThread(&otherThreads, thread);
0N/A if (node != NULL) {
0N/A moveNode(&otherThreads, &runningThreads, node);
0N/A } else {
0N/A /*
0N/A * Get a thread node for the reporting thread. For thread start
0N/A * events, or if this event precedes a thread start event,
0N/A * the thread node may need to be created.
0N/A *
0N/A * It is possible for certain events (notably method entry/exit)
0N/A * to precede thread start for some VM implementations.
0N/A */
0N/A node = insertThread(env, &runningThreads, thread);
0N/A }
0N/A
0N/A if (ei == EI_THREAD_START) {
0N/A node->isStarted = JNI_TRUE;
0N/A processDeferredEventModes(env, thread, node);
0N/A }
0N/A
0N/A node->current_ei = ei;
0N/A eventBag = node->eventBag;
0N/A if (node->suspendOnStart) {
0N/A threadToSuspend = node->thread;
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A if (threadToSuspend != NULL) {
0N/A /*
0N/A * An attempt was made to suspend this thread before it started.
0N/A * We must suspend it now, before it starts to run. This must
0N/A * be done with no locks held.
0N/A */
0N/A eventHelper_suspendThread(sessionID, threadToSuspend);
0N/A }
0N/A
0N/A return eventBag;
0N/A}
0N/A
0N/Astatic void
0N/AdoPendingTasks(JNIEnv *env, ThreadNode *node)
0N/A{
0N/A /*
0N/A * Take care of any pending interrupts/stops, and clear out
0N/A * info on pending interrupts/stops.
0N/A */
0N/A if (node->pendingInterrupt) {
0N/A JVMTI_FUNC_PTR(gdata->jvmti,InterruptThread)
0N/A (gdata->jvmti, node->thread);
0N/A /*
0N/A * TO DO: Log error
0N/A */
0N/A node->pendingInterrupt = JNI_FALSE;
0N/A }
0N/A
0N/A if (node->pendingStop != NULL) {
0N/A JVMTI_FUNC_PTR(gdata->jvmti,StopThread)
0N/A (gdata->jvmti, node->thread, node->pendingStop);
0N/A /*
0N/A * TO DO: Log error
0N/A */
0N/A tossGlobalRef(env, &(node->pendingStop));
0N/A }
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_onEventHandlerExit(EventIndex ei, jthread thread,
0N/A struct bag *eventBag)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A log_debugee_location("threadControl_onEventHandlerExit()", thread, NULL, 0);
0N/A
0N/A if (ei == EI_THREAD_END) {
0N/A eventHandler_lock(); /* for proper lock order */
0N/A }
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node == NULL) {
0N/A EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"thread list corrupted");
0N/A } else {
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A if (ei == EI_THREAD_END) {
0N/A jboolean inResume = (node->resumeFrameDepth > 0);
0N/A removeThread(env, &runningThreads, thread);
0N/A node = NULL; /* has been freed */
0N/A
0N/A /*
0N/A * Clean up mechanism used to detect end of
0N/A * resume.
0N/A */
0N/A if (inResume) {
0N/A notifyAppResumeComplete();
0N/A }
0N/A } else {
0N/A /* No point in doing this if the thread is about to die.*/
0N/A doPendingTasks(env, node);
0N/A node->eventBag = eventBag;
0N/A node->current_ei = 0;
0N/A }
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A if (ei == EI_THREAD_END) {
0N/A eventHandler_unlock();
0N/A }
0N/A}
0N/A
0N/A/* Returns JDWP flavored status and status flags. */
0N/AjvmtiError
0N/AthreadControl_applicationThreadStatus(jthread thread,
0N/A jdwpThreadStatus *pstatus, jint *statusFlags)
0N/A{
0N/A ThreadNode *node;
0N/A jvmtiError error;
0N/A jint state;
0N/A
0N/A log_debugee_location("threadControl_applicationThreadStatus()", thread, NULL, 0);
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A error = threadState(thread, &state);
0N/A *pstatus = map2jdwpThreadStatus(state);
0N/A *statusFlags = map2jdwpSuspendStatus(state);
0N/A
0N/A if (error == JVMTI_ERROR_NONE) {
0N/A node = findThread(&runningThreads, thread);
0N/A if ((node != NULL) && HANDLING_EVENT(node)) {
0N/A /*
0N/A * While processing an event, an application thread is always
0N/A * considered to be running even if its handler happens to be
0N/A * cond waiting on an internal debugger monitor, etc.
0N/A *
0N/A * Leave suspend status untouched since it is not possible
0N/A * to distinguish debugger suspends from app suspends.
0N/A */
0N/A *pstatus = JDWP_THREAD_STATUS(RUNNING);
0N/A }
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_interrupt(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_ERROR_NONE;
0N/A
0N/A log_debugee_location("threadControl_interrupt()", thread, NULL, 0);
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if ((node == NULL) || !HANDLING_EVENT(node)) {
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,InterruptThread)
0N/A (gdata->jvmti, thread);
0N/A } else {
0N/A /*
0N/A * Hold any interrupts until after the event is processed.
0N/A */
0N/A node->pendingInterrupt = JNI_TRUE;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_clearCLEInfo(JNIEnv *env, jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A node->cleInfo.ei = 0;
0N/A if (node->cleInfo.clazz != NULL) {
0N/A tossGlobalRef(env, &(node->cleInfo.clazz));
0N/A }
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Ajboolean
0N/AthreadControl_cmpCLEInfo(JNIEnv *env, jthread thread, jclass clazz,
0N/A jmethodID method, jlocation location)
0N/A{
0N/A ThreadNode *node;
0N/A jboolean result;
0N/A
0N/A result = JNI_FALSE;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL && node->cleInfo.ei != 0 &&
0N/A node->cleInfo.method == method &&
0N/A node->cleInfo.location == location &&
0N/A (isSameObject(env, node->cleInfo.clazz, clazz))) {
0N/A result = JNI_TRUE; /* we have a match */
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return result;
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_saveCLEInfo(JNIEnv *env, jthread thread, EventIndex ei,
0N/A jclass clazz, jmethodID method, jlocation location)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A node->cleInfo.ei = ei;
0N/A /* Create a class ref that will live beyond */
0N/A /* the end of this call */
0N/A saveGlobalRef(env, clazz, &(node->cleInfo.clazz));
0N/A /* if returned clazz is NULL, we just won't match */
0N/A node->cleInfo.method = method;
0N/A node->cleInfo.location = location;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_setPendingInterrupt(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A node->pendingInterrupt = JNI_TRUE;
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_stop(jthread thread, jobject throwable)
0N/A{
0N/A ThreadNode *node;
0N/A jvmtiError error;
0N/A
0N/A error = JVMTI_ERROR_NONE;
0N/A
0N/A log_debugee_location("threadControl_stop()", thread, NULL, 0);
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A
0N/A node = findThread(&runningThreads, thread);
0N/A if ((node == NULL) || !HANDLING_EVENT(node)) {
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,StopThread)
0N/A (gdata->jvmti, thread, throwable);
0N/A } else {
0N/A JNIEnv *env;
0N/A
0N/A /*
0N/A * Hold any stops until after the event is processed.
0N/A */
0N/A env = getEnv();
0N/A saveGlobalRef(env, throwable, &(node->pendingStop));
0N/A }
0N/A
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return error;
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AdetachHelper(JNIEnv *env, ThreadNode *node, void *arg)
0N/A{
0N/A invoker_detach(&node->currentInvoke);
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_detachInvokes(void)
0N/A{
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A invoker_lock(); /* for proper lock order */
0N/A debugMonitorEnter(threadLock);
0N/A (void)enumerateOverThreadList(env, &runningThreads, detachHelper, NULL);
0N/A debugMonitorExit(threadLock);
0N/A invoker_unlock();
0N/A}
0N/A
0N/Astatic jvmtiError
0N/AresetHelper(JNIEnv *env, ThreadNode *node, void *arg)
0N/A{
0N/A if (node->toBeResumed) {
0N/A LOG_MISC(("thread=%p resumed", node->thread));
0N/A (void)JVMTI_FUNC_PTR(gdata->jvmti,ResumeThread)(gdata->jvmti, node->thread);
0N/A node->frameGeneration++; /* Increment on each resume */
0N/A }
0N/A stepControl_clearRequest(node->thread, &node->currentStep);
0N/A node->toBeResumed = JNI_FALSE;
0N/A node->suspendCount = 0;
0N/A node->suspendOnStart = JNI_FALSE;
0N/A
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/Avoid
0N/AthreadControl_reset(void)
0N/A{
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A eventHandler_lock(); /* for proper lock order */
0N/A debugMonitorEnter(threadLock);
0N/A (void)enumerateOverThreadList(env, &runningThreads, resetHelper, NULL);
0N/A (void)enumerateOverThreadList(env, &otherThreads, resetHelper, NULL);
0N/A
0N/A removeResumed(env, &otherThreads);
0N/A
0N/A freeDeferredEventModes(env);
0N/A
0N/A suspendAllCount = 0;
0N/A
0N/A /* Everything should have been resumed */
0N/A JDI_ASSERT(otherThreads.first == NULL);
0N/A
0N/A debugMonitorExit(threadLock);
0N/A eventHandler_unlock();
0N/A}
0N/A
0N/AjvmtiEventMode
0N/AthreadControl_getInstructionStepMode(jthread thread)
0N/A{
0N/A ThreadNode *node;
0N/A jvmtiEventMode mode;
0N/A
0N/A mode = JVMTI_DISABLE;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A node = findThread(&runningThreads, thread);
0N/A if (node != NULL) {
0N/A mode = node->instructionStepMode;
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A return mode;
0N/A}
0N/A
0N/AjvmtiError
0N/AthreadControl_setEventMode(jvmtiEventMode mode, EventIndex ei, jthread thread)
0N/A{
0N/A jvmtiError error;
0N/A
0N/A /* Global event */
0N/A if ( thread == NULL ) {
0N/A error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventNotificationMode)
0N/A (gdata->jvmti, mode, eventIndex2jvmti(ei), thread);
0N/A } else {
0N/A /* Thread event */
0N/A ThreadNode *node;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A node = findThread(&runningThreads, thread);
0N/A if ((node == NULL) || (!node->isStarted)) {
0N/A JNIEnv *env;
0N/A
0N/A env = getEnv();
0N/A error = addDeferredEventMode(env, mode, ei, thread);
0N/A } else {
0N/A error = threadSetEventNotificationMode(node,
0N/A mode, ei, thread);
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A }
0N/A return error;
0N/A}
0N/A
0N/A/*
0N/A * Returns the current thread, if the thread has generated at least
0N/A * one event, and has not generated a thread end event.
0N/A */
0N/Ajthread threadControl_currentThread(void)
0N/A{
0N/A jthread thread;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(&runningThreads, NULL);
0N/A thread = (node == NULL) ? NULL : node->thread;
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return thread;
0N/A}
0N/A
0N/Ajlong
0N/AthreadControl_getFrameGeneration(jthread thread)
0N/A{
0N/A jlong frameGeneration = -1;
0N/A
0N/A debugMonitorEnter(threadLock);
0N/A {
0N/A ThreadNode *node;
0N/A
0N/A node = findThread(NULL, thread);
0N/A
0N/A if (node != NULL) {
0N/A frameGeneration = node->frameGeneration;
0N/A }
0N/A }
0N/A debugMonitorExit(threadLock);
0N/A
0N/A return frameGeneration;
0N/A}