Skip to content

Commit 85effc8

Browse files
authored
Allow SIGINT (ctrl-C) to be forwarded to debugger process (#596)
* Allow SIGINT (ctrl-C) to be forwarded to debugger process * Send the correct signal to process
1 parent 88468f2 commit 85effc8

File tree

8 files changed

+203
-43
lines changed

8 files changed

+203
-43
lines changed

cli/debug/debug.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package debug
1818
import (
1919
"context"
2020
"os"
21+
"os/signal"
2122

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

72+
// Intercept SIGINT and forward them to debug process
73+
ctrlc := make(chan os.Signal, 1)
74+
signal.Notify(ctrlc, os.Interrupt)
75+
7176
if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{
7277
Instance: &dbg.Instance{Id: instance.GetId()},
7378
Fqbn: fqbn,
7479
SketchPath: sketchPath.String(),
7580
Port: port,
7681
ImportFile: importFile,
77-
}, os.Stdin, os.Stdout); err != nil {
82+
}, os.Stdin, os.Stdout, ctrlc); err != nil {
7883
feedback.Errorf("Error during Debug: %v", err)
7984
os.Exit(errorcodes.ErrGeneric)
8085
}

commands/daemon/debug.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package daemon
1717

1818
import (
19+
"os"
20+
1921
"github.com/arduino/arduino-cli/arduino/utils"
2022
cmd "github.com/arduino/arduino-cli/commands/debug"
2123
dbg "github.com/arduino/arduino-cli/rpc/debug"
@@ -43,14 +45,20 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
4345
}
4446

4547
// Launch debug recipe attaching stdin and out to grpc streaming
48+
signalChan := make(chan os.Signal)
49+
defer close(signalChan)
4650
resp, err := cmd.Debug(stream.Context(), req,
4751
utils.ConsumeStreamFrom(func() ([]byte, error) {
4852
command, err := stream.Recv()
53+
if command.GetSendInterrupt() {
54+
signalChan <- os.Interrupt
55+
}
4956
return command.GetData(), err
5057
}),
5158
utils.FeedStreamTo(func(data []byte) {
5259
stream.Send(&dbg.DebugResp{Data: data})
53-
}))
60+
}),
61+
signalChan)
5462
if err != nil {
5563
return (err)
5664
}

commands/debug/debug.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package debug
1818
import (
1919
"context"
2020
"fmt"
21-
"github.com/pkg/errors"
2221
"io"
2322
"os"
2423
"path/filepath"
@@ -33,6 +32,7 @@ import (
3332
dbg "github.com/arduino/arduino-cli/rpc/debug"
3433
"github.com/arduino/go-paths-helper"
3534
"github.com/arduino/go-properties-orderedmap"
35+
"github.com/pkg/errors"
3636
"github.com/sirupsen/logrus"
3737
)
3838

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

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

76+
if interrupt != nil {
77+
go func() {
78+
for {
79+
if sig, ok := <-interrupt; !ok {
80+
break
81+
} else {
82+
cmd.Process.Signal(sig)
83+
}
84+
}
85+
}()
86+
}
87+
7688
go func() {
7789
// Copy data from passed inStream into command stdIn
7890
io.Copy(in, inStream)

rpc/commands/commands.pb.go

+86-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/debug/debug.pb.go

+48-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/debug/debug.proto

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ message DebugReq {
4040

4141
// The data to be sent to the target being monitored.
4242
bytes data = 2;
43+
44+
// Set this to true to send and Interrupt signal to the debugger process
45+
bool send_interrupt = 3;
4346
}
4447

4548
message DebugConfigReq {

0 commit comments

Comments
 (0)