1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: A simple execution monitor to test if wx samples crash at startup or not
4 // Author: Francesco Montorsi
8 // Copyright: (c) 2009 Francesco Montorsi
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/cmdline.h"
31 #include "wx/vector.h"
32 #include "wx/process.h"
33 #include "wx/sstream.h"
35 #include "wx/filename.h"
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 class MonitoredProcess
: public wxProcess
49 { Redirect(); m_crashed
=false; m_exitCode
=0; }
51 void OnTerminate(int WXUNUSED(pid
), int status
)
53 wxStringOutputStream out
, err
;
54 if (GetInputStream()) out
.Write(*GetInputStream());
55 if (GetErrorStream()) err
.Write(*GetErrorStream());
57 //wxPrintf("%s\n", stdout.GetString());
58 //wxPrintf("%s\n", stderr.GetString());
60 // when wx programs assert on wxGTK/wxMac, they put on stderr a message like:
61 // [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy
62 // but then the assert dialog pop-ups and thus the app doesn't exit
63 // FIXME: make assertion detection work also under other platforms
64 // see http://trac.wxwidgets.org/ticket/10697
65 m_crashed
= out
.GetString().Contains("assert") ||
66 err
.GetString().Contains("assert");
72 wxProcess::Kill(GetPid());
74 // wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
75 // but we still need to call it!
82 int GetExitCode() const
83 { return m_exitCode
; }
93 MonitorData(const wxString
& cmd
) : program(cmd
) {}
96 MonitoredProcess process
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 bool TestExec(const wxVector
<wxFileName
>& programs
, long timeout
)
106 wxVector
<MonitorData
*> data
;
108 // run all programs specified as command line parameters
110 for (i
=0; i
<programs
.size(); i
++)
112 MonitorData
*dt
= new MonitorData(programs
[i
].GetFullPath());
114 long pid
= wxExecute(programs
[i
].GetFullPath(), wxEXEC_ASYNC
, &dt
->process
);
116 wxLogError("could not run the program '%s'", programs
[i
].GetFullPath());
119 wxLogMessage("started program '%s' (pid %d)...",
120 programs
[i
].GetFullPath(), pid
);
121 wxASSERT(dt
->process
.GetPid() == pid
);
127 // sleep some moments
130 // check if all processes are still running
132 for (i
=0; i
<data
.size(); i
++)
134 MonitoredProcess
& proc
= data
[i
]->process
;
135 const wxString
& prog
= data
[i
]->program
;
137 if (wxProcess::Exists(proc
.GetPid()))
141 // this typically never happens, at least when running wx-programs
142 // built with debug builds of wx (see MonitoredProcess::OnTerminate;
143 // even if an asserts fail the app doesn't automatically close!):
145 wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
146 prog
, proc
.GetPid());
150 if (data
[i
]->process
.Crashed())
153 wxLogMessage("program '%s' (pid %d) crashed...",
154 prog
, proc
.GetPid());
157 wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
158 prog
, proc
.GetPid(), proc
.GetExitCode());
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 int main(int argc
, char **argv
)
170 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "execmon");
172 wxInitializer initializer
;
175 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
179 static const wxCmdLineEntryDesc cmdLineDesc
[] =
181 { wxCMD_LINE_SWITCH
, "h", "help",
182 "show this help message",
183 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
184 { wxCMD_LINE_OPTION
, "t", "timeout",
185 "kills all processes still alive after 'num' seconds",
186 wxCMD_LINE_VAL_NUMBER
, 0 },
187 { wxCMD_LINE_PARAM
, "", "",
189 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
194 wxLog::DisableTimestamp();
196 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
197 switch ( parser
.Parse() )
206 wxVector
<wxFileName
> programs
;
207 for (unsigned int i
=0; i
<parser
.GetParamCount(); i
++)
209 wxFileName
fn(parser
.GetParam(i
));
210 if (!fn
.IsAbsolute())
213 programs
.push_back(fn
);
217 if (!parser
.Found("t", &timeout
))
220 return TestExec(programs
, timeout
) ? 0 : 1;