...
1
2
3
4
5 package main
6
7
11 import "C"
12 import (
13 "context"
14 "fmt"
15 "log"
16 "os"
17 "runtime/trace"
18 )
19
20 func init() {
21 register("Trace", Trace)
22 }
23
24
25 func Trace() {
26 file, err := os.CreateTemp("", "testprogcgo_trace")
27 if err != nil {
28 log.Fatalf("failed to create temp file: %s", err)
29 }
30 defer file.Close()
31
32 if err := trace.Start(file); err != nil {
33 log.Fatal(err)
34 }
35 defer trace.Stop()
36
37 goCalledFromGo()
38 <-goCalledFromCThreadChan
39
40 fmt.Printf("trace path:%s", file.Name())
41 }
42
43
44
45 func goCalledFromGo() {
46 C.cCalledFromGo()
47 }
48
49
50 func goCalledFromC() {
51 trace.Log(context.Background(), "goCalledFromC", "")
52 }
53
54 var goCalledFromCThreadChan = make(chan struct{})
55
56
57 func goCalledFromCThread() {
58 trace.Log(context.Background(), "goCalledFromCThread", "")
59 close(goCalledFromCThreadChan)
60 }
61
View as plain text