Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
G
glog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
glog
Commits
e5d36443
Commit
e5d36443
authored
Dec 21, 2016
by
Andrew Schwartzmeyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support stacktrace on Windows
parent
9c1d0e6f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
18 deletions
+57
-18
CMakeLists.txt
CMakeLists.txt
+5
-0
stacktrace_windows-inl.h
src/stacktrace_windows-inl.h
+45
-18
utilities.h
src/utilities.h
+7
-0
No files found.
CMakeLists.txt
View file @
e5d36443
...
...
@@ -455,6 +455,11 @@ if (HAVE_EXECINFO_H)
set
(
HAVE_STACKTRACE 1
)
endif
(
HAVE_EXECINFO_H
)
if
(
WIN32
)
set
(
HAVE_STACKTRACE 1
)
target_link_libraries
(
glog PUBLIC Dbghelp.lib
)
endif
(
WIN32
)
if
(
UNIX
OR
(
APPLE AND HAVE_DLADDR
))
set
(
HAVE_SYMBOLIZE 1
)
endif
(
UNIX
OR
(
APPLE AND HAVE_DLADDR
))
...
...
src/stacktrace_windows-inl.h
View file @
e5d36443
...
...
@@ -27,33 +27,60 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
Portable implementation - just use glibc
//
Author: Andrew Schwartzmeyer
//
// Note: The glibc implementation may cause a call to malloc.
// This can cause a deadlock in HeapProfiler.
#include <execinfo.h>
#include <string.h>
// Windows implementation - just use CaptureStackBackTrace
#include "port.h"
#include "stacktrace.h"
#include "Dbghelp.h"
#include <vector>
_START_GOOGLE_NAMESPACE_
static
bool
ready_to_run
=
false
;
class
StackTraceInit
{
public
:
HANDLE
hProcess
;
StackTraceInit
()
{
// Initialize the symbol handler
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms680344(v=vs.85).aspx
hProcess
=
GetCurrentProcess
();
SymSetOptions
(
SYMOPT_UNDNAME
|
SYMOPT_DEFERRED_LOADS
);
SymInitialize
(
hProcess
,
NULL
,
true
);
ready_to_run
=
true
;
}
~
StackTraceInit
()
{
SymCleanup
(
hProcess
);
ready_to_run
=
false
;
}
};
static
const
StackTraceInit
module_initializer
;
// Force initialization
// If you change this function, also change GetStackFrames below.
int
GetStackTrace
(
void
**
result
,
int
max_depth
,
int
skip_count
)
{
static
const
int
kStackLength
=
64
;
void
*
stack
[
kStackLength
];
int
size
;
size
=
backtrace
(
stack
,
kStackLength
);
if
(
!
ready_to_run
)
{
return
0
;
}
skip_count
++
;
// we want to skip the current frame as well
int
result_count
=
size
-
skip_count
;
if
(
result_count
<
0
)
result_count
=
0
;
if
(
result_count
>
max_depth
)
result_count
=
max_depth
;
for
(
int
i
=
0
;
i
<
result_count
;
i
++
)
result
[
i
]
=
stack
[
i
+
skip_count
];
if
(
max_depth
>
64
)
{
max_depth
=
64
;
}
std
::
vector
<
void
*>
stack
(
max_depth
);
// This API is thread-safe (moreover it walks only the current thread).
int
size
=
CaptureStackBackTrace
(
skip_count
,
max_depth
,
&
stack
[
0
],
NULL
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
// Resolve symbol information from address.
char
buffer
[
sizeof
(
SYMBOL_INFO
)
+
MAX_SYM_NAME
*
sizeof
(
TCHAR
)];
SYMBOL_INFO
*
symbol
=
reinterpret_cast
<
SYMBOL_INFO
*>
(
buffer
);
symbol
->
SizeOfStruct
=
sizeof
(
SYMBOL_INFO
);
symbol
->
MaxNameLen
=
MAX_SYM_NAME
;
SymFromAddr
(
module_initializer
.
hProcess
,
reinterpret_cast
<
DWORD64
>
(
stack
[
i
]),
0
,
symbol
);
result
[
i
]
=
stack
[
i
];
}
return
result_count
;
return
size
;
}
_END_GOOGLE_NAMESPACE_
src/utilities.h
View file @
e5d36443
...
...
@@ -99,6 +99,8 @@
// malloc() from the unwinder. This is a problem because we're
// trying to use the unwinder to instrument malloc().
//
// 4) The Windows API CaptureStackTrace.
//
// Note: if you add a new implementation here, make sure it works
// correctly when GetStackTrace() is called with max_depth == 0.
// Some code may do that.
...
...
@@ -112,6 +114,8 @@
# define STACKTRACE_H "stacktrace_x86_64-inl.h"
# elif (defined(__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
# define STACKTRACE_H "stacktrace_powerpc-inl.h"
# elif defined(OS_WINDOWS)
# define STACKTRACE_H "stacktrace_windows-inl.h"
# endif
#endif
...
...
@@ -143,6 +147,9 @@ namespace glog_internal_namespace_ {
#ifdef HAVE___ATTRIBUTE__
# define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
# define HAVE_ATTRIBUTE_NOINLINE
#elif defined(OS_WINDOWS)
# define ATTRIBUTE_NOINLINE __declspec(noinline)
# define HAVE_ATTRIBUTE_NOINLINE
#else
# define ATTRIBUTE_NOINLINE
#endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment