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 }