~/syscall# gdb ./a.out GNU gdb (Ubuntu 8.1-0ubuntu3.1) 8.1.0.20180409-git ...... Reading symbols from ./a.out...done. (gdb) l 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <linux/kernel.h> 5 #include <sys/syscall.h> 6 #include <string.h> 7 8 int main () 9 { 10 char * words = "I am liuchao from user mode."; (gdb) b 10 Breakpoint 1 at 0x6e2: file syscall.c, line 10. (gdb) r Starting program: /root/syscall/a.out
Breakpoint 1, main () at syscall.c:10 10 char * words = "I am liuchao from user mode."; (gdb) n 12 ret = syscall(333, words, strlen(words)+1); (gdb) p words $1 = 0x5555555547c4 "I am liuchao from user mode." (gdb) s __strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:79 (gdb) bt #0 __strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:79 #1 0x00005555555546f9 in main () at syscall.c:12 (gdb) c Continuing. return 63 from kernel mode. [Inferior 1 (process 1774) exited normally] (gdb) q
/mnt/vdc/linux-source-4.15.0# gdb vmlinux GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 ...... To enable execution of this file add add-auto-load-safe-path /mnt/vdc/linux-source-4.15.0/vmlinux-gdb.py ...... (gdb) b sys_sayhelloworld Breakpoint 1 at 0xffffffff8109e2f0: file kernel/sys.c, line 192. (gdb) target remote :1234 Remote debugging using :1234 native_safe_halt () at ./arch/x86/include/asm/irqflags.h:61 61 } (gdb) c Continuing. [Switching to Thread 2] Thread 2 hit Breakpoint 1, sys_sayhelloworld (words=0x563cbfa907c4 "I am liuchao from user mode.", count=29) at kernel/sys.c:192 192 { (gdb) bt #0 sys_sayhelloworld (words=0x55b2811537c4 "I am liuchao from user mode.", count=29) at kernel/sys.c:192 #1 0xffffffff810039f7 in do_syscall_64 (regs=0xffffc9000133bf58) at arch/x86/entry/common.c:290 #2 0xffffffff81a00081 in entry_SYSCALL_64 () at arch/x86/entry/entry_64.S:237 (gdb) n 195 if(count >= 1024){ (gdb) n 198 copy_from_user(buffer, words, count); (gdb) n 199 ret=printk("User Mode says %s to the Kernel Mode!", buffer); (gdb) p buffer $1 = "I am liuchao from user mode.\000\177\000\000\... (gdb) n 200 return ret; (gdb) p ret $2 = 63 (gdb) c (gdb) n do_syscall_64 (regs=0xffffc9000133bf58) at arch/x86/entry/common.c:295 295 syscall_return_slowpath(regs); (gdb) s syscall_return_slowpath (regs=<optimized out>) at arch/x86/entry/common.c:295 (gdb) n 268 prepare_exit_to_usermode(regs); (gdb) n do_syscall_64 (regs=0xffffc9000133bf58) at arch/x86/entry/common.c:296 296 } (gdb) n entry_SYSCALL_64 () at arch/x86/entry/entry_64.S:246 246 movq RCX(%rsp), %rcx ...... (gdb) n entry_SYSCALL_64 () at arch/x86/entry/entry_64.S:330 330 USERGS_SYSRET64