File: processtracker_linux.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (85 lines) | stat: -rw-r--r-- 2,586 bytes parent folder | download
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
/*
  processtracker_linux.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Filipe Azevedo <filipe.azevedo@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

// A said crossplatform way (using ptrace) is available at
// https://forum.juce.com/t/detecting-if-a-process-is-being-run-under-a-debugger/2098

#include "processtracker_linux.h"

#include <QFile>
#include <QStringList>
#include <QStringDecoder>

namespace {
static QString readFile(const QString &filePath, const QByteArray &codec = QByteArrayLiteral("UTF-8"))
{
    QFile file(filePath);

    if (!file.exists()) {
        qWarning("%s: File does not exists", Q_FUNC_INFO);
        return QString();
    }

    if (!file.open(QIODevice::ReadOnly)) {
        qWarning("%s: File can not be opened", Q_FUNC_INFO);
        return QString();
    }

    return QStringDecoder(codec).decode(file.readAll());
}
}

using namespace GammaRay;

ProcessTrackerBackendLinux::ProcessTrackerBackendLinux(QObject *parent)
    : GammaRay::ProcessTrackerBackend(parent)
{
}

void ProcessTrackerBackendLinux::checkProcess(qint64 pid)
{
    GammaRay::ProcessTrackerInfo pinfo(pid);
    QString buffer(readFile(QString::fromLatin1("/proc/%1/status").arg(pid)));

    if (!buffer.isEmpty()) {
        const QStringList values = buffer.remove(QLatin1Char('\t')).split(QLatin1Char('\n'));

        for (const QString &value : values) {
            if (value.startsWith(QStringLiteral("TracerPid:"))) {
                pinfo.traced = value.split(QLatin1Char(':')).value(1).simplified().toLongLong();
            } else if (value.startsWith(QStringLiteral("State:"))) {
                const char status(value.split(QLatin1Char(':')).value(1).simplified()[0].toUpper().toLatin1());

                // status decoding as taken from fs/proc/array.c
                switch (status) {
                case 'T': {
                    pinfo.state = GammaRay::ProcessTracker::Suspended;
                    break;
                }

                case 'S': // Sleeping
                case 'R': {
                    pinfo.state = GammaRay::ProcessTracker::Running;
                    break;
                }

                    // case 'Z': // Zombie
                    // case 'D': // Disk Sleep
                    // case 'W': // Paging
                }
            }
        }
    }

    emit processChecked(pinfo);
}