Skip to content

os/exec: #42803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrisFang5 opened this issue Nov 24, 2020 · 1 comment
Closed

os/exec: #42803

chrisFang5 opened this issue Nov 24, 2020 · 1 comment

Comments

@chrisFang5
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.15.2 linux/amd64

Does this issue reproduce with the latest release?

1.15.2 is latest release now(2020/11/24)

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/fanghongwei/.cache/go-build"
GOENV="/home/fanghongwei/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/fanghongwei/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/fanghongwei/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn/"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/fanghongwei/go/src/mycode/fuzz/gdb/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build641017757=/tmp/go-build -gno-record-gcc-switches"

What did you do?

my code as below:

package main

import (
    "context"
    "io"
    "log"
    "os/exec"
    "sync"
    "time"
)

func main() {
    test()
}

func test() {
    var err error

    ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100000)
    // cmd := exec.CommandContext(ctx, "/usr/bin/gdb")
    cmd := exec.CommandContext(ctx, "/usr/bin/gdb", "-ex", "run", "./must_panic")
    log.Printf("command:%s", cmd.String())

    defer cancel()

    out, err := cmd.StdoutPipe()
    must(err)
    // defer out.Close()

    fderr, err := cmd.StderrPipe()
    must(err)
    // defer fderr.Close()

    err = cmd.Start()
    must(err)

    wg := sync.WaitGroup{}
    wg.Add(1)
    go func() {
        defer wg.Done()
        log.Printf("wait result:%v", cmd.Wait())
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        handle("stdout", out)
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        handle("stderr", fderr)
    }()

    wg.Wait()
}

func handle(tag string, reader io.Reader) {
    buffer := make([]byte, 0, 4096)
    for {
        n, err := reader.Read(buffer)
        log.Printf("%s, n:%d, err:%v", tag, n, err)
        if n > 0 {
            log.Printf("***************%s:%s***************", tag, buffer[:n])
        }
        if io.EOF == err {
            log.Printf("fderr touch the end")
            break
        }
        if nil != err {
            break
        }
    }
}

func must(err error) {
    if nil != err {
        panic(err)
    }
}

output in cli:

....
....
2020/11/24 18:40:48 stdout, n:0, err:<nil>
2020/11/24 18:40:48 stdout, n:0, err:<nil>
2020/11/24 18:40:48 stdout, n:0, err:<nil>
2020/11/24 18:40:48 stdout, n:0, err:<nil>
2020/11/24 18:40:48 stdout, n:0, err:read |0: file already closed
2020/11/24 18:40:48 wait result:<nil>
2020/11/24 18:40:48 stderr, n:0, err:<nil>
2020/11/24 18:40:48 stderr, n:0, err:read |0: file already closed

must_panic is a golang process which must panic when executed

[fanghongwei@5d527337538b gdb]$ ./must_panic 
2020/11/24 18:52:28 user:uesr, passwd:passwd
panic: my friends and I get up early every day, so it is honored for us!

goroutine 1 [running]:
main.main()
        /home/fanghongwei/go/src/mycode/fuzz/gdb/panic/main.go:17 +0x14c

while I use another way to get stdout/err, this could get what I expected.

func main() {
    test()
}

func test() {
    var err error

    ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100000)
    // cmd := exec.CommandContext(ctx, "/usr/bin/gdb")
    cmd := exec.CommandContext(ctx, "/usr/bin/gdb", "-ex", "run", "./must_panic")
    log.Printf("command:%s", cmd.String())

    defer cancel()

    var sout bytes.Buffer
    var serr bytes.Buffer

    cmd.Stdout = &sout
    cmd.Stderr = &serr

    err = cmd.Start()
    must(err)

    log.Printf("wait result:%v", cmd.Wait())
    log.Printf("out:%s***********************", sout.Bytes())
    log.Printf("err:%s***********************", serr.Bytes())

}

What did you expect to see?

2020/11/24 18:46:46 command:/usr/bin/gdb -ex run ./must_panic
2020/11/24 18:46:46 wait result:<nil>
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./must_panic...done.
To enable execution of this file add
        add-auto-load-safe-path /usr/local/go/src/runtime/runtime-gdb.py
line to your configuration file "/home/fanghongwei/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/fanghongwei/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
Starting program: /home/fanghongwei/go/src/mycode/fuzz/gdb/must_panic 
[New LWP 11420]
[New LWP 11421]
[New LWP 11422]
[LWP 11420 exited]
[LWP 11416 exited]
[LWP 11421 exited]
[Inferior 1 (process 11416) exited with code 02]
(gdb) quit
***********************
2020/11/24 18:46:46 err:warning: File "/usr/local/go/src/runtime/runtime-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
2020/11/24 18:46:46 user:uesr, passwd:passwd
panic: my friends and I get up early every day, so it is honored for us!

goroutine 1 [running]:
main.main()
        /home/fanghongwei/go/src/mycode/fuzz/gdb/panic/main.go:17 +0x14c
***********************
@mvdan
Copy link
Member

mvdan commented Nov 24, 2020

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions. You can come back to the issue tracker if or when you're pretty certain that you have found a specific bug in Go.

@mvdan mvdan closed this as completed Nov 24, 2020
@golang golang locked and limited conversation to collaborators Nov 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants