-
Notifications
You must be signed in to change notification settings - Fork 4
/
clangcovdump.h
94 lines (82 loc) · 2.94 KB
/
clangcovdump.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright (c) 2021 Qiang Liu <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CLANG_COV_DUMP_H
#define CLANG_COV_DUMP_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#define DEFAULT_PROFILE "clangcovdump.profraw"
static int duration = 0;
static char* llvm_profile_file = (char *)DEFAULT_PROFILE;
static int coverage_dump_precision = 1;
static int coverage_dump_precision_th = 600;
#ifdef __cplusplus
extern "C" void __llvm_profile_initialize_file(void);
extern "C" void __llvm_profile_set_filename(char *);
extern "C" int __llvm_profile_write_file(void);
extern "C" int __llvm_state_write_file(char *);
#else
void __llvm_profile_initialize_file(void);
void __llvm_profile_set_filename(char *);
int __llvm_profile_write_file(void);
int __llvm_state_write_file(char *);
#endif
static int llvm_profile_dump() {
char filename[256];
char filename_state[256];
time_t now;
time(&now);
snprintf(filename, 256, "%s-%ld", llvm_profile_file, now);
__llvm_profile_set_filename(filename);
// TODO: Disable this by default
// snprintf(filename_state, 256, "s%s-%ld", llvm_profile_file, now);
// __llvm_state_write_file(filename_state);
return __llvm_profile_write_file();
}
static void sig_handler(int signum) {
switch (signum) {
case SIGALRM:
// following dump
llvm_profile_dump();
duration += coverage_dump_precision;
if (duration > coverage_dump_precision_th && \
duration <= coverage_dump_precision_th + coverage_dump_precision) {
coverage_dump_precision = coverage_dump_precision_th;
}
alarm(coverage_dump_precision);
break;
}
}
static void llvm_profile_initialize_file(bool periodic) {
static int init = 0;
if (!init) {
__llvm_profile_initialize_file();
char *f = getenv("LLVM_PROFILE_FILE");
if (f) { llvm_profile_file = f; }
char *p = getenv("COVEARGE_DUMP_PRECISION");
if (p) { coverage_dump_precision = atoi(p); }
char *t = getenv("COVERAGE_DUMP_PRECISION_TH");
if (t) { coverage_dump_precision_th = atoi(t); }
// first dump
llvm_profile_dump();
if (periodic) {
signal(SIGALRM, sig_handler);
alarm(coverage_dump_precision);
}
init = 1;
}
}
#endif /* CLANG_COV_DUMP_H */