| 3906 | |
| 3907 | == /proc fs with seq_file == |
| 3908 | * [http://wiki.bit-hive.com/tomizoo/pg/proc%20fs%20%A4%F2%BB%C8%A4%C3%A4%BF%A5%AB%A1%BC%A5%CD%A5%EB%BE%F0%CA%F3%A4%CE%C9%BD%BC%A8%20%A4%BD%A4%CE2 proc fs を使ったカーネル情報の表示 その2 - とみぞーノート] |
| 3909 | * [http://pylone.jp/blog/tips_seq_file seq_fileの使い方 - Pylone Blog] |
| 3910 | |
| 3911 | * 参考 |
| 3912 | * fs/proc/loadavg.c |
| 3913 | * single_open |
| 3914 | * fs/proc/devices.c |
| 3915 | * seq_open |
| 3916 | |
| 3917 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko entry=1000 |
| 3918 | * s0711489@ubuntu-lucid64:~$ sudo mknod /dev/stack c 251 0 |
| 3919 | * s0711489@ubuntu-lucid64:~$ sudo chmod 666 /dev/stack |
| 3920 | * s0711489@ubuntu-lucid64:~$ dd if=/dev/zero of=/dev/stack |
| 3921 | {{{ |
| 3922 | dd: writing to `/dev/stack': No space left on device |
| 3923 | 1001+0 records in |
| 3924 | 1000+0 records out |
| 3925 | 512000 bytes (512 kB) copied, 0.0338233 s, 15.1 MB/s |
| 3926 | }}} |
| 3927 | |
| 3928 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 3929 | {{{ |
| 3930 | stack: 1000 |
| 3931 | stack.data[0].length: 512 |
| 3932 | stack.data[1].length: 512 |
| 3933 | |
| 3934 | (snip) |
| 3935 | |
| 3936 | stack.data[997].length: 512 |
| 3937 | stack.data[998].length: 512 |
| 3938 | stack.data[999].length: 512 |
| 3939 | }}} |
| 3940 | |
| 3941 | * s0711489@ubuntu-lucid64:~$ tailf /var/log/kern.log |
| 3942 | {{{ |
| 3943 | Nov 24 03:26:17 ubuntu-lucid64 kernel: [ 4468.813821] /proc/stackmod is opened |
| 3944 | }}} |
| 3945 | |
| 3946 | === no stack, no /proc output === |
| 3947 | * s0711489@ubuntu-lucid64:~$ echo hoge > /dev/stack |
| 3948 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 3949 | {{{ |
| 3950 | stack: 1 |
| 3951 | stack.data[0].length: 5 |
| 3952 | }}} |
| 3953 | * s0711489@ubuntu-lucid64:~$ cat /dev/stack |
| 3954 | {{{ |
| 3955 | hoge |
| 3956 | }}} |
| 3957 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 3958 | {{{ |
| 3959 | }}} |
| 3960 | |
| 3961 | === debug with gdb === |
| 3962 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.text |
| 3963 | {{{ |
| 3964 | 0xffffffffa0056000 |
| 3965 | }}} |
| 3966 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.data |
| 3967 | {{{ |
| 3968 | 0xffffffffa00568e8 |
| 3969 | }}} |
| 3970 | * s0711489@ubuntu-lucid64:~$ cat /sys/module/stackmod/sections/.bss |
| 3971 | {{{ |
| 3972 | 0xffffffffa0056b30 |
| 3973 | }}} |
| 3974 | |
| 3975 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 3976 | {{{ |
| 3977 | }}} |
| 3978 | {{{ |
| 3979 | (gdb) file vmlinux |
| 3980 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/vmlinux...(no debugging symbols found)...done. |
| 3981 | (gdb) add-symbol-file ../../04/stackmod/stackmod.o 0xffffffffa0056000 -s .data 0xffffffffa00568e8 -s .bss 0xffffffffa0056b30 |
| 3982 | add symbol table from file "../../04/stackmod/stackmod.o" at |
| 3983 | .text_addr = 0xffffffffa0056000 |
| 3984 | .data_addr = 0xffffffffa00568e8 |
| 3985 | .bss_addr = 0xffffffffa0056b30 |
| 3986 | (y or n) y |
| 3987 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.o...done. |
| 3988 | (gdb) target remote localhost:8864 |
| 3989 | Remote debugging using localhost:8864 |
| 3990 | 0xffffffff810097a9 in native_safe_halt () |
| 3991 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/x86_64/arch/x86/include/asm/irqflags.h:49 |
| 3992 | 49 asm volatile("sti; hlt": : :"memory"); |
| 3993 | (gdb) b proc_seq_start |
| 3994 | Breakpoint 1 at 0xffffffffa00560cb: file /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.c, line 153. |
| 3995 | (gdb) c |
| 3996 | Continuing. |
| 3997 | |
| 3998 | Breakpoint 1, proc_seq_start (m=0xffff880017e3a180, pos=0xffff88001b737e98) |
| 3999 | at /home/ugrad/07/s0711489/coursework/KernelHack/04/stackmod/stackmod.c:153 |
| 4000 | 153 if (*pos == 0) { |
| 4001 | (gdb) p *pos |
| 4002 | $1 = 0 |
| 4003 | (gdb) n |
| 4004 | 154 seq_printf(m, "stack: %d\n", stack.depth); |
| 4005 | (gdb) |
| 4006 | 157 if (*pos < stack.depth) { |
| 4007 | (gdb) |
| 4008 | 162 } |
| 4009 | (gdb) |
| 4010 | 157 if (*pos < stack.depth) { |
| 4011 | (gdb) |
| 4012 | 162 } |
| 4013 | (gdb) |
| 4014 | seq_read (file=0xffff88001b66c240, |
| 4015 | buf=0xc8a000 <Address 0xc8a000 out of bounds>, size=32768, |
| 4016 | ppos=0xffff88001b737f48) at fs/seq_file.c:197 |
| 4017 | 197 if (!p || IS_ERR(p)) |
| 4018 | (gdb) bt |
| 4019 | #0 seq_read (file=0xffff88001b66c240, |
| 4020 | buf=0xc8a000 <Address 0xc8a000 out of bounds>, size=32768, |
| 4021 | ppos=0xffff88001b737f48) at fs/seq_file.c:197 |
| 4022 | #1 0xffffffff81124b07 in proc_reg_read (file=0xffff88001b66c240, |
| 4023 | buf=0xc8a000 <Address 0xc8a000 out of bounds>, count=32768, |
| 4024 | ppos=0xffff88001b737f48) at fs/proc/inode.c:163 |
| 4025 | #2 0xffffffff810df784 in vfs_read (file=0xffff88001b66c240, |
| 4026 | buf=0xc8a000 <Address 0xc8a000 out of bounds>, count=0, |
| 4027 | pos=0xffff88001b737f48) at fs/read_write.c:310 |
| 4028 | #3 0xffffffff810dfa2b in sys_read (fd=<value optimized out>, |
| 4029 | buf=0xc8a000 <Address 0xc8a000 out of bounds>, count=32768) |
| 4030 | at fs/read_write.c:400 |
| 4031 | #4 0xffffffff810029eb in ?? () |
| 4032 | #5 0x0000000000000246 in ?? () |
| 4033 | #6 0x00007fffced80000 in ?? () |
| 4034 | #7 0x0000000000000000 in ?? () |
| 4035 | (gdb) c |
| 4036 | Continuing. |
| 4037 | }}} |
| 4038 | |
| 4039 | === fix it to read /proc correctly === |
| 4040 | * s0711489@ubuntu-lucid64:~$ sudo insmod stackmod.ko |
| 4041 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 4042 | {{{ |
| 4043 | stack: 0 |
| 4044 | }}} |
| 4045 | * s0711489@ubuntu-lucid64:~$ sudo mknod /dev/stack c 251 0 |
| 4046 | * s0711489@ubuntu-lucid64:~$ sudo chmod 666 /dev/stack |
| 4047 | * s0711489@ubuntu-lucid64:~$ echo hoge > /dev/stack |
| 4048 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 4049 | {{{ |
| 4050 | stack: 1 |
| 4051 | stack.data[0].length: 5 |
| 4052 | }}} |
| 4053 | * s0711489@ubuntu-lucid64:~$ cat /dev/stack |
| 4054 | {{{ |
| 4055 | hoge |
| 4056 | }}} |
| 4057 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 4058 | {{{ |
| 4059 | stack: 0 |
| 4060 | }}} |
| 4061 | |
| 4062 | * s0711489@ubuntu-lucid64:~$ dd if=/dev/zero of=/dev/stack bs=10 |
| 4063 | {{{ |
| 4064 | dd: writing `/dev/stack': No space left on device |
| 4065 | 129+0 records in |
| 4066 | 128+0 records out |
| 4067 | 1280 bytes (1.3 kB) copied, 0.00669531 s, 191 kB/s |
| 4068 | }}} |
| 4069 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 4070 | {{{ |
| 4071 | stack: 128 |
| 4072 | stack.data[0].length: 10 |
| 4073 | stack.data[1].length: 10 |
| 4074 | |
| 4075 | (snip) |
| 4076 | |
| 4077 | stack.data[125].length: 10 |
| 4078 | stack.data[126].length: 10 |
| 4079 | stack.data[127].length: 10 |
| 4080 | }}} |
| 4081 | * s0711489@ubuntu-lucid64:~$ cat /dev/stack |
| 4082 | * s0711489@ubuntu-lucid64:~$ cat /proc/stackmod |
| 4083 | {{{ |
| 4084 | stack: 0 |
| 4085 | }}} |