run_tests_valgrind.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <ccan/foreach/foreach.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <limits.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <err.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. /* Note: we already test safe_mode in run_tests.c */
  18. static const char *can_run_vg(struct manifest *m)
  19. {
  20. unsigned int timeleft = default_timeout_ms;
  21. char *output = run_command(m, &timeleft,
  22. "valgrind -q --error-exitcode=0 true");
  23. if (output)
  24. return talloc_asprintf(m, "No valgrind support: %s", output);
  25. return NULL;
  26. }
  27. /* FIXME: Run examples, too! */
  28. static void do_run_tests_vg(struct manifest *m,
  29. bool keep,
  30. unsigned int *timeleft,
  31. struct score *score)
  32. {
  33. struct ccan_file *i;
  34. struct list_head *list;
  35. char *cmdout;
  36. score->total = 0;
  37. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  38. list_for_each(list, i, list) {
  39. score->total++;
  40. cmdout = run_command(m, timeleft,
  41. "valgrind -q --error-exitcode=100 %s",
  42. i->compiled);
  43. if (cmdout) {
  44. score->error = "Running under valgrind";
  45. score_file_error(score, i, 0, cmdout);
  46. } else
  47. score->score++;
  48. }
  49. }
  50. if (score->score == score->total)
  51. score->pass = true;
  52. }
  53. /* Gcc's warn_unused_result is fascist bullshit. */
  54. #define doesnt_matter()
  55. static void run_under_debugger_vg(struct manifest *m, struct score *score)
  56. {
  57. struct file_error *first;
  58. char *command;
  59. if (!ask("Should I run the first failing test under the debugger?"))
  60. return;
  61. first = list_top(&score->per_file_errors, struct file_error, list);
  62. command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
  63. first->file->compiled);
  64. if (system(command))
  65. doesnt_matter();
  66. }
  67. struct ccanlint run_tests_vg = {
  68. .key = "valgrind-tests",
  69. .name = "Module's run and api tests succeed under valgrind",
  70. .can_run = can_run_vg,
  71. .check = do_run_tests_vg,
  72. .handle = run_under_debugger_vg
  73. };
  74. REGISTER_TEST(run_tests_vg, &run_tests, NULL);