github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/os/debug.go (about)

     1  package os
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"runtime/pprof"
     8  )
     9  
    10  func PrintAllGoroutines() {
    11  	// XXX is this the best way?
    12  	func() {
    13  		defer func() {
    14  			if r := recover(); r != nil {
    15  				// #1
    16  				buf := make([]byte, 1<<16)
    17  				stackSize := runtime.Stack(buf, true)
    18  				fmt.Printf("%s\n", string(buf[0:stackSize]))
    19  
    20  				// #2
    21  				os.Stdout.Write([]byte("pprof.Lookup('goroutine'):\n"))
    22  				pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
    23  			}
    24  		}()
    25  		panic("THIS_PANIC_INDUCED_FOR_DEBUGGING") // XXX
    26  	}()
    27  }