Skip to content

Commit

Permalink
[Log] dump stacktrace from glog lib (ray-project#11360)
Browse files Browse the repository at this point in the history
* dump stacktrace from glog lib

* fix windows compile

* add comments for getcallstack
  • Loading branch information
ashione committed Oct 14, 2020
1 parent abc6126 commit 149ec5f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions bazel/ray_deps_setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def ray_deps_setup():
"//thirdparty/patches:glog-log-pid-tid.patch",
"//thirdparty/patches:glog-stack-trace.patch",
"//thirdparty/patches:glog-suffix-log.patch",
"//thirdparty/patches:glog-dump-stacktrack.patch",
],
)

Expand Down
8 changes: 8 additions & 0 deletions src/ray/util/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@

namespace ray {

std::string GetCallTrace() {
std::string return_message = "Cannot get callstack information.";
#ifdef RAY_USE_GLOG
return google::GetStackTraceToString();
#endif
return return_message;
}

#ifdef RAY_USE_GLOG
struct StdoutLogger : public google::base::Logger {
std::ostream &out() { return std::cout; }
Expand Down
5 changes: 5 additions & 0 deletions src/ray/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ enum { ERROR = 0 };
#endif

namespace ray {
/// In order to use the get stacktrace method in other non-glog scenarios, we
/// have added a patch to allow glog to return the current call stack information
/// through the internal interface. This function `GetCallTrace` is a wrapper
/// providing a new detection function for debug or something like that.
std::string GetCallTrace();

enum class RayLogLevel { DEBUG = -1, INFO = 0, WARNING = 1, ERROR = 2, FATAL = 3 };

Expand Down
25 changes: 25 additions & 0 deletions src/ray/util/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ TEST(LogPerfTest, PerfTest) {
RayLog::ShutDownRayLog();
}

std::string TestFunctionLevel0() {
std::string call_trace = GetCallTrace();
RAY_LOG(INFO) << "TestFunctionLevel0\n" << call_trace;
return call_trace;
}

std::string TestFunctionLevel1() {
RAY_LOG(INFO) << "TestFunctionLevel1:";
return TestFunctionLevel0();
}

std::string TestFunctionLevel2() {
RAY_LOG(INFO) << "TestFunctionLevel2:";
return TestFunctionLevel1();
}

TEST(PrintLogTest, CallstackTraceTest) {
auto ret0 = TestFunctionLevel0();
EXPECT_TRUE(ret0.find("TestFunctionLevel0") != std::string::npos);
auto ret1 = TestFunctionLevel1();
EXPECT_TRUE(ret1.find("TestFunctionLevel1") != std::string::npos);
auto ret2 = TestFunctionLevel2();
EXPECT_TRUE(ret2.find("TestFunctionLevel2") != std::string::npos);
}

} // namespace ray

int main(int argc, char **argv) {
Expand Down
74 changes: 74 additions & 0 deletions thirdparty/patches/glog-dump-stacktrack.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
diff --git src/glog/logging.h.in src/glog/logging.h.in
index 64bed1a..0ff073f 100644
--- src/glog/logging.h.in
+++ src/glog/logging.h.in
@@ -1676,6 +1676,9 @@ class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
// words, stack traces of other threads won't be shown.
GOOGLE_GLOG_DLL_DECL void InstallFailureSignalHandler();

+// Providing stack trace function for upper level.
+GOOGLE_GLOG_DLL_DECL std::string GetStackTraceToString();
+
// Installs a function that is used for writing the failure dump. "data"
// is the pointer to the beginning of a message to be written, and "size"
// is the size of the message. You should not expect the data is
diff --git src/logging.cc src/logging.cc
index 733ce8c..e988eb9 100644
--- src/logging.cc
+++ src/logging.cc
@@ -2336,4 +2336,13 @@ void DisableLogCleaner() {
log_cleaner_enabled_ = false;
}

+std::string GetStackTraceToString() {
+ // Collect stacktrace from utilities.
+ std::string stacktrace;
+ // The second parameter stands for skip count. The top stack frames will be
+ // skipped to dump from stack if it's non-zero.
+ DumpStackTraceToString(&stacktrace, 0);
+ return stacktrace;
+}
+
_END_GOOGLE_NAMESPACE_
diff --git src/utilities.cc src/utilities.cc
index 9a1e35d..3808162 100644
--- src/utilities.cc
+++ src/utilities.cc
@@ -318,8 +318,8 @@ static void MyUserNameInitializer() {
REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());

#ifdef HAVE_STACKTRACE
-void DumpStackTraceToString(string* stacktrace) {
- DumpStackTrace(1, DebugWriteToString, stacktrace);
+void DumpStackTraceToString(string* stacktrace, int depth) {
+ DumpStackTrace(depth, DebugWriteToString, stacktrace);
}
#endif

diff --git src/utilities.h src/utilities.h
index c66f914..39c48e8 100644
--- src/utilities.h
+++ src/utilities.h
@@ -209,7 +209,7 @@ inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
#endif
}

-void DumpStackTraceToString(std::string* stacktrace);
+void DumpStackTraceToString(std::string* stacktrace, int depth = 1);

struct CrashReason {
CrashReason() : filename(0), line_number(0), message(0), depth(0) {}
diff --git src/windows/glog/logging.h src/windows/glog/logging.h
index 2a739f9..4ff2a79 100755
--- src/windows/glog/logging.h
+++ src/windows/glog/logging.h
@@ -1677,6 +1677,9 @@ class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
// words, stack traces of other threads won't be shown.
GOOGLE_GLOG_DLL_DECL void InstallFailureSignalHandler();

+// Providing stack trace function for upper level.
+GOOGLE_GLOG_DLL_DECL std::string GetStackTraceToString();
+
// Installs a function that is used for writing the failure dump. "data"
// is the pointer to the beginning of a message to be written, and "size"
// is the size of the message. You should not expect the data is

0 comments on commit 149ec5f

Please sign in to comment.