Debian Codegraph @

src/internal/trace/version/version.go

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