debugger: consolidate listing of local processes

Change-Id: I409fd58b3e9bb10de50e18e3658790c73e3fd521
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
This commit is contained in:
hjk
2011-11-03 12:01:50 +01:00
committed by hjk
parent 8c25afbec4
commit ea63113364
10 changed files with 148 additions and 153 deletions

View File

@@ -56,7 +56,7 @@
#include "watchutils.h" #include "watchutils.h"
#include "gdb/gdbmi.h" #include "gdb/gdbmi.h"
#include "shared/cdbsymbolpathlisteditor.h" #include "shared/cdbsymbolpathlisteditor.h"
#include "shared/dbgwinutils.h" #include "shared/hostutils.h"
#include <TranslationUnit.h> #include <TranslationUnit.h>

View File

@@ -37,7 +37,7 @@
#include "bytearrayinputstream.h" #include "bytearrayinputstream.h"
#include "gdb/gdbmi.h" #include "gdb/gdbmi.h"
#include "disassemblerlines.h" #include "disassemblerlines.h"
#include "shared/dbgwinutils.h" #include "shared/hostutils.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>

View File

@@ -34,7 +34,7 @@
#include "debuggerconstants.h" #include "debuggerconstants.h"
#include "cdb/cdbengine.h" #include "cdb/cdbengine.h"
#include "shared/dbgwinutils.h" #include "shared/hostutils.h"
#include "ui_attachcoredialog.h" #include "ui_attachcoredialog.h"
#include "ui_attachexternaldialog.h" #include "ui_attachexternaldialog.h"
@@ -50,10 +50,8 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QRegExp> #include <QtCore/QRegExp>
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtGui/QStandardItemModel> #include <QtGui/QStandardItemModel>
#include <QtGui/QHeaderView> #include <QtGui/QHeaderView>
@@ -280,107 +278,6 @@ void AttachCoreDialog::changed()
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(isValid()); 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 // AttachExternalDialog
@@ -452,7 +349,7 @@ QPushButton *AttachExternalDialog::okButton() const
void AttachExternalDialog::rebuildProcessList() void AttachExternalDialog::rebuildProcessList()
{ {
m_model->populate(processList(), m_selfPid); m_model->populate(hostProcessList(), m_selfPid);
m_ui->procView->expandAll(); m_ui->procView->expandAll();
m_ui->procView->resizeColumnToContents(0); m_ui->procView->resizeColumnToContents(0);
m_ui->procView->resizeColumnToContents(1); m_ui->procView->resizeColumnToContents(1);

View File

@@ -62,14 +62,6 @@ namespace Internal {
class ProcessListFilterModel; class ProcessListFilterModel;
struct ProcData
{
QString ppid;
QString name;
QString image;
QString state;
};
class AttachCoreDialog : public QDialog class AttachCoreDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT

View File

@@ -36,6 +36,8 @@
#include "codadevice.h" #include "codadevice.h"
#include "codautils.h" #include "codautils.h"
#include "gdbmi.h" #include "gdbmi.h"
#include "hostutils.h"
#include "symbiandevicemanager.h" #include "symbiandevicemanager.h"
#include "registerhandler.h" #include "registerhandler.h"
@@ -57,8 +59,6 @@
#include <QtNetwork/QTcpServer> #include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket> #include <QtNetwork/QTcpSocket>
#include "dbgwinutils.h"
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
# include <sys/types.h> # include <sys/types.h>
# include <unistd.h> # include <unistd.h>

View File

@@ -67,7 +67,7 @@
#include "threadshandler.h" #include "threadshandler.h"
#include "watchhandler.h" #include "watchhandler.h"
#include "debuggersourcepathmappingwidget.h" #include "debuggersourcepathmappingwidget.h"
#include "dbgwinutils.h" #include "hostutils.h"
#include "logwindow.h" #include "logwindow.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>

View File

@@ -38,7 +38,7 @@
#include "procinterrupt.h" #include "procinterrupt.h"
#include "debuggerstringutils.h" #include "debuggerstringutils.h"
#include "debuggercore.h" #include "debuggercore.h"
#include "dbgwinutils.h" #include "shared/hostutils.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>

View File

@@ -30,39 +30,43 @@
** **
**************************************************************************/ **************************************************************************/
#include "winutils.h" #include "hostutils.h"
#include "dbgwinutils.h"
#include "debuggerdialogs.h"
#include "breakpoint.h" #include "breakpoint.h"
#include <utils/synchronousprocess.h>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QLibrary>
#include <QtCore/QProcess>
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QTextStream> #include <QtCore/QTextStream>
// Enable Win API of XP SP1 and later
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# define _WIN32_WINNT 0x0502
# include <windows.h> // Enable Win API of XP SP1 and later
# include <utils/winutils.h> #define _WIN32_WINNT 0x0502
# if !defined(PROCESS_SUSPEND_RESUME) // Check flag for MinGW #include <windows.h>
# define PROCESS_SUSPEND_RESUME (0x0800) #include <utils/winutils.h>
# endif // PROCESS_SUSPEND_RESUME #if !defined(PROCESS_SUSPEND_RESUME) // Check flag for MinGW
#endif // Q_OS_WIN # define PROCESS_SUSPEND_RESUME (0x0800)
#endif // PROCESS_SUSPEND_RESUME
#include <tlhelp32.h> #include <tlhelp32.h>
#include <psapi.h> #include <psapi.h>
#include <QtCore/QLibrary>
#endif // Q_OS_WIN
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
#ifdef Q_OS_WIN
// Resolve QueryFullProcessImageNameW out of kernel32.dll due // Resolve QueryFullProcessImageNameW out of kernel32.dll due
// to incomplete MinGW import libs and it not being present // to incomplete MinGW import libs and it not being present
// on Windows XP. // on Windows XP.
static inline BOOL queryFullProcessImageName(HANDLE h, static BOOL queryFullProcessImageName(HANDLE h, DWORD flags, LPWSTR buffer, DWORD *size)
DWORD flags,
LPWSTR buffer,
DWORD *size)
{ {
// Resolve required symbols from the kernel32.dll // Resolve required symbols from the kernel32.dll
typedef BOOL (WINAPI *QueryFullProcessImageNameWProtoType) typedef BOOL (WINAPI *QueryFullProcessImageNameWProtoType)
@@ -79,7 +83,7 @@ static inline BOOL queryFullProcessImageName(HANDLE h,
return (*queryFullProcessImageNameW)(h, flags, buffer, size); return (*queryFullProcessImageNameW)(h, flags, buffer, size);
} }
static inline QString imageName(DWORD processId) static QString imageName(DWORD processId)
{ {
QString rc; QString rc;
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, processId); HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, processId);
@@ -93,7 +97,7 @@ static inline QString imageName(DWORD processId)
return rc; return rc;
} }
QList<ProcData> winProcessList() static QList<ProcData> winProcessList()
{ {
QList<ProcData> rc; QList<ProcData> rc;
@@ -292,7 +296,7 @@ void formatWindowsException(unsigned long code, quint64 address,
bool isDebuggerWinException(long code) bool isDebuggerWinException(long code)
{ {
return code ==EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP; return code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP;
} }
bool isFatalWinException(long code) bool isFatalWinException(long code)
@@ -313,5 +317,100 @@ bool isFatalWinException(long code)
return true; 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 Internal
} // namespace Debugger } // namespace Debugger

View File

@@ -30,24 +30,31 @@
** **
**************************************************************************/ **************************************************************************/
#ifndef DEBUGGER_DBG_WINUTILS_H #ifndef DEBUGGER_HOSTUTILS_H
#define DEBUGGER_DBG_WINUTILS_H #define DEBUGGER_HOSTUTILS_H
#include <QtCore/QList> #include <QtCore/QList>
#include <QtCore/QString>
#ifdef Q_OS_WIN
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QString;
class QTextStream; class QTextStream;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Debugger { namespace Debugger {
namespace Internal { 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. // Resume a suspended thread by id.
bool winResumeThread(unsigned long dwThreadId, QString *errorMessage); bool winResumeThread(unsigned long dwThreadId, QString *errorMessage);
@@ -85,9 +92,9 @@ bool isFatalWinException(long code);
// Check for EXCEPTION_BREAKPOINT, EXCEPTION_SINGLE_STEP // Check for EXCEPTION_BREAKPOINT, EXCEPTION_SINGLE_STEP
bool isDebuggerWinException(long code); bool isDebuggerWinException(long code);
#endif // defined(Q_OS_WIN)
} // namespace Internal } // namespace Internal
} // namespace Debugger } // namespace Debugger
#endif // defined(Q_OS_WIN) #endif // DEBUGGER_HOSTUTILS_H
#endif // DEBUGGER_DBG_WINUTILS_H

View File

@@ -1,16 +1,16 @@
SOURCES += $$PWD/backtrace.cpp \ SOURCES += $$PWD/backtrace.cpp \
$$PWD/cdbsymbolpathlisteditor.cpp $$PWD/cdbsymbolpathlisteditor.cpp \
$$PWD/hostutils.cpp
HEADERS += $$PWD/backtrace.h \ HEADERS += $$PWD/backtrace.h \
$$PWD/cdbsymbolpathlisteditor.h \ $$PWD/cdbsymbolpathlisteditor.h \
$$PWD/dbgwinutils.h $$PWD/hostutils.h
INCLUDEPATH+=$$PWD INCLUDEPATH += $$PWD
win32 { win32 {
SOURCES += $$PWD/peutils.cpp \
$$PWD/dbgwinutils.cpp
SOURCES += $$PWD/peutils.cpp
HEADERS += $$PWD/peutils.h HEADERS += $$PWD/peutils.h
win32-msvc* { win32-msvc* {