2016-05-04 21:39:44 +08:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
2020-05-01 09:44:43 +08:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2020-05-05 07:16:05 +08:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2021-07-13 20:57:40 +08:00
|
|
|
"golang.org/x/sys/windows"
|
2020-05-01 09:44:43 +08:00
|
|
|
)
|
|
|
|
|
2016-05-04 21:39:44 +08:00
|
|
|
type PROCESS_MEMORY_COUNTERS struct {
|
|
|
|
CB uint32
|
|
|
|
PageFaultCount uint32
|
|
|
|
PeakWorkingSetSize uint64
|
|
|
|
WorkingSetSize uint64
|
|
|
|
QuotaPeakPagedPoolUsage uint64
|
|
|
|
QuotaPagedPoolUsage uint64
|
|
|
|
QuotaPeakNonPagedPoolUsage uint64
|
|
|
|
QuotaNonPagedPoolUsage uint64
|
|
|
|
PagefileUsage uint64
|
|
|
|
PeakPagefileUsage uint64
|
|
|
|
}
|
2020-05-01 09:44:43 +08:00
|
|
|
|
2021-07-13 20:57:40 +08:00
|
|
|
func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) {
|
2020-05-01 09:44:43 +08:00
|
|
|
if is32BitProcess {
|
|
|
|
//we are on a 64-bit process reading an external 32-bit process
|
|
|
|
var wow64 uint
|
|
|
|
|
|
|
|
ret, _, _ := common.ProcNtQueryInformationProcess.Call(
|
|
|
|
uintptr(procHandle),
|
|
|
|
uintptr(common.ProcessWow64Information),
|
|
|
|
uintptr(unsafe.Pointer(&wow64)),
|
|
|
|
uintptr(unsafe.Sizeof(wow64)),
|
|
|
|
uintptr(0),
|
|
|
|
)
|
2021-07-13 20:57:40 +08:00
|
|
|
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
|
|
|
|
return uint64(wow64), nil
|
|
|
|
} else {
|
|
|
|
return 0, windows.NTStatus(ret)
|
2020-05-01 09:44:43 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//we are on a 64-bit process reading an external 64-bit process
|
2020-05-05 07:16:05 +08:00
|
|
|
var info processBasicInformation64
|
2020-05-01 09:44:43 +08:00
|
|
|
|
|
|
|
ret, _, _ := common.ProcNtQueryInformationProcess.Call(
|
|
|
|
uintptr(procHandle),
|
|
|
|
uintptr(common.ProcessBasicInformation),
|
|
|
|
uintptr(unsafe.Pointer(&info)),
|
|
|
|
uintptr(unsafe.Sizeof(info)),
|
|
|
|
uintptr(0),
|
|
|
|
)
|
2021-07-13 20:57:40 +08:00
|
|
|
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
|
|
|
|
return info.PebBaseAddress, nil
|
|
|
|
} else {
|
|
|
|
return 0, windows.NTStatus(ret)
|
2020-05-01 09:44:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readProcessMemory(procHandle syscall.Handle, _ bool, address uint64, size uint) []byte {
|
|
|
|
var read uint
|
|
|
|
|
|
|
|
buffer := make([]byte, size)
|
|
|
|
|
|
|
|
ret, _, _ := common.ProcNtReadVirtualMemory.Call(
|
|
|
|
uintptr(procHandle),
|
|
|
|
uintptr(address),
|
|
|
|
uintptr(unsafe.Pointer(&buffer[0])),
|
|
|
|
uintptr(size),
|
|
|
|
uintptr(unsafe.Pointer(&read)),
|
|
|
|
)
|
|
|
|
if int(ret) >= 0 && read > 0 {
|
|
|
|
return buffer[:read]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|