Skip to content
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

Supports sending output to clients when running programs remotely #3253

Merged
merged 61 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
4a06698
wip: Support sending output when remote debug
tttoad Jan 17, 2023
6968a79
wip: Support local output and remote output
tttoad Jan 19, 2023
105696d
wip: fix stderr and stdout assignment error
tttoad Jan 19, 2023
444eeb1
wip: optimize code
tttoad Jan 20, 2023
3869956
wip: Only if outputMode is "remote" is the redirected console output
tttoad Jan 21, 2023
8a43523
wip: Redirected debugMode output(Not tested on windows)
tttoad Jan 31, 2023
4e47c69
wip: support remote debugging output redirection of windows
tttoad Jan 31, 2023
8350405
wip: real-time write back output
tttoad Feb 1, 2023
9c96fbe
wip: support for windows
tttoad Feb 2, 2023
c4b8ce2
wip: fix windows remote debug not output
tttoad Feb 2, 2023
a605ff9
wip: fix truncated output redirection
tttoad Feb 2, 2023
d56ac9f
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Feb 2, 2023
984e012
wip: delete printfln
tttoad Feb 3, 2023
63341e7
wip: use debugger.Config to pass redirect(macOS)
tttoad Feb 9, 2023
d5f5fb3
wip: use debugger.Config to pass redirect(linux)
tttoad Feb 11, 2023
4621f4c
wip: Change redirect to a concrete type
tttoad Feb 11, 2023
f433b23
wip: s.wg.wait before sending "terminated"
tttoad Feb 11, 2023
ffad8a4
wip: add proc/redirect test(darwin and linux)
tttoad Feb 17, 2023
a1467d6
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Feb 18, 2023
70216a5
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Feb 19, 2023
902235d
wip: Fix test failure on windows
tttoad Feb 20, 2023
9e08369
fix: undefined: proc.Redirects
tttoad Feb 20, 2023
0b3c066
fix: compile failure
tttoad Feb 20, 2023
38958bc
wip: Remove useless code
tttoad Feb 20, 2023
fc89882
fix: filename error
tttoad Feb 23, 2023
e6a0c78
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Feb 26, 2023
359ee2c
fix: os.file not close
tttoad Feb 26, 2023
ad0dec1
test: add server_test.redirect
tttoad Mar 1, 2023
73c7076
fix: Remove 'eol' from end of file
tttoad Mar 2, 2023
61092e2
fix: gdbserial: File not closed in file mode.
tttoad Mar 2, 2023
bdd13c9
feat: Remove "only-remote". Fix spelling mistakes.
tttoad Mar 3, 2023
f43b3d3
fix: spelling mistakes
tttoad Mar 3, 2023
28cea47
refactor: redirect
tttoad Mar 8, 2023
c0b0d07
fix: stdout and stderr are not set to default values
tttoad Mar 9, 2023
4617c1f
fix: Restore code logic for rr.openRedirects()
tttoad Mar 9, 2023
123de13
fix: Optimization Code
tttoad Mar 15, 2023
980d430
fix: utiltest
tttoad Mar 15, 2023
72c6718
fix: execpt out
tttoad Mar 15, 2023
f83ec15
fix: Resource release for redirects
tttoad Mar 18, 2023
31dba49
fix: build failure
tttoad Mar 18, 2023
8ebdb9d
fix: clean->clear
tttoad Mar 18, 2023
7d441c2
fix: build failure
tttoad Mar 19, 2023
dfa96ba
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Mar 19, 2023
ca76f9d
fix: test failure
tttoad Mar 19, 2023
36d0e92
fix: Optimization Code
Mar 20, 2023
6e33d4a
style: remove useless code
tttoad Mar 27, 2023
702341d
refactor: namedpipe
tttoad Apr 1, 2023
5aeb3f8
refactor: namedpipe, launch ...
tttoad Apr 4, 2023
10e0a66
fix: freebsd compile failure
tttoad Apr 4, 2023
1e3701b
fix: proc_darwin compile failure
tttoad Apr 4, 2023
4c8f86b
style: remove useless code
tttoad Apr 4, 2023
c016bc1
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Apr 5, 2023
a4f0491
feat: add d.config.Stdxx check on debug.Restart
tttoad Apr 8, 2023
0c2f3ad
style: formatting and adding comments
tttoad May 1, 2023
02d96ec
style: formatting and adding comments
tttoad May 1, 2023
44fa283
feat: add d.config.Stdxx check on debug.Restart
tttoad Apr 8, 2023
768fad8
style: namedpipe->redirector
tttoad May 12, 2023
f27bfcf
Merge branch 'feat-console' of github.com:tttoad/delve into feat-console
tttoad May 12, 2023
7dd6f49
style: namedPipe->redirector
tttoad May 12, 2023
7725eae
Merge branch 'master' into feat-console
tttoad Jun 13, 2023
38969ab
Merge branch 'master' of github.com:tttoad/delve into feat-console
tttoad Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/proc/gdbserial/gdbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import (
"github.com/go-delve/delve/pkg/proc/internal/ebpf"
"github.com/go-delve/delve/pkg/proc/linutil"
"github.com/go-delve/delve/pkg/proc/macutil"
"github.com/go-delve/delve/pkg/proc/redirect"
isatty "github.com/mattn/go-isatty"
)

