Skip to content

Commit

Permalink
fix: trace-ebpf flag output (#632)
Browse files Browse the repository at this point in the history
* fix: trace-ebpf flag output

* chore: using trace Validate method

* chore: Validate erro msg

Co-authored-by: krol3 <krol3@users.noreply.github.com>
  • Loading branch information
krol3 and krol3 committed Apr 9, 2021
1 parent feb1677 commit 2317a86
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tracee-ebpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ DOCKER_BUILDER ?= tracee-builder
# DOCKER_BUILDER_KERN_SRC(/BLD) is where the docker builder looks for kernel headers
DOCKER_BUILDER_KERN_BLD ?= $(if $(shell readlink $(KERN_BLD_PATH)),$(shell readlink $(KERN_BLD_PATH)),$(KERN_BLD_PATH))
DOCKER_BUILDER_KERN_SRC ?= $(if $(shell readlink $(KERN_SRC_PATH)),$(shell readlink $(KERN_SRC_PATH)),$(KERN_SRC_PATH))
# DOCKER_BUILDER_KERN_SRC_MNT is the kernel headers directory to mount into the docker builder container. DOCKER_BUILDER_KERN_SRC should usually be a decendent of this path.
# DOCKER_BUILDER_KERN_SRC_MNT is the kernel headers directory to mount into the docker builder container. DOCKER_BUILDER_KERN_SRC should usually be a descendent of this path.
DOCKER_BUILDER_KERN_SRC_MNT ?= $(dir $(DOCKER_BUILDER_KERN_SRC))

$(OUT_DIR):
Expand Down
28 changes: 21 additions & 7 deletions tracee-ebpf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ Control how and where output is printed.
Possible options:
[format:]{table,table-verbose,json,gob,gotemplate=/path/to/template} output events in the specified format. for gotemplate, specify the mandatory template file
out-file:/path/to/file write the output to a specified file. the path to the file will be created if not existing and the file will be deleted if existing (deafult: stdout)
err-file:/path/to/file write the errors to a specified file. the path to the file will be created if not existing and the file will be deleted if existing (deafult: stderr)
out-file:/path/to/file write the output to a specified file. the path to the file will be created if not existing and the file will be deleted if existing (default: stdout)
err-file:/path/to/file write the errors to a specified file. the path to the file will be created if not existing and the file will be deleted if existing (default: stderr)
option:{stack-addresses,detect-syscall,exec-env} augment output according to given options (default: none)
stack-addresses include stack memory addresses for each event
detect-syscall when tracing kernel functions which are not syscalls, detect and show the original syscall that called that function
Expand All @@ -174,16 +174,27 @@ Use this flag multiple times to choose multiple capture options
outputParts := strings.SplitN(o, ":", 2)
numParts := len(outputParts)
if numParts == 1 {
res.Format = outputParts[0]
err := res.Validate()
if err != nil {
return res, err
}
outputParts = append(outputParts, outputParts[0])
outputParts[0] = "format"
}
if outputParts[0] == "format" {

switch outputParts[0] {
case "format":
res.Format = outputParts[1]
} else if outputParts[0] == "out-file" {
err := res.Validate()
if err != nil {
return res, err
}
case "out-file":
res.OutPath = outputParts[1]
} else if outputParts[0] == "err-file" {
case "err-file":
res.ErrPath = outputParts[1]
} else if outputParts[0] == "option" {
case "option":
switch outputParts[1] {
case "stack-addresses":
res.StackAddresses = true
Expand All @@ -192,10 +203,13 @@ Use this flag multiple times to choose multiple capture options
case "exec-env":
res.ExecEnv = true
default:
return res, fmt.Errorf("invalid output option: %s, use '--option help' for more info", outputParts[1])
return res, fmt.Errorf("invalid output option: %s, use '--output help' for more info", outputParts[1])
}
default:
return res, fmt.Errorf("invalid output value: %s, use '--output help' for more info", outputParts[1])
}
}

if res.Format == "" {
res.Format = "table"
}
Expand Down
8 changes: 4 additions & 4 deletions tracee-ebpf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,19 +1162,19 @@ func TestPrepareOutput(t *testing.T) {
expectedOutput: tracee.OutputConfig{
Format: "foo",
},
expectedError: nil,
expectedError: errors.New("unrecognized output format: foo. Valid format values: 'table', 'table-verbose', 'json', 'gob' or 'gotemplate='. Use '--output help' for more info."),
},
{
testName: "invalid output option",
outputSlice: []string{"option:"},
expectedOutput: tracee.OutputConfig{},
expectedError: errors.New("invalid output option: , use '--option help' for more info"),
expectedError: errors.New("invalid output option: , use '--output help' for more info"),
},
{
testName: "invalid output option 2",
outputSlice: []string{"option:foo"},
expectedOutput: tracee.OutputConfig{},
expectedError: errors.New("invalid output option: foo, use '--option help' for more info"),
expectedError: errors.New("invalid output option: foo, use '--output help' for more info"),
},
{
testName: "format explicit",
Expand Down Expand Up @@ -1215,7 +1215,7 @@ func TestPrepareOutput(t *testing.T) {
expectedOutput: tracee.OutputConfig{
Format: "out-file",
},
expectedError: nil,
expectedError: errors.New("unrecognized output format: out-file. Valid format values: 'table', 'table-verbose', 'json', 'gob' or 'gotemplate='. Use '--output help' for more info."),
},
{
testName: "err",
Expand Down
2 changes: 1 addition & 1 deletion tracee-ebpf/tracee/tracee.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type OutputConfig struct {
// Validate does static validation of the configuration
func (cfg OutputConfig) Validate() error {
if cfg.Format != "table" && cfg.Format != "table-verbose" && cfg.Format != "json" && cfg.Format != "gob" && !strings.HasPrefix(cfg.Format, "gotemplate=") {
return fmt.Errorf("unrecognized output format: %s", cfg.Format)
return fmt.Errorf("unrecognized output format: %s. Valid format values: 'table', 'table-verbose', 'json', 'gob' or 'gotemplate='. Use '--output help' for more info.", cfg.Format)
}
return nil
}
Expand Down

0 comments on commit 2317a86

Please sign in to comment.