2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2015-01-14 16:58:10 +01:00
|
|
|
|
|
|
|
|
#include "terminal.h"
|
|
|
|
|
|
2022-07-05 15:37:08 +02:00
|
|
|
#include "debuggertr.h"
|
|
|
|
|
|
2017-09-27 08:22:02 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2022-01-25 11:29:23 +01:00
|
|
|
#include <projectexplorer/runconfiguration.h>
|
|
|
|
|
|
2022-08-24 14:55:12 +02:00
|
|
|
#include <utils/environment.h>
|
2017-09-27 08:22:02 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2022-01-25 11:29:23 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/qtcprocess.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;
|
|
|
|
|
|
2022-07-05 15:37:08 +02:00
|
|
|
namespace Debugger::Internal {
|
2015-01-14 16:58:10 +01:00
|
|
|
|
|
|
|
|
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
|
2022-08-24 14:55:12 +02:00
|
|
|
if (!qtcEnvironmentVariableIsSet("QTC_USE_PTY"))
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_masterFd = ::open("/dev/ptmx", O_RDWR);
|
|
|
|
|
if (m_masterFd < 0) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: Cannot open /dev/ptmx: %1").arg(currentError()));
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *sName = ptsname(m_masterFd);
|
|
|
|
|
if (!sName) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: ptsname failed: %1").arg(currentError()));
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_slaveName = sName;
|
|
|
|
|
|
|
|
|
|
struct stat s;
|
|
|
|
|
int r = ::stat(m_slaveName.constData(), &s);
|
|
|
|
|
if (r != 0) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: Error: %1").arg(currentError()));
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!S_ISCHR(s.st_mode)) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::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) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: grantpt failed: %1").arg(currentError()));
|
2015-01-14 16:58:10 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = unlockpt(m_masterFd);
|
|
|
|
|
if (r != 0) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: unlock failed: %1").arg(currentError()));
|
2015-01-14 16:58:10 +01:00
|
|
|
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) {
|
2022-07-05 15:37:08 +02:00
|
|
|
error(Tr::tr("Terminal: Read failed: %1").arg(QString::fromLatin1(strerror(err))));
|
2015-01-14 16:58:10 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-02-25 12:38:44 +01:00
|
|
|
void TerminalRunner::kickoffProcess()
|
|
|
|
|
{
|
2022-01-25 11:29:23 +01:00
|
|
|
if (m_stubProc)
|
|
|
|
|
m_stubProc->kickoffProcess();
|
2021-02-25 12:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 08:30:19 +01:00
|
|
|
void TerminalRunner::interrupt()
|
2021-02-16 08:57:08 +01:00
|
|
|
{
|
2022-01-25 11:29:23 +01:00
|
|
|
if (m_stubProc)
|
2022-03-04 08:30:19 +01:00
|
|
|
m_stubProc->interrupt();
|
2021-02-16 08:57:08 +01:00
|
|
|
}
|
|
|
|
|
|
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);
|
2022-01-25 11:29:23 +01:00
|
|
|
QTC_ASSERT(!m_stubProc, reportFailure({}); return);
|
2021-03-09 11:53:18 +01:00
|
|
|
Runnable stub = m_stubRunnable();
|
|
|
|
|
|
2022-02-10 19:25:03 +01:00
|
|
|
m_stubProc = new QtcProcess(this);
|
|
|
|
|
m_stubProc->setTerminalMode(HostOsInfo::isWindowsHost()
|
2022-02-18 00:56:14 +01:00
|
|
|
? TerminalMode::Suspend : TerminalMode::Debug);
|
2022-02-10 19:25:03 +01:00
|
|
|
|
2022-01-25 11:29:23 +01:00
|
|
|
connect(m_stubProc, &QtcProcess::started,
|
|
|
|
|
this, &TerminalRunner::stubStarted);
|
2022-04-14 11:03:32 +02:00
|
|
|
connect(m_stubProc, &QtcProcess::done,
|
|
|
|
|
this, &TerminalRunner::stubDone);
|
2022-01-25 11:29:23 +01:00
|
|
|
|
|
|
|
|
m_stubProc->setEnvironment(stub.environment);
|
|
|
|
|
m_stubProc->setWorkingDirectory(stub.workingDirectory);
|
2017-09-27 08:22:02 +02:00
|
|
|
|
|
|
|
|
// Error message for user is delivered via a signal.
|
2022-02-01 14:51:23 +01:00
|
|
|
m_stubProc->setCommand(stub.command);
|
2022-01-25 11:29:23 +01:00
|
|
|
m_stubProc->start();
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TerminalRunner::stop()
|
|
|
|
|
{
|
2022-10-19 13:50:23 +02:00
|
|
|
if (m_stubProc && m_stubProc->isRunning())
|
2022-06-16 10:34:22 +02:00
|
|
|
m_stubProc->stop();
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TerminalRunner::stubStarted()
|
|
|
|
|
{
|
2022-01-25 11:29:23 +01:00
|
|
|
m_applicationPid = m_stubProc->processId();
|
2022-04-11 16:37:59 +02:00
|
|
|
m_applicationMainThreadId = m_stubProc->applicationMainThreadId();
|
2017-09-27 08:22:02 +02:00
|
|
|
reportStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 11:03:32 +02:00
|
|
|
void TerminalRunner::stubDone()
|
2017-09-27 08:22:02 +02:00
|
|
|
{
|
2022-04-14 11:03:32 +02:00
|
|
|
if (m_stubProc->error() != QProcess::UnknownError)
|
|
|
|
|
reportFailure(m_stubProc->errorString());
|
|
|
|
|
if (m_stubProc->error() != QProcess::FailedToStart)
|
|
|
|
|
reportDone();
|
2017-09-27 08:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-05 15:37:08 +02:00
|
|
|
} // Debugger::Internal
|
2015-01-14 16:58:10 +01:00
|
|
|
|