public ModifiedUTF8StringData(string value)
     : base(IntPtr.Zero)
 {
     byte[] data = ModifiedUTF8Encoding.GetBytes(value);
     _data = Marshal.AllocHGlobal(data.Length);
     Marshal.Copy(data, 0, _data, data.Length);
 }
Example #2
0
        public jvmtiError GetClassSignature(jclass classHandle, out string signature, out string genericSignature)
        {
            signature        = null;
            genericSignature = null;

            IntPtr signaturePtr;
            IntPtr genericPtr;

            RawInterface.GetClassSignature(this, classHandle, out signaturePtr, out genericPtr);

            try
            {
                unsafe
                {
                    if (signaturePtr != IntPtr.Zero)
                    {
                        signature = ModifiedUTF8Encoding.GetString((byte *)signaturePtr);
                    }
                    if (genericPtr != IntPtr.Zero)
                    {
                        genericSignature = ModifiedUTF8Encoding.GetString((byte *)genericPtr);
                    }
                }

                return(jvmtiError.None);
            }
            finally
            {
                Deallocate(signaturePtr);
                Deallocate(genericPtr);
            }
        }
Example #3
0
        public jvmtiError GetSystemProperty(string name, out string value)
        {
            value = null;

            using (ModifiedUTF8StringData property = new ModifiedUTF8StringData(name))
            {
                IntPtr     valuePtr;
                jvmtiError error = RawInterface.GetSystemProperty(this, property, out valuePtr);
                if (error != jvmtiError.None)
                {
                    return(error);
                }

                unsafe
                {
                    if (valuePtr != IntPtr.Zero)
                    {
                        value = ModifiedUTF8Encoding.GetString((byte *)valuePtr);
                        Deallocate(valuePtr);
                    }
                }

                return(jvmtiError.None);
            }
        }
 public string GetString()
 {
     unsafe
     {
         return(ModifiedUTF8Encoding.GetString((byte *)_data));
     }
 }
Example #5
0
        public jvmtiError GetSourceFileName(JvmVirtualMachineRemoteHandle virtualMachine, JvmClassRemoteHandle @class, out string sourceName)
        {
            JavaVM machine = JavaVM.GetInstance(virtualMachine);

            string     sourceNameResult = null;
            jvmtiError result           = jvmtiError.Internal;

            machine.InvokeOnJvmThread(
                (environment) =>
            {
                jvmtiInterface rawInterface = environment.RawInterface;

                IntPtr sourceNamePtr = IntPtr.Zero;
                try
                {
                    result = rawInterface.GetSourceFileName(environment.Handle, @class, out sourceNamePtr);

                    unsafe
                    {
                        if (sourceNamePtr != IntPtr.Zero)
                        {
                            sourceNameResult = ModifiedUTF8Encoding.GetString((byte *)sourceNamePtr);
                        }
                    }
                }
                finally
                {
                    rawInterface.Deallocate(environment.Handle, sourceNamePtr);
                }
            });

            sourceName = sourceNameResult;
            return(result);
        }
Example #6
0
        public static unsafe int OnAttach(IntPtr vmPtr, IntPtr optionsPtr, IntPtr reserved)
        {
            _loaded = true;

            JavaVM vm = JavaVM.GetOrCreateInstance(new JavaVMHandle(vmPtr));

            string options = null;

            if (optionsPtr != IntPtr.Zero)
            {
                options = ModifiedUTF8Encoding.GetString((byte *)optionsPtr);
            }

            return(0);
        }
Example #7
0
        public jvmtiError GetMethodName(MethodId methodId, out string name, out string signature, out string genericSignature)
        {
            name             = null;
            signature        = null;
            genericSignature = null;

            IntPtr     namePtr;
            IntPtr     signaturePtr;
            IntPtr     genericPtr;
            jvmtiError error = RawInterface.GetMethodName(this, new jmethodID((IntPtr)methodId.Handle), out namePtr, out signaturePtr, out genericPtr);

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

            try
            {
                unsafe
                {
                    if (namePtr != IntPtr.Zero)
                    {
                        name = ModifiedUTF8Encoding.GetString((byte *)namePtr);
                    }
                    if (signaturePtr != IntPtr.Zero)
                    {
                        signature = ModifiedUTF8Encoding.GetString((byte *)signaturePtr);
                    }
                    if (genericPtr != IntPtr.Zero)
                    {
                        genericSignature = ModifiedUTF8Encoding.GetString((byte *)genericPtr);
                    }
                }
            }
            finally
            {
                Deallocate(namePtr);
                Deallocate(signaturePtr);
                Deallocate(genericPtr);
            }

            return(jvmtiError.None);
        }
Example #8
0
        public jvmtiError GetClassSignature(JvmVirtualMachineRemoteHandle virtualMachine, JvmClassRemoteHandle @class, out string signature, out string generic)
        {
            JavaVM machine = JavaVM.GetInstance(virtualMachine);

            string     signatureResult = null;
            string     genericResult   = null;
            jvmtiError result          = jvmtiError.Internal;

            machine.InvokeOnJvmThread(
                (environment) =>
            {
                jvmtiInterface rawInterface = environment.RawInterface;

                IntPtr signaturePtr = IntPtr.Zero;
                IntPtr genericPtr   = IntPtr.Zero;
                try
                {
                    result = rawInterface.GetClassSignature(environment.Handle, @class, out signaturePtr, out genericPtr);

                    unsafe
                    {
                        if (signaturePtr != IntPtr.Zero)
                        {
                            signatureResult = ModifiedUTF8Encoding.GetString((byte *)signaturePtr);
                        }
                        if (genericPtr != IntPtr.Zero)
                        {
                            genericResult = ModifiedUTF8Encoding.GetString((byte *)genericPtr);
                        }
                    }
                }
                finally
                {
                    rawInterface.Deallocate(environment.Handle, signaturePtr);
                    rawInterface.Deallocate(environment.Handle, genericPtr);
                }
            });

            signature = signatureResult;
            generic   = genericResult;
            return(result);
        }
Example #9
0
        public jvmtiError GetSourceDebugExtension(jclass classHandle, out string extension)
        {
            extension = null;

            IntPtr     sourceDebugExtensionPtr;
            jvmtiError error = RawInterface.GetSourceDebugExtension(this, classHandle, out sourceDebugExtensionPtr);

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

            if (sourceDebugExtensionPtr != IntPtr.Zero)
            {
                unsafe
                {
                    extension = ModifiedUTF8Encoding.GetString((byte *)sourceDebugExtensionPtr);
                    Deallocate(sourceDebugExtensionPtr);
                }
            }

            return(jvmtiError.None);
        }
