2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include "../libsigrokdecode.h"
21 #include <libsigrok/libsigrok.h>
27 #include <sys/types.h>
32 #include <sys/resource.h>
38 #include "../config.h"
41 int statistics = FALSE;
42 char *coverage_report;
75 struct cvg *get_mod_cov(PyObject *py_cov, char *module_name);
78 static void logmsg(char *prefix, FILE *out, const char *format, va_list args)
81 fprintf(out, "%s", prefix);
82 vfprintf(out, format, args);
86 static void DBG(const char *format, ...)
92 va_start(args, format);
93 logmsg("DBG: runtc: ", stdout, format, args);
97 static void ERR(const char *format, ...)
101 va_start(args, format);
102 logmsg("Error: ", stderr, format, args);
106 static int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
110 if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
111 logmsg("Error: sr: ", stderr, format, args);
113 logmsg("DBG: sr: ", stdout, format, args);
118 static int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
122 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
123 logmsg("Error: srd: ", stderr, format, args);
125 logmsg("DBG: srd: ", stdout, format, args);
130 static void usage(char *msg)
133 fprintf(stderr, "%s\n", msg);
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");
143 printf(" -c <coverage report> (optional)\n");
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. */
151 static char *py_str_as_str(const PyObject *py_str)
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);
164 static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
167 PyObject *pydata, *pyrepr;
171 DBG("Python output from %s", pdata->pdo->di->inst_id);
173 pydata = pdata->data;
174 DBG("ptr %p", pydata);
176 if (strcmp(pdata->pdo->di->inst_id, op->pd))
177 /* This is not the PD selected for output. */
180 if (!(pyrepr = PyObject_Repr(pydata))) {
181 ERR("Invalid Python object.");
184 s = py_str_as_str(pyrepr);
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);
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);
200 static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
202 struct srd_proto_data_binary *pdb;
207 DBG("Binary output from %s", pdata->pdo->di->inst_id);
211 if (strcmp(pdata->pdo->di->inst_id, op->pd))
212 /* This is not the PD selected for output. */
215 if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
217 * This output takes a specific binary class,
218 * but not the one that just came in.
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]);
229 g_string_append(out, "\n");
230 if (write(op->outfd, out->str, out->len) == -1)
231 ERR("SRD_OUTPUT_BINARY callback write failure!");
235 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
237 struct srd_decoder *dec;
238 struct srd_proto_data_annotation *pda;
244 DBG("Annotation output from %s", pdata->pdo->di->inst_id);
247 dec = pdata->pdo->di->decoder;
248 if (strcmp(pdata->pdo->di->inst_id, op->pd))
249 /* This is not the PD selected for output. */
252 if (op->class_idx != -1 && op->class_idx != pda->ann_format)
254 * This output takes a specific annotation class,
255 * but not the one that just came in.
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)
268 ERR("SRD_OUTPUT_ANN callback write failure!");
269 g_string_free(line, TRUE);
273 static void sr_cb(const struct sr_dev_inst *sdi,
274 const struct sr_datafeed_packet *packet, void *cb_data)
276 const struct sr_datafeed_logic *logic;
277 struct srd_session *sess;
281 static int samplecnt = 0;
285 switch (packet->type) {
287 DBG("Received SR_DF_HEADER");
288 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
290 ERR("Getting samplerate failed");
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");
300 if (srd_session_start(sess) != SRD_OK) {
301 ERR("Session start failed");
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;
314 DBG("Received SR_DF_END");
320 static int run_testcase(char *infile, GSList *pdlist, struct output *op)
322 struct srd_session *sess;
323 struct srd_decoder *dec;
324 struct srd_decoder_inst *di, *prev_di;
325 srd_pd_output_callback_t cb;
328 struct option *option;
330 GHashTable *probes, *opts;
334 char **decoder_class;
337 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
338 ERR("Unable to open %s for writing: %s", op->outfile,
344 if (sr_session_load(infile) != SR_OK)
347 if (srd_session_new(&sess) != SRD_OK)
349 sr_session_datafeed_callback_add(sr_cb, sess);
354 case SRD_OUTPUT_BINARY:
357 case SRD_OUTPUT_PYTHON:
363 srd_pd_output_callback_add(sess, op->type, cb, op);
367 for (pdl = pdlist; pdl; pdl = pdl->next) {
369 if (srd_decoder_load(pd->name) != SRD_OK)
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) {
377 g_hash_table_insert(opts, option->key, option->value);
379 if (!(di = srd_inst_new(sess, pd->name, opts)))
381 g_hash_table_destroy(opts);
385 probes = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
386 (GDestroyNotify)g_variant_unref);
388 for (l = pd->probes; l; l = l->next) {
390 if (probe->probe > max_probe)
391 max_probe = probe->probe;
392 gvar = g_variant_new_int32(probe->probe);
393 g_variant_ref_sink(gvar);
394 g_hash_table_insert(probes, probe->name, gvar);
396 if (srd_inst_probe_set_all(di, probes,
397 (max_probe + 8) / 8) != SRD_OK)
399 g_hash_table_destroy(probes);
402 /* If this is not the first decoder in the list, stack it
403 * on top of the previous one. */
405 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
406 ERR("Failed to stack decoder instances.");
413 /* Resolve top decoder's class index, so we can match. */
414 dec = srd_decoder_get_by_id(pd->name);
416 if (op->type == SRD_OUTPUT_ANN)
417 l = dec->annotations;
418 else if (op->type == SRD_OUTPUT_BINARY)
421 /* Only annotations and binary can have a class. */
425 decoder_class = l->data;
426 if (!strcmp(decoder_class[0], op->class)) {
433 if (op->class_idx == -1) {
434 ERR("Output class '%s' not found in decoder %s.",
435 op->class, pd->name);
438 DBG("Class %s index is %d", op->class, op->class_idx);
445 srd_session_destroy(sess);
453 static PyObject *start_coverage(GSList *pdlist)
455 PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov;
459 DBG("Starting coverage.");
461 if (!(py_mod = PyImport_ImportModule("coverage")))
464 if (!(py_pdlist = PyList_New(0)))
466 for (l = pdlist; l; l = l->next) {
468 py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name);
469 if (PyList_Append(py_pdlist, py_pd) < 0)
473 if (!(py_func = PyObject_GetAttrString(py_mod, "coverage")))
475 if (!(py_args = PyTuple_New(0)))
477 if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist)))
479 if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs)))
481 if (!(PyObject_CallMethod(py_cov, "start", NULL)))
483 Py_DecRef(py_pdlist);
485 Py_DecRef(py_kwargs);
491 struct cvg *get_mod_cov(PyObject *py_cov, char *module_name)
493 PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd, *py_result;
497 int lines, missed, i;
500 if (!(py_mod = PyImport_ImportModule(module_name)))
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);
512 while ((de = readdir(d))) {
513 if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3))
516 if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2")))
518 if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name)))
520 if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd)))
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);
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);
545 static int report_coverage(PyObject *py_cov, GSList *pdlist)
547 PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct;
550 struct cvg *cvg_mod, *cvg_all;
553 DBG("Making coverage report.");
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) {
559 if (!(cvg_mod = get_mod_cov(py_cov, pd->name)))
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);
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);
572 /* Write text report to file. */
573 /* io.open(coverage_report, "w") */
574 if (!(py_mod = PyImport_ImportModule("io")))
576 if (!(py_func = PyObject_GetAttrString(py_mod, "open")))
578 if (!(py_args = PyTuple_New(0)))
580 if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report,
583 if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs)))
585 Py_DecRef(py_kwargs);
588 /* py_cov.report(file=py_outfile) */
589 if (!(py_func = PyObject_GetAttrString(py_cov, "report")))
591 if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile)))
593 if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs)))
596 Py_DecRef(py_kwargs);
599 /* py_outfile.close() */
600 if (!(py_func = PyObject_GetAttrString(py_outfile, "close")))
602 if (!PyObject_Call(py_func, py_args, NULL))
604 Py_DecRef(py_outfile);
612 int main(int argc, char **argv)
614 struct sr_context *ctx;
619 struct option *option;
622 char c, *opt_infile, **kv, **opstr;
624 op = malloc(sizeof(struct output));
635 while ((c = getopt(argc, argv, "dP:p:o:i:O:f:c:S")) != -1) {
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);
648 if (g_slist_length(pdlist) == 0) {
649 /* No previous -P. */
650 ERR("Syntax error at '%s'", optarg);
653 kv = g_strsplit(optarg, "=", 0);
654 if (!kv[0] || (!kv[1] || kv[2])) {
656 ERR("Syntax error at '%s'", optarg);
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);
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);
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);
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;
693 ERR("Unknown output type '%s'", opstr[1]);
698 op->class = g_strdup(opstr[2]);
702 op->outfile = g_strdup(optarg);
706 coverage_report = optarg;
717 if (g_slist_length(pdlist) == 0)
721 if (!op->pd || op->type == -1)
724 sr_log_callback_set(sr_log, NULL);
725 if (sr_init(&ctx) != SR_OK)
728 srd_log_callback_set(srd_log, NULL);
729 if (srd_init(DECODERS_DIR) != SRD_OK)
732 if (coverage_report) {
733 if (!(coverage = start_coverage(pdlist))) {
734 DBG("Failed to start coverage.");
735 if (PyErr_Occurred()) {
743 if (!run_testcase(opt_infile, pdlist, op))
747 DBG("Stopping coverage.");
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.");
754 DBG("Coverage report in %s", coverage_report);
756 if (PyErr_Occurred()) {