Commit ea631133 authored by hjk's avatar hjk Committed by hjk

debugger: consolidate listing of local processes

Change-Id: I409fd58b3e9bb10de50e18e3658790c73e3fd521
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@nokia.com>
parent 8c25afbe
......@@ -56,7 +56,7 @@
#include "watchutils.h"
#include "gdb/gdbmi.h"
#include "shared/cdbsymbolpathlisteditor.h"
#include "shared/dbgwinutils.h"
#include "shared/hostutils.h"
#include <TranslationUnit.h>
......
......@@ -37,7 +37,7 @@
#include "bytearrayinputstream.h"
#include "gdb/gdbmi.h"
#include "disassemblerlines.h"
#include "shared/dbgwinutils.h"
#include "shared/hostutils.h"
#include <utils/qtcassert.h>
......
......@@ -34,7 +34,7 @@
#include "debuggerconstants.h"
#include "cdb/cdbengine.h"
#include "shared/dbgwinutils.h"
#include "shared/hostutils.h"
#include "ui_attachcoredialog.h"
#include "ui_attachexternaldialog.h"
......@@ -50,10 +50,8 @@
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QRegExp>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QCoreApplication>
#include <QtGui/QStandardItemModel>
#include <QtGui/QHeaderView>
......@@ -280,107 +278,6 @@ void AttachCoreDialog::changed()
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(isValid());
}
///////////////////////////////////////////////////////////////////////
//
// Process model helpers
//
///////////////////////////////////////////////////////////////////////
#ifndef 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 (!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(' ');
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;
}
#endif // Q_OS_WIN
static QList<ProcData> processList()
{
#ifdef Q_OS_WIN
return winProcessList();
#else
return unixProcessList();
#endif
}
///////////////////////////////////////////////////////////////////////
//
// AttachExternalDialog
......@@ -452,7 +349,7 @@ QPushButton *AttachExternalDialog::okButton() const
void AttachExternalDialog::rebuildProcessList()
{
m_model->populate(processList(), m_selfPid);
m_model->populate(hostProcessList(), m_selfPid);
m_ui->procView->expandAll();
m_ui->procView->resizeColumnToContents(0);
m_ui->procView->resizeColumnToContents(1);
......
......@@ -62,14 +62,6 @@ namespace Internal {
class ProcessListFilterModel;
struct ProcData
{
QString ppid;
QString name;
QString image;
QString state;
};
class AttachCoreDialog : public QDialog
{
Q_OBJECT
......
......@@ -36,6 +36,8 @@
#include "codadevice.h"
#include "codautils.h"
#include "gdbmi.h"
#include "hostutils.h"
#include "symbiandevicemanager.h"
#include "registerhandler.h"
......@@ -57,8 +59,6 @@
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include "dbgwinutils.h"
#ifndef Q_OS_WIN
# include <sys/types.h>
# include <unistd.h>
......
......@@ -67,7 +67,7 @@
#include "threadshandler.h"
#include "watchhandler.h"
#include "debuggersourcepathmappingwidget.h"
#include "dbgwinutils.h"
#include "hostutils.h"
#include "logwindow.h"
#include <coreplugin/icore.h>
......
......@@ -38,7 +38,7 @@
#include "procinterrupt.h"
#include "debuggerstringutils.h"
#include "debuggercore.h"
#include "dbgwinutils.h"
#include "shared/hostutils.h"
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
......
......@@ -30,39 +30,43 @@
**
**************************************************************************/
#include "winutils.h"
#include "dbgwinutils.h"
#include "debuggerdialogs.h"
#include "hostutils.h"
#include "breakpoint.h"
#include <utils/synchronousprocess.h>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QLibrary>
#include <QtCore/QProcess>
#include <QtCore/QString>
#include <QtCore/QTextStream>
// Enable Win API of XP SP1 and later
#ifdef Q_OS_WIN
# define _WIN32_WINNT 0x0502
# include <windows.h>
# include <utils/winutils.h>
# if !defined(PROCESS_SUSPEND_RESUME) // Check flag for MinGW
# define PROCESS_SUSPEND_RESUME (0x0800)
# endif // PROCESS_SUSPEND_RESUME
#endif // Q_OS_WIN
// Enable Win API of XP SP1 and later
#define _WIN32_WINNT 0x0502
#include <windows.h>
#include <utils/winutils.h>
#if !defined(PROCESS_SUSPEND_RESUME) // Check flag for MinGW
# define PROCESS_SUSPEND_RESUME (0x0800)
#endif // PROCESS_SUSPEND_RESUME
#include <tlhelp32.h>
#include <psapi.h>
#include <QtCore/QLibrary>
#endif // Q_OS_WIN
namespace Debugger {
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 inline BOOL queryFullProcessImageName(HANDLE h,
DWORD flags,
LPWSTR buffer,
DWORD *size)
static BOOL queryFullProcessImageName(HANDLE h, DWORD flags, LPWSTR buffer, DWORD *size)
{
// Resolve required symbols from the kernel32.dll
typedef BOOL (WINAPI *QueryFullProcessImageNameWProtoType)
......@@ -79,7 +83,7 @@ static inline BOOL queryFullProcessImageName(HANDLE h,
return (*queryFullProcessImageNameW)(h, flags, buffer, size);
}
static inline QString imageName(DWORD processId)
static QString imageName(DWORD processId)
{
QString rc;
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, processId);
......@@ -93,7 +97,7 @@ static inline QString imageName(DWORD processId)
return rc;
}
QList<ProcData> winProcessList()
static QList<ProcData> winProcessList()
{
QList<ProcData> rc;
......@@ -292,7 +296,7 @@ void formatWindowsException(unsigned long code, quint64 address,
bool isDebuggerWinException(long code)
{
return code ==EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP;
return code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP;
}
bool isFatalWinException(long code)
......@@ -313,5 +317,100 @@ 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(' ');
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
} // namespace Debugger
......@@ -30,24 +30,31 @@
**
**************************************************************************/
#ifndef DEBUGGER_DBG_WINUTILS_H
#define DEBUGGER_DBG_WINUTILS_H
#ifndef DEBUGGER_HOSTUTILS_H
#define DEBUGGER_HOSTUTILS_H
#include <QtCore/QList>
#ifdef Q_OS_WIN
#include <QtCore/QString>
QT_BEGIN_NAMESPACE
class QString;
class QTextStream;
QT_END_NAMESPACE
namespace Debugger {
namespace Internal {
struct ProcData; // debuggerdialogs, used by the process listing dialogs
struct ProcData
{
QString ppid;
QString name;
QString image;
QString state;
};
QList<ProcData> winProcessList();
QList<ProcData> hostProcessList();
#ifdef Q_OS_WIN
// Resume a suspended thread by id.
bool winResumeThread(unsigned long dwThreadId, QString *errorMessage);
......@@ -85,9 +92,9 @@ bool isFatalWinException(long code);
// Check for EXCEPTION_BREAKPOINT, EXCEPTION_SINGLE_STEP
bool isDebuggerWinException(long code);
#endif // defined(Q_OS_WIN)
} // namespace Internal
} // namespace Debugger
#endif // defined(Q_OS_WIN)
#endif // DEBUGGER_DBG_WINUTILS_H
#endif // DEBUGGER_HOSTUTILS_H
SOURCES += $$PWD/backtrace.cpp \
$$PWD/cdbsymbolpathlisteditor.cpp
$$PWD/cdbsymbolpathlisteditor.cpp \
$$PWD/hostutils.cpp
HEADERS += $$PWD/backtrace.h \
$$PWD/cdbsymbolpathlisteditor.h \
$$PWD/dbgwinutils.h
$$PWD/hostutils.h
INCLUDEPATH+=$$PWD
INCLUDEPATH += $$PWD
win32 {
SOURCES += $$PWD/peutils.cpp \
$$PWD/dbgwinutils.cpp
SOURCES += $$PWD/peutils.cpp
HEADERS += $$PWD/peutils.h
win32-msvc* {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment