]> The Tcpdump Group git mirrors - tcpdump/blob - instrument-functions.c
Revert "Clean a bunch of fuzzed files not to fuzz the container."
[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 print_only_global;
90 symbol_info syminfo;
91 struct stat statbuf;
92 int i;
93
94 if (!stat(ND_FILE_FLAG_OFF, &statbuf))
95 return;
96
97 /* If no errors, this block should be executed one time */
98 if (!abfd) {
99 char pgm_name[1024];
100 long symsize;
101
102 if (!stat(ND_FILE_FLAG_GLOBAL, &statbuf))
103 print_only_global = 1;
104
105 ssize_t ret = readlink("/proc/self/exe", pgm_name, sizeof(pgm_name));
106 if (ret == -1) {
107 perror("failed to find executable\n");
108 return;
109 }
110 pgm_name[ret] = 0;
111
112 bfd_init();
113
114 abfd = bfd_openr(pgm_name, NULL);
115 if (!abfd) {
116 bfd_perror("bfd_openr");
117 return;
118 }
119
120 if (!bfd_check_format(abfd, bfd_object)) {
121 bfd_perror("bfd_check_format");
122 return;
123 }
124
125 if((symsize = bfd_get_symtab_upper_bound(abfd)) == -1) {
126 bfd_perror("bfd_get_symtab_upper_bound");
127 return;
128 }
129
130 symtab = (asymbol **)malloc(symsize);
131 symcount = bfd_canonicalize_symtab(abfd, symtab);
132 if (symcount < 0) {
133 free (symtab);
134 bfd_perror ("bfd_canonicalize_symtab");
135 return;
136 }
137
138 if ((text = bfd_get_section_by_name(abfd, ".text")) == NULL) {
139 bfd_perror("bfd_get_section_by_name");
140 return;
141 }
142 vma = text->vma;
143 }
144
145 if (print_only_global) {
146 int found;
147
148 i = 0;
149 found = 0;
150 while (i < symcount && !found) {
151 bfd_get_symbol_info(abfd, symtab[i], &syminfo);
152 if ((void *)syminfo.value == this_fn) {
153 found = 1;
154 }
155 i++;
156 }
157 /* type == 'T' for a global function */
158 if (found == 1 && syminfo.type != 'T')
159 return;
160 }
161
162 /* Current function */
163 if ((bfd_vma)this_fn < vma) {
164 printf("[ERROR address this_fn]");
165 } else {
166 const char *file;
167 const char *func;
168 unsigned int line;
169
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]");
173 } else {
174 if (action == ENTER)
175 profile_func_level += 1;
176 /* Indentation */
177 for (i = 0 ; i < profile_func_level ; i++)
178 putchar(' ');
179 if (action == ENTER)
180 printf("[>> ");
181 else
182 printf("[<< ");
183 /* Function name */
184 if (func == NULL || *func == '\0')
185 printf("???");
186 else
187 printf("%s", func);
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]");
194 } else {
195 if (!bfd_find_nearest_line(abfd, text, symtab,
196 (bfd_vma)call_site - vma, &file,
197 &func, &line)) {
198 printf("[ERROR bfd_find_nearest_line call_site]");
199 } else {
200 printf(" from ");
201 /* Function name */
202 if (func == NULL || *func == '\0')
203 printf("???");
204 else
205 printf("%s", func);
206 /* File name */
207 if (file == NULL || *file == '\0')
208 printf(" ??:");
209 else {
210 char *slashp = strrchr(file, '/');
211 if (slashp != NULL)
212 file = slashp + 1;
213 printf(" %s:", file);
214 }
215 /* Line number */
216 if (line == 0)
217 printf("?");
218 else
219 printf("%u", line);
220 printf("]");
221 }
222 }
223 }
224 putchar('\n');
225 if (action == EXIT)
226 profile_func_level -= 1;
227 }
228 }
229 fflush(stdout);
230 }
231
232 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */