X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Ftests_pass_valgrind.c;h=773a85e66a5a17aca87595bb0ca8f67441158a2e;hb=0621cac3bf1b5cf4c90de56e0e43b0acde0c94f1;hp=9622b300a20af1afe1023b33a980d42ccf3951fe;hpb=99faa47cc632f10b26e577c860657528e1bf2712;p=ccan diff --git a/tools/ccanlint/tests/tests_pass_valgrind.c b/tools/ccanlint/tests/tests_pass_valgrind.c index 9622b300..773a85e6 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,20 +18,28 @@ #include #include -REGISTER_TEST(tests_pass_valgrind); - /* 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; } +static void do_leakcheck_vg(struct manifest *m, + unsigned int *timeleft, + struct score *score); + +static 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); + + /* Example output: ==2749== Conditional jump or move depends on uninitialised value(s) ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263) @@ -133,76 +142,83 @@ static char *analyze_output(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, 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, *log; - bool pass; + char *err, *output; + const char *options; + score->total++; + options = concat(score, + per_file_options(&tests_pass_valgrind, + i)); + if (streq(options, "FAIL")) { + i->leak_info = NULL; + continue; + } - /* FIXME: Valgrind's output sucks. XML is unreadable by - * humans *and* doesn't support children reporting. */ - log = talloc_asprintf(score, - "%s.valgrind-log", i->compiled); - if (!keep) - talloc_set_destructor(log, - unlink_file_destructor); - - pass = run_command(score, timeleft, &cmdout, - "valgrind -q --error-exitcode=101" - " --leak-check=full" - " --log-fd=3 %s %s" - " 3> %s", - tests_pass_valgrind.options ? - tests_pass_valgrind.options : "", - i->compiled, log); - output = grab_file(i, log, NULL); - /* No valgrind errors? Expect it to pass... */ + output = grab_file(i, i->valgrind_log, NULL); + /* No valgrind errors? */ if (!output || output[0] == '\0') { - if (!pass) { - err = talloc_asprintf(score, - "Test failed:\n" - "%s", - cmdout); - } else - err = NULL; + err = NULL; i->leak_info = NULL; } else { i->leak_info = analyze_output(output, &err); } - if (err) + if (err) { score_file_error(score, i, 0, "%s", err); - else + score->pass = false; + } else score->score++; } } - - if (score->score == score->total) - score->pass = true; } static void do_leakcheck_vg(struct manifest *m, - bool keep, unsigned int *timeleft, struct score *score) { 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, "%s", i->leak_info); @@ -226,14 +242,19 @@ 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 --leak-check=full --db-attach=yes%s %s", - tests_pass_valgrind.options ? - tests_pass_valgrind.options : "", - first->file->compiled); + concat(score, + per_file_options(&tests_pass_valgrind, + first->file)), + first->file->compiled[COMPILE_NORMAL]); if (system(command)) doesnt_matter(); } @@ -247,12 +268,4 @@ struct ccanlint tests_pass_valgrind = { .takes_options = true, .needs = "tests_pass" }; - -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, - .needs = "tests_pass_valgrind" -}; - -REGISTER_TEST(tests_pass_valgrind_noleaks); +REGISTER_TEST(tests_pass_valgrind);