1 /*===- GCDAProfiling.c - Support library for GCDA file emission -----------===*\
2 |*
3 |*                     The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 |*===----------------------------------------------------------------------===*|
9 |*
10 |* This file implements the call back routines for the gcov profiling
11 |* instrumentation pass. Link against this library when running code through
12 |* the -insert-gcov-profiling LLVM pass.
13 |*
14 |* We emit files in a corrupt version of GCOV's "gcda" file format. These files
15 |* are only close enough that LCOV will happily parse them. Anything that lcov
16 |* ignores is missing.
17 |*
18 |* TODO: gcov is multi-process safe by having each exit open the existing file
19 |* and append to it. We'd like to achieve that and be thread-safe too.
20 |*
21 \*===----------------------------------------------------------------------===*/
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #ifdef _WIN32
30 #include <direct.h>
31 #endif
32 
33 #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
34 
35 #if !I386_FREEBSD
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #endif
39 
40 #if !defined(_MSC_VER) && !I386_FREEBSD
41 #include <stdint.h>
42 #endif
43 
44 #if defined(_MSC_VER)
45 typedef unsigned int uint32_t;
46 typedef unsigned long long uint64_t;
47 #elif I386_FREEBSD
48 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
49  * FreeBSD 10, r232261) when compiled in 32-bit mode.
50  */
51 typedef unsigned char uint8_t;
52 typedef unsigned int uint32_t;
53 typedef unsigned long long uint64_t;
54 int mkdir(const char*, unsigned short);
55 #endif
56 
57 /* #define DEBUG_GCDAPROFILING */
58 
59 /*
60  * --- GCOV file format I/O primitives ---
61  */
62 
63 /*
64  * The current file name we're outputting. Used primarily for error logging.
65  */
66 static char *filename = NULL;
67 
68 /*
69  * The current file we're outputting.
70  */
71 static FILE *output_file = NULL;
72 
73 /*
74  * Buffer that we write things into.
75  */
76 #define WRITE_BUFFER_SIZE (128 * 1024)
77 static char *write_buffer = NULL;
78 static uint64_t cur_buffer_size = 0;
79 static uint64_t cur_pos = 0;
80 static uint64_t file_size = 0;
81 static int new_file = 0;
82 static int fd = -1;
83 
84 /*
85  * A list of functions to write out the data.
86  */
87 typedef void (*writeout_fn)();
88 
89 struct writeout_fn_node {
90   writeout_fn fn;
91   struct writeout_fn_node *next;
92 };
93 
94 static struct writeout_fn_node *writeout_fn_head = NULL;
95 static struct writeout_fn_node *writeout_fn_tail = NULL;
96 
97 /*
98  *  A list of flush functions that our __gcov_flush() function should call.
99  */
100 typedef void (*flush_fn)();
101 
102 struct flush_fn_node {
103   flush_fn fn;
104   struct flush_fn_node *next;
105 };
106 
107 static struct flush_fn_node *flush_fn_head = NULL;
108 static struct flush_fn_node *flush_fn_tail = NULL;
109 
resize_write_buffer(uint64_t size)110 static void resize_write_buffer(uint64_t size) {
111   if (!new_file) return;
112   size += cur_pos;
113   if (size <= cur_buffer_size) return;
114   size = (size - 1) / WRITE_BUFFER_SIZE + 1;
115   size *= WRITE_BUFFER_SIZE;
116   write_buffer = realloc(write_buffer, size);
117   cur_buffer_size = size;
118 }
119 
write_bytes(const char * s,size_t len)120 static void write_bytes(const char *s, size_t len) {
121   resize_write_buffer(len);
122   memcpy(&write_buffer[cur_pos], s, len);
123   cur_pos += len;
124 }
125 
write_32bit_value(uint32_t i)126 static void write_32bit_value(uint32_t i) {
127   write_bytes((char*)&i, 4);
128 }
129 
write_64bit_value(uint64_t i)130 static void write_64bit_value(uint64_t i) {
131   write_bytes((char*)&i, 8);
132 }
133 
length_of_string(const char * s)134 static uint32_t length_of_string(const char *s) {
135   return (strlen(s) / 4) + 1;
136 }
137 
write_string(const char * s)138 static void write_string(const char *s) {
139   uint32_t len = length_of_string(s);
140   write_32bit_value(len);
141   write_bytes(s, strlen(s));
142   write_bytes("\0\0\0\0", 4 - (strlen(s) % 4));
143 }
144 
read_32bit_value()145 static uint32_t read_32bit_value() {
146   uint32_t val;
147 
148   if (new_file)
149     return (uint32_t)-1;
150 
151   val = *(uint32_t*)&write_buffer[cur_pos];
152   cur_pos += 4;
153   return val;
154 }
155 
read_64bit_value()156 static uint64_t read_64bit_value() {
157   uint64_t val;
158 
159   if (new_file)
160     return (uint64_t)-1;
161 
162   val = *(uint64_t*)&write_buffer[cur_pos];
163   cur_pos += 8;
164   return val;
165 }
166 
mangle_filename(const char * orig_filename)167 static char *mangle_filename(const char *orig_filename) {
168   char *new_filename;
169   size_t filename_len, prefix_len;
170   int prefix_strip;
171   int level = 0;
172   const char *fname, *ptr;
173   const char *prefix = getenv("GCOV_PREFIX");
174   const char *prefix_strip_str = getenv("GCOV_PREFIX_STRIP");
175 
176   if (prefix == NULL || prefix[0] == '\0')
177     return strdup(orig_filename);
178 
179   if (prefix_strip_str) {
180     prefix_strip = atoi(prefix_strip_str);
181 
182     /* Negative GCOV_PREFIX_STRIP values are ignored */
183     if (prefix_strip < 0)
184       prefix_strip = 0;
185   } else {
186     prefix_strip = 0;
187   }
188 
189   fname = orig_filename;
190   for (level = 0, ptr = fname + 1; level < prefix_strip; ++ptr) {
191     if (*ptr == '\0')
192       break;
193     if (*ptr != '/')
194       continue;
195     fname = ptr;
196     ++level;
197   }
198 
199   filename_len = strlen(fname);
200   prefix_len = strlen(prefix);
201   new_filename = malloc(prefix_len + 1 + filename_len + 1);
202   memcpy(new_filename, prefix, prefix_len);
203 
204   if (prefix[prefix_len - 1] != '/')
205     new_filename[prefix_len++] = '/';
206   memcpy(new_filename + prefix_len, fname, filename_len + 1);
207 
208   return new_filename;
209 }
210 
recursive_mkdir(char * path)211 static void recursive_mkdir(char *path) {
212   int i;
213 
214   for (i = 1; path[i] != '\0'; ++i) {
215     if (path[i] != '/') continue;
216     path[i] = '\0';
217 #ifdef _WIN32
218     _mkdir(path);
219 #else
220     mkdir(path, 0755);  /* Some of these will fail, ignore it. */
221 #endif
222     path[i] = '/';
223   }
224 }
225 
map_file()226 static int map_file() {
227   fseek(output_file, 0L, SEEK_END);
228   file_size = ftell(output_file);
229 
230   /* A size of 0 is invalid to `mmap'. Return a fail here, but don't issue an
231    * error message because it should "just work" for the user. */
232   if (file_size == 0)
233     return -1;
234 
235   write_buffer = mmap(0, file_size, PROT_READ | PROT_WRITE,
236                       MAP_FILE | MAP_SHARED, fd, 0);
237   if (write_buffer == (void *)-1) {
238     int errnum = errno;
239     fprintf(stderr, "profiling: %s: cannot map: %s\n", filename,
240             strerror(errnum));
241     return -1;
242   }
243   return 0;
244 }
245 
unmap_file()246 static void unmap_file() {
247 #if !defined(__minix)
248   if (msync(write_buffer, file_size, MS_SYNC) == -1) {
249     int errnum = errno;
250     fprintf(stderr, "profiling: %s: cannot msync: %s\n", filename,
251             strerror(errnum));
252   }
253 #endif /* !defined(__minix) */
254 
255   /* We explicitly ignore errors from unmapping because at this point the data
256    * is written and we don't care.
257    */
258   (void)munmap(write_buffer, file_size);
259   write_buffer = NULL;
260   file_size = 0;
261 }
262 
263 /*
264  * --- LLVM line counter API ---
265  */
266 
267 /* A file in this case is a translation unit. Each .o file built with line
268  * profiling enabled will emit to a different file. Only one file may be
269  * started at a time.
270  */
llvm_gcda_start_file(const char * orig_filename,const char version[4],uint32_t checksum)271 void llvm_gcda_start_file(const char *orig_filename, const char version[4],
272                           uint32_t checksum) {
273   const char *mode = "r+b";
274   filename = mangle_filename(orig_filename);
275 
276   /* Try just opening the file. */
277   new_file = 0;
278   fd = open(filename, O_RDWR);
279 
280   if (fd == -1) {
281     /* Try opening the file, creating it if necessary. */
282     new_file = 1;
283     mode = "w+b";
284     fd = open(filename, O_RDWR | O_CREAT, 0644);
285     if (fd == -1) {
286       /* Try creating the directories first then opening the file. */
287       recursive_mkdir(filename);
288       fd = open(filename, O_RDWR | O_CREAT, 0644);
289       if (fd == -1) {
290         /* Bah! It's hopeless. */
291         int errnum = errno;
292         fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
293                 strerror(errnum));
294         return;
295       }
296     }
297   }
298 
299   output_file = fdopen(fd, mode);
300 
301   /* Initialize the write buffer. */
302   write_buffer = NULL;
303   cur_buffer_size = 0;
304   cur_pos = 0;
305 
306   if (new_file) {
307     resize_write_buffer(WRITE_BUFFER_SIZE);
308     memset(write_buffer, 0, WRITE_BUFFER_SIZE);
309   } else {
310     if (map_file() == -1) {
311       /* mmap failed, try to recover by clobbering */
312       new_file = 1;
313       write_buffer = NULL;
314       cur_buffer_size = 0;
315       resize_write_buffer(WRITE_BUFFER_SIZE);
316       memset(write_buffer, 0, WRITE_BUFFER_SIZE);
317     }
318   }
319 
320   /* gcda file, version, stamp checksum. */
321   write_bytes("adcg", 4);
322   write_bytes(version, 4);
323   write_32bit_value(checksum);
324 
325 #ifdef DEBUG_GCDAPROFILING
326   fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
327 #endif
328 }
329 
330 /* Given an array of pointers to counters (counters), increment the n-th one,
331  * where we're also given a pointer to n (predecessor).
332  */
llvm_gcda_increment_indirect_counter(uint32_t * predecessor,uint64_t ** counters)333 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
334                                           uint64_t **counters) {
335   uint64_t *counter;
336   uint32_t pred;
337 
338   pred = *predecessor;
339   if (pred == 0xffffffff)
340     return;
341   counter = counters[pred];
342 
343   /* Don't crash if the pred# is out of sync. This can happen due to threads,
344      or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
345   if (counter)
346     ++*counter;
347 #ifdef DEBUG_GCDAPROFILING
348   else
349     fprintf(stderr,
350             "llvmgcda: increment_indirect_counter counters=%08llx, pred=%u\n",
351             *counter, *predecessor);
352 #endif
353 }
354 
llvm_gcda_emit_function(uint32_t ident,const char * function_name,uint32_t func_checksum,uint8_t use_extra_checksum,uint32_t cfg_checksum)355 void llvm_gcda_emit_function(uint32_t ident, const char *function_name,
356                              uint32_t func_checksum, uint8_t use_extra_checksum,
357                              uint32_t cfg_checksum) {
358   uint32_t len = 2;
359 
360   if (use_extra_checksum)
361     len++;
362 #ifdef DEBUG_GCDAPROFILING
363   fprintf(stderr, "llvmgcda: function id=0x%08x name=%s\n", ident,
364           function_name ? function_name : "NULL");
365 #endif
366   if (!output_file) return;
367 
368   /* function tag */
369   write_bytes("\0\0\0\1", 4);
370   if (function_name)
371     len += 1 + length_of_string(function_name);
372   write_32bit_value(len);
373   write_32bit_value(ident);
374   write_32bit_value(func_checksum);
375   if (use_extra_checksum)
376     write_32bit_value(cfg_checksum);
377   if (function_name)
378     write_string(function_name);
379 }
380 
llvm_gcda_emit_arcs(uint32_t num_counters,uint64_t * counters)381 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
382   uint32_t i;
383   uint64_t *old_ctrs = NULL;
384   uint32_t val = 0;
385   uint64_t save_cur_pos = cur_pos;
386 
387   if (!output_file) return;
388 
389   val = read_32bit_value();
390 
391   if (val != (uint32_t)-1) {
392     /* There are counters present in the file. Merge them. */
393     if (val != 0x01a10000) {
394       fprintf(stderr, "profiling:invalid arc tag (0x%08x)\n", val);
395       return;
396     }
397 
398     val = read_32bit_value();
399     if (val == (uint32_t)-1 || val / 2 != num_counters) {
400       fprintf(stderr, "profiling:invalid number of counters (%d)\n", val);
401       return;
402     }
403 
404     old_ctrs = malloc(sizeof(uint64_t) * num_counters);
405     for (i = 0; i < num_counters; ++i)
406       old_ctrs[i] = read_64bit_value();
407   }
408 
409   cur_pos = save_cur_pos;
410 
411   /* Counter #1 (arcs) tag */
412   write_bytes("\0\0\xa1\1", 4);
413   write_32bit_value(num_counters * 2);
414   for (i = 0; i < num_counters; ++i) {
415     counters[i] += (old_ctrs ? old_ctrs[i] : 0);
416     write_64bit_value(counters[i]);
417   }
418 
419   free(old_ctrs);
420 
421 #ifdef DEBUG_GCDAPROFILING
422   fprintf(stderr, "llvmgcda:   %u arcs\n", num_counters);
423   for (i = 0; i < num_counters; ++i)
424     fprintf(stderr, "llvmgcda:   %llu\n", (unsigned long long)counters[i]);
425 #endif
426 }
427 
llvm_gcda_summary_info()428 void llvm_gcda_summary_info() {
429   const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */
430   uint32_t i;
431   uint32_t runs = 1;
432   uint32_t val = 0;
433   uint64_t save_cur_pos = cur_pos;
434 
435   if (!output_file) return;
436 
437   val = read_32bit_value();
438 
439   if (val != (uint32_t)-1) {
440     /* There are counters present in the file. Merge them. */
441     if (val != 0xa1000000) {
442       fprintf(stderr, "profiling:invalid object tag (0x%08x)\n", val);
443       return;
444     }
445 
446     val = read_32bit_value(); /* length */
447     if (val != obj_summary_len) {
448       fprintf(stderr, "profiling:invalid object length (%d)\n", val);
449       return;
450     }
451 
452     read_32bit_value(); /* checksum, unused */
453     read_32bit_value(); /* num, unused */
454     runs += read_32bit_value(); /* Add previous run count to new counter. */
455   }
456 
457   cur_pos = save_cur_pos;
458 
459   /* Object summary tag */
460   write_bytes("\0\0\0\xa1", 4);
461   write_32bit_value(obj_summary_len);
462   write_32bit_value(0); /* checksum, unused */
463   write_32bit_value(0); /* num, unused */
464   write_32bit_value(runs);
465   for (i = 3; i < obj_summary_len; ++i)
466     write_32bit_value(0);
467 
468   /* Program summary tag */
469   write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */
470   write_32bit_value(0); /* 0 length */
471 
472 #ifdef DEBUG_GCDAPROFILING
473   fprintf(stderr, "llvmgcda:   %u runs\n", runs);
474 #endif
475 }
476 
llvm_gcda_end_file()477 void llvm_gcda_end_file() {
478   /* Write out EOF record. */
479   if (output_file) {
480     write_bytes("\0\0\0\0\0\0\0\0", 8);
481 
482     if (new_file) {
483       fwrite(write_buffer, cur_pos, 1, output_file);
484       free(write_buffer);
485     } else {
486       unmap_file();
487     }
488 
489     fclose(output_file);
490     output_file = NULL;
491     write_buffer = NULL;
492   }
493   free(filename);
494 
495 #ifdef DEBUG_GCDAPROFILING
496   fprintf(stderr, "llvmgcda: -----\n");
497 #endif
498 }
499 
llvm_register_writeout_function(writeout_fn fn)500 void llvm_register_writeout_function(writeout_fn fn) {
501   struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node));
502   new_node->fn = fn;
503   new_node->next = NULL;
504 
505   if (!writeout_fn_head) {
506     writeout_fn_head = writeout_fn_tail = new_node;
507   } else {
508     writeout_fn_tail->next = new_node;
509     writeout_fn_tail = new_node;
510   }
511 }
512 
llvm_writeout_files()513 void llvm_writeout_files() {
514   struct writeout_fn_node *curr = writeout_fn_head;
515 
516   while (curr) {
517     curr->fn();
518     curr = curr->next;
519   }
520 }
521 
llvm_delete_writeout_function_list()522 void llvm_delete_writeout_function_list() {
523   while (writeout_fn_head) {
524     struct writeout_fn_node *node = writeout_fn_head;
525     writeout_fn_head = writeout_fn_head->next;
526     free(node);
527   }
528 
529   writeout_fn_head = writeout_fn_tail = NULL;
530 }
531 
llvm_register_flush_function(flush_fn fn)532 void llvm_register_flush_function(flush_fn fn) {
533   struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node));
534   new_node->fn = fn;
535   new_node->next = NULL;
536 
537   if (!flush_fn_head) {
538     flush_fn_head = flush_fn_tail = new_node;
539   } else {
540     flush_fn_tail->next = new_node;
541     flush_fn_tail = new_node;
542   }
543 }
544 
__gcov_flush()545 void __gcov_flush() {
546   struct flush_fn_node *curr = flush_fn_head;
547 
548   while (curr) {
549     curr->fn();
550     curr = curr->next;
551   }
552 }
553 
llvm_delete_flush_function_list()554 void llvm_delete_flush_function_list() {
555   while (flush_fn_head) {
556     struct flush_fn_node *node = flush_fn_head;
557     flush_fn_head = flush_fn_head->next;
558     free(node);
559   }
560 
561   flush_fn_head = flush_fn_tail = NULL;
562 }
563 
llvm_gcov_init(writeout_fn wfn,flush_fn ffn)564 void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) {
565   static int atexit_ran = 0;
566 
567   if (wfn)
568     llvm_register_writeout_function(wfn);
569 
570   if (ffn)
571     llvm_register_flush_function(ffn);
572 
573   if (atexit_ran == 0) {
574     atexit_ran = 1;
575 
576     /* Make sure we write out the data and delete the data structures. */
577     atexit(llvm_delete_flush_function_list);
578     atexit(llvm_delete_writeout_function_list);
579     atexit(llvm_writeout_files);
580   }
581 }
582