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