Skip to content

Commit

Permalink
Merge pull request #2 from danteslimbo/dev
Browse files Browse the repository at this point in the history
Add ofile option.
  • Loading branch information
ZhangYet authored Jul 4, 2024
2 parents 6fbf590 + 7b1460a commit fde6c44
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Usage: ./dust [options]
Available options:
-h, --help show help
-i, --interval uint32 set monitor time in seconds (default 10)
-o, --ofile string output file
-p, --pid uint32 filter pid
-v, --version show version
```
Expand All @@ -24,4 +25,4 @@ sudo ./dust -p {the_process_you_want_to_trace} -i 30
## TODO
- [ ] kprobe filters.
- [ ] kprobe.multi supports.
- [ ] output to files.
- [x] output to files.
12 changes: 9 additions & 3 deletions internal/dust/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type output struct {
kprobeMulti bool // TODO
kfreeReasons map[uint64]string
ifaceCache map[uint64]map[uint32]string
ofile string
}

// outputStructured is a struct to hold the data for the json output
Expand Down Expand Up @@ -48,8 +49,15 @@ type jsonTuple struct {
Proto uint8 `json:"proto,omitempty"`
}

func NewOutput(addr2Name Addr2Name, kprobeMulti bool) (*output, error) {
func NewOutput(addr2Name Addr2Name, kprobeMulti bool, ofile string) (*output, error) {
writer := os.Stdout
if ofile != "" {
var err error
writer, err = os.OpenFile(ofile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
}

var ifs map[uint64]map[uint32]string

Expand All @@ -70,8 +78,6 @@ func (o *output) Close() {
}

func (o *output) PrintHeader() {
// TODO

}

// PrintJson prints the event in JSON format
Expand Down
2 changes: 2 additions & 0 deletions internal/dust/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Flags struct {

Pid uint32
Interval uint32
Ofile string
}

func (f *Flags) SetFlags() {
flag.BoolVarP(&f.ShowVersion, "version", "v", false, "show version")
flag.BoolVarP(&f.ShowHelp, "help", "h", false, "show help")
flag.Uint32VarP(&f.Interval, "interval", "i", 10, "set monitor time in seconds")
flag.Uint32VarP(&f.Pid, "pid", "p", 0, "filter pid")
flag.StringVarP(&f.Ofile, "ofile", "o", "", "output file")

flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [options] \n", os.Args[0])
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func main() {

log.Println("Listening for events..")

output, err := dust.NewOutput(addr2name, false)
output, err := dust.NewOutput(addr2name, false, flags.Ofile)
if err != nil {
log.Fatalf("Failed to create output: %s", err)
}
Expand Down

0 comments on commit fde6c44

Please sign in to comment.