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
This commit is contained in:
chainhelen 2020-06-02 14:07:03 -05:00 committed by GitHub
parent 4be75bec59
commit 04a0891913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 12 deletions

22
_fixtures/issue2023.go Normal file

@ -0,0 +1,22 @@
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 var processArgs []string
dlvArgs, targetArgs := splitArgs(cmd, args) 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 { if traceAttachPid == 0 {
var dlvArgsLen = len(dlvArgs) if dlvArgsLen >= 2 && traceExecFile != "" {
fmt.Fprintln(os.Stderr, "Cannot specify package when using exec.")
if dlvArgsLen == 1 { return 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]
} }
debugname := traceExecFile debugname := traceExecFile

@ -628,6 +628,52 @@ func TestTrace(t *testing.T) {
cmd.Wait() 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) { func TestTraceBreakpointExists(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)