summaryrefslogtreecommitdiffstats
path: root/app/perfheader.cpp
blob: 9987e3bbc24a0d83a4c68163d220a4b35f611ab7 (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Perf Profiler Add-on.
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in
** the file LICENSE.GPLv3 included in the packaging of this file. Please
** review the following information to ensure the GNU General Public License
** requirements will be met: https://www.gnu.org/licenses/gpl.html.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "perfheader.h"

#include <QDebug>

PerfHeader::PerfHeader(QIODevice *source)  :
    m_source(source), m_magic(0), m_size(0), m_attrSize(0)
{
    connect(source, &QIODevice::readyRead, this, &PerfHeader::read);
    connect(source, &QIODevice::aboutToClose, this, &PerfHeader::error);
    for (uint i = 0; i < sizeof(m_features) / sizeof(quint64); ++i)
        m_features[i] = 0;
}

void PerfHeader::read()
{
    const uint featureParts = sizeof(m_features) / sizeof(quint64);

    QDataStream stream(m_source);
    if (m_size == 0) {
        if (!m_source->isSequential() && m_source->size() < pipeHeaderFixedLength()) {
            qWarning() << "File is too small for perf header.";
            emit error();
            return;
        }

        if (m_source->bytesAvailable() < pipeHeaderFixedLength())
            return;

        stream >> m_magic;
        if (m_magic != s_magicSame && m_magic != s_magicSwitched) {
            qWarning() << "invalid magic:" << m_magic;
            qWarning() << "we don't support V1 perf data";
            emit error();
            return;
        } else {
            stream.setByteOrder(byteOrder());
        }

        stream >> m_size;
    }

    if (m_size < pipeHeaderFixedLength()) {
        qWarning() << "Header claims to be smaller than magic + size:" << m_size;
        emit error();
        return;
    } else if (m_size > pipeHeaderFixedLength()) {
        // read extended header information only available in perf.data files,
        // not in piped perf streams

        if (!m_source->isSequential() && m_source->size() < fileHeaderFixedLength()) {
            qWarning() << "File is too small for perf header.";
            emit error();
            return;
        }

        if (m_source->bytesAvailable() < fileHeaderFixedLength() - pipeHeaderFixedLength())
            return;

        // file header
        stream >> m_attrSize >> m_attrs >> m_data >> m_eventTypes;
        for (uint i = 0; i < featureParts; ++i)
            stream >> m_features[i];

        if (m_magic == s_magicSwitched && !hasFeature(HOSTNAME)) {

            quint32 *features32 = reinterpret_cast<quint32 *>(&m_features[0]);
            for (uint i = 0; i < featureParts; ++i)
                qSwap(features32[i * 2], features32[i * 2 + 1]);

            if (!hasFeature(HOSTNAME)) {
                // It borked: blank it all
                qWarning() << "bad feature data:" << m_features;
                for (uint i = 0; i < featureParts; ++i)
                    m_features[i] = 0;
                setFeature(BUILD_ID);
            }
        }

        const auto minimumFileSize = static_cast<qint64>(dataOffset() + dataSize());
        if (!m_source->isSequential() && m_source->size() < minimumFileSize) {
            qWarning() << "File is too small to hold perf data.";
            emit error();
            return;
        }

        if (m_size > fileHeaderFixedLength()) {
            if (m_size > std::numeric_limits<int>::max()) {
                qWarning() << "Excessively large perf file header:" << m_size;
                emit error();
                return;
            }
            qWarning() << "Header not completely read.";
            stream.skipRawData(static_cast<int>(m_size) - fileHeaderFixedLength());
        }
    } else {
        // pipe header, anything to do here?
    }

    disconnect(m_source, &QIODevice::readyRead, this, &PerfHeader::read);
    disconnect(m_source, &QIODevice::aboutToClose, this, &PerfHeader::error);
    m_source = nullptr;
    emit finished();
}

quint16 PerfHeader::pipeHeaderFixedLength()
{
    return sizeof(m_magic) + sizeof(m_size);
}

quint16 PerfHeader::fileHeaderFixedLength()
{
    return pipeHeaderFixedLength()
            + sizeof(m_attrSize)
            + 3 * PerfFileSection::fixedLength() // m_attrs, m_data, m_eventTypes
            + sizeof(m_features);
}

QDataStream::ByteOrder PerfHeader::byteOrder() const
{
    // magic is read in QDataStream's default big endian mode
    return m_magic == s_magicSame ? QDataStream::BigEndian : QDataStream::LittleEndian;
}

void PerfHeader::setFeature(PerfHeader::Feature feature)
{
    Q_ASSERT(feature >= 0 && feature < sizeof(m_features) * sizeof(quint64));
    m_features[feature / 64] |= (1ULL << (feature % 64));
}

void PerfHeader::clearFeature(PerfHeader::Feature feature)
{
    Q_ASSERT(feature >= 0 && feature < sizeof(m_features) * sizeof(quint64));
    m_features[feature / 64] &= ~(1ULL << (feature % 64));
}

bool PerfHeader::hasFeature(PerfHeader::Feature feature) const
{
    Q_ASSERT(feature >= 0 && feature < sizeof(m_features) * sizeof(quint64));
    return (m_features[feature / 64] & (1ULL << (feature % 64))) != 0;
}