查看进程的内存布局

代码:和静态库中的代码是一样的

#vector.h 

1 #ifndef vector_h
 2 #define vector_h
 3 
 4 void addVec(int *xP, int *yP, int *zP, int Num);
 5 void mulVec(int *xP, int *yP, int *zP, int Num);                                        
 6 
 7 #endif

#addVec.c

 1 #include "vector.h"
 2                                                                                         
 3 void addVec(int *xP, int *yP, int *zP, int Num){
 4     for(int i = 0; i < n; i++){
 5         zP[i] = xP[i] + yP[i];
 6     }
 7 
 8     return;
 9 }
~   

#mulVec.c

 1 #include "vector.h"                                                                     
 2 
 3 void mulVec(int *xP, int *yP, int *zP, int Num){
 4     for(int i = 0; i < n; i++){
 5         zP[i] = xP[i] * yP[i];
 6     }
 7 
 8     return;
 9 }


#testVec.c

 1 #include <stdio.h>                                                                      
 2 #include "vector.h"
 3 
 4 int x[2] = {1, 2};
 5 int y[2] = {3, 4};
 6 int z[2];
 7 
 8 int main(int argc, char **argv)
 9 {
10     addVec(x, y, z, 2);
11     printf("z = [%d %d]\n", z[0],  z[1]);
12 
13     return 0;
14 }

  Makefile文件:

 1  1 testVec:testVec.o libVector.a
 2  2     gcc -g $^ -o $@
 3  3 libVector.a:addVec.o mulVec.o
 4  4     ar crs $@ $^
 5  5 addVec.o:addVec.c
 6  6     gcc -g -Og -c addVec.c - addVec.o
 7  7 mulVec.o:mulVec.c
 8  8     gcc -g -Og -c mulVec.c - mulVec.o
 9  9 testVec.o:testVec.c
10 10     gcc -g -Og -c testVec.c - testVec.o 

运行程序,并让其停下来:

gdb testVec 
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testVec...
(gdb) l
1       #include <stdio.h>
2       #include "vector.h"
3
4       int x[2] = {1, 2};
5       int y[2] = {3, 4};
6       int z[2];
7
8       int main(int argc, char **argv)
9       {
10              addVec(x, y, z, 2);
11              printf("z = [%d %d]\n", z[0],  z[1]);
12
13              return 0;
14      }
(gdb) b main
Breakpoint 1 at 0x1148: file testVec.c, line 10.
(gdb) r
Starting program: /home/nication/WORKM/studyCode/gnuC/chapter04/l

Breakpoint 1, main (argc=1, argv=0x7fffffffd6c8) at testVec.c:10
10              addVec(x, y, z, 2);

 查看进程id

ps aux | grep testVec
nication    3215  0.2  0.5 417040 46104 pts/1    Sl+  15:58   0:00 gdb testVec
nication    3225  0.0  0.0   2212   780 pts/1    t    15:58   0:00  /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
nication    3230  0.0  0.0   9480  2324 pts/2    S+   15:59   0:00 grep --colour=auto testVec

  获取到testVec的进程编号为3225,查看3225的maps:

