1 // Copyright (c) 2012, 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
30 // linux_core_dumper_unittest.cc:
31 // Unit tests for google_breakpad::LinuxCoreDumoer.
32
33 #include <string>
34
35 #include "breakpad_googletest_includes.h"
36 #include "client/linux/minidump_writer/linux_core_dumper.h"
37 #include "common/linux/tests/crash_generator.h"
38 #include "common/using_std_string.h"
39
40 using namespace google_breakpad;
41
TEST(LinuxCoreDumperTest,BuildProcPath)42 TEST(LinuxCoreDumperTest, BuildProcPath) {
43 const pid_t pid = getpid();
44 const char procfs_path[] = "/procfs_copy";
45 LinuxCoreDumper dumper(getpid(), "core_file", procfs_path);
46
47 char maps_path[NAME_MAX] = "";
48 char maps_path_expected[NAME_MAX];
49 snprintf(maps_path_expected, sizeof(maps_path_expected),
50 "%s/maps", procfs_path);
51 EXPECT_TRUE(dumper.BuildProcPath(maps_path, pid, "maps"));
52 EXPECT_STREQ(maps_path_expected, maps_path);
53
54 EXPECT_FALSE(dumper.BuildProcPath(NULL, pid, "maps"));
55 EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, ""));
56 EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, NULL));
57
58 char long_node[NAME_MAX];
59 size_t long_node_len = NAME_MAX - strlen(procfs_path) - 1;
60 memset(long_node, 'a', long_node_len);
61 long_node[long_node_len] = '\0';
62 EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, long_node));
63 }
64
TEST(LinuxCoreDumperTest,VerifyDumpWithMultipleThreads)65 TEST(LinuxCoreDumperTest, VerifyDumpWithMultipleThreads) {
66 CrashGenerator crash_generator;
67 if (!crash_generator.HasDefaultCorePattern()) {
68 fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test "
69 "is skipped due to non-default core pattern\n");
70 return;
71 }
72
73 const unsigned kNumOfThreads = 3;
74 const unsigned kCrashThread = 1;
75 const int kCrashSignal = SIGABRT;
76 pid_t child_pid;
77 ASSERT_TRUE(crash_generator.CreateChildCrash(kNumOfThreads, kCrashThread,
78 kCrashSignal, &child_pid));
79
80 const string core_file = crash_generator.GetCoreFilePath();
81 const string procfs_path = crash_generator.GetDirectoryOfProcFilesCopy();
82
83 #if defined(__ANDROID__)
84 struct stat st;
85 if (stat(core_file.c_str(), &st) != 0) {
86 fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test is "
87 "skipped due to no core file being generated");
88 return;
89 }
90 #endif
91
92 LinuxCoreDumper dumper(child_pid, core_file.c_str(), procfs_path.c_str());
93
94 EXPECT_TRUE(dumper.Init());
95
96 EXPECT_TRUE(dumper.IsPostMortem());
97
98 // These are no-ops and should always return true.
99 EXPECT_TRUE(dumper.ThreadsSuspend());
100 EXPECT_TRUE(dumper.ThreadsResume());
101
102 // LinuxCoreDumper cannot determine the crash address and thus it always
103 // sets the crash address to 0.
104 EXPECT_EQ(0U, dumper.crash_address());
105 EXPECT_EQ(kCrashSignal, dumper.crash_signal());
106 EXPECT_EQ(crash_generator.GetThreadId(kCrashThread),
107 dumper.crash_thread());
108
109 EXPECT_EQ(kNumOfThreads, dumper.threads().size());
110 for (unsigned i = 0; i < kNumOfThreads; ++i) {
111 ThreadInfo info;
112 EXPECT_TRUE(dumper.GetThreadInfoByIndex(i, &info));
113 const void* stack;
114 size_t stack_len;
115 EXPECT_TRUE(dumper.GetStackInfo(&stack, &stack_len, info.stack_pointer));
116 EXPECT_EQ(getpid(), info.ppid);
117 }
118 }
119