Expand Down Expand Up @@ -450,7 +451,7 @@ func getLdEnvVars() []string {
// LLDBLaunch starts an instance of lldb-server and connects to it, asking
// it to launch the specified target program with the specified arguments
// (cmd) on the specified directory wd.
func LLDBLaunch(cmd []string, wd string, flags proc.LaunchFlags, debugInfoDirs []string, tty string, redirects [3]string) (*proc.Target, error) {
func LLDBLaunch(cmd []string, wd string, flags proc.LaunchFlags, debugInfoDirs []string, tty string, redirects redirect.Redirect) (*proc.Target, error) {
if runtime.GOOS == "windows" {
return nil, ErrUnsupportedOS
}
Expand Down Expand Up @@ -483,11 +484,20 @@ func LLDBLaunch(cmd []string, wd string, flags proc.LaunchFlags, debugInfoDirs [
} else {
found := [3]bool{}
names := [3]string{"stdin", "stdout", "stderr"}
for i := range redirects {
if redirects[i] != "" {
rds := [3]string{}
if paths, err := redirects.RedirectPath(); err != nil {
if !errors.Is(redirect.ErrorNotImplemented, err) {
return nil, err
}
} else {
rds = paths
}

for i := range rds {
if rds[i] != "" {
found[i] = true
hasRedirects = true
args = append(args, fmt.Sprintf("--%s-path", names[i]), redirects[i])
args = append(args, fmt.Sprintf("--%s-path", names[i]), rds[i])
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/proc/gdbserial/rr.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ func openRedirects(redirects [3]string, quiet bool) (stdin, stdout, stderr *os.F
}
return dflt
}

var f *os.File
f, err = os.Create(path)
if f != nil {
toclose = append(toclose, f)
}

return f
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/proc/native/nonative_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"github.com/go-delve/delve/pkg/proc"
"github.com/go-delve/delve/pkg/proc/amd64util"
"github.com/go-delve/delve/pkg/proc/internal/ebpf"
"github.com/go-delve/delve/pkg/proc/redirect"
)

var ErrNativeBackendDisabled = errors.New("native backend disabled during compilation")

// Launch returns ErrNativeBackendDisabled.
func Launch(_ []string, _ string, _ proc.LaunchFlags, _ []string, _ string, _ [3]string) (*proc.Target, error) {
func Launch(_ []string, _ string, _ proc.LaunchFlags, _ []string, _ string, _ redirect.Redirect) (*proc.Target, error) {
return nil, ErrNativeBackendDisabled
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/proc/native/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"

"github.com/go-delve/delve/pkg/proc"
"github.com/go-delve/delve/pkg/util"
)

// Process represents all of the information the debugger
Expand Down Expand Up @@ -324,7 +325,11 @@ func openRedirects(redirects [3]string, foreground bool) (stdin, stdout, stderr
return dflt
}
var f *os.File
f, err = os.Create(path)
if pipe, ok := util.GetRedirectStrore().Load(path); ok {
f = pipe.Writer
} else {
f, err = os.Create(path)
}
if f != nil {
toclose = append(toclose, f)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/proc/redirect/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package redirect

import (
"errors"
"os"
)

var ErrorNotImplemented = errors.New("not implemented")

// Redirect
type Redirect interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a concrete type, put it in pkg/proc, not in its own package, make it describe a single redirect rather than three redirects in one. Do not repeat the type name in the name of its methods. For example, it should be (*Redirect).Path not (*Redirect).RedirectPath.

Restart is a word, it shouldn't be spelled ReStart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it describe a single redirect rather than three redirects in one

Still TBD.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean WriterFiles [3]*os.File -> StdinWriter *os.File, StdoutWriter *os.File, StderrWriter *os.File

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be a string/io.Writer pair:

type OutputRedirect struct {
     Path string
     Writer io.Writer
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

type OutputRedirect struct {
	Path string
	File *os.File
}

Launch(.....,redirect [3]OutputRedirect) 

I can't use io.Writer or io.WriteColser, because in gdbserial/rr.go it needs *os.File.
In service/service.go, the data will be read from this structure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarzilli Need a reply.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you suggested is ok.

// RedirectPath
RedirectPath() (redirects [3]string, err error)
// RedirectFile
RedirectReaderFile() (readerFile [3]*os.File, err error)
// RedirectWriterFile
RedirectWriterFile() (writerFile [3]*os.File, err error)
// ReStart
ReStart() error
}
36 changes: 36 additions & 0 deletions pkg/util/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package util
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shouldn't be used anymore and should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.


import (
"os"
"sync"
)

var (
redirectMap = &RedirectStroe{rs: sync.Map{}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but passing around pipes by stuffing them in a global variable is not acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only in windows system need to use the pipeline...
Using debugger.Config to pass the pipeline will change the definition of the native.Launch function.
Is this acceptable to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Also this will eventually need tests, both the nodebug path and the normal path. And Debugger.Restart needs to return an error in case pipe redirects are used (it would be even better if it worked correctly instead of erroring but that's hard and dap doesn't call Restart anyway).

)

type RedirectStroe struct {
rs sync.Map
}

func GetRedirectStrore() *RedirectStroe {
return redirectMap
}

func (r *RedirectStroe) Store(key string, val *Pipe) {
r.rs.Store(key, val)
}

func (r *RedirectStroe) Load(key string) (*Pipe, bool) {
val, ok := r.rs.Load(key)
if ok {
return val.(*Pipe), true
}

return nil, false
}

type Pipe struct {
Reader *os.File
Writer *os.File
}
156 changes: 150 additions & 6 deletions service/dap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,29 @@ type Session struct {
// changeStateMu must be held for a request to protect itself from another goroutine
// changing the state of the running process at the same time.
changeStateMu sync.Mutex

// outputMode specifies how to print the program's output.
outputMode outputMode

// stdoutReader the programs's stdout.
stdoutReader io.ReadCloser

// stderrReader the program's stderr.
stderrReader io.ReadCloser

// wg the WaitGroup that needs to wait before sending a terminated event.
wg sync.WaitGroup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a more descriptive name instead of just wg.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}

type outputMode int8

const (
// outputToStd os.Stdin and os.Stdout
outputToStd outputMode = 1 << iota
// outputToDAP Sending program output to the client.
outputToDAP
)

// Config is all the information needed to start the debugger, handle
// DAP connection traffic and signal to the server when it is time to stop.
type Config struct {
Expand Down Expand Up @@ -786,6 +807,13 @@ func (s *Session) handleRequest(request dap.Message) {
}

func (s *Session) send(message dap.Message) {
if event, ok := message.(*dap.OutputEvent); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't hijack the send method to do other things, it should be done by the caller of this method, or maybe in a method that both waits for the readers to finish and then sends the terminated event.

if event.GetEvent().Event == "terminated" {
// wait for all tasks(output redirects) to finish
s.wg.Wait()
}
}

jsonmsg, _ := json.Marshal(message)
s.config.log.Debug("[-> to client]", string(jsonmsg))
// TODO(polina): consider using a channel for all the sends and to have a dedicated
Expand Down Expand Up @@ -974,7 +1002,7 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) {

var cmd string
var out []byte
var err error

switch args.Mode {
case "debug":
cmd, out, err = gobuild.GoBuildCombinedOutput(args.Output, []string{args.Program}, args.BuildFlags)
Expand Down Expand Up @@ -1025,9 +1053,61 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) {
argsToLog.Cwd, _ = filepath.Abs(args.Cwd)
s.config.log.Debugf("launching binary '%s' with config: %s", debugbinary, prettyPrint(argsToLog))

var redirected = false
s.outputMode |= outputToStd
switch args.OutputMode {
aarzilli marked this conversation as resolved.
Show resolved Hide resolved
case "remote":
s.outputMode |= outputToDAP
redirected = true
case "only-remote":
s.outputMode = outputToDAP
redirected = true
case "local", "":
// noting
default:
s.sendShowUserErrorResponse(request.Request, FailedToLaunch, "Failed to launch",
fmt.Sprintf("invalid debug configuration - unsupported 'outputMode' attribute %q", args.OutputMode))
return
}

readerFunc := func(reader io.Reader, category string) {
var stdWriter io.Writer
if category == "stdout" {
stdWriter = os.Stdout
} else {
stdWriter = os.Stderr
}

var out [1024]byte
for {
n, err := reader.Read(out[:])
if err != nil {
if errors.Is(io.EOF, err) {
return
}
s.config.log.Errorf("failed read by %s - %v ", category, err)
return
}
outs := string(out[:n])

if s.outputMode&outputToStd != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never happen now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.I will remove the part related to outputMode that has redirected enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

fmt.Fprintf(stdWriter, outs)
}

if s.outputMode&outputToDAP != 0 {
s.send(&dap.OutputEvent{
Event: *newEvent("output"),
Body: dap.OutputEventBody{
Output: outs,
Category: category,
}})
}
}
}

if args.NoDebug {
s.mu.Lock()
cmd, err := s.newNoDebugProcess(debugbinary, args.Args, s.config.Debugger.WorkingDir)
cmd, err := s.newNoDebugProcess(debugbinary, args.Args, s.config.Debugger.WorkingDir, redirected)
s.mu.Unlock()
if err != nil {
s.sendShowUserErrorResponse(request.Request, FailedToLaunch, "Failed to launch", err.Error())
Expand All @@ -1039,16 +1119,65 @@ func (s *Session) onLaunchRequest(request *dap.LaunchRequest) {

// Start the program on a different goroutine, so we can listen for disconnect request.
go func() {
if redirected {
s.wg.Add(1)
go func() {
defer s.wg.Done()
readerFunc(s.stdoutReader, "stdout")
}()

s.wg.Add(1)
go func() {
defer s.wg.Done()
readerFunc(s.stderrReader, "stderr")
}()
// Wait for the input and output to be read
}
if err := cmd.Wait(); err != nil {
s.config.log.Debugf("program exited with error: %v", err)
}

close(s.noDebugProcess.exited)
s.logToConsole(proc.ErrProcessExited{Pid: cmd.ProcessState.Pid(), Status: cmd.ProcessState.ExitCode()}.Error())
s.send(&dap.TerminatedEvent{Event: *newEvent("terminated")})
}()
return
}

if redirected {
redirector, err := NewRedirector()
if err != nil {
s.sendShowUserErrorResponse(request.Request, InternalError, "Internal Error",
fmt.Sprintf("failed to generate stdio pipes - %v", err))
return
}

s.config.Debugger.Redirect = redirector
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err = redirector.ReadRedirect("stdout", func(reader io.Reader) {
readerFunc(reader, "stdout")
}); err != nil {
s.sendShowUserErrorResponse(request.Request, InternalError, "Internal Error",
fmt.Sprintf("failed to open stdout pipe - %v", err))
return
}

}()
s.wg.Add(1)
go func() {
defer s.wg.Done()
if err = redirector.ReadRedirect("stderr", func(reader io.Reader) {
readerFunc(reader, "stderr")
}); err != nil {
s.sendShowUserErrorResponse(request.Request, FailedToLaunch, "Failed to launch",
fmt.Sprintf("failed to open stderr pipe - %v", err))
return
}
}()
}

func() {
s.mu.Lock()
defer s.mu.Unlock() // Make sure to unlock in case of panic that will become internal error
Expand Down Expand Up @@ -1082,15 +1211,30 @@ func (s *Session) getPackageDir(pkg string) string {

// newNoDebugProcess is called from onLaunchRequest (run goroutine) and
// requires holding mu lock. It prepares process exec.Cmd to be started.
func (s *Session) newNoDebugProcess(program string, targetArgs []string, wd string) (*exec.Cmd, error) {
func (s *Session) newNoDebugProcess(program string, targetArgs []string, wd string, redirected bool) (cmd *exec.Cmd, err error) {
if s.noDebugProcess != nil {
return nil, fmt.Errorf("another launch request is in progress")
}
cmd := exec.Command(program, targetArgs...)
cmd.Stdout, cmd.Stderr, cmd.Stdin, cmd.Dir = os.Stdout, os.Stderr, os.Stdin, wd
if err := cmd.Start(); err != nil {

cmd = exec.Command(program, targetArgs...)
aarzilli marked this conversation as resolved.
Show resolved Hide resolved
cmd.Stdin, cmd.Dir = os.Stdin, wd

if redirected {
if s.stderrReader, err = cmd.StderrPipe(); err != nil {
return nil, err
}

if s.stdoutReader, err = cmd.StdoutPipe(); err != nil {
return nil, err
}
} else {
cmd.Stdout, cmd.Stderr = os.Stdin, os.Stderr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr

}

if err = cmd.Start(); err != nil {
return nil, err
}

s.noDebugProcess = &process{Cmd: cmd, exited: make(chan struct{})}
return cmd, nil
}
Expand Down
Loading