April 2005
Intermediate to advanced
344 pages
11h 59m
English
Often
it is necessary
to hook into programs to understand what system calls they invoke.
The strace tool can do this. For example, consider
the following C program, which simply prints the
/etc/passwd file:
#include <stdio.h>
int main(void)
{
FILE *myfile;
char tempstring[1024];
if(!(myfile=fopen("/etc/passwd","r")))
{
fprintf(stderr,"Could not open file");
exit(1);
}
while(!feof(myfile))
{
fscanf(myfile,"%s",tempstring);
fprintf(stdout,"%s",tempstring);
}
exit(0);
}Assuming you have compiled the preceding code with the
gcc compiler to produce an executable called
a.out, run the following strace
command:
[notroot]$ strace -o strace.out ./a.out > /dev/nullNow the output from strace is stored in
strace.out. Take a look at it to see all the
function calls invoked by a.out. For example,
issue the following grep command to realize that
the fopen( ) library call in
a.out invokes the open( )
system call to open the /etc/passwd file:
[notroot]$ grep "/etc/passwd" strace.out
open("/etc/passwd", O_RDONLY) = 3Read now
Unlock full access