1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: A simple execution monitor to test if wx samples crash at startup or not
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2009 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/cmdline.h"
32 #include "wx/vector.h"
33 #include "wx/process.h"
34 #include "wx/sstream.h"
36 #include "wx/filename.h"
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class MonitoredProcess
: public wxProcess
52 { Redirect(); m_crashed
=false; m_exitCode
=0; }
54 void OnTerminate(int WXUNUSED(pid
), int status
)
56 wxStringOutputStream out
, err
;
57 if (GetInputStream()) out
.Write(*GetInputStream());
58 if (GetErrorStream()) err
.Write(*GetErrorStream());
60 //wxPrintf("%s\n", stdout.GetString());
61 //wxPrintf("%s\n", stderr.GetString());
63 // when wx programs assert on wxGTK/wxMac, they put on stderr a message like:
64 // [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy
65 // but then the assert dialog pop-ups and thus the app doesn't exit
66 // FIXME: make assertion detection work also under other platforms
67 // see http://trac.wxwidgets.org/ticket/10697
68 m_crashed
= out
.GetString().Contains("assert") ||
69 err
.GetString().Contains("assert");
75 wxProcess::Kill(GetPid());
77 // wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
78 // but we still need to call it!
85 int GetExitCode() const
86 { return m_exitCode
; }
96 MonitorData(const wxString
& cmd
) : program(cmd
) {}
99 MonitoredProcess process
;
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 bool TestExec(const wxVector
<wxFileName
>& programs
, long timeout
)
109 wxVector
<MonitorData
*> data
;
111 // run all programs specified as command line parameters
113 for (i
=0; i
<programs
.size(); i
++)
115 MonitorData
*dt
= new MonitorData(programs
[i
].GetFullPath());
117 long pid
= wxExecute(programs
[i
].GetFullPath(), wxEXEC_ASYNC
, &dt
->process
);
120 wxLogError("could not run the program '%s'", programs
[i
].GetFullPath());
124 wxLogMessage("started program '%s' (pid %d)...",
125 programs
[i
].GetFullPath(), pid
);
126 wxASSERT(dt
->process
.GetPid() == pid
);
132 // sleep some moments
135 // check if all processes are still running
137 for (i
=0; i
<data
.size(); i
++)
139 MonitoredProcess
& proc
= data
[i
]->process
;
140 const wxString
& prog
= data
[i
]->program
;
142 if (wxProcess::Exists(proc
.GetPid()))
146 // this typically never happens, at least when running wx-programs
147 // built with debug builds of wx (see MonitoredProcess::OnTerminate;
148 // even if an asserts fail the app doesn't automatically close!):
150 wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
151 prog
, proc
.GetPid());
155 if (data
[i
]->process
.Crashed())
158 wxLogMessage("program '%s' (pid %d) crashed...",
159 prog
, proc
.GetPid());
162 wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
163 prog
, proc
.GetPid(), proc
.GetExitCode());
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 int main(int argc
, char **argv
)
175 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "execmon");
177 wxInitializer initializer
;
180 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
184 static const wxCmdLineEntryDesc cmdLineDesc
[] =
186 { wxCMD_LINE_SWITCH
, "h", "help",
187 "show this help message",
188 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
189 { wxCMD_LINE_OPTION
, "t", "timeout",
190 "kills all processes still alive after 'num' seconds",
191 wxCMD_LINE_VAL_NUMBER
, 0 },
192 { wxCMD_LINE_PARAM
, "", "",
194 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
199 wxLog::DisableTimestamp();
201 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
202 switch ( parser
.Parse() )
211 wxVector
<wxFileName
> programs
;
212 for (unsigned int i
=0; i
<parser
.GetParamCount(); i
++)
214 wxFileName
fn(parser
.GetParam(i
));
215 if (!fn
.IsAbsolute())
218 programs
.push_back(fn
);
222 if (!parser
.Found("t", &timeout
))
225 return TestExec(programs
, timeout
) ? 0 : 1;