-
Notifications
You must be signed in to change notification settings - Fork 3
/
coveragedb.cxx
373 lines (314 loc) · 10.2 KB
/
coveragedb.cxx
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// coveragedb.cxx
// Copyright (C) 2007 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "parse.h"
#include "coveragedb.h"
#include "config.h"
#include "elaborate.h"
#include "tapsets.h"
#include "session.h"
#include "util.h"
#ifdef HAVE_LIBSQLITE3
#include <iostream>
#include <sqlite3.h>
#include <cstdlib>
using namespace std;
void print_coverage_info(systemtap_session &s)
{
// print out used probes
clog << _("---- used probes-----") << endl;
for (unsigned i=0; i<s.probes.size(); i++) {
// walk through the chain of probes
vector<probe*> used_probe_list;
s.probes[i]->collect_derivation_chain(used_probe_list);
for (unsigned j=0; j<used_probe_list.size(); ++j) {
for (unsigned k=0; k< used_probe_list[j]->locations.size(); ++k)
clog << _("probe: ")
<< used_probe_list[j]->locations[k]->components.front()->tok->location << endl;
}
clog << "----" << endl;
// for each probe print used and unused variables
for (unsigned j=0; j<s.probes[i]->locals.size(); ++j) {
clog << _("local: ") << s.probes[i]->locals[j]->tok->location << endl;
}
for (unsigned j=0; j<s.probes[i]->unused_locals.size(); ++j) {
clog << _("unused_local: ")
<< s.probes[i]->unused_locals[j]->tok->location
<< endl;
}
}
// print out unused probes
clog << _("---- unused probes----- ") << endl;
for (unsigned i=0; i<s.unused_probes.size(); i++) {
// walk through the chain of probes
vector<probe*> unused_probe_list;
s.unused_probes[i]->collect_derivation_chain(unused_probe_list);
for (unsigned j=0; j<unused_probe_list.size(); ++j) {
for (unsigned k=0; k< unused_probe_list[j]->locations.size(); ++k)
clog << _("probe: ")
<< unused_probe_list[j]->locations[k]->components.front()->tok->location << endl;
}
}
// print out used functions
clog << _("---- used functions----- ") << endl;
for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
{
clog << "function: " << it->second->tok->location
<< " " << it->second->name
<< endl;
}
// print out unused functions
clog << _("---- unused functions----- ") << endl;
for (unsigned i=0; i<s.unused_functions.size(); i++) {
clog << _("unused_function: ") << s.unused_functions[i]->tok->location
<< " " << s.unused_functions[i]->name
<< endl;
}
// print out used globals
clog << _("---- used globals----- ") << endl;
for (unsigned i=0; i<s.globals.size(); i++) {
clog << _("globals: ") << s.globals[i]->tok->location
<< " " << s.globals[i]->name
<< endl;
}
// print out unused globals
clog << _("---- unused globals----- ") << endl;
for (unsigned i=0; i<s.unused_globals.size(); i++) {
clog << _("globals: ") << s.unused_globals[i]->tok->location
<< " " << s.unused_globals[i]->name
<< endl;
}
}
bool
has_table(sqlite3 *db, const char * table)
{
int rc, rows, columns;
char *errmsg;
char **results = NULL;
ostringstream command;
command << "SELECT name FROM sqlite_master "
<< "WHERE type='table' AND name='" << table << "'";
rc = sqlite3_get_table(db, command.str().c_str(),
&results, &rows, &columns, &errmsg);
if(rc != SQLITE_OK) {
cerr << _("Error in statement: ") << command << " [" << errmsg << "]."
<< endl;
}
sqlite3_free_table(results);
return (rows !=0);
}
bool
has_index(sqlite3 *db, const char * index)
{
int rc, rows, columns;
char *errmsg;
char **results = NULL;
ostringstream command;
command << "SELECT name FROM sqlite_master "
<< "WHERE type='index' AND name='" << index << "'";
rc = sqlite3_get_table(db, command.str().c_str(),
&results, &rows, &columns, &errmsg);
if(rc != SQLITE_OK) {
cerr << _("Error in statement: ") << command << " [" << errmsg << "]."
<< endl;
}
sqlite3_free_table(results);
return (rows !=0);
}
void sql_stmt(sqlite3 *db, const char* stmt)
{
char *errmsg;
int ret;
// cerr << "sqlite: " << stmt << endl;
ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
if(ret != SQLITE_OK) {
cerr << _("Error in statement: ") << stmt << " [" << errmsg << "]."
<< endl;
}
}
void enter_element(sqlite3 *db, coverage_element &x)
{
ostringstream command;
command << "insert or ignore into counts values ('"
<< x.file << "', '"
<< x.line << "', '"
<< x.col << "', '"
<< x.type << "','"
<< x.name << "', '"
<< x.parent <<"',"
<< "'0', '0')";
sql_stmt(db, command.str().c_str());
}
void increment_element(sqlite3 *db, coverage_element &x)
{
ostringstream command;
// make sure value in table
command << "insert or ignore into counts values ('"
<< x.file << "', '"
<< x.line << "', '"
<< x.col << "', '"
<< x.type << "','"
<< x.name << "', '"
<< x.parent <<"',"
<< "'0', '0'); "
// increment appropriate value
<< "update counts set compiled=compiled+"
<< x.compiled << " where ("
<< "file=='" << x.file << "' and "
<< "line=='" << x.line << "' and "
<< "col=='" << x.col << "' and "
<< "type=='" << x.type << "' and "
<< "name=='" << x.name << "')";
sql_stmt(db, command.str().c_str());
}
void
sql_update_used_probes(sqlite3 *db, systemtap_session &s)
{
// update database used probes
for (unsigned i=0; i<s.probes.size(); i++) {
// walk through the chain of probes
vector<probe*> used_probe_list;
s.probes[i]->collect_derivation_chain(used_probe_list);
for (unsigned j=0; j<used_probe_list.size(); ++j) {
for (unsigned k=0; k< used_probe_list[j]->locations.size(); ++k){
struct source_loc place = used_probe_list[j]->locations[k]->components.front()->tok->location;
coverage_element x(place);
x.type = db_type_probe;
x.name = used_probe_list[j]->locations[k]->str();
x.compiled = 1;
increment_element(db, x);
}
}
// for each probe update used and unused variables
for (unsigned j=0; j<s.probes[i]->locals.size(); ++j) {
struct source_loc place = s.probes[i]->locals[j]->tok->location;
coverage_element x(place);
x.type = db_type_local;
x.name = s.probes[i]->locals[j]->tok->content;
x.compiled = 1;
increment_element(db, x);
}
for (unsigned j=0; j<s.probes[i]->unused_locals.size(); ++j) {
struct source_loc place = s.probes[i]->unused_locals[j]->tok->location;
coverage_element x(place);
x.type = db_type_local;
x.name = s.probes[i]->unused_locals[j]->tok->content;
x.compiled = 0;
increment_element(db, x);
}
}
}
void
sql_update_unused_probes(sqlite3 *db, systemtap_session &s)
{
// update database unused probes
for (unsigned i=0; i<s.unused_probes.size(); i++) {
// walk through the chain of probes
vector<probe*> unused_probe_list;
s.unused_probes[i]->collect_derivation_chain(unused_probe_list);
for (unsigned j=0; j<unused_probe_list.size(); ++j) {
for (unsigned k=0; k< unused_probe_list[j]->locations.size(); ++k) {
struct source_loc place = unused_probe_list[j]->locations[k]->components.front()->tok->location;
coverage_element x(place);
x.type = db_type_probe;
x.name = unused_probe_list[j]->locations[k]->str();
x.compiled = 0;
increment_element(db, x);
}
}
}
}
void
sql_update_used_functions(sqlite3 *db, systemtap_session &s)
{
// update db used functions
for (map<string,functiondecl*>::iterator it = s.functions.begin(); it != s.functions.end(); it++)
{
struct source_loc place = it->second->tok->location;
coverage_element x(place);
x.type = db_type_function;
x.name = it->second->name;
x.compiled = 1;
increment_element(db, x);
}
}
void
sql_update_unused_functions(sqlite3 *db, systemtap_session &s)
{
// update db unused functions
for (unsigned i=0; i<s.unused_functions.size(); i++) {
struct source_loc place = s.unused_functions[i]->tok->location;
coverage_element x(place);
x.type = db_type_function;
x.name = s.unused_functions[i]->name;
x.compiled = 0;
increment_element(db, x);
}
}
void
sql_update_used_globals(sqlite3 *db, systemtap_session &s)
{
// update db used globals
for (unsigned i=0; i<s.globals.size(); i++) {
struct source_loc place = s.globals[i]->tok->location;
coverage_element x(place);
x.type = db_type_global;
x.name = s.globals[i]->name;
x.compiled = 1;
increment_element(db, x);
}
}
void
sql_update_unused_globals(sqlite3 *db, systemtap_session &s)
{
// update db unused globals
for (unsigned i=0; i<s.unused_globals.size(); i++) {
struct source_loc place = s.unused_globals[i]->tok->location;
coverage_element x(place);
x.type = db_type_global;
x.name = s.unused_globals[i]->name;
x.compiled = 0;
increment_element(db, x);
}
}
void update_coverage_db(systemtap_session &s)
{
sqlite3 *db;
int rc;
string filename(s.data_path + "/" + s.kernel_release + ".db");
rc = sqlite3_open(filename.c_str(), &db);
if( rc ){
cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return;
}
// lock the database
sql_stmt(db, "begin");
string create_table("create table counts ("
"file text, line integer, col integer, "
"type text, name text, parent text, "
"compiled integer, executed integer)");
string create_index("create unique index tokens on counts (file, line, col, "
"type, name)");
// make sure the table is there
if (!has_table(db, "counts"))
sql_stmt(db, create_table.c_str());
// make sure the index is there
if (!has_index(db, "tokens"))
sql_stmt(db, create_index.c_str());
sql_update_used_probes(db, s);
sql_update_unused_probes(db, s);
sql_update_used_functions(db, s);
sql_update_unused_functions(db, s);
sql_update_used_globals(db, s);
sql_update_unused_globals(db, s);
// unlock the database and close database
sql_stmt(db, "commit");
sqlite3_close(db);
}
#endif /* HAVE_LIBSQLITE3 */
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */