run_tests_valgrind.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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;
  22. if (!run_command(m, &timeleft, &output,
  23. "valgrind -q --error-exitcode=0 true"))
  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. if (run_command(score, timeleft, &cmdout,
  41. "valgrind -q --error-exitcode=100%s %s",
  42. run_tests_vg.options ?
  43. run_tests_vg.options : "",
  44. i->compiled)) {
  45. score->score++;
  46. } else {
  47. score_file_error(score, i, 0, cmdout);
  48. }
  49. }
  50. }
  51. if (score->score == score->total)
  52. score->pass = true;
  53. }
  54. /* Gcc's warn_unused_result is fascist bullshit. */
  55. #define doesnt_matter()
  56. static void run_under_debugger_vg(struct manifest *m, struct score *score)
  57. {
  58. struct file_error *first;
  59. char *command;
  60. if (!ask("Should I run the first failing test under the debugger?"))
  61. return;
  62. first = list_top(&score->per_file_errors, struct file_error, list);
  63. command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
  64. first->file->compiled);
  65. if (system(command))
  66. doesnt_matter();
  67. }
  68. struct ccanlint run_tests_vg = {
  69. .key = "valgrind-tests",
  70. .name = "Module's run and api tests succeed under valgrind",
  71. .can_run = can_run_vg,
  72. .check = do_run_tests_vg,
  73. .handle = run_under_debugger_vg,
  74. .takes_options = true
  75. };
  76. REGISTER_TEST(run_tests_vg, &run_tests, NULL);