2015-01-14 16:58:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-01-14 16:58:10 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-01-14 16:58:10 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-01-14 16:58:10 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "terminal.h"
|
|
|
|
|
|
Debugger: Make most views per-engine instead of singletons
This is a step towards properly supporting multiple debugger
sessions side-by-side.
The combined C++-and-QML engine has been removed, instead a
combined setup creates now two individual engines, under a single
DebuggerRunTool but mostly independent with no combined state
machine. This requires a few more clicks in some cases, but
makes it easier to direct e.g. interrupt requests to the
interesting engine.
Care has been taken to not change the UX of the single debugger
session use case if possible. The fat debug button operates
as-before in that case, i.e. switches to Interrupt if the
single active runconfiguration runs in the debugger etc.
Most views are made per-engine, running an engine creates
a new Perspective, which is destroyed when the run control dies.
The snapshot view remains global and becomes primary source
of information on a "current engine" that receives all menu
and otherwise global input.
There is a new global "Breakpoint Preset" view containing
all "static" breakpoint data. When an engine starts up it
"claims" breakpoint it believes it can handle, but operates
on a copy of the static data. The markers of the static
version are suppressed as long as an engine controls a
breakpoint (that inclusive all resolved locations), but are
re-instatet once the engine quits.
The old Breakpoint class that already contained this split
per-instance was split into a new Breakpoint and a
GlobalBreakpoint class, with a per-engine model for Breakpoints,
and a singleton model containing GlobalBreakpoints.
There is a new CppDebuggerEngine intermediate level serving as
base for C++ (or, rather, "compiled") binary debugging, i.e.
{Gdb,Lldb,Cdb}Engine, taking over bits of the current DebuggerEngine
base that are not applicable to non-binary debuggers.
Change-Id: I9994f4c188379b4aee0c4f379edd4759fbb0bd43
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: hjk <hjk@qt.io>
2018-07-31 12:30:48 +02:00
|
|
|
#include <projectexplorer/runconfiguration.h>
|
2015-01-14 16:58:10 +01:00
|
|
|
|
2017-09-27 08:22:02 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2015-01-14 16:58:10 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2017-09-27 08:22:02 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2015-01-14 16:58:10 +01:00
|
|
|
|
Debugger: Make most views per-engine instead of singletons
This is a step towards properly supporting multiple debugger
sessions side-by-side.
The combined C++-and-QML engine has been removed, instead a
combined setup creates now two individual engines, under a single
DebuggerRunTool but mostly independent with no combined state
machine. This requires a few more clicks in some cases, but
makes it easier to direct e.g. interrupt requests to the
interesting engine.
Care has been taken to not change the UX of the single debugger
session use case if possible. The fat debug button operates
as-before in that case, i.e. switches to Interrupt if the
single active runconfiguration runs in the debugger etc.
Most views are made per-engine, running an engine creates
a new Perspective, which is destroyed when the run control dies.
The snapshot view remains global and becomes primary source
of information on a "current engine" that receives all menu
and otherwise global input.
There is a new global "Breakpoint Preset" view containing
all "static" breakpoint data. When an engine starts up it
"claims" breakpoint it believes it can handle, but operates
on a copy of the static data. The markers of the static
version are suppressed as long as an engine controls a
breakpoint (that inclusive all resolved locations), but are
re-instatet once the engine quits.
The old Breakpoint class that already contained this split
per-instance was split into a new Breakpoint and a
GlobalBreakpoint class, with a per-engine model for Breakpoints,
and a singleton model containing GlobalBreakpoints.
There is a new CppDebuggerEngine intermediate level serving as
base for C++ (or, rather, "compiled") binary debugging, i.e.
{Gdb,Lldb,Cdb}Engine, taking over bits of the current DebuggerEngine
base that are not applicable to non-binary debuggers.
Change-Id: I9994f4c188379b4aee0c4f379edd4759fbb0bd43
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: hjk <hjk@qt.io>
2018-07-31 12:30:48 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QIODevice>
|
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
|
|
2015-01-14 16:58:10 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
|
# define DEBUGGER_USE_TERMINAL
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGGER_USE_TERMINAL
|
|
|
|
|
# include <errno.h>
|
|
|
|
|
# include <fcntl.h>
|
|
|
|
|
# include <stdlib.h>
|
|
|
|
|
# include <string.h>
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
|
# include <sys/stat.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-09-27 08:22:02 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2015-01-14 16:58:10 +01:00
|
|
|
namespace Debugger {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
static QString currentError()
|
|
|
|
|
{
|
|
|
|
|
int err = errno;
|
|
|
|
|
return QString::fromLatin1(strerror(err));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Terminal::Terminal(QObject *parent)
|
2018-02-01 10:59:24 +01:00
|
|
|
: QObject(parent)
|
2015-01-14 16:58:10 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Terminal::setup()
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUGGER_USE_TERMINAL
|
|
|
|
|
if (!qEnvironmentVariableIsSet("QTC_USE_PTY"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_masterFd = ::open("/dev/ptmx", O_RDWR);
|
|
|
|
|
if (m_masterFd < 0) {
|
|
|
|
|
error(tr("Terminal: Cannot open /dev/ptmx: %1").arg(currentError()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *sName = ptsname(m_masterFd);
|
|
|
|
|
if (!sName) {
|
|
|
|
|
error(tr("Terminal: ptsname failed: %1").arg(currentError()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_slaveName = sName;
|
|
|
|
|
|
|
|
|
|
struct stat s;
|
|
|
|
|
int r = ::stat(m_slaveName.constData(), &s);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
error(tr("Terminal: Error: %1").arg(currentError()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!S_ISCHR(s.st_mode)) {
|
2015-03-16 15:34:45 +01:00
|
|
|
error(tr("Terminal: Slave is no character device."));
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_masterReader = new QSocketNotifier(m_masterFd, QSocketNotifier::Read, this);
|
|
|
|
|
connect(m_masterReader, &QSocketNotifier::activated,
|
|
|
|
|
this, &Terminal::onSlaveReaderActivated);
|
|
|
|
|
|
|
|
|
|
r = grantpt(m_masterFd);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
error(tr("Terminal: grantpt failed: %1").arg(currentError()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = unlockpt(m_masterFd);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
error(tr("Terminal: unlock failed: %1").arg(currentError()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_isUsable = true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Terminal::isUsable() const
|
|
|
|
|
{
|
|
|
|
|
return m_isUsable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Terminal::write(const QByteArray &msg)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUGGER_USE_TERMINAL
|
|
|
|
|
return ::write(m_masterFd, msg.constData(), msg.size());
|
|
|
|
|
#else
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(msg)
|
2015-01-14 16:58:10 +01:00
|
|
|
return -1;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Terminal::sendInterrupt()
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUGGER_USE_TERMINAL
|
|
|
|
|
if (!m_isUsable)
|
|
|
|
|
return false;
|
|
|
|
|
ssize_t written = ::write(m_masterFd, "\003", 1);
|
|
|
|
|
return written == 1;
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Terminal::onSlaveReaderActivated(int fd)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUGGER_USE_TERMINAL
|
|
|
|
|
ssize_t available = 0;
|
|
|
|
|
int ret = ::ioctl(fd, FIONREAD, (char *) &available);
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QByteArray buffer(available, Qt::Uninitialized);
|
|
|
|
|
ssize_t got = ::read(fd, buffer.data(), available);
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (got < 0) {
|
|
|
|
|
error(tr("Terminal: Read failed: %1").arg(QString::fromLatin1(strerror(err))));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
buffer.resize(got);
|
|
|
|
|
if (got >= 0)
|
|
|
|
|
stdOutReady(QString::fromUtf8(buffer));
|
|
|
|
|
#else
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(fd)
|
2015-01-14 16:58:10 +01:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 11:53:18 +01:00
|
|
|
TerminalRunner::TerminalRunner(RunControl *runControl,
|
|
|
|
|
const std::function<Runnable()> &stubRunnable)
|
|
|
|
|
: RunWorker(runControl), m_stubRunnable(stubRunnable)
|
2017-09-27 08:22:02 +02:00
|
|
|
{
|
2018-08-21 08:28:27 +02:00
|
|
|
setId("TerminalRunner");
|
2017-09-27 08:22:02 +02:00
|
|
|
|
2022-01-19 14:02:20 +01:00
|
|
|
connect(&m_stubProc, &ConsoleProcess::errorOccurred,
|
2017-09-27 08:22:02 +02:00
|
|
|
this, &TerminalRunner::stubError);
|
2022-01-18 19:10:30 +01:00
|
|
|
connect(&m_stubProc, &ConsoleProcess::started,
|
2017-09-27 08:22:02 +02:00
|
|
|
this, &TerminalRunner::stubStarted);
|
2022-01-18 19:10:30 +01:00
|
|
|
connect(&m_stubProc, &ConsoleProcess::finished,
|
|
|
|
|
this, &TerminalRunner::reportDone);
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-25 12:38:44 +01:00
|
|
|
void TerminalRunner::kickoffProcess()
|
|
|
|
|
{
|
|
|
|
|
m_stubProc.kickoffProcess();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 08:57:08 +01:00
|
|
|
void TerminalRunner::interruptProcess()
|
|
|
|
|
{
|
|
|
|
|
m_stubProc.interruptProcess();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 16:55:28 +01:00
|
|
|
void TerminalRunner::setRunAsRoot(bool on)
|
|
|
|
|
{
|
|
|
|
|
m_runAsRoot = on;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 08:22:02 +02:00
|
|
|
void TerminalRunner::start()
|
|
|
|
|
{
|
2021-03-09 11:53:18 +01:00
|
|
|
QTC_ASSERT(m_stubRunnable, reportFailure({}); return);
|
|
|
|
|
Runnable stub = m_stubRunnable();
|
|
|
|
|
|
2021-02-15 16:55:28 +01:00
|
|
|
if (m_runAsRoot) {
|
|
|
|
|
m_stubProc.setRunAsRoot(true);
|
2021-03-09 11:53:18 +01:00
|
|
|
RunControl::provideAskPassEntry(stub.environment);
|
2021-02-15 16:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-09 11:53:18 +01:00
|
|
|
m_stubProc.setEnvironment(stub.environment);
|
|
|
|
|
m_stubProc.setWorkingDirectory(stub.workingDirectory);
|
2017-09-27 08:22:02 +02:00
|
|
|
|
|
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
2018-09-21 02:00:43 +03:00
|
|
|
m_stubProc.setMode(ConsoleProcess::Suspend);
|
2017-09-27 08:22:02 +02:00
|
|
|
} else {
|
|
|
|
|
m_stubProc.setMode(ConsoleProcess::Debug);
|
|
|
|
|
m_stubProc.setSettings(Core::ICore::settings());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error message for user is delivered via a signal.
|
2021-08-10 09:19:30 +02:00
|
|
|
m_stubProc.setCommand(stub.command);
|
2019-06-12 15:25:51 +02:00
|
|
|
m_stubProc.start();
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TerminalRunner::stop()
|
|
|
|
|
{
|
|
|
|
|
m_stubProc.stop();
|
|
|
|
|
reportStopped();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TerminalRunner::stubStarted()
|
|
|
|
|
{
|
2022-01-20 17:04:21 +01:00
|
|
|
m_applicationPid = m_stubProc.processId();
|
2017-09-27 08:22:02 +02:00
|
|
|
m_applicationMainThreadId = m_stubProc.applicationMainThreadID();
|
|
|
|
|
reportStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 14:02:20 +01:00
|
|
|
void TerminalRunner::stubError()
|
2017-09-27 08:22:02 +02:00
|
|
|
{
|
2022-01-19 14:02:20 +01:00
|
|
|
reportFailure(m_stubProc.errorString());
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-14 16:58:10 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|
|
|
|
|
|