github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/trace/v2/testdata/mktests.go (about)

     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  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"github.com/go-asm/go/trace/v2/raw"
    13  	"github.com/go-asm/go/trace/v2/version"
    14  	"io"
    15  	"log"
    16  	"os"
    17  	"os/exec"
    18  )
    19  
    20  func main() {
    21  	// Create command.
    22  	var trace, stderr bytes.Buffer
    23  	cmd := exec.Command("go", "run", "./testprog/main.go")
    24  	// TODO(mknyszek): Remove if goexperiment.Exectracer2 becomes the default.
    25  	cmd.Env = append(os.Environ(), "GOEXPERIMENT=exectracer2")
    26  	cmd.Stdout = &trace
    27  	cmd.Stderr = &stderr
    28  
    29  	// Run trace program; the trace will appear in stdout.
    30  	fmt.Fprintln(os.Stderr, "running trace program...")
    31  	if err := cmd.Run(); err != nil {
    32  		log.Fatalf("running trace program: %v:\n%s", err, stderr.String())
    33  	}
    34  
    35  	// Create file.
    36  	f, err := os.Create(fmt.Sprintf("./go1%d.test", version.Current))
    37  	if err != nil {
    38  		log.Fatalf("creating output file: %v", err)
    39  	}
    40  	defer f.Close()
    41  
    42  	// Write out the trace.
    43  	r, err := raw.NewReader(&trace)
    44  	if err != nil {
    45  		log.Fatalf("reading trace: %v", err)
    46  	}
    47  	w, err := raw.NewTextWriter(f, version.Current)
    48  	for {
    49  		ev, err := r.ReadEvent()
    50  		if err == io.EOF {
    51  			break
    52  		}
    53  		if err != nil {
    54  			log.Fatalf("reading trace: %v", err)
    55  		}
    56  		if err := w.WriteEvent(ev); err != nil {
    57  			log.Fatalf("writing trace: %v", err)
    58  		}
    59  	}
    60  }