]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
6463feb2 UH |
40 | static int debug = FALSE; |
41 | static int statistics = FALSE; | |
42 | static char *coverage_report; | |
43 | static struct sr_context *ctx; | |
dd37a782 UH |
44 | |
45 | struct channel { | |
46 | char *name; | |
47 | int channel; | |
48 | }; | |
49 | ||
50 | struct option { | |
51 | char *key; | |
52 | GVariant *value; | |
53 | }; | |
54 | ||
b59c504e UH |
55 | struct initial_pin_info { |
56 | char *name; | |
57 | int value; | |
58 | }; | |
59 | ||
dd37a782 | 60 | struct pd { |
d116194c | 61 | const char *name; |
dd37a782 UH |
62 | GSList *channels; |
63 | GSList *options; | |
b59c504e | 64 | GSList *initial_pins; |
dd37a782 UH |
65 | }; |
66 | ||
67 | struct output { | |
d116194c | 68 | const char *pd; |
31ddb2fe | 69 | const char *pd_id; |
dd37a782 | 70 | int type; |
d116194c | 71 | const char *class; |
dd37a782 | 72 | int class_idx; |
d116194c | 73 | const char *outfile; |
dd37a782 UH |
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 | ||
d116194c UH |
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); | |
6463feb2 | 86 | static struct cvg *cvg_new(void); |
d116194c | 87 | static gboolean find_missed_line(struct cvg *cvg, const char *linespec); |
dd37a782 | 88 | |
d116194c | 89 | static void logmsg(const char *prefix, FILE *out, const char *format, va_list args) |
dd37a782 UH |
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 | ||
d116194c | 141 | static void usage(const char *msg) |
dd37a782 UH |
142 | { |
143 | if (msg) | |
144 | fprintf(stderr, "%s\n", msg); | |
145 | ||
987819ad GS |
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"); | |
b59c504e | 151 | printf(" -N <channelname=initial-pin-value> (optional)\n"); |
dd37a782 UH |
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"); | |
987819ad | 156 | printf(" -S (enables statistics)\n"); |
dd37a782 UH |
157 | exit(msg ? 1 : 0); |
158 | ||
159 | } | |
160 | ||
95418690 UH |
161 | /* |
162 | * This is a neutered version of libsigrokdecode's py_str_as_str(). It | |
dd37a782 | 163 | * does no error checking, but then the only strings it processes are |
95418690 UH |
164 | * generated by Python's repr(), so are known good. |
165 | */ | |
dd37a782 UH |
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 | ||
31ddb2fe GS |
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 | ||
dd37a782 UH |
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 | ||
31ddb2fe | 208 | DBG("Python output from %s", pdata->pdo->di->inst_id); |
dd37a782 UH |
209 | op = cb_data; |
210 | pydata = pdata->data; | |
211 | DBG("ptr %p", pydata); | |
212 | ||
31ddb2fe | 213 | if (strcmp(pdata->pdo->di->inst_id, op->pd_id)) |
dd37a782 UH |
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 | ||
31ddb2fe | 224 | /* Output format for testing is '<ss>-<es> <decoder-id>: <repr>\n'. */ |
dd37a782 UH |
225 | out = g_string_sized_new(128); |
226 | g_string_printf(out, "%" PRIu64 "-%" PRIu64 " %s: %s\n", | |
227 | pdata->start_sample, pdata->end_sample, | |
914b125b | 228 | pdata->pdo->di->decoder->id, s); |
dd37a782 UH |
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 | ||
31ddb2fe | 244 | DBG("Binary output from %s", pdata->pdo->di->inst_id); |
dd37a782 UH |
245 | op = cb_data; |
246 | pdb = pdata->data; | |
247 | ||
31ddb2fe | 248 | if (strcmp(pdata->pdo->di->inst_id, op->pd_id)) |
dd37a782 UH |
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, | |
914b125b | 262 | pdata->pdo->di->decoder->id); |
dd37a782 UH |
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 | { | |
31ddb2fe | 274 | struct srd_decoder_inst *di; |
dd37a782 UH |
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 | ||
31ddb2fe GS |
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 | */ | |
dd37a782 UH |
287 | op = cb_data; |
288 | pda = pdata->data; | |
31ddb2fe GS |
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)) | |
dd37a782 UH |
293 | /* This is not the PD selected for output. */ |
294 | return; | |
295 | ||
ec3de18f | 296 | if (op->class_idx != -1 && op->class_idx != pda->ann_class) |
dd37a782 UH |
297 | /* |
298 | * This output takes a specific annotation class, | |
299 | * but not the one that just came in. | |
300 | */ | |
301 | return; | |
302 | ||
31ddb2fe GS |
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 | */ | |
ec3de18f | 309 | dec_ann = g_slist_nth_data(dec->annotations, pda->ann_class); |
dd37a782 UH |
310 | line = g_string_sized_new(256); |
311 | g_string_printf(line, "%" PRIu64 "-%" PRIu64 " %s: %s:", | |
312 | pdata->start_sample, pdata->end_sample, | |
31ddb2fe | 313 | dec->id, dec_ann[0]); |
dd37a782 UH |
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 | { | |
6688fad4 | 326 | static int samplecnt = 0; |
dd37a782 UH |
327 | const struct sr_datafeed_logic *logic; |
328 | struct srd_session *sess; | |
329 | GVariant *gvar; | |
330 | uint64_t samplerate; | |
331 | int num_samples; | |
02192227 | 332 | struct sr_dev_driver *driver; |
dd37a782 UH |
333 | |
334 | sess = cb_data; | |
335 | ||
02192227 UH |
336 | driver = sr_dev_inst_driver_get(sdi); |
337 | ||
dd37a782 UH |
338 | switch (packet->type) { |
339 | case SR_DF_HEADER: | |
340 | DBG("Received SR_DF_HEADER"); | |
02192227 | 341 | if (sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, |
dd37a782 UH |
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; | |
47de7a66 UH |
361 | DBG("Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).", |
362 | logic->length, logic->unitsize); | |
dd37a782 | 363 | srd_session_send(sess, samplecnt, samplecnt + num_samples, |
cb5b23ce | 364 | logic->data, logic->length, logic->unitsize); |
6688fad4 | 365 | samplecnt += num_samples; |
dd37a782 UH |
366 | break; |
367 | case SR_DF_END: | |
368 | DBG("Received SR_DF_END"); | |
369 | break; | |
370 | } | |
371 | ||
372 | } | |
373 | ||
d116194c | 374 | static int run_testcase(const char *infile, GSList *pdlist, struct output *op) |
dd37a782 UH |
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; | |
b59c504e | 385 | GSList *pdl, *l, *l2, *devices; |
9cb2e89a | 386 | int idx, i; |
dd37a782 UH |
387 | int max_channel; |
388 | char **decoder_class; | |
389 | struct sr_session *sr_sess; | |
9cb2e89a UH |
390 | gboolean is_number; |
391 | const char *s; | |
b59c504e UH |
392 | GArray *initial_pins; |
393 | struct initial_pin_info *initial_pin; | |
dd37a782 UH |
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, | |
8a8c92bd | 398 | g_strerror(errno)); |
dd37a782 UH |
399 | return FALSE; |
400 | } | |
401 | } | |
402 | ||
25a196ad | 403 | if (sr_session_load(ctx, infile, &sr_sess) != SR_OK) |
dd37a782 UH |
404 | return FALSE; |
405 | ||
47de7a66 | 406 | sr_session_dev_list(sr_sess, &devices); |
47de7a66 | 407 | |
dd37a782 UH |
408 | if (srd_session_new(&sess) != SRD_OK) |
409 | return FALSE; | |
410 | sr_session_datafeed_callback_add(sr_sess, sr_cb, sess); | |
411 | switch (op->type) { | |
412 | case SRD_OUTPUT_ANN: | |
413 | cb = srd_cb_ann; | |
414 | break; | |
415 | case SRD_OUTPUT_BINARY: | |
416 | cb = srd_cb_bin; | |
417 | break; | |
418 | case SRD_OUTPUT_PYTHON: | |
419 | cb = srd_cb_py; | |
420 | break; | |
421 | default: | |
422 | return FALSE; | |
423 | } | |
424 | srd_pd_output_callback_add(sess, op->type, cb, op); | |
425 | ||
426 | prev_di = NULL; | |
427 | pd = NULL; | |
428 | for (pdl = pdlist; pdl; pdl = pdl->next) { | |
429 | pd = pdl->data; | |
430 | if (srd_decoder_load(pd->name) != SRD_OK) | |
431 | return FALSE; | |
432 | ||
433 | /* Instantiate decoder and pass in options. */ | |
434 | opts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
435 | (GDestroyNotify)g_variant_unref); | |
436 | for (l = pd->options; l; l = l->next) { | |
437 | option = l->data; | |
9cb2e89a UH |
438 | |
439 | is_number = TRUE; | |
440 | s = g_variant_get_string(option->value, NULL); | |
441 | for (i = 0; i < (int)strlen(s); i++) { | |
442 | if (!isdigit(s[i])) | |
443 | is_number = FALSE; | |
444 | } | |
445 | ||
446 | if (is_number) { | |
447 | /* Integer option value */ | |
448 | g_hash_table_insert(opts, option->key, | |
449 | g_variant_new_int64(strtoull(s, NULL, 10))); | |
450 | } else { | |
451 | /* String option value */ | |
452 | g_hash_table_insert(opts, option->key, option->value); | |
453 | } | |
dd37a782 UH |
454 | } |
455 | if (!(di = srd_inst_new(sess, pd->name, opts))) | |
456 | return FALSE; | |
457 | g_hash_table_destroy(opts); | |
458 | ||
31ddb2fe GS |
459 | /* |
460 | * Get (a reference to) the decoder instance's ID if we | |
461 | * are about to receive PD output from it. We need to | |
462 | * filter output that carries the decoder instance's name. | |
463 | */ | |
464 | if (strcmp(pd->name, op->pd) == 0) { | |
465 | op->pd_id = di->inst_id; | |
466 | DBG("Decoder of type \"%s\" has instance ID \"%s\".", | |
467 | op->pd, op->pd_id); | |
468 | } | |
469 | ||
dd37a782 UH |
470 | /* Map channels. */ |
471 | if (pd->channels) { | |
472 | channels = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, | |
473 | (GDestroyNotify)g_variant_unref); | |
474 | max_channel = 0; | |
475 | for (l = pd->channels; l; l = l->next) { | |
476 | channel = l->data; | |
477 | if (channel->channel > max_channel) | |
478 | max_channel = channel->channel; | |
479 | gvar = g_variant_new_int32(channel->channel); | |
480 | g_variant_ref_sink(gvar); | |
481 | g_hash_table_insert(channels, channel->name, gvar); | |
482 | } | |
47de7a66 | 483 | |
cb5b23ce | 484 | if (srd_inst_channel_set_all(di, channels) != SRD_OK) |
dd37a782 UH |
485 | return FALSE; |
486 | g_hash_table_destroy(channels); | |
487 | } | |
488 | ||
b59c504e UH |
489 | /* Set initial pins. */ |
490 | if (pd->initial_pins) { | |
491 | initial_pins = g_array_sized_new(FALSE, TRUE, sizeof(uint8_t), | |
492 | di->dec_num_channels); | |
493 | g_array_set_size(initial_pins, di->dec_num_channels); | |
494 | memset(initial_pins->data, SRD_INITIAL_PIN_SAME_AS_SAMPLE0, | |
495 | di->dec_num_channels); | |
496 | ||
497 | for (l = pd->channels, idx = 0; l; l = l->next, idx++) { | |
498 | channel = l->data; | |
499 | for (l2 = pd->initial_pins; l2; l2 = l2->next) { | |
500 | initial_pin = l2->data; | |
501 | if (!strcmp(initial_pin->name, channel->name)) | |
502 | initial_pins->data[idx] = initial_pin->value; | |
503 | } | |
504 | } | |
505 | ||
506 | if (srd_inst_initial_pins_set_all(di, initial_pins) != SRD_OK) | |
507 | return FALSE; | |
508 | g_array_free(initial_pins, TRUE); | |
509 | } | |
510 | ||
95418690 UH |
511 | /* |
512 | * If this is not the first decoder in the list, stack it | |
513 | * on top of the previous one. | |
514 | */ | |
dd37a782 UH |
515 | if (prev_di) { |
516 | if (srd_inst_stack(sess, prev_di, di) != SRD_OK) { | |
517 | ERR("Failed to stack decoder instances."); | |
518 | return FALSE; | |
519 | } | |
520 | } | |
521 | prev_di = di; | |
522 | } | |
31ddb2fe GS |
523 | /* |
524 | * Bail out if we haven't created an instance of the selected | |
525 | * decoder type of which we shall grab output data from. | |
526 | */ | |
527 | if (!op->pd_id) | |
528 | return FALSE; | |
dd37a782 | 529 | |
31ddb2fe | 530 | /* Resolve selected decoder's class index, so we can match. */ |
dd37a782 UH |
531 | dec = srd_decoder_get_by_id(pd->name); |
532 | if (op->class) { | |
533 | if (op->type == SRD_OUTPUT_ANN) | |
534 | l = dec->annotations; | |
535 | else if (op->type == SRD_OUTPUT_BINARY) | |
536 | l = dec->binary; | |
537 | else | |
538 | /* Only annotations and binary can have a class. */ | |
539 | return FALSE; | |
540 | idx = 0; | |
541 | while (l) { | |
542 | decoder_class = l->data; | |
543 | if (!strcmp(decoder_class[0], op->class)) { | |
544 | op->class_idx = idx; | |
545 | break; | |
59060ffc GS |
546 | } |
547 | idx++; | |
dd37a782 UH |
548 | l = l->next; |
549 | } | |
550 | if (op->class_idx == -1) { | |
551 | ERR("Output class '%s' not found in decoder %s.", | |
552 | op->class, pd->name); | |
553 | return FALSE; | |
59060ffc GS |
554 | } |
555 | DBG("Class %s index is %d", op->class, op->class_idx); | |
dd37a782 UH |
556 | } |
557 | ||
558 | sr_session_start(sr_sess); | |
559 | sr_session_run(sr_sess); | |
560 | sr_session_stop(sr_sess); | |
561 | ||
562 | srd_session_destroy(sess); | |
563 | ||
564 | if (op->outfile) | |
565 | close(op->outfd); | |
566 | ||
567 | return TRUE; | |
568 | } | |
569 | ||
570 | static PyObject *start_coverage(GSList *pdlist) | |
571 | { | |
572 | PyObject *py_mod, *py_pdlist, *py_pd, *py_func, *py_args, *py_kwargs, *py_cov; | |
573 | GSList *l; | |
574 | struct pd *pd; | |
575 | ||
576 | DBG("Starting coverage."); | |
577 | ||
578 | if (!(py_mod = PyImport_ImportModule("coverage"))) | |
579 | return NULL; | |
580 | ||
581 | if (!(py_pdlist = PyList_New(0))) | |
582 | return NULL; | |
583 | for (l = pdlist; l; l = l->next) { | |
584 | pd = l->data; | |
585 | py_pd = PyUnicode_FromFormat("*/%s/*.py", pd->name); | |
586 | if (PyList_Append(py_pdlist, py_pd) < 0) | |
587 | return NULL; | |
588 | Py_DecRef(py_pd); | |
589 | } | |
590 | if (!(py_func = PyObject_GetAttrString(py_mod, "coverage"))) | |
591 | return NULL; | |
592 | if (!(py_args = PyTuple_New(0))) | |
593 | return NULL; | |
594 | if (!(py_kwargs = Py_BuildValue("{sO}", "include", py_pdlist))) | |
595 | return NULL; | |
596 | if (!(py_cov = PyObject_Call(py_func, py_args, py_kwargs))) | |
597 | return NULL; | |
598 | if (!(PyObject_CallMethod(py_cov, "start", NULL))) | |
599 | return NULL; | |
600 | Py_DecRef(py_pdlist); | |
601 | Py_DecRef(py_args); | |
602 | Py_DecRef(py_kwargs); | |
603 | Py_DecRef(py_func); | |
604 | ||
605 | return py_cov; | |
606 | } | |
607 | ||
d116194c | 608 | static struct cvg *get_mod_cov(PyObject *py_cov, const char *module_name) |
dd37a782 UH |
609 | { |
610 | PyObject *py_mod, *py_pathlist, *py_path, *py_func, *py_pd; | |
611 | PyObject *py_result, *py_missed, *py_item; | |
612 | DIR *d; | |
613 | struct dirent *de; | |
614 | struct cvg *cvg_mod; | |
615 | int num_lines, num_missed, linenum, i, j; | |
616 | char *path, *linespec; | |
617 | ||
618 | if (!(py_mod = PyImport_ImportModule(module_name))) | |
619 | return NULL; | |
620 | ||
621 | cvg_mod = NULL; | |
622 | py_pathlist = PyObject_GetAttrString(py_mod, "__path__"); | |
623 | for (i = 0; i < PyList_Size(py_pathlist); i++) { | |
624 | py_path = PyList_GetItem(py_pathlist, i); | |
95418690 | 625 | PyUnicode_FSConverter(PyList_GetItem(py_pathlist, i), &py_path); |
dd37a782 UH |
626 | path = PyBytes_AS_STRING(py_path); |
627 | if (!(d = opendir(path))) { | |
628 | ERR("Invalid module path '%s'", path); | |
629 | return NULL; | |
630 | } | |
631 | while ((de = readdir(d))) { | |
632 | if (strncmp(de->d_name + strlen(de->d_name) - 3, ".py", 3)) | |
633 | continue; | |
634 | ||
635 | if (!(py_func = PyObject_GetAttrString(py_cov, "analysis2"))) | |
636 | return NULL; | |
637 | if (!(py_pd = PyUnicode_FromFormat("%s/%s", path, de->d_name))) | |
638 | return NULL; | |
639 | if (!(py_result = PyObject_CallFunction(py_func, "O", py_pd))) | |
640 | return NULL; | |
641 | Py_DecRef(py_pd); | |
642 | Py_DecRef(py_func); | |
643 | ||
644 | if (!cvg_mod) | |
645 | cvg_mod = cvg_new(); | |
646 | if (PyTuple_Size(py_result) != 5) { | |
647 | ERR("Invalid result from coverage of '%s/%s'", path, de->d_name); | |
648 | return NULL; | |
649 | } | |
650 | num_lines = PyList_Size(PyTuple_GetItem(py_result, 1)); | |
651 | py_missed = PyTuple_GetItem(py_result, 3); | |
652 | num_missed = PyList_Size(py_missed); | |
653 | cvg_mod->num_lines += num_lines; | |
654 | cvg_mod->num_missed += num_missed; | |
655 | for (j = 0; j < num_missed; j++) { | |
656 | py_item = PyList_GetItem(py_missed, j); | |
657 | linenum = PyLong_AsLong(py_item); | |
658 | linespec = g_strdup_printf("%s/%s:%d", module_name, | |
659 | de->d_name, linenum); | |
660 | cvg_mod->missed_lines = g_slist_append(cvg_mod->missed_lines, linespec); | |
661 | } | |
662 | DBG("Coverage for %s/%s: %d lines, %d missed.", | |
663 | module_name, de->d_name, num_lines, num_missed); | |
664 | Py_DecRef(py_result); | |
665 | } | |
666 | } | |
667 | if (cvg_mod->num_lines) | |
668 | cvg_mod->coverage = 100 - ((float)cvg_mod->num_missed / (float)cvg_mod->num_lines * 100); | |
669 | ||
670 | Py_DecRef(py_mod); | |
671 | Py_DecRef(py_path); | |
672 | ||
673 | return cvg_mod; | |
674 | } | |
675 | ||
6463feb2 | 676 | static struct cvg *cvg_new(void) |
dd37a782 UH |
677 | { |
678 | struct cvg *cvg; | |
679 | ||
680 | cvg = calloc(1, sizeof(struct cvg)); | |
681 | ||
682 | return cvg; | |
683 | } | |
684 | ||
d116194c | 685 | static gboolean find_missed_line(struct cvg *cvg, const char *linespec) |
dd37a782 UH |
686 | { |
687 | GSList *l; | |
688 | ||
689 | for (l = cvg->missed_lines; l; l = l->next) | |
690 | if (!strcmp(l->data, linespec)) | |
691 | return TRUE; | |
692 | ||
693 | return FALSE; | |
694 | } | |
695 | ||
d116194c | 696 | static void cvg_add(struct cvg *dst, const struct cvg *src) |
dd37a782 UH |
697 | { |
698 | GSList *l; | |
699 | char *linespec; | |
700 | ||
701 | dst->num_lines += src->num_lines; | |
702 | dst->num_missed += src->num_missed; | |
703 | for (l = src->missed_lines; l; l = l->next) { | |
704 | linespec = l->data; | |
705 | if (!find_missed_line(dst, linespec)) | |
706 | dst->missed_lines = g_slist_append(dst->missed_lines, linespec); | |
707 | } | |
708 | ||
709 | } | |
710 | ||
711 | static int report_coverage(PyObject *py_cov, GSList *pdlist) | |
712 | { | |
713 | PyObject *py_func, *py_mod, *py_args, *py_kwargs, *py_outfile, *py_pct; | |
714 | GSList *l, *ml; | |
715 | struct pd *pd; | |
716 | struct cvg *cvg_mod, *cvg_all; | |
717 | float total_coverage; | |
718 | int lines, missed, cnt; | |
719 | ||
720 | DBG("Making coverage report."); | |
721 | ||
722 | /* Get coverage for each module in the stack. */ | |
723 | lines = missed = 0; | |
724 | cvg_all = cvg_new(); | |
725 | for (cnt = 0, l = pdlist; l; l = l->next, cnt++) { | |
726 | pd = l->data; | |
727 | if (!(cvg_mod = get_mod_cov(py_cov, pd->name))) | |
728 | return FALSE; | |
729 | printf("coverage: scope=%s coverage=%.0f%% lines=%d missed=%d " | |
730 | "missed_lines=", pd->name, cvg_mod->coverage, | |
731 | cvg_mod->num_lines, cvg_mod->num_missed); | |
732 | for (ml = cvg_mod->missed_lines; ml; ml = ml->next) { | |
733 | if (ml != cvg_mod->missed_lines) | |
734 | printf(","); | |
735 | printf("%s", (char *)ml->data); | |
736 | } | |
737 | printf("\n"); | |
738 | lines += cvg_mod->num_lines; | |
739 | missed += cvg_mod->num_missed; | |
740 | cvg_add(cvg_all, cvg_mod); | |
741 | DBG("Coverage for module %s: %d lines, %d missed", pd->name, | |
742 | cvg_mod->num_lines, cvg_mod->num_missed); | |
743 | } | |
744 | lines /= cnt; | |
745 | missed /= cnt; | |
746 | total_coverage = 100 - ((float)missed / (float)lines * 100); | |
747 | ||
748 | /* Machine-readable stats on stdout. */ | |
749 | printf("coverage: scope=all coverage=%.0f%% lines=%d missed=%d\n", | |
750 | total_coverage, cvg_all->num_lines, cvg_all->num_missed); | |
751 | ||
752 | /* Write text report to file. */ | |
753 | /* io.open(coverage_report, "w") */ | |
754 | if (!(py_mod = PyImport_ImportModule("io"))) | |
755 | return FALSE; | |
756 | if (!(py_func = PyObject_GetAttrString(py_mod, "open"))) | |
757 | return FALSE; | |
758 | if (!(py_args = PyTuple_New(0))) | |
759 | return FALSE; | |
760 | if (!(py_kwargs = Py_BuildValue("{ssss}", "file", coverage_report, | |
761 | "mode", "w"))) | |
762 | return FALSE; | |
763 | if (!(py_outfile = PyObject_Call(py_func, py_args, py_kwargs))) | |
764 | return FALSE; | |
765 | Py_DecRef(py_kwargs); | |
766 | Py_DecRef(py_func); | |
767 | ||
768 | /* py_cov.report(file=py_outfile) */ | |
769 | if (!(py_func = PyObject_GetAttrString(py_cov, "report"))) | |
770 | return FALSE; | |
771 | if (!(py_kwargs = Py_BuildValue("{sO}", "file", py_outfile))) | |
772 | return FALSE; | |
773 | if (!(py_pct = PyObject_Call(py_func, py_args, py_kwargs))) | |
774 | return FALSE; | |
775 | Py_DecRef(py_pct); | |
776 | Py_DecRef(py_kwargs); | |
777 | Py_DecRef(py_func); | |
778 | ||
779 | /* py_outfile.close() */ | |
780 | if (!(py_func = PyObject_GetAttrString(py_outfile, "close"))) | |
781 | return FALSE; | |
782 | if (!PyObject_Call(py_func, py_args, NULL)) | |
783 | return FALSE; | |
784 | Py_DecRef(py_outfile); | |
785 | Py_DecRef(py_func); | |
786 | Py_DecRef(py_args); | |
787 | Py_DecRef(py_mod); | |
788 | ||
789 | return TRUE; | |
790 | } | |
791 | ||
792 | int main(int argc, char **argv) | |
793 | { | |
dd37a782 UH |
794 | PyObject *coverage; |
795 | GSList *pdlist; | |
796 | struct pd *pd; | |
797 | struct channel *channel; | |
798 | struct option *option; | |
799 | struct output *op; | |
800 | int ret, c; | |
801 | char *opt_infile, **kv, **opstr; | |
b59c504e | 802 | struct initial_pin_info *initial_pin; |
dd37a782 UH |
803 | |
804 | op = malloc(sizeof(struct output)); | |
805 | op->pd = NULL; | |
31ddb2fe | 806 | op->pd_id = NULL; |
dd37a782 UH |
807 | op->type = -1; |
808 | op->class = NULL; | |
809 | op->class_idx = -1; | |
810 | op->outfd = 1; | |
811 | ||
812 | pdlist = NULL; | |
813 | opt_infile = NULL; | |
814 | pd = NULL; | |
815 | coverage = NULL; | |
b59c504e | 816 | while ((c = getopt(argc, argv, "dP:p:o:N:i:O:f:c:S")) != -1) { |
dd37a782 UH |
817 | switch (c) { |
818 | case 'd': | |
819 | debug = TRUE; | |
820 | break; | |
821 | case 'P': | |
822 | pd = g_malloc(sizeof(struct pd)); | |
823 | pd->name = g_strdup(optarg); | |
b59c504e | 824 | pd->channels = pd->options = pd->initial_pins = NULL; |
dd37a782 UH |
825 | pdlist = g_slist_append(pdlist, pd); |
826 | break; | |
827 | case 'p': | |
828 | case 'o': | |
b59c504e | 829 | case 'N': |
dd37a782 UH |
830 | if (g_slist_length(pdlist) == 0) { |
831 | /* No previous -P. */ | |
832 | ERR("Syntax error at '%s'", optarg); | |
833 | usage(NULL); | |
834 | } | |
835 | kv = g_strsplit(optarg, "=", 0); | |
836 | if (!kv[0] || (!kv[1] || kv[2])) { | |
837 | /* Need x=y. */ | |
838 | ERR("Syntax error at '%s'", optarg); | |
839 | g_strfreev(kv); | |
840 | usage(NULL); | |
841 | } | |
842 | if (c == 'p') { | |
843 | channel = malloc(sizeof(struct channel)); | |
844 | channel->name = g_strdup(kv[0]); | |
ecec131b | 845 | channel->channel = strtoul(kv[1], NULL, 10); |
dd37a782 UH |
846 | /* Apply to last PD. */ |
847 | pd->channels = g_slist_append(pd->channels, channel); | |
b59c504e | 848 | } else if (c == 'o') { |
dd37a782 UH |
849 | option = malloc(sizeof(struct option)); |
850 | option->key = g_strdup(kv[0]); | |
851 | option->value = g_variant_new_string(kv[1]); | |
ecec131b | 852 | g_variant_ref_sink(option->value); |
dd37a782 UH |
853 | /* Apply to last PD. */ |
854 | pd->options = g_slist_append(pd->options, option); | |
b59c504e UH |
855 | } else { |
856 | initial_pin = malloc(sizeof(struct initial_pin_info)); | |
857 | initial_pin->name = g_strdup(kv[0]); | |
858 | initial_pin->value = strtoul(kv[1], NULL, 10); | |
859 | /* Apply to last PD. */ | |
860 | pd->initial_pins = g_slist_append(pd->initial_pins, initial_pin); | |
dd37a782 UH |
861 | } |
862 | break; | |
863 | case 'i': | |
864 | opt_infile = optarg; | |
865 | break; | |
866 | case 'O': | |
867 | opstr = g_strsplit(optarg, ":", 0); | |
868 | if (!opstr[0] || !opstr[1]) { | |
869 | /* Need at least abc:def. */ | |
870 | ERR("Syntax error at '%s'", optarg); | |
871 | g_strfreev(opstr); | |
872 | usage(NULL); | |
873 | } | |
874 | op->pd = g_strdup(opstr[0]); | |
875 | if (!strcmp(opstr[1], "annotation")) | |
876 | op->type = SRD_OUTPUT_ANN; | |
877 | else if (!strcmp(opstr[1], "binary")) | |
878 | op->type = SRD_OUTPUT_BINARY; | |
879 | else if (!strcmp(opstr[1], "python")) | |
880 | op->type = SRD_OUTPUT_PYTHON; | |
881 | else if (!strcmp(opstr[1], "exception")) | |
882 | /* Doesn't matter, we just need it to bomb out. */ | |
883 | op->type = SRD_OUTPUT_PYTHON; | |
884 | else { | |
885 | ERR("Unknown output type '%s'", opstr[1]); | |
886 | g_strfreev(opstr); | |
887 | usage(NULL); | |
888 | } | |
889 | if (opstr[2]) | |
890 | op->class = g_strdup(opstr[2]); | |
891 | g_strfreev(opstr); | |
892 | break; | |
893 | case 'f': | |
894 | op->outfile = g_strdup(optarg); | |
895 | op->outfd = -1; | |
896 | break; | |
897 | case 'c': | |
898 | coverage_report = optarg; | |
899 | break; | |
900 | case 'S': | |
901 | statistics = TRUE; | |
902 | break; | |
903 | default: | |
904 | usage(NULL); | |
905 | } | |
906 | } | |
907 | if (argc > optind) | |
908 | usage(NULL); | |
909 | if (g_slist_length(pdlist) == 0) | |
910 | usage(NULL); | |
911 | if (!opt_infile) | |
912 | usage(NULL); | |
913 | if (!op->pd || op->type == -1) | |
914 | usage(NULL); | |
915 | ||
916 | sr_log_callback_set(sr_log, NULL); | |
917 | if (sr_init(&ctx) != SR_OK) | |
918 | return 1; | |
919 | ||
920 | srd_log_callback_set(srd_log, NULL); | |
921 | if (srd_init(DECODERS_DIR) != SRD_OK) | |
922 | return 1; | |
923 | ||
924 | if (coverage_report) { | |
925 | if (!(coverage = start_coverage(pdlist))) { | |
926 | DBG("Failed to start coverage."); | |
927 | if (PyErr_Occurred()) { | |
928 | PyErr_PrintEx(0); | |
929 | PyErr_Clear(); | |
930 | } | |
931 | } | |
932 | } | |
933 | ||
934 | ret = 0; | |
935 | if (!run_testcase(opt_infile, pdlist, op)) | |
936 | ret = 1; | |
937 | ||
938 | if (coverage) { | |
939 | DBG("Stopping coverage."); | |
940 | ||
941 | if (!(PyObject_CallMethod(coverage, "stop", NULL))) | |
942 | ERR("Failed to stop coverage."); | |
943 | else if (!(report_coverage(coverage, pdlist))) | |
944 | ERR("Failed to make coverage report."); | |
945 | else | |
946 | DBG("Coverage report in %s", coverage_report); | |
947 | ||
948 | if (PyErr_Occurred()) { | |
949 | PyErr_PrintEx(0); | |
950 | PyErr_Clear(); | |
951 | } | |
952 | Py_DecRef(coverage); | |
953 | } | |
954 | ||
955 | srd_exit(); | |
956 | sr_exit(ctx); | |
957 | ||
958 | return ret; | |
959 | } |