forked from qt-creator/qt-creator
debugger: cleanup hostProcessList
This is now using the lists in the device implementation. Change-Id: I112faf25d20a94315ec3b432d39ae6cf66762225 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@nokia.com>
This commit is contained in:
@@ -56,61 +56,6 @@ namespace Internal {
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
|
||||
// Resolve QueryFullProcessImageNameW out of kernel32.dll due
|
||||
// to incomplete MinGW import libs and it not being present
|
||||
// on Windows XP.
|
||||
static BOOL queryFullProcessImageName(HANDLE h, DWORD flags, LPWSTR buffer, DWORD *size)
|
||||
{
|
||||
// Resolve required symbols from the kernel32.dll
|
||||
typedef BOOL (WINAPI *QueryFullProcessImageNameWProtoType)
|
||||
(HANDLE, DWORD, LPWSTR, PDWORD);
|
||||
static QueryFullProcessImageNameWProtoType queryFullProcessImageNameW = 0;
|
||||
if (!queryFullProcessImageNameW) {
|
||||
QLibrary kernel32Lib(QLatin1String("kernel32.dll"), 0);
|
||||
if (kernel32Lib.isLoaded() || kernel32Lib.load())
|
||||
queryFullProcessImageNameW = (QueryFullProcessImageNameWProtoType)kernel32Lib.resolve("QueryFullProcessImageNameW");
|
||||
}
|
||||
if (!queryFullProcessImageNameW)
|
||||
return FALSE;
|
||||
// Read out process
|
||||
return (*queryFullProcessImageNameW)(h, flags, buffer, size);
|
||||
}
|
||||
|
||||
static QString imageName(DWORD processId)
|
||||
{
|
||||
QString rc;
|
||||
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, processId);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
return rc;
|
||||
WCHAR buffer[MAX_PATH];
|
||||
DWORD bufSize = MAX_PATH;
|
||||
if (queryFullProcessImageName(handle, 0, buffer, &bufSize))
|
||||
rc = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer));
|
||||
CloseHandle(handle);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static QList<ProcData> winProcessList()
|
||||
{
|
||||
QList<ProcData> rc;
|
||||
|
||||
PROCESSENTRY32 pe;
|
||||
pe.dwSize = sizeof(PROCESSENTRY32);
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (snapshot == INVALID_HANDLE_VALUE)
|
||||
return rc;
|
||||
|
||||
for (bool hasNext = Process32First(snapshot, &pe); hasNext; hasNext = Process32Next(snapshot, &pe)) {
|
||||
ProcData procData;
|
||||
procData.ppid = QString::number(pe.th32ProcessID);
|
||||
procData.name = QString::fromUtf16(reinterpret_cast<ushort*>(pe.szExeFile));
|
||||
procData.image = imageName(pe.th32ProcessID);
|
||||
rc.push_back(procData);
|
||||
}
|
||||
CloseHandle(snapshot);
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool winResumeThread(unsigned long dwThreadId, QString *errorMessage)
|
||||
{
|
||||
bool ok = false;
|
||||
@@ -284,99 +229,6 @@ bool isFatalWinException(long code)
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<ProcData> hostProcessList()
|
||||
{
|
||||
return winProcessList();
|
||||
}
|
||||
|
||||
#else // Q_OS_WIN
|
||||
|
||||
static bool isUnixProcessId(const QString &procname)
|
||||
{
|
||||
for (int i = 0; i != procname.size(); ++i)
|
||||
if (!procname.at(i).isDigit())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Determine UNIX processes by running ps
|
||||
static QList<ProcData> unixProcessListPS()
|
||||
{
|
||||
#ifdef Q_OS_MAC
|
||||
static const char formatC[] = "pid state command";
|
||||
#else
|
||||
static const char formatC[] = "pid,state,cmd";
|
||||
#endif
|
||||
QList<ProcData> rc;
|
||||
QProcess psProcess;
|
||||
QStringList args;
|
||||
args << QLatin1String("-e") << QLatin1String("-o") << QLatin1String(formatC);
|
||||
psProcess.start(QLatin1String("ps"), args);
|
||||
if (!psProcess.waitForStarted())
|
||||
return rc;
|
||||
QByteArray output;
|
||||
if (!Utils::SynchronousProcess::readDataFromProcess(psProcess, 30000, &output, 0, false))
|
||||
return rc;
|
||||
// Split "457 S+ /Users/foo.app"
|
||||
const QStringList lines = QString::fromLocal8Bit(output).split(QLatin1Char('\n'));
|
||||
const int lineCount = lines.size();
|
||||
const QChar blank = QLatin1Char(' ');
|
||||
for (int l = 1; l < lineCount; l++) { // Skip header
|
||||
const QString line = lines.at(l).simplified();
|
||||
const int pidSep = line.indexOf(blank);
|
||||
const int cmdSep = pidSep != -1 ? line.indexOf(blank, pidSep + 1) : -1;
|
||||
if (cmdSep > 0) {
|
||||
ProcData procData;
|
||||
procData.ppid = line.left(pidSep);
|
||||
procData.state = line.mid(pidSep + 1, cmdSep - pidSep - 1);
|
||||
procData.name = line.mid(cmdSep + 1);
|
||||
rc.push_back(procData);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Determine UNIX processes by reading "/proc". Default to ps if
|
||||
// it does not exist
|
||||
static QList<ProcData> unixProcessList()
|
||||
{
|
||||
const QDir procDir(QLatin1String("/proc/"));
|
||||
if (!procDir.exists())
|
||||
return unixProcessListPS();
|
||||
QList<ProcData> rc;
|
||||
const QStringList procIds = procDir.entryList();
|
||||
if (procIds.isEmpty())
|
||||
return rc;
|
||||
foreach (const QString &procId, procIds) {
|
||||
if (!isUnixProcessId(procId))
|
||||
continue;
|
||||
QString filename = QLatin1String("/proc/");
|
||||
filename += procId;
|
||||
filename += QLatin1String("/stat");
|
||||
QFile file(filename);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
continue; // process may have exited
|
||||
|
||||
const QStringList data = QString::fromLocal8Bit(file.readAll()).split(QLatin1Char(' '));
|
||||
ProcData proc;
|
||||
proc.ppid = procId;
|
||||
proc.name = data.at(1);
|
||||
if (proc.name.startsWith(QLatin1Char('(')) && proc.name.endsWith(QLatin1Char(')'))) {
|
||||
proc.name.truncate(proc.name.size() - 1);
|
||||
proc.name.remove(0, 1);
|
||||
}
|
||||
proc.state = data.at(2);
|
||||
// PPID is element 3
|
||||
rc.push_back(proc);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
QList<ProcData> hostProcessList()
|
||||
{
|
||||
return unixProcessList();
|
||||
}
|
||||
|
||||
#endif // Q_OS_WIN
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user