Exemplo n.º 1
0
        private JvmtiEnvironment(JavaVM virtualMachine, jvmtiEnvHandle handle)
        {
            Contract.Requires<ArgumentNullException>(virtualMachine != null, "virtualMachine");

            _virtualMachine = virtualMachine;
            _handle = handle;
            _rawInterface = (jvmtiInterface)Marshal.PtrToStructure(Marshal.ReadIntPtr(handle.Handle), typeof(jvmtiInterface));

            jvmtiCapabilities previousCapabilities;
            JvmtiErrorHandler.ThrowOnFailure(RawInterface.GetCapabilities(this, out previousCapabilities));

            jvmtiCapabilities potentialCapabilities;
            JvmtiErrorHandler.ThrowOnFailure(RawInterface.GetPotentialCapabilities(this, out potentialCapabilities));

            var capabilities =
                new jvmtiCapabilities(
                    jvmtiCapabilities.CapabilityFlags1.CanTagObjects
                    | jvmtiCapabilities.CapabilityFlags1.CanGetSourceFileName
                    | jvmtiCapabilities.CapabilityFlags1.CanGetLineNumbers
                    | jvmtiCapabilities.CapabilityFlags1.CanGetSourceDebugExtension
                    | jvmtiCapabilities.CapabilityFlags1.CanAccessLocalVariables
                    | jvmtiCapabilities.CapabilityFlags1.CanGenerateSingleStepEvents
                    | jvmtiCapabilities.CapabilityFlags1.CanGenerateExceptionEvents
                    | jvmtiCapabilities.CapabilityFlags1.CanGenerateBreakpointEvents
                    | jvmtiCapabilities.CapabilityFlags1.CanGenerateFramePopEvents
                    | jvmtiCapabilities.CapabilityFlags1.CanGenerateAllClassHookEvents
                    | jvmtiCapabilities.CapabilityFlags1.CanGetBytecodes
                    | jvmtiCapabilities.CapabilityFlags1.CanSuspend,
                    jvmtiCapabilities.CapabilityFlags2.CanGetConstantPool
                    );
            JvmtiErrorHandler.ThrowOnFailure(RawInterface.AddCapabilities(this, ref capabilities));

            jvmtiCapabilities newCapabilities;
            JvmtiErrorHandler.ThrowOnFailure(RawInterface.GetCapabilities(this, out newCapabilities));
        }
Exemplo n.º 2
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());
            }
        }
Exemplo n.º 3
0
        private void HandleObjectFree(jvmtiEnvHandle env, long tag)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleObjectFree(environment, tag);
            }
        }
Exemplo n.º 4
0
        private void HandleGarbageCollectionFinish(jvmtiEnvHandle env)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleGarbageCollectionFinish(environment);
            }
        }
Exemplo n.º 5
0
        private void HandleDynamicCodeGenerated(jvmtiEnvHandle env, ModifiedUTF8StringData name, IntPtr address, int length)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleDynamicCodeGenerated(environment, name.GetString(), address, length);
            }
        }
Exemplo n.º 6
0
        private void HandleVMStart(jvmtiEnvHandle env, JNIEnvHandle jniEnv)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleVMStart(environment);
            }
        }
Exemplo n.º 7
0
        private void HandleDataDumpRequest(jvmtiEnvHandle env)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                processor.HandleDataDumpRequest(environment);
            }
        }
Exemplo n.º 8
0
        private void HandleCompiledMethodUnload(jvmtiEnvHandle env, jmethodID methodId, IntPtr codeAddress)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);
            JvmMethod      method      = new JvmMethod(environment, methodId);

            foreach (var processor in _processors)
            {
                processor.HandleCompiledMethodUnload(environment, method, codeAddress);
            }
        }
Exemplo n.º 9
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);
            }
        }
Exemplo n.º 10
0
        private void HandleCompiledMethodLoad(jvmtiEnvHandle env, jmethodID method, int codeSize, IntPtr codeAddress, int mapLength, jvmtiAddressLocationMap[] map, IntPtr compileInfo)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            foreach (var processor in _processors)
            {
                throw new NotImplementedException();
                //processor.HandleCompiledMethodLoad(environment, method, codeSize, codeAddress, map2, compileInfo);
            }
        }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
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);
            }
        }
