diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-16 21:17:30 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-16 21:17:30 +0000 |
commit | 5485cdcb5bc72c71dd67d982dadf76dd9c78328c (patch) | |
tree | 7f3bc0080230211be3adf17777cdf697c91bebb5 /base/debug_util_posix.cc | |
parent | 32a66aed0e57642c8f0da0a4a34383bbc9250a32 (diff) | |
download | chromium_src-5485cdcb5bc72c71dd67d982dadf76dd9c78328c.zip chromium_src-5485cdcb5bc72c71dd67d982dadf76dd9c78328c.tar.gz chromium_src-5485cdcb5bc72c71dd67d982dadf76dd9c78328c.tar.bz2 |
Add StackTrace debugging utility class.
For the moment, it only works on Linux, although it should be pretty easy to
get it working on Mac with __builtin_return_address and __builtin_frame_address.
It will mostly fail to resolve functions. Use this wrapper script:
import os
import sys
import subprocess
import re
address = re.compile('.*\[(0x[0-9a-fA-F]{4,8})\].*')
if __name__ == '__main__':
p = subprocess.Popen(sys.argv[1:], stderr = subprocess.STDOUT, stdout = subprocess.PIPE)
addr2line = subprocess.Popen(['addr2line', '-e', sys.argv[1], '-f', '-C', '-s'],
stdout = subprocess.PIPE, stdin = subprocess.PIPE)
for line in p.stdout.readlines():
m = address.match(line);
if m is not None:
addr2line.stdin.write(m.groups()[0] + '\n')
function = addr2line.stdout.readline()[:-1]
location = addr2line.stdout.readline()[:-1]
sys.stdout.write('%s (%s)\n' % (function, location))
else:
sys.stdout.write(line)
Review URL: http://codereview.chromium.org/18303
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8218 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/debug_util_posix.cc')
-rw-r--r-- | base/debug_util_posix.cc | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc index a83e144..466bb0e 100644 --- a/base/debug_util_posix.cc +++ b/base/debug_util_posix.cc @@ -2,8 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "build/build_config.h" #include "base/debug_util.h" +#if defined(OS_LINUX) +#include <unistd.h> +#include <execinfo.h> +#endif + +#include <stdio.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/sysctl.h> @@ -90,3 +97,37 @@ bool DebugUtil::BeingDebugged() { void DebugUtil::BreakDebugger() { asm ("int3"); } + +#if defined(OS_LINUX) + +StackTrace::StackTrace() { + static const unsigned kMaxCallers = 256; + + void* callers[kMaxCallers]; + int count = backtrace(callers, kMaxCallers); + trace_.resize(count); + memcpy(&trace_[0], callers, sizeof(void*) * count); +} + +void StackTrace::PrintBacktrace() { + fflush(stderr); + backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO); +} + +#elif defined(OS_MACOSX) + +// TODO(port): complete this code +StackTrace::StackTrace() { } + +StackTrace::PrintBacktrace() { + NOTIMPLEMENTED(); +} + +#endif // defined(OS_MACOSX) + +const void *const *StackTrace::Addresses(size_t* count) { + *count = trace_.size(); + if (trace_.size()) + return &trace_[0]; + return NULL; +} |