1 // Copyright 2021 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.
5 // Package godebug makes the settings in the $GODEBUG environment variable
6 // available to other packages. These settings are often used for compatibility
7 // tweaks, when we need to change a default behavior but want to let users
8 // opt back in to the original. For example GODEBUG=http2server=0 disables
9 // HTTP/2 support in the net/http server.
11 // In typical usage, code should declare a Setting as a global
12 // and then call Value each time the current setting value is needed:
14 // var http2server = godebug.New("http2server")
16 // func ServeConn(c net.Conn) {
17 // if http2server.Value() == "0" {
24 // Each time a non-default setting causes a change in program behavior,
25 // code should call [Setting.IncNonDefault] to increment a counter that can
26 // be reported by [runtime/metrics.Read].
27 // Note that counters used with IncNonDefault must be added to
28 // various tables in other packages. See the [Setting.IncNonDefault]
29 // documentation for details.
35 _ "unsafe" // go:linkname
38 // A Setting is a single setting in the $GODEBUG environment variable.
46 value atomic.Pointer[string]
47 nonDefaultOnce sync.Once
48 nonDefault atomic.Uint64
51 // New returns a new Setting for the $GODEBUG setting with the given name.
52 func New(name string) *Setting {
53 return &Setting{name: name}
56 // Name returns the name of the setting.
57 func (s *Setting) Name() string {
61 // String returns a printable form for the setting: name=value.
62 func (s *Setting) String() string {
63 return s.name + "=" + s.Value()
66 // IncNonDefault increments the non-default behavior counter
67 // associated with the given setting.
68 // This counter is exposed in the runtime/metrics value
69 // /godebug/non-default-behavior/<name>:events.
71 // Note that Value must be called at least once before IncNonDefault.
73 // Any GODEBUG setting that can call IncNonDefault must be listed
74 // in three more places:
76 // - the table in ../runtime/metrics.go (search for non-default-behavior)
77 // - the table in ../../runtime/metrics/description.go (search for non-default-behavior; run 'go generate' afterward)
78 // - the table in ../../cmd/go/internal/load/godebug.go (search for defaultGodebugs)
79 func (s *Setting) IncNonDefault() {
80 s.nonDefaultOnce.Do(s.register)
84 func (s *Setting) register() {
85 registerMetric("/godebug/non-default-behavior/"+s.name+":events", s.nonDefault.Load)
88 // cache is a cache of all the GODEBUG settings,
89 // a locked map[string]*atomic.Pointer[string].
91 // All Settings with the same name share a single
92 // *atomic.Pointer[string], so that when GODEBUG
93 // changes only that single atomic string pointer
94 // needs to be updated.
96 // A name appears in the values map either if it is the
97 // name of a Setting for which Value has been called
98 // at least once, or if the name has ever appeared in
99 // a name=value pair in the $GODEBUG environment variable.
100 // Once entered into the map, the name is never removed.
101 var cache sync.Map // name string -> value *atomic.Pointer[string]
105 // Value returns the current value for the GODEBUG setting s.
107 // Value maintains an internal cache that is synchronized
108 // with changes to the $GODEBUG environment variable,
109 // making Value efficient to call as frequently as needed.
110 // Clients should therefore typically not attempt their own
111 // caching of Value's result.
112 func (s *Setting) Value() string {
114 s.setting = lookup(s.name)
116 return *s.value.Load()
119 // lookup returns the unique *setting value for the given name.
120 func lookup(name string) *setting {
121 if v, ok := cache.Load(name); ok {
125 s.value.Store(&empty)
126 if v, loaded := cache.LoadOrStore(name, s); loaded {
127 // Lost race: someone else created it. Use theirs.
134 // setUpdate is provided by package runtime.
135 // It calls update(def, env), where def is the default GODEBUG setting
136 // and env is the current value of the $GODEBUG environment variable.
137 // After that first call, the runtime calls update(def, env)
138 // again each time the environment variable changes
139 // (due to use of os.Setenv, for example).
141 //go:linkname setUpdate
142 func setUpdate(update func(string, string))
144 // registerMetric is provided by package runtime.
145 // It forwards registrations to runtime/metrics.
147 //go:linkname registerMetric
148 func registerMetric(name string, read func() uint64)
150 // setNewNonDefaultInc is provided by package runtime.
151 // The runtime can do
153 // inc := newNonDefaultInc(name)
157 // inc := godebug.New(name).IncNonDefault
159 // since it cannot import godebug.
161 //go:linkname setNewIncNonDefault
162 func setNewIncNonDefault(newIncNonDefault func(string) func())
166 setNewIncNonDefault(newIncNonDefault)
169 func newIncNonDefault(name string) func() {
172 return s.IncNonDefault
175 var updateMu sync.Mutex
177 // update records an updated GODEBUG setting.
178 // def is the default GODEBUG setting for the running binary,
179 // and env is the current value of the $GODEBUG environment variable.
180 func update(def, env string) {
182 defer updateMu.Unlock()
184 // Update all the cached values, creating new ones as needed.
185 // We parse the environment variable first, so that any settings it has
186 // are already locked in place (did[name] = true) before we consider
188 did := make(map[string]bool)
192 // Clear any cached values that are no longer present.
193 cache.Range(func(name, s any) bool {
194 if !did[name.(string)] {
195 s.(*setting).value.Store(&empty)
201 // parse parses the GODEBUG setting string s,
202 // which has the form k=v,k2=v2,k3=v3.
203 // Later settings override earlier ones.
204 // Parse only updates settings k=v for which did[k] = false.
205 // It also sets did[k] = true for settings that it updates.
206 func parse(did map[string]bool, s string) {
207 // Scan the string backward so that later settings are used
208 // and earlier settings are ignored.
209 // Note that a forward scan would cause cached values
210 // to temporarily use the ignored value before being
211 // updated to the "correct" one.
214 for i := end - 1; i >= -1; i-- {
215 if i == -1 || s[i] == ',' {
217 name, value := s[i+1:eq], s[eq+1:end]
220 lookup(name).value.Store(&value)
225 } else if s[i] == '=' {