{"record":{"id":"4b87459ed5c84da6","repo":"golang/go","slug":"failed-to-create-trace-reader-w","errorCode":null,"errorMessage":"failed to create trace reader: %w","messagePattern":"failed to create trace reader: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/trace/main.go","lineNumber":353,"sourceCode":"\t\tpct := float64(progress) / float64(size) * 100\n\t\tlog.Printf(\"%s of %s (%.1f%%) processed...\", byteCount(progress), byteCount(size), pct)\n\t}\n\treturn\n}\n\ntype parsedTrace struct {\n\tevents      []trace.Event\n\tsummary     *trace.Summary\n\tsize, valid int64\n\terr         error\n}\n\nfunc parseTrace(rr io.Reader, size int64) (*parsedTrace, error) {\n\t// Set up the reader.\n\tcr := countingReader{r: rr}\n\tr, err := trace.NewReader(&cr)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create trace reader: %w\", err)\n\t}\n\n\t// Set up state.\n\ts := trace.NewSummarizer()\n\tt := new(parsedTrace)\n\tvar validBytes int64\n\tvar validEvents int\n\tfor {\n\t\tev, err := r.ReadEvent()\n\t\tif err == io.EOF {\n\t\t\tvalidBytes = cr.bytesRead.Load()\n\t\t\tvalidEvents = len(t.events)\n\t\t\tbreak\n\t\t}\n\t\tif err != nil {\n\t\t\tt.err = err\n\t\t\tbreak\n\t\t}","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/trace/main.go#L335-L371","documentation":"`parseTrace` constructs a trace reader via `trace.NewReader`. If the header of the trace file is unreadable, indicates an unsupported/unknown trace version, or the file is empty/truncated below the header size, `NewReader` returns an error that is wrapped here. This happens before any event is read — the file never even made it to the parser.","triggerScenarios":"Feeding `go tool trace` a non-trace file (a random binary, a pprof heap profile, a core dump); a trace captured with a much newer Go version whose header the running toolchain doesn't recognize; a zero-byte or truncated-at-header trace; bytes captured without `runtime.Trace` framing.","commonSituations":"Passing a `.pprof` CPU profile by mistake; trace recorded on Go 1.2x and opened on Go 1.1x; SCP/rsync transfer that truncated; mixing up `go tool trace` and `go tool pprof` arguments.","solutions":["Confirm the file is a Go execution trace: it should start with `gotr` followed by a NUL byte (the trace magic) — `head -c 5 <file> | xxd`.","Re-record the trace with the same or older Go toolchain than the one opening it.","Make sure you invoked `runtime.StartTrace`/`runtime/trace.Start` and stopped cleanly; an unclean stop can truncate the header on some platforms.","Use `go tool trace -d=wire <file>` to inspect raw framing once the header is valid."],"exampleFix":"// before\n$ go tool trace heap.prof   # wrong file type\n// after\n$ go tool trace trace.out","handlingStrategy":"validation","validationCode":"// quick magic check before invoking go tool trace\nhdr := make([]byte, 5)\nif _, err := f.ReadAt(hdr, 0); err == nil && string(hdr) == \"gotr\\x00\" {\n    // looks like a Go trace\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't confuse pprof profiles (.pprof) with execution traces (trace.out).","Record and open traces with the same Go toolchain version."],"tags":["go-tool-trace","file-format","trace","version-mismatch"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}