summaryrefslogtreecommitdiffstats
path: root/app/perfelfmap.cpp
blob: e891fa0b304024caf582d84ab387c240b1706565 (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
/****************************************************************************
**
** Copyright (C) 2017 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 "perfelfmap.h"
#include "perfdata.h"

#include <QDebug>

QDebug operator<<(QDebug stream, const PerfElfMap::ElfInfo& info)
{
    stream.nospace() << "ElfInfo{"
                     << "file=" << info.file.fileName() << ", "
                     << "found=" << info.found << ", "
                     << "addr=" << info.addr << ", "
                     << "len=" << info.length << ", "
                     << "pgoff=" << info.pgoff << ", "
                     << "timeAdded=" << info.timeAdded << ", "
                     << "}";
    return stream.space();
}

namespace {
struct SortByAddr
{
    bool operator()(const PerfElfMap::ElfInfo &lhs, const quint64 addr) const
    {
        return lhs.addr < addr;
    }

    bool operator()(const quint64 addr, const PerfElfMap::ElfInfo &rhs) const
    {
        return addr < rhs.addr;
    }
};
}

bool PerfElfMap::registerElf(const quint64 addr, const quint64 len, quint64 pgoff,
                             const quint64 time, const QFileInfo &fullPath)
{
    bool cacheInvalid = false;
    const quint64 addrEnd = addr + len;
    const bool isFile = fullPath.isFile();

    QVarLengthArray<ElfInfo, 8> newElfs;
    QVarLengthArray<int, 8> removedElfs;
    for (auto i = m_elfs.begin(), end = m_elfs.end(); i != end && i->addr < addrEnd; ++i) {
        const quint64 iEnd = i->addr + i->length;
        if (iEnd <= addr)
            continue;

        // Newly added elf overwrites existing one. Mark the existing one as overwritten and
        // reinsert any fragments of it that remain.

        if (i->addr < addr) {
            newElfs.push_back(ElfInfo(i->file, i->addr, addr - i->addr,
                                      i->pgoff, time));
        }
        if (iEnd > addrEnd) {
            newElfs.push_back(ElfInfo(i->file, addrEnd, iEnd - addrEnd,
                                      i->pgoff + addrEnd - i->addr, time));
        }

        removedElfs.push_back(std::distance(m_elfs.begin(), i));

        // Overlapping module. Clear the cache, but only when the section is actually backed by a
        // file. Otherwise, we will see tons of overlapping heap/anon sections which don't actually
        // invalidate our caches
        if (isFile || i->found)
            cacheInvalid = true;
    }

    // remove the overwritten elfs, iterate from the back to not invalidate the indices
    for (auto it = removedElfs.rbegin(), end = removedElfs.rend(); it != end; ++it)
        m_elfs.remove(*it);

    newElfs.push_back(ElfInfo(fullPath, addr, len, pgoff, time));

    for (const auto &elf : newElfs) {
        auto it = std::lower_bound(m_elfs.begin(), m_elfs.end(),
                                   elf.addr, SortByAddr());
        m_elfs.insert(it, elf);
    }

    return cacheInvalid;
}

PerfElfMap::ElfInfo PerfElfMap::findElf(quint64 ip, quint64 timestamp) const
{
    auto i = std::upper_bound(m_elfs.begin(), m_elfs.end(), ip, SortByAddr());
    if (i == m_elfs.constEnd() || i->addr != ip) {
        if (i != m_elfs.constBegin())
            --i;
        else
            return {};
    }

    while (true) {
        if (i->timeAdded <= timestamp)
            return (i->addr + i->length > ip) ? *i : ElfInfo();

        if (i == m_elfs.constBegin())
            return {};

        --i;
    }
}

bool PerfElfMap::isAddressInRange(quint64 addr) const
{
    if (m_elfs.isEmpty())
        return false;

    const auto &first = m_elfs.first();
    const auto &last = m_elfs.last();
    return first.addr <= addr && addr < (last.addr + last.length);
}