From 5485cdcb5bc72c71dd67d982dadf76dd9c78328c Mon Sep 17 00:00:00 2001 From: "agl@chromium.org" Date: Fri, 16 Jan 2009 21:17:30 +0000 Subject: 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 --- base/debug_util_posix.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'base/debug_util_posix.cc') 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 +#include +#endif + +#include #include #include #include @@ -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; +} -- cgit v1.1