]>
Commit | Line | Data |
---|---|---|
aa113d87 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: execmon.cpp | |
3 | // Purpose: A simple execution monitor to test if wx samples crash at startup or not | |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 25/3/09 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2009 Francesco Montorsi | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
dbe31039 VZ |
28 | #include "wx/app.h" |
29 | #include "wx/log.h" | |
aa113d87 FM |
30 | #endif // WX_PRECOMP |
31 | ||
32 | #include "wx/cmdline.h" | |
33 | #include "wx/vector.h" | |
34 | #include "wx/process.h" | |
35 | #include "wx/sstream.h" | |
36 | #include "wx/utils.h" | |
37 | #include "wx/filename.h" | |
40531a9f FM |
38 | #include "wx/app.h" |
39 | #include "wx/log.h" | |
aa113d87 FM |
40 | |
41 | // ============================================================================ | |
42 | // implementation | |
43 | // ============================================================================ | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // utility classes | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | class MonitoredProcess : public wxProcess | |
50 | { | |
51 | public: | |
52 | MonitoredProcess() | |
53 | { Redirect(); m_crashed=false; m_exitCode=0; } | |
3814912f | 54 | |
aa113d87 FM |
55 | void OnTerminate(int WXUNUSED(pid), int status) |
56 | { | |
3814912f VZ |
57 | wxStringOutputStream out, err; |
58 | if (GetInputStream()) out.Write(*GetInputStream()); | |
59 | if (GetErrorStream()) err.Write(*GetErrorStream()); | |
60 | ||
aa113d87 FM |
61 | //wxPrintf("%s\n", stdout.GetString()); |
62 | //wxPrintf("%s\n", stderr.GetString()); | |
63 | ||
64 | // when wx programs assert on wxGTK/wxMac, they put on stderr a message like: | |
65 | // [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy | |
66 | // but then the assert dialog pop-ups and thus the app doesn't exit | |
67 | // FIXME: make assertion detection work also under other platforms | |
68 | // see http://trac.wxwidgets.org/ticket/10697 | |
3814912f VZ |
69 | m_crashed = out.GetString().Contains("assert") || |
70 | err.GetString().Contains("assert"); | |
aa113d87 FM |
71 | m_exitCode = status; |
72 | } | |
3814912f | 73 | |
aa113d87 FM |
74 | void Kill() |
75 | { | |
76 | wxProcess::Kill(GetPid()); | |
3814912f | 77 | |
aa113d87 FM |
78 | // wxProcess::Kill doesn't trigger a call to OnTerminate() normally... |
79 | // but we still need to call it! | |
80 | OnTerminate(0, -1); | |
81 | } | |
3814912f | 82 | |
aa113d87 FM |
83 | bool Crashed() const |
84 | { return m_crashed; } | |
3814912f | 85 | |
aa113d87 FM |
86 | int GetExitCode() const |
87 | { return m_exitCode; } | |
3814912f | 88 | |
aa113d87 FM |
89 | private: |
90 | bool m_crashed; | |
91 | int m_exitCode; | |
92 | }; | |
93 | ||
94 | class MonitorData | |
95 | { | |
96 | public: | |
97 | MonitorData(const wxString& cmd) : program(cmd) {} | |
98 | ||
99 | wxString program; | |
100 | MonitoredProcess process; | |
101 | }; | |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // the real main | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | bool TestExec(const wxVector<wxFileName>& programs, long timeout) | |
108 | { | |
3814912f | 109 | size_t i; |
aa113d87 | 110 | wxVector<MonitorData*> data; |
3814912f | 111 | |
aa113d87 FM |
112 | // run all programs specified as command line parameters |
113 | wxArrayLong procID; | |
3814912f | 114 | for (i=0; i<programs.size(); i++) |
aa113d87 FM |
115 | { |
116 | MonitorData *dt = new MonitorData(programs[i].GetFullPath()); | |
3814912f | 117 | |
aa113d87 FM |
118 | long pid = wxExecute(programs[i].GetFullPath(), wxEXEC_ASYNC, &dt->process); |
119 | if (pid == 0) | |
120 | wxLogError("could not run the program '%s'", programs[i].GetFullPath()); | |
121 | else | |
122 | { | |
3814912f | 123 | wxLogMessage("started program '%s' (pid %d)...", |
aa113d87 FM |
124 | programs[i].GetFullPath(), pid); |
125 | wxASSERT(dt->process.GetPid() == pid); | |
126 | ||
127 | data.push_back(dt); | |
128 | } | |
129 | } | |
3814912f | 130 | |
aa113d87 FM |
131 | // sleep some moments |
132 | wxSleep(timeout); | |
3814912f | 133 | |
aa113d87 FM |
134 | // check if all processes are still running |
135 | bool allok = true; | |
3814912f | 136 | for (i=0; i<data.size(); i++) |
aa113d87 FM |
137 | { |
138 | MonitoredProcess& proc = data[i]->process; | |
139 | const wxString& prog = data[i]->program; | |
3814912f | 140 | |
aa113d87 FM |
141 | if (wxProcess::Exists(proc.GetPid())) |
142 | proc.Kill(); | |
143 | else | |
144 | { | |
145 | // this typically never happens, at least when running wx-programs | |
146 | // built with debug builds of wx (see MonitoredProcess::OnTerminate; | |
147 | // even if an asserts fail the app doesn't automatically close!): | |
3814912f VZ |
148 | |
149 | wxLogMessage("program '%s' (pid %d) is NOT running anymore...", | |
aa113d87 FM |
150 | prog, proc.GetPid()); |
151 | allok = false; | |
152 | } | |
153 | ||
154 | if (data[i]->process.Crashed()) | |
155 | { | |
156 | allok = false; | |
3814912f | 157 | wxLogMessage("program '%s' (pid %d) crashed...", |
aa113d87 FM |
158 | prog, proc.GetPid()); |
159 | } | |
160 | else | |
3814912f | 161 | wxLogMessage("program '%s' (pid %d) ended with exit code %d...", |
aa113d87 FM |
162 | prog, proc.GetPid(), proc.GetExitCode()); |
163 | } | |
3814912f | 164 | |
aa113d87 FM |
165 | return allok; |
166 | } | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // main | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | int main(int argc, char **argv) | |
173 | { | |
174 | wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "execmon"); | |
175 | ||
176 | wxInitializer initializer; | |
177 | if ( !initializer ) | |
178 | { | |
179 | fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); | |
180 | return -1; | |
181 | } | |
182 | ||
183 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
184 | { | |
3814912f | 185 | { wxCMD_LINE_SWITCH, "h", "help", |
aa113d87 FM |
186 | "show this help message", |
187 | wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, | |
3814912f | 188 | { wxCMD_LINE_OPTION, "t", "timeout", |
aa113d87 FM |
189 | "kills all processes still alive after 'num' seconds", |
190 | wxCMD_LINE_VAL_NUMBER, 0 }, | |
3814912f | 191 | { wxCMD_LINE_PARAM, "", "", |
aa113d87 FM |
192 | "program-to-run", |
193 | wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, | |
194 | ||
195 | { wxCMD_LINE_NONE } | |
196 | }; | |
3814912f | 197 | |
aa113d87 FM |
198 | wxLog::DisableTimestamp(); |
199 | ||
200 | wxCmdLineParser parser(cmdLineDesc, argc, argv); | |
201 | switch ( parser.Parse() ) | |
202 | { | |
203 | case -1: | |
204 | // help was shown | |
205 | break; | |
206 | ||
207 | case 0: | |
208 | { | |
209 | // check arguments | |
210 | wxVector<wxFileName> programs; | |
211 | for (unsigned int i=0; i<parser.GetParamCount(); i++) | |
212 | { | |
213 | wxFileName fn(parser.GetParam(i)); | |
214 | if (!fn.IsAbsolute()) | |
215 | fn.MakeAbsolute(); | |
3814912f | 216 | |
aa113d87 FM |
217 | programs.push_back(fn); |
218 | } | |
3814912f | 219 | |
aa113d87 FM |
220 | long timeout; |
221 | if (!parser.Found("t", &timeout)) | |
222 | timeout = 3; | |
3814912f | 223 | |
aa113d87 FM |
224 | return TestExec(programs, timeout) ? 0 : 1; |
225 | } | |
226 | break; | |
227 | ||
228 | default: | |
229 | // syntax error | |
230 | break; | |
231 | } | |
232 | ||
233 | return 0; | |
234 | } | |
235 |