Abstract: Fan
Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
1. The experimental environment is a lab building virtual machine environment using the course configuration, open the command line client, CD Linuxkernel directory, use the command rm-rf menu to delete the original code, use Git clone https://github.com/mengning/ Menu.git Gets the latest menu code, then the CD menu enters the menu subfolder, uses VI test.c to open the file, copies the code from last week's experiment into TEST.C, constructs two functions, and becomes the menu's two menu items.
cd LinuxKernelrm menu -rfgit clone https://github.com/mengning/menu.gitcd menumake rootfs
Running results See
2, add 2 function code VI test.c in TEST.C
int menufork (int argc, char *argv[]) {pid_t fpid; int count = 0; Fpid = fork (); if (Fpid < 0) printf ("Error in fork!"); else if (Fpid = = 0) {printf ("I am the child process, my process ID is%d\n", getpid ()); count++; } else {printf ("I am the parent process, my process ID is%d\n", getpid ()); count++; } printf ("Count:%d\n", count); return 0;} int menuforkasm (int argc, char *argv[]) {pid_t fpid; int count = 0; ASM volatile ("mov $,%%ebx\n\t" "Mov $0x2,%%eax\n\t" "int $0x80\n\t" "mov %%eax,%0\n\t ":" =m "(Fpid)); if (Fpid < 0) printf ("Error in fork!"); else if (Fpid = = 0) {printf ("I am the child process, my process ID is%d\n", getpid ()); count++; } else {printf ("I am the parent process, my process ID is%d\n", getpid ()); count++; } printf ("Count:%d\n", count); return 0;}
Add two lines of command to the main () function
Enter two functions (wrong several times)
3. Run 2 new functions menufork, menufork-asm
After the function is compiled, it means that there are two processes, parent and child processes, and two processes return different results, the function value returned by the child process is 0, the parent process returns the ID of the child process, so you can determine whether it is a child process or a parent process by using a function value.
4. Debugging code with GDB
gdbfile linux-3.18.6/vmlinux 加载调试用的符号表target remote:1234b start_kernel 设置断点cb sys_fork
Make a breakpoint on the sys_getpid.
Finally unable to continue tracking debug code, System_call () is not a normal function, GDB cannot stop here.
5. Brief summary
This experiment is relatively easy to understand, but the operation is a bit cumbersome, in the input code also appeared several times the error caused the time greatly increased, fortunately completed. Reviewing the whole experiment, the system protects the interrupt address and environment while entering the kernel state, then calls the system function, associates each system call with the related function through the interrupt vector table, executes the system function, resumes the interrupt environment, and proceeds to execute the program.
Linux kernel Analysis section fifth, three layers of the system call (bottom)