delve/pkg/proc/gdbserial/rr.go

338 lines
8.0 KiB
Go
Raw Normal View History

package gdbserial
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
"syscall"
"github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/pkg/proc"
)
const (
delveRecordFlagsEnvVar = "DELVE_RR_RECORD_FLAGS"
delveReplayFlagsEnvVar = "DELVE_RR_REPLAY_FLAGS"
)
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
// RecordAsync configures rr to record the execution of the specified
// program. Returns a run function which will actually record the program, a
// stop function which will prematurely terminate the recording of the
// program.
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
func RecordAsync(cmd []string, wd string, quiet bool, stdin string, stdout proc.OutputRedirect, stderr proc.OutputRedirect) (run func() (string, error), stop func() error, err error) {
if err := checkRRAvailable(); err != nil {
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
return nil, nil, err
}
rfd, wfd, err := os.Pipe()
if err != nil {
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
return nil, nil, err
}
args := make([]string, 0, len(cmd)+2)
args = append(args, "record", "--print-trace-dir=3")
args = append(args, config.SplitQuotedFields(os.Getenv(delveRecordFlagsEnvVar), '"')...)
args = append(args, cmd...)
rrcmd := exec.Command("rr", args...)
var closefn func()
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
rrcmd.Stdin, rrcmd.Stdout, rrcmd.Stderr, closefn, err = openRedirects(stdin, stdout, stderr, quiet)
if err != nil {
return nil, nil, err
}
rrcmd.ExtraFiles = []*os.File{wfd}
rrcmd.Dir = wd
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
tracedirChan := make(chan string)
go func() {
bs, _ := io.ReadAll(rfd)
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
tracedirChan <- strings.TrimSpace(string(bs))
}()
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
run = func() (string, error) {
err := rrcmd.Run()
closefn()
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
_ = wfd.Close()
tracedir := <-tracedirChan
return tracedir, err
}
stop = func() error {
return rrcmd.Process.Signal(syscall.SIGTERM)
}
return run, stop, nil
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
func openRedirects(stdinPath string, stdoutOR proc.OutputRedirect, stderrOR proc.OutputRedirect, quiet bool) (stdin, stdout, stderr *os.File, closefn func(), err error) {
toclose := []*os.File{}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
if stdinPath != "" {
stdin, err = os.Open(stdinPath)
if err != nil {
return nil, nil, nil, nil, err
}
toclose = append(toclose, stdin)
} else {
stdin = os.Stdin
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
create := func(redirect proc.OutputRedirect, dflt *os.File) (f *os.File) {
if redirect.Path != "" {
f, err = os.Create(redirect.Path)
if f != nil {
toclose = append(toclose, f)
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
return f
} else if redirect.File != nil {
toclose = append(toclose, redirect.File)
return redirect.File
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
if quiet {
return nil
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
return dflt
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
stdout = create(stdoutOR, os.Stdout)
if err != nil {
return nil, nil, nil, nil, err
}
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
stderr = create(stderrOR, os.Stderr)
if err != nil {
return nil, nil, nil, nil, err
}
closefn = func() {
for _, f := range toclose {
_ = f.Close()
}
}
return stdin, stdout, stderr, closefn, nil
}
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
// Record uses rr to record the execution of the specified program and
// returns the trace directory's path.
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
func Record(cmd []string, wd string, quiet bool, stdin string, stdout proc.OutputRedirect, stderr proc.OutputRedirect) (tracedir string, err error) {
run, _, err := RecordAsync(cmd, wd, quiet, stdin, stdout, stderr)
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
if err != nil {
return "", err
}
// ignore run errors, it could be the program crashing
proc/gdbserial,debugger: allow clients to stop a recording (#1890) Allows Delve clients to stop a recording midway by sending a Command('halt') request. This is implemented by changing debugger.New to start recording the process on a separate goroutine while holding the processMutex locked. By locking the processMutex we ensure that almost all RPC requests will block until the recording is done, since we can not respond correctly to any of them. API calls that do not require manipulating or examining the target process, such as "IsMulticlient", "SetApiVersion" and "GetState(nowait=true)" will work while we are recording the process. Two other internal changes are made to the API: both GetState and Restart become asynchronous requests, like Command. Restart because this way it can be interrupted by a StopRecording request if the rerecord option is passed. GetState because clients need a call that will block until the recording is compelted and can also be interrupted with a StopRecording. Clients that are uninterested in allowing the user to stop a recording can ignore this change, since eventually they will make a request to Delve that will block until the recording is completed. Clients that wish to support this feature must: 1. call GetState(nowait=false) after connecting to Delve, before any call that would need to manipulate the target process 2. allow the user to send a StopRecording request during the initial GetState call 3. allow the user to send a StopRecording request during any subsequent Restart(rerecord=true) request (if supported). Implements #1747
2020-03-24 16:09:28 +00:00
return run()
}
// Replay starts an instance of rr in replay mode, with the specified trace
// directory, and connects to it.
func Replay(tracedir string, quiet, deleteOnDetach bool, debugInfoDirs []string, rrOnProcessPid int, cmdline string) (*proc.TargetGroup, error) {
if err := checkRRAvailable(); err != nil {
return nil, err
}
args := []string{
"replay",
"--dbgport=0",
}
if rrOnProcessPid != 0 {
args = append(args, fmt.Sprintf("--onprocess=%d", rrOnProcessPid))
}
args = append(args, config.SplitQuotedFields(os.Getenv(delveReplayFlagsEnvVar), '\'')...)
args = append(args, tracedir)
rrcmd := exec.Command("rr", args...)
rrcmd.Stdout = os.Stdout
stderr, err := rrcmd.StderrPipe()
if err != nil {
return nil, err
}
rrcmd.SysProcAttr = sysProcAttr(false)
initch := make(chan rrInit)
go rrStderrParser(stderr, initch, quiet)
err = rrcmd.Start()
if err != nil {
return nil, err
}
init := <-initch
if init.err != nil {
rrcmd.Process.Kill()
return nil, init.err
}
p := newProcess(rrcmd.Process)
p.tracedir = tracedir
p.conn.useXcmd = true // 'rr' does not support the 'M' command which is what we would usually use to write memory, this is only important during function calls, in any other situation writing memory will fail anyway.
if deleteOnDetach {
p.onDetach = func() {
safeRemoveAll(p.tracedir)
}
}
tgt, err := p.Dial(init.port, init.exe, cmdline, 0, debugInfoDirs, proc.StopLaunched)
if err != nil {
rrcmd.Process.Kill()
return nil, err
}
return tgt, nil
}
// ErrPerfEventParanoid is the error returned by Reply and Record if
// /proc/sys/kernel/perf_event_paranoid is greater than 1.
type ErrPerfEventParanoid struct {
actual int
}
func (err ErrPerfEventParanoid) Error() string {
return fmt.Sprintf("rr needs /proc/sys/kernel/perf_event_paranoid <= 1, but it is %d", err.actual)
}
func checkRRAvailable() error {
if _, err := exec.LookPath("rr"); err != nil {
return &ErrBackendUnavailable{}
}
// Check that /proc/sys/kernel/perf_event_paranoid doesn't exist or is <= 1.
buf, err := os.ReadFile("/proc/sys/kernel/perf_event_paranoid")
if err == nil {
perfEventParanoid, _ := strconv.Atoi(strings.TrimSpace(string(buf)))
if perfEventParanoid > 1 {
return ErrPerfEventParanoid{perfEventParanoid}
}
}
return nil
}
type rrInit struct {
port string
exe string
err error
}
const (
rrGdbCommandLegacyPrefix = " gdb "
rrGdbCommandPrefix = " 'gdb' "
rrGdbLaunchLegacyPrefix = "Launch gdb with"
rrGdbLaunchPrefix = "Launch debugger with"
targetCmd = "target extended-remote "
)
func rrStderrParser(stderr io.ReadCloser, initch chan<- rrInit, quiet bool) {
rd := bufio.NewReader(stderr)
defer stderr.Close()
for {
line, err := rd.ReadString('\n')
if err != nil {
initch <- rrInit{"", "", err}
close(initch)
return
}
var flags string
var foundPrefix bool
if flags, foundPrefix = strings.CutPrefix(line, rrGdbCommandPrefix); !foundPrefix {
flags, foundPrefix = strings.CutPrefix(line, rrGdbCommandLegacyPrefix)
}
if foundPrefix {
initch <- rrParseGdbCommand(flags)
close(initch)
break
}
if strings.HasPrefix(line, rrGdbLaunchPrefix) || strings.HasPrefix(line, rrGdbLaunchLegacyPrefix) {
continue
}
if !quiet {
os.Stderr.WriteString(line)
}
}
io.Copy(os.Stderr, rd)
}
type ErrMalformedRRGdbCommand struct {
line, reason string
}
func (err *ErrMalformedRRGdbCommand) Error() string {
return fmt.Sprintf("malformed gdb command %q: %s", err.line, err.reason)
}
func rrParseGdbCommand(line string) rrInit {
port := ""
fields := config.SplitQuotedFields(line, '\'')
for i := 0; i < len(fields); i++ {
switch fields[i] {
case "-ex":
if i+1 >= len(fields) {
return rrInit{err: &ErrMalformedRRGdbCommand{line, "-ex not followed by an argument"}}
}
arg := fields[i+1]
if !strings.HasPrefix(arg, targetCmd) {
continue
}
port = arg[len(targetCmd):]
i++
case "-l":
// skip argument
i++
}
}
if port == "" {
return rrInit{err: &ErrMalformedRRGdbCommand{line, "could not find -ex argument"}}
}
exe := fields[len(fields)-1]
return rrInit{port: port, exe: exe}
}
// RecordAndReplay acts like calling Record and then Replay.
pkg/proc,service/*: Supports sending output to clients when running programs remotely (#3253) * wip: Support sending output when remote debug * wip: Support local output and remote output * wip: fix stderr and stdout assignment error * wip: optimize code * wip: Only if outputMode is "remote" is the redirected console output * wip: Redirected debugMode output(Not tested on windows) * wip: support remote debugging output redirection of windows * wip: real-time write back output * wip: support for windows * wip: fix windows remote debug not output * wip: fix truncated output redirection * wip: delete printfln * wip: use debugger.Config to pass redirect(macOS) * wip: use debugger.Config to pass redirect(linux) * wip: Change redirect to a concrete type * wip: s.wg.wait before sending "terminated" * wip: add proc/redirect test(darwin and linux) * Merge branch 'master' of github.com:tttoad/delve into feat-console * wip: Fix test failure on windows * fix: undefined: proc.Redirects * fix: compile failure * wip: Remove useless code * fix: filename error * fix: os.file not close * test: add server_test.redirect * fix: Remove 'eol' from end of file * fix: gdbserial: File not closed in file mode. (in reality, gdbserial will never use file mode) * feat: Remove "only-remote". Fix spelling mistakes. * fix: spelling mistakes * refactor: redirect * fix: stdout and stderr are not set to default values * fix: Restore code logic for rr.openRedirects() * fix: Optimization Code * fix: utiltest * fix: execpt out * fix: Resource release for redirects * fix: build failure * fix: clean->clear * fix: build failure * fix: test failure * fix: Optimization Code * style: remove useless code * refactor: namedpipe * refactor: namedpipe, launch ... * fix: freebsd compile failure * fix: proc_darwin compile failure * style: remove useless code * feat: add d.config.Stdxx check on debug.Restart * style: formatting and adding comments * style: formatting and adding comments * feat: add d.config.Stdxx check on debug.Restart * style: namedpipe->redirector * style: namedPipe->redirector --------- Co-authored-by: 李翔 <qian.fu2@amh-group.com>
2023-07-05 15:39:01 +00:00
func RecordAndReplay(cmd []string, wd string, quiet bool, debugInfoDirs []string, stdin string, stdout proc.OutputRedirect, stderr proc.OutputRedirect) (*proc.TargetGroup, string, error) {
tracedir, err := Record(cmd, wd, quiet, stdin, stdout, stderr)
if tracedir == "" {
return nil, "", err
}
t, err := Replay(tracedir, quiet, true, debugInfoDirs, 0, strings.Join(cmd, " "))
return t, tracedir, err
}
// safeRemoveAll removes dir and its contents but only as long as dir does
// not contain directories.
func safeRemoveAll(dir string) {
fis, err := os.ReadDir(dir)
if err != nil {
return
}
for _, fi := range fis {
if fi.IsDir() {
return
}
}
for _, fi := range fis {
if err := os.Remove(filepath.Join(dir, fi.Name())); err != nil {
return
}
}
os.Remove(dir)
}