|
| 1 | +// Copyright 2024 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 | +//go:build go1.25 |
| 6 | + |
| 7 | +package debug |
| 8 | + |
| 9 | +import ( |
| 10 | + "bufio" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "os/exec" |
| 16 | + "path/filepath" |
| 17 | + "runtime/trace" |
| 18 | + "strings" |
| 19 | + "sync" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +// The FlightRecorder is a global resource, so create at most one per process. |
| 24 | +var getRecorder = sync.OnceValues(func() (*trace.FlightRecorder, error) { |
| 25 | + fr := trace.NewFlightRecorder(trace.FlightRecorderConfig{ |
| 26 | + // half a minute is usually enough to know "what just happened?" |
| 27 | + MinAge: 30 * time.Second, |
| 28 | + }) |
| 29 | + if err := fr.Start(); err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + return fr, nil |
| 33 | +}) |
| 34 | + |
| 35 | +func startFlightRecorder() (http.HandlerFunc, error) { |
| 36 | + fr, err := getRecorder() |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + // Return a handler that writes the most recent flight record, |
| 42 | + // starts a trace viewer server, and redirects to it. |
| 43 | + return func(w http.ResponseWriter, r *http.Request) { |
| 44 | + errorf := func(format string, args ...any) { |
| 45 | + msg := fmt.Sprintf(format, args...) |
| 46 | + http.Error(w, msg, http.StatusInternalServerError) |
| 47 | + } |
| 48 | + |
| 49 | + // Write the most recent flight record into a temp file. |
| 50 | + f, err := os.CreateTemp("", "flightrecord") |
| 51 | + if err != nil { |
| 52 | + errorf("can't create temp file for flight record: %v", err) |
| 53 | + return |
| 54 | + } |
| 55 | + if _, err := fr.WriteTo(f); err != nil { |
| 56 | + f.Close() |
| 57 | + errorf("failed to write flight record: %s", err) |
| 58 | + return |
| 59 | + } |
| 60 | + if err := f.Close(); err != nil { |
| 61 | + errorf("failed to close flight record: %s", err) |
| 62 | + return |
| 63 | + } |
| 64 | + tracefile, err := filepath.Abs(f.Name()) |
| 65 | + if err != nil { |
| 66 | + errorf("can't absolutize name of trace file: %v", err) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Run 'go tool trace' to start a new trace-viewer |
| 71 | + // web server process. It will run until gopls terminates. |
| 72 | + // (It would be nicer if we could just link it in; see #66843.) |
| 73 | + cmd := exec.Command("go", "tool", "trace", tracefile) |
| 74 | + |
| 75 | + // Don't connect trace's std{out,err} to our os.Stderr directly, |
| 76 | + // otherwise the child may outlive the parent in tests, |
| 77 | + // and 'go test' will complain about unclosed pipes. |
| 78 | + // Instead, interpose a pipe that will close when gopls exits. |
| 79 | + // See CL 677262 for a better solution (a cmd/trace flag). |
| 80 | + // (#66843 is of course better still.) |
| 81 | + // Also, this notifies us of the server's readiness and URL. |
| 82 | + urlC := make(chan string) |
| 83 | + { |
| 84 | + r, w, err := os.Pipe() |
| 85 | + if err != nil { |
| 86 | + errorf("can't create pipe: %v", err) |
| 87 | + return |
| 88 | + } |
| 89 | + go func() { |
| 90 | + // Copy from the pipe to stderr, |
| 91 | + // keeping an eye out for the "listening on URL" string. |
| 92 | + scan := bufio.NewScanner(r) |
| 93 | + for scan.Scan() { |
| 94 | + line := scan.Text() |
| 95 | + if _, url, ok := strings.Cut(line, "Trace viewer is listening on "); ok { |
| 96 | + urlC <- url |
| 97 | + } |
| 98 | + fmt.Fprintln(os.Stderr, line) |
| 99 | + } |
| 100 | + if err := scan.Err(); err != nil { |
| 101 | + log.Printf("reading from pipe to cmd/trace: %v", err) |
| 102 | + } |
| 103 | + }() |
| 104 | + cmd.Stderr = w |
| 105 | + cmd.Stdout = w |
| 106 | + } |
| 107 | + |
| 108 | + // Suppress the usual cmd/trace behavior of opening a new |
| 109 | + // browser tab by setting BROWSER to /usr/bin/true (a no-op). |
| 110 | + cmd.Env = append(os.Environ(), "BROWSER=true") |
| 111 | + if err := cmd.Start(); err != nil { |
| 112 | + errorf("failed to start trace server: %s", err) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + select { |
| 117 | + case addr := <-urlC: |
| 118 | + // Success! Send a redirect to the new location. |
| 119 | + // (This URL bypasses the help screen at /.) |
| 120 | + http.Redirect(w, r, addr+"/trace?view=proc", 302) |
| 121 | + |
| 122 | + case <-r.Context().Done(): |
| 123 | + errorf("canceled") |
| 124 | + |
| 125 | + case <-time.After(10 * time.Second): |
| 126 | + errorf("trace viewer failed to start", err) |
| 127 | + } |
| 128 | + }, nil |
| 129 | +} |
0 commit comments