public Error GetThisObject(out TaggedObjectId thisObject, ThreadId thread, FrameId frame)
        {
            int frameIndex = (int)frame.Handle;
            FrameLocationData[] frames = new FrameLocationData[1];
            Error errorCode = GetRawThreadFrames(out frames, thread, frameIndex, 1);
            if (errorCode != Error.None)
            {
                thisObject = default(TaggedObjectId);
                return errorCode;
            }

            frame = frames[0].FrameId;

            byte[] packet = new byte[HeaderSize + ThreadIdSize + FrameIdSize];
            int id = GetMessageId();
            SerializeHeader(packet, id, StackFrameCommand.ThisObject);
            WriteObjectId(packet, HeaderSize, thread);
            WriteFrameId(packet, HeaderSize + ThreadIdSize, frame);

            byte[] response = SendPacket(id, packet);
            errorCode = ReadErrorCode(response);
            if (errorCode != Error.None)
            {
                thisObject = default(TaggedObjectId);
                return errorCode;
            }

            int offset = HeaderSize;
            thisObject = ReadTaggedObjectId(response, ref offset);
            return Error.None;
        }
        public Error PopFrames(ThreadId thread, FrameId frame)
        {
            int frameIndex = (int)frame.Handle;
            FrameLocationData[] frames = new FrameLocationData[1];
            Error errorCode = GetRawThreadFrames(out frames, thread, frameIndex, 1);
            if (errorCode != Error.None)
                return errorCode;

            frame = frames[0].FrameId;

            byte[] packet = new byte[HeaderSize + ThreadIdSize + FrameIdSize];
            int id = GetMessageId();
            SerializeHeader(packet, id, StackFrameCommand.PopFrames);
            WriteObjectId(packet, HeaderSize, thread);
            WriteFrameId(packet, HeaderSize + ThreadIdSize, frame);

            byte[] response = SendPacket(id, packet);
            return ReadErrorCode(response);
        }
        public Error GetValues(out Value[] values, ThreadId thread, FrameId frame, int[] slots)
        {
            int frameIndex = (int)frame.Handle;
            FrameLocationData[] frames = new FrameLocationData[1];
            Error errorCode = GetRawThreadFrames(out frames, thread, frameIndex, 1);
            if (errorCode != Error.None)
            {
                values = null;
                return errorCode;
            }

            frame = frames[0].FrameId;

            VariableData[] variableData;
            errorCode = GetMethodVariableTable(out variableData, frames[0].Location.Class, frames[0].Location.Method);
            if (errorCode != Error.None)
            {
                values = null;
                return errorCode;
            }

            Tag[] tags = new Tag[slots.Length];
            for (int i = 0; i < slots.Length; i++)
            {
                tags[i] = Tag.Object;
                foreach (VariableData variable in variableData)
                {
                    if (variable.Slot != slots[i])
                        continue;

                    if (variable.CodeIndex > frames[0].Location.Index)
                        continue;

                    if (variable.CodeIndex + variable.Length <= frames[0].Location.Index)
                        continue;

                    if (string.IsNullOrEmpty(variable.Signature))
                        continue;

                    tags[i] = (Tag)variable.Signature[0];
                    break;
                }
            }

            byte[] packet = new byte[HeaderSize + ThreadIdSize + FrameIdSize + sizeof(int) + (slots.Length * (sizeof(int) + sizeof(byte)))];
            int id = GetMessageId();
            SerializeHeader(packet, id, StackFrameCommand.GetValues);
            WriteObjectId(packet, HeaderSize, thread);
            WriteFrameId(packet, HeaderSize + ThreadIdSize, frame);
            WriteInt32(packet, HeaderSize + ThreadIdSize + FrameIdSize, slots.Length);

            int baseOffset = HeaderSize + ThreadIdSize + FrameIdSize + sizeof(int);
            for (int i = 0; i < slots.Length; i++)
            {
                int slotOffset = baseOffset + i * (sizeof(int) + sizeof(byte));
                WriteInt32(packet, slotOffset, slots[i]);
                packet[slotOffset + sizeof(int)] = (byte)tags[i];
            }

            byte[] response = SendPacket(id, packet);
            errorCode = ReadErrorCode(response);
            if (errorCode != Error.None)
            {
                values = null;
                return errorCode;
            }

            int offset = HeaderSize;
            int valueCount = ReadInt32(response, ref offset);
            values = new Value[valueCount];
            for (int i = 0; i < valueCount; i++)
            {
                values[i] = ReadValue(response, ref offset);
            }

            return Error.None;
        }
        public Error SetValues(ThreadId thread, FrameId frame, int[] slots, Value[] values)
        {
            int frameIndex = (int)frame.Handle;
            FrameLocationData[] frames = new FrameLocationData[1];
            Error errorCode = GetRawThreadFrames(out frames, thread, frameIndex, 1);
            if (errorCode != Error.None)
                return errorCode;

            frame = frames[0].FrameId;

            throw new NotImplementedException();
        }
        public Error GetRawThreadFrames(out FrameLocationData[] frames, ThreadId thread, int startFrame, int length)
        {
            byte[] packet = new byte[HeaderSize + ThreadIdSize + sizeof(int) + sizeof(int)];
            int id = GetMessageId();
            SerializeHeader(packet, id, ThreadReferenceCommand.Frames);
            WriteObjectId(packet, HeaderSize, thread);
            WriteInt32(packet, HeaderSize + ThreadIdSize, startFrame);
            WriteInt32(packet, HeaderSize + ThreadIdSize + sizeof(int), length);

            byte[] response = SendPacket(id, packet);
            Error errorCode = ReadErrorCode(response);
            if (errorCode != Error.None)
            {
                frames = null;
                return errorCode;
            }

            int offset = HeaderSize;
            int frameCount = ReadInt32(response, ref offset);
            frames = new FrameLocationData[frameCount];
            for (int i = 0; i < frameCount; i++)
            {
                FrameId frameId = ReadFrameId(response, ref offset);
                Location location = ReadLocation(response, ref offset);
                frames[i] = new FrameLocationData(frameId, location);
            }

            return Error.None;
        }
        public Error GetThreadFrames(out FrameLocationData[] frames, ThreadId thread, int startFrame, int length)
        {
            Error errorCode = GetRawThreadFrames(out frames, thread, startFrame, length);
            if (errorCode != Error.None)
                return errorCode;

            for (int i = 0; i < frames.Length; i++)
            {
                frames[i] = new FrameLocationData(new FrameId(startFrame + i), frames[i].Location);
            }

            return Error.None;
        }
        public Error GetThreadFrames(ThreadId threadId, int startFrame, int length, out FrameLocationData[] frames)
        {
            frames = null;

            JniEnvironment nativeEnvironment;
            JvmtiEnvironment environment;
            jvmtiError error = GetEnvironment(out environment, out nativeEnvironment);
            if (error != jvmtiError.None)
                return GetStandardError(error);

            error = environment.GetStackTrace(nativeEnvironment, threadId, startFrame, length, out frames);
            if (error != jvmtiError.None)
                return GetStandardError(error);

            return Error.None;
        }
Exemple #8
0
 internal StackFrame GetMirrorOf(ThreadReference thread, FrameLocationData frame)
 {
     Location location = GetMirrorOf(frame.Location);
     return new StackFrame(this, frame.FrameId, thread, location);
 }
        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;
            }
        }