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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "startgdbserverdialog.h"
#include "remotelinuxprocesslist.h"
#include "linuxdeviceconfiguration.h"
#include "remotelinuxusedportsgatherer.h"
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/profilechooser.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/devicesupport/devicemanagermodel.h>
#include <utils/pathchooser.h>
#include <utils/portlist.h>
#include <utils/qtcassert.h>
#include <ssh/sshconnection.h>
#include <ssh/sshremoteprocessrunner.h>
#include <QVariant>
#include <QSettings>
#include <QAction>
#include <QApplication>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QSpacerItem>
#include <QTableView>
#include <QTextBrowser>
#include <QVBoxLayout>
using namespace Core;
using namespace ProjectExplorer;
using namespace QSsh;
using namespace Utils;
const char LastProfile[] = "RemoteLinux/LastProfile";
const char LastDevice[] = "RemoteLinux/LastDevice";
const char LastProcessName[] = "RemoteLinux/LastProcessName";
//const char LastLocalExecutable[] = "RemoteLinux/LastLocalExecutable";
namespace RemoteLinux {
namespace Internal {
class StartGdbServerDialogPrivate
{
public:
StartGdbServerDialogPrivate(StartGdbServerDialog *q);
LinuxDeviceConfiguration::ConstPtr currentDevice() const
{
return deviceManagerModel->device(deviceComboBox->currentIndex())
.dynamicCast<const LinuxDeviceConfiguration>();
}
StartGdbServerDialog *q;
bool startServerOnly;
AbstractRemoteLinuxProcessList *processList;
QSortFilterProxyModel proxyModel;
DeviceManagerModel *deviceManagerModel;
QComboBox *deviceComboBox;
QLineEdit *processFilterLineEdit;
QTableView *tableView;
QPushButton *attachProcessButton;
QTextBrowser *textBrowser;
QPushButton *closeButton;
ProfileChooser *profileChooser;
RemoteLinuxUsedPortsGatherer gatherer;
SshRemoteProcessRunner runner;
QSettings *settings;
QString remoteCommandLine;
QString remoteExecutable;
};
StartGdbServerDialogPrivate::StartGdbServerDialogPrivate(StartGdbServerDialog *q)
: q(q), startServerOnly(true), processList(0)
{
settings = ICore::settings();
deviceComboBox = new QComboBox(q);
profileChooser = new ProfileChooser(q);
// sysrootPathChooser = new PathChooser(q);
// sysrootPathChooser->setExpectedKind(PathChooser::Directory);
// sysrootPathChooser->setPromptDialogTitle(StartGdbServerDialog::tr("Select Sysroot"));
// sysrootPathChooser->setPath(settings->value(LastSysroot).toString());
//executablePathChooser = new PathChooser(q);
//executablePathChooser->setExpectedKind(PathChooser::File);
//executablePathChooser->setPromptDialogTitle(StartGdbServerDialog::tr("Select Executable"));
//executablePathChooser->setPath(settings->value(LastLocalExecutable).toString());
processFilterLineEdit = new QLineEdit(q);
processFilterLineEdit->setText(settings->value(LastProcessName).toString());
processFilterLineEdit->selectAll();
tableView = new QTableView(q);
tableView->setShowGrid(false);
tableView->setSortingEnabled(true);
tableView->horizontalHeader()->setDefaultSectionSize(100);
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setVisible(false);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
attachProcessButton = new QPushButton(q);
attachProcessButton->setText(StartGdbServerDialog::tr("&Attach to Selected Process"));
closeButton = new QPushButton(q);
closeButton->setText(StartGdbServerDialog::tr("Close"));
textBrowser = new QTextBrowser(q);
textBrowser->setEnabled(false);
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow(StartGdbServerDialog::tr("Device:"), deviceComboBox);
formLayout->addRow(StartGdbServerDialog::tr("Target:"), profileChooser);
formLayout->addRow(StartGdbServerDialog::tr("&Filter entries:"), processFilterLineEdit);
QHBoxLayout *horizontalLayout2 = new QHBoxLayout();
horizontalLayout2->addStretch(1);
horizontalLayout2->addWidget(attachProcessButton);
horizontalLayout2->addWidget(closeButton);
formLayout->addRow(tableView);
formLayout->addRow(textBrowser);
formLayout->addRow(horizontalLayout2);
q->setLayout(formLayout);
}
} // namespace Internal
StartGdbServerDialog::StartGdbServerDialog(QWidget *parent) :
QDialog(parent),
d(new Internal::StartGdbServerDialogPrivate(this))
{
setWindowTitle(tr("List of Remote Processes"));
d->deviceManagerModel = new DeviceManagerModel(DeviceManager::instance(), this);
QObject::connect(d->closeButton, SIGNAL(clicked()), this, SLOT(reject()));
d->deviceComboBox->setModel(d->deviceManagerModel);
d->deviceComboBox->setCurrentIndex(d->settings->value(LastDevice).toInt());
connect(&d->gatherer, SIGNAL(error(QString)), SLOT(portGathererError(QString)));
connect(&d->gatherer, SIGNAL(portListReady()), SLOT(portListReady()));
if (d->deviceManagerModel->rowCount() == 0) {
d->tableView->setEnabled(false);
} else {
d->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
d->proxyModel.setDynamicSortFilter(true);
d->proxyModel.setFilterKeyColumn(-1);
d->tableView->setModel(&d->proxyModel);
connect(d->processFilterLineEdit, SIGNAL(textChanged(QString)),
&d->proxyModel, SLOT(setFilterRegExp(QString)));
connect(d->tableView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
SLOT(updateButtons()));
connect(d->profileChooser, SIGNAL(activated(int)),
SLOT(updateButtons()));
//connect(d->updateListButton, SIGNAL(clicked()),
// SLOT(updateProcessList()));
connect(d->attachProcessButton, SIGNAL(clicked()), SLOT(attachToProcess()));
connect(&d->proxyModel, SIGNAL(layoutChanged()),
SLOT(handleProcessListUpdated()));
connect(d->deviceComboBox, SIGNAL(currentIndexChanged(int)),
SLOT(attachToDevice(int)));
updateButtons();
attachToDevice(d->deviceComboBox->currentIndex());
}
}
StartGdbServerDialog::~StartGdbServerDialog()
{
delete d->processList;
delete d;
}
void StartGdbServerDialog::attachToDevice(int modelIndex)
{
LinuxDeviceConfiguration::ConstPtr device
= d->deviceManagerModel->device(modelIndex)
.dynamicCast<const LinuxDeviceConfiguration>();
// TODO: display error on non-matching device.
if (!device)
return;
delete d->processList;
d->processList = new GenericRemoteLinuxProcessList(device);
d->proxyModel.setSourceModel(d->processList);
connect(d->processList, SIGNAL(error(QString)),
SLOT(handleRemoteError(QString)));
connect(d->processList, SIGNAL(modelReset()),
SLOT(handleProcessListUpdated()));
connect(d->processList, SIGNAL(processKilled()),
SLOT(handleProcessKilled()), Qt::QueuedConnection);
updateProcessList();
}
void StartGdbServerDialog::handleRemoteError(const QString &errorMsg)
{
QMessageBox::critical(this, tr("Remote Error"), errorMsg);
updateButtons();
}
void StartGdbServerDialog::handleProcessListUpdated()
{
d->tableView->resizeRowsToContents();
updateButtons();
}
void StartGdbServerDialog::updateProcessList()
{
d->attachProcessButton->setEnabled(false);
d->processList->update();
d->proxyModel.setFilterRegExp(QString());
d->proxyModel.setFilterRegExp(d->processFilterLineEdit->text());
updateButtons();
}
void StartGdbServerDialog::attachToProcess()
{
const QModelIndexList &indexes =
d->tableView->selectionModel()->selectedIndexes();
if (indexes.empty())
return;
d->attachProcessButton->setEnabled(false);
LinuxDeviceConfiguration::ConstPtr device = d->currentDevice();
if (!device)
return;
PortList ports = device->freePorts();
const int port = d->gatherer.getNextFreePort(&ports);
const int row = d->proxyModel.mapToSource(indexes.first()).row();
QTC_ASSERT(row >= 0, return);
RemoteProcess process = d->processList->at(row);
d->remoteCommandLine = process.cmdLine;
d->remoteExecutable = process.exe;
if (port == -1) {
reportFailure();
return;
}
d->settings->setValue(LastProfile, d->profileChooser->currentProfileId().toString());
d->settings->setValue(LastDevice, d->deviceComboBox->currentIndex());
d->settings->setValue(LastProcessName, d->processFilterLineEdit->text());
startGdbServerOnPort(port, process.pid);
}
void StartGdbServerDialog::reportFailure()
{
QTC_ASSERT(false, /**/);
logMessage(tr("Process aborted"));
}
void StartGdbServerDialog::logMessage(const QString &line)
{
d->textBrowser->append(line);
}
void StartGdbServerDialog::handleProcessKilled()
{
updateProcessList();
}
void StartGdbServerDialog::updateButtons()
{
d->attachProcessButton->setEnabled(d->tableView->selectionModel()->hasSelection()
|| d->proxyModel.rowCount() == 1);
}
void StartGdbServerDialog::portGathererError(const QString &text)
{
logMessage(tr("Could not retrieve list of free ports:"));
logMessage(text);
reportFailure();
}
void StartGdbServerDialog::portListReady()
{
updateButtons();
}
void StartGdbServerDialog::startGdbServer()
{
d->startServerOnly = true;
if (exec() == QDialog::Rejected)
return;
d->gatherer.start(d->currentDevice());
}
void StartGdbServerDialog::attachToRemoteProcess()
{
d->startServerOnly = false;
if (exec() == QDialog::Rejected)
return;
d->gatherer.start(d->currentDevice());
}
void StartGdbServerDialog::handleConnectionError()
{
logMessage(tr("Connection error: %1").arg(d->runner.lastConnectionErrorString()));
emit processAborted();
}
void StartGdbServerDialog::handleProcessStarted()
{
logMessage(tr("Starting gdbserver..."));
}
void StartGdbServerDialog::handleProcessOutputAvailable()
{
logMessage(QString::fromUtf8(d->runner.readAllStandardOutput().trimmed()));
}
void StartGdbServerDialog::handleProcessErrorOutput()
{
const QByteArray ba = d->runner.readAllStandardError();
logMessage(QString::fromUtf8(ba.trimmed()));
// "Attached; pid = 16740"
// "Listening on port 10000"
foreach (const QByteArray &line, ba.split('\n')) {
if (line.startsWith("Listening on port")) {
const int port = line.mid(18).trimmed().toInt();
reportOpenPort(port);
}
}
}
void StartGdbServerDialog::reportOpenPort(int port)
{
logMessage(tr("Port %1 is now accessible.").arg(port));
LinuxDeviceConfiguration::ConstPtr device = d->currentDevice();
QString channel = QString("%1:%2").arg(device->sshParameters().host).arg(port);
logMessage(tr("Server started on %1").arg(channel));
const char *member = d->startServerOnly ? "gdbServerStarted" : "attachedToProcess";
QObject *ob = ExtensionSystem::PluginManager::getObjectByName("DebuggerCore");
if (ob) {
QMetaObject::invokeMethod(ob, member, Qt::QueuedConnection,
Q_ARG(QString, channel),
Q_ARG(QString, d->profileChooser->currentProfileId().toString()),
Q_ARG(QString, d->remoteCommandLine),
Q_ARG(QString, d->remoteExecutable));
}
close();
}
void StartGdbServerDialog::handleProcessClosed(int status)
{
logMessage(tr("Process gdbserver finished. Status: %1").arg(status));
}
void StartGdbServerDialog::startGdbServerOnPort(int port, int pid)
{
LinuxDeviceConfiguration::ConstPtr device = d->currentDevice();
connect(&d->runner, SIGNAL(connectionError()), SLOT(handleConnectionError()));
connect(&d->runner, SIGNAL(processStarted()), SLOT(handleProcessStarted()));
connect(&d->runner, SIGNAL(readyReadStandardOutput()), SLOT(handleProcessOutputAvailable()));
connect(&d->runner, SIGNAL(readyReadStandardError()), SLOT(handleProcessErrorOutput()));
connect(&d->runner, SIGNAL(processClosed(int)), SLOT(handleProcessClosed(int)));
QByteArray cmd = "/usr/bin/gdbserver --attach :"
+ QByteArray::number(port) + " " + QByteArray::number(pid);
logMessage(tr("Running command: %1").arg(QString::fromLatin1(cmd)));
d->runner.run(cmd, device->sshParameters());
}
} // namespace RemoteLinux
|