]> The Tcpdump Group git mirrors - tcpdump/blob - instrument-functions.c
gre: add support for MikroTik Ethernet-over-IP hack.
[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 <unistd.h>
18 #include <bfd.h>
19
20 /*
21 * Generate instrumentation calls for entry and exit to functions.
22 * Just after function entry and just before function exit, the
23 * following profiling functions are called with the address of the
24 * current function and its call site (currently not use).
25 *
26 * The attribute 'no_instrument_function' causes this instrumentation is
27 * not done.
28 *
29 * These profiling functions call print_debug(). This function prints the
30 * current function name with indentation and call level.
31 * If entering in a function it prints also the calling function name with
32 * file name and line number.
33 *
34 * If the environment variable INSTRUMENT is
35 * unset or set to an empty string, print nothing, like with no instrumentation
36 * set to "all" or "a", print all the functions names
37 * set to "global" or "g", print only the global functions names
38 */
39
40 #define ND_NO_INSTRUMENT __attribute__((no_instrument_function))
41
42 /* Store the function call level, used also in pretty_print_packet() */
43 extern int profile_func_level;
44 int profile_func_level = -1;
45
46 typedef enum {
47 ENTER,
48 EXIT
49 } action_type;
50
51 void __cyg_profile_func_enter(void *this_fn, void *call_site) ND_NO_INSTRUMENT;
52
53 void __cyg_profile_func_exit(void *this_fn, void *call_site) ND_NO_INSTRUMENT;
54
55 static void print_debug(void *this_fn, void *call_site, action_type action)
56 ND_NO_INSTRUMENT;
57
58 void
59 __cyg_profile_func_enter(void *this_fn, void *call_site)
60 {
61 print_debug(this_fn, call_site, ENTER);
62 }
63
64 void
65 __cyg_profile_func_exit(void *this_fn, void *call_site)
66 {
67 print_debug(this_fn, call_site, EXIT);
68 }
69
70 static void print_debug(void *this_fn, void *call_site, action_type action)
71 {
72 static bfd* abfd;
73 static asymbol **symtab;
74 static long symcount;
75 static asection *text;
76 static bfd_vma vma;
77 static int instrument_set;
78 static int instrument_off;
79 static int instrument_global;
80 int i;
81
82 if (!instrument_set) {
83 static char *instrument_type;
84
85 /* Get the configuration environment variable INSTRUMENT value if any */
86 instrument_type = getenv("INSTRUMENT");
87 /* unset or set to an empty string ? */
88 if (instrument_type == NULL ||
89 !strncmp(instrument_type, "", sizeof(""))) {
90 instrument_off = 1;
91 } else {
92 /* set to "global" or "g" ? */
93 if (!strncmp(instrument_type, "global", sizeof("global")) ||
94 !strncmp(instrument_type, "g", sizeof("g")))
95 instrument_global = 1;
96 else if (strncmp(instrument_type, "all", sizeof("all")) &&
97 strncmp(instrument_type, "a", sizeof("a"))) {
98 fprintf(stderr, "INSTRUMENT can be only \"\", \"all\", \"a\", "
99 "\"global\" or \"g\".\n");
100 exit(1);
101 }
102 }
103 instrument_set = 1;
104 }
105
106 if (instrument_off)
107 return;
108
109 /* If no errors, this block should be executed one time */
110 if (!abfd) {
111 char pgm_name[1024];
112 long symsize;
113
114 ssize_t ret = readlink("/proc/self/exe", pgm_name, sizeof(pgm_name));
115 if (ret == -1) {
116 perror("failed to find executable");
117 return;
118 }
119 if (ret == sizeof(pgm_name)) {
120 /* no space for the '\0' */
121 printf("truncation may have occurred\n");
122 return;
123 }
124 pgm_name[ret] = '\0';
125
126 bfd_init();
127
128 abfd = bfd_openr(pgm_name, NULL);
129 if (!abfd) {
130 bfd_perror("bfd_openr");
131 return;
132 }
133
134 if (!bfd_check_format(abfd, bfd_object)) {
135 bfd_perror("bfd_check_format");
136 return;
137 }
138
139 if((symsize = bfd_get_symtab_upper_bound(abfd)) == -1) {
140 bfd_perror("bfd_get_symtab_upper_bound");
141 return;
142 }
143
144 symtab = (asymbol **)malloc(symsize);
145 symcount = bfd_canonicalize_symtab(abfd, symtab);
146 if (symcount < 0) {
147 free(symtab);
148 bfd_perror("bfd_canonicalize_symtab");
149 return;
150 }
151
152 if ((text = bfd_get_section_by_name(abfd, ".text")) == NULL) {
153 bfd_perror("bfd_get_section_by_name");
154 return;
155 }
156 vma = text->vma;
157 }
158
159 if (instrument_global) {
160 symbol_info syminfo;
161 int found;
162
163 i = 0;
164 found = 0;
165 while (i < symcount && !found) {
166 bfd_get_symbol_info(abfd, symtab[i], &syminfo);
167 if ((void *)syminfo.value == this_fn) {
168 found = 1;
169 }
170 i++;
171 }
172 /* type == 'T' for a global function */
173 if (found == 1 && syminfo.type != 'T')
174 return;
175 }
176
177 /* Current function */
178 if ((bfd_vma)this_fn < vma) {
179 printf("[ERROR address this_fn]");
180 } else {
181 const char *file;
182 const char *func;
183 unsigned int line;
184
185 if (!bfd_find_nearest_line(abfd, text, symtab, (bfd_vma)this_fn - vma,
186 &file, &func, &line)) {
187 printf("[ERROR bfd_find_nearest_line this_fn]");
188 } else {
189 if (action == ENTER)
190 profile_func_level += 1;
191 /* Indentation */
192 for (i = 0 ; i < profile_func_level ; i++)
193 putchar(' ');
194 if (action == ENTER)
195 printf("[>> ");
196 else
197 printf("[<< ");
198 /* Function name */
199 if (func == NULL || *func == '\0')
200 printf("???");
201 else
202 printf("%s", func);
203 printf(" (%d)", profile_func_level);
204 /* Print the "from" part except for the main function) */
205 if (action == ENTER && func != NULL &&
206 strncmp(func, "main", sizeof("main"))) {
207 /* Calling function */
208 if ((bfd_vma)call_site < vma) {
209 printf("[ERROR address call_site]");
210 } else {
211 if (!bfd_find_nearest_line(abfd, text, symtab,
212 (bfd_vma)call_site - vma, &file,
213 &func, &line)) {
214 printf("[ERROR bfd_find_nearest_line call_site]");
215 } else {
216 printf(" from ");
217 /* Function name */
218 if (func == NULL || *func == '\0')
219 printf("???");
220 else
221 printf("%s", func);
222 /* File name */
223 if (file == NULL || *file == '\0')
224 printf(" ??:");
225 else {
226 char *slashp = strrchr(file, '/');
227 if (slashp != NULL)
228 file = slashp + 1;
229 printf(" %s:", file);
230 }
231 /* Line number */
232 if (line == 0)
233 printf("?");
234 else
235 printf("%u", line);
236 printf("]");
237 }
238 }
239 }
240 putchar('\n');
241 if (action == EXIT)
242 profile_func_level -= 1;
243 }
244 }
245 fflush(stdout);
246 }
247
248 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */