]> The Tcpdump Group git mirrors - tcpdump/blob - instrument-functions.c
instrument functions: Enhance the output
[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 "tcpdump_instrument_functions_global.devel"
78
79 /* If this file exists, print nothing, like with no instrumentation */
80 #define ND_FILE_FLAG_OFF "tcpdump_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, found;
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 i = 0;
147 found = 0;
148 while (i < symcount && !found) {
149 bfd_get_symbol_info(abfd, symtab[i], &syminfo);
150 if ((void *)syminfo.value == this_fn) {
151 found = 1;
152 }
153 i++;
154 }
155 /* type == 'T' for a global function */
156 if (found == 1 && syminfo.type != 'T')
157 return;
158 }
159
160 /* Current function */
161 if ((bfd_vma)this_fn < vma) {
162 printf("[ERROR address this_fn]");
163 } else {
164 const char *file;
165 const char *func;
166 unsigned int line;
167
168 if (!bfd_find_nearest_line(abfd, text, symtab, (bfd_vma)this_fn - vma,
169 &file, &func, &line)) {
170 printf("[ERROR bfd_find_nearest_line this_fn]");
171 } else {
172 if (action == ENTER)
173 profile_func_level += 1;
174 /* Indentation */
175 for (i = 0 ; i < profile_func_level ; i++)
176 putchar(' ');
177 if (action == ENTER)
178 printf("[>> ");
179 else
180 printf("[<< ");
181 /* Function name */
182 if (func == NULL || *func == '\0')
183 printf("???");
184 else
185 printf("%s", func);
186 printf(" (%d)", profile_func_level);
187 /* Print the "from" part except for the main function) */
188 if (action == ENTER && strncmp(func, "main", sizeof("main"))) {
189 /* Calling function */
190 if ((bfd_vma)call_site < vma) {
191 printf("[ERROR address call_site]");
192 } else {
193 if (!bfd_find_nearest_line(abfd, text, symtab,
194 (bfd_vma)call_site - vma, &file,
195 &func, &line)) {
196 printf("[ERROR bfd_find_nearest_line call_site]");
197 } else {
198 printf(" from ");
199 /* Function name */
200 if (func == NULL || *func == '\0')
201 printf("???");
202 else
203 printf("%s", func);
204 /* File name */
205 if (file == NULL || *file == '\0')
206 printf(" ??:");
207 else {
208 char *slashp = strrchr(file, '/');
209 if (slashp != NULL)
210 file = slashp + 1;
211 printf(" %s:", file);
212 }
213 /* Line number */
214 if (line == 0)
215 printf("?");
216 else
217 printf("%u", line);
218 printf("]");
219 }
220 }
221 }
222 putchar('\n');
223 if (action == EXIT)
224 profile_func_level -= 1;
225 }
226 }
227 fflush(stdout);
228 }
229
230 /* vi: set tabstop=4 softtabstop=0 shiftwidth=4 smarttab autoindent : */