process_windows_386.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build windows
  2. package process
  3. import (
  4. "errors"
  5. "syscall"
  6. "unsafe"
  7. "github.com/shirou/gopsutil/internal/common"
  8. "golang.org/x/sys/windows"
  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 status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
  34. return uint64(info.PebBaseAddress), nil
  35. } else {
  36. return 0, windows.NTStatus(ret)
  37. }
  38. } else {
  39. //we are on a 32-bit process reading an external 64-bit process
  40. if common.ProcNtWow64QueryInformationProcess64.Find() == nil { //avoid panic
  41. var info processBasicInformation64
  42. ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call(
  43. uintptr(procHandle),
  44. uintptr(common.ProcessBasicInformation),
  45. uintptr(unsafe.Pointer(&info)),
  46. uintptr(unsafe.Sizeof(info)),
  47. uintptr(0),
  48. )
  49. if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
  50. return info.PebBaseAddress, nil
  51. } else {
  52. return 0, windows.NTStatus(ret)
  53. }
  54. } else {
  55. return 0, errors.New("can't find API to query 64 bit process from 32 bit")
  56. }
  57. }
  58. }
  59. func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte {
  60. if is32BitProcess {
  61. var read uint
  62. buffer := make([]byte, size)
  63. ret, _, _ := common.ProcNtReadVirtualMemory.Call(
  64. uintptr(h),
  65. uintptr(address),
  66. uintptr(unsafe.Pointer(&buffer[0])),
  67. uintptr(size),
  68. uintptr(unsafe.Pointer(&read)),
  69. )
  70. if int(ret) >= 0 && read > 0 {
  71. return buffer[:read]
  72. }
  73. } else {
  74. //reading a 64-bit process from a 32-bit one
  75. if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic
  76. var read uint64
  77. buffer := make([]byte, size)
  78. ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call(
  79. uintptr(h),
  80. uintptr(address & 0xFFFFFFFF), //the call expects a 64-bit value
  81. uintptr(address >> 32),
  82. uintptr(unsafe.Pointer(&buffer[0])),
  83. uintptr(size), //the call expects a 64-bit value
  84. uintptr(0), //but size is 32-bit so pass zero as the high dword
  85. uintptr(unsafe.Pointer(&read)),
  86. )
  87. if int(ret) >= 0 && read > 0 {
  88. return buffer[:uint(read)]
  89. }
  90. }
  91. }
  92. //if we reach here, an error happened
  93. return nil
  94. }