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"
32 #include "wx/cmdline.h"
33 #include "wx/vector.h"
34 #include "wx/process.h"
35 #include "wx/sstream.h"
37 #include "wx/filename.h"
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class MonitoredProcess
: public wxProcess
53 { Redirect(); m_crashed
=false; m_exitCode
=0; }
55 void OnTerminate(int WXUNUSED(pid
), int status
)
57 wxStringOutputStream out
, err
;
58 if (GetInputStream()) out
.Write(*GetInputStream());
59 if (GetErrorStream()) err
.Write(*GetErrorStream());
61 //wxPrintf("%s\n", stdout.GetString());
62 //wxPrintf("%s\n", stderr.GetString());
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
69 m_crashed
= out
.GetString().Contains("assert") ||
70 err
.GetString().Contains("assert");
76 wxProcess::Kill(GetPid());
78 // wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
79 // but we still need to call it!
86 int GetExitCode() const
87 { return m_exitCode
; }
97 MonitorData(const wxString
& cmd
) : program(cmd
) {}
100 MonitoredProcess process
;
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 bool TestExec(const wxVector
<wxFileName
>& programs
, long timeout
)
110 wxVector
<MonitorData
*> data
;
112 // run all programs specified as command line parameters
114 for (i
=0; i
<programs
.size(); i
++)
116 MonitorData
*dt
= new MonitorData(programs
[i
].GetFullPath());
118 long pid
= wxExecute(programs
[i
].GetFullPath(), wxEXEC_ASYNC
, &dt
->process
);
120 wxLogError("could not run the program '%s'", programs
[i
].GetFullPath());
123 wxLogMessage("started program '%s' (pid %d)...",
124 programs
[i
].GetFullPath(), pid
);
125 wxASSERT(dt
->process
.GetPid() == pid
);
131 // sleep some moments
134 // check if all processes are still running
136 for (i
=0; i
<data
.size(); i
++)
138 MonitoredProcess
& proc
= data
[i
]->process
;
139 const wxString
& prog
= data
[i
]->program
;
141 if (wxProcess::Exists(proc
.GetPid()))
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!):
149 wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
150 prog
, proc
.GetPid());
154 if (data
[i
]->process
.Crashed())
157 wxLogMessage("program '%s' (pid %d) crashed...",
158 prog
, proc
.GetPid());
161 wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
162 prog
, proc
.GetPid(), proc
.GetExitCode());
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 int main(int argc
, char **argv
)
174 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "execmon");
176 wxInitializer initializer
;
179 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
183 static const wxCmdLineEntryDesc cmdLineDesc
[] =
185 { wxCMD_LINE_SWITCH
, "h", "help",
186 "show this help message",
187 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
188 { wxCMD_LINE_OPTION
, "t", "timeout",
189 "kills all processes still alive after 'num' seconds",
190 wxCMD_LINE_VAL_NUMBER
, 0 },
191 { wxCMD_LINE_PARAM
, "", "",
193 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
198 wxLog::DisableTimestamp();
200 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
201 switch ( parser
.Parse() )
210 wxVector
<wxFileName
> programs
;
211 for (unsigned int i
=0; i
<parser
.GetParamCount(); i
++)
213 wxFileName
fn(parser
.GetParam(i
));
214 if (!fn
.IsAbsolute())
217 programs
.push_back(fn
);
221 if (!parser
.Found("t", &timeout
))
224 return TestExec(programs
, timeout
) ? 0 : 1;