| 1 |
// Copyright 2018 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 |
package debug |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"runtime" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
_ "unsafe" // for go:linkname |
| 13 |
) |
| 14 |
|
| 15 |
// exported from runtime |
| 16 |
func modinfo() string |
| 17 |
|
| 18 |
// ReadBuildInfo returns the build information embedded |
| 19 |
// in the running binary. The information is available only |
| 20 |
// in binaries built with module support. |
| 21 |
func ReadBuildInfo() (info *BuildInfo, ok bool) { |
| 22 |
data := modinfo() |
| 23 |
if len(data) < 32 { |
| 24 |
return nil, false |
| 25 |
} |
| 26 |
data = data[16 : len(data)-16] |
| 27 |
bi, err := ParseBuildInfo(data) |
| 28 |
if err != nil { |
| 29 |
return nil, false |
| 30 |
} |
| 31 |
|
| 32 |
// The go version is stored separately from other build info, mostly for |
| 33 |
// historical reasons. It is not part of the modinfo() string, and |
| 34 |
// ParseBuildInfo does not recognize it. We inject it here to hide this |
| 35 |
// awkwardness from the user. |
| 36 |
bi.GoVersion = runtime.Version() |
| 37 |
|
| 38 |
return bi, true |
| 39 |
} |
| 40 |
|
| 41 |
// BuildInfo represents the build information read from a Go binary. |
| 42 |
type BuildInfo struct { |
| 43 |
GoVersion string // Version of Go that produced this binary. |
| 44 |
Path string // The main package path |
| 45 |
Main Module // The module containing the main package |
| 46 |
Deps []*Module // Module dependencies |
| 47 |
Settings []BuildSetting // Other information about the build. |
| 48 |
} |
| 49 |
|
| 50 |
// Module represents a module. |
| 51 |
type Module struct { |
| 52 |
Path string // module path |
| 53 |
Version string // module version |
| 54 |
Sum string // checksum |
| 55 |
Replace *Module // replaced by this module |
| 56 |
} |
| 57 |
|
| 58 |
// BuildSetting describes a setting that may be used to understand how the |
| 59 |
// binary was built. For example, VCS commit and dirty status is stored here. |
| 60 |
type BuildSetting struct { |
| 61 |
// Key and Value describe the build setting. |
| 62 |
// Key must not contain an equals sign, space, tab, or newline. |
| 63 |
// Value must not contain newlines ('\n'). |
| 64 |
Key, Value string |
| 65 |
} |
| 66 |
|
| 67 |
// quoteKey reports whether key is required to be quoted. |
| 68 |
func quoteKey(key string) bool { |
| 69 |
return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`") |
| 70 |
} |
| 71 |
|
| 72 |
// quoteValue reports whether value is required to be quoted. |
| 73 |
func quoteValue(value string) bool { |
| 74 |
return strings.ContainsAny(value, " \t\r\n\"`") |
| 75 |
} |
| 76 |
|
| 77 |
func (bi *BuildInfo) String() string { |
| 78 |
buf := new(strings.Builder) |
| 79 |
if bi.GoVersion != "" { |
| 80 |
fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion) |
| 81 |
} |
| 82 |
if bi.Path != "" { |
| 83 |
fmt.Fprintf(buf, "path\t%s\n", bi.Path) |
| 84 |
} |
| 85 |
var formatMod func(string, Module) |
| 86 |
formatMod = func(word string, m Module) { |
| 87 |
buf.WriteString(word) |
| 88 |
buf.WriteByte('\t') |
| 89 |
buf.WriteString(m.Path) |
| 90 |
buf.WriteByte('\t') |
| 91 |
buf.WriteString(m.Version) |
| 92 |
if m.Replace == nil { |
| 93 |
buf.WriteByte('\t') |
| 94 |
buf.WriteString(m.Sum) |
| 95 |
} else { |
| 96 |
buf.WriteByte('\n') |
| 97 |
formatMod("=>", *m.Replace) |
| 98 |
} |
| 99 |
buf.WriteByte('\n') |
| 100 |
} |
| 101 |
if bi.Main != (Module{}) { |
| 102 |
formatMod("mod", bi.Main) |
| 103 |
} |
| 104 |
for _, dep := range bi.Deps { |
| 105 |
formatMod("dep", *dep) |
| 106 |
} |
| 107 |
for _, s := range bi.Settings { |
| 108 |
key := s.Key |
| 109 |
if quoteKey(key) { |
| 110 |
key = strconv.Quote(key) |
| 111 |
} |
| 112 |
value := s.Value |
| 113 |
if quoteValue(value) { |
| 114 |
value = strconv.Quote(value) |
| 115 |
} |
| 116 |
fmt.Fprintf(buf, "build\t%s=%s\n", key, value) |
| 117 |
} |
| 118 |
|
| 119 |
return buf.String() |
| 120 |
} |
| 121 |
|
| 122 |
func ParseBuildInfo(data string) (bi *BuildInfo, err error) { |
| 123 |
lineNum := 1 |
| 124 |
defer func() { |
| 125 |
if err != nil { |
| 126 |
err = fmt.Errorf("could not parse Go build info: line %d: %w", lineNum, err) |
| 127 |
} |
| 128 |
}() |
| 129 |
|
| 130 |
var ( |
| 131 |
pathLine = "path\t" |
| 132 |
modLine = "mod\t" |
| 133 |
depLine = "dep\t" |
| 134 |
repLine = "=>\t" |
| 135 |
buildLine = "build\t" |
| 136 |
newline = "\n" |
| 137 |
tab = "\t" |
| 138 |
) |
| 139 |
|
| 140 |
readModuleLine := func(elem []string) (Module, error) { |
| 141 |
if len(elem) != 2 && len(elem) != 3 { |
| 142 |
return Module{}, fmt.Errorf("expected 2 or 3 columns; got %d", len(elem)) |
| 143 |
} |
| 144 |
version := elem[1] |
| 145 |
sum := "" |
| 146 |
if len(elem) == 3 { |
| 147 |
sum = elem[2] |
| 148 |
} |
| 149 |
return Module{ |
| 150 |
Path: elem[0], |
| 151 |
Version: version, |
| 152 |
Sum: sum, |
| 153 |
}, nil |
| 154 |
} |
| 155 |
|
| 156 |
bi = new(BuildInfo) |
| 157 |
var ( |
| 158 |
last *Module |
| 159 |
line string |
| 160 |
ok bool |
| 161 |
) |
| 162 |
// Reverse of BuildInfo.String(), except for go version. |
| 163 |
for len(data) > 0 { |
| 164 |
line, data, ok = strings.Cut(data, newline) |
| 165 |
if !ok { |
| 166 |
break |
| 167 |
} |
| 168 |
switch { |
| 169 |
case strings.HasPrefix(line, pathLine): |
| 170 |
elem := line[len(pathLine):] |
| 171 |
bi.Path = string(elem) |
| 172 |
case strings.HasPrefix(line, modLine): |
| 173 |
elem := strings.Split(line[len(modLine):], tab) |
| 174 |
last = &bi.Main |
| 175 |
*last, err = readModuleLine(elem) |
| 176 |
if err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
case strings.HasPrefix(line, depLine): |
| 180 |
elem := strings.Split(line[len(depLine):], tab) |
| 181 |
last = new(Module) |
| 182 |
bi.Deps = append(bi.Deps, last) |
| 183 |
*last, err = readModuleLine(elem) |
| 184 |
if err != nil { |
| 185 |
return nil, err |
| 186 |
} |
| 187 |
case strings.HasPrefix(line, repLine): |
| 188 |
elem := strings.Split(line[len(repLine):], tab) |
| 189 |
if len(elem) != 3 { |
| 190 |
return nil, fmt.Errorf("expected 3 columns for replacement; got %d", len(elem)) |
| 191 |
} |
| 192 |
if last == nil { |
| 193 |
return nil, fmt.Errorf("replacement with no module on previous line") |
| 194 |
} |
| 195 |
last.Replace = &Module{ |
| 196 |
Path: string(elem[0]), |
| 197 |
Version: string(elem[1]), |
| 198 |
Sum: string(elem[2]), |
| 199 |
} |
| 200 |
last = nil |
| 201 |
case strings.HasPrefix(line, buildLine): |
| 202 |
kv := line[len(buildLine):] |
| 203 |
if len(kv) < 1 { |
| 204 |
return nil, fmt.Errorf("build line missing '='") |
| 205 |
} |
| 206 |
|
| 207 |
var key, rawValue string |
| 208 |
switch kv[0] { |
| 209 |
case '=': |
| 210 |
return nil, fmt.Errorf("build line with missing key") |
| 211 |
|
| 212 |
case '`', '"': |
| 213 |
rawKey, err := strconv.QuotedPrefix(kv) |
| 214 |
if err != nil { |
| 215 |
return nil, fmt.Errorf("invalid quoted key in build line") |
| 216 |
} |
| 217 |
if len(kv) == len(rawKey) { |
| 218 |
return nil, fmt.Errorf("build line missing '=' after quoted key") |
| 219 |
} |
| 220 |
if c := kv[len(rawKey)]; c != '=' { |
| 221 |
return nil, fmt.Errorf("unexpected character after quoted key: %q", c) |
| 222 |
} |
| 223 |
key, _ = strconv.Unquote(rawKey) |
| 224 |
rawValue = kv[len(rawKey)+1:] |
| 225 |
|
| 226 |
default: |
| 227 |
var ok bool |
| 228 |
key, rawValue, ok = strings.Cut(kv, "=") |
| 229 |
if !ok { |
| 230 |
return nil, fmt.Errorf("build line missing '=' after key") |
| 231 |
} |
| 232 |
if quoteKey(key) { |
| 233 |
return nil, fmt.Errorf("unquoted key %q must be quoted", key) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
var value string |
| 238 |
if len(rawValue) > 0 { |
| 239 |
switch rawValue[0] { |
| 240 |
case '`', '"': |
| 241 |
var err error |
| 242 |
value, err = strconv.Unquote(rawValue) |
| 243 |
if err != nil { |
| 244 |
return nil, fmt.Errorf("invalid quoted value in build line") |
| 245 |
} |
| 246 |
|
| 247 |
default: |
| 248 |
value = rawValue |
| 249 |
if quoteValue(value) { |
| 250 |
return nil, fmt.Errorf("unquoted value %q must be quoted", value) |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
bi.Settings = append(bi.Settings, BuildSetting{Key: key, Value: value}) |
| 256 |
} |
| 257 |
lineNum++ |
| 258 |
} |
| 259 |
return bi, nil |
| 260 |
} |