thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 1 | // Copyright (c) 2010, 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 | |
| 30 | // This code deals with the mechanics of getting information about a crashed |
| 31 | // process. Since this code may run in a compromised address space, the same |
| 32 | // rules apply as detailed at the top of minidump_writer.h: no libc calls and |
| 33 | // use the alternative allocator. |
| 34 | |
| 35 | #include "client/linux/minidump_writer/linux_dumper.h" |
| 36 | |
ted.mielczarek | 4337826 | 2010-10-21 13:55:07 +0000 | [diff] [blame] | 37 | #include <asm/ptrace.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 38 | #include <assert.h> |
ted.mielczarek | 4337826 | 2010-10-21 13:55:07 +0000 | [diff] [blame] | 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 41 | #include <limits.h> |
ted.mielczarek | 4337826 | 2010-10-21 13:55:07 +0000 | [diff] [blame] | 42 | #if !defined(__ANDROID__) |
| 43 | #include <link.h> |
| 44 | #endif |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 45 | #include <stddef.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <stdio.h> |
| 48 | #include <string.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 49 | #include <sys/ptrace.h> |
| 50 | #include <sys/wait.h> |
ted.mielczarek | 4337826 | 2010-10-21 13:55:07 +0000 | [diff] [blame] | 51 | #include <unistd.h> |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 52 | |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 53 | #include <algorithm> |
| 54 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 55 | #include "client/linux/minidump_writer/directory_reader.h" |
| 56 | #include "client/linux/minidump_writer/line_reader.h" |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 57 | #include "common/linux/file_id.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 58 | #include "common/linux/linux_libc_support.h" |
thestig@chromium.org | 0e3b702 | 2010-09-15 22:31:57 +0000 | [diff] [blame] | 59 | #include "third_party/lss/linux_syscall_support.h" |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 60 | |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 61 | static const char kMappedFileUnsafePrefix[] = "/dev/"; |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 62 | static const char kDeletedSuffix[] = " (deleted)"; |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 63 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 64 | // Suspend a thread by attaching to it. |
| 65 | static bool SuspendThread(pid_t pid) { |
| 66 | // This may fail if the thread has just died or debugged. |
| 67 | errno = 0; |
| 68 | if (sys_ptrace(PTRACE_ATTACH, pid, NULL, NULL) != 0 && |
| 69 | errno != 0) { |
| 70 | return false; |
| 71 | } |
| 72 | while (sys_waitpid(pid, NULL, __WALL) < 0) { |
| 73 | if (errno != EINTR) { |
| 74 | sys_ptrace(PTRACE_DETACH, pid, NULL, NULL); |
| 75 | return false; |
| 76 | } |
| 77 | } |
thestig@chromium.org | f5c8f6f | 2010-08-14 01:41:39 +0000 | [diff] [blame] | 78 | #if defined(__i386) || defined(__x86_64) |
| 79 | // On x86, the stack pointer is NULL or -1, when executing trusted code in |
| 80 | // the seccomp sandbox. Not only does this cause difficulties down the line |
| 81 | // when trying to dump the thread's stack, it also results in the minidumps |
| 82 | // containing information about the trusted threads. This information is |
| 83 | // generally completely meaningless and just pollutes the minidumps. |
| 84 | // We thus test the stack pointer and exclude any threads that are part of |
| 85 | // the seccomp sandbox's trusted code. |
| 86 | user_regs_struct regs; |
| 87 | if (sys_ptrace(PTRACE_GETREGS, pid, NULL, ®s) == -1 || |
| 88 | #if defined(__i386) |
| 89 | !regs.esp |
| 90 | #elif defined(__x86_64) |
| 91 | !regs.rsp |
| 92 | #endif |
| 93 | ) { |
| 94 | sys_ptrace(PTRACE_DETACH, pid, NULL, NULL); |
| 95 | return false; |
| 96 | } |
| 97 | #endif |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // Resume a thread by detaching from it. |
| 102 | static bool ResumeThread(pid_t pid) { |
| 103 | return sys_ptrace(PTRACE_DETACH, pid, NULL, NULL) >= 0; |
| 104 | } |
| 105 | |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 106 | inline static bool IsMappedFileOpenUnsafe( |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 107 | const google_breakpad::MappingInfo& mapping) { |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 108 | // It is unsafe to attempt to open a mapped file that lives under /dev, |
| 109 | // because the semantics of the open may be driver-specific so we'd risk |
| 110 | // hanging the crash dumper. And a file in /dev/ almost certainly has no |
| 111 | // ELF file identifier anyways. |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 112 | return my_strncmp(mapping.name, |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 113 | kMappedFileUnsafePrefix, |
| 114 | sizeof(kMappedFileUnsafePrefix) - 1) == 0; |
| 115 | } |
| 116 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 117 | namespace google_breakpad { |
| 118 | |
| 119 | LinuxDumper::LinuxDumper(int pid) |
| 120 | : pid_(pid), |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 121 | threads_suspended_(false), |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 122 | threads_(&allocator_, 8), |
| 123 | mappings_(&allocator_) { |
| 124 | } |
| 125 | |
| 126 | bool LinuxDumper::Init() { |
| 127 | return EnumerateThreads(&threads_) && |
| 128 | EnumerateMappings(&mappings_); |
| 129 | } |
| 130 | |
| 131 | bool LinuxDumper::ThreadsSuspend() { |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 132 | if (threads_suspended_) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 133 | return true; |
thestig@chromium.org | f5c8f6f | 2010-08-14 01:41:39 +0000 | [diff] [blame] | 134 | for (size_t i = 0; i < threads_.size(); ++i) { |
| 135 | if (!SuspendThread(threads_[i])) { |
| 136 | // If the thread either disappeared before we could attach to it, or if |
| 137 | // it was part of the seccomp sandbox's trusted code, it is OK to |
| 138 | // silently drop it from the minidump. |
| 139 | memmove(&threads_[i], &threads_[i+1], |
| 140 | (threads_.size() - i - 1) * sizeof(threads_[i])); |
| 141 | threads_.resize(threads_.size() - 1); |
| 142 | --i; |
| 143 | } |
| 144 | } |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 145 | threads_suspended_ = true; |
thestig@chromium.org | f5c8f6f | 2010-08-14 01:41:39 +0000 | [diff] [blame] | 146 | return threads_.size() > 0; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool LinuxDumper::ThreadsResume() { |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 150 | if (!threads_suspended_) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 151 | return false; |
| 152 | bool good = true; |
| 153 | for (size_t i = 0; i < threads_.size(); ++i) |
| 154 | good &= ResumeThread(threads_[i]); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 155 | threads_suspended_ = false; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 156 | return good; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | LinuxDumper::BuildProcPath(char* path, pid_t pid, const char* node) const { |
| 161 | assert(path); |
| 162 | if (!path) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | path[0] = '\0'; |
| 167 | |
| 168 | const unsigned pid_len = my_int_len(pid); |
| 169 | |
| 170 | assert(node); |
| 171 | if (!node) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | size_t node_len = my_strlen(node); |
| 176 | assert(node_len < NAME_MAX); |
| 177 | if (node_len >= NAME_MAX) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | assert(node_len > 0); |
| 182 | if (node_len == 0) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | assert(pid > 0); |
| 187 | if (pid <= 0) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | const size_t total_length = 6 + pid_len + 1 + node_len; |
| 192 | |
| 193 | assert(total_length < NAME_MAX); |
| 194 | if (total_length >= NAME_MAX) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | memcpy(path, "/proc/", 6); |
| 199 | my_itos(path + 6, pid, pid_len); |
| 200 | memcpy(path + 6 + pid_len, "/", 1); |
| 201 | memcpy(path + 6 + pid_len + 1, node, node_len); |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 202 | path[total_length] = '\0'; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 205 | bool |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 206 | LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping, |
jessicag.feedback@gmail.com | 23c8299 | 2011-03-30 21:42:27 +0000 | [diff] [blame] | 207 | bool member, |
| 208 | unsigned int mapping_id, |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 209 | uint8_t identifier[sizeof(MDGUID)]) |
| 210 | { |
jessicag.feedback@gmail.com | 23c8299 | 2011-03-30 21:42:27 +0000 | [diff] [blame] | 211 | assert(!member || mapping_id < mappings_.size()); |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 212 | my_memset(identifier, 0, sizeof(MDGUID)); |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 213 | if (IsMappedFileOpenUnsafe(mapping)) |
nealsid | 6bc523c | 2010-05-10 20:35:54 +0000 | [diff] [blame] | 214 | return false; |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 215 | |
ted.mielczarek | 1807e38 | 2011-05-06 23:23:31 +0000 | [diff] [blame^] | 216 | // Special-case linux-gate because it's not a real file. |
| 217 | if (my_strcmp(mapping.name, kLinuxGateLibraryName) == 0) { |
| 218 | const uintptr_t kPageSize = getpagesize(); |
| 219 | void* linux_gate = NULL; |
| 220 | if (pid_ == sys_getpid()) { |
| 221 | linux_gate = reinterpret_cast<void*>(mapping.start_addr); |
| 222 | } else { |
| 223 | linux_gate = allocator_.Alloc(kPageSize); |
| 224 | CopyFromProcess(linux_gate, pid_, |
| 225 | reinterpret_cast<const void*>(mapping.start_addr), |
| 226 | kPageSize); |
| 227 | } |
| 228 | return FileID::ElfFileIdentifierFromMappedFile(linux_gate, identifier); |
| 229 | } |
| 230 | |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 231 | char filename[NAME_MAX]; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 232 | size_t filename_len = my_strlen(mapping.name); |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 233 | assert(filename_len < NAME_MAX); |
| 234 | if (filename_len >= NAME_MAX) |
| 235 | return false; |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 236 | memcpy(filename, mapping.name, filename_len); |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 237 | filename[filename_len] = '\0'; |
| 238 | bool filename_modified = HandleDeletedFileInMapping(filename); |
| 239 | |
| 240 | int fd = sys_open(filename, O_RDONLY, 0); |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 241 | if (fd < 0) |
| 242 | return false; |
| 243 | struct kernel_stat st; |
| 244 | if (sys_fstat(fd, &st) != 0) { |
| 245 | sys_close(fd); |
| 246 | return false; |
| 247 | } |
ted.mielczarek | 9f211b4 | 2009-12-23 20:44:32 +0000 | [diff] [blame] | 248 | #if defined(__x86_64) |
| 249 | #define sys_mmap2 sys_mmap |
| 250 | #endif |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 251 | void* base = sys_mmap2(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 252 | sys_close(fd); |
| 253 | if (base == MAP_FAILED) |
| 254 | return false; |
| 255 | |
| 256 | bool success = FileID::ElfFileIdentifierFromMappedFile(base, identifier); |
| 257 | sys_munmap(base, st.st_size); |
jessicag.feedback@gmail.com | 23c8299 | 2011-03-30 21:42:27 +0000 | [diff] [blame] | 258 | if (success && member && filename_modified) { |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 259 | mappings_[mapping_id]->name[filename_len - |
| 260 | sizeof(kDeletedSuffix) + 1] = '\0'; |
| 261 | } |
| 262 | |
ted.mielczarek | 0a5fc5d | 2009-12-23 17:09:27 +0000 | [diff] [blame] | 263 | return success; |
| 264 | } |
| 265 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 266 | void* |
| 267 | LinuxDumper::FindBeginningOfLinuxGateSharedLibrary(const pid_t pid) const { |
kmixter@chromium.org | b5dfa28 | 2010-12-08 22:26:20 +0000 | [diff] [blame] | 268 | char auxv_path[NAME_MAX]; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 269 | BuildProcPath(auxv_path, pid, "auxv"); |
| 270 | |
| 271 | // If BuildProcPath errors out due to invalid input, we'll handle it when |
| 272 | // we try to sys_open the file. |
| 273 | |
| 274 | // Find the AT_SYSINFO_EHDR entry for linux-gate.so |
| 275 | // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more |
| 276 | // information. |
| 277 | int fd = sys_open(auxv_path, O_RDONLY, 0); |
| 278 | if (fd < 0) { |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | elf_aux_entry one_aux_entry; |
| 283 | while (sys_read(fd, |
| 284 | &one_aux_entry, |
| 285 | sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) && |
| 286 | one_aux_entry.a_type != AT_NULL) { |
| 287 | if (one_aux_entry.a_type == AT_SYSINFO_EHDR) { |
| 288 | close(fd); |
| 289 | return reinterpret_cast<void*>(one_aux_entry.a_un.a_val); |
| 290 | } |
| 291 | } |
| 292 | close(fd); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | bool |
| 297 | LinuxDumper::EnumerateMappings(wasteful_vector<MappingInfo*>* result) const { |
kmixter@chromium.org | b5dfa28 | 2010-12-08 22:26:20 +0000 | [diff] [blame] | 298 | char maps_path[NAME_MAX]; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 299 | BuildProcPath(maps_path, pid_, "maps"); |
| 300 | |
| 301 | // linux_gate_loc is the beginning of the kernel's mapping of |
| 302 | // linux-gate.so in the process. It doesn't actually show up in the |
| 303 | // maps list as a filename, so we use the aux vector to find it's |
| 304 | // load location and special case it's entry when creating the list |
| 305 | // of mappings. |
| 306 | const void* linux_gate_loc; |
| 307 | linux_gate_loc = FindBeginningOfLinuxGateSharedLibrary(pid_); |
| 308 | |
| 309 | const int fd = sys_open(maps_path, O_RDONLY, 0); |
| 310 | if (fd < 0) |
| 311 | return false; |
| 312 | LineReader* const line_reader = new(allocator_) LineReader(fd); |
| 313 | |
| 314 | const char* line; |
| 315 | unsigned line_len; |
| 316 | while (line_reader->GetNextLine(&line, &line_len)) { |
| 317 | uintptr_t start_addr, end_addr, offset; |
| 318 | |
| 319 | const char* i1 = my_read_hex_ptr(&start_addr, line); |
| 320 | if (*i1 == '-') { |
| 321 | const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1); |
| 322 | if (*i2 == ' ') { |
| 323 | const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */); |
| 324 | if (*i3 == ' ') { |
ted.mielczarek | b0201df | 2011-03-14 17:04:09 +0000 | [diff] [blame] | 325 | const char* name = NULL; |
| 326 | // Only copy name if the name is a valid path name, or if |
| 327 | // it's the VDSO image. |
| 328 | if (((name = my_strchr(line, '/')) == NULL) && |
| 329 | linux_gate_loc && |
| 330 | reinterpret_cast<void*>(start_addr) == linux_gate_loc) { |
| 331 | name = kLinuxGateLibraryName; |
| 332 | offset = 0; |
| 333 | } |
| 334 | // Merge adjacent mappings with the same name into one module, |
| 335 | // assuming they're a single library mapped by the dynamic linker |
| 336 | if (name && result->size()) { |
| 337 | MappingInfo* module = (*result)[result->size() - 1]; |
| 338 | if ((start_addr == module->start_addr + module->size) && |
| 339 | (my_strlen(name) == my_strlen(module->name)) && |
| 340 | (my_strncmp(name, module->name, my_strlen(name)) == 0)) { |
| 341 | module->size = end_addr - module->start_addr; |
| 342 | line_reader->PopLine(line_len); |
| 343 | continue; |
| 344 | } |
| 345 | } |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 346 | MappingInfo* const module = new(allocator_) MappingInfo; |
| 347 | memset(module, 0, sizeof(MappingInfo)); |
| 348 | module->start_addr = start_addr; |
| 349 | module->size = end_addr - start_addr; |
| 350 | module->offset = offset; |
ted.mielczarek | b0201df | 2011-03-14 17:04:09 +0000 | [diff] [blame] | 351 | if (name != NULL) { |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 352 | const unsigned l = my_strlen(name); |
| 353 | if (l < sizeof(module->name)) |
| 354 | memcpy(module->name, name, l); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 355 | } |
| 356 | result->push_back(module); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | line_reader->PopLine(line_len); |
| 361 | } |
| 362 | |
| 363 | sys_close(fd); |
| 364 | |
| 365 | return result->size() > 0; |
| 366 | } |
| 367 | |
| 368 | // Parse /proc/$pid/task to list all the threads of the process identified by |
| 369 | // pid. |
| 370 | bool LinuxDumper::EnumerateThreads(wasteful_vector<pid_t>* result) const { |
kmixter@chromium.org | b5dfa28 | 2010-12-08 22:26:20 +0000 | [diff] [blame] | 371 | char task_path[NAME_MAX]; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 372 | BuildProcPath(task_path, pid_, "task"); |
| 373 | |
| 374 | const int fd = sys_open(task_path, O_RDONLY | O_DIRECTORY, 0); |
| 375 | if (fd < 0) |
| 376 | return false; |
| 377 | DirectoryReader* dir_reader = new(allocator_) DirectoryReader(fd); |
| 378 | |
| 379 | // The directory may contain duplicate entries which we filter by assuming |
| 380 | // that they are consecutive. |
| 381 | int last_tid = -1; |
| 382 | const char* dent_name; |
| 383 | while (dir_reader->GetNextEntry(&dent_name)) { |
| 384 | if (my_strcmp(dent_name, ".") && |
| 385 | my_strcmp(dent_name, "..")) { |
| 386 | int tid = 0; |
| 387 | if (my_strtoui(&tid, dent_name) && |
| 388 | last_tid != tid) { |
| 389 | last_tid = tid; |
| 390 | result->push_back(tid); |
| 391 | } |
| 392 | } |
| 393 | dir_reader->PopEntry(); |
| 394 | } |
| 395 | |
| 396 | sys_close(fd); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | // Read thread info from /proc/$pid/status. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 401 | // Fill out the |tgid|, |ppid| and |pid| members of |info|. If unavailable, |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 402 | // these members are set to -1. Returns true iff all three members are |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 403 | // available. |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 404 | bool LinuxDumper::ThreadInfoGet(pid_t tid, ThreadInfo* info) { |
| 405 | assert(info != NULL); |
kmixter@chromium.org | b5dfa28 | 2010-12-08 22:26:20 +0000 | [diff] [blame] | 406 | char status_path[NAME_MAX]; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 407 | BuildProcPath(status_path, tid, "status"); |
| 408 | |
| 409 | const int fd = open(status_path, O_RDONLY); |
| 410 | if (fd < 0) |
| 411 | return false; |
| 412 | |
| 413 | LineReader* const line_reader = new(allocator_) LineReader(fd); |
| 414 | const char* line; |
| 415 | unsigned line_len; |
| 416 | |
| 417 | info->ppid = info->tgid = -1; |
| 418 | |
| 419 | while (line_reader->GetNextLine(&line, &line_len)) { |
| 420 | if (my_strncmp("Tgid:\t", line, 6) == 0) { |
| 421 | my_strtoui(&info->tgid, line + 6); |
| 422 | } else if (my_strncmp("PPid:\t", line, 6) == 0) { |
| 423 | my_strtoui(&info->ppid, line + 6); |
| 424 | } |
| 425 | |
| 426 | line_reader->PopLine(line_len); |
| 427 | } |
| 428 | |
| 429 | if (info->ppid == -1 || info->tgid == -1) |
| 430 | return false; |
| 431 | |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 432 | if (sys_ptrace(PTRACE_GETREGS, tid, NULL, &info->regs) == -1) { |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 436 | #if !defined(__ANDROID__) |
| 437 | if (sys_ptrace(PTRACE_GETFPREGS, tid, NULL, &info->fpregs) == -1) { |
| 438 | return false; |
| 439 | } |
| 440 | #endif |
| 441 | |
nealsid | 096992f | 2009-12-01 21:35:52 +0000 | [diff] [blame] | 442 | #if defined(__i386) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 443 | if (sys_ptrace(PTRACE_GETFPXREGS, tid, NULL, &info->fpxregs) == -1) |
| 444 | return false; |
nealsid | 096992f | 2009-12-01 21:35:52 +0000 | [diff] [blame] | 445 | #endif |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 446 | |
nealsid | 096992f | 2009-12-01 21:35:52 +0000 | [diff] [blame] | 447 | #if defined(__i386) || defined(__x86_64) |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 448 | for (unsigned i = 0; i < ThreadInfo::kNumDebugRegisters; ++i) { |
| 449 | if (sys_ptrace( |
| 450 | PTRACE_PEEKUSER, tid, |
| 451 | reinterpret_cast<void*> (offsetof(struct user, |
| 452 | u_debugreg[0]) + i * |
| 453 | sizeof(debugreg_t)), |
| 454 | &info->dregs[i]) == -1) { |
| 455 | return false; |
| 456 | } |
| 457 | } |
| 458 | #endif |
| 459 | |
| 460 | const uint8_t* stack_pointer; |
| 461 | #if defined(__i386) |
| 462 | memcpy(&stack_pointer, &info->regs.esp, sizeof(info->regs.esp)); |
| 463 | #elif defined(__x86_64) |
| 464 | memcpy(&stack_pointer, &info->regs.rsp, sizeof(info->regs.rsp)); |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 465 | #elif defined(__ARM_EABI__) |
ted.mielczarek | cfc8628 | 2010-10-20 15:51:38 +0000 | [diff] [blame] | 466 | memcpy(&stack_pointer, &info->regs.ARM_sp, sizeof(info->regs.ARM_sp)); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 467 | #else |
| 468 | #error "This code hasn't been ported to your platform yet." |
| 469 | #endif |
| 470 | |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 471 | return GetStackInfo(&info->stack, &info->stack_len, |
| 472 | (uintptr_t) stack_pointer); |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // Get information about the stack, given the stack pointer. We don't try to |
| 476 | // walk the stack since we might not have all the information needed to do |
| 477 | // unwind. So we just grab, up to, 32k of stack. |
| 478 | bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len, |
| 479 | uintptr_t int_stack_pointer) { |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 480 | // Move the stack pointer to the bottom of the page that it's in. |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 481 | const uintptr_t page_size = getpagesize(); |
| 482 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 483 | uint8_t* const stack_pointer = |
| 484 | reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1)); |
| 485 | |
| 486 | // The number of bytes of stack which we try to capture. |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 487 | static const ptrdiff_t kStackToCapture = 32 * 1024; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 488 | |
| 489 | const MappingInfo* mapping = FindMapping(stack_pointer); |
| 490 | if (!mapping) |
| 491 | return false; |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 492 | const ptrdiff_t offset = stack_pointer - (uint8_t*) mapping->start_addr; |
| 493 | const ptrdiff_t distance_to_end = |
| 494 | static_cast<ptrdiff_t>(mapping->size) - offset; |
| 495 | *stack_len = distance_to_end > kStackToCapture ? |
| 496 | kStackToCapture : distance_to_end; |
| 497 | *stack = stack_pointer; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 498 | return true; |
| 499 | } |
| 500 | |
| 501 | // static |
| 502 | void LinuxDumper::CopyFromProcess(void* dest, pid_t child, const void* src, |
| 503 | size_t length) { |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 504 | unsigned long tmp = 55; |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 505 | size_t done = 0; |
| 506 | static const size_t word_size = sizeof(tmp); |
| 507 | uint8_t* const local = (uint8_t*) dest; |
| 508 | uint8_t* const remote = (uint8_t*) src; |
| 509 | |
| 510 | while (done < length) { |
| 511 | const size_t l = length - done > word_size ? word_size : length - done; |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 512 | if (sys_ptrace(PTRACE_PEEKDATA, child, remote + done, &tmp) == -1) { |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 513 | tmp = 0; |
nealsid | de545c0 | 2010-03-02 00:39:48 +0000 | [diff] [blame] | 514 | } |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 515 | memcpy(local + done, &tmp, l); |
| 516 | done += l; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Find the mapping which the given memory address falls in. |
| 521 | const MappingInfo* LinuxDumper::FindMapping(const void* address) const { |
| 522 | const uintptr_t addr = (uintptr_t) address; |
| 523 | |
| 524 | for (size_t i = 0; i < mappings_.size(); ++i) { |
| 525 | const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr); |
| 526 | if (addr >= start && addr - start < mappings_[i]->size) |
| 527 | return mappings_[i]; |
| 528 | } |
| 529 | |
| 530 | return NULL; |
| 531 | } |
| 532 | |
ted.mielczarek | ef7262d | 2010-12-13 22:10:23 +0000 | [diff] [blame] | 533 | bool LinuxDumper::HandleDeletedFileInMapping(char* path) const { |
thestig@chromium.org | 3665a7d | 2010-11-19 19:57:07 +0000 | [diff] [blame] | 534 | static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1; |
| 535 | |
| 536 | // Check for ' (deleted)' in |path|. |
| 537 | // |path| has to be at least as long as "/x (deleted)". |
| 538 | const size_t path_len = my_strlen(path); |
| 539 | if (path_len < kDeletedSuffixLen + 2) |
| 540 | return false; |
| 541 | if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix, |
| 542 | kDeletedSuffixLen) != 0) { |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | // Check |path| against the /proc/pid/exe 'symlink'. |
| 547 | char exe_link[NAME_MAX]; |
| 548 | char new_path[NAME_MAX]; |
| 549 | BuildProcPath(exe_link, pid_, "exe"); |
| 550 | ssize_t new_path_len = sys_readlink(exe_link, new_path, NAME_MAX); |
| 551 | if (new_path_len <= 0 || new_path_len == NAME_MAX) |
| 552 | return false; |
| 553 | new_path[new_path_len] = '\0'; |
| 554 | if (my_strcmp(path, new_path) != 0) |
| 555 | return false; |
| 556 | |
| 557 | // Check to see if someone actually named their executable 'foo (deleted)'. |
| 558 | struct kernel_stat exe_stat; |
| 559 | struct kernel_stat new_path_stat; |
| 560 | if (sys_stat(exe_link, &exe_stat) == 0 && |
| 561 | sys_stat(new_path, &new_path_stat) == 0 && |
| 562 | exe_stat.st_dev == new_path_stat.st_dev && |
| 563 | exe_stat.st_ino == new_path_stat.st_ino) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | memcpy(path, exe_link, NAME_MAX); |
| 568 | return true; |
| 569 | } |
| 570 | |
nealsid | b0baafc | 2009-08-17 23:12:53 +0000 | [diff] [blame] | 571 | } // namespace google_breakpad |