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"
39 // ============================================================================
41 // ============================================================================
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class MonitoredProcess
: public wxProcess
51 { Redirect(); m_crashed
=false; m_exitCode
=0; }
53 void OnTerminate(int WXUNUSED(pid
), int status
)
55 wxStringOutputStream out
, err
;
56 if (GetInputStream()) out
.Write(*GetInputStream());
57 if (GetErrorStream()) err
.Write(*GetErrorStream());
59 //wxPrintf("%s\n", stdout.GetString());
60 //wxPrintf("%s\n", stderr.GetString());
62 // when wx programs assert on wxGTK/wxMac, they put on stderr a message like:
63 // [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy
64 // but then the assert dialog pop-ups and thus the app doesn't exit
65 // FIXME: make assertion detection work also under other platforms
66 // see http://trac.wxwidgets.org/ticket/10697
67 m_crashed
= out
.GetString().Contains("assert") ||
68 err
.GetString().Contains("assert");
74 wxProcess::Kill(GetPid());
76 // wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
77 // but we still need to call it!
84 int GetExitCode() const
85 { return m_exitCode
; }
95 MonitorData(const wxString
& cmd
) : program(cmd
) {}
98 MonitoredProcess process
;
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 bool TestExec(const wxVector
<wxFileName
>& programs
, long timeout
)
108 wxVector
<MonitorData
*> data
;
110 // run all programs specified as command line parameters
112 for (i
=0; i
<programs
.size(); i
++)
114 MonitorData
*dt
= new MonitorData(programs
[i
].GetFullPath());
116 long pid
= wxExecute(programs
[i
].GetFullPath(), wxEXEC_ASYNC
, &dt
->process
);
118 wxLogError("could not run the program '%s'", programs
[i
].GetFullPath());
121 wxLogMessage("started program '%s' (pid %d)...",
122 programs
[i
].GetFullPath(), pid
);
123 wxASSERT(dt
->process
.GetPid() == pid
);
129 // sleep some moments
132 // check if all processes are still running
134 for (i
=0; i
<data
.size(); i
++)
136 MonitoredProcess
& proc
= data
[i
]->process
;
137 const wxString
& prog
= data
[i
]->program
;
139 if (wxProcess::Exists(proc
.GetPid()))
143 // this typically never happens, at least when running wx-programs
144 // built with debug builds of wx (see MonitoredProcess::OnTerminate;
145 // even if an asserts fail the app doesn't automatically close!):
147 wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
148 prog
, proc
.GetPid());
152 if (data
[i
]->process
.Crashed())
155 wxLogMessage("program '%s' (pid %d) crashed...",
156 prog
, proc
.GetPid());
159 wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
160 prog
, proc
.GetPid(), proc
.GetExitCode());
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 int main(int argc
, char **argv
)
172 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "execmon");
174 wxInitializer initializer
;
177 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
181 static const wxCmdLineEntryDesc cmdLineDesc
[] =
183 { wxCMD_LINE_SWITCH
, "h", "help",
184 "show this help message",
185 wxCMD_LINE_VAL_NONE
, wxCMD_LINE_OPTION_HELP
},
186 { wxCMD_LINE_OPTION
, "t", "timeout",
187 "kills all processes still alive after 'num' seconds",
188 wxCMD_LINE_VAL_NUMBER
, 0 },
189 { wxCMD_LINE_PARAM
, "", "",
191 wxCMD_LINE_VAL_STRING
, wxCMD_LINE_PARAM_MULTIPLE
},
196 wxLog::DisableTimestamp();
198 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
199 switch ( parser
.Parse() )
208 wxVector
<wxFileName
> programs
;
209 for (unsigned int i
=0; i
<parser
.GetParamCount(); i
++)
211 wxFileName
fn(parser
.GetParam(i
));
212 if (!fn
.IsAbsolute())
215 programs
.push_back(fn
);
219 if (!parser
.Found("t", &timeout
))
222 return TestExec(programs
, timeout
) ? 0 : 1;