private static jthread alloc_thread(JNIEnvHandle jniEnv) { jniNativeInterface nativeInterface = (jniNativeInterface)Marshal.PtrToStructure(Marshal.ReadIntPtr(jniEnv.Handle), typeof(jniNativeInterface)); jclass @class = nativeInterface.FindClass(jniEnv, "java/lang/Thread"); if (@class == jclass.Null) { throw new Exception("ERROR: JNI: Cannot find %s with FindClass."); } nativeInterface.ExceptionClear(jniEnv); jmethodID method = nativeInterface.GetMethodID(jniEnv, @class, "<init>", "()V"); if (method == jmethodID.Null) { throw new Exception("Cannot find Thread constructor method."); } nativeInterface.ExceptionClear(jniEnv); jthread result = (jthread)nativeInterface.NewObject(jniEnv, @class, method); if (result == jthread.Null) { throw new Exception("Cannot create new Thread object"); } nativeInterface.ExceptionClear(jniEnv); return(result); }
internal JvmThreadInfo GetThreadInfo(JvmThreadReference thread, JNIEnvHandle jniEnv) { jvmtiThreadInfo info; ThrowOnFailure(_rawInterface.GetThreadInfo(_env, (jthread)thread, out info)); return(new JvmThreadInfo(this, jniEnv, info, true)); }
public JvmThreadReference GetCurrentThread(JNIEnvHandle jniEnv) { jthread threadHandle; ThrowOnFailure(_rawInterface.GetCurrentThread(_env, out threadHandle)); return(JvmThreadReference.FromHandle(this, jniEnv, threadHandle, true)); }
internal JvmThreadInfo(JvmEnvironment environment, JNIEnvHandle jniEnv, jvmtiThreadInfo threadInfo, bool freeLocalReference) { this.Name = threadInfo.Name; this.Priority = threadInfo._priority; this.IsDaemon = threadInfo._isDaemon != 0; this.ThreadGroup = JvmThreadGroupReference.FromHandle(environment, jniEnv, threadInfo._threadGroup, freeLocalReference); this.ContextClassLoader = JvmObjectReference.FromHandle(environment, jniEnv, threadInfo._contextClassLoader, freeLocalReference); }
private void HandleResourceExhausted(jvmtiEnvHandle env, JNIEnvHandle jniEnv, JvmResourceExhaustedFlags flags, IntPtr reserved, ModifiedUTF8StringData description) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); foreach (var processor in _processors) { processor.HandleResourceExhausted(environment, flags, reserved, description.GetString()); } }
private void HandleVMStart(jvmtiEnvHandle env, JNIEnvHandle jniEnv) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); foreach (var processor in _processors) { processor.HandleVMStart(environment); } }
internal JvmNativeEnvironment(JvmEnvironment environment, JNIEnvHandle nativeEnvironmentHandle, jniNativeInterface nativeInterface) { Contract.Requires <ArgumentNullException>(environment != null, "environment"); Contract.Requires <ArgumentException>(nativeEnvironmentHandle != JNIEnvHandle.Null); _environment = environment; _nativeEnvironmentHandle = nativeEnvironmentHandle; _nativeInterface = nativeInterface; }
private void HandleThreadEnd(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); foreach (var processor in _processors) { processor.HandleThreadEnd(environment, thread); } }
private void HandleBreakpoint(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID method, jlocation jlocation) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmLocation location = new JvmLocation(environment, method, jlocation); foreach (var processor in _processors) { processor.HandleBreakpoint(environment, thread, location); } }
private void HandleClassLoad(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jclass classHandle) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmClassReference @class = JvmClassReference.FromHandle(environment, jniEnv, classHandle, true); foreach (var processor in _processors) { processor.HandleClassLoad(environment, thread, @class); } }
private void HandleVMDeath(jvmtiEnvHandle env, JNIEnvHandle jniEnv) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); foreach (var processor in _processors) { processor.HandleVMDeath(environment); } environment.VirtualMachine.ShutdownAgentDispatchers(); }
private void HandleMethodExit(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, bool wasPoppedByException, jvalue returnValue) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmMethod method = new JvmMethod(environment, methodId); foreach (var processor in _processors) { processor.HandleMethodExit(environment, thread, method, wasPoppedByException, returnValue); } }
private void HandleMethodEntry(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmMethod method = new JvmMethod(environment, methodId); foreach (var processor in _processors) { processor.HandleMethodEntry(environment, thread, method); } }
public static JvmObjectReference FromHandle(JvmEnvironment environment, JNIEnvHandle jniEnv, jobject objectHandle, bool freeLocalReference) { if (objectHandle == jobject.Null) { return(null); } JvmNativeEnvironment nativeEnvironment = environment.GetNativeFunctionTable(jniEnv); return(new JvmObjectReference(environment, nativeEnvironment, objectHandle, freeLocalReference)); }
public static JvmThreadReference FromHandle(JvmEnvironment environment, JNIEnvHandle jniEnv, jthread threadHandle, bool freeLocalReference) { if (threadHandle == jthread.Null) { return(null); } JvmNativeEnvironment nativeEnvironment = environment.GetNativeFunctionTable(jniEnv); return(new JvmThreadReference(environment, nativeEnvironment, threadHandle, freeLocalReference)); }
public static JvmClassReference FromHandle(JvmEnvironment environment, JNIEnvHandle jniEnv, jclass classHandle, bool freeLocalReference) { if (classHandle == jclass.Null) { return(null); } JvmNativeEnvironment nativeEnvironment = environment.GetNativeFunctionTable(jniEnv); return(new JvmClassReference(environment, nativeEnvironment, classHandle, freeLocalReference)); }
private void HandleMonitorContendedEntered(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jobject objectHandle) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); foreach (var processor in _processors) { processor.HandleMonitorContendedEntered(environment, thread, @object); } }
private void HandleMonitorWait(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jobject objectHandle, long millisecondsTimeout) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); foreach (var processor in _processors) { processor.HandleMonitorWait(environment, thread, @object, millisecondsTimeout); } }
private void HandleVMInit(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); jvmtiError result = environment.RawInterface.RunAgentThread(env, alloc_thread(jniEnv), DispatchJvmEvents, IntPtr.Zero, JvmThreadPriority.Maximum); foreach (var processor in _processors) { processor.HandleVMInitialization(environment, thread); } }
private void HandleClassFileLoadHook(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jclass classBeingRedefinedHandle, jobject loaderHandle, ModifiedUTF8StringData name, jobject protectionDomainHandle, int classDataLength, IntPtr classData, ref int newClassDataLength, ref IntPtr newClassData) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmClassReference classBeingRedefined = JvmClassReference.FromHandle(environment, jniEnv, classBeingRedefinedHandle, true); JvmObjectReference loader = JvmObjectReference.FromHandle(environment, jniEnv, loaderHandle, true); JvmObjectReference protectionDomain = JvmObjectReference.FromHandle(environment, jniEnv, protectionDomainHandle, true); foreach (var processor in _processors) { processor.HandleClassFileLoadHook(environment, classBeingRedefined, loader, name.GetString(), protectionDomain); } }
private void HandleVMObjectAlloc(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jobject objectHandle, jclass objectClassHandle, long size) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); JvmClassReference objectClass = JvmClassReference.FromHandle(environment, jniEnv, objectClassHandle, true); foreach (var processor in _processors) { processor.HandleVMObjectAllocation(environment, thread, @object, objectClass, size); } }
private void HandleExceptionCatch(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID method, jlocation jlocation, jobject exceptionHandle) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmLocation location = new JvmLocation(environment, method, jlocation); JvmObjectReference exception = JvmObjectReference.FromHandle(environment, jniEnv, exceptionHandle, true); foreach (var processor in _processors) { processor.HandleExceptionCatch(environment, thread, location, exception); } }
private void HandleNativeMethodBind(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, IntPtr address, ref IntPtr newAddressPtr) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmMethod method = new JvmMethod(environment, methodId); foreach (var processor in _processors) { IntPtr?newAddress = null; processor.HandleNativeMethodBind(environment, thread, method, address, ref newAddress); } }
internal static JniEnvironment GetOrCreateInstance(JNIEnvHandle handle, out bool created) { bool wasCreated = false; JniEnvironment environment = _instances.GetOrAdd(handle, i => { wasCreated = true; return CreateVirtualMachine(i); }); created = wasCreated; return environment; }
private void HandleFieldModification(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID method, jlocation jlocation, jclass fieldClassHandle, jobject @objectHandle, jfieldID fieldId, byte signatureType, jvalue newValue) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); JvmLocation location = new JvmLocation(environment, method, jlocation); JvmClassReference fieldClass = JvmClassReference.FromHandle(environment, jniEnv, fieldClassHandle, true); JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); JvmField field = new JvmField(environment, fieldId); foreach (var processor in _processors) { processor.HandleFieldModification(environment, thread, location, fieldClass, @object, field, signatureType, newValue); } }
internal JvmNativeEnvironment GetNativeFunctionTable(JNIEnvHandle nativeEnvironmentHandle) { IntPtr functionTable; ThrowOnFailure(_rawInterface.GetJNIFunctionTable(_env, out functionTable)); try { jniNativeInterface nativeInterface = (jniNativeInterface)Marshal.PtrToStructure(functionTable, typeof(jniNativeInterface)); return(new JvmNativeEnvironment(this, nativeEnvironmentHandle, nativeInterface)); } finally { if (functionTable != IntPtr.Zero) { Deallocate(functionTable); } } }
private void HandleVMDeath(jvmtiEnvHandle env, JNIEnvHandle jniEnv) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle> method = HandleVMDeath; AgentEventDispatcher.Invoke(method, env, jniEnv); return; } JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment; JniErrorHandler.ThrowOnFailure(VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, false)); EventFilter[] filters = GetEventFilters(EventKind.VirtualMachineDeath); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, default(ThreadId), default(TaggedReferenceTypeId), default(Location?))) { ApplySuspendPolicy(environment, nativeEnvironment, filter.SuspendPolicy, default(ThreadId)); Callback.VirtualMachineDeath(filter.SuspendPolicy, filter.RequestId); } } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //foreach (var processor in _processors) //{ // processor.HandleVMDeath(environment); //} //environment.VirtualMachine.ShutdownAgentDispatchers(); }
private void AgentDispatcherThread(jvmtiEnvHandle env, JNIEnvHandle jniEnv, IntPtr arg) { JniEnvironment nativeEnvironment; VirtualMachine.AttachCurrentThreadAsDaemon(Environment, out nativeEnvironment, true); _agentEventDispatcher = Dispatcher.CurrentDispatcher; _agentStartedEvent.Set(); Dispatcher.Run(); }
public JniEnvironment(JNIEnvHandle handle) { _handle = handle; _rawInterface = (jniNativeInterface)Marshal.PtrToStructure(Marshal.ReadIntPtr(handle.Handle), typeof(jniNativeInterface)); }
private void HandleSingleStep(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, jlocation jlocation) { JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); TaggedReferenceTypeId declaringClass; MethodId method = new MethodId(methodId.Handle); ulong index = (ulong)jlocation.Value; JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass)); Location location = new Location(declaringClass, method, index); EventFilter[] filters = GetEventFilters(EventKind.SingleStep); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), location)) { SendSingleStepEvent(environment, filter, threadId, location); } } }
private void HandleException(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, jlocation jlocation, jobject exceptionHandle, jmethodID catchMethodId, jlocation catchjLocation) { // don't send exception events from an agent thread if (VirtualMachine.IsAgentThread.Value) return; JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv); TaggedReferenceTypeId declaringClass; MethodId method = new MethodId(methodId.Handle); ulong index = (ulong)jlocation.Value; JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass)); Location location = new Location(declaringClass, method, index); Location catchLocation; method = new MethodId(catchMethodId.Handle); index = (ulong)catchjLocation.Value; if (catchMethodId.Handle != IntPtr.Zero) { JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass)); catchLocation = new Location(declaringClass, method, index); } else { catchLocation = default(Location); } TaggedObjectId exceptionId = VirtualMachine.TrackLocalObjectReference(exceptionHandle, environment, nativeEnvironment, false); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); EventFilter[] filters = GetEventFilters(EventKind.Exception); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), location)) { SendExceptionEvent(environment, filter, threadId, location, exceptionId, catchLocation); } } ////Location location = new Location(); ////Location catchLocation = new Location(); //throw new NotImplementedException(); #if false ThreadId threadId = GetObjectId(ref threadHandle); TaggedObjectId exception = GetObjectId(ref exceptionHandle); EventFilter[] filters = GetEventFilters(EventKind.Exception); foreach (var filter in filters) { if (filter.ProcessEvent(threadId, default(TaggedReferenceTypeId))) { ApplySuspendPolicy(environment, filter.SuspendPolicy, threadId); Callback.HandleException(filter.SuspendPolicy, filter.RequestId, threadId, location, exception, catchLocation); } } #endif //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmLocation location = new JvmLocation(environment, method, jlocation); //JvmObjectReference exception = JvmObjectReference.FromHandle(environment, jniEnv, exceptionHandle, true); //JvmLocation catchLocation = new JvmLocation(environment, catchMethod, catchjLocation); //foreach (var processor in _processors) //{ // processor.HandleException(environment, thread, location, exception, catchLocation); //} }
private void HandleClassPrepare(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jclass classHandle) { bool preventSuspend = VirtualMachine.IsAgentThread.Value; JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv); string signature; IntPtr signaturePtr; IntPtr genericPtr; JvmtiErrorHandler.ThrowOnFailure(RawInterface.GetClassSignature(env, classHandle, out signaturePtr, out genericPtr)); try { unsafe { signature = ModifiedUTF8Encoding.GetString((byte*)signaturePtr); } } finally { RawInterface.Deallocate(env, signaturePtr); RawInterface.Deallocate(env, genericPtr); } ClassStatus classStatus = 0; jvmtiClassStatus internalClassStatus; JvmtiErrorHandler.ThrowOnFailure(RawInterface.GetClassStatus(env, classHandle, out internalClassStatus)); if ((internalClassStatus & jvmtiClassStatus.Error) != 0) classStatus |= ClassStatus.Error; if ((internalClassStatus & jvmtiClassStatus.Initialized) != 0) classStatus |= ClassStatus.Initialized; if ((internalClassStatus & jvmtiClassStatus.Prepared) != 0) classStatus |= ClassStatus.Prepared; if ((internalClassStatus & jvmtiClassStatus.Verified) != 0) classStatus |= ClassStatus.Verified; ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); TaggedReferenceTypeId classId = VirtualMachine.TrackLocalClassReference(classHandle, environment, nativeEnvironment, false); EventFilter[] filters = GetEventFilters(EventKind.ClassPrepare); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, classId, default(Location?))) { SendClassPrepareEvent(environment, filter, threadId, classId, signature, classStatus, preventSuspend); } } }
private void HandleClassFileLoadHook(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jclass classBeingRedefinedHandle, jobject loaderHandle, IntPtr name, jobject protectionDomainHandle, int classDataLength, IntPtr classData, ref int newClassDataLength, ref IntPtr newClassData) { // need to extract the exception_table for each method ClassFile classFile = ClassFile.FromMemory(classData, 0); ConstantClass constantClass = classFile.ThisClass; ConstantUtf8 constantClassSignature = (ConstantUtf8)classFile.ConstantPool[constantClass.NameIndex - 1]; string classSignature = constantClassSignature.Value; if (classSignature[0] != '[') classSignature = 'L' + classSignature + ';'; long classLoaderTag; JvmtiErrorHandler.ThrowOnFailure(Environment.TagClassLoader(loaderHandle, out classLoaderTag)); foreach (var methodInfo in classFile.Methods) { if ((methodInfo.Modifiers & (AccessModifiers.Abstract | AccessModifiers.Native)) != 0) continue; Code codeAttribute = methodInfo.Attributes.OfType<Code>().SingleOrDefault(); if (codeAttribute == null) continue; ConstantUtf8 constantMethodName = (ConstantUtf8)classFile.ConstantPool[methodInfo.NameIndex - 1]; string methodName = constantMethodName.Value; ConstantUtf8 constantMethodSignature = (ConstantUtf8)classFile.ConstantPool[methodInfo.DescriptorIndex - 1]; string methodSignature = constantMethodSignature.Value; VirtualMachine.SetExceptionTable(classLoaderTag, classSignature, methodName, methodSignature, codeAttribute.ExceptionTable); } #if false if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread //Action<jvmtiEnvHandle, JNIEnvHandle, jclass, jobject, ModifiedUTF8StringData, jobject, int, IntPtr> method = HandleClassFileLoadHook; //AgentEventDispatcher.Invoke(method, env, jniEnv); //return; throw new NotImplementedException(); } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmClassReference classBeingRedefined = JvmClassReference.FromHandle(environment, jniEnv, classBeingRedefinedHandle, true); //JvmObjectReference loader = JvmObjectReference.FromHandle(environment, jniEnv, loaderHandle, true); //JvmObjectReference protectionDomain = JvmObjectReference.FromHandle(environment, jniEnv, protectionDomainHandle, true); //foreach (var processor in _processors) //{ // processor.HandleClassFileLoadHook(environment, classBeingRedefined, loader, name.GetString(), protectionDomain); //} #endif }
private void HandleMethodExit(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, bool wasPoppedByException, jvalue returnValue) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jmethodID, bool, jvalue> method = HandleMethodExit; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle, methodId, wasPoppedByException, returnValue); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmMethod method = new JvmMethod(environment, methodId); //foreach (var processor in _processors) //{ // processor.HandleMethodExit(environment, thread, method, wasPoppedByException, returnValue); //} }
private void HandleFieldModification(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, jlocation jlocation, jclass fieldClassHandle, jobject objectHandle, jfieldID fieldId, byte signatureType, jvalue newValue) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jmethodID, jlocation, jclass, jobject, jfieldID, byte, jvalue> method = HandleFieldModification; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle, methodId, jlocation, fieldClassHandle, objectHandle, fieldId, signatureType, newValue); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmLocation location = new JvmLocation(environment, method, jlocation); //JvmClassReference fieldClass = JvmClassReference.FromHandle(environment, jniEnv, fieldClassHandle, true); //JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); //JvmField field = new JvmField(environment, fieldId); //foreach (var processor in _processors) //{ // processor.HandleFieldModification(environment, thread, location, fieldClass, @object, field, signatureType, newValue); //} }
private void HandleBreakpoint(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, jlocation jlocation) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jmethodID, jlocation> invokeMethod = HandleBreakpoint; AgentEventDispatcher.Invoke(invokeMethod, env, jniEnv, threadHandle, methodId, jlocation); return; } JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment; JniErrorHandler.ThrowOnFailure(VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, false)); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); TaggedReferenceTypeId declaringClass; MethodId method = new MethodId(methodId.Handle); ulong index = (ulong)jlocation.Value; JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass)); Location location = new Location(declaringClass, method, index); EventFilter[] filters = GetEventFilters(EventKind.Breakpoint); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), location)) { ApplySuspendPolicy(environment, nativeEnvironment, filter.SuspendPolicy, threadId); Callback.Breakpoint(filter.SuspendPolicy, filter.RequestId, threadId, location); } } }
private static void DispatchJvmEvents(jvmtiEnvHandle env, JNIEnvHandle jniEnv, IntPtr arg) { JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); environment.VirtualMachine.PushAgentDispatcherFrame(new DispatcherFrame(true), environment); }
private static JniEnvironment CreateVirtualMachine(JNIEnvHandle handle) { return new JniEnvironment(handle); }
private void HandleThreadEnd(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle) { /* This event is always sent on the thread that's starting. if it's an agent thread, just * ignore the event to hide it from the IDE. */ if (VirtualMachine.IsAgentThread.Value) return; // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread> method = HandleThreadEndImpl; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle); return; }
private void HandleNativeMethodBind(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, IntPtr address, ref IntPtr newAddressPtr) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread //Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jmethodID, IntPtr, ReferenceTypeData intptr> method = HandleNativeMethodBind; //AgentEventDispatcher.Invoke(method, env, jniEnv); //return; throw new NotImplementedException(); } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmMethod method = new JvmMethod(environment, methodId); //foreach (var processor in _processors) //{ // IntPtr? newAddress = null; // processor.HandleNativeMethodBind(environment, thread, method, address, ref newAddress); //} }
private void HandleThreadEndImpl(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle) { JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment; JniErrorHandler.ThrowOnFailure(VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, false)); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); EventFilter[] filters = GetEventFilters(EventKind.ThreadEnd); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), default(Location?))) { ApplySuspendPolicy(environment, nativeEnvironment, filter.SuspendPolicy, threadId); Callback.ThreadDeath(filter.SuspendPolicy, filter.RequestId, threadId); } } }
private void HandleMonitorContendedEntered(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jobject objectHandle) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jobject> method = HandleMonitorContendedEntered; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle, objectHandle); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); //foreach (var processor in _processors) //{ // processor.HandleMonitorContendedEntered(environment, thread, @object); //} }
private void HandleClassLoad(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jclass classHandle) { //JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); //JniEnvironment nativeEnvironment; //JniErrorHandler.ThrowOnFailure(VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, false)); //VirtualMachine.HandleClassLoad(environment, nativeEnvironment, classHandle); }
private void HandleResourceExhausted(jvmtiEnvHandle env, JNIEnvHandle jniEnv, JvmResourceExhaustedFlags flags, IntPtr reserved, IntPtr description) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, JvmResourceExhaustedFlags, IntPtr, IntPtr> method = HandleResourceExhausted; AgentEventDispatcher.Invoke(method, env, jniEnv, flags, reserved, description); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //foreach (var processor in _processors) //{ // processor.HandleResourceExhausted(environment, flags, reserved, description.GetString()); //} }
private void HandleVMStart(jvmtiEnvHandle env, JNIEnvHandle jniEnv) { /********************** * Note: there are no dispatchers available at this point. */ }
private void HandleVMObjectAlloc(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jobject objectHandle, jclass objectClassHandle, long size) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jobject, jclass, long> method = HandleVMObjectAlloc; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle, objectHandle, objectClassHandle, size); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmObjectReference @object = JvmObjectReference.FromHandle(environment, jniEnv, objectHandle, true); //JvmClassReference objectClass = JvmClassReference.FromHandle(environment, jniEnv, objectClassHandle, true); //foreach (var processor in _processors) //{ // processor.HandleVMObjectAllocation(environment, thread, @object, objectClass, size); //} }
private void HandleExceptionCatch(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, jlocation jlocation, jobject exceptionHandle) { if (!VirtualMachine.IsAgentThread.Value) { // ignore events before VMInit if (AgentEventDispatcher == null) return; // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread, jmethodID, jlocation, jobject> method = HandleExceptionCatch; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle, methodId, jlocation, exceptionHandle); return; } //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env); //JvmThreadReference thread = JvmThreadReference.FromHandle(environment, jniEnv, threadHandle, true); //JvmLocation location = new JvmLocation(environment, method, jlocation); //JvmObjectReference exception = JvmObjectReference.FromHandle(environment, jniEnv, exceptionHandle, true); //foreach (var processor in _processors) //{ // processor.HandleExceptionCatch(environment, thread, location, exception); //} }
private void HandleVMInit(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle) { JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment; if (AgentEventDispatcher == null) { if (VirtualMachine.IsAgentThread.Value) throw new InvalidOperationException(); nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv); VirtualMachine.HandleVMInit(environment, nativeEnvironment, threadHandle); InitializeAgentThread(environment, nativeEnvironment); } if (!VirtualMachine.IsAgentThread.Value) { // dispatch this call to an agent thread Action<jvmtiEnvHandle, JNIEnvHandle, jthread> method = HandleVMInit; AgentEventDispatcher.Invoke(method, env, jniEnv, threadHandle); return; } JniErrorHandler.ThrowOnFailure(VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, false)); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); bool sent = false; EventFilter[] filters = GetEventFilters(EventKind.VirtualMachineStart); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), default(Location?))) { ApplySuspendPolicy(environment, nativeEnvironment, filter.SuspendPolicy, threadId); Callback.VirtualMachineStart(filter.SuspendPolicy, filter.RequestId, threadId); sent = true; } } if (!sent) { ApplySuspendPolicy(environment, nativeEnvironment, SuspendPolicy.All, threadId); Callback.VirtualMachineStart(SuspendPolicy.All, default(RequestId), threadId); } }
private void HandleFramePop(jvmtiEnvHandle env, JNIEnvHandle jniEnv, jthread threadHandle, jmethodID methodId, bool wasPoppedByException) { try { JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(_service.VirtualMachine, env); JniEnvironment nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv); TaggedReferenceTypeId declaringClass; MethodId method = new MethodId(methodId.Handle); jlocation jlocation; JvmtiErrorHandler.ThrowOnFailure(environment.GetFrameLocation(threadHandle, 1, out methodId, out jlocation)); ulong index = (ulong)jlocation.Value; JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass)); Location location = new Location(declaringClass, method, index); ThreadId threadId = VirtualMachine.TrackLocalThreadReference(threadHandle, environment, nativeEnvironment, false); EventFilter[] filters = GetEventFilters(EventKind.FramePop); foreach (var filter in filters) { if (filter.ProcessEvent(environment, nativeEnvironment, this, threadId, default(TaggedReferenceTypeId), location)) { if (filter.InternalEventKind == EventKind.SingleStep) { // remove the frame pop event JvmtiErrorHandler.ThrowOnFailure((jvmtiError)ClearEventInternal(EventKind.FramePop, filter.RequestId)); // set an actual step filter to respond when the thread arrives in the parent frame JvmtiErrorHandler.ThrowOnFailure((jvmtiError)SetEventInternal(environment, nativeEnvironment, EventKind.SingleStep, filter)); } else { SendFramePopEvent(environment, filter, threadId, location, wasPoppedByException); } } } } catch (Exception e) { string caption = "Exception while handling a frame pop event"; System.Windows.Forms.MessageBox.Show(e.Message + System.Environment.NewLine + System.Environment.NewLine + e.StackTrace, caption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); throw; } }
internal static JniEnvironment GetOrCreateInstance(JNIEnvHandle handle) { bool created; return GetOrCreateInstance(handle, out created); }