thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2011 Google Inc. |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 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 | |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 31 | #include <sys/poll.h> |
| 32 | #include <sys/stat.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 33 | #include <sys/syscall.h> |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <string> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 38 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 39 | #include "breakpad_googletest_includes.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 40 | #include "client/linux/handler/exception_handler.h" |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 41 | #include "client/linux/minidump_writer/linux_dumper.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 42 | #include "client/linux/minidump_writer/minidump_writer.h" |
ted.mielczarek | f480ba1 | 2010-02-05 18:21:31 +0000 | [diff] [blame] | 43 | #include "common/linux/eintr_wrapper.h" |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 44 | #include "common/linux/file_id.h" |
benchan@chromium.org | c9fb1f6 | 2011-12-21 17:51:40 +0000 | [diff] [blame] | 45 | #include "common/linux/safe_readlink.h" |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 46 | #include "common/tests/auto_tempdir.h" |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 47 | #include "google_breakpad/processor/minidump.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 48 | |
| 49 | using namespace google_breakpad; |
| 50 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 51 | // Length of a formatted GUID string = |
| 52 | // sizeof(MDGUID) * 2 + 4 (for dashes) + 1 (null terminator) |
| 53 | const int kGUIDStringSize = 37; |
| 54 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | typedef testing::Test MinidumpWriterTest; |
| 57 | } |
| 58 | |
| 59 | TEST(MinidumpWriterTest, Setup) { |
| 60 | int fds[2]; |
| 61 | ASSERT_NE(-1, pipe(fds)); |
| 62 | |
| 63 | const pid_t child = fork(); |
| 64 | if (child == 0) { |
| 65 | close(fds[1]); |
| 66 | char b; |
| 67 | HANDLE_EINTR(read(fds[0], &b, sizeof(b))); |
| 68 | close(fds[0]); |
| 69 | syscall(__NR_exit); |
| 70 | } |
| 71 | close(fds[0]); |
| 72 | |
| 73 | ExceptionHandler::CrashContext context; |
| 74 | memset(&context, 0, sizeof(context)); |
| 75 | |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 76 | AutoTempDir temp_dir; |
| 77 | std::string templ = temp_dir.path() + "/minidump-writer-unittest"; |
kmixter@chromium.org | b5dfa28 | 2010-12-08 22:26:20 +0000 | [diff] [blame] | 78 | // Set a non-zero tid to avoid tripping asserts. |
| 79 | context.tid = 1; |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 80 | ASSERT_TRUE(WriteMinidump(templ.c_str(), child, &context, sizeof(context))); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 81 | struct stat st; |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 82 | ASSERT_EQ(stat(templ.c_str(), &st), 0); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 83 | ASSERT_GT(st.st_size, 0u); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 84 | |
| 85 | close(fds[1]); |
| 86 | } |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 87 | |
| 88 | // Test that mapping info can be specified when writing a minidump, |
| 89 | // and that it ends up in the module list of the minidump. |
| 90 | TEST(MinidumpWriterTest, MappingInfo) { |
| 91 | int fds[2]; |
| 92 | ASSERT_NE(-1, pipe(fds)); |
| 93 | |
| 94 | // These are defined here so the parent can use them to check the |
| 95 | // data from the minidump afterwards. |
| 96 | const u_int32_t kMemorySize = sysconf(_SC_PAGESIZE); |
| 97 | const char* kMemoryName = "a fake module"; |
| 98 | const u_int8_t kModuleGUID[sizeof(MDGUID)] = { |
| 99 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 100 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF |
| 101 | }; |
| 102 | char module_identifier_buffer[kGUIDStringSize]; |
| 103 | FileID::ConvertIdentifierToString(kModuleGUID, |
| 104 | module_identifier_buffer, |
| 105 | sizeof(module_identifier_buffer)); |
| 106 | string module_identifier(module_identifier_buffer); |
| 107 | // Strip out dashes |
| 108 | size_t pos; |
| 109 | while ((pos = module_identifier.find('-')) != string::npos) { |
| 110 | module_identifier.erase(pos, 1); |
| 111 | } |
| 112 | // And append a zero, because module IDs include an "age" field |
| 113 | // which is always zero on Linux. |
| 114 | module_identifier += "0"; |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 115 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 116 | // Get some memory. |
| 117 | char* memory = |
| 118 | reinterpret_cast<char*>(mmap(NULL, |
| 119 | kMemorySize, |
| 120 | PROT_READ | PROT_WRITE, |
| 121 | MAP_PRIVATE | MAP_ANON, |
| 122 | -1, |
| 123 | 0)); |
Michael Krebs | 0efd915 | 2012-01-25 19:11:10 -0800 | [diff] [blame^] | 124 | const uintptr_t kMemoryAddress = reinterpret_cast<uintptr_t>(memory); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 125 | ASSERT_TRUE(memory); |
| 126 | |
| 127 | const pid_t child = fork(); |
| 128 | if (child == 0) { |
| 129 | close(fds[1]); |
| 130 | char b; |
| 131 | HANDLE_EINTR(read(fds[0], &b, sizeof(b))); |
| 132 | close(fds[0]); |
| 133 | syscall(__NR_exit); |
| 134 | } |
| 135 | close(fds[0]); |
| 136 | |
| 137 | ExceptionHandler::CrashContext context; |
| 138 | memset(&context, 0, sizeof(context)); |
| 139 | context.tid = 1; |
| 140 | |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 141 | AutoTempDir temp_dir; |
| 142 | std::string templ = temp_dir.path() + "/minidump-writer-unittest"; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 143 | |
| 144 | // Add information about the mapped memory. |
| 145 | MappingInfo info; |
| 146 | info.start_addr = kMemoryAddress; |
| 147 | info.size = kMemorySize; |
| 148 | info.offset = 0; |
| 149 | strcpy(info.name, kMemoryName); |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 150 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 151 | MappingList mappings; |
| 152 | MappingEntry mapping; |
| 153 | mapping.first = info; |
| 154 | memcpy(mapping.second, kModuleGUID, sizeof(MDGUID)); |
| 155 | mappings.push_back(mapping); |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 156 | ASSERT_TRUE(WriteMinidump(templ.c_str(), child, &context, sizeof(context), |
| 157 | mappings)); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 158 | |
| 159 | // Read the minidump. Load the module list, and ensure that |
| 160 | // the mmap'ed |memory| is listed with the given module name |
| 161 | // and debug ID. |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 162 | Minidump minidump(templ.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 163 | ASSERT_TRUE(minidump.Read()); |
| 164 | |
| 165 | MinidumpModuleList* module_list = minidump.GetModuleList(); |
| 166 | ASSERT_TRUE(module_list); |
| 167 | const MinidumpModule* module = |
| 168 | module_list->GetModuleForAddress(kMemoryAddress); |
| 169 | ASSERT_TRUE(module); |
| 170 | |
| 171 | EXPECT_EQ(kMemoryAddress, module->base_address()); |
| 172 | EXPECT_EQ(kMemorySize, module->size()); |
| 173 | EXPECT_EQ(kMemoryName, module->code_file()); |
| 174 | EXPECT_EQ(module_identifier, module->debug_identifier()); |
| 175 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 176 | close(fds[1]); |
| 177 | } |
| 178 | |
| 179 | // Test that mapping info can be specified, and that it overrides |
| 180 | // existing mappings that are wholly contained within the specified |
| 181 | // range. |
| 182 | TEST(MinidumpWriterTest, MappingInfoContained) { |
| 183 | int fds[2]; |
| 184 | ASSERT_NE(-1, pipe(fds)); |
| 185 | |
| 186 | // These are defined here so the parent can use them to check the |
| 187 | // data from the minidump afterwards. |
| 188 | const u_int32_t kMemorySize = sysconf(_SC_PAGESIZE); |
| 189 | const char* kMemoryName = "a fake module"; |
| 190 | const u_int8_t kModuleGUID[sizeof(MDGUID)] = { |
| 191 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 192 | 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF |
| 193 | }; |
| 194 | char module_identifier_buffer[kGUIDStringSize]; |
| 195 | FileID::ConvertIdentifierToString(kModuleGUID, |
| 196 | module_identifier_buffer, |
| 197 | sizeof(module_identifier_buffer)); |
| 198 | string module_identifier(module_identifier_buffer); |
| 199 | // Strip out dashes |
| 200 | size_t pos; |
| 201 | while ((pos = module_identifier.find('-')) != string::npos) { |
| 202 | module_identifier.erase(pos, 1); |
| 203 | } |
| 204 | // And append a zero, because module IDs include an "age" field |
| 205 | // which is always zero on Linux. |
| 206 | module_identifier += "0"; |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 207 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 208 | // mmap a file |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 209 | AutoTempDir temp_dir; |
| 210 | std::string tempfile = temp_dir.path() + "/minidump-writer-unittest-temp"; |
| 211 | int fd = open(tempfile.c_str(), O_RDWR | O_CREAT, 0); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 212 | ASSERT_NE(-1, fd); |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 213 | unlink(tempfile.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 214 | // fill with zeros |
| 215 | char buffer[kMemorySize]; |
| 216 | memset(buffer, 0, kMemorySize); |
| 217 | ASSERT_EQ(kMemorySize, write(fd, buffer, kMemorySize)); |
| 218 | lseek(fd, 0, SEEK_SET); |
| 219 | |
| 220 | char* memory = |
| 221 | reinterpret_cast<char*>(mmap(NULL, |
| 222 | kMemorySize, |
| 223 | PROT_READ | PROT_WRITE, |
| 224 | MAP_PRIVATE, |
| 225 | fd, |
| 226 | 0)); |
Michael Krebs | 0efd915 | 2012-01-25 19:11:10 -0800 | [diff] [blame^] | 227 | const uintptr_t kMemoryAddress = reinterpret_cast<uintptr_t>(memory); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 228 | ASSERT_TRUE(memory); |
| 229 | close(fd); |
| 230 | |
| 231 | const pid_t child = fork(); |
| 232 | if (child == 0) { |
| 233 | close(fds[1]); |
| 234 | char b; |
| 235 | HANDLE_EINTR(read(fds[0], &b, sizeof(b))); |
| 236 | close(fds[0]); |
| 237 | syscall(__NR_exit); |
| 238 | } |
| 239 | close(fds[0]); |
| 240 | |
| 241 | ExceptionHandler::CrashContext context; |
| 242 | memset(&context, 0, sizeof(context)); |
| 243 | context.tid = 1; |
| 244 | |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 245 | std::string dumpfile = temp_dir.path() + "/minidump-writer-unittest"; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 246 | |
| 247 | // Add information about the mapped memory. Report it as being larger than |
| 248 | // it actually is. |
| 249 | MappingInfo info; |
| 250 | info.start_addr = kMemoryAddress - kMemorySize; |
| 251 | info.size = kMemorySize * 3; |
| 252 | info.offset = 0; |
| 253 | strcpy(info.name, kMemoryName); |
| 254 | |
| 255 | MappingList mappings; |
| 256 | MappingEntry mapping; |
| 257 | mapping.first = info; |
| 258 | memcpy(mapping.second, kModuleGUID, sizeof(MDGUID)); |
| 259 | mappings.push_back(mapping); |
thestig@chromium.org | 972ae49 | 2011-01-12 00:14:27 +0000 | [diff] [blame] | 260 | ASSERT_TRUE( |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 261 | WriteMinidump(dumpfile.c_str(), child, &context, sizeof(context), |
| 262 | mappings)); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 263 | |
| 264 | // Read the minidump. Load the module list, and ensure that |
| 265 | // the mmap'ed |memory| is listed with the given module name |
| 266 | // and debug ID. |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 267 | Minidump minidump(dumpfile.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 268 | ASSERT_TRUE(minidump.Read()); |
| 269 | |
| 270 | MinidumpModuleList* module_list = minidump.GetModuleList(); |
| 271 | ASSERT_TRUE(module_list); |
| 272 | const MinidumpModule* module = |
| 273 | module_list->GetModuleForAddress(kMemoryAddress); |
| 274 | ASSERT_TRUE(module); |
| 275 | |
| 276 | EXPECT_EQ(info.start_addr, module->base_address()); |
| 277 | EXPECT_EQ(info.size, module->size()); |
| 278 | EXPECT_EQ(kMemoryName, module->code_file()); |
| 279 | EXPECT_EQ(module_identifier, module->debug_identifier()); |
| 280 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 281 | close(fds[1]); |
| 282 | } |
| 283 | |
| 284 | TEST(MinidumpWriterTest, DeletedBinary) { |
| 285 | static const int kNumberOfThreadsInHelperProgram = 1; |
| 286 | char kNumberOfThreadsArgument[2]; |
| 287 | sprintf(kNumberOfThreadsArgument, "%d", kNumberOfThreadsInHelperProgram); |
| 288 | |
| 289 | // Locate helper binary next to the current binary. |
| 290 | char self_path[PATH_MAX]; |
benchan@chromium.org | c9fb1f6 | 2011-12-21 17:51:40 +0000 | [diff] [blame] | 291 | if (!SafeReadLink("/proc/self/exe", self_path)) { |
| 292 | FAIL() << "readlink failed"; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 293 | exit(1); |
| 294 | } |
| 295 | string helper_path(self_path); |
| 296 | size_t pos = helper_path.rfind('/'); |
| 297 | if (pos == string::npos) { |
| 298 | FAIL() << "no trailing slash in path: " << helper_path; |
| 299 | exit(1); |
| 300 | } |
| 301 | helper_path.erase(pos + 1); |
| 302 | helper_path += "linux_dumper_unittest_helper"; |
| 303 | |
| 304 | // Copy binary to a temp file. |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 305 | AutoTempDir temp_dir; |
| 306 | std::string binpath = temp_dir.path() + "/linux-dumper-unittest-helper"; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 307 | char cmdline[2 * PATH_MAX]; |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 308 | sprintf(cmdline, "/bin/cp \"%s\" \"%s\"", helper_path.c_str(), |
| 309 | binpath.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 310 | ASSERT_EQ(0, system(cmdline)); |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 311 | ASSERT_EQ(0, chmod(binpath.c_str(), 0755)); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 312 | |
| 313 | int fds[2]; |
| 314 | ASSERT_NE(-1, pipe(fds)); |
| 315 | |
| 316 | pid_t child_pid = fork(); |
| 317 | if (child_pid == 0) { |
| 318 | // In child process. |
| 319 | close(fds[0]); |
| 320 | |
| 321 | // Pass the pipe fd and the number of threads as arguments. |
| 322 | char pipe_fd_string[8]; |
| 323 | sprintf(pipe_fd_string, "%d", fds[1]); |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 324 | execl(binpath.c_str(), |
| 325 | binpath.c_str(), |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 326 | pipe_fd_string, |
| 327 | kNumberOfThreadsArgument, |
| 328 | NULL); |
| 329 | } |
| 330 | close(fds[1]); |
| 331 | // Wait for the child process to signal that it's ready. |
| 332 | struct pollfd pfd; |
| 333 | memset(&pfd, 0, sizeof(pfd)); |
| 334 | pfd.fd = fds[0]; |
| 335 | pfd.events = POLLIN | POLLERR; |
| 336 | |
| 337 | const int r = HANDLE_EINTR(poll(&pfd, 1, 1000)); |
| 338 | ASSERT_EQ(1, r); |
| 339 | ASSERT_TRUE(pfd.revents & POLLIN); |
| 340 | uint8_t junk; |
| 341 | read(fds[0], &junk, sizeof(junk)); |
| 342 | close(fds[0]); |
| 343 | |
| 344 | // Child is ready now. |
| 345 | // Unlink the test binary. |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 346 | unlink(binpath.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 347 | |
| 348 | ExceptionHandler::CrashContext context; |
| 349 | memset(&context, 0, sizeof(context)); |
| 350 | |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 351 | std::string templ = temp_dir.path() + "/minidump-writer-unittest"; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 352 | // Set a non-zero tid to avoid tripping asserts. |
| 353 | context.tid = 1; |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 354 | ASSERT_TRUE(WriteMinidump(templ.c_str(), child_pid, &context, |
| 355 | sizeof(context))); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 356 | kill(child_pid, SIGKILL); |
| 357 | |
| 358 | struct stat st; |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 359 | ASSERT_EQ(stat(templ.c_str(), &st), 0); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 360 | ASSERT_GT(st.st_size, 0u); |
| 361 | |
| 362 | |
| 363 | |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 364 | Minidump minidump(templ.c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 365 | ASSERT_TRUE(minidump.Read()); |
| 366 | |
| 367 | // Check that the main module filename is correct. |
| 368 | MinidumpModuleList* module_list = minidump.GetModuleList(); |
| 369 | ASSERT_TRUE(module_list); |
| 370 | const MinidumpModule* module = module_list->GetMainModule(); |
qsr@chromium.org | b12089f | 2011-11-23 14:22:05 +0000 | [diff] [blame] | 371 | EXPECT_STREQ(binpath.c_str(), module->code_file().c_str()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 372 | // Check that the file ID is correct. |
| 373 | FileID fileid(helper_path.c_str()); |
| 374 | uint8_t identifier[sizeof(MDGUID)]; |
| 375 | EXPECT_TRUE(fileid.ElfFileIdentifier(identifier)); |
| 376 | char identifier_string[kGUIDStringSize]; |
| 377 | FileID::ConvertIdentifierToString(identifier, |
| 378 | identifier_string, |
| 379 | kGUIDStringSize); |
| 380 | string module_identifier(identifier_string); |
| 381 | // Strip out dashes |
| 382 | while ((pos = module_identifier.find('-')) != string::npos) { |
| 383 | module_identifier.erase(pos, 1); |
| 384 | } |
| 385 | // And append a zero, because module IDs include an "age" field |
| 386 | // which is always zero on Linux. |
| 387 | module_identifier += "0"; |
| 388 | EXPECT_EQ(module_identifier, module->debug_identifier()); |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 389 | } |