diff --git a/os_darwin.go b/os_darwin.go index ed21a79..1c91671 100644 --- a/os_darwin.go +++ b/os_darwin.go @@ -1,3 +1,3 @@ package goid -type sigset uint32 \ No newline at end of file +type sigset uint32 diff --git a/os_linux.go b/os_linux.go index 2cff8c9..43bbadd 100644 --- a/os_linux.go +++ b/os_linux.go @@ -1,3 +1,3 @@ package goid -type sigset [2]uint32 \ No newline at end of file +type sigset [2]uint32 diff --git a/p_m_go1.12.go b/p_m_go1.12.go index 3d503ed..51a6cb1 100644 --- a/p_m_go1.12.go +++ b/p_m_go1.12.go @@ -11,22 +11,22 @@ type mutex struct { type p struct { lock mutex - id int32 // Here is pid + id int32 // Here is pid } type m struct { - g0 uintptr // goroutine with scheduling stack - morebuf gobuf // gobuf arg to morestack - divmod uint32 // div/mod denominator for arm - known to liblink + g0 uintptr // goroutine with scheduling stack + morebuf gobuf // gobuf arg to morestack + divmod uint32 // div/mod denominator for arm - known to liblink // Fields not known to debuggers. - procid uint64 // for debuggers, but offset not hard-coded - gsignal uintptr // signal-handling g - goSigStack gsignalStack // Go-allocated signal handling stack - sigmask sigset // storage for saved signal mask - tls [6]uintptr // thread-local storage (for x86 extern register) - mstartfn func() - curg uintptr // current running goroutine - caughtsig uintptr // goroutine running during fatal signal - p *p // attached p for executing go code (nil if not executing go code) + procid uint64 // for debuggers, but offset not hard-coded + gsignal uintptr // signal-handling g + goSigStack gsignalStack // Go-allocated signal handling stack + sigmask sigset // storage for saved signal mask + tls [6]uintptr // thread-local storage (for x86 extern register) + mstartfn func() + curg uintptr // current running goroutine + caughtsig uintptr // goroutine running during fatal signal + p *p // attached p for executing go code (nil if not executing go code) } diff --git a/p_m_go1.13.go b/p_m_go1.13.go index 4a51333..d9ffffb 100644 --- a/p_m_go1.13.go +++ b/p_m_go1.13.go @@ -14,18 +14,18 @@ type p struct { } type m struct { - g0 uintptr // goroutine with scheduling stack - morebuf gobuf // gobuf arg to morestack - divmod uint32 // div/mod denominator for arm - known to liblink + g0 uintptr // goroutine with scheduling stack + morebuf gobuf // gobuf arg to morestack + divmod uint32 // div/mod denominator for arm - known to liblink // Fields not known to debuggers. - procid uint64 // for debuggers, but offset not hard-coded - gsignal uintptr // signal-handling g - goSigStack gsignalStack // Go-allocated signal handling stack - sigmask sigset // storage for saved signal mask - tls [6]uintptr // thread-local storage (for x86 extern register) - mstartfn func() - curg uintptr // current running goroutine - caughtsig uintptr // goroutine running during fatal signal - p *p // attached p for executing go code (nil if not executing go code) + procid uint64 // for debuggers, but offset not hard-coded + gsignal uintptr // signal-handling g + goSigStack gsignalStack // Go-allocated signal handling stack + sigmask sigset // storage for saved signal mask + tls [6]uintptr // thread-local storage (for x86 extern register) + mstartfn func() + curg uintptr // current running goroutine + caughtsig uintptr // goroutine running during fatal signal + p *p // attached p for executing go code (nil if not executing go code) } diff --git a/pid_go1.5_amd64.go b/pid_go1.5_amd64.go index f57eb35..a57e797 100644 --- a/pid_go1.5_amd64.go +++ b/pid_go1.5_amd64.go @@ -18,14 +18,18 @@ package goid -import "unsafe" +import ( + "unsafe" +) + +var _ = unsafe.Sizeof(0) //go:nosplit -func getM() uintptr +func getPid() uintptr //go:nosplit func GetPid() int { - return int((*m)(unsafe.Pointer(getM())).p.id) + return int(getPid()) } //go:linkname procPin runtime.procPin diff --git a/pid_go1.5_amd64.s b/pid_go1.5_amd64.s index 132a291..06125e5 100644 --- a/pid_go1.5_amd64.s +++ b/pid_go1.5_amd64.s @@ -21,9 +21,11 @@ #include "go_asm.h" #include "textflag.h" -// func getM() int64 -TEXT ·getM(SB),NOSPLIT,$0-8 +// func getPid() int64 +TEXT ·getPid(SB),NOSPLIT,$0-8 MOVQ (TLS), R14 MOVQ g_m(R14), R13 + MOVQ m_p(R13), R14 + MOVL p_id(R14), R13 MOVQ R13, ret+0(FP) RET diff --git a/pid_go1.5_amd64_test.go b/pid_go1.5_amd64_test.go index 17a6788..5f7ced7 100644 --- a/pid_go1.5_amd64_test.go +++ b/pid_go1.5_amd64_test.go @@ -10,7 +10,7 @@ import ( func TestPid(t *testing.T) { p1 := GetPid() p2 := getTID() - if GetPid() != getTID() { - t.Fatalf("The result of GetPid %d is not equal to procPin %d!", p1, p2) + if p1 != p2 { + t.Fatalf("The result of GetPid %d procPin %d are not equal!", p1, p2) } }