diff options
author | We-unite <3205135446@qq.com> | 2024-09-02 16:45:07 +0800 |
---|---|---|
committer | We-unite <3205135446@qq.com> | 2024-09-02 16:45:07 +0800 |
commit | 08207d77be79afc6f75d1611726b92bdf622717f (patch) | |
tree | 918991217807ff18025b998407b87bcd31d4ddc3 /filter/files.go | |
parent | f9f8f35ccd8b505a827d40f95c52ed039512b79d (diff) | |
download | godo-master.tar.gz godo-master.zip |
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.
Diffstat (limited to 'filter/files.go')
-rw-r--r-- | filter/files.go | 102 |
1 files changed, 102 insertions, 0 deletions
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 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "path" | ||
6 | "sort" | ||
7 | "syscall" | ||
8 | ) | ||
9 | |||
10 | type FileFlag struct { | ||
11 | Value uint64 | ||
12 | Name string | ||
13 | } | ||
14 | |||
15 | var FileFlags = []FileFlag{ | ||
16 | {Value: syscall.O_APPEND, Name: "O_APPEND"}, | ||
17 | {Value: syscall.O_ASYNC, Name: "O_ASYNC"}, | ||
18 | {Value: syscall.O_CLOEXEC, Name: "O_CLOEXEC"}, | ||
19 | {Value: syscall.O_CREAT, Name: "O_CREAT"}, | ||
20 | {Value: syscall.O_DIRECT, Name: "O_DIRECT"}, | ||
21 | {Value: syscall.O_DIRECTORY, Name: "O_DIRECTORY"}, | ||
22 | {Value: syscall.O_DSYNC, Name: "O_DSYNC"}, | ||
23 | {Value: syscall.O_EXCL, Name: "O_EXCL"}, | ||
24 | {Value: syscall.O_FSYNC, Name: "O_FSYNC"}, | ||
25 | {Value: syscall.O_NDELAY, Name: "O_NDELAY"}, | ||
26 | {Value: syscall.O_NOATIME, Name: "O_NOATIME"}, | ||
27 | {Value: syscall.O_NOCTTY, Name: "O_NOCTTY"}, | ||
28 | {Value: syscall.O_NOFOLLOW, Name: "O_NOFOLLOW"}, | ||
29 | {Value: syscall.O_NONBLOCK, Name: "O_NONBLOCK"}, | ||
30 | {Value: syscall.O_RDONLY, Name: "O_RDONLY"}, | ||
31 | {Value: syscall.O_RDWR, Name: "O_RDWR"}, | ||
32 | {Value: syscall.O_RSYNC, Name: "O_RSYNC"}, | ||
33 | {Value: syscall.O_SYNC, Name: "O_SYNC"}, | ||
34 | {Value: syscall.O_TRUNC, Name: "O_TRUNC"}, | ||
35 | {Value: syscall.O_WRONLY, Name: "O_WRONLY"}, | ||
36 | } | ||
37 | |||
38 | func filtFiles(pRawFileData *[]File) { | ||
39 | rawFileData := *pRawFileData | ||
40 | files = make([]File, 0) | ||
41 | |||
42 | // 所有文件按照特定顺序排 | ||
43 | sort.Slice(rawFileData, func(i, j int) bool { | ||
44 | pi := &rawFileData[i] | ||
45 | pj := &rawFileData[j] | ||
46 | |||
47 | if pi.FileName < pj.FileName { | ||
48 | return true | ||
49 | } else if pi.FileName > pj.FileName { | ||
50 | return false | ||
51 | } | ||
52 | if pi.Pid < pj.Pid { | ||
53 | return true | ||
54 | } else if pi.Pid > pj.Pid { | ||
55 | return false | ||
56 | } | ||
57 | if pi.Fd < pj.Fd { | ||
58 | return true | ||
59 | } else if pi.Fd > pj.Fd { | ||
60 | return false | ||
61 | } | ||
62 | if pi.OpenTimestamp.Before(pj.OpenTimestamp) { | ||
63 | return true | ||
64 | } else { | ||
65 | return false | ||
66 | } | ||
67 | }) | ||
68 | |||
69 | for _, file := range rawFileData { | ||
70 | tgid := findTgid[file.Pid] | ||
71 | pTgidNode, exists := helloTree[tgid] | ||
72 | if !exists { | ||
73 | continue | ||
74 | } | ||
75 | if file.CloseTimestamp.IsZero() { | ||
76 | index, exists := pTgidNode.FindPid[file.Pid] | ||
77 | if !exists || index < 0 || index >= len(pTgidNode.Threads) { | ||
78 | continue | ||
79 | } | ||
80 | file.CloseTimestamp = pTgidNode.Threads[index].ExitTimestamp | ||
81 | } | ||
82 | file.FileName = path.Clean(file.FileName) | ||
83 | files = append(files, file) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | // 解析 Flags[1] 的值为描述性字符串 | ||
88 | func parseFlags(flag uint64) string { | ||
89 | var result string | ||
90 | for _, fileFlag := range FileFlags { | ||
91 | if flag&fileFlag.Value == fileFlag.Value { | ||
92 | if result != "" { | ||
93 | result += " | " | ||
94 | } | ||
95 | result += fileFlag.Name | ||
96 | } | ||
97 | } | ||
98 | if result == "" { | ||
99 | return fmt.Sprintf("0x%x", flag) // 返回原始十六进制值 | ||
100 | } | ||
101 | return result | ||
102 | } | ||