]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add distribution-specific info for Linux to bug command
authorMohit Agarwal <mohit@sdf.org>
Wed, 7 Sep 2016 04:06:58 +0000 (09:36 +0530)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 15 Sep 2016 18:42:07 +0000 (18:42 +0000)
Also remove the hard-coded path for getting glibc information.

As an example, the following is the diff for `go bug` on Ubuntu before
and after the change:

>>>
--- /tmp/01     2016-09-13 15:08:53.202758043 +0530
+++ /tmp/02     2016-09-13 21:38:17.485039867 +0530
@@ -1,7 +1,7 @@
 Please check whether the issue also reproduces on the latest release, go1.7.1.

 ```
-go version devel +bdb3b79 Wed Sep 7 03:23:44 2016 +0000 linux/amd64
+go version devel +cb13150 Wed Sep 7 09:46:58 2016 +0530 linux/amd64
 GOARCH="amd64"
 GOBIN=""
 GOEXE=""
@@ -18,5 +18,23 @@
 CXX="g++"
 CGO_ENABLED="1"
 uname -sr: Linux 4.4.0-36-generic
+Distributor ID:        Ubuntu
+Description:   Ubuntu 16.04.1 LTS
+Release:       16.04
+Codename:      xenial
+/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.23-0ubuntu3) stable release version 2.23, by Roland McGrath et al.
 gdb --version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
 ```
<<<

Change-Id: I7e3730a797af0f94d6e43fe4743ab48bc0f11f1b
Reviewed-on: https://go-review.googlesource.com/28581
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/go/bug.go

index b6d8e35b5a98fc7eef094e08c34fbbec9b47777c..5d2f6676b3ce280e64d4bbc32d3ac7c6a1c37118 100644 (file)
@@ -9,7 +9,10 @@ import (
        "fmt"
        "io"
        "io/ioutil"
+       "os"
        "os/exec"
+       "path/filepath"
+       "regexp"
        "runtime"
        "strings"
 )
@@ -74,7 +77,8 @@ func printOSDetails(w io.Writer) {
                printCmdOut(w, "", "sw_vers")
        case "linux":
                printCmdOut(w, "uname -sr: ", "uname", "-sr")
-               printCmdOut(w, "libc:", "/lib/libc.so.6")
+               printCmdOut(w, "", "lsb_release", "-a")
+               printGlibcVersion(w)
        case "openbsd", "netbsd", "freebsd", "dragonfly":
                printCmdOut(w, "uname -v: ", "uname", "-v")
        case "solaris":
@@ -97,10 +101,7 @@ func printCDetails(w io.Writer) {
                // There's apparently no combination of command line flags
                // to get gdb to spit out its version without the license and warranty.
                // Print up to the first newline.
-               idx := bytes.Index(out, []byte{'\n'})
-               line := out[:idx]
-               line = bytes.TrimSpace(line)
-               fmt.Fprintf(w, "gdb --version: %s\n", line)
+               fmt.Fprintf(w, "gdb --version: %s\n", firstLine(out))
        } else {
                if buildV {
                        fmt.Printf("failed to run gdb --version: %v\n", err)
@@ -145,3 +146,56 @@ func printCmdOut(w io.Writer, prefix, path string, args ...string) {
        }
        fmt.Fprintf(w, "%s%s\n", prefix, bytes.TrimSpace(out))
 }
+
+// firstLine returns the first line of a given byte slice.
+func firstLine(buf []byte) []byte {
+       idx := bytes.IndexByte(buf, '\n')
+       if idx > 0 {
+               buf = buf[:idx]
+       }
+       return bytes.TrimSpace(buf)
+}
+
+// printGlibcVersion prints information about the glibc version.
+// It ignores failures.
+func printGlibcVersion(w io.Writer) {
+       tempdir := os.TempDir()
+       if tempdir == "" {
+               return
+       }
+       src := []byte(`int main() {}`)
+       srcfile := filepath.Join(tempdir, "go-bug.c")
+       outfile := filepath.Join(tempdir, "go-bug")
+       err := ioutil.WriteFile(srcfile, src, 0644)
+       if err != nil {
+               return
+       }
+       defer os.Remove(srcfile)
+       cmd := exec.Command("gcc", "-o", outfile, srcfile)
+       if _, err = cmd.CombinedOutput(); err != nil {
+               return
+       }
+       defer os.Remove(outfile)
+
+       cmd = exec.Command("ldd", outfile)
+       out, err := cmd.CombinedOutput()
+       if err != nil {
+               return
+       }
+       re := regexp.MustCompile(`libc\.so[^ ]* => ([^ ]+)`)
+       m := re.FindStringSubmatch(string(out))
+       if m == nil {
+               return
+       }
+       cmd = exec.Command(m[1])
+       out, err = cmd.Output()
+       if err != nil {
+               return
+       }
+       fmt.Fprintf(w, "%s: %s\n", m[1], firstLine(out))
+
+       // print another line (the one containing version string) in case of musl libc
+       if idx := bytes.IndexByte(out, '\n'); bytes.Index(out, []byte("musl")) != -1 && idx > -1 {
+               fmt.Fprintf(w, "%s\n", firstLine(out[idx+1:]))
+       }
+}