Debugging with GDB Manual HP WDB v6.3 (5900-2180, August 2012)
assert( r != (void *)-1 );
s = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert( s != (void *)-1 );
t = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
assert( t != (void *)-1 );
fd2 = open("testfile2", O_RDWR);
if ( fd2 == -1 )
{
perror("open");
return -1;
}
u = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd2, 0);
if (u == MAP_FAILED)
printf("PROT_READ|PROT_WRITE, MAP_SHARED failed, errno = %d\n", errno);
else
assert( u != (void *)-1 );
printf("p %p q %p r %p s %p t %p u %p\n", p, q, r, s, t, u);
abort();
return 0;
}
Compile and run the program to generate a core.
$ aCC -Ae -o mmap_test -g mmap.c
$ ./mmap_test
p 7aadc000 q 7aadb000 r 7aada000 s 7aad9000 t c1a91000 u c4406000 ABORT instruction (core dumped)
Now, debug the core file with gdb.
$ gdb mmap_test core
…
Core was generated by `mmap_test'.
Program terminated with signal 6, Aborted.
#0 0x60000000c035d750:0 in kill+0x30 () from /usr/lib/hpux32/libc.so.1
(gdb) frame 3
#3 0x4001400:0 in main () at mmap.c:55
55 abort();
(gdb) p p
$2 = 0x200000007aadc000 ""
(gdb) p q
$3 = 0x200000007aadb000 ""
(gdb) p r
$4 = 0x200000007aada000 <Address 0x200000007aada000 out of bounds>(gdb) p s
$5 = 0x200000007aad9000 "#include <stdlib.h>\n#include <stdio.h>\nint main(){\n foo();\n bar();\n
return 0;\n}\nint foo(){\n int * res = malloc(4);\n printf(\"*res %d\\n\", *res);\n return 1;\n}\nint
bar(){\n printf(\"Hel"...
(gdb) p t
$6 = 0x60000000c1a91000 <Address 0x60000000c1a91000 out of bounds>
(gdb) p u
$7 = 0x60000000c4406000 <Address 0x60000000c4406000 out of bounds>
The pointers r, s, t are from mmap-ed regions which are missing in corefile.
(gdb) help mmapfile
Add contents of memory mapped files missing from core dump.
Usage:
mmapfile <FILENAME> <MAP-ADDRESS> <FILE-OFFSET> <LENGTH>
This can be used if the core file does not contain mmap-ed regions
from the live run. This can happen for files mmap-ed with
MAP_PRIVATE with RO permissions, or for files mmap-ed with
MAP_SHARED. With this command, the user can access data from
the mmap-ed files, which are missing from the core file. The
``info target'' command displays all the segments from the
core file.
Now, add the region supposed to be pointed by "r".
(gdb) p r
$4 = 0x200000007aada000 <Address 0x200000007aada000 out of bounds>
(gdb) mmapfile testfile 0x200000007aada000 0 0xe1
(gdb) p r
$8 = 0x200000007aada000 "#include <stdlib.h>\n#include <stdio.h>\nint main(){\n foo();\n bar();\n
return 0;\n}\nint foo(){\n int * res = malloc(4);\n printf(\"*res %d\\n\", *res);\n return 1;\n}\nint
bar(){\n printf(\"Hel"...
(gdb) i target
14.15 Debugging Core Files 159