process_windows_arm64.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //go:build windows && arm64
  2. // +build windows,arm64
  3. package process
  4. import (
  5. "errors"
  6. "syscall"
  7. "unsafe"
  8. "github.com/shirou/gopsutil/internal/common"
  9. )
  10. type PROCESS_MEMORY_COUNTERS struct {
  11. CB uint32
  12. PageFaultCount uint32
  13. PeakWorkingSetSize uint32
  14. WorkingSetSize uint32
  15. QuotaPeakPagedPoolUsage uint32
  16. QuotaPagedPoolUsage uint32
  17. QuotaPeakNonPagedPoolUsage uint32
  18. QuotaNonPagedPoolUsage uint32
  19. PagefileUsage uint32
  20. PeakPagefileUsage uint32
  21. }
  22. func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) {
  23. if is32BitProcess {
  24. //we are on a 32-bit process reading an external 32-bit process
  25. var info processBasicInformation32
  26. ret, _, _ := common.ProcNtQueryInformationProcess.Call(
  27. uintptr(procHandle),
  28. uintptr(common.ProcessBasicInformation),
  29. uintptr(unsafe.Pointer(&info)),
  30. uintptr(unsafe.Sizeof(info)),
  31. uintptr(0),
  32. )
  33. if int(ret) >= 0 {
  34. return uint64(info.PebBaseAddress), nil
  35. }
  36. } else {
  37. //we are on a 32-bit process reading an external 64-bit process
  38. if common.ProcNtWow64QueryInformationProcess64.Find() == nil { //avoid panic
  39. var info processBasicInformation64
  40. ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call(
  41. uintptr(procHandle),
  42. uintptr(common.ProcessBasicInformation),
  43. uintptr(unsafe.Pointer(&info)),
  44. uintptr(unsafe.Sizeof(info)),
  45. uintptr(0),
  46. )
  47. if int(ret) >= 0 {
  48. return info.PebBaseAddress, nil
  49. }
  50. }
  51. }
  52. //return 0 on error
  53. return 0, errors.New("could not query PEB address")
  54. }
  55. func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte {
  56. if is32BitProcess {
  57. var read uint
  58. buffer := make([]byte, size)
  59. ret, _, _ := common.ProcNtReadVirtualMemory.Call(
  60. uintptr(h),
  61. uintptr(address),
  62. uintptr(unsafe.Pointer(&buffer[0])),
  63. uintptr(size),
  64. uintptr(unsafe.Pointer(&read)),
  65. )
  66. if int(ret) >= 0 && read > 0 {
  67. return buffer[:read]
  68. }
  69. } else {
  70. //reading a 64-bit process from a 32-bit one
  71. if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic
  72. var read uint64
  73. buffer := make([]byte, size)
  74. ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call(
  75. uintptr(h),
  76. uintptr(address&0xFFFFFFFF), //the call expects a 64-bit value
  77. uintptr(address>>32),
  78. uintptr(unsafe.Pointer(&buffer[0])),
  79. uintptr(size), //the call expects a 64-bit value
  80. uintptr(0), //but size is 32-bit so pass zero as the high dword
  81. uintptr(unsafe.Pointer(&read)),
  82. )
  83. if int(ret) >= 0 && read > 0 {
  84. return buffer[:uint(read)]
  85. }
  86. }
  87. }
  88. //if we reach here, an error happened
  89. return nil
  90. }