aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/symbol.cpp
blob: d51cc832f1cc591c381839befd5429846a409ec0 (plain) (blame)
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
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "PDB repository" project.
// For details, see https://github.com/egor-tensin/pdb-repo.
// Distributed under the MIT License.

#include "pdb/all.hpp"

#include <boost/nowide/convert.hpp>

#include <SafeInt.hpp>

#include <dbghelp.h>
#include <windows.h>

#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <string>
#include <type_traits>

namespace pdb {
namespace {

std::size_t calc_size(const SymbolInfo::Impl& impl) {
    try {
        static constexpr auto char_size = sizeof(std::remove_extent<decltype(impl.Name)>::type);
        return SafeInt<std::size_t>{impl.SizeOfStruct} + (impl.NameLen - 1) * char_size;
    } catch (const SafeIntException&) {
        throw std::runtime_error{"invalid SYMBOL_INFO size"};
    }
}

unsigned long cast_line_number(DWORD impl) {
    unsigned long dest = 0;

    if (!SafeCast(impl, dest))
        throw std::runtime_error{"invalid line number"};

    return dest;
}

} // namespace

SymbolInfo::SymbolInfo() {
    buffer.fill(0);
    get_impl().SizeOfStruct = sizeof(Impl);
    get_impl().MaxNameLen = MAX_SYM_NAME;
}

SymbolInfo::SymbolInfo(const Impl& impl) : SymbolInfo{} {
    if (impl.SizeOfStruct != sizeof(impl))
        throw std::runtime_error{"invalid SYMBOL_INFO.SizeOfStruct"};
    const auto impl_size = calc_size(impl);
    if (impl_size > buffer.size())
        throw std::runtime_error{"SYMBOL_INFO is too large"};
    std::memcpy(buffer.data(), &impl, impl_size);
}

std::string SymbolInfo::get_name() const {
    // SymFromAddrW, contrary to SymFromAddrA, seems to include the terminating
    // null character in NameLen.
    return boost::nowide::narrow(get_impl().Name);
}

LineInfo::LineInfo(const Impl& impl)
    : file_path{boost::nowide::narrow(impl.FileName)},
      line_number{cast_line_number(impl.LineNumber)} {}

} // namespace pdb