blob: a859bde6137f8d4dd0ba2950b52f0f6c25ae0bad [file] [log] [blame]
nealsidb0baafc2009-08-17 23:12:53 +00001// Copyright (c) 2009, 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
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000030#include <limits.h>
nealsidb0baafc2009-08-17 23:12:53 +000031#include <unistd.h>
nealsidde545c02010-03-02 00:39:48 +000032#include <signal.h>
33#include <sys/types.h>
nealsidb0baafc2009-08-17 23:12:53 +000034
nealsidde545c02010-03-02 00:39:48 +000035#include "breakpad_googletest_includes.h"
nealsidb0baafc2009-08-17 23:12:53 +000036#include "client/linux/minidump_writer/linux_dumper.h"
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000037#include "common/linux/file_id.h"
nealsidde545c02010-03-02 00:39:48 +000038#include "common/linux/memory.h"
nealsidb0baafc2009-08-17 23:12:53 +000039
40using namespace google_breakpad;
41
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +000042// This provides a wrapper around system calls which may be
43// interrupted by a signal and return EINTR. See man 7 signal.
44#define HANDLE_EINTR(x) ({ \
45 typeof(x) __eintr_result__; \
46 do { \
47 __eintr_result__ = x; \
48 } while (__eintr_result__ == -1 && errno == EINTR); \
49 __eintr_result__;\
50})
51
nealsidb0baafc2009-08-17 23:12:53 +000052namespace {
53typedef testing::Test LinuxDumperTest;
54}
55
56TEST(LinuxDumperTest, Setup) {
57 LinuxDumper dumper(getpid());
58}
59
60TEST(LinuxDumperTest, FindMappings) {
61 LinuxDumper dumper(getpid());
62 ASSERT_TRUE(dumper.Init());
63
64 ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(getpid)));
65 ASSERT_TRUE(dumper.FindMapping(reinterpret_cast<void*>(printf)));
66 ASSERT_FALSE(dumper.FindMapping(NULL));
67}
68
69TEST(LinuxDumperTest, ThreadList) {
70 LinuxDumper dumper(getpid());
71 ASSERT_TRUE(dumper.Init());
72
nealsidde545c02010-03-02 00:39:48 +000073 ASSERT_GE(dumper.threads().size(), (size_t)1);
nealsidb0baafc2009-08-17 23:12:53 +000074 bool found = false;
75 for (size_t i = 0; i < dumper.threads().size(); ++i) {
76 if (dumper.threads()[i] == getpid()) {
77 found = true;
78 break;
79 }
80 }
81}
82
Ken Mixter5492dd22010-11-11 15:58:22 -080083// Comment out this test due to crosbug.com/6712. Only seems to
84// fail on heavily loaded buildbots and is written with timing
85// assumptions.
86#if 0
nealsidde545c02010-03-02 00:39:48 +000087TEST(LinuxDumperTest, VerifyStackReadWithMultipleThreads) {
88 static const int kNumberOfThreadsInHelperProgram = 5;
89 char kNumberOfThreadsArgument[2];
90 sprintf(kNumberOfThreadsArgument, "%d", kNumberOfThreadsInHelperProgram);
91
92 pid_t child_pid = fork();
93 if (child_pid == 0) {
94 // Set the number of threads
95 execl("src/client/linux/linux_dumper_unittest_helper",
96 "linux_dumper_unittest_helper",
97 kNumberOfThreadsArgument,
98 NULL);
99 // Kill if we get here.
100 printf("Errno from exec: %d", errno);
101 FAIL() << "Exec failed: " << strerror(errno);
102 exit(0);
103 }
104 // The sleep is flaky, but prevents us from reading
105 // the child process before all threads have been created.
106 sleep(1);
107 LinuxDumper dumper(child_pid);
108 EXPECT_TRUE(dumper.Init());
109 EXPECT_EQ((size_t)kNumberOfThreadsInHelperProgram, dumper.threads().size());
110 EXPECT_TRUE(dumper.ThreadsSuspend());
111
112 ThreadInfo one_thread;
113 for(size_t i = 0; i < dumper.threads().size(); ++i) {
114 EXPECT_TRUE(dumper.ThreadInfoGet(dumper.threads()[i], &one_thread));
jimblandyb68b8002010-03-29 18:23:42 +0000115 // In the helper program, we stored a pointer to the thread id in a
116 // specific register. Check that we can recover its value.
nealsidde545c02010-03-02 00:39:48 +0000117#if defined(__ARM_EABI__)
jimblandyb68b8002010-03-29 18:23:42 +0000118 pid_t *process_tid_location = (pid_t *)(one_thread.regs.uregs[3]);
nealsidde545c02010-03-02 00:39:48 +0000119#elif defined(__i386)
jimblandyb68b8002010-03-29 18:23:42 +0000120 pid_t *process_tid_location = (pid_t *)(one_thread.regs.ecx);
nealsidde545c02010-03-02 00:39:48 +0000121#elif defined(__x86_64)
jimblandyb68b8002010-03-29 18:23:42 +0000122 pid_t *process_tid_location = (pid_t *)(one_thread.regs.rcx);
nealsidde545c02010-03-02 00:39:48 +0000123#else
jimblandyb68b8002010-03-29 18:23:42 +0000124#error This test has not been ported to this platform.
nealsidde545c02010-03-02 00:39:48 +0000125#endif
126 pid_t one_thread_id;
127 dumper.CopyFromProcess(&one_thread_id,
128 dumper.threads()[i],
129 process_tid_location,
130 4);
131 EXPECT_EQ(dumper.threads()[i], one_thread_id);
132 }
133 kill(child_pid, SIGKILL);
134}
Ken Mixter5492dd22010-11-11 15:58:22 -0800135#endif
nealsidde545c02010-03-02 00:39:48 +0000136
nealsidb0baafc2009-08-17 23:12:53 +0000137TEST(LinuxDumperTest, BuildProcPath) {
138 const pid_t pid = getpid();
139 LinuxDumper dumper(pid);
140
141 char maps_path[256] = "dummymappath";
142 char maps_path_expected[256];
143 snprintf(maps_path_expected, sizeof(maps_path_expected),
144 "/proc/%d/maps", pid);
145 dumper.BuildProcPath(maps_path, pid, "maps");
146 ASSERT_STREQ(maps_path, maps_path_expected);
147
148 // In release mode, we expect BuildProcPath to handle the invalid
149 // parameters correctly and fill map_path with an empty
150 // NULL-terminated string.
151#ifdef NDEBUG
152 snprintf(maps_path, sizeof(maps_path), "dummymappath");
153 dumper.BuildProcPath(maps_path, 0, "maps");
154 EXPECT_STREQ(maps_path, "");
155
156 snprintf(maps_path, sizeof(maps_path), "dummymappath");
157 dumper.BuildProcPath(maps_path, getpid(), "");
158 EXPECT_STREQ(maps_path, "");
159
160 snprintf(maps_path, sizeof(maps_path), "dummymappath");
161 dumper.BuildProcPath(maps_path, getpid(), NULL);
162 EXPECT_STREQ(maps_path, "");
163#endif
164}
165
nealsidde545c02010-03-02 00:39:48 +0000166#if !defined(__ARM_EABI__)
nealsidb0baafc2009-08-17 23:12:53 +0000167TEST(LinuxDumperTest, MappingsIncludeLinuxGate) {
168 LinuxDumper dumper(getpid());
169 ASSERT_TRUE(dumper.Init());
170
171 void* linux_gate_loc = dumper.FindBeginningOfLinuxGateSharedLibrary(getpid());
nealsidde545c02010-03-02 00:39:48 +0000172 ASSERT_TRUE(linux_gate_loc);
173 bool found_linux_gate = false;
nealsidb0baafc2009-08-17 23:12:53 +0000174
nealsidde545c02010-03-02 00:39:48 +0000175 const wasteful_vector<MappingInfo*> mappings = dumper.mappings();
176 const MappingInfo* mapping;
177 for (unsigned i = 0; i < mappings.size(); ++i) {
178 mapping = mappings[i];
179 if (!strcmp(mapping->name, kLinuxGateLibraryName)) {
180 found_linux_gate = true;
181 break;
nealsidb0baafc2009-08-17 23:12:53 +0000182 }
nealsidb0baafc2009-08-17 23:12:53 +0000183 }
nealsidde545c02010-03-02 00:39:48 +0000184 EXPECT_TRUE(found_linux_gate);
185 EXPECT_EQ(linux_gate_loc, reinterpret_cast<void*>(mapping->start_addr));
186 EXPECT_EQ(0, memcmp(linux_gate_loc, ELFMAG, SELFMAG));
nealsidb0baafc2009-08-17 23:12:53 +0000187}
nealsidde545c02010-03-02 00:39:48 +0000188#endif
ted.mielczarek0a5fc5d2009-12-23 17:09:27 +0000189
190TEST(LinuxDumperTest, FileIDsMatch) {
191 // Calculate the File ID of our binary using both
192 // FileID::ElfFileIdentifier and LinuxDumper::ElfFileIdentifierForMapping
193 // and ensure that we get the same result from both.
194 char exe_name[PATH_MAX];
195 ssize_t len = readlink("/proc/self/exe", exe_name, PATH_MAX - 1);
196 ASSERT_NE(len, -1);
197 exe_name[len] = '\0';
198
199 int fds[2];
200 ASSERT_NE(-1, pipe(fds));
201
202 // fork a child so we can ptrace it
203 const pid_t child = fork();
204 if (child == 0) {
205 close(fds[1]);
206 // now wait forever for the parent
207 char b;
208 HANDLE_EINTR(read(fds[0], &b, sizeof(b)));
209 close(fds[0]);
210 syscall(__NR_exit);
211 }
212 close(fds[0]);
213
214 LinuxDumper dumper(child);
215 ASSERT_TRUE(dumper.Init());
216 const wasteful_vector<MappingInfo*> mappings = dumper.mappings();
217 bool found_exe = false;
218 unsigned i;
219 for (i = 0; i < mappings.size(); ++i) {
220 const MappingInfo* mapping = mappings[i];
221 if (!strcmp(mapping->name, exe_name)) {
222 found_exe = true;
223 break;
224 }
225 }
226 ASSERT_TRUE(found_exe);
227
228 uint8_t identifier1[sizeof(MDGUID)];
229 uint8_t identifier2[sizeof(MDGUID)];
230 EXPECT_TRUE(dumper.ElfFileIdentifierForMapping(i, identifier1));
231 FileID fileid(exe_name);
232 EXPECT_TRUE(fileid.ElfFileIdentifier(identifier2));
233 char identifier_string1[37];
234 char identifier_string2[37];
235 FileID::ConvertIdentifierToString(identifier1, identifier_string1,
236 37);
237 FileID::ConvertIdentifierToString(identifier2, identifier_string2,
238 37);
239 EXPECT_STREQ(identifier_string1, identifier_string2);
240 close(fds[1]);
241}