Debugging Core Files Using HP WDB (5900-1573; WDB 6.2; January 2011)

Example 7 mmapfile command usage in GDB
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <assert.h>
5 #include <unistd.h>
6 #include <sys/fcntl.h>
7 #include <sys/mman.h>
8
9 int main()
10 {
11 int devzfd;
12 int fd, fd2;
13 char *p, *q, *r, *s, *t, *u;
14
15 devzfd = open("/dev/zero", O_RDONLY);
16 if ( devzfd == -1 )
17 {
18 perror("open");
19 return -1;
20 }
21 fd = open("testfile", O_RDWR);
22 if ( fd == -1 )
23 {
24 perror("open");
25 return -1;
26 }
27 p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
28 assert( p != (void *)-1 );
29
30 q = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, devzfd, 0);
31 assert( q != (void *)-1 );
32
33 r = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
34 assert( r != (void *)-1 );
35
36 s = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
37 assert( s != (void *)-1 );
38
39 t = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
40 assert( t != (void *)-1 );
41
42 fd2 = open("testfile2", O_RDWR);
43 if ( fd2 == -1 )
44 {
45 perror("open");
46 return -1;
47 }
48 u = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
49 if (u == MAP_FAILED)
50 printf("PROT_READ|PROT_WRITE, MAP_SHARED failed, errno = %d\n", errno);
51 else
52 assert( u != (void *)-1 );
53
54 printf("p %p q %p r %p s %p t %p u %p\n", p, q, r, s, t, u);
55 abort();
56 return 0;
57 }
Compile and run the program to generate a core.
Support for mmapfile command 37