1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/*
SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "framestackmodel.h"
#include <QFileInfo>
#include <QIcon>
#include <QMimeType>
#include <QMimeDatabase>
#include <KLocalizedString>
#include <KColorScheme>
#include "../../interfaces/icore.h"
#include "../../interfaces/iprojectcontroller.h"
#include "../interfaces/isession.h"
#include <debug.h>
namespace KDevelop {
class FrameStackModelPrivate
{
public:
explicit FrameStackModelPrivate(FrameStackModel* q) : q(q) {}
void update();
QModelIndex indexForThreadNumber(int threadNumber);
FrameStackModel* q;
int m_currentThread = -1;
int m_currentFrame = -1;
int m_crashedThreadIndex = -1;
// used to count how often a user has scrolled down and more frames needed to be fetched;
// this way, the number of frames fetched in each chunk can be increased if the user wants
// to scroll far
int m_subsequentFrameFetchOperations = 0;
bool m_updateCurrentFrameOnNextFetch = false;
QVector<FrameStackModel::ThreadItem> m_threads;
QHash<int, QVector<FrameStackModel::FrameItem>> m_frames;
QHash<int, bool> m_hasMoreFrames;
// Caches
mutable QHash<QString, bool> m_fileExistsCache;
};
FrameStackModel::FrameStackModel(IDebugSession *session)
: IFrameStackModel(session)
, d_ptr(new FrameStackModelPrivate(this))
{
connect(session, &IDebugSession::stateChanged, this, &FrameStackModel::stateChanged);
}
FrameStackModel::~FrameStackModel()
{
}
void FrameStackModel::setThreads(const QVector<ThreadItem>& threads)
{
Q_D(FrameStackModel);
qCDebug(DEBUGGER) << threads.count();
if (!d->m_threads.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, d->m_threads.count()-1);
d->m_threads.clear();
endRemoveRows();
}
if (!threads.isEmpty()) {
beginInsertRows(QModelIndex(), 0, threads.count()-1);
d->m_threads = threads;
endInsertRows();
}
}
QModelIndex FrameStackModelPrivate::indexForThreadNumber(int threadNumber)
{
int i=0;
for (const auto& t : qAsConst(m_threads)) {
if (t.nr == threadNumber) {
return q->index(i, 0);
}
i++;
}
return QModelIndex();
}
void FrameStackModel::setFrames(int threadNumber, const QVector<FrameItem>& frames)
{
Q_D(FrameStackModel);
QModelIndex threadIndex = d->indexForThreadNumber(threadNumber);
Q_ASSERT(threadIndex.isValid());
if (!d->m_frames[threadNumber].isEmpty()) {
beginRemoveRows(threadIndex, 0, d->m_frames[threadNumber].count()-1);
d->m_frames[threadNumber].clear();
endRemoveRows();
}
if (!frames.isEmpty()) {
beginInsertRows(threadIndex, 0, frames.count()-1);
d->m_frames[threadNumber] = frames;
endInsertRows();
}
if (d->m_currentThread == threadNumber && d->m_updateCurrentFrameOnNextFetch) {
d->m_currentFrame = 0;
d->m_updateCurrentFrameOnNextFetch = false;
}
d->m_fileExistsCache.clear();
session()->raiseEvent(IDebugSession::thread_or_frame_changed);
// FIXME: Ugly hack. Apparently, when rows are added, the selection
// in the view is cleared. Emit this so that some frame is still
// selected.
emit currentFrameChanged(d->m_currentFrame);
}
void FrameStackModel::insertFrames(int threadNumber, const QVector<FrameItem>& frames)
{
Q_D(FrameStackModel);
QModelIndex threadIndex = d->indexForThreadNumber(threadNumber);
Q_ASSERT(threadIndex.isValid());
beginInsertRows(threadIndex, d->m_frames[threadNumber].count()-1,
d->m_frames[threadNumber].count()+frames.count()-1);
d->m_frames[threadNumber] << frames;
endInsertRows();
}
void FrameStackModel::setHasMoreFrames(int threadNumber, bool hasMoreFrames)
{
Q_D(FrameStackModel);
d->m_hasMoreFrames[threadNumber] = hasMoreFrames;
}
FrameStackModel::FrameItem FrameStackModel::frame(const QModelIndex& index)
{
Q_D(FrameStackModel);
Q_ASSERT(index.internalId());
Q_ASSERT(static_cast<quintptr>(d->m_threads.count()) >= index.internalId());
const ThreadItem &thread = d->m_threads.at(index.internalId()-1);
return d->m_frames[thread.nr].at(index.row());
}
QVector<FrameStackModel::FrameItem> FrameStackModel::frames(int threadNumber) const
{
Q_D(const FrameStackModel);
return d->m_frames.value(threadNumber);
}
QVariant FrameStackModel::data(const QModelIndex& index, int role) const
{
Q_D(const FrameStackModel);
if (!index.internalId()) {
//thread
if (d->m_threads.count() <= index.row()) return QVariant();
const ThreadItem &thread = d->m_threads.at(index.row());
if (index.column() == 0) {
if (role == Qt::DisplayRole) {
if (thread.nr == d->m_crashedThreadIndex) {
return i18nc("#thread-id at function-name or address", "#%1 at %2 (crashed)", thread.nr, thread.name);
} else {
return i18nc("#thread-id at function-name or address", "#%1 at %2", thread.nr, thread.name);
}
} else if (role == Qt::ForegroundRole) {
if (thread.nr == d->m_crashedThreadIndex) {
KColorScheme scheme(QPalette::Active);
return scheme.foreground(KColorScheme::NegativeText).color();
}
}
}
} else {
//frame
if (static_cast<quintptr>(d->m_threads.count()) < index.internalId()) return QVariant();
const ThreadItem &thread = d->m_threads.at(index.internalId()-1);
if (d->m_frames[thread.nr].count() <= index.row()) return QVariant();
const FrameItem &frame = d->m_frames[thread.nr].at(index.row());
if (index.column() == 0) {
if (role == Qt::DisplayRole) {
return QVariant(QString::number(frame.nr));
}
} else if (index.column() == 1) {
if (role == Qt::DisplayRole) {
return QVariant(frame.name);
}
} else if (index.column() == 2) {
if (role == Qt::DisplayRole) {
QString ret = ICore::self()->projectController()
->prettyFileName(frame.file, IProjectController::FormatPlain);
if (frame.line != -1) {
ret += QLatin1Char(':') + QString::number(frame.line + 1);
}
return ret;
} else if (role == Qt::DecorationRole) {
QMimeType mime = QMimeDatabase().mimeTypeForUrl(frame.file);
return QIcon::fromTheme(mime.iconName());
} else if (role == Qt::ForegroundRole) {
const auto fileName = frame.file.toLocalFile();
auto cacheIt = d->m_fileExistsCache.find(fileName);
if (cacheIt == d->m_fileExistsCache.end()) {
cacheIt = d->m_fileExistsCache.insert(fileName, QFileInfo::exists(fileName));
}
const bool fileExists = cacheIt.value();
if (!fileExists) {
KColorScheme scheme(QPalette::Active);
return scheme.foreground(KColorScheme::InactiveText).color();
}
}
}
}
return QVariant();
}
int FrameStackModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 3;
}
int FrameStackModel::rowCount(const QModelIndex& parent) const
{
Q_D(const FrameStackModel);
if (!parent.isValid()) {
return d->m_threads.count();
} else if (!parent.internalId() && parent.column() == 0) {
if (parent.row() < d->m_threads.count()) {
return d->m_frames[d->m_threads.at(parent.row()).nr].count();
}
}
return 0;
}
QModelIndex FrameStackModel::parent(const QModelIndex& child) const
{
if (!child.internalId()) {
return QModelIndex();
} else {
return index(child.internalId()-1, 0);
}
}
QModelIndex FrameStackModel::index(int row, int column, const QModelIndex& parent) const
{
Q_D(const FrameStackModel);
if (parent.isValid()) {
Q_ASSERT(!parent.internalId());
Q_ASSERT(parent.column() == 0);
Q_ASSERT(parent.row() < d->m_threads.count());
return createIndex(row, column, parent.row()+1);
} else {
return createIndex(row, column);
}
}
QVariant FrameStackModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0) {
return i18n("Depth");
} else if (section == 1) {
return i18n("Function");
} else if (section == 2) {
return i18n("Source");
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
void FrameStackModel::setCurrentThread(int threadNumber)
{
Q_D(FrameStackModel);
qCDebug(DEBUGGER) << threadNumber;
if (d->m_currentThread != threadNumber && threadNumber != -1) {
// FIXME: this logic means that if we switch to thread 3 and
// then to thread 2 and then to thread 3, we'll request frames
// for thread 3 again, even if the program was not run in between
// and therefore frames could not have changed.
d->m_currentFrame = 0; //set before fetchFrames else --frame argument would be wrong
d->m_updateCurrentFrameOnNextFetch = true;
fetchFrames(threadNumber, 0, 20);
}
if (threadNumber != d->m_currentThread) {
d->m_currentFrame = 0;
d->m_currentThread = threadNumber;
emit currentFrameChanged(d->m_currentFrame);
}
qCDebug(DEBUGGER) << "currentThread: " << d->m_currentThread << "currentFrame: " << d->m_currentFrame;
emit currentThreadChanged(threadNumber);
session()->raiseEvent(IDebugSession::thread_or_frame_changed);
}
void FrameStackModel::setCurrentThread(const QModelIndex& index)
{
Q_D(const FrameStackModel);
Q_ASSERT(index.isValid());
Q_ASSERT(!index.internalId());
Q_ASSERT(index.column() == 0);
setCurrentThread(d->m_threads[index.row()].nr);
}
void FrameStackModel::setCrashedThreadIndex(int index)
{
Q_D(FrameStackModel);
d->m_crashedThreadIndex = index;
}
int FrameStackModel::crashedThreadIndex() const
{
Q_D(const FrameStackModel);
return d->m_crashedThreadIndex;
}
int FrameStackModel::currentThread() const
{
Q_D(const FrameStackModel);
return d->m_currentThread;
}
QModelIndex FrameStackModel::currentThreadIndex() const
{
Q_D(const FrameStackModel);
int i = 0;
for (const ThreadItem& t : qAsConst(d->m_threads)) {
if (t.nr == currentThread()) {
return index(i, 0);
}
++i;
}
return QModelIndex();
}
int FrameStackModel::currentFrame() const
{
Q_D(const FrameStackModel);
return d->m_currentFrame;
}
QModelIndex FrameStackModel::currentFrameIndex() const
{
Q_D(const FrameStackModel);
return index(d->m_currentFrame, 0, currentThreadIndex());
}
void FrameStackModel::setCurrentFrame(int frame)
{
Q_D(FrameStackModel);
qCDebug(DEBUGGER) << frame;
if (frame != d->m_currentFrame)
{
d->m_currentFrame = frame;
session()->raiseEvent(IDebugSession::thread_or_frame_changed);
emit currentFrameChanged(frame);
}
}
void FrameStackModelPrivate::update()
{
m_subsequentFrameFetchOperations = 0;
q->fetchThreads();
if (m_currentThread != -1) {
q->fetchFrames(m_currentThread, 0, 20);
}
}
void FrameStackModel::handleEvent(IDebugSession::event_t event)
{
Q_D(FrameStackModel);
switch (event)
{
case IDebugSession::program_state_changed:
d->update();
break;
default:
break;
}
}
void FrameStackModel::stateChanged(IDebugSession::DebuggerState state)
{
Q_D(FrameStackModel);
if (state == IDebugSession::PausedState) {
setCurrentFrame(-1);
d->m_updateCurrentFrameOnNextFetch = true;
} else if (state == IDebugSession::EndedState || state == IDebugSession::NotStartedState) {
setThreads(QVector<FrameStackModel::ThreadItem>());
}
}
// FIXME: it should be possible to fetch more frames for
// an arbitrary thread, without making it current.
void FrameStackModel::fetchMoreFrames()
{
Q_D(FrameStackModel);
d->m_subsequentFrameFetchOperations += 1;
const int fetch = 20 * d->m_subsequentFrameFetchOperations * d->m_subsequentFrameFetchOperations;
if (d->m_currentThread != -1 && d->m_hasMoreFrames[d->m_currentThread]) {
setHasMoreFrames(d->m_currentThread, false);
fetchFrames(d->m_currentThread,
d->m_frames[d->m_currentThread].count(),
d->m_frames[d->m_currentThread].count()-1+fetch);
}
}
}
|