Example #1
0
        internal jvmtiError AddGlobalReference(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, jobject obj)
        {
            long       tag;
            jvmtiError error = environment.GetTag(obj, out tag);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            if (tag == 0)
            {
                return(jvmtiError.IllegalArgument);
            }

            _globalReferences.AddOrUpdate(tag,
                                          t =>
            {
                return(Tuple.Create(nativeEnvironment.NewGlobalReference(obj), 1));
            },
                                          (t, v) =>
            {
                if (v.Item2 == 0)
                {
                    return(Tuple.Create(nativeEnvironment.NewGlobalReference(obj), 1));
                }
                else
                {
                    return(Tuple.Create(v.Item1, v.Item2 + 1));
                }
            });

            return(jvmtiError.None);
        }
        public LocalObjectReferenceHolder(JniEnvironment nativeEnvironment, jobject reference)
        {
            Contract.Requires<ArgumentNullException>(nativeEnvironment != null, "nativeEnvironment");

            _nativeEnvironment = nativeEnvironment;
            _reference = _nativeEnvironment.NewLocalReference(reference);
        }
        public LocalClassReferenceHolder(JniEnvironment nativeEnvironment, jclass reference)
        {
            Contract.Requires<ArgumentNullException>(nativeEnvironment != null, "nativeEnvironment");

            _nativeEnvironment = nativeEnvironment;
            _reference = (jclass)_nativeEnvironment.NewLocalReference(reference);
        }
        public LocalObjectReferenceHolder(JniEnvironment nativeEnvironment, jobject reference)
        {
            Contract.Requires <ArgumentNullException>(nativeEnvironment != null, "nativeEnvironment");

            _nativeEnvironment = nativeEnvironment;
            _reference         = _nativeEnvironment.NewLocalReference(reference);
        }
        public LocalThreadReferenceHolder(JniEnvironment nativeEnvironment, jthread reference)
        {
            Contract.Requires <ArgumentNullException>(nativeEnvironment != null, "nativeEnvironment");

            _nativeEnvironment = nativeEnvironment;
            _reference         = (jthread)_nativeEnvironment.NewLocalReference(reference);
        }
Example #6
0
        public jvalue(JavaVM virtualMachine, JvmtiEnvironment environment, JniEnvironment nativeEnvironment, Value value)
            : this()
        {
            if (value.Data == 0)
                return;

            switch (value.Tag)
            {
            case Tag.Byte:
                ByteValue = (byte)value.Data;
                break;

            case Tag.Char:
                CharValue = (char)value.Data;
                break;

            case Tag.Float:
                IntValue = (int)(uint)value.Data;
                break;

            case Tag.Int:
                IntValue = (int)value.Data;
                break;

            case Tag.Double:
                LongValue = (long)(ulong)value.Data;
                break;

            case Tag.Long:
                LongValue = value.Data;
                break;

            case Tag.Short:
                ShortValue = (short)value.Data;
                break;

            case Tag.Boolean:
                break;

            case Tag.Array:
            case Tag.Object:
            case Tag.String:
            case Tag.Thread:
            case Tag.ThreadGroup:
            case Tag.ClassLoader:
            case Tag.ClassObject:
                if (value.Data == 0)
                    return;

                ObjectValue = virtualMachine.GetLocalReferenceForObject(nativeEnvironment, new ObjectId(value.Data)).Value;
                break;

            case Tag.Void:
            case Tag.Invalid:
            default:
                throw new ArgumentException();
            }
        }
            public static EventFilter CreateFilter(EventKind internalEventKind, JvmtiEnvironment environment, JniEnvironment nativeEnvironment, RequestId requestId, SuspendPolicy suspendPolicy, ImmutableList<EventRequestModifier> modifiers)
            {
                if (modifiers.Count == 0)
                    return new PassThroughEventFilter(internalEventKind, requestId, suspendPolicy, modifiers);

                EventFilter[] elements = modifiers.Select(modifier => CreateFilter(internalEventKind, environment, nativeEnvironment, requestId, suspendPolicy, modifiers, modifier)).ToArray();
                if (elements.Length == 1)
                    return elements[0];

                return new AggregateEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, elements);
            }
Example #8
0
        internal LocalThreadReferenceHolder GetLocalReferenceForThread(JniEnvironment nativeEnvironment, ThreadId threadId)
        {
            LocalThreadReferenceHolder thread;
            var error = GetLocalReferenceForThread(nativeEnvironment, threadId, out thread);

            if (error != jvmtiError.None)
            {
                return(new LocalThreadReferenceHolder());
            }

            return(thread);
        }
Example #9
0
        internal LocalObjectReferenceHolder GetLocalReferenceForObject(JniEnvironment nativeEnvironment, ObjectId objectId)
        {
            LocalObjectReferenceHolder thread;
            jvmtiError error = GetLocalReferenceForObject(nativeEnvironment, objectId, out thread);

            if (error != jvmtiError.None)
            {
                return(new LocalObjectReferenceHolder());
            }

            return(thread);
        }
Example #10
0
        internal LocalClassReferenceHolder GetLocalReferenceForClass(JniEnvironment nativeEnvironment, ReferenceTypeId classId)
        {
            LocalClassReferenceHolder thread;
            var error = GetLocalReferenceForClass(nativeEnvironment, classId, out thread);

            if (error != jvmtiError.None)
            {
                return(new LocalClassReferenceHolder());
            }

            return(thread);
        }
        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 #12
0
        public jvmtiError GetFrameCount(JniEnvironment nativeEnvironment, ThreadId threadId, out int frameCount)
        {
            frameCount = 0;

            using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threadId))
            {
                if (!thread.IsAlive)
                {
                    return(jvmtiError.InvalidThread);
                }

                return(RawInterface.GetFrameCount(this, thread.Value, out frameCount));
            }
        }
Example #13
0
        internal jvmtiError GetLocalReferenceForClass(JniEnvironment nativeEnvironment, ReferenceTypeId typeId, out LocalClassReferenceHolder thread)
        {
            thread = default(LocalClassReferenceHolder);

            jclass     threadHandle;
            jvmtiError error = GetClass(typeId, out threadHandle);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            thread = new LocalClassReferenceHolder(nativeEnvironment, threadHandle);
            return(jvmtiError.None);
        }
Example #14
0
        internal jvmtiError GetLocalReferenceForObject(JniEnvironment nativeEnvironment, ObjectId objectId, out LocalObjectReferenceHolder thread)
        {
            thread = default(LocalObjectReferenceHolder);

            jobject    threadHandle;
            jvmtiError error = GetObject(objectId, out threadHandle);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            thread = new LocalObjectReferenceHolder(nativeEnvironment, threadHandle);
            return(jvmtiError.None);
        }
