]> git.sesse.net Git - casparcg/blob - common/os/windows/stack_trace.cpp
c01d06acbe87624488ef9ec8ff98fd5734fb768e
[casparcg] / common / os / windows / stack_trace.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../../stdafx.h"
23
24 #include "../stack_trace.h"
25 #include "../../utf.h"
26
27 #include "../../compiler/vs/StackWalker.h"
28
29 #include <utility>
30
31 #include <tbb/enumerable_thread_specific.h>
32
33 namespace caspar {
34
35 std::wstring get_call_stack()
36 {
37         class log_call_stack_walker : public StackWalker
38         {
39                 std::string str_;
40         public:
41                 log_call_stack_walker() : StackWalker() {}
42
43                 std::string flush()
44                 {
45                         return std::move(str_);
46                 }
47         protected:
48                 virtual void OnSymInit(LPCSTR szSearchPath, DWORD symOptions, LPCSTR szUserName) override
49                 {
50                 }
51                 virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion) override
52                 {
53                 }
54                 virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr) override
55                 {
56                 }
57                 virtual void OnOutput(LPCSTR szText) override
58                 {
59                         std::string str = szText;
60
61                         if(str.find("caspar::get_call_stack") == std::string::npos && str.find("StackWalker::ShowCallstack") == std::string::npos)
62                                 str_ += std::move(str);
63                 }
64         };
65
66         static tbb::enumerable_thread_specific<log_call_stack_walker> walkers;
67         try
68         {
69                 auto& walker = walkers.local();
70                 walker.ShowCallstack();
71                 return u16(walker.flush());
72         }
73         catch(...)
74         {
75                 return L"!!!";
76         }
77 }
78
79 }