summaryrefslogtreecommitdiff
path: root/src/mongo/util/stack_introspect.cpp
blob: 7511991c1d2169af7c19f73578391d32fed8d254 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// stack_introspect.cpp

#include "mongo/util/stack_introspect.h"

#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <vector>

#include "mongo/util/concurrency/mutex.h"
#include "mongo/util/text.h"

using namespace std;

#ifdef MONGO_HAVE_EXECINFO_BACKTRACE

#include <execinfo.h>
#include <cxxabi.h>

namespace mongo {
    
    namespace {
    
        int maxBackTraceFrames = 25;

        bool isNameAConstructorOrDesctructor( string name ) {
            //cout << "XX : " << name << endl;
            
            size_t x = name.rfind( '(' );
            if ( name[name.size()-1] != ')' || x == string::npos )
                return false;
            
            name = name.substr( 0 , x );
            
            vector<string> pieces = StringSplitter::split( name , "::" );
            
            if ( pieces.size() < 2 )
                return false;
            
            string method = pieces[pieces.size()-1];
            string clazz = pieces[pieces.size()-2];
            
            if ( method[0] == '~' )
                method = method.substr(1);
            
            if ( name.find( "Geo" ) != string::npos ) 
                return false;
            
            if ( name.find( "Tests" ) != string::npos )
                return false;
            
            return method == clazz;
        }
        
        class Cache {
        public:
            
            Cache() : _mutex( "ObjectLifyCycleCache" ){}
            
            bool inCache( void* name , bool& val ) const {
                SimpleMutex::scoped_lock lk( _mutex );
                map<void*,bool>::const_iterator it = _map.find( name );
                if ( it == _map.end() )
                    return false;
                val = it->second;
                return true;
            }
            
            void set( void* name , bool val ) {
                SimpleMutex::scoped_lock lk( _mutex );
                _map[name] = val;
            }
            
        private:
            map<void*,bool> _map;
            mutable SimpleMutex _mutex;
        };
        
        Cache &cache = *(new Cache());
    }
    
    bool inConstructorChain( bool printOffending ){
        void* b[maxBackTraceFrames];
        int size = ::backtrace( b, maxBackTraceFrames );

        char** strings = 0;
        
        for ( int i = 0; i < size; i++ ) {
            
            {
                bool temp = false;
                if ( cache.inCache( b[i] , temp ) ) {
                    if ( temp )
                        return true;
                    continue;
                }
            }

            if ( ! strings ) 
                strings = ::backtrace_symbols( b, size );

            string symbol = strings[i];

            size_t l = symbol.find( '(' );
            size_t r = symbol.find( '+' );
            if ( l == string::npos || r == string::npos )
                continue;
            symbol = symbol.substr( l + 1 , r-l-1);
            if ( symbol.size() == 0 )
                continue;

            int status = -1;
            char * nice = abi::__cxa_demangle( symbol.c_str() , 0 , 0 , &status );
            if ( status ) 
                continue;
            
            string myNiceCopy = nice;
            
            if ( isNameAConstructorOrDesctructor( nice ) ) {
                if ( printOffending ) 
                    std::cout << "found a constructor in the call tree: " << nice << "\n" << symbol << std::endl;
                ::free( strings );
                ::free( nice );
                cache.set( b[i] , true );
                return true;
            }

            ::free( nice );

            cache.set( b[i] , false );

        }
        ::free( strings );

        return false;
    }

    bool inConstructorChainSupported() {
#if defined(__linux__)
        return true;
#else
        return false;
#endif
    }
}

#else

namespace mongo {   
    bool inConstructorChain( bool ){ return false; }
    bool inConstructorChainSupported() { return false; }
}

#endif  // defined(MONGO_HAVE_EXECINFO_BACKTRACE)