michael@0: // Copyright (c) 2010 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // dump_stabs_unittest.cc: Unit tests for StabsToModule. michael@0: michael@0: #include michael@0: michael@0: #include "breakpad_googletest_includes.h" michael@0: #include "common/stabs_to_module.h" michael@0: michael@0: using google_breakpad::Module; michael@0: using google_breakpad::StabsToModule; michael@0: using std::vector; michael@0: michael@0: TEST(StabsToModule, SimpleCU) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // Feed in a simple compilation unit that defines a function with michael@0: // one line. michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0x9f4d1271e50db93bLL, michael@0: "build-directory")); michael@0: EXPECT_TRUE(h.StartFunction("function", 0xfde4abbed390c394LL)); michael@0: EXPECT_TRUE(h.Line(0xfde4abbed390c394LL, "source-file-name", 174823314)); michael@0: EXPECT_TRUE(h.EndFunction(0xfde4abbed390c3a4LL)); michael@0: EXPECT_TRUE(h.EndCompilationUnit(0xfee4abbed390c3a4LL)); michael@0: h.Finalize(); michael@0: michael@0: // Now check to see what has been added to the Module. michael@0: Module::File *file = m.FindExistingFile("source-file-name"); michael@0: ASSERT_TRUE(file != NULL); michael@0: michael@0: vector functions; michael@0: m.GetFunctions(&functions, functions.end()); michael@0: ASSERT_EQ((size_t) 1, functions.size()); michael@0: Module::Function *function = functions[0]; michael@0: EXPECT_STREQ("function", function->name.c_str()); michael@0: EXPECT_EQ(0xfde4abbed390c394LL, function->address); michael@0: EXPECT_EQ(0x10U, function->size); michael@0: EXPECT_EQ(0U, function->parameter_size); michael@0: ASSERT_EQ((size_t) 1, function->lines.size()); michael@0: Module::Line *line = &function->lines[0]; michael@0: EXPECT_EQ(0xfde4abbed390c394LL, line->address); michael@0: EXPECT_EQ(0x10U, line->size); // derived from EndFunction michael@0: EXPECT_TRUE(line->file == file); michael@0: EXPECT_EQ(174823314, line->number); michael@0: } michael@0: michael@0: #ifdef __GNUC__ michael@0: // Function name mangling can vary by compiler, so only run mangled-name michael@0: // tests on GCC for simplicity's sake. michael@0: TEST(StabsToModule, Externs) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // Feed in a few Extern symbols. michael@0: EXPECT_TRUE(h.Extern("_foo", 0xffff)); michael@0: EXPECT_TRUE(h.Extern("__Z21dyldGlobalLockAcquirev", 0xaaaa)); michael@0: EXPECT_TRUE(h.Extern("_MorphTableGetNextMorphChain", 0x1111)); michael@0: h.Finalize(); michael@0: michael@0: // Now check to see what has been added to the Module. michael@0: vector externs; michael@0: m.GetExterns(&externs, externs.end()); michael@0: ASSERT_EQ((size_t) 3, externs.size()); michael@0: Module::Extern *extern1 = externs[0]; michael@0: EXPECT_STREQ("MorphTableGetNextMorphChain", extern1->name.c_str()); michael@0: EXPECT_EQ((Module::Address)0x1111, extern1->address); michael@0: Module::Extern *extern2 = externs[1]; michael@0: EXPECT_STREQ("dyldGlobalLockAcquire()", extern2->name.c_str()); michael@0: EXPECT_EQ((Module::Address)0xaaaa, extern2->address); michael@0: Module::Extern *extern3 = externs[2]; michael@0: EXPECT_STREQ("foo", extern3->name.c_str()); michael@0: EXPECT_EQ((Module::Address)0xffff, extern3->address); michael@0: } michael@0: #endif // __GNUC__ michael@0: michael@0: TEST(StabsToModule, DuplicateFunctionNames) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // Compilation unit with one function, mangled name. michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda36ecf7f46cLL, michael@0: "build-directory")); michael@0: EXPECT_TRUE(h.StartFunction("funcfoo", michael@0: 0xf2cfda36ecf7f46dLL)); michael@0: EXPECT_TRUE(h.EndFunction(0)); michael@0: EXPECT_TRUE(h.StartFunction("funcfoo", michael@0: 0xf2cfda36ecf7f46dLL)); michael@0: EXPECT_TRUE(h.EndFunction(0)); michael@0: EXPECT_TRUE(h.EndCompilationUnit(0)); michael@0: michael@0: h.Finalize(); michael@0: michael@0: // Now check to see what has been added to the Module. michael@0: Module::File *file = m.FindExistingFile("compilation-unit"); michael@0: ASSERT_TRUE(file != NULL); michael@0: michael@0: vector functions; michael@0: m.GetFunctions(&functions, functions.end()); michael@0: ASSERT_EQ(1U, functions.size()); michael@0: michael@0: Module::Function *function = functions[0]; michael@0: EXPECT_EQ(0xf2cfda36ecf7f46dLL, function->address); michael@0: EXPECT_LT(0U, function->size); // should have used dummy size michael@0: EXPECT_EQ(0U, function->parameter_size); michael@0: ASSERT_EQ(0U, function->lines.size()); michael@0: } michael@0: michael@0: TEST(InferSizes, LineSize) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // Feed in a simple compilation unit that defines a function with michael@0: // one line. michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xb4513962eff94e92LL, michael@0: "build-directory")); michael@0: EXPECT_TRUE(h.StartFunction("function", 0xb4513962eff94e92LL)); michael@0: EXPECT_TRUE(h.Line(0xb4513962eff94e92LL, "source-file-name-1", 77396614)); michael@0: EXPECT_TRUE(h.Line(0xb4513963eff94e92LL, "source-file-name-2", 87660088)); michael@0: EXPECT_TRUE(h.EndFunction(0)); // unknown function end address michael@0: EXPECT_TRUE(h.EndCompilationUnit(0)); // unknown CU end address michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit-2", 0xb4523963eff94e92LL, michael@0: "build-directory-2")); // next boundary michael@0: EXPECT_TRUE(h.EndCompilationUnit(0)); michael@0: h.Finalize(); michael@0: michael@0: // Now check to see what has been added to the Module. michael@0: Module::File *file1 = m.FindExistingFile("source-file-name-1"); michael@0: ASSERT_TRUE(file1 != NULL); michael@0: Module::File *file2 = m.FindExistingFile("source-file-name-2"); michael@0: ASSERT_TRUE(file2 != NULL); michael@0: michael@0: vector functions; michael@0: m.GetFunctions(&functions, functions.end()); michael@0: ASSERT_EQ((size_t) 1, functions.size()); michael@0: michael@0: Module::Function *function = functions[0]; michael@0: EXPECT_STREQ("function", function->name.c_str()); michael@0: EXPECT_EQ(0xb4513962eff94e92LL, function->address); michael@0: EXPECT_EQ(0x1000100000000ULL, function->size); // inferred from CU end michael@0: EXPECT_EQ(0U, function->parameter_size); michael@0: ASSERT_EQ((size_t) 2, function->lines.size()); michael@0: michael@0: Module::Line *line1 = &function->lines[0]; michael@0: EXPECT_EQ(0xb4513962eff94e92LL, line1->address); michael@0: EXPECT_EQ(0x100000000ULL, line1->size); // derived from EndFunction michael@0: EXPECT_TRUE(line1->file == file1); michael@0: EXPECT_EQ(77396614, line1->number); michael@0: michael@0: Module::Line *line2 = &function->lines[1]; michael@0: EXPECT_EQ(0xb4513963eff94e92LL, line2->address); michael@0: EXPECT_EQ(0x1000000000000ULL, line2->size); // derived from EndFunction michael@0: EXPECT_TRUE(line2->file == file2); michael@0: EXPECT_EQ(87660088, line2->number); michael@0: } michael@0: michael@0: #ifdef __GNUC__ michael@0: // Function name mangling can vary by compiler, so only run mangled-name michael@0: // tests on GCC for simplicity's sake. michael@0: TEST(FunctionNames, Mangled) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // Compilation unit with one function, mangled name. michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda63cef7f46cLL, michael@0: "build-directory")); michael@0: EXPECT_TRUE(h.StartFunction("_ZNSt6vectorIySaIyEE9push_backERKy", michael@0: 0xf2cfda63cef7f46dLL)); michael@0: EXPECT_TRUE(h.EndFunction(0)); michael@0: EXPECT_TRUE(h.EndCompilationUnit(0)); michael@0: michael@0: h.Finalize(); michael@0: michael@0: // Now check to see what has been added to the Module. michael@0: Module::File *file = m.FindExistingFile("compilation-unit"); michael@0: ASSERT_TRUE(file != NULL); michael@0: michael@0: vector functions; michael@0: m.GetFunctions(&functions, functions.end()); michael@0: ASSERT_EQ(1U, functions.size()); michael@0: michael@0: Module::Function *function = functions[0]; michael@0: // This is GCC-specific, but we shouldn't be seeing STABS data anywhere michael@0: // but Linux. michael@0: EXPECT_STREQ("std::vector >::" michael@0: "push_back(unsigned long long const&)", michael@0: function->name.c_str()); michael@0: EXPECT_EQ(0xf2cfda63cef7f46dLL, function->address); michael@0: EXPECT_LT(0U, function->size); // should have used dummy size michael@0: EXPECT_EQ(0U, function->parameter_size); michael@0: ASSERT_EQ(0U, function->lines.size()); michael@0: } michael@0: #endif // __GNUC__ michael@0: michael@0: // The GNU toolchain can omit functions that are not used; however, michael@0: // when it does so, it doesn't clean up the debugging information that michael@0: // refers to them. In STABS, this results in compilation units whose michael@0: // SO addresses are zero. michael@0: TEST(Omitted, Function) { michael@0: Module m("name", "os", "arch", "id"); michael@0: StabsToModule h(&m); michael@0: michael@0: // The StartCompilationUnit and EndCompilationUnit calls may both have an michael@0: // address of zero if the compilation unit has had sections removed. michael@0: EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0, "build-directory")); michael@0: EXPECT_TRUE(h.StartFunction("function", 0x2a133596)); michael@0: EXPECT_TRUE(h.EndFunction(0)); michael@0: EXPECT_TRUE(h.EndCompilationUnit(0)); michael@0: } michael@0: michael@0: // TODO --- if we actually cared about STABS. Even without these we've michael@0: // got full coverage of non-failure source lines in dump_stabs.cc. michael@0: michael@0: // Line size from next line michael@0: // Line size from function end michael@0: // Line size from next function start michael@0: // line size from cu end michael@0: // line size from next cu start michael@0: // fallback size is something plausible michael@0: michael@0: // function size from function end michael@0: // function size from next function start michael@0: // function size from cu end michael@0: // function size from next cu start michael@0: // fallback size is something plausible michael@0: michael@0: // omitting functions outside the compilation unit's address range michael@0: // zero-line, one-line, many-line functions