Example #15
0
        public jvmtiError GetCurrentThread(JniEnvironment nativeEnvironment, out ThreadId thread)
        {
            thread = default(ThreadId);

            jthread    threadPtr;
            jvmtiError error = RawInterface.GetCurrentThread(this, out threadPtr);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            thread = VirtualMachine.TrackLocalThreadReference(threadPtr, this, nativeEnvironment, true);
            return(jvmtiError.None);
        }
Example #16
0
        public jvmtiError GetClassSignature(JniEnvironment nativeEnvironment, ReferenceTypeId classId, out string signature, out string genericSignature)
        {
            signature        = null;
            genericSignature = null;

            using (var classHandle = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, classId))
            {
                if (!classHandle.IsAlive)
                {
                    return(jvmtiError.InvalidClass);
                }

                return(GetClassSignature(classHandle.Value, out signature, out genericSignature));
            }
        }
Example #17
0
        public jvmtiError GetLocalObject(JniEnvironment nativeEnvironment, jthread thread, int depth, int slot, out TaggedObjectId value)
        {
            value = default(TaggedObjectId);

            jobject    obj;
            jvmtiError error = RawInterface.GetLocalObject(this, thread, depth, slot, out obj);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            value = VirtualMachine.TrackLocalObjectReference(obj, this, nativeEnvironment, true);
            return(jvmtiError.None);
        }
Example #18
0
        internal jvmtiError GetLocalReferenceForThread(JniEnvironment nativeEnvironment, ThreadId threadId, out LocalThreadReferenceHolder thread)
        {
            thread = default(LocalThreadReferenceHolder);

            jthread    threadHandle;
            jvmtiError error = GetThread(threadId, out threadHandle);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            thread = new LocalThreadReferenceHolder(nativeEnvironment, threadHandle);
            return(jvmtiError.None);
        }
Example #19
0
        public jvmtiError GetMethodDeclaringClass(JniEnvironment nativeEnvironment, MethodId methodId, out TaggedReferenceTypeId declaringClass)
        {
            declaringClass = default(TaggedReferenceTypeId);

            jclass     classHandle;
            jvmtiError error = RawInterface.GetMethodDeclaringClass(this, (jmethodID)methodId, out classHandle);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            declaringClass = VirtualMachine.TrackLocalClassReference(classHandle, this, nativeEnvironment, true);
            return(jvmtiError.None);
        }
            private static EventFilter CreateFilter(EventKind internalEventKind, JvmtiEnvironment environment, JniEnvironment nativeEnvironment, RequestId requestId, SuspendPolicy suspendPolicy, ImmutableList<EventRequestModifier> modifiers, EventRequestModifier modifier)
            {
                switch (modifier.Kind)
                {
                case ModifierKind.Count:
                    return new CountEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, modifier.Count);

                case ModifierKind.ThreadFilter:
                    return new ThreadEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, modifier.Thread);

                case ModifierKind.ClassTypeFilter:
                    throw new NotImplementedException();

                case ModifierKind.ClassMatchFilter:
                    throw new NotImplementedException();

                case ModifierKind.ClassExcludeFilter:
                    throw new NotImplementedException();

                case ModifierKind.LocationFilter:
                    return new LocationEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, modifier.Location);

                case ModifierKind.ExceptionFilter:
                    return new ExceptionEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, modifier.ExceptionOrNull, modifier.Caught, modifier.Uncaught);

                case ModifierKind.FieldFilter:
                    throw new NotImplementedException();

                case ModifierKind.Step:
                    return new StepEventFilter(internalEventKind, requestId, suspendPolicy, modifiers, modifier.Thread, environment, nativeEnvironment, modifier.StepSize, modifier.StepDepth);

                case ModifierKind.InstanceFilter:
                    throw new NotImplementedException();

                case ModifierKind.SourceNameMatchFilter:
                    throw new NotImplementedException();

                case ModifierKind.Conditional:
                    throw new NotImplementedException();

                case ModifierKind.Invalid:
                default:
                    throw new ArgumentException();
                }
            }
Example #21
0
        public jvmtiError GetClassFields(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out FieldId[] fields)
        {
            fields = null;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                {
                    return(jvmtiError.InvalidClass);
                }

                int        fieldsCount;
                IntPtr     fieldsPtr;
                jvmtiError error = RawInterface.GetClassFields(this, @class.Value, out fieldsCount, out fieldsPtr);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                try
                {
                    List <FieldId> fieldList = new List <FieldId>();
                    unsafe
                    {
                        jfieldID *fieldHandles = (jfieldID *)fieldsPtr;
                        for (int i = 0; i < fieldsCount; i++)
                        {
                            if (fieldHandles[i] == jfieldID.Null)
                            {
                                continue;
                            }

                            fieldList.Add(fieldHandles[i]);
                        }
                    }

                    fields = fieldList.ToArray();
                    return(jvmtiError.None);
                }
                finally
                {
                    Deallocate(fieldsPtr);
                }
            }
        }
Example #22
0
        public jvmtiError GetClassMethods(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out MethodId[] methods)
        {
            methods = null;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                {
                    return(jvmtiError.InvalidClass);
                }

                int        methodsCount;
                IntPtr     methodsPtr;
                jvmtiError error = RawInterface.GetClassMethods(this, @class.Value, out methodsCount, out methodsPtr);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                try
                {
                    List <MethodId> methodList = new List <MethodId>();
                    unsafe
                    {
                        jmethodID *methodHandles = (jmethodID *)methodsPtr;
                        for (int i = 0; i < methodsCount; i++)
                        {
                            if (methodHandles[i] == jmethodID.Null)
                            {
                                continue;
                            }

                            methodList.Add(new MethodId(methodHandles[i].Handle));
                        }
                    }

                    methods = methodList.ToArray();
                    return(jvmtiError.None);
                }
                finally
                {
                    Deallocate(methodsPtr);
                }
            }
        }
Example #23
0
        public int AttachCurrentThreadAsDaemon(JvmtiEnvironment environment, out JniEnvironment nativeEnvironment, bool agentThread)
        {
            JavaVMAttachArgs args = new JavaVMAttachArgs(jniVersion.Version1_6, IntPtr.Zero, jthreadGroup.Null);

            bool alreadyAgent = IsAgentThread.Value;

            if (agentThread && !alreadyAgent)
            {
                IsAgentThread.Value = true;
            }

            JNIEnvHandle jniEnv;
            int          error = RawInterface.AttachCurrentThreadAsDaemon(this, out jniEnv, ref args);

            if (error == 0)
            {
                bool created;
                nativeEnvironment = JniEnvironment.GetOrCreateInstance(jniEnv, out created);
                if (agentThread && !alreadyAgent)
                {
                    if (environment == null)
                    {
                        GetEnvironment(out environment);
                    }

                    jthread thread;
                    JvmtiErrorHandler.ThrowOnFailure(environment.RawInterface.GetCurrentThread(environment, out thread));
                    if (thread != jthread.Null)
                    {
                        ThreadId threadId = TrackLocalThreadReference(thread, environment, nativeEnvironment, true);
                        lock (_agentThreads)
                        {
                            _agentThreads.Add(threadId);
                        }
                    }
                }
            }
            else
            {
                nativeEnvironment = null;
            }

            return(error);
        }
