aboutsummaryrefslogtreecommitdiff
path: root/src/internal/trace
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2025-10-31 17:37:26 -0400
committerMichael Pratt <mpratt@google.com>2025-11-10 12:21:57 -0800
commitbf58e7845eeba5f253f152f5f86523a7582d2a26 (patch)
tree995d0712cdcf5021ab60b6b02150b5cab4c3edcf /src/internal/trace
parent052c192a4cf853f0a613eacc623beca35f8c0e24 (diff)
downloadgo-bf58e7845eeba5f253f152f5f86523a7582d2a26.tar.xz
internal/trace: add "command" to convert text traces to raw
This is primarily helpful for parsing traces dumped via CI. cmd/dist doesn't like commands in std which are not actually part of the Go distribution. So rather than using a real command, this is actually a test which does the conversion. Change-Id: I6a6a636c829a4acc0bce8cf7548105ad59d83c67 Reviewed-on: https://go-review.googlesource.com/c/go/+/716882 Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/internal/trace')
-rw-r--r--src/internal/trace/testtrace/helpers.go1
-rw-r--r--src/internal/trace/testtrace/helpers_test.go79
2 files changed, 80 insertions, 0 deletions
diff --git a/src/internal/trace/testtrace/helpers.go b/src/internal/trace/testtrace/helpers.go
index 50f6825bab..ef676a9a14 100644
--- a/src/internal/trace/testtrace/helpers.go
+++ b/src/internal/trace/testtrace/helpers.go
@@ -37,6 +37,7 @@ func Dump(t *testing.T, testName string, traceBytes []byte, forceToFile bool) {
t.Logf("text trace too large to dump (%d bytes)", len(s))
} else {
t.Log(s)
+ t.Log("Convert this to a raw trace with `go test internal/trace/testtrace -covert in.tracetxt -out out.trace`")
}
} else {
// We asked to dump the trace or failed. Write the trace to a file.
diff --git a/src/internal/trace/testtrace/helpers_test.go b/src/internal/trace/testtrace/helpers_test.go
new file mode 100644
index 0000000000..3b874ac631
--- /dev/null
+++ b/src/internal/trace/testtrace/helpers_test.go
@@ -0,0 +1,79 @@
+// Copyright 2025 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.
+
+package testtrace
+
+import (
+ "flag"
+ "fmt"
+ "io"
+ "internal/trace/raw"
+ "os"
+ "testing"
+)
+
+var (
+ convert = flag.String("convert", "", "Path to trace text file to convert to binary format")
+ output = flag.String("out", "", "Output path for converted trace")
+)
+
+// TestConvertDump is not actually a test, it is a tool for converting trace
+// text dumps generated by Dump into the binary trace format. Set -convert and
+// -o to perform a converison.
+//
+// go test internal/trace/testtrace -convert in.tracetxt -out out.trace
+//
+// This would be cleaner as a dedicated internal command rather than a test,
+// but cmd/dist does not handle internal (non-distributed) commands in std
+// well.
+func TestConvertDump(t *testing.T) {
+ if *convert == "" {
+ t.Skip("Set -convert to convert a trace text file")
+ }
+ if *output == "" {
+ t.Fatal("Set -o to specify conversion output")
+ }
+
+ if err := convertDump(*convert, *output); err != nil {
+ t.Error(err)
+ }
+}
+
+func convertDump(inPath, outPath string) error {
+ in, err := os.Open(inPath)
+ if err != nil {
+ return fmt.Errorf("error opening input: %v", err)
+ }
+ defer in.Close()
+
+ out, err := os.Create(outPath)
+ if err != nil {
+ return fmt.Errorf("error creating output: %v", err)
+ }
+ defer out.Close()
+
+ tr, err := raw.NewTextReader(in)
+ if err != nil {
+ return fmt.Errorf("error creating text reader: %v", err)
+ }
+ tw, err := raw.NewWriter(out, tr.Version())
+ if err != nil {
+ return fmt.Errorf("error creating raw writer: %v", err)
+ }
+
+ for {
+ ev, err := tr.ReadEvent()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return fmt.Errorf("bad trace file: %v", err)
+ }
+ if err := tw.WriteEvent(ev); err != nil {
+ return fmt.Errorf("failed to write trace bytes: %v", err)
+ }
+ }
+
+ return nil
+}