Example #10
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 #11
0
        public static unsafe int OnLoad(IntPtr vmPtr, IntPtr optionsPtr, IntPtr reserved)
        {
            _loaded = true;

            JavaVM vm = JavaVM.GetOrCreateInstance(new JavaVMHandle(vmPtr));

            string optionsString = null;

            if (optionsPtr != IntPtr.Zero)
            {
                optionsString = ModifiedUTF8Encoding.GetString((byte *)optionsPtr);
            }

            string[] options = new string[0];
            if (optionsString != null)
            {
                options = optionsString.Split(',', ';');
            }

#if false
            // quick test
            GetEnvironmentVersion(vm);

            Action <JavaVM> action = GetEnvironmentVersion;
            IAsyncResult    result = action.BeginInvoke(vm, null, null);
            result.AsyncWaitHandle.WaitOne();
#endif

            AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve;

            if (options.Contains("ShowAgentExceptions", StringComparer.OrdinalIgnoreCase))
            {
                AppDomain.CurrentDomain.FirstChanceException += HandleFirstChanceException;
                AppDomain.CurrentDomain.UnhandledException   += HandleUnhandledException;
                //AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
            }

            if (options.Contains("DisableStatementStepping", StringComparer.OrdinalIgnoreCase))
            {
                JavaVM.DisableStatementStepping = true;
            }

            List <WaitHandle> waitHandles = new List <WaitHandle>();
            Binding           binding;

            /*
             * start the wcf services and wait for the client to connect
             */

#if false
            /* IJvmEventsService
             */
            _jvmEventsPublisherHost = new ServiceHost(typeof(JvmEventsPublisher));
            Binding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                ReceiveTimeout = TimeSpan.MaxValue,
                SendTimeout    = TimeSpan.MaxValue
            };

            _jvmEventsPublisherHost.AddServiceEndpoint(typeof(IJvmEventsService), binding, "net.pipe://localhost/Tvl.Java.DebugHost/JvmEventsService/");
            IAsyncResult jvmEventsPublisherStartResult = _jvmEventsPublisherHost.BeginOpen(null, null);
            waitHandles.Add(jvmEventsPublisherStartResult.AsyncWaitHandle);

            /* IJvmToolsInterfaceService
             */
            _jvmToolsInterfaceHost = new ServiceHost(typeof(JvmToolsInterfaceService));
            binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                ReceiveTimeout = TimeSpan.MaxValue,
                SendTimeout    = TimeSpan.MaxValue
            };

            _jvmToolsInterfaceHost.AddServiceEndpoint(typeof(IJvmToolsInterfaceService), binding, "net.pipe://localhost/Tvl.Java.DebugHost/JvmToolsInterfaceService/");
            IAsyncResult toolsInterfaceStartResult = _jvmToolsInterfaceHost.BeginOpen(null, null);
            waitHandles.Add(toolsInterfaceStartResult.AsyncWaitHandle);

            /* IJvmDebugSessionService
             */
            _jvmDebugSessionHost = new ServiceHost(typeof(JvmDebugSessionService));
            binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                ReceiveTimeout = TimeSpan.MaxValue,
                SendTimeout    = TimeSpan.MaxValue
            };

            _jvmDebugSessionHost.AddServiceEndpoint(typeof(IJvmDebugSessionService), binding, "net.pipe://localhost/Tvl.Java.DebugHost/JvmDebugSessionService/");
            IAsyncResult debugSessionStartResult = _jvmDebugSessionHost.BeginOpen(null, null);
            waitHandles.Add(debugSessionStartResult.AsyncWaitHandle);
#endif

            /* IDebugProtocolService
             */
            var debugProtocolService = new DebugProtocolService(vm);
            _debugProtocolHost = new ServiceHost(debugProtocolService);
            binding            = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                MaxReceivedMessageSize = 10 * 1024 * 1024,
                ReceiveTimeout         = TimeSpan.MaxValue,
                SendTimeout            = TimeSpan.MaxValue
            };

            _debugProtocolHost.AddServiceEndpoint(typeof(IDebugProtocolService), binding, "net.pipe://localhost/Tvl.Java.DebugHost/DebugProtocolService/");
            IAsyncResult debugProtocolStartResult = _debugProtocolHost.BeginOpen(null, null);
            waitHandles.Add(debugProtocolStartResult.AsyncWaitHandle);

            /* Wait for the services to finish opening
             */
            WaitHandle.WaitAll(waitHandles.ToArray());

            EventWaitHandle eventWaitHandle = null;
            try
            {
                eventWaitHandle = EventWaitHandle.OpenExisting(string.Format("JavaDebuggerInitHandle{0}", Process.GetCurrentProcess().Id));
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                // must have been launched without the debugger
            }

            if (eventWaitHandle != null)
            {
                eventWaitHandle.Set();
                Action           waitAction  = _debuggerAttachComplete.Wait;
                IAsyncResult     waitResult  = waitAction.BeginInvoke(vm.HandleAsyncOperationComplete, null);
                DispatcherFrame  frame       = new DispatcherFrame(true);
                JvmtiEnvironment environment = debugProtocolService.Environment;
                vm.PushDispatcherFrame(frame, environment, waitResult);
            }

            return(0);
        }