Example #24
0
        public jvmtiError ResumeThreads(JniEnvironment nativeEnvironment, ThreadId[] threads, out jvmtiError[] errors)
        {
            List <Tuple <int, LocalThreadReferenceHolder> > threadsToResume = new List <Tuple <int, LocalThreadReferenceHolder> >();

            errors = new jvmtiError[threads.Length];
            for (int i = 0; i < threads.Length; i++)
            {
                LocalThreadReferenceHolder thread;
                errors[i] = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threads[i], out thread);
                if (errors[i] == jvmtiError.None && thread.IsAlive)
                {
                    int suspendCount = VirtualMachine.SuspendCounts.AddOrUpdate(threads[i], 0, (existingId, existingValue) => existingValue - 1);
                    if (suspendCount == 0)
                    {
                        threadsToResume.Add(Tuple.Create(i, thread));
                    }
                }
            }

            if (threadsToResume.Count == 0)
            {
                return(jvmtiError.None);
            }

            jthread[]    requestList        = threadsToResume.Select(i => i.Item2.Value).ToArray();
            jvmtiError[] intermediateErrors = new jvmtiError[requestList.Length];
            jvmtiError   error = RawInterface.ResumeThreadList(this, requestList.Length, requestList, intermediateErrors);

            for (int i = 0; i < intermediateErrors.Length; i++)
            {
                errors[threadsToResume[i].Item1] = intermediateErrors[i];
                if (error != jvmtiError.None || intermediateErrors[i] != jvmtiError.None)
                {
                    VirtualMachine.SuspendCounts.AddOrUpdate(threads[threadsToResume[i].Item1], 0, (existingId, existingValue) => existingValue + 1);
                }
            }

            foreach (var referencePair in threadsToResume)
            {
                referencePair.Item2.Dispose();
            }

            return(error);
        }
Example #25
0
        public jvmtiError GetStackTrace(JniEnvironment nativeEnvironment, ThreadId threadId, int startDepth, int maxFrameCount, out FrameLocationData[] frames)
        {
            frames = null;

            using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threadId))
            {
                if (!thread.IsAlive)
                {
                    return(jvmtiError.InvalidThread);
                }

                jvmtiFrameInfo[] frameBuffer = new jvmtiFrameInfo[maxFrameCount];
                int        frameCount;
                jvmtiError error = RawInterface.GetStackTrace(this, thread.Value, startDepth, maxFrameCount, frameBuffer, out frameCount);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                var frameData = new FrameLocationData[frameCount];
                for (int i = 0; i < frameCount; i++)
                {
                    TaggedReferenceTypeId declaringClass;
                    MethodId method = new MethodId(frameBuffer[i]._method.Handle);
                    ulong    index  = (ulong)frameBuffer[i]._location.Value;
                    error = GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass);
                    if (error != jvmtiError.None)
                    {
                        return(error);
                    }

                    FrameId  frameId  = new FrameId(startDepth + i);
                    Location location = new Location(declaringClass, method, index);
                    frameData[i] = new FrameLocationData(frameId, location);
                }

                frames = frameData;
                return(jvmtiError.None);
            }
        }
Example #26
0
        public jvmtiError GetAllThreads(JniEnvironment nativeEnvironment, out ThreadId[] threads)
        {
            threads = null;

            int        threadsCount;
            IntPtr     threadsPtr;
            jvmtiError error = RawInterface.GetAllThreads(this, out threadsCount, out threadsPtr);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            try
            {
                HashSet <ThreadId> threadSet = new HashSet <ThreadId>();
                unsafe
                {
                    jthread *threadHandles = (jthread *)threadsPtr;
                    for (int i = 0; i < threadsCount; i++)
                    {
                        if (threadHandles[i] == jthread.Null)
                        {
                            continue;
                        }

                        threadSet.Add(VirtualMachine.TrackLocalThreadReference(threadHandles[i], this, nativeEnvironment, true));
                    }
                }

                threadSet.ExceptWith(_virtualMachine.AgentThreads);
                threads = threadSet.ToArray();
                return(jvmtiError.None);
            }
            finally
            {
                Deallocate(threadsPtr);
            }
        }
Example #27
0
        public jvmtiError GetClassModifiers(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out AccessModifiers modifiers)
        {
            modifiers = 0;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                {
                    return(jvmtiError.InvalidClass);
                }

                JvmAccessModifiers modifiersPtr;
                jvmtiError         error = RawInterface.GetClassModifiers(this, @class.Value, out modifiersPtr);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                modifiers = (AccessModifiers)modifiersPtr;
                return(jvmtiError.None);
            }
        }
Example #28
0
        internal jvmtiError RemoveGlobalReference(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, jobject obj)
        {
            long       tag;
            jvmtiError error = environment.GetTag(obj, out tag);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            if (tag == 0)
            {
                return(jvmtiError.IllegalArgument);
            }

            var result = _globalReferences.AddOrUpdate(tag,
                                                       t =>
            {
                return(Tuple.Create(jobject.Null, 0));
            },
                                                       (t, v) =>
            {
                if (v.Item2 == 0)
                {
                    return(Tuple.Create(jobject.Null, 0));
                }
                else if (v.Item2 == 1)
                {
                    nativeEnvironment.DeleteGlobalReference(v.Item1);
                    return(Tuple.Create(jobject.Null, 0));
                }
                else
                {
                    return(Tuple.Create(v.Item1, v.Item2 - 1));
                }
            });

            return(jvmtiError.None);
        }
Example #29
0
        public jvmtiError GetImplementedInterfaces(JniEnvironment nativeEnvironment, jclass classHandle, out TaggedReferenceTypeId[] interfaces)
        {
            interfaces = null;

            int        interfacesCount;
            IntPtr     interfacesPtr;
            jvmtiError error = RawInterface.GetImplementedInterfaces(this, classHandle, out interfacesCount, out interfacesPtr);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            try
            {
                List <TaggedReferenceTypeId> interfaceList = new List <TaggedReferenceTypeId>();
                unsafe
                {
                    jclass *interfaceHandles = (jclass *)interfacesPtr;
                    for (int i = 0; i < interfacesCount; i++)
                    {
                        if (interfaceHandles[i] == jclass.Null)
                        {
                            continue;
                        }

                        interfaceList.Add(VirtualMachine.TrackLocalClassReference(interfaceHandles[i], this, nativeEnvironment, true));
                    }
                }

                interfaces = interfaceList.ToArray();
                return(jvmtiError.None);
            }
            finally
            {
                Deallocate(interfacesPtr);
            }
        }
