]>
The Tcpdump Group git mirrors - tcpslice/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 instrument_off
;
90 static int print_only_global
;
95 if (!instrument_off
) {
97 if (!stat(ND_FILE_FLAG_OFF
, &statbuf
)) {
104 /* If no errors, this block should be executed one time */
109 if (!stat(ND_FILE_FLAG_GLOBAL
, &statbuf
))
110 print_only_global
= 1;
112 ssize_t ret
= readlink("/proc/self/exe", pgm_name
, sizeof(pgm_name
));
114 perror("failed to find executable\n");
117 if (ret
== sizeof(pgm_name
)) {
118 /* no space for the '\0' */
119 printf("truncation may have occurred\n");
122 pgm_name
[ret
] = '\0';
126 abfd
= bfd_openr(pgm_name
, NULL
);
128 bfd_perror("bfd_openr");
132 if (!bfd_check_format(abfd
, bfd_object
)) {
133 bfd_perror("bfd_check_format");
137 if((symsize
= bfd_get_symtab_upper_bound(abfd
)) == -1) {
138 bfd_perror("bfd_get_symtab_upper_bound");
142 symtab
= (asymbol
**)malloc(symsize
);
143 symcount
= bfd_canonicalize_symtab(abfd
, symtab
);
146 bfd_perror ("bfd_canonicalize_symtab");
150 if ((text
= bfd_get_section_by_name(abfd
, ".text")) == NULL
) {
151 bfd_perror("bfd_get_section_by_name");
157 if (print_only_global
) {
162 while (i
< symcount
&& !found
) {
163 bfd_get_symbol_info(abfd
, symtab
[i
], &syminfo
);
164 if ((void *)syminfo
.value
== this_fn
) {
169 /* type == 'T' for a global function */
170 if (found
== 1 && syminfo
.type
!= 'T')
174 /* Current function */
175 if ((bfd_vma
)this_fn
< vma
) {
176 printf("[ERROR address this_fn]");
182 if (!bfd_find_nearest_line(abfd
, text
, symtab
, (bfd_vma
)this_fn
- vma
,
183 &file
, &func
, &line
)) {
184 printf("[ERROR bfd_find_nearest_line this_fn]");
187 profile_func_level
+= 1;
189 for (i
= 0 ; i
< profile_func_level
; i
++)
196 if (func
== NULL
|| *func
== '\0')
200 printf(" (%d)", profile_func_level
);
201 /* Print the "from" part except for the main function) */
202 if (action
== ENTER
&& strncmp(func
, "main", sizeof("main"))) {
203 /* Calling function */
204 if ((bfd_vma
)call_site
< vma
) {
205 printf("[ERROR address call_site]");
207 if (!bfd_find_nearest_line(abfd
, text
, symtab
,
208 (bfd_vma
)call_site
- vma
, &file
,
210 printf("[ERROR bfd_find_nearest_line call_site]");
214 if (func
== NULL
|| *func
== '\0')
219 if (file
== NULL
|| *file
== '\0')
222 char *slashp
= strrchr(file
, '/');
225 printf(" %s:", file
);
238 profile_func_level
-= 1;
244 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */