Merge pull request #120 from ribalda/master
[platform/upstream/glog.git] / src / symbolize_unittest.cc
index 05cb8a1..a0a9737 100644 (file)
@@ -49,10 +49,22 @@ using namespace GFLAGS_NAMESPACE;
 using namespace std;
 using namespace GOOGLE_NAMESPACE;
 
-#if defined(HAVE_STACKTRACE) && defined(__ELF__)
+#if defined(HAVE_STACKTRACE)
 
 #define always_inline
 
+// A wrapper function for Symbolize() to make the unit test simple.
+static const char *TrySymbolize(void *pc) {
+  static char symbol[4096];
+  if (Symbolize(pc, symbol, sizeof(symbol))) {
+    return symbol;
+  } else {
+    return NULL;
+  }
+}
+
+# if defined(__ELF__)
+
 // This unit tests make sense only with GCC.
 // Uses lots of GCC specific features.
 #if defined(__GNUC__) && !defined(__OPENCC__)
@@ -70,16 +82,6 @@ using namespace GOOGLE_NAMESPACE;
 #  endif  // defined(__i386__) || defined(__x86_64__)
 #endif
 
-// A wrapper function for Symbolize() to make the unit test simple.
-static const char *TrySymbolize(void *pc) {
-  static char symbol[4096];
-  if (Symbolize(pc, symbol, sizeof(symbol))) {
-    return symbol;
-  } else {
-    return NULL;
-  }
-}
-
 // Make them C linkage to avoid mangled names.
 extern "C" {
 void nonstatic_func() {
@@ -99,7 +101,13 @@ TEST(Symbolize, Symbolize) {
 
   // Compilers should give us pointers to them.
   EXPECT_STREQ("nonstatic_func", TrySymbolize((void *)(&nonstatic_func)));
-  EXPECT_STREQ("static_func", TrySymbolize((void *)(&static_func)));
+
+  // The name of an internal linkage symbol is not specified; allow either a
+  // mangled or an unmangled name here.
+  const char *static_func_symbol = TrySymbolize((void *)(&static_func));
+  CHECK(NULL != static_func_symbol);
+  EXPECT_TRUE(strcmp("static_func", static_func_symbol) == 0 ||
+              strcmp("static_func()", static_func_symbol) == 0);
 
   EXPECT_TRUE(NULL == TrySymbolize(NULL));
 }
@@ -254,8 +262,13 @@ static const char *SymbolizeStackConsumption(void *pc, int *stack_consumed) {
   return g_symbolize_result;
 }
 
+#ifdef __ppc64__
+// Symbolize stack consumption should be within 4kB.
+const int kStackConsumptionUpperLimit = 4096;
+#else
 // Symbolize stack consumption should be within 2kB.
 const int kStackConsumptionUpperLimit = 2048;
+#endif
 
 TEST(Symbolize, SymbolizeStackConsumption) {
   int stack_consumed;
@@ -267,9 +280,13 @@ TEST(Symbolize, SymbolizeStackConsumption) {
   EXPECT_GT(stack_consumed, 0);
   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
 
+  // The name of an internal linkage symbol is not specified; allow either a
+  // mangled or an unmangled name here.
   symbol = SymbolizeStackConsumption((void *)(&static_func),
                                      &stack_consumed);
-  EXPECT_STREQ("static_func", symbol);
+  CHECK(NULL != symbol);
+  EXPECT_TRUE(strcmp("static_func", symbol) == 0 ||
+              strcmp("static_func()", symbol) == 0);
   EXPECT_GT(stack_consumed, 0);
   EXPECT_LT(stack_consumed, kStackConsumptionUpperLimit);
 }
@@ -340,11 +357,42 @@ void ATTRIBUTE_NOINLINE TestWithReturnAddress() {
 #endif
 }
 
+# elif defined(OS_WINDOWS)
+
+#include <intrin.h>
+#pragma intrinsic(_ReturnAddress)
+
+struct Foo {
+  static void func(int x);
+};
+
+__declspec(noinline) void Foo::func(int x) {
+  volatile int a = x;
+  ++a;
+}
+
+TEST(Symbolize, SymbolizeWithDemangling) {
+  Foo::func(100);
+  const char* ret = TrySymbolize((void *)(&Foo::func));
+  EXPECT_STREQ("public: static void __cdecl Foo::func(int)", ret);
+}
+
+__declspec(noinline) void TestWithReturnAddress() {
+  void *return_address = _ReturnAddress();
+  const char *symbol = TrySymbolize(return_address);
+  CHECK(symbol != NULL);
+  CHECK_STREQ(symbol, "main");
+  cout << "Test case TestWithReturnAddress passed." << endl;
+}
+# endif  // __ELF__
+#endif  // HAVE_STACKTRACE
+
 int main(int argc, char **argv) {
   FLAGS_logtostderr = true;
   InitGoogleLogging(argv[0]);
   InitGoogleTest(&argc, argv);
-#ifdef HAVE_SYMBOLIZE
+#if defined(HAVE_SYMBOLIZE)
+# if defined(__ELF__)
   // We don't want to get affected by the callback interface, that may be
   // used to install some callback function at InitGoogle() time.
   InstallSymbolizeCallback(NULL);
@@ -353,18 +401,15 @@ int main(int argc, char **argv) {
   TestWithPCInsideNonInlineFunction();
   TestWithReturnAddress();
   return RUN_ALL_TESTS();
-#else
-  return 0;
-#endif
-}
-
-#else
-int main() {
-#ifdef HAVE_SYMBOLIZE
+# elif defined(OS_WINDOWS)
+  TestWithReturnAddress();
+  return RUN_ALL_TESTS();
+# else  // OS_WINDOWS
   printf("PASS (no symbolize_unittest support)\n");
+  return 0;
+# endif  // __ELF__
 #else
   printf("PASS (no symbolize support)\n");
-#endif
   return 0;
+#endif  // HAVE_SYMBOLIZE
 }
-#endif  // HAVE_STACKTRACE