source: tags/ms_r16q2/TEMPLATES/ut_valgrinded.h

Last change on this file was 11889, checked in by westram, 10 years ago
  • add missing comment to closing #endif of test-sections
File size: 3.4 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : ut_valgrinded.h                                    //
4//   Purpose   : wrapper to call subprocesses inside valgrind       //
5//                                                                  //
6//   Coded by Ralf Westram (coder@reallysoft.de) in February 2011   //
7//   Institute of Microbiology (Technical University Munich)        //
8//   http://www.arb-home.de/                                        //
9//                                                                  //
10// ================================================================ //
11
12#ifndef UT_VALGRINDED_H
13#define UT_VALGRINDED_H
14
15
16#ifdef UNIT_TESTS
17
18#ifndef ARB_MSG_H
19#include <arb_msg.h>
20#endif
21#ifndef _SYS_STAT_H
22#include <sys/stat.h>
23#endif
24
25#define UTVG_CALL_SEEN "flag.valgrind.callseen"
26
27namespace utvg {
28
29    inline const char *flag_name(const char *name) {
30        const char *ARBHOME = getenv("ARBHOME");
31        const int   BUFSIZE = 200;
32        static char buf[BUFSIZE];
33
34        IF_ASSERTION_USED(int printed =)
35            snprintf(buf, BUFSIZE, "%s/UNIT_TESTER/valgrind/%s", ARBHOME, name);
36        arb_assert(printed<BUFSIZE);
37
38        return buf;
39    }
40    inline bool flag_exists(const char *name) {
41        const char  *path = flag_name(name);
42        struct stat  stt;
43
44        return stat(path, &stt) == 0 && S_ISREG(stt.st_mode);
45    }
46    inline void raise_flag(const char *name) {
47        const char *path = flag_name(name);
48        FILE       *fp   = fopen(path, "w");
49        arb_assert(fp);
50        fclose(fp);
51    }
52
53    struct valgrind_info {
54        bool wanted;
55        bool leaks;
56        bool reachable;
57
58        valgrind_info() {
59            // The following flag files are generated by ../UNIT_TESTER/Makefile.suite
60            // which reads the settings from ../UNIT_TESTER/Makefile.setup.local
61            wanted    = flag_exists("flag.valgrind");
62            leaks     = flag_exists("flag.valgrind.leaks");
63            reachable = flag_exists("flag.valgrind.reachable");
64        }
65    };
66
67    inline const valgrind_info& get_valgrind_info() {
68        static valgrind_info vinfo;
69        return vinfo;
70    }
71};
72
73inline void make_valgrinded_call(char *&command) {
74    using namespace utvg;
75    const valgrind_info& valgrind = get_valgrind_info();
76    if (valgrind.wanted) {
77// #define VALGRIND_ONLY_SOME
78#if defined(VALGRIND_ONLY_SOME)
79        bool perform_valgrind = false;
80
81        perform_valgrind = perform_valgrind || strstr(command, "arb_pt_server");
82        perform_valgrind = perform_valgrind || strstr(command, "arb_primer");
83
84        if (!perform_valgrind) return;
85#endif
86
87        const char *switches           = valgrind.leaks ? (valgrind.reachable ? "-l -r" : "-l") : "";
88        char       *valgrinded_command = GBS_global_string_copy("$ARBHOME/UNIT_TESTER/valgrind/arb_valgrind_logged CALL %s -c 15 %s", switches, command);
89        freeset(command, valgrinded_command);
90
91        utvg::raise_flag(UTVG_CALL_SEEN);
92    }
93}
94
95inline bool will_valgrind_calls() { return utvg::get_valgrind_info().wanted; }
96inline bool seen_valgrinded_call() { return utvg::flag_exists(UTVG_CALL_SEEN); }
97
98#else // !UNIT_TESTS
99
100#define make_valgrinded_call(command)
101inline bool will_valgrind_calls() { return false; }
102inline bool seen_valgrinded_call() { return false; }
103
104#endif // UNIT_TESTS
105
106
107#else
108#error ut_valgrinded.h included twice
109#endif // UT_VALGRINDED_H
Note: See TracBrowser for help on using the repository browser.