Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3137)

Unified Diff: base/debug_util_posix.cc

Issue 18303: Add StackTrace debugging utility class. (Closed)
Patch Set: Addressing comments Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/debug_util_posix.cc
diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc
index a83e144a853d673b4096de33ab8a4e8b509e7e3c..060f443fb39747461c3f5dfa4ba1c844e5b9fa8f 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,38 @@ 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();
+ fprintf(stderr, "<printing stacktraces not implemented>\n");
Evan Martin 2009/01/16 18:30:08 you won't need this anymore with the above
+}
+
+#endif // defined(OS_MACOSX)
+
+const void *const *StackTrace::Addresses(size_t* count) {
+ *count = trace_.size();
+ if (trace_.size())
+ return &trace_[0];
+ return NULL;
+}
« no previous file with comments | « base/debug_util.h ('k') | base/debug_util_win.cc » ('j') | base/debug_util_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698