]> The Tcpdump Group git mirrors - tcpdump/blob - instrument-functions.c
fb2314dd3f02c25561e9bca7b342b76192d10f37
[tcpdump] / instrument-functions.c
1 /*
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.
12 */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <bfd.h>
21
22 /*
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).
27 *
28 * The attribute 'no_instrument_function' causes this instrumentation is
29 * not done.
30 *
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.
35 *
36 * To configure the printing of only the global functions names:
37 * $ make instrument_global
38 *
39 * To go back to print all the functions names:
40 * $ make instrument_all
41 *
42 * To print nothing, like with no instrumentation:
43 * $ make instrument_off
44 */
45
46 #define ND_NO_INSTRUMENT __attribute__((no_instrument_function))
47
48 /* Store the function call level, used also in pretty_print_packet() */
49 extern int profile_func_level;
50 int profile_func_level = -1;
51
52 typedef enum {
53 ENTER,
54 EXIT
55 } action_type;
56
57 void __cyg_profile_func_enter(void *this_fn, void *call_site) ND_NO_INSTRUMENT;
58
59 void __cyg_profile_func_exit(void *this_fn, void *call_site) ND_NO_INSTRUMENT;
60
61 static void print_debug(void *this_fn, void *call_site, action_type action)
62 ND_NO_INSTRUMENT;
63
64 void
65 __cyg_profile_func_enter(void *this_fn, void *call_site)
66 {
67 print_debug(this_fn, call_site, ENTER);
68 }
69
70 void
71 __cyg_profile_func_exit(void *this_fn, void *call_site)
72 {
73 print_debug(this_fn, call_site, EXIT);
74 }
75
76 /* If this file exists, print only the global functions */
77 #define ND_FILE_FLAG_GLOBAL "instrument_functions_global.devel"
78
79 /* If this file exists, print nothing, like with no instrumentation */
80 #define ND_FILE_FLAG_OFF "instrument_functions_off.devel"
81
82 static void print_debug(void *this_fn, void *call_site, action_type action)
83 {
84 static bfd* abfd;
85 static asymbol **symtab;
86 static long symcount;
87 static asection *text;
88 static bfd_vma vma;
89 static int instrument_off;
90 static int print_only_global;
91 symbol_info syminfo;
92 struct stat statbuf;
93 int i;
94
95 if (!instrument_off) {
96 /* one-time test */
97 if (!stat(ND_FILE_FLAG_OFF, &statbuf)) {
98 instrument_off = 1;
99 return;
100 }
101 } else
102 return;
103
104 /* If no errors, this block should be executed one time */
105 if (!abfd) {
106 char pgm_name[1024];
107 long symsize;
108
109 if (!stat(ND_FILE_FLAG_GLOBAL, &statbuf))
110 print_only_global = 1;
111
112 ssize_t ret = readlink("/proc/self/exe", pgm_name, sizeof(pgm_name));
113 if (ret == -1) {
114 perror("failed to find executable\n");
115 return;
116 }
117 pgm_name[ret] = 0;
118
119 bfd_init();
120
121 abfd = bfd_openr(pgm_name, NULL);
122 if (!abfd) {
123 bfd_perror("bfd_openr");
124 return;
125 }
126
127 if (!bfd_check_format(abfd, bfd_object)) {
128 bfd_perror("bfd_check_format");
129 return;
130 }
131
132 if((symsize = bfd_get_symtab_upper_bound(abfd)) == -1) {
133 bfd_perror("bfd_get_symtab_upper_bound");
134 return;
135 }
136
137 symtab = (asymbol **)malloc(symsize);
138 symcount = bfd_canonicalize_symtab(abfd, symtab);
139 if (symcount < 0) {
140 free (symtab);
141 bfd_perror ("bfd_canonicalize_symtab");
142 return;
143 }
144
145 if ((text = bfd_get_section_by_name(abfd, ".text")) == NULL) {
146 bfd_perror("bfd_get_section_by_name");
147 return;
148 }
149 vma = text->vma;
150 }
151
152 if (print_only_global) {
153 int found;
154
155 i = 0;
156 found = 0;
157 while (i < symcount && !found) {
158 bfd_get_symbol_info(abfd, symtab[i], &syminfo);
159 if ((void *)syminfo.value == this_fn) {
160 found = 1;
161 }
162 i++;
163 }
164 /* type == 'T' for a global function */
165 if (found == 1 && syminfo.type != 'T')
166 return;
167 }
168
169 /* Current function */
170 if ((bfd_vma)this_fn < vma) {
171 printf("[ERROR address this_fn]");
172 } else {
173 const char *file;
174 const char *func;
175 unsigned int line;
176
177 if (!bfd_find_nearest_line(abfd, text, symtab, (bfd_vma)this_fn - vma,
178 &file, &func, &line)) {
179 printf("[ERROR bfd_find_nearest_line this_fn]");
180 } else {
181 if (action == ENTER)
182 profile_func_level += 1;
183 /* Indentation */
184 for (i = 0 ; i < profile_func_level ; i++)
185 putchar(' ');
186 if (action == ENTER)
187 printf("[>> ");
188 else
189 printf("[<< ");
190 /* Function name */
191 if (func == NULL || *func == '\0')
192 printf("???");
193 else
194 printf("%s", func);
195 printf(" (%d)", profile_func_level);
196 /* Print the "from" part except for the main function) */
197 if (action == ENTER && strncmp(func, "main", sizeof("main"))) {
198 /* Calling function */
199 if ((bfd_vma)call_site < vma) {
200 printf("[ERROR address call_site]");
201 } else {
202 if (!bfd_find_nearest_line(abfd, text, symtab,
203 (bfd_vma)call_site - vma, &file,
204 &func, &line)) {
205 printf("[ERROR bfd_find_nearest_line call_site]");
206 } else {
207 printf(" from ");
208 /* Function name */
209 if (func == NULL || *func == '\0')
210 printf("???");
211 else
212 printf("%s", func);
213 /* File name */
214 if (file == NULL || *file == '\0')
215 printf(" ??:");
216 else {
217 char *slashp = strrchr(file, '/');
218 if (slashp != NULL)
219 file = slashp + 1;
220 printf(" %s:", file);
221 }
222 /* Line number */
223 if (line == 0)
224 printf("?");
225 else
226 printf("%u", line);
227 printf("]");
228 }
229 }
230 }
231 putchar('\n');
232 if (action == EXIT)
233 profile_func_level -= 1;
234 }
235 }
236 fflush(stdout);
237 }
238
239 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */