FuncForPC misses the test function because it is inlined. Using FuncForPC 
on the results of Callers does not work correctly in the presence of 
inlining. That is essentially why CallersFrames was introduced.

The function name is missing because you're using the wrong expression for 
it. You should use frame.Function, not frame.Func.Name().
        // Func is the Func value of this call frame. This may be nil
        // for non-Go code or fully inlined functions.
Name() returns "" for a nil *Func.

BTW, You're not using the second return value of CallersFrames quite 
correctly.  The second return value from Next is not an ok value, it is a 
"more" value. If the second result is false, the first result is still ok 
and should be used.
In other words, swap your fmt.Printf and if !ok { break } statements.

On Monday, February 18, 2019 at 11:52:52 AM UTC-8, qiaojia...@qq.com wrote:
>
> *What version of Go are you using (go version)?*
> 1.9.3~latest version
>
> *What operating system and processor architecture are you using?*
> x86-64
>
> *What did you do?*
> test runtime.Callers function in a panic process, test code as below:
>
>
> package main
>
> import (
>  "fmt"
>  "runtime"
> )
>
> func test() {
>  var zero = 0
>  _ = 1/zero
> }
>
> func Stack() {
>
>  var pc [16]uintptr
>  n := runtime.Callers(0, pc[:])
>  fmt.Println(n,pc)
>  callers := pc[:n]
>  frames := runtime.CallersFrames(callers)  //use CallersFrames
>  for {
>  frame, ok := frames.Next()
>  if !ok {
>  break
>  }
>  fmt.Printf("%s %s %d\n", frame.Func.Name(), frame.File, frame.Line)
>
>  }
>
>  fmt.Println("============================")
>  var name, file string
>  var line int
>  for _, pc := range pc[:n] {
>  fn := runtime.FuncForPC(pc)     ////use FuncForPC
>  if fn == nil {
>  fmt.Println("fn = nil")
>  continue
>  }
>  file, line = fn.FileLine(pc)
>  name = fn.Name()
>  fmt.Printf("%s %s %d\n", name, file, line)
>  }
> }
>
> func main() {
>  defer func(){
>  if err:=recover();err!=nil{
>  Stack()
>  ///mt.Println()
>  }
>  }()
>
>  test()
> }
>
>
> *What did you expect to see?*
> all stack dispalyed,like:
>
> *What did you see instead?*
> miss some function missing.
>
> 9 [163520 878784 880704 556224 329504 323712 880512 341920 564417 0 0 0 0 0 0 
> 0]
> runtime.Callers /usr/local/go/src/runtime/extern.go 218
> main.Stack /tmp/sandbox512058979/main.go 16
> main.main.func1 /tmp/sandbox512058979/main.go 47
> runtime.call16 /usr/local/go/src/runtime/asm_amd64p32.s 392
> runtime.gopanic /usr/local/go/src/runtime/panic.go 513
> runtime.panicdivide /usr/local/go/src/runtime/panic.go 61 
> /tmp/sandbox512058979/main.go 10                              --------- use 
> CallerFrames miss function name
> main.main /tmp/sandbox512058979/main.go 52
> runtime.main /usr/local/go/src/runtime/proc.go 201
> ============================
> runtime.Callers /usr/local/go/src/runtime/extern.go 218
> main.Stack /tmp/sandbox512058979/main.go 16
> main.main.func1 /tmp/sandbox512058979/main.go 47
> runtime.call16 /usr/local/go/src/runtime/asm_amd64p32.s 392
> runtime.gopanic /usr/local/go/src/runtime/panic.go 514
> runtime.panicdivide /usr/local/go/src/runtime/panic.go 61main.main 
> /tmp/sandbox512058979/main.go 10                   ---------- use FuncForPC 
> miss stack main.test, and this line number is wrong
> runtime.main /usr/local/go/src/runtime/proc.go 210
> runtime.goexit /usr/local/go/src/runtime/asm_amd64p32.s 524
>
>
> *when i run program in debug mod, the result is OK.*
>            10 [4223048 4818604 4821394 4518130 4363057 4358245 4818349 
> 4821271 4370786 4527169 0 0 0 0 0 0]
>
> runtime.Callers D:/Go/src/runtime/extern.go 212
> main.Stack E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 16
> main.main.func1 E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 47
> runtime.call32 D:/Go/src/runtime/asm_amd64.s 509
> runtime.gopanic D:/Go/src/runtime/panic.go 491
> runtime.panicdivide D:/Go/src/runtime/panic.go 42
> main.test E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 10
> main.main E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 52
> runtime.main D:/Go/src/runtime/proc.go 195
> ============================
> runtime.Callers D:/Go/src/runtime/extern.go 212
> main.Stack E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 16
> main.main.func1 E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 46
> runtime.call32 D:/Go/src/runtime/asm_amd64.s 509
> runtime.gopanic D:/Go/src/runtime/panic.go 492
> runtime.panicdivide D:/Go/src/runtime/panic.go 42
> main.test E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 10
> main.main E:/Git/UDM/MicroServices/GoService/src/testactor/main.go 53
> runtime.main D:/Go/src/runtime/proc.go 204
> runtime.goexit D:/Go/src/runtime/asm_amd64.s 2338
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to