Exemplo n.º 13
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);
            }
        }
Exemplo n.º 14
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);
            }
        }
Exemplo n.º 15
0
        private void HandleVMDeath(jvmtiEnvHandle env, JNIEnvHandle jniEnv)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

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

            environment.VirtualMachine.ShutdownAgentDispatchers();
        }
Exemplo n.º 16
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);
            }
        }
Exemplo n.º 17
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);
            }
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
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);
            }
        }
Exemplo n.º 20
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);
            }
        }
Exemplo n.º 21
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);
            }
        }
Exemplo n.º 22
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);
            }
        }
Exemplo n.º 23
0
        internal static JvmEnvironment GetOrCreateEnvironment(JavaVM virtualMachine, jvmtiEnvHandle env)
        {
            Contract.Requires <ArgumentNullException>(virtualMachine != null, "virtualMachine");

            JvmEnvironment environment = _environments.GetOrAdd(env, CreateEnvironment);

            if (environment.VirtualMachine == null)
            {
                environment.VirtualMachine = virtualMachine;
            }

            return(environment);
        }
Exemplo n.º 24
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);
            }
        }
            private void HandleCompiledMethodUnload(jvmtiEnvHandle env, jmethodID methodId, IntPtr codeAddress)
            {
                if (!VirtualMachine.IsAgentThread.Value)
                {
                    // ignore events before VMInit
                    if (AgentEventDispatcher == null)
                        return;

                    // dispatch this call to an agent thread
                    Action<jvmtiEnvHandle, jmethodID, IntPtr> method = HandleCompiledMethodUnload;
                    AgentEventDispatcher.Invoke(method, env, methodId, codeAddress);
                    return;
                }

                //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);
                //JvmMethod method = new JvmMethod(environment, methodId);

                //foreach (var processor in _processors)
                //{
                //    processor.HandleCompiledMethodUnload(environment, method, codeAddress);
                //}
            }
            private void HandleCompiledMethodLoad(jvmtiEnvHandle env, jmethodID methodId, int codeSize, IntPtr codeAddress, int mapLength, jvmtiAddressLocationMap[] map, IntPtr compileInfo)
            {
                if (!VirtualMachine.IsAgentThread.Value)
                {
                    // ignore events before VMInit
                    if (AgentEventDispatcher == null)
                        return;

                    // dispatch this call to an agent thread
                    Action<jvmtiEnvHandle, jmethodID, int, IntPtr, int, jvmtiAddressLocationMap[], IntPtr> method = HandleCompiledMethodLoad;
                    AgentEventDispatcher.Invoke(method, env, methodId, codeSize, codeAddress, mapLength, map, compileInfo);
                    return;
                }

                //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

                //foreach (var processor in _processors)
                //{
                //    throw new NotImplementedException();
                //    //processor.HandleCompiledMethodLoad(environment, method, codeSize, codeAddress, map2, compileInfo);
                //}
            }
            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 AgentDispatcherThread(jvmtiEnvHandle env, JNIEnvHandle jniEnv, IntPtr arg)
 {
     JniEnvironment nativeEnvironment;
     VirtualMachine.AttachCurrentThreadAsDaemon(Environment, out nativeEnvironment, true);
     _agentEventDispatcher = Dispatcher.CurrentDispatcher;
     _agentStartedEvent.Set();
     Dispatcher.Run();
 }
            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 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());
                //}
            }
Exemplo n.º 31
0
 private static JvmEnvironment CreateEnvironment(jvmtiEnvHandle env)
 {
     return(new JvmEnvironment(env));
 }
