Other Forms of diff Utility

Using ltrace and strace Utilities 223 It can read existing C files or take its input from standard input. This utility is not exten- sively used in C software development but may be useful in some cases.

7.6 Using ltrace and strace Utilities

The ltrace program is a tracing utility for library function calls. It runs a program and logs all library function calls by that program. You can also use this utility to log system calls made by a program. The utility can also monitor child processes created by fork or clone system calls. This utility is very useful to quickly trace the failure point of an executable program. The utility may also print the time at which a particular function call or system call is executed with a reso- lution of microseconds. Consider the simple single-line program that prints the string “Hello world” and then exits. Using ltrace with the executable of this program produces the following result. [rootboota ltrace] ltrace -S -tt .a.out 22:21:48.325970 SYS_uname0xbffff3b4 = 0 22:21:48.327037 SYS_brkNULL = 0x080495f8 22:21:48.327511 SYS_mmap0xbffff104, 0xcccccccd, 0x400165f8, 4096, 640 = 0x40017000 22:21:48.328212 SYS_openetcld.so.preload, 0, 010 = -2 22:21:48.329000 SYS_openetcld.so.cache, 0, 00 = 3 22:21:48.329657 SYS_1973, 0xbfffea64, 0, 0xbfffea64, 0 = 0 22:21:48.331719 SYS_mmap0xbfffea34, 0, 0x400165f8, 1, 3 = 0x40018000 22:21:48.332460 SYS_close3 = 0 22:21:48.332908 SYS_openlibi686libc.so.6, 0, 027777765514 = 3 22:21:48.333620 SYS_read3, \177ELF\001\001\001, 1024 = 1024 22:21:48.334256 SYS_1973, 0xbfffeaa4, 3, 0xbfffeaa4, 0 = 0 22:21:48.334917 SYS_mmap0xbfffe994, 0x0012f728, 0x400165f8, 0xbfffe9c0, 5 = 0x4002c000 22:21:48.335584 SYS_mprotect0x40152000, 38696, 0, 0x4002c000, 0x00126000 = 0 22:21:48.336209 SYS_mmap0xbfffe994, 24576, 0x400165f8, 0xbfffe9cc, 3 = 0x40152000 22:21:48.336953 SYS_mmap0xbfffe994, 0xbfffe9cc, 0x400165f8, 0x40158000, 14120 = 0x40158000 22:21:48.337642 SYS_close3 = 0 22:21:48.340431 SYS_munmap0x40018000, 77871 = 0 22:21:48.341060 SYS_getpid = 32540 22:21:48.341562 __libc_start_main0x08048460, 1, 0xbffff88c, 0x080482e4, 0x080484c0 unfinished ... 22:21:48.342232 __register_frame_info0x08049508, 0x080495e0, 0xbffff828, 0x0804838e, 0x080482e4 = 0x401575e0 22:21:48.343064 printfHello world\n unfinished ... 224 Chapter 7 • Miscellaneous Tools 22:21:48.343813 SYS_1971, 0xbfffeff0, 0x401569e4, 0x40154a60, 0x40154a60 = 0 22:21:48.344450 SYS_1920, 4096, 3, 34, -1 = 0x40018000 22:21:48.345154 SYS_ioctl1, 21505, 0xbfffef20, 0xbfffef80, 1024 = 0 22:21:48.345890 SYS_write1, Hello world\n, 12Hello world = 12 22:21:48.346542 ... printf resumed = 12 22:21:48.346878 __deregister_frame_info0x08049508, 0x4000d816, 0x400171ec, 0x40017310, 7 = 0x080495e0 22:21:48.347746 SYS_munmap0x40018000, 4096 = 0 22:21:48.348235 SYS_exit12 = void 22:21:48.348706 +++ exited status 12 +++ [rootboota ltrace] The first column in each row shows the current time followed by a number that shows microseconds. The remaining part of the line shows the system call of the library call used. By comparing the time in two consecutive lines, you can find out the time taken by a particular sys- tem call. By looking at the output, you can also find out if a program fails during a particular system call. This may be especially helpful when testing device drivers and reading or writing to these drivers fails for some reason. The most common options used with this utility are shown in Table 7-4. The command can also read options either from its configuration file etc ltrace.conf or from .ltrace.conf in the home directory of the user. The strace utility is a more comprehensive tool and it can be used to separate different system calls. For example, it can be used to display information about network related system Table 7-4 Common options used with the ltrace utility Option Description -d Debug. Displays extra information about trace. -f Trace child processes. -S Display system calls and library calls. -r Display time difference between successive lines. -tt Display time stamp with each line with a resolution to microseconds. -o filename Record output to a file. -V Display version. -h Display help.