debuggerdialogs.cpp 10.5 KB
Newer Older
hjk's avatar
hjk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/**************************************************************************
**
** 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"

32 33 34 35 36
#include "ui_attachcoredialog.h"
#include "ui_attachexternaldialog.h"
#include "ui_attachremotedialog.h"
#include "ui_startexternaldialog.h"

Friedemann Kleint's avatar
Friedemann Kleint committed
37 38 39 40
#ifdef Q_OS_WIN
#  include "dbgwinutils.h"
#endif

hjk's avatar
hjk committed
41 42 43 44 45 46 47
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtGui/QStandardItemModel>
#include <QtGui/QHeaderView>
#include <QtGui/QFileDialog>
#include <QtGui/QPushButton>
Friedemann Kleint's avatar
Friedemann Kleint committed
48 49
#include <QtGui/QProxyModel>
#include <QtGui/QSortFilterProxyModel>
hjk's avatar
hjk committed
50 51 52 53 54 55 56 57 58 59

using namespace Debugger::Internal;

///////////////////////////////////////////////////////////////////////
//
// AttachCoreDialog
//
///////////////////////////////////////////////////////////////////////

AttachCoreDialog::AttachCoreDialog(QWidget *parent)
60
  : QDialog(parent), m_ui(new Ui::AttachCoreDialog)
hjk's avatar
hjk committed
61
{
62 63 64 65 66 67 68 69 70
    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);
hjk's avatar
hjk committed
71

72 73 74 75 76 77 78 79
    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

AttachCoreDialog::~AttachCoreDialog()
{
    delete m_ui;
}
hjk's avatar
hjk committed
80

81 82 83
QString AttachCoreDialog::executableFile() const
{
    return m_ui->execFileName->path();
hjk's avatar
hjk committed
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99
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);
}
hjk's avatar
hjk committed
100 101 102

///////////////////////////////////////////////////////////////////////
//
Friedemann Kleint's avatar
Friedemann Kleint committed
103
// process model helpers
hjk's avatar
hjk committed
104 105 106
//
///////////////////////////////////////////////////////////////////////

Friedemann Kleint's avatar
Friedemann Kleint committed
107
static QStandardItemModel *createProcessModel(QObject *parent)
108
{
Friedemann Kleint's avatar
Friedemann Kleint committed
109 110 111 112 113 114 115
    QStandardItemModel *rc = new QStandardItemModel(parent);
    QStringList columns;
    columns << AttachExternalDialog::tr("Process ID")
            << AttachExternalDialog::tr("Name")
            << AttachExternalDialog::tr("State");
    rc->setHorizontalHeaderLabels(columns);
    return rc;
116 117
}

hjk's avatar
hjk committed
118 119 120 121 122 123 124 125
static bool isProcessName(const QString &procname)
{
    for (int i = 0; i != procname.size(); ++i)
        if (!procname.at(i).isDigit())
            return false;
    return true;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
126
bool operator<(const ProcData &p1, const ProcData &p2)
hjk's avatar
hjk committed
127
{
Friedemann Kleint's avatar
Friedemann Kleint committed
128
    return p1.name < p2.name;
hjk's avatar
hjk committed
129 130
}

Friedemann Kleint's avatar
Friedemann Kleint committed
131 132
// Determine UNIX processes by reading "/proc"
static QList<ProcData> unixProcessList()
hjk's avatar
hjk committed
133
{
Friedemann Kleint's avatar
Friedemann Kleint committed
134 135 136 137
    QList<ProcData> rc;
    const QStringList procnames = QDir(QLatin1String("/proc/")).entryList();
    if (procnames.isEmpty())
        return rc;
hjk's avatar
hjk committed
138 139 140 141

    foreach (const QString &procname, procnames) {
        if (!isProcessName(procname))
            continue;
Friedemann Kleint's avatar
Friedemann Kleint committed
142 143 144
        QString filename = QLatin1String("/proc/");
        filename += procname;
        filename += QLatin1String("/stat");
hjk's avatar
hjk committed
145 146
        QFile file(filename);
        file.open(QIODevice::ReadOnly);
Friedemann Kleint's avatar
Friedemann Kleint committed
147
        const QStringList data = QString::fromLocal8Bit(file.readAll()).split(' ');
hjk's avatar
hjk committed
148 149
        ProcData proc;
        proc.name = data.at(1);
Friedemann Kleint's avatar
Friedemann Kleint committed
150
        if (proc.name.startsWith(QLatin1Char('(')) && proc.name.endsWith(QLatin1Char(')')))
hjk's avatar
hjk committed
151 152 153
            proc.name = proc.name.mid(1, proc.name.size() - 2);
        proc.state = data.at(2);
        proc.ppid = data.at(3);
Friedemann Kleint's avatar
Friedemann Kleint committed
154
        rc.push_back(proc);
hjk's avatar
hjk committed
155
    }
Friedemann Kleint's avatar
Friedemann Kleint committed
156 157
    return rc;
}
hjk's avatar
hjk committed
158

Friedemann Kleint's avatar
Friedemann Kleint committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static void populateProcessModel(QStandardItemModel *model)
{
#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);

    QStandardItem *root  = model->invisibleRootItem();
    foreach(const ProcData &proc, processes) {
        QList<QStandardItem *> row;
        row.append(new QStandardItem(proc.ppid));
        row.append(new QStandardItem(proc.name));
        if (!proc.image.isEmpty())
            row.back()->setToolTip(proc.image);
        row.append(new QStandardItem(proc.state));
        root->appendRow(row);
    }
}
hjk's avatar
hjk committed
182

Friedemann Kleint's avatar
Friedemann Kleint committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
///////////////////////////////////////////////////////////////////////
//
// AttachExternalDialog
//
///////////////////////////////////////////////////////////////////////

AttachExternalDialog::AttachExternalDialog(QWidget *parent) :
        QDialog(parent),
        m_ui(new Ui::AttachExternalDialog),
        m_model(createProcessModel(this)),
        m_proxyModel(new QSortFilterProxyModel(this))
{
    m_ui->setupUi(this);
    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);

    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->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;
}

void AttachExternalDialog::rebuildProcessList()
{
    populateProcessModel(m_model);
228 229 230 231
    m_ui->procView->expandAll();
    m_ui->procView->resizeColumnToContents(0);
    m_ui->procView->resizeColumnToContents(1);
    m_ui->procView->sortByColumn(1, Qt::AscendingOrder);
hjk's avatar
hjk committed
232 233
}

Friedemann Kleint's avatar
Friedemann Kleint committed
234
void AttachExternalDialog::procSelected(const QModelIndex &proxyIndex)
hjk's avatar
hjk committed
235
{
Friedemann Kleint's avatar
Friedemann Kleint committed
236
    const QModelIndex index0 = m_proxyModel->mapToSource(proxyIndex);
hjk's avatar
hjk committed
237
    QModelIndex index = index0.sibling(index0.row(), 0);
Friedemann Kleint's avatar
Friedemann Kleint committed
238 239 240 241
    if (const QStandardItem *item = m_model->itemFromIndex(index)) {
        m_ui->pidLineEdit->setText(item->text());
        m_ui->buttonBox->button(QDialogButtonBox::Ok)->animateClick();
    }
hjk's avatar
hjk committed
242 243 244 245
}

int AttachExternalDialog::attachPID() const
{
246
    return m_ui->pidLineEdit->text().toInt();
hjk's avatar
hjk committed
247 248 249 250 251 252 253 254
}

///////////////////////////////////////////////////////////////////////
//
// AttachRemoteDialog
//
///////////////////////////////////////////////////////////////////////

Friedemann Kleint's avatar
Friedemann Kleint committed
255 256 257 258
AttachRemoteDialog::AttachRemoteDialog(QWidget *parent, const QString &pid) :
        QDialog(parent),
        m_ui(new Ui::AttachRemoteDialog),
        m_model(createProcessModel(this))
hjk's avatar
hjk committed
259
{
260 261
    m_ui->setupUi(this);
    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
hjk's avatar
hjk committed
262 263
    m_defaultPID = pid;

Friedemann Kleint's avatar
Friedemann Kleint committed
264
    m_ui->procView->setModel(m_model);
265
    m_ui->procView->setSortingEnabled(true);
hjk's avatar
hjk committed
266

267 268
    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
hjk's avatar
hjk committed
269

270 271
    connect(m_ui->procView, SIGNAL(activated(QModelIndex)),
        this, SLOT(procSelected(QModelIndex)));
hjk's avatar
hjk committed
272

273
    m_ui->pidLineEdit->setText(m_defaultPID);
hjk's avatar
hjk committed
274 275 276
    rebuildProcessList();
}

277 278 279 280 281
AttachRemoteDialog::~AttachRemoteDialog()
{
    delete m_ui;
}

hjk's avatar
hjk committed
282 283
void AttachRemoteDialog::rebuildProcessList()
{
Friedemann Kleint's avatar
Friedemann Kleint committed
284
    populateProcessModel(m_model);
285 286 287
    m_ui->procView->expandAll();
    m_ui->procView->resizeColumnToContents(0);
    m_ui->procView->resizeColumnToContents(1);
hjk's avatar
hjk committed
288 289 290 291 292 293 294 295
}

void AttachRemoteDialog::procSelected(const QModelIndex &index0)
{
    QModelIndex index = index0.sibling(index0.row(), 0);
    QStandardItem *item = m_model->itemFromIndex(index);
    if (!item)
        return;
296
    m_ui->pidLineEdit->setText(item->text());
hjk's avatar
hjk committed
297 298 299 300 301
    accept();
}

int AttachRemoteDialog::attachPID() const
{
302
    return m_ui->pidLineEdit->text().toInt();
hjk's avatar
hjk committed
303 304 305 306 307 308 309 310 311 312
}

///////////////////////////////////////////////////////////////////////
//
// StartExternalDialog
//
///////////////////////////////////////////////////////////////////////


StartExternalDialog::StartExternalDialog(QWidget *parent)
313
  : QDialog(parent), m_ui(new Ui::StartExternalDialog)
hjk's avatar
hjk committed
314
{
315 316 317 318
    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);
hjk's avatar
hjk committed
319 320 321 322 323

    //execLabel->setHidden(false);
    //execEdit->setHidden(false);
    //browseButton->setHidden(false);

324 325
    m_ui->execLabel->setText(tr("Executable:"));
    m_ui->argLabel->setText(tr("Arguments:"));
hjk's avatar
hjk committed
326

327 328 329 330 331 332 333
    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

StartExternalDialog::~StartExternalDialog()
{
    delete m_ui;
hjk's avatar
hjk committed
334 335 336 337
}

void StartExternalDialog::setExecutableFile(const QString &str)
{
338
    m_ui->execFile->setPath(str);
hjk's avatar
hjk committed
339 340 341 342
}

void StartExternalDialog::setExecutableArguments(const QString &str)
{
343
    m_ui->argsEdit->setText(str);
hjk's avatar
hjk committed
344 345 346 347
}

QString StartExternalDialog::executableFile() const
{
348
    return m_ui->execFile->path();
hjk's avatar
hjk committed
349 350 351 352
}

QString StartExternalDialog::executableArguments() const
{
353
    return m_ui->argsEdit->text();
hjk's avatar
hjk committed
354
}