]>
sourceware.org Git - systemtap.git/blob - coveragedb.cxx
2 // Copyright (C) 2007 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
11 #include "coveragedb.h"
12 #include "elaborate.h"
17 #ifdef HAVE_LIBSQLITE3
25 void print_coverage_info(systemtap_session
&s
)
27 // print out used probes
28 clog
<< _("---- used probes-----") << endl
;
29 for (unsigned i
=0; i
<s
.probes
.size(); i
++) {
30 // walk through the chain of probes
31 vector
<probe
*> used_probe_list
;
32 s
.probes
[i
]->collect_derivation_chain(used_probe_list
);
33 for (unsigned j
=0; j
<used_probe_list
.size(); ++j
) {
34 for (unsigned k
=0; k
< used_probe_list
[j
]->locations
.size(); ++k
)
36 << used_probe_list
[j
]->locations
[k
]->components
.front()->tok
->location
<< endl
;
39 clog
<< "----" << endl
;
40 // for each probe print used and unused variables
41 for (unsigned j
=0; j
<s
.probes
[i
]->locals
.size(); ++j
) {
42 clog
<< _("local: ") << s
.probes
[i
]->locals
[j
]->tok
->location
<< endl
;
44 for (unsigned j
=0; j
<s
.probes
[i
]->unused_locals
.size(); ++j
) {
45 clog
<< _("unused_local: ")
46 << s
.probes
[i
]->unused_locals
[j
]->tok
->location
50 // print out unused probes
51 clog
<< _("---- unused probes----- ") << endl
;
52 for (unsigned i
=0; i
<s
.unused_probes
.size(); i
++) {
53 // walk through the chain of probes
54 vector
<probe
*> unused_probe_list
;
55 s
.unused_probes
[i
]->collect_derivation_chain(unused_probe_list
);
56 for (unsigned j
=0; j
<unused_probe_list
.size(); ++j
) {
57 for (unsigned k
=0; k
< unused_probe_list
[j
]->locations
.size(); ++k
)
59 << unused_probe_list
[j
]->locations
[k
]->components
.front()->tok
->location
<< endl
;
63 // print out used functions
64 clog
<< _("---- used functions----- ") << endl
;
65 for (map
<string
,functiondecl
*>::iterator it
= s
.functions
.begin(); it
!= s
.functions
.end(); it
++)
67 clog
<< "function: " << it
->second
->tok
->location
68 << " " << it
->second
->name
71 // print out unused functions
72 clog
<< _("---- unused functions----- ") << endl
;
73 for (unsigned i
=0; i
<s
.unused_functions
.size(); i
++) {
74 clog
<< _("unused_function: ") << s
.unused_functions
[i
]->tok
->location
75 << " " << s
.unused_functions
[i
]->name
78 // print out used globals
79 clog
<< _("---- used globals----- ") << endl
;
80 for (unsigned i
=0; i
<s
.globals
.size(); i
++) {
81 clog
<< _("globals: ") << s
.globals
[i
]->tok
->location
82 << " " << s
.globals
[i
]->name
85 // print out unused globals
86 clog
<< _("---- unused globals----- ") << endl
;
87 for (unsigned i
=0; i
<s
.unused_globals
.size(); i
++) {
88 clog
<< _("globals: ") << s
.unused_globals
[i
]->tok
->location
89 << " " << s
.unused_globals
[i
]->name
96 has_table(sqlite3
*db
, const char * table
)
98 int rc
, rows
, columns
;
100 char **results
= NULL
;
102 ostringstream command
;
103 command
<< "SELECT name FROM sqlite_master "
104 << "WHERE type='table' AND name='" << table
<< "'";
106 rc
= sqlite3_get_table(db
, command
.str().c_str(),
107 &results
, &rows
, &columns
, &errmsg
);
109 if(rc
!= SQLITE_OK
) {
110 cerr
<< _("Error in statement: ") << command
.str() << " [" << errmsg
<< "]."
113 sqlite3_free_table(results
);
119 has_index(sqlite3
*db
, const char * index
)
121 int rc
, rows
, columns
;
123 char **results
= NULL
;
125 ostringstream command
;
126 command
<< "SELECT name FROM sqlite_master "
127 << "WHERE type='index' AND name='" << index
<< "'";
129 rc
= sqlite3_get_table(db
, command
.str().c_str(),
130 &results
, &rows
, &columns
, &errmsg
);
132 if(rc
!= SQLITE_OK
) {
133 cerr
<< _("Error in statement: ") << command
.str() << " [" << errmsg
<< "]."
136 sqlite3_free_table(results
);
141 void sql_stmt(sqlite3
*db
, const char* stmt
)
146 // cerr << "sqlite: " << stmt << endl;
148 ret
= sqlite3_exec(db
, stmt
, 0, 0, &errmsg
);
150 if(ret
!= SQLITE_OK
) {
151 cerr
<< _("Error in statement: ") << stmt
<< " [" << errmsg
<< "]."
156 void enter_element(sqlite3
*db
, coverage_element
&x
)
158 ostringstream command
;
159 command
<< "insert or ignore into counts values ('"
167 sql_stmt(db
, command
.str().c_str());
171 void increment_element(sqlite3
*db
, coverage_element
&x
)
173 ostringstream command
;
174 // make sure value in table
175 command
<< "insert or ignore into counts values ('"
183 // increment appropriate value
184 << "update counts set compiled=compiled+"
185 << x
.compiled
<< " where ("
186 << "file=='" << x
.file
<< "' and "
187 << "line=='" << x
.line
<< "' and "
188 << "col=='" << x
.col
<< "' and "
189 << "type=='" << x
.type
<< "' and "
190 << "name=='" << x
.name
<< "')";
191 sql_stmt(db
, command
.str().c_str());
196 sql_update_used_probes(sqlite3
*db
, systemtap_session
&s
)
198 // update database used probes
199 for (unsigned i
=0; i
<s
.probes
.size(); i
++) {
200 // walk through the chain of probes
201 vector
<probe
*> used_probe_list
;
202 s
.probes
[i
]->collect_derivation_chain(used_probe_list
);
203 for (unsigned j
=0; j
<used_probe_list
.size(); ++j
) {
204 for (unsigned k
=0; k
< used_probe_list
[j
]->locations
.size(); ++k
){
205 struct source_loc place
= used_probe_list
[j
]->locations
[k
]->components
.front()->tok
->location
;
206 coverage_element
x(place
);
208 x
.type
= db_type_probe
;
209 x
.name
= used_probe_list
[j
]->locations
[k
]->str();
211 increment_element(db
, x
);
215 // for each probe update used and unused variables
216 for (unsigned j
=0; j
<s
.probes
[i
]->locals
.size(); ++j
) {
217 struct source_loc place
= s
.probes
[i
]->locals
[j
]->tok
->location
;
218 coverage_element
x(place
);
220 x
.type
= db_type_local
;
221 x
.name
= s
.probes
[i
]->locals
[j
]->tok
->content
;
223 increment_element(db
, x
);
225 for (unsigned j
=0; j
<s
.probes
[i
]->unused_locals
.size(); ++j
) {
226 struct source_loc place
= s
.probes
[i
]->unused_locals
[j
]->tok
->location
;
227 coverage_element
x(place
);
229 x
.type
= db_type_local
;
230 x
.name
= s
.probes
[i
]->unused_locals
[j
]->tok
->content
;
232 increment_element(db
, x
);
239 sql_update_unused_probes(sqlite3
*db
, systemtap_session
&s
)
241 // update database unused probes
242 for (unsigned i
=0; i
<s
.unused_probes
.size(); i
++) {
243 // walk through the chain of probes
244 vector
<probe
*> unused_probe_list
;
245 s
.unused_probes
[i
]->collect_derivation_chain(unused_probe_list
);
246 for (unsigned j
=0; j
<unused_probe_list
.size(); ++j
) {
247 for (unsigned k
=0; k
< unused_probe_list
[j
]->locations
.size(); ++k
) {
249 struct source_loc place
= unused_probe_list
[j
]->locations
[k
]->components
.front()->tok
->location
;
250 coverage_element
x(place
);
252 x
.type
= db_type_probe
;
253 x
.name
= unused_probe_list
[j
]->locations
[k
]->str();
255 increment_element(db
, x
);
263 sql_update_used_functions(sqlite3
*db
, systemtap_session
&s
)
265 // update db used functions
266 for (map
<string
,functiondecl
*>::iterator it
= s
.functions
.begin(); it
!= s
.functions
.end(); it
++)
268 struct source_loc place
= it
->second
->tok
->location
;
269 coverage_element
x(place
);
271 x
.type
= db_type_function
;
272 x
.name
= it
->second
->name
;
274 increment_element(db
, x
);
280 sql_update_unused_functions(sqlite3
*db
, systemtap_session
&s
)
282 // update db unused functions
283 for (unsigned i
=0; i
<s
.unused_functions
.size(); i
++) {
284 struct source_loc place
= s
.unused_functions
[i
]->tok
->location
;
285 coverage_element
x(place
);
287 x
.type
= db_type_function
;
288 x
.name
= s
.unused_functions
[i
]->name
;
290 increment_element(db
, x
);
296 sql_update_used_globals(sqlite3
*db
, systemtap_session
&s
)
298 // update db used globals
299 for (unsigned i
=0; i
<s
.globals
.size(); i
++) {
300 struct source_loc place
= s
.globals
[i
]->tok
->location
;
301 coverage_element
x(place
);
303 x
.type
= db_type_global
;
304 x
.name
= s
.globals
[i
]->name
;
306 increment_element(db
, x
);
312 sql_update_unused_globals(sqlite3
*db
, systemtap_session
&s
)
314 // update db unused globals
315 for (unsigned i
=0; i
<s
.unused_globals
.size(); i
++) {
316 struct source_loc place
= s
.unused_globals
[i
]->tok
->location
;
317 coverage_element
x(place
);
319 x
.type
= db_type_global
;
320 x
.name
= s
.unused_globals
[i
]->name
;
322 increment_element(db
, x
);
326 void update_coverage_db(systemtap_session
&s
)
331 string
filename(s
.data_path
+ "/" + s
.kernel_release
+ ".db");
333 rc
= sqlite3_open(filename
.c_str(), &db
);
335 cerr
<< "Can't open database: " << sqlite3_errmsg(db
) << endl
;
341 sql_stmt(db
, "begin");
343 string
create_table("create table counts ("
344 "file text, line integer, col integer, "
345 "type text, name text, parent text, "
346 "compiled integer, executed integer)");
347 string
create_index("create unique index tokens on counts (file, line, col, "
350 // make sure the table is there
351 if (!has_table(db
, "counts"))
352 sql_stmt(db
, create_table
.c_str());
354 // make sure the index is there
355 if (!has_index(db
, "tokens"))
356 sql_stmt(db
, create_index
.c_str());
358 sql_update_used_probes(db
, s
);
359 sql_update_unused_probes(db
, s
);
360 sql_update_used_functions(db
, s
);
361 sql_update_unused_functions(db
, s
);
362 sql_update_used_globals(db
, s
);
363 sql_update_unused_globals(db
, s
);
365 // unlock the database and close database
366 sql_stmt(db
, "commit");
371 #endif /* HAVE_LIBSQLITE3 */
373 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.050126 seconds and 5 git commands to generate.