diff --git a/go.mod b/go.mod index 9d67ddb..e4a7b66 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/1pkg/gotcha go 1.15 require ( - github.com/1pkg/golocal v0.7.0 + github.com/1pkg/golocal v0.8.0 github.com/1pkg/gomonkey v1.0.5 github.com/stretchr/testify v1.6.1 ) diff --git a/go.sum b/go.sum index 9bf4c94..7011ada 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/1pkg/golocal v0.7.0 h1:wHjXW0Cmja4CuTnl4spIv4MrKB3CaBjPdldAlHZJHjI= github.com/1pkg/golocal v0.7.0/go.mod h1:darSWWpnYPb3rdz5WsiCA1ucgFzVuTlVG6sLKlrWokg= +github.com/1pkg/golocal v0.8.0 h1:4sIPTYrYYBpWE697oF2ZMT1lhZ4+zloTaKbE9lDMbxk= +github.com/1pkg/golocal v0.8.0/go.mod h1:darSWWpnYPb3rdz5WsiCA1ucgFzVuTlVG6sLKlrWokg= github.com/1pkg/gomonkey v1.0.5 h1:vtzc5Vx699CZYjAk83Ac7NRJ+gH0xiLEYXJ/Vp+CqjY= github.com/1pkg/gomonkey v1.0.5/go.mod h1:fYSRBs8LM1seoFsRvlswGy9KCkxvsEBByqtUz54GOqQ= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/malloc.go b/malloc.go index fe03459..72109a9 100644 --- a/malloc.go +++ b/malloc.go @@ -30,22 +30,20 @@ func init() { ls := golocal.LStore(maxTracers) // patch malloc with permanent decorator gomonkey.PermanentDecorate(mallocgc, func(size uintptr, tp *tp, needzero bool) unsafe.Pointer { - // unfortunately we can't use local store get here + // unfortunately we can't use local store direct calls here // as it causes `unknown caller pc` stack fatal error. - if !ls.Locked() { - ls.Lock() - gctx, ok := ls.Store[golocal.ID()] - ls.Unlock() - if ok { - // trace allocations for caller tracer goroutine. - bytes := int64(size) - objs := int64(1) - if tp != nil { - bytes = int64(tp.size) - objs = int64(size) / bytes - } - (*gotchactx)(unsafe.Pointer(gctx)).Add(bytes, objs, 1) + id := ls.RLock() + gctxPtr, ok := ls.Store[id] + ls.RUnlock() + if ok { + // trace allocations for caller tracer goroutine. + bytes := int64(size) + objs := int64(1) + if tp != nil { + bytes = int64(tp.size) + objs = int64(size) / bytes } + (*gotchactx)(unsafe.Pointer(gctxPtr)).Add(bytes, objs, 1) } return nil }, 24, 53, []byte{