Skip to content

Commit

Permalink
Add support for 1.5, 1.6, 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
zellyn committed Jun 20, 2016
1 parent b7dcc76 commit 1eefe3e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion goid_go1.4.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.

// +build go1.4
// +build go1.4,!go1.5

package goid

Expand Down
2 changes: 1 addition & 1 deletion goid_go1.4.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// See http://tip.golang.org/misc/cgo/test/backdoor/thunk.s.

// +build amd64 amd64p32 arm 386
// +build go1.4
// +build go1.4,!go1.5

#include "textflag.h"

Expand Down
103 changes: 103 additions & 0 deletions goid_go1.5plus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// +build go1.5

package goid

import (
"runtime"
"strings"
"unsafe"
)

// Just enough of the structs from runtime2.go to get the offset to goid.

type stack struct {
lo uintptr
hi uintptr
}

type gobuf struct {
sp uintptr
pc uintptr
g uintptr
ctxt uintptr
ret uint64
lr uintptr
bp uintptr
}

type g15 struct {
stack stack
stackguard0 uintptr
stackguard1 uintptr

_panic uintptr
_defer uintptr
m uintptr
stackAlloc uintptr
sched gobuf
syscallsp uintptr
syscallpc uintptr
stkbar []uintptr
stkbarPos uintptr
param unsafe.Pointer
atomicstatus uint32
stackLock uint32
goid int64 // Here it is!
}

type g16plus struct {
stack stack
stackguard0 uintptr
stackguard1 uintptr

_panic uintptr
_defer uintptr
m uintptr
stackAlloc uintptr
sched gobuf
syscallsp uintptr
syscallpc uintptr
stkbar []uintptr
stkbarPos uintptr
stktopsp uintptr
param unsafe.Pointer
atomicstatus uint32
stackLock uint32
goid int64 // Here it is!
}

// Backdoor access to runtime·getg().
func getg() uintptr // in goid.s

// The goid is in the G struct, which varies from version to
// version. See runtime.h from the Go sources.

func get15() int64 {
var gg *g15
gg = (*g15)(unsafe.Pointer(getg()))
return gg.goid
}

func get16plus() int64 {
var gg *g16plus
gg = (*g16plus)(unsafe.Pointer(getg()))
return gg.goid
}

// Get returns the id of the current goroutine.
var Get func() int64

func init() {
Get = func() int64 { return 0 }

v := runtime.Version()
if strings.HasPrefix(v, "go1.") {
switch v[4] {
case '5':
Get = get15
case '6', '7':
// Tested on go1.6.2, go1.7beta2
Get = get16plus
}
}
}
12 changes: 12 additions & 0 deletions goid_go1.5plus.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Assembly to mimic runtime.getg.

// +build amd64 amd64p32
// +build go1.5

#include "textflag.h"

// func getg() uintptr
TEXT ·getg(SB),NOSPLIT,$0-8
MOVQ (TLS), BX
MOVQ BX, ret+0(FP)
RET

0 comments on commit 1eefe3e

Please sign in to comment.