テストプログラムの作成
% cat skv.c
#include <sys/sysctl.h>
#include <stdio.h>
int main()
{
int mib[2];
size_t len;
char buf[1024];
mib[0] = CTL_KERN;
mib[1] = KERN_VERSION;
len = sizeof(buf);
sysctl(mib, 2, &buf, &len, NULL, 0);
printf("CTL_KERN/KERN_VERSION=\"%s\"\n", buf);
}
% cc skv.c
% ./a.out
CTL_KERN/KERN_VERSION="FreeBSD 15.0-CURRENT #0 n265729-9b03a5de73d4-dirty: Tue Oct 10 18:39:54 JST 2023
root@msrvkx:/usr/obj/usr/src/amd64.amd64/sys/XIJ
"
%
デバッグ仕様でコンパイル
% cc -g -O0 skv.c
gdbでトレース
% gdb ./a.out
GNU gdb (GDB) 13.2 [GDB v13.2 for FreeBSD]
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-portbld-freebsd15.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
(gdb) break main
Breakpoint 1 at 0x2016db: file skv.c, line 10.
(gdb) step
The program is not being run.
(gdb) run
Starting program: /home/kiri/projects/kbug/bof/2023/No.5/list/a.out
Breakpoint 1, main () at skv.c:10
10 mib[0] = CTL_KERN;
(gdb) step
11 mib[1] = KERN_VERSION;
(gdb) step
12 len = sizeof(buf);
(gdb) step
13 sysctl(mib, 2, &buf, &len, NULL, 0);
(gdb) step
sysctl (name=0x7fffffffe728, namelen=2, oldp=0x7fffffffe320, oldlenp=0x7fffffffe720, newp=0x0,
newlen=0) at /usr/src/lib/libc/gen/sysctl.c:73
warning: Source file is more recent than executable.
73 orig_oldlen = oldlenp != NULL ? *oldlenp : 0;
(gdb) step
74 retval = __sysctl(name, namelen, oldp, oldlenp, newp, newlen);
(gdb) step
__sysctl () at __sysctl.S:4
warning: Source file is more recent than executable.
4 RSYSCALL(__sysctl)
(gdb) step
sysctl (name=0x7fffffffe728, namelen=2, oldp=0x7fffffffe320, oldlenp=0x7fffffffe720, newp=0x0,
newlen=<optimized out>) at /usr/src/lib/libc/gen/sysctl.c:83
83 if (retval != 0 || name[0] != CTL_USER)
(gdb) step
215 }
(gdb) step
main () at skv.c:14
14 printf("CTL_KERN/KERN_VERSION=\"%s\"\n", buf);
(gdb) print buf
$1 = "FreeBSD 15.0-CURRENT #0 n265729-9b03a5de73d4-dirty: Tue Oct 10 18:39:54 JST 2023\n root@msrvkx:/usr/obj/usr/src/amd64.amd64/sys/XIJ\n\000\0008\262\"\000\b\000\000\000\001\000\000\000\377\177\000\000\bh\"\000\b\000\000\000\260\351$\000\b\000\000\000\370\345\377\377\377\177\000\000\030\344\377\377\000\000\000\000@\345\377\377\377\177\000\000\220\344\377\377\377\177\000\000"...
(gdb)
/usr/src/lib/libc/amd64/SYS.hで定義
#define RSYSCALL(name) ENTRY(__sys_##name); \
WEAK_REFERENCE(__sys_##name, name); \
WEAK_REFERENCE(__sys_##name, _##name); \
mov $SYS_##name,%eax; KERNCALL; \
jb HIDENAME(cerror); ret; \
END(__sys_##name)