Example #30
0
        public jvmtiError GetTopThreadGroups(JniEnvironment nativeEnvironment, out ThreadGroupId[] threadGroups)
        {
            threadGroups = null;

            int        threadsGroupsCount;
            IntPtr     threadsGroupsPtr;
            jvmtiError error = RawInterface.GetTopThreadGroups(this, out threadsGroupsCount, out threadsGroupsPtr);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            try
            {
                List <ThreadGroupId> threadGroupList = new List <ThreadGroupId>();
                unsafe
                {
                    jthreadGroup *threadGroupHandles = (jthreadGroup *)threadsGroupsPtr;
                    for (int i = 0; i < threadsGroupsCount; i++)
                    {
                        if (threadGroupHandles[i] == jthreadGroup.Null)
                        {
                            continue;
                        }

                        threadGroupList.Add((ThreadGroupId)VirtualMachine.TrackLocalObjectReference(threadGroupHandles[i], this, nativeEnvironment, true));
                    }
                }

                threadGroups = threadGroupList.ToArray();
                return(jvmtiError.None);
            }
            finally
            {
                Deallocate(threadsGroupsPtr);
            }
        }
Example #31
0
        public jvmtiError GetLoadedClasses(JniEnvironment nativeEnvironment, out TaggedReferenceTypeId[] classes)
        {
            classes = null;

            int        classCount;
            IntPtr     classesPtr;
            jvmtiError error = RawInterface.GetLoadedClasses(this, out classCount, out classesPtr);

            if (error != jvmtiError.None)
            {
                return(error);
            }

            try
            {
                List <TaggedReferenceTypeId> classList = new List <TaggedReferenceTypeId>();
                unsafe
                {
                    jclass *classHandles = (jclass *)classesPtr;
                    for (int i = 0; i < classCount; i++)
                    {
                        if (classHandles[i] == jclass.Null)
                        {
                            continue;
                        }

                        classList.Add(VirtualMachine.TrackLocalClassReference(classHandles[i], this, nativeEnvironment, true));
                    }
                }

                classes = classList.ToArray();
                return(jvmtiError.None);
            }
            finally
            {
                Deallocate(classesPtr);
            }
        }
Example #32
0
        internal jclass FindBaseClass(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, jclass classHandle, string signature)
        {
            string currentSignature;
            string genericSignature;

            JvmtiErrorHandler.ThrowOnFailure(environment.GetClassSignature(classHandle, out currentSignature, out genericSignature));
            if (currentSignature == signature)
            {
                return((jclass)nativeEnvironment.NewGlobalReference(classHandle));
            }

            jclass superClass = nativeEnvironment.GetSuperclass(classHandle);

            if (superClass == jclass.Null)
            {
                return(jclass.Null);
            }

            jclass result = FindBaseClass(environment, nativeEnvironment, superClass, signature);

            nativeEnvironment.DeleteLocalReference(superClass);
            return(result);
        }
Example #33
0
        internal void HandleVMInit(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, jthread thread)
        {
            jclass threadClass = nativeEnvironment.GetObjectClass(thread);

            _threadClass = FindBaseClass(environment, nativeEnvironment, threadClass, "Ljava/lang/Thread;");

            {
                jclass classClass = nativeEnvironment.GetObjectClass(threadClass);
                _classClass = FindBaseClass(environment, nativeEnvironment, classClass, "Ljava/lang/Class;");
                nativeEnvironment.DeleteLocalReference(classClass);
            }

            {
                jvmtiThreadInfo threadInfo;
                JvmtiErrorHandler.ThrowOnFailure(environment.GetThreadInfo(thread, out threadInfo));
                jclass threadGroupClass = nativeEnvironment.GetObjectClass(threadInfo._threadGroup);
                _threadGroupClass = FindBaseClass(environment, nativeEnvironment, threadGroupClass, "Ljava/lang/ThreadGroup;");

                jclass classLoaderClass = nativeEnvironment.GetObjectClass(threadInfo._contextClassLoader);
                _classLoaderClass = FindBaseClass(environment, nativeEnvironment, classLoaderClass, "Ljava/lang/ClassLoader;");
                nativeEnvironment.DeleteLocalReference(classLoaderClass);

                nativeEnvironment.DeleteLocalReference(threadGroupClass);
                nativeEnvironment.DeleteLocalReference(threadInfo._contextClassLoader);
                nativeEnvironment.DeleteLocalReference(threadInfo._threadGroup);
                environment.Deallocate(threadInfo._name);
            }

            nativeEnvironment.DeleteLocalReference(threadClass);

            jobject stringObject = nativeEnvironment.NewString(string.Empty);
            jclass  stringClass  = nativeEnvironment.GetObjectClass(stringObject);

            _stringClass = FindBaseClass(environment, nativeEnvironment, stringClass, "Ljava/lang/String;");
            nativeEnvironment.DeleteLocalReference(stringObject);
            nativeEnvironment.DeleteLocalReference(stringClass);
        }
Example #34
0
        public jvmtiError ResumeThread(JniEnvironment nativeEnvironment, ThreadId threadId)
        {
            using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threadId))
            {
                if (!thread.IsAlive)
                {
                    return(jvmtiError.InvalidThread);
                }

                int suspendCount = VirtualMachine.SuspendCounts.AddOrUpdate(threadId, 0, (existingId, existingValue) => Math.Max(0, existingValue - 1));
                if (suspendCount > 0)
                {
                    return(jvmtiError.None);
                }

                jvmtiError error = RawInterface.ResumeThread(this, thread.Value);
                if (error != jvmtiError.None)
                {
                    VirtualMachine.SuspendCounts.AddOrUpdate(threadId, 0, (existingId, existingValue) => existingValue + 1);
                }

                return(error);
            }
        }
Example #35
0
        public jvmtiError GetSourceFileName(JniEnvironment nativeEnvironment, ReferenceTypeId classId, out string sourceName)
        {
            sourceName = null;

            using (var classHandle = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, classId))
            {
                if (!classHandle.IsAlive)
                {
                    return(jvmtiError.InvalidClass);
                }

                IntPtr     sourceNamePtr;
                jvmtiError error = RawInterface.GetSourceFileName(this, classHandle.Value, out sourceNamePtr);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                try
                {
                    unsafe
                    {
                        if (sourceNamePtr != IntPtr.Zero)
                        {
                            sourceName = ModifiedUTF8Encoding.GetString((byte *)sourceNamePtr);
                        }
                    }

                    return(jvmtiError.None);
                }
                finally
                {
                    Deallocate(sourceNamePtr);
                }
            }
        }
            public override bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location)
            {
                foreach (EventFilter filter in _filters)
                {
                    if (!filter.ProcessEvent(environment, nativeEnvironment, processor, thread, @class, location))
                        return false;
                }

                return true;
            }
