Debian Codegraph @

trace/internal/version/version.go

raw symbols SCIP
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; DO NOT EDIT.
6
7 //go:build go1.23
8
9 package version
10
11 import (
12 "fmt"
13 "io"
14
15 "golang.org/x/exp/trace/internal/tracev2"
16 )
17
18 // Version represents the version of a trace file.
19 type Version uint32
20
21 const (
22 Go111 Version = 11 // v1
23 Go119 Version = 19 // v1
24 Go121 Version = 21 // v1
25 Go122 Version = 22 // v2
26 Go123 Version = 23 // v2
27 Go125 Version = 25 // v2
28 Go126 Version = 26 // v2
29 Current = Go126
30 )
31
32 var versions = map[Version][]tracev2.EventSpec{
33 // Go 1.11–1.21 use a different parser and are only set here for the sake of
34 // Version.Valid.
35 Go111: nil,
36 Go119: nil,
37 Go121: nil,
38
39 Go122: tracev2.Specs()[:tracev2.EvUserLog+1], // All events after are Go 1.23+.
40 Go123: tracev2.Specs()[:tracev2.EvExperimentalBatch+1], // All events after are Go 1.25+.
41 Go125: tracev2.Specs()[:tracev2.EvClockSnapshot+1], // All events after are Go 1.26+.
42 Go126: tracev2.Specs(),
43 }
44
45 // Specs returns the set of event.Specs for this version.
46 func (v Version) Specs() []tracev2.EventSpec {
47 return versions[v]
48 }
49
50 // EventName returns a string name of a wire format event
51 // for a particular trace version.
52 func (v Version) EventName(typ tracev2.EventType) string {
53 if !v.Valid() {
54 return "<invalid trace version>"
55 }
56 s := v.Specs()
57 if len(s) == 0 {
58 return "<v1 trace event type>"
59 }
60 if int(typ) < len(s) && s[typ].Name != "" {
61 return s[typ].Name
62 }
63 return fmt.Sprintf("Invalid(%d)", typ)
64 }
65
66 func (v Version) Valid() bool {
67 _, ok := versions[v]
68 return ok
69 }
70
71 // headerFmt is the format of the header of all Go execution traces.
72 const headerFmt = "go 1.%d trace\x00\x00\x00"
73
74 // ReadHeader reads the version of the trace out of the trace file's
75 // header, whose prefix must be present in v.
76 func ReadHeader(r io.Reader) (Version, error) {
77 var v Version
78 _, err := fmt.Fscanf(r, headerFmt, &v)
79 if err != nil {
80 return v, fmt.Errorf("bad file format: not a Go execution trace?")
81 }
82 if !v.Valid() {
83 return v, fmt.Errorf("unknown or unsupported trace version go 1.%d", v)
84 }
85 return v, nil
86 }
87
88 // WriteHeader writes a header for a trace version v to w.
89 func WriteHeader(w io.Writer, v Version) (int, error) {
90 return fmt.Fprintf(w, headerFmt, v)
91 }