Files
qt-creator/src/plugins/debugger/debuggerdialogs.cpp

388 lines
11 KiB
C++
Raw Normal View History

2009-03-02 14:04:03 +01:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/
#include "debuggerdialogs.h"
#include "ui_attachcoredialog.h"
#include "ui_attachexternaldialog.h"
#include "ui_attachremotedialog.h"
#include "ui_startexternaldialog.h"
2009-03-04 16:00:43 +01:00
#ifdef Q_OS_WIN
# include "dbgwinutils.h"
#endif
2009-03-02 14:04:03 +01:00
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QCoreApplication>
2009-03-02 14:04:03 +01:00
#include <QtGui/QStandardItemModel>
#include <QtGui/QHeaderView>
#include <QtGui/QFileDialog>
#include <QtGui/QPushButton>
2009-03-04 16:00:43 +01:00
#include <QtGui/QProxyModel>
#include <QtGui/QSortFilterProxyModel>
2009-03-02 14:04:03 +01:00
using namespace Debugger::Internal;
///////////////////////////////////////////////////////////////////////
//
// AttachCoreDialog
//
///////////////////////////////////////////////////////////////////////
AttachCoreDialog::AttachCoreDialog(QWidget *parent)
: QDialog(parent), m_ui(new Ui::AttachCoreDialog)
2009-03-02 14:04:03 +01:00
{
m_ui->setupUi(this);
m_ui->execFileName->setExpectedKind(Core::Utils::PathChooser::File);
m_ui->execFileName->setPromptDialogTitle(tr("Select Executable"));
m_ui->coreFileName->setExpectedKind(Core::Utils::PathChooser::File);
m_ui->coreFileName->setPromptDialogTitle(tr("Select Executable"));
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
2009-03-02 14:04:03 +01:00
connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
AttachCoreDialog::~AttachCoreDialog()
{
delete m_ui;
}
2009-03-02 14:04:03 +01:00
QString AttachCoreDialog::executableFile() const
{
return m_ui->execFileName->path();
2009-03-02 14:04:03 +01:00
}
void AttachCoreDialog::setExecutableFile(const QString &fileName)
{
m_ui->execFileName->setPath(fileName);
}
QString AttachCoreDialog::coreFile() const
{
return m_ui->coreFileName->path();
}
void AttachCoreDialog::setCoreFile(const QString &fileName)
{
m_ui->coreFileName->setPath(fileName);
}
2009-03-02 14:04:03 +01:00
///////////////////////////////////////////////////////////////////////
//
2009-03-04 16:00:43 +01:00
// process model helpers
2009-03-02 14:04:03 +01:00
//
///////////////////////////////////////////////////////////////////////
2009-03-04 16:00:43 +01:00
static QStandardItemModel *createProcessModel(QObject *parent)
{
2009-03-04 16:00:43 +01:00
QStandardItemModel *rc = new QStandardItemModel(parent);
QStringList columns;
columns << AttachExternalDialog::tr("Process ID")
<< AttachExternalDialog::tr("Name")
<< AttachExternalDialog::tr("State");
rc->setHorizontalHeaderLabels(columns);
return rc;
}
static bool isUnixProcessId(const QString &procname)
2009-03-02 14:04:03 +01:00
{
for (int i = 0; i != procname.size(); ++i)
if (!procname.at(i).isDigit())
return false;
return true;
}
2009-03-05 09:46:36 +01:00
QT_BEGIN_NAMESPACE
2009-03-04 16:00:43 +01:00
bool operator<(const ProcData &p1, const ProcData &p2)
2009-03-02 14:04:03 +01:00
{
2009-03-04 16:00:43 +01:00
return p1.name < p2.name;
2009-03-02 14:04:03 +01:00
}
2009-03-05 09:46:36 +01:00
QT_END_NAMESPACE
2009-03-02 14:04:03 +01:00
2009-03-04 16:00:43 +01:00
// Determine UNIX processes by reading "/proc"
static QList<ProcData> unixProcessList()
2009-03-02 14:04:03 +01:00
{
2009-03-04 16:00:43 +01:00
QList<ProcData> rc;
const QStringList procIds = QDir(QLatin1String("/proc/")).entryList();
if (procIds.isEmpty())
2009-03-04 16:00:43 +01:00
return rc;
2009-03-02 14:04:03 +01:00
foreach (const QString &procId, procIds) {
if (!isUnixProcessId(procId))
2009-03-02 14:04:03 +01:00
continue;
2009-03-04 16:00:43 +01:00
QString filename = QLatin1String("/proc/");
filename += procId;
2009-03-04 16:00:43 +01:00
filename += QLatin1String("/stat");
2009-03-02 14:04:03 +01:00
QFile file(filename);
file.open(QIODevice::ReadOnly);
2009-03-04 16:00:43 +01:00
const QStringList data = QString::fromLocal8Bit(file.readAll()).split(' ');
2009-03-02 14:04:03 +01:00
ProcData proc;
proc.ppid = procId;
2009-03-02 14:04:03 +01:00
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);
}
2009-03-02 14:04:03 +01:00
proc.state = data.at(2);
// PPID is element 3
2009-03-04 16:00:43 +01:00
rc.push_back(proc);
2009-03-02 14:04:03 +01:00
}
2009-03-04 16:00:43 +01:00
return rc;
}
2009-03-02 14:04:03 +01:00
static inline QStandardItem *createStandardItem(const QString &text,
bool enabled)
{
QStandardItem *rc = new QStandardItem(text);
rc->setEnabled(enabled);
return rc;
}
// Populate a standard item model with a list
// of processes and gray out the excludePid.
static void populateProcessModel(QStandardItemModel *model,
const QString &excludePid = QString())
2009-03-04 16:00:43 +01:00
{
#ifdef Q_OS_WIN
QList<ProcData> processes = winProcessList();
#else
QList<ProcData> processes = unixProcessList();
#endif
qStableSort(processes);
if (const int rowCount = model->rowCount())
model->removeRows(0, rowCount);
2009-03-04 16:00:43 +01:00
QStandardItem *root = model->invisibleRootItem();
foreach(const ProcData &proc, processes) {
const bool enabled = proc.ppid != excludePid;
2009-03-04 16:00:43 +01:00
QList<QStandardItem *> row;
row.append(createStandardItem(proc.ppid, enabled));
row.append(createStandardItem(proc.name, enabled));
2009-03-04 16:00:43 +01:00
if (!proc.image.isEmpty())
row.back()->setToolTip(proc.image);
row.append(createStandardItem(proc.state, enabled));
2009-03-04 16:00:43 +01:00
root->appendRow(row);
}
}
2009-03-02 14:04:03 +01:00
2009-03-04 16:00:43 +01:00
///////////////////////////////////////////////////////////////////////
//
// AttachExternalDialog
//
///////////////////////////////////////////////////////////////////////
AttachExternalDialog::AttachExternalDialog(QWidget *parent) :
QDialog(parent),
m_selfPid(QString::number(QCoreApplication::applicationPid())),
2009-03-04 16:00:43 +01:00
m_ui(new Ui::AttachExternalDialog),
2009-03-05 09:46:36 +01:00
m_proxyModel(new QSortFilterProxyModel(this)),
m_model(createProcessModel(this))
2009-03-04 16:00:43 +01:00
{
m_ui->setupUi(this);
okButton()->setDefault(true);
okButton()->setEnabled(false);
2009-03-04 16:00:43 +01:00
m_proxyModel->setSourceModel(m_model);
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel->setFilterKeyColumn(1);
m_ui->procView->setModel(m_proxyModel);
m_ui->procView->setSortingEnabled(true);
connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QPushButton *refreshButton = new QPushButton(tr("Refresh"));
connect(refreshButton, SIGNAL(clicked()), this, SLOT(rebuildProcessList()));
m_ui->buttonBox->addButton(refreshButton, QDialogButtonBox::ActionRole);
connect(m_ui->procView, SIGNAL(activated(QModelIndex)),
this, SLOT(procSelected(QModelIndex)));
connect(m_ui->pidLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(pidChanged(QString)));
2009-03-04 16:00:43 +01:00
connect(m_ui->filterClearToolButton, SIGNAL(clicked()),
m_ui->filterLineEdit, SLOT(clear()));
connect(m_ui->filterLineEdit, SIGNAL(textChanged(QString)),
m_proxyModel, SLOT(setFilterFixedString(QString)));
rebuildProcessList();
}
AttachExternalDialog::~AttachExternalDialog()
{
delete m_ui;
}
QPushButton *AttachExternalDialog::okButton() const
{
return m_ui->buttonBox->button(QDialogButtonBox::Ok);
}
2009-03-04 16:00:43 +01:00
void AttachExternalDialog::rebuildProcessList()
{
populateProcessModel(m_model, m_selfPid);
m_ui->procView->expandAll();
m_ui->procView->resizeColumnToContents(0);
m_ui->procView->resizeColumnToContents(1);
2009-03-02 14:04:03 +01:00
}
2009-03-04 16:00:43 +01:00
void AttachExternalDialog::procSelected(const QModelIndex &proxyIndex)
2009-03-02 14:04:03 +01:00
{
2009-03-04 16:00:43 +01:00
const QModelIndex index0 = m_proxyModel->mapToSource(proxyIndex);
2009-03-02 14:04:03 +01:00
QModelIndex index = index0.sibling(index0.row(), 0);
2009-03-04 16:00:43 +01:00
if (const QStandardItem *item = m_model->itemFromIndex(index)) {
m_ui->pidLineEdit->setText(item->text());
if (okButton()->isEnabled())
okButton()->animateClick();
2009-03-04 16:00:43 +01:00
}
2009-03-02 14:04:03 +01:00
}
int AttachExternalDialog::attachPID() const
{
return m_ui->pidLineEdit->text().toInt();
2009-03-02 14:04:03 +01:00
}
void AttachExternalDialog::pidChanged(const QString &pid)
{
okButton()->setEnabled(!pid.isEmpty() && pid != m_selfPid);
}
2009-03-02 14:04:03 +01:00
///////////////////////////////////////////////////////////////////////
//
// AttachRemoteDialog
//
///////////////////////////////////////////////////////////////////////
2009-03-04 16:00:43 +01:00
AttachRemoteDialog::AttachRemoteDialog(QWidget *parent, const QString &pid) :
QDialog(parent),
m_ui(new Ui::AttachRemoteDialog),
m_model(createProcessModel(this))
2009-03-02 14:04:03 +01:00
{
m_ui->setupUi(this);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
2009-03-02 14:04:03 +01:00
m_defaultPID = pid;
2009-03-04 16:00:43 +01:00
m_ui->procView->setModel(m_model);
m_ui->procView->setSortingEnabled(true);
2009-03-02 14:04:03 +01:00
connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
2009-03-02 14:04:03 +01:00
connect(m_ui->procView, SIGNAL(activated(QModelIndex)),
this, SLOT(procSelected(QModelIndex)));
2009-03-02 14:04:03 +01:00
m_ui->pidLineEdit->setText(m_defaultPID);
2009-03-02 14:04:03 +01:00
rebuildProcessList();
}
AttachRemoteDialog::~AttachRemoteDialog()
{
delete m_ui;
}
2009-03-02 14:04:03 +01:00
void AttachRemoteDialog::rebuildProcessList()
{
2009-03-04 16:00:43 +01:00
populateProcessModel(m_model);
m_ui->procView->expandAll();
m_ui->procView->resizeColumnToContents(0);
m_ui->procView->resizeColumnToContents(1);
2009-03-02 14:04:03 +01:00
}
void AttachRemoteDialog::procSelected(const QModelIndex &index0)
{
QModelIndex index = index0.sibling(index0.row(), 0);
QStandardItem *item = m_model->itemFromIndex(index);
if (!item)
return;
m_ui->pidLineEdit->setText(item->text());
2009-03-02 14:04:03 +01:00
accept();
}
int AttachRemoteDialog::attachPID() const
{
return m_ui->pidLineEdit->text().toInt();
2009-03-02 14:04:03 +01:00
}
///////////////////////////////////////////////////////////////////////
//
// StartExternalDialog
//
///////////////////////////////////////////////////////////////////////
StartExternalDialog::StartExternalDialog(QWidget *parent)
: QDialog(parent), m_ui(new Ui::StartExternalDialog)
2009-03-02 14:04:03 +01:00
{
m_ui->setupUi(this);
m_ui->execFile->setExpectedKind(Core::Utils::PathChooser::File);
m_ui->execFile->setPromptDialogTitle(tr("Select Executable"));
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
2009-03-02 14:04:03 +01:00
//execLabel->setHidden(false);
//execEdit->setHidden(false);
//browseButton->setHidden(false);
m_ui->execLabel->setText(tr("Executable:"));
m_ui->argLabel->setText(tr("Arguments:"));
2009-03-02 14:04:03 +01:00
connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
StartExternalDialog::~StartExternalDialog()
{
delete m_ui;
2009-03-02 14:04:03 +01:00
}
void StartExternalDialog::setExecutableFile(const QString &str)
{
m_ui->execFile->setPath(str);
2009-03-02 14:04:03 +01:00
}
void StartExternalDialog::setExecutableArguments(const QString &str)
{
m_ui->argsEdit->setText(str);
2009-03-02 14:04:03 +01:00
}
QString StartExternalDialog::executableFile() const
{
return m_ui->execFile->path();
2009-03-02 14:04:03 +01:00
}
QString StartExternalDialog::executableArguments() const
{
return m_ui->argsEdit->text();
2009-03-02 14:04:03 +01:00
}