]>
Commit | Line | Data |
---|---|---|
fbd226c3 BV |
1 | /* |
2 | * This file is part of the libsigrokdecode project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "../libsigrokdecode.h" | |
21 | #include <libsigrok/libsigrok.h> | |
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <stdarg.h> | |
25 | #include <unistd.h> | |
26 | #include <errno.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
29 | #include <fcntl.h> | |
30 | #include <time.h> | |
31 | #include <sys/time.h> | |
32 | #include <sys/resource.h> | |
5bc679c6 | 33 | #include <dirent.h> |
fbd226c3 BV |
34 | #include <glib.h> |
35 | #ifdef __LINUX__ | |
36 | #include <sched.h> | |
37 | #endif | |
38 | #include "../config.h" | |
39 | ||
fbd226c3 BV |
40 | int debug = FALSE; |
41 | int statistics = FALSE; | |
5bc679c6 | 42 | char *coverage_report; |
fbd226c3 BV |
43 | |
44 | struct probe { | |
45 | char *name; | |
46 | int probe; | |
47 | }; | |
48 | ||
49 | struct option { | |
50 | char *key; | |
51 | char *value; | |
52 | }; | |
53 | ||
54 | struct pd { | |
55 | char *name; | |
56 | GSList *probes; | |
57 | GSList *options; | |
58 | }; | |
59 | ||
60 | struct output { | |
61 | char *pd; | |
62 | int type; | |
63 | char *class; | |
64 | int class_idx; | |
65 | char *outfile; | |
66 | int outfd; | |
67 | }; | |
68 | ||
5bc679c6 BV |
69 | struct cvg { |
70 | int lines; | |
71 | int missed; | |
72 | }; | |
73 | ||
74 | ||
75 | struct cvg *get_mod_cov(PyObject *py_cov, char *module_name); | |
76 | ||
fbd226c3 | 77 | |
4467372a | 78 | static void logmsg(char *prefix, FILE *out, const char *format, va_list args) |
fbd226c3 BV |
79 | { |
80 | if (prefix) | |
81 | fprintf(out, "%s", prefix); | |
82 | vfprintf(out, format, args); | |
83 | fprintf(out, "\n"); | |
84 | } | |
85 | ||
4467372a | 86 | static void DBG(const char *format, ...) |
fbd226c3 BV |
87 | { |
88 | va_list args; | |
89 | ||
90 | if (!debug) | |
91 | return; | |
92 | va_start(args, format); | |
5bc679c6 | 93 | logmsg("DBG: runtc: ", stdout, format, args); |
fbd226c3 BV |
94 | va_end(args); |
95 | } | |
96 | ||
4467372a | 97 | static void ERR(const char *format, ...) |
fbd226c3 BV |
98 | { |
99 | va_list args; | |
100 | ||
101 | va_start(args, format); | |
102 | logmsg("Error: ", stderr, format, args); | |
103 | va_end(args); | |
104 | } | |
105 | ||
4467372a | 106 | static int sr_log(void *cb_data, int loglevel, const char *format, va_list args) |
caa4b2cc BV |
107 | { |
108 | (void)cb_data; | |
109 | ||
110 | if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN) | |
111 | logmsg("Error: sr: ", stderr, format, args); | |
112 | else if (debug) | |
113 | logmsg("DBG: sr: ", stdout, format, args); | |
114 | ||
115 | return SRD_OK; | |
116 | } | |
117 | ||
4467372a | 118 | static int srd_log(void *cb_data, int loglevel, const char *format, va_list args) |
fbd226c3 BV |
119 | { |
120 | (void)cb_data; | |
121 | ||
122 | if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN) | |
123 | logmsg("Error: srd: ", stderr, format, args); | |
caa4b2cc | 124 | else if (debug) |
fbd226c3 BV |
125 | logmsg("DBG: srd: ", stdout, format, args); |
126 | ||
127 | return SRD_OK; | |
128 | } | |
129 | ||
4467372a | 130 | static void usage(char *msg) |
fbd226c3 BV |
131 | { |
132 | if (msg) | |
133 | fprintf(stderr, "%s\n", msg); | |
134 | ||
fbd226c3 BV |
135 | printf("Usage: runtc [-dPpoiOf]\n"); |
136 | printf(" -d Debug\n"); | |
137 | printf(" -P <protocol decoder>\n"); | |
138 | printf(" -p <probename=probenum> (optional)\n"); | |
139 | printf(" -o <probeoption=value> (optional)\n"); | |
140 | printf(" -i <input file>\n"); | |
141 | printf(" -O <output-pd:output-type[:output-class]>\n"); | |
142 | printf(" -f <output file> (optional)\n"); | |
5bc679c6 | 143 | printf(" -c <coverage report> (optional)\n"); |
fbd226c3 BV |
144 | exit(msg ? 1 : 0); |
145 | ||
146 | } | |
147 | ||
b7e15e0e BV |
148 | /* This is a neutered version of libsigrokdecode's py_str_as_str(). It |
149 | * does no error checking, but then the only strings it processes are | |
150 | * generated by Python's repr(), so are known good. */ | |
4467372a | 151 | static char *py_str_as_str(const PyObject *py_str) |
b7e15e0e BV |
152 | { |
153 | PyObject *py_encstr; | |
154 | char *str, *outstr; | |
155 | ||
156 | py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL); | |
157 | str = PyBytes_AS_STRING(py_encstr); | |
158 | outstr = g_strdup(str); | |
159 | Py_DecRef(py_encstr); | |
160 | ||
161 | return outstr; | |
162 | } | |
163 | ||
164 | static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data) | |
d7d693b5 | 165 | { |
d7d693b5 | 166 | struct output *op; |
b7e15e0e BV |
167 | PyObject *pydata, *pyrepr; |
168 | GString *out; | |
169 | char *s; | |
d7d693b5 | 170 | |
b7e15e0e | 171 | DBG("Python output from %s", pdata->pdo->di->inst_id); |
d7d693b5 | 172 | op = cb_data; |
b7e15e0e BV |
173 | pydata = pdata->data; |
174 | DBG("ptr %p", pydata); | |
175 | ||
176 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) | |
177 | /* This is not the PD selected for output. */ | |
178 | return; | |
179 | ||
180 | if (!(pyrepr = PyObject_Repr(pydata))) { | |
181 | ERR("Invalid Python object."); | |
d7d693b5 | 182 | return; |
b7e15e0e BV |
183 | } |
184 | s = py_str_as_str(pyrepr); | |
185 | Py_DecRef(pyrepr); | |
186 | ||
187 | /* Output format for testing is '<ss>-<es> <inst-id>: <repr>\n' */ | |
188 | out = g_string_sized_new(128); | |
189 | g_string_printf(out, "%"PRIu64"-%"PRIu64" %s: %s\n", | |
190 | pdata->start_sample, pdata->end_sample, | |
191 | pdata->pdo->di->inst_id, s); | |
192 | g_free(s); | |
193 | if (write(op->outfd, out->str, out->len) == -1) | |
194 | ERR("SRD_OUTPUT_PYTHON callback write failure!"); | |
195 | DBG("wrote '%s'", out->str); | |
196 | g_string_free(out, TRUE); | |
197 | ||
198 | } | |
199 | ||
200 | static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data) | |
201 | { | |
202 | struct srd_proto_data_binary *pdb; | |
203 | struct output *op; | |
97578cb1 BV |
204 | GString *out; |
205 | unsigned int i; | |
d7d693b5 BV |
206 | |
207 | DBG("Binary output from %s", pdata->pdo->di->inst_id); | |
b7e15e0e | 208 | op = cb_data; |
d7d693b5 BV |
209 | pdb = pdata->data; |
210 | ||
211 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) | |
212 | /* This is not the PD selected for output. */ | |
213 | return; | |
214 | ||
215 | if (op->class_idx != -1 && op->class_idx != pdb->bin_class) | |
216 | /* | |
217 | * This output takes a specific binary class, | |
218 | * but not the one that just came in. | |
219 | */ | |
220 | return; | |
221 | ||
97578cb1 BV |
222 | out = g_string_sized_new(128); |
223 | g_string_printf(out, "%"PRIu64"-%"PRIu64" %s:", | |
224 | pdata->start_sample, pdata->end_sample, | |
225 | pdata->pdo->di->inst_id); | |
226 | for (i = 0; i < pdb->size; i++) { | |
227 | g_string_append_printf(out, " %.2x", pdb->data[i]); | |
228 | } | |
229 | g_string_append(out, "\n"); | |
230 | if (write(op->outfd, out->str, out->len) == -1) | |
b7e15e0e | 231 | ERR("SRD_OUTPUT_BINARY callback write failure!"); |
d7d693b5 BV |
232 | |
233 | } | |
234 | ||
fbd226c3 BV |
235 | static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data) |
236 | { | |
237 | struct srd_decoder *dec; | |
238 | struct srd_proto_data_annotation *pda; | |
239 | struct output *op; | |
240 | GString *line; | |
241 | int i; | |
242 | char **dec_ann; | |
243 | ||
d7d693b5 | 244 | DBG("Annotation output from %s", pdata->pdo->di->inst_id); |
b7e15e0e | 245 | op = cb_data; |
fbd226c3 BV |
246 | pda = pdata->data; |
247 | dec = pdata->pdo->di->decoder; | |
fbd226c3 BV |
248 | if (strcmp(pdata->pdo->di->inst_id, op->pd)) |
249 | /* This is not the PD selected for output. */ | |
250 | return; | |
251 | ||
252 | if (op->class_idx != -1 && op->class_idx != pda->ann_format) | |
d7d693b5 BV |
253 | /* |
254 | * This output takes a specific annotation class, | |
255 | * but not the one that just came in. | |
256 | */ | |
fbd226c3 BV |
257 | return; |
258 | ||
259 | dec_ann = g_slist_nth_data(dec->annotations, pda->ann_format); | |
260 | line = g_string_sized_new(256); | |
261 | g_string_printf(line, "%"PRIu64"-%"PRIu64" %s: %s:", | |
262 | pdata->start_sample, pdata->end_sample, | |
263 | pdata->pdo->di->inst_id, dec_ann[0]); | |
264 | for (i = 0; pda->ann_text[i]; i++) | |
265 | g_string_append_printf(line, " \"%s\"", pda->ann_text[i]); | |
266 | g_string_append(line, "\n"); | |
267 | if (write(op->outfd, line->str, line->len) == -1) | |
b7e15e0e | 268 | ERR("SRD_OUTPUT_ANN callback write failure!"); |
fbd226c3 BV |
269 | g_string_free(line, TRUE); |
270 | ||
271 | } | |
272 | ||
273 | static void sr_cb(const struct sr_dev_inst *sdi, | |
274 | const struct sr_datafeed_packet *packet, void *cb_data) | |
275 | { | |
276 | const struct sr_datafeed_logic *logic; | |
277 | struct srd_session *sess; | |
278 | GVariant *gvar; | |
279 | uint64_t samplerate; | |
280 | int num_samples; | |
281 | static int samplecnt = 0; | |
282 | ||
283 | sess = cb_data; | |
284 | ||
285 | switch (packet->type) { | |
286 | case SR_DF_HEADER: | |
287 | DBG("Received SR_DF_HEADER"); | |
288 | if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, | |
289 | &gvar) != SR_OK) { | |
290 | ERR("Getting samplerate failed"); | |
291 | break; | |
292 | } | |
293 | samplerate = g_variant_get_uint64(gvar); | |
294 | g_variant_unref(gvar); | |
295 | if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE, | |
296 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
297 | ERR("Setting samplerate failed"); | |
298 | break; | |
299 | } | |
300 | if (srd_session_start(sess) != SRD_OK) { | |
301 | ERR("Session start failed"); | |
302 | break; | |
303 | } | |
304 | break; | |
305 | case SR_DF_LOGIC: | |
306 | logic = packet->payload; | |
307 | num_samples = logic->length / logic->unitsize; | |
308 | DBG("Received SR_DF_LOGIC: %d samples", num_samples); | |
309 | srd_session_send(sess, samplecnt, samplecnt + num_samples, | |
310 | logic->data, logic->length); | |
311 | samplecnt += logic->length / logic->unitsize; | |
312 | break; | |
313 | case SR_DF_END: | |
314 | DBG("Received SR_DF_END"); | |
315 | break; | |
316 | } | |
317 | ||
318 | } | |
319 | ||
fbd226c3 BV |
320 | static int run_testcase(char *infile, GSList *pdlist, struct output *op) |
321 | { | |
322 | struct srd_session *sess; | |
323 | struct srd_decoder *dec; | |
324 | struct srd_decoder_inst *di, *prev_di; | |
b7e15e0e | 325 | srd_pd_output_callback_t cb; |
fbd226c3 BV |
326 | struct pd *pd; |
327 | struct probe *probe; | |
328 | struct option *option; | |
329 | GVariant *gvar; | |
330 | GHashTable *probes, *opts; | |
6b85745a | 331 | GSList *pdl, *l; |
fbd226c3 | 332 | int idx; |
9eec72cb | 333 | int max_probe; |
6b85745a | 334 | char **decoder_class; |
fbd226c3 BV |
335 | |
336 | if (op->outfile) { | |
337 | if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) { | |
338 | ERR("Unable to open %s for writing: %s", op->outfile, | |
339 | strerror(errno)); | |
340 | return FALSE; | |
341 | } | |
342 | } | |
343 | ||
344 | if (sr_session_load(infile) != SR_OK) | |
345 | return FALSE; | |
346 | ||
347 | if (srd_session_new(&sess) != SRD_OK) | |
348 | return FALSE; | |
349 | sr_session_datafeed_callback_add(sr_cb, sess); | |
b7e15e0e BV |
350 | switch (op->type) { |
351 | case SRD_OUTPUT_ANN: | |
352 | cb = srd_cb_ann; | |
353 | break; | |
354 | case SRD_OUTPUT_BINARY: | |
355 | cb = srd_cb_bin; | |
356 | break; | |
357 | case SRD_OUTPUT_PYTHON: | |
358 | cb = srd_cb_py; | |
359 | break; | |
360 | default: | |
361 | return FALSE; | |
362 | } | |
363 | srd_pd_output_callback_add(sess, op->type, cb, op); | |
fbd226c3 BV |
364 | |
365 | prev_di = NULL; | |
366 | pd = NULL; | |
367 | for (pdl = pdlist; pdl; pdl = pdl->next) { | |
368 | pd = pdl->data; | |
369 | if (srd_decoder_load(pd->name) != SRD_OK) | |
370 | return FALSE; | |
371 | ||
372 | /* Instantiate decoder and pass in options. */ | |
373 | opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
374 | (GDestroyNotify)g_variant_unref); | |
375 | for (l = pd->options; l; l = l->next) { | |
376 | option = l->data; | |
377 | g_hash_table_insert(opts, option->key, option->value); | |
378 | } | |
379 | if (!(di = srd_inst_new(sess, pd->name, opts))) | |
380 | return FALSE; | |
381 | g_hash_table_destroy(opts); | |
382 | ||
383 | /* Map probes. */ | |
384 | if (pd->probes) { | |
385 | probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
386 | (GDestroyNotify)g_variant_unref); | |
9eec72cb | 387 | max_probe = 0; |
fbd226c3 BV |
388 | for (l = pd->probes; l; l = l->next) { |
389 | probe = l->data; | |
9eec72cb DE |
390 | if (probe->probe > max_probe) |
391 | max_probe = probe->probe; | |
fbd226c3 BV |
392 | gvar = g_variant_new_int32(probe->probe); |
393 | g_variant_ref_sink(gvar); | |
394 | g_hash_table_insert(probes, probe->name, gvar); | |
395 | } | |
9eec72cb DE |
396 | if (srd_inst_probe_set_all(di, probes, |
397 | (max_probe + 8) / 8) != SRD_OK) | |
fbd226c3 BV |
398 | return FALSE; |
399 | g_hash_table_destroy(probes); | |
400 | } | |
401 | ||
fbd226c3 BV |
402 | /* If this is not the first decoder in the list, stack it |
403 | * on top of the previous one. */ | |
404 | if (prev_di) { | |
405 | if (srd_inst_stack(sess, prev_di, di) != SRD_OK) { | |
406 | ERR("Failed to stack decoder instances."); | |
407 | return FALSE; | |
408 | } | |
409 | } | |
410 | prev_di = di; | |
411 | } | |
412 | ||
413 | /* Resolve top decoder's class index, so we can match. */ | |
414 | dec = srd_decoder_get_by_id(pd->name); | |
415 | if (op->class) { | |
416 | if (op->type == SRD_OUTPUT_ANN) | |
6b85745a | 417 | l = dec->annotations; |
fbd226c3 | 418 | else if (op->type == SRD_OUTPUT_BINARY) |
6b85745a | 419 | l = dec->binary; |
fbd226c3 | 420 | else |
b7e15e0e | 421 | /* Only annotations and binary can have a class. */ |
fbd226c3 BV |
422 | return FALSE; |
423 | idx = 0; | |
2de277b8 | 424 | while (l) { |
6b85745a BV |
425 | decoder_class = l->data; |
426 | if (!strcmp(decoder_class[0], op->class)) { | |
fbd226c3 BV |
427 | op->class_idx = idx; |
428 | break; | |
429 | } else | |
430 | idx++; | |
6b85745a | 431 | l = l->next; |
fbd226c3 BV |
432 | } |
433 | if (op->class_idx == -1) { | |
434 | ERR("Output class '%s' not found in decoder %s.", | |
435 | op->class, pd->name); | |
436 | return FALSE; | |
d7d693b5 BV |
437 | } else |
438 | DBG("Class %s index is %d", op->class, op->class_idx); | |
fbd226c3 BV |
439 | } |
440 | ||
441 | sr_session_start(); | |
442 | sr_session_run(); | |
443 | sr_session_stop(); | |
444 | ||
445 | srd_session_destroy(sess); | |
446 | ||
447 | if (op->outfile) | |
448 | close(op->outfd); | |
449 | ||
450 | return TRUE; | |
451 | } | |
452 | ||
5bc679c6 BV |
453 | static PyObject *start_coverage(GSList *pdlist) |
454 | { | |
455 | PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov; | |
456 | GSList *l; | |
457 | struct pd *pd; | |
458 | ||
459 | DBG("Starting coverage."); | |
460 | ||
461 | if (!(py_mod = PyImport_ImportModule("coverage"))) | |
462 | return NULL; | |
463 | ||
464 | if (!(py_pdlist = PyList_New(0))) | |
465 | return NULL; | |
466 | for (l = pdlist; l; l = l->next) { | |
467 | pd = l->data; | |
468 | py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name); | |
469 | if (PyList_Append(py_pdlist, py_pd) < 0) | |
470 | return NULL; | |
471 | Py_DecRef(py_pd); | |
472 | } | |
473 | if (!(py_func = PyObject_GetAttrString(py_mod, "coverage"))) | |
474 | return NULL; | |
475 | if (!(py_args = PyTuple_New(0))) | |
476 | return NULL; | |
477 | if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist))) | |
478 | return NULL; | |
479 | if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs))) | |
480 | return NULL; | |
481 | if (!(PyObject_CallMethod(py_cov, "start", NULL))) | |
482 | return NULL; | |
483 | Py_DecRef(py_pdlist); | |
484 | Py_DecRef(py_args); | |
485 | Py_DecRef(py_kwargs); | |
486 | Py_DecRef(py_func); | |
487 | ||
488 | return py_cov; | |
489 | } | |
490 | ||
491 | struct cvg *get_mod_cov(PyObject *py_cov, char *module_name) | |
492 | { | |
493 | PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd, *py_result; | |
494 | DIR *d; | |
495 | struct dirent *de; | |
496 | struct cvg *cvg_mod; | |
497 | int lines, missed, i; | |
498 | char *path; | |
499 | ||
500 | if (!(py_mod = PyImport_ImportModule(module_name))) | |
501 | return NULL; | |
502 | ||
503 | cvg_mod = NULL; | |
504 | py_pathlist = PyObject_GetAttrString(py_mod, "__path__"); | |
505 | for (i = 0; i < PyList_Size(py_pathlist); i++) { | |
506 | py_path = PyList_GetItem(py_pathlist, i); | |
507 | path = PyUnicode_AsUTF8AndSize(py_path, NULL); | |
508 | if (!(d = opendir(path))) { | |
509 | ERR("Invalid module path '%s'", path); | |
510 | return NULL; | |
511 | } | |
512 | while ((de = readdir(d))) { | |
513 | if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3)) | |
514 | continue; | |
515 | ||
516 | if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2"))) | |
517 | return NULL; | |
518 | if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name))) | |
519 | return NULL; | |
520 | if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd))) | |
521 | return NULL; | |
522 | Py_DecRef(py_pd); | |
523 | Py_DecRef(py_func); | |
524 | ||
525 | if (!cvg_mod) | |
526 | cvg_mod = calloc(1, sizeof(struct cvg)); | |
527 | if (PyTuple_Size(py_result) != 5) { | |
528 | ERR("Invalid result from coverage of '%s/%s'", path, de->d_name); | |
529 | return NULL; | |
530 | } | |
531 | lines = PyList_Size(PyTuple_GetItem(py_result, 1)); | |
532 | missed = PyList_Size(PyTuple_GetItem(py_result, 3)); | |
533 | DBG("Coverage for %s/%s: %d lines, %d missed.", module_name, de->d_name, lines, missed); | |
534 | cvg_mod->lines += lines; | |
535 | cvg_mod->missed += missed; | |
536 | Py_DecRef(py_result); | |
537 | } | |
538 | } | |
539 | ||
540 | Py_DecRef(py_mod); | |
541 | ||
542 | return cvg_mod; | |
543 | } | |
544 | ||
545 | static int report_coverage(PyObject *py_cov, GSList *pdlist) | |
546 | { | |
547 | PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct; | |
548 | GSList *l; | |
549 | struct pd *pd; | |
550 | struct cvg *cvg_mod, *cvg_all; | |
551 | float total; | |
552 | ||
553 | DBG("Making coverage report."); | |
554 | ||
555 | /* Get coverage for each module in the stack. */ | |
556 | cvg_all = calloc(1, sizeof(struct cvg)); | |
557 | for (l = pdlist; l; l = l->next) { | |
558 | pd = l->data; | |
559 | if (!(cvg_mod = get_mod_cov(py_cov, pd->name))) | |
560 | return FALSE; | |
561 | cvg_all->lines += cvg_mod->lines; | |
562 | cvg_all->missed += cvg_mod->missed; | |
563 | DBG("Coverage for module %s: %d lines, %d missed", pd->name, | |
564 | cvg_mod->lines, cvg_mod->missed); | |
565 | } | |
566 | ||
567 | /* Machine-readable stats on stdout. */ | |
568 | total = 100 - ((float)cvg_all->missed / (float)cvg_all->lines * 100); | |
569 | printf("coverage: lines=%d missed=%d coverage=%.0f%%\n", | |
570 | cvg_all->lines, cvg_all->missed, total); | |
571 | ||
572 | /* Write text report to file. */ | |
573 | /* io.open(coverage_report, "w") */ | |
574 | if (!(py_mod = PyImport_ImportModule("io"))) | |
575 | return FALSE; | |
576 | if (!(py_func = PyObject_GetAttrString(py_mod, "open"))) | |
577 | return FALSE; | |
578 | if (!(py_args = PyTuple_New(0))) | |
579 | return FALSE; | |
580 | if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report, | |
581 | "mode", "w"))) | |
582 | return FALSE; | |
583 | if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs))) | |
584 | return FALSE; | |
585 | Py_DecRef(py_kwargs); | |
586 | Py_DecRef(py_func); | |
587 | ||
588 | /* py_cov.report(file=py_outfile) */ | |
589 | if (!(py_func = PyObject_GetAttrString(py_cov, "report"))) | |
590 | return FALSE; | |
591 | if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile))) | |
592 | return FALSE; | |
593 | if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs))) | |
594 | return FALSE; | |
595 | Py_DecRef(py_pct); | |
596 | Py_DecRef(py_kwargs); | |
597 | Py_DecRef(py_func); | |
598 | ||
599 | /* py_outfile.close() */ | |
600 | if (!(py_func = PyObject_GetAttrString(py_outfile, "close"))) | |
601 | return FALSE; | |
602 | if (!PyObject_Call(py_func, py_args, NULL)) | |
603 | return FALSE; | |
604 | Py_DecRef(py_outfile); | |
605 | Py_DecRef(py_func); | |
606 | Py_DecRef(py_args); | |
607 | Py_DecRef(py_mod); | |
608 | ||
609 | return TRUE; | |
610 | } | |
611 | ||
fbd226c3 BV |
612 | int main(int argc, char **argv) |
613 | { | |
614 | struct sr_context *ctx; | |
5bc679c6 | 615 | PyObject *coverage; |
fbd226c3 BV |
616 | GSList *pdlist; |
617 | struct pd *pd; | |
618 | struct probe *probe; | |
619 | struct option *option; | |
620 | struct output *op; | |
932606db | 621 | int ret; |
fbd226c3 BV |
622 | char c, *opt_infile, **kv, **opstr; |
623 | ||
624 | op = malloc(sizeof(struct output)); | |
625 | op->pd = NULL; | |
626 | op->type = -1; | |
627 | op->class = NULL; | |
628 | op->class_idx = -1; | |
629 | op->outfd = 1; | |
630 | ||
631 | pdlist = NULL; | |
632 | opt_infile = NULL; | |
633 | pd = NULL; | |
5bc679c6 BV |
634 | coverage = NULL; |
635 | while ((c = getopt(argc, argv, "dP:p:o:i:O:f:c:S")) != -1) { | |
fbd226c3 BV |
636 | switch(c) { |
637 | case 'd': | |
638 | debug = TRUE; | |
639 | break; | |
640 | case 'P': | |
641 | pd = g_malloc(sizeof(struct pd)); | |
642 | pd->name = g_strdup(optarg); | |
643 | pd->probes = pd->options = NULL; | |
644 | pdlist = g_slist_append(pdlist, pd); | |
645 | break; | |
646 | case 'p': | |
647 | case 'o': | |
648 | if (g_slist_length(pdlist) == 0) { | |
649 | /* No previous -P. */ | |
650 | ERR("Syntax error at '%s'", optarg); | |
651 | usage(NULL); | |
652 | } | |
653 | kv = g_strsplit(optarg, "=", 0); | |
654 | if (!kv[0] || (!kv[1] || kv[2])) { | |
655 | /* Need x=y. */ | |
656 | ERR("Syntax error at '%s'", optarg); | |
657 | g_strfreev(kv); | |
658 | usage(NULL); | |
659 | } | |
660 | if (c == 'p') { | |
661 | probe = malloc(sizeof(struct probe)); | |
662 | probe->name = g_strdup(kv[0]); | |
663 | probe->probe = strtoul(kv[1], 0, 10); | |
664 | /* Apply to last PD. */ | |
665 | pd->probes = g_slist_append(pd->probes, probe); | |
666 | } else { | |
667 | option = malloc(sizeof(struct option)); | |
668 | option->key = g_strdup(kv[0]); | |
669 | option->value = g_strdup(kv[1]); | |
670 | /* Apply to last PD. */ | |
671 | pd->options = g_slist_append(pd->options, option); | |
672 | } | |
673 | break; | |
674 | case 'i': | |
675 | opt_infile = optarg; | |
676 | break; | |
677 | case 'O': | |
678 | opstr = g_strsplit(optarg, ":", 0); | |
679 | if (!opstr[0] || !opstr[1]) { | |
680 | /* Need at least abc:def. */ | |
681 | ERR("Syntax error at '%s'", optarg); | |
682 | g_strfreev(opstr); | |
683 | usage(NULL); | |
684 | } | |
685 | op->pd = g_strdup(opstr[0]); | |
686 | if (!strcmp(opstr[1], "annotation")) | |
687 | op->type = SRD_OUTPUT_ANN; | |
688 | else if (!strcmp(opstr[1], "binary")) | |
689 | op->type = SRD_OUTPUT_BINARY; | |
690 | else if (!strcmp(opstr[1], "python")) | |
691 | op->type = SRD_OUTPUT_PYTHON; | |
692 | else { | |
693 | ERR("Unknown output type '%s'", opstr[1]); | |
694 | g_strfreev(opstr); | |
695 | usage(NULL); | |
696 | } | |
697 | if (opstr[2]) | |
698 | op->class = g_strdup(opstr[2]); | |
699 | g_strfreev(opstr); | |
700 | break; | |
701 | case 'f': | |
702 | op->outfile = g_strdup(optarg); | |
703 | op->outfd = -1; | |
704 | break; | |
5bc679c6 BV |
705 | case 'c': |
706 | coverage_report = optarg; | |
707 | break; | |
fbd226c3 BV |
708 | case 'S': |
709 | statistics = TRUE; | |
710 | break; | |
711 | default: | |
712 | usage(NULL); | |
713 | } | |
714 | } | |
715 | if (argc > optind) | |
716 | usage(NULL); | |
717 | if (g_slist_length(pdlist) == 0) | |
718 | usage(NULL); | |
719 | if (!opt_infile) | |
720 | usage(NULL); | |
721 | if (!op->pd || op->type == -1) | |
722 | usage(NULL); | |
723 | ||
caa4b2cc | 724 | sr_log_callback_set(sr_log, NULL); |
fbd226c3 BV |
725 | if (sr_init(&ctx) != SR_OK) |
726 | return 1; | |
727 | ||
728 | srd_log_callback_set(srd_log, NULL); | |
b7482381 | 729 | if (srd_init(DECODERS_DIR) != SRD_OK) |
fbd226c3 BV |
730 | return 1; |
731 | ||
5bc679c6 BV |
732 | if (coverage_report) { |
733 | if (!(coverage = start_coverage(pdlist))) { | |
734 | DBG("Failed to start coverage."); | |
735 | if (PyErr_Occurred()) { | |
736 | PyErr_PrintEx(0); | |
737 | PyErr_Clear(); | |
738 | } | |
739 | } | |
740 | } | |
741 | ||
932606db BV |
742 | ret = 0; |
743 | if (!run_testcase(opt_infile, pdlist, op)) | |
744 | ret = 1; | |
fbd226c3 | 745 | |
5bc679c6 BV |
746 | if (coverage) { |
747 | DBG("Stopping coverage."); | |
748 | ||
749 | if (!(PyObject_CallMethod(coverage, "stop", NULL))) | |
750 | ERR("Failed to stop coverage."); | |
751 | else if (!(report_coverage(coverage, pdlist))) | |
752 | ERR("Failed to make coverage report."); | |
753 | else | |
754 | DBG("Coverage report in %s", coverage_report); | |
755 | ||
756 | if (PyErr_Occurred()) { | |
757 | PyErr_PrintEx(0); | |
758 | PyErr_Clear(); | |
759 | } | |
760 | Py_DecRef(coverage); | |
761 | } | |
762 | ||
fbd226c3 BV |
763 | srd_exit(); |
764 | sr_exit(ctx); | |
765 | ||
932606db | 766 | return ret; |
fbd226c3 | 767 | } |