-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cpp
executable file
·195 lines (168 loc) · 4.48 KB
/
main.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <string>
#include <list>
#include <vector>
#include <map>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <assert.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <unordered_map>
#include <fcntl.h>
#include <sstream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <set>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
const int open_debug = 0;
#define LLOG(...) if (open_debug) {llog("[DEBUG] ", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);}
#define LERR(...) if (open_debug) {llog("[ERROR] ", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);}
void llog(const char *header, const char *file, const char *func, int pos, const char *fmt, ...) {
FILE *pLog = NULL;
time_t clock1;
struct tm *tptr;
va_list ap;
pLog = fopen("luacov.log", "a+");
if (pLog == NULL) {
return;
}
clock1 = time(0);
tptr = localtime(&clock1);
struct timeval tv;
gettimeofday(&tv, NULL);
fprintf(pLog, "===========================[%d.%d.%d, %d.%d.%d %llu]%s:%d,%s:===========================\n%s",
tptr->tm_year + 1990, tptr->tm_mon + 1,
tptr->tm_mday, tptr->tm_hour, tptr->tm_min,
tptr->tm_sec, (long long) ((tv.tv_sec) * 1000 + (tv.tv_usec) / 1000), file, pos, func, header);
va_start(ap, fmt);
vfprintf(pLog, fmt, ap);
fprintf(pLog, "\n\n");
va_end(ap);
va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n\n");
va_end(ap);
fclose(pLog);
}
std::unordered_map <std::string, uint64_t> gdata;
int gcount;
int glasttime;
std::string gfile;
int gautosave;
static void flush_file(int fd, const char *buf, size_t len) {
while (len > 0) {
ssize_t r = write(fd, buf, len);
buf += r;
len -= r;
}
}
static void flush() {
int fd = open(gfile.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (fd < 0) {
LERR("open file fail %s", gfile.c_str());
return;
}
for (std::unordered_map<std::string, uint64_t>::iterator it = gdata.begin(); it != gdata.end(); it++) {
int len = it->first.length();
flush_file(fd, (const char *) &len, sizeof(len));
flush_file(fd, it->first.c_str(), len);
uint64_t count = it->second;
flush_file(fd, (const char *) &count, sizeof(count));
}
close(fd);
}
static void hook_handler(lua_State *L, lua_Debug *par) {
if (par->event != LUA_HOOKLINE) {
LERR("hook_handler diff event %d", par->event);
return;
}
lua_Debug ar;
ar.source = 0;
int ret = lua_getstack(L, 0, &ar);
if (ret == 0) {
LERR("hook_handler lua_getstack fail %d", ret);
return;
}
ret = lua_getinfo(L, "S", &ar);
if (ret == 0) {
LERR("hook_handler lua_getinfo fail %d", ret);
return;
}
if (ar.source == 0) {
LERR("hook_handler source nil ");
return;
}
if (ar.source[0] != '@') {
LLOG("hook_handler source error %s ", ar.source);
return;
}
char buff[128] = {0};
snprintf(buff, sizeof(buff) - 1, "%d", par->currentline);
std::string d = ar.source;
d = d + ":";
d = d + buff;
gdata[d]++;
if (gautosave > 0) {
gcount++;
if (gcount % 10000 == 0) {
if (time(0) - glasttime > gautosave) {
glasttime = time(0);
LLOG("hook_handler %s %d", d.c_str(), gdata.size());
flush();
}
}
}
}
extern "C" void start_cov(lua_State *L, const char *file, int autosave) {
if (gdata.size() > 0) {
return;
}
gfile = file;
gautosave = autosave;
gcount = 0;
glasttime = 0;
lua_sethook(L, hook_handler, LUA_MASKLINE, 0);
}
extern "C" void stop_cov(lua_State *L) {
lua_sethook(L, 0, 0, 0);
flush();
gdata.clear();
}
static int lstart(lua_State *L) {
const char *file = lua_tostring(L, 1);
int autosave = (int) lua_tointeger(L, 2);
start_cov(L, file, autosave);
return 0;
}
static int lstop(lua_State *L) {
stop_cov(L);
return 0;
}
extern "C" int luaopen_libclua(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"start", lstart},
{"stop", lstop},
{NULL, NULL},
};
luaL_newlib(L, l);
return 1;
}