| 1 | package driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 6 | "net/urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/" |
| 7 | "osscip-go gomod github.com/golang/go/src go1.24.0 os/" |
| 8 | "path/filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/" |
| 9 | ) |
| 10 | |
| 11 | // settings holds pprof settings. |
| 12 | type settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings# struct { |
| 13 | // Configs holds a list of named UI configurations. |
| 14 | Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. []namedConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig# `json:"configs"` |
| 15 | } |
| 16 | |
| 17 | // namedConfig associates a name with a config. |
| 18 | type namedConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig# struct { |
| 19 | Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name. string `json:"name"` |
| 20 | configscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#config. |
| 21 | } |
| 22 | |
| 23 | // settingsFileName returns the name of the file where settings should be saved. |
| 24 | func settingsFileNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settingsFileName().() (string, error) { |
| 25 | // Return "pprof/settings.json" under os.UserConfigDir(). |
| 26 | dir, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.UserConfigDirscip-go gomod github.com/golang/go/src go1.24.0 os/UserConfigDir().() |
| 27 | if err != nil { |
| 28 | return "", err |
| 29 | } |
| 30 | return filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(dir, "pprof", "settings.json"), nil |
| 31 | } |
| 32 | |
| 33 | // readSettings reads settings from fname. |
| 34 | func readSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/readSettings().(fname string) (*settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#, error) { |
| 35 | data, err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.24.0 os/ReadFile().(fname) |
| 36 | if err != nil { |
| 37 | if osscip-go gomod github.com/golang/go/src go1.24.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.24.0 os/IsNotExist().(err) { |
| 38 | return &settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#{}, nil |
| 39 | } |
| 40 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("could not read settings: %w", err) |
| 41 | } |
| 42 | settings := &settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#{} |
| 43 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/Unmarshal().(data, settings); err != nil { |
| 44 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("could not parse settings: %w", err) |
| 45 | } |
| 46 | for i := range settings.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. { |
| 47 | settings.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs.[i].resetTransientscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#resetTransient().() |
| 48 | } |
| 49 | return settings, nil |
| 50 | } |
| 51 | |
| 52 | // writeSettings saves settings to fname. |
| 53 | func writeSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/writeSettings().(fname string, settings *settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#) error { |
| 54 | data, err := jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/.MarshalIndentscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/MarshalIndent().(settings, "", " ") |
| 55 | if err != nil { |
| 56 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("could not encode settings: %w", err) |
| 57 | } |
| 58 | |
| 59 | // create the settings directory if it does not exist |
| 60 | // XDG specifies permissions 0700 when creating settings dirs: |
| 61 | // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 62 | if err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.24.0 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Dir().(fname), 0700); err != nil { |
| 63 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("failed to create settings directory: %w", err) |
| 64 | } |
| 65 | |
| 66 | if err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.24.0 os/WriteFile().(fname, data, 0644); err != nil { |
| 67 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("failed to write settings: %w", err) |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | // configMenuEntry holds information for a single config menu entry. |
| 73 | type configMenuEntryscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry# struct { |
| 74 | Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#Name. string |
| 75 | URLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#URL. string |
| 76 | Currentscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#Current. bool // Is this the currently selected config? |
| 77 | UserConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#UserConfig. bool // Is this a user-provided config? |
| 78 | } |
| 79 | |
| 80 | // configMenu returns a list of items to add to a menu in the web UI. |
| 81 | func configMenuscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenu().(fname string, u urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#) []configMenuEntryscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry# { |
| 82 | // Start with system configs. |
| 83 | configs := []namedConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#{{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name.: "Default", configscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#config.: defaultConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/defaultConfig().()}} |
| 84 | if settings, err := readSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/readSettings().(fname); err == nil { |
| 85 | // Add user configs. |
| 86 | configs = append(configs, settings.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs....) |
| 87 | } |
| 88 | |
| 89 | // Convert to menu entries. |
| 90 | result := make([]configMenuEntryscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#, len(configs)) |
| 91 | lastMatch := -1 |
| 92 | for i, cfg := range configs { |
| 93 | dst, changed := cfg.makeURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#makeURL().(u) |
| 94 | if !changed { |
| 95 | lastMatch = i |
| 96 | } |
| 97 | // Use a relative URL to work in presence of stripping/redirects in webui.go. |
| 98 | rel := &urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#{RawQueryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#RawQuery.: dst.RawQueryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#RawQuery., ForceQueryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#ForceQuery.: true} |
| 99 | result[i] = configMenuEntryscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#{ |
| 100 | Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#Name.: cfg.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name., |
| 101 | URLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#URL.: rel.Stringscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#String().(), |
| 102 | UserConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#UserConfig.: (i != 0), |
| 103 | } |
| 104 | } |
| 105 | // Mark the last matching config as current |
| 106 | if lastMatch >= 0 { |
| 107 | result[lastMatch].Currentscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/configMenuEntry#Current. = true |
| 108 | } |
| 109 | return result |
| 110 | } |
| 111 | |
| 112 | // editSettings edits settings by applying fn to them. |
| 113 | func editSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/editSettings().(fname string, fn func(s *settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#) error) error { |
| 114 | settings, err := readSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/readSettings().(fname) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if err := fn(settings); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | return writeSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/writeSettings().(fname, settings) |
| 122 | } |
| 123 | |
| 124 | // setConfig saves the config specified in request to fname. |
| 125 | func setConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setConfig().(fname string, request urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#) error { |
| 126 | q := request.Queryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Query().() |
| 127 | name := q.Getscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Values#Get().("config") |
| 128 | if name == "" { |
| 129 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("invalid config name") |
| 130 | } |
| 131 | cfg := currentConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/currentConfig().() |
| 132 | if err := cfg.applyURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/config#applyURL().(q); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | return editSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/editSettings().(fname, func(s *settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#) error { |
| 136 | for i, c := range s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. { |
| 137 | if c.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name. == name { |
| 138 | s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs.[i].configscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#config. = cfg |
| 139 | return nil |
| 140 | } |
| 141 | } |
| 142 | s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. = append(s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs., namedConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name.: name, configscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#config.: cfg}) |
| 143 | return nil |
| 144 | }) |
| 145 | } |
| 146 | |
| 147 | // removeConfig removes config from fname. |
| 148 | func removeConfigscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/removeConfig().(fname, config string) error { |
| 149 | return editSettingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/editSettings().(fname, func(s *settingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#) error { |
| 150 | for i, c := range s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. { |
| 151 | if c.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/namedConfig#Name. == config { |
| 152 | s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs. = append(s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs.[:i], s.Configsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/settings#Configs.[i+1:]...) |
| 153 | return nil |
| 154 | } |
| 155 | } |
| 156 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("config %s not found", config) |
| 157 | }) |
| 158 | } |