Silence -Wunused-variable in release builds.
[llvm/stm8.git] / runtime / libprofile / GCDAProfiling.c
blobe066b227c5c183922215a9975261097257ffdcb1
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.
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.
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.
21 \*===----------------------------------------------------------------------===*/
23 #include "llvm/Support/DataTypes.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #ifdef _MSC_VER
30 #include <direct.h>
31 #endif
33 /* #define DEBUG_GCDAPROFILING */
36 * --- GCOV file format I/O primitives ---
39 static FILE *output_file = NULL;
41 static void write_int32(uint32_t i) {
42 fwrite(&i, 4, 1, output_file);
45 static void write_int64(uint64_t i) {
46 uint32_t lo, hi;
47 lo = i >> 0;
48 hi = i >> 32;
50 write_int32(lo);
51 write_int32(hi);
54 static uint32_t length_of_string(const char *s) {
55 return (strlen(s) / 4) + 1;
58 static void write_string(const char *s) {
59 uint32_t len = length_of_string(s);
60 write_int32(len);
61 fwrite(s, strlen(s), 1, output_file);
62 fwrite("\0\0\0\0", 4 - (strlen(s) % 4), 1, output_file);
65 static char *mangle_filename(const char *orig_filename) {
66 /* TODO: handle GCOV_PREFIX_STRIP */
67 const char *prefix;
68 char *filename = 0;
70 prefix = getenv("GCOV_PREFIX");
72 if (!prefix)
73 return strdup(orig_filename);
75 filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
76 strcpy(filename, prefix);
77 strcat(filename, "/");
78 strcat(filename, orig_filename);
80 return filename;
83 static void recursive_mkdir(const char *filename) {
84 char *pathname;
85 int i, e;
87 for (i = 1, e = strlen(filename); i != e; ++i) {
88 if (filename[i] == '/') {
89 pathname = malloc(i + 1);
90 strncpy(pathname, filename, i);
91 pathname[i] = '\0';
92 #ifdef _WIN32
93 _mkdir(pathname);
94 #else
95 mkdir(pathname, 0750); /* some of these will fail, ignore it. */
96 #endif
97 free(pathname);
103 * --- LLVM line counter API ---
106 /* A file in this case is a translation unit. Each .o file built with line
107 * profiling enabled will emit to a different file. Only one file may be
108 * started at a time.
110 void llvm_gcda_start_file(const char *orig_filename) {
111 char *filename;
112 filename = mangle_filename(orig_filename);
113 recursive_mkdir(filename);
114 output_file = fopen(filename, "wb");
116 /* gcda file, version 404*, stamp LLVM. */
117 fwrite("adcg*404MVLL", 12, 1, output_file);
119 #ifdef DEBUG_GCDAPROFILING
120 printf("llvmgcda: [%s]\n", orig_filename);
121 #endif
123 free(filename);
126 /* Given an array of pointers to counters (counters), increment the n-th one,
127 * where we're also given a pointer to n (predecessor).
129 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
130 uint64_t **counters) {
131 uint64_t *counter;
132 uint32_t pred;
134 pred = *predecessor;
135 if (pred == 0xffffffff)
136 return;
137 counter = counters[pred];
139 /* Don't crash if the pred# is out of sync. This can happen due to threads,
140 or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
141 if (counter)
142 ++*counter;
143 #ifdef DEBUG_GCDAPROFILING
144 else
145 printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
146 state_table_row, *predecessor);
147 #endif
150 void llvm_gcda_emit_function(uint32_t ident, const char *function_name) {
151 #ifdef DEBUG_GCDAPROFILING
152 printf("llvmgcda: function id=%x\n", ident);
153 #endif
155 /* function tag */
156 fwrite("\0\0\0\1", 4, 1, output_file);
157 write_int32(3 + 1 + length_of_string(function_name));
158 write_int32(ident);
159 write_int32(0);
160 write_int32(0);
161 write_string(function_name);
164 void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) {
165 uint32_t i;
166 /* counter #1 (arcs) tag */
167 fwrite("\0\0\xa1\1", 4, 1, output_file);
168 write_int32(num_counters * 2);
169 for (i = 0; i < num_counters; ++i) {
170 write_int64(counters[i]);
173 #ifdef DEBUG_GCDAPROFILING
174 printf("llvmgcda: %u arcs\n", num_counters);
175 for (i = 0; i < num_counters; ++i) {
176 printf("llvmgcda: %llu\n", (unsigned long long)counters[i]);
178 #endif
181 void llvm_gcda_end_file() {
182 /* Write out EOF record. */
183 fwrite("\0\0\0\0\0\0\0\0", 8, 1, output_file);
184 fclose(output_file);
185 output_file = NULL;
187 #ifdef DEBUG_GCDAPROFILING
188 printf("llvmgcda: -----\n");
189 #endif