// NOTE: Func does not expose the actual unexported fields, because we return *Func
// values to users, and we want to keep them from being able to overwrite the data
// with (say) *f = Func{}.
-// All code operating on a *Func must call raw to get the *_func instead.
+// All code operating on a *Func must call raw() to get the *_func
+// or funcInfo() to get the funcInfo instead.
// A Func represents a Go function in the running binary.
type Func struct {
return (*_func)(unsafe.Pointer(f))
}
+func (f *Func) funcInfo() funcInfo {
+ fn := f.raw()
+ return funcInfo{fn, findmoduledatap(fn.entry)}
+}
+
// PCDATA and FUNCDATA table indexes.
//
// See funcdata.h and ../cmd/internal/obj/funcdata.go.
for i := 0; i < nftab; i++ {
// NOTE: ftab[nftab].entry is legal; it is the address beyond the final function.
if datap.ftab[i].entry > datap.ftab[i+1].entry {
- f1 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff]))
- f2 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff]))
+ f1 := funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff])), datap}
+ f2 := funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff])), datap}
f2name := "end"
if i+1 < nftab {
f2name = funcname(f2)
}
println("function symbol table not sorted by program counter:", hex(datap.ftab[i].entry), funcname(f1), ">", hex(datap.ftab[i+1].entry), f2name)
for j := 0; j <= i; j++ {
- print("\t", hex(datap.ftab[j].entry), " ", funcname((*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[j].funcoff]))), "\n")
+ print("\t", hex(datap.ftab[j].entry), " ", funcname(funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[j].funcoff])), datap}), "\n")
}
throw("invalid runtime symbol table")
}
// But don't use the next PC if it corresponds to a foreign object chunk
// (no pcln table, f2.pcln == 0). That chunk might have an alignment
// more than 16 bytes.
- f := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff]))
+ f := funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i].funcoff])), datap}
end := f.entry
if i+1 < nftab {
- f2 := (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff]))
+ f2 := funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[i+1].funcoff])), datap}
if f2.pcln != 0 {
end = f2.entry - 16
if end < f.entry {
// FuncForPC returns a *Func describing the function that contains the
// given program counter address, or else nil.
func FuncForPC(pc uintptr) *Func {
- return (*Func)(unsafe.Pointer(findfunc(pc)))
+ return (*Func)(unsafe.Pointer(findfunc(pc)._func))
}
// Name returns the name of the function.
func (f *Func) Name() string {
- return funcname(f.raw())
+ return funcname(f.funcInfo())
}
// Entry returns the entry address of the function.
func (f *Func) FileLine(pc uintptr) (file string, line int) {
// Pass strict=false here, because anyone can call this function,
// and they might just be wrong about targetpc belonging to f.
- file, line32 := funcline1(f.raw(), pc, false)
+ file, line32 := funcline1(f.funcInfo(), pc, false)
return file, int(line32)
}
return nil
}
-func findfunc(pc uintptr) *_func {
+type funcInfo struct {
+ *_func
+ datap *moduledata
+}
+
+func (f funcInfo) valid() bool {
+ return f._func != nil
+}
+
+func findfunc(pc uintptr) funcInfo {
datap := findmoduledatap(pc)
if datap == nil {
- return nil
+ return funcInfo{}
}
const nsub = uintptr(len(findfuncbucket{}.subbuckets))
idx++
}
}
- return (*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[idx].funcoff]))
+ return funcInfo{(*_func)(unsafe.Pointer(&datap.pclntable[datap.ftab[idx].funcoff])), datap}
}
type pcvalueCache struct {
val int32
}
-func pcvalue(f *_func, off int32, targetpc uintptr, cache *pcvalueCache, strict bool) int32 {
+func pcvalue(f funcInfo, off int32, targetpc uintptr, cache *pcvalueCache, strict bool) int32 {
if off == 0 {
return -1
}
}
}
- datap := findmoduledatap(f.entry) // inefficient
- if datap == nil {
+ if !f.valid() {
if strict && panicking == 0 {
print("runtime: no module data for ", hex(f.entry), "\n")
throw("no module data")
}
return -1
}
+ datap := f.datap
p := datap.pclntable[off:]
pc := f.entry
val := int32(-1)
return -1
}
-func cfuncname(f *_func) *byte {
- if f == nil || f.nameoff == 0 {
- return nil
- }
- datap := findmoduledatap(f.entry) // inefficient
- if datap == nil {
+func cfuncname(f funcInfo) *byte {
+ if !f.valid() || f.nameoff == 0 {
return nil
}
- return &datap.pclntable[f.nameoff]
+ return &f.datap.pclntable[f.nameoff]
}
-func funcname(f *_func) string {
+func funcname(f funcInfo) string {
return gostringnocopy(cfuncname(f))
}
-func funcnameFromNameoff(f *_func, nameoff int32) string {
- datap := findmoduledatap(f.entry) // inefficient
- if datap == nil {
+func funcnameFromNameoff(f funcInfo, nameoff int32) string {
+ datap := f.datap
+ if !f.valid() {
return ""
}
cstr := &datap.pclntable[nameoff]
return gostringnocopy(cstr)
}
-func funcfile(f *_func, fileno int32) string {
- datap := findmoduledatap(f.entry) // inefficient
- if datap == nil {
+func funcfile(f funcInfo, fileno int32) string {
+ datap := f.datap
+ if !f.valid() {
return "?"
}
return gostringnocopy(&datap.pclntable[datap.filetab[fileno]])
}
-func funcline1(f *_func, targetpc uintptr, strict bool) (file string, line int32) {
- datap := findmoduledatap(f.entry) // inefficient
- if datap == nil {
+func funcline1(f funcInfo, targetpc uintptr, strict bool) (file string, line int32) {
+ datap := f.datap
+ if !f.valid() {
return "?", 0
}
fileno := int(pcvalue(f, f.pcfile, targetpc, nil, strict))
return
}
-func funcline(f *_func, targetpc uintptr) (file string, line int32) {
+func funcline(f funcInfo, targetpc uintptr) (file string, line int32) {
return funcline1(f, targetpc, true)
}
-func funcspdelta(f *_func, targetpc uintptr, cache *pcvalueCache) int32 {
+func funcspdelta(f funcInfo, targetpc uintptr, cache *pcvalueCache) int32 {
x := pcvalue(f, f.pcsp, targetpc, cache, true)
if x&(sys.PtrSize-1) != 0 {
print("invalid spdelta ", funcname(f), " ", hex(f.entry), " ", hex(targetpc), " ", hex(f.pcsp), " ", x, "\n")
return x
}
-func pcdatavalue(f *_func, table int32, targetpc uintptr, cache *pcvalueCache) int32 {
+func pcdatavalue(f funcInfo, table int32, targetpc uintptr, cache *pcvalueCache) int32 {
if table < 0 || table >= f.npcdata {
return -1
}
return pcvalue(f, off, targetpc, cache, true)
}
-func funcdata(f *_func, i int32) unsafe.Pointer {
+func funcdata(f funcInfo, i int32) unsafe.Pointer {
if i < 0 || i >= f.nfuncdata {
return nil
}
p := add(unsafe.Pointer(&f.nfuncdata), unsafe.Sizeof(f.nfuncdata)+uintptr(f.npcdata)*4)
if sys.PtrSize == 8 && uintptr(p)&4 != 0 {
- if uintptr(unsafe.Pointer(f))&4 != 0 {
- println("runtime: misaligned func", f)
+ if uintptr(unsafe.Pointer(f._func))&4 != 0 {
+ println("runtime: misaligned func", f._func)
}
p = add(p, 4)
}
if fn == nil {
// Defer of nil function. Args don't matter.
frame.pc = 0
- frame.fn = nil
+ frame.fn = funcInfo{}
frame.argp = 0
frame.arglen = 0
frame.argmap = nil
} else {
frame.pc = fn.fn
f := findfunc(frame.pc)
- if f == nil {
+ if !f.valid() {
print("runtime: unknown pc in defer ", hex(frame.pc), "\n")
throw("unknown pc")
}
}
f := findfunc(frame.pc)
- if f == nil {
+ if !f.valid() {
if callback != nil {
print("runtime: unknown pc ", hex(frame.pc), "\n")
throw("unknown pc")
frame.fp += sys.RegSize
}
}
- var flr *_func
+ var flr funcInfo
if topofstack(f) {
frame.lr = 0
- flr = nil
+ flr = funcInfo{}
} else if usesLR && f.entry == jmpdeferPC {
// jmpdefer modifies SP/LR/PC non-atomically.
// If a profiling interrupt arrives during jmpdefer,
}
}
flr = findfunc(frame.lr)
- if flr == nil {
+ if !flr.valid() {
// This happens if you get a profiling interrupt at just the wrong time.
// In that context it is okay to stop early.
// But if callback is set, we're doing a garbage collection and must
waspanic = f.entry == sigpanicPC
// Do not unwind past the bottom of the stack.
- if flr == nil {
+ if !flr.valid() {
break
}
}
f = findfunc(frame.pc)
frame.fn = f
- if f == nil {
+ if !f.valid() {
frame.pc = x
} else if funcspdelta(f, frame.pc, &cache) == 0 {
frame.lr = x
// call, ctxt must be nil (getArgInfo will retrieve what it needs from
// the active stack frame). If this is a deferred call, ctxt must be
// the function object that was deferred.
-func getArgInfo(frame *stkframe, f *_func, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) {
+func getArgInfo(frame *stkframe, f funcInfo, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) {
arglen = uintptr(f.args)
if needArgMap && f.args == _ArgsSizeUnknown {
// Extract argument bitmaps for reflect stubs from the calls they made to reflect.
// Show what created goroutine, except main goroutine (goid 1).
pc := gp.gopc
f := findfunc(pc)
- if f != nil && showframe(f, gp, false) && gp.goid != 1 {
+ if f.valid() && showframe(f, gp, false) && gp.goid != 1 {
print("created by ", funcname(f), "\n")
tracepc := pc // back up to CALL instruction for funcline.
if pc > f.entry {
return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
}
-func showframe(f *_func, gp *g, firstFrame bool) bool {
+func showframe(f funcInfo, gp *g, firstFrame bool) bool {
g := getg()
if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
return true
return true
}
- return level > 1 || f != nil && contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
+ return level > 1 || f.valid() && contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
}
// isExportedRuntime reports whether name is an exported runtime function.
}
// Does f mark the top of a goroutine stack?
-func topofstack(f *_func) bool {
+func topofstack(f funcInfo) bool {
pc := f.entry
return pc == goexitPC ||
pc == mstartPC ||