1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
29 #if defined(inhibit_libc)
32 void __gcov_flush (void) {}
36 void __gcov_reset (void) {}
40 void __gcov_dump (void) {}
45 extern void gcov_clear (void) ATTRIBUTE_HIDDEN
;
46 extern void gcov_exit (void) ATTRIBUTE_HIDDEN
;
47 extern void set_gcov_dump_complete (void) ATTRIBUTE_HIDDEN
;
48 extern void reset_gcov_dump_complete (void) ATTRIBUTE_HIDDEN
;
52 #ifdef __GTHREAD_MUTEX_INIT
53 ATTRIBUTE_HIDDEN __gthread_mutex_t __gcov_flush_mx
= __GTHREAD_MUTEX_INIT
;
54 #define init_mx_once()
56 __gthread_mutex_t __gcov_flush_mx ATTRIBUTE_HIDDEN
;
61 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
66 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
67 __gthread_once (&once
, init_mx
);
71 /* Called before fork or exec - write out profile information gathered so
72 far and reset it to zero. This avoids duplication or loss of the
73 profile information gathered so far. */
79 __gthread_mutex_lock (&__gcov_flush_mx
);
84 __gthread_mutex_unlock (&__gcov_flush_mx
);
87 #endif /* L_gcov_flush */
91 /* Function that can be called from application to reset counters to zero,
92 in order to collect profile in region of interest. */
98 /* Re-enable dumping to support collecting profile in multiple regions
100 reset_gcov_dump_complete ();
103 #endif /* L_gcov_reset */
107 /* Function that can be called from application to write profile collected
108 so far, in order to collect profile in region of interest. */
114 /* Prevent profile from being dumped a second time on application exit. */
115 set_gcov_dump_complete ();
118 /* Emitted in coverage.c. */
119 extern gcov_unsigned_t __gcov_test_coverage
;
121 unsigned int __gcov_profiling_for_test_coverage (void);
123 /* Function that can be called from application to distinguish binaries
124 instrumented for coverage from those instrumented for profile
125 optimization (e.g. -fprofile-generate). */
127 unsigned int __gcov_profiling_for_test_coverage (void)
129 return __gcov_test_coverage
;
132 typedef void (*gcov_dumper_type
) (void);
135 gcov_dumper_type dumper
;
136 struct dumper_entry
*next_dumper
;
139 static struct dumper_entry this_dumper
= {&__gcov_dump
, 0};
141 /* global dumper list with default visibilty. */
142 struct dumper_entry
*__gcov_dumper_list
;
144 #ifdef __GTHREAD_MUTEX_INIT
145 __gthread_mutex_t __gcov_dump_mx
= __GTHREAD_MUTEX_INIT
;
146 #define init_mx_once()
148 __gthread_mutex_t __gcov_dump_mx
;
153 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_dump_mx
);
158 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
159 __gthread_once (&once
, init_mx
);
163 /* Register the library private __gcov_dump method
164 to the global list. */
166 __attribute__((constructor
))
168 register_dumper (void)
171 __gthread_mutex_lock (&__gcov_dump_mx
);
172 this_dumper
.next_dumper
= __gcov_dumper_list
;
173 __gcov_dumper_list
= &this_dumper
;
174 __gthread_mutex_unlock (&__gcov_dump_mx
);
177 __attribute__((destructor
))
179 unregister_dumper (void)
181 struct dumper_entry
*dumper
;
182 struct dumper_entry
*prev_dumper
= 0;
185 __gthread_mutex_lock (&__gcov_dump_mx
);
186 dumper
= __gcov_dumper_list
;
190 if (dumper
->dumper
== &__gcov_dump
)
193 prev_dumper
->next_dumper
= dumper
->next_dumper
;
195 __gcov_dumper_list
= dumper
->next_dumper
;
198 prev_dumper
= dumper
;
199 dumper
= dumper
->next_dumper
;
201 __gthread_mutex_unlock (&__gcov_dump_mx
);
204 /* Public interface to dump profile data for all shared libraries
205 via registered dumpers from the libraries. This interface
206 has default visibility (unlike gcov_dump which has hidden
210 __gcov_dump_all (void)
212 struct dumper_entry
*dumper
;
215 __gthread_mutex_lock (&__gcov_dump_mx
);
217 dumper
= __gcov_dumper_list
;
221 dumper
= dumper
->next_dumper
;
223 __gthread_mutex_unlock (&__gcov_dump_mx
);
226 #endif /* L_gcov_dump */
228 #ifdef L_gcov_sampling
230 /* Emitted in coverage.c. */
232 /* Sampling period. */
233 extern gcov_unsigned_t __gcov_sampling_period
;
234 extern gcov_unsigned_t __gcov_has_sampling
;
235 void __gcov_set_sampling_period (unsigned int period
);
236 unsigned int __gcov_sampling_enabled ();
237 /* Per thread sample counter. */
238 __thread gcov_unsigned_t __gcov_sample_counter
= 0;
240 /* Set sampling period to PERIOD. */
242 void __gcov_set_sampling_period (unsigned int period
)
244 gcc_assert (__gcov_has_sampling
);
245 __gcov_sampling_period
= period
;
248 unsigned int __gcov_sampling_enabled ()
250 return __gcov_has_sampling
;
257 /* Profile directory prefix specified to -fprofile-generate=. */
258 extern char * __gcov_profile_prefix
;
260 char *__gcov_get_profile_prefix ()
262 return __gcov_profile_prefix
;
269 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
270 that they are not counted twice. */
276 extern __gthread_mutex_t __gcov_flush_mx
;
280 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
286 /* A wrapper for the execl function. Flushes the accumulated profiling data, so
287 that they are not lost. */
290 __gcov_execl (const char *path
, char *arg
, ...)
302 while (va_arg (ap
, char *))
306 args
= (char **) alloca (length
* sizeof (void *));
308 for (i
= 1; i
< length
; i
++)
309 args
[i
] = va_arg (aq
, char *);
312 return execv (path
, args
);
317 /* A wrapper for the execlp function. Flushes the accumulated profiling data, so
318 that they are not lost. */
321 __gcov_execlp (const char *path
, char *arg
, ...)
333 while (va_arg (ap
, char *))
337 args
= (char **) alloca (length
* sizeof (void *));
339 for (i
= 1; i
< length
; i
++)
340 args
[i
] = va_arg (aq
, char *);
343 return execvp (path
, args
);
348 /* A wrapper for the execle function. Flushes the accumulated profiling data, so
349 that they are not lost. */
352 __gcov_execle (const char *path
, char *arg
, ...)
365 while (va_arg (ap
, char *))
369 args
= (char **) alloca (length
* sizeof (void *));
371 for (i
= 1; i
< length
; i
++)
372 args
[i
] = va_arg (aq
, char *);
373 envp
= va_arg (aq
, char **);
376 return execve (path
, args
, envp
);
381 /* A wrapper for the execv function. Flushes the accumulated profiling data, so
382 that they are not lost. */
385 __gcov_execv (const char *path
, char *const argv
[])
388 return execv (path
, argv
);
393 /* A wrapper for the execvp function. Flushes the accumulated profiling data, so
394 that they are not lost. */
397 __gcov_execvp (const char *path
, char *const argv
[])
400 return execvp (path
, argv
);
405 /* A wrapper for the execve function. Flushes the accumulated profiling data, so
406 that they are not lost. */
409 __gcov_execve (const char *path
, char *const argv
[], char *const envp
[])
412 return execve (path
, argv
, envp
);
415 #endif /* inhibit_libc */