/thirdparty/breakpad/common/stabs_to_module_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 258 lines · 152 code · 37 blank · 69 comment · 8 complexity · 60a66fabdd4dd8e21ca2848992a3d759 MD5 · raw file

  1. // Copyright (c) 2010 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
  30. // dump_stabs_unittest.cc: Unit tests for StabsToModule.
  31. #include <vector>
  32. #include "breakpad_googletest_includes.h"
  33. #include "common/stabs_to_module.h"
  34. using google_breakpad::Module;
  35. using google_breakpad::StabsToModule;
  36. using std::vector;
  37. TEST(StabsToModule, SimpleCU) {
  38. Module m("name", "os", "arch", "id");
  39. StabsToModule h(&m);
  40. // Feed in a simple compilation unit that defines a function with
  41. // one line.
  42. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0x9f4d1271e50db93bLL,
  43. "build-directory"));
  44. EXPECT_TRUE(h.StartFunction("function", 0xfde4abbed390c394LL));
  45. EXPECT_TRUE(h.Line(0xfde4abbed390c394LL, "source-file-name", 174823314));
  46. EXPECT_TRUE(h.EndFunction(0xfde4abbed390c3a4LL));
  47. EXPECT_TRUE(h.EndCompilationUnit(0xfee4abbed390c3a4LL));
  48. h.Finalize();
  49. // Now check to see what has been added to the Module.
  50. Module::File *file = m.FindExistingFile("source-file-name");
  51. ASSERT_TRUE(file != NULL);
  52. vector<Module::Function *> functions;
  53. m.GetFunctions(&functions, functions.end());
  54. ASSERT_EQ((size_t) 1, functions.size());
  55. Module::Function *function = functions[0];
  56. EXPECT_STREQ("function", function->name.c_str());
  57. EXPECT_EQ(0xfde4abbed390c394LL, function->address);
  58. EXPECT_EQ(0x10U, function->size);
  59. EXPECT_EQ(0U, function->parameter_size);
  60. ASSERT_EQ((size_t) 1, function->lines.size());
  61. Module::Line *line = &function->lines[0];
  62. EXPECT_EQ(0xfde4abbed390c394LL, line->address);
  63. EXPECT_EQ(0x10U, line->size); // derived from EndFunction
  64. EXPECT_TRUE(line->file == file);
  65. EXPECT_EQ(174823314, line->number);
  66. }
  67. #ifdef __GNUC__
  68. // Function name mangling can vary by compiler, so only run mangled-name
  69. // tests on GCC for simplicity's sake.
  70. TEST(StabsToModule, Externs) {
  71. Module m("name", "os", "arch", "id");
  72. StabsToModule h(&m);
  73. // Feed in a few Extern symbols.
  74. EXPECT_TRUE(h.Extern("_foo", 0xffff));
  75. EXPECT_TRUE(h.Extern("__Z21dyldGlobalLockAcquirev", 0xaaaa));
  76. EXPECT_TRUE(h.Extern("_MorphTableGetNextMorphChain", 0x1111));
  77. h.Finalize();
  78. // Now check to see what has been added to the Module.
  79. vector<Module::Extern *> externs;
  80. m.GetExterns(&externs, externs.end());
  81. ASSERT_EQ((size_t) 3, externs.size());
  82. Module::Extern *extern1 = externs[0];
  83. EXPECT_STREQ("MorphTableGetNextMorphChain", extern1->name.c_str());
  84. EXPECT_EQ((Module::Address)0x1111, extern1->address);
  85. Module::Extern *extern2 = externs[1];
  86. EXPECT_STREQ("dyldGlobalLockAcquire()", extern2->name.c_str());
  87. EXPECT_EQ((Module::Address)0xaaaa, extern2->address);
  88. Module::Extern *extern3 = externs[2];
  89. EXPECT_STREQ("foo", extern3->name.c_str());
  90. EXPECT_EQ((Module::Address)0xffff, extern3->address);
  91. }
  92. #endif // __GNUC__
  93. TEST(StabsToModule, DuplicateFunctionNames) {
  94. Module m("name", "os", "arch", "id");
  95. StabsToModule h(&m);
  96. // Compilation unit with one function, mangled name.
  97. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda36ecf7f46cLL,
  98. "build-directory"));
  99. EXPECT_TRUE(h.StartFunction("funcfoo",
  100. 0xf2cfda36ecf7f46dLL));
  101. EXPECT_TRUE(h.EndFunction(0));
  102. EXPECT_TRUE(h.StartFunction("funcfoo",
  103. 0xf2cfda36ecf7f46dLL));
  104. EXPECT_TRUE(h.EndFunction(0));
  105. EXPECT_TRUE(h.EndCompilationUnit(0));
  106. h.Finalize();
  107. // Now check to see what has been added to the Module.
  108. Module::File *file = m.FindExistingFile("compilation-unit");
  109. ASSERT_TRUE(file != NULL);
  110. vector<Module::Function *> functions;
  111. m.GetFunctions(&functions, functions.end());
  112. ASSERT_EQ(1U, functions.size());
  113. Module::Function *function = functions[0];
  114. EXPECT_EQ(0xf2cfda36ecf7f46dLL, function->address);
  115. EXPECT_LT(0U, function->size); // should have used dummy size
  116. EXPECT_EQ(0U, function->parameter_size);
  117. ASSERT_EQ(0U, function->lines.size());
  118. }
  119. TEST(InferSizes, LineSize) {
  120. Module m("name", "os", "arch", "id");
  121. StabsToModule h(&m);
  122. // Feed in a simple compilation unit that defines a function with
  123. // one line.
  124. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xb4513962eff94e92LL,
  125. "build-directory"));
  126. EXPECT_TRUE(h.StartFunction("function", 0xb4513962eff94e92LL));
  127. EXPECT_TRUE(h.Line(0xb4513962eff94e92LL, "source-file-name-1", 77396614));
  128. EXPECT_TRUE(h.Line(0xb4513963eff94e92LL, "source-file-name-2", 87660088));
  129. EXPECT_TRUE(h.EndFunction(0)); // unknown function end address
  130. EXPECT_TRUE(h.EndCompilationUnit(0)); // unknown CU end address
  131. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit-2", 0xb4523963eff94e92LL,
  132. "build-directory-2")); // next boundary
  133. EXPECT_TRUE(h.EndCompilationUnit(0));
  134. h.Finalize();
  135. // Now check to see what has been added to the Module.
  136. Module::File *file1 = m.FindExistingFile("source-file-name-1");
  137. ASSERT_TRUE(file1 != NULL);
  138. Module::File *file2 = m.FindExistingFile("source-file-name-2");
  139. ASSERT_TRUE(file2 != NULL);
  140. vector<Module::Function *> functions;
  141. m.GetFunctions(&functions, functions.end());
  142. ASSERT_EQ((size_t) 1, functions.size());
  143. Module::Function *function = functions[0];
  144. EXPECT_STREQ("function", function->name.c_str());
  145. EXPECT_EQ(0xb4513962eff94e92LL, function->address);
  146. EXPECT_EQ(0x1000100000000ULL, function->size); // inferred from CU end
  147. EXPECT_EQ(0U, function->parameter_size);
  148. ASSERT_EQ((size_t) 2, function->lines.size());
  149. Module::Line *line1 = &function->lines[0];
  150. EXPECT_EQ(0xb4513962eff94e92LL, line1->address);
  151. EXPECT_EQ(0x100000000ULL, line1->size); // derived from EndFunction
  152. EXPECT_TRUE(line1->file == file1);
  153. EXPECT_EQ(77396614, line1->number);
  154. Module::Line *line2 = &function->lines[1];
  155. EXPECT_EQ(0xb4513963eff94e92LL, line2->address);
  156. EXPECT_EQ(0x1000000000000ULL, line2->size); // derived from EndFunction
  157. EXPECT_TRUE(line2->file == file2);
  158. EXPECT_EQ(87660088, line2->number);
  159. }
  160. #ifdef __GNUC__
  161. // Function name mangling can vary by compiler, so only run mangled-name
  162. // tests on GCC for simplicity's sake.
  163. TEST(FunctionNames, Mangled) {
  164. Module m("name", "os", "arch", "id");
  165. StabsToModule h(&m);
  166. // Compilation unit with one function, mangled name.
  167. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda63cef7f46cLL,
  168. "build-directory"));
  169. EXPECT_TRUE(h.StartFunction("_ZNSt6vectorIySaIyEE9push_backERKy",
  170. 0xf2cfda63cef7f46dLL));
  171. EXPECT_TRUE(h.EndFunction(0));
  172. EXPECT_TRUE(h.EndCompilationUnit(0));
  173. h.Finalize();
  174. // Now check to see what has been added to the Module.
  175. Module::File *file = m.FindExistingFile("compilation-unit");
  176. ASSERT_TRUE(file != NULL);
  177. vector<Module::Function *> functions;
  178. m.GetFunctions(&functions, functions.end());
  179. ASSERT_EQ(1U, functions.size());
  180. Module::Function *function = functions[0];
  181. // This is GCC-specific, but we shouldn't be seeing STABS data anywhere
  182. // but Linux.
  183. EXPECT_STREQ("std::vector<unsigned long long, "
  184. "std::allocator<unsigned long long> >::"
  185. "push_back(unsigned long long const&)",
  186. function->name.c_str());
  187. EXPECT_EQ(0xf2cfda63cef7f46dLL, function->address);
  188. EXPECT_LT(0U, function->size); // should have used dummy size
  189. EXPECT_EQ(0U, function->parameter_size);
  190. ASSERT_EQ(0U, function->lines.size());
  191. }
  192. #endif // __GNUC__
  193. // The GNU toolchain can omit functions that are not used; however,
  194. // when it does so, it doesn't clean up the debugging information that
  195. // refers to them. In STABS, this results in compilation units whose
  196. // SO addresses are zero.
  197. TEST(Omitted, Function) {
  198. Module m("name", "os", "arch", "id");
  199. StabsToModule h(&m);
  200. // The StartCompilationUnit and EndCompilationUnit calls may both have an
  201. // address of zero if the compilation unit has had sections removed.
  202. EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0, "build-directory"));
  203. EXPECT_TRUE(h.StartFunction("function", 0x2a133596));
  204. EXPECT_TRUE(h.EndFunction(0));
  205. EXPECT_TRUE(h.EndCompilationUnit(0));
  206. }
  207. // TODO --- if we actually cared about STABS. Even without these we've
  208. // got full coverage of non-failure source lines in dump_stabs.cc.
  209. // Line size from next line
  210. // Line size from function end
  211. // Line size from next function start
  212. // line size from cu end
  213. // line size from next cu start
  214. // fallback size is something plausible
  215. // function size from function end
  216. // function size from next function start
  217. // function size from cu end
  218. // function size from next cu start
  219. // fallback size is something plausible
  220. // omitting functions outside the compilation unit's address range
  221. // zero-line, one-line, many-line functions