defaults.h (GCOV_TYPE_SIZE): Remove.
[platform/upstream/gcc.git] / gcc / gcov-io.h
1 /* File format for coverage information
2    Copyright (C) 1996, 1997, 1998, 2000, 2002,
3    2003 Free Software Foundation, Inc.
4    Contributed by Bob Manson <manson@cygnus.com>.
5    Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24 /* As a special exception, if you link this library with other files,
25    some of which are compiled with GCC, to produce an executable,
26    this library does not by itself cause the resulting executable
27    to be covered by the GNU General Public License.
28    This exception does not however invalidate any other reasons why
29    the executable file might be covered by the GNU General Public License.  */
30
31 /* Coverage information is held in two files.  A basic block graph
32    file, which is generated by the compiler, and a counter file, which
33    is generated by the program under test.  Both files use a similar
34    structure.  We do not attempt to make these files backwards
35    compatible with previous versions, as you only need coverage
36    information when developing a program.  We do hold version
37    information, so that mismatches can be detected, and we use a
38    format that allows tools to skip information they do not understand
39    or are not interested in.
40
41    Numbers are recorded in big endian unsigned binary form.  Either in
42    32 or 64 bits.  Strings are stored with a length count and NUL
43    terminator, and 0 to 3 bytes of zero padding up to the next 4 byte
44    boundary.  Zero length and NULL strings are simply stored as a
45    length of zero (they have no trailing NUL or padding).
46
47         int32:  byte3 byte2 byte1 byte0
48         int64:  byte7 byte6 byte5 byte4 byte3 byte2 byte1 byte0
49         string: int32:0 | int32:length char* char:0 padding
50         padding: | char:0 | char:0 char:0 | char:0 char:0 char:0
51         item: int32 | int64 | string
52
53    The basic format of the files is
54
55         file : int32:magic int32:version record*
56
57    The magic ident is different for the bbg and the counter files.
58    The version is the same for both files and is derived from gcc's
59    version number.  Although the ident and version are formally 32 bit
60    numbers, they are derived from 4 character ASCII strings.  The
61    version number consists of the single character major version
62    number, a two character minor version number (leading zero for
63    versions less than 10), and a single character indicating the
64    status of the release.  That will be 'e' experimental, 'p'
65    prerelease and 'r' for release.  Because, by good fortune, these are
66    in alphabetical order, string collating can be used to compare
67    version strings, and because numbers are stored big endian, numeric
68    comparison can be used when it is read as a 32 bit value.  Be aware
69    that the 'e' designation will (naturally) be unstable and might be
70    incompatible with itself.  For gcc 3.4 experimental, it would be
71    '304e' (0x33303465).  When the major version reaches 10, the letters
72    A-Z will be used.  Assuming minor increments releases every 6
73    months, we have to make a major increment every 50 years.  Assuming
74    major increments releases every 5 years, we're ok for the next 155
75    years -- good enough for me.
76
77    A record has a tag, length and variable amount of data.
78
79         record: header data
80         header: int32:tag int32:length
81         data: item*
82
83    Records are not nested, but there is a record hierarchy.  Tag
84    numbers reflect this hierarchy.  Tags are unique across bbg and da
85    files.  Some record types have a varying amount of data.  The LENGTH
86    is usually used to determine how much data.  The tag value is split
87    into 4 8-bit fields, one for each of four possible levels.  The
88    most significant is allocated first.  Unused levels are zero.
89    Active levels are odd-valued, so that the LSB of the level is one.
90    A sub-level incorporates the values of its superlevels.  This
91    formatting allows you to determine the tag heirarchy, without
92    understanding the tags themselves, and is similar to the standard
93    section numbering used in technical documents.  Level values
94    [1..3f] are used for common tags, values [41..9f] for the graph
95    file and [a1..ff] for the counter file.
96
97    The basic block graph file contains the following records
98         bbg:  unit function-graph*
99         unit: header int32:checksum string:source
100         function-graph: announce_function basic_blocks {arcs | lines}*
101         announce_function: header int32:ident int32:checksum
102                 string:name string:source int32:lineno
103         basic_block: header int32:flags*
104         arcs: header int32:block_no arc*
105         arc:  int32:dest_block int32:flags
106         lines: header int32:block_no line*
107                int32:0 string:NULL
108         line:  int32:line_no | int32:0 string:filename
109
110    The BASIC_BLOCK record holds per-bb flags.  The number of blocks
111    can be inferred from its data length.  There is one ARCS record per
112    basic block.  The number of arcs from a bb is implicit from the
113    data length.  It enumerates the destination bb and per-arc flags.
114    There is one LINES record per basic block, it enumerates the source
115    lines which belong to that basic block.  Source file names are
116    introduced by a line number of 0, following lines are from the new
117    source file.  The initial source file for the function is NULL, but
118    the current source file should be remembered from one LINES record
119    to the next.  The end of a block is indicated by an empty filename
120    - this does not reset the current source file.  Note there is no
121    ordering of the ARCS and LINES records: they may be in any order,
122    interleaved in any manner.  The current filename follows the order
123    the LINES records are stored in the file, *not* the ordering of the
124    blocks they are for.
125
126    The data file contains the following records.
127         da: {unit function-data* summary:object summary:program*}*
128         unit: header int32:checksum
129         function-data:  announce_function arc_counts
130         announce_function: header int32:ident int32:checksum
131         arc_counts: header int64:count*
132         summary: int32:checksum {count-summary}GCOV_COUNTERS
133         count-summary:  int32:num int32:runs int64:sum
134                         int64:max int64:sum_max
135
136    The ANNOUNCE_FUNCTION record is the same as that in the BBG file,
137    but without the source location.
138    The ARC_COUNTS gives the counter values for those arcs that are
139    instrumented.  The SUMMARY records give information about the whole
140    object file and about the whole program.  The checksum is used for
141    whole program summaries, and disambiguates different programs which
142    include the same instrumented object file.  There may be several
143    program summaries, each with a unique checksum.  The object
144    summary's checkum is zero.  Note that the da file might contain
145    information from several runs concatenated, or the data might be
146    merged.
147
148    This file is included by both the compiler, gcov tools and the
149    runtime support library libgcov. IN_LIBGCOV and IN_GCOV are used to
150    distinguish which case is which.  If IN_LIBGCOV is non-zero,
151    libgcov is being built. If IN_GCOV is non-zero, the gcov tools are
152    being built. Otherwise the compiler is being built. IN_GCOV may be
153    positive or negative. If positive, we are compiling a tool that
154    requires additional functions (see the code for knowledge of what
155    those functions are).  */
156
157 #ifndef GCC_GCOV_IO_H
158 #define GCC_GCOV_IO_H
159
160 #if IN_LIBGCOV
161 /* About the target */
162
163 typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
164 typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
165 #if LONG_LONG_TYPE_SIZE > 32
166 typedef signed gcov_type __attribute__ ((mode (DI)));
167 #else
168 typedef signed gcov_type __attribute__ ((mode (SI)));
169 #endif
170
171 #if defined (TARGET_HAS_F_SETLKW)
172 #define GCOV_LOCKED 1
173 #else
174 #define GCOV_LOCKED 0
175 #endif
176
177 #else /* !IN_LIBGCOV */
178 /* About the host */
179
180 typedef unsigned gcov_unsigned_t;
181 typedef unsigned gcov_position_t;
182 /* gcov_type is typedef'd elsewhere for the compiler */
183 #if IN_GCOV
184 #define GCOV_LINKAGE static
185 typedef HOST_WIDEST_INT gcov_type;
186 #if IN_GCOV > 0
187 #include <sys/types.h>
188 #endif
189 #else /*!IN_GCOV */
190 #if LONG_LONG_TYPE_SIZE > 32
191 #define GCOV_TYPE_NODE intDI_type_node
192 #else
193 #define GCOV_TYPE_NODE intSI_type_node
194 #endif
195 #endif
196
197 #if defined (HOST_HAS_F_SETLKW)
198 #define GCOV_LOCKED 1
199 #else
200 #define GCOV_LOCKED 0
201 #endif
202
203 #endif /* !IN_LIBGCOV */
204
205 /* In gcov we want function linkage to be static, so we do not
206    polute the global namespace. In libgcov we need these functions
207    to be extern, so prefix them with __gcov so that we do not conflict.
208    In the compiler we want it extern, so that they can be accessed from
209    elsewhere.  */
210 #if IN_LIBGCOV
211 #define gcov_var __gcov_var
212 #define gcov_open __gcov_open
213 #define gcov_close __gcov_close
214 #define gcov_write_bytes __gcov_write_bytes
215 #define gcov_write_unsigned __gcov_write_unsigned
216 #define gcov_write_counter __gcov_write_counter
217 #pragma GCC poison gcov_write_string
218 #pragma GCC poison gcov_write_tag
219 #pragma GCC poison gcov_write_length
220 #define gcov_write_tag_length __gcov_write_tag_length
221 #define gcov_write_summary __gcov_write_summary
222 #define gcov_read_bytes __gcov_read_bytes
223 #define gcov_read_unsigned __gcov_read_unsigned
224 #define gcov_read_counter __gcov_read_counter
225 #pragma GCC poison gcov_read_string
226 #define gcov_read_summary __gcov_read_summary
227 #define gcov_position __gcov_position
228 #define gcov_sync __gcov_sync
229 #define gcov_seek __gcov_seek
230 #define gcov_rewrite __gcov_rewrite
231 #define gcov_is_eof __gcov_is_eof
232 #define gcov_is_error __gcov_is_error
233 #pragma GCC poison gcov_time
234 #endif
235
236 #ifndef GCOV_LINKAGE
237 #define GCOV_LINKAGE extern
238 #endif
239
240 /* File suffixes.  */
241 #define GCOV_DATA_SUFFIX ".da"
242 #define GCOV_GRAPH_SUFFIX ".bbg"
243
244 /* File magic.  */
245 #define GCOV_DATA_MAGIC  0x67636f76 /* "gcov" */
246 #define GCOV_GRAPH_MAGIC 0x67626267 /* "gbbg" */
247
248 /* gcov-iov.h is automatically generated by the makefile from
249    version.c, it looks like
250         #define GCOV_VERSION ((unsigned)0x89abcdef)
251 */
252 #include "gcov-iov.h"
253
254 /* The record tags.  Values [1..3f] are for tags which may be in either
255    file.  Values [41..9f] for those in the bbg file and [a1..ff] for
256    the data file.  */
257
258 #define GCOV_TAG_FUNCTION        ((unsigned)0x01000000)
259 #define GCOV_TAG_FUNCTION_LENGTH (2 * 4)
260 #define GCOV_TAG_BLOCKS          ((unsigned)0x01410000)
261 #define GCOV_TAG_BLOCKS_LENGTH(NUM) ((NUM) * 4)
262 #define GCOV_TAG_ARCS            ((unsigned)0x01430000)
263 #define GCOV_TAG_ARCS_LENGTH(NUM)  (1 * 4 + (NUM) * (2 * 4))
264 #define GCOV_TAG_LINES           ((unsigned)0x01450000)
265 #define GCOV_TAG_COUNTER_BASE    ((unsigned)0x01a10000) /* First counter */
266 #define GCOV_TAG_COUNTER_LENGTH(NUM) ((NUM) * 8)
267 #define GCOV_TAG_OBJECT_SUMMARY  ((unsigned)0xa1000000)
268 #define GCOV_TAG_PROGRAM_SUMMARY ((unsigned)0xa3000000)
269 #define GCOV_TAG_SUMMARY_LENGTH  (1 * 4 + GCOV_COUNTERS_SUMMABLE * (2 * 4 + 3 * 8))
270
271 /* Counters that are collected.  */
272 #define GCOV_COUNTER_ARCS       0  /* Arc transitions.  */
273 #define GCOV_COUNTERS_SUMMABLE  1  /* Counters which can be
274                                       summaried. */
275 #define GCOV_COUNTERS           1
276
277 /* A list of human readable names of the counters */
278 #define GCOV_COUNTER_NAMES      {"arcs"}
279
280 /* Names of merge functions for counters.  */
281 #define GCOV_MERGE_FUNCTIONS    {"__gcov_merge_add"}
282
283 /* Convert a counter index to a tag. */
284 #define GCOV_TAG_FOR_COUNTER(COUNT)                             \
285         (GCOV_TAG_COUNTER_BASE + ((COUNT) << 17))
286 /* Convert a tag to a counter.  */
287 #define GCOV_COUNTER_FOR_TAG(TAG)                                       \
288         (((TAG) - GCOV_TAG_COUNTER_BASE) >> 17)
289 /* Check whether a tag is a counter tag.  */
290 #define GCOV_TAG_IS_COUNTER(TAG)                                \
291         (!((TAG) & 0xFFFF) && GCOV_COUNTER_FOR_TAG (TAG) < GCOV_COUNTERS)
292
293 /* The tag level mask has 1's in the position of the inner levels, &
294    the lsb of the current level, and zero on the current and outer
295    levels.  */
296 #define GCOV_TAG_MASK(TAG) (((TAG) - 1) ^ (TAG))
297
298 /* Return nonzero if SUB is an immediate subtag of TAG.  */
299 #define GCOV_TAG_IS_SUBTAG(TAG,SUB)                             \
300         (GCOV_TAG_MASK (TAG) >> 8 == GCOV_TAG_MASK (SUB)        \
301          && !(((SUB) ^ (TAG)) & ~GCOV_TAG_MASK(TAG)))
302
303 /* Return nonzero if SUB is at a sublevel to TAG.  */
304 #define GCOV_TAG_IS_SUBLEVEL(TAG,SUB)                           \
305         (GCOV_TAG_MASK (TAG) > GCOV_TAG_MASK (SUB))
306
307 /* Basic block flags.  */
308 #define GCOV_BLOCK_UNEXPECTED   (1 << 1)
309
310 /* Arc flags.  */
311 #define GCOV_ARC_ON_TREE        (1 << 0)
312 #define GCOV_ARC_FAKE           (1 << 1)
313 #define GCOV_ARC_FALLTHROUGH    (1 << 2)
314
315 /* Structured records.  */
316
317 /* Cumulative counter data.   */
318 struct gcov_ctr_summary
319 {
320   gcov_unsigned_t num;          /* number of counters.  */
321   gcov_unsigned_t runs;         /* number of program runs */
322   gcov_type sum_all;            /* sum of all counters accumulated. */
323   gcov_type run_max;            /* maximum value on a single run.  */
324   gcov_type sum_max;            /* sum of individual run max values. */
325 };
326
327 /* Object & program summary record.  */
328 struct gcov_summary
329 {
330   gcov_unsigned_t checksum;     /* checksum of program */
331   struct gcov_ctr_summary ctrs[GCOV_COUNTERS_SUMMABLE];
332 };
333
334 /* Structures embedded in coveraged program.  The structures generated
335    by write_profile must match these.  */
336
337 #if IN_LIBGCOV
338 /* Information about a single function.  This uses the trailing array
339    idiom. The number of counters is determined from the counter_mask
340    in gcov_info.  We hold an array of function info, so have to
341    explicitly calculate the correct array stride.  */
342 struct gcov_fn_info
343 {
344   gcov_unsigned_t ident;        /* unique ident of function */
345   gcov_unsigned_t checksum;     /* function checksum */
346   unsigned n_ctrs[0];           /* instrumented counters */
347 };
348
349 /* Type of function used to merge counters.  */
350 typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
351
352 /* Information about counters.  */
353 struct gcov_ctr_info
354 {
355   gcov_unsigned_t num;          /* number of counters.  */
356   gcov_type *values;            /* their values.  */
357   gcov_merge_fn merge;          /* The function used to merge them.  */
358 };
359
360 /* Information about a single object file.  */
361 struct gcov_info
362 {
363   gcov_unsigned_t version;      /* expected version number */
364   struct gcov_info *next;       /* link to next, used by libgcc */
365
366   const char *filename;         /* output file name */
367
368   unsigned n_functions;         /* number of functions */
369   const struct gcov_fn_info *functions; /* table of functions */
370
371   unsigned ctr_mask;            /* mask of counters instrumented.  */
372   struct gcov_ctr_info counts[0]; /* count data. The number of bits
373                                      set in the ctr_mask field
374                                      determines how big this array
375                                      is.  */
376 };
377
378 /* Register a new object file module.  */
379 extern void __gcov_init (struct gcov_info *);
380
381 /* Called before fork, to avoid double counting.  */
382 extern void __gcov_flush (void);
383
384 /* The merge function that just sums the counters.  */
385 extern void __gcov_merge_add (gcov_type *, unsigned);
386 #endif /* IN_LIBGCOV */
387
388 #if IN_LIBGCOV >= 0
389
390 /* Because small reads and writes, interspersed with seeks cause lots
391    of disk activity, we buffer the entire count files.  */
392
393 GCOV_LINKAGE struct gcov_var
394 {
395   FILE *file;
396   gcov_position_t position;
397   gcov_position_t length;
398   size_t alloc;
399   unsigned modified;
400   int error;
401   unsigned char *buffer;
402 } gcov_var;
403
404 /* Functions for reading and writing gcov files.  */
405 GCOV_LINKAGE int gcov_open (const char */*name*/, int /*truncate*/);
406 GCOV_LINKAGE int gcov_close (void);
407 #if !IN_GCOV
408 GCOV_LINKAGE unsigned char *gcov_write_bytes (unsigned);
409 GCOV_LINKAGE void gcov_write_unsigned (gcov_unsigned_t);
410 #if IN_LIBGCOV
411 GCOV_LINKAGE void gcov_write_counter (gcov_type);
412 #else
413 GCOV_LINKAGE void gcov_write_string (const char *);
414 #endif
415 #if !IN_LIBGCOV
416 GCOV_LINKAGE gcov_position_t gcov_write_tag (gcov_unsigned_t);
417 GCOV_LINKAGE void gcov_write_length (gcov_position_t /*position*/);
418 #else
419 GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t);
420 GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
421                                       const struct gcov_summary *);
422 #endif
423 #endif /* !IN_GCOV */
424 GCOV_LINKAGE const unsigned char *gcov_read_bytes (unsigned);
425 GCOV_LINKAGE gcov_unsigned_t gcov_read_unsigned (void);
426 GCOV_LINKAGE gcov_type gcov_read_counter (void);
427 #if !IN_LIBGCOV
428 GCOV_LINKAGE const char *gcov_read_string (void);
429 #endif
430 GCOV_LINKAGE void gcov_read_summary (struct gcov_summary *);
431 static gcov_position_t gcov_position (void);
432 static void gcov_sync (gcov_position_t /*base*/, gcov_unsigned_t /*length */);
433 static void gcov_seek (gcov_position_t /*position*/);
434 static void gcov_rewrite (void);
435 static int gcov_is_eof (void);
436 static int gcov_is_error (void);
437 #if IN_GCOV > 0
438 GCOV_LINKAGE time_t gcov_time (void);
439 #endif
440
441 /* Save the current position in the gcov file.  */
442
443 static inline gcov_position_t
444 gcov_position (void)
445 {
446   return gcov_var.position;
447 }
448
449 /* Reset to a known position.  BASE should have been obtained from
450    gcov_save_position, LENGTH should be a record length, or zero.  */
451
452 static inline void
453 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
454 {
455   if (gcov_var.buffer)
456     {
457       base += length;
458       if (gcov_var.length < base)
459         {
460           gcov_var.error = 1;
461           base = gcov_var.length;
462         }
463       gcov_var.position = base;
464     }
465 }
466
467 /* Move to the end of the gcov file.  */
468
469 static inline void
470 gcov_seek (gcov_position_t base)
471 {
472   gcov_var.position = base < gcov_var.length ? base : gcov_var.length;
473 }
474
475 /* Move to beginning of file and intialize for writing.  */
476
477 static inline void
478 gcov_rewrite (void)
479 {
480   gcov_var.position = 0;
481 }
482
483 /* Tests whether we have reached end of .da file.  */
484
485 static inline int
486 gcov_is_eof ()
487 {
488   return gcov_var.position == gcov_var.length;
489 }
490
491 /* Return non-zero if the error flag is set.  */
492
493 static inline int
494 gcov_is_error ()
495 {
496   return gcov_var.file ? gcov_var.error : 1;
497 }
498
499 #endif /* IN_LIBGCOV >= 0 */
500
501 #endif /* GCC_GCOV_IO_H */