// Purpose:: Just copy the string provided by the user and print.
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
void *my_memcpy(char *,char *, short);
void printf_info(char *);
// Global Arrary
extern char *garray[3] = {NULL};
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("Please enter the the inputs in appropriate manner\n");
printf("<%s>
return 1;
}
short len = atoi(argv[2]);
char *str = argv[1];
char *dest = (char *)malloc((len +1)*sizeof(char));
if(NULL==dest)
printf("Error while allocating memory\n");
garray[0] = str;
garray[1] = dest;
garray[2] = (char *)&len;
my_memcpy(dest, str, len);
free(dest);
return 0;
}
// Copy the given memory region
void *my_memcpy(char *dest, char *source, short length) {
int i;
char *base = dest;
for(i =0; i < length ; i++) {
*dest = *source ;
dest = dest +1;
source = source +1;
}
printf_info(base);
return base;
}
// Print the information
void printf_info(char *info) {
int *segv, length;
int *i = NULL;
printf("desired string is: %s\n",info);
length= *garray[2];
//This would generate the SIGSEGV signal
*segv = *i;
}
Now lets compile and run this program. I have given otest as my program file for above test program.
mantosh@ubuntu:~/Desktop$ ./otest LinuxWorldRocks 10
desired string is: LinuxWorld
Segmentation fault (core dumped)
mantosh@ubuntu:~/Desktop$ ls -al
total 148
-rw------- 1 mantosh mantosh 286720 2011-02-19 18:07 core.23515
Now we have core-dump file. Now lets load into the gdb and try to get
some useful information.
mantosh@ubuntu:~/Desktop$ gdb
(gdb) core core.23515
(no debugging symbols found)
Core was generated by `./otest LinuxWorldRocks 10'.
Program terminated with signal 11, Segmentation fault.
[New process 23515]
==> Signal 11(SIGSEGV) was the reason for this core-dump file
==> pid of a program is 23515
#0 0x080485f8 in ?? ()
(gdb) bt
#0 0x080485f8 in ?? ()
#1 0x080485c2 in ?? ()
#2 0x0804855d in ?? ()
#3 0xb7df2775 in ?? ()
#4 0x08048401 in ?? ()
(gdb) symbol ./otest
Reading symbols from /home/mantosh/Desktop/otest...done.
(gdb) bt
#0 0x080485f8 in printf_info (info=0x8ec5008 "LinuxWorld") at test.c:58
#1 0x080485c2 in my_memcpy (dest=0x8ec5012 "", source=0xbfb9c6fe "Rocks",
length=10) at test.c:47
#2 0x0804855d in main (argc=3, argv=0xbfb9b3f4) at test.c:33
(gdb) bt full
==> bt full would display additional information of all variables along
==> with the function call.
#0 0x080485f8 in printf_info (info=0x8ec5008 "LinuxWorld") at test.c:58
segv = (int *) 0xbfb9b328
length = 10
i = (int *) 0x0
#1 0x080485c2 in my_memcpy (dest=0x8ec5012 "", source=0xbfb9c6fe "Rocks", length=10) at test.c:47
i = 10
base = 0x8ec5008 "LinuxWorld"
#2 0x0804855d in main (argc=3, argv=0xbfb9b3f4) at test.c:33
len = 10
str = 0xbfb9c6f4 "LinuxWorldRocks"
dest = 0x8ec5008 "LinuxWorld"
(gdb) f 2
#2 0x0804855d in main (argc=3, argv=0xbfb9b3f4) at test.c:33
33 my_memcpy(dest, str, len);
(gdb) info args
argc = 3
argv = (char **) 0xbfb9b3f4
(gdb) p garray
==> garray is global variable defined in the program, so we can access it
==> from anywhere in core-dump. However we will not get the information
==> while giving command like info locals inside any particular frame.
$1 = {[0] = 0xbfb9c6f4 "LinuxWorldRocks",
[1] = 0x8ec5008 "LinuxWorld",
[2] = 0xbfb9b352 "\n"}
(gdb) p *garray[2]
$2 = 10 '\n'
(gdb) p argv[0]
$3 = 0xbfb9c6ec "./otest"
(gdb) p argv[1]
$4 = 0xbfb9c6f4 "LinuxWorldRocks"
(gdb) p argv[2]
$5 = 0xbfb9c704 "10"
(gdb) p argv[3]
$6 = 0x0
(gdb) x/3xw argv
0xbfb9b3f4: 0xbfb9c6ec 0xbfb9c6f4 0xbfb9c704
(gdb) x/4xw argv
==> 4 unit display. Notice argv would be terminated by null pointer.
0xbfb9b3f4: 0xbfb9c6ec 0xbfb9c6f4 0xbfb9c704 0x00000000
(gdb) info locals
len = 10
str = 0xbfb9c6f4 "LinuxWorldRocks"
dest = 0x8ec5008 "LinuxWorld"
(gdb) p &str
$7 = (char **) 0xbfb9b34c
(gdb) x/1xw &str
0xbfb9b34c: 0xbfb9c6f4
(gdb) x/10cb 0xbfb9c6f4
0xbfb9c6f4:76 'L' 105 'i' 110 'n' 117 'u' 120 'x' 87 'W' 111 'o' 114 'r'
0xbfb9c6fc: 108 'l' 100 'd'
(gdb) p garray
$8 = {[0] = 0xbfb9c6f4 "LinuxWorldRocks",
[1] = 0x8ec5008 "LinuxWorld",
[2] = 0xbfb9b352 "\n"}
(gdb) x/3xw garray
==> Since we have defined garray as array of pointers. So by this command we
==> get the address information of 3 variables.
0x804a02c
(gdb) info frame
Stack level 2, frame at 0xbfb9b360:
eip = 0x804855d in main (test.c:33); saved eip 0xb7df2775
caller of frame at 0xbfb9b330
source language c.
Arglist at 0xbfb9b32c, args: argc=3, argv=0xbfb9b3f4
Locals at 0xbfb9b32c, Previous frame's sp at 0xbfb9b354
Saved registers:
ebp at 0xbfb9b358, eip at 0xbfb9b35c
(gdb) frame 1
#1 0x080485c2 in my_memcpy (dest=0x8ec5012 "", source=0xbfb9c6fe "Rocks", length=10) at test.c:47
47 printf_info(base);
(gdb) info locals
i = 10
base = 0x8ec5008 "LinuxWorld"
(gdb) p *base
$9 = 76 'L'
(gdb) p *base@10
$10 = "LinuxWorld"
(gdb) p *base@12
$11 = "LinuxWorld\000"
(gdb) p &i
$12 = (int *) 0xbfb9b324
(gdb) x/4db &i
0xbfb9b324: 10 0 0 0
(gdb) x/1dw &i
0xbfb9b324: 10
(gdb) info args
dest = 0x8ec5012 ""
source = 0xbfb9c6fe "Rocks"
length = 10
(gdb) frame 0
#0 0x080485f8 in printf_info (info=0x8ec5008 "LinuxWorld") at test.c:58
58 *segv = *i;
(gdb) info locals
segv = (int *) 0xbfb9b328
length = 10
i = (int *) 0x0
(gdb) info args
info = 0x8ec5008 "LinuxWorld"
(gdb) ptype garray
type = char *[3]
(gdb) p *garray
$13 = 0xbfb9c6f4 "LinuxWorldRocks"
(gdb) p *garray@3
$14 = {[0] = 0xbfb9c6f4 "LinuxWorldRocks",
[1] = 0x8ec5008 "LinuxWorld",
[2] = 0xbfb9b352 "\n"}
(gdb) info reg
eax 0x0 0
ecx 0x0 0
edx 0xb7f3c0d0 -1208762160
ebx 0xb7f3aff4 -1208766476
esp 0xbfb9b2f0 0xbfb9b2f0
ebp 0xbfb9b308 0xbfb9b308
esi 0x8048620 134514208
edi 0x80483e0 134513632
eip 0x80485f8 0x80485f8
eflags 0x10296 [ PF AF SF IF RF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
(gdb) x/i $eip
0x80485f8
0x80485f8
(gdb) info file
Symbols from "/home/mantosh/Desktop/otest".
Local core dump file:
`/home/mantosh/Desktop/core.23515', file type elf32-i386.
0x08048000 - 0x08048000 is load1
0x08049000 - 0x0804a000 is load2
0x0804a000 - 0x0804b000 is load3
0x08ec5000 - 0x08ee6000 is load4
0xb7ddb000 - 0xb7ddc000 is load5
0xb7ddc000 - 0xb7ddc000 is load6
0xb7f38000 - 0xb7f38000 is load7
0xb7f39000 - 0xb7f3b000 is load8
0xb7f3b000 - 0xb7f3c000 is load9
0xb7f3c000 - 0xb7f3f000 is load10
0xb7f56000 - 0xb7f59000 is load11
0xb7f59000 - 0xb7f5a000 is load12
0xb7f5a000 - 0xb7f5a000 is load13
0xb7f76000 - 0xb7f77000 is load14
0xb7f77000 - 0xb7f78000 is load15
0xbfb88000 - 0xbfb9d000 is load16
(gdb) q
====================================================
// Test Program which generate SIGFPE signal.
/// Purpose:: Just add two number provided by the user
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int add_sum(int * );
void fun_sigfpe(int *);
int main(int argc, char *argv[]) {
int a[2];
int sum;
if(argc != 3) {
printf("Please enter the the inputs in appropriate manner\n");
printf("<%s>
return 1;
}
a[0] = atoi(argv[1]);
a[1] = atoi(argv[2]);
sum = add_sum(a);
return 0;
}
// Addition of 2 number
int add_sum(int x[]) {
int temp;
temp = x[0] +x[1];
printf("sum = %d\n",temp);
fun_sigfpe(x);
return temp;
}
// Dummy function for generating the SIGFPE signal
void fun_sigfpe(int x[]) {
int *a = NULL;
int diff,c,d;
a = malloc(2*sizeof(int));
memcpy(a,x,2*sizeof(int));
c = *a;
a = a+ 1;
d = *a;
if(c !=d)
c = d;
diff = c -d ;
// This would generate SIGFPE signal
diff = c/diff;
}
Now lets compile and run this program. I have given otest as my program file for above test program.
mantosh@ubuntu:~/Desktop$ ./otest 22 03
sum = 25
Floating point exception (core dumped)
Now we have core-dump file. Now lets load into the gdb and try to get
some useful information.
mantosh@ubuntu:~/Desktop$ gdb
(gdb) core core.25586
(no debugging symbols found)
Core was generated by `./otest 22 03'.
Program terminated with signal 8, Arithmetic exception.
[New process 25586]
#0 0x080485d7 in ?? ()
==> Signal 08(SIGFPE) was the reason for this core-dump file
==> pid of a program is 25586
(gdb) bt
#0 0x080485d7 in ?? ()
#1 0x08048561 in ?? ()
#2 0x08048514 in ?? ()
#3 0xb7f01775 in ?? ()
#4 0x08048401 in ?? ()
(gdb) symbol ./otest
Reading symbols from /home/mantosh/Desktop/otest...done.
(gdb) bt
#0 0x080485d7 in fun_sigfpe (x=0xbfdf0648) at test.c:56
#1 0x08048561 in add_sum (x=0xbfdf0648) at test.c:36
#2 0x08048514 in main (argc=3, argv=0xbfdf06f4) at test.c:24
(gdb) bt full
#0 0x080485d7 in fun_sigfpe (x=0xbfdf0648) at test.c:56
a = (int *) 0x839f00c
diff = 0
c = 3
d = 3
#1 0x08048561 in add_sum (x=0xbfdf0648) at test.c:36
temp = 25
#2 0x08048514 in main (argc=3, argv=0xbfdf06f4) at test.c:24
a = {[0] = 22,
[1] = 3}
sum = -1207469952
(gdb) f 2
#2 0x08048514 in main (argc=3, argv=0xbfdf06f4) at test.c:24
24 sum = add_sum(a);
(gdb) info locals
a = {[0] = 22,
[1] = 3}
sum = -1207469952
(gdb) p a
$1 = {[0] = 22,
[1] = 3}
(gdb) x/2dw a
==> This would print the 2 unit value from the base address a. Its same as
==> we are printing array a as i did in above command.
0xbfdf0648: 22 3
(gdb) info args
argc = 3
argv = (char **) 0xbfdf06f4
(gdb) x/3xw argv
0xbfdf06f4: 0xbfdf26f9 0xbfdf2701 0xbfdf2704
(gdb) x/4xw argv
0xbfdf06f4: 0xbfdf26f9 0xbfdf2701 0xbfdf2704 0x00000000
(gdb) bt
#0 0x080485d7 in fun_sigfpe (x=0xbfdf0648) at test.c:56
#1 0x08048561 in add_sum (x=0xbfdf0648) at test.c:36
#2 0x08048514 in main (argc=3, argv=0xbfdf06f4) at test.c:24
(gdb) f 1
#1 0x08048561 in add_sum (x=0xbfdf0648) at test.c:36
36 fun_sigfpe(x);
(gdb) info locals
temp = 25
(gdb) info args
x = (int *) 0xbfdf0648
(gdb) f 0
#0 0x080485d7 in fun_sigfpe (x=0xbfdf0648) at test.c:56
56 diff = c/diff;
(gdb) info locals
a = (int *) 0x839f00c
diff = 0
c = 3
d = 3
(gdb) ptype a
type = int *
(gdb) info args
x = (int *) 0xbfdf0648
(gdb) p x
$2 = (int *) 0xbfdf0648
(gdb) p *x
$3 = 22
(gdb) p *x@2
$4 = {[0] = 22,
[1] = 3}
(gdb) info reg
eax 0x3 3
ecx 0x0 0
edx 0x0 0
ebx 0xb8049ff4 -1207656460
esp 0xbfdf05e0 0xbfdf05e0
ebp 0xbfdf0608 0xbfdf0608
esi 0x80485f0 134514160
edi 0x80483e0 134513632
eip 0x80485d7 0x80485d7
eflags 0x10246 [ PF ZF IF RF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
(gdb) x/i $eip
0x80485d7
(gdb) info frame
Stack level 0, frame at 0xbfdf0610:
eip = 0x80485d7 in fun_sigfpe (test.c:56); saved eip 0x8048561
called by frame at 0xbfdf0630
source language c.
Arglist at 0xbfdf05dc, args: x=0xbfdf0648
Locals at 0xbfdf05dc, Previous frame's sp is 0xbfdf0610
Saved registers:
ebp at 0xbfdf0608, eip at 0xbfdf060c
(gdb) q
====================================================
Now i have written a program, in which kernel would generate the core-dump
file because i have called abort() system call. This sample program display
the entries in a particular directory as we get by using utility "ls".
Now for demonstration purpose i take the number from the users which decide
whether abort() would be called or not. If we give numbers which is larger than the number of current entries in a particular directory,program exists
normally. Otherwise it display some entries and then make core-dump file for
this particular program.
// Test Program which generate SIGABRT signal.
// Purpose:: Display the basic information of each entry in a directory
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"unistd.h"
#include"dirent.h"
#include"sys/types.h"
#include"sys/stat.h"
#define MAX_ENTRY 50
extern struct dirent *entry[MAX_ENTRY] = {NULL};
extern struct stat sb[MAX_ENTRY] = {NULL};
extern int abrtcounter = 0;
int open_read( char *);
void display_entry( int );
int main(int argc, char *argv[]) {
char *name;
name = malloc(40*sizeof(char));
memset(name, '\0', 40*sizeof(char));
if( argc !=3) {
strcpy(name, ".");
abrtcounter = 1000;
}
else {
strcpy(name, argv[1] );
abrtcounter = atoi(argv[2]);
}
open_read(name);
free(name);
return 0;
}
// Function for opening and reading the directory information
int open_read( char *dirname) {
DIR *dir;
int ret;
int i , nentry;
dir = opendir(dirname);
ret = dirfd(dir);
if(ret == -1) {
printf("Error while reading the directory\n");
return 1;
}
for(i =0;; i++) {
entry[i] = readdir(dir);
if (entry[i] == NULL)
break;
}
// Save the number of entries counter
nentry = i;
ret = closedir (dir);
if(ret == -1) {
printf("Error while closing the directory\n");
}
display_entry(nentry);
return nentry;
}
// Function for displaying each entry information
void display_entry( int nentry) {
int i,ret;
printf("Total Number of entries: %d\n", nentry);
printf("Inode\taccess\tlink\tUid\tGid\tSize\tName\n");
for(i = 0;i < nentry ;i++) {
ret = stat( entry[i]->d_name, &sb[i]);
if(i ==abrtcounter)
// This line would generate SIGABRT signal
abort();
printf("%d\t%d\t%d\t%d\t%d\t%d\t%s\n",(int)sb[i].st_ino,
sb[i].st_mode, sb[i].st_nlink, sb[i].st_uid, sb[i].st_gid,
(int )sb[i].st_size, entry[i]->d_name);
}
return;
}
Now lets compile and run this program. I have given otest as my program
file for above test program.
mantosh@ubuntu:~/Desktop$ ./otest
Total Number of entries: 10
Inode access link Uid Gid Size Name
491338 16877 7 1000 1000 4096 practice
184457 33188 1 1000 1000 1845 test.c
244715 33261 1 1000 1000 12274 otest
184451 33188 1 1000 1000 25812 blog.c
237904 16895 6 1000 1000 4096 .
240305 16877 105 1000 1000 4096 ..
245380 16877 3 1000 1000 4096 LinuxPI
524903 16877 2 1000 1000 4096 OSystem
184455 33188 1 1000 1000 8358 Core-Dump.c
531516 16877 6 1000 1000 4096 gnu-linux
mantosh@ubuntu:~/Desktop$ ./otest . 16
Total Number of entries: 10
Inode access link Uid Gid Size Name
491338 16877 7 1000 1000 4096 practice
184457 33188 1 1000 1000 1845 test.c
244715 33261 1 1000 1000 12274 otest
184451 33188 1 1000 1000 25812 blog.c
237904 16895 6 1000 1000 4096 .
240305 16877 105 1000 1000 4096 ..
245380 16877 3 1000 1000 4096 LinuxPI
524903 16877 2 1000 1000 4096 OSystem
184455 33188 1 1000 1000 8358 Core-Dump.c
531516 16877 6 1000 1000 4096 gnu-linux
mantosh@ubuntu:~/Desktop$ ./otest . 6
Total Number of entries: 10
Inode access link Uid Gid Size Name
491338 16877 7 1000 1000 4096 practice
184457 33188 1 1000 1000 1845 test.c
244715 33261 1 1000 1000 12274 otest
184451 33188 1 1000 1000 25812 blog.c
237904 16895 6 1000 1000 4096 .
240305 16877 105 1000 1000 4096 ..
Aborted (core dumped)
Now lets load the core-dump file into the gdb.
mantosh@ubuntu:~/Desktop$ gdb
(gdb) core core.26853
(no debugging symbols found)
Core was generated by `./otest . 6'.
Program terminated with signal 6, Aborted.
[New process 26853]
==> Signal 06(SIGABRT) was the reason for this core-dump file
==> pid of a program is 265853
#0 0xb7fb9430 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fb9430 in __kernel_vsyscall ()
#1 0xb7e676d0 in ?? ()
#2 0xb7e69098 in ?? ()
#3 0x08048880 in ?? ()
#4 0x08048806 in ?? ()
#5 0x0804874e in ?? ()
#6 0xb7e52775 in ?? ()
#7 0x08048611 in ?? ()
(gdb) symbol ./otest
Reading symbols from /home/mantosh/Desktop/otest...done.
(gdb) bt
#0 0xb7fb9430 in __kernel_vsyscall ()
#1 0xb7e676d0 in ?? ()
#2 0xb7e69098 in ?? ()
#3 0x08048880 in display_entry (nentry=10) at test.c:87
#4 0x08048806 in open_read (dirname=0x8f1c008 ".") at test.c:70
#5 0x0804874e in main (argc=3, argv=0xbfab6804) at test.c:36
(gdb) info frame
Stack level 0, frame at 0xbfab6588:
eip = 0xb7fb9430 in __kernel_vsyscall; saved eip 0xb7e676d0
called by frame at 0xbfab6598
Arglist at 0xbfab6580, args:
Locals at 0xbfab6580, Previous frame's sp is 0xbfab6588
Saved registers:
ebp at 0xbfab6578, eip at 0xbfab6584
(gdb) f 5
#5 0x0804874e in main (argc=3, argv=0xbfab6804) at test.c:36
36 open_read(name);
(gdb) info args
argc = 3
argv = (char **) 0xbfab6804
(gdb) x/4xw argv
0xbfab6804: 0xbfab86fb 0xbfab8703 0xbfab8705 0x00000000
(gdb) p argv
$1 = (char **) 0xbfab6804
(gdb) p *argv@3
$2 = {[0] = 0xbfab86fb "./otest",
[1] = 0xbfab8703 ".",
[2] = 0xbfab8705 "6"}
(gdb) info locals
name = 0x8f1c008 "."
(gdb) p abrtcounter
==> abrtcounter, sb[1], *entry[1] are global variable. So we can check
==> their values from any frame in the program.
$3 = 6
(gdb) p sb[1]
$4 = {
st_dev = 1792,
__pad1 = 0,
st_ino = 184457,
st_mode = 33188,
st_nlink = 1,
st_uid = 1000,
st_gid = 1000,
st_rdev = 0,
__pad2 = 0,
st_size = 1845,
st_blksize = 4096,
st_blocks = 8,
st_atim = {
tv_sec = 1298123976,
tv_nsec = 0
},
st_mtim = {
tv_sec = 1298123613,
tv_nsec = 0
},
st_ctim = {
tv_sec = 1298123613,
tv_nsec = 0
},
__unused4 = 0,
__unused5 = 0
}
(gdb) p entry[1]
$5 = (struct dirent *) 0x8f1c064
(gdb) p *entry[1]
$6 = {
d_ino = 184457,
d_off = 328961605,
d_reclen = 20,
d_type = 8 '\b',
d_name = "test.c\000\000\b��\003\000K��\035\024\000\botest\000\000\000\b\203�\002\000�~�2\024\000\bblog.c\000\000\bP�\003\000��\205>\020\000\004.\000\000\000\004��\003\000���A\020\000\004..\000\000\004\204�\003\000\031��C\024\000\004LinuxPI\000\004g\002\b\000s��T\024\000\004OSystem\000\004\207�\002\000���U\030\000\bCore-Dump.c\000\b<\034\b\000���\177\030\000\004gnu-linux\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
}
(gdb) ptype sb[1]
type = struct stat {
__dev_t st_dev;
short unsigned int __pad1;
__ino_t st_ino;
__mode_t st_mode;
__nlink_t st_nlink;
__uid_t st_uid;
__gid_t st_gid;
__dev_t st_rdev;
short unsigned int __pad2;
__off_t st_size;
__blksize_t st_blksize;
__blkcnt_t st_blocks;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
long unsigned int __unused4;
long unsigned int __unused5;
}
(gdb) ptype entry[1]
type = struct dirent {
__ino_t d_ino;
__off_t d_off;
short unsigned int d_reclen;
unsigned char d_type;
char d_name[256];
} *
(gdb) p *(struct stat *)0x8f1c064
==> Here i am just typecasting the address 0x8f1c064 which is of type
==>(struct dirent *). Notice how dangerous if we do typecast the different
==> kind of structures.Be careful while typecasting the pointers into the
==> other type into the program code.
$7 = {
st_dev = 1412879335114854537,
__pad1 = 20,
st_ino = 779383653,
st_mode = 134217827,
st_nlink = 244715,
st_uid = 498858059,
st_gid = 1862795284,
st_rdev = 576460754257143156,
__pad2 = 53379,
st_size = 852524765,
st_blksize = 1644691476,
st_blocks = 778530668,
st_atim = {
tv_sec = 134217827,
tv_nsec = 237904
},
st_mtim = {
tv_sec = 1048952490,
tv_nsec = 772014096
},
st_ctim = {
tv_sec = 67108864,
tv_nsec = 240305
},
__unused4 = 1103745453,
__unused5 = 772014096
}
(gdb) p &sb[1]
$8 = (struct stat *) 0x804a1b8
(gdb) p *(struct dirent *)0x804a1b8
==> Here i am typecasting the address 0x804a1b8 which is having type
==>(struct stat *) type into the (struct dirent *).
$9 = {
d_ino = 1792,
d_off = 0,
d_reclen = 0,
d_type = 0 '\0',
d_name = "\000\211�\002\000�\201\000\000\001\000\000\000�\003\000\000�\003\000\000\000\000\000\000\000\000\000\000\000\000\000\0005\a\000\000\000\020\000\000\b\000\000\000��_M\000\000\000\000]�_M\000\000\000\000]�_M\000\000\000\000\000\000\000\000\000\000\000\000\000\a\000\000\000\000\000\000\000\000\000\000��\003\000�\201\000\000\001\000\000\000�\003\000\000�\003\000\000\000\000\000\000\000\000\000\000\000\000\000\000�/\000\000\000\020\000\000\030\000\000\000��_M\000\000\000\000\236�_M\000\000\000\000\236�_M\000\000\000\000\000\000\000\000\000\000\000\000\000\a\000\000\000\000\000\000\000\000\000\000\203�\002\000�\201\000\000\001\000\000\000�\003\000\000�\003\000\000\000\000\000\000\000\000\000\000\000\000\000\000�d\000\000\000\020\000\0008\000\000\000��_M\000\000\000\000��_M\000\000\000\000��_M\000\000\000\000\000\000\000\000\000\000\000\000\000\a"
}
(gdb) bt
#0 0xb7fb9430 in __kernel_vsyscall ()
#1 0xb7e676d0 in ?? ()
#2 0xb7e69098 in ?? ()
#3 0x08048880 in display_entry (nentry=10) at test.c:87
#4 0x08048806 in open_read (dirname=0x8f1c008 ".") at test.c:70
#5 0x0804874e in main (argc=3, argv=0xbfab6804) at test.c:36
(gdb) info locals
name = 0x8f1c008 "."
(gdb) ptype name
type = char *
(gdb) p *(int *)(name -4)
==> Since name store the base address which has been returned from
==> heap. So if we go 4 byte lower to the base address and see the value
==> it would be more than the sizeof values has been passed to malloc.
==> In this case we passed 40, so we can see the value stored is 49. This
==> information would be used internally when we pass the base address to
==> free(). Please notice the concept which i have discussed is highly
==> system dependent and i am talking about the Linux machine.
$10 = 49
(gdb) frame 4
#4 0x08048806 in open_read (dirname=0x8f1c008 ".") at test.c:70
70 display_entry(nentry);
(gdb) info locals
dir = (DIR *) 0x8f1c038
ret = 0
i = 10
nentry = 10
(gdb) ptype dir
type = struct __dirstream {
} *
(gdb) p *dir
$12 =
(gdb) f 3
#3 0x08048880 in display_entry (nentry=10) at test.c:87
87 abort();
(gdb) info locals
i = 6
ret = 0
(gdb) p abrtcounter
$13 = 6
(gdb) p MAX_ENTRY
No symbol "MAX_ENTRY" in current context.
(gdb) info reg
eax 0x0 0
ecx 0x68e5 26853
edx 0x6 6
ebx 0x68e5 26853
esp 0xbfab66c0 0xbfab66c0
ebp 0xbfab6708 0xbfab6708
esi 0x3e8 1000
edi 0xb7f9aff4 -1208373260
eip 0x8048880 0x8048880
eflags 0x246 [ PF ZF IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x33 51
(gdb) x/i $eip
0x8048880
(gdb) bt full
#0 0xb7fb9430 in __kernel_vsyscall ()
No symbol table info available.
#1 0xb7e676d0 in ?? ()
No symbol table info available.
#2 0xb7e69098 in ?? ()
No symbol table info available.
#3 0x08048880 in display_entry (nentry=10) at test.c:87
i = 6
ret = 0
#4 0x08048806 in open_read (dirname=0x8f1c008 ".") at test.c:70
dir = (DIR *) 0x8f1c038
ret = 0
i = 10
nentry = 10
#5 0x0804874e in main (argc=3, argv=0xbfab6804) at test.c:36
name = 0x8f1c008 "."
(gdb) quit