go run 调试误区

go 刘宇帅 6年前 阅读量: 1577

我们写一个关于系统信号量的程序并用 go run 调试。
测试代码如下:

package main

import (
    "os"
    "os/signal"
    "fmt"
    "syscall"
)

func main() {
    sig := make(chan os.Signal)
    signal.Notify(sig, syscall.SIGUSR2)
    for {
        select {
        case s := <-sig:
            fmt.Println(s.String())
        }
    }
}

使用 go run main.go 运行程序,并使用ps -ef|grep main.go 找到进程 ID,然后传递 USR2 信号。

kill -USR2 [id]

发现程序并没有任何输出,其实我们只要知道 go run 命令的原理这两点就不难理解,go run 命令会编译 go 源码为可执行文件并保存到临时目录,然后执行可执行文件,所以我们运行的 go run 命令的进程号并不是我们的 go 代码进程号。
我们可以使用 go run -x main.go 查看 go run 命令执行的一些列命令集合。

> $ go run -x main.go
WORK=/var/folders/7l/7jhnyrh96bb75x9ljjt2vyz80000gn/T/go-build415823243
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/liushuai/Library/Caches/go-build/fd/fd82f5fac0921efb6ce9dd7abb086e6324c196d2d4c71186f461975d358cc314-d
packagefile fmt=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/fmt.a
packagefile os=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/os.a
packagefile os/signal=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/os/signal.a
packagefile syscall=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/syscall.a
packagefile runtime=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/runtime.a
packagefile errors=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/errors.a
packagefile io=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/io.a
packagefile math=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/math.a
packagefile reflect=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/reflect.a
packagefile strconv=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/strconv.a
packagefile sync=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/sync.a
packagefile unicode/utf8=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/unicode/utf8.a
packagefile internal/poll=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/poll.a
packagefile internal/syscall/unix=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/syscall/unix.a
packagefile internal/testlog=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/testlog.a
packagefile sync/atomic=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/sync/atomic.a
packagefile time=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/time.a
packagefile internal/race=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/race.a
packagefile internal/bytealg=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/bytealg.a
packagefile internal/cpu=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/internal/cpu.a
packagefile runtime/internal/atomic=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/runtime/internal/atomic.a
packagefile runtime/internal/sys=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/runtime/internal/sys.a
packagefile unicode=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/unicode.a
packagefile math/bits=/usr/local/Cellar/go/1.11/libexec/pkg/darwin_amd64/math/bits.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/Cellar/go/1.11/libexec/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/main -importcfg $WORK/b001/importcfg.link -s -w -buildmode=exe -buildid=leDM4Psk9hp9WmZnpLM_/_e3zL8UstMmCxJEhM2eG/ogq34jjayAFU3M9ke-dB/leDM4Psk9hp9WmZnpLM_ -extld=clang /Users/liushuai/Library/Caches/go-build/fd/fd82f5fac0921efb6ce9dd7abb086e6324c196d2d4c71186f461975d358cc314-d
$WORK/b001/exe/main

第一行的 WORK 变量的值就是临时执行文件放置目录的目录。最后一行 $WORK/b001/exe/main 就是运行临时目录文件,我们可以根据 WORK 变量名使用 ps -ef|grep “WORK” 找到我们程序运行的真正进程 id。
我这里找到进程ID方法如下:

> $ ps -ef|grep "/var/folders/7l/7jhnyrh96bb75x9ljjt2vyz80000gn/T/go-build415823243"|grep -v grep |awk '{print $2}' 
32315

发送 USR2 信号

kill -USR2 32315

可以看到如下输出

user defined signal 2

使用 -x 未免复杂,我们可以再 go run 后添加一个特定字符串就可以很容易找到了。
比如,我们以下方式启动

> $ go run main.go asdfadsfadsf

查找进程 id

> $ ps -ef|grep asdfadsfadsf |grep -v grep                                                                          [16:19:26]
  501 32542 20124   0  4:19下午 ttys011    0:00.21 go run main.go asdfadsfadsf
  501 32552 32542   0  4:19下午 ttys011    0:00.00 /var/folders/7l/7jhnyrh96bb75x9ljjt2vyz80000gn/T/go-build688063792/b001/exe/main asdfadsfadsf

第二个进程即我们代码运行的真正进程。

提示

功能待开通!


暂无评论~