Example #1
0
        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);
        }
Example #2
0
        internal JvmThreadInfo GetThreadInfo(JvmThreadReference thread, JNIEnvHandle jniEnv)
        {
            jvmtiThreadInfo info;

            ThrowOnFailure(_rawInterface.GetThreadInfo(_env, (jthread)thread, out info));
            return(new JvmThreadInfo(this, jniEnv, info, true));
        }
Example #3
0
        public JvmThreadReference GetCurrentThread(JNIEnvHandle jniEnv)
        {
            jthread threadHandle;

            ThrowOnFailure(_rawInterface.GetCurrentThread(_env, out threadHandle));
            return(JvmThreadReference.FromHandle(this, jniEnv, threadHandle, true));
        }
Example #4
0
 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);
 }
Example #5
0
        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());
            }
        }
Example #6
0
        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;
        }
Example #8
0
        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);
            }
        }
Example #9
0
        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);
            }
        }
Example #10
0
        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);
            }
        }
Example #11
0
        private void HandleVMDeath(jvmtiEnvHandle env, JNIEnvHandle jniEnv)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleVMDeath(environment);
            }

            environment.VirtualMachine.ShutdownAgentDispatchers();
        }
Example #12
0
        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);
            }
        }
Example #13
0
        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);
            }
        }
Example #14
0
        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));
        }
Example #17
0
        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);
            }
        }
Example #18
0
        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);
            }
        }
Example #19
0
        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);
            }
        }
Example #20
0
        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);
            }
        }
Example #21
0
        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);
            }
        }
Example #22
0
        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);
            }
        }
Example #23
0
        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);
            }
        }
Example #24
0
        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;
        }
Example #25
0
        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);
            }
        }
Example #26
0
        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();
 }
Example #29
0
 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);
                    }
                }
            }
Example #37
0
        private static void DispatchJvmEvents(jvmtiEnvHandle env, JNIEnvHandle jniEnv, IntPtr arg)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            environment.VirtualMachine.PushAgentDispatcherFrame(new DispatcherFrame(true), environment);
        }
Example #38
0
 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;
                }
            }
Example #50
0
 internal static JniEnvironment GetOrCreateInstance(JNIEnvHandle handle)
 {
     bool created;
     return GetOrCreateInstance(handle, out created);
 }