2023-11-13 08:57:14 +00:00
|
|
|
|
// Copyright 2023 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2023-05-08 22:29:52 +00:00
|
|
|
|
package version
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
|
2025-01-24 20:36:51 +00:00
|
|
|
|
"internal/trace/tracev2"
|
2023-05-08 22:29:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Version represents the version of a trace file.
|
|
|
|
|
type Version uint32
|
|
|
|
|
|
|
|
|
|
const (
|
2025-01-24 20:56:47 +00:00
|
|
|
|
Go111 Version = 11 // v1
|
|
|
|
|
Go119 Version = 19 // v1
|
|
|
|
|
Go121 Version = 21 // v1
|
|
|
|
|
Go122 Version = 22 // v2
|
|
|
|
|
Go123 Version = 23 // v2
|
2025-02-28 16:07:16 +01:00
|
|
|
|
Go125 Version = 25 // v2
|
2025-08-05 21:37:07 +00:00
|
|
|
|
Go126 Version = 26 // v2
|
|
|
|
|
Current = Go126
|
2023-05-08 22:29:52 +00:00
|
|
|
|
)
|
|
|
|
|
|
2025-01-29 17:16:02 +00:00
|
|
|
|
var versions = map[Version][]tracev2.EventSpec{
|
2024-01-20 17:37:50 +01:00
|
|
|
|
// Go 1.11–1.21 use a different parser and are only set here for the sake of
|
|
|
|
|
// Version.Valid.
|
|
|
|
|
Go111: nil,
|
|
|
|
|
Go119: nil,
|
|
|
|
|
Go121: nil,
|
|
|
|
|
|
2025-02-28 16:07:16 +01:00
|
|
|
|
Go122: tracev2.Specs()[:tracev2.EvUserLog+1], // All events after are Go 1.23+.
|
|
|
|
|
Go123: tracev2.Specs()[:tracev2.EvExperimentalBatch+1], // All events after are Go 1.25+.
|
2025-08-05 21:37:07 +00:00
|
|
|
|
Go125: tracev2.Specs()[:tracev2.EvClockSnapshot+1], // All events after are Go 1.26+.
|
|
|
|
|
Go126: tracev2.Specs(),
|
2023-05-08 22:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Specs returns the set of event.Specs for this version.
|
2025-01-29 17:16:02 +00:00
|
|
|
|
func (v Version) Specs() []tracev2.EventSpec {
|
2023-05-08 22:29:52 +00:00
|
|
|
|
return versions[v]
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 20:56:47 +00:00
|
|
|
|
// EventName returns a string name of a wire format event
|
|
|
|
|
// for a particular trace version.
|
2025-01-29 17:16:02 +00:00
|
|
|
|
func (v Version) EventName(typ tracev2.EventType) string {
|
2025-01-24 20:56:47 +00:00
|
|
|
|
if !v.Valid() {
|
|
|
|
|
return "<invalid trace version>"
|
|
|
|
|
}
|
|
|
|
|
s := v.Specs()
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
|
return "<v1 trace event type>"
|
|
|
|
|
}
|
|
|
|
|
if int(typ) < len(s) && s[typ].Name != "" {
|
|
|
|
|
return s[typ].Name
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("Invalid(%d)", typ)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 22:29:52 +00:00
|
|
|
|
func (v Version) Valid() bool {
|
|
|
|
|
_, ok := versions[v]
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// headerFmt is the format of the header of all Go execution traces.
|
|
|
|
|
const headerFmt = "go 1.%d trace\x00\x00\x00"
|
|
|
|
|
|
|
|
|
|
// ReadHeader reads the version of the trace out of the trace file's
|
|
|
|
|
// header, whose prefix must be present in v.
|
|
|
|
|
func ReadHeader(r io.Reader) (Version, error) {
|
|
|
|
|
var v Version
|
|
|
|
|
_, err := fmt.Fscanf(r, headerFmt, &v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return v, fmt.Errorf("bad file format: not a Go execution trace?")
|
|
|
|
|
}
|
|
|
|
|
if !v.Valid() {
|
|
|
|
|
return v, fmt.Errorf("unknown or unsupported trace version go 1.%d", v)
|
|
|
|
|
}
|
|
|
|
|
return v, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteHeader writes a header for a trace version v to w.
|
|
|
|
|
func WriteHeader(w io.Writer, v Version) (int, error) {
|
|
|
|
|
return fmt.Fprintf(w, headerFmt, v)
|
|
|
|
|
}
|