Example #37
0
        private jvmtiError GetEnvironment(out JvmtiEnvironment environment, out JniEnvironment nativeEnvironment)
        {
            environment = null;
            nativeEnvironment = null;

            environment = _environment;
            int error = VirtualMachine.AttachCurrentThreadAsDaemon(environment, out nativeEnvironment, true);
            if (error != 0)
                return GetToolsErrorForJniError(error);

            //error = VirtualMachine.GetEnvironment(out environment);
            //if (error != 0)
            //    return GetToolsErrorForJniError(error);

            return jvmtiError.None;
        }
Example #38
0
        public jvmtiError GetFrameCount(JniEnvironment nativeEnvironment, ThreadId threadId, out int frameCount)
        {
            frameCount = 0;

            using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threadId))
            {
                if (!thread.IsAlive)
                    return jvmtiError.InvalidThread;

                return RawInterface.GetFrameCount(this, thread.Value, out frameCount);
            }
        }
            public StepEventFilter(EventKind internalEventKind, RequestId requestId, SuspendPolicy suspendPolicy, IEnumerable<EventRequestModifier> modifiers, ThreadId thread, JvmtiEnvironment environment, JniEnvironment nativeEnvironment, StepSize size, StepDepth depth)
                : base(internalEventKind, requestId, suspendPolicy, modifiers, thread)
            {
                if (size == StepSize.Statement && JavaVM.DisableStatementStepping)
                    size = StepSize.Line;

                _size = size;
                _depth = depth;

                // gather reference information for the thread
                using (var threadHandle = environment.VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, thread))
                {
                    if (threadHandle.IsAlive)
                    {
                        jvmtiError error = environment.GetFrameLocation(threadHandle.Value, 0, out _lastMethod, out _lastLocation);
                        if (error == jvmtiError.None)
                            error = environment.GetFrameCount(threadHandle.Value, out _stackDepth);

                        if (error == jvmtiError.None)
                            _hasMethodInfo = true;

                        UpdateLastLine(environment);

                        if (error == jvmtiError.None && size == StepSize.Statement && (depth == StepDepth.Over || depth == StepDepth.Into))
                        {
                            byte[] bytecode;
                            JvmtiErrorHandler.ThrowOnFailure(environment.GetBytecodes(_lastMethod, out bytecode));
                            _disassembledMethod = BytecodeDisassembler.Disassemble(bytecode);

                            TaggedReferenceTypeId declaringClass;
                            JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodDeclaringClass(nativeEnvironment, _lastMethod, out declaringClass));
                            using (var classHandle = environment.VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringClass.TypeId))
                            {
                                int constantPoolCount;
                                byte[] data;
                                JvmtiErrorHandler.ThrowOnFailure(environment.GetConstantPool(classHandle.Value, out constantPoolCount, out data));

                                List<ConstantPoolEntry> entryList = new List<ConstantPoolEntry>();
                                int currentPosition = 0;
                                for (int i = 0; i < constantPoolCount - 1; i++)
                                {
                                    entryList.Add(ConstantPoolEntry.FromBytes(data, ref currentPosition));
                                    switch (entryList.Last().Type)
                                    {
                                    case ConstantType.Double:
                                    case ConstantType.Long:
                                        // these entries take 2 slots
                                        entryList.Add(ConstantPoolEntry.Reserved);
                                        i++;
                                        break;

                                    default:
                                        break;
                                    }
                                }

                                _constantPool = entryList.AsReadOnly();

                                string classSignature;
                                string classGenericSignature;
                                JvmtiErrorHandler.ThrowOnFailure(environment.GetClassSignature(classHandle.Value, out classSignature, out classGenericSignature));
                                string methodName;
                                string methodSignature;
                                string methodGenericSignature;
                                JvmtiErrorHandler.ThrowOnFailure(environment.GetMethodName(_lastMethod, out methodName, out methodSignature, out methodGenericSignature));

                                jobject classLoader;
                                JvmtiErrorHandler.ThrowOnFailure(environment.GetClassLoader(classHandle.Value, out classLoader));
                                long classLoaderTag;
                                JvmtiErrorHandler.ThrowOnFailure(environment.TagClassLoader(classLoader, out classLoaderTag));

                                ReadOnlyCollection<ExceptionTableEntry> exceptionTable;
                                JvmtiErrorHandler.ThrowOnFailure(environment.VirtualMachine.GetExceptionTable(classLoaderTag, classSignature, methodName, methodSignature, out exceptionTable));

                                _evaluationStackDepths = BytecodeDisassembler.GetEvaluationStackDepths(_disassembledMethod, _constantPool, exceptionTable);
                            }
                        }
                    }
                }
            }
            private bool ShouldSkipCurrentMethod(JavaVM virtualMachine, JvmtiEnvironment environment, JniEnvironment nativeEnvironment, jthread thread, int stackDepth, Location location, out bool convertToFramePop)
            {
                Contract.Assert(_depth == StepDepth.Into || _depth == StepDepth.Over);

                convertToFramePop = false;

                if (!_hasMethodInfo || stackDepth < _stackDepth || (stackDepth == _stackDepth && (location.Method.Equals(_lastMethod))))
                    return false;

                /*
                 * change this to a Frame Pop event if we're not in a native frame
                 */
                bool native;
                jvmtiError error = environment.IsMethodNative(location.Method, out native);
                if (error != jvmtiError.None)
                    return false;

                convertToFramePop = !native;

                if (_depth == StepDepth.Over || native)
                    return true;

                JvmAccessModifiers modifiers;
                error = environment.GetMethodModifiers(location.Method, out modifiers);
                if (error != jvmtiError.None || ((modifiers & JvmAccessModifiers.Static) != 0))
                    return false;

                jobject thisObject;
                error = environment.GetLocalObject(thread, 0, 0, out thisObject);
                if (error != jvmtiError.None)
                    return false;

                try
                {
                    bool classLoader = nativeEnvironment.IsInstanceOf(thisObject, virtualMachine.ClassLoaderClass);
                    if (!classLoader)
                        return false;

                    string name;
                    string signature;
                    string genericSignature;
                    error = environment.GetMethodName(location.Method, out name, out signature, out genericSignature);
                    if (error != jvmtiError.None)
                        return false;

                    if (name == "loadClass" && signature == "(Ljava/lang/String;)Ljava/lang/Class;")
                        return true;

                    if (name == "checkPackageAccess" && signature == "(Ljava/lang/Class;Ljava/security/ProtectionDomain;)V")
                        return true;

                    return false;
                }
                finally
                {
                    nativeEnvironment.DeleteLocalReference(thisObject);
                }
            }
