Source file src/cmd/trace/testdata/mktests.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  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"internal/trace/raw"
    13  	"internal/trace/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  	cmd.Stdout = &trace
    25  	cmd.Stderr = &stderr
    26  
    27  	// Run trace program; the trace will appear in stdout.
    28  	fmt.Fprintln(os.Stderr, "running trace program...")
    29  	if err := cmd.Run(); err != nil {
    30  		log.Fatalf("running trace program: %v:\n%s", err, stderr.String())
    31  	}
    32  
    33  	// Create file.
    34  	f, err := os.Create(fmt.Sprintf("./go1%d.test", version.Current))
    35  	if err != nil {
    36  		log.Fatalf("creating output file: %v", err)
    37  	}
    38  	defer f.Close()
    39  
    40  	// Write out the trace.
    41  	r, err := raw.NewReader(&trace)
    42  	if err != nil {
    43  		log.Fatalf("reading trace: %v", err)
    44  	}
    45  	w, err := raw.NewTextWriter(f, version.Current)
    46  	for {
    47  		ev, err := r.ReadEvent()
    48  		if err == io.EOF {
    49  			break
    50  		}
    51  		if err != nil {
    52  			log.Fatalf("reading trace: %v", err)
    53  		}
    54  		if err := w.WriteEvent(ev); err != nil {
    55  			log.Fatalf("writing trace: %v", err)
    56  		}
    57  	}
    58  }
    59  

View as plain text