2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
|
2009-02-19 23:21:18 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-19 23:21:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "threadexecutor.h"
|
|
|
|
#include "cppcheck.h"
|
|
|
|
#include <iostream>
|
2009-03-01 20:56:27 +01:00
|
|
|
#include <algorithm>
|
2011-03-04 23:27:29 +01:00
|
|
|
#ifdef THREADING_MODEL_FORK
|
2009-02-20 20:40:42 +01:00
|
|
|
#include <sys/wait.h>
|
2009-02-19 23:21:18 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2009-02-20 20:53:14 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2009-03-06 21:46:32 +01:00
|
|
|
#include <cstdio>
|
2010-07-07 14:42:39 +02:00
|
|
|
#include <errno.h>
|
2010-12-30 21:35:53 +01:00
|
|
|
#include <time.h>
|
2009-02-20 20:40:42 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2011-04-19 13:52:32 +02:00
|
|
|
ThreadExecutor::ThreadExecutor(const std::vector<std::string> &filenames, const std::map<std::string, long> &filesizes, Settings &settings, ErrorLogger &errorLogger)
|
|
|
|
: _filenames(filenames), _filesizes(filesizes), _settings(settings), _errorLogger(errorLogger), _fileCount(0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2011-03-04 23:27:29 +01:00
|
|
|
#ifdef THREADING_MODEL_FORK
|
2011-03-05 04:43:22 +01:00
|
|
|
_wpipe = 0;
|
2010-12-09 19:03:41 +01:00
|
|
|
#endif
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadExecutor::~ThreadExecutor()
|
|
|
|
{
|
|
|
|
//dtor
|
|
|
|
}
|
|
|
|
|
2010-06-14 22:18:09 +02:00
|
|
|
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
|
|
|
|
{
|
|
|
|
_fileContents[ path ] = content;
|
|
|
|
}
|
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-03-04 23:27:29 +01:00
|
|
|
////// This code is for platforms that support fork() only ////////////////////
|
2009-02-20 20:40:42 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-03-04 23:27:29 +01:00
|
|
|
#ifdef THREADING_MODEL_FORK
|
2009-02-20 20:40:42 +01:00
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
char type = 0;
|
2011-03-05 04:43:22 +01:00
|
|
|
if (read(rpipe, &type, 1) <= 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2010-07-31 14:44:08 +02:00
|
|
|
if (errno == EAGAIN)
|
2010-07-07 14:42:39 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type != '1' && type != '2' && type != '3')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2009-02-20 20:40:42 +01:00
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
2009-02-19 23:21:18 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int len = 0;
|
2011-03-05 04:43:22 +01:00
|
|
|
if (read(rpipe, &len, sizeof(len)) <= 0)
|
2009-03-01 21:08:47 +01:00
|
|
|
{
|
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
char *buf = new char[len];
|
2011-03-05 04:43:22 +01:00
|
|
|
if (read(rpipe, buf, len) <= 0)
|
2009-03-01 21:08:47 +01:00
|
|
|
{
|
|
|
|
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (type == '1')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
_errorLogger.reportOut(buf);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type == '2')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
ErrorLogger::ErrorMessage msg;
|
|
|
|
msg.deserialize(buf);
|
2009-03-01 20:34:32 +01:00
|
|
|
|
2011-02-16 02:12:15 +01:00
|
|
|
std::string file;
|
|
|
|
unsigned int line(0);
|
|
|
|
if (!msg._callStack.empty())
|
2009-03-01 20:34:32 +01:00
|
|
|
{
|
2011-02-16 02:12:15 +01:00
|
|
|
file = msg._callStack.back().getfile(false);
|
|
|
|
line = msg._callStack.back().line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_settings.nomsg.isSuppressed(msg._id, file, line))
|
|
|
|
{
|
|
|
|
// Alert only about unique errors
|
|
|
|
std::string errmsg = msg.toString(_settings._verbose);
|
|
|
|
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
|
|
|
|
{
|
|
|
|
_errorList.push_back(errmsg);
|
|
|
|
_errorLogger.reportErr(msg);
|
|
|
|
}
|
2009-03-01 20:34:32 +01:00
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (type == '3')
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
std::istringstream iss(buf);
|
|
|
|
unsigned int fileResult = 0;
|
|
|
|
iss >> fileResult;
|
|
|
|
result += fileResult;
|
2010-07-07 14:42:39 +02:00
|
|
|
delete [] buf;
|
|
|
|
return -1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buf;
|
2010-07-07 14:42:39 +02:00
|
|
|
return 1;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
|
|
|
_fileCount = 0;
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
2011-04-19 13:52:32 +02:00
|
|
|
long totalfilesize = 0;
|
|
|
|
for (std::map<std::string, long>::const_iterator i = _filesizes.begin(); i != _filesizes.end(); ++i)
|
|
|
|
{
|
|
|
|
totalfilesize += i->second;
|
|
|
|
}
|
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
std::list<int> rpipes;
|
|
|
|
std::map<pid_t, std::string> childFile;
|
2011-04-19 13:52:32 +02:00
|
|
|
std::map<int, std::string> pipeFile;
|
|
|
|
long processedsize = 0;
|
2010-07-07 14:42:39 +02:00
|
|
|
unsigned int i = 0;
|
2010-07-31 14:44:08 +02:00
|
|
|
while (true)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2010-07-07 14:42:39 +02:00
|
|
|
// Start a new child
|
2011-03-05 04:43:22 +01:00
|
|
|
if (i < _filenames.size() && rpipes.size() < _settings._jobs)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2011-03-05 04:43:22 +01:00
|
|
|
int pipes[2];
|
|
|
|
if (pipe(pipes) == -1)
|
|
|
|
{
|
|
|
|
perror("pipe");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0)
|
|
|
|
{
|
|
|
|
perror("fcntl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(pipes[0], F_SETFL, flags | O_NONBLOCK) < 0)
|
|
|
|
{
|
|
|
|
perror("fcntl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-07-07 14:42:39 +02:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
// Error
|
|
|
|
std::cerr << "Failed to create child process" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
else if (pid == 0)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2011-03-05 04:43:22 +01:00
|
|
|
close(pipes[0]);
|
|
|
|
_wpipe = pipes[1];
|
|
|
|
|
2011-02-16 02:12:15 +01:00
|
|
|
CppCheck fileChecker(*this, false);
|
2010-07-07 14:42:39 +02:00
|
|
|
fileChecker.settings(_settings);
|
2011-04-24 18:10:25 +02:00
|
|
|
unsigned int resultOfCheck = 0;
|
2010-07-07 14:42:39 +02:00
|
|
|
|
|
|
|
if (_fileContents.size() > 0 && _fileContents.find(_filenames[i]) != _fileContents.end())
|
|
|
|
{
|
|
|
|
// File content was given as a string
|
2011-04-24 18:10:25 +02:00
|
|
|
resultOfCheck = fileChecker.check(_filenames[i], _fileContents[ _filenames[i] ]);
|
2010-07-07 14:42:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Read file from a file
|
2011-04-24 18:10:25 +02:00
|
|
|
resultOfCheck = fileChecker.check(_filenames[i]);
|
2010-07-07 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << resultOfCheck;
|
|
|
|
writeToPipe('3', oss.str());
|
|
|
|
exit(0);
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
close(pipes[1]);
|
|
|
|
rpipes.push_back(pipes[0]);
|
|
|
|
childFile[pid] = _filenames[i];
|
2011-04-19 13:52:32 +02:00
|
|
|
pipeFile[pipes[0]] = _filenames[i];
|
2011-03-05 04:43:22 +01:00
|
|
|
|
2010-07-07 14:42:39 +02:00
|
|
|
++i;
|
|
|
|
}
|
2011-03-05 04:43:22 +01:00
|
|
|
else if (!rpipes.empty())
|
2010-07-07 14:42:39 +02:00
|
|
|
{
|
2011-03-05 04:43:22 +01:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
for (std::list<int>::const_iterator rp = rpipes.begin(); rp != rpipes.end(); ++rp)
|
|
|
|
FD_SET(*rp, &rfds);
|
|
|
|
|
|
|
|
int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (r > 0)
|
2010-07-07 14:42:39 +02:00
|
|
|
{
|
2011-03-05 04:43:22 +01:00
|
|
|
std::list<int>::iterator rp = rpipes.begin();
|
|
|
|
while (rp != rpipes.end())
|
2010-12-30 21:35:53 +01:00
|
|
|
{
|
2011-03-05 04:43:22 +01:00
|
|
|
if (FD_ISSET(*rp, &rfds))
|
|
|
|
{
|
|
|
|
int readRes = handleRead(*rp, result);
|
|
|
|
if (readRes == -1)
|
|
|
|
{
|
2011-04-19 13:52:32 +02:00
|
|
|
long size = 0;
|
|
|
|
std::map<int, std::string>::iterator p = pipeFile.find(*rp);
|
|
|
|
if (p != pipeFile.end())
|
|
|
|
{
|
|
|
|
std::string name = p->second;
|
|
|
|
pipeFile.erase(p);
|
|
|
|
std::map<std::string, long>::const_iterator fs = _filesizes.find(name);
|
|
|
|
if (fs != _filesizes.end())
|
|
|
|
{
|
|
|
|
size = fs->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_fileCount++;
|
|
|
|
processedsize += size;
|
|
|
|
_errorLogger.reportStatus(_fileCount, _filenames.size(), processedsize, totalfilesize);
|
|
|
|
|
2011-03-05 04:43:22 +01:00
|
|
|
close(*rp);
|
|
|
|
rp = rpipes.erase(rp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++rp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++rp;
|
2010-12-30 21:35:53 +01:00
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int stat = 0;
|
2011-03-05 04:43:22 +01:00
|
|
|
pid_t child = waitpid(0, &stat, WNOHANG);
|
|
|
|
if (child > 0)
|
|
|
|
{
|
|
|
|
std::string childname;
|
|
|
|
std::map<pid_t, std::string>::iterator c = childFile.find(child);
|
|
|
|
if (c != childFile.end())
|
|
|
|
{
|
|
|
|
childname = c->second;
|
|
|
|
childFile.erase(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFSIGNALED(stat))
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "Internal error: Child process crashed with signal " << WTERMSIG(stat);
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
|
|
|
|
locations.push_back(ErrorLogger::ErrorMessage::FileLocation(childname, 0));
|
|
|
|
const ErrorLogger::ErrorMessage errmsg(locations,
|
|
|
|
Severity::error,
|
|
|
|
oss.str(),
|
2011-04-14 18:02:01 +02:00
|
|
|
"cppcheckError",
|
|
|
|
false);
|
2011-03-05 04:43:22 +01:00
|
|
|
_errorLogger.reportErr(errmsg);
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
2011-03-05 04:43:22 +01:00
|
|
|
else
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
2010-07-07 14:42:39 +02:00
|
|
|
// All done
|
|
|
|
break;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::writeToPipe(char type, const std::string &data)
|
|
|
|
{
|
2010-12-31 12:04:37 +01:00
|
|
|
unsigned int len = data.length() + 1;
|
2009-02-19 23:21:18 +01:00
|
|
|
char *out = new char[ len + 1 + sizeof(len)];
|
|
|
|
out[0] = type;
|
2009-02-20 20:53:14 +01:00
|
|
|
std::memcpy(&(out[1]), &len, sizeof(len));
|
|
|
|
std::memcpy(&(out[1+sizeof(len)]), data.c_str(), len);
|
2011-03-05 04:43:22 +01:00
|
|
|
if (write(_wpipe, out, len + 1 + sizeof(len)) <= 0)
|
2009-03-01 21:08:47 +01:00
|
|
|
{
|
2009-05-09 21:40:22 +02:00
|
|
|
delete [] out;
|
|
|
|
out = 0;
|
2009-03-01 21:08:47 +01:00
|
|
|
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
2009-05-09 21:40:22 +02:00
|
|
|
|
|
|
|
delete [] out;
|
2009-02-19 23:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportOut(const std::string &outmsg)
|
|
|
|
{
|
|
|
|
writeToPipe('1', outmsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|
|
|
{
|
|
|
|
writeToPipe('2', msg.serialize());
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:52:32 +02:00
|
|
|
void ThreadExecutor::reportStatus(unsigned int /*fileindex*/, unsigned int /*filecount*/, long /*sizedone*/, long /*sizetotal*/)
|
2009-02-19 23:21:18 +01:00
|
|
|
{
|
|
|
|
// Not used
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2009-02-20 20:40:42 +01:00
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2009-02-20 20:40:42 +01:00
|
|
|
void ThreadExecutor::reportOut(const std::string &/*outmsg*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
void ThreadExecutor::reportErr(const ErrorLogger::ErrorMessage &/*msg*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-19 13:52:32 +02:00
|
|
|
void ThreadExecutor::reportStatus(unsigned int /*fileindex*/, unsigned int /*filecount*/, long /*sizedone*/, long /*sizetotal*/)
|
2009-02-20 20:40:42 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2009-02-19 23:21:18 +01:00
|
|
|
#endif
|