From 08207d77be79afc6f75d1611726b92bdf622717f Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Mon, 2 Sep 2024 16:45:07 +0800 Subject: Show filt result in tree&json, fix sth in listener In the listener, I change the order coroutines are started to avoid 'send on a closed channel'. Besides, the method to get syscall names and numbers are not so universial, so let's go back to check unistd.h. In the filter, the output is set to be written to ./log dir. Pid tree are shown in logs/tree.log, and detail info in pids.log, while file info in the logs/files.log. tree.log shows a tree just like `tree` command, the other two files are written in json. What's more, the flags while opening files are also checked ans showed in files.log. --- filter/files.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 filter/files.go (limited to 'filter/files.go') diff --git a/filter/files.go b/filter/files.go new file mode 100644 index 0000000..e8c0cd3 --- /dev/null +++ b/filter/files.go @@ -0,0 +1,102 @@ +package main + +import ( + "fmt" + "path" + "sort" + "syscall" +) + +type FileFlag struct { + Value uint64 + Name string +} + +var FileFlags = []FileFlag{ + {Value: syscall.O_APPEND, Name: "O_APPEND"}, + {Value: syscall.O_ASYNC, Name: "O_ASYNC"}, + {Value: syscall.O_CLOEXEC, Name: "O_CLOEXEC"}, + {Value: syscall.O_CREAT, Name: "O_CREAT"}, + {Value: syscall.O_DIRECT, Name: "O_DIRECT"}, + {Value: syscall.O_DIRECTORY, Name: "O_DIRECTORY"}, + {Value: syscall.O_DSYNC, Name: "O_DSYNC"}, + {Value: syscall.O_EXCL, Name: "O_EXCL"}, + {Value: syscall.O_FSYNC, Name: "O_FSYNC"}, + {Value: syscall.O_NDELAY, Name: "O_NDELAY"}, + {Value: syscall.O_NOATIME, Name: "O_NOATIME"}, + {Value: syscall.O_NOCTTY, Name: "O_NOCTTY"}, + {Value: syscall.O_NOFOLLOW, Name: "O_NOFOLLOW"}, + {Value: syscall.O_NONBLOCK, Name: "O_NONBLOCK"}, + {Value: syscall.O_RDONLY, Name: "O_RDONLY"}, + {Value: syscall.O_RDWR, Name: "O_RDWR"}, + {Value: syscall.O_RSYNC, Name: "O_RSYNC"}, + {Value: syscall.O_SYNC, Name: "O_SYNC"}, + {Value: syscall.O_TRUNC, Name: "O_TRUNC"}, + {Value: syscall.O_WRONLY, Name: "O_WRONLY"}, +} + +func filtFiles(pRawFileData *[]File) { + rawFileData := *pRawFileData + files = make([]File, 0) + + // 所有文件按照特定顺序排 + sort.Slice(rawFileData, func(i, j int) bool { + pi := &rawFileData[i] + pj := &rawFileData[j] + + if pi.FileName < pj.FileName { + return true + } else if pi.FileName > pj.FileName { + return false + } + if pi.Pid < pj.Pid { + return true + } else if pi.Pid > pj.Pid { + return false + } + if pi.Fd < pj.Fd { + return true + } else if pi.Fd > pj.Fd { + return false + } + if pi.OpenTimestamp.Before(pj.OpenTimestamp) { + return true + } else { + return false + } + }) + + for _, file := range rawFileData { + tgid := findTgid[file.Pid] + pTgidNode, exists := helloTree[tgid] + if !exists { + continue + } + if file.CloseTimestamp.IsZero() { + index, exists := pTgidNode.FindPid[file.Pid] + if !exists || index < 0 || index >= len(pTgidNode.Threads) { + continue + } + file.CloseTimestamp = pTgidNode.Threads[index].ExitTimestamp + } + file.FileName = path.Clean(file.FileName) + files = append(files, file) + } +} + +// 解析 Flags[1] 的值为描述性字符串 +func parseFlags(flag uint64) string { + var result string + for _, fileFlag := range FileFlags { + if flag&fileFlag.Value == fileFlag.Value { + if result != "" { + result += " | " + } + result += fileFlag.Name + } + } + if result == "" { + return fmt.Sprintf("0x%x", flag) // 返回原始十六进制值 + } + return result +} -- cgit v1.2.3-70-g09d2