温馨提示×

centos readdir怎么调试

小樊
45
2026-09-15 06:53:02
栏目: 智能运维

CentOS 上调试 readdir(无论是 glibc 的 readdir(),还是内核里的 readdir 系统调用),常见方法如下,按“用户态 → 内核态”循序渐进。


一、用户态调试(最常用)

1. 写最小复现程序

先确认问题是否可复现:

#define _GNU_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[]) {
    DIR *dir = opendir(argv[1]);
    if (!dir) {
        perror("opendir");
        return 1;
    }

    struct dirent *de;
    while ((de = readdir(dir)) != NULL) {
        printf("inode=%ld name=%s type=%d\n",
               (long)de->d_ino, de->d_name, de->d_type);
    }

    if (errno) {
        perror("readdir");
    }

    closedir(dir);
    return 0;
}

编译:

gcc readdir_test.c -o readdir_test

运行:

./readdir_test /some/dir

2. 用 strace 看系统调用(强烈推荐)

strace -f -e trace=file ./readdir_test /path

或只看 readdir 相关:

strace -e trace=open,openat,getdents,getdents64,readdir ./readdir_test /path

重点看:

  • openat()
  • getdents64()
  • 返回值 / errno

示例输出:

openat(AT_FDCWD, "/path", O_RDONLY|O_NONBLOCK|O_DIRECTORY) = 3
getdents64(3, /* 10 entries */, 32768) = 1234

3. 用 gdb 单步调试

gdb ./readdir_test
(gdb) break readdir
(gdb) run /path

如果 readdir 是 glibc 函数,可以:

catch syscall getdents64

4. 打开 glibc 调试信息

安装调试符号:

debuginfo-install glibc

然后:

gdb ./readdir_test
(gdb) step

可进入 glibc 的 readdir 实现。


二、文件系统层问题排查

5. 检查目录本身是否正常

ls -la /path
stat /path

6. 挂载参数问题

某些挂载选项会影响 readdir

mount | grep /path

常见问题:

  • NFS + actimeo
  • overlayfs
  • fuse

三、内核态调试(高级)

7. 查看内核里的 readdir

相关系统调用:

  • getdents
  • getdents64

函数路径(示例):

fs/readdir.c
  -> vfs_readdir()
    -> f_op->iterate / iterate_shared

8. 用 ftrace 跟踪内核函数

# 查看是否支持
cat /sys/kernel/debug/tracing/available_filter_functions | grep readdir

示例:

cd /sys/kernel/debug/tracing
echo function > current_tracer
echo vfs_readdir > set_ftrace_filter
echo 1 > tracing_on
# 执行程序
echo 0 > tracing_on
cat trace

9. 用 perf 粗粒度分析

perf trace -e getdents64 ./readdir_test /path

四、常见坑位小结

现象 可能原因
readdir 返回 NULL 但 errno=0 目录读完(正常)
ENOTDIR 路径不是目录
EOVERFLOW inode 太大(用 getdents64)
NFS 卡住 网络 / 挂载参数
顺序异常 文件系统不保证顺序
overlayfs 丢文件 lower/upper 合并问题

五、我可以进一步帮你

如果你愿意,可以告诉我:

  1. CentOS 版本(cat /etc/redhat-release
  2. 文件系统类型(mount
  3. 具体报错或异常行为
  4. 用户程序 readdir 还是 内核模块 / 文件系统

我可以直接给你针对性的调试命令或代码

0