X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Ftests_pass_valgrind.c;h=64cb29e21c438ef8e1c59fd744188413b395a608;hp=4365f2a60227bc1a259ad4671d23efa72f87f0e9;hb=ad6a6711a1b703793e49206e10d6dcf1722eb424;hpb=30d5682cc59801c553d71c2d9ababbbd48578576 diff --git a/tools/ccanlint/tests/tests_pass_valgrind.c b/tools/ccanlint/tests/tests_pass_valgrind.c index 4365f2a6..64cb29e2 100644 --- a/tools/ccanlint/tests/tests_pass_valgrind.c +++ b/tools/ccanlint/tests/tests_pass_valgrind.c @@ -5,6 +5,7 @@ #include #include #include +#include "tests_pass.h" #include #include #include @@ -17,17 +18,14 @@ #include #include -struct ccanlint run_tests_vg; +REGISTER_TEST(tests_pass_valgrind); +REGISTER_TEST(tests_pass_valgrind_noleaks); /* Note: we already test safe_mode in run_tests.c */ static const char *can_run_vg(struct manifest *m) { - unsigned int timeleft = default_timeout_ms; - char *output; - - if (!run_command(m, &timeleft, &output, - "valgrind -q --error-exitcode=0 true")) - return talloc_asprintf(m, "No valgrind support: %s", output); + if (!do_valgrind) + return talloc_asprintf(m, "No valgrind support"); return NULL; } @@ -49,14 +47,30 @@ static bool blank_line(const char *line) return line[strspn(line, "=0123456789 ")] == '\0'; } -static char *get_leaks(const char *output, char **errs) +/* Removes matching lines from lines array, returns them. FIXME: Hacky. */ +static char **extract_matching(const char *prefix, char *lines[]) { - char *leaks = talloc_strdup(output, ""); - unsigned int i, num; - char **lines = strsplit(output, output, "\n", &num); + unsigned int i, num_ret = 0; + char **ret = talloc_array(lines, char *, talloc_array_length(lines)); - *errs = talloc_strdup(output, ""); - for (i = 0; i < num; i++) { + for (i = 0; i < talloc_array_length(lines) - 1; i++) { + if (strstarts(lines[i], prefix)) { + ret[num_ret++] = talloc_steal(ret, lines[i]); + lines[i] = (char *)""; + } + } + ret[num_ret++] = NULL; + + /* Make sure length is correct! */ + return talloc_realloc(NULL, ret, char *, num_ret); +} + +static char *get_leaks(char *lines[], char **errs) +{ + char *leaks = talloc_strdup(lines, ""); + unsigned int i; + + for (i = 0; i < talloc_array_length(lines) - 1; i++) { if (strstr(lines[i], " lost ")) { /* A leak... */ if (strstr(lines[i], " definitely lost ")) { @@ -81,6 +95,31 @@ static char *get_leaks(const char *output, char **errs) } } } + return leaks; +} + +/* Returns leaks, and sets errs[] */ +static char *analyze_output(const char *output, char **errs) +{ + char *leaks = talloc_strdup(output, ""); + unsigned int i; + char **lines = strsplit(output, output, "\n"); + + *errs = talloc_strdup(output, ""); + for (i = 0; i < talloc_array_length(lines) - 1; i++) { + unsigned int preflen = strspn(lines[i], "=0123456789"); + char *prefix, **sublines; + + /* Ignore erased lines, or weird stuff. */ + if (preflen == 0) + continue; + + prefix = talloc_strndup(output, lines[i], preflen); + sublines = extract_matching(prefix, lines); + + leaks = talloc_append_string(leaks, get_leaks(sublines, errs)); + } + if (!leaks[0]) { talloc_free(leaks); leaks = NULL; @@ -92,48 +131,61 @@ static char *get_leaks(const char *output, char **errs) return leaks; } +static const char *concat(struct score *score, char *bits[]) +{ + unsigned int i; + char *ret = talloc_strdup(score, ""); + + for (i = 0; bits[i]; i++) { + if (i) + ret = talloc_append_string(ret, " "); + ret = talloc_append_string(ret, bits[i]); + } + return ret; +} + /* FIXME: Run examples, too! */ static void do_run_tests_vg(struct manifest *m, - bool keep, + bool keep, unsigned int *timeleft, struct score *score) { struct ccan_file *i; struct list_head *list; - char *cmdout; /* This is slow, so we run once but grab leak info. */ score->total = 0; + score->pass = true; foreach_ptr(list, &m->run_tests, &m->api_tests) { list_for_each(list, i, list) { - char *output, *err; + char *err, *output; + const char *options; + score->total++; - /* FIXME: Valgrind's output sucks. XML is unreadable by - * humans, and you can't have both. */ - run_command(score, timeleft, &cmdout, - "valgrind -q --error-exitcode=101" - " --leak-check=full" - " --log-fd=3 %s %s" - " 3> valgrind.log", - run_tests_vg.options ? - run_tests_vg.options : "", - i->compiled); - output = grab_file(i, "valgrind.log", NULL); + options = concat(score, + per_file_options(&tests_pass_valgrind, + i)); + if (streq(options, "FAIL")) + continue; + + if (keep) + talloc_set_destructor(i->valgrind_log, NULL); + + output = grab_file(i, i->valgrind_log, NULL); + /* No valgrind errors? */ if (!output || output[0] == '\0') { err = NULL; i->leak_info = NULL; } else { - i->leak_info = get_leaks(output, &err); + i->leak_info = analyze_output(output, &err); } - if (err) - score_file_error(score, i, 0, err); - else + if (err) { + score_file_error(score, i, 0, "%s", err); + score->pass = false; + } else score->score++; } } - - if (score->score == score->total) - score->pass = true; } static void do_leakcheck_vg(struct manifest *m, @@ -143,20 +195,34 @@ static void do_leakcheck_vg(struct manifest *m, { struct ccan_file *i; struct list_head *list; + char **options; bool leaks = false; foreach_ptr(list, &m->run_tests, &m->api_tests) { list_for_each(list, i, list) { + options = per_file_options(&tests_pass_valgrind_noleaks, + i); + if (options[0]) { + if (streq(options[0], "FAIL")) { + leaks = true; + continue; + } + errx(1, "Unknown leakcheck options '%s'", + options[0]); + } + if (i->leak_info) { - score_file_error(score, i, 0, i->leak_info); + score_file_error(score, i, 0, "%s", + i->leak_info); leaks = true; } } } + /* FIXME: We don't fail for this, since many tests leak. */ + score->pass = true; if (!leaks) { score->score = 1; - score->pass = true; } } @@ -168,13 +234,18 @@ static void run_under_debugger_vg(struct manifest *m, struct score *score) struct file_error *first; char *command; + /* Don't ask anything if they suppressed tests. */ + if (score->pass) + return; + if (!ask("Should I run the first failing test under the debugger?")) return; first = list_top(&score->per_file_errors, struct file_error, list); - command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s", - run_tests_vg.options ? - run_tests_vg.options : "", + command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s", + concat(score, + per_file_options(&tests_pass_valgrind, + first->file)), first->file->compiled); if (system(command)) doesnt_matter(); @@ -190,13 +261,11 @@ struct ccanlint tests_pass_valgrind = { .needs = "tests_pass" }; -REGISTER_TEST(tests_pass_valgrind); - struct ccanlint tests_pass_valgrind_noleaks = { .key = "tests_pass_valgrind_noleaks", .name = "Module's run and api tests have no memory leaks", .check = do_leakcheck_vg, + .takes_options = true, .needs = "tests_pass_valgrind" }; -REGISTER_TEST(tests_pass_valgrind_noleaks);