未验证 提交 04a08919 编写于 作者: C chainhelen 提交者: GitHub

cmd/dlv: do not ignore regex when tracing pid (#2069)

The trace command line subcommand ignored the regexp argument
when the traced process was specified using the '-p' option.

Fixes #2023
上级 4be75bec
package main
import (
"fmt"
"time"
)
func A() {
fmt.Printf("hello delve\n")
}
func main() {
count := 0
for {
A()
time.Sleep(time.Millisecond * time.Duration(100))
if count >= 30 {
break
}
count++
}
}
......@@ -466,20 +466,19 @@ func traceCmd(cmd *cobra.Command, args []string) {
var processArgs []string
dlvArgs, targetArgs := splitArgs(cmd, args)
var dlvArgsLen = len(dlvArgs)
if dlvArgsLen == 1 {
regexp = args[0]
dlvArgs = dlvArgs[0:0]
} else if dlvArgsLen >= 2 {
regexp = dlvArgs[dlvArgsLen-1]
dlvArgs = dlvArgs[:dlvArgsLen-1]
}
if traceAttachPid == 0 {
var dlvArgsLen = len(dlvArgs)
if dlvArgsLen == 1 {
regexp = args[0]
dlvArgs = dlvArgs[0:0]
} else if dlvArgsLen >= 2 {
if traceExecFile != "" {
fmt.Fprintln(os.Stderr, "Cannot specify package when using exec.")
return 1
}
regexp = dlvArgs[dlvArgsLen-1]
dlvArgs = dlvArgs[:dlvArgsLen-1]
if dlvArgsLen >= 2 && traceExecFile != "" {
fmt.Fprintln(os.Stderr, "Cannot specify package when using exec.")
return 1
}
debugname := traceExecFile
......
......@@ -628,6 +628,52 @@ func TestTrace(t *testing.T) {
cmd.Wait()
}
func TestTracePid(t *testing.T) {
if runtime.GOOS == "linux" {
bs, _ := ioutil.ReadFile("/proc/sys/kernel/yama/ptrace_scope")
if bs == nil || strings.TrimSpace(string(bs)) != "0" {
t.Logf("can not run TestAttachDetach: %v\n", bs)
return
}
}
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
expected := []byte("goroutine(1): main.A() => ()\n")
// make process run
fix := protest.BuildFixture("issue2023", 0)
targetCmd := exec.Command(fix.Path)
if err := targetCmd.Start(); err != nil {
t.Fatal(err)
}
if targetCmd.Process == nil || targetCmd.Process.Pid == 0 {
t.Fatal("expected target process runninng")
}
defer targetCmd.Process.Kill()
// dlv attach the process by pid
cmd := exec.Command(dlvbin, "trace", "-p", strconv.Itoa(targetCmd.Process.Pid), "main.A")
rdr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
err = cmd.Start()
if err != nil {
t.Fatalf("error running trace: %#v", err)
}
output, err := ioutil.ReadAll(rdr)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(output, expected) {
t.Fatalf("expected:\n%s\ngot:\n%s", string(expected), string(output))
}
cmd.Wait()
}
func TestTraceBreakpointExists(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册