Книга: Embedded Linux Primer: A Practical, Real-World Approach

13.4.3. ltrace

13.4.3. ltrace

The ltrace and strace utilities are closely related. The ltrace utility does for library calls what strace does for system calls. It is invoked in a similar fashion: Precede the program to be traced by the tracer utility, as follows:

$ ltrace ./example

Listing 13-7 reproduces the output of ltrace on a small example program that executes a handful of standard C library calls.

Listing 13-7. Example ltrace Output

$ ltrace ./example
__libc_start_main(0x8048594, 1, 0xbffff944, 0x80486b4, 0x80486fc <unfinished ...>
malloc(256)                                        = 0x804a008
getenv("HOME")                                     = "/home/chris"
strncpy(0x804a008, "/home", 5)                      = 0x804a008
fopen("foo.txt", "w")                               = 0x804a110
printf("$HOME = %sn", "/home/chris"$HOME =  /home/chris
)             = 20
fprintf(0x804a110, "$HOME = %sn", "/home/chris")   = 20
fclose(0x804a110)                                  = 0
remove("foo.txt")                                  = 0
free(0x804a008)                                    = <void>
+++ exited (status 0) +++
$

For each library call, the name of the call is displayed, along with varying portions of the parameters to the call. Similar to strace, the return value of the library call is then displayed. As with strace, this tool can be used on programs for which source code is not available.

As with strace, a variety of switches affect the behavior of ltrace. You can display the value of the program counter at each library call, which can be helpful in understanding your application's program flow. As with strace, you can use -c to accumulate and report count, error, and time statistics, making a useful simple profiling tool. Listing 13-8 displays the results of our simple example program using the -c option.

Listing 13-8. Profiling Using ltrace

$ ltrace -c ./example
$HOME = /home/chris
% time     seconds  usecs/call     calls      function
------ -----------  ----------- --------- ----------------
24.16    0.000231         231         1 printf
16.53    0.000158         158         1 fclose
16.00    0.000153         153         1 fopen
13.70    0.000131         131         1 malloc
10.67    0.000102         102         1 remove
  9.31    0.000089          89         1 fprintf
  3.35    0.000032          32         1 getenv
  3.14    0.000030          30         1 free
  3.14    0.000030          30         1 strncpy
------ ----------- ----------- --------- ----------------
100.00    0.000956                     9 total

The ltrace tool is available only for programs that have been compiled to use dynamically linked shared library objects. This is the usual default, so unless you explicitly specify -static when compiling, you can use ltrace on the resulting binary. Again similar to strace, you must use an ltrace binary that has been compiled for your target architecture. These utilities are run on the target, not the host development system.

Оглавление книги


Генерация: 0.047. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз