Commit d6636e41 authored by Lasse Holmstedt's avatar Lasse Holmstedt
Browse files

Debugger mode for simultaneous QML and C++ debugging (with plugins)

If you have plugins that extend qml, you probably want to debug them
too. This is now possible with the "Start debugging QML and C++
Simultaneously" debug menu entry. Just select a QML project which uses
c++ plugins and start debugging in this mode.
parent 3fe1617a
......@@ -229,8 +229,6 @@ QString ObjectPropertiesView::propertyBaseClass(const QDeclarativeDebugObjectRef
QmlJSEditor::ModelManagerInterface *modelManager = pluginManager->getObject<QmlJSEditor::ModelManagerInterface>();
QmlJS::Snapshot snapshot = modelManager->snapshot();
//qDebug() << property.name() << object.source().url().path();
QmlJS::Document::Ptr document = snapshot.document(object.source().url().path());
if (document.isNull()) {
......@@ -241,7 +239,6 @@ QString ObjectPropertiesView::propertyBaseClass(const QDeclarativeDebugObjectRef
contents = ins.readAll();
inFile.close();
}
//qDebug() << contents;
document = QmlJS::Document::create(object.source().url().path());
document->setSource(contents);
......@@ -301,7 +298,6 @@ void ObjectPropertiesView::setObject(const QDeclarativeDebugObjectReference &obj
baseClassItems.insert(baseClassName, baseClassItem);
qDebug() << "Baseclass" << baseClassName;
}
currentParentItem = baseClassItems.value(baseClassName);
item = new PropertiesViewItem(currentParentItem);
......
......@@ -184,8 +184,6 @@ void ObjectTree::buildTree(const QDeclarativeDebugObjectReference &obj, QTreeWid
if (!parent)
clear();
qDebug() << obj.className();
if (obj.contextDebugId() < 0 && !m_showUninspectableItems)
return;
......
......@@ -162,7 +162,8 @@ QmlInspector::QmlInspector(QObject *parent)
m_inspectorOutputDock(0),
m_connectionTimer(new QTimer(this)),
m_connectionAttempts(0),
m_simultaneousCppAndQmlDebugMode(false)
m_simultaneousCppAndQmlDebugMode(false),
m_debugMode(StandaloneMode)
{
m_instance = this;
m_watchTableModel = new Internal::WatchTableModel(0, this);
......@@ -174,6 +175,8 @@ QmlInspector::QmlInspector(QObject *parent)
// m_frameRateWidget = new Internal::CanvasFrameRate;
// m_frameRateWidget->setObjectName(QLatin1String("QmlDebugFrameRate"));
Debugger::DebuggerManager *debugManager = Debugger::DebuggerManager::instance();
connect(debugManager, SIGNAL(stateChanged(int)), this, SLOT(debuggerStateChanged(int)));
m_editablePropertyTypes = QStringList() << "qreal" << "bool" << "QString"
<< "int" << "QVariant" << "QUrl" << "QColor";
......@@ -202,6 +205,7 @@ void QmlInspector::pollInspector()
tr("Failed to connect to debugger"),
tr("Could not connect to debugger server.") );
}
updateMenuActions();
}
bool QmlInspector::setDebugConfigurationDataFromProject(ProjectExplorer::Project *projectToDebug)
......@@ -227,11 +231,6 @@ bool QmlInspector::setDebugConfigurationDataFromProject(ProjectExplorer::Project
void QmlInspector::startQmlProjectDebugger()
{
m_simultaneousCppAndQmlDebugMode = false;
startConnectionTimer();
}
void QmlInspector::startConnectionTimer()
{
m_connectionTimer->start();
}
......@@ -269,6 +268,7 @@ bool QmlInspector::connectToViewer()
void QmlInspector::disconnectFromViewer()
{
m_conn->disconnectFromHost();
updateMenuActions();
}
void QmlInspector::connectionStateChanged()
......@@ -285,6 +285,8 @@ void QmlInspector::connectionStateChanged()
resetViews();
updateMenuActions();
break;
}
case QAbstractSocket::HostLookupState:
......@@ -445,14 +447,14 @@ void QmlInspector::createDockWidgets()
core->addContextObject(m_propWatcherContext);
core->addContextObject(m_context);
QAction *attachToExternalAction = new QAction(this);
attachToExternalAction->setText(tr("Start Debugging C++ and QML Simultaneously..."));
connect(attachToExternalAction, SIGNAL(triggered()),
this, SLOT(attachToExternalQmlApplication()));
m_simultaneousDebugAction = new QAction(this);
m_simultaneousDebugAction->setText(tr("Start Debugging C++ and QML Simultaneously..."));
connect(m_simultaneousDebugAction, SIGNAL(triggered()),
this, SLOT(simultaneouslyDebugQmlCppApplication()));
Core::ActionManager *am = core->actionManager();
Core::ActionContainer *mstart = am->actionContainer(ProjectExplorer::Constants::M_DEBUG_STARTDEBUGGING);
Core::Command *cmd = am->registerAction(attachToExternalAction, Constants::M_ATTACH_TO_EXTERNAL,
Core::Command *cmd = am->registerAction(m_simultaneousDebugAction, Constants::M_DEBUG_SIMULTANEOUSLY,
QList<int>() << m_context->context());
cmd->setAttribute(Core::Command::CA_Hide);
mstart->addAction(cmd, Core::Constants::G_DEFAULT_ONE);
......@@ -471,128 +473,222 @@ void QmlInspector::createDockWidgets()
SLOT(setContextHelpId(QString)));
}
void QmlInspector::attachToExternalQmlApplication()
void QmlInspector::simultaneouslyDebugQmlCppApplication()
{
QString errorMessage;
ProjectExplorer::ProjectExplorerPlugin *pex = ProjectExplorer::ProjectExplorerPlugin::instance();
ProjectExplorer::Project *project = pex->startupProject();
if (!project)
errorMessage = QString(tr("No project was found."));
else {
if (project->id() == "QmlProjectManager.QmlProject")
errorMessage = attachToQmlViewerAsExternalApp(project);
else {
errorMessage = attachToExternalCppAppWithQml(project);
}
}
if (!errorMessage.isEmpty())
QMessageBox::warning(Core::ICore::instance()->mainWindow(), "Failed to debug C++ and QML", errorMessage);
}
QString QmlInspector::attachToQmlViewerAsExternalApp(ProjectExplorer::Project *project)
{
m_debugMode = QmlProjectWithCppPlugins;
QmlProjectManager::QmlProjectRunConfiguration* runConfig =
qobject_cast<QmlProjectManager::QmlProjectRunConfiguration*>(project->activeTarget()->activeRunConfiguration());
if (!runConfig)
return QString(tr("No run configurations were found for the project '%1'.").arg(project->displayName()));
Internal::StartExternalQmlDialog dlg(Debugger::DebuggerUISwitcher::instance()->mainWindow());
QString importPathArgument = "-I";
QString execArgs;
if (runConfig->viewerArguments().contains(importPathArgument))
execArgs = runConfig->viewerArguments().join(" ");
else {
QFileInfo qmlFileInfo(runConfig->viewerArguments().last());
importPathArgument.append(" " + qmlFileInfo.absolutePath() + " ");
execArgs = importPathArgument + runConfig->viewerArguments().join(" ");
}
dlg.setPort(runConfig->debugServerPort());
dlg.setDebuggerUrl(runConfig->debugServerAddress());
dlg.setProjectDisplayName(project->displayName());
dlg.setDebugMode(Internal::StartExternalQmlDialog::QmlProjectWithCppPlugins);
dlg.setQmlViewerArguments(execArgs);
dlg.setQmlViewerPath(runConfig->viewerPath());
if (dlg.exec() != QDialog::Accepted)
return QString();
m_runConfigurationDebugData.serverAddress = dlg.debuggerUrl();
m_runConfigurationDebugData.serverPort = dlg.port();
m_settings.setExternalPort(dlg.port());
m_settings.setExternalUrl(dlg.debuggerUrl());
ProjectExplorer::Environment customEnv = ProjectExplorer::Environment::systemEnvironment(); // empty env by default
customEnv.set(QmlProjectManager::Constants::E_QML_DEBUG_SERVER_PORT, QString::number(m_settings.externalPort()));
Debugger::Internal::DebuggerRunControl *debuggableRunControl = createDebuggerRunControl(runConfig,
dlg.qmlViewerPath(), dlg.qmlViewerArguments());
return executeDebuggerRunControl(debuggableRunControl, &customEnv);
}
QString QmlInspector::attachToExternalCppAppWithQml(ProjectExplorer::Project *project)
{
m_debugMode = CppProjectWithQmlEngines;
ProjectExplorer::LocalApplicationRunConfiguration* runConfig =
qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration*>(project->activeTarget()->activeRunConfiguration());
QString errorMessage;
if (!project)
errorMessage = QString(tr("No project was found."));
else if (!project->activeTarget() || !project->activeTarget()->activeRunConfiguration())
errorMessage = QString(tr("No run configurations were found for the project '%1'.").arg(project->displayName()));
if (!project->activeTarget() || !project->activeTarget()->activeRunConfiguration())
return QString(tr("No run configurations were found for the project '%1'.").arg(project->displayName()));
else if (!runConfig)
errorMessage = QString(tr("No valid run configuration was found for the project %1. "
return QString(tr("No valid run configuration was found for the project %1. "
"Only locally runnable configurations are supported.\n"
"Please check your project settings.").arg(project->displayName()));
Internal::StartExternalQmlDialog dlg(Debugger::DebuggerUISwitcher::instance()->mainWindow());
if (errorMessage.isEmpty()) {
dlg.setPort(m_settings.externalPort());
dlg.setDebuggerUrl(m_settings.externalUrl());
dlg.setProjectDisplayName(project->displayName());
dlg.setDebugMode(Internal::StartExternalQmlDialog::CppProjectWithQmlEngine);
if (dlg.exec() != QDialog::Accepted)
return QString();
Internal::StartExternalQmlDialog dlg(Debugger::DebuggerUISwitcher::instance()->mainWindow());
m_runConfigurationDebugData.serverAddress = dlg.debuggerUrl();
m_runConfigurationDebugData.serverPort = dlg.port();
m_settings.setExternalPort(dlg.port());
m_settings.setExternalUrl(dlg.debuggerUrl());
dlg.setPort(m_settings.externalPort());
dlg.setDebuggerUrl(m_settings.externalUrl());
dlg.setProjectDisplayName(project->displayName());
if (dlg.exec() != QDialog::Accepted)
return;
ProjectExplorer::Environment customEnv = runConfig->environment();
customEnv.set(QmlProjectManager::Constants::E_QML_DEBUG_SERVER_PORT, QString::number(m_settings.externalPort()));
Debugger::Internal::DebuggerRunControl *debuggableRunControl = createDebuggerRunControl(runConfig);
return executeDebuggerRunControl(debuggableRunControl, &customEnv);
}
m_runConfigurationDebugData.serverAddress = dlg.debuggerUrl();
m_runConfigurationDebugData.serverPort = dlg.port();
m_settings.setExternalPort(dlg.port());
m_settings.setExternalUrl(dlg.debuggerUrl());
QString QmlInspector::executeDebuggerRunControl(Debugger::Internal::DebuggerRunControl *debuggableRunControl, ProjectExplorer::Environment *environment)
{
ProjectExplorer::ProjectExplorerPlugin *pex = ProjectExplorer::ProjectExplorerPlugin::instance();
ProjectExplorer::Environment customEnv = runConfig->environment();
customEnv.set(QmlProjectManager::Constants::E_QML_DEBUG_SERVER_PORT, QString::number(m_settings.externalPort()));
// to make sure we have a valid, debuggable run control, find the correct factory for it
if (debuggableRunControl) {
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
const QList<Debugger::Internal::DebuggerRunControlFactory *> factories = pm->getObjects<Debugger::Internal::DebuggerRunControlFactory>();
// to make sure we have a valid, debuggable run control, find the correct factory for it
if (factories.length() && factories.first()->canRun(runConfig, ProjectExplorer::Constants::DEBUGMODE)) {
// modify the env
debuggableRunControl->setCustomEnvironment(*environment);
pex->startRunControl(debuggableRunControl, ProjectExplorer::Constants::DEBUGMODE);
m_simultaneousCppAndQmlDebugMode = true;
ProjectExplorer::RunControl *runControl = factories.first()->create(runConfig, ProjectExplorer::Constants::DEBUGMODE);
Debugger::Internal::DebuggerRunControl *debuggableRunControl = qobject_cast<Debugger::Internal::DebuggerRunControl *>(runControl);
// modify the env
debuggableRunControl->setCustomEnvironment(customEnv);
return QString();
}
return QString(tr("A valid run control was not registered in Qt Creator for this project run configuration."));;
}
Debugger::DebuggerManager *debugManager = Debugger::DebuggerManager::instance();
connect(debugManager, SIGNAL(stateChanged(int)), this, SLOT(debuggerStateChanged(int)));
Debugger::Internal::DebuggerRunControl *QmlInspector::createDebuggerRunControl(ProjectExplorer::RunConfiguration *runConfig,
const QString &executableFile, const QString &executableArguments)
{
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
const QList<Debugger::Internal::DebuggerRunControlFactory *> factories = pm->getObjects<Debugger::Internal::DebuggerRunControlFactory>();
ProjectExplorer::RunControl *runControl = 0;
if (m_debugMode == QmlProjectWithCppPlugins) {
pex->startRunControl(debuggableRunControl, ProjectExplorer::Constants::DEBUGMODE);
m_simultaneousCppAndQmlDebugMode = true;
const Debugger::DebuggerStartParametersPtr sp(new Debugger::DebuggerStartParameters);
sp->startMode = Debugger::StartExternal;
sp->executable = executableFile;
sp->processArgs = executableArguments.split(QLatin1Char(' '));
} else {
errorMessage = QString(tr("A valid run control was not registered in Qt Creator for this project run configuration."));
runControl = factories.first()->create(sp);
return qobject_cast<Debugger::Internal::DebuggerRunControl *>(runControl);
} else if (m_debugMode == CppProjectWithQmlEngines) {
if (factories.length() && factories.first()->canRun(runConfig, ProjectExplorer::Constants::DEBUGMODE)) {
runControl = factories.first()->create(runConfig, ProjectExplorer::Constants::DEBUGMODE);
return qobject_cast<Debugger::Internal::DebuggerRunControl *>(runControl);
}
}
return 0;
}
}
void QmlInspector::updateMenuActions()
{
if (!errorMessage.isEmpty())
QMessageBox::warning(Core::ICore::instance()->mainWindow(), "Failed to debug C++ and QML", errorMessage);
bool enabled = true;
if (m_simultaneousCppAndQmlDebugMode)
enabled = (m_cppDebuggerState == Debugger::DebuggerNotReady && (!m_conn || m_conn->state() == QAbstractSocket::UnconnectedState));
else
enabled = (!m_conn || m_conn->state() == QAbstractSocket::UnconnectedState);
m_simultaneousDebugAction->setEnabled(enabled);
}
void QmlInspector::debuggerStateChanged(int newState)
{
if (!m_simultaneousCppAndQmlDebugMode)
return;
if (m_simultaneousCppAndQmlDebugMode) {
switch(newState) {
case Debugger::EngineStarting:
{
m_connectionInitialized = false;
switch(newState) {
case Debugger::EngineStarting:
{
m_connectionInitialized = false;
break;
}
case Debugger::AdapterStartFailed:
case Debugger::InferiorStartFailed:
emit statusMessage(QString(tr("Debugging failed: could not start C++ debugger.")));
break;
}
case Debugger::AdapterStartFailed:
case Debugger::InferiorStartFailed:
disconnect(Debugger::DebuggerManager::instance(), SIGNAL(stateChanged(int)), this, SLOT(debuggerStateChanged(int)));
emit statusMessage(QString(tr("Debugging failed: could not start C++ debugger.")));
break;
case Debugger::InferiorRunningRequested:
{
if (m_cppDebuggerState == Debugger::InferiorStopped) {
// re-enable UI again
case Debugger::InferiorRunningRequested:
{
if (m_cppDebuggerState == Debugger::InferiorStopped) {
// re-enable UI again
m_objectTreeWidget->setEnabled(true);
m_propertiesWidget->setEnabled(true);
m_expressionWidget->setEnabled(true);
}
break;
}
case Debugger::InferiorRunning:
{
if (!m_connectionInitialized) {
m_connectionInitialized = true;
m_connectionTimer->setInterval(ConnectionAttemptSimultaneousInterval);
m_connectionTimer->start();
}
break;
}
case Debugger::InferiorStopped:
{
m_objectTreeWidget->setEnabled(false);
m_propertiesWidget->setEnabled(false);
m_expressionWidget->setEnabled(false);
break;
}
case Debugger::EngineShuttingDown:
{
m_connectionInitialized = false;
// here it's safe to enable the debugger windows again -
// disabled ones look ugly.
m_objectTreeWidget->setEnabled(true);
m_propertiesWidget->setEnabled(true);
m_expressionWidget->setEnabled(true);
m_simultaneousCppAndQmlDebugMode = false;
break;
}
default:
break;
}
case Debugger::InferiorRunning:
{
if (!m_connectionInitialized) {
m_connectionInitialized = true;
m_connectionTimer->setInterval(ConnectionAttemptSimultaneousInterval);
startConnectionTimer();
}
break;
}
case Debugger::InferiorStopped:
{
m_objectTreeWidget->setEnabled(false);
m_propertiesWidget->setEnabled(false);
m_expressionWidget->setEnabled(false);
break;
}
case Debugger::EngineShuttingDown:
{
m_connectionInitialized = false;
// here it's safe to enable the debugger windows again -
// disabled ones look ugly.
m_objectTreeWidget->setEnabled(true);
m_propertiesWidget->setEnabled(true);
m_expressionWidget->setEnabled(true);
m_simultaneousCppAndQmlDebugMode = false;
break;
}
default:
break;
}
m_cppDebuggerState = newState;
updateMenuActions();
}
......
......@@ -56,11 +56,17 @@ QT_END_NAMESPACE
namespace ProjectExplorer {
class Project;
class Environment;
}
namespace Core {
class IContext;
}
namespace Debugger {
namespace Internal {
class DebuggerRunControl;
} // Internal
} // Debugger
namespace Qml {
......@@ -85,6 +91,12 @@ class QMLINSPECTOR_EXPORT QmlInspector : public QObject
Q_OBJECT
public:
enum DebugMode {
StandaloneMode,
CppProjectWithQmlEngines,
QmlProjectWithCppPlugins
};
QmlInspector(QObject *parent = 0);
~QmlInspector();
......@@ -109,7 +121,6 @@ public slots:
void setSimpleDockWidgetArrangement();
private slots:
void startConnectionTimer();
void connectionStateChanged();
void connectionError();
void reloadEngines();
......@@ -117,12 +128,20 @@ private slots:
void queryEngineContext(int);
void contextChanged();
void treeObjectActivated(const QDeclarativeDebugObjectReference &obj);
void attachToExternalQmlApplication();
void simultaneouslyDebugQmlCppApplication();
void debuggerStateChanged(int newState);
void pollInspector();
private:
void updateMenuActions();
Debugger::Internal::DebuggerRunControl *createDebuggerRunControl(ProjectExplorer::RunConfiguration *runConfig,
const QString &executableFile = QString(),
const QString &executableArguments = QString());
QString executeDebuggerRunControl(Debugger::Internal::DebuggerRunControl *debuggableRunControl, ProjectExplorer::Environment *environment);
QString attachToQmlViewerAsExternalApp(ProjectExplorer::Project *project);
QString attachToExternalCppAppWithQml(ProjectExplorer::Project *project);
bool addQuotesForData(const QVariant &value) const;
void resetViews();
......@@ -151,6 +170,8 @@ private:
Internal::InspectorContext *m_context;
Internal::InspectorContext *m_propWatcherContext;
QAction *m_simultaneousDebugAction;
QTimer *m_connectionTimer;
int m_connectionAttempts;
......@@ -159,9 +180,11 @@ private:
QStringList m_editablePropertyTypes;
// simultaneous debug mode stuff
int m_cppDebuggerState;
bool m_connectionInitialized;
bool m_simultaneousCppAndQmlDebugMode;
DebugMode m_debugMode;
static QmlInspector *m_instance;
};
......
......@@ -39,7 +39,7 @@ namespace Qml {
const char * const C_INSPECTOR = "QmlInspector";
const char * const COMPLETE_THIS = "QmlInspector.CompleteThis";
const char * const M_ATTACH_TO_EXTERNAL = "QmlInspector.Menu.AttachToExternal";
const char * const M_DEBUG_SIMULTANEOUSLY = "QmlInspector.Menu.SimultaneousDebug";
const char * const LANG_QML = "QML";
......
......@@ -10,7 +10,7 @@ namespace Internal {
StartExternalQmlDialog::StartExternalQmlDialog(QWidget *parent)
: QDialog(parent), m_ui(new Ui::StartExternalQmlDialog)
: QDialog(parent), m_ui(new Ui::StartExternalQmlDialog), m_debugMode(QmlProjectWithCppPlugins)
{
m_ui->setupUi(this);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
......@@ -50,5 +50,45 @@ void StartExternalQmlDialog::setProjectDisplayName(const QString &projectName)
m_ui->projectDisplayName->setText(projectName);
}
void StartExternalQmlDialog::setQmlViewerPath(const QString &path)
{
m_ui->viewerPathLineEdit->setText(path);
}
QString StartExternalQmlDialog::qmlViewerPath() const
{
return m_ui->viewerPathLineEdit->text();
}
void StartExternalQmlDialog::setQmlViewerArguments(const QString &arguments)
{
m_ui->viewerArgumentsLineEdit->setText(arguments);
}
QString StartExternalQmlDialog::qmlViewerArguments() const
{
return m_ui->viewerArgumentsLineEdit->text();
}
void StartExternalQmlDialog::setDebugMode(DebugMode mode)
{
m_debugMode = mode;
if (m_debugMode == QmlProjectWithCppPlugins) {
m_ui->labelViewerPath->show();
m_ui->viewerPathLineEdit->show();
m_ui->labelViewerArguments->show();
m_ui->viewerArgumentsLineEdit->show();
setMinimumHeight(270);
resize(width(), 270);
} else {
m_ui->labelViewerPath->hide();
m_ui->viewerPathLineEdit->hide();
m_ui->labelViewerArguments->hide();
m_ui->viewerArgumentsLineEdit->hide();
setMinimumHeight(200);
resize(width(), 200);
}
}
} // Internal
} // Qml
......@@ -21,6 +21,19 @@ public:
explicit StartExternalQmlDialog(QWidget *parent);
~StartExternalQmlDialog();
enum DebugMode {
QmlProjectWithCppPlugins,
CppProjectWithQmlEngine
};
void setDebugMode(DebugMode mode);
void setQmlViewerPath(const QString &path);
QString qmlViewerPath() const;
void setQmlViewerArguments(const QString &arguments);
QString qmlViewerArguments() const;
void setDebuggerUrl(const QString &url);
QString debuggerUrl() const;
......@@ -33,6 +46,7 @@ private slots:
private:
Ui::StartExternalQmlDialog *m_ui;
DebugMode m_debugMode;
};
} // Internal
......
......@@ -7,11 +7,17 @@
<x>0</x>
<y>0</y>
<width>425</width>
<height>198</height>
<height>278</height>