]>
The Tcpdump Group git mirrors - tcpdump/blob - instrument-functions.c
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that: (1) source code
4 * distributions retain the above copyright notice and this paragraph
5 * in its entirety, and (2) distributions including binary code include
6 * the above copyright notice and this paragraph in its entirety in
7 * the documentation or other materials provided with the distribution.
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE.
17 #include <sys/types.h>
23 * Generate instrumentation calls for entry and exit to functions.
24 * Just after function entry and just before function exit, the
25 * following profiling functions are called with the address of the
26 * current function and its call site (currently not use).
28 * The attribute 'no_instrument_function' causes this instrumentation is
31 * These profiling functions call print_debug(). This function prints the
32 * current function name with indentation and call level.
33 * If entering in a function it prints also the calling function name with
34 * file name and line number.
36 * To configure the printing of only the global functions names:
37 * $ make instrument_global
39 * To go back to print all the functions names:
40 * $ make instrument_all
42 * To print nothing, like with no instrumentation:
43 * $ make instrument_off
46 #define ND_NO_INSTRUMENT __attribute__((no_instrument_function))
48 /* Store the function call level, used also in pretty_print_packet() */
49 extern int profile_func_level
;
50 int profile_func_level
= -1;
57 void __cyg_profile_func_enter(void *this_fn
, void *call_site
) ND_NO_INSTRUMENT
;
59 void __cyg_profile_func_exit(void *this_fn
, void *call_site
) ND_NO_INSTRUMENT
;
61 static void print_debug(void *this_fn
, void *call_site
, action_type action
)
65 __cyg_profile_func_enter(void *this_fn
, void *call_site
)
67 print_debug(this_fn
, call_site
, ENTER
);
71 __cyg_profile_func_exit(void *this_fn
, void *call_site
)
73 print_debug(this_fn
, call_site
, EXIT
);
76 /* If this file exists, print only the global functions */
77 #define ND_FILE_FLAG_GLOBAL "instrument_functions_global.devel"
79 /* If this file exists, print nothing, like with no instrumentation */
80 #define ND_FILE_FLAG_OFF "instrument_functions_off.devel"
82 static void print_debug(void *this_fn
, void *call_site
, action_type action
)
85 static asymbol
**symtab
;
87 static asection
*text
;
89 static int print_only_global
;
94 if (!stat(ND_FILE_FLAG_OFF
, &statbuf
))
97 /* If no errors, this block should be executed one time */
102 if (!stat(ND_FILE_FLAG_GLOBAL
, &statbuf
))
103 print_only_global
= 1;
105 ssize_t ret
= readlink("/proc/self/exe", pgm_name
, sizeof(pgm_name
));
107 perror("failed to find executable\n");
114 abfd
= bfd_openr(pgm_name
, NULL
);
116 bfd_perror("bfd_openr");
120 if (!bfd_check_format(abfd
, bfd_object
)) {
121 bfd_perror("bfd_check_format");
125 if((symsize
= bfd_get_symtab_upper_bound(abfd
)) == -1) {
126 bfd_perror("bfd_get_symtab_upper_bound");
130 symtab
= (asymbol
**)malloc(symsize
);
131 symcount
= bfd_canonicalize_symtab(abfd
, symtab
);
134 bfd_perror ("bfd_canonicalize_symtab");
138 if ((text
= bfd_get_section_by_name(abfd
, ".text")) == NULL
) {
139 bfd_perror("bfd_get_section_by_name");
145 if (print_only_global
) {
150 while (i
< symcount
&& !found
) {
151 bfd_get_symbol_info(abfd
, symtab
[i
], &syminfo
);
152 if ((void *)syminfo
.value
== this_fn
) {
157 /* type == 'T' for a global function */
158 if (found
== 1 && syminfo
.type
!= 'T')
162 /* Current function */
163 if ((bfd_vma
)this_fn
< vma
) {
164 printf("[ERROR address this_fn]");
170 if (!bfd_find_nearest_line(abfd
, text
, symtab
, (bfd_vma
)this_fn
- vma
,
171 &file
, &func
, &line
)) {
172 printf("[ERROR bfd_find_nearest_line this_fn]");
175 profile_func_level
+= 1;
177 for (i
= 0 ; i
< profile_func_level
; i
++)
184 if (func
== NULL
|| *func
== '\0')
188 printf(" (%d)", profile_func_level
);
189 /* Print the "from" part except for the main function) */
190 if (action
== ENTER
&& strncmp(func
, "main", sizeof("main"))) {
191 /* Calling function */
192 if ((bfd_vma
)call_site
< vma
) {
193 printf("[ERROR address call_site]");
195 if (!bfd_find_nearest_line(abfd
, text
, symtab
,
196 (bfd_vma
)call_site
- vma
, &file
,
198 printf("[ERROR bfd_find_nearest_line call_site]");
202 if (func
== NULL
|| *func
== '\0')
207 if (file
== NULL
|| *file
== '\0')
210 char *slashp
= strrchr(file
, '/');
213 printf(" %s:", file
);
226 profile_func_level
-= 1;
232 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */