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

feat: add environment variables support for command execution #67

Merged
merged 3 commits into from
Jan 10, 2025
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
4 changes: 3 additions & 1 deletion cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

var functionSymbols string
var envVars []string
var commandOutput bool
var commandError bool
var libbpfOutput bool
Expand Down Expand Up @@ -66,7 +67,7 @@ by passing the function name symbol and the binary args.
errorCh := make(chan error)
ctx := context.Background()

ebpf, err := captor.InitProbes(functionSymbol, args, opts)
ebpf, err := captor.InitProbes(functionSymbol, args, envVars, opts)
if err != nil {
return fmt.Errorf("error setting up ebpf module: %w", err)
}
Expand Down Expand Up @@ -100,6 +101,7 @@ func init() {

captureCmd.Flags().StringVarP(&functionSymbols, "functions", "f", "", "Name of the function symbols to be traced")
captureCmd.MarkFlagRequired("functions")
captureCmd.Flags().StringSliceVarP(&envVars, "env-vars", "E", []string{}, "Additional environment variables to pass to the executed command")

captureCmd.Flags().BoolVarP(&commandOutput, "include-cmd-stdout", "c", false, "Include the executed command output")
captureCmd.Flags().BoolVarP(&commandError, "include-cmd-stderr", "e", false, "Include the executed command error")
Expand Down
2 changes: 1 addition & 1 deletion cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var huntCmd = &cobra.Command{
errorCh := make(chan error)
ctx := context.Background()

ebpf, err := captor.InitProbes(functionSymbol, captureArgs, opts)
ebpf, err := captor.InitProbes(functionSymbol, captureArgs, envVars, opts)
if err != nil {
return fmt.Errorf("error setting up ebpf module: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions docs/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ To do so, you have the `--dump-interval/-i` flag available. This flag gives you
harpoon capture -f main.main --dump-interval 2 -- ./binary_name
```

## Tracing a program that requires environment variables to run

`harpoon` provides support to pass environment variables to the executed command.

This is really useful to trace the command changing its behaviour through env vars.

Here's how you can pass multiple env vars to the executed command:

```sh
harpoon capture -f main.main -E "VAR1=value1" -E "VAR2=value2" -- ./binary_name
```

## Tracing from unit-tests

`harpoon` has additional commands other than `capture`. The commands are `analyze` and `hunt`.
Expand Down
5 changes: 4 additions & 1 deletion internal/ebpf/probesfacade/captor/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ type ebpfSetup struct {
lostCh chan uint64
opts CaptureOptions
cmd []string
env []string
}

// InitProbes setup the ebpf module attaching probes and tracepoints
// to the ebpf program.
// Returns the ebpfSetup struct in case of seccess, an error in case of failure.
func InitProbes(functionSymbol string, cmdArgs []string, opts CaptureOptions) (*ebpfSetup, error) {
func InitProbes(functionSymbol string, cmdArgs []string, env []string, opts CaptureOptions) (*ebpfSetup, error) {
if len(cmdArgs) == 0 {
return nil, errors.New("error no arguments provided, at least 1 argument is required")
}
Expand Down Expand Up @@ -138,6 +139,7 @@ func InitProbes(functionSymbol string, cmdArgs []string, opts CaptureOptions) (*
lostCh: lostChannel,
opts: opts,
cmd: cmdArgs,
env: env,
}, nil
}

Expand Down Expand Up @@ -173,6 +175,7 @@ func (ebpf *ebpfSetup) Capture(ctx context.Context, resultCh chan []uint32, erro
// running command to trace its syscalls
go executor.Run(
ebpf.cmd,
ebpf.env,
ebpf.opts.CommandOutput,
ebpf.opts.CommandError,
&wg,
Expand Down
8 changes: 7 additions & 1 deletion internal/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import (

// Run execute the command and wait for its end.
// The cmdOutput argument is used to print the command output.
func Run(cmd []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, errorCh chan<- string) {
func Run(cmd []string, envVars []string, cmdOutput, cmdError bool, wg *sync.WaitGroup, outputCh, errorCh chan<- string) {
defer func() {
wg.Done()
}()

command := exec.Command(cmd[0], cmd[1:]...)
// assign custom env variables to the command
env := os.Environ()
if len(envVars) > 0 {
env = append(env, envVars...)
}
command.Env = env
stdout, _ := command.StdoutPipe()
stderr, _ := command.StderrPipe()

Expand Down
3 changes: 3 additions & 0 deletions tests/integration.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ stdout 'write'
exec harpoon capture -f main.main -c -- ./bin/example-app coin
stdout 'stdout: \[flip coin\]'

exec harpoon capture -f main.main -E "VAR1=0" -E "VAR2=1" -- ./bin/example-app coin
stdout 'write'

exec harpoon capture -f main.main -e -- ./bin/example-app streams
stdout 'stderr: 0'

Expand Down
Loading