1// Copyright 2023 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/* 8// Defined in trace_*.c. 9void cCalledFromGo(void); 10*/ 11import "C" 12import ( 13 "context" 14 "fmt" 15 "log" 16 "os" 17 "runtime/trace" 18) 19 20func init() { 21 register("Trace", Trace) 22} 23 24// Trace is used by TestTraceUnwindCGO. 25func 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// goCalledFromGo calls cCalledFromGo which calls back into goCalledFromC and 44// goCalledFromCThread. 45func goCalledFromGo() { 46 C.cCalledFromGo() 47} 48 49//export goCalledFromC 50func goCalledFromC() { 51 trace.Log(context.Background(), "goCalledFromC", "") 52} 53 54var goCalledFromCThreadChan = make(chan struct{}) 55 56//export goCalledFromCThread 57func goCalledFromCThread() { 58 trace.Log(context.Background(), "goCalledFromCThread", "") 59 close(goCalledFromCThreadChan) 60} 61