背景

对linux文件系统的一些学习

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  cat print_log.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    char log_name[] = "test.log";
    while (1) {
        FILE *fp = fopen(log_name, "a+");
        if (!fp) {
            perror("open file failed ");
            abort();
        }

        fprintf(fp, "hello systemtap\n");
        //fflush(fp);
        fclose(fp);

        sleep(5);
    }
    return 0;
}

  cat makefile
CC = gcc
CFLAGS = -g -O0 -Wall
TARGET = print_log
all : $(TARGET)
$(TARGET) : print_log.c
    $(CC) $(CFLAGS) print_log.c -o $(TARGET)

clean:
    rm -f $(TARGET)


  cat test.stp
#!/usr/bin/stap

probe vfs.write {
    if (pid() == target()) {
        if (@defined($file->f_path->dentry)) {
            dev_nr=$file->f_path->dentry->d_inode->i_sb->s_dev
            inode_nr = $file->f_path->dentry->d_inode->i_ino
        } else {
            dev_nr=$file->f_dentry->d_inode->i_sb->s_dev
            inode_nr = $file->f_dentry->d_inode->i_ino
        }

        dentry = @cast(file, "file")->f_path->dentry;
        printf("file_name: %s\n", d_name(dentry))
        printf ("%s(%d) %s 0x%x/%u\n",execname(), pid(), ppfunc(), dev_nr, inode_nr)
        printf("--------- user space trace ---------\n")
        print_ubacktrace()
        printf("-------- kernel space trace --------\n")
        print_backtrace()
        printf("------------------------------------\n")
    }
}


# 首先运行print_log
# sudo stap -d /usr/lib64/libc-2.17.so -d print_log -x pid test.stp
```c++
file_name: test.log
print_log(2724) vfs_write 0xfd00002/5110626
--------- user space trace ---------
 0x7f739d56b9b0 : __write_nocancel+0x7/0x57 [/usr/lib64/libc-2.17.so]
 0x7f739d4f6213 : _IO_file_write@@GLIBC_2.2.5+0x43/0xa0 [/usr/lib64/libc-2.17.so]
 0x7f739d4f7a2e : _IO_do_write@@GLIBC_2.2.5+0xbe/0x1b0 [/usr/lib64/libc-2.17.so]
 0x7f739d4f71d8 : _IO_file_close_it@@GLIBC_2.2.5+0x188/0x1a0 [/usr/lib64/libc-2.17.so]
 0x7f739d4ea1a8 : _IO_fclose@@GLIBC_2.2.5+0x1b8/0x242 [/usr/lib64/libc-2.17.so]
 0x4006f9 : main+0x6c/0x83 [/home/parallels/study/systemtap/test_src/print_log]
 0x7f739d49e505 : __libc_start_main+0xf5/0x1c0 [/usr/lib64/libc-2.17.so]
 0x4005c9 : _start+0x29/0x30 [/home/parallels/study/systemtap/test_src/print_log]
-------- kernel space trace --------
 0xffffffffaf81f0c0 : vfs_write+0x0/0x1f0 [kernel]
 0xffffffffaf81ffaf : sys_write+0x7f/0xf0 [kernel]
 0xffffffffafd25a1b : tracesys+0xa3/0xc9 [kernel]
 0x7f739d56b9b0

vfs.write其实是vfs_write的缩写

➜ sudo stap -L ‘kernel.function(“sys_write”)’ kernel.function(“SyS_write@fs/read_write.c:584”) $fd:long int $buf:long int $count:long int $ret:long int

源码路径/usr/src/debug/kernel-3.10.0-862.11.6.el7/linux-3.10.0-862.11.6.el7.x86_64/fs/read_write.c ```

file system