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