#include int main(int argc,char *argv[]){char buffer[100];getcwd(buffer, sizeof(buffer));printf("The current directory is: %s\n", buffer);printf("prog name : %s \n" , argv[0]);return 0;}
gcc -Wall -pipe -g -static -o getpath getpath.c
#include main(int argc, char * argv[]){char resolved_path[80];realpath(argv[0],resolved_path);printf("resolved_path: %s\n", resolved_path);sleep(300);}
执行:strings getpath| grep getpath
#include #include #include int a = 0; //全局初始化区char *p1; //全局未初始化区main(int argc,char **argv){int b; //栈char s[] = "abc"; //栈char *p2; //栈char *p3 = "123456";// 123456\0在常量区,p3在栈上。static int c =0;// 全局(静态)初始化区static int uc,uc1,uc2;// 全局(静态)未初始化区p1 = (char *)malloc(10);p2 = (char *)malloc(20);//分配得来得10和20字节的区域就在堆区。strcpy(p1, "123456");// 123456\0放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。printf("堆p1 \t\t\t%p\n",p1);printf("堆p2 \t\t\t%p\n",p2);printf("栈&p3 \t\t\t%p\n",&p3);printf("栈&p2 \t\t\t%p\n",&p2);printf("栈s \t\t\t%p\n",s);printf("栈&s[1] \t\t%p\n",&s[1]);printf("栈&b \t\t\t%p\n",&b);printf("main地址\t\t%p\n",main);printf("文本常量区\t\t%p\n",p3);printf("全局初始化区\t\t%p\n",&a);printf("(静态)初始化区\t%p\n",&c);printf("全局未初始化区\t\t%p\n",&p1);printf("(静态)未初始化区\t%p\n",&uc);printf("(静态)未初始化区\t%p\n",&uc1);printf("(静态)未初始化区\t%p\n",&uc2);char *p;if(p=getenv("PATH")){printf("USER=%p\n",p);}printf("ARGC\t%p\n",&argc);printf("ARGV\t%p\n",argv);printf("ARGV\t%p\n",*argv);sleep(300);}
编译一下:gcc -Wall -pipe -g -static -o memoryshow memoryshow.c
[root@localhost ~]# ./memoryshow堆p1 0xa3e8b0堆p2 0xa3e8d0栈&p3 0x7fff94fec4a0栈&p2 0x7fff94fec4a8栈s 0x7fff94fec4b0栈&s[1] 0x7fff94fec4b1栈&b 0x7fff94fec4b4main地址 0×400494文本常量区 0x47f3b0全局初始化区 0x6a6e50(静态)初始化区 0x6a6e54全局未初始化区 0x6a9ba8(静态)未初始化区 0x6a6e58(静态)未初始化区 0x6a6e5c(静态)未初始化区 0x6a6e60USER=0x7fff94fece29ARGC 0x7fff94fec49cARGV 0x7fff94fec598ARGV 0x7fff94fec82b
[root@localhost ~]# ps -ef | grep showmemoryroot 2715 2150 0 21:58 pts/1 00:00:00 grep showmemory[root@localhost ~]# ps -ef | grep memoryroot 2713 2087 0 21:58 pts/0 00:00:00 ./memoryshowroot 2717 2150 0 21:58 pts/1 00:00:00 grep memory[root@localhost ~]# cat /proc/2713/maps00400000-004a6000 r-xp 00000000 fd:00 524196 /root/memoryshow006a6000-006a7000 rw-p 000a6000 fd:00 524196 /root/memoryshow006a7000-006aa000 rw-p 00000000 00:00 000a3d000-00a60000 rw-p 00000000 00:00 0 [heap]7f050a5c2000-7f050a5c3000 rw-p 00000000 00:00 07fff94fd8000-7fff94fed000 rw-p 00000000 00:00 0 [stack]7fff94fff000-7fff95000000 r-xp 00000000 00:00 0 [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
[root@localhost ~]# ./memoryshow堆p1 0x76b8b0堆p2 0x76b8d0栈&p3 0x7fff9987a9f0栈&p2 0x7fff9987a9f8栈s 0x7fff9987aa00栈&s[1] 0x7fff9987aa01栈&b 0x7fff9987aa04main地址 0×400494文本常量区 0x47f3b0全局初始化区 0x6a6e50(静态)初始化区 0x6a6e54全局未初始化区 0x6a9ba8(静态)未初始化区 0x6a6e58(静态)未初始化区 0x6a6e5c(静态)未初始化区 0x6a6e60USER=0x7fff9987ce29ARGC 0x7fff9987a9ecARGV 0x7fff9987aae8ARGV 0x7fff9987c82b
[root@localhost ~]# cat /proc/2713/mapscat: /proc/2713/maps: No such file or directory[root@localhost ~]# ps -ef | grep memoryroot 2719 2087 0 21:59 pts/0 00:00:00 ./memoryshowroot 2722 2150 0 21:59 pts/1 00:00:00 grep memory[root@localhost ~]# cat /proc/2719/maps00400000-004a6000 r-xp 00000000 fd:00 524196 /root/memoryshow006a6000-006a7000 rw-p 000a6000 fd:00 524196 /root/memoryshow006a7000-006aa000 rw-p 00000000 00:00 00076a000-0078d000 rw-p 00000000 00:00 0 [heap]7f9987494000-7f9987495000 rw-p 00000000 00:00 07fff99868000-7fff9987d000 rw-p 00000000 00:00 0 [stack]7fff999ff000-7fff99a00000 r-xp 00000000 00:00 0 [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
[root@localhost ~]# gdb memoryshowReading symbols from /root/memoryshow…done.(gdb) b mainBreakpoint 1 at 0x4004a3: file memoryshow.c, line 11.(gdb) rStarting program: /root/memoryshowBreakpoint 1, main (argc=1, argv=0x7fffffffe5c8) at memoryshow.c:1111 char s[] = "abc"; //栈(gdb) b 44Breakpoint 2 at 0x4006db: file memoryshow.c, line 44.(gdb) cContinuing.堆p1 0x6ab8b0堆p2 0x6ab8d0栈&p3 0x7fffffffe4d0栈&p2 0x7fffffffe4d8栈s 0x7fffffffe4e0栈&s[1] 0x7fffffffe4e1栈&b 0x7fffffffe4e4main地址 0×400494文本常量区 0x47f3b0全局初始化区 0x6a6e50(静态)初始化区 0x6a6e54全局未初始化区 0x6a9ba8(静态)未初始化区 0x6a6e58(静态)未初始化区 0x6a6e5c(静态)未初始化区 0x6a6e60USER=0x7fffffffedffARGC 0x7fffffffe4ccARGV 0x7fffffffe5c8ARGV 0x7fffffffe81bBreakpoint 2, main (argc=1, argv=0x7fffffffe5c8) at memoryshow.c:4545 sleep(300);(gdb) x/30s 0x7fffffffedff0x7fffffffedff: "/usr/local/maven/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/jrockit/bin:/usr/local/redis/bin:/usr/local/mysql/bin:/root/bin"0x7fffffffee9c: "MAIL=/var/spool/mail/root"0x7fffffffeeb6: "nodeName=_0_218"0x7fffffffeec6: "_=/usr/bin/gdb"0x7fffffffeed5: "PWD=/root"0x7fffffffeedf: "JAVA_HOME=/usr/local/jrockit"0x7fffffffeefc: "LANG=en_US.UTF-8"0x7fffffffef0d: "configDir=/data/config"0x7fffffffef24: "LINES=43"0x7fffffffef2d: "HISTCONTROL=ignoredups"0x7fffffffef44: "HOME=/root"0x7fffffffef4f: "SHLVL=1"0x7fffffffef57: "M2_HOME=/usr/local/maven"0x7fffffffef70: "LOGNAME=root"0x7fffffffef7d: "SSH_CONNECTION=192.168.0.158 62555 192.168.0.98 22"0x7fffffffefb0: "LESSOPEN=|/usr/bin/lesspipe.sh %s"0x7fffffffefd2: "G_BROKEN_FILENAMES=1"0x7fffffffefe7: "/root/memoryshow" <–注意这里0x7fffffffeff8: ""0x7fffffffeff9: ""0x7fffffffeffa: ""0x7fffffffeffb: ""0x7fffffffeffc: ""0x7fffffffeffd: ""0x7fffffffeffe: ""0x7fffffffefff: ""0x7ffffffff000:0x7ffffffff000:0x7ffffffff000:0x7ffffffff000:
#include #include #include #define MAXBUFSIZE 1024int main ( int argc, char * argv[] ) {char buf[ MAXBUFSIZE ];int count;count = readlink( "/proc/self/exe", buf, MAXBUFSIZE );if ( count < 0 || count >= MAXBUFSIZE ) {printf( "Failed\n" );return( EXIT_FAILURE );}buf[ count ] = '\0';printf( "/proc/self/exe -> [%s]\n", buf );return( EXIT_SUCCESS );} /* end of main */
http://www.wjthink.org/?p=522