Example #41
0
        public jvmtiError GetClassModifiers(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out AccessModifiers modifiers)
        {
            modifiers = 0;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                    return jvmtiError.InvalidClass;

                JvmAccessModifiers modifiersPtr;
                jvmtiError error = RawInterface.GetClassModifiers(this, @class.Value, out modifiersPtr);
                if (error != jvmtiError.None)
                    return error;

                modifiers = (AccessModifiers)modifiersPtr;
                return jvmtiError.None;
            }
        }
 private void InitializeAgentThread(JvmtiEnvironment environment, JniEnvironment nativeEnvironment)
 {
     _agentStartedEvent = new ManualResetEventSlim(false);
     _agentThread = CreateAgentThread(nativeEnvironment);
     _agentCallbackDelegate = AgentDispatcherThread;
     JvmtiErrorHandler.ThrowOnFailure(environment.RawInterface.RunAgentThread(environment, _agentThread, _agentCallbackDelegate, IntPtr.Zero, JvmThreadPriority.Maximum));
     _agentStartedEvent.Wait();
 }
Example #43
0
        public jvmtiError GetClassFields(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out FieldId[] fields)
        {
            fields = null;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                    return jvmtiError.InvalidClass;

                int fieldsCount;
                IntPtr fieldsPtr;
                jvmtiError error = RawInterface.GetClassFields(this, @class.Value, out fieldsCount, out fieldsPtr);
                if (error != jvmtiError.None)
                    return error;

                try
                {
                    List<FieldId> fieldList = new List<FieldId>();
                    unsafe
                    {
                        jfieldID* fieldHandles = (jfieldID*)fieldsPtr;
                        for (int i = 0; i < fieldsCount; i++)
                        {
                            if (fieldHandles[i] == jfieldID.Null)
                                continue;

                            fieldList.Add(fieldHandles[i]);
                        }
                    }

                    fields = fieldList.ToArray();
                    return jvmtiError.None;
                }
                finally
                {
                    Deallocate(fieldsPtr);
                }
            }
        }
Example #44
0
        public jvmtiError GetClassMethods(JniEnvironment nativeEnvironment, ReferenceTypeId declaringType, out MethodId[] methods)
        {
            methods = null;

            using (var @class = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, declaringType))
            {
                if ([email protected])
                    return jvmtiError.InvalidClass;

                int methodsCount;
                IntPtr methodsPtr;
                jvmtiError error = RawInterface.GetClassMethods(this, @class.Value, out methodsCount, out methodsPtr);
                if (error != jvmtiError.None)
                    return error;

                try
                {
                    List<MethodId> methodList = new List<MethodId>();
                    unsafe
                    {
                        jmethodID* methodHandles = (jmethodID*)methodsPtr;
                        for (int i = 0; i < methodsCount; i++)
                        {
                            if (methodHandles[i] == jmethodID.Null)
                                continue;

                            methodList.Add(new MethodId(methodHandles[i].Handle));
                        }
                    }

                    methods = methodList.ToArray();
                    return jvmtiError.None;
                }
                finally
                {
                    Deallocate(methodsPtr);
                }
            }
        }
Example #45
0
        public jvmtiError GetLoadedClasses(JniEnvironment nativeEnvironment, out TaggedReferenceTypeId[] classes)
        {
            classes = null;

            int classCount;
            IntPtr classesPtr;
            jvmtiError error = RawInterface.GetLoadedClasses(this, out classCount, out classesPtr);
            if (error != jvmtiError.None)
                return error;

            try
            {
                List<TaggedReferenceTypeId> classList = new List<TaggedReferenceTypeId>();
                unsafe
                {
                    jclass* classHandles = (jclass*)classesPtr;
                    for (int i = 0; i < classCount; i++)
                    {
                        if (classHandles[i] == jclass.Null)
                            continue;

                        classList.Add(VirtualMachine.TrackLocalClassReference(classHandles[i], this, nativeEnvironment, true));
                    }
                }

                classes = classList.ToArray();
                return jvmtiError.None;
            }
            finally
            {
                Deallocate(classesPtr);
            }
        }
 public override bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location)
 {
     return true;
 }
            public Error SetEventInternal(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventKind eventKind, EventFilter filter)
            {
                EventRequestModifier locationModifier = default(EventRequestModifier);
                EventRequestModifier stepModifier = default(EventRequestModifier);

                switch (eventKind)
                {
                case EventKind.Breakpoint:
                    // we're going to need the location modifier to set the breakpoint
                    if (filter.Modifiers.Count == 0 || filter.Modifiers[0].Kind != ModifierKind.LocationFilter)
                        return Error.IllegalArgument;

                    locationModifier = filter.Modifiers[0];
                    break;

                case EventKind.SingleStep:
                    // the first modifier contains the step properties
                    if (filter.Modifiers.Count == 0 || filter.Modifiers[0].Kind != ModifierKind.Step)
                        return Error.IllegalArgument;

                    stepModifier = filter.Modifiers[0];
                    break;

                case EventKind.FramePop:
                    if (filter.InternalEventKind == EventKind.SingleStep)
                        goto case EventKind.SingleStep;

                    break;

                default:
                    break;
                }

                lock (_eventRequests)
                {
                    Dictionary<RequestId, EventFilter> requests;
                    if (!_eventRequests.TryGetValue(eventKind, out requests))
                    {
                        requests = new Dictionary<RequestId, EventFilter>();
                        _eventRequests.Add(eventKind, requests);
                    }

                    requests.Add(filter.RequestId, filter);
                    if (requests.Count == 1)
                    {
                        JvmEventType? eventToEnable = GetJvmEventType(eventKind);

                        if (eventToEnable != null)
                        {
                            jvmtiError error = Environment.SetEventNotificationMode(JvmEventMode.Enable, eventToEnable.Value);
                            if (error != jvmtiError.None)
                                return GetStandardError(error);
                        }
                    }
                }

                switch (eventKind)
                {
                case EventKind.Breakpoint:
                    {
                        Contract.Assert(locationModifier.Kind == ModifierKind.LocationFilter);
                        jmethodID methodId = locationModifier.Location.Method;
                        jlocation location = new jlocation((long)locationModifier.Location.Index);
                        jvmtiError error = Environment.SetBreakpoint(methodId, location);
                        if (error != jvmtiError.None)
                            return GetStandardError(error);

                        break;
                    }

                case EventKind.FramePop:
                    if (filter.InternalEventKind == EventKind.SingleStep)
                    {
                        using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, stepModifier.Thread))
                        {
                            if (!thread.IsAlive)
                                return Error.InvalidThread;

                            jvmtiError error = environment.RawInterface.NotifyFramePop(environment, thread.Value, 0);
                            if (error != jvmtiError.None)
                                return GetStandardError(error);
                        }
                    }

                    break;

                default:
                    break;
                }

                return Error.None;
            }
            public override bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location)
            {
                _current++;
                if (_current == _count)
                {
                    _current = 0;
                    return true;
                }

                return false;
            }
 public abstract bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location);
