Пример #1
0
        private void EnumThreads()
        {
            var threads = SystemInformation.EnumThreads(_process.Id);

            _threads = new List <ThreadStack>(threads.Length);

            foreach (var thread in threads)
            {
                using (var hThread = _driver.OpenThreadHandle(ThreadAccessMask.QueryInformation, thread.Id)) {
                    if (hThread == null)
                    {
                        continue;
                    }

                    var nt = NativeThread.FromHandle(hThread.DangerousGetHandle(), false);
                    nt.GetStackLimits(_hProcess, out var stackBase, out var stackLimit);
                    if (stackBase > 0)
                    {
                        _threads.Add(new ThreadStack {
                            Base = stackBase, Limit = stackLimit, ThreadId = thread.Id, ProcessId = thread.ProcessId
                        });
                    }
                }
            }
        }
Пример #2
0
 private void Serialize(NativeThread t)
 {
     Serialize(t.ThreadId);
     Serialize(t.Name);
     Serialize(t.SuspendCount);
     Serialize(t.PriorityClass);
     Serialize(t.Priority);
     Serialize(t.ContextData);
     Serialize(t.Teb);
 }
Пример #3
0
        private static unsafe NativeProcess CreateProcessInternal(string applicationName, string commandLine, CreateProcessFlags flags, StartupInfoW startupInfo, string currentDirectory, out NativeThread firstThread)
        {
            ProcessInformation processInfo;

            bool success = CreateProcessW(applicationName, commandLine, null, null, false, (UInt32)flags, null, currentDirectory, &startupInfo, &processInfo);

            if (!success)
            {
                throw new Win32Exception();
            }

            firstThread = new NativeThread(new SafeThreadHandle(processInfo.hThread));
            return(new NativeProcess(new SafeProcessHandle(processInfo.hProcess)));
        }
Пример #4
0
 static void Main(string[] args)
 {
     foreach (var thread in SystemInformation.EnumThreads())
     {
         Console.Write($"TID={thread.Id}, PID={thread.ProcessId}");
         var nt = NativeThread.TryOpen(ThreadAccessMask.QueryInformation, thread.Id);
         if (nt != null)
         {
             nt.GetStackLimits(out var min, out var max);
             Console.Write($" Stack: 0x{min:X} 0x{max:X}");
         }
         Console.WriteLine();
     }
 }
Пример #5
0
        private void DumpThread(ThreadEntry threadEntry)
        {
            using (NativeThread thread = threadEntry.Open(ThreadAcccessRights.GetContext | ThreadAcccessRights.SuspendResume | ThreadAcccessRights.QueryInformation)) {
                walker = new StackWalker(thread, memoryReader, resolver);

                try {
                    thread.Suspend();
                    var stack = walker.Walk();
                    foreach (var frame in stack)
                    {
                        var fun = resolver.FindFunctionAtAddr((IntPtr)frame.returnAddress);
                        Console.WriteLine($"{fun.name} {fun.virtualAddress-frame.returnAddress}");
                    }
                } finally {
                    thread.Resume();
                }
            }
        }
