github.com/criyle/go-sandbox@v0.10.3/ptracer/context_linux.go (about) 1 package ptracer 2 3 import ( 4 "os" 5 "syscall" 6 ) 7 8 // Context is the context for current syscall trap 9 // used to retrive syscall number and arguments 10 type Context struct { 11 // Pid is current context process pid 12 Pid int 13 // current reg context (platform dependent) 14 regs syscall.PtraceRegs 15 } 16 17 var ( 18 // UseVMReadv determine whether use ProcessVMReadv syscall to read str 19 // initial true and becomes false if tried and failed with ENOSYS 20 UseVMReadv = true 21 pageSize = 4 << 10 22 ) 23 24 func init() { 25 pageSize = os.Getpagesize() 26 } 27 28 func getTrapContext(pid int) (*Context, error) { 29 var regs syscall.PtraceRegs 30 //err := syscall.PtraceGetRegs(pid, ®s) 31 err := ptraceGetRegSet(pid, ®s) 32 if err != nil { 33 return nil, err 34 } 35 return &Context{ 36 Pid: pid, 37 regs: regs, 38 }, nil 39 } 40 41 // GetString get the string from process data segment 42 func (c *Context) GetString(addr uintptr) string { 43 buff := make([]byte, syscall.PathMax) 44 if UseVMReadv { 45 if err := vmReadStr(c.Pid, addr, buff); err != nil { 46 // if ENOSYS, then disable this function 47 if no, ok := err.(syscall.Errno); ok { 48 if no == syscall.ENOSYS { 49 UseVMReadv = false 50 } 51 } 52 } else { 53 return string(buff[:clen(buff)]) 54 } 55 } 56 syscall.PtracePeekData(c.Pid, addr, buff) 57 return string(buff[:clen(buff)]) 58 }