Commit 7975ff15 authored by hjk's avatar hjk Committed by hjk
Browse files

analyzer: remove some leftovers from the valgrind merge

Change-Id: Ide127840c2dca16927e3be9b95c6dd29d0ee42fc
Reviewed-on: http://codereview.qt.nokia.com/524

Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent 4bd73afb
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.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 info@qt.nokia.com.
**
**************************************************************************/
#include "callgrindtool.h"
#include "callgrindconstants.h"
#include "callgrindcostdelegate.h"
#include "callgrindcostview.h"
#include "callgrindengine.h"
#include "callgrindsettings.h"
#include "callgrindtextmark.h"
#include "callgrindvisualisation.h"
#include <valgrind/callgrind/callgrindcallmodel.h>
#include <valgrind/callgrind/callgrindcostitem.h>
#include <valgrind/callgrind/callgrinddatamodel.h>
#include <valgrind/callgrind/callgrindfunction.h>
#include <valgrind/callgrind/callgrindfunctioncall.h>
#include <valgrind/callgrind/callgrindparsedata.h>
#include <valgrind/callgrind/callgrindproxymodel.h>
#include <valgrind/callgrind/callgrindstackbrowser.h>
#include <analyzerbase/analyzermanager.h>
#include <analyzerbase/analyzersettings.h>
#include <analyzerbase/analyzerutils.h>
#include <analyzerbase/analyzerconstants.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/Overview.h>
#include <cppeditor/cppeditorconstants.h>
#include <extensionsystem/iplugin.h>
#include <texteditor/basetexteditor.h>
#include <utils/qtcassert.h>
#include <utils/fancymainwindow.h>
#include <utils/styledbar.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTimer>
#include <QtGui/QAction>
#include <QtGui/QActionGroup>
#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QDockWidget>
#include <QtGui/QGraphicsItem>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QMenu>
#include <QtGui/QSortFilterProxyModel>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtGui/QVBoxLayout>
// shared/cplusplus includes
#include <Symbols.h>
using namespace Analyzer;
using namespace Core;
using namespace Valgrind::Callgrind;
namespace Callgrind {
namespace Internal {
class CallgrindToolPrivate : public QObject
{
Q_OBJECT
public:
friend class CallgrindTool;
explicit CallgrindToolPrivate(CallgrindTool *parent);
virtual ~CallgrindToolPrivate();
void setParseData(ParseData *data);
CostDelegate::CostFormat costFormat() const;
QWidget *createControlWidget();
void initializeDockWidgets();
void ensureDockWidgets();
void doClear(bool clearParseData);
void updateEventCombo();
IAnalyzerEngine *createEngine(const AnalyzerStartParameters &sp,
ProjectExplorer::RunConfiguration *runConfiguration);
signals:
void cycleDetectionEnabled(bool enabled);
void dumpRequested();
void resetRequested();
void pauseToggled(bool checked);
public slots:
void slotClear();
void slotRequestDump();
void selectFunction(const Valgrind::Callgrind::Function *);
void setCostFormat(Callgrind::Internal::CostDelegate::CostFormat format);
void enableCycleDetection(bool enabled);
void setCostEvent(int index);
/// This function will add custom text marks to the editor
/// \note Call this after the data model has been populated
void createTextMarks();
/// This function will clear all text marks from the editor
void clearTextMarks();
void updateFilterString();
void updateCostFormat();
void handleFilterProjectCosts();
void handleShowCostsAction();
void handleShowCostsOfFunction();
void slotGoToOverview();
void stackBrowserChanged();
/// If \param busy is true, all widgets get a busy cursor when hovered
void setBusy(bool busy);
void dataFunctionSelected(const QModelIndex &index);
void calleeFunctionSelected(const QModelIndex &index);
void callerFunctionSelected(const QModelIndex &index);
void visualisationFunctionSelected(const Valgrind::Callgrind::Function *function);
void showParserResults(const Valgrind::Callgrind::ParseData *data);
void takeParserData(CallgrindEngine *engine);
void engineStarting(const Analyzer::IAnalyzerEngine *);
void engineFinished();
void editorOpened(Core::IEditor *);
void requestContextMenu(TextEditor::ITextEditor *editor, int line, QMenu *menu);
public:
CallgrindTool *q;
DataModel *m_dataModel;
DataProxyModel *m_proxyModel;
StackBrowser *m_stackBrowser;
CallModel *m_callersModel;
CallModel *m_calleesModel;
// callgrind widgets
CostView *m_flatView;
CostView *m_callersView;
CostView *m_calleesView;
Visualisation *m_visualisation;
// navigation
QAction *m_goToOverview;
QAction *m_goBack;
QLineEdit *m_searchFilter;
// cost formatting
QAction *m_filterProjectCosts;
QAction *m_costAbsolute;
QAction *m_costRelative;
QAction *m_costRelativeToParent;
QAction *m_cycleDetection;
QComboBox *m_eventCombo;
QTimer *m_updateTimer;
QVector<CallgrindTextMark *> m_textMarks;
QAction *m_dumpAction;
QAction *m_resetAction;
QAction *m_pauseAction;
QAction *m_showCostsOfFunctionAction;
QString m_toggleCollectFunction;
CallgrindGlobalSettings *m_settings; // Not owned
};
CallgrindToolPrivate::CallgrindToolPrivate(CallgrindTool *parent)
: q(parent)
, m_dataModel(new DataModel(this))
, m_proxyModel(new DataProxyModel(this))
, m_stackBrowser(new StackBrowser(this))
, m_callersModel(new CallModel(this))
, m_calleesModel(new CallModel(this))
, m_flatView(0)
, m_callersView(0)
, m_calleesView(0)
, m_visualisation(0)
, m_goToOverview(0)
, m_goBack(0)
, m_searchFilter(0)
, m_filterProjectCosts(0)
, m_costAbsolute(0)
, m_costRelative(0)
, m_costRelativeToParent(0)
, m_eventCombo(0)
, m_updateTimer(new QTimer(this))
, m_dumpAction(0)
, m_resetAction(0)
, m_pauseAction(0)
, m_showCostsOfFunctionAction(0)
, m_settings(0)
{
connect(m_stackBrowser, SIGNAL(currentChanged()), this, SLOT(stackBrowserChanged()));
m_updateTimer->setInterval(200);
m_updateTimer->setSingleShot(true);
connect(m_updateTimer, SIGNAL(timeout()), SLOT(updateFilterString()));
m_proxyModel->setSourceModel(m_dataModel);
m_proxyModel->setDynamicSortFilter(true);
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_proxyModel->setFilterKeyColumn(DataModel::NameColumn);
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_settings = AnalyzerGlobalSettings::instance()->subConfig<CallgrindGlobalSettings>();
}
CallgrindToolPrivate::~CallgrindToolPrivate()
{
qDeleteAll(m_textMarks);
doClear(false);
}
void CallgrindToolPrivate::slotGoToOverview()
{
selectFunction(0);
}
void CallgrindToolPrivate::slotClear()
{
doClear(true);
}
void CallgrindToolPrivate::doClear(bool clearParseData)
{
if (clearParseData) // Crashed when done from destructor.
setParseData(0);
// clear filters
if (m_filterProjectCosts)
m_filterProjectCosts->setChecked(false);
m_proxyModel->setFilterBaseDir(QString());
if (m_searchFilter)
m_searchFilter->setText(QString());
m_proxyModel->setFilterFixedString(QString());
}
void CallgrindToolPrivate::setBusy(bool busy)
{
QCursor cursor(busy ? Qt::BusyCursor : Qt::ArrowCursor);
m_flatView->setCursor(cursor);
m_calleesView->setCursor(cursor);
m_callersView->setCursor(cursor);
m_visualisation->setCursor(cursor);
}
void CallgrindToolPrivate::selectFunction(const Function *func)
{
if (!func) {
m_goToOverview->setDisabled(true);
m_flatView->clearSelection();
m_visualisation->setFunction(0);
m_callersModel->clear();
m_calleesModel->clear();
return;
}
m_goToOverview->setEnabled(true);
const QModelIndex index = m_dataModel->indexForObject(func);
const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index);
m_flatView->selectionModel()->clearSelection();
m_flatView->selectionModel()->setCurrentIndex(proxyIndex,
QItemSelectionModel::ClearAndSelect |
QItemSelectionModel::Rows);
m_flatView->scrollTo(proxyIndex);
m_callersModel->setCalls(func->incomingCalls(), func);
m_calleesModel->setCalls(func->outgoingCalls(), func);
m_visualisation->setFunction(func);
const Function *item = m_stackBrowser->current();
if (!item || item != func)
m_stackBrowser->select(func);
if (QFile::exists(func->file())) {
///TODO: custom position support?
int line = func->lineNumber();
TextEditor::BaseTextEditorWidget::openEditorAt(func->file(), qMax(line, 0));
}
}
void CallgrindToolPrivate::stackBrowserChanged()
{
const Function *item = m_stackBrowser->current();
m_goBack->setEnabled(item != 0);
selectFunction(item);
}
void CallgrindToolPrivate::updateFilterString()
{
m_proxyModel->setFilterFixedString(m_searchFilter->text());
}
void CallgrindToolPrivate::setCostFormat(CostDelegate::CostFormat format)
{
switch (format) {
case CostDelegate::FormatAbsolute:
m_costAbsolute->setChecked(true);
break;
case CostDelegate::FormatRelative:
m_costRelative->setChecked(true);
break;
case CostDelegate::FormatRelativeToParent:
m_costRelativeToParent->setChecked(true);
break;
}
}
void CallgrindToolPrivate::setCostEvent(int index)
{
// prevent assert in model, don't try to set event to -1
// happens when we clear the eventcombo
if (index == -1)
index = 0;
m_dataModel->setCostEvent(index);
m_calleesModel->setCostEvent(index);
m_callersModel->setCostEvent(index);
}
void CallgrindToolPrivate::enableCycleDetection(bool enabled)
{
m_cycleDetection->setChecked(enabled);
}
// Following functions can be called with actions=0 or widgets=0
// depending on initialization sequence (whether callgrind was current).
CostDelegate::CostFormat CallgrindToolPrivate::costFormat() const
{
if (m_costRelativeToParent && m_costRelativeToParent->isChecked())
return CostDelegate::FormatRelativeToParent;
if (m_costRelative && m_costRelative->isChecked())
return CostDelegate::FormatRelative;
return CostDelegate::FormatAbsolute;
}
void CallgrindToolPrivate::updateCostFormat()
{
const CostDelegate::CostFormat format = costFormat();
if (m_flatView)
m_flatView->setCostFormat(format);
if (m_calleesView) {
m_calleesView->setCostFormat(format);
m_callersView->setCostFormat(format);
}
if (m_settings)
m_settings->setCostFormat(format);
}
void CallgrindToolPrivate::handleFilterProjectCosts()
{
ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
ProjectExplorer::Project *pro = pe->currentProject();
QTC_ASSERT(pro, return)
if (m_filterProjectCosts->isChecked()) {
const QString projectDir = pro->projectDirectory();
m_proxyModel->setFilterBaseDir(projectDir);
}
else {
m_proxyModel->setFilterBaseDir(QString());
}
}
void CallgrindToolPrivate::dataFunctionSelected(const QModelIndex &index)
{
const Function *func = index.data(DataModel::FunctionRole).value<const Function *>();
QTC_ASSERT(func, return);
selectFunction(func);
}
void CallgrindToolPrivate::calleeFunctionSelected(const QModelIndex &index)
{
const FunctionCall *call = index.data(CallModel::FunctionCallRole).value<const FunctionCall *>();
QTC_ASSERT(call, return);
selectFunction(call->callee());
}
void CallgrindToolPrivate::callerFunctionSelected(const QModelIndex &index)
{
const FunctionCall *call = index.data(CallModel::FunctionCallRole).value<const FunctionCall *>();
QTC_ASSERT(call, return);
selectFunction(call->caller());
}
void CallgrindToolPrivate::visualisationFunctionSelected(const Function *function)
{
if (function && function == m_visualisation->function())
// up-navigation when the initial function was activated
m_stackBrowser->goBack();
else
selectFunction(function);
}
void CallgrindToolPrivate::setParseData(ParseData *data)
{
// we have new parse data, invalidate filters in the proxy model
m_visualisation->setFunction(0);
// invalidate parse data in the data model
delete m_dataModel->parseData();
if (data && data->events().isEmpty()) {
// might happen if the user cancelled the profile run
// callgrind then sometimes produces empty callgrind.out.PID files
delete data;
data = 0;
}
m_dataModel->setParseData(data);
m_calleesModel->setParseData(data);
m_callersModel->setParseData(data);
updateEventCombo();
// clear history for new data
m_stackBrowser->clear();
// unset busy state
setBusy(false);
}
void CallgrindToolPrivate::updateEventCombo()
{
QTC_ASSERT(m_eventCombo, return)
m_eventCombo->clear();
const ParseData *data = m_dataModel->parseData();
if (!data || data->events().isEmpty()) {
m_eventCombo->hide();
return;
}
m_eventCombo->show();
foreach (const QString &event, data->events())
m_eventCombo->addItem(ParseData::prettyStringForEvent(event));
}
static QToolButton *createToolButton(QAction *action)
{
QToolButton *button = new QToolButton;
button->setDefaultAction(action);
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
return button;
}
CallgrindTool::CallgrindTool(QObject *parent)
: Analyzer::IAnalyzerTool(parent)
{
d = new CallgrindToolPrivate(this);
Core::ICore *core = Core::ICore::instance();
// EditorManager
QObject *editorManager = core->editorManager();
connect(editorManager, SIGNAL(editorOpened(Core::IEditor*)),
d, SLOT(editorOpened(Core::IEditor*)));
}
CallgrindTool::~CallgrindTool()
{
delete d;
}
QString CallgrindTool::id() const
{
return "Callgrind";
}
QString CallgrindTool::displayName() const
{
return tr("Profile");
}
IAnalyzerTool::ToolMode CallgrindTool::mode() const
{
return ReleaseMode;
}
void CallgrindTool::initialize()
{
}
void CallgrindTool::extensionsInitialized()
{
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *actionManager = core->actionManager();
Core::Context analyzerContext = Core::Context(Analyzer::Constants::C_ANALYZEMODE);
// check if there is a CppEditor context menu, if true, add our own context menu actions
if (Core::ActionContainer *editorContextMenu =
actionManager->actionContainer(CppEditor::Constants::M_CONTEXT)) {
QAction *action = 0;
Core::Command *cmd = 0;
action = new QAction(this);
action->setSeparator(true);
cmd = actionManager->registerAction(action, "Analyzer.Callgrind.ContextMenu.Sep",
analyzerContext);
editorContextMenu->addAction(cmd);
action = new QAction(tr("Profile Costs of this Function and its Callees"), this);
action->setIcon(QIcon(Analyzer::Constants::ANALYZER_CONTROL_START_ICON));
connect(action, SIGNAL(triggered()), d, SLOT(handleShowCostsOfFunction()));
cmd = actionManager->registerAction(action, "Analyzer.Callgrind.ShowCostsOfFunction",
analyzerContext);
editorContextMenu->addAction(cmd);
cmd->setAttribute(Core::Command::CA_Hide);
cmd->setAttribute(Core::Command::CA_NonConfigurable);
d->m_showCostsOfFunctionAction = action;
}
}
void CallgrindTool::initializeDockWidgets()
{
d->initializeDockWidgets();
}
void CallgrindToolPrivate::initializeDockWidgets()
{
AnalyzerManager *am = AnalyzerManager::instance();
Utils::FancyMainWindow *mw = am->mainWindow();
m_visualisation = new Visualisation(mw);
m_visualisation->setFrameStyle(QFrame::NoFrame);
m_visualisation->setObjectName("Valgrind.CallgrindToolPrivate.Visualisation");
m_visualisation->setModel(m_dataModel);
connect(m_visualisation, SIGNAL(functionActivated(const Valgrind::Callgrind::Function*)),
this, SLOT(visualisationFunctionSelected(const Valgrind::Callgrind::Function*)));
m_callersView = new CostView(mw);
m_callersView->sortByColumn(CallModel::CostColumn);
m_callersView->setObjectName("Valgrind.CallgrindToolPrivate.CallersView");
m_callersView->setFrameStyle(QFrame::NoFrame);
// enable sorting
QSortFilterProxyModel *callerProxy = new QSortFilterProxyModel(m_callersModel);
callerProxy->setSourceModel(m_callersModel);
m_callersView->setModel(callerProxy);
m_callersView->hideColumn(CallModel::CalleeColumn);
connect(m_callersView, SIGNAL(activated(QModelIndex)),
this, SLOT(callerFunctionSelected(QModelIndex)));
m_calleesView = new CostView(mw);
m_calleesView->sortByColumn(CallModel::CostColumn);
m_calleesView->setObjectName("Valgrind.CallgrindToolPrivate.CalleesView");
m_calleesView->setFrameStyle(QFrame::NoFrame);
// enable sorting
QSortFilterProxyModel *calleeProxy = new QSortFilterProxyModel(m_calleesModel);
calleeProxy->setSourceModel(m_calleesModel);
m_calleesView->setModel(calleeProxy);
m_calleesView->hideColumn(CallModel::CallerColumn);