Fix valgrind running with debugger.
[ccan] / tools / ccanlint / tests / run_tests_valgrind.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 /* Note: we already test safe_mode in run_tests.c */
18 static const char *can_run_vg(struct manifest *m)
19 {
20         char *output = run_command(m, "valgrind -q true");
21
22         if (output)
23                 return talloc_asprintf(m, "No valgrind support: %s", output);
24         return NULL;
25 }
26
27 struct run_tests_result {
28         struct list_node list;
29         struct ccan_file *file;
30         const char *output;
31 };
32
33 static void *do_run_tests_vg(struct manifest *m)
34 {
35         struct list_head *list = talloc(m, struct list_head);
36         struct run_tests_result *res;
37         struct ccan_file *i;
38         char *cmdout;
39
40         list_head_init(list);
41
42         list_for_each(&m->run_tests, i, list) {
43                 run_tests.total_score++;
44                 /* FIXME: timeout here */
45                 cmdout = run_command(m, "valgrind -q %s", i->compiled);
46                 if (cmdout) {
47                         res = talloc(list, struct run_tests_result);
48                         res->file = i;
49                         res->output = talloc_steal(res, cmdout);
50                         list_add_tail(list, &res->list);
51                 }
52         }
53
54         list_for_each(&m->api_tests, i, list) {
55                 run_tests.total_score++;
56                 /* FIXME: timeout here */
57                 cmdout = run_command(m, "valgrind -q %s", i->compiled);
58                 if (cmdout) {
59                         res = talloc(list, struct run_tests_result);
60                         res->file = i;
61                         res->output = talloc_steal(res, cmdout);
62                         list_add_tail(list, &res->list);
63                 }
64         }
65
66         if (list_empty(list)) {
67                 talloc_free(list);
68                 list = NULL;
69         }
70
71         return list;
72 }
73
74 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
75 {
76         struct list_head *list = check_result;
77         struct run_tests_result *i;
78         unsigned int score = run_tests.total_score;
79
80         list_for_each(list, i, list)
81                 score--;
82         return score;
83 }
84
85 static const char *describe_run_tests_vg(struct manifest *m,
86                                          void *check_result)
87 {
88         struct list_head *list = check_result;
89         char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
90         struct run_tests_result *i;
91
92         list_for_each(list, i, list)
93                 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
94                                                  i->file->name, i->output);
95         return descrip;
96 }
97
98 static void run_under_debugger_vg(struct manifest *m, void *check_result)
99 {
100         struct list_head *list = check_result;
101         struct run_tests_result *first;
102         char *command;
103
104         if (!ask("Should I run the first failing test under the debugger?"))
105                 return;
106
107         first = list_top(list, struct run_tests_result, list);
108         command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
109                                   first->file->compiled);
110         system(command);
111 }
112
113 struct ccanlint run_tests_vg = {
114         .name = "run and api tests under valgrind",
115         .score = score_run_tests_vg,
116         .check = do_run_tests_vg,
117         .describe = describe_run_tests_vg,
118         .can_run = can_run_vg,
119         .handle = run_under_debugger_vg
120 };
121
122 REGISTER_TEST(run_tests_vg, &run_tests, NULL);