标题:
linux程序运行分析
[打印本页]
作者:
yuyang911220
时间:
2017-2-24 11:05
标题:
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 <stdlib.h>
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 <main+0>: push %ebp
0x08048349 <main+1>: mov %esp,%ebp
0x0804834b <main+3>: sub $0x8,%esp
0x0804834e <main+6>: and $0xfffffff0,%esp
0x08048351 <main+9>: mov $0x0,%eax
0x08048356 <main+14>: add $0xf,%eax
0x08048359 <main+17>: add $0xf,%eax
0x0804835c <main+20>: shr $0x4,%eax
0x0804835f <main+23>: shl $0x4,%eax
0x08048362 <main+26>: sub %eax,%esp
0x08048364 <main+28>: movl $0x1,0xfffffffc(%ebp)
0x0804836b <main+35>: movl $0x2,0xfffffff8(%ebp)
0x08048372 <main+42>: pushl 0xfffffff8(%ebp)
0x08048375 <main+45>: pushl 0xfffffffc(%ebp)
0x08048378 <main+48>: call 0x8048334 <foo>
0x0804837d <main+53>: add $0x8,%esp
0x08048380 <main+56>: mov $0x0,%eax
0x08048385 <main+61>: leave
0x08048386 <main+62>: ret
End of assembler dump.
(gdb) disass foo
Dump of assembler code for function foo:
0x08048334 <foo+0>: push %ebp
0x08048335 <foo+1>: mov %esp,%ebp
0x08048337 <foo+3>: sub $0x4,%esp
0x0804833a <foo+6>: movl $0x3,0xfffffffc(%ebp)
0x08048341 <foo+13>: mov $0x0,%eax
0x08048346 <foo+18>: leave
0x08048347 <foo+19>: 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
(gdb) i reg eip
eip 0x8048348 0x8048348
可以看到当前eip的内容是0x8048348,要执行的指令是push %ebp
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0