runtc: print errors at more failure points
[sigrok-test.git] / decoder / runtc.c
1 /*
2  * This file is part of the sigrok-test 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/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
40 static int debug = FALSE;
41 static int statistics = FALSE;
42 static char *coverage_report;
43 static struct sr_context *ctx;
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 initial_pin_info {
56         char *name;
57         int value;
58 };
59
60 struct pd {
61         const char *name;
62         GSList *channels;
63         GSList *options;
64         GSList *initial_pins;
65 };
66
67 struct output {
68         const char *pd;
69         const char *pd_id;
70         int type;
71         const char *class;
72         int class_idx;
73         const char *outfile;
74         int outfd;
75 };
76
77 struct cvg {
78         int num_lines;
79         int num_missed;
80         float coverage;
81         GSList *missed_lines;
82 };
83
84 static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name);
85 static void cvg_add(struct cvg *dst, const struct cvg *src);
86 static struct cvg *cvg_new(void);
87 static gboolean find_missed_line(struct cvg *cvg, const char *linespec);
88
89 static void logmsg(const char *prefix, FILE *out, const char *format, va_list args)
90 {
91         if (prefix)
92                 fprintf(out, "%s", prefix);
93         vfprintf(out, format, args);
94         fprintf(out, "\n");
95 }
96
97 static void DBG(const char *format, ...)
98 {
99         va_list args;
100
101         if (!debug)
102                 return;
103         va_start(args, format);
104         logmsg("DBG: runtc: ", stdout, format, args);
105         va_end(args);
106 }
107
108 static void ERR(const char *format, ...)
109 {
110         va_list args;
111
112         va_start(args, format);
113         logmsg("Error: ", stderr, format, args);
114         va_end(args);
115 }
116
117 static int sr_log(void *cb_data, int loglevel, const char *format, va_list args)
118 {
119         (void)cb_data;
120
121         if (loglevel == SR_LOG_ERR || loglevel == SR_LOG_WARN)
122                 logmsg("Error: sr: ", stderr, format, args);
123         else if (debug)
124                 logmsg("DBG: sr: ", stdout, format, args);
125
126         return SRD_OK;
127 }
128
129 static int srd_log(void *cb_data, int loglevel, const char *format, va_list args)
130 {
131         (void)cb_data;
132
133         if (loglevel == SRD_LOG_ERR || loglevel == SRD_LOG_WARN)
134                 logmsg("Error: srd: ", stderr, format, args);
135         else if (debug)
136                 logmsg("DBG: srd: ", stdout, format, args);
137
138         return SRD_OK;
139 }
140
141 static void usage(const char *msg)
142 {
143         if (msg)
144                 fprintf(stderr, "%s\n", msg);
145
146         printf("Usage: runtc [-dPpoiOfcS]\n");
147         printf("  -d  (enables debug output)\n");
148         printf("  -P <protocol decoder>\n");
149         printf("  -p <channelname=channelnum> (optional)\n");
150         printf("  -o <channeloption=value> (optional)\n");
151         printf("  -N <channelname=initial-pin-value> (optional)\n");
152         printf("  -i <input file>\n");
153         printf("  -O <output-pd:output-type[:output-class]>\n");
154         printf("  -f <output file> (optional)\n");
155         printf("  -c <coverage report> (optional)\n");
156         printf("  -S  (enables statistics)\n");
157         exit(msg ? 1 : 0);
158
159 }
160
161 /*
162  * This is a neutered version of libsigrokdecode's py_str_as_str(). It
163  * does no error checking, but then the only strings it processes are
164  * generated by Python's repr(), so are known good.
165  */
166 static char *py_str_as_str(const PyObject *py_str)
167 {
168         PyObject *py_encstr;
169         char *str, *outstr;
170
171         py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, "utf-8", NULL);
172         str = PyBytes_AS_STRING(py_encstr);
173         outstr = g_strdup(str);
174         Py_DecRef(py_encstr);
175
176         return outstr;
177 }
178
179 /*
180  * The following routines are callbacks for libsigrokdecode. They receive
181  * output from protocol decoders, optionally dropping data to only forward
182  * a selected decoder's or class' information. Output is written to either
183  * a specified file or stdout, an external process will compare captured
184  * output against expectations.
185  *
186  * Note that runtc(1) output emits the decoder "class" name instead of the
187  * instance name. So that generated output remains compatible with existing
188  * .output files which hold expected output of test cases. Without this
189  * approach, developers had to "anticipate" instance names from test.conf
190  * setups (and knowledge about internal implementation details of the srd
191  * library), and adjust .output files to reflect those names. Or specify
192  * instance names in each and every test.conf description (-o inst_id=ID).
193  *
194  * It's assumed that runtc(1) is used to check stacked decoders, but not
195  * multiple stacks in parallel and no stacks with multiple instances of
196  * decoders of the same type. When such configurations become desirable,
197  * runtc(1) needs to emit the instance name, and test configurations and
198  * output expectations need adjustment.
199  */
200
201 static void srd_cb_py(struct srd_proto_data *pdata, void *cb_data)
202 {
203         struct output *op;
204         PyObject *pydata, *pyrepr;
205         GString *out;
206         char *s;
207
208         DBG("Python output from %s", pdata->pdo->di->inst_id);
209         op = cb_data;
210         pydata = pdata->data;
211         DBG("ptr %p", pydata);
212
213         if (strcmp(pdata->pdo->di->inst_id, op->pd_id))
214                 /* This is not the PD selected for output. */
215                 return;
216
217         if (!(pyrepr = PyObject_Repr(pydata))) {
218                 ERR("Invalid Python object.");
219                 return;
220         }
221         s = py_str_as_str(pyrepr);
222         Py_DecRef(pyrepr);
223
224         /* Output format for testing is '<ss>-<es> <decoder-id>: <repr>\n'. */
225         out = g_string_sized_new(128);
226         g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s: %s\n",
227                         pdata->start_sample, pdata->end_sample,
228                         pdata->pdo->di->decoder->id, s);
229         g_free(s);
230         if (write(op->outfd, out->str, out->len) == -1)
231                 ERR("SRD_OUTPUT_PYTHON callback write failure!");
232         DBG("wrote '%s'", out->str);
233         g_string_free(out, TRUE);
234
235 }
236
237 static void srd_cb_bin(struct srd_proto_data *pdata, void *cb_data)
238 {
239         struct srd_proto_data_binary *pdb;
240         struct output *op;
241         GString *out;
242         unsigned int i;
243
244         DBG("Binary output from %s", pdata->pdo->di->inst_id);
245         op = cb_data;
246         pdb = pdata->data;
247
248         if (strcmp(pdata->pdo->di->inst_id, op->pd_id))
249                 /* This is not the PD selected for output. */
250                 return;
251
252         if (op->class_idx != -1 && op->class_idx != pdb->bin_class)
253                 /*
254                  * This output takes a specific binary class,
255                  * but not the one that just came in.
256                  */
257                 return;
258
259         out = g_string_sized_new(128);
260         g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s:",
261                         pdata->start_sample, pdata->end_sample,
262                         pdata->pdo->di->decoder->id);
263         for (i = 0; i < pdb->size; i++) {
264                 g_string_append_printf(out, " %.2x", pdb->data[i]);
265         }
266         g_string_append(out, "\n");
267         if (write(op->outfd, out->str, out->len) == -1)
268                 ERR("SRD_OUTPUT_BINARY callback write failure!");
269
270 }
271
272 static void srd_cb_ann(struct srd_proto_data *pdata, void *cb_data)
273 {
274         struct srd_decoder_inst *di;
275         struct srd_decoder *dec;
276         struct srd_proto_data_annotation *pda;
277         struct output *op;
278         GString *line;
279         int i;
280         char **dec_ann;
281
282         /*
283          * Only inspect received annotations when they originate from
284          * the selected protocol decoder, and an optionally specified
285          * annotation class matches the received data.
286          */
287         op = cb_data;
288         pda = pdata->data;
289         di = pdata->pdo->di;
290         dec = di->decoder;
291         DBG("Annotation output from %s", di->inst_id);
292         if (strcmp(di->inst_id, op->pd_id))
293                 /* This is not the PD selected for output. */
294                 return;
295
296         if (op->class_idx != -1 && op->class_idx != pda->ann_class)
297                 /*
298                  * This output takes a specific annotation class,
299                  * but not the one that just came in.
300                  */
301                 return;
302
303         /*
304          * Print the annotation information in textual representation
305          * to the specified output file. Prefix the annotation strings
306          * with the start and end sample number, the decoder name, and
307          * the annotation name.
308          */
309         dec_ann = g_slist_nth_data(dec->annotations, pda->ann_class);
310         line = g_string_sized_new(256);
311         g_string_printf(line, "%" PRIu64 "-%" PRIu64 " %s: %s:",
312                         pdata->start_sample, pdata->end_sample,
313                         dec->id, dec_ann[0]);
314         for (i = 0; pda->ann_text[i]; i++)
315                 g_string_append_printf(line, " \"%s\"", pda->ann_text[i]);
316         g_string_append(line, "\n");
317         if (write(op->outfd, line->str, line->len) == -1)
318                 ERR("SRD_OUTPUT_ANN callback write failure!");
319         g_string_free(line, TRUE);
320
321 }
322
323 static void sr_cb(const struct sr_dev_inst *sdi,
324                 const struct sr_datafeed_packet *packet, void *cb_data)
325 {
326         static int samplecnt = 0;
327         const struct sr_datafeed_logic *logic;
328         struct srd_session *sess;
329         GVariant *gvar;
330         uint64_t samplerate;
331         int num_samples;
332         struct sr_dev_driver *driver;
333
334         sess = cb_data;
335
336         driver = sr_dev_inst_driver_get(sdi);
337
338         switch (packet->type) {
339         case SR_DF_HEADER:
340                 DBG("Received SR_DF_HEADER");
341                 if (sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
342                                 &gvar) != SR_OK) {
343                         ERR("Getting samplerate failed");
344                         break;
345                 }
346                 samplerate = g_variant_get_uint64(gvar);
347                 g_variant_unref(gvar);
348                 if (srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE,
349                                 g_variant_new_uint64(samplerate)) != SRD_OK) {
350                         ERR("Setting samplerate failed");
351                         break;
352                 }
353                 if (srd_session_start(sess) != SRD_OK) {
354                         ERR("Session start failed");
355                         break;
356                 }
357                 break;
358         case SR_DF_LOGIC:
359                 logic = packet->payload;
360                 num_samples = logic->length / logic->unitsize;
361                 DBG("Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
362                         logic->length, logic->unitsize);
363                 srd_session_send(sess, samplecnt, samplecnt + num_samples,
364                                 logic->data, logic->length, logic->unitsize);
365                 samplecnt += num_samples;
366                 break;
367         case SR_DF_END:
368                 DBG("Received SR_DF_END");
369                 break;
370         }
371
372 }
373
374 static int run_testcase(const char *infile, GSList *pdlist, struct output *op)
375 {
376         struct srd_session *sess;
377         struct srd_decoder *dec;
378         struct srd_decoder_inst *di, *prev_di;
379         srd_pd_output_callback cb;
380         struct pd *pd;
381         struct channel *channel;
382         struct option *option;
383         GVariant *gvar;
384         GHashTable *channels, *opts;
385         GSList *pdl, *l, *l2, *devices;
386         int idx, i;
387         int max_channel;
388         char **decoder_class;
389         struct sr_session *sr_sess;
390         gboolean is_number;
391         const char *s;
392         GArray *initial_pins;
393         struct initial_pin_info *initial_pin;
394
395         if (op->outfile) {
396                 if ((op->outfd = open(op->outfile, O_CREAT|O_WRONLY, 0600)) == -1) {
397                         ERR("Unable to open %s for writing: %s", op->outfile,
398                                         g_strerror(errno));
399                         return FALSE;
400                 }
401         }
402
403         if (sr_session_load(ctx, infile, &sr_sess) != SR_OK){
404                 ERR("sr_session_load() failed");
405                 return FALSE;
406         }
407
408         sr_session_dev_list(sr_sess, &devices);
409
410         if (srd_session_new(&sess) != SRD_OK) {
411                 ERR("srd_session_new() failed");
412                 return FALSE;
413         }
414         sr_session_datafeed_callback_add(sr_sess, sr_cb, sess);
415         switch (op->type) {
416         case SRD_OUTPUT_ANN:
417                 cb = srd_cb_ann;
418                 break;
419         case SRD_OUTPUT_BINARY:
420                 cb = srd_cb_bin;
421                 break;
422         case SRD_OUTPUT_PYTHON:
423                 cb = srd_cb_py;
424                 break;
425         default:
426                 ERR("Invalid op->type");
427                 return FALSE;
428         }
429         srd_pd_output_callback_add(sess, op->type, cb, op);
430
431         prev_di = NULL;
432         pd = NULL;
433         for (pdl = pdlist; pdl; pdl = pdl->next) {
434                 pd = pdl->data;
435                 if (srd_decoder_load(pd->name) != SRD_OK) {
436                         ERR("srd_decoder_load() failed");
437                         return FALSE;
438                 }
439
440                 /* Instantiate decoder and pass in options. */
441                 opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
442                                 (GDestroyNotify)g_variant_unref);
443                 for (l = pd->options; l; l = l->next) {
444                         option = l->data;
445
446                         is_number = TRUE;
447                         s = g_variant_get_string(option->value, NULL);
448                         for (i = 0; i < (int)strlen(s); i++) {
449                                 if (!isdigit(s[i]))
450                                         is_number = FALSE;
451                         }
452
453                         if (is_number) {
454                                 /* Integer option value */
455                                 g_hash_table_insert(opts, option->key,
456                                   g_variant_new_int64(strtoull(s, NULL, 10)));
457                         } else {
458                                 /* String option value */
459                                 g_hash_table_insert(opts, option->key, option->value);
460                         }
461                 }
462                 if (!(di = srd_inst_new(sess, pd->name, opts))) {
463                         ERR("srd_inst_new() failed");
464                         return FALSE;
465                 }
466                 g_hash_table_destroy(opts);
467
468                 /*
469                  * Get (a reference to) the decoder instance's ID if we
470                  * are about to receive PD output from it. We need to
471                  * filter output that carries the decoder instance's name.
472                  */
473                 if (strcmp(pd->name, op->pd) == 0) {
474                         op->pd_id = di->inst_id;
475                         DBG("Decoder of type \"%s\" has instance ID \"%s\".",
476                             op->pd, op->pd_id);
477                 }
478
479                 /* Map channels. */
480                 if (pd->channels) {
481                         channels = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
482                                         (GDestroyNotify)g_variant_unref);
483                         max_channel = 0;
484                         for (l = pd->channels; l; l = l->next) {
485                                 channel = l->data;
486                                 if (channel->channel > max_channel)
487                                         max_channel = channel->channel;
488                                 gvar = g_variant_new_int32(channel->channel);
489                                 g_variant_ref_sink(gvar);
490                                 g_hash_table_insert(channels, channel->name, gvar);
491                         }
492
493                         if (srd_inst_channel_set_all(di, channels) != SRD_OK) {
494                                 ERR("srd_inst_channel_set_all() failed");
495                                 return FALSE;
496                         }
497                         g_hash_table_destroy(channels);
498                 }
499
500                 /* Set initial pins. */
501                 if (pd->initial_pins) {
502                         initial_pins = g_array_sized_new(FALSE, TRUE, sizeof(uint8_t),
503                                                 di->dec_num_channels);
504                         g_array_set_size(initial_pins, di->dec_num_channels);
505                         memset(initial_pins->data, SRD_INITIAL_PIN_SAME_AS_SAMPLE0,
506                                 di->dec_num_channels);
507
508                         for (l = pd->channels, idx = 0; l; l = l->next, idx++) {
509                                 channel = l->data;
510                                 for (l2 = pd->initial_pins; l2; l2 = l2->next) {
511                                         initial_pin = l2->data;
512                                         if (!strcmp(initial_pin->name, channel->name))
513                                                 initial_pins->data[idx] = initial_pin->value;
514                                 }
515                         }
516
517                         if (srd_inst_initial_pins_set_all(di, initial_pins) != SRD_OK) {
518                                 ERR("srd_inst_initial_pins_set_all() failed");
519                                 return FALSE;
520                         }
521                         g_array_free(initial_pins, TRUE);
522                 }
523
524                 /*
525                  * If this is not the first decoder in the list, stack it
526                  * on top of the previous one.
527                  */
528                 if (prev_di) {
529                         if (srd_inst_stack(sess, prev_di, di) != SRD_OK) {
530                                 ERR("Failed to stack decoder instances.");
531                                 return FALSE;
532                         }
533                 }
534                 prev_di = di;
535         }
536         /*
537          * Bail out if we haven't created an instance of the selected
538          * decoder type of which we shall grab output data from.
539          */
540         if (!op->pd_id) {
541                 ERR("No / invalid decoder");
542                 return FALSE;
543         }
544
545         /* Resolve selected decoder's class index, so we can match. */
546         dec = srd_decoder_get_by_id(pd->name);
547         if (op->class) {
548                 if (op->type == SRD_OUTPUT_ANN)
549                         l = dec->annotations;
550                 else if (op->type == SRD_OUTPUT_BINARY)
551                         l = dec->binary;
552                 else {
553                         /* Only annotations and binary can have a class. */
554                         ERR("Invalid decoder class");
555                         return FALSE;
556                 }
557                 idx = 0;
558                 while (l) {
559                         decoder_class = l->data;
560                         if (!strcmp(decoder_class[0], op->class)) {
561                                 op->class_idx = idx;
562                                 break;
563                         }
564                         idx++;
565                         l = l->next;
566                 }
567                 if (op->class_idx == -1) {
568                         ERR("Output class '%s' not found in decoder %s.",
569                                         op->class, pd->name);
570                         return FALSE;
571                 }
572                 DBG("Class %s index is %d", op->class, op->class_idx);
573         }
574
575         sr_session_start(sr_sess);
576         sr_session_run(sr_sess);
577         sr_session_stop(sr_sess);
578
579         srd_session_destroy(sess);
580
581         if (op->outfile)
582                 close(op->outfd);
583
584         return TRUE;
585 }
586
587 static PyObject *start_coverage(GSList *pdlist)
588 {
589         PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov;
590         GSList *l;
591         struct pd *pd;
592
593         DBG("Starting coverage.");
594
595         if (!(py_mod = PyImport_ImportModule("coverage")))
596                 return NULL;
597
598         if (!(py_pdlist = PyList_New(0)))
599                 return NULL;
600         for (l = pdlist; l; l = l->next) {
601                 pd = l->data;
602                 py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name);
603                 if (PyList_Append(py_pdlist, py_pd) < 0)
604                         return NULL;
605                 Py_DecRef(py_pd);
606         }
607         if (!(py_func = PyObject_GetAttrString(py_mod, "coverage")))
608                 return NULL;
609         if (!(py_args = PyTuple_New(0)))
610                 return NULL;
611         if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist)))
612                 return NULL;
613         if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs)))
614                 return NULL;
615         if (!(PyObject_CallMethod(py_cov, "start", NULL)))
616                 return NULL;
617         Py_DecRef(py_pdlist);
618         Py_DecRef(py_args);
619         Py_DecRef(py_kwargs);
620         Py_DecRef(py_func);
621
622         return py_cov;
623 }
624
625 static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name)
626 {
627         PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd;
628         PyObject *py_result, *py_missed, *py_item;
629         DIR *d;
630         struct dirent *de;
631         struct cvg *cvg_mod;
632         int num_lines, num_missed, linenum, i, j;
633         char *path, *linespec;
634
635         if (!(py_mod = PyImport_ImportModule(module_name)))
636                 return NULL;
637
638         cvg_mod = NULL;
639         py_pathlist = PyObject_GetAttrString(py_mod, "__path__");
640         for (i = 0; i < PyList_Size(py_pathlist); i++) {
641                 py_path = PyList_GetItem(py_pathlist, i);
642                 PyUnicode_FSConverter(PyList_GetItem(py_pathlist, i), &py_path);
643                 path = PyBytes_AS_STRING(py_path);
644                 if (!(d = opendir(path))) {
645                         ERR("Invalid module path '%s'", path);
646                         return NULL;
647                 }
648                 while ((de = readdir(d))) {
649                         if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3))
650                                 continue;
651
652                         if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2")))
653                                 return NULL;
654                         if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name)))
655                                 return NULL;
656                         if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd)))
657                                 return NULL;
658                         Py_DecRef(py_pd);
659                         Py_DecRef(py_func);
660
661                         if (!cvg_mod)
662                                 cvg_mod = cvg_new();
663                         if (PyTuple_Size(py_result) != 5) {
664                                 ERR("Invalid result from coverage of '%s/%s'", path, de->d_name);
665                                 return NULL;
666                         }
667                         num_lines = PyList_Size(PyTuple_GetItem(py_result, 1));
668                         py_missed = PyTuple_GetItem(py_result, 3);
669                         num_missed = PyList_Size(py_missed);
670                         cvg_mod->num_lines += num_lines;
671                         cvg_mod->num_missed += num_missed;
672                         for (j = 0; j < num_missed; j++) {
673                                 py_item = PyList_GetItem(py_missed, j);
674                                 linenum = PyLong_AsLong(py_item);
675                                 linespec = g_strdup_printf("%s/%s:%d", module_name,
676                                                 de->d_name, linenum);
677                                 cvg_mod->missed_lines = g_slist_append(cvg_mod->missed_lines, linespec);
678                         }
679                         DBG("Coverage for %s/%s: %d lines, %d missed.",
680                                         module_name, de->d_name, num_lines, num_missed);
681                         Py_DecRef(py_result);
682                 }
683         }
684         if (cvg_mod->num_lines)
685                 cvg_mod->coverage = 100 - ((float)cvg_mod->num_missed / (float)cvg_mod->num_lines * 100);
686
687         Py_DecRef(py_mod);
688         Py_DecRef(py_path);
689
690         return cvg_mod;
691 }
692
693 static struct cvg *cvg_new(void)
694 {
695         struct cvg *cvg;
696
697         cvg = calloc(1, sizeof(struct cvg));
698
699         return cvg;
700 }
701
702 static gboolean find_missed_line(struct cvg *cvg, const char *linespec)
703 {
704         GSList *l;
705
706         for (l = cvg->missed_lines; l; l = l->next)
707                 if (!strcmp(l->data, linespec))
708                         return TRUE;
709
710         return FALSE;
711 }
712
713 static void cvg_add(struct cvg *dst, const struct cvg *src)
714 {
715         GSList *l;
716         char *linespec;
717
718         dst->num_lines += src->num_lines;
719         dst->num_missed += src->num_missed;
720         for (l = src->missed_lines; l; l = l->next) {
721                 linespec = l->data;
722                 if (!find_missed_line(dst, linespec))
723                         dst->missed_lines = g_slist_append(dst->missed_lines, linespec);
724         }
725
726 }
727
728 static int report_coverage(PyObject *py_cov, GSList *pdlist)
729 {
730         PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct;
731         GSList *l, *ml;
732         struct pd *pd;
733         struct cvg *cvg_mod, *cvg_all;
734         float total_coverage;
735         int lines, missed, cnt;
736
737         DBG("Making coverage report.");
738
739         /* Get coverage for each module in the stack. */
740         lines = missed = 0;
741         cvg_all = cvg_new();
742         for (cnt = 0, l = pdlist; l; l = l->next, cnt++) {
743                 pd = l->data;
744                 if (!(cvg_mod = get_mod_cov(py_cov, pd->name)))
745                         return FALSE;
746                 printf("coverage: scope=%s coverage=%.0f%% lines=%d missed=%d "
747                                 "missed_lines=", pd->name, cvg_mod->coverage,
748                                 cvg_mod->num_lines, cvg_mod->num_missed);
749                 for (ml = cvg_mod->missed_lines; ml; ml = ml->next) {
750                         if (ml != cvg_mod->missed_lines)
751                                 printf(",");
752                         printf("%s", (char *)ml->data);
753                 }
754                 printf("\n");
755                 lines += cvg_mod->num_lines;
756                 missed += cvg_mod->num_missed;
757                 cvg_add(cvg_all, cvg_mod);
758                 DBG("Coverage for module %s: %d lines, %d missed", pd->name,
759                                 cvg_mod->num_lines, cvg_mod->num_missed);
760         }
761         lines /= cnt;
762         missed /= cnt;
763         total_coverage = 100 - ((float)missed / (float)lines * 100);
764
765         /* Machine-readable stats on stdout. */
766         printf("coverage: scope=all coverage=%.0f%% lines=%d missed=%d\n",
767                         total_coverage, cvg_all->num_lines, cvg_all->num_missed);
768
769         /* Write text report to file. */
770         /* io.open(coverage_report, "w") */
771         if (!(py_mod = PyImport_ImportModule("io")))
772                 return FALSE;
773         if (!(py_func = PyObject_GetAttrString(py_mod, "open")))
774                 return FALSE;
775         if (!(py_args = PyTuple_New(0)))
776                 return FALSE;
777         if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report,
778                         "mode", "w")))
779                 return FALSE;
780         if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs)))
781                 return FALSE;
782         Py_DecRef(py_kwargs);
783         Py_DecRef(py_func);
784
785         /* py_cov.report(file=py_outfile) */
786         if (!(py_func = PyObject_GetAttrString(py_cov, "report")))
787                 return FALSE;
788         if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile)))
789                 return FALSE;
790         if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs)))
791                 return FALSE;
792         Py_DecRef(py_pct);
793         Py_DecRef(py_kwargs);
794         Py_DecRef(py_func);
795
796         /* py_outfile.close() */
797         if (!(py_func = PyObject_GetAttrString(py_outfile, "close")))
798                 return FALSE;
799         if (!PyObject_Call(py_func, py_args, NULL))
800                 return FALSE;
801         Py_DecRef(py_outfile);
802         Py_DecRef(py_func);
803         Py_DecRef(py_args);
804         Py_DecRef(py_mod);
805
806         return TRUE;
807 }
808
809 int main(int argc, char **argv)
810 {
811         PyObject *coverage;
812         GSList *pdlist;
813         struct pd *pd;
814         struct channel *channel;
815         struct option *option;
816         struct output *op;
817         int ret, c;
818         char *opt_infile, **kv, **opstr;
819         struct initial_pin_info *initial_pin;
820
821         op = malloc(sizeof(struct output));
822         op->pd = NULL;
823         op->pd_id = NULL;
824         op->type = -1;
825         op->class = NULL;
826         op->class_idx = -1;
827         op->outfd = 1;
828
829         pdlist = NULL;
830         opt_infile = NULL;
831         pd = NULL;
832         coverage = NULL;
833         while ((c = getopt(argc, argv, "dP:p:o:N:i:O:f:c:S")) != -1) {
834                 switch (c) {
835                 case 'd':
836                         debug = TRUE;
837                         break;
838                 case 'P':
839                         pd = g_malloc(sizeof(struct pd));
840                         pd->name = g_strdup(optarg);
841                         pd->channels = pd->options = pd->initial_pins = NULL;
842                         pdlist = g_slist_append(pdlist, pd);
843                         break;
844                 case 'p':
845                 case 'o':
846                 case 'N':
847                         if (g_slist_length(pdlist) == 0) {
848                                 /* No previous -P. */
849                                 ERR("Syntax error at '%s'", optarg);
850                                 usage(NULL);
851                         }
852                         kv = g_strsplit(optarg, "=", 0);
853                         if (!kv[0] || (!kv[1] || kv[2])) {
854                                 /* Need x=y. */
855                                 ERR("Syntax error at '%s'", optarg);
856                                 g_strfreev(kv);
857                                 usage(NULL);
858                         }
859                         if (c == 'p') {
860                                 channel = malloc(sizeof(struct channel));
861                                 channel->name = g_strdup(kv[0]);
862                                 channel->channel = strtoul(kv[1], NULL, 10);
863                                 /* Apply to last PD. */
864                                 pd->channels = g_slist_append(pd->channels, channel);
865                         } else if (c == 'o') {
866                                 option = malloc(sizeof(struct option));
867                                 option->key = g_strdup(kv[0]);
868                                 option->value = g_variant_new_string(kv[1]);
869                                 g_variant_ref_sink(option->value);
870                                 /* Apply to last PD. */
871                                 pd->options = g_slist_append(pd->options, option);
872                         } else {
873                                 initial_pin = malloc(sizeof(struct initial_pin_info));
874                                 initial_pin->name = g_strdup(kv[0]);
875                                 initial_pin->value = strtoul(kv[1], NULL, 10);
876                                 /* Apply to last PD. */
877                                 pd->initial_pins = g_slist_append(pd->initial_pins, initial_pin);
878                         }
879                         break;
880                 case 'i':
881                         opt_infile = optarg;
882                         break;
883                 case 'O':
884                         opstr = g_strsplit(optarg, ":", 0);
885                         if (!opstr[0] || !opstr[1]) {
886                                 /* Need at least abc:def. */
887                                 ERR("Syntax error at '%s'", optarg);
888                                 g_strfreev(opstr);
889                                 usage(NULL);
890                         }
891                         op->pd = g_strdup(opstr[0]);
892                         if (!strcmp(opstr[1], "annotation"))
893                                 op->type = SRD_OUTPUT_ANN;
894                         else if (!strcmp(opstr[1], "binary"))
895                                 op->type = SRD_OUTPUT_BINARY;
896                         else if (!strcmp(opstr[1], "python"))
897                                 op->type = SRD_OUTPUT_PYTHON;
898                         else if (!strcmp(opstr[1], "exception"))
899                                 /* Doesn't matter, we just need it to bomb out. */
900                                 op->type = SRD_OUTPUT_PYTHON;
901                         else {
902                                 ERR("Unknown output type '%s'", opstr[1]);
903                                 g_strfreev(opstr);
904                                 usage(NULL);
905                         }
906                         if (opstr[2])
907                                 op->class = g_strdup(opstr[2]);
908                         g_strfreev(opstr);
909                         break;
910                 case 'f':
911                         op->outfile = g_strdup(optarg);
912                         op->outfd = -1;
913                         break;
914                 case 'c':
915                         coverage_report = optarg;
916                         break;
917                 case 'S':
918                         statistics = TRUE;
919                         break;
920                 default:
921                         usage(NULL);
922                 }
923         }
924         if (argc > optind)
925                 usage(NULL);
926         if (g_slist_length(pdlist) == 0)
927                 usage(NULL);
928         if (!opt_infile)
929                 usage(NULL);
930         if (!op->pd || op->type == -1)
931                 usage(NULL);
932
933         sr_log_callback_set(sr_log, NULL);
934         if (sr_init(&ctx) != SR_OK)
935                 return 1;
936
937         srd_log_callback_set(srd_log, NULL);
938         if (srd_init(DECODERS_DIR) != SRD_OK)
939                 return 1;
940
941         if (coverage_report) {
942                 if (!(coverage = start_coverage(pdlist))) {
943                         DBG("Failed to start coverage.");
944                         if (PyErr_Occurred()) {
945                                 PyErr_PrintEx(0);
946                                 PyErr_Clear();
947                         }
948                 }
949         }
950
951         ret = 0;
952         if (!run_testcase(opt_infile, pdlist, op))
953                 ret = 1;
954
955         if (coverage) {
956                 DBG("Stopping coverage.");
957
958                 if (!(PyObject_CallMethod(coverage, "stop", NULL)))
959                         ERR("Failed to stop coverage.");
960                 else if (!(report_coverage(coverage, pdlist)))
961                         ERR("Failed to make coverage report.");
962                 else
963                         DBG("Coverage report in %s", coverage_report);
964
965                 if (PyErr_Occurred()) {
966                         PyErr_PrintEx(0);
967                         PyErr_Clear();
968                 }
969                 Py_DecRef(coverage);
970         }
971
972         srd_exit();
973         sr_exit(ctx);
974
975         return ret;
976 }