Source file src/runtime/testdata/testprogcgo/trace.go

     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  
     5  package main
     6  
     7  /*
     8  // Defined in trace_*.c.
     9  void cCalledFromGo(void);
    10  */
    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  // Trace is used by TestTraceUnwindCGO.
    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  // goCalledFromGo calls cCalledFromGo which calls back into goCalledFromC and
    44  // goCalledFromCThread.
    45  func goCalledFromGo() {
    46  	C.cCalledFromGo()
    47  }
    48  
    49  //export goCalledFromC
    50  func goCalledFromC() {
    51  	trace.Log(context.Background(), "goCalledFromC", "")
    52  }
    53  
    54  var goCalledFromCThreadChan = make(chan struct{})
    55  
    56  //export goCalledFromCThread
    57  func goCalledFromCThread() {
    58  	trace.Log(context.Background(), "goCalledFromCThread", "")
    59  	close(goCalledFromCThreadChan)
    60  }
    61  

View as plain text