golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/internal/version/version.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  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  package version
    10  
    11  import (
    12  	"fmt"
    13  	"io"
    14  
    15  	"golang.org/x/exp/trace/internal/event"
    16  	"golang.org/x/exp/trace/internal/event/go122"
    17  )
    18  
    19  // Version represents the version of a trace file.
    20  type Version uint32
    21  
    22  const (
    23  	Go111   Version = 11
    24  	Go119   Version = 19
    25  	Go121   Version = 21
    26  	Go122   Version = 22
    27  	Go123   Version = 23
    28  	Current         = Go123
    29  )
    30  
    31  var versions = map[Version][]event.Spec{
    32  	// Go 1.11–1.21 use a different parser and are only set here for the sake of
    33  	// Version.Valid.
    34  	Go111: nil,
    35  	Go119: nil,
    36  	Go121: nil,
    37  
    38  	Go122: go122.Specs(),
    39  	// Go 1.23 adds backwards-incompatible events, but
    40  	// traces produced by Go 1.22 are also always valid
    41  	// Go 1.23 traces.
    42  	Go123: go122.Specs(),
    43  }
    44  
    45  // Specs returns the set of event.Specs for this version.
    46  func (v Version) Specs() []event.Spec {
    47  	return versions[v]
    48  }
    49  
    50  func (v Version) Valid() bool {
    51  	_, ok := versions[v]
    52  	return ok
    53  }
    54  
    55  // headerFmt is the format of the header of all Go execution traces.
    56  const headerFmt = "go 1.%d trace\x00\x00\x00"
    57  
    58  // ReadHeader reads the version of the trace out of the trace file's
    59  // header, whose prefix must be present in v.
    60  func ReadHeader(r io.Reader) (Version, error) {
    61  	var v Version
    62  	_, err := fmt.Fscanf(r, headerFmt, &v)
    63  	if err != nil {
    64  		return v, fmt.Errorf("bad file format: not a Go execution trace?")
    65  	}
    66  	if !v.Valid() {
    67  		return v, fmt.Errorf("unknown or unsupported trace version go 1.%d", v)
    68  	}
    69  	return v, nil
    70  }
    71  
    72  // WriteHeader writes a header for a trace version v to w.
    73  func WriteHeader(w io.Writer, v Version) (int, error) {
    74  	return fmt.Fprintf(w, headerFmt, v)
    75  }