Skip to content

Commit

Permalink
use pipe for communication between server and client to not consume a…
Browse files Browse the repository at this point in the history
… port for each operation
  • Loading branch information
ahmedakef committed Jan 19, 2025
1 parent 4705277 commit f6fa1d5
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 110 deletions.
46 changes: 46 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/ahmedakef/gotutor/gateway"
"github.com/ahmedakef/gotutor/serialize"
"github.com/rs/zerolog"
)

func getAndWriteSteps(ctx context.Context, client *gateway.Debug, logger zerolog.Logger, multipleGoroutines bool) error {

defer func() {
logger.Info().Msg("killing the debugger")
err := client.Detach(true)
if err != nil {
logger.Error().Err(err).Msg("failed to halt the execution")
}
}()

serializer := serialize.NewSerializer(client, logger, multipleGoroutines)
steps, err := serializer.ExecutionSteps(ctx)
if err != nil {
return fmt.Errorf("failed to get execution steps: %w", err)
}
// put the result in steps.json file
file, err := os.OpenFile("steps.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open steps.json file: %w", err)
}
defer file.Close()

err = json.NewEncoder(file).Encode(steps)
if err != nil {
return fmt.Errorf("failed to encode steps: %w", err)
}
// Explicitly flush the file buffer
err = file.Sync()
if err != nil {
return fmt.Errorf("failed to flush file buffer: %w", err)
}
return nil
}
17 changes: 16 additions & 1 deletion cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package cmd
import (
"context"
"fmt"

"github.com/ahmedakef/gotutor/dlv"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand All @@ -31,7 +33,19 @@ func connect(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to get multiple-goroutines flag: %w", err)
}
err = getAndWriteSteps(ctx, logger, multipleGoroutines)

addr, err := cmd.Flags().GetString("address")
if err != nil {
return fmt.Errorf("failed to get address flag: %w", err)
}

client, err := dlv.Connect(addr)
if err != nil {
logger.Error().Err(err).Msg("failed to connect to server")
return nil
}

err = getAndWriteSteps(ctx, client, logger, multipleGoroutines)
if err != nil {
logger.Error().Err(err).Msg("getAndWriteSteps")
return nil
Expand All @@ -40,5 +54,6 @@ func connect(cmd *cobra.Command, args []string) error {
}

func init() {
connectCmd.Flags().String("address", ":8083", "address of the server to connect to")
rootCmd.AddCommand(connectCmd)
}
38 changes: 16 additions & 22 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package cmd
import (
"context"
"fmt"
"time"

"github.com/rs/zerolog"

"github.com/ahmedakef/gotutor/dlv"
"github.com/go-delve/delve/pkg/gobuild"
"github.com/go-delve/delve/service/debugger"
"github.com/spf13/cobra"
)

Expand All @@ -30,43 +30,37 @@ to quickly create a Cobra application.`,
}

func debug(cmd *cobra.Command, args []string) error {
multipleGoroutines, err := cmd.Flags().GetBool("multiple-goroutines")
if err != nil {
return fmt.Errorf("failed to get multiple-goroutines flag: %w", err)
}

ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
logger := ctx.Value("logger").(zerolog.Logger)

sourcePath := ""
if len(args) == 1 {
sourcePath = args[0]
}
binaryPath, err := dlv.Build(sourcePath, "")
if err != nil {
return fmt.Errorf("failed to build binary: %w", err)
logger.Error().Err(err).Msg("failed to build binary")
return nil
}
defer gobuild.Remove(binaryPath)
debugServerErr := make(chan error, 1)
go func() {
err := dlv.RunDebugServer(binaryPath, addr)
debugServerErr <- err
}()
time.Sleep(1 * time.Second)

ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
logger := ctx.Value("logger").(zerolog.Logger)
multipleGoroutines, err := cmd.Flags().GetBool("multiple-goroutines")
client, err := dlv.RunServerAndGetClient(binaryPath, sourcePath, dlv.GetBuildFlags(), debugger.ExecutingGeneratedFile)
if err != nil {
return fmt.Errorf("failed to get multiple-goroutines flag: %w", err)
return fmt.Errorf("failed to connect: %w", err)
}
err = getAndWriteSteps(ctx, logger, multipleGoroutines)

err = getAndWriteSteps(ctx, client, logger, multipleGoroutines)
if err != nil {
logger.Error().Err(err).Msg("getAndWriteSteps")
return nil
}

select {
case err := <-debugServerErr:
if err != nil {
logger.Error().Err(err).Msg("debugServer error occurred")
}
default:
}

return nil
}

Expand Down
42 changes: 17 additions & 25 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ package cmd
import (
"context"
"fmt"
"github.com/rs/zerolog"
"time"

"github.com/ahmedakef/gotutor/dlv"
"github.com/go-delve/delve/service/debugger"
"github.com/rs/zerolog"

"github.com/spf13/cobra"
)

var addr = ":8083"

// execCmd represents the exec command
var execCmd = &cobra.Command{
Use: "exec",
Expand All @@ -30,37 +29,30 @@ to quickly create a Cobra application.`,
}

func execute(cmd *cobra.Command, args []string) error {
binaryPath := "."
if len(args) == 1 {
binaryPath = args[0]
multipleGoroutines, err := cmd.Flags().GetBool("multiple-goroutines")
if err != nil {
return fmt.Errorf("failed to get multiple-goroutines flag: %w", err)
}

debugServerErr := make(chan error, 1)
go func() {
err := dlv.RunDebugServer(binaryPath, addr)
debugServerErr <- err
}()
time.Sleep(1 * time.Second)

ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
logger := ctx.Value("logger").(zerolog.Logger)
multipleGoroutines, err := cmd.Flags().GetBool("multiple-goroutines")
if err != nil {
return fmt.Errorf("failed to get multiple-goroutines flag: %w", err)

binaryPath := "."
if len(args) == 1 {
binaryPath = args[0]
}
err = getAndWriteSteps(ctx, logger, multipleGoroutines)

client, err := dlv.RunServerAndGetClient(binaryPath, "", dlv.GetBuildFlags(), debugger.ExecutingGeneratedFile)
if err != nil {
logger.Error().Err(err).Msg("getAndWriteSteps")
logger.Error().Err(err).Msg("failed to connect to server")
return nil
}

select {
case err := <-debugServerErr:
if err != nil {
logger.Error().Err(err).Msg("debugServer error occurred")
}
default:
err = getAndWriteSteps(ctx, client, logger, multipleGoroutines)
if err != nil {
logger.Error().Err(err).Msg("getAndWriteSteps")
return nil
}
return nil

Expand Down
57 changes: 0 additions & 57 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"github.com/ahmedakef/gotutor/serialize"

"github.com/ahmedakef/gotutor/gateway"

"github.com/ahmedakef/gotutor/dlv"

"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,52 +44,3 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().Bool("multiple-goroutines", false, "handle multiple goroutines // not well supported yet")
}

func dlvGatewayClient(logger zerolog.Logger) (*gateway.Debug, error) {
rpcClient, err := dlv.Connect(addr)
if err != nil {
logger.Error().Err(err).Msg("failed to connect to server")
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
client := gateway.NewDebug(rpcClient)
return client, nil

}

func getAndWriteSteps(ctx context.Context, logger zerolog.Logger, multipleGoroutines bool) error {
client, err := dlvGatewayClient(logger)
if err != nil {
return fmt.Errorf("failed to create dlvGatewayClient: %w", err)
}

defer func() {
logger.Info().Msg("killing the debugger")
err = client.Detach(true)
if err != nil {
logger.Error().Err(err).Msg("failed to halt the execution")
}
}()

serializer := serialize.NewSerializer(client, logger, multipleGoroutines)
steps, err := serializer.ExecutionSteps(ctx)
if err != nil {
return fmt.Errorf("failed to get execution steps: %w", err)
}
// put the result in steps.json file
file, err := os.OpenFile("steps.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open steps.json file: %w", err)
}
defer file.Close()

err = json.NewEncoder(file).Encode(steps)
if err != nil {
return fmt.Errorf("failed to encode steps: %w", err)
}
// Explicitly flush the file buffer
err = file.Sync()
if err != nil {
return fmt.Errorf("failed to flush file buffer: %w", err)
}
return nil
}
7 changes: 4 additions & 3 deletions dlv/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"github.com/go-delve/delve/pkg/goversion"
)

// given a sourcePath, build the binary in temporary directory and return the path to the binary
// Build builds the binary in temporary directory and return the path to the binary given a sourcePath
func Build(sourcePath string, outputPrefix string) (string, error) {
args := []string{sourcePath}
debugName, err := buildBinary(args, outputPrefix, false)
return debugName, err
}

func buildBinary(args []string, outputPrefix string, isTest bool) (string, error) {
buildFlags := getBuildFlags()
buildFlags := GetBuildFlags()
var debugName string
var err error
if isTest {
Expand All @@ -33,7 +33,8 @@ func buildBinary(args []string, outputPrefix string, isTest bool) (string, error
return debugName, err
}

func getBuildFlags() string {
// GetBuildFlags returns the default build flags for the current platform
func GetBuildFlags() string {
buildFlagsDefault := ""
if runtime.GOOS == "windows" {
ver, _ := goversion.Installed()
Expand Down
Loading

0 comments on commit f6fa1d5

Please sign in to comment.