Skip to content

Allow SIGINT (ctrl-C) to be forwarded to debugger process #596

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

Merged
merged 2 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package debug
import (
"context"
"os"
"os/signal"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
Expand Down Expand Up @@ -68,13 +69,17 @@ func run(command *cobra.Command, args []string) {
}
sketchPath := initSketchPath(path)

// Intercept SIGINT and forward them to debug process
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)

if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{
Instance: &dbg.Instance{Id: instance.GetId()},
Fqbn: fqbn,
SketchPath: sketchPath.String(),
Port: port,
ImportFile: importFile,
}, os.Stdin, os.Stdout); err != nil {
}, os.Stdin, os.Stdout, ctrlc); err != nil {
feedback.Errorf("Error during Debug: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
Expand Down
10 changes: 9 additions & 1 deletion commands/daemon/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package daemon

import (
"os"

"github.com/arduino/arduino-cli/arduino/utils"
cmd "github.com/arduino/arduino-cli/commands/debug"
dbg "github.com/arduino/arduino-cli/rpc/debug"
Expand Down Expand Up @@ -43,14 +45,20 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
}

// Launch debug recipe attaching stdin and out to grpc streaming
signalChan := make(chan os.Signal)
defer close(signalChan)
resp, err := cmd.Debug(stream.Context(), req,
utils.ConsumeStreamFrom(func() ([]byte, error) {
command, err := stream.Recv()
if command.GetSendInterrupt() {
signalChan <- os.Interrupt
}
return command.GetData(), err
}),
utils.FeedStreamTo(func(data []byte) {
stream.Send(&dbg.DebugResp{Data: data})
}))
}),
signalChan)
if err != nil {
return (err)
}
Expand Down
16 changes: 14 additions & 2 deletions commands/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package debug
import (
"context"
"fmt"
"github.com/pkg/errors"
"io"
"os"
"path/filepath"
Expand All @@ -33,6 +32,7 @@ import (
dbg "github.com/arduino/arduino-cli/rpc/debug"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -42,7 +42,7 @@ import (
// grpc Out <- tool stdOut
// grpc Out <- tool stdErr
// It also implements tool process lifecycle management
func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out io.Writer) (*dbg.DebugResp, error) {
func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out io.Writer, interrupt <-chan os.Signal) (*dbg.DebugResp, error) {

// Get tool commandLine from core recipe
pm := commands.GetPackageManager(req.GetInstance().GetId())
Expand Down Expand Up @@ -73,6 +73,18 @@ func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out
return &dbg.DebugResp{Error: err.Error()}, nil
}

if interrupt != nil {
go func() {
for {
if sig, ok := <-interrupt; !ok {
break
} else {
cmd.Process.Signal(sig)
}
}
}()
}

go func() {
// Copy data from passed inStream into command stdIn
io.Copy(in, inStream)
Expand Down
90 changes: 86 additions & 4 deletions rpc/commands/commands.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 48 additions & 27 deletions rpc/debug/debug.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/debug/debug.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ message DebugReq {

// The data to be sent to the target being monitored.
bytes data = 2;

// Set this to true to send and Interrupt signal to the debugger process
bool send_interrupt = 3;
}

message DebugConfigReq {
Expand Down
Loading