summaryrefslogtreecommitdiffstats
path: root/app/perfaddresscache.cpp
blob: 27b646c6d4760526a6cb29dc02ff0b154f9db881 (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
/****************************************************************************
**
** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Contact: http://www.qt.io/licensing/
**
** 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 "perfaddresscache.h"

namespace {
quint64 relativeAddress(const PerfElfMap::ElfInfo& elf, quint64 addr)
{
    Q_ASSERT(elf.isValid());
    Q_ASSERT(elf.addr <= addr);
    Q_ASSERT((elf.addr + elf.length) > addr);
    return addr - elf.addr;
}
}

PerfAddressCache::AddressCacheEntry PerfAddressCache::find(const PerfElfMap::ElfInfo& elf, quint64 addr,
                                                           OffsetAddressCache *invalidAddressCache) const
{
    if (elf.isValid())
        return m_cache.value(elf.originalPath).value(relativeAddress(elf, addr));
    else
        return invalidAddressCache->value(addr);
}

void PerfAddressCache::cache(const PerfElfMap::ElfInfo& elf, quint64 addr,
                             const PerfAddressCache::AddressCacheEntry& entry,
                             OffsetAddressCache *invalidAddressCache)
{
    if (elf.isValid())
        m_cache[elf.originalPath][relativeAddress(elf, addr)] = entry;
    else
        (*invalidAddressCache)[addr] = entry;
}

static bool operator<(const PerfAddressCache::SymbolCacheEntry &lhs, quint64 addr)
{
    return lhs.offset < addr;
}

PerfAddressCache::SymbolCacheEntry PerfAddressCache::findSymbol(const PerfElfMap::ElfInfo& elf,
                                                                quint64 addr) const
{
    Q_ASSERT(elf.isValid());
    const auto &symbols = m_symbolCache.value(elf.originalPath);
    const auto relAddr = relativeAddress(elf, addr);
    auto it = std::lower_bound(symbols.begin(), symbols.end(), relAddr);

    if (it != symbols.end() && it->offset == relAddr)
        return *it;
    if (it == symbols.begin())
        return {};

    --it;

    if (it->offset <= relAddr && it->offset + it->size > relAddr)
        return *it;
    return {};
}

void PerfAddressCache::cacheSymbol(const PerfElfMap::ElfInfo& elf, quint64 startAddr, quint64 size,
                                   const QByteArray& symname)
{
    Q_ASSERT(elf.isValid());
    auto &symbols = m_symbolCache[elf.originalPath];
    const auto offset = relativeAddress(elf, startAddr);
    auto it = std::lower_bound(symbols.begin(), symbols.end(), offset);
    symbols.insert(it, {offset, size, symname});
}