golang 打印程序的启动流程。runtime 包示例。

package main

import (
    "fmt"
	"runtime"
)

func main() {
    for skip := 0; ; skip++ {
        pc, file, line, ok := runtime.Caller(skip)
        if !ok {
            break
        }

        p := runtime.FuncForPC(pc)
        fnfile, fnline := p.FileLine(0)

        fmt.Printf("skip = %d, pc = 0x%08X\n", skip, pc)
        fmt.Printf("  func: file = %s, line = L%03d, name = %s, entry = 0x%08X\n", fnfile, fnline, p.Name(), p.Entry())
        fmt.Printf("  call: file = %s, line = L%03d\n", file, line)
    }
}

打印程序的启动流程。

输出:

skip = 0, pc = 0x0049324B
  func: file = /tmp/sandbox841664779/prog.go, line = L010, name = main.main, entry = 0x00492FC0
  call: file = /tmp/sandbox841664779/prog.go, line = L012
skip = 1, pc = 0x00431575
  func: file = /usr/local/go-faketime/src/runtime/proc.go, line = L113, name = runtime.main, entry = 0x00431380
  call: file = /usr/local/go-faketime/src/runtime/proc.go, line = L203
skip = 2, pc = 0x0045BC30
  func: file = /usr/local/go-faketime/src/runtime/asm_amd64.s, line = L1373, name = runtime.goexit, entry = 0x0045BC30
  call: file = /usr/local/go-faketime/src/runtime/asm_amd64.s, line = L1373

 

你可能感兴趣的:(Golang)