run_tests_valgrind.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* Note: we already test safe_mode in run_tests.c */
  17. static const char *can_run_vg(struct manifest *m)
  18. {
  19. char *output = run_command(m, "valgrind -q true");
  20. if (output)
  21. return talloc_asprintf(m, "No valgrind support: %s", output);
  22. return NULL;
  23. }
  24. struct run_tests_result {
  25. struct list_node list;
  26. struct ccan_file *file;
  27. const char *output;
  28. };
  29. static void *do_run_tests_vg(struct manifest *m)
  30. {
  31. struct list_head *list = talloc(m, struct list_head);
  32. struct run_tests_result *res;
  33. struct ccan_file *i;
  34. char *cmdout;
  35. list_head_init(list);
  36. list_for_each(&m->run_tests, i, list) {
  37. run_tests.total_score++;
  38. /* FIXME: timeout here */
  39. cmdout = run_command(m, "valgrind -q %s", i->compiled);
  40. if (cmdout) {
  41. res = talloc(list, struct run_tests_result);
  42. res->file = i;
  43. res->output = talloc_steal(res, cmdout);
  44. list_add_tail(list, &res->list);
  45. }
  46. }
  47. list_for_each(&m->api_tests, i, list) {
  48. run_tests.total_score++;
  49. /* FIXME: timeout here */
  50. cmdout = run_command(m, "valgrind -q %s", i->compiled);
  51. if (cmdout) {
  52. res = talloc(list, struct run_tests_result);
  53. res->file = i;
  54. res->output = talloc_steal(res, cmdout);
  55. list_add_tail(list, &res->list);
  56. }
  57. }
  58. if (list_empty(list)) {
  59. talloc_free(list);
  60. list = NULL;
  61. }
  62. return list;
  63. }
  64. static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
  65. {
  66. struct list_head *list = check_result;
  67. struct run_tests_result *i;
  68. unsigned int score = run_tests.total_score;
  69. list_for_each(list, i, list)
  70. score--;
  71. return score;
  72. }
  73. static const char *describe_run_tests_vg(struct manifest *m,
  74. void *check_result)
  75. {
  76. struct list_head *list = check_result;
  77. char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
  78. struct run_tests_result *i;
  79. list_for_each(list, i, list)
  80. descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
  81. i->file->name, i->output);
  82. return descrip;
  83. }
  84. static void run_under_debugger_vg(struct manifest *m, void *check_result)
  85. {
  86. struct list_head *list = check_result;
  87. struct run_tests_result *first;
  88. if (!ask("Should I run the first failing test under the debugger?"))
  89. return;
  90. first = list_top(list, struct run_tests_result, list);
  91. run_command(m, "valgrind --db-attach=yes %s", first->file->compiled);
  92. }
  93. struct ccanlint run_tests_vg = {
  94. .name = "run and api tests under valgrind",
  95. .score = score_run_tests_vg,
  96. .check = do_run_tests_vg,
  97. .describe = describe_run_tests_vg,
  98. .can_run = can_run_vg,
  99. .handle = run_under_debugger_vg
  100. };
  101. REGISTER_TEST(run_tests_vg, &run_tests, NULL);