cat /proc/3225/maps 
555555554000-555555555000 r--p 00000000 6962746                    /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
555555555000-555555556000 r-xp 00001000 08:0a 6962746                    /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
555555556000-555555557000 r--p 00002000 08:0a 6962746                    /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
555555557000-555555558000 r--p 00002000 08:0a 6962746                    /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
555555558000-555555559000 rw-p 00003000 08:0a 6962746                    /home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec
7ffff7d95000-7ffff7d97000 rw-p 00000000 00:00 0  
7ffff7d97000-7ffff7dbd000 r--p 00000000 08:09 3678828                    /usr/lib/libc-2.32.so
7ffff7dbd000-7ffff7f0a000 r-xp 00026000 08:09 3678828                    /usr/lib/libc-2.32.so
7ffff7f0a000-7ffff7f56000 r--p 00173000 08:09 3678828                    /usr/lib/libc-2.32.so
7ffff7f56000-7ffff7f59000 r--p 001be000 08:09 3678828                    /usr/lib/libc-2.32.so
7ffff7f59000-7ffff7f5c000 rw-p 001c1000 08:09 3678828                    /usr/lib/libc-2.32.so
7ffff7f5c000-7ffff7f62000 rw-p 00000000 00:00 0  
7ffff7fca000-7ffff7fce000 r--p 00000000 00:00 0                          [vvar]
7ffff7fce000-7ffff7fd0000 r-xp 00000000 00:00 0                          [vdso]
7ffff7fd0000-7ffff7fd2000 r--p 00000000 08:09 3678814                    /usr/lib/ld-2.32.so
7ffff7fd2000-7ffff7ff3000 r-xp 00002000 08:09 3678814                    /usr/lib/ld-2.32.so
7ffff7ff3000-7ffff7ffc000 r--p 00023000 08:09 3678814                    /usr/lib/ld-2.32.so
7ffff7ffc000-7ffff7ffd000 r--p 0002b000 08:09 3678814                    /usr/lib/ld-2.32.so
7ffff7ffd000-7ffff7fff000 rw-p 0002c000 08:09 3678814                    /usr/lib/ld-2.32.so
7ffffffdd000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]  
maps文件说明:  

  第一列:表示该内存段的起始地址

  第二列:表示该内存段对应的权限,表明该地址区间的访问属性,r w x p对应位无权限用-代替 可读 可写 执行 私有

  第三列:如果内存段的信息是文件映射,该列为内存段中的信息在文件中的映射页的偏移

  第四列:如果内存段的信息是文件映射,该列为文件所在的设备号;若是匿名映射,设备号为00:00

  第五列:如果内存段的信息是文件映射,该列为文件的文件索引号;若是匿名隐射,文件索引号为0

  第六列:如果是内存段的信息是文件映射,该列为映射的文件名;若是匿名隐射,该列信息可能是映射到的栈[stack]或者其他数据区

 每一行对应的是程序中不同的内存段:

  555555554000-555555555000:表示的是该内存段的起始地址和结束地址; 

      r--p: 表示的是该内存段是可读(read),可执行(excute),私有(personal);

  00000000表示的是该内存段的信息映射到文件节点6962746对应的文件的偏移量,将该页的信息拷贝到该内存段
 08:0a:是设备号,linux 对于每个设备都抽象成设备文件进行管理操作,每一个设备由主设备号(确定是哪一类设备)和次设备号(确定该类下具体一个设备)结合确定,所有的设备文件都存放在/dev下,可以进一步的查看该设备号对应的设备,如下图,对应的为sda10,是硬盘的sda10分区
cat /proc/devices 
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 21 sg
 29 fb
116 alsa
128 ptm
136 pts
180 usb
188 ttyUSB
189 usb_device
202 cpu/msr
203 cpu/cpuid
216 rfcomm
226 drm
237 aux
238 cec
239 mei
240 binder
241 hidraw
242 nvme
243 bsg
244 watchdog
245 remoteproc
246 ptp
247 pps
248 lirc
249 rtc
250 dma_heap
251 dax
252 dimmctl
253 ndctl
254 gpiochip

Block devices:
  7 loop
  8 sd
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
259 blkext

其中:

6962746:为当前可执行文件的文件索引节点号,也就是inode的索引号,可以通过该索引号,找到一个inode对象,通过该对象可以可以找改文件在磁盘上的具体位置:/home/nication/WORKM/studyCode/gnuC/chapter04/lib/static/testVec对应的就是映射文件名。

https://blog.csdn.net/w1300048671/article/details/72821389

posted @ 2020-12-24 15:47  叕叒双又  阅读(398)  评论(0编辑  收藏  举报