Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [403]

By Root 1080 0
giving it an unusually large data set to churn on, as in the previous example.

If gprof is more than you need, calls is a program that displays a tree of all function calls in your C source code. This can be useful to either generate an index of all called functions or produce a high-level hierarchical report of the structure of a program.

Use of calls is simple: you tell it the names of the source files to map out, and a function-call tree is displayed. For example:

papaya$ calls scan.c

1 level1 [scan.c]

2 getid [scan.c]

3 getc

4 eatwhite [scan.c]

5 getc

6 ungetc

7 strcmp

8 eatwhite [see line 4]

9 balance [scan.c]

10 eatwhite [see line 4]

By default, calls lists only one instance of each called function at each level of the tree (so that if printf is called five times in a given function, it is listed only once). The -a switch prints all instances. calls has several other options as well; using calls -h gives you a summary.

Using strace

strace is a tool that displays the system calls being executed by a running program. This can be extremely useful for real-time monitoring of a program's activity, although it does take some knowledge of programming at the system-call level. For example, when the library routine printf is used within a program, strace displays information only about the underlying write system call when it is executed. Also, strace can be quite verbose: many system calls are executed within a program that the programmer may not be aware of. However, strace is a good way to quickly determine the cause of a program crash or other strange failure.

Take the "Hello, World!" program given earlier in the chapter. Running strace on the executable hello gives us the following:

papaya$ strace hello

execve("./hello", ["hello"], [/* 49 vars */]) = 0

mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,\

-1, 0) = 0x40007000

mprotect(0x40000000, 20881, PROT_READ|PROT_WRITE|PROT_EXEC) = 0

mprotect(0x8048000, 4922, PROT_READ|PROT_WRITE|PROT_EXEC) = 0

stat("/etc/ld.so.cache", {st_mode=S_IFREG|0644, st_size=18612,\

...}) = 0

open("/etc/ld.so.cache", O_RDONLY) = 3

mmap(0, 18612, PROT_READ, MAP_SHARED, 3, 0) = 0x40008000

close(3) = 0

stat("/etc/ld.so.preload", 0xbffff52c) = -1 ENOENT (No such\

file or directory)

open("/usr/local/KDE/lib/libc.so.5", O_RDONLY) = -1 ENOENT (No\

such file or directory)

open("/usr/local/qt/lib/libc.so.5", O_RDONLY) = -1 ENOENT (No\

such file or directory)

open("/lib/libc.so.5", O_RDONLY) = 3

read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3"..., 4096) = 4096

mmap(0, 770048, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = \

0x4000d000

mmap(0x4000d000, 538959, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_\

FIXED, 3, 0) = 0x4000d000

mmap(0x40091000, 21564, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_\

FIXED, 3, 0x83000) = 0x40091000

mmap(0x40097000, 204584, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_\

FIXED|MAP_ANONYMOUS, -1, 0) = 0x40097000

close(3) = 0

mprotect(0x4000d000, 538959, PROT_READ|PROT_WRITE|PROT_EXEC) = 0

munmap(0x40008000, 18612) = 0

mprotect(0x8048000, 4922, PROT_READ|PROT_EXEC) = 0

mprotect(0x4000d000, 538959, PROT_READ|PROT_EXEC) = 0

mprotect(0x40000000, 20881, PROT_READ|PROT_EXEC) = 0

personality(PER_LINUX) = 0

geteuid() = 501

getuid() = 501

getgid() = 100

getegid() = 100

fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(3, 10), ...}) = 0

mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,\

-1, 0) = 0x40008000

ioctl(1, TCGETS, {B9600 opost isig icanon echo ....}) = 0

write(1, "Hello World!\n", 13Hello World!

) = 13

_exit(0) = ?

papaya$

This may be much more than you expected to see from a simple program. Let's walk through it, briefly, to explain what's going on.

The first call, execve, starts the program. All the mmap, mprotect, and munmap calls come from the kernel's memory management and are not really interesting here. In the three consecutive open calls, the loader is looking for the C library and finds it on the third try. The library header is then read and the library mapped into memory. After a few more memory-management

Return Main Page Previous Page Next Page

®Online Book Reader