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