2 * This file is part of the sigrok-test 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/>.
23 #include <libsigrokdecode/libsigrokdecode.h>
24 #include <libsigrok/libsigrok.h>
30 #include <sys/types.h>
35 #include <sys/resource.h>
42 #define CHUNK_SIZE (4 * 1024 * 1024)
44 static int debug = FALSE;
45 static int statistics = FALSE;
46 static char *coverage_report;
47 static struct sr_context *ctx;
59 struct initial_pin_info {
95 static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name);
96 static void cvg_add(struct cvg *dst, const struct cvg *src);
97 static struct cvg *cvg_new(void);
98 static gboolean find_missed_line(struct cvg *cvg, const char *linespec);
100 static void logmsg(const char *prefix, FILE *out, const char *format, va_list args)
103 fprintf(out, "%s", prefix);
104 vfprintf(out, format, args);
108 static void DBG(const char *format, ...)
114 va_start(args, format);
115 logmsg("DBG: runtc: ", stdout, format, args);
119 static void ERR(const char *format, ...)
123 va_start(args, format);
124 logmsg("Error: ", stderr, format, args);
128 static int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
132 if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
133 logmsg("Error: sr: ", stderr, format, args);
135 logmsg("DBG: sr: ", stdout, format, args);
140 static int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
144 if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
145 logmsg("Error: srd: ", stderr, format, args);
147 logmsg("DBG: srd: ", stdout, format, args);
152 static void usage(const char *msg)
155 fprintf(stderr, "%s\n", msg);
157 printf("Usage: runtc [-dPpoiOfcS]\n");
158 printf(" -d (enables debug output)\n");
159 printf(" -P <protocol decoder>\n");
160 printf(" -p <channelname=channelnum> (optional)\n");
161 printf(" -o <channeloption=value> (optional)\n");
162 printf(" -N <channelname=initial-pin-value> (optional)\n");
163 printf(" -i <input file>\n");
164 printf(" -I <input format> (optional)\n");
165 printf(" -O <output-pd:output-type[:output-class]>\n");
166 printf(" -f <output file> (optional)\n");
167 printf(" -c <coverage report> (optional)\n");
168 printf(" -S (enables statistics)\n");
174 * This is a neutered version of libsigrokdecode's py_str_as_str(). It
175 * does no error checking, but then the only strings it processes are
176 * generated by Python's repr(), so are known good.
178 static char *py_str_as_str(const PyObject *py_str)
183 py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL);
184 str = PyBytes_AS_STRING(py_encstr);
185 outstr = g_strdup(str);
186 Py_DecRef(py_encstr);
192 * The following routines are callbacks for libsigrokdecode. They receive
193 * output from protocol decoders, optionally dropping data to only forward
194 * a selected decoder's or class' information. Output is written to either
195 * a specified file or stdout, an external process will compare captured
196 * output against expectations.
198 * Note that runtc(1) output emits the decoder "class" name instead of the
199 * instance name. So that generated output remains compatible with existing
200 * .output files which hold expected output of test cases. Without this
201 * approach, developers had to "anticipate" instance names from test.conf
202 * setups (and knowledge about internal implementation details of the srd
203 * library), and adjust .output files to reflect those names. Or specify
204 * instance names in each and every test.conf description (-o inst_id=ID).
206 * It's assumed that runtc(1) is used to check stacked decoders, but not
207 * multiple stacks in parallel and no stacks with multiple instances of
208 * decoders of the same type. When such configurations become desirable,
209 * runtc(1) needs to emit the instance name, and test configurations and
210 * output expectations need adjustment.
213 static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
216 PyObject *pydata, *pyrepr;
220 DBG("Python output from %s", pdata->pdo->di->inst_id);
222 pydata = pdata->data;
223 DBG("ptr %p", pydata);
225 if (strcmp(pdata->pdo->di->inst_id, op->pd_id))
226 /* This is not the PD selected for output. */
229 if (!(pyrepr = PyObject_Repr(pydata))) {
230 ERR("Invalid Python object.");
233 s = py_str_as_str(pyrepr);
236 /* Output format for testing is '<ss>-<es> <decoder-id>: <repr>\n'. */
237 out = g_string_sized_new(128);
238 g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s: %s\n",
239 pdata->start_sample, pdata->end_sample,
240 pdata->pdo->di->decoder->id, s);
242 if (write(op->outfd, out->str, out->len) == -1)
243 ERR("SRD_OUTPUT_PYTHON callback write failure!");
244 DBG("wrote '%s'", out->str);
245 g_string_free(out, TRUE);
249 static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
251 struct srd_proto_data_binary *pdb;
256 DBG("Binary output from %s", pdata->pdo->di->inst_id);
260 if (strcmp(pdata->pdo->di->inst_id, op->pd_id))
261 /* This is not the PD selected for output. */
264 if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
266 * This output takes a specific binary class,
267 * but not the one that just came in.
271 out = g_string_sized_new(128);
272 g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s:",
273 pdata->start_sample, pdata->end_sample,
274 pdata->pdo->di->decoder->id);
275 for (i = 0; i < pdb->size; i++) {
276 g_string_append_printf(out, " %.2x", pdb->data[i]);
278 g_string_append(out, "\n");
279 if (write(op->outfd, out->str, out->len) == -1)
280 ERR("SRD_OUTPUT_BINARY callback write failure!");
284 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
286 struct srd_decoder_inst *di;
287 struct srd_decoder *dec;
288 struct srd_proto_data_annotation *pda;
295 * Only inspect received annotations when they originate from
296 * the selected protocol decoder, and an optionally specified
297 * annotation class matches the received data.
303 DBG("Annotation output from %s", di->inst_id);
304 if (strcmp(di->inst_id, op->pd_id))
305 /* This is not the PD selected for output. */
308 if (op->class_idx != -1 && op->class_idx != pda->ann_class)
310 * This output takes a specific annotation class,
311 * but not the one that just came in.
316 * Print the annotation information in textual representation
317 * to the specified output file. Prefix the annotation strings
318 * with the start and end sample number, the decoder name, and
319 * the annotation name.
321 dec_ann = g_slist_nth_data(dec->annotations, pda->ann_class);
322 line = g_string_sized_new(256);
323 g_string_printf(line, "%" PRIu64 "-%" PRIu64 " %s: %s:",
324 pdata->start_sample, pdata->end_sample,
325 dec->id, dec_ann[0]);
326 for (i = 0; pda->ann_text[i]; i++)
327 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
328 g_string_append(line, "\n");
329 if (write(op->outfd, line->str, line->len) == -1)
330 ERR("SRD_OUTPUT_ANN callback write failure!");
331 g_string_free(line, TRUE);
335 static void sr_cb(const struct sr_dev_inst *sdi,
336 const struct sr_datafeed_packet *packet, void *cb_data)
338 static int samplecnt = 0;
339 static gboolean start_sent;
341 const struct sr_datafeed_logic *logic;
342 struct srd_session *sess;
343 const struct sr_datafeed_meta *meta;
344 struct sr_config *src;
350 struct sr_dev_driver *driver;
354 driver = sr_dev_inst_driver_get(sdi);
356 switch (packet->type) {
358 DBG("Received SR_DF_META");
359 meta = packet->payload;
360 for (l = meta->config; l; l = l->next) {
363 case SR_CONF_SAMPLERATE:
364 samplerate = g_variant_get_uint64(src->data);
365 ret = srd_session_metadata_set(sess,
367 g_variant_new_uint64(samplerate));
369 ERR("Setting samplerate failed (meta)");
378 DBG("Received SR_DF_HEADER");
379 if (sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
381 DBG("Getting samplerate failed (SR_DF_HEADER)");
384 samplerate = g_variant_get_uint64(gvar);
385 g_variant_unref(gvar);
386 ret = srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
387 g_variant_new_uint64(samplerate));
389 ERR("Setting samplerate failed (header)");
392 DBG("Received SR_DF_LOGIC");
394 if (srd_session_start(sess) != SRD_OK) {
395 ERR("Session start failed");
400 logic = packet->payload;
401 num_samples = logic->length / logic->unitsize;
402 DBG("Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
403 logic->length, logic->unitsize);
404 srd_session_send(sess, samplecnt, samplecnt + num_samples,
405 logic->data, logic->length, logic->unitsize);
406 samplecnt += num_samples;
409 DBG("Received SR_DF_END");
410 #if defined HAVE_SRD_SESSION_SEND_EOF && HAVE_SRD_SESSION_SEND_EOF
411 (void)srd_session_send_eof(sess);
418 static GHashTable *parse_input_options(const struct sr_option **pd_opts,
421 GHashTable *set_opts;
422 GVariant *pd_def, *gvar;
426 const char *spec_text, *s;
428 set_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
429 (GDestroyNotify)g_variant_unref);
430 for (idx = 0; pd_opts[idx]; idx++) {
431 pd_key = pd_opts[idx]->id;
432 /* Is the PD provided option in the set of input specs? */
434 for (l = user_specs; l; l = l->next) {
436 if (strncmp(spec_text, pd_key, strlen(pd_key)) != 0)
438 spec_text += strlen(pd_key);
440 /* Found 'key' up to end of text. */
444 if (*spec_text == '=') {
445 /* Found 'key=...', position to RHS value. */
453 * Normalize the input text for the user specified value.
454 * A key without an explicit value is useful for booleans.
459 * Convert the text to the PD default value's data type.
460 * Store the resulting variable in the hash which gets
461 * passed to the input module.
463 pd_def = pd_opts[idx]->def;
464 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_UINT32)) {
465 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
466 g_hash_table_insert(set_opts, g_strdup(pd_key),
467 g_variant_ref_sink(gvar));
470 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_INT32)) {
471 gvar = g_variant_new_int32(strtol(s, NULL, 10));
472 g_hash_table_insert(set_opts, g_strdup(pd_key),
473 g_variant_ref_sink(gvar));
476 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_UINT64)) {
477 gvar = g_variant_new_uint64(strtoull(s, NULL, 10));
478 g_hash_table_insert(set_opts, g_strdup(pd_key),
479 g_variant_ref_sink(gvar));
482 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_DOUBLE)) {
483 gvar = g_variant_new_double(strtod(s, NULL));
484 g_hash_table_insert(set_opts, g_strdup(pd_key),
485 g_variant_ref_sink(gvar));
488 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_STRING)) {
489 gvar = g_variant_new_string(s);
490 g_hash_table_insert(set_opts, g_strdup(pd_key),
491 g_variant_ref_sink(gvar));
494 if (g_variant_is_of_type(pd_def, G_VARIANT_TYPE_BOOLEAN)) {
496 if (strcmp(s, "false") == 0 || strcmp(s, "no") == 0) {
498 } else if (strcmp(s, "true") == 0 || strcmp(s, "yes") == 0) {
501 ERR("Cannot convert '%s' to boolean", s);
504 gvar = g_variant_new_boolean(b);
505 g_hash_table_insert(set_opts, g_strdup(pd_key),
506 g_variant_ref_sink(gvar));
509 ERR("Unsupported data type for option '%s'", pd_key);
516 static int run_testcase(struct input *inp, GSList *pdlist, struct output *op)
518 struct srd_session *sess;
519 struct srd_decoder *dec;
520 struct srd_decoder_inst *di, *prev_di;
521 srd_pd_output_callback cb;
523 struct channel *channel;
524 struct option *option;
526 GHashTable *channels, *opts;
527 GSList *pdl, *l, *l2, *devices;
530 char **decoder_class;
531 struct sr_session *sr_sess;
532 const struct sr_input *in;
533 const struct sr_input_module *imod;
534 const struct sr_option **options;
535 GHashTable *mod_opts;
538 GArray *initial_pins;
539 struct initial_pin_info *initial_pin;
542 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
543 ERR("Unable to open %s for writing: %s", op->outfile,
549 /* Tell "session files" (.sr format) from other input modules. */
552 if (inp->format && strcmp(inp->format, "match") == 0) {
553 /* Automatic format detection. */
555 ERR("Automatic file format won't take options");
558 ret = sr_input_scan_file(inp->filename, &in);
559 if (ret != SR_OK || !in) {
560 ERR("Cannot open input file (format match)");
563 sr_session_new(ctx, &sr_sess);
565 } else if (inp->format) {
566 /* Caller specified format, potentially with options. */
567 imod = sr_input_find(inp->format);
569 ERR("Cannot find specified input module");
573 options = sr_input_options_get(imod);
574 if (!options && inp->opt_list) {
575 ERR("Input module does not support options");
579 mod_opts = parse_input_options(options, inp->opt_list);
581 ERR("Cannot process input module options");
585 sr_input_options_free(options);
586 in = sr_input_new(imod, mod_opts);
588 ERR("Cannot create input module instance");
592 g_hash_table_destroy(mod_opts);
593 sr_session_new(ctx, &sr_sess);
596 /* No caller's format spec, assume .sr session file. */
597 ret = sr_session_load(ctx, inp->filename, &sr_sess);
598 if (ret != SR_OK || !sr_sess) {
599 ERR("Cannot open session file");
603 if (ret != SR_OK || !sr_sess) {
604 ERR("Failed to open input file");
608 /* Check file access early for non-session files. */
610 fd = open(inp->filename, O_RDONLY);
612 ERR("Cannot access input file (input module)");
618 sr_session_dev_list(sr_sess, &devices);
620 if (srd_session_new(&sess) != SRD_OK) {
621 ERR("srd_session_new() failed");
624 sr_session_datafeed_callback_add(sr_sess, sr_cb, sess);
629 case SRD_OUTPUT_BINARY:
632 case SRD_OUTPUT_PYTHON:
636 ERR("Invalid op->type");
639 srd_pd_output_callback_add(sess, op->type, cb, op);
643 for (pdl = pdlist; pdl; pdl = pdl->next) {
645 if (srd_decoder_load(pd->name) != SRD_OK) {
646 ERR("srd_decoder_load() failed");
650 /* Instantiate decoder and pass in options. */
651 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
652 (GDestroyNotify)g_variant_unref);
653 for (l = pd->options; l; l = l->next) {
657 s = g_variant_get_string(option->value, NULL);
658 for (i = 0; i < (int)strlen(s); i++) {
664 /* Integer option value */
665 g_hash_table_insert(opts, option->key,
666 g_variant_new_int64(strtoull(s, NULL, 10)));
668 /* String option value */
669 g_hash_table_insert(opts, option->key, option->value);
672 if (!(di = srd_inst_new(sess, pd->name, opts))) {
673 ERR("srd_inst_new() failed");
676 g_hash_table_destroy(opts);
679 * Get (a reference to) the decoder instance's ID if we
680 * are about to receive PD output from it. We need to
681 * filter output that carries the decoder instance's name.
683 if (strcmp(pd->name, op->pd) == 0) {
684 op->pd_id = di->inst_id;
685 DBG("Decoder of type \"%s\" has instance ID \"%s\".",
691 channels = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
692 (GDestroyNotify)g_variant_unref);
694 for (l = pd->channels; l; l = l->next) {
696 if (channel->channel > max_channel)
697 max_channel = channel->channel;
698 gvar = g_variant_new_int32(channel->channel);
699 g_variant_ref_sink(gvar);
700 g_hash_table_insert(channels, channel->name, gvar);
703 if (srd_inst_channel_set_all(di, channels) != SRD_OK) {
704 ERR("srd_inst_channel_set_all() failed");
707 g_hash_table_destroy(channels);
710 /* Set initial pins. */
711 if (pd->initial_pins) {
712 initial_pins = g_array_sized_new(FALSE, TRUE, sizeof(uint8_t),
713 di->dec_num_channels);
714 g_array_set_size(initial_pins, di->dec_num_channels);
715 memset(initial_pins->data, SRD_INITIAL_PIN_SAME_AS_SAMPLE0,
716 di->dec_num_channels);
718 for (l = pd->channels, idx = 0; l; l = l->next, idx++) {
720 for (l2 = pd->initial_pins; l2; l2 = l2->next) {
721 initial_pin = l2->data;
722 if (!strcmp(initial_pin->name, channel->name))
723 initial_pins->data[idx] = initial_pin->value;
727 if (srd_inst_initial_pins_set_all(di, initial_pins) != SRD_OK) {
728 ERR("srd_inst_initial_pins_set_all() failed");
731 g_array_free(initial_pins, TRUE);
735 * If this is not the first decoder in the list, stack it
736 * on top of the previous one.
739 if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
740 ERR("Failed to stack decoder instances.");
747 * Bail out if we haven't created an instance of the selected
748 * decoder type of which we shall grab output data from.
751 ERR("No / invalid decoder");
755 /* Resolve selected decoder's class index, so we can match. */
756 dec = srd_decoder_get_by_id(pd->name);
758 if (op->type == SRD_OUTPUT_ANN)
759 l = dec->annotations;
760 else if (op->type == SRD_OUTPUT_BINARY)
763 /* Only annotations and binary can have a class. */
764 ERR("Invalid decoder class");
769 decoder_class = l->data;
770 if (!strcmp(decoder_class[0], op->class_)) {
777 if (op->class_idx == -1) {
778 ERR("Output class '%s' not found in decoder %s.",
779 op->class_, pd->name);
782 DBG("Class %s index is %d", op->class_, op->class_idx);
786 * Run a convenience sequence for session files. Run a custom
787 * read loop for non-native file formats (import modules).
789 * TODO Ideally the libsigrok library would provide transparent
790 * access to either kind of file. Either wrap an input module
791 * similar to "session files", or turn the currently "special"
792 * session file into a regular input module. It's unfortunate
793 * that multiple applications need to re-invent this logic.
796 sr_session_start(sr_sess);
797 sr_session_run(sr_sess);
798 sr_session_stop(sr_sess);
803 struct sr_dev_inst *sdi;
806 fd = open(inp->filename, O_RDONLY);
808 ERR("Cannot open input file (read loop)");
811 buf = g_string_sized_new(CHUNK_SIZE);
814 g_string_truncate(buf, 0);
815 len = read(fd, buf->str, buf->allocated_len);
817 ERR("Cannot read from input file (read loop)");
823 ret = sr_input_send(in, buf);
825 ERR("Cannot process input file content");
829 sdi = sr_input_dev_inst_get(in);
830 if (!got_sdi && sdi) {
831 /* First time we got a valid sdi. */
832 if (sr_session_dev_add(sr_sess, sdi) != SR_OK) {
833 ERR("Cannot use device after sdi creation");
841 g_string_free(buf, TRUE);
843 sr_session_destroy(sr_sess);
846 srd_session_destroy(sess);
854 static PyObject *start_coverage(GSList *pdlist)
856 PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov;
860 DBG("Starting coverage.");
862 if (!(py_mod = PyImport_ImportModule("coverage")))
865 if (!(py_pdlist = PyList_New(0)))
867 for (l = pdlist; l; l = l->next) {
869 py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name);
870 if (PyList_Append(py_pdlist, py_pd) < 0)
874 if (!(py_func = PyObject_GetAttrString(py_mod, "coverage")))
876 if (!(py_args = PyTuple_New(0)))
878 if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist)))
880 if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs)))
882 if (!(PyObject_CallMethod(py_cov, "start", NULL)))
884 Py_DecRef(py_pdlist);
886 Py_DecRef(py_kwargs);
892 static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name)
894 PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd;
895 PyObject *py_result, *py_missed, *py_item;
899 int num_lines, num_missed, linenum, i, j;
900 char *path, *linespec;
902 if (!(py_mod = PyImport_ImportModule(module_name)))
906 py_pathlist = PyObject_GetAttrString(py_mod, "__path__");
907 for (i = 0; i < PyList_Size(py_pathlist); i++) {
908 py_path = PyList_GetItem(py_pathlist, i);
909 PyUnicode_FSConverter(PyList_GetItem(py_pathlist, i), &py_path);
910 path = PyBytes_AS_STRING(py_path);
911 if (!(d = opendir(path))) {
912 ERR("Invalid module path '%s'", path);
915 while ((de = readdir(d))) {
916 if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3))
919 if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2")))
921 if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name)))
923 if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd)))
930 if (PyTuple_Size(py_result) != 5) {
931 ERR("Invalid result from coverage of '%s/%s'", path, de->d_name);
934 num_lines = PyList_Size(PyTuple_GetItem(py_result, 1));
935 py_missed = PyTuple_GetItem(py_result, 3);
936 num_missed = PyList_Size(py_missed);
937 cvg_mod->num_lines += num_lines;
938 cvg_mod->num_missed += num_missed;
939 for (j = 0; j < num_missed; j++) {
940 py_item = PyList_GetItem(py_missed, j);
941 linenum = PyLong_AsLong(py_item);
942 linespec = g_strdup_printf("%s/%s:%d", module_name,
943 de->d_name, linenum);
944 cvg_mod->missed_lines = g_slist_append(cvg_mod->missed_lines, linespec);
946 DBG("Coverage for %s/%s: %d lines, %d missed.",
947 module_name, de->d_name, num_lines, num_missed);
948 Py_DecRef(py_result);
951 if (cvg_mod->num_lines)
952 cvg_mod->coverage = 100 - ((float)cvg_mod->num_missed / (float)cvg_mod->num_lines * 100);
960 static struct cvg *cvg_new(void)
964 cvg = calloc(1, sizeof(struct cvg));
969 static gboolean find_missed_line(struct cvg *cvg, const char *linespec)
973 for (l = cvg->missed_lines; l; l = l->next)
974 if (!strcmp(l->data, linespec))
980 static void cvg_add(struct cvg *dst, const struct cvg *src)
985 dst->num_lines += src->num_lines;
986 dst->num_missed += src->num_missed;
987 for (l = src->missed_lines; l; l = l->next) {
989 if (!find_missed_line(dst, linespec))
990 dst->missed_lines = g_slist_append(dst->missed_lines, linespec);
995 static int report_coverage(PyObject *py_cov, GSList *pdlist)
997 PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct;
1000 struct cvg *cvg_mod, *cvg_all;
1001 float total_coverage;
1002 int lines, missed, cnt;
1004 DBG("Making coverage report.");
1006 /* Get coverage for each module in the stack. */
1008 cvg_all = cvg_new();
1009 for (cnt = 0, l = pdlist; l; l = l->next, cnt++) {
1011 if (!(cvg_mod = get_mod_cov(py_cov, pd->name)))
1013 printf("coverage: scope=%s coverage=%.0f%% lines=%d missed=%d "
1014 "missed_lines=", pd->name, cvg_mod->coverage,
1015 cvg_mod->num_lines, cvg_mod->num_missed);
1016 for (ml = cvg_mod->missed_lines; ml; ml = ml->next) {
1017 if (ml != cvg_mod->missed_lines)
1019 printf("%s", (char *)ml->data);
1022 lines += cvg_mod->num_lines;
1023 missed += cvg_mod->num_missed;
1024 cvg_add(cvg_all, cvg_mod);
1025 DBG("Coverage for module %s: %d lines, %d missed", pd->name,
1026 cvg_mod->num_lines, cvg_mod->num_missed);
1030 total_coverage = 100 - ((float)missed / (float)lines * 100);
1032 /* Machine-readable stats on stdout. */
1033 printf("coverage: scope=all coverage=%.0f%% lines=%d missed=%d\n",
1034 total_coverage, cvg_all->num_lines, cvg_all->num_missed);
1036 /* Write text report to file. */
1037 /* io.open(coverage_report, "w") */
1038 if (!(py_mod = PyImport_ImportModule("io")))
1040 if (!(py_func = PyObject_GetAttrString(py_mod, "open")))
1042 if (!(py_args = PyTuple_New(0)))
1044 if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report,
1047 if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs)))
1049 Py_DecRef(py_kwargs);
1052 /* py_cov.report(file=py_outfile) */
1053 if (!(py_func = PyObject_GetAttrString(py_cov, "report")))
1055 if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile)))
1057 if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs)))
1060 Py_DecRef(py_kwargs);
1063 /* py_outfile.close() */
1064 if (!(py_func = PyObject_GetAttrString(py_outfile, "close")))
1066 if (!PyObject_Call(py_func, py_args, NULL))
1068 Py_DecRef(py_outfile);
1076 int main(int argc, char **argv)
1081 struct channel *channel;
1082 struct option *option;
1087 struct initial_pin_info *initial_pin;
1089 inp = malloc(sizeof(*inp));
1090 inp->filename = NULL;
1092 inp->opt_list = NULL;
1093 inp->opt_hash = NULL;
1095 op = malloc(sizeof(struct output));
1106 while ((c = getopt(argc, argv, "dP:p:o:N:i:I:O:f:c:S")) != -1) {
1112 pd = g_malloc(sizeof(struct pd));
1113 pd->name = g_strdup(optarg);
1114 pd->channels = pd->options = pd->initial_pins = NULL;
1115 pdlist = g_slist_append(pdlist, pd);
1120 if (g_slist_length(pdlist) == 0) {
1121 /* No previous -P. */
1122 ERR("Syntax error at '%s'", optarg);
1125 kv = g_strsplit(optarg, "=", 0);
1126 if (!kv[0] || (!kv[1] || kv[2])) {
1128 ERR("Syntax error at '%s'", optarg);
1133 channel = malloc(sizeof(struct channel));
1134 channel->name = g_strdup(kv[0]);
1135 channel->channel = strtoul(kv[1], NULL, 10);
1136 /* Apply to last PD. */
1137 pd->channels = g_slist_append(pd->channels, channel);
1138 } else if (c == 'o') {
1139 option = malloc(sizeof(struct option));
1140 option->key = g_strdup(kv[0]);
1141 option->value = g_variant_new_string(kv[1]);
1142 g_variant_ref_sink(option->value);
1143 /* Apply to last PD. */
1144 pd->options = g_slist_append(pd->options, option);
1146 initial_pin = malloc(sizeof(struct initial_pin_info));
1147 initial_pin->name = g_strdup(kv[0]);
1148 initial_pin->value = strtoul(kv[1], NULL, 10);
1149 /* Apply to last PD. */
1150 pd->initial_pins = g_slist_append(pd->initial_pins, initial_pin);
1154 inp->filename = optarg;
1157 /* First arg is the format name, others are options. */
1159 inp->format = optarg;
1162 inp->opt_list = g_slist_append(inp->opt_list, optarg);
1165 opstr = g_strsplit(optarg, ":", 0);
1166 if (!opstr[0] || !opstr[1]) {
1167 /* Need at least abc:def. */
1168 ERR("Syntax error at '%s'", optarg);
1172 op->pd = g_strdup(opstr[0]);
1173 if (!strcmp(opstr[1], "annotation"))
1174 op->type = SRD_OUTPUT_ANN;
1175 else if (!strcmp(opstr[1], "binary"))
1176 op->type = SRD_OUTPUT_BINARY;
1177 else if (!strcmp(opstr[1], "python"))
1178 op->type = SRD_OUTPUT_PYTHON;
1179 else if (!strcmp(opstr[1], "exception"))
1180 /* Doesn't matter, we just need it to bomb out. */
1181 op->type = SRD_OUTPUT_PYTHON;
1183 ERR("Unknown output type '%s'", opstr[1]);
1188 op->class_ = g_strdup(opstr[2]);
1192 op->outfile = g_strdup(optarg);
1196 coverage_report = optarg;
1207 if (g_slist_length(pdlist) == 0)
1211 if (!op->pd || op->type == -1)
1214 sr_log_callback_set(sr_log, NULL);
1215 if (sr_init(&ctx) != SR_OK)
1218 srd_log_callback_set(srd_log, NULL);
1219 if (srd_init(DECODERS_DIR) != SRD_OK)
1222 if (coverage_report) {
1223 if (!(coverage = start_coverage(pdlist))) {
1224 DBG("Failed to start coverage.");
1225 if (PyErr_Occurred()) {
1233 if (!run_testcase(inp, pdlist, op))
1237 DBG("Stopping coverage.");
1239 if (!(PyObject_CallMethod(coverage, "stop", NULL)))
1240 ERR("Failed to stop coverage.");
1241 else if (!(report_coverage(coverage, pdlist)))
1242 ERR("Failed to make coverage report.");
1244 DBG("Coverage report in %s", coverage_report);
1246 if (PyErr_Occurred()) {
1250 Py_DecRef(coverage);