這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
This is a short post describing the procedure for discovering which version of Go was used to compile a Go binary.
This procedure relies on the fact that each Go program includes a copy of the version string reported by runtime.Version()
. Linker magic ensures that this value will be present in the final binary irrespective of whether runtime.Version()
is called by the resulting program. The value in question is stored in the runtime.buildVersion
variable and can be recovered by a debugger.
The rest of this post describes the mechanisms for recovering the contents of runtime.buildVersion
on various platforms.
Linux/FreeBSD/OpenBSD/NetBSD
If you’re on a Linux or *BSD platform, you can recover the binary build version with gdb
.
% gdb $HOME/bin/godocGNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1Copyright (C) 2016 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>(gdb) p 'runtime.buildVersion'$1 = 0xa9ceb8 "go1.8.3"
Darwin
The debugging situation on OS X isn’t great, but here are several options.
gdb
gdb
was removed from the XCode toolchain following the switch from gcc
to llvm
. If you are running a version of XCode that has gdb
, you should used the instructions from the previous section.
lldb
I’ve not been able to find a shipping version of lldb
that can interpret the Go string
syntax. If I discover the correct incantation, I’ll update this post. Until then, use delve
Delve
Delve can be used to print the value of runtime.buildVersion
.
% dlv exec $HOME/bin/godocType 'help' for list of commands.(dlv) b main.mainBreakpoint 1 set at 0x15596eb for main.main() ./golang.org/x/tools/cmd/godoc/main.go:156(dlv) c> main.main() ./golang.org/x/tools/cmd/godoc/main.go:156 (hits goroutine(1):1 total:1) (PC: 0x15596eb) 151: } 152: } 153: log.Fatalf("too many redirects") 154: } 155:=> 156: func main() { 157: flag.Usage = usage 158: flag.Parse() 159: 160: playEnabled = *showPlayground 161:(dlv) p runtime.buildVersion"go1.8.1"
Windows
No idea, sorry. If someone wants to figure out the correct WinDbg incantation, please let me know and I’ll link to you from this post.
Related posts:
- OS X has a built in WiFi scanner
- Introducing profile, super simple profiling for Go programs
- Cross compilation just got a whole lot better in Go 1.5
- Simple profiling package moved, updated