summaryrefslogtreecommitdiffstats
path: root/app/perfaddresscache.cpp
blob: 46938cfc6c84d707f14bfd4dd2d32d1e4286d5a3 (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
/****************************************************************************
**
** 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"

#include "perfdwarfdiecache.h"

#include <algorithm>

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

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, const PerfAddressCache::SymbolCacheEntry &rhs)
{
    return lhs.offset < rhs.offset;
}

static bool operator==(const PerfAddressCache::SymbolCacheEntry &lhs, const PerfAddressCache::SymbolCacheEntry &rhs)
{
    return lhs.offset == rhs.offset && lhs.size == rhs.size;
}

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


bool PerfAddressCache::hasSymbolCache(const QByteArray &filePath) const
{
    return m_symbolCache.contains(filePath);
}

PerfAddressCache::SymbolCacheEntry PerfAddressCache::findSymbol(const QByteArray& filePath, quint64 relAddr)
{
    auto &symbols = m_symbolCache[filePath];
    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 || (it->size == 0))) {
        // demangle symbols on demand instead of demangling all symbols directly
        // hopefully most of the symbols we won't ever encounter after all
        if (!it->demangled) {
            it->symname = demangle(it->symname);
            it->demangled = true;
        }
        return *it;
    }
    return {};
}

void PerfAddressCache::setSymbolCache(const QByteArray &filePath, SymbolCache cache)
{
    /*
     * use stable_sort to produce results that are comparable to what addr2line would
     * return when we have entries like this in the symtab:
     *
     * 000000000045a130 l     F .text  0000000000000033 .hidden __memmove_avx_unaligned
     * 000000000045a180 l     F .text  00000000000003d8 .hidden __memmove_avx_unaligned_erms
     * 000000000045a180 l     F .text  00000000000003d8 .hidden __memcpy_avx_unaligned_erms
     * 000000000045a130 l     F .text  0000000000000033 .hidden __memcpy_avx_unaligned
     *
     * here, addr2line would always find the first entry. we want to do the same
     */

    std::stable_sort(cache.begin(), cache.end());
    cache.erase(std::unique(cache.begin(), cache.end()), cache.end());
    m_symbolCache[filePath] = cache;
}