linux程序运行分析

试验环境:

[jackie@cluster3 cLearn]$ gcc -v
Reading specs from /usr/lib/gcc/i386-RedHat-linux/3.4.6/specs
Configured with: ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –enable-shared –enable-threads=posix –disable-checking –with-system-zlib –enable-__cxa_atexit –disable-libunwind-exceptions –enable-java-awt=gtk –host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
[jackie@cluster3 cLearn]$ uname -a
Linux cluster3 2.6.20 #11 SMP Tue Feb 13 20:53:01 CST 2007 i686 i686 i386 GNU/Linux

函数调用分析

1. 编写测试程序
#include

int foo(int fi,int fj)
{
        int fk;
        fk = 3;
        return 0;
}
int main()
{
        int mi;
        int mj;
        mi=1;
        mj=2;
        foo(mi,mj);
        return 0;
}
2.编译代码
gcc -g -o cc cc.c
3.用gdb进行debug:gdb cc
(gdb) list
4       {
5               int fk;
6               fk = 3;
7               return 0;
8       }
9       int main()
10      {
11              int mi;
12              int mj;
13              mi=1;
(gdb)
14              mj=2;
15              foo(mi,mj);
16              return 0;
17      }
(2)查看汇编代码:
(gdb) disass main
Dump of assembler code for function main:
0x08048348 :    push   %ebp
0x08048349 :    mov    %esp,%ebp
0x0804834b :    sub    $0x8,%esp
0x0804834e :    and    $0xfffffff0,%esp
0x08048351 :    mov    $0x0,%eax
0x08048356 :   add    $0xf,%eax
0x08048359 :   add    $0xf,%eax
0x0804835c :   shr    $0x4,%eax
0x0804835f :   shl    $0x4,%eax
0x08048362 :   sub    %eax,%esp
0x08048364 :   movl   $0x1,0xfffffffc(%ebp)
0x0804836b :   movl   $0x2,0xfffffff8(%ebp)
0x08048372 :   pushl  0xfffffff8(%ebp)
0x08048375 :   pushl  0xfffffffc(%ebp)
0x08048378 :   call   0x8048334
0x0804837d :   add    $0x8,%esp
0x08048380 :   mov    $0x0,%eax
0x08048385 :   leave
0x08048386 :   ret
End of assembler dump.
(gdb) disass foo
Dump of assembler code for function foo:
0x08048334 :     push   %ebp
0x08048335 :     mov    %esp,%ebp
0x08048337 :     sub    $0x4,%esp
0x0804833a :     movl   $0x3,0xfffffffc(%ebp)
0x08048341 :    mov    $0x0,%eax
0x08048346 :    leave
0x08048347 :    ret
End of assembler dump.
(3)在主函数设置断点,并执行程序,让程序在main函数刚开始时暂停:
(gdb) break 9
Breakpoint 1 at 0x8048348: file cc.c, line 9.
(gdb) run
Starting program: /home/jackie/SourceDIR/cLearn/cc
Reading symbols from shared object read from target memory…done.
Loaded system supplied DSO at 0xb7f44000

Breakpoint 1, main () at cc.c:10
10      {
(4)查看关键寄存器内容:
(gdb) i reg esp
esp            0xbffbb77c       0xbffbb77c
(gdb) i reg ebp
ebp            0xbffbb7d8       0xbffbb7d8
(g