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