Example #50
0
        public jvmtiError GetMethodDeclaringClass(JniEnvironment nativeEnvironment, MethodId methodId, out TaggedReferenceTypeId declaringClass)
        {
            declaringClass = default(TaggedReferenceTypeId);

            jclass classHandle;
            jvmtiError error = RawInterface.GetMethodDeclaringClass(this, (jmethodID)methodId, out classHandle);
            if (error != jvmtiError.None)
                return error;

            declaringClass = VirtualMachine.TrackLocalClassReference(classHandle, this, nativeEnvironment, true);
            return jvmtiError.None;
        }
            public override bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location)
            {
                if (!base.ProcessEvent(environment, nativeEnvironment, processor, thread, @class, location))
                    return false;

                // Step Out is implemented with Frame Pop events set at the correct depth
                if (_depth == StepDepth.Out)
                {
                    if (location.HasValue && !location.Value.Method.Equals(_lastMethod))
                    {
                        _lastLocation = new jlocation((long)location.Value.Index);
                        _lastMethod = location.Value.Method;
                        UpdateLastLine(environment);
                    }

                    return true;
                }

                using (var threadHandle = environment.VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, thread))
                {
                    int stackDepth;
                    JvmtiErrorHandler.ThrowOnFailure(environment.GetFrameCount(threadHandle.Value, out stackDepth));

                    if (_hasMethodInfo && stackDepth > _stackDepth)
                    {
                        bool convertToFramePop;
                        if (location.HasValue && (!_convertedToFramePop || !_framePopMethod.Equals(location.Value.Method)) && ShouldSkipCurrentMethod(processor.VirtualMachine, environment, nativeEnvironment, threadHandle.Value, stackDepth, location.Value, out convertToFramePop))
                        {
                            if (convertToFramePop)
                            {
                                // remove the single step event
                                JvmtiErrorHandler.ThrowOnFailure((jvmtiError)processor.ClearEventInternal(EventKind.FramePop, this.RequestId));
                                JvmtiErrorHandler.ThrowOnFailure((jvmtiError)processor.ClearEventInternal(EventKind.SingleStep, this.RequestId));
                                // set an actual step filter to respond when the thread arrives back in this frame
                                JvmtiErrorHandler.ThrowOnFailure((jvmtiError)processor.SetEventInternal(environment, nativeEnvironment, EventKind.FramePop, this));
                                _convertedToFramePop = true;
                                _framePopMethod = location.Value.Method;
                            }

                            return false;
                        }
                        else
                        {
                            _convertedToFramePop = false;
                            return true;
                        }
                    }
                    else if (stackDepth == _stackDepth)
                    {
                        if (_size == StepSize.Statement && _disassembledMethod != null)
                        {
                            int instructionIndex = _disassembledMethod.Instructions.FindIndex(i => (uint)i.Offset == location.Value.Index);
                            if (instructionIndex >= 0 && _evaluationStackDepths != null && (_evaluationStackDepths[instructionIndex] ?? 0) != 0)
                            {
                                return false;
                            }
                            else if (instructionIndex >= 0 && _disassembledMethod.Instructions[instructionIndex].OpCode.FlowControl == JavaFlowControl.Branch)
                            {
                                // follow branch instructions before stopping
                                return false;
                            }
                        }
                        else if (_lastLine != null)
                        {
                            // see if we're on the same line
                            LineNumberData[] lines;
                            jvmtiError error = environment.GetLineNumberTable(location.Value.Method, out lines);
                            if (error == jvmtiError.None)
                            {
                                LineNumberData entry = lines.LastOrDefault(i => i.LineCodeIndex <= (long)location.Value.Index);
                                if (entry.LineNumber == _lastLine)
                                    return false;
                            }
                        }
                    }

                    if (location.HasValue)
                    {
                        _lastLocation = new jlocation((long)location.Value.Index);
                        _lastMethod = location.Value.Method;
                        UpdateLastLine(environment);
                    }

                    _stackDepth = stackDepth;
                    return true;
                }
            }
Example #52
0
        public jvmtiError GetSourceFileName(JniEnvironment nativeEnvironment, ReferenceTypeId classId, out string sourceName)
        {
            sourceName = null;

            using (var classHandle = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, classId))
            {
                if (!classHandle.IsAlive)
                    return jvmtiError.InvalidClass;

                IntPtr sourceNamePtr;
                jvmtiError error = RawInterface.GetSourceFileName(this, classHandle.Value, out sourceNamePtr);
                if (error != jvmtiError.None)
                    return error;

                try
                {
                    unsafe
                    {
                        if (sourceNamePtr != IntPtr.Zero)
                            sourceName = ModifiedUTF8Encoding.GetString((byte*)sourceNamePtr);
                    }

                    return jvmtiError.None;
                }
                finally
                {
                    Deallocate(sourceNamePtr);
                }
            }
        }
Example #53
0
        public jvmtiError GetStackTrace(JniEnvironment nativeEnvironment, ThreadId threadId, int startDepth, int maxFrameCount, out FrameLocationData[] frames)
        {
            frames = null;

            using (var thread = VirtualMachine.GetLocalReferenceForThread(nativeEnvironment, threadId))
            {
                if (!thread.IsAlive)
                    return jvmtiError.InvalidThread;

                jvmtiFrameInfo[] frameBuffer = new jvmtiFrameInfo[maxFrameCount];
                int frameCount;
                jvmtiError error = RawInterface.GetStackTrace(this, thread.Value, startDepth, maxFrameCount, frameBuffer, out frameCount);
                if (error != jvmtiError.None)
                    return error;

                var frameData = new FrameLocationData[frameCount];
                for (int i = 0; i < frameCount; i++)
                {
                    TaggedReferenceTypeId declaringClass;
                    MethodId method = new MethodId(frameBuffer[i]._method.Handle);
                    ulong index = (ulong)frameBuffer[i]._location.Value;
                    error = GetMethodDeclaringClass(nativeEnvironment, method, out declaringClass);
                    if (error != jvmtiError.None)
                        return error;

                    FrameId frameId = new FrameId(startDepth + i);
                    Location location = new Location(declaringClass, method, index);
                    frameData[i] = new FrameLocationData(frameId, location);
                }

                frames = frameData;
                return jvmtiError.None;
            }
        }
