1
2
3
4
5 package debug
6
7 import (
8 "fmt"
9 "runtime"
10 "strconv"
11 "strings"
12 )
13
14
15 func modinfo() string
16
17
18
19
20 func ReadBuildInfo() (info *BuildInfo, ok bool) {
21 data := modinfo()
22 if len(data) < 32 {
23 return nil, false
24 }
25 data = data[16 : len(data)-16]
26 bi, err := ParseBuildInfo(data)
27 if err != nil {
28 return nil, false
29 }
30
31
32
33
34
35 bi.GoVersion = runtime.Version()
36
37 return bi, true
38 }
39
40
41 type BuildInfo struct {
42
43
44 GoVersion string
45
46
47
48 Path string
49
50
51 Main Module
52
53
54
55 Deps []*Module
56
57
58 Settings []BuildSetting
59 }
60
61
62 type Module struct {
63 Path string
64 Version string
65 Sum string
66 Replace *Module
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 type BuildSetting struct {
88
89
90
91 Key, Value string
92 }
93
94
95 func quoteKey(key string) bool {
96 return len(key) == 0 || strings.ContainsAny(key, "= \t\r\n\"`")
97 }
98
99
100 func quoteValue(value string) bool {
101 return strings.ContainsAny(value, " \t\r\n\"`")
102 }
103
104 func (bi *BuildInfo) String() string {
105 buf := new(strings.Builder)
106 if bi.GoVersion != "" {
107 fmt.Fprintf(buf, "go\t%s\n", bi.GoVersion)
108 }
109 if bi.Path != "" {
110 fmt.Fprintf(buf, "path\t%s\n", bi.Path)
111 }
112 var formatMod func(string, Module)
113 formatMod = func(word string, m Module) {
114 buf.WriteString(word)
115 buf.WriteByte('\t')
116 buf.WriteString(m.Path)
117 buf.WriteByte('\t')
118 buf.WriteString(m.Version)
119 if m.Replace == nil {
120 buf.WriteByte('\t')
121 buf.WriteString(m.Sum)
122 } else {
123 buf.WriteByte('\n')
124 formatMod("=>", *m.Replace)
125 }
126 buf.WriteByte('\n')
127 }
128 if bi.Main != (Module{}) {
129 formatMod("mod", bi.Main)
130 }
131 for _, dep := range bi.Deps {
132 formatMod("dep", *dep)
133 }
134 for _, s := range bi.Settings {
135 key := s.Key
136 if quoteKey(key) {
137 key = strconv.Quote(key)
138 }
139 value := s.Value
140 if quoteValue(value) {
141 value = strconv.Quote(value)
142 }
143 fmt.Fprintf(buf, "build\t%s=%s\n", key, value)
144 }
145
146 return buf.String()
147 }
148
149 func ParseBuildInfo(data string) (bi *BuildInfo, err error) {
150 lineNum := 1
151 defer func() {
152 if err != nil {
153 err = fmt.Errorf("could not parse Go build info: line %d: %w", lineNum, err)
154 }
155 }()
156
157 var (
158 pathLine = "path\t"
159 modLine = "mod\t"
160 depLine = "dep\t"
161 repLine = "=>\t"
162 buildLine = "build\t"
163 newline = "\n"
164 tab = "\t"
165 )
166
167 readModuleLine := func(elem []string) (Module, error) {
168 if len(elem) != 2 && len(elem) != 3 {
169 return Module{}, fmt.Errorf("expected 2 or 3 columns; got %d", len(elem))
170 }
171 version := elem[1]
172 sum := ""
173 if len(elem) == 3 {
174 sum = elem[2]
175 }
176 return Module{
177 Path: elem[0],
178 Version: version,
179 Sum: sum,
180 }, nil
181 }
182
183 bi = new(BuildInfo)
184 var (
185 last *Module
186 line string
187 ok bool
188 )
189
190 for len(data) > 0 {
191 line, data, ok = strings.Cut(data, newline)
192 if !ok {
193 break
194 }
195 switch {
196 case strings.HasPrefix(line, pathLine):
197 elem := line[len(pathLine):]
198 bi.Path = string(elem)
199 case strings.HasPrefix(line, modLine):
200 elem := strings.Split(line[len(modLine):], tab)
201 last = &bi.Main
202 *last, err = readModuleLine(elem)
203 if err != nil {
204 return nil, err
205 }
206 case strings.HasPrefix(line, depLine):
207 elem := strings.Split(line[len(depLine):], tab)
208 last = new(Module)
209 bi.Deps = append(bi.Deps, last)
210 *last, err = readModuleLine(elem)
211 if err != nil {
212 return nil, err
213 }
214 case strings.HasPrefix(line, repLine):
215 elem := strings.Split(line[len(repLine):], tab)
216 if len(elem) != 3 {
217 return nil, fmt.Errorf("expected 3 columns for replacement; got %d", len(elem))
218 }
219 if last == nil {
220 return nil, fmt.Errorf("replacement with no module on previous line")
221 }
222 last.Replace = &Module{
223 Path: string(elem[0]),
224 Version: string(elem[1]),
225 Sum: string(elem[2]),
226 }
227 last = nil
228 case strings.HasPrefix(line, buildLine):
229 kv := line[len(buildLine):]
230 if len(kv) < 1 {
231 return nil, fmt.Errorf("build line missing '='")
232 }
233
234 var key, rawValue string
235 switch kv[0] {
236 case '=':
237 return nil, fmt.Errorf("build line with missing key")
238
239 case '`', '"':
240 rawKey, err := strconv.QuotedPrefix(kv)
241 if err != nil {
242 return nil, fmt.Errorf("invalid quoted key in build line")
243 }
244 if len(kv) == len(rawKey) {
245 return nil, fmt.Errorf("build line missing '=' after quoted key")
246 }
247 if c := kv[len(rawKey)]; c != '=' {
248 return nil, fmt.Errorf("unexpected character after quoted key: %q", c)
249 }
250 key, _ = strconv.Unquote(rawKey)
251 rawValue = kv[len(rawKey)+1:]
252
253 default:
254 var ok bool
255 key, rawValue, ok = strings.Cut(kv, "=")
256 if !ok {
257 return nil, fmt.Errorf("build line missing '=' after key")
258 }
259 if quoteKey(key) {
260 return nil, fmt.Errorf("unquoted key %q must be quoted", key)
261 }
262 }
263
264 var value string
265 if len(rawValue) > 0 {
266 switch rawValue[0] {
267 case '`', '"':
268 var err error
269 value, err = strconv.Unquote(rawValue)
270 if err != nil {
271 return nil, fmt.Errorf("invalid quoted value in build line")
272 }
273
274 default:
275 value = rawValue
276 if quoteValue(value) {
277 return nil, fmt.Errorf("unquoted value %q must be quoted", value)
278 }
279 }
280 }
281
282 bi.Settings = append(bi.Settings, BuildSetting{Key: key, Value: value})
283 }
284 lineNum++
285 }
286 return bi, nil
287 }
288
View as plain text