summaryrefslogtreecommitdiff
path: root/indra/llcommon/llcallstack.cpp
blob: c0be4f598ead40e064d29a6d953f3ba0101f2bd2 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * @file llcallstack.cpp
 * @brief run-time extraction of the current callstack
 *
 * $LicenseInfo:firstyear=2016&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2016, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llcommon.h"
#include "llcallstack.h"
#include "StackWalker.h"
#include "llthreadlocalstorage.h"

#if LL_WINDOWS
class LLCallStackImpl: public StackWalker
{
public:
    LLCallStackImpl():
        StackWalker(false,0) // non-verbose, options = 0
    {
    }
    ~LLCallStackImpl()
    {
    }
    void getStack(std::vector<std::string>& stack, S32 skip_count=0, bool verbose=false)
    {
        m_stack.clear();
        ShowCallstack(verbose);
        // Skip the first few lines because they're just bookkeeping for LLCallStack,
        // plus any additional lines requested to skip.
        S32 first_line = skip_count + 3;
        for (S32 i=first_line; i<m_stack.size(); ++i)
        {
            stack.push_back(m_stack[i]);
        }
    }
protected:
    virtual void OnOutput(LPCSTR szText)
    {
        m_stack.push_back(szText);
    }
    std::vector<std::string> m_stack;
};
#else
// Stub - not implemented currently on other platforms.
class LLCallStackImpl
{
public:
    LLCallStackImpl() {}
    ~LLCallStackImpl() {}
    void getStack(std::vector<std::string>& stack, S32 skip_count=0, bool verbose=false)
    {
        stack.clear();
    }
};
#endif

LLCallStackImpl *LLCallStack::s_impl = NULL;

LLCallStack::LLCallStack(S32 skip_count, bool verbose):
    m_skipCount(skip_count),
    m_verbose(verbose)
{
    if (!s_impl)
    {
        s_impl = new LLCallStackImpl;
    }
    LLTimer t;
    s_impl->getStack(m_strings, m_skipCount, m_verbose);
}

bool LLCallStack::contains(const std::string& str)
{
    for (const std::string& src_str : m_strings)
    {
        if (src_str.find(str) != std::string::npos)
        {
            return true;
        }
    }
    return false;
}

std::ostream& operator<<(std::ostream& s, const LLCallStack& call_stack)
{
#ifndef LL_RELEASE_FOR_DOWNLOAD
    for (const std::string& str : call_stack.m_strings)
    {
        s << str;
    }
#else
    s << "UNAVAILABLE IN RELEASE";
#endif
    return s;
}

LLContextStrings::LLContextStrings()
{
}

// static
LLContextStrings* LLContextStrings::getThreadLocalInstance()
{
    LLContextStrings *cons = LLThreadLocalSingletonPointer<LLContextStrings>::getInstance();
    if (!cons)
    {
        LLThreadLocalSingletonPointer<LLContextStrings>::setInstance(new LLContextStrings);
    }
    return LLThreadLocalSingletonPointer<LLContextStrings>::getInstance();
}

// static
void LLContextStrings::addContextString(const std::string& str)
{
    LLContextStrings *cons = getThreadLocalInstance();
    //LL_INFOS() << "CTX " << (S32)cons << " ADD " << str << " CNT " << cons->m_contextStrings[str] << LL_ENDL;
    cons->m_contextStrings[str]++;
}

// static
void LLContextStrings::removeContextString(const std::string& str)
{
    LLContextStrings *cons = getThreadLocalInstance();
    cons->m_contextStrings[str]--;
    //LL_INFOS() << "CTX " << (S32)cons << " REMOVE " << str << " CNT " << cons->m_contextStrings[str] << LL_ENDL;
    if (cons->m_contextStrings[str] == 0)
    {
        cons->m_contextStrings.erase(str);
    }
}

// static
bool LLContextStrings::contains(const std::string& str)
{
    const std::map<std::string,S32>& strings =
        LLThreadLocalSingletonPointer<LLContextStrings>::getInstance()->m_contextStrings;
    for (const std::map<std::string,S32>::value_type& str_pair : strings)
    {
        if (str_pair.first.find(str) != std::string::npos)
        {
            return true;
        }
    }
    return false;
}

// static
void LLContextStrings::output(std::ostream& os)
{
    const std::map<std::string,S32>& strings =
        LLThreadLocalSingletonPointer<LLContextStrings>::getInstance()->m_contextStrings;
    for (const std::map<std::string,S32>::value_type& str_pair : strings)
    {
        os << str_pair.first << "[" << str_pair.second << "]" << "\n";
    }
}

// static
std::ostream& operator<<(std::ostream& s, const LLContextStatus& context_status)
{
    LLThreadLocalSingletonPointer<LLContextStrings>::getInstance()->output(s);
    return s;
}

bool LLContextStatus::contains(const std::string& str)
{
    return LLThreadLocalSingletonPointer<LLContextStrings>::getInstance()->contains(str);
}