process_windows_amd64.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // +build windows
  2. package process
  3. import (
  4. "syscall"
  5. "unsafe"
  6. "github.com/shirou/gopsutil/internal/common"
  7. "golang.org/x/sys/windows"
  8. )
  9. type PROCESS_MEMORY_COUNTERS struct {
  10. CB uint32
  11. PageFaultCount uint32
  12. PeakWorkingSetSize uint64
  13. WorkingSetSize uint64
  14. QuotaPeakPagedPoolUsage uint64
  15. QuotaPagedPoolUsage uint64
  16. QuotaPeakNonPagedPoolUsage uint64
  17. QuotaNonPagedPoolUsage uint64
  18. PagefileUsage uint64
  19. PeakPagefileUsage uint64
  20. }
  21. func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) {
  22. if is32BitProcess {
  23. //we are on a 64-bit process reading an external 32-bit process
  24. var wow64 uint
  25. ret, _, _ := common.ProcNtQueryInformationProcess.Call(
  26. uintptr(procHandle),
  27. uintptr(common.ProcessWow64Information),
  28. uintptr(unsafe.Pointer(&wow64)),
  29. uintptr(unsafe.Sizeof(wow64)),
  30. uintptr(0),
  31. )
  32. if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
  33. return uint64(wow64), nil
  34. } else {
  35. return 0, windows.NTStatus(ret)
  36. }
  37. } else {
  38. //we are on a 64-bit process reading an external 64-bit process
  39. var info processBasicInformation64
  40. ret, _, _ := common.ProcNtQueryInformationProcess.Call(
  41. uintptr(procHandle),
  42. uintptr(common.ProcessBasicInformation),
  43. uintptr(unsafe.Pointer(&info)),
  44. uintptr(unsafe.Sizeof(info)),
  45. uintptr(0),
  46. )
  47. if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
  48. return info.PebBaseAddress, nil
  49. } else {
  50. return 0, windows.NTStatus(ret)
  51. }
  52. }
  53. }
  54. func readProcessMemory(procHandle syscall.Handle, _ bool, address uint64, size uint) []byte {
  55. var read uint
  56. buffer := make([]byte, size)
  57. ret, _, _ := common.ProcNtReadVirtualMemory.Call(
  58. uintptr(procHandle),
  59. uintptr(address),
  60. uintptr(unsafe.Pointer(&buffer[0])),
  61. uintptr(size),
  62. uintptr(unsafe.Pointer(&read)),
  63. )
  64. if int(ret) >= 0 && read > 0 {
  65. return buffer[:read]
  66. }
  67. return nil
  68. }