Example #54
0
        public jvmtiError GetImplementedInterfaces(JniEnvironment nativeEnvironment, jclass classHandle, out TaggedReferenceTypeId[] interfaces)
        {
            interfaces = null;

            int interfacesCount;
            IntPtr interfacesPtr;
            jvmtiError  error = RawInterface.GetImplementedInterfaces(this, classHandle, out interfacesCount, out interfacesPtr);
            if (error != jvmtiError.None)
                return error;

            try
            {
                List<TaggedReferenceTypeId> interfaceList = new List<TaggedReferenceTypeId>();
                unsafe
                {
                    jclass* interfaceHandles = (jclass*)interfacesPtr;
                    for (int i = 0; i < interfacesCount; i++)
                    {
                        if (interfaceHandles[i] == jclass.Null)
                            continue;

                        interfaceList.Add(VirtualMachine.TrackLocalClassReference(interfaceHandles[i], this, nativeEnvironment, true));
                    }
                }

                interfaces = interfaceList.ToArray();
                return jvmtiError.None;
            }
            finally
            {
                Deallocate(interfacesPtr);
            }
        }
            public Error SetEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventKind eventKind, SuspendPolicy suspendPolicy, ImmutableList<EventRequestModifier> modifiers, bool internalRequest, out RequestId requestId)
            {
                Contract.Requires<ArgumentNullException>(modifiers != null, "modifiers");

                requestId = default(RequestId);

                EventKind internalEventKind = eventKind;

                EventRequestModifier locationModifier = default(EventRequestModifier);
                EventRequestModifier stepModifier = default(EventRequestModifier);

                switch (eventKind)
                {
                case EventKind.Breakpoint:
                    // we're going to need the location modifier to set the breakpoint
                    if (modifiers.Count == 0 || modifiers[0].Kind != ModifierKind.LocationFilter)
                        return Error.IllegalArgument;

                    locationModifier = modifiers[0];
                    break;

                case EventKind.SingleStep:
                    // the first modifier contains the step properties
                    if (modifiers.Count == 0 || modifiers[0].Kind != ModifierKind.Step)
                        return Error.IllegalArgument;

                    stepModifier = modifiers[0];
                    if (stepModifier.StepDepth == StepDepth.Out)
                    {
                        // we want to attach the filter as a frame pop request instead of a step request
                        eventKind = EventKind.FramePop;
                        internalEventKind = EventKind.SingleStep;
                    }

                    break;

                default:
                    break;
                }

                requestId = new RequestId(Interlocked.Increment(ref _nextRequestId));
                if (internalRequest)
                    requestId = new RequestId(-requestId.Id);

                EventFilter filter = EventFilter.CreateFilter(internalEventKind, environment, nativeEnvironment, requestId, suspendPolicy, modifiers);
                return SetEventInternal(environment, nativeEnvironment, eventKind, filter);
            }
Example #56
0
        public jvmtiError GetClassSignature(JniEnvironment nativeEnvironment, ReferenceTypeId classId, out string signature, out string genericSignature)
        {
            signature = null;
            genericSignature = null;

            using (var classHandle = VirtualMachine.GetLocalReferenceForClass(nativeEnvironment, classId))
            {
                if (!classHandle.IsAlive)
                    return jvmtiError.InvalidClass;

                return GetClassSignature(classHandle.Value, out signature, out genericSignature);
            }
        }
            private jvmtiError ApplySuspendPolicy(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, SuspendPolicy policy, ThreadId eventThread)
            {
                if (policy == SuspendPolicy.EventThread && eventThread == default(ThreadId))
                {
                    return jvmtiError.InvalidThread;
                }

                switch (policy)
                {
                case SuspendPolicy.None:
                    return jvmtiError.None;

                case SuspendPolicy.EventThread:
                    return environment.SuspendThread(nativeEnvironment, eventThread);

                case SuspendPolicy.All:
                    ThreadId[] requestList;
                    JvmtiErrorHandler.ThrowOnFailure(environment.GetAllThreads(nativeEnvironment, out requestList));
                    jvmtiError[] errors;
                    return environment.SuspendThreads(nativeEnvironment, requestList, out errors);

                default:
                    throw new ArgumentException("Invalid suspend policy.");
                }
            }
            public override bool ProcessEvent(JvmtiEnvironment environment, JniEnvironment nativeEnvironment, EventProcessor processor, ThreadId thread, TaggedReferenceTypeId @class, Location? location)
            {
                if (!location.HasValue)
                    return false;

                return _location.Index == location.Value.Index
                    && _location.Class == location.Value.Class
                    && _location.Method == location.Value.Method;
            }
            private jthread CreateAgentThread(JniEnvironment nativeEnvironment)
            {
                jclass @class = nativeEnvironment.FindClass("java/lang/Thread");
                if (@class == jclass.Null)
                    throw new Exception("ERROR: JNI: Cannot find java/lang/Thread with FindClass.");

                jmethodID method = nativeEnvironment.GetMethodId(@class, "<init>", "()V");
                if (method == jmethodID.Null)
                    throw new Exception("Cannot find Thread constructor method.");

                jthread result = (jthread)nativeEnvironment.NewObject(@class, method);
                if (result == jthread.Null)
                    throw new Exception("Cannot create new Thread object");

                jthread agentThread = (jthread)nativeEnvironment.NewGlobalReference(result);
                if (result == jthread.Null)
                    throw new Exception("Cannot create a new global reference for the agent thread.");

                // don't need to keep the local reference around
                nativeEnvironment.DeleteLocalReference(result);

                return agentThread;
            }
Example #60
0
        public jvmtiError GetTopThreadGroups(JniEnvironment nativeEnvironment, out ThreadGroupId[] threadGroups)
        {
            threadGroups = null;

            int threadsGroupsCount;
            IntPtr threadsGroupsPtr;
            jvmtiError error = RawInterface.GetTopThreadGroups(this, out threadsGroupsCount, out threadsGroupsPtr);
            if (error != jvmtiError.None)
                return error;

            try
            {
                List<ThreadGroupId> threadGroupList = new List<ThreadGroupId>();
                unsafe
                {
                    jthreadGroup* threadGroupHandles = (jthreadGroup*)threadsGroupsPtr;
                    for (int i = 0; i < threadsGroupsCount; i++)
                    {
                        if (threadGroupHandles[i] == jthreadGroup.Null)
                            continue;

                        threadGroupList.Add((ThreadGroupId)VirtualMachine.TrackLocalObjectReference(threadGroupHandles[i], this, nativeEnvironment, true));
                    }
                }

                threadGroups = threadGroupList.ToArray();
                return jvmtiError.None;
            }
            finally
            {
                Deallocate(threadsGroupsPtr);
            }
        }