1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2018 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)
30 /* If libc and its header files are not available, provide dummy functions. */
33 void __gcov_init (struct gcov_info
*p
__attribute__ ((unused
))) {}
36 #else /* inhibit_libc */
47 /* A utility function for outputting errors. */
48 static int gcov_error (const char *, ...);
51 static void gcov_error_exit (void);
58 struct gcov_fn_buffer
*next
;
60 struct gcov_fn_info info
;
61 /* note gcov_fn_info ends in a trailing array. */
64 struct gcov_summary_buffer
66 struct gcov_summary_buffer
*next
;
67 struct gcov_summary summary
;
70 /* A struct that bundles all the related information about the
75 char *filename
; /* filename buffer */
76 int strip
; /* leading chars to strip from filename */
77 char *prefix
; /* prefix string */
80 static struct gcov_fn_buffer
*
81 free_fn_data (const struct gcov_info
*gi_ptr
, struct gcov_fn_buffer
*buffer
,
84 struct gcov_fn_buffer
*next
;
85 unsigned ix
, n_ctr
= 0;
91 for (ix
= 0; ix
!= limit
; ix
++)
92 if (gi_ptr
->merge
[ix
])
93 free (buffer
->info
.ctrs
[n_ctr
++].values
);
98 static struct gcov_fn_buffer
**
99 buffer_fn_data (const char *filename
, const struct gcov_info
*gi_ptr
,
100 struct gcov_fn_buffer
**end_ptr
, unsigned fn_ix
)
102 unsigned n_ctrs
= 0, ix
= 0;
103 struct gcov_fn_buffer
*fn_buffer
;
106 for (ix
= GCOV_COUNTERS
; ix
--;)
107 if (gi_ptr
->merge
[ix
])
110 len
= sizeof (*fn_buffer
) + sizeof (fn_buffer
->info
.ctrs
[0]) * n_ctrs
;
111 fn_buffer
= (struct gcov_fn_buffer
*) xmalloc (len
);
117 fn_buffer
->fn_ix
= fn_ix
;
118 fn_buffer
->info
.ident
= gcov_read_unsigned ();
119 fn_buffer
->info
.lineno_checksum
= gcov_read_unsigned ();
120 fn_buffer
->info
.cfg_checksum
= gcov_read_unsigned ();
122 for (n_ctrs
= ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
124 gcov_unsigned_t length
;
127 if (!gi_ptr
->merge
[ix
])
130 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix
))
136 length
= GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
137 len
= length
* sizeof (gcov_type
);
138 values
= (gcov_type
*) xmalloc (len
);
142 fn_buffer
->info
.ctrs
[n_ctrs
].num
= length
;
143 fn_buffer
->info
.ctrs
[n_ctrs
].values
= values
;
146 *values
++ = gcov_read_counter ();
150 *end_ptr
= fn_buffer
;
151 return &fn_buffer
->next
;
154 gcov_error ("profiling:%s:Function %u %s %u \n", filename
, fn_ix
,
155 len
? "cannot allocate" : "counter mismatch", len
? len
: ix
);
157 return (struct gcov_fn_buffer
**)free_fn_data (gi_ptr
, fn_buffer
, ix
);
160 /* Check if VERSION of the info block PTR matches libgcov one.
161 Return 1 on success, or zero in case of versions mismatch.
162 If FILENAME is not NULL, its value used for reporting purposes
163 instead of value from the info block. */
166 gcov_version (struct gcov_info
*ptr
, gcov_unsigned_t version
,
167 const char *filename
)
169 if (version
!= GCOV_VERSION
)
173 GCOV_UNSIGNED2STRING (v
, version
);
174 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
176 gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
177 filename
? filename
: ptr
->filename
, e
, v
);
183 /* buffer for the fn_data from another program. */
184 static struct gcov_fn_buffer
*fn_buffer
;
186 /* Including system dependent components. */
187 #include "libgcov-driver-system.c"
189 /* This function merges counters in GI_PTR to an existing gcda file.
191 Return -1 on error. In this case, caller will goto read_fatal. */
194 merge_one_data (const char *filename
,
195 struct gcov_info
*gi_ptr
,
196 struct gcov_summary
*summary
)
198 gcov_unsigned_t tag
, length
;
202 struct gcov_fn_buffer
**fn_tail
= &fn_buffer
;
204 length
= gcov_read_unsigned ();
205 if (!gcov_version (gi_ptr
, length
, filename
))
208 length
= gcov_read_unsigned ();
209 if (length
!= gi_ptr
->stamp
)
211 /* Read from a different compilation. Overwrite the file. */
212 gcov_error ("profiling:%s:overwriting an existing profile data "
213 "with a different timestamp\n", filename
);
217 tag
= gcov_read_unsigned ();
218 if (tag
!= GCOV_TAG_OBJECT_SUMMARY
)
220 length
= gcov_read_unsigned ();
221 gcc_assert (length
> 0);
222 gcov_read_summary (summary
);
224 tag
= gcov_read_unsigned ();
225 /* Merge execution counts for each function. */
226 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
;
227 f_ix
++, tag
= gcov_read_unsigned ())
229 const struct gcov_ctr_info
*ci_ptr
;
230 const struct gcov_fn_info
*gfi_ptr
= gi_ptr
->functions
[f_ix
];
232 if (tag
!= GCOV_TAG_FUNCTION
)
235 length
= gcov_read_unsigned ();
237 /* This function did not appear in the other program.
238 We have nothing to merge. */
241 if (length
!= GCOV_TAG_FUNCTION_LENGTH
)
244 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
246 /* This function appears in the other program. We
247 need to buffer the information in order to write
248 it back out -- we'll be inserting data before
249 this point, so cannot simply keep the data in the
251 fn_tail
= buffer_fn_data (filename
, gi_ptr
, fn_tail
, f_ix
);
257 length
= gcov_read_unsigned ();
258 if (length
!= gfi_ptr
->ident
)
261 length
= gcov_read_unsigned ();
262 if (length
!= gfi_ptr
->lineno_checksum
)
265 length
= gcov_read_unsigned ();
266 if (length
!= gfi_ptr
->cfg_checksum
)
269 ci_ptr
= gfi_ptr
->ctrs
;
270 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
272 gcov_merge_fn merge
= gi_ptr
->merge
[t_ix
];
277 tag
= gcov_read_unsigned ();
278 length
= gcov_read_unsigned ();
279 if (tag
!= GCOV_TAG_FOR_COUNTER (t_ix
)
280 || length
!= GCOV_TAG_COUNTER_LENGTH (ci_ptr
->num
))
282 (*merge
) (ci_ptr
->values
, ci_ptr
->num
);
285 if ((error
= gcov_is_error ()))
292 gcov_error ("profiling:%s:Merge mismatch for %s %u\n",
293 filename
, f_ix
>= 0 ? "function" : "summary",
294 f_ix
< 0 ? -1 - f_ix
: f_ix
);
300 gcov_error ("profiling:%s:%s merging\n", filename
,
301 error
< 0 ? "Overflow": "Error");
305 /* Write counters in GI_PTR and the summary in PRG to a gcda file. In
306 the case of appending to an existing file, SUMMARY_POS will be non-zero.
307 We will write the file starting from SUMMAY_POS. */
310 write_one_data (const struct gcov_info
*gi_ptr
,
311 const struct gcov_summary
*prg_p
)
315 gcov_write_tag_length (GCOV_DATA_MAGIC
, GCOV_VERSION
);
316 gcov_write_unsigned (gi_ptr
->stamp
);
318 /* Generate whole program statistics. */
319 gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY
, prg_p
);
321 /* Write execution counts for each function. */
322 for (f_ix
= 0; f_ix
!= gi_ptr
->n_functions
; f_ix
++)
324 unsigned buffered
= 0;
325 const struct gcov_fn_info
*gfi_ptr
;
326 const struct gcov_ctr_info
*ci_ptr
;
327 gcov_unsigned_t length
;
330 if (fn_buffer
&& fn_buffer
->fn_ix
== f_ix
)
332 /* Buffered data from another program. */
334 gfi_ptr
= &fn_buffer
->info
;
335 length
= GCOV_TAG_FUNCTION_LENGTH
;
339 gfi_ptr
= gi_ptr
->functions
[f_ix
];
340 if (gfi_ptr
&& gfi_ptr
->key
== gi_ptr
)
341 length
= GCOV_TAG_FUNCTION_LENGTH
;
346 gcov_write_tag_length (GCOV_TAG_FUNCTION
, length
);
350 gcov_write_unsigned (gfi_ptr
->ident
);
351 gcov_write_unsigned (gfi_ptr
->lineno_checksum
);
352 gcov_write_unsigned (gfi_ptr
->cfg_checksum
);
354 ci_ptr
= gfi_ptr
->ctrs
;
355 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
357 gcov_unsigned_t n_counts
;
360 if (!gi_ptr
->merge
[t_ix
])
363 n_counts
= ci_ptr
->num
;
364 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
365 GCOV_TAG_COUNTER_LENGTH (n_counts
));
366 c_ptr
= ci_ptr
->values
;
368 gcov_write_counter (*c_ptr
++);
372 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
375 gcov_write_unsigned (0);
378 /* Helper function for merging summary. */
381 merge_summary (int run_counted
, struct gcov_summary
*summary
,
387 summary
->sum_max
+= run_max
;
391 /* Sort N entries in VALUE_ARRAY in descending order.
392 Each entry in VALUE_ARRAY has two values. The sorting
393 is based on the second value. */
396 gcov_sort_n_vals (gcov_type
*value_array
, int n
)
400 for (j
= 2; j
< n
; j
+= 2)
402 gcov_type cur_ent
[2];
404 cur_ent
[0] = value_array
[j
];
405 cur_ent
[1] = value_array
[j
+ 1];
407 while (k
>= 0 && value_array
[k
+ 1] < cur_ent
[1])
409 value_array
[k
+ 2] = value_array
[k
];
410 value_array
[k
+ 3] = value_array
[k
+1];
413 value_array
[k
+ 2] = cur_ent
[0];
414 value_array
[k
+ 3] = cur_ent
[1];
418 /* Sort the profile counters for all indirect call sites. Counters
419 for each call site are allocated in array COUNTERS. */
422 gcov_sort_icall_topn_counter (const struct gcov_ctr_info
*counters
)
426 int n
= counters
->num
;
428 gcc_assert (!(n
% GCOV_ICALL_TOPN_NCOUNTS
));
429 values
= counters
->values
;
431 for (i
= 0; i
< n
; i
+= GCOV_ICALL_TOPN_NCOUNTS
)
433 gcov_type
*value_array
= &values
[i
+ 1];
434 gcov_sort_n_vals (value_array
, GCOV_ICALL_TOPN_NCOUNTS
- 1);
438 /* Sort topn indirect_call profile counters in GI_PTR. */
441 gcov_sort_topn_counter_arrays (const struct gcov_info
*gi_ptr
)
445 const struct gcov_fn_info
*gfi_ptr
;
446 const struct gcov_ctr_info
*ci_ptr
;
448 if (!gi_ptr
->merge
[GCOV_COUNTER_ICALL_TOPNV
])
451 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
453 gfi_ptr
= gi_ptr
->functions
[f_ix
];
454 ci_ptr
= gfi_ptr
->ctrs
;
455 for (i
= 0; i
< GCOV_COUNTERS
; i
++)
457 if (!gi_ptr
->merge
[i
])
459 if (i
== GCOV_COUNTER_ICALL_TOPNV
)
461 gcov_sort_icall_topn_counter (ci_ptr
);
469 /* Dump the coverage counts for one gcov_info object. We merge with existing
470 counts when possible, to avoid growing the .da files ad infinitum. We use
471 this program's checksum to make sure we only accumulate whole program
472 statistics to the correct summary. An object file might be embedded
473 in two separate programs, and we must keep the two program
474 summaries separate. */
477 dump_one_gcov (struct gcov_info
*gi_ptr
, struct gcov_filename
*gf
,
478 unsigned run_counted
, gcov_type run_max
)
480 struct gcov_summary summary
= {};
486 gcov_sort_topn_counter_arrays (gi_ptr
);
488 error
= gcov_exit_open_gcda_file (gi_ptr
, gf
);
492 tag
= gcov_read_unsigned ();
495 /* Merge data from file. */
496 if (tag
!= GCOV_DATA_MAGIC
)
498 gcov_error ("profiling:%s:Not a gcov data file\n", gf
->filename
);
501 error
= merge_one_data (gf
->filename
, gi_ptr
, &summary
);
508 merge_summary (run_counted
, &summary
, run_max
);
510 write_one_data (gi_ptr
, &summary
);
515 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
517 if ((error
= gcov_close ()))
518 gcov_error (error
< 0 ?
519 "profiling:%s:Overflow writing\n" :
520 "profiling:%s:Error writing\n",
525 /* Dump all the coverage counts for the program. It first computes program
526 summary and then traverses gcov_list list and dumps the gcov_info
527 objects one by one. */
533 gcov_do_dump (struct gcov_info
*list
, int run_counted
)
535 struct gcov_info
*gi_ptr
;
536 struct gcov_filename gf
;
538 /* Compute run_max of this program run. */
539 gcov_type run_max
= 0;
540 for (gi_ptr
= list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
541 for (unsigned f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
543 const struct gcov_ctr_info
*cinfo
544 = &gi_ptr
->functions
[f_ix
]->ctrs
[GCOV_COUNTER_ARCS
];
546 for (unsigned i
= 0; i
< cinfo
->num
; i
++)
547 if (run_max
< cinfo
->values
[i
])
548 run_max
= cinfo
->values
[i
];
551 allocate_filename_struct (&gf
);
553 /* Now merge each file. */
554 for (gi_ptr
= list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
556 dump_one_gcov (gi_ptr
, &gf
, run_counted
, run_max
);
565 __attribute__ ((unused
))
566 gcov_get_filename (struct gcov_info
*list
)
568 return list
->filename
;
574 __gcov_dump_one (struct gcov_root
*root
)
579 gcov_do_dump (root
->list
, root
->run_counted
);
582 root
->run_counted
= 1;
585 /* Per-dynamic-object gcov state. */
586 struct gcov_root __gcov_root
;
588 /* Exactly one of these will be live in the process image. */
589 struct gcov_master __gcov_master
=
595 __gcov_dump_one (&__gcov_root
);
596 if (__gcov_root
.next
)
597 __gcov_root
.next
->prev
= __gcov_root
.prev
;
598 if (__gcov_root
.prev
)
599 __gcov_root
.prev
->next
= __gcov_root
.next
;
601 __gcov_master
.root
= __gcov_root
.next
;
606 /* Add a new object file onto the bb chain. Invoked automatically
607 when running an object file's global ctors. */
610 __gcov_init (struct gcov_info
*info
)
612 if (!info
->version
|| !info
->n_functions
)
614 if (gcov_version (info
, info
->version
, 0))
616 if (!__gcov_root
.list
)
618 /* Add to master list and at exit function. */
619 if (gcov_version (NULL
, __gcov_master
.version
, "<master>"))
621 __gcov_root
.next
= __gcov_master
.root
;
622 if (__gcov_master
.root
)
623 __gcov_master
.root
->prev
= &__gcov_root
;
624 __gcov_master
.root
= &__gcov_root
;
628 info
->next
= __gcov_root
.list
;
629 __gcov_root
.list
= info
;
632 #endif /* !IN_GCOV_TOOL */
634 #endif /* inhibit_libc */