Пример #6
0
        /// <summary>
        ///
        /// </summary>
        public virtual void OpenDevice(UsbDevice device)
        {
            //
            if ((m_UsbIntf = FindInterface(device)) == null)
            {
                Log.e("UsbStream", "Can't find the UsbInterface" + device.ToString() + ".");
                OnOpenFailure();
                return;
            }
            //
            m_UsbConnection = m_UsbMgr.OpenDevice(m_UsbDevice = device);
            m_UsbConnection.ClaimInterface(m_UsbIntf, true);
            //
            m_UsbReqIn = new UsbRequest();
            m_UsbReqIn.Initialize(m_UsbConnection, m_UsbIntf.intIn);

            // Check read buffer size.
            if (m_SizeRead == -1)
            {
                Log.i("UsbStream", "Use default read buffer size:" + (m_SizeRead = 64).ToString());
            }

#if USB_HANDLER_IN_UNITY
            //
            m_UsbBufferIn = ByteBuffer.Allocate(m_SizeRead);
            //
            m_ReadThread = new NativeThread(ReadThread_Step); //Looper
            m_ReadThread.StartLoop();                         //
#elif USB_HANDLER_IN_ANDROID
            AndroidJavaObject handler = new AndroidJavaObject
                                            ("android.unity.SafeUsbHandler", m_UsbConnection.m_SealedPtr, m_UsbReqIn.m_SealedPtr, m_SizeRead);
            handler.Call("setCallback", new CallbackProxy(this));
            m_ReadThread = new NativeThread(handler);
            m_ReadThread.Start2();
#endif
            //
            UsbManager.main.onUsbDeviceDetached += OnUsbDeviceDetached;
            if (m_OpenCallback != null)
            {
                m_OpenCallback.OnStreamOpenSuccess(this);
            }
            //
            m_IsOpen = true;
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        public override void Close()
        {
            // Forcibly close the read thread.
            if (m_ReadThread != null)
            {
                m_ReadThread.Abort(); m_ReadThread = null;
            }
            if (m_IsOpen)
            {
                //
                m_UsbConnection.ReleaseInterface(m_UsbIntf);
                m_UsbConnection.Close();

                // Clean up
                UsbManager.main.onUsbDeviceAttached -= OnUsbDeviceAttached;
                UsbManager.main.onUsbDeviceDetached -= OnUsbDeviceDetached;
                AndroidPtr.Free(ref m_UsbDevice);
                AndroidPtr.Free(ref m_UsbIntf);
                AndroidPtr.Free(ref m_UsbConnection);
                AndroidPtr.Free(ref m_UsbReqIn);
                //
                m_IsOpen = false;
            }
        }
Пример #8
0
		public NativeThread Open(ThreadAcccessRights rights = ThreadAcccessRights.All, bool inheritable = false) {
			return NativeThread.Open(ThreadId, rights, inheritable);
		}
Пример #9
0
        public ThreadViewModel(ThreadExtendedInformation info)
        {
            Info = info;

            _nativeThread = NativeThread.TryOpen(ThreadAccessMask.QueryLimitedInformation, info.ThreadId);
        }
Пример #10
0
        public static NativeProcess CreateProcess(string applicationName, string commandLine, CreateProcessFlags flags, StartupInfoW startupInfo, ProcThreadAttributeList atts, string currentDirectory, out NativeThread firstThread)
        {
            if ((flags & ~SupportedCreateProcessFlags) != 0)
            {
                throw new ArgumentException("Unsupported CreateProcessFlags given!");
            }
            if ((startupInfo.dwFlags & ~SupportedStartupInfoFlags) != 0)
            {
                throw new ArgumentException("Unsupported StartupInfoFlags given");
            }
            if (atts.IsDisposed)
            {
                throw new ObjectDisposedException("Atts");
            }

            StartupInfoExW startupInfoEx = new StartupInfoExW(startupInfo, atts);

            flags |= CreateProcessFlags.ExtendedStartupInfoPresent;

            return(CreateProcessInternal(applicationName, commandLine, flags, startupInfoEx, currentDirectory, out firstThread));
        }
Пример #11
0
        public static NativeProcess CreateProcess(string applicationName, string commandLine, CreateProcessFlags flags, StartupInfoFlags flags2, ProcThreadAttributeList atts, string currentDirectory, out NativeThread firstThread)
        {
            StartupInfoW startupInfo = new StartupInfoW(flags2);

            return(CreateProcess(applicationName, commandLine, flags, startupInfo, atts, currentDirectory, out firstThread));
        }
Пример #12
0
        public static NativeProcess CreateProcess(string applicationName, string commandLine, CreateProcessFlags flags, StartupInfoW startupInfo, string currentDirectory, out NativeThread firstThread)
        {
            if ((flags & ~SupportedCreateProcessFlags) != 0)
            {
                throw new ArgumentException("Unsupported CreateProcessFlags given!");
            }
            if ((startupInfo.dwFlags & ~SupportedStartupInfoFlags) != 0)
            {
                throw new ArgumentException("Unsupported StartupInfoFlags given");
            }

            return(CreateProcessInternal(applicationName, commandLine, flags, startupInfo, currentDirectory, out firstThread));
        }
Пример #13
0
 public StackWalker(NativeThread thread, ProcessMemoryAccessor memoryReader, SymbolResolver resolver)
 {
     walker = new DiaStackWalker();
     helper = new StackWalkHelper(thread, memoryReader, resolver);
 }
Пример #14
0
 internal StackWalkHelper(NativeThread thread, ProcessMemoryAccessor memoryAccessor, SymbolResolver resolver)
 {
     Thread         = thread;
     MemoryAccessor = memoryAccessor;
     Resolver       = resolver;
 }