Commit 77ef011a authored by Peifeng Yu's avatar Peifeng Yu
Browse files

Move environment var settings into debugger specific code

parent 77698ceb
......@@ -44,7 +44,6 @@
#include <interfaces/idocument.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilaunchconfiguration.h>
#include <util/environmentgrouplist.h>
#include <util/processlinemaker.h>
#include <KConfigGroup>
......@@ -266,25 +265,12 @@ bool MIDebugSession::startDebugging(ILaunchConfiguration* cfg, IExecutePlugin* i
}
addCommand(EnvironmentCd, '"' + dir + '"');
// Set the environment variables
EnvironmentGroupList l(KSharedConfig::openConfig());
QString envgrp = iexec->environmentGroup(cfg);
if (envgrp.isEmpty()) {
qCWarning(DEBUGGERCOMMON) << i18n("No environment group specified, looks like a broken "
"configuration, please check run configuration '%1'. "
"Using default environment group.", cfg->name());
envgrp = l.defaultGroup();
}
for (const auto &envvar : l.createEnvironment(envgrp, {})) {
addCommand(GdbSet, "environment " + envvar);
}
// Set the run arguments
if (!arguments.isEmpty())
addCommand(ExecArguments, KShell::joinArgs(arguments));
// Do other debugger specific config options and actually start the inferior program
if (!execInferior(cfg, executable)) {
if (!execInferior(cfg, iexec, executable)) {
return false;
}
......
......@@ -275,7 +275,8 @@ protected:
/**
* Further config the debugger and start the inferior program (either local or remote).
*/
virtual bool execInferior(KDevelop::ILaunchConfiguration *cfg, const QString &executable) = 0;
virtual bool execInferior(KDevelop::ILaunchConfiguration *cfg, IExecutePlugin *iexec,
const QString &executable) = 0;
/**
* Manipulate debugger instance state
......
......@@ -35,9 +35,11 @@
#include <debugger/breakpoint/breakpoint.h>
#include <debugger/breakpoint/breakpointmodel.h>
#include <execute/iexecuteplugin.h>
#include <interfaces/icore.h>
#include <interfaces/idebugcontroller.h>
#include <interfaces/ilaunchconfiguration.h>
#include <util/environmentgrouplist.h>
#include <KLocalizedString>
#include <KMessageBox>
......@@ -123,7 +125,7 @@ void DebugSession::initializeDebugger()
qCDebug(DEBUGGERGDB) << "Initialized GDB";
}
void DebugSession::configure(ILaunchConfiguration *cfg)
void DebugSession::configure(ILaunchConfiguration *cfg, IExecutePlugin *iexec)
{
// Read Configuration values
KConfigGroup grp = cfg->config();
......@@ -161,15 +163,29 @@ void DebugSession::configure(ILaunchConfiguration *cfg)
addCommand(MI::GdbSet, "print asm-demangle off");
}
// Set the environment variables
EnvironmentGroupList l(KSharedConfig::openConfig());
QString envgrp = iexec->environmentGroup(cfg);
if (envgrp.isEmpty()) {
qCWarning(DEBUGGERCOMMON) << i18n("No environment group specified, looks like a broken "
"configuration, please check run configuration '%1'. "
"Using default environment group.", cfg->name());
envgrp = l.defaultGroup();
}
for (const auto &envvar : l.createEnvironment(envgrp, {})) {
addCommand(GdbSet, "environment " + envvar);
}
qCDebug(DEBUGGERGDB) << "Per inferior configuration done";
}
bool DebugSession::execInferior(ILaunchConfiguration *cfg, const QString &executable)
bool DebugSession::execInferior(ILaunchConfiguration *cfg, IExecutePlugin *iexec,
const QString &executable)
{
qCDebug(DEBUGGERGDB) << "Executing inferior";
// debugger specific config
configure(cfg);
configure(cfg, iexec);
KConfigGroup grp = cfg->config();
QUrl configGdbScript = grp.readEntry(KDevMI::remoteGdbConfigEntry, QUrl());
......
......@@ -75,9 +75,10 @@ public:
protected:
GdbDebugger *createDebugger() const override;
void initializeDebugger() override;
bool execInferior(KDevelop::ILaunchConfiguration *cfg, const QString &executable) override;
bool execInferior(KDevelop::ILaunchConfiguration *cfg, IExecutePlugin *iexec,
const QString &executable) override;
void configure(KDevelop::ILaunchConfiguration *cfg);
void configure(KDevelop::ILaunchConfiguration *cfg, IExecutePlugin *iexec);
private Q_SLOTS:
void handleVersion(const QStringList& s);
......
......@@ -5,6 +5,7 @@ add_debuggable_executable(debugeerecursion SRCS debugeerecursion.cpp)
add_debuggable_executable(debugeespace SRCS "debugee space.cpp")
add_debuggable_executable(debugeemultilocbreakpoint SRCS debugeemultilocbreakpoint.cpp)
add_debuggable_executable(debugeemultiplebreakpoint SRCS debugeemultiplebreakpoint.cpp)
add_debuggable_executable(debugeeechoenv SRCS debugeeechoenv.cpp)
add_debuggable_executable(debugeethreads SRCS debugeethreads.cpp)
target_link_libraries(debugeethreads Qt5::Core)
......
/*
* Copyright 2016 Aetf <aetf@unlimitedcodeworks.xyz>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <iostream>
#include <cstdlib>
int main() {
char * value = std::getenv("VariableA");
if (value) {
std::cout << value << std::endl;
} else {
std::cout << "Not found!" << std::endl;
}
value = std::getenv("VariableB");
if (value) {
std::cout << value << std::endl;
} else {
std::cout << "Not found!" << std::endl;
}
return 0;
}
......@@ -38,6 +38,7 @@
#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <shell/shellextension.h>
#include <util/environmentgrouplist.h>
#include <KIO/Global>
#include <KProcess>
......@@ -126,6 +127,16 @@ void GdbTest::init()
vc->watches()->clear();
}
class WritableEnvironmentGroupList : public KDevelop::EnvironmentGroupList
{
public:
explicit WritableEnvironmentGroupList(KConfig* config) : EnvironmentGroupList(config) {}
using EnvironmentGroupList::variables;
using EnvironmentGroupList::saveSettings;
using EnvironmentGroupList::removeGroup;
};
class TestLaunchConfiguration : public KDevelop::ILaunchConfiguration
{
public:
......@@ -147,6 +158,8 @@ public:
QString name() const override { return QString("Test-Launch"); }
KDevelop::IProject* project() const override { return 0; }
KDevelop::LaunchConfigurationType* type() const override { return 0; }
KConfig *rootConfig() { return c; }
private:
KConfigGroup cfg;
KConfig *c;
......@@ -289,6 +302,35 @@ void GdbTest::testStdOut()
}
}
void GdbTest::testEnvironmentSet()
{
TestDebugSession *session = new TestDebugSession;
TestLaunchConfiguration cfg(findExecutable("debugeeechoenv"));
cfg.config().writeEntry("EnvironmentGroup", "GdbTestGroup");
WritableEnvironmentGroupList envGroups(cfg.rootConfig());
envGroups.removeGroup("GdbTestGroup");
auto &envs = envGroups.variables("GdbTestGroup");
envs["VariableA"] = "-A' \" complex --value";
envs["VariableB"] = "-B' \" complex --value";
envGroups.saveSettings(cfg.rootConfig());
QSignalSpy outputSpy(session, &TestDebugSession::inferiorStdoutLines);
QVERIFY(session->startDebugging(&cfg, m_iface));
WAIT_FOR_STATE(session, KDevelop::IDebugSession::EndedState);
QCOMPARE(outputSpy.count(), 2);
QList<QVariant> arguments = outputSpy.takeFirst();
QCOMPARE(arguments.count(), 1);
QCOMPARE(arguments.first().toStringList(), QStringList() << "-A' \" complex --value");
arguments = outputSpy.takeFirst();
QCOMPARE(arguments.count(), 1);
QCOMPARE(arguments.first().toStringList(), QStringList() << "-B' \" complex --value");
}
void GdbTest::testBreakpoint()
{
TestDebugSession *session = new TestDebugSession;
......
......@@ -39,6 +39,7 @@ private Q_SLOTS:
void init();
void testStdOut();
void testEnvironmentSet();
void testBreakpoint();
void testDisableBreakpoint();
void testChangeLocationBreakpoint();
......
......@@ -31,9 +31,11 @@
#include <debugger/breakpoint/breakpoint.h>
#include <debugger/breakpoint/breakpointmodel.h>
#include <execute/iexecuteplugin.h>
#include <interfaces/icore.h>
#include <interfaces/idebugcontroller.h>
#include <interfaces/ilaunchconfiguration.h>
#include <util/environmentgrouplist.h>
#include <KConfigGroup>
#include <KLocalizedString>
......@@ -119,7 +121,7 @@ void DebugSession::initializeDebugger()
qCDebug(DEBUGGERLLDB) << "Initialized LLDB";
}
void DebugSession::configure(ILaunchConfiguration *cfg)
void DebugSession::configure(ILaunchConfiguration *cfg, IExecutePlugin *)
{
// Read Configuration values
KConfigGroup grp = cfg->config();
......@@ -155,12 +157,13 @@ void DebugSession::configure(ILaunchConfiguration *cfg)
qCDebug(DEBUGGERGDB) << "Per inferior configuration done";
}
bool DebugSession::execInferior(ILaunchConfiguration *cfg, const QString &executable)
bool DebugSession::execInferior(ILaunchConfiguration *cfg, IExecutePlugin *iexec,
const QString &executable)
{
qCDebug(DEBUGGERGDB) << "Executing inferior";
// debugger specific config
configure(cfg);
configure(cfg, iexec);
// config that can't be placed in configure()
KConfigGroup grp = cfg->config();
......@@ -218,6 +221,25 @@ bool DebugSession::execInferior(ILaunchConfiguration *cfg, const QString &execut
CmdHandlesError);
raiseEvent(connected_to_program);
// Set the environment variables has effect only after setting the executable
EnvironmentGroupList l(KSharedConfig::openConfig());
QString envgrp = iexec->environmentGroup(cfg);
if (envgrp.isEmpty()) {
qCWarning(DEBUGGERCOMMON) << i18n("No environment group specified, looks like a broken "
"configuration, please check run configuration '%1'. "
"Using default environment group.", cfg->name());
envgrp = l.defaultGroup();
}
const auto &const_l = l;
QStringList vars;
for (auto it = const_l.variables(envgrp).constBegin(),
ite = const_l.variables(envgrp).constEnd();
it != ite; ++it) {
vars.append(QStringLiteral("%0=%1").arg(it.key(), doubleQuoteArg(it.value())));
}
// actually using lldb command 'settings set target.env-vars' which accepts multiple values
addCommand(GdbSet, "environment " + vars.join(" "));
addCommand(new SentinelCommand([this]() {
breakpointController()->initSendBreakpoints();
addCommand(MI::ExecRun, QString(), CmdMaybeStartsRunning);
......
......@@ -65,9 +65,10 @@ public Q_SLOTS:
protected:
LldbDebugger *createDebugger() const override;
void initializeDebugger() override;
bool execInferior(KDevelop::ILaunchConfiguration *cfg, const QString &executable) override;
bool execInferior(KDevelop::ILaunchConfiguration *cfg, IExecutePlugin *iexec,
const QString &executable) override;
void configure(KDevelop::ILaunchConfiguration *cfg);
void configure(KDevelop::ILaunchConfiguration *cfg, IExecutePlugin *iexec);
void ensureDebuggerListening() override;
......
......@@ -29,5 +29,12 @@ int main() {
} else {
std::cout << "Not found!" << std::endl;
}
value = std::getenv("VariableB");
if (value) {
std::cout << value << std::endl;
} else {
std::cout << "Not found!" << std::endl;
}
return 0;
}
......@@ -102,11 +102,6 @@ public:
cfg.writeEntry("isExecutable", true);
cfg.writeEntry("Executable", executable);
cfg.writeEntry("Working Directory", workingDirectory);
WritableEnvironmentGroupList groupList(c);
groupList.removeGroup("LldbTestGroup");
groupList.variables("LldbTestGroup")["VariableA"] = "A good value";
groupList.saveSettings(c);
}
~TestLaunchConfiguration() override {
delete c;
......@@ -116,6 +111,8 @@ public:
QString name() const override { return QString("Test-Launch"); }
KDevelop::IProject* project() const override { return 0; }
KDevelop::LaunchConfigurationType* type() const override { return 0; }
KConfig *rootConfig() { return c; }
private:
KConfigGroup cfg;
KConfig *c;
......@@ -257,17 +254,29 @@ void LldbTest::testEnvironmentSet()
{
TestDebugSession *session = new TestDebugSession;
TestLaunchConfiguration cfg(findExecutable("lldb_debugeeechoenv"));
cfg.config().writeEntry("EnvironmentGroup", "LldbTestGroup");
WritableEnvironmentGroupList envGroups(cfg.rootConfig());
envGroups.removeGroup("LldbTestGroup");
auto &envs = envGroups.variables("LldbTestGroup");
envs["VariableA"] = "-A' \" complex --value";
envs["VariableB"] = "-B' \" complex --value";
envGroups.saveSettings(cfg.rootConfig());
QSignalSpy outputSpy(session, &TestDebugSession::inferiorStdoutLines);
QVERIFY(session->startDebugging(&cfg, m_iface));
WAIT_FOR_STATE(session, KDevelop::IDebugSession::EndedState);
QCOMPARE(outputSpy.count(), 1);
QCOMPARE(outputSpy.count(), 2);
QList<QVariant> arguments = outputSpy.takeFirst();
QCOMPARE(arguments.count(), 1);
QCOMPARE(arguments.first().toStringList(), QStringList() << "A good value");
QCOMPARE(arguments.first().toStringList(), QStringList() << "-A' \" complex --value");
arguments = outputSpy.takeFirst();
QCOMPARE(arguments.count(), 1);
QCOMPARE(arguments.first().toStringList(), QStringList() << "-B' \" complex --value");
}
void LldbTest::testBreakpoint()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment