2009-04-12 07:44:00 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: execmon.cpp
|
|
|
|
// Purpose: A simple execution monitor to test if wx samples crash at startup or not
|
|
|
|
// Author: Francesco Montorsi
|
|
|
|
// Modified by:
|
|
|
|
// Created: 25/3/09
|
|
|
|
// Copyright: (c) 2009 Francesco Montorsi
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2009-04-12 07:44:00 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2009-04-12 14:42:58 -04:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
2009-04-12 07:44:00 -04:00
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/cmdline.h"
|
|
|
|
#include "wx/vector.h"
|
|
|
|
#include "wx/process.h"
|
|
|
|
#include "wx/sstream.h"
|
|
|
|
#include "wx/utils.h"
|
|
|
|
#include "wx/filename.h"
|
2009-04-12 18:10:21 -04:00
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
2009-04-12 07:44:00 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// utility classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MonitoredProcess : public wxProcess
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MonitoredProcess()
|
|
|
|
{ Redirect(); m_crashed=false; m_exitCode=0; }
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
void OnTerminate(int WXUNUSED(pid), int status)
|
|
|
|
{
|
2009-04-12 14:41:51 -04:00
|
|
|
wxStringOutputStream out, err;
|
|
|
|
if (GetInputStream()) out.Write(*GetInputStream());
|
|
|
|
if (GetErrorStream()) err.Write(*GetErrorStream());
|
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
//wxPrintf("%s\n", stdout.GetString());
|
|
|
|
//wxPrintf("%s\n", stderr.GetString());
|
|
|
|
|
|
|
|
// when wx programs assert on wxGTK/wxMac, they put on stderr a message like:
|
|
|
|
// [Debug] date somefilename.pp(nnnn): assert "xxxxx" failed in yyyy
|
|
|
|
// but then the assert dialog pop-ups and thus the app doesn't exit
|
|
|
|
// FIXME: make assertion detection work also under other platforms
|
2017-07-04 15:15:14 -04:00
|
|
|
// see https://trac.wxwidgets.org/ticket/10697
|
2009-04-12 14:41:51 -04:00
|
|
|
m_crashed = out.GetString().Contains("assert") ||
|
|
|
|
err.GetString().Contains("assert");
|
2009-04-12 07:44:00 -04:00
|
|
|
m_exitCode = status;
|
|
|
|
}
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
void Kill()
|
|
|
|
{
|
|
|
|
wxProcess::Kill(GetPid());
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
// wxProcess::Kill doesn't trigger a call to OnTerminate() normally...
|
|
|
|
// but we still need to call it!
|
|
|
|
OnTerminate(0, -1);
|
|
|
|
}
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
bool Crashed() const
|
|
|
|
{ return m_crashed; }
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
int GetExitCode() const
|
|
|
|
{ return m_exitCode; }
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
private:
|
|
|
|
bool m_crashed;
|
|
|
|
int m_exitCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MonitorData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MonitorData(const wxString& cmd) : program(cmd) {}
|
|
|
|
|
|
|
|
wxString program;
|
|
|
|
MonitoredProcess process;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the real main
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool TestExec(const wxVector<wxFileName>& programs, long timeout)
|
|
|
|
{
|
2009-04-12 14:41:51 -04:00
|
|
|
size_t i;
|
2009-04-12 07:44:00 -04:00
|
|
|
wxVector<MonitorData*> data;
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
// run all programs specified as command line parameters
|
|
|
|
wxArrayLong procID;
|
2009-04-12 14:41:51 -04:00
|
|
|
for (i=0; i<programs.size(); i++)
|
2009-04-12 07:44:00 -04:00
|
|
|
{
|
|
|
|
MonitorData *dt = new MonitorData(programs[i].GetFullPath());
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
long pid = wxExecute(programs[i].GetFullPath(), wxEXEC_ASYNC, &dt->process);
|
|
|
|
if (pid == 0)
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2009-04-12 07:44:00 -04:00
|
|
|
wxLogError("could not run the program '%s'", programs[i].GetFullPath());
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2009-04-12 07:44:00 -04:00
|
|
|
else
|
|
|
|
{
|
2009-04-12 14:41:51 -04:00
|
|
|
wxLogMessage("started program '%s' (pid %d)...",
|
2009-04-12 07:44:00 -04:00
|
|
|
programs[i].GetFullPath(), pid);
|
|
|
|
wxASSERT(dt->process.GetPid() == pid);
|
|
|
|
|
|
|
|
data.push_back(dt);
|
|
|
|
}
|
|
|
|
}
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
// sleep some moments
|
|
|
|
wxSleep(timeout);
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
// check if all processes are still running
|
|
|
|
bool allok = true;
|
2009-04-12 14:41:51 -04:00
|
|
|
for (i=0; i<data.size(); i++)
|
2009-04-12 07:44:00 -04:00
|
|
|
{
|
|
|
|
MonitoredProcess& proc = data[i]->process;
|
|
|
|
const wxString& prog = data[i]->program;
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
if (wxProcess::Exists(proc.GetPid()))
|
|
|
|
proc.Kill();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this typically never happens, at least when running wx-programs
|
|
|
|
// built with debug builds of wx (see MonitoredProcess::OnTerminate;
|
|
|
|
// even if an asserts fail the app doesn't automatically close!):
|
2009-04-12 14:41:51 -04:00
|
|
|
|
|
|
|
wxLogMessage("program '%s' (pid %d) is NOT running anymore...",
|
2009-04-12 07:44:00 -04:00
|
|
|
prog, proc.GetPid());
|
|
|
|
allok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data[i]->process.Crashed())
|
|
|
|
{
|
|
|
|
allok = false;
|
2009-04-12 14:41:51 -04:00
|
|
|
wxLogMessage("program '%s' (pid %d) crashed...",
|
2009-04-12 07:44:00 -04:00
|
|
|
prog, proc.GetPid());
|
|
|
|
}
|
|
|
|
else
|
2009-04-12 14:41:51 -04:00
|
|
|
wxLogMessage("program '%s' (pid %d) ended with exit code %d...",
|
2009-04-12 07:44:00 -04:00
|
|
|
prog, proc.GetPid(), proc.GetExitCode());
|
|
|
|
}
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
return allok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "execmon");
|
|
|
|
|
|
|
|
wxInitializer initializer;
|
|
|
|
if ( !initializer )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const wxCmdLineEntryDesc cmdLineDesc[] =
|
|
|
|
{
|
2009-04-12 14:41:51 -04:00
|
|
|
{ wxCMD_LINE_SWITCH, "h", "help",
|
2009-04-12 07:44:00 -04:00
|
|
|
"show this help message",
|
|
|
|
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
|
2009-04-12 14:41:51 -04:00
|
|
|
{ wxCMD_LINE_OPTION, "t", "timeout",
|
2009-04-12 07:44:00 -04:00
|
|
|
"kills all processes still alive after 'num' seconds",
|
|
|
|
wxCMD_LINE_VAL_NUMBER, 0 },
|
2009-04-12 14:41:51 -04:00
|
|
|
{ wxCMD_LINE_PARAM, "", "",
|
2009-04-12 07:44:00 -04:00
|
|
|
"program-to-run",
|
|
|
|
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
|
|
|
|
|
|
|
|
{ wxCMD_LINE_NONE }
|
|
|
|
};
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
wxLog::DisableTimestamp();
|
|
|
|
|
|
|
|
wxCmdLineParser parser(cmdLineDesc, argc, argv);
|
|
|
|
switch ( parser.Parse() )
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
// help was shown
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// check arguments
|
|
|
|
wxVector<wxFileName> programs;
|
|
|
|
for (unsigned int i=0; i<parser.GetParamCount(); i++)
|
|
|
|
{
|
|
|
|
wxFileName fn(parser.GetParam(i));
|
|
|
|
if (!fn.IsAbsolute())
|
|
|
|
fn.MakeAbsolute();
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
programs.push_back(fn);
|
|
|
|
}
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
long timeout;
|
|
|
|
if (!parser.Found("t", &timeout))
|
|
|
|
timeout = 3;
|
2009-04-12 14:41:51 -04:00
|
|
|
|
2009-04-12 07:44:00 -04:00
|
|
|
return TestExec(programs, timeout) ? 0 : 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// syntax error
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|