-#ifdef L_bb
-
-struct bb_function_info {
- long checksum;
- int arc_count;
- const char *name;
-};
-
-/* Structure emitted by --profile-arcs */
-struct bb
-{
- long zero_word;
- const char *filename;
- gcov_type *counts;
- long ncounts;
- struct bb *next;
-
- /* Older GCC's did not emit these fields. */
- long sizeof_bb;
- struct bb_function_info *function_infos;
-};
-
-#ifndef inhibit_libc
-
-/* Arc profile dumper. Requires atexit and stdio. */
-
-#undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
-#include <stdio.h>
-
-#include "gcov-io.h"
-#include <string.h>
-#ifdef TARGET_HAS_F_SETLKW
-#include <fcntl.h>
-#include <errno.h>
-#endif
-
-/* Chain of per-object file bb structures. */
-static struct bb *bb_head;
-
-/* Dump the coverage counts. We merge with existing counts when
- possible, to avoid growing the .da files ad infinitum. */
-
-void
-__bb_exit_func (void)
-{
- struct bb *ptr;
- int i;
- gcov_type program_sum = 0;
- gcov_type program_max = 0;
- long program_arcs = 0;
- gcov_type merged_sum = 0;
- gcov_type merged_max = 0;
- long merged_arcs = 0;
-
-#if defined (TARGET_HAS_F_SETLKW)
- struct flock s_flock;
-
- s_flock.l_type = F_WRLCK;
- s_flock.l_whence = SEEK_SET;
- s_flock.l_start = 0;
- s_flock.l_len = 0; /* Until EOF. */
- s_flock.l_pid = getpid ();
-#endif
-
- /* Non-merged stats for this program. */
- for (ptr = bb_head; ptr; ptr = ptr->next)
- {
- for (i = 0; i < ptr->ncounts; i++)
- {
- program_sum += ptr->counts[i];
-
- if (ptr->counts[i] > program_max)
- program_max = ptr->counts[i];
- }
- program_arcs += ptr->ncounts;
- }
-
- for (ptr = bb_head; ptr; ptr = ptr->next)
- {
- FILE *da_file;
- gcov_type object_max = 0;
- gcov_type object_sum = 0;
- long object_functions = 0;
- int merging = 0;
- int error = 0;
- struct bb_function_info *fn_info;
- gcov_type *count_ptr;
-
- /* Open for modification */
- da_file = fopen (ptr->filename, "r+b");
-
- if (da_file)
- merging = 1;
- else
- {
- /* Try for appending */
- da_file = fopen (ptr->filename, "ab");
- /* Some old systems might not allow the 'b' mode modifier.
- Therefore, try to open without it. This can lead to a
- race condition so that when you delete and re-create the
- file, the file might be opened in text mode, but then,
- you shouldn't delete the file in the first place. */
- if (!da_file)
- da_file = fopen (ptr->filename, "a");
- }
-
- if (!da_file)
- {
- fprintf (stderr, "arc profiling: Can't open output file %s.\n",
- ptr->filename);
- ptr->filename = 0;
- continue;
- }
-
-#if defined (TARGET_HAS_F_SETLKW)
- /* After a fork, another process might try to read and/or write
- the same file simultanously. So if we can, lock the file to
- avoid race conditions. */
- while (fcntl (fileno (da_file), F_SETLKW, &s_flock)
- && errno == EINTR)
- continue;
-#endif
- for (fn_info = ptr->function_infos; fn_info->arc_count != -1; fn_info++)
- object_functions++;
-
- if (merging)
- {
- /* Merge data from file. */
- long tmp_long;
- gcov_type tmp_gcov;
-
- if (/* magic */
- (__read_long (&tmp_long, da_file, 4) || tmp_long != -123l)
- /* functions in object file. */
- || (__read_long (&tmp_long, da_file, 4)
- || tmp_long != object_functions)
- /* extension block, skipped */
- || (__read_long (&tmp_long, da_file, 4)
- || fseek (da_file, tmp_long, SEEK_CUR)))
- {
- read_error:;
- fprintf (stderr, "arc profiling: Error merging output file %s.\n",
- ptr->filename);
- clearerr (da_file);
- }
- else
- {
- /* Merge execution counts for each function. */
- count_ptr = ptr->counts;
-
- for (fn_info = ptr->function_infos; fn_info->arc_count != -1;
- fn_info++)
- {
- if (/* function name delim */
- (__read_long (&tmp_long, da_file, 4)
- || tmp_long != -1)
- /* function name length */
- || (__read_long (&tmp_long, da_file, 4)
- || tmp_long != (long) strlen (fn_info->name))
- /* skip string */
- || fseek (da_file, ((tmp_long + 1) + 3) & ~3, SEEK_CUR)
- /* function name delim */
- || (__read_long (&tmp_long, da_file, 4)
- || tmp_long != -1))
- goto read_error;
-
- if (/* function checksum */
- (__read_long (&tmp_long, da_file, 4)
- || tmp_long != fn_info->checksum)
- /* arc count */
- || (__read_long (&tmp_long, da_file, 4)
- || tmp_long != fn_info->arc_count))
- goto read_error;
-
- for (i = fn_info->arc_count; i > 0; i--, count_ptr++)
- if (__read_gcov_type (&tmp_gcov, da_file, 8))
- goto read_error;
- else
- *count_ptr += tmp_gcov;
- }
- }
- fseek (da_file, 0, SEEK_SET);
- }
-
- /* Calculate the per-object statistics. */
- for (i = 0; i < ptr->ncounts; i++)
- {
- object_sum += ptr->counts[i];
-
- if (ptr->counts[i] > object_max)
- object_max = ptr->counts[i];
- }
- merged_sum += object_sum;
- if (merged_max < object_max)
- merged_max = object_max;
- merged_arcs += ptr->ncounts;
-
- /* Write out the data. */
- if (/* magic */
- __write_long (-123, da_file, 4)
- /* number of functions in object file. */
- || __write_long (object_functions, da_file, 4)
- /* length of extra data in bytes. */
- || __write_long ((4 + 8 + 8) + (4 + 8 + 8), da_file, 4)
-
- /* whole program statistics. If merging write per-object
- now, rewrite later */
- /* number of instrumented arcs. */
- || __write_long (merging ? ptr->ncounts : program_arcs, da_file, 4)
- /* sum of counters. */
- || __write_gcov_type (merging ? object_sum : program_sum, da_file, 8)
- /* maximal counter. */
- || __write_gcov_type (merging ? object_max : program_max, da_file, 8)
-
- /* per-object statistics. */
- /* number of counters. */
- || __write_long (ptr->ncounts, da_file, 4)
- /* sum of counters. */
- || __write_gcov_type (object_sum, da_file, 8)
- /* maximal counter. */
- || __write_gcov_type (object_max, da_file, 8))
- {
- write_error:;
- fprintf (stderr, "arc profiling: Error writing output file %s.\n",
- ptr->filename);
- error = 1;
- }
- else
- {
- /* Write execution counts for each function. */
- count_ptr = ptr->counts;
-
- for (fn_info = ptr->function_infos; fn_info->arc_count != -1;
- fn_info++)
- {
- if (__write_gcov_string (fn_info->name,
- strlen (fn_info->name), da_file, -1)
- || __write_long (fn_info->checksum, da_file, 4)
- || __write_long (fn_info->arc_count, da_file, 4))
- goto write_error;
-
- for (i = fn_info->arc_count; i > 0; i--, count_ptr++)
- if (__write_gcov_type (*count_ptr, da_file, 8))
- goto write_error; /* RIP Edsger Dijkstra */
- }
- }
-
- if (fclose (da_file))
- {
- fprintf (stderr, "arc profiling: Error closing output file %s.\n",
- ptr->filename);
- error = 1;
- }
- if (error || !merging)
- ptr->filename = 0;
- }
-
- /* Upate whole program statistics. */
- for (ptr = bb_head; ptr; ptr = ptr->next)
- if (ptr->filename)
- {
- FILE *da_file;
-
- da_file = fopen (ptr->filename, "r+b");
- if (!da_file)
- {
- fprintf (stderr, "arc profiling: Cannot reopen %s.\n",
- ptr->filename);
- continue;
- }
-
-#if defined (TARGET_HAS_F_SETLKW)
- while (fcntl (fileno (da_file), F_SETLKW, &s_flock)
- && errno == EINTR)
- continue;
-#endif
-
- if (fseek (da_file, 4 * 3, SEEK_SET)
- /* number of instrumented arcs. */
- || __write_long (merged_arcs, da_file, 4)
- /* sum of counters. */
- || __write_gcov_type (merged_sum, da_file, 8)
- /* maximal counter. */
- || __write_gcov_type (merged_max, da_file, 8))
- fprintf (stderr, "arc profiling: Error updating program header %s.\n",
- ptr->filename);
- if (fclose (da_file))
- fprintf (stderr, "arc profiling: Error reclosing %s\n",
- ptr->filename);
- }
-}
-
-/* Add a new object file onto the bb chain. Invoked automatically
- when running an object file's global ctors. */
-
-void
-__bb_init_func (struct bb *blocks)
-{
- if (blocks->zero_word)
- return;
-
- /* Initialize destructor and per-thread data. */
- if (!bb_head)
- atexit (__bb_exit_func);
-
- /* Set up linked list. */
- blocks->zero_word = 1;
- blocks->next = bb_head;
- bb_head = blocks;
-}
-
-/* Called before fork or exec - write out profile information gathered so
- far and reset it to zero. This avoids duplication or loss of the
- profile information gathered so far. */
-
-void
-__bb_fork_func (void)
-{
- struct bb *ptr;
-
- __bb_exit_func ();
- for (ptr = bb_head; ptr != (struct bb *) 0; ptr = ptr->next)
- {
- long i;
- for (i = ptr->ncounts - 1; i >= 0; i--)
- ptr->counts[i] = 0;
- }
-}
-
-#endif /* not inhibit_libc */
-#endif /* L_bb */