1// Copyright 2021 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7// Test taking a goroutine profile with C traceback.
8
9/*
10// Defined in gprof_c.c.
11void CallGoSleep(void);
12void gprofCgoTraceback(void* parg);
13void gprofCgoContext(void* parg);
14*/
15import "C"
16
17import (
18	"fmt"
19	"io"
20	"runtime"
21	"runtime/pprof"
22	"time"
23	"unsafe"
24)
25
26func init() {
27	register("GoroutineProfile", GoroutineProfile)
28}
29
30func GoroutineProfile() {
31	runtime.SetCgoTraceback(0, unsafe.Pointer(C.gprofCgoTraceback), unsafe.Pointer(C.gprofCgoContext), nil)
32
33	go C.CallGoSleep()
34	go C.CallGoSleep()
35	go C.CallGoSleep()
36	time.Sleep(1 * time.Second)
37
38	prof := pprof.Lookup("goroutine")
39	prof.WriteTo(io.Discard, 1)
40	fmt.Println("OK")
41}
42
43//export GoSleep
44func GoSleep() {
45	time.Sleep(time.Hour)
46}
47