Commit b51c1570 authored by Russ Cox's avatar Russ Cox

cmd/go: move runtime/debug.modinfo to runtime.modinfo

It is easier to ensure that the symbol is always present
if we move it to package runtime. Avoids init-time work.
Also moves it next to buildVersion, the other similar symbol.

Setting up for "go version <binary>".

For #31624.

Change-Id: I943724469ce6992153e701257eb6f12da88c8e4e
Reviewed-on: https://go-review.googlesource.com/c/go/+/173341
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent fac3b5d0
...@@ -247,21 +247,15 @@ func findModule(target, path string) module.Version { ...@@ -247,21 +247,15 @@ func findModule(target, path string) module.Version {
} }
func ModInfoProg(info string) []byte { func ModInfoProg(info string) []byte {
// Inject a variable with the debug information as runtime/debug.modinfo, // Inject a variable with the debug information as runtime.modinfo,
// but compile it in package main so that it is specific to the binary. // but compile it in package main so that it is specific to the binary.
//
// The variable must be a literal so that it will have the correct value // The variable must be a literal so that it will have the correct value
// before the initializer for package main runs. // before the initializer for package main runs.
// //
// We also want the value to be present even if runtime/debug.modinfo is // The runtime startup code refers to the variable, which keeps it live in all binaries.
// otherwise unused in the rest of the program. Reading it in an init function
// suffices for now.
return []byte(fmt.Sprintf(`package main return []byte(fmt.Sprintf(`package main
import _ "unsafe" import _ "unsafe"
//go:linkname __debug_modinfo__ runtime/debug.modinfo //go:linkname __debug_modinfo__ runtime.modinfo
var __debug_modinfo__ = %q var __debug_modinfo__ = %q
var keepalive_modinfo = __debug_modinfo__
func init() { keepalive_modinfo = __debug_modinfo__ }
`, string(infoStart)+info+string(infoEnd))) `, string(infoStart)+info+string(infoEnd)))
} }
...@@ -57,3 +57,8 @@ func NumCgoCall() int64 { ...@@ -57,3 +57,8 @@ func NumCgoCall() int64 {
func NumGoroutine() int { func NumGoroutine() int {
return int(gcount()) return int(gcount())
} }
//go:linkname debug_modinfo runtime/debug.modinfo
func debug_modinfo() string {
return modinfo
}
...@@ -8,14 +8,14 @@ import ( ...@@ -8,14 +8,14 @@ import (
"strings" "strings"
) )
// set using cmd/go/internal/modload.ModInfoProg // exported from runtime
var modinfo string func modinfo() string
// ReadBuildInfo returns the build information embedded // ReadBuildInfo returns the build information embedded
// in the running binary. The information is available only // in the running binary. The information is available only
// in binaries built with module support. // in binaries built with module support.
func ReadBuildInfo() (info *BuildInfo, ok bool) { func ReadBuildInfo() (info *BuildInfo, ok bool) {
return readBuildInfo(modinfo) return readBuildInfo(modinfo())
} }
// BuildInfo represents the build information read from // BuildInfo represents the build information read from
......
...@@ -13,6 +13,9 @@ import ( ...@@ -13,6 +13,9 @@ import (
var buildVersion = sys.TheVersion var buildVersion = sys.TheVersion
// set using cmd/go/internal/modload.ModInfoProg
var modinfo string
// Goroutine scheduler // Goroutine scheduler
// The scheduler's job is to distribute ready-to-run goroutines over worker threads. // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
// //
...@@ -577,6 +580,11 @@ func schedinit() { ...@@ -577,6 +580,11 @@ func schedinit() {
// to ensure runtime·buildVersion is kept in the resulting binary. // to ensure runtime·buildVersion is kept in the resulting binary.
buildVersion = "unknown" buildVersion = "unknown"
} }
if len(modinfo) == 1 {
// Condition should never trigger. This code just serves
// to ensure runtime·modinfo is kept in the resulting binary.
modinfo = ""
}
} }
func dumpgstatus(gp *g) { func dumpgstatus(gp *g) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment