Linux内核及分析 第四周 扒开系统调用的三层皮(上)

 

实验过程

  1. 选择20号系统调用getpid(取得进程识别码)
  2. 在网上查询getpid函数的C语言代码以及其嵌入式汇编语句

    C语言代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    int main(int argc, const char *argv[])
    {
        pid_t tt;
        tt = getpid();
        printf("%u\n", tt);
        return 0;
    }
    

    嵌入式汇编语句:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    int main(int argc, const char *argv[])
    {
        pid_t tt;
        asm volatile(
        "mov $0x14, %%eax\n\t"
        "int $0x80\n\t"
        "mov %%eax, %0\n\t"
        :"=m" (tt)
                );
        printf("%u\n", tt);
        return 0;
    }
    
  3. 进入实验楼打开XFCE,在cd Code目录下输入指令: vi getpid.c;新建并打开getpid.c文件,在VI中输入在网上查阅的实现getpid的函数代码后,保存并退出。
  4. 再用gcc将该函数代码进行编译。通过输入指令./getpid即可得出目前进程号为:29895

5.修改getpid.c,改为嵌入式汇编语句,保存并退出后,使用以下命令gcc getpid -o getpid.c -m32编译

6.使用以下命令 ./getpid 运行得到目前的进程号为11926

实验总结

1. 系统调用的三层皮

1.API(xyz)

2.中断向量(system_call)

3.中断服务程序(sys_xyz)

2. 内嵌汇编调用system call

1.系统调用号放在eax中。

2.系统调用的参数,按照顺序分别放在ebx、ecx、edx、esi、edi和ebp中

3.返回值使用eax传递

 

作者: 王雪铖

原创作品转载请注明出处

《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

posted on 2016-03-20 16:32  20135105  阅读(221)  评论(0编辑  收藏  举报