Exemplo n.º 32
0
        private static void DispatchJvmEvents(jvmtiEnvHandle env, JNIEnvHandle jniEnv, IntPtr arg)
        {
            JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

            environment.VirtualMachine.PushAgentDispatcherFrame(new DispatcherFrame(true), environment);
        }
            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 HandleDynamicCodeGenerated(jvmtiEnvHandle env, IntPtr name, IntPtr address, int length)
            {
                if (!VirtualMachine.IsAgentThread.Value)
                {
                    // ignore events before VMInit
                    if (AgentEventDispatcher == null)
                        return;

                    // dispatch this call to an agent thread
                    Action<jvmtiEnvHandle, IntPtr, IntPtr, int> method = HandleDynamicCodeGenerated;
                    AgentEventDispatcher.Invoke(method, env, name, address, length);
                    return;
                }

                //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

                //foreach (var processor in _processors)
                //{
                //    processor.HandleDynamicCodeGenerated(environment, name.GetString(), address, length);
                //}
            }
            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 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 void HandleObjectFree(jvmtiEnvHandle env, long tag)
            {
                if (!VirtualMachine.IsAgentThread.Value)
                {
                    // ignore events before VMInit
                    if (AgentEventDispatcher == null)
                        return;

                    // dispatch this call to an agent thread
                    Action<jvmtiEnvHandle, long> method = HandleObjectFree;
                    AgentEventDispatcher.Invoke(method, env, tag);
                    return;
                }

                //JvmEnvironment environment = JvmEnvironment.GetEnvironment(env);

                //foreach (var processor in _processors)
                //{
                //    processor.HandleObjectFree(environment, tag);
                //}
            }
            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 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 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 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 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 AttachImpl(jvmtiEnvHandle env)
            {
                JvmtiEnvironment environment = JvmtiEnvironment.GetOrCreateInstance(VirtualMachine, env);

                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventCallbacks(_eventCallbacks));

                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.VMStart));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.VMInit));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.VMDeath));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ThreadStart));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ThreadEnd));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ClassLoad));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ClassPrepare));
                JvmtiErrorHandler.ThrowOnFailure(environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ClassFileLoadHook));

#if false
                jvmtiCapabilities capabilities;
                JvmtiErrorHandler.ThrowOnFailure(environment.GetCapabilities(out capabilities));

                environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ClassFileLoadHook);
                environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.DynamicCodeGenerated);
                environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.DataDumpRequest);
                environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ResourceExhausted);
                //environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.DataResetRequest);

                if (capabilities.CanGenerateExceptionEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.Exception);
                if (capabilities.CanGenerateExceptionEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ExceptionCatch);
                if (capabilities.CanGenerateSingleStepEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.SingleStep);
                if (capabilities.CanGenerateFramePopEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.FramePop);
                if (capabilities.CanGenerateBreakpointEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.Breakpoint);
                if (capabilities.CanGenerateFieldAccessEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.FieldAccess);
                if (capabilities.CanGenerateFieldModificationEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.FieldModification);
                if (capabilities.CanGenerateMethodEntryEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MethodEntry);
                if (capabilities.CanGenerateMethodExitEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MethodExit);
                if (capabilities.CanGenerateNativeMethodBindEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.NativeMethodBind);
                if (capabilities.CanGenerateCompiledMethodLoadEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.CompiledMethodLoad);
                if (capabilities.CanGenerateCompiledMethodLoadEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.CompiledMethodUnload);
                if (capabilities.CanGenerateMonitorEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MonitorWait);
                if (capabilities.CanGenerateMonitorEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MonitorWaited);
                if (capabilities.CanGenerateMonitorEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MonitorContendedEnter);
                if (capabilities.CanGenerateMonitorEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.MonitorContendedEntered);
                if (capabilities.CanGenerateGarbageCollectionEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.GarbageCollectionStart);
                if (capabilities.CanGenerateGarbageCollectionEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.GarbageCollectionFinish);
                if (capabilities.CanGenerateObjectFreeEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.ObjectFree);
                if (capabilities.CanGenerateVmObjectAllocEvents)
                    environment.SetEventNotificationMode(JvmEventMode.Enable, JvmEventType.VMObjectAlloc);
#endif
            }
            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);
            }
Exemplo n.º 49
0
 internal static JvmtiEnvironment GetOrCreateInstance(JavaVM virtualMachine, jvmtiEnvHandle handle)
 {
     return _instances.GetOrAdd(handle, i => CreateVirtualMachine(virtualMachine, i));
 }
 private void HandleVMStart(jvmtiEnvHandle env, JNIEnvHandle jniEnv)
 {
     /**********************
      * Note: there are no dispatchers available at this point.
      */
 }
Exemplo n.º 51
0
 private static JvmtiEnvironment CreateVirtualMachine(JavaVM virtualMachine, jvmtiEnvHandle handle)
 {
     return new JvmtiEnvironment(virtualMachine, handle);
 }
            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);
                //}
            }
Exemplo n.º 53
0
        private JvmEnvironment(jvmtiEnvHandle env)
        {
            Contract.Requires <ArgumentException>(env != jvmtiEnvHandle.Null);

            jvmtiInterface rawInterface = (jvmtiInterface)Marshal.PtrToStructure(Marshal.ReadIntPtr(env.Handle), typeof(jvmtiInterface));

            Contract.Assert(rawInterface.GetCapabilities != null);
            Contract.Assert(rawInterface.Allocate != null);
            Contract.Assert(rawInterface.Deallocate != null);
            Contract.Assert(rawInterface.GetThreadState != null);
            Contract.Assert(rawInterface.GetCurrentThread != null);
            Contract.Assert(rawInterface.GetAllThreads != null);
            Contract.Assert(rawInterface.GetThreadInfo != null);
            Contract.Assert(rawInterface.RunAgentThread != null);
            Contract.Assert(rawInterface.SetThreadLocalStorage != null);
            Contract.Assert(rawInterface.GetThreadLocalStorage != null);
            Contract.Assert(rawInterface.GetTopThreadGroups != null);
            Contract.Assert(rawInterface.GetThreadGroupInfo != null);
            Contract.Assert(rawInterface.GetThreadGroupChildren != null);
            Contract.Assert(rawInterface.GetStackTrace != null);
            Contract.Assert(rawInterface.GetAllStackTraces != null);
            Contract.Assert(rawInterface.GetThreadListStackTraces != null);
            Contract.Assert(rawInterface.GetFrameCount != null);
            Contract.Assert(rawInterface.GetFrameLocation != null);
            Contract.Assert(rawInterface.ForceGarbageCollection != null);
            Contract.Assert(rawInterface.GetLoadedClasses != null);
            Contract.Assert(rawInterface.GetClassLoaderClasses != null);
            Contract.Assert(rawInterface.GetClassSignature != null);
            Contract.Assert(rawInterface.GetClassStatus != null);
            Contract.Assert(rawInterface.GetClassModifiers != null);
            Contract.Assert(rawInterface.GetClassMethods != null);
            Contract.Assert(rawInterface.GetClassFields != null);
            Contract.Assert(rawInterface.GetImplementedInterfaces != null);
            Contract.Assert(rawInterface.GetClassVersionNumbers != null);
            Contract.Assert(rawInterface.IsInterface != null);
            Contract.Assert(rawInterface.IsArrayClass != null);
            Contract.Assert(rawInterface.IsModifiableClass != null);
            Contract.Assert(rawInterface.GetClassLoader != null);
            Contract.Assert(rawInterface.GetObjectSize != null);
            Contract.Assert(rawInterface.GetObjectHashCode != null);
            Contract.Assert(rawInterface.GetFieldName != null);
            Contract.Assert(rawInterface.GetFieldDeclaringClass != null);
            Contract.Assert(rawInterface.GetFieldModifiers != null);
            Contract.Assert(rawInterface.GetMethodName != null);
            Contract.Assert(rawInterface.GetMethodDeclaringClass != null);
            Contract.Assert(rawInterface.GetMethodModifiers != null);
            Contract.Assert(rawInterface.GetMaxLocals != null);
            Contract.Assert(rawInterface.GetArgumentsSize != null);
            Contract.Assert(rawInterface.GetMethodLocation != null);
            Contract.Assert(rawInterface.IsMethodNative != null);
            Contract.Assert(rawInterface.IsMethodObsolete != null);
            Contract.Assert(rawInterface.CreateRawMonitor != null);
            Contract.Assert(rawInterface.DestroyRawMonitor != null);
            Contract.Assert(rawInterface.RawMonitorEnter != null);
            Contract.Assert(rawInterface.RawMonitorExit != null);
            Contract.Assert(rawInterface.RawMonitorWait != null);
            Contract.Assert(rawInterface.RawMonitorNotify != null);
            Contract.Assert(rawInterface.RawMonitorNotifyAll != null);
            Contract.Assert(rawInterface.SetJNIFunctionTable != null);
            Contract.Assert(rawInterface.GetJNIFunctionTable != null);
            Contract.Assert(rawInterface.SetEventCallbacks != null);
            Contract.Assert(rawInterface.SetEventNotificationMode != null);
            Contract.Assert(rawInterface.GenerateEvents != null);
            Contract.Assert(rawInterface.GetExtensionFunctions != null);
            Contract.Assert(rawInterface.GetExtensionEvents != null);
            Contract.Assert(rawInterface.SetExtensionEventCallback != null);
            Contract.Assert(rawInterface.GetPotentialCapabilities != null);
            Contract.Assert(rawInterface.AddCapabilities != null);
            Contract.Assert(rawInterface.RelinquishCapabilities != null);
            Contract.Assert(rawInterface.GetCapabilities != null);
            Contract.Assert(rawInterface.GetTimerInfo != null);
            Contract.Assert(rawInterface.GetTime != null);
            Contract.Assert(rawInterface.GetAvailableProcessors != null);
            Contract.Assert(rawInterface.AddToBootstrapClassLoaderSearch != null);
            Contract.Assert(rawInterface.AddToSystemClassLoaderSearch != null);
            Contract.Assert(rawInterface.GetSystemProperties != null);
            Contract.Assert(rawInterface.GetSystemProperty != null);
            Contract.Assert(rawInterface.SetSystemProperty != null);
            Contract.Assert(rawInterface.GetPhase != null);
            Contract.Assert(rawInterface.DisposeEnvironment != null);
            Contract.Assert(rawInterface.SetEnvironmentLocalStorage != null);
            Contract.Assert(rawInterface.GetEnvironmentLocalStorage != null);
            Contract.Assert(rawInterface.GetVersionNumber != null);
            Contract.Assert(rawInterface.GetErrorName != null);
            Contract.Assert(rawInterface.SetVerboseFlag != null);
            Contract.Assert(rawInterface.GetJLocationFormat != null);

            jvmtiCapabilities capabilities;

            if (rawInterface.GetPotentialCapabilities(env, out capabilities) != jvmtiError.None)
            {
                throw new InvalidOperationException();
            }

            Contract.Assert(!capabilities.CanSuspend || rawInterface.SuspendThread != null);
            Contract.Assert(!capabilities.CanSuspend || rawInterface.SuspendThreadList != null);
            Contract.Assert(!capabilities.CanSuspend || rawInterface.ResumeThread != null);
            Contract.Assert(!capabilities.CanSuspend || rawInterface.ResumeThreadList != null);

            Contract.Assert(!capabilities.CanSignalThread || rawInterface.StopThread != null);
            Contract.Assert(!capabilities.CanSignalThread || rawInterface.InterruptThread != null);

            Contract.Assert(!capabilities.CanGetOwnedMonitorInfo || rawInterface.GetOwnedMonitorInfo != null);

            Contract.Assert(!capabilities.CanGetOwnedMonitorStackDepthInfo || rawInterface.GetOwnedMonitorStackDepthInfo != null);
            Contract.Assert(!capabilities.CanGetCurrentContendedMonitor || rawInterface.GetCurrentContendedMonitor != null);
            Contract.Assert(!capabilities.CanPopFrame || rawInterface.PopFrame != null);
            Contract.Assert(!capabilities.CanGenerateFramePopEvents || rawInterface.NotifyFramePop != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnObject != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnInt != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnLong != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnFloat != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnDouble != null);
            Contract.Assert(!capabilities.CanForceEarlyReturn || rawInterface.ForceEarlyReturnVoid != null);
            Contract.Assert(!capabilities.CanTagObjects || rawInterface.FollowReferences != null);
            Contract.Assert(!capabilities.CanTagObjects || rawInterface.IterateThroughHeap != null);
            Contract.Assert(!capabilities.CanTagObjects || rawInterface.GetTag != null);
            Contract.Assert(!capabilities.CanTagObjects || rawInterface.SetTag != null);
            Contract.Assert(!capabilities.CanTagObjects || rawInterface.GetObjectsWithTags != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalObject != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalInstance != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalInt != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalLong != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalFloat != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalDouble != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.SetLocalObject != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.SetLocalInt != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.SetLocalLong != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.SetLocalFloat != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.SetLocalDouble != null);
            Contract.Assert(!capabilities.CanGenerateBreakpointEvents || rawInterface.SetBreakpoint != null);
            Contract.Assert(!capabilities.CanGenerateBreakpointEvents || rawInterface.ClearBreakpoint != null);
            Contract.Assert(!capabilities.CanGenerateFieldAccessEvents || rawInterface.SetFieldAccessWatch != null);
            Contract.Assert(!capabilities.CanGenerateFieldAccessEvents || rawInterface.ClearFieldAccessWatch != null);
            Contract.Assert(!capabilities.CanGenerateFieldModificationEvents || rawInterface.SetFieldModificationWatch != null);
            Contract.Assert(!capabilities.CanGenerateFieldModificationEvents || rawInterface.ClearFieldModificationWatch != null);

            Contract.Assert(!capabilities.CanGetSourceFileName || rawInterface.GetSourceFileName != null);

            Contract.Assert(!capabilities.CanGetConstantPool || rawInterface.GetConstantPool != null);

            Contract.Assert(!capabilities.CanGetSourceDebugExtension || rawInterface.GetSourceDebugExtension != null);
            Contract.Assert(!capabilities.CanRetransformClasses || rawInterface.RetransformClasses != null);
            Contract.Assert(!capabilities.CanRedefineClasses || rawInterface.RedefineClasses != null);

            Contract.Assert(!capabilities.CanGetMonitorInfo || rawInterface.GetObjectMonitorUsage != null);

            Contract.Assert(!capabilities.CanGetSyntheticAttribute || rawInterface.IsFieldSynthetic != null);
            Contract.Assert(!capabilities.CanGetLineNumbers || rawInterface.GetLineNumberTable != null);
            Contract.Assert(!capabilities.CanAccessLocalVariables || rawInterface.GetLocalVariableTable != null);
            Contract.Assert(!capabilities.CanGetBytecodes || rawInterface.GetBytecodes != null);
            Contract.Assert(!capabilities.CanGetSyntheticAttribute || rawInterface.IsMethodSynthetic != null);
            Contract.Assert(!capabilities.CanSetNativeMethodPrefix || rawInterface.SetNativeMethodPrefix != null);
            Contract.Assert(!capabilities.CanSetNativeMethodPrefix || rawInterface.SetNativeMethodPrefixes != null);

            Contract.Assert(!capabilities.CanGetCurrentThreadCpuTime || rawInterface.GetCurrentThreadCpuTimerInfo != null);
            Contract.Assert(!capabilities.CanGetCurrentThreadCpuTime || rawInterface.GetCurrentThreadCpuTime != null);
            Contract.Assert(!capabilities.CanGetThreadCpuTime || rawInterface.GetCurrentThreadCpuTimerInfo != null);
            Contract.Assert(!capabilities.CanGetThreadCpuTime || rawInterface.GetCurrentThreadCpuTime != null);

            capabilities = new jvmtiCapabilities(
                jvmtiCapabilities.CapabilityFlags1.CanTagObjects
                | jvmtiCapabilities.CapabilityFlags1.CanGetSourceFileName
                | jvmtiCapabilities.CapabilityFlags1.CanGetLineNumbers
                );
            rawInterface.AddCapabilities(env, ref capabilities);

            _env          = env;
            _rawInterface = rawInterface;

            _eventManager = new JvmEventManager(this);
            _eventManager.Attach();
        }
            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;
                }
            }
Exemplo n.º 55
0
 internal static JvmEnvironment GetEnvironment(jvmtiEnvHandle env)
 {
     return(_environments[env]);
 }