2013-11-13 01:24:49 +01:00
|
|
|
/* Routines required for instrumenting a program. */
|
|
|
|
/* Compile this one with gcc. */
|
2021-01-04 10:26:59 +01:00
|
|
|
/* Copyright (C) 1989-2021 Free Software Foundation, Inc.
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
This file is part of GCC.
|
|
|
|
|
|
|
|
GCC is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
2014-01-08 17:37:08 +01:00
|
|
|
#include "libgcov.h"
|
2013-11-13 01:24:49 +01:00
|
|
|
#include "gthr.h"
|
|
|
|
|
|
|
|
#if defined(inhibit_libc)
|
|
|
|
|
|
|
|
#ifdef L_gcov_reset
|
|
|
|
void __gcov_reset (void) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_dump
|
|
|
|
void __gcov_dump (void) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-05-05 16:15:47 +02:00
|
|
|
extern __gthread_mutex_t __gcov_mx ATTRIBUTE_HIDDEN;
|
2013-11-13 01:24:49 +01:00
|
|
|
|
2020-05-05 16:15:45 +02:00
|
|
|
#ifdef L_gcov_lock_unlock
|
2013-11-13 01:24:49 +01:00
|
|
|
#ifdef __GTHREAD_MUTEX_INIT
|
2020-05-05 16:15:47 +02:00
|
|
|
__gthread_mutex_t __gcov_mx = __GTHREAD_MUTEX_INIT;
|
2013-11-13 01:24:49 +01:00
|
|
|
#define init_mx_once()
|
|
|
|
#else
|
2020-05-05 16:15:47 +02:00
|
|
|
__gthread_mutex_t __gcov_mx;
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_mx (void)
|
|
|
|
{
|
2020-05-05 16:15:47 +02:00
|
|
|
__GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_mx);
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
2014-08-01 12:05:42 +02:00
|
|
|
|
2013-11-13 01:24:49 +01:00
|
|
|
static void
|
|
|
|
init_mx_once (void)
|
|
|
|
{
|
|
|
|
static __gthread_once_t once = __GTHREAD_ONCE_INIT;
|
|
|
|
__gthread_once (&once, init_mx);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-05 16:15:45 +02:00
|
|
|
/* Lock critical section for __gcov_dump and __gcov_reset functions. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__gcov_lock (void)
|
|
|
|
{
|
|
|
|
init_mx_once ();
|
2020-05-05 16:15:47 +02:00
|
|
|
__gthread_mutex_lock (&__gcov_mx);
|
2020-05-05 16:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock critical section for __gcov_dump and __gcov_reset functions. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__gcov_unlock (void)
|
|
|
|
{
|
2020-05-05 16:15:47 +02:00
|
|
|
__gthread_mutex_unlock (&__gcov_mx);
|
2020-05-05 16:15:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-13 01:24:49 +01:00
|
|
|
#ifdef L_gcov_reset
|
|
|
|
|
2014-08-07 20:02:06 +02:00
|
|
|
/* Reset all counters to zero. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gcov_clear (const struct gcov_info *list)
|
|
|
|
{
|
|
|
|
const struct gcov_info *gi_ptr;
|
|
|
|
|
|
|
|
for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
|
|
|
|
{
|
|
|
|
unsigned f_ix;
|
|
|
|
|
|
|
|
for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
|
|
|
|
{
|
|
|
|
unsigned t_ix;
|
|
|
|
const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
|
|
|
|
|
|
|
|
if (!gfi_ptr || gfi_ptr->key != gi_ptr)
|
|
|
|
continue;
|
|
|
|
const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs;
|
|
|
|
for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++)
|
|
|
|
{
|
|
|
|
if (!gi_ptr->merge[t_ix])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
|
|
|
|
ci_ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 01:24:49 +01:00
|
|
|
/* Function that can be called from application to reset counters to zero,
|
|
|
|
in order to collect profile in region of interest. */
|
|
|
|
|
|
|
|
void
|
2014-09-03 02:43:00 +02:00
|
|
|
__gcov_reset_int (void)
|
2013-11-13 01:24:49 +01:00
|
|
|
{
|
2014-09-17 22:13:17 +02:00
|
|
|
struct gcov_root *root;
|
|
|
|
|
|
|
|
/* If we're compatible with the master, iterate over everything,
|
|
|
|
otherise just do us. */
|
|
|
|
for (root = __gcov_master.version == GCOV_VERSION
|
|
|
|
? __gcov_master.root : &__gcov_root; root; root = root->next)
|
|
|
|
{
|
|
|
|
gcov_clear (root->list);
|
|
|
|
root->dumped = 0;
|
|
|
|
}
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:15:45 +02:00
|
|
|
/* Exported function __gcov_reset. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__gcov_reset (void)
|
|
|
|
{
|
|
|
|
__gcov_lock ();
|
|
|
|
|
|
|
|
__gcov_reset_int ();
|
|
|
|
|
|
|
|
__gcov_unlock ();
|
|
|
|
}
|
2014-09-03 02:43:00 +02:00
|
|
|
|
2013-11-13 01:24:49 +01:00
|
|
|
#endif /* L_gcov_reset */
|
|
|
|
|
|
|
|
#ifdef L_gcov_dump
|
|
|
|
/* Function that can be called from application to write profile collected
|
|
|
|
so far, in order to collect profile in region of interest. */
|
|
|
|
|
|
|
|
void
|
2014-09-17 22:13:17 +02:00
|
|
|
__gcov_dump_int (void)
|
2013-11-13 01:24:49 +01:00
|
|
|
{
|
2014-09-17 22:13:17 +02:00
|
|
|
struct gcov_root *root;
|
|
|
|
|
|
|
|
/* If we're compatible with the master, iterate over everything,
|
|
|
|
otherise just do us. */
|
|
|
|
for (root = __gcov_master.version == GCOV_VERSION
|
|
|
|
? __gcov_master.root : &__gcov_root; root; root = root->next)
|
|
|
|
__gcov_dump_one (root);
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:15:45 +02:00
|
|
|
/* Exported function __gcov_dump. */
|
|
|
|
|
|
|
|
void
|
|
|
|
__gcov_dump (void)
|
|
|
|
{
|
|
|
|
__gcov_lock ();
|
|
|
|
|
|
|
|
__gcov_dump_int ();
|
|
|
|
|
|
|
|
__gcov_unlock ();
|
|
|
|
}
|
2014-09-17 22:13:17 +02:00
|
|
|
|
2013-11-13 01:24:49 +01:00
|
|
|
#endif /* L_gcov_dump */
|
|
|
|
|
|
|
|
#ifdef L_gcov_fork
|
2020-05-05 16:15:46 +02:00
|
|
|
/* A wrapper for the fork function. We reset counters in the child
|
|
|
|
so that they are not counted twice. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
pid_t
|
|
|
|
__gcov_fork (void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork ();
|
|
|
|
if (pid == 0)
|
2020-05-05 16:15:46 +02:00
|
|
|
{
|
2020-05-05 16:15:47 +02:00
|
|
|
__GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_mx);
|
2020-05-05 16:15:46 +02:00
|
|
|
/* We do not need locking as we are the only thread in the child. */
|
|
|
|
__gcov_reset_int ();
|
|
|
|
}
|
2013-11-13 01:24:49 +01:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execl
|
2014-09-17 22:13:17 +02:00
|
|
|
/* A wrapper for the execl function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execl (const char *path, char *arg, ...)
|
|
|
|
{
|
|
|
|
va_list ap, aq;
|
|
|
|
unsigned i, length;
|
|
|
|
char **args;
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
va_start (ap, arg);
|
|
|
|
va_copy (aq, ap);
|
|
|
|
|
|
|
|
length = 2;
|
|
|
|
while (va_arg (ap, char *))
|
|
|
|
length++;
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
args = (char **) alloca (length * sizeof (void *));
|
|
|
|
args[0] = arg;
|
|
|
|
for (i = 1; i < length; i++)
|
|
|
|
args[i] = va_arg (aq, char *);
|
|
|
|
va_end (aq);
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
int ret = execv (path, args);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execlp
|
2014-08-01 12:05:42 +02:00
|
|
|
/* A wrapper for the execlp function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execlp (const char *path, char *arg, ...)
|
|
|
|
{
|
|
|
|
va_list ap, aq;
|
|
|
|
unsigned i, length;
|
|
|
|
char **args;
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
va_start (ap, arg);
|
|
|
|
va_copy (aq, ap);
|
|
|
|
|
|
|
|
length = 2;
|
|
|
|
while (va_arg (ap, char *))
|
|
|
|
length++;
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
args = (char **) alloca (length * sizeof (void *));
|
|
|
|
args[0] = arg;
|
|
|
|
for (i = 1; i < length; i++)
|
|
|
|
args[i] = va_arg (aq, char *);
|
|
|
|
va_end (aq);
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
int ret = execvp (path, args);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execle
|
2014-08-01 12:05:42 +02:00
|
|
|
/* A wrapper for the execle function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execle (const char *path, char *arg, ...)
|
|
|
|
{
|
|
|
|
va_list ap, aq;
|
|
|
|
unsigned i, length;
|
|
|
|
char **args;
|
|
|
|
char **envp;
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
va_start (ap, arg);
|
|
|
|
va_copy (aq, ap);
|
|
|
|
|
|
|
|
length = 2;
|
|
|
|
while (va_arg (ap, char *))
|
|
|
|
length++;
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
args = (char **) alloca (length * sizeof (void *));
|
|
|
|
args[0] = arg;
|
|
|
|
for (i = 1; i < length; i++)
|
|
|
|
args[i] = va_arg (aq, char *);
|
|
|
|
envp = va_arg (aq, char **);
|
|
|
|
va_end (aq);
|
|
|
|
|
2020-05-05 16:15:46 +02:00
|
|
|
int ret = execve (path, args, envp);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execv
|
2014-08-01 12:05:42 +02:00
|
|
|
/* A wrapper for the execv function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execv (const char *path, char *const argv[])
|
|
|
|
{
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
|
|
|
int ret = execv (path, argv);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execvp
|
2014-08-01 12:05:42 +02:00
|
|
|
/* A wrapper for the execvp function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execvp (const char *path, char *const argv[])
|
|
|
|
{
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
|
|
|
int ret = execvp (path, argv);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef L_gcov_execve
|
2014-08-01 12:05:42 +02:00
|
|
|
/* A wrapper for the execve function. Flushes the accumulated
|
|
|
|
profiling data, so that they are not lost. */
|
2013-11-13 01:24:49 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
__gcov_execve (const char *path, char *const argv[], char *const envp[])
|
|
|
|
{
|
2020-05-05 16:15:46 +02:00
|
|
|
/* Dump counters only, they will be lost after exec. */
|
|
|
|
__gcov_dump ();
|
|
|
|
int ret = execve (path, argv, envp);
|
|
|
|
/* We reach this code only when execv fails, reset counter then here. */
|
|
|
|
__gcov_reset ();
|
|
|
|
return ret;
|
2013-11-13 01:24:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* inhibit_libc */
|