File size: 3,365 Bytes
f091ba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef _WIN32

#include "OfflineSymbolResolver.h"

#include <fstream>
#include <iostream>
#include <string>
#include <array>
#include <sstream>
#include <memory>
#include <stdio.h>

std::string ExecShellCommand( const char* cmd )
{
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
    if( !pipe )
    {
        return "";
    }
    while( fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr )
    {
        result += buffer.data();
    }
    return result;
}

class SymbolResolver
{
public:
    SymbolResolver()
    {
        std::stringstream result( ExecShellCommand("which addr2line") );
        std::getline(result, m_addr2LinePath);

        if( !m_addr2LinePath.length() )
        {
            std::cerr << "'addr2line' was not found in the system, please installed it" << std::endl;
        }
        else
        {
            std::cout << "Using 'addr2line' found at: '" << m_addr2LinePath.c_str() << "'" << std::endl;
        }
    }

    bool ResolveSymbols( const std::string& imagePath, const FrameEntryList& inputEntryList,
                         SymbolEntryList& resolvedEntries )
    {
        if (!m_addr2LinePath.length()) return false;

        // generate a single addr2line cmd line for all addresses in one invocation
        std::stringstream ss;
        ss << m_addr2LinePath << " -C -f -e " << imagePath << " -a ";
        for ( const FrameEntry& entry : inputEntryList )
        {
            ss << " 0x" << std::hex << entry.symbolOffset;
        }

        std::string resultStr = ExecShellCommand( ss.str().c_str() );
        std::stringstream result(resultStr);
        //printf("executing: '%s' got '%s'\n", ss.str().c_str(), result.str().c_str());

        // The output is 2 lines per entry with the following contents:
        // hex_address_of_symbol
        // symbol_name
        // file:line

        for( size_t i = 0; i < inputEntryList.size(); ++i )
        {
            const FrameEntry& inputEntry = inputEntryList[i];

            SymbolEntry newEntry;

            std::string addr;
            std::getline( result, addr );
            std::getline( result, newEntry.name );
            if (newEntry.name == "??")
            {
                newEntry.name = "[unknown] + " + std::to_string(inputEntry.symbolOffset);
            }

            std::string fileLine;
            std::getline(result, fileLine);
            if ( fileLine != "??:?" )
            {
                size_t pos = fileLine.find_last_of(':');
                if ( pos != std::string::npos )
                {
                    newEntry.file = fileLine.substr( 0, pos );
                    std::string lineStr = fileLine.substr( pos + 1 );
                    char* after = nullptr;
                    newEntry.line = strtol( lineStr.c_str(), &after, 10 );
                }
            }

            resolvedEntries.push_back( std::move(newEntry) );
        }

        return true;
    }

private:
    std::string m_addr2LinePath;
};

bool ResolveSymbols( const std::string& imagePath, const FrameEntryList& inputEntryList,
                     SymbolEntryList& resolvedEntries )
{
    static SymbolResolver symbolResolver;
    return symbolResolver.ResolveSymbols( imagePath, inputEntryList, resolvedEntries );
}

#